Subversion Repositories SmartDukaan

Rev

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