Subversion Repositories SmartDukaan

Rev

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