Subversion Repositories SmartDukaan

Rev

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