Subversion Repositories SmartDukaan

Rev

Rev 1886 | Rev 2591 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
104 ashish 1
'''
2
Created on 29-Mar-2010
3
 
4
@author: ashish
5
'''
6
from shop2020.model.v1.order.impl import DataService
7
from shop2020.model.v1.order.impl.DataAccessors import create_transaction,\
1382 varun.gupt 8
    create_order, get_new_transaction, get_transactions_for_customer, get_transaction_status,\
9
    get_line_items_for_order, get_transaction, get_transactions_for_shopping_cart_id,\
10
    change_transaction_status, get_orders_for_customer,get_orders_for_transaction, get_order,\
11
    get_returnable_orders_for_customer, get_cancellable_orders_for_customer, get_orders_by_billing_date,\
12
    get_all_orders, change_order_status, get_alerts, set_alert, add_billing_details,\
13
    mark_orders_as_manifested, close_session, accept_order, bill_order, mark_orders_as_picked_up,\
1208 chandransh 14
    mark_orders_as_delivered, mark_orders_as_failed, add_jacket_number,\
1405 ankur.sing 15
    order_outofstock, batch_orders, update_non_delivery_reason, enqueue_transaction_info_email,\
1627 ankur.sing 16
    get_undelivered_orders, get_order_for_customer, get_valid_order_count,\
1886 ankur.sing 17
    get_cust_count_with_successful_txn, get_valid_orders_amount_range,\
2536 chandransh 18
    get_valid_orders, toggle_doa_flag, request_pickup_number, authorize_pickup
1405 ankur.sing 19
 
104 ashish 20
from shop2020.model.v1.order.impl.Convertors import to_t_transaction,\
1348 chandransh 21
    to_t_alert, to_t_order, to_t_lineitem
483 rajveer 22
from shop2020.thriftpy.model.v1.order.ttypes import Transaction, OrderStatus,\
23
    LineItem, Order
738 chandransh 24
from shop2020.utils.Utils import to_py_date, get_fdate_tdate
104 ashish 25
 
26
class OrderServiceHandler:
27
 
1249 chandransh 28
    def __init__(self, dbname='transaction'):
483 rajveer 29
        """
104 ashish 30
        Constructor
483 rajveer 31
        """
1249 chandransh 32
        DataService.initialize(dbname)
104 ashish 33
 
34
    def createTransaction(self, transaction):
35
        """
36
        Parameters:
37
         - transaction
38
        """
766 rajveer 39
        try:
40
            return create_transaction(transaction)
41
        finally:
42
            close_session()
43
 
104 ashish 44
    def getTransaction(self, id):
45
        """
46
            Get transaction methods.
47
 
48
        Parameters:
49
         - id
50
        """
766 rajveer 51
        try:
52
            transaction = get_transaction(id)
53
            return to_t_transaction(transaction)
54
        finally:
55
            close_session()
56
 
483 rajveer 57
    def getTransactionsForCustomer(self, customerId, from_date, to_date, status):
104 ashish 58
        """
59
        Parameters:
483 rajveer 60
         - customerId
104 ashish 61
         - from_date
62
         - to_date
483 rajveer 63
         - status
104 ashish 64
        """
766 rajveer 65
        try:
66
            current_from_date, current_to_date = get_fdate_tdate(from_date, to_date)
67
 
68
            transactions = get_transactions_for_customer(customerId, current_from_date, current_to_date, status)
69
            t_transaction = []
70
            for transaction in transactions:
71
                t_transaction.append(to_t_transaction(transaction))
72
            return t_transaction
73
        finally:
74
            close_session()
483 rajveer 75
 
76
    def getTransactionsForShoppingCartId(self, shoppingCartId):
77
        """
78
        Parameters:
79
            - shoppingCartId
80
        """
766 rajveer 81
        try:
82
            transactions = get_transactions_for_shopping_cart_id(shoppingCartId)
83
            t_transaction = []
84
            for transaction in transactions:
85
                t_transaction.append(to_t_transaction(transaction))
86
            return t_transaction
87
        finally:
88
            close_session()
104 ashish 89
 
483 rajveer 90
    def getTransactionStatus(self, transactionId):
104 ashish 91
        """
92
        Parameters:
483 rajveer 93
         - transactionId
94
        """
766 rajveer 95
        try:
96
            return get_transaction_status(transactionId)
97
        finally:
98
            close_session()
99
 
483 rajveer 100
    def changeTransactionStatus(self, transactionId, status, description):
101
        """
102
        Parameters:
103
         - transactionId
104 ashish 104
         - status
483 rajveer 105
         - description
106
        """
766 rajveer 107
        try:
108
            return change_transaction_status(transactionId, status, description)
109
        finally:
110
            close_session()
111
 
1528 ankur.sing 112
    def getOrdersForTransaction(self, transactionId, customerId):
483 rajveer 113
        """
1528 ankur.sing 114
        Returns list of orders for given transaction Id. Also filters based on customer Id so that
115
        only user who owns the transaction can view its order details.
116
 
483 rajveer 117
        Parameters:
118
         - transactionId
1528 ankur.sing 119
         - customerId
483 rajveer 120
        """
766 rajveer 121
        try:
1528 ankur.sing 122
            orders = get_orders_for_transaction(transactionId, customerId)
766 rajveer 123
            return [to_t_order(order) for order in orders]    
124
        finally:
125
            close_session()
126
 
483 rajveer 127
    def getAllOrders(self, status, from_date, to_date, warehouse_id):
128
        """
129
        Parameters:
130
         - status
104 ashish 131
         - from_date
132
         - to_date
483 rajveer 133
         - warehouse_id
104 ashish 134
        """
766 rajveer 135
        try:
136
            current_from_date, current_to_date = get_fdate_tdate(from_date, to_date)        
137
            orders = get_all_orders(status, current_from_date, current_to_date, warehouse_id)
138
            return [to_t_order(order) for order in orders]
139
        finally:
140
            close_session()
141
 
995 varun.gupt 142
    def getOrdersByBillingDate(self, status, start_billing_date, end_billing_date, warehouse_id):
143
        """
144
        Parameters:
145
         - status
146
         - start_billing_date
147
         - end_billing_date
148
         - warehouse_id
149
        """
150
        try:
151
            current_start_billing_date, current_end_billing_date = get_fdate_tdate(start_billing_date, end_billing_date)
152
            orders = get_orders_by_billing_date(status, current_start_billing_date, current_end_billing_date, warehouse_id)
153
            return [to_t_order(order) for order in orders]
154
        finally:
155
            close_session()
1382 varun.gupt 156
 
157
    def getReturnableOrdersForCustomer(self, customerId, limit = None):
158
        """
159
        Parameters:
160
         - customerId
161
         - limit
162
        """
163
        try:
164
            return get_returnable_orders_for_customer(customerId, limit)
165
        finally:
166
            close_session()
995 varun.gupt 167
 
1382 varun.gupt 168
    def getCancellableOrdersForCustomer(self, customerId, limit = None):
169
        """
170
        Parameters:
171
         - customerId
172
         - limit
173
        """
174
        try:
175
            return get_cancellable_orders_for_customer(customerId, limit)
176
        finally:
177
            close_session()
178
 
483 rajveer 179
    def changeOrderStatus(self, orderId, status, description):
180
        """
181
        Parameters:
182
         - orderId
183
         - status
184
         - description
185
         - 
186
        """
766 rajveer 187
        try:
188
            return change_order_status(self, orderId, status, description)
189
        finally:
190
            close_session()
921 rajveer 191
 
192
    def acceptOrder(self, orderId):
193
        """
194
        Parameters:
195
         - orderId
196
        """
197
        try:
198
            return accept_order(self, orderId)
199
        finally:
200
            close_session()
201
 
202
    def billOrder(self, orderId):
203
        """
204
        Parameters:
205
         - orderId
206
        """
207
        try:
208
            return bill_order(self, orderId)
209
        finally:
210
            close_session()
766 rajveer 211
 
483 rajveer 212
    def getOrdersForCustomer(self, customerId, from_date, to_date, status):
104 ashish 213
        """
214
        Parameters:
215
         - customerId
216
         - from_date
217
         - to_date
218
         - status
219
        """
766 rajveer 220
        try:
221
            current_from_date, current_to_date = get_fdate_tdate(from_date, to_date)
222
 
223
            orders = get_orders_for_customer(customerId, current_from_date, current_to_date, status)
224
            return [to_t_order(order) for order in orders]
225
        finally:
226
            close_session()
1528 ankur.sing 227
 
228
    def getOrderForCustomer(self, orderId, customerId):
229
        """
230
        Returns an order for the order Id. Also checks if the order belongs to the customer whose Id is passed.
231
        Throws exception if either order Id is invalid or order does not below to the customer whose Id is passed.
232
 
233
        Parameters:
234
         - customerId
235
         - orderId
236
        """
237
        try:
238
            return to_t_order(get_order_for_customer(orderId, customerId))
239
        finally:
240
            close_session()
766 rajveer 241
 
483 rajveer 242
    def getOrder(self, id):
243
        """
244
        Parameters:
245
         - id
246
        """
766 rajveer 247
        try:
248
            return to_t_order(get_order(id))
249
        finally:
250
            close_session()
251
 
483 rajveer 252
    def getLineItemsForOrder(self, orderId):
253
        """
254
        Parameters:
255
         - orderId
256
        """
766 rajveer 257
        try:
258
            lineitems = get_line_items_for_order(orderId)
259
            t_lineitems = []
260
            for lineitem in lineitems:
261
                t_lineitems.append(to_t_lineitem(lineitem))
262
            return t_lineitems
263
        finally:
264
            close_session()
1149 chandransh 265
 
266
    def addBillingDetails(self, orderId, invoice_number, billed_by):
494 rajveer 267
        """
1149 chandransh 268
        Add billing details such as the bill number and the biller to the Order.
269
 
494 rajveer 270
        Parameters:
271
         - orderId
272
         - invoice_number
273
         - billed_by
274
        """
766 rajveer 275
        try:
1149 chandransh 276
            return add_billing_details(orderId, invoice_number, billed_by)
766 rajveer 277
        finally:
278
            close_session()
1149 chandransh 279
 
280
    def addJacketNumber(self, orderId, jacketNumber):
281
        """
282
        Adds jacket number to the order. Return false if it doesn't find the order with the given ID.
283
 
284
        Parameters:
285
         - orderId
286
         - jacketNumber
287
        """
288
        try:
289
            return add_jacket_number(orderId, jacketNumber)
290
        finally:
291
            close_session()
1220 chandransh 292
 
293
    def batchOrders(self, warehouseId):
294
        """
295
        Create a batch of all the pending orders for the given warehouse.
296
        The returned list is orderd by created_timestamp.
297
        If there are no pending orders, an empty list is returned.
1208 chandransh 298
 
1220 chandransh 299
        Parameters:
300
         - warehouseId
301
        """
302
        try:
303
            pending_orders = batch_orders(warehouseId)
304
            return [to_t_order(order) for order in pending_orders]
305
        finally:
306
            close_session()
307
 
1208 chandransh 308
    def markOrderAsOutOfStock(self, orderId):
309
        """
310
        Mark the given order as out of stock. Throws an exception if the order with the given Id couldn't be found.
311
 
312
 
313
        Parameters:
314
         - orderId
315
        """
316
        try:
317
            return order_outofstock(orderId)
318
        finally:
319
            close_session()
320
 
759 chandransh 321
    def markOrdersAsManifested(self, warehouseId, providerId):
322
        """
323
        Marks all BILLED orders for a warehouse and a provider as SHIPPED_FROM_WH
324
 
325
        Parameters:
326
         - warehouseId
327
         - providerId
328
        """
766 rajveer 329
        try:
330
            return mark_orders_as_manifested(warehouseId, providerId)
331
        finally:
332
            close_session()
1113 chandransh 333
 
334
    def markOrdersAsPickedUp(self, providerId, pickupDetails):
335
        """
336
        Marks all SHIPPED_FROM_WH orders of the previous day for a provider as SHIPPED_TO_LOGISTICS.
337
        Returns a list of orders that were shipped from warehouse but did not appear in the pick-up report.
338
        Raises an exception if we encounter report for an AWB number that we did not ship.
339
 
340
        Parameters:
341
         - providerId
342
         - pickupDetails
343
        """
344
        try:
345
            orders_not_picked_up = mark_orders_as_picked_up(providerId, pickupDetails)
346
            return [to_t_order(order) for order in orders_not_picked_up]
347
        finally:
348
            close_session()
1132 chandransh 349
 
350
    def markOrdersAsDelivered(self, providerId, deliveredOrders):
351
        """
352
        Marks all orders with AWBs in the given map as delivered. Also sets the delivery timestamp and
353
        the name of the receiver.
354
        Raises an exception if we encounter report for an AWB number that we did not ship.
355
 
356
        Parameters:
357
         - providerId
358
         - deliveredOrders
359
        """
360
        try:
361
            mark_orders_as_delivered(providerId, deliveredOrders)
362
        finally:
363
            close_session()
1135 chandransh 364
 
365
    def markOrdersAsFailed(self, providerId, returnedOrders):
366
        """
367
        Mark all orders with AWBs in the given map as failed. Also sets the delivery timestamp.
368
        Raises an exception if we encounter report for an AWB number that we did not ship.
369
 
370
        Parameters:
371
         - providerId
372
         - returnedOrders
373
        """
374
        try:
375
            mark_orders_as_failed(providerId, returnedOrders)
376
        finally:
377
            close_session()
1246 chandransh 378
 
379
    def updateNonDeliveryReason(self, providerId, undeliveredOrders):
380
        """
381
        Update the status description of orders whose AWB numbers are keys of the Map.
382
 
383
        Parameters:
384
         - providerId
385
         - undelivered_orders
386
        """
387
        try:
388
            update_non_delivery_reason(providerId, undeliveredOrders)
389
        finally:
390
            close_session()
1405 ankur.sing 391
 
392
    def getUndeliveredOrders(self, providerId, warehouseId):
393
        """
394
        Returns the list of orders whose delivery time has passed but have not been
395
        delivered yet for the given provider and warehouse. To get a complete list of
396
        undelivered orders, pass them as -1.
397
        Returns an empty list if no such orders exist.
398
 
399
        Parameters:
400
         - providerId
401
         - warehouseId
402
        """
403
        try:
404
            undelivered_orders = get_undelivered_orders(providerId, warehouseId)
405
            return [to_t_order(order) for order in undelivered_orders]
406
        finally:
407
            close_session()
1351 varun.gupt 408
 
1398 varun.gupt 409
    def enqueueTransactionInfoEmail(self, transactionId):
1351 varun.gupt 410
        """
1398 varun.gupt 411
        Save the email containing order details to be sent to customer later by batch job
1351 varun.gupt 412
 
413
        Parameters:
414
         - transactionId
415
        """
416
        try:
1398 varun.gupt 417
            return enqueue_transaction_info_email(transactionId)
1351 varun.gupt 418
        finally:
419
            close_session()
420
 
483 rajveer 421
    def getAlerts(self, orderId, valid):
422
        """
423
        Parameters:
424
         - orderId
425
         - valid
426
        """
766 rajveer 427
        try:
428
            alerts = get_alerts(orderId, valid)
429
 
430
            t_alerts = []
431
 
432
            for alert in alerts:
433
                t_alerts.append(to_t_alert(alert)) 
434
 
435
            return t_alerts
436
        finally:
437
            close_session()
438
 
483 rajveer 439
    def setAlert(self, orderId, unset, type, comment):
440
        """
441
        Parameters:
442
         - orderId
443
         - unset
444
         - type
445
         - comment
446
        """
766 rajveer 447
        try:
448
            set_alert(orderId, unset, type, comment)
449
        finally:
450
            close_session()
1596 ankur.sing 451
 
452
    def getValidOrderCount(self, ):
453
        """
454
        Return the number of valid orders. (OrderStatus >= OrderStatus.SUBMITTED_FOR_PROCESSING)
455
        """
1731 ankur.sing 456
        try:
457
            return get_valid_order_count()
458
        finally:
459
            close_session()
1627 ankur.sing 460
 
461
    def getNoOfCustomersWithSuccessfulTransaction(self, ):
462
        """
463
        Returns the number of distinct customers who have done successful transactions
464
        """
1731 ankur.sing 465
        try:
466
            return get_cust_count_with_successful_txn()
467
        finally:
468
            close_session()
1627 ankur.sing 469
 
1731 ankur.sing 470
 
471
    def getValidOrdersAmountRange(self, ):
1627 ankur.sing 472
        """
1731 ankur.sing 473
        Returns the minimum and maximum amounts of a valid order. (OrderStatus >= OrderStatus.SUBMITTED_FOR_PROCESSING)
474
        List contains two values, first minimum amount and second maximum amount.
1627 ankur.sing 475
        """
1731 ankur.sing 476
        try:
477
            return get_valid_orders_amount_range()
478
        finally:
479
            close_session()
1627 ankur.sing 480
 
1886 ankur.sing 481
    def getValidOrders(self, limit):
482
        """
483
        Returns list of Orders in descending order by Order creation date. List is restricted to limit Orders.
484
        If limit is passed as 0, then all valid Orders are returned.
485
 
486
        Parameters:
487
         - limit
488
        """
489
        try:
490
            return [to_t_order(order) for order in get_valid_orders(limit)]
491
        finally:
492
            close_session()
104 ashish 493
 
2536 chandransh 494
    def toggleDOAFlag(self, orderId):
104 ashish 495
        """
2536 chandransh 496
        Toggle the DOA flag of an order. This should be used to flag an order for follow-up and unflag it when the follow-up is complete.
497
        Returns the final flag status.
498
        Throws an exception if the order with the given id couldn't be found or if the order status is not DELVIERY_SUCCESS.
483 rajveer 499
 
132 ashish 500
        Parameters:
2536 chandransh 501
         - orderId
132 ashish 502
        """
2536 chandransh 503
        try:
504
            return toggle_doa_flag(orderId)
505
        finally:
506
            close_session()
483 rajveer 507
 
2536 chandransh 508
    def requestPickupNumber(self, orderId):
104 ashish 509
        """
2536 chandransh 510
        Sends out an email to the account manager of the original courier provider used to ship the order.
511
        If the order status was DELIVERY_SUCCESS, it is changed to be DOA_PICKUP_REQUESTED.
512
        If the order status was DOA_PICKUP_REQUESTED, it is left unchanged.
513
        For any other status, it returns false.
514
        Throws an exception if the order with the given id couldn't be found.
104 ashish 515
 
516
        Parameters:
2536 chandransh 517
         - orderId
104 ashish 518
        """
2536 chandransh 519
        try:
520
            return request_pickup_number(orderId)
521
        finally:
522
            close_session()
483 rajveer 523
 
2536 chandransh 524
    def authorizePickup(self, orderId, pickupNumber):
304 ashish 525
        """
2536 chandransh 526
        If the order status is DOA_PICKUP_REQUESTED, it does the following
527
            1. Sends out an email to the customer with the dispatch advice that he has to print as an attachment.
528
            2. Changes order status to be DOA_PICKUP_AUTHORIZED.
529
            3. Returns true
530
        If the order is any other status, it returns false.
531
        Throws an exception if the order with the given id couldn't be found.
304 ashish 532
 
533
        Parameters:
2536 chandransh 534
         - orderId
535
         - pickupNumber
304 ashish 536
        """
2536 chandransh 537
        try:
538
            return authorize_pickup(orderId, pickupNumber)
539
        finally:
540
            close_session()    
541
 
542
    def closeSession(self, ):
543
        close_session()