Subversion Repositories SmartDukaan

Rev

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