/
mom.py
351 lines (323 loc) · 12.6 KB
/
mom.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
342
343
344
345
346
347
348
349
350
351
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
MOM SQL Client
"""
from ConfigParser import SafeConfigParser, Error
from datetime import date
import logging
import os
from pymssql import connect, InterfaceError
class MOMClient(object):
""" MOM SQL Client """
CC_TYPES = {'MC': 'MasterCard',
'VI': 'Visa',
'DI': 'Discover',
'AM': 'American Express',
'DU': 'Manual'}
def __init__(self):
self.conn = None
def get_mom_connection(self):
""" Gets SQL Server connection to MOM """
config = SafeConfigParser()
config.read(os.path.join(os.path.dirname(__file__), 'config.ini'))
try:
momdb_host = config.get("momdb", "host")
momdb_user = config.get("momdb", "user")
momdb_password = config.get("momdb", "password")
momdb_db = config.get("momdb", "db")
except Error as error:
msg = "Config section [momdb] bad or missing: %s" % error.message
logging.error(msg)
raise Exception(msg)
try:
logging.info('Connecting to MOM...')
if self.conn is None:
self.conn = connect(host=momdb_host, user=momdb_user,
password=momdb_password,
database=momdb_db, as_dict=True)
return self.conn
except InterfaceError as error:
msg = "Error connecting to SQL Server: %s" % error.message
logging.error(msg)
raise Exception(msg)
except Error as error:
logging.error(error.message)
raise
def get_new_orders(self):
"""
Get new orders from MOM by calling sproc "Emailer_GetNewOrders"
"""
orders_dict = {}
try:
conn = self.get_mom_connection()
cur = conn.cursor()
cur.callproc("EmailVision_GetNewOrders")
for row in cur:
logging.debug("CUSTNUM=%d, FIRSTNAME=%s" % (
row['CUSTNUM'], row['FIRSTNAME']))
order = Order(row)
if order in orders_dict:
orders_dict[order].append(order)
else:
orders_dict[order] = [order]
# conn.close()
except Error as error:
logging.error(error.message)
raise
orders_list = self.normalize_orders_dict(orders_dict)
return orders_list
def get_upcoming_autoship_orders(self):
""" Gets upcoming Autoship orders for prenotice email """
orders_dict = {}
try:
conn = self.get_mom_connection()
cur = conn.cursor()
cur.callproc("GetAutoShipPreNotice")
for row in cur:
logging.debug("CUSTNUM=%d, FIRSTNAME=%s" % (
row['CUSTNUM'], row['FIRSTNAME']))
order = Order(row)
if order in orders_dict:
orders_dict[order].append(order)
else:
orders_dict[order] = [order]
# conn.close()
except Error as error:
logging.error(error.message)
raise
orders_list = self.normalize_orders_dict(orders_dict)
return orders_list
def get_backorders(self):
""" Gets backorders for notice email """
orders_dict = {}
try:
conn = self.get_mom_connection()
cur = conn.cursor()
cur.callproc("EmailVision_GetBackOrders")
for row in cur:
order = Order(row)
if order in orders_dict:
orders_dict[order].append(order)
else:
orders_dict[order] = [order]
except Error as error:
logging.error(error.message)
raise
orders_list = self.normalize_orders_dict(orders_dict)
return orders_list
def get_shipped_orders(self):
""" Gets shipped orders for notice email """
orders_dict = {}
try:
conn = self.get_mom_connection()
cur = conn.cursor()
cur.callproc("EmailVision_GetShipped")
for row in cur:
order = Order(row)
if order in orders_dict:
orders_dict[order].append(order)
else:
orders_dict[order] = [order]
except Error as error:
logging.error(error.message)
raise
orders_list = self.normalize_orders_dict(orders_dict)
return orders_list
def normalize_orders_dict(self, orders_dict):
""" normalize orders into Order->OrderItems """
orders_list = []
for orders in orders_dict:
# some orders have >1 "order" (line item)
for order_line in orders_dict[orders]:
order_item = OrderItem()
order_item.sku = order_line.sku
order_item.description = order_line.description
order_item.discount = order_line.discount
order_item.qty = order_line.qty
order_item.list_price = order_line.list_price
order_item.total = order_line.list_price * order_line.qty
orders.order_items.append(order_item)
logging.info("Order %s\tItem %s" %
(order_line.order_num, order_item.sku))
for order in orders_dict:
logging.info("Order %s has %d line items: %s" %
(order.order_num,
len(order.order_items),
order.order_items))
order.subtotal = \
float(order.total) - \
float(order.tax) - \
float(order.shipping_fee) - \
float(order.discount) - \
float(order.promocode_discount)
orders_list.append(order)
return orders_list
class Order(object):
""" Order """
def __init__(self, row=None):
if row is None:
self.order_num = ''
self.cust_num = ''
self.first_name = ''
self.last_name = ''
self.email = ''
self.expect_ship = date.max
self.billing_address1 = ''
self.billing_address2 = ''
self.billing_city = ''
self.billing_state = ''
self.billing_zip = ''
self.discount = 0.00
self.payment_type = ''
self.sku = ''
self.description = ''
self.list_price = ''
self.unit_price = ''
self.ext_price = ''
self.order_date = date.min
self.qty = 0
self.payment_last4 = ''
self.tax = 0.00
self.shipping_fee = 0.00
self.subtotal = 0.00
self.total = 0.00
self.promocode = ''
self.promocode_discount = 0.00
self.shipping_address1 = ''
self.shipping_address2 = ''
self.shipping_city = ''
self.shipping_state = ''
self.shipping_zip = ''
self.ship_type = ''
self.tracking_num = ''
self.tracking_url = ''
self.source_key = ''
self.order_items = []
else:
self.order_num = int(row['ORDERNO'])
self.cust_num = row.get('CUSTNUM', '')
self.first_name = row['FIRSTNAME']
self.last_name = row.get('LASTNAME', '')
self.email = row.get('EMAIL', 'mark.richman@nutrihealth.com')
self.expect_ship = row.get('SHIP_DATE', date.max)
if self.expect_ship == date.max:
self.expect_ship = row.get('NEXT_SHIP', date.max)
self.billing_address1 = row.get('ADDR', '')
self.billing_address2 = row.get('ADDR2', '')
self.billing_city = row.get('CITY', '')
self.billing_state = row.get('STATE', '')
self.billing_zip = row.get('ZIPCODE', '')
self.discount = row.get('DISCOUNT', 0.00)
self.payment_type = \
MOMClient.CC_TYPES.get(row.get('CARDTYPE', ''), 'Other')
self.payment_last4 = ''
self.sku = row.get('ITEM', '')
self.description = row.get('DESC1', '')
self.list_price = row.get('IT_UNLIST', 0)
self.unit_price = ''
self.ext_price = ''
self.order_date = row.get('ODR_DATE', date.min)
self.qty = row.get('QUANTO', 0)
self.tax = row.get('TAX', 0)
self.shipping_fee = row.get('SHIPPING', 0)
self.subtotal = row.get('ORD_TOTAL', 0)
self.total = row.get('ORD_TOTAL', 0)
self.promocode = 'Not Available'
self.promocode_discount = 0.00
self.shipping_address1 = row.get('ADDR', '')
self.shipping_address2 = row.get('ADDR2', '')
self.shipping_city = row.get('CITY', '')
self.shipping_state = row.get('STATE', '')
self.shipping_zip = row.get('ZIPCODE', '')
self.ship_type = ''
self.tracking_num = row.get('TRACKINGNO', 'Not Available')
if self.tracking_num == '':
self.tracking_num = 'Not Available'
self.tracking_url = ''
self.source_key = row.get('SourceKey', 'Not Available')
if self.source_key == '':
self.source_key = 'Not Available'
self.order_items = []
def __hash__(self):
return hash(self.order_num)
def __eq__(self, other):
return self.order_num == other.order_num
def html_table(self):
""" Returns order as a collection of HTML rows """
table = ""
for order_item in self.order_items:
table += (
" <tr>"
" <td>" + order_item.sku + "</td>"
" <td>" + order_item.description + "</td>"
" <td>" + str(order_item.qty) + "</td>"
" <td>" + "$%0.2f" % order_item.list_price + "</td>"
" <td>" + "$%0.2f" % order_item.total + "</td>"
" </tr>")
logging.debug(table)
return table
def html_table_autoship(self):
""" Returns order as a collection of HTML rows """
table = ""
for order_item in self.order_items:
table += (
" <tr>"
" <td>" + order_item.sku + "</td>"
" <td>" + order_item.description + "</td>"
" <td>" + str(order_item.qty) + "</td>"
" <td>" + "$%0.2f" % order_item.list_price + "</td>"
" <td>" + "$%0.2f" % order_item.total + "</td>"
" </tr>")
logging.debug(table)
return table
class OrderItem(object):
""" Order Item """
def __init__(self, row=None):
if row is None:
self.order_num = ''
self.expect_ship = date.today()
self.sku = ''
self.description = ''
self.list_price = ''
self.unit_price = ''
self.ext_price = ''
self.discount = 0.00
self.qty = 0
self.tax = 0.00
self.shipping = 0.00
self.ship_type = ''
self.tracking_num = ''
self.tracking_url = ''
self.source_key = ''
self.total = 0.00
else:
self.order_num = row['ORDERNO']
self.expect_ship = row['NEXT_SHIP']
self.sku = row['ITEM']
self.description = row['DESC1']
self.discount = float(row['DISCOUNT'])
self.list_price = float(row['IT_UNLIST'])
self.unit_price = float(row['IT_UNLIST'])
self.ext_price = float(row['IT_UNLIST'])
self.qty = int(row['QUANTO'])
self.tax = float(row['TAX'])
self.shipping = float(row['SHIPPING'])
self.ship_type = ''
self.tracking_num = row.get('TRACKINGNO', 'Not Available')
self.tracking_url = ''
self.source_key = row['SourceKey']
self.total = self.qty * self.list_price
def html_row(self):
""" Returns order item as an HTML row """
row = (" <tr>"
" <td>" + self.sku + "</td>"
" <td>" + self.description + "</td>"
" <td>" + str(self.qty) + "</td>"
" <td>" + "$%0.2f" % self.list_price + "</td>"
" <td>" + "$%0.2f" % self.total + "</td>"
" </tr>")
logging.debug(row)
return row
if __name__ == '__main__':
pass