-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestSnippet07-30-18.py
341 lines (304 loc) · 12.3 KB
/
TestSnippet07-30-18.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
import pandas
import Levenshtein as l
from sqlalchemy import create_engine
import urllib
import numpy as np
SQL_customers = """select *
from
dbo.OXB_Customer"""
SQL_billingaddresses="""select '' as billingid, '' as custid, c.BillAddressAddr1 as billto,
c.BillAddressAddr2 as billingaddress1,c.BillAddressAddr3 as billingaddress2,
c.BillAddressCity, c.BillAddressState as billingaddstate, c.BillAddressPostalCode as billingaddzip,
c.Phone as billingphone, '' as billingphoneextn,c.Email as billingemail, '' as billingnote,
'' as oldbillingid
from dbo.OXB_Customer c"""
SQL_invoice_orders="""select '' as orderid, rj.custid, rj.billingid, rj.jobsiteid,oxi.ShipDate as orderdate,
oxil.Memo as ordercomments, '' as orderadd, oxil.SubTotal as orderamount, oxi.TxnID
from AcquisitionTools.dbo.RAWjobsites rj
inner join
OXB_Invoice oxi
on oxi.CustomerRefListID=rj.oldjobsiteid
left join
OXB_InvoiceLine oxil
on oxil.TxnID=oxi.TxnID"""
SQL_invoice_order_details="""select distinct '' as orderdetailid, ro.orderid,olm. WindRiverID as orderserviceid , olm.[Wind River Item] as orderservicedescription,
il.InvoiceLineQuantity as orderservicequantity, il.InvoiceLineAmount,
il.InvoiceLineRate as orderserviceprice, olm.ListID,ro.orderponumber
from AcquisitionTools.dbo.RAWorders ro
inner join
OXB_InvoiceLine il
on ro.orderponumber=il.TxnID
inner join
OXB_LINEMAPPING olm
on olm.ListID=il.InvoiceLineItemRefListID"""
SQL_interactions = """select '' as interactionid, '' as custid, '' as jobsitedid, '' as orderId,
'' as interactiondate, 'CSC - Note' as interactiontype, ca.Notes as interationnote,
1 as status, '' as reminderdate, getdate() as createddate, getdate() as updateddate
from dbo.OXB_CustomerAddtionalNote ca"""
params_tools = urllib.parse.quote("DRIVER=ODBC Driver 13 for SQL Server;SERVER=WRE-SASAC;DATABASE=AcquisitionTools;trusted_connection=yes;")
engine_tools = create_engine('mssql+pyodbc:///?odbc_connect=%s' % params_tools)
with engine_tools.connect() as cn_tools:
RAWcustomers = pandas.read_sql_table('RAWcustomers', cn_tools)
RAWbillingaddress = pandas.read_sql_table('RAWbillingaddress', cn_tools)
RAWjobsites = pandas.read_sql_table('RAWjobsites', cn_tools)
RAWorders = pandas.read_sql_table('RAWorders', cn_tools)
RAWorderdetails = pandas.read_sql_table('RAWorderdetails', cn_tools)
RAWinteractions = pandas.read_sql_table('RAWinteractions', cn_tools)
# load the source tables
params_sandbox = urllib.parse.quote("DRIVER=ODBC Driver 13 for SQL Server;SERVER=WRE-SASAC;DATABASE=AcquisitionSandbox;trusted_connection=yes;")
engine_sandbox = create_engine('mssql+pyodbc:///?odbc_connect=%s' % params_sandbox)
with engine_sandbox.connect() as cn_sandbox:
#invoice_lines = pandas.read_sql(sql=SQL_invoice_line, con=cn_sandbox)
#existing_jobsites = pandas.read_sql(sql=SQL_existing_cust_jobsites, con=cn_sandbox)
#existing_interactions=pandas.read_sql(sql=SQL_existing_interactions, con=cn_sandbox)
#new_invoices = pandas.read_sql(sql=SQL_invoices_new_customers, con=cn_sandbox)
#customers = pandas.read_sql(sql=SQL_customers, con=cn_sandbox)
#billingaddresses=pandas.read_sql(sql=SQL_billingaddresses, con=cn_sandbox)
#RAWorders = pandas.DataFrame(index = np.arange(0, invoices.shape[0]) + np.arange(0, new_invoices.shape[0]), columns = list(RAWorders))
invoices = pandas.read_sql(sql=SQL_invoice_orders, con=cn_sandbox)
invoice_lines=pandas.read_sql(sql=SQL_invoice_order_details,con=cn_sandbox)
#RAWorderdetails = pandas.DataFrame(index = np.arange(0, invoice_lines.shape[0]), columns = list(RAWorderdetails))
#RAWinteractions = pandas.DataFrame(index = np.arange(0,existing_interactions.shape[0]), columns=list(RAWinteractions))
# Drummac orderid = 9000003
#orderid=2000862
# Drummac serviceid=15371944
#orderserviceid = 19224018
#custid = 0
#billingid=0
#jobsiteid = 0
#RAWcustomers = pandas.DataFrame(index = np.arange(0, customers.shape[0]), columns = list(RAWcustomers))
#Rawbillingaddress=pandas.DataFrame(index = np.arange(0, billingaddresses.shape[0]), columns = list(RAWbillingaddress))
RAWbillingaddress=pandas.DataFrame(index = np.arange(0, billingaddresses.shape[0]), columns = list(RAWbillingaddress))
RAWjobsites=pandas.DataFrame(index = np.arange(0, customers.shape[0]), columns = list(RAWjobsites))
RAWorders=pandas.DataFrame(index = np.arange(0, invoices.shape[0]), columns = list(RAWorders))
RAWorderdetails=pandas.DataFrame(index=np.arange(0, invoice_lines.shape[0]), columns=list(RAWorderdetails))
commercialWords=[
'& sons',
'& Sons',
'& sons',
'& Sons',
'& Hardware,'
'& hardware'
'& Recreation',
'& recreation',
'aviation',
'Aviation',
'apartment',
'Apartment',
'associate',
'Associate',
'Assisted living,'
'assisted living',
'Association',
'association',
'Bagel',
'Bar',
'bar',
'bagel',
'Brewery'
'brewery'
'Build',
'build',
'build',
'Com' ,
'Com',
'com',
'Company',
'company',
'Coffee',
'coffee',
'Coffee',
'church',
'Church',
'contractor',
'Contractor',
'construction',
'Construction',
'condominium',
'Condominium',
'Co, Inc',
'co, inc',
'co,inc',
'Concrete',
'concrete',
'Drive',
'drive',
'District',
'district',
'Development',
'development',
'Equipment',
'equipment',
'Excavation',
'excavation',
'England',
'england',
'Estates',
'estates',
'Enterprise',
'enterprise',
'Family',
'family',
'Facility',
'facility',
'Hotel',
'hotel',
'Heat',
'heat',
'Inn',
'inn',
'Investor',
'Investment'
'investor',
'Inc',
'Inc',
'inc',
'llc',
'LLC',
'management',
'Management',
'mgmt',
'Motel',
'motel',
'Operating',
'operating',
'Office',
'office',
'office',
'Park',
'park',
'Parks',
'parks',
'Paint',
'paint',
'Property',
'property',
'plumbing' ,
'Plumbing' ,
'Pizza',
'pizza',
'Recreation',
'recreation',
'Restaurant',
'restaurant',
'realty',
'realty',
'rectory',
'Rectory',
'Rectory',
'school',
'School',
'service',
'Service',
'septic',
'Septic',
'Store',
'store',
'Trade',
'trade',
'Tech',
'tech',
'Town',
'town',
'Water',
'water',
'Inc.',
'inc.',
'express',
'Express']
def convertCommercial(string):
return any(substring in string for substring in commercialWords)
def get_phone(phone):
if phone is not None and phone[:1] != '1':
return ri.Phone.replace('-','')[:10]
elif phone is not None:
return ri.Phone.replace('-','')[1:9]
elif phone is None:
return None
else:
return phone
def GetTermId(argument):
switcher = {
"Net 15":6,
"Net 30":2,
"Net 30/":2,
"Net 30(Per Jules)":2,
"Net 60":7,
"Net 10":5
}
#print(switcher.get(argument,1))
return switcher.get(argument,1)
#.to_clipboard(sep = '\t', index = False)
"""for i, ri in customers.iterrows():
print('i' , i, custid)
custid = custid + 1
RAWcustomers.custid[i] = custid
#RAWcustomers.custacquisitionid[i] = 106
RAWcustomers.custacquisitionid=113
RAWcustomers.custaddcity[i] = ri.BillAddressCity
RAWcustomers.custaddress1[i] = ri.BillAddressAddr2
RAWcustomers.custaddress2[i] = ri.BillAddressAddr3
RAWcustomers.custaddstate[i] = ri.BillAddressState
RAWcustomers.custaddzip[i] = ri.BillAddressPostalCode[:5] if ri.BillAddressPostalCode is not None else str(ri.BillAddressPostalCode)
try:
RAWcustomers.custlastname[i] = ri.BillAddressAddr1 if (convertCommercial(ri.BillAddressAddr1)) is True else ri.LastName
except TypeError:
print('Dont know what is going on')
#RAWcustomers.custlastname[i]=ri.LastName
try:
RAWcustomers.custfirstname[i] = None if (convertCommercial(ri.BillAddressAddr1)) is True else ri.FirstName
except TypeError:
print('Dont know what is going on')
try:
RAWcustomers.custcategoryid[i] = 2 if (convertCommercial(ri.BillAddressAddr1)) is True else 1
except TypeError:
print('Dont know what is going on')
try:
RAWcustomers.custprimaryphone[i] = get_phone(ri.Phone)
except AttributeError:
print('Dont know what is going on')
#RAWcustomers.custprimaryphone[i] = ri.Phone
RAWcustomers.custprimaryphoneext[i] = ri.Phone[-3:] if ri.Phone is not None and 'ext' in ri.Phone else None
try:
RAWcustomers.custsecondaryphone[i] = get_phone(ri.AltPhone)
except AttributeError:
print('Dont know what is going on')
RAWcustomers.custemail[i]=ri.Email
try:
RAWcustomers.custfax[i]=get_phone(ri.Fax)
except AttributeError:
print('Dont know what is going on')
RAWcustomers.custshowprice[i] = True
RAWcustomers.custisCOD[i]=1 if ri.TermsRefFullName=="Due on receipt" else 0
RAWcustomers.custinvtermid[i] = GetTermId(ri.TermsRefFullName)
RAWcustomers.custcreditlimit= None
RAWcustomers.oldcustid[i] = ri.ListID"""
print(RAWcustomers.shape)
print(RAWbillingaddress.shape)
print(RAWorders.shape)
print(RAWorderdetails.shape)
#orderid=1
#for i, ri in invoices.iterrows():
#orderid = orderid + 1
#print('i' , i, ri['custid'])
#RAWorders['orderid'][i] = "OXB" + str(orderid).zfill(7)
#RAWorders['orderdate'][i] = ri['ShipDate']
#RAWorders['orderdate'][i]=ri['orderdate']
#RAWorders['orderamount'][i] = ri['orderamount']
#RAWorders['orderponumber'][i] = ri['TxnID']
#RAWorders['jobsiteid'][i] = ri['jobsiteid']
#RAWorders['custid'][i] = ri['custid']
#RAWorders['ordercomments']=ri['ordercomments']
#RAWorders['billingid']=ri['billingid']
orderdetailid=0
for i, ri in invoice_lines.iterrows():
print ('i', i, orderdetailid)
orderdetailid = orderdetailid + 1
RAWorderdetails.orderdetailid[i] = orderdetailid
#RAWorderdetails.orderid[k] = "A1G" + str(orderid).zfill(7)
RAWorderdetails.orderid=ri['orderid']
RAWorderdetails.orderserviceid[i] = ri['orderserviceid']
RAWorderdetails.orderservicedescription[i] = ri['orderservicedescription']
RAWorderdetails.orderservicequantity[i]= ri['orderservicequantity']
RAWorderdetails.orderserviceprice[i] = ri['orderserviceprice']
RAWorderdetails.orderservicerevenue[i] = ri['InvoiceLineAmount']