Subversion Repositories SmartDukaan

Rev

Rev 4133 | Rev 4258 | 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,\
2819 chandransh 20
    process_return, get_return_order, mark_doas_as_picked_up,\
3451 chandransh 21
    create_purchase_order, verify_order, is_alive, get_orders_by_shipping_date,\
3956 chandransh 22
    update_weight, change_warehouse, change_product, add_delay_reason,\
4008 mandeep.dh 23
    reconcile_cod_collection, get_transactions_requiring_extra_processing,\
4133 chandransh 24
    mark_transaction_as_processed, get_item_wise_risky_orders_count,\
4247 rajveer 25
    get_orders_in_batch, get_order_count,\
26
    mark_order_cancellation_request_received, mark_order_as_payment_flag_removed,\
27
    mark_order_cancellation_request_denied
1405 ankur.sing 28
 
104 ashish 29
from shop2020.model.v1.order.impl.Convertors import to_t_transaction,\
1348 chandransh 30
    to_t_alert, to_t_order, to_t_lineitem
483 rajveer 31
from shop2020.thriftpy.model.v1.order.ttypes import Transaction, OrderStatus,\
2783 chandransh 32
    LineItem, Order, TransactionServiceException
738 chandransh 33
from shop2020.utils.Utils import to_py_date, get_fdate_tdate
104 ashish 34
 
35
class OrderServiceHandler:
36
 
3187 rajveer 37
    def __init__(self, dbname='transaction', db_hostname='localhost'):
483 rajveer 38
        """
104 ashish 39
        Constructor
483 rajveer 40
        """
3187 rajveer 41
        DataService.initialize(dbname, db_hostname)
104 ashish 42
 
43
    def createTransaction(self, transaction):
44
        """
45
        Parameters:
46
         - transaction
47
        """
766 rajveer 48
        try:
49
            return create_transaction(transaction)
50
        finally:
51
            close_session()
52
 
104 ashish 53
    def getTransaction(self, id):
54
        """
55
            Get transaction methods.
56
 
57
        Parameters:
58
         - id
59
        """
766 rajveer 60
        try:
61
            transaction = get_transaction(id)
62
            return to_t_transaction(transaction)
63
        finally:
64
            close_session()
65
 
483 rajveer 66
    def getTransactionsForCustomer(self, customerId, from_date, to_date, status):
104 ashish 67
        """
68
        Parameters:
483 rajveer 69
         - customerId
104 ashish 70
         - from_date
71
         - to_date
483 rajveer 72
         - status
104 ashish 73
        """
766 rajveer 74
        try:
75
            current_from_date, current_to_date = get_fdate_tdate(from_date, to_date)
76
 
77
            transactions = get_transactions_for_customer(customerId, current_from_date, current_to_date, status)
78
            t_transaction = []
79
            for transaction in transactions:
80
                t_transaction.append(to_t_transaction(transaction))
81
            return t_transaction
82
        finally:
83
            close_session()
483 rajveer 84
 
85
    def getTransactionsForShoppingCartId(self, shoppingCartId):
86
        """
87
        Parameters:
88
            - shoppingCartId
89
        """
766 rajveer 90
        try:
91
            transactions = get_transactions_for_shopping_cart_id(shoppingCartId)
92
            t_transaction = []
93
            for transaction in transactions:
94
                t_transaction.append(to_t_transaction(transaction))
95
            return t_transaction
96
        finally:
97
            close_session()
104 ashish 98
 
483 rajveer 99
    def getTransactionStatus(self, transactionId):
104 ashish 100
        """
101
        Parameters:
483 rajveer 102
         - transactionId
103
        """
766 rajveer 104
        try:
105
            return get_transaction_status(transactionId)
106
        finally:
107
            close_session()
108
 
483 rajveer 109
    def changeTransactionStatus(self, transactionId, status, description):
110
        """
111
        Parameters:
112
         - transactionId
104 ashish 113
         - status
483 rajveer 114
         - description
115
        """
766 rajveer 116
        try:
117
            return change_transaction_status(transactionId, status, description)
118
        finally:
119
            close_session()
120
 
1528 ankur.sing 121
    def getOrdersForTransaction(self, transactionId, customerId):
483 rajveer 122
        """
1528 ankur.sing 123
        Returns list of orders for given transaction Id. Also filters based on customer Id so that
124
        only user who owns the transaction can view its order details.
125
 
483 rajveer 126
        Parameters:
127
         - transactionId
1528 ankur.sing 128
         - customerId
483 rajveer 129
        """
766 rajveer 130
        try:
1528 ankur.sing 131
            orders = get_orders_for_transaction(transactionId, customerId)
766 rajveer 132
            return [to_t_order(order) for order in orders]    
133
        finally:
134
            close_session()
135
 
483 rajveer 136
    def getAllOrders(self, status, from_date, to_date, warehouse_id):
137
        """
138
        Parameters:
139
         - status
104 ashish 140
         - from_date
141
         - to_date
483 rajveer 142
         - warehouse_id
104 ashish 143
        """
766 rajveer 144
        try:
145
            current_from_date, current_to_date = get_fdate_tdate(from_date, to_date)        
146
            orders = get_all_orders(status, current_from_date, current_to_date, warehouse_id)
147
            return [to_t_order(order) for order in orders]
148
        finally:
149
            close_session()
4133 chandransh 150
 
151
    def getOrdersInBatch(self, statuses, offset, limit, warehouse_id):
152
        """
153
        Returns at most 'limit' orders with the given statuses for the given warehouse starting from the given offset.
154
        Pass the status as null and the limit as 0 to ignore them.
155
 
156
        Parameters:
157
         - statuses
158
         - offset
159
         - limit
160
         - warehouse_id
161
        """
162
        try:
163
            orders = get_orders_in_batch(statuses, offset, limit, warehouse_id)
164
            return [to_t_order(order) for order in orders]
165
        finally:
166
            close_session()
167
 
168
    def getOrderCount(self, statuses, warehouseId):
169
        """
170
        Returns the count of orders with the given statuses assigned to the given warehouse.
171
 
172
        Parameters:
173
         - statuses
174
         - warehouseId
175
        """
176
        try:
177
            return get_order_count(statuses, warehouseId)
178
        finally:
179
            close_session()
180
 
995 varun.gupt 181
    def getOrdersByBillingDate(self, status, start_billing_date, end_billing_date, warehouse_id):
182
        """
183
        Parameters:
184
         - status
185
         - start_billing_date
186
         - end_billing_date
187
         - warehouse_id
188
        """
189
        try:
190
            current_start_billing_date, current_end_billing_date = get_fdate_tdate(start_billing_date, end_billing_date)
191
            orders = get_orders_by_billing_date(status, current_start_billing_date, current_end_billing_date, warehouse_id)
192
            return [to_t_order(order) for order in orders]
193
        finally:
194
            close_session()
1382 varun.gupt 195
 
3451 chandransh 196
    def getOrdersByShippingDate(self, fromShippingDate, toShippingDate, providerId, warehouseId, cod):
3427 chandransh 197
        """
198
        Returns orders for a particular provider and warehouse which were shipped between the given dates.
3451 chandransh 199
        Returned orders comprise of COD orders if cod parameter is true. It comprises of prepaid orders otherwise.
200
        Pass providerId and warehouseId as -1 to ignore both these parameters.
3427 chandransh 201
 
202
        Parameters:
203
         - fromShippingDate
204
         - toShippingDate
205
         - providerId
206
         - warehouseId
3451 chandransh 207
         - cod
3427 chandransh 208
        """
209
        try:
210
            from_shipping_date, to_shipping_date = get_fdate_tdate(fromShippingDate, toShippingDate)
3451 chandransh 211
            orders = get_orders_by_shipping_date(from_shipping_date, to_shipping_date, providerId, warehouseId, cod)
3427 chandransh 212
            return [to_t_order(order) for order in orders]
213
        finally:
214
            close_session()
215
 
1382 varun.gupt 216
    def getReturnableOrdersForCustomer(self, customerId, limit = None):
217
        """
218
        Parameters:
219
         - customerId
220
         - limit
221
        """
222
        try:
223
            return get_returnable_orders_for_customer(customerId, limit)
224
        finally:
225
            close_session()
995 varun.gupt 226
 
1382 varun.gupt 227
    def getCancellableOrdersForCustomer(self, customerId, limit = None):
228
        """
229
        Parameters:
230
         - customerId
231
         - limit
232
        """
233
        try:
234
            return get_cancellable_orders_for_customer(customerId, limit)
235
        finally:
236
            close_session()
237
 
483 rajveer 238
    def changeOrderStatus(self, orderId, status, description):
239
        """
240
        Parameters:
241
         - orderId
242
         - status
243
         - description
244
         - 
245
        """
766 rajveer 246
        try:
247
            return change_order_status(self, orderId, status, description)
248
        finally:
249
            close_session()
921 rajveer 250
 
3064 chandransh 251
    def verifyOrder(self, orderId):
252
        """
253
        Marks the given order as SUBMITTED_FOR_PROCESSING and updates the verified
254
        timestamp. It is intended to be used for COD orders but can be harmlessly
255
        used for all other orders as well.
256
        Throws an exception if no such order exists.
257
 
258
        Parameters:
259
         - orderId
260
        """
261
        try:
262
            return verify_order(self, orderId)
263
        finally:
264
            close_session()
265
 
921 rajveer 266
    def acceptOrder(self, orderId):
267
        """
3064 chandransh 268
        Marks the given order as ACCEPTED and updates the accepted timestamp. If the
269
        given order is not a COD order, it also captures the payment if the same has
270
        not been captured.
271
        Throws an exception if no such order exists.
272
 
921 rajveer 273
        Parameters:
274
         - orderId
275
        """
276
        try:
277
            return accept_order(self, orderId)
278
        finally:
279
            close_session()
280
 
281
    def billOrder(self, orderId):
282
        """
283
        Parameters:
284
         - orderId
285
        """
286
        try:
287
            return bill_order(self, orderId)
288
        finally:
289
            close_session()
766 rajveer 290
 
3014 chandransh 291
    def getOrdersForCustomer(self, customerId, from_date, to_date, statuses):
104 ashish 292
        """
3014 chandransh 293
        Returns list of orders for the given customer created between the given dates and having the given statuses.
294
        Pass and empty list to ignore filtering on statuses.
295
 
104 ashish 296
        Parameters:
297
         - customerId
298
         - from_date
299
         - to_date
3014 chandransh 300
         - statuses
104 ashish 301
        """
766 rajveer 302
        try:
303
            current_from_date, current_to_date = get_fdate_tdate(from_date, to_date)
304
 
3014 chandransh 305
            orders = get_orders_for_customer(customerId, current_from_date, current_to_date, statuses)
766 rajveer 306
            return [to_t_order(order) for order in orders]
307
        finally:
308
            close_session()
1528 ankur.sing 309
 
310
    def getOrderForCustomer(self, orderId, customerId):
311
        """
312
        Returns an order for the order Id. Also checks if the order belongs to the customer whose Id is passed.
313
        Throws exception if either order Id is invalid or order does not below to the customer whose Id is passed.
314
 
315
        Parameters:
316
         - customerId
317
         - orderId
318
        """
319
        try:
320
            return to_t_order(get_order_for_customer(orderId, customerId))
321
        finally:
322
            close_session()
766 rajveer 323
 
483 rajveer 324
    def getOrder(self, id):
325
        """
326
        Parameters:
327
         - id
328
        """
766 rajveer 329
        try:
330
            return to_t_order(get_order(id))
331
        finally:
332
            close_session()
333
 
483 rajveer 334
    def getLineItemsForOrder(self, orderId):
335
        """
336
        Parameters:
337
         - orderId
338
        """
766 rajveer 339
        try:
340
            lineitems = get_line_items_for_order(orderId)
341
            t_lineitems = []
342
            for lineitem in lineitems:
343
                t_lineitems.append(to_t_lineitem(lineitem))
344
            return t_lineitems
345
        finally:
346
            close_session()
1149 chandransh 347
 
348
    def addBillingDetails(self, orderId, invoice_number, billed_by):
494 rajveer 349
        """
1149 chandransh 350
        Add billing details such as the bill number and the biller to the Order.
351
 
494 rajveer 352
        Parameters:
353
         - orderId
354
         - invoice_number
355
         - billed_by
356
        """
766 rajveer 357
        try:
1149 chandransh 358
            return add_billing_details(orderId, invoice_number, billed_by)
766 rajveer 359
        finally:
360
            close_session()
1149 chandransh 361
 
2842 chandransh 362
    def addJacketNumber(self, orderId, jacketNumber, imeiNumber, itemNumber, billedBy, billingType):
1149 chandransh 363
        """
2783 chandransh 364
        Adds jacket number and IMEI no. to the order. Doesn't update the IMEI no. if a -1 is supplied.
365
        Also marks the order as billed and sets the billing timestamp.
366
        Return false if it doesn't find the order with the given ID.
1149 chandransh 367
 
368
        Parameters:
369
         - orderId
370
         - jacketNumber
2783 chandransh 371
         - imeiNumber
372
         - itemNumber
373
         - billedBy
1149 chandransh 374
        """
375
        try:
2842 chandransh 376
            return add_jacket_number(self, orderId, jacketNumber, imeiNumber, itemNumber, billedBy, billingType)
2783 chandransh 377
        except TransactionServiceException:
378
            print "ERROR: Couldn't find the order with the given id"
379
            return False
1149 chandransh 380
        finally:
381
            close_session()
1220 chandransh 382
 
383
    def batchOrders(self, warehouseId):
384
        """
385
        Create a batch of all the pending orders for the given warehouse.
386
        The returned list is orderd by created_timestamp.
387
        If there are no pending orders, an empty list is returned.
1208 chandransh 388
 
1220 chandransh 389
        Parameters:
390
         - warehouseId
391
        """
392
        try:
393
            pending_orders = batch_orders(warehouseId)
394
            return [to_t_order(order) for order in pending_orders]
395
        finally:
396
            close_session()
397
 
1208 chandransh 398
    def markOrderAsOutOfStock(self, orderId):
399
        """
400
        Mark the given order as out of stock. Throws an exception if the order with the given Id couldn't be found.
401
 
402
 
403
        Parameters:
404
         - orderId
405
        """
406
        try:
407
            return order_outofstock(orderId)
408
        finally:
409
            close_session()
410
 
3064 chandransh 411
    def markOrdersAsManifested(self, warehouseId, providerId, cod):
759 chandransh 412
        """
3064 chandransh 413
        Depending on the third parameter, marks either all prepaid or all cod orders BILLED by the
414
        given warehouse and were picked up by the given provider as SHIPPED_FROM_WH.
759 chandransh 415
 
416
        Parameters:
417
         - warehouseId
418
         - providerId
3064 chandransh 419
         - cod
759 chandransh 420
        """
766 rajveer 421
        try:
3064 chandransh 422
            return mark_orders_as_manifested(warehouseId, providerId, cod)
766 rajveer 423
        finally:
424
            close_session()
1113 chandransh 425
 
426
    def markOrdersAsPickedUp(self, providerId, pickupDetails):
427
        """
428
        Marks all SHIPPED_FROM_WH orders of the previous day for a provider as SHIPPED_TO_LOGISTICS.
429
        Returns a list of orders that were shipped from warehouse but did not appear in the pick-up report.
430
        Raises an exception if we encounter report for an AWB number that we did not ship.
431
 
432
        Parameters:
433
         - providerId
434
         - pickupDetails
435
        """
436
        try:
437
            orders_not_picked_up = mark_orders_as_picked_up(providerId, pickupDetails)
438
            return [to_t_order(order) for order in orders_not_picked_up]
439
        finally:
440
            close_session()
1132 chandransh 441
 
442
    def markOrdersAsDelivered(self, providerId, deliveredOrders):
443
        """
444
        Marks all orders with AWBs in the given map as delivered. Also sets the delivery timestamp and
445
        the name of the receiver.
446
        Raises an exception if we encounter report for an AWB number that we did not ship.
447
 
448
        Parameters:
449
         - providerId
450
         - deliveredOrders
451
        """
452
        try:
453
            mark_orders_as_delivered(providerId, deliveredOrders)
454
        finally:
455
            close_session()
1135 chandransh 456
 
457
    def markOrdersAsFailed(self, providerId, returnedOrders):
458
        """
459
        Mark all orders with AWBs in the given map as failed. Also sets the delivery timestamp.
460
        Raises an exception if we encounter report for an AWB number that we did not ship.
461
 
462
        Parameters:
463
         - providerId
464
         - returnedOrders
465
        """
466
        try:
467
            mark_orders_as_failed(providerId, returnedOrders)
468
        finally:
469
            close_session()
1246 chandransh 470
 
471
    def updateNonDeliveryReason(self, providerId, undeliveredOrders):
472
        """
473
        Update the status description of orders whose AWB numbers are keys of the Map.
474
 
475
        Parameters:
476
         - providerId
477
         - undelivered_orders
478
        """
479
        try:
480
            update_non_delivery_reason(providerId, undeliveredOrders)
481
        finally:
482
            close_session()
1405 ankur.sing 483
 
484
    def getUndeliveredOrders(self, providerId, warehouseId):
485
        """
486
        Returns the list of orders whose delivery time has passed but have not been
487
        delivered yet for the given provider and warehouse. To get a complete list of
488
        undelivered orders, pass them as -1.
489
        Returns an empty list if no such orders exist.
490
 
491
        Parameters:
492
         - providerId
493
         - warehouseId
494
        """
495
        try:
496
            undelivered_orders = get_undelivered_orders(providerId, warehouseId)
497
            return [to_t_order(order) for order in undelivered_orders]
498
        finally:
499
            close_session()
1351 varun.gupt 500
 
1398 varun.gupt 501
    def enqueueTransactionInfoEmail(self, transactionId):
1351 varun.gupt 502
        """
1398 varun.gupt 503
        Save the email containing order details to be sent to customer later by batch job
1351 varun.gupt 504
 
505
        Parameters:
506
         - transactionId
507
        """
508
        try:
1398 varun.gupt 509
            return enqueue_transaction_info_email(transactionId)
1351 varun.gupt 510
        finally:
511
            close_session()
512
 
483 rajveer 513
    def getAlerts(self, orderId, valid):
514
        """
515
        Parameters:
516
         - orderId
517
         - valid
518
        """
766 rajveer 519
        try:
520
            alerts = get_alerts(orderId, valid)
521
 
522
            t_alerts = []
523
 
524
            for alert in alerts:
525
                t_alerts.append(to_t_alert(alert)) 
526
 
527
            return t_alerts
528
        finally:
529
            close_session()
530
 
483 rajveer 531
    def setAlert(self, orderId, unset, type, comment):
532
        """
533
        Parameters:
534
         - orderId
535
         - unset
536
         - type
537
         - comment
538
        """
766 rajveer 539
        try:
540
            set_alert(orderId, unset, type, comment)
541
        finally:
542
            close_session()
1596 ankur.sing 543
 
544
    def getValidOrderCount(self, ):
545
        """
546
        Return the number of valid orders. (OrderStatus >= OrderStatus.SUBMITTED_FOR_PROCESSING)
547
        """
1731 ankur.sing 548
        try:
549
            return get_valid_order_count()
550
        finally:
551
            close_session()
1627 ankur.sing 552
 
553
    def getNoOfCustomersWithSuccessfulTransaction(self, ):
554
        """
555
        Returns the number of distinct customers who have done successful transactions
556
        """
1731 ankur.sing 557
        try:
558
            return get_cust_count_with_successful_txn()
559
        finally:
560
            close_session()
1627 ankur.sing 561
 
1731 ankur.sing 562
 
563
    def getValidOrdersAmountRange(self, ):
1627 ankur.sing 564
        """
1731 ankur.sing 565
        Returns the minimum and maximum amounts of a valid order. (OrderStatus >= OrderStatus.SUBMITTED_FOR_PROCESSING)
566
        List contains two values, first minimum amount and second maximum amount.
1627 ankur.sing 567
        """
1731 ankur.sing 568
        try:
569
            return get_valid_orders_amount_range()
570
        finally:
571
            close_session()
1627 ankur.sing 572
 
1886 ankur.sing 573
    def getValidOrders(self, limit):
574
        """
575
        Returns list of Orders in descending order by Order creation date. List is restricted to limit Orders.
576
        If limit is passed as 0, then all valid Orders are returned.
577
 
578
        Parameters:
579
         - limit
580
        """
581
        try:
582
            return [to_t_order(order) for order in get_valid_orders(limit)]
583
        finally:
584
            close_session()
104 ashish 585
 
2536 chandransh 586
    def toggleDOAFlag(self, orderId):
104 ashish 587
        """
2536 chandransh 588
        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.
589
        Returns the final flag status.
590
        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 591
 
132 ashish 592
        Parameters:
2536 chandransh 593
         - orderId
132 ashish 594
        """
2536 chandransh 595
        try:
596
            return toggle_doa_flag(orderId)
597
        finally:
598
            close_session()
483 rajveer 599
 
2536 chandransh 600
    def requestPickupNumber(self, orderId):
104 ashish 601
        """
2536 chandransh 602
        Sends out an email to the account manager of the original courier provider used to ship the order.
603
        If the order status was DELIVERY_SUCCESS, it is changed to be DOA_PICKUP_REQUESTED.
604
        If the order status was DOA_PICKUP_REQUESTED, it is left unchanged.
605
        For any other status, it returns false.
606
        Throws an exception if the order with the given id couldn't be found.
104 ashish 607
 
608
        Parameters:
2536 chandransh 609
         - orderId
104 ashish 610
        """
2536 chandransh 611
        try:
612
            return request_pickup_number(orderId)
613
        finally:
614
            close_session()
483 rajveer 615
 
2536 chandransh 616
    def authorizePickup(self, orderId, pickupNumber):
304 ashish 617
        """
2536 chandransh 618
        If the order status is DOA_PICKUP_REQUESTED, it does the following
619
            1. Sends out an email to the customer with the dispatch advice that he has to print as an attachment.
620
            2. Changes order status to be DOA_PICKUP_AUTHORIZED.
621
            3. Returns true
622
        If the order is any other status, it returns false.
623
        Throws an exception if the order with the given id couldn't be found.
304 ashish 624
 
625
        Parameters:
2536 chandransh 626
         - orderId
627
         - pickupNumber
304 ashish 628
        """
2536 chandransh 629
        try:
630
            return authorize_pickup(orderId, pickupNumber)
631
        finally:
632
            close_session()    
2764 chandransh 633
 
634
    def markDoasAsPickedUp(self, providerId, pickupDetails):
635
        """
636
        Marks all DOA_PICKUP_AUTHORIZED orders of the previous day for a provider as DOA_RETURN_IN_TRANSIT.
637
        Returns a list of orders that were authorized for pickup but did not appear in the pick-up report.
638
 
639
        Parameters:
640
         - providerId
641
         - pickupDetails
642
        """
643
        try:
644
            orders_not_picked_up = mark_doas_as_picked_up(providerId, pickupDetails)
645
            return [to_t_order(order) for order in orders_not_picked_up]
646
        finally:
647
            close_session()
2591 chandransh 648
 
2616 chandransh 649
    def receiveReturn(self, orderId):
2591 chandransh 650
        """
2599 chandransh 651
        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 652
        If the order is in any other state, it returns false.
653
        Throws an exception if the order with the given id couldn't be found.
654
 
655
        Parameters:
656
         - orderId
657
        """
658
        try:
2616 chandransh 659
            return receive_return(orderId)
2591 chandransh 660
        finally:
661
            close_session()
662
 
663
    def validateDoa(self, orderId, isValid):
664
        """
2599 chandransh 665
        Used to validate the DOA certificate for an order in the DOA_RECEIVED state. If the certificate is valid,
2609 chandransh 666
        the order state is changed to DOA_CERT_PENDING.
2591 chandransh 667
        If the certificate is invalid, the order state is changed to DOA_CERT_INVALID.
668
        If the order is in any other state, it returns false.
669
        Throws an exception if the order with the given id couldn't be found.
670
 
671
        Parameters:
672
         - orderId
673
         - isValid
674
        """
675
        try:
676
            return validate_doa(orderId, isValid)
677
        finally:
678
            close_session()
2628 chandransh 679
 
680
    def reshipOrder(self, orderId):
681
        """
682
        If the order is in SALES_RET_RECEIVED or DOA_CERT_INVALID state, it does the following:
683
            1. Creates a new order for processing in the BILLED state. All billing information is saved.
684
            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.
685
 
686
        If the order is in DOA_CERT_VALID state, it does the following:
687
            1. Creates a new order for processing in the SUBMITTED_FOR_PROCESSING state.
688
            2. Creates a return order for the warehouse executive to return the DOA material.
689
            3. Marks the current order as the final DOA_RESHIPPED state.
690
 
691
        Returns the id of the newly created order.
692
 
693
        Throws an exception if the order with the given id couldn't be found.
694
 
695
        Parameters:
696
         - orderId
697
        """
698
        try:
699
            return reship_order(orderId)
700
        finally:
701
            close_session()
702
 
3226 chandransh 703
    def refundOrder(self, orderId, refundedBy, reason):
2628 chandransh 704
        """
705
        If the order is in SALES_RET_RECEIVED, DOA_CERT_VALID or DOA_CERT_INVALID state, it does the following:
706
            1. Creates a refund request for batch processing.
707
            2. Creates a return order for the warehouse executive to return the shipped material.
708
            3. Marks the current order as SALES_RET_REFUNDED, DOA_VALID_REFUNDED or DOA_INVALID_REFUNDED final states.
709
 
710
        If the order is in SUBMITTED_FOR_PROCESSING or INVENTORY_LOW state, it does the following:
711
            1. Creates a refund request for batch processing.
3226 chandransh 712
            2. Cancels the reservation of the item in the warehouse.
713
            3. Marks the current order as the REFUNDED final state.
2628 chandransh 714
 
3226 chandransh 715
        For all COD orders, if the order is in INIT, SUBMITTED_FOR_PROCESSING or INVENTORY_LOW state, it does the following:
716
            1. Cancels the reservation of the item in the warehouse.
717
            2. Marks the current order as CANCELED.
718
 
719
        In all cases, it updates the reason for cancellation or refund and the person who performed the action.
720
 
2628 chandransh 721
        Returns True if it is successful, False otherwise.
722
 
723
        Throws an exception if the order with the given id couldn't be found.
724
 
725
        Parameters:
726
         - orderId
3226 chandransh 727
         - refundedBy
728
         - reason
2628 chandransh 729
        """
730
        try:
3226 chandransh 731
            return refund_order(orderId, refundedBy, reason)
2628 chandransh 732
        finally:
733
            close_session()
734
 
2697 chandransh 735
    def getReturnOrders(self, warehouseId, fromDate, toDate):
736
        """
737
        Get all return orders created between the from and to dates for the given warehouse.
738
        Ignores the warehouse if it is passed as -1.
739
 
740
        Parameters:
741
         - warehouseId
742
         - fromDate
743
         - toDate
744
        """
745
        try:
746
            from_date, to_date = get_fdate_tdate(fromDate, toDate)
747
            return get_return_orders(warehouseId, from_date, to_date)
748
        finally:
749
            close_session()
750
 
2700 chandransh 751
    def getReturnOrder(self, id):
752
        """
753
        Returns the ReturnOrder corresponding to the given id.
754
        Throws an exception if the return order with the given id couldn't be found.
755
 
756
        Parameters:
757
         - id
758
        """
759
        try:
760
            return get_return_order(id)
761
        finally:
762
            close_session()
763
 
2697 chandransh 764
    def processReturn(self, returnOrderId):
765
        """
766
        Marks the return order with the given id as processed. Raises an exception if no such return order exists.
767
 
768
        Parameters:
769
         - returnOrderId
770
        """
771
        try:
772
            process_return(returnOrderId)
773
        finally:
774
            close_session()
2591 chandransh 775
 
2819 chandransh 776
    def createPurchaseOrder(self, warehouseId):
777
        """
778
        Creates a purchase order corresponding to all the pending orders of the given warehouse.
779
        Returns the PO no. of the newly created purchase order.
780
 
781
        Parameters:
782
         - warehouseId
783
        """
784
        try:
785
            return create_purchase_order(warehouseId)
786
        finally:
787
            close_session()
3451 chandransh 788
 
789
    def updateWeight(self, orderId, weight):
790
        """
791
        Set the weight of the given order to the provided value. Will attempt to update the weight of the item in the catalog as well.
792
 
793
        Parameters:
794
         - orderId
795
         - weight
796
        """
797
        try:
798
            order = update_weight(orderId, weight)
799
            return to_t_order(order)
800
        finally:
801
            close_session()
802
 
3469 chandransh 803
    def changeItem(self, orderId, itemId):
804
        """
805
        Change the item to be shipped for this order. Also adjusts the reservation in the inventory accordingly.
806
        Currently, it also ensures that only a different color of the given item is shipped.
807
 
808
        Parameters:
809
         - orderId
810
         - itemId
811
        """
812
        try:
813
            order = change_product(orderId, itemId)
814
            return to_t_order(order)
815
        finally:
816
            close_session()
817
 
818
    def shiftToWarehouse(self, orderId, warehouseId):
819
        """
820
        Moves the given order to the given warehouse. Also adjusts the inventory reservations accordingly.
821
 
822
        Parameters:
823
         - orderId
824
         - warehouseId
825
        """
826
        try:
827
            order = change_warehouse(orderId, warehouseId)
828
            return to_t_order(order)
829
        finally:
830
            close_session()
3553 chandransh 831
 
3986 chandransh 832
    def addDelayReason(self, orderId, delayReason, furtherDelay):
3553 chandransh 833
        """
834
        Adds the given delay reason to the given order.
3986 chandransh 835
        Increases the expected delivery time of the given order by the given no. of days.
3553 chandransh 836
        Raises an exception if no order with the given id can be found.
3469 chandransh 837
 
3553 chandransh 838
        Parameters:
839
         - orderId
840
         - delayReason
841
        """
842
        try:
3986 chandransh 843
            return add_delay_reason(orderId, delayReason, furtherDelay)
3553 chandransh 844
        finally:
845
            close_session()
846
 
3956 chandransh 847
    def reconcileCodCollection(self, collectedAmountMap, xferBy, xferTxnId, xferDate):
848
        """
849
        Marks the COD orders with given AWB nos. as having been processed.
850
        Updates the captured amount for the corresponding payment.
851
 
852
        Returns a map of AWBs which were not processed and the associated reason. An AWB is not processed if:
853
        1. There is no order corresponding to an AWB number.
854
        2. The captured amount for a payment exceeds the total payment.
855
        3. The order corresponding to an AWB no. is in a state prior to DELIVERY_SUCCESS.
856
 
857
        Parameters:
858
         - collectedAmountMap
859
         - xferBy
860
         - xferTxnId
861
         - xferDate
862
        """
863
        try:
864
            return reconcile_cod_collection(collectedAmountMap, xferBy, xferTxnId, xferDate)
865
        finally:
866
            close_session()
4008 mandeep.dh 867
 
868
    def getTransactionsRequiringExtraProcessing(self, category):
869
        """
870
        Returns the list of transactions that require some extra processing and
871
        which belong to a particular category. This is currently used by CRM
872
        application.
873
        """
874
        try:
875
            return get_transactions_requiring_extra_processing(category)
876
        finally:
877
            close_session()
878
 
879
    def markTransactionAsProcessed(self, transactionId, category):
880
        """
881
        Marks a particular transaction as processed for a particular category.
882
        It essentially deletes the transaction if it is processed for a particular
883
        category. This is currently used by CRM application.
884
        """
885
        try:
886
            return mark_transaction_as_processed(transactionId, category)
887
        finally:
888
            close_session()
4018 chandransh 889
 
890
    def getItemWiseRiskyOrdersCount(self, ):
891
        """
892
        Returns a map containing the number of risky orders keyed by item id. A risky order
893
        is defined as one whose shipping date is about to expire.
894
        """
895
        try:
896
            return get_item_wise_risky_orders_count()
897
        finally:
898
            close_session()
3956 chandransh 899
 
4247 rajveer 900
    def markOrderCancellationRequestReceived(self, orderId):
901
        """
902
        Mark order as cancellation request received. If customer sends request of cancellation of
903
        a particular order, this method will be called. It will just change status of the order
904
        depending on its current status. It also records the previous status, so that we can move
905
        back to that status if cancellation request is denied.
906
 
907
        Parameters:
908
         - orderId
909
        """
910
        try:
911
            return mark_order_cancellation_request_received(orderId)
912
        finally:
913
            close_session()
914
 
915
 
916
    def markOrderAsPaymentFlagRemoved(self, orderId):
917
        """
918
        If we and/or payment has decided to accept the order, this method needs to be called.
919
        Changed order status and payment status
920
 
921
        Parameters:
922
         - orderId
923
        """
924
        try:
925
            return mark_order_as_payment_flag_removed(orderId)
926
        finally:
927
            close_session()
928
 
929
    def markOrderCancellationRequestDenied(self, orderId):
930
        """
931
        If we decide to not to cancel order, we will move the order ro previous status.
932
 
933
        Parameters:
934
         - orderId
935
        """
936
        try:
937
            return mark_order_cancellation_request_denied(orderId)
938
        finally:
939
            close_session()
940
 
941
    def markOrderCancellationRequestConfirmed(self, orderId):
942
        """
943
        If we decide to to cancel order, CRM will call this method to move the status of order to
944
        cancellation request confirmed. After this OM will be able to cancel the order.
945
 
946
        Parameters:
947
         - orderId
948
        """
949
        try:
950
            return mark_order_cancellation_request_confirmed(orderId)
951
        finally:
952
            close_session()
953
 
2536 chandransh 954
    def closeSession(self, ):
955
        close_session()
3376 rajveer 956
 
957
    def isAlive(self, ):
958
        """
959
        For checking weather service is active alive or not. It also checks connectivity with database
960
        """
961
        try:
962
            return is_alive()
963
        finally:
4247 rajveer 964
            close_session()