Subversion Repositories SmartDukaan

Rev

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