| 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,\
|
| 685 |
chandransh |
7 |
Alerts, Order
|
| 104 |
ashish |
8 |
from shop2020.thriftpy.model.v1.order.ttypes import Transaction as T_Transaction,\
|
| 690 |
chandransh |
9 |
TransactionServiceException, TransactionStatus, OrderStatus
|
| 104 |
ashish |
10 |
import datetime
|
| 122 |
ashish |
11 |
from elixir import *
|
| 855 |
rajveer |
12 |
from shop2020.utils.Utils import to_py_date, to_java_date
|
| 104 |
ashish |
13 |
from sqlalchemy.sql.expression import and_
|
| 483 |
rajveer |
14 |
from shop2020.model.v1 import order
|
| 743 |
rajveer |
15 |
from shop2020.clients.LogisticsClient import LogisticsClient
|
| 871 |
chandransh |
16 |
from shop2020.clients.InventoryClient import InventoryClient
|
| 104 |
ashish |
17 |
|
|
|
18 |
def get_new_transaction():
|
|
|
19 |
transaction = Transaction()
|
|
|
20 |
transaction.createdOn = datetime.datetime.now()
|
| 738 |
chandransh |
21 |
transaction.status = TransactionStatus.INIT
|
| 104 |
ashish |
22 |
transaction.status_message = "New transaction"
|
| 483 |
rajveer |
23 |
return transaction
|
|
|
24 |
|
|
|
25 |
def create_order(t_order):
|
|
|
26 |
order = Order()
|
|
|
27 |
if t_order.warehouse_id:
|
|
|
28 |
order.warehouse_id = t_order.warehouse_id
|
| 509 |
rajveer |
29 |
if t_order.logistics_provider_id:
|
| 483 |
rajveer |
30 |
order.logistics_provider_id = t_order.logistics_provider_id
|
|
|
31 |
order.airwaybill_no = t_order.airwaybill_no
|
|
|
32 |
order.tracking_id = t_order.tracking_id
|
|
|
33 |
if t_order.expected_delivery_time:
|
|
|
34 |
order.expected_delivery_time = to_py_date(t_order.expected_delivery_time)
|
|
|
35 |
if t_order.customer_id:
|
|
|
36 |
order.customer_id = t_order.customer_id
|
|
|
37 |
order.customer_name = t_order.customer_name
|
| 860 |
rajveer |
38 |
order.customer_city = t_order.customer_city
|
|
|
39 |
order.customer_state = t_order.customer_state
|
| 483 |
rajveer |
40 |
order.customer_mobilenumber = t_order.customer_mobilenumber
|
|
|
41 |
order.customer_pincode = t_order.customer_pincode
|
| 738 |
chandransh |
42 |
order.customer_address1 = t_order.customer_address1
|
|
|
43 |
order.customer_address2 = t_order.customer_address2
|
| 483 |
rajveer |
44 |
order.customer_email = t_order.customer_email
|
|
|
45 |
#new status and status description to be added
|
| 716 |
rajveer |
46 |
order.status = t_order.status
|
| 483 |
rajveer |
47 |
order.statusDescription = t_order.statusDescription
|
|
|
48 |
order.total_amount = t_order.total_amount
|
|
|
49 |
order.total_weight = t_order.total_weight
|
|
|
50 |
if t_order.created_timestamp:
|
|
|
51 |
order.created_timestamp = to_py_date(t_order.created_timestamp)
|
|
|
52 |
else:
|
| 685 |
chandransh |
53 |
order.created_timestamp = datetime.datetime.now()
|
| 483 |
rajveer |
54 |
return order
|
|
|
55 |
|
|
|
56 |
def create_transaction(t_transaction):
|
|
|
57 |
t_orders = t_transaction.orders
|
|
|
58 |
if not t_orders:
|
|
|
59 |
raise TransactionServiceException(101, "Orders missing from the transaction")
|
|
|
60 |
transaction = get_new_transaction()
|
|
|
61 |
transaction.customer_id = t_transaction.customer_id
|
|
|
62 |
transaction.shopping_cart_id = t_transaction.shoppingCartid
|
|
|
63 |
|
|
|
64 |
for t_order in t_orders:
|
|
|
65 |
order = create_order(t_order)
|
|
|
66 |
order.transaction = transaction
|
|
|
67 |
for line_item in t_order.lineitems:
|
|
|
68 |
litem = LineItem()
|
| 716 |
rajveer |
69 |
litem.item_id = line_item.item_id
|
| 963 |
chandransh |
70 |
litem.productGroup = line_item.productGroup
|
| 483 |
rajveer |
71 |
litem.brand = line_item.brand
|
|
|
72 |
if line_item.model_number:
|
|
|
73 |
litem.model_number = line_item.model_number
|
|
|
74 |
if line_item.model_name:
|
|
|
75 |
litem.model_name = line_item.model_name
|
| 908 |
chandransh |
76 |
if line_item.color:
|
|
|
77 |
litem.color = line_item.color
|
| 483 |
rajveer |
78 |
if line_item.extra_info:
|
|
|
79 |
litem.extra_info = line_item.extra_info
|
|
|
80 |
litem.quantity = line_item.quantity
|
|
|
81 |
litem.unit_price = line_item.unit_price
|
|
|
82 |
litem.unit_weight = line_item.unit_weight
|
|
|
83 |
litem.total_price = line_item.total_price
|
|
|
84 |
litem.total_weight = line_item.total_weight
|
|
|
85 |
"""
|
|
|
86 |
if line_item.addedOn:
|
|
|
87 |
litem.added_on = to_py_date(line_item.addedOn)
|
|
|
88 |
else:
|
|
|
89 |
litem.added_on = datetime.datetime.now()
|
|
|
90 |
"""
|
|
|
91 |
litem.order = order
|
|
|
92 |
session.commit()
|
|
|
93 |
return transaction.id
|
|
|
94 |
|
|
|
95 |
def get_transaction(transaction_id):
|
|
|
96 |
transaction = Transaction.get_by(id=transaction_id)
|
|
|
97 |
if not transaction:
|
|
|
98 |
raise TransactionServiceException(108, "no such transaction")
|
|
|
99 |
|
|
|
100 |
return transaction
|
|
|
101 |
|
|
|
102 |
def get_transactions_for_customer(customer_id, from_date, to_date, status):
|
|
|
103 |
if not customer_id:
|
|
|
104 |
raise TransactionServiceException(101, "bad customer id")
|
|
|
105 |
query = Transaction.query.filter(Transaction.customer_id == customer_id)
|
|
|
106 |
if status:
|
|
|
107 |
query = query.filter(Transaction.status == status)
|
|
|
108 |
if from_date:
|
|
|
109 |
query = query.filter(Transaction.createdOn >from_date)
|
|
|
110 |
if to_date:
|
|
|
111 |
query = query.filter(Transaction.createdOn < to_date)
|
|
|
112 |
return query.all()
|
|
|
113 |
|
|
|
114 |
def get_transactions_for_shopping_cart_id(shopping_cart_id):
|
|
|
115 |
transactions = Transaction.query.filter_by(shopping_cart_id=shopping_cart_id).all()
|
|
|
116 |
if not transactions:
|
|
|
117 |
raise TransactionServiceException(108, "transactions for this shopping cart")
|
|
|
118 |
return transactions
|
|
|
119 |
|
|
|
120 |
def get_transaction_status(transaction_id):
|
|
|
121 |
if not transaction_id:
|
|
|
122 |
raise TransactionServiceException(101, "bad transaction id")
|
|
|
123 |
|
|
|
124 |
transaction = get_transaction(transaction_id)
|
|
|
125 |
|
|
|
126 |
return transaction.status
|
|
|
127 |
|
|
|
128 |
def change_transaction_status(transaction_id, new_status, description):
|
|
|
129 |
transaction = get_transaction(transaction_id)
|
|
|
130 |
transaction.status = new_status
|
|
|
131 |
transaction.status_message = description
|
| 690 |
chandransh |
132 |
if new_status == TransactionStatus.FAILED:
|
|
|
133 |
for order in transaction.orders:
|
|
|
134 |
order.status = OrderStatus.PAYMENT_FAILED
|
|
|
135 |
elif new_status == TransactionStatus.IN_PROCESS:
|
|
|
136 |
for order in transaction.orders:
|
|
|
137 |
order.status = OrderStatus.SUBMITTED_FOR_PROCESSING
|
| 743 |
rajveer |
138 |
#After we got payment success, we will set logistics info also
|
|
|
139 |
logistics_client = LogisticsClient().get_client()
|
|
|
140 |
#FIXME line item is only one now. If multiple will come, need to fix.
|
| 871 |
chandransh |
141 |
item_id = order.lineitems[0].item_id
|
|
|
142 |
logistics_info = logistics_client.getLogisticsInfo(order.customer_pincode, item_id)
|
| 743 |
rajveer |
143 |
|
|
|
144 |
order.warehouse_id = logistics_info.warehouseId
|
|
|
145 |
order.logistics_provider_id = logistics_info.providerId
|
|
|
146 |
order.airwaybill_no = logistics_info.airway_billno
|
|
|
147 |
order.tracking_id = order.airwaybill_no
|
| 855 |
rajveer |
148 |
order.expected_delivery_time = datetime.datetime.now() + datetime.timedelta(days=logistics_info.deliveryTime)
|
| 871 |
chandransh |
149 |
|
|
|
150 |
catalog_client = InventoryClient().get_client()
|
|
|
151 |
catalog_client.reserveItemInWarehouse(item_id, logistics_info.warehouseId, order.lineitems[0].quantity)
|
| 483 |
rajveer |
152 |
session.commit()
|
|
|
153 |
return True
|
|
|
154 |
|
|
|
155 |
|
|
|
156 |
def get_order(order_id):
|
|
|
157 |
order = Order.get_by(id=order_id)
|
|
|
158 |
if not order:
|
|
|
159 |
raise TransactionServiceException(108, "no such order")
|
|
|
160 |
|
|
|
161 |
return order
|
|
|
162 |
|
|
|
163 |
def get_orders_for_customer(customer_id, from_date, to_date, status):
|
|
|
164 |
if not customer_id:
|
|
|
165 |
raise TransactionServiceException(101, "bad customer id")
|
|
|
166 |
query = Order.query.filter(Order.customer_id == customer_id)
|
|
|
167 |
if status:
|
|
|
168 |
query = query.filter(Order.status == status)
|
|
|
169 |
if from_date:
|
|
|
170 |
query = query.filter(Order.created_timestamp >from_date)
|
|
|
171 |
if to_date:
|
|
|
172 |
query = query.filter(Order.created_timestamp < to_date)
|
|
|
173 |
return query.all()
|
|
|
174 |
|
|
|
175 |
def get_all_orders(status, from_date, to_date, warehouse_id):
|
|
|
176 |
if not warehouse_id:
|
|
|
177 |
raise TransactionServiceException(101, "bad warehouse id")
|
|
|
178 |
query = Order.query.filter(Order.warehouse_id == warehouse_id)
|
|
|
179 |
if status:
|
|
|
180 |
query = query.filter(Order.status == status)
|
|
|
181 |
if from_date:
|
|
|
182 |
query = query.filter(Order.created_timestamp >from_date)
|
|
|
183 |
if to_date:
|
|
|
184 |
query = query.filter(Order.created_timestamp < to_date)
|
|
|
185 |
return query.all()
|
|
|
186 |
|
|
|
187 |
def change_order_status(self, orderId, status, description):
|
|
|
188 |
order = get_order(orderId)
|
|
|
189 |
order.status = status
|
|
|
190 |
order.statusDescription = description
|
|
|
191 |
session.commit()
|
|
|
192 |
return True
|
|
|
193 |
|
| 921 |
rajveer |
194 |
def accept_order(self, orderId):
|
|
|
195 |
order = get_order(orderId)
|
|
|
196 |
order.status = OrderStatus.ACCEPTED
|
|
|
197 |
order.statusDescription = "Order Accepted"
|
|
|
198 |
order.accepted_timestamp = datetime.datetime.now()
|
|
|
199 |
session.commit()
|
|
|
200 |
return True
|
|
|
201 |
|
|
|
202 |
def bill_order(self, orderId):
|
|
|
203 |
order = get_order(orderId)
|
|
|
204 |
order.status = OrderStatus.BILLED
|
|
|
205 |
order.statusDescription = "Order Billed"
|
|
|
206 |
order.billing_timestamp = datetime.datetime.now()
|
|
|
207 |
session.commit()
|
|
|
208 |
return True
|
|
|
209 |
|
| 483 |
rajveer |
210 |
def get_orders_for_transaction(transaction_id):
|
|
|
211 |
orders = Order.query.filter_by(transaction_id=transaction_id).all()
|
|
|
212 |
if not orders:
|
|
|
213 |
raise TransactionServiceException(101, "No order for the transaction")
|
|
|
214 |
return orders
|
|
|
215 |
|
|
|
216 |
def get_line_items_for_order(order_id):
|
|
|
217 |
query = LineItem.query.filter(LineItem.order_id == order_id)
|
|
|
218 |
return query.all()
|
|
|
219 |
|
|
|
220 |
|
| 642 |
chandransh |
221 |
def add_billing_details(orderId, invoice_number, jacket_number, billed_by):
|
| 494 |
rajveer |
222 |
order = Order.get_by(id=orderId)
|
|
|
223 |
if not order:
|
|
|
224 |
raise TransactionServiceException(101, "No order found for the givem orderid")
|
|
|
225 |
order.invoice_number = invoice_number
|
| 642 |
chandransh |
226 |
order.jacket_number = jacket_number
|
| 494 |
rajveer |
227 |
order.billed_by = billed_by
|
|
|
228 |
session.commit()
|
|
|
229 |
return True
|
| 759 |
chandransh |
230 |
|
|
|
231 |
def mark_orders_as_manifested(warehouse_id, provider_id):
|
|
|
232 |
try:
|
| 986 |
chandransh |
233 |
current_timestamp = datetime.datetime.now()
|
| 852 |
chandransh |
234 |
orders = Order.query.filter_by(warehouse_id = warehouse_id, logistics_provider_id = provider_id, status = OrderStatus.BILLED).all()
|
| 759 |
chandransh |
235 |
for order in orders:
|
|
|
236 |
order.status = OrderStatus.SHIPPED_FROM_WH
|
| 986 |
chandransh |
237 |
order.shipping_timestamp = current_timestamp
|
| 759 |
chandransh |
238 |
session.commit()
|
|
|
239 |
return True
|
|
|
240 |
except:
|
|
|
241 |
return False
|
| 494 |
rajveer |
242 |
|
| 759 |
chandransh |
243 |
|
| 483 |
rajveer |
244 |
def get_alerts(order_id, all):
|
|
|
245 |
query = Alerts.query.filter_by(order_id=order_id)
|
|
|
246 |
if not all:
|
|
|
247 |
query = query.filter_by(valid=True)
|
|
|
248 |
return query.all()
|
|
|
249 |
|
|
|
250 |
def set_alert(order_id, unset, type, comment):
|
|
|
251 |
#get existing alert
|
|
|
252 |
if unset:
|
|
|
253 |
query = Alerts.query.filter_by(order_id=order_id)
|
|
|
254 |
query = query.filter_by(type=type)
|
|
|
255 |
alerts = query.all()
|
|
|
256 |
for alert in alerts:
|
|
|
257 |
if alert.valid:
|
|
|
258 |
alert.valid = False
|
|
|
259 |
alert.comment = comment
|
|
|
260 |
alert.time_unset = datetime.datetime.now()
|
|
|
261 |
else:
|
|
|
262 |
alert = Alerts()
|
|
|
263 |
alert.order_id = order_id
|
|
|
264 |
alert.valid = True
|
|
|
265 |
alert.type = type
|
|
|
266 |
alert.comment = comment
|
|
|
267 |
alert.time_set = datetime.datetime.now()
|
| 766 |
rajveer |
268 |
session.commit()
|
|
|
269 |
|
|
|
270 |
def close_session():
|
|
|
271 |
if session.is_active:
|
|
|
272 |
print "session is active. closing it."
|
|
|
273 |
session.close()
|