Subversion Repositories SmartDukaan

Rev

Rev 4386 | Rev 4410 | 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,\
4394 rajveer 12
    get_all_orders, change_order_status, get_alerts, add_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
 
4394 rajveer 494
    def getAlerts(self, type, status, timestamp):
483 rajveer 495
        """
496
        Parameters:
4394 rajveer 497
         - type
498
         - status
499
         - timestamp
483 rajveer 500
        """
766 rajveer 501
        try:
4394 rajveer 502
            alerts = get_alerts(type, status, timestamp)
766 rajveer 503
 
504
            t_alerts = []
505
            for alert in alerts:
506
                t_alerts.append(to_t_alert(alert)) 
507
 
508
            return t_alerts
509
        finally:
510
            close_session()
4394 rajveer 511
 
512
    def addAlert(self, type, description):
483 rajveer 513
        """
514
        Parameters:
515
         - type
4394 rajveer 516
         - description
483 rajveer 517
        """
766 rajveer 518
        try:
4394 rajveer 519
            add_alert(type, description)
766 rajveer 520
        finally:
521
            close_session()
1596 ankur.sing 522
 
523
    def getValidOrderCount(self, ):
524
        """
525
        Return the number of valid orders. (OrderStatus >= OrderStatus.SUBMITTED_FOR_PROCESSING)
526
        """
1731 ankur.sing 527
        try:
528
            return get_valid_order_count()
529
        finally:
530
            close_session()
1627 ankur.sing 531
 
532
    def getNoOfCustomersWithSuccessfulTransaction(self, ):
533
        """
534
        Returns the number of distinct customers who have done successful transactions
535
        """
1731 ankur.sing 536
        try:
537
            return get_cust_count_with_successful_txn()
538
        finally:
539
            close_session()
1627 ankur.sing 540
 
1731 ankur.sing 541
 
542
    def getValidOrdersAmountRange(self, ):
1627 ankur.sing 543
        """
1731 ankur.sing 544
        Returns the minimum and maximum amounts of a valid order. (OrderStatus >= OrderStatus.SUBMITTED_FOR_PROCESSING)
545
        List contains two values, first minimum amount and second maximum amount.
1627 ankur.sing 546
        """
1731 ankur.sing 547
        try:
548
            return get_valid_orders_amount_range()
549
        finally:
550
            close_session()
1627 ankur.sing 551
 
1886 ankur.sing 552
    def getValidOrders(self, limit):
553
        """
554
        Returns list of Orders in descending order by Order creation date. List is restricted to limit Orders.
555
        If limit is passed as 0, then all valid Orders are returned.
556
 
557
        Parameters:
558
         - limit
559
        """
560
        try:
561
            return [to_t_order(order) for order in get_valid_orders(limit)]
562
        finally:
563
            close_session()
104 ashish 564
 
2536 chandransh 565
    def toggleDOAFlag(self, orderId):
104 ashish 566
        """
2536 chandransh 567
        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.
568
        Returns the final flag status.
569
        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 570
 
132 ashish 571
        Parameters:
2536 chandransh 572
         - orderId
132 ashish 573
        """
2536 chandransh 574
        try:
575
            return toggle_doa_flag(orderId)
576
        finally:
577
            close_session()
483 rajveer 578
 
2536 chandransh 579
    def requestPickupNumber(self, orderId):
104 ashish 580
        """
2536 chandransh 581
        Sends out an email to the account manager of the original courier provider used to ship the order.
582
        If the order status was DELIVERY_SUCCESS, it is changed to be DOA_PICKUP_REQUESTED.
583
        If the order status was DOA_PICKUP_REQUESTED, it is left unchanged.
584
        For any other status, it returns false.
585
        Throws an exception if the order with the given id couldn't be found.
104 ashish 586
 
587
        Parameters:
2536 chandransh 588
         - orderId
104 ashish 589
        """
2536 chandransh 590
        try:
591
            return request_pickup_number(orderId)
592
        finally:
593
            close_session()
483 rajveer 594
 
2536 chandransh 595
    def authorizePickup(self, orderId, pickupNumber):
304 ashish 596
        """
2536 chandransh 597
        If the order status is DOA_PICKUP_REQUESTED, it does the following
598
            1. Sends out an email to the customer with the dispatch advice that he has to print as an attachment.
599
            2. Changes order status to be DOA_PICKUP_AUTHORIZED.
600
            3. Returns true
601
        If the order is any other status, it returns false.
602
        Throws an exception if the order with the given id couldn't be found.
304 ashish 603
 
604
        Parameters:
2536 chandransh 605
         - orderId
606
         - pickupNumber
304 ashish 607
        """
2536 chandransh 608
        try:
609
            return authorize_pickup(orderId, pickupNumber)
610
        finally:
611
            close_session()    
2764 chandransh 612
 
613
    def markDoasAsPickedUp(self, providerId, pickupDetails):
614
        """
615
        Marks all DOA_PICKUP_AUTHORIZED orders of the previous day for a provider as DOA_RETURN_IN_TRANSIT.
616
        Returns a list of orders that were authorized for pickup but did not appear in the pick-up report.
617
 
618
        Parameters:
619
         - providerId
620
         - pickupDetails
621
        """
622
        try:
623
            orders_not_picked_up = mark_doas_as_picked_up(providerId, pickupDetails)
624
            return [to_t_order(order) for order in orders_not_picked_up]
625
        finally:
626
            close_session()
2591 chandransh 627
 
2616 chandransh 628
    def receiveReturn(self, orderId):
2591 chandransh 629
        """
2599 chandransh 630
        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 631
        If the order is in any other state, it returns false.
632
        Throws an exception if the order with the given id couldn't be found.
633
 
634
        Parameters:
635
         - orderId
636
        """
637
        try:
2616 chandransh 638
            return receive_return(orderId)
2591 chandransh 639
        finally:
640
            close_session()
641
 
642
    def validateDoa(self, orderId, isValid):
643
        """
2599 chandransh 644
        Used to validate the DOA certificate for an order in the DOA_RECEIVED state. If the certificate is valid,
2609 chandransh 645
        the order state is changed to DOA_CERT_PENDING.
2591 chandransh 646
        If the certificate is invalid, the order state is changed to DOA_CERT_INVALID.
647
        If the order is in any other state, it returns false.
648
        Throws an exception if the order with the given id couldn't be found.
649
 
650
        Parameters:
651
         - orderId
652
         - isValid
653
        """
654
        try:
655
            return validate_doa(orderId, isValid)
656
        finally:
657
            close_session()
2628 chandransh 658
 
659
    def reshipOrder(self, orderId):
660
        """
661
        If the order is in SALES_RET_RECEIVED or DOA_CERT_INVALID state, it does the following:
662
            1. Creates a new order for processing in the BILLED state. All billing information is saved.
663
            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.
664
 
665
        If the order is in DOA_CERT_VALID state, it does the following:
666
            1. Creates a new order for processing in the SUBMITTED_FOR_PROCESSING state.
667
            2. Creates a return order for the warehouse executive to return the DOA material.
668
            3. Marks the current order as the final DOA_RESHIPPED state.
669
 
670
        Returns the id of the newly created order.
671
 
672
        Throws an exception if the order with the given id couldn't be found.
673
 
674
        Parameters:
675
         - orderId
676
        """
677
        try:
678
            return reship_order(orderId)
679
        finally:
680
            close_session()
681
 
3226 chandransh 682
    def refundOrder(self, orderId, refundedBy, reason):
2628 chandransh 683
        """
684
        If the order is in SALES_RET_RECEIVED, DOA_CERT_VALID or DOA_CERT_INVALID state, it does the following:
685
            1. Creates a refund request for batch processing.
686
            2. Creates a return order for the warehouse executive to return the shipped material.
687
            3. Marks the current order as SALES_RET_REFUNDED, DOA_VALID_REFUNDED or DOA_INVALID_REFUNDED final states.
688
 
689
        If the order is in SUBMITTED_FOR_PROCESSING or INVENTORY_LOW state, it does the following:
690
            1. Creates a refund request for batch processing.
3226 chandransh 691
            2. Cancels the reservation of the item in the warehouse.
692
            3. Marks the current order as the REFUNDED final state.
2628 chandransh 693
 
3226 chandransh 694
        For all COD orders, if the order is in INIT, SUBMITTED_FOR_PROCESSING or INVENTORY_LOW state, it does the following:
695
            1. Cancels the reservation of the item in the warehouse.
696
            2. Marks the current order as CANCELED.
697
 
698
        In all cases, it updates the reason for cancellation or refund and the person who performed the action.
699
 
2628 chandransh 700
        Returns True if it is successful, False otherwise.
701
 
702
        Throws an exception if the order with the given id couldn't be found.
703
 
704
        Parameters:
705
         - orderId
3226 chandransh 706
         - refundedBy
707
         - reason
2628 chandransh 708
        """
709
        try:
3226 chandransh 710
            return refund_order(orderId, refundedBy, reason)
2628 chandransh 711
        finally:
712
            close_session()
713
 
2697 chandransh 714
    def getReturnOrders(self, warehouseId, fromDate, toDate):
715
        """
716
        Get all return orders created between the from and to dates for the given warehouse.
717
        Ignores the warehouse if it is passed as -1.
718
 
719
        Parameters:
720
         - warehouseId
721
         - fromDate
722
         - toDate
723
        """
724
        try:
725
            from_date, to_date = get_fdate_tdate(fromDate, toDate)
726
            return get_return_orders(warehouseId, from_date, to_date)
727
        finally:
728
            close_session()
729
 
2700 chandransh 730
    def getReturnOrder(self, id):
731
        """
732
        Returns the ReturnOrder corresponding to the given id.
733
        Throws an exception if the return order with the given id couldn't be found.
734
 
735
        Parameters:
736
         - id
737
        """
738
        try:
739
            return get_return_order(id)
740
        finally:
741
            close_session()
742
 
2697 chandransh 743
    def processReturn(self, returnOrderId):
744
        """
745
        Marks the return order with the given id as processed. Raises an exception if no such return order exists.
746
 
747
        Parameters:
748
         - returnOrderId
749
        """
750
        try:
751
            process_return(returnOrderId)
752
        finally:
753
            close_session()
2591 chandransh 754
 
2819 chandransh 755
    def createPurchaseOrder(self, warehouseId):
756
        """
757
        Creates a purchase order corresponding to all the pending orders of the given warehouse.
758
        Returns the PO no. of the newly created purchase order.
759
 
760
        Parameters:
761
         - warehouseId
762
        """
763
        try:
764
            return create_purchase_order(warehouseId)
765
        finally:
766
            close_session()
3451 chandransh 767
 
768
    def updateWeight(self, orderId, weight):
769
        """
770
        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.
771
 
772
        Parameters:
773
         - orderId
774
         - weight
775
        """
776
        try:
777
            order = update_weight(orderId, weight)
778
            return to_t_order(order)
779
        finally:
780
            close_session()
781
 
3469 chandransh 782
    def changeItem(self, orderId, itemId):
783
        """
784
        Change the item to be shipped for this order. Also adjusts the reservation in the inventory accordingly.
785
        Currently, it also ensures that only a different color of the given item is shipped.
786
 
787
        Parameters:
788
         - orderId
789
         - itemId
790
        """
791
        try:
792
            order = change_product(orderId, itemId)
793
            return to_t_order(order)
794
        finally:
795
            close_session()
796
 
797
    def shiftToWarehouse(self, orderId, warehouseId):
798
        """
799
        Moves the given order to the given warehouse. Also adjusts the inventory reservations accordingly.
800
 
801
        Parameters:
802
         - orderId
803
         - warehouseId
804
        """
805
        try:
806
            order = change_warehouse(orderId, warehouseId)
807
            return to_t_order(order)
808
        finally:
809
            close_session()
3553 chandransh 810
 
3986 chandransh 811
    def addDelayReason(self, orderId, delayReason, furtherDelay):
3553 chandransh 812
        """
813
        Adds the given delay reason to the given order.
3986 chandransh 814
        Increases the expected delivery time of the given order by the given no. of days.
3553 chandransh 815
        Raises an exception if no order with the given id can be found.
3469 chandransh 816
 
3553 chandransh 817
        Parameters:
818
         - orderId
819
         - delayReason
820
        """
821
        try:
3986 chandransh 822
            return add_delay_reason(orderId, delayReason, furtherDelay)
3553 chandransh 823
        finally:
824
            close_session()
825
 
3956 chandransh 826
    def reconcileCodCollection(self, collectedAmountMap, xferBy, xferTxnId, xferDate):
827
        """
828
        Marks the COD orders with given AWB nos. as having been processed.
829
        Updates the captured amount for the corresponding payment.
830
 
831
        Returns a map of AWBs which were not processed and the associated reason. An AWB is not processed if:
832
        1. There is no order corresponding to an AWB number.
833
        2. The captured amount for a payment exceeds the total payment.
834
        3. The order corresponding to an AWB no. is in a state prior to DELIVERY_SUCCESS.
835
 
836
        Parameters:
837
         - collectedAmountMap
838
         - xferBy
839
         - xferTxnId
840
         - xferDate
841
        """
842
        try:
843
            return reconcile_cod_collection(collectedAmountMap, xferBy, xferTxnId, xferDate)
844
        finally:
845
            close_session()
4008 mandeep.dh 846
 
847
    def getTransactionsRequiringExtraProcessing(self, category):
848
        """
849
        Returns the list of transactions that require some extra processing and
850
        which belong to a particular category. This is currently used by CRM
851
        application.
852
        """
853
        try:
854
            return get_transactions_requiring_extra_processing(category)
855
        finally:
856
            close_session()
857
 
858
    def markTransactionAsProcessed(self, transactionId, category):
859
        """
860
        Marks a particular transaction as processed for a particular category.
861
        It essentially deletes the transaction if it is processed for a particular
862
        category. This is currently used by CRM application.
863
        """
864
        try:
865
            return mark_transaction_as_processed(transactionId, category)
866
        finally:
867
            close_session()
4018 chandransh 868
 
869
    def getItemWiseRiskyOrdersCount(self, ):
870
        """
871
        Returns a map containing the number of risky orders keyed by item id. A risky order
872
        is defined as one whose shipping date is about to expire.
873
        """
874
        try:
875
            return get_item_wise_risky_orders_count()
876
        finally:
877
            close_session()
3956 chandransh 878
 
4247 rajveer 879
    def markOrderCancellationRequestReceived(self, orderId):
880
        """
881
        Mark order as cancellation request received. If customer sends request of cancellation of
882
        a particular order, this method will be called. It will just change status of the order
883
        depending on its current status. It also records the previous status, so that we can move
884
        back to that status if cancellation request is denied.
885
 
886
        Parameters:
887
         - orderId
888
        """
889
        try:
890
            return mark_order_cancellation_request_received(orderId)
891
        finally:
892
            close_session()
893
 
894
 
4258 rajveer 895
    def markTransactionAsPaymentFlagRemoved(self, transactionId):
4247 rajveer 896
        """
4258 rajveer 897
        If we and/or payment gateway has decided to accept the payment, this method needs to be called.
898
        Changed transaction and all orders status to payment accepted.
4247 rajveer 899
 
900
        Parameters:
4258 rajveer 901
         - transactionId
4247 rajveer 902
        """
903
        try:
4259 anupam.sin 904
            return mark_transaction_as_payment_flag_removed(transactionId)
4247 rajveer 905
        finally:
906
            close_session()
4259 anupam.sin 907
 
908
    def refundTransaction(self, transactionId, refundedBy, reason):
909
        """
910
        This method is called when a flagged payment is deemed unserviceable and the corresponding orders
911
        need to be cancelled
912
 
913
        Parameters:
914
         - transactionId
915
        """
916
        try:
917
            return refund_transaction(transactionId, refundedBy, reason)
918
        finally:
919
            close_session()
4247 rajveer 920
 
921
    def markOrderCancellationRequestDenied(self, orderId):
922
        """
923
        If we decide to not to cancel order, we will move the order ro previous status.
924
 
925
        Parameters:
926
         - orderId
927
        """
928
        try:
929
            return mark_order_cancellation_request_denied(orderId)
930
        finally:
931
            close_session()
932
 
933
    def markOrderCancellationRequestConfirmed(self, orderId):
934
        """
935
        If we decide to to cancel order, CRM will call this method to move the status of order to
936
        cancellation request confirmed. After this OM will be able to cancel the order.
937
 
938
        Parameters:
939
         - orderId
940
        """
941
        try:
942
            return mark_order_cancellation_request_confirmed(orderId)
943
        finally:
944
            close_session()
945
 
4324 mandeep.dh 946
    def updateShipmentAddress(self, orderId, addressId):
947
        """
948
        Updates shipment address of an order. Delivery and shipping date estimates
949
        etc. are also updated here.
950
 
951
        Throws TransactionServiceException in case address change is not
952
        possible due to certain reasons such as new pincode in address is
953
        not serviceable etc.
954
 
955
        Parameters:
956
         - orderId
957
         - addressId
958
        """
959
        try:
960
            update_shipment_address(orderId, addressId)
961
        finally:
962
            close_session()
963
 
4285 rajveer 964
    def acceptOrdersForItemId(self, itemId, inventory):
965
        """
966
        Marks the orders as ACCEPTED for the given itemId and inventory. It also updates the accepted timestamp. If the
967
        given order is not a COD order, it also captures the payment if the same has not been captured.
968
 
969
        Parameters:
970
         - itemId
971
         - inventory
972
        """
973
        try:
974
            return accept_orders_for_item_id(itemId, inventory)
975
        finally:
976
            close_session()
977
 
4303 rajveer 978
 
979
 
4369 rajveer 980
    def markOrdersAsPORaised(self, vendorId, itemId, quantity, estimate, isReminder):
4303 rajveer 981
        """
982
        Parameters:
983
         - vendorId
984
         - itemId
985
         - quantity
986
         - estimate
4369 rajveer 987
         - isReminder
4303 rajveer 988
        """
989
        try:
4369 rajveer 990
            return mark_orders_as_po_raised(vendorId, itemId, quantity, estimate, isReminder)
4303 rajveer 991
        finally:
992
            close_session()
993
 
4369 rajveer 994
    def markOrdersAsReversalInitiated(self, vendorId, itemId, quantity, estimate, isReminder):
4303 rajveer 995
        """
996
        Parameters:
997
         - vendorId
998
         - itemId
999
         - quantity
1000
         - estimate
4369 rajveer 1001
         - isReminder
4303 rajveer 1002
        """
1003
        try:
4369 rajveer 1004
            return mark_orders_as_reversal_initiated(vendorId, itemId, quantity, estimate, isReminder)
4303 rajveer 1005
        finally:
1006
            close_session()
1007
 
4369 rajveer 1008
    def markOrdersAsNotAvailabke(self, vendorId, itemId, quantity, estimate, isReminder):
4303 rajveer 1009
        """
1010
        Parameters:
1011
         - vendorId
1012
         - itemId
1013
         - quantity
1014
         - estimate
4369 rajveer 1015
         - isReminder
4303 rajveer 1016
        """
1017
        try:
4369 rajveer 1018
            return mark_orders_as_not_available(vendorId, itemId, quantity, estimate, isReminder)
4303 rajveer 1019
        finally:
1020
            close_session()
1021
 
1022
 
4369 rajveer 1023
    def markOrdersAsTimeout(self, vendorId):
1024
        """
1025
        Parameters:
1026
         - vendorId
1027
        """
1028
        try:
1029
            return mark_orders_as_timeout(vendorId)
1030
        finally:
1031
            close_session()
4386 anupam.sin 1032
 
1033
    def getOrderForAwb(self, awb):
1034
        """
1035
        Parameters:
1036
         - AirwayBill Number
1037
        """
1038
        try:
1039
            return to_t_order(get_order_for_awb(awb))
1040
        finally:
1041
            close_session()
4369 rajveer 1042
 
1043
 
2536 chandransh 1044
    def closeSession(self, ):
1045
        close_session()
3376 rajveer 1046
 
1047
    def isAlive(self, ):
1048
        """
1049
        For checking weather service is active alive or not. It also checks connectivity with database
1050
        """
1051
        try:
1052
            return is_alive()
1053
        finally:
4247 rajveer 1054
            close_session()