| 104 |
ashish |
1 |
'''
|
|
|
2 |
Created on 29-Mar-2010
|
|
|
3 |
|
| 2536 |
chandransh |
4 |
@author: Chandranshu
|
| 104 |
ashish |
5 |
'''
|
| 2536 |
chandransh |
6 |
|
|
|
7 |
import sys
|
|
|
8 |
import os
|
|
|
9 |
import datetime
|
|
|
10 |
import time
|
|
|
11 |
from datetime import date
|
|
|
12 |
from textwrap import dedent
|
| 1262 |
varun.gupt |
13 |
from string import Template
|
|
|
14 |
|
| 2536 |
chandransh |
15 |
from sqlalchemy.sql.expression import and_, or_, desc
|
|
|
16 |
from sqlalchemy.sql import func
|
|
|
17 |
|
|
|
18 |
from elixir import *
|
|
|
19 |
|
|
|
20 |
from reportlab.pdfgen.canvas import Canvas
|
|
|
21 |
from reportlab.rl_config import defaultPageSize
|
|
|
22 |
from reportlab.lib.units import inch
|
|
|
23 |
PAGE_HEIGHT=defaultPageSize[1]
|
|
|
24 |
PAGE_WIDTH=defaultPageSize[0]
|
|
|
25 |
|
| 104 |
ashish |
26 |
from shop2020.model.v1.order.impl.DataService import Transaction, LineItem,\
|
| 2628 |
chandransh |
27 |
Alerts, Order, BatchNoGenerator, ReturnOrder
|
| 104 |
ashish |
28 |
from shop2020.thriftpy.model.v1.order.ttypes import Transaction as T_Transaction,\
|
| 690 |
chandransh |
29 |
TransactionServiceException, TransactionStatus, OrderStatus
|
| 855 |
rajveer |
30 |
from shop2020.utils.Utils import to_py_date, to_java_date
|
| 483 |
rajveer |
31 |
from shop2020.model.v1 import order
|
| 743 |
rajveer |
32 |
from shop2020.clients.LogisticsClient import LogisticsClient
|
| 871 |
chandransh |
33 |
from shop2020.clients.InventoryClient import InventoryClient
|
| 1262 |
varun.gupt |
34 |
from shop2020.clients.UserClient import UserClient
|
| 1351 |
varun.gupt |
35 |
from shop2020.clients.HelperClient import HelperClient
|
| 2536 |
chandransh |
36 |
from shop2020.utils.EmailAttachmentSender import mail, get_attachment_part
|
| 104 |
ashish |
37 |
|
|
|
38 |
def get_new_transaction():
|
|
|
39 |
transaction = Transaction()
|
|
|
40 |
transaction.createdOn = datetime.datetime.now()
|
| 738 |
chandransh |
41 |
transaction.status = TransactionStatus.INIT
|
| 104 |
ashish |
42 |
transaction.status_message = "New transaction"
|
| 483 |
rajveer |
43 |
return transaction
|
|
|
44 |
|
|
|
45 |
def create_order(t_order):
|
|
|
46 |
order = Order()
|
|
|
47 |
if t_order.warehouse_id:
|
|
|
48 |
order.warehouse_id = t_order.warehouse_id
|
| 509 |
rajveer |
49 |
if t_order.logistics_provider_id:
|
| 483 |
rajveer |
50 |
order.logistics_provider_id = t_order.logistics_provider_id
|
|
|
51 |
order.airwaybill_no = t_order.airwaybill_no
|
|
|
52 |
order.tracking_id = t_order.tracking_id
|
|
|
53 |
if t_order.expected_delivery_time:
|
|
|
54 |
order.expected_delivery_time = to_py_date(t_order.expected_delivery_time)
|
|
|
55 |
if t_order.customer_id:
|
|
|
56 |
order.customer_id = t_order.customer_id
|
|
|
57 |
order.customer_name = t_order.customer_name
|
| 860 |
rajveer |
58 |
order.customer_city = t_order.customer_city
|
|
|
59 |
order.customer_state = t_order.customer_state
|
| 483 |
rajveer |
60 |
order.customer_mobilenumber = t_order.customer_mobilenumber
|
|
|
61 |
order.customer_pincode = t_order.customer_pincode
|
| 738 |
chandransh |
62 |
order.customer_address1 = t_order.customer_address1
|
|
|
63 |
order.customer_address2 = t_order.customer_address2
|
| 483 |
rajveer |
64 |
order.customer_email = t_order.customer_email
|
|
|
65 |
#new status and status description to be added
|
| 716 |
rajveer |
66 |
order.status = t_order.status
|
| 483 |
rajveer |
67 |
order.statusDescription = t_order.statusDescription
|
|
|
68 |
order.total_amount = t_order.total_amount
|
|
|
69 |
order.total_weight = t_order.total_weight
|
|
|
70 |
if t_order.created_timestamp:
|
|
|
71 |
order.created_timestamp = to_py_date(t_order.created_timestamp)
|
|
|
72 |
else:
|
| 685 |
chandransh |
73 |
order.created_timestamp = datetime.datetime.now()
|
| 483 |
rajveer |
74 |
return order
|
|
|
75 |
|
|
|
76 |
def create_transaction(t_transaction):
|
|
|
77 |
t_orders = t_transaction.orders
|
|
|
78 |
if not t_orders:
|
|
|
79 |
raise TransactionServiceException(101, "Orders missing from the transaction")
|
|
|
80 |
transaction = get_new_transaction()
|
|
|
81 |
transaction.customer_id = t_transaction.customer_id
|
|
|
82 |
transaction.shopping_cart_id = t_transaction.shoppingCartid
|
| 2219 |
varun.gupt |
83 |
transaction.coupon_code = t_transaction.coupon_code
|
| 483 |
rajveer |
84 |
|
|
|
85 |
for t_order in t_orders:
|
|
|
86 |
order = create_order(t_order)
|
|
|
87 |
order.transaction = transaction
|
|
|
88 |
for line_item in t_order.lineitems:
|
|
|
89 |
litem = LineItem()
|
| 716 |
rajveer |
90 |
litem.item_id = line_item.item_id
|
| 963 |
chandransh |
91 |
litem.productGroup = line_item.productGroup
|
| 483 |
rajveer |
92 |
litem.brand = line_item.brand
|
|
|
93 |
if line_item.model_number:
|
|
|
94 |
litem.model_number = line_item.model_number
|
|
|
95 |
if line_item.model_name:
|
|
|
96 |
litem.model_name = line_item.model_name
|
| 908 |
chandransh |
97 |
if line_item.color:
|
|
|
98 |
litem.color = line_item.color
|
| 483 |
rajveer |
99 |
if line_item.extra_info:
|
|
|
100 |
litem.extra_info = line_item.extra_info
|
|
|
101 |
litem.quantity = line_item.quantity
|
|
|
102 |
litem.unit_price = line_item.unit_price
|
|
|
103 |
litem.unit_weight = line_item.unit_weight
|
|
|
104 |
litem.total_price = line_item.total_price
|
|
|
105 |
litem.total_weight = line_item.total_weight
|
| 1262 |
varun.gupt |
106 |
litem.transfer_price = line_item.transfer_price
|
| 1976 |
varun.gupt |
107 |
|
| 483 |
rajveer |
108 |
"""
|
|
|
109 |
if line_item.addedOn:
|
|
|
110 |
litem.added_on = to_py_date(line_item.addedOn)
|
|
|
111 |
else:
|
|
|
112 |
litem.added_on = datetime.datetime.now()
|
|
|
113 |
"""
|
|
|
114 |
litem.order = order
|
|
|
115 |
session.commit()
|
|
|
116 |
return transaction.id
|
|
|
117 |
|
|
|
118 |
def get_transaction(transaction_id):
|
|
|
119 |
transaction = Transaction.get_by(id=transaction_id)
|
|
|
120 |
if not transaction:
|
|
|
121 |
raise TransactionServiceException(108, "no such transaction")
|
|
|
122 |
|
|
|
123 |
return transaction
|
|
|
124 |
|
|
|
125 |
def get_transactions_for_customer(customer_id, from_date, to_date, status):
|
|
|
126 |
if not customer_id:
|
|
|
127 |
raise TransactionServiceException(101, "bad customer id")
|
|
|
128 |
query = Transaction.query.filter(Transaction.customer_id == customer_id)
|
| 1493 |
vikas |
129 |
if status is not None:
|
| 483 |
rajveer |
130 |
query = query.filter(Transaction.status == status)
|
|
|
131 |
if from_date:
|
|
|
132 |
query = query.filter(Transaction.createdOn >from_date)
|
|
|
133 |
if to_date:
|
|
|
134 |
query = query.filter(Transaction.createdOn < to_date)
|
|
|
135 |
return query.all()
|
|
|
136 |
|
|
|
137 |
def get_transactions_for_shopping_cart_id(shopping_cart_id):
|
|
|
138 |
transactions = Transaction.query.filter_by(shopping_cart_id=shopping_cart_id).all()
|
|
|
139 |
return transactions
|
|
|
140 |
|
|
|
141 |
def get_transaction_status(transaction_id):
|
|
|
142 |
if not transaction_id:
|
|
|
143 |
raise TransactionServiceException(101, "bad transaction id")
|
|
|
144 |
|
|
|
145 |
transaction = get_transaction(transaction_id)
|
|
|
146 |
|
|
|
147 |
return transaction.status
|
|
|
148 |
|
|
|
149 |
def change_transaction_status(transaction_id, new_status, description):
|
|
|
150 |
transaction = get_transaction(transaction_id)
|
|
|
151 |
transaction.status = new_status
|
|
|
152 |
transaction.status_message = description
|
| 690 |
chandransh |
153 |
if new_status == TransactionStatus.FAILED:
|
|
|
154 |
for order in transaction.orders:
|
|
|
155 |
order.status = OrderStatus.PAYMENT_FAILED
|
| 2339 |
chandransh |
156 |
order.statusDescription = "Payment Failed"
|
| 690 |
chandransh |
157 |
elif new_status == TransactionStatus.IN_PROCESS:
|
|
|
158 |
for order in transaction.orders:
|
|
|
159 |
order.status = OrderStatus.SUBMITTED_FOR_PROCESSING
|
| 1219 |
chandransh |
160 |
order.statusDescription = "Submitted to warehouse"
|
| 743 |
rajveer |
161 |
#After we got payment success, we will set logistics info also
|
|
|
162 |
logistics_client = LogisticsClient().get_client()
|
|
|
163 |
#FIXME line item is only one now. If multiple will come, need to fix.
|
| 871 |
chandransh |
164 |
item_id = order.lineitems[0].item_id
|
|
|
165 |
logistics_info = logistics_client.getLogisticsInfo(order.customer_pincode, item_id)
|
| 743 |
rajveer |
166 |
|
| 1730 |
ankur.sing |
167 |
current_time = datetime.datetime.now()
|
|
|
168 |
logistics_info.deliveryTime = adjust_delivery_time(current_time, logistics_info.deliveryTime)
|
|
|
169 |
|
| 743 |
rajveer |
170 |
order.warehouse_id = logistics_info.warehouseId
|
|
|
171 |
order.logistics_provider_id = logistics_info.providerId
|
|
|
172 |
order.airwaybill_no = logistics_info.airway_billno
|
|
|
173 |
order.tracking_id = order.airwaybill_no
|
| 1730 |
ankur.sing |
174 |
order.expected_delivery_time = current_time + datetime.timedelta(days=logistics_info.deliveryTime)
|
| 871 |
chandransh |
175 |
|
|
|
176 |
catalog_client = InventoryClient().get_client()
|
|
|
177 |
catalog_client.reserveItemInWarehouse(item_id, logistics_info.warehouseId, order.lineitems[0].quantity)
|
| 1351 |
varun.gupt |
178 |
|
| 1341 |
chandransh |
179 |
item_pricing = catalog_client.getItemPricing(item_id, order.warehouse_id)
|
|
|
180 |
order.lineitems[0].transfer_price = item_pricing.transferPrice
|
| 1262 |
varun.gupt |
181 |
|
| 483 |
rajveer |
182 |
session.commit()
|
|
|
183 |
return True
|
|
|
184 |
|
| 1730 |
ankur.sing |
185 |
def adjust_delivery_time(order_time, delivery_days):
|
|
|
186 |
order_date = order_time.date()
|
|
|
187 |
|
|
|
188 |
delivery_date = order_date + datetime.timedelta(days = delivery_days)
|
|
|
189 |
final_delivery_date = delivery_date
|
|
|
190 |
|
|
|
191 |
time_format = "%Y-%m-%d %H:%M:%S.%f"
|
|
|
192 |
t = time.strptime(str(order_time), time_format)
|
|
|
193 |
midnightTime = datetime.datetime(*t[:3])
|
|
|
194 |
fromDate = to_java_date(midnightTime)
|
|
|
195 |
toDate = -1L
|
|
|
196 |
|
|
|
197 |
logistics_client = LogisticsClient().get_client()
|
|
|
198 |
holiday_dates = logistics_client.getHolidays(fromDate, toDate)
|
|
|
199 |
holiday_dates = [(to_py_date(h_date)).date() for h_date in holiday_dates]
|
|
|
200 |
|
|
|
201 |
while order_date <= final_delivery_date:
|
|
|
202 |
if order_date.weekday() == 6 or is_holiday(holiday_dates, order_date):
|
|
|
203 |
delivery_days = delivery_days + 1
|
|
|
204 |
final_delivery_date = final_delivery_date + datetime.timedelta(days = 1)
|
|
|
205 |
order_date = order_date + datetime.timedelta(days = 1)
|
|
|
206 |
return delivery_days
|
|
|
207 |
|
|
|
208 |
|
|
|
209 |
|
|
|
210 |
def is_holiday(holidays, date):
|
|
|
211 |
try:
|
|
|
212 |
holidays.index(date)
|
|
|
213 |
return 1
|
|
|
214 |
except:
|
|
|
215 |
return 0
|
|
|
216 |
|
| 1398 |
varun.gupt |
217 |
def enqueue_transaction_info_email(transaction_id):
|
| 1262 |
varun.gupt |
218 |
transaction = get_transaction(transaction_id)
|
|
|
219 |
|
|
|
220 |
html_header = """
|
|
|
221 |
<html>
|
|
|
222 |
<body>
|
|
|
223 |
<div>
|
|
|
224 |
<p>
|
|
|
225 |
Hello,<br /><br />
|
| 1742 |
rajveer |
226 |
Thanks for placing order with us. Following are the details of your order:
|
| 1262 |
varun.gupt |
227 |
</p>
|
|
|
228 |
<div>
|
|
|
229 |
Order Date: $order_date
|
| 1569 |
varun.gupt |
230 |
</div>
|
| 1262 |
varun.gupt |
231 |
<div>
|
|
|
232 |
<table>
|
| 1569 |
varun.gupt |
233 |
<tr><td colspan="5"><hr /></td></tr>
|
|
|
234 |
<tr><td colspan="5" align="left"><b>Order Details</b></td></tr>
|
|
|
235 |
<tr><td colspan="5"><hr /></td></tr>
|
| 1262 |
varun.gupt |
236 |
<tr>
|
| 2050 |
varun.gupt |
237 |
<th width="100">Order No.</th>
|
|
|
238 |
<th>Product</th>
|
|
|
239 |
<th width="100">Quantity</th>
|
|
|
240 |
<th width="100">Unit Price</th>
|
|
|
241 |
<th width="100">Amount</th>
|
| 1262 |
varun.gupt |
242 |
</tr>
|
|
|
243 |
"""
|
| 1398 |
varun.gupt |
244 |
user_email = ""
|
| 1262 |
varun.gupt |
245 |
total_amount = 0.0
|
|
|
246 |
html_table = ""
|
| 1569 |
varun.gupt |
247 |
order_date = datetime.datetime.now()
|
| 1262 |
varun.gupt |
248 |
|
|
|
249 |
for order in transaction.orders:
|
|
|
250 |
order_date = order.created_timestamp
|
| 1398 |
varun.gupt |
251 |
user_email = order.customer_email
|
| 1569 |
varun.gupt |
252 |
html_tr = "<tr><td align='center'>" + str(order.id) + "</td>"
|
| 1262 |
varun.gupt |
253 |
|
|
|
254 |
for lineitem in order.lineitems:
|
| 2050 |
varun.gupt |
255 |
lineitem_total_price = round(lineitem.total_price, 2)
|
|
|
256 |
|
| 1276 |
chandransh |
257 |
html_tr += "<td>" + str(lineitem) + "</td>"
|
| 2050 |
varun.gupt |
258 |
html_tr += "<td align='center'>" + ("%.0f" % lineitem.quantity) + "</td>"
|
|
|
259 |
html_tr += "<td align='center'> Rs. " + ("%.2f" % lineitem.unit_price) + "</td>"
|
|
|
260 |
html_tr += "<td align='center'> Rs. " + ("%.2f" % lineitem_total_price) + "</td></tr>"
|
|
|
261 |
total_amount += lineitem_total_price
|
| 1262 |
varun.gupt |
262 |
html_table += html_tr
|
| 2050 |
varun.gupt |
263 |
|
| 1262 |
varun.gupt |
264 |
html_footer = """
|
|
|
265 |
<tr><td colspan=5> </td></tr>
|
| 1565 |
varun.gupt |
266 |
<tr><td colspan=5><hr /></td></tr>
|
| 1262 |
varun.gupt |
267 |
<tr>
|
|
|
268 |
<td colspan=4>Total Amount</td>
|
| 2050 |
varun.gupt |
269 |
<td> Rs. $total_amount</td>
|
| 1262 |
varun.gupt |
270 |
</tr>
|
| 1569 |
varun.gupt |
271 |
<tr><td colspan=5><hr /></td></tr>
|
| 1262 |
varun.gupt |
272 |
</table>
|
| 1569 |
varun.gupt |
273 |
</div>
|
| 1262 |
varun.gupt |
274 |
<p>
|
|
|
275 |
Best Wishes,<br />
|
|
|
276 |
Saholic Team
|
|
|
277 |
</p>
|
|
|
278 |
</div>
|
|
|
279 |
</body>
|
|
|
280 |
</html>
|
|
|
281 |
"""
|
| 1398 |
varun.gupt |
282 |
|
| 1569 |
varun.gupt |
283 |
dt = datetime.datetime.strptime(str(order_date), "%Y-%m-%d %H:%M:%S")
|
| 1565 |
varun.gupt |
284 |
formated_order_date = dt.strftime("%A, %d. %B %Y %I:%M%p")
|
|
|
285 |
|
| 1262 |
varun.gupt |
286 |
email_header = Template(html_header).substitute(dict(order_date = formated_order_date))
|
| 2050 |
varun.gupt |
287 |
email_footer = Template(html_footer).substitute(dict(total_amount = "%.2f" % total_amount))
|
| 1262 |
varun.gupt |
288 |
|
| 1351 |
varun.gupt |
289 |
try:
|
|
|
290 |
helper_client = HelperClient().get_client()
|
| 1398 |
varun.gupt |
291 |
helper_client.saveUserEmailForSending(user_email, "", "Saholic - Order Details", email_header + html_table + email_footer, str(transaction_id), "TransactionInfo")
|
| 1351 |
varun.gupt |
292 |
return True
|
|
|
293 |
except Exception as e:
|
|
|
294 |
print e
|
|
|
295 |
return False
|
| 1262 |
varun.gupt |
296 |
|
| 483 |
rajveer |
297 |
def get_order(order_id):
|
|
|
298 |
order = Order.get_by(id=order_id)
|
|
|
299 |
if not order:
|
|
|
300 |
raise TransactionServiceException(108, "no such order")
|
|
|
301 |
|
|
|
302 |
return order
|
|
|
303 |
|
|
|
304 |
def get_orders_for_customer(customer_id, from_date, to_date, status):
|
|
|
305 |
if not customer_id:
|
|
|
306 |
raise TransactionServiceException(101, "bad customer id")
|
|
|
307 |
query = Order.query.filter(Order.customer_id == customer_id)
|
|
|
308 |
if status:
|
|
|
309 |
query = query.filter(Order.status == status)
|
|
|
310 |
if from_date:
|
|
|
311 |
query = query.filter(Order.created_timestamp >from_date)
|
|
|
312 |
if to_date:
|
|
|
313 |
query = query.filter(Order.created_timestamp < to_date)
|
|
|
314 |
return query.all()
|
|
|
315 |
|
| 1528 |
ankur.sing |
316 |
def get_order_for_customer(order_id, customer_id):
|
|
|
317 |
if not customer_id:
|
|
|
318 |
raise TransactionServiceException(101, "bad customer id")
|
|
|
319 |
order = get_order(order_id)
|
|
|
320 |
if order.customer_id != customer_id:
|
|
|
321 |
raise TransactionServiceException(108, "order: " + str(order_id) + " does not belong to customer: " + str(customer_id))
|
|
|
322 |
return order
|
|
|
323 |
|
| 483 |
rajveer |
324 |
def get_all_orders(status, from_date, to_date, warehouse_id):
|
|
|
325 |
if not warehouse_id:
|
|
|
326 |
raise TransactionServiceException(101, "bad warehouse id")
|
|
|
327 |
query = Order.query.filter(Order.warehouse_id == warehouse_id)
|
|
|
328 |
if status:
|
|
|
329 |
query = query.filter(Order.status == status)
|
|
|
330 |
if from_date:
|
|
|
331 |
query = query.filter(Order.created_timestamp >from_date)
|
|
|
332 |
if to_date:
|
|
|
333 |
query = query.filter(Order.created_timestamp < to_date)
|
|
|
334 |
return query.all()
|
|
|
335 |
|
| 1382 |
varun.gupt |
336 |
def get_returnable_orders_for_customer(customer_id, limit = None):
|
|
|
337 |
if not customer_id:
|
|
|
338 |
raise TransactionServiceException(101, "bad customer id")
|
|
|
339 |
|
|
|
340 |
query = Order.query.filter(Order.customer_id == customer_id)
|
|
|
341 |
query = query.filter(Order.status != OrderStatus.PAYMENT_FAILED)
|
|
|
342 |
query = query.filter(Order.status != OrderStatus.REJECTED)
|
|
|
343 |
query = query.filter(Order.status > OrderStatus.SHIPPED_FROM_WH)
|
|
|
344 |
query = query.filter(or_(Order.status < OrderStatus.DELIVERY_SUCCESS, Order.delivery_timestamp < datetime.datetime.now() + datetime.timedelta(hours = 48)))
|
|
|
345 |
orders = query.all()
|
|
|
346 |
order_ids = [order.id for order in orders]
|
|
|
347 |
return order_ids
|
|
|
348 |
|
|
|
349 |
def get_cancellable_orders_for_customer(customer_id, limit = None):
|
|
|
350 |
if not customer_id:
|
|
|
351 |
raise TransactionServiceException(101, "bad customer id")
|
|
|
352 |
|
|
|
353 |
query = Order.query.filter(Order.customer_id == customer_id)
|
|
|
354 |
query = query.filter(Order.status != OrderStatus.REJECTED)
|
| 1483 |
varun.gupt |
355 |
query = query.filter(Order.status >= OrderStatus.SUBMITTED_FOR_PROCESSING)
|
| 1382 |
varun.gupt |
356 |
query = query.filter(Order.status < OrderStatus.BILLED)
|
|
|
357 |
orders = query.all()
|
|
|
358 |
return [order.id for order in orders]
|
|
|
359 |
|
| 995 |
varun.gupt |
360 |
def get_orders_by_billing_date(status, start_billing_date, end_billing_date, warehouse_id):
|
|
|
361 |
if not warehouse_id:
|
|
|
362 |
raise TransactionServiceException(101, "bad warehouse id")
|
|
|
363 |
|
|
|
364 |
query = Order.query.filter(Order.warehouse_id == warehouse_id)
|
|
|
365 |
|
|
|
366 |
if status:
|
|
|
367 |
query = query.filter(Order.status == status)
|
|
|
368 |
|
|
|
369 |
if start_billing_date:
|
|
|
370 |
query = query.filter(Order.billing_timestamp >= start_billing_date)
|
|
|
371 |
|
|
|
372 |
if end_billing_date:
|
|
|
373 |
query = query.filter(Order.billing_timestamp <= end_billing_date)
|
|
|
374 |
|
|
|
375 |
return query.all()
|
|
|
376 |
|
| 1528 |
ankur.sing |
377 |
def get_orders_for_transaction(transaction_id, customer_id):
|
|
|
378 |
orders = Order.query.filter_by(transaction_id=transaction_id, customer_id=customer_id).all()
|
|
|
379 |
|
| 1208 |
chandransh |
380 |
if not orders:
|
|
|
381 |
raise TransactionServiceException(101, "No order for the transaction")
|
|
|
382 |
return orders
|
|
|
383 |
|
| 1405 |
ankur.sing |
384 |
def get_undelivered_orders(provider_id, warehouse_id):
|
|
|
385 |
query = Order.query
|
|
|
386 |
if provider_id != -1:
|
|
|
387 |
query = query.filter_by(Order.logistics_provider_id == provider_id)
|
|
|
388 |
if warehouse_id != -1:
|
|
|
389 |
query = query.filter(Order.warehouse_id == warehouse_id)
|
|
|
390 |
query = query.filter(and_(Order.status != OrderStatus.DELIVERY_SUCCESS, Order.status != OrderStatus.FAILED, \
|
|
|
391 |
Order.status > OrderStatus.SHIPPED_FROM_WH))
|
| 1479 |
ankur.sing |
392 |
#Get last midnight time instead of current time as courier company would have sent the
|
|
|
393 |
#delivery records updated at least till midnight and not by the current time.
|
|
|
394 |
time_format = "%Y-%m-%d %H:%M:%S.%f"
|
|
|
395 |
t = time.strptime(str(datetime.datetime.now()), time_format)
|
|
|
396 |
midnightTime = datetime.datetime(*t[:3])
|
|
|
397 |
query = query.filter(Order.expected_delivery_time < midnightTime)
|
| 1405 |
ankur.sing |
398 |
orders = query.all()
|
|
|
399 |
return orders
|
|
|
400 |
|
| 1208 |
chandransh |
401 |
def get_line_items_for_order(order_id):
|
|
|
402 |
query = LineItem.query.filter(LineItem.order_id == order_id)
|
|
|
403 |
return query.all()
|
|
|
404 |
|
| 483 |
rajveer |
405 |
def change_order_status(self, orderId, status, description):
|
|
|
406 |
order = get_order(orderId)
|
|
|
407 |
order.status = status
|
|
|
408 |
order.statusDescription = description
|
|
|
409 |
session.commit()
|
|
|
410 |
return True
|
|
|
411 |
|
| 921 |
rajveer |
412 |
def accept_order(self, orderId):
|
|
|
413 |
order = get_order(orderId)
|
|
|
414 |
order.status = OrderStatus.ACCEPTED
|
|
|
415 |
order.statusDescription = "Order Accepted"
|
|
|
416 |
order.accepted_timestamp = datetime.datetime.now()
|
|
|
417 |
session.commit()
|
|
|
418 |
return True
|
|
|
419 |
|
|
|
420 |
def bill_order(self, orderId):
|
|
|
421 |
order = get_order(orderId)
|
|
|
422 |
order.status = OrderStatus.BILLED
|
|
|
423 |
order.statusDescription = "Order Billed"
|
|
|
424 |
order.billing_timestamp = datetime.datetime.now()
|
|
|
425 |
session.commit()
|
| 1208 |
chandransh |
426 |
return True
|
| 921 |
rajveer |
427 |
|
| 1149 |
chandransh |
428 |
def add_billing_details(orderId, invoice_number, billed_by):
|
| 494 |
rajveer |
429 |
order = Order.get_by(id=orderId)
|
|
|
430 |
if not order:
|
|
|
431 |
raise TransactionServiceException(101, "No order found for the givem orderid")
|
|
|
432 |
order.invoice_number = invoice_number
|
|
|
433 |
order.billed_by = billed_by
|
|
|
434 |
session.commit()
|
|
|
435 |
return True
|
| 759 |
chandransh |
436 |
|
| 1149 |
chandransh |
437 |
def add_jacket_number(orderId, jacket_number):
|
|
|
438 |
order = Order.get_by(id=orderId)
|
|
|
439 |
if not order:
|
|
|
440 |
raise TransactionServiceException(101, "No order found for the givem orderid")
|
|
|
441 |
order.jacket_number = jacket_number
|
|
|
442 |
session.commit()
|
|
|
443 |
return True
|
|
|
444 |
|
| 1220 |
chandransh |
445 |
def batch_orders(warehouseId):
|
|
|
446 |
if not warehouseId:
|
|
|
447 |
raise TransactionServiceException(101, "bad warehouse id")
|
| 1225 |
chandransh |
448 |
|
|
|
449 |
batchno_generator = BatchNoGenerator()
|
|
|
450 |
session.commit()
|
| 1220 |
chandransh |
451 |
query = Order.query.filter_by(warehouse_id=warehouseId)
|
|
|
452 |
query = query.filter_by(status=OrderStatus.SUBMITTED_FOR_PROCESSING)
|
|
|
453 |
query = query.order_by(Order.created_timestamp)
|
|
|
454 |
pending_orders = query.all()
|
| 1225 |
chandransh |
455 |
|
| 1220 |
chandransh |
456 |
serial_no = 1
|
|
|
457 |
for order in pending_orders:
|
| 1276 |
chandransh |
458 |
order.batchNo = batchno_generator.id
|
| 1220 |
chandransh |
459 |
order.serialNo = serial_no
|
|
|
460 |
serial_no += 1
|
|
|
461 |
session.commit()
|
|
|
462 |
pending_orders = query.all()
|
|
|
463 |
return pending_orders
|
|
|
464 |
|
|
|
465 |
def order_outofstock(orderId):
|
| 1911 |
chandransh |
466 |
'''
|
|
|
467 |
Mark order as out of stock
|
|
|
468 |
'''
|
| 1220 |
chandransh |
469 |
order = get_order(orderId)
|
|
|
470 |
order.status = OrderStatus.INVENTORY_LOW
|
|
|
471 |
order.statusDescription = "Low Inventory"
|
|
|
472 |
order.outofstock_timestamp = datetime.datetime.now()
|
|
|
473 |
session.commit()
|
|
|
474 |
return True
|
|
|
475 |
|
| 759 |
chandransh |
476 |
def mark_orders_as_manifested(warehouse_id, provider_id):
|
|
|
477 |
try:
|
| 986 |
chandransh |
478 |
current_timestamp = datetime.datetime.now()
|
| 852 |
chandransh |
479 |
orders = Order.query.filter_by(warehouse_id = warehouse_id, logistics_provider_id = provider_id, status = OrderStatus.BILLED).all()
|
| 759 |
chandransh |
480 |
for order in orders:
|
|
|
481 |
order.status = OrderStatus.SHIPPED_FROM_WH
|
| 987 |
chandransh |
482 |
order.statusDescription = "Order shipped from warehouse"
|
| 986 |
chandransh |
483 |
order.shipping_timestamp = current_timestamp
|
| 759 |
chandransh |
484 |
session.commit()
|
|
|
485 |
return True
|
|
|
486 |
except:
|
|
|
487 |
return False
|
| 1113 |
chandransh |
488 |
|
|
|
489 |
def mark_orders_as_picked_up(provider_id, pickup_details):
|
|
|
490 |
for awb, pickup_timestamp in pickup_details.iteritems():
|
| 1135 |
chandransh |
491 |
order = Order.query.filter_by(airwaybill_no=awb, logistics_provider_id = provider_id).first()
|
| 2684 |
chandransh |
492 |
if order == None or order.status != OrderStatus.SHIPPED_FROM_WH:
|
| 2220 |
chandransh |
493 |
#raise TransactionServiceException(102, "No order found for the awb: " + awb)
|
|
|
494 |
continue
|
| 1113 |
chandransh |
495 |
order.status = OrderStatus.SHIPPED_TO_LOGST
|
|
|
496 |
order.statusDescription = "Order picked up by Courier Company"
|
| 1246 |
chandransh |
497 |
order.pickup_timestamp = pickup_timestamp
|
| 1113 |
chandransh |
498 |
session.commit()
|
| 1217 |
chandransh |
499 |
|
| 1132 |
chandransh |
500 |
current_time = datetime.datetime.now()
|
|
|
501 |
to_datetime = datetime.datetime(current_time.year, current_time.month, current_time.day)
|
| 1246 |
chandransh |
502 |
from_datetime = to_datetime - datetime.timedelta(2)
|
| 1132 |
chandransh |
503 |
orders_not_picked_up = Order.query.filter_by(logistics_provider_id = provider_id).filter_by(status=OrderStatus.SHIPPED_FROM_WH).filter(Order.shipping_timestamp >= from_datetime).filter(Order.shipping_timestamp <= to_datetime).all()
|
| 1113 |
chandransh |
504 |
return orders_not_picked_up
|
|
|
505 |
|
| 1132 |
chandransh |
506 |
def mark_orders_as_delivered(provider_id, delivered_orders):
|
|
|
507 |
for awb, detail in delivered_orders.iteritems():
|
| 1135 |
chandransh |
508 |
order = Order.query.filter_by(airwaybill_no=awb, logistics_provider_id = provider_id).first()
|
| 2684 |
chandransh |
509 |
if order == None or order.status not in [OrderStatus.SHIPPED_FROM_WH, OrderStatus.SHIPPED_TO_LOGST]:
|
| 1911 |
chandransh |
510 |
continue
|
|
|
511 |
#raise TransactionServiceException(103, "No order found for the awb:" + awb)
|
| 1132 |
chandransh |
512 |
order.status = OrderStatus.DELIVERY_SUCCESS
|
|
|
513 |
order.statusDescription = "Order delivered"
|
|
|
514 |
order.delivery_timestamp, order.receiver = detail.split('|')
|
|
|
515 |
session.commit()
|
|
|
516 |
|
| 1135 |
chandransh |
517 |
def mark_orders_as_failed(provider_id, returned_orders):
|
|
|
518 |
for awb, detail in returned_orders.iteritems():
|
|
|
519 |
order = Order.query.filter_by(airwaybill_no=awb, logistics_provider_id = provider_id).first()
|
| 2684 |
chandransh |
520 |
if order == None or order.status not in [OrderStatus.SHIPPED_FROM_WH, OrderStatus.SHIPPED_TO_LOGST]:
|
|
|
521 |
continue
|
|
|
522 |
#raise TransactionServiceException(103, "No order found for the awb:" + awb)
|
|
|
523 |
order.status = OrderStatus.SALES_RETURN_IN_TRANSIT
|
| 1135 |
chandransh |
524 |
order.delivery_timestamp, reason = detail.split('|')
|
|
|
525 |
order.statusDescription = "Order Returned to Origin:" + reason
|
| 1911 |
chandransh |
526 |
session.commit()
|
| 1135 |
chandransh |
527 |
|
| 1246 |
chandransh |
528 |
def update_non_delivery_reason(provider_id, undelivered_orders):
|
|
|
529 |
for awb, reason in undelivered_orders.iteritems():
|
|
|
530 |
order = Order.query.filter_by(airwaybill_no=awb, logistics_provider_id = provider_id).first()
|
|
|
531 |
if order == None:
|
| 2684 |
chandransh |
532 |
continue
|
|
|
533 |
#raise TransactionServiceException(103, "No order found for the awb:" + awb)
|
| 1246 |
chandransh |
534 |
order.statusDescription = reason
|
|
|
535 |
session.commit()
|
|
|
536 |
|
| 483 |
rajveer |
537 |
def get_alerts(order_id, all):
|
|
|
538 |
query = Alerts.query.filter_by(order_id=order_id)
|
|
|
539 |
if not all:
|
|
|
540 |
query = query.filter_by(valid=True)
|
|
|
541 |
return query.all()
|
|
|
542 |
|
|
|
543 |
def set_alert(order_id, unset, type, comment):
|
|
|
544 |
#get existing alert
|
|
|
545 |
if unset:
|
|
|
546 |
query = Alerts.query.filter_by(order_id=order_id)
|
|
|
547 |
query = query.filter_by(type=type)
|
|
|
548 |
alerts = query.all()
|
|
|
549 |
for alert in alerts:
|
|
|
550 |
if alert.valid:
|
|
|
551 |
alert.valid = False
|
|
|
552 |
alert.comment = comment
|
|
|
553 |
alert.time_unset = datetime.datetime.now()
|
|
|
554 |
else:
|
|
|
555 |
alert = Alerts()
|
|
|
556 |
alert.order_id = order_id
|
|
|
557 |
alert.valid = True
|
|
|
558 |
alert.type = type
|
|
|
559 |
alert.comment = comment
|
|
|
560 |
alert.time_set = datetime.datetime.now()
|
| 766 |
rajveer |
561 |
session.commit()
|
|
|
562 |
|
| 1596 |
ankur.sing |
563 |
def get_valid_order_count():
|
|
|
564 |
return Order.query.filter(Order.status >= OrderStatus.SUBMITTED_FOR_PROCESSING).count()
|
|
|
565 |
|
| 1627 |
ankur.sing |
566 |
def get_cust_count_with_successful_txn():
|
|
|
567 |
return session.query(Order.customer_id).filter(Order.status >= OrderStatus.SUBMITTED_FOR_PROCESSING).distinct().count()
|
|
|
568 |
|
| 1730 |
ankur.sing |
569 |
def get_valid_orders_amount_range():
|
| 1627 |
ankur.sing |
570 |
orders = Order.query.filter(Order.status >= OrderStatus.SUBMITTED_FOR_PROCESSING).all()
|
|
|
571 |
order_amounts = [o.total_amount for o in orders]
|
| 1730 |
ankur.sing |
572 |
return [min(order_amounts), max(order_amounts)]
|
| 1627 |
ankur.sing |
573 |
|
| 1886 |
ankur.sing |
574 |
def get_valid_orders(limit):
|
|
|
575 |
query = Order.query.filter(Order.status >= OrderStatus.SUBMITTED_FOR_PROCESSING)
|
|
|
576 |
query = query.order_by(desc(Order.created_timestamp))
|
|
|
577 |
if limit > 0:
|
|
|
578 |
query = query.limit(limit)
|
|
|
579 |
elif limit < 0:
|
|
|
580 |
raise TransactionServiceException(101, "Invalid argument limit " + limit)
|
|
|
581 |
return query.all()
|
|
|
582 |
|
| 2536 |
chandransh |
583 |
def toggle_doa_flag(order_id):
|
|
|
584 |
order = get_order(order_id)
|
|
|
585 |
if(order.doaFlag):
|
|
|
586 |
order.doaFlag = False
|
|
|
587 |
else:
|
|
|
588 |
order.doaFlag = True
|
|
|
589 |
session.commit()
|
|
|
590 |
return order.doaFlag
|
|
|
591 |
|
|
|
592 |
def request_pickup_number(order_id):
|
|
|
593 |
order = get_order(order_id)
|
|
|
594 |
if order.status != OrderStatus.DELIVERY_SUCCESS and order.status != OrderStatus.DOA_PICKUP_REQUESTED:
|
|
|
595 |
return False
|
|
|
596 |
from_user = 'cnc.center@shop2020.in'
|
|
|
597 |
from_pwd = '5h0p2o2o'
|
|
|
598 |
|
|
|
599 |
subject = "Pickup request"
|
|
|
600 |
try:
|
|
|
601 |
catalog_client = InventoryClient().get_client()
|
|
|
602 |
warehouse = catalog_client.getWarehouse(order.warehouse_id)
|
|
|
603 |
|
|
|
604 |
logistics_client = LogisticsClient().get_client()
|
|
|
605 |
provider = logistics_client.getProvider(order.logistics_provider_id)
|
|
|
606 |
to_addr = provider.email
|
|
|
607 |
raw_message = '''
|
|
|
608 |
Dear Sir/Madam,
|
|
|
609 |
|
| 2559 |
chandransh |
610 |
Kindly arrange a pickup today from %(customer_city)s. Pickup and delivery addresses are mentioned below.
|
| 2536 |
chandransh |
611 |
|
|
|
612 |
Pickup CODE: %(provider_code)s
|
|
|
613 |
Total Weight: %(order_weight)s kg
|
|
|
614 |
|
| 2559 |
chandransh |
615 |
Pickup Address:
|
| 2536 |
chandransh |
616 |
|
|
|
617 |
%(customer_address)s
|
|
|
618 |
|
|
|
619 |
Delivery Address:
|
|
|
620 |
|
|
|
621 |
%(warehouse_executive)s
|
|
|
622 |
%(warehouse_address)s
|
|
|
623 |
PIN %(warehouse_pin)s
|
|
|
624 |
Phone: %(warehouse_phone)s
|
|
|
625 |
|
|
|
626 |
Thanks and Regards
|
|
|
627 |
Anand Sinha
|
|
|
628 |
'''
|
| 2559 |
chandransh |
629 |
message = dedent(raw_message) % { 'customer_city' : order.customer_city,
|
|
|
630 |
'provider_code' : provider.accountNo,
|
| 2536 |
chandransh |
631 |
'order_weight' : str(order.total_weight),
|
|
|
632 |
'customer_address' : __get_order_address(order),
|
|
|
633 |
'warehouse_executive' : 'Parmod Kumar',
|
|
|
634 |
'warehouse_address' : warehouse.location,
|
|
|
635 |
'warehouse_pin' : warehouse.pincode,
|
|
|
636 |
'warehouse_phone': '9971573026'}
|
|
|
637 |
mail(from_user, from_pwd, to_addr, subject, message)
|
|
|
638 |
order.status = OrderStatus.DOA_PICKUP_REQUESTED
|
|
|
639 |
order.statusDescription = "Pick up requested for DOA"
|
|
|
640 |
session.commit()
|
|
|
641 |
return True
|
|
|
642 |
except Exception as e:
|
|
|
643 |
print sys.exc_info()[0]
|
|
|
644 |
return False
|
|
|
645 |
|
|
|
646 |
def authorize_pickup(order_id, pickup_number):
|
|
|
647 |
order = get_order(order_id)
|
|
|
648 |
if order.status != OrderStatus.DOA_PICKUP_REQUESTED:
|
|
|
649 |
return False
|
|
|
650 |
from_user = 'help@shop2020.in'
|
|
|
651 |
from_pwd = '5h0p2o2o'
|
|
|
652 |
subject = 'Pickup details'
|
|
|
653 |
filename = "/tmp/" + str(order.id) +".pdf"
|
|
|
654 |
try:
|
|
|
655 |
catalog_client = InventoryClient().get_client()
|
|
|
656 |
warehouse = catalog_client.getWarehouse(order.warehouse_id)
|
|
|
657 |
|
|
|
658 |
logistics_client = LogisticsClient().get_client()
|
|
|
659 |
provider = logistics_client.getProvider(order.logistics_provider_id)
|
|
|
660 |
provider_name = provider.name
|
|
|
661 |
|
|
|
662 |
to_addr = order.customer_email
|
|
|
663 |
today = date.today().strftime("%d-%B-%Y (%A)")
|
|
|
664 |
|
|
|
665 |
raw_message = '''
|
|
|
666 |
Dear Mr. %(customer_name)s,
|
|
|
667 |
|
|
|
668 |
In reference to the return of order ID %(order_id)d. We request you to kindly pack the box properly with bubble wrap, and send it along with the original "DOA" certificate and the invoice.
|
|
|
669 |
Take a printout of the attachment in this mail which contains the delivery address and paste it on the packed parcel.
|
|
|
670 |
We have instructed %(provider_name)s to pick up the material from the below mentioned address on %(date)s. %(provider_name)s pickup request number is %(pickup_request_no)s. Kindly keep the parcel ready.
|
|
|
671 |
|
|
|
672 |
Pickup will happen from the below mentioned address:-
|
|
|
673 |
|
|
|
674 |
%(customer_address)s
|
|
|
675 |
|
|
|
676 |
Do let us know in case of any concerns
|
|
|
677 |
|
|
|
678 |
Thanks & Regards
|
|
|
679 |
Saholic Team
|
|
|
680 |
'''
|
|
|
681 |
message = dedent(raw_message) % {'customer_name' : order.customer_name,
|
|
|
682 |
'order_id' : order_id,
|
|
|
683 |
'provider_name' : provider_name,
|
|
|
684 |
'date': today,
|
|
|
685 |
'pickup_request_no' : pickup_number,
|
|
|
686 |
'customer_address' : __get_order_address(order)}
|
|
|
687 |
print message
|
|
|
688 |
__generate_return_advice(order, warehouse, provider, filename)
|
|
|
689 |
mail(from_user, from_pwd, to_addr, subject, message, get_attachment_part(filename))
|
|
|
690 |
order.pickupRequestNo = pickup_number
|
|
|
691 |
order.status = OrderStatus.DOA_RETURN_AUTHORIZED
|
|
|
692 |
order.statusDescription = "DOA pick up authorized"
|
|
|
693 |
session.commit()
|
|
|
694 |
return True
|
|
|
695 |
except Exception as e:
|
|
|
696 |
print sys.exc_info()[0]
|
|
|
697 |
return False
|
|
|
698 |
finally:
|
|
|
699 |
if os.path.exists(filename):
|
|
|
700 |
os.remove(filename)
|
|
|
701 |
|
| 2616 |
chandransh |
702 |
def receive_return(order_id):
|
| 2591 |
chandransh |
703 |
order = get_order(order_id)
|
| 2616 |
chandransh |
704 |
if order.status in [OrderStatus.DOA_RETURN_AUTHORIZED, OrderStatus.DOA_RETURN_IN_TRANSIT]:
|
|
|
705 |
order.status = OrderStatus.DOA_RECEIVED
|
|
|
706 |
order.statusDescription = "DOA package received"
|
|
|
707 |
session.commit()
|
|
|
708 |
elif order.status == OrderStatus.SALES_RETURN_IN_TRANSIT :
|
|
|
709 |
order.status = OrderStatus.SALES_RET_RECEIVED
|
|
|
710 |
order.statusDescription = "Sales return received"
|
|
|
711 |
session.commit()
|
|
|
712 |
else:
|
| 2591 |
chandransh |
713 |
return False
|
|
|
714 |
return True
|
|
|
715 |
|
|
|
716 |
def validate_doa(order_id, is_valid):
|
|
|
717 |
order = get_order(order_id)
|
| 2599 |
chandransh |
718 |
if order.status != OrderStatus.DOA_RECEIVED:
|
| 2591 |
chandransh |
719 |
return False
|
|
|
720 |
if is_valid:
|
| 2609 |
chandransh |
721 |
order.status = OrderStatus.DOA_CERT_VALID
|
|
|
722 |
order.statusDescription = "DOA Certificate Valid"
|
| 2591 |
chandransh |
723 |
else:
|
|
|
724 |
order.status = OrderStatus.DOA_CERT_INVALID
|
|
|
725 |
order.statusDescription = "DOA Certificate Invalid"
|
|
|
726 |
session.commit()
|
|
|
727 |
return True
|
|
|
728 |
|
| 2628 |
chandransh |
729 |
def reship_order(order_id):
|
|
|
730 |
"""
|
|
|
731 |
If the order is in SALES_RET_RECEIVED or DOA_CERT_INVALID state, it does the following:
|
|
|
732 |
1. Creates a new order for processing in the BILLED state. All billing information is saved.
|
|
|
733 |
2. Marks the current order as one of the final states SALES_RET_RESHIPPED and DOA_INVALID_RESHIPPED depending on what state the order started in.
|
|
|
734 |
|
|
|
735 |
If the order is in DOA_CERT_VALID state, it does the following:
|
|
|
736 |
1. Creates a new order for processing in the SUBMITTED_FOR_PROCESSING state.
|
|
|
737 |
2. Creates a return order for the warehouse executive to return the DOA material.
|
|
|
738 |
3. Marks the current order as the final DOA_RESHIPPED state.
|
|
|
739 |
|
|
|
740 |
Returns the id of the newly created order.
|
|
|
741 |
|
|
|
742 |
Throws an exception if the order with the given id couldn't be found.
|
|
|
743 |
|
|
|
744 |
Parameters:
|
|
|
745 |
- orderId
|
|
|
746 |
"""
|
|
|
747 |
order = get_order(order_id)
|
|
|
748 |
if order.status == OrderStatus.SALES_RET_RECEIVED:
|
|
|
749 |
new_order = __clone_order(order, True)
|
|
|
750 |
order.status = OrderStatus.SALES_RET_RESHIPPED
|
|
|
751 |
order.statusDescription = "Order Reshipped"
|
|
|
752 |
session.commit()
|
|
|
753 |
elif order.status == OrderStatus.DOA_CERT_INVALID:
|
|
|
754 |
new_order = __clone_order(order, True)
|
|
|
755 |
order.status = OrderStatus.DOA_INVALID_RESHIPPED
|
|
|
756 |
order.statusDescription = "Order Reshipped"
|
|
|
757 |
session.commit()
|
|
|
758 |
elif order.status == OrderStatus.DOA_CERT_VALID:
|
|
|
759 |
new_order = __clone_order(order, False)
|
|
|
760 |
ret_order = __create_return_order(order)
|
|
|
761 |
order.status = OrderStatus.DOA_RESHIPPED
|
|
|
762 |
order.statusDescription = "Order Reshipped"
|
|
|
763 |
session.commit()
|
|
|
764 |
else:
|
|
|
765 |
raise TransactionServiceException(112, "This order can't be reshipped")
|
|
|
766 |
|
|
|
767 |
order.new_order_id = new_order.id
|
|
|
768 |
session.commit()
|
|
|
769 |
|
|
|
770 |
return new_order.id
|
|
|
771 |
|
|
|
772 |
def refund_order(order_id):
|
|
|
773 |
"""
|
|
|
774 |
If the order is in SALES_RET_RECEIVED, DOA_CERT_VALID or DOA_CERT_INVALID state, it does the following:
|
|
|
775 |
1. Creates a refund request for batch processing.
|
|
|
776 |
2. Creates a return order for the warehouse executive to return the shipped material.
|
|
|
777 |
3. Marks the current order as SALES_RET_REFUNDED, DOA_VALID_REFUNDED or DOA_INVALID_REFUNDED final states.
|
|
|
778 |
|
|
|
779 |
If the order is in SUBMITTED_FOR_PROCESSING or INVENTORY_LOW state, it does the following:
|
|
|
780 |
1. Creates a refund request for batch processing.
|
|
|
781 |
2. Marks the current order as the REFUNDED final state.
|
|
|
782 |
|
|
|
783 |
Returns True if it is successful.
|
|
|
784 |
|
|
|
785 |
Throws an exception if the order with the given id couldn't be found.
|
|
|
786 |
|
|
|
787 |
Parameters:
|
|
|
788 |
- orderId
|
|
|
789 |
"""
|
|
|
790 |
status_transition = {OrderStatus.SALES_RET_RECEIVED : OrderStatus.SALES_RET_REFUNDED,
|
|
|
791 |
OrderStatus.DOA_CERT_INVALID : OrderStatus.DOA_INVALID_REFUNDED,
|
|
|
792 |
OrderStatus.DOA_CERT_VALID : OrderStatus.DOA_VALID_REFUNDED,
|
|
|
793 |
OrderStatus.SUBMITTED_FOR_PROCESSING : OrderStatus.REFUNDED,
|
|
|
794 |
OrderStatus.INVENTORY_LOW : OrderStatus.REFUNDED
|
|
|
795 |
}
|
|
|
796 |
order = get_order(order_id)
|
|
|
797 |
if order.status in [OrderStatus.SALES_RET_RECEIVED, OrderStatus.DOA_CERT_INVALID, OrderStatus.DOA_CERT_VALID] :
|
|
|
798 |
__create_return_order(order)
|
|
|
799 |
__create_refund(order)
|
|
|
800 |
order.status = status_transition[order.status]
|
|
|
801 |
order.statusDescription = "Refunded"
|
|
|
802 |
elif order.status in [OrderStatus.SUBMITTED_FOR_PROCESSING, OrderStatus.INVENTORY_LOW]:
|
|
|
803 |
__create_refund(order)
|
|
|
804 |
order.status = status_transition[order.status]
|
|
|
805 |
order.statusDescription = "Refunded"
|
|
|
806 |
else:
|
|
|
807 |
raise TransactionServiceException(114, "This order can't be refunded")
|
|
|
808 |
|
|
|
809 |
session.commit()
|
|
|
810 |
return True
|
|
|
811 |
|
|
|
812 |
def __clone_order(order, should_copy_billing_info):
|
|
|
813 |
current_time = datetime.datetime.now()
|
|
|
814 |
new_order = Order()
|
|
|
815 |
new_order.customer_id = order.customer_id
|
|
|
816 |
new_order.customer_name = order.customer_name
|
|
|
817 |
new_order.customer_city = order.customer_city
|
|
|
818 |
new_order.customer_state = order.customer_state
|
|
|
819 |
new_order.customer_mobilenumber = order.customer_mobilenumber
|
|
|
820 |
new_order.customer_pincode = order.customer_pincode
|
|
|
821 |
new_order.customer_address1 = order.customer_address1
|
|
|
822 |
new_order.customer_address2 = order.customer_address2
|
|
|
823 |
new_order.customer_email = order.customer_email
|
|
|
824 |
new_order.total_amount = order.total_amount
|
|
|
825 |
new_order.total_weight = order.total_weight
|
|
|
826 |
new_order.status = OrderStatus.SUBMITTED_FOR_PROCESSING
|
|
|
827 |
new_order.statusDescription = 'Submitted for Processing'
|
|
|
828 |
new_order.created_timestamp = current_time
|
|
|
829 |
|
|
|
830 |
new_order.transaction = order.transaction
|
|
|
831 |
|
|
|
832 |
for line_item in order.lineitems:
|
|
|
833 |
litem = LineItem()
|
|
|
834 |
litem.item_id = line_item.item_id
|
|
|
835 |
litem.productGroup = line_item.productGroup
|
|
|
836 |
litem.brand = line_item.brand
|
|
|
837 |
if line_item.model_number:
|
|
|
838 |
litem.model_number = line_item.model_number
|
|
|
839 |
if line_item.model_name:
|
|
|
840 |
litem.model_name = line_item.model_name
|
|
|
841 |
if line_item.color:
|
|
|
842 |
litem.color = line_item.color
|
|
|
843 |
if line_item.extra_info:
|
|
|
844 |
litem.extra_info = line_item.extra_info
|
|
|
845 |
litem.quantity = line_item.quantity
|
|
|
846 |
litem.unit_price = line_item.unit_price
|
|
|
847 |
litem.unit_weight = line_item.unit_weight
|
|
|
848 |
litem.total_price = line_item.total_price
|
|
|
849 |
litem.total_weight = line_item.total_weight
|
|
|
850 |
litem.transfer_price = line_item.transfer_price
|
|
|
851 |
litem.order = new_order
|
|
|
852 |
|
|
|
853 |
logistics_client = LogisticsClient().get_client()
|
|
|
854 |
item_id = new_order.lineitems[0].item_id
|
|
|
855 |
logistics_info = logistics_client.getLogisticsInfo(new_order.customer_pincode, item_id) #TODO: We should be able to pass another flag to suggest ignoring the inventory situation.
|
|
|
856 |
logistics_info.deliveryTime = adjust_delivery_time(current_time, logistics_info.deliveryTime)
|
|
|
857 |
|
|
|
858 |
new_order.warehouse_id = logistics_info.warehouseId
|
|
|
859 |
new_order.logistics_provider_id = logistics_info.providerId
|
|
|
860 |
new_order.airwaybill_no = logistics_info.airway_billno
|
|
|
861 |
new_order.tracking_id = new_order.airwaybill_no
|
|
|
862 |
new_order.expected_delivery_time = current_time + datetime.timedelta(days=logistics_info.deliveryTime)
|
|
|
863 |
|
|
|
864 |
if should_copy_billing_info:
|
|
|
865 |
new_order.invoice_number = order.invoice_number
|
|
|
866 |
new_order.billed_by = order.billed_by
|
|
|
867 |
new_order.warehouse_id = order.warehouse_id
|
|
|
868 |
new_order.accepted_timestamp = current_time
|
|
|
869 |
new_order.billing_timestamp = current_time
|
|
|
870 |
new_order.status = OrderStatus.BILLED
|
|
|
871 |
new_order.statusDescription = 'Order Billed'
|
|
|
872 |
else:
|
|
|
873 |
catalog_client = InventoryClient().get_client()
|
|
|
874 |
catalog_client.reserveItemInWarehouse(item_id, logistics_info.warehouseId, new_order.lineitems[0].quantity)
|
|
|
875 |
|
|
|
876 |
item_pricing = catalog_client.getItemPricing(item_id, order.warehouse_id)
|
|
|
877 |
new_order.lineitems[0].transfer_price = item_pricing.transferPrice
|
|
|
878 |
|
|
|
879 |
return new_order
|
|
|
880 |
|
|
|
881 |
def __create_return_order(order):
|
|
|
882 |
ret_order = ReturnOrder()
|
|
|
883 |
ret_order.order_id = order.id
|
|
|
884 |
ret_order.jacket_number = order.jacket_number
|
|
|
885 |
ret_order.invoice_number = order.invoice_number
|
|
|
886 |
|
|
|
887 |
line_item = order.lineitems[0]
|
|
|
888 |
ret_order.item_id = line_item.item_id
|
|
|
889 |
ret_order.productGroup = line_item.productGroup
|
|
|
890 |
ret_order.brand = line_item.brand
|
|
|
891 |
ret_order.model_number = line_item.model_number
|
|
|
892 |
ret_order.model_name = line_item.model_name
|
|
|
893 |
ret_order.color = line_item.color
|
|
|
894 |
ret_order.total_price = line_item.total_price
|
|
|
895 |
ret_order.transfer_price = line_item.transfer_price
|
| 2631 |
chandransh |
896 |
ret_order.processed_status = False
|
| 2628 |
chandransh |
897 |
ret_order.created_timestamp = datetime.datetime.now()
|
|
|
898 |
return ret_order
|
|
|
899 |
|
|
|
900 |
def __create_refund(order):
|
|
|
901 |
pass
|
|
|
902 |
|
| 2536 |
chandransh |
903 |
def __get_order_address(order):
|
|
|
904 |
address = order.customer_name + "\n" + order.customer_address1 + "\n"
|
|
|
905 |
if order.customer_address2:
|
|
|
906 |
address = address + order.customer_address2 + "\n"
|
|
|
907 |
address = address + order.customer_city + "\n"
|
|
|
908 |
address = address + order.customer_state + "\n"
|
|
|
909 |
address = address + "PIN " + order.customer_pincode + "\n"
|
|
|
910 |
address = address + "Phone: " + order.customer_mobilenumber
|
|
|
911 |
return address
|
|
|
912 |
|
|
|
913 |
def __generate_return_advice(order, warehouse, provider, filename):
|
|
|
914 |
pdf = Canvas(filename)
|
|
|
915 |
pdf.setFont('Times-Bold',16)
|
|
|
916 |
address_text = pdf.beginText(inch, PAGE_HEIGHT - inch)
|
|
|
917 |
address_text.textLine("To")
|
|
|
918 |
address_text.textLine("Parmod Kumar")
|
|
|
919 |
for line in warehouse.location.split("\n"):
|
|
|
920 |
address_text.textLine(line)
|
|
|
921 |
address_text.textLine("PIN " + warehouse.pincode)
|
|
|
922 |
address_text.textLine("")
|
|
|
923 |
address_text.textLine("Phone: 9971573026")
|
|
|
924 |
pdf.drawText(address_text)
|
|
|
925 |
|
|
|
926 |
pdf.setFont('Times-Roman',12)
|
|
|
927 |
order_text = pdf.beginText(inch, PAGE_HEIGHT - 4 * inch)
|
|
|
928 |
order_text.textLine("Pickup CODE: " + provider.accountNo)
|
|
|
929 |
lineitem = order.lineitems[0]
|
|
|
930 |
order_text.textLine("Content : " + lineitem.brand + " " + lineitem.model_number + " " + lineitem.color)
|
|
|
931 |
order_text.textLine("Declared Value: Rs. " + str(order.total_amount))
|
|
|
932 |
order_text.textLine("Order ID : " + str(order.id))
|
|
|
933 |
pdf.drawText(order_text)
|
|
|
934 |
pdf.showPage()
|
|
|
935 |
pdf.save()
|
|
|
936 |
return filename
|
|
|
937 |
|
| 766 |
rajveer |
938 |
def close_session():
|
|
|
939 |
if session.is_active:
|
|
|
940 |
print "session is active. closing it."
|
|
|
941 |
session.close()
|