Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

212 строки
9.9KB

  1. <%
  2. '=======================================================================================================================
  3. ' IMPORT SERVICE
  4. ' Inserts parsed spreadsheet rows into businesses inside one transaction.
  5. ' Duplicates (same phone digits OR same email) are skipped, both against existing rows and within the file itself.
  6. '=======================================================================================================================
  7. Function ImportService_TextParam(cmd, size)
  8. cmd.Parameters.Append cmd.CreateParameter(, 202, 1, size, Null) ' adVarWChar, adParamInput
  9. End Function
  10. Function ImportService_NullIfEmpty(s)
  11. If Len(s) = 0 Then ImportService_NullIfEmpty = Null Else ImportService_NullIfEmpty = s
  12. End Function
  13. ' tbl : result of ImportFile_Read(path, 0)
  14. ' colOf : Dictionary field key -> column index (only mapped fields)
  15. ' defaultIndustry : industry name used when a row has none ("" = leave blank)
  16. ' markCustomers : True to flag every imported row as a previous customer
  17. ' Returns a Dictionary: read, added, dup_db, dup_file, no_name, new_industries, failed, error, batch_id, dups (Dictionary of samples)
  18. Function ImportService_Run(tbl, colOf, defaultIndustry, markCustomers, fileName)
  19. Dim res : Set res = CreateObject("Scripting.Dictionary")
  20. res("read") = 0 : res("added") = 0 : res("dup_db") = 0 : res("dup_file") = 0
  21. res("no_name") = 0 : res("new_industries") = 0 : res("failed") = False : res("error") = ""
  22. res("batch_id") = 0
  23. Dim dups : Set dups = CreateObject("Scripting.Dictionary")
  24. Set res("dups") = dups
  25. Dim conn : Set conn = DAL().Connection
  26. Dim rs, phones, emails, industries
  27. Set phones = CreateObject("Scripting.Dictionary") : phones.CompareMode = 1
  28. Set emails = CreateObject("Scripting.Dictionary") : emails.CompareMode = 1
  29. Set industries = CreateObject("Scripting.Dictionary") : industries.CompareMode = 1
  30. Set rs = conn.Execute("SELECT phone_digits, email FROM businesses")
  31. Do While Not rs.EOF
  32. If Len(rs(0) & "") > 0 Then phones(rs(0) & "") = 1
  33. If Len(rs(1) & "") > 0 Then emails(LCase(rs(1) & "")) = 1
  34. rs.MoveNext
  35. Loop
  36. rs.Close
  37. Set rs = conn.Execute("SELECT id, name FROM industries")
  38. Do While Not rs.EOF
  39. industries(rs(1) & "") = CLng(rs(0))
  40. rs.MoveNext
  41. Loop
  42. rs.Close
  43. ' Prepared insert for businesses
  44. Dim cmd : Set cmd = Server.CreateObject("ADODB.Command")
  45. Set cmd.ActiveConnection = conn
  46. cmd.CommandType = 1
  47. cmd.CommandText = "INSERT INTO businesses (name, contact_name, industry_id, phone, phone_digits, email, website, " & _
  48. "address1, city, state, zip, source, status, is_customer, notes, import_batch_id, created_at) " & _
  49. "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,Now())"
  50. ImportService_TextParam cmd, 255 ' name
  51. ImportService_TextParam cmd, 150 ' contact_name
  52. cmd.Parameters.Append cmd.CreateParameter(, 3, 1, , Null) ' industry_id
  53. ImportService_TextParam cmd, 40 ' phone
  54. ImportService_TextParam cmd, 20 ' phone_digits
  55. ImportService_TextParam cmd, 255 ' email
  56. ImportService_TextParam cmd, 255 ' website
  57. ImportService_TextParam cmd, 255 ' address1
  58. ImportService_TextParam cmd, 100 ' city
  59. ImportService_TextParam cmd, 50 ' state
  60. ImportService_TextParam cmd, 20 ' zip
  61. ImportService_TextParam cmd, 255 ' source
  62. ImportService_TextParam cmd, 20 ' status
  63. cmd.Parameters.Append cmd.CreateParameter(, 3, 1, , 0) ' is_customer
  64. cmd.Parameters.Append cmd.CreateParameter(, 203, 1, 4000, Null) ' notes (adLongVarWChar)
  65. cmd.Parameters.Append cmd.CreateParameter(, 3, 1, , 0) ' import_batch_id
  66. Dim indCmd : Set indCmd = Server.CreateObject("ADODB.Command")
  67. Set indCmd.ActiveConnection = conn
  68. indCmd.CommandType = 1
  69. indCmd.CommandText = "INSERT INTO industries (name) VALUES (?)"
  70. ImportService_TextParam indCmd, 100
  71. Dim rows : Set rows = tbl("rows")
  72. Dim i, rec, nm, contact, ind, phone, digits, email, website, addr, city, st, zip, notes
  73. Dim indId, reason, srcLabel, cust, k
  74. srcLabel = Left(fileName, 255)
  75. If markCustomers Then cust = 1 Else cust = 0
  76. On Error Resume Next
  77. conn.BeginTrans
  78. ' Batch row (counts are updated at the end)
  79. conn.Execute "INSERT INTO import_batches (file_name, imported_at, rows_read, rows_added, rows_skipped) VALUES ('" & _
  80. Replace(Left(fileName, 255), "'", "''") & "', Now(), 0, 0, 0)", , 128
  81. If Err.Number = 0 Then
  82. Set rs = conn.Execute("SELECT @@IDENTITY")
  83. res("batch_id") = CLng(rs(0))
  84. rs.Close
  85. End If
  86. If Err.Number = 0 Then
  87. For i = 0 To rows.Count - 1
  88. If Err.Number <> 0 Then Exit For
  89. rec = rows(i)
  90. res("read") = res("read") + 1
  91. nm = Left(ImportFile_Cell(rec, ImportService_Col(colOf, "name")), 255)
  92. If Len(nm) = 0 Then
  93. res("no_name") = res("no_name") + 1
  94. Else
  95. contact = Left(ImportFile_Cell(rec, ImportService_Col(colOf, "contact_name")), 150)
  96. ind = Left(ImportFile_Cell(rec, ImportService_Col(colOf, "industry")), 100)
  97. phone = Left(ImportFile_Cell(rec, ImportService_Col(colOf, "phone")), 40)
  98. email = Left(ImportFile_Cell(rec, ImportService_Col(colOf, "email")), 255)
  99. website = Left(ImportFile_Cell(rec, ImportService_Col(colOf, "website")), 255)
  100. addr = Left(ImportFile_Cell(rec, ImportService_Col(colOf, "address1")), 255)
  101. city = Left(ImportFile_Cell(rec, ImportService_Col(colOf, "city")), 100)
  102. st = Left(ImportFile_Cell(rec, ImportService_Col(colOf, "state")), 50)
  103. zip = Left(ImportFile_NormalizeZip(ImportFile_Cell(rec, ImportService_Col(colOf, "zip"))), 20)
  104. notes = Left(ImportFile_Cell(rec, ImportService_Col(colOf, "notes")), 4000)
  105. If InStr(email, "@") = 0 Then email = ""
  106. email = LCase(email)
  107. digits = ImportFile_PhoneDigits(phone)
  108. reason = ""
  109. If Len(digits) > 0 Then
  110. If phones.Exists(digits) Then reason = "phone"
  111. End If
  112. If Len(reason) = 0 And Len(email) > 0 Then
  113. If emails.Exists(email) Then reason = "email"
  114. End If
  115. If Len(reason) > 0 Then
  116. If (reason = "phone" And phones(digits) = 2) Or (reason = "email" And emails(email) = 2) Then
  117. res("dup_file") = res("dup_file") + 1
  118. Else
  119. res("dup_db") = res("dup_db") + 1
  120. End If
  121. If dups.Count < 25 Then dups.Add dups.Count, Array(i + 2, nm, reason)
  122. Else
  123. ' Resolve industry (create when new)
  124. indId = Null
  125. If Len(ind) = 0 Then ind = defaultIndustry
  126. If Len(ind) > 0 Then
  127. If industries.Exists(ind) Then
  128. indId = industries(ind)
  129. Else
  130. indCmd.Parameters(0).Value = ind
  131. indCmd.Execute , , 128
  132. If Err.Number <> 0 Then Exit For
  133. Set rs = conn.Execute("SELECT @@IDENTITY")
  134. indId = CLng(rs(0))
  135. rs.Close
  136. industries(ind) = indId
  137. res("new_industries") = res("new_industries") + 1
  138. End If
  139. End If
  140. cmd.Parameters(0).Value = nm
  141. cmd.Parameters(1).Value = ImportService_NullIfEmpty(contact)
  142. cmd.Parameters(2).Value = indId
  143. cmd.Parameters(3).Value = ImportService_NullIfEmpty(phone)
  144. cmd.Parameters(4).Value = ImportService_NullIfEmpty(digits)
  145. cmd.Parameters(5).Value = ImportService_NullIfEmpty(email)
  146. cmd.Parameters(6).Value = ImportService_NullIfEmpty(website)
  147. cmd.Parameters(7).Value = ImportService_NullIfEmpty(addr)
  148. cmd.Parameters(8).Value = ImportService_NullIfEmpty(city)
  149. cmd.Parameters(9).Value = ImportService_NullIfEmpty(st)
  150. cmd.Parameters(10).Value = ImportService_NullIfEmpty(zip)
  151. cmd.Parameters(11).Value = ImportService_NullIfEmpty(srcLabel)
  152. cmd.Parameters(12).Value = "new"
  153. cmd.Parameters(13).Value = cust
  154. cmd.Parameters(14).Value = ImportService_NullIfEmpty(notes)
  155. cmd.Parameters(15).Value = res("batch_id")
  156. cmd.Execute , , 128
  157. If Err.Number <> 0 Then Exit For
  158. res("added") = res("added") + 1
  159. If Len(digits) > 0 Then phones(digits) = 2
  160. If Len(email) > 0 Then emails(email) = 2
  161. End If
  162. End If
  163. Next
  164. End If
  165. If Err.Number <> 0 Then
  166. res("failed") = True
  167. res("error") = "Row " & (res("read") + 1) & ": " & Err.Description
  168. Err.Clear
  169. conn.RollbackTrans
  170. Else
  171. conn.Execute "UPDATE import_batches SET rows_read = " & CLng(res("read")) & ", rows_added = " & CLng(res("added")) & _
  172. ", rows_skipped = " & CLng(res("read") - res("added")) & " WHERE id = " & CLng(res("batch_id")), , 128
  173. If Err.Number <> 0 Then
  174. res("failed") = True
  175. res("error") = Err.Description
  176. Err.Clear
  177. conn.RollbackTrans
  178. Else
  179. conn.CommitTrans
  180. End If
  181. End If
  182. On Error GoTo 0
  183. Set ImportService_Run = res
  184. End Function
  185. Function ImportService_Col(colOf, key)
  186. If colOf.Exists(key) Then ImportService_Col = colOf(key) Else ImportService_Col = -1
  187. End Function
  188. %>

Powered by TurnKey Linux.