| 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,\
|
|
|
7 |
AdditionalInfo, Payment, Shipment, TransactionTrail, Billing
|
|
|
8 |
from shop2020.thriftpy.model.v1.order.ttypes import Transaction as T_Transaction,\
|
|
|
9 |
TransactionServiceException
|
|
|
10 |
import datetime
|
|
|
11 |
from shop2020.utils.OrderStatus import status_transaction_init,\
|
|
|
12 |
status_shipment_shipped, status_shipment_delivered
|
| 122 |
ashish |
13 |
from elixir import *
|
| 104 |
ashish |
14 |
from shop2020.utils.Utils import to_py_date
|
|
|
15 |
from sqlalchemy.sql.expression import and_
|
|
|
16 |
|
|
|
17 |
|
|
|
18 |
def get_new_transaction():
|
|
|
19 |
transaction = Transaction()
|
|
|
20 |
transaction.createdOn = datetime.datetime.now()
|
|
|
21 |
transaction.status = status_transaction_init
|
|
|
22 |
transaction.status_message = "New transaction"
|
|
|
23 |
transaction.trails = []
|
|
|
24 |
session.commit()
|
|
|
25 |
return transaction
|
|
|
26 |
|
|
|
27 |
def create_transaction(t_transaction):
|
|
|
28 |
orderinfo = t_transaction.orderInfo
|
|
|
29 |
if not orderinfo:
|
|
|
30 |
raise TransactionServiceException(101, "Orderinfo missing from the transaction")
|
|
|
31 |
transaction = get_new_transaction()
|
|
|
32 |
for line_item in orderinfo.lineitems:
|
|
|
33 |
litem = LineItem()
|
|
|
34 |
litem.item_id = line_item.item
|
|
|
35 |
litem.added_on = to_py_date(line_item.addedOn)
|
|
|
36 |
transaction.lineitems.append(litem)
|
|
|
37 |
if line_item.additionalInfo:
|
|
|
38 |
for k,v in line_item.additionalInfo.iteritems():
|
|
|
39 |
additional_info = AdditionalInfo()
|
|
|
40 |
additional_info.key = k
|
|
|
41 |
additional_info.value = v
|
|
|
42 |
litem.additional_info.append(additional_info)
|
|
|
43 |
|
|
|
44 |
session.commit()
|
|
|
45 |
return transaction.id
|
|
|
46 |
|
|
|
47 |
|
|
|
48 |
def get_transaction(transaction_id):
|
|
|
49 |
transaction = Transaction.get_by(id=transaction_id)
|
|
|
50 |
if not transaction:
|
|
|
51 |
raise TransactionServiceException(108, "no such transaction")
|
|
|
52 |
|
|
|
53 |
return transaction
|
|
|
54 |
|
|
|
55 |
def get_all_transactions(status, from_date, to_date):
|
|
|
56 |
if not status:
|
|
|
57 |
if not from_date and not to_date:
|
|
|
58 |
#get all between from_date and to_date
|
|
|
59 |
return Transaction.query.all()
|
|
|
60 |
|
|
|
61 |
if not from_date:
|
|
|
62 |
return Transaction.query.filter(Transaction.createdOn < to_date).all()
|
|
|
63 |
|
|
|
64 |
if not to_date:
|
|
|
65 |
return Transaction.query.filter(Transaction.createdOn > to_date).all()
|
|
|
66 |
return Transaction.query.filter(and_(Transaction.createdOn < to_date , Transaction.createdOn > from_date)).all()
|
|
|
67 |
|
|
|
68 |
|
|
|
69 |
if not from_date and not to_date:
|
|
|
70 |
return Transaction.query.filter(Transaction.status == status).all()
|
|
|
71 |
if not from_date:
|
|
|
72 |
return Transaction.query.filter(and_(Transaction.status == status, Transaction.createdOn <to_date)).all()
|
|
|
73 |
if not from_date:
|
|
|
74 |
return Transaction.query.filter(and_(Transaction.status == status, Transaction.createdOn >from_date)).all()
|
|
|
75 |
if not from_date:
|
|
|
76 |
return Transaction.query.filter(and_(Transaction.status == status, and_(Transaction.createdOn <to_date, Transaction.createdOn > from_date))).all()
|
|
|
77 |
|
|
|
78 |
|
|
|
79 |
def get_transactions_for_shipment_status(status, from_date, to_date):
|
|
|
80 |
|
|
|
81 |
if not status:
|
|
|
82 |
raise TransactionServiceException(101, "unknown status")
|
|
|
83 |
|
|
|
84 |
query = Shipment.query.filter(Shipment.status == status)
|
|
|
85 |
|
|
|
86 |
if from_date:
|
|
|
87 |
query = query.filter(Shipment.shipment_time >from_date)
|
|
|
88 |
if to_date:
|
|
|
89 |
query = query.filter(Shipment.shipment_time <to_date)
|
|
|
90 |
|
|
|
91 |
shipments = query.all()
|
|
|
92 |
|
|
|
93 |
transactions = {}
|
|
|
94 |
for shipment in shipments:
|
|
|
95 |
if not transactions[shipment.transaction.id]:
|
|
|
96 |
transactions.append(shipment.transaction)
|
|
|
97 |
return transactions.values()
|
|
|
98 |
|
|
|
99 |
|
|
|
100 |
def get_transactions_for_customer(customer_id, from_date, to_date, status):
|
|
|
101 |
if not customer_id:
|
|
|
102 |
raise TransactionServiceException(101, "bas customer id")
|
|
|
103 |
query = Transaction.query.filter(Transaction.customer_id == customer_id)
|
|
|
104 |
if status:
|
|
|
105 |
query = query.filter(Transaction.status == status)
|
|
|
106 |
if from_date:
|
|
|
107 |
query = query.filter(Transaction.createdOn >from_date)
|
|
|
108 |
if to_date:
|
|
|
109 |
query = query.filter(Transaction.createdOn < to_date)
|
|
|
110 |
return query.all()
|
|
|
111 |
|
|
|
112 |
def get_transactions_for_customer_shipment(customer_id, from_date, to_date, status):
|
|
|
113 |
|
|
|
114 |
if not customer_id:
|
|
|
115 |
raise TransactionServiceException(101, "unknown customer id")
|
|
|
116 |
|
|
|
117 |
query = Shipment.query.filter(Shipment.transaction.customerId == customer_id)
|
|
|
118 |
|
|
|
119 |
if status:
|
|
|
120 |
query = query.filter(Shipment.status == status)
|
|
|
121 |
|
|
|
122 |
if from_date:
|
|
|
123 |
query = query.filter(Shipment.shipment_time >from_date)
|
|
|
124 |
|
|
|
125 |
if to_date:
|
|
|
126 |
query = query.filter(Shipment.shipment_time <to_date)
|
|
|
127 |
|
|
|
128 |
shipments = query.all()
|
|
|
129 |
|
|
|
130 |
transactions = {}
|
|
|
131 |
for shipment in shipments:
|
|
|
132 |
if not transactions[shipment.transaction.id]:
|
|
|
133 |
transactions.append(shipment.transaction)
|
|
|
134 |
return transactions.values()
|
|
|
135 |
|
|
|
136 |
def get_transaction_status(transaction_id):
|
|
|
137 |
|
|
|
138 |
if not transaction_id:
|
|
|
139 |
raise TransactionServiceException(101, "bad transaction id")
|
|
|
140 |
|
|
|
141 |
transaction = get_transaction(transaction_id)
|
|
|
142 |
|
|
|
143 |
return transaction.status
|
|
|
144 |
|
|
|
145 |
def change_transaction_status(transaction_id, new_status, description):
|
|
|
146 |
transaction = get_transaction(transaction_id)
|
|
|
147 |
trail = TransactionTrail()
|
|
|
148 |
trail.old_value = str(transaction.status)
|
|
|
149 |
trail.new_value = str(new_status)
|
|
|
150 |
transaction.status = new_status
|
|
|
151 |
trail.transaction = transaction
|
|
|
152 |
trail.timestamp = datetime.datetime.now()
|
|
|
153 |
trail.description = description
|
|
|
154 |
transaction.status_message = description
|
|
|
155 |
session.commit()
|
|
|
156 |
return True
|
|
|
157 |
|
|
|
158 |
def set_shipping_tracker_info(transaction_id, shipping_id, tracking_id, airwayBillNo, provider):
|
|
|
159 |
shipment = Shipment.get_by(id=shipping_id)
|
|
|
160 |
if not shipment:
|
|
|
161 |
raise TransactionServiceException(101, "not such shipment found")
|
|
|
162 |
|
|
|
163 |
transaction = shipment.transaction
|
|
|
164 |
if transaction.id != transaction_id:
|
|
|
165 |
raise TransactionServiceException(108, "Looks like this is invalid transaction")
|
|
|
166 |
shipment.tracking_id = tracking_id
|
|
|
167 |
shipment.airway_bill_no = airwayBillNo
|
|
|
168 |
shipment.provider = provider
|
|
|
169 |
trail = TransactionTrail()
|
|
|
170 |
trail.timestamp = datetime.datetime.now()
|
|
|
171 |
trail.description = "shipping_id %d:tracking_id %s:airwayBill %s: provider %s" %(shipping_id, tracking_id, airwayBillNo, provider)
|
|
|
172 |
trail.transaction = transaction
|
|
|
173 |
session.commit()
|
|
|
174 |
return True
|
|
|
175 |
|
|
|
176 |
def change_shipping_status(transaction_id, shipping_id, status, description):
|
|
|
177 |
shipment = Shipment.get_by(id=shipping_id)
|
|
|
178 |
if not shipment:
|
|
|
179 |
raise TransactionServiceException(101, "not such shipment found")
|
|
|
180 |
|
|
|
181 |
transaction = shipment.transaction
|
|
|
182 |
if transaction.id != transaction_id:
|
|
|
183 |
raise TransactionServiceException(108, "Looks like this is invalid transaction")
|
|
|
184 |
|
|
|
185 |
trail = TransactionTrail()
|
|
|
186 |
trail.old_value = str(shipment.status)
|
|
|
187 |
trail.new_value = str(status)
|
|
|
188 |
shipment.status = status
|
|
|
189 |
trail.transaction = transaction
|
|
|
190 |
trail.timestamp = datetime.datetime.now()
|
|
|
191 |
trail.description = "Shipping status change : %s"%(description)
|
|
|
192 |
shipment.status_message = description
|
|
|
193 |
session.commit()
|
|
|
194 |
return True
|
|
|
195 |
|
|
|
196 |
def set_shipping_date(transaction_id, shipping_id, timestamp):
|
|
|
197 |
shipment = Shipment.get_by(id=shipping_id)
|
|
|
198 |
if not shipment:
|
|
|
199 |
raise TransactionServiceException(101, "not such shipment found")
|
|
|
200 |
|
|
|
201 |
transaction = shipment.transaction
|
|
|
202 |
if transaction.id != transaction_id:
|
|
|
203 |
raise TransactionServiceException(108, "Looks like this is invalid transaction")
|
|
|
204 |
|
|
|
205 |
trail = TransactionTrail()
|
|
|
206 |
trail.old_value = str(shipment.status)
|
|
|
207 |
trail.new_value = str(status_shipment_shipped)
|
|
|
208 |
shipment.status = status_shipment_shipped
|
|
|
209 |
trail.transaction = transaction
|
|
|
210 |
trail.timestamp = datetime.datetime.now()
|
|
|
211 |
trail.description = "Shipping status change : %s"%(shipment.id)
|
|
|
212 |
shipment.status_message = "Shipment dispatched"
|
|
|
213 |
shipment.shipment_time = timestamp
|
|
|
214 |
session.commit()
|
|
|
215 |
return True
|
|
|
216 |
|
|
|
217 |
|
|
|
218 |
def set_delivery_date(transaction_id, shipping_id, timestamp):
|
|
|
219 |
shipment = Shipment.get_by(id=shipping_id)
|
|
|
220 |
if not shipment:
|
|
|
221 |
raise TransactionServiceException(101, "not such shipment found")
|
|
|
222 |
|
|
|
223 |
transaction = shipment.transaction
|
|
|
224 |
if transaction.id != transaction_id:
|
|
|
225 |
raise TransactionServiceException(108, "Looks like this is invalid transaction")
|
|
|
226 |
|
|
|
227 |
trail = TransactionTrail()
|
|
|
228 |
trail.old_value = str(shipment.status)
|
|
|
229 |
trail.new_value = str(status_shipment_delivered)
|
|
|
230 |
shipment.status = status_shipment_delivered
|
|
|
231 |
trail.transaction = transaction
|
|
|
232 |
trail.timestamp = datetime.datetime.now()
|
|
|
233 |
trail.description = "Shipping status change : %s"%(shipment.id)
|
|
|
234 |
shipment.status_message = "Shipment delivered"
|
|
|
235 |
shipment.delivery_time = timestamp
|
|
|
236 |
session.commit()
|
|
|
237 |
return True
|
|
|
238 |
|
|
|
239 |
def set_billing(transaction_id, t_billing):
|
|
|
240 |
transaction = get_transaction(transaction_id)
|
|
|
241 |
line_item = LineItem.get_by(id = t_billing.lineItem.id)
|
|
|
242 |
if not line_item:
|
|
|
243 |
raise TransactionServiceException(101, "unable to get this line item for id %d" % (t_billing.lineItem.id))
|
|
|
244 |
billing = Billing()
|
|
|
245 |
billing.line_item = line_item
|
|
|
246 |
billing.bill_number = t_billing.billNumber
|
|
|
247 |
try:
|
|
|
248 |
billing.created_on = to_py_date(t_billing.generatedTimestamp)
|
|
|
249 |
except:
|
|
|
250 |
raise TransactionServiceException(108, "bad time stamp found while adding billing for transaction id %d" %(transaction.id))
|
|
|
251 |
billing.generated_by = t_billing.generatedby
|
|
|
252 |
session.commit()
|
|
|
253 |
return True
|
|
|
254 |
|
|
|
255 |
def get_line_items(transaction_id):
|
|
|
256 |
transaction = get_transaction(transaction_id)
|
|
|
257 |
if not transaction.lineitems:
|
|
|
258 |
raise TransactionServiceException(108, "no lineitems with transaction %d"%(transaction_id))
|
|
|
259 |
return transaction.lineitems
|
|
|
260 |
|
|
|
261 |
def get_shipments(transaction_id):
|
|
|
262 |
transaction = get_transaction(transaction_id)
|
|
|
263 |
if not transaction.shipments:
|
|
|
264 |
raise TransactionServiceException(108, "no shipments with transaction %d"%(transaction_id))
|
|
|
265 |
return transaction.shipments
|
|
|
266 |
|
|
|
267 |
def get_billings(transaction_id):
|
|
|
268 |
transaction = get_transaction(transaction_id)
|
|
|
269 |
if not transaction.lineitems:
|
|
|
270 |
raise TransactionServiceException(108, "no lineitems, hence no billing with transaction %d"%(transaction_id))
|
|
|
271 |
billings = []
|
|
|
272 |
for lineitem in transaction.lineitems:
|
|
|
273 |
billings.append(lineitem.billing)
|
|
|
274 |
return billings
|
|
|
275 |
return transaction.shipments
|