| 104 |
ashish |
1 |
'''
|
|
|
2 |
Created on 29-Mar-2010
|
|
|
3 |
|
|
|
4 |
@author: ashish
|
|
|
5 |
'''
|
|
|
6 |
from shop2020.model.v1.order.impl.DataService import Transaction, LineItem,\
|
| 304 |
ashish |
7 |
AdditionalInfo, Payment, Shipment, TransactionTrail, Billing, ItemInfo,\
|
|
|
8 |
Alerts, ExtraItemInfo
|
| 104 |
ashish |
9 |
from shop2020.thriftpy.model.v1.order.ttypes import Transaction as T_Transaction,\
|
|
|
10 |
TransactionServiceException
|
|
|
11 |
import datetime
|
|
|
12 |
from shop2020.utils.OrderStatus import status_transaction_init,\
|
| 194 |
ashish |
13 |
status_shipment_shipped, status_shipment_delivered, get_c_status
|
| 122 |
ashish |
14 |
from elixir import *
|
| 104 |
ashish |
15 |
from shop2020.utils.Utils import to_py_date
|
|
|
16 |
from sqlalchemy.sql.expression import and_
|
|
|
17 |
|
|
|
18 |
|
|
|
19 |
def get_new_transaction():
|
|
|
20 |
transaction = Transaction()
|
|
|
21 |
transaction.createdOn = datetime.datetime.now()
|
| 353 |
ashish |
22 |
transaction.status = status_transaction_init
|
| 104 |
ashish |
23 |
transaction.status_message = "New transaction"
|
|
|
24 |
transaction.trails = []
|
|
|
25 |
session.commit()
|
|
|
26 |
return transaction
|
|
|
27 |
|
|
|
28 |
def create_transaction(t_transaction):
|
|
|
29 |
orderinfo = t_transaction.orderInfo
|
|
|
30 |
if not orderinfo:
|
|
|
31 |
raise TransactionServiceException(101, "Orderinfo missing from the transaction")
|
|
|
32 |
transaction = get_new_transaction()
|
|
|
33 |
for line_item in orderinfo.lineitems:
|
|
|
34 |
litem = LineItem()
|
| 132 |
ashish |
35 |
if line_item.item.id:
|
|
|
36 |
litem.item_id = line_item.item.id
|
|
|
37 |
item_info = ItemInfo()
|
|
|
38 |
item_info.price = line_item.item.price
|
|
|
39 |
item_info.weight = line_item.item.weight
|
|
|
40 |
litem.item_info = item_info
|
| 104 |
ashish |
41 |
litem.added_on = to_py_date(line_item.addedOn)
|
|
|
42 |
transaction.lineitems.append(litem)
|
|
|
43 |
if line_item.additionalInfo:
|
|
|
44 |
for k,v in line_item.additionalInfo.iteritems():
|
|
|
45 |
additional_info = AdditionalInfo()
|
|
|
46 |
additional_info.key = k
|
|
|
47 |
additional_info.value = v
|
|
|
48 |
litem.additional_info.append(additional_info)
|
| 358 |
ashish |
49 |
if t_transaction.warehouse_id:
|
|
|
50 |
transaction.warehouse_id = t_transaction.warehouse_id
|
| 132 |
ashish |
51 |
if t_transaction.shoppingCartid:
|
|
|
52 |
transaction.shopping_cart_id = t_transaction.shoppingCartid
|
|
|
53 |
transaction.customer_id = t_transaction.customer_id
|
|
|
54 |
transaction.expected_delivery_time = to_py_date(t_transaction.expectedDeliveryTime)
|
| 194 |
ashish |
55 |
#no need to set status. It is already set.
|
|
|
56 |
#transaction.status = t_transaction.transactionStatus
|
| 132 |
ashish |
57 |
transaction.status_message = t_transaction.statusDescription
|
|
|
58 |
transaction.createdOn = to_py_date(t_transaction.createdOn)
|
| 104 |
ashish |
59 |
|
| 132 |
ashish |
60 |
#add payment info
|
|
|
61 |
#transaction.payments=[]
|
|
|
62 |
if t_transaction.paymentInfo:
|
|
|
63 |
if not t_transaction.paymentInfo.payments:
|
|
|
64 |
raise TransactionServiceException(108, "no payments found")
|
|
|
65 |
|
|
|
66 |
for k,v in t_transaction.paymentInfo.payments.iteritems():
|
|
|
67 |
payment = Payment()
|
|
|
68 |
payment.merchant_tx_id = v.merchant_tx_id
|
|
|
69 |
payment.bank_tx_id = v.bank_tx_id
|
|
|
70 |
payment.mode = v.mode
|
|
|
71 |
payment.amount = v.amount
|
|
|
72 |
payment.payment_id = v.id
|
|
|
73 |
payment.status = v.status
|
|
|
74 |
payment.status_message = v.description
|
|
|
75 |
payment.start_time = to_py_date(v.submissionTimestamp)
|
|
|
76 |
payment.finish_time = to_py_date(v.completionTimestamp)
|
|
|
77 |
transaction.payments.append(payment)
|
| 194 |
ashish |
78 |
if not t_transaction.shipmentInfo:
|
|
|
79 |
raise TransactionServiceException(109, "no shipping info attached with this")
|
|
|
80 |
transaction.shipments = []
|
|
|
81 |
for v in t_transaction.shipmentInfo.shipments:
|
|
|
82 |
shipment = Shipment()
|
|
|
83 |
shipment.weight = v.weight
|
|
|
84 |
shipment.insurance_value = v.insurance
|
|
|
85 |
shipment.status = get_c_status(v.status);
|
| 304 |
ashish |
86 |
shipment.address = v.address
|
| 194 |
ashish |
87 |
transaction.shipments.append(shipment)
|
| 104 |
ashish |
88 |
session.commit()
|
|
|
89 |
return transaction.id
|
|
|
90 |
|
|
|
91 |
|
|
|
92 |
def get_transaction(transaction_id):
|
|
|
93 |
transaction = Transaction.get_by(id=transaction_id)
|
|
|
94 |
if not transaction:
|
|
|
95 |
raise TransactionServiceException(108, "no such transaction")
|
|
|
96 |
|
|
|
97 |
return transaction
|
|
|
98 |
|
| 358 |
ashish |
99 |
def get_all_transactions(status, from_date, to_date, warehouse_id):
|
|
|
100 |
|
|
|
101 |
query = Transaction.query
|
|
|
102 |
if warehouse_id:
|
|
|
103 |
query = query.filter_by(warehouse_id=warehouse_id)
|
|
|
104 |
|
| 104 |
ashish |
105 |
if not status:
|
|
|
106 |
if not from_date and not to_date:
|
|
|
107 |
#get all between from_date and to_date
|
| 358 |
ashish |
108 |
return query.all()
|
| 104 |
ashish |
109 |
|
|
|
110 |
if not from_date:
|
| 358 |
ashish |
111 |
return query.filter(Transaction.createdOn < to_date).all()
|
| 104 |
ashish |
112 |
|
|
|
113 |
if not to_date:
|
| 358 |
ashish |
114 |
return query.filter(Transaction.createdOn > to_date).all()
|
|
|
115 |
return query.filter(and_(Transaction.createdOn < to_date , Transaction.createdOn > from_date)).all()
|
| 104 |
ashish |
116 |
|
|
|
117 |
|
|
|
118 |
if not from_date and not to_date:
|
| 358 |
ashish |
119 |
return query.filter(Transaction.status == status).all()
|
| 104 |
ashish |
120 |
if not from_date:
|
| 358 |
ashish |
121 |
return query.filter(and_(Transaction.status == status, Transaction.createdOn <to_date)).all()
|
| 104 |
ashish |
122 |
if not from_date:
|
| 358 |
ashish |
123 |
return query.filter(and_(Transaction.status == status, Transaction.createdOn >from_date)).all()
|
| 104 |
ashish |
124 |
if not from_date:
|
| 358 |
ashish |
125 |
return query.filter(and_(Transaction.status == status, and_(Transaction.createdOn <to_date, Transaction.createdOn > from_date))).all()
|
| 104 |
ashish |
126 |
|
|
|
127 |
|
|
|
128 |
def get_transactions_for_shipment_status(status, from_date, to_date):
|
|
|
129 |
|
|
|
130 |
if not status:
|
|
|
131 |
raise TransactionServiceException(101, "unknown status")
|
|
|
132 |
|
|
|
133 |
query = Shipment.query.filter(Shipment.status == status)
|
|
|
134 |
|
|
|
135 |
if from_date:
|
|
|
136 |
query = query.filter(Shipment.shipment_time >from_date)
|
|
|
137 |
if to_date:
|
|
|
138 |
query = query.filter(Shipment.shipment_time <to_date)
|
|
|
139 |
|
|
|
140 |
shipments = query.all()
|
|
|
141 |
|
|
|
142 |
transactions = {}
|
|
|
143 |
for shipment in shipments:
|
|
|
144 |
if not transactions[shipment.transaction.id]:
|
|
|
145 |
transactions.append(shipment.transaction)
|
|
|
146 |
return transactions.values()
|
|
|
147 |
|
|
|
148 |
|
|
|
149 |
def get_transactions_for_customer(customer_id, from_date, to_date, status):
|
|
|
150 |
if not customer_id:
|
|
|
151 |
raise TransactionServiceException(101, "bas customer id")
|
|
|
152 |
query = Transaction.query.filter(Transaction.customer_id == customer_id)
|
|
|
153 |
if status:
|
|
|
154 |
query = query.filter(Transaction.status == status)
|
|
|
155 |
if from_date:
|
|
|
156 |
query = query.filter(Transaction.createdOn >from_date)
|
|
|
157 |
if to_date:
|
|
|
158 |
query = query.filter(Transaction.createdOn < to_date)
|
|
|
159 |
return query.all()
|
|
|
160 |
|
|
|
161 |
def get_transactions_for_customer_shipment(customer_id, from_date, to_date, status):
|
|
|
162 |
|
|
|
163 |
if not customer_id:
|
|
|
164 |
raise TransactionServiceException(101, "unknown customer id")
|
|
|
165 |
|
|
|
166 |
query = Shipment.query.filter(Shipment.transaction.customerId == customer_id)
|
|
|
167 |
|
|
|
168 |
if status:
|
|
|
169 |
query = query.filter(Shipment.status == status)
|
|
|
170 |
|
|
|
171 |
if from_date:
|
|
|
172 |
query = query.filter(Shipment.shipment_time >from_date)
|
|
|
173 |
|
|
|
174 |
if to_date:
|
|
|
175 |
query = query.filter(Shipment.shipment_time <to_date)
|
|
|
176 |
|
|
|
177 |
shipments = query.all()
|
|
|
178 |
|
|
|
179 |
transactions = {}
|
|
|
180 |
for shipment in shipments:
|
|
|
181 |
if not transactions[shipment.transaction.id]:
|
|
|
182 |
transactions.append(shipment.transaction)
|
|
|
183 |
return transactions.values()
|
|
|
184 |
|
|
|
185 |
def get_transaction_status(transaction_id):
|
|
|
186 |
|
|
|
187 |
if not transaction_id:
|
|
|
188 |
raise TransactionServiceException(101, "bad transaction id")
|
|
|
189 |
|
|
|
190 |
transaction = get_transaction(transaction_id)
|
|
|
191 |
|
|
|
192 |
return transaction.status
|
|
|
193 |
|
|
|
194 |
def change_transaction_status(transaction_id, new_status, description):
|
|
|
195 |
transaction = get_transaction(transaction_id)
|
|
|
196 |
trail = TransactionTrail()
|
|
|
197 |
trail.old_value = str(transaction.status)
|
|
|
198 |
trail.new_value = str(new_status)
|
|
|
199 |
transaction.status = new_status
|
|
|
200 |
trail.transaction = transaction
|
|
|
201 |
trail.timestamp = datetime.datetime.now()
|
|
|
202 |
trail.description = description
|
|
|
203 |
transaction.status_message = description
|
|
|
204 |
session.commit()
|
|
|
205 |
return True
|
|
|
206 |
|
|
|
207 |
def set_shipping_tracker_info(transaction_id, shipping_id, tracking_id, airwayBillNo, provider):
|
|
|
208 |
shipment = Shipment.get_by(id=shipping_id)
|
|
|
209 |
if not shipment:
|
|
|
210 |
raise TransactionServiceException(101, "not such shipment found")
|
|
|
211 |
|
|
|
212 |
transaction = shipment.transaction
|
|
|
213 |
if transaction.id != transaction_id:
|
|
|
214 |
raise TransactionServiceException(108, "Looks like this is invalid transaction")
|
|
|
215 |
shipment.tracking_id = tracking_id
|
|
|
216 |
shipment.airway_bill_no = airwayBillNo
|
|
|
217 |
shipment.provider = provider
|
|
|
218 |
trail = TransactionTrail()
|
|
|
219 |
trail.timestamp = datetime.datetime.now()
|
|
|
220 |
trail.description = "shipping_id %d:tracking_id %s:airwayBill %s: provider %s" %(shipping_id, tracking_id, airwayBillNo, provider)
|
|
|
221 |
trail.transaction = transaction
|
|
|
222 |
session.commit()
|
|
|
223 |
return True
|
|
|
224 |
|
|
|
225 |
def change_shipping_status(transaction_id, shipping_id, status, description):
|
|
|
226 |
shipment = Shipment.get_by(id=shipping_id)
|
|
|
227 |
if not shipment:
|
|
|
228 |
raise TransactionServiceException(101, "not such shipment found")
|
|
|
229 |
|
|
|
230 |
transaction = shipment.transaction
|
|
|
231 |
if transaction.id != transaction_id:
|
|
|
232 |
raise TransactionServiceException(108, "Looks like this is invalid transaction")
|
|
|
233 |
|
|
|
234 |
trail = TransactionTrail()
|
|
|
235 |
trail.old_value = str(shipment.status)
|
|
|
236 |
trail.new_value = str(status)
|
|
|
237 |
shipment.status = status
|
|
|
238 |
trail.transaction = transaction
|
|
|
239 |
trail.timestamp = datetime.datetime.now()
|
|
|
240 |
trail.description = "Shipping status change : %s"%(description)
|
|
|
241 |
shipment.status_message = description
|
|
|
242 |
session.commit()
|
|
|
243 |
return True
|
|
|
244 |
|
|
|
245 |
def set_shipping_date(transaction_id, shipping_id, timestamp):
|
|
|
246 |
shipment = Shipment.get_by(id=shipping_id)
|
|
|
247 |
if not shipment:
|
|
|
248 |
raise TransactionServiceException(101, "not such shipment found")
|
|
|
249 |
|
|
|
250 |
transaction = shipment.transaction
|
|
|
251 |
if transaction.id != transaction_id:
|
|
|
252 |
raise TransactionServiceException(108, "Looks like this is invalid transaction")
|
|
|
253 |
|
|
|
254 |
trail = TransactionTrail()
|
|
|
255 |
trail.old_value = str(shipment.status)
|
|
|
256 |
trail.new_value = str(status_shipment_shipped)
|
|
|
257 |
shipment.status = status_shipment_shipped
|
|
|
258 |
trail.transaction = transaction
|
|
|
259 |
trail.timestamp = datetime.datetime.now()
|
|
|
260 |
trail.description = "Shipping status change : %s"%(shipment.id)
|
|
|
261 |
shipment.status_message = "Shipment dispatched"
|
|
|
262 |
shipment.shipment_time = timestamp
|
|
|
263 |
session.commit()
|
|
|
264 |
return True
|
|
|
265 |
|
|
|
266 |
|
|
|
267 |
def set_delivery_date(transaction_id, shipping_id, timestamp):
|
|
|
268 |
shipment = Shipment.get_by(id=shipping_id)
|
|
|
269 |
if not shipment:
|
|
|
270 |
raise TransactionServiceException(101, "not such shipment found")
|
|
|
271 |
|
|
|
272 |
transaction = shipment.transaction
|
|
|
273 |
if transaction.id != transaction_id:
|
|
|
274 |
raise TransactionServiceException(108, "Looks like this is invalid transaction")
|
|
|
275 |
|
|
|
276 |
trail = TransactionTrail()
|
|
|
277 |
trail.old_value = str(shipment.status)
|
|
|
278 |
trail.new_value = str(status_shipment_delivered)
|
|
|
279 |
shipment.status = status_shipment_delivered
|
|
|
280 |
trail.transaction = transaction
|
|
|
281 |
trail.timestamp = datetime.datetime.now()
|
|
|
282 |
trail.description = "Shipping status change : %s"%(shipment.id)
|
|
|
283 |
shipment.status_message = "Shipment delivered"
|
|
|
284 |
shipment.delivery_time = timestamp
|
|
|
285 |
session.commit()
|
|
|
286 |
return True
|
|
|
287 |
|
|
|
288 |
def set_billing(transaction_id, t_billing):
|
|
|
289 |
transaction = get_transaction(transaction_id)
|
| 194 |
ashish |
290 |
line_item = LineItem.query.filter_by(transaction_id=transaction_id).all()
|
|
|
291 |
#line_item = LineItem.get_by(id = t_billing.lineItem.id)
|
| 104 |
ashish |
292 |
if not line_item:
|
|
|
293 |
raise TransactionServiceException(101, "unable to get this line item for id %d" % (t_billing.lineItem.id))
|
|
|
294 |
billing = Billing()
|
|
|
295 |
billing.line_item = line_item
|
|
|
296 |
billing.bill_number = t_billing.billNumber
|
|
|
297 |
try:
|
|
|
298 |
billing.created_on = to_py_date(t_billing.generatedTimestamp)
|
|
|
299 |
except:
|
|
|
300 |
raise TransactionServiceException(108, "bad time stamp found while adding billing for transaction id %d" %(transaction.id))
|
| 194 |
ashish |
301 |
billing.generated_by = t_billing.generatedBy
|
| 104 |
ashish |
302 |
session.commit()
|
|
|
303 |
return True
|
|
|
304 |
|
|
|
305 |
def get_line_items(transaction_id):
|
|
|
306 |
transaction = get_transaction(transaction_id)
|
|
|
307 |
if not transaction.lineitems:
|
|
|
308 |
raise TransactionServiceException(108, "no lineitems with transaction %d"%(transaction_id))
|
|
|
309 |
return transaction.lineitems
|
|
|
310 |
|
|
|
311 |
def get_shipments(transaction_id):
|
|
|
312 |
transaction = get_transaction(transaction_id)
|
|
|
313 |
if not transaction.shipments:
|
|
|
314 |
raise TransactionServiceException(108, "no shipments with transaction %d"%(transaction_id))
|
|
|
315 |
return transaction.shipments
|
|
|
316 |
|
|
|
317 |
def get_billings(transaction_id):
|
|
|
318 |
transaction = get_transaction(transaction_id)
|
|
|
319 |
if not transaction.lineitems:
|
|
|
320 |
raise TransactionServiceException(108, "no lineitems, hence no billing with transaction %d"%(transaction_id))
|
|
|
321 |
billings = []
|
|
|
322 |
for lineitem in transaction.lineitems:
|
|
|
323 |
billings.append(lineitem.billing)
|
|
|
324 |
return billings
|
| 132 |
ashish |
325 |
return transaction.shipments
|
|
|
326 |
|
|
|
327 |
def get_transactions_for_shopping_cart_id(shopping_cart_id):
|
|
|
328 |
transactions = Transaction.query.filter_by(shopping_cart_id=shopping_cart_id).all()
|
|
|
329 |
if not transactions:
|
|
|
330 |
raise TransactionServiceException(108, "transactions for this shopping cart")
|
| 304 |
ashish |
331 |
return transactions
|
|
|
332 |
|
|
|
333 |
def get_alerts(transaction_id, all):
|
|
|
334 |
query = Alerts.query.filter_by(transaction_id=transaction_id)
|
|
|
335 |
if not all:
|
|
|
336 |
query = query.filter_by(valid=True)
|
|
|
337 |
return query.all()
|
|
|
338 |
|
|
|
339 |
def set_alert(transaction_id, unset, type, comment):
|
|
|
340 |
#get existing alert
|
|
|
341 |
if unset:
|
|
|
342 |
query = Alerts.query.filter_by(transaction_id=transaction_id)
|
|
|
343 |
query = query.filter_by(type=type)
|
|
|
344 |
alerts = query.all()
|
|
|
345 |
for alert in alerts:
|
|
|
346 |
if alert.valid:
|
|
|
347 |
alert.valid = False
|
|
|
348 |
alert.comment = comment
|
|
|
349 |
alert.time_unset = datetime.datetime.now()
|
|
|
350 |
else:
|
|
|
351 |
alert = Alerts()
|
|
|
352 |
alert.transaction_id = transaction_id
|
|
|
353 |
alert.valid = True
|
|
|
354 |
alert.type = type
|
|
|
355 |
alert.comment = comment
|
|
|
356 |
alert.time_set = datetime.datetime.now()
|
|
|
357 |
session.commit()
|
|
|
358 |
|
|
|
359 |
def get_extra_info(transaction_id):
|
|
|
360 |
return ExtraItemInfo.get_by(transaction_id=transaction_id)
|
|
|
361 |
|
|
|
362 |
def set_extra_info(transaction_id, sku_id, model, vendor, colour):
|
|
|
363 |
extra_item_info = ExtraItemInfo()
|
|
|
364 |
extra_item_info.transaction_id = transaction_id
|
|
|
365 |
extra_item_info.sku_id = sku_id
|
|
|
366 |
extra_item_info.model = model
|
|
|
367 |
extra_item_info.vendor = vendor
|
|
|
368 |
extra_item_info.colour = colour
|
|
|
369 |
session.commit()
|