Subversion Repositories SmartDukaan

Rev

Rev 4506 | Rev 4561 | 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,\
4481 anupam.sin 33
    mark_orders_as_shipped_from_warehouse, mark_alerts_as_seen, \
4488 rajveer 34
    mark_order_doa_request_received, mark_order_doa_request_authorized,\
4495 rajveer 35
    mark_order_return_request_received, mark_order_return_request_authorized,\
4506 phani.kuma 36
    validate_return_product, get_orders_for_provider_for_status
1405 ankur.sing 37
 
104 ashish 38
from shop2020.model.v1.order.impl.Convertors import to_t_transaction,\
1348 chandransh 39
    to_t_alert, to_t_order, to_t_lineitem
483 rajveer 40
from shop2020.thriftpy.model.v1.order.ttypes import Transaction, OrderStatus,\
2783 chandransh 41
    LineItem, Order, TransactionServiceException
738 chandransh 42
from shop2020.utils.Utils import to_py_date, get_fdate_tdate
104 ashish 43
 
44
class OrderServiceHandler:
45
 
3187 rajveer 46
    def __init__(self, dbname='transaction', db_hostname='localhost'):
483 rajveer 47
        """
104 ashish 48
        Constructor
483 rajveer 49
        """
3187 rajveer 50
        DataService.initialize(dbname, db_hostname)
104 ashish 51
 
52
    def createTransaction(self, transaction):
53
        """
54
        Parameters:
55
         - transaction
56
        """
766 rajveer 57
        try:
58
            return create_transaction(transaction)
59
        finally:
60
            close_session()
61
 
104 ashish 62
    def getTransaction(self, id):
63
        """
64
            Get transaction methods.
65
 
66
        Parameters:
67
         - id
68
        """
766 rajveer 69
        try:
70
            transaction = get_transaction(id)
71
            return to_t_transaction(transaction)
72
        finally:
73
            close_session()
74
 
483 rajveer 75
    def getTransactionsForCustomer(self, customerId, from_date, to_date, status):
104 ashish 76
        """
77
        Parameters:
483 rajveer 78
         - customerId
104 ashish 79
         - from_date
80
         - to_date
483 rajveer 81
         - status
104 ashish 82
        """
766 rajveer 83
        try:
84
            current_from_date, current_to_date = get_fdate_tdate(from_date, to_date)
85
 
86
            transactions = get_transactions_for_customer(customerId, current_from_date, current_to_date, status)
87
            t_transaction = []
88
            for transaction in transactions:
89
                t_transaction.append(to_t_transaction(transaction))
90
            return t_transaction
91
        finally:
92
            close_session()
483 rajveer 93
 
94
    def getTransactionsForShoppingCartId(self, shoppingCartId):
95
        """
96
        Parameters:
97
            - shoppingCartId
98
        """
766 rajveer 99
        try:
100
            transactions = get_transactions_for_shopping_cart_id(shoppingCartId)
101
            t_transaction = []
102
            for transaction in transactions:
103
                t_transaction.append(to_t_transaction(transaction))
104
            return t_transaction
105
        finally:
106
            close_session()
104 ashish 107
 
483 rajveer 108
    def getTransactionStatus(self, transactionId):
104 ashish 109
        """
110
        Parameters:
483 rajveer 111
         - transactionId
112
        """
766 rajveer 113
        try:
114
            return get_transaction_status(transactionId)
115
        finally:
116
            close_session()
117
 
483 rajveer 118
    def changeTransactionStatus(self, transactionId, status, description):
119
        """
120
        Parameters:
121
         - transactionId
104 ashish 122
         - status
483 rajveer 123
         - description
124
        """
766 rajveer 125
        try:
126
            return change_transaction_status(transactionId, status, description)
127
        finally:
128
            close_session()
129
 
1528 ankur.sing 130
    def getOrdersForTransaction(self, transactionId, customerId):
483 rajveer 131
        """
1528 ankur.sing 132
        Returns list of orders for given transaction Id. Also filters based on customer Id so that
133
        only user who owns the transaction can view its order details.
134
 
483 rajveer 135
        Parameters:
136
         - transactionId
1528 ankur.sing 137
         - customerId
483 rajveer 138
        """
766 rajveer 139
        try:
1528 ankur.sing 140
            orders = get_orders_for_transaction(transactionId, customerId)
766 rajveer 141
            return [to_t_order(order) for order in orders]    
142
        finally:
143
            close_session()
144
 
483 rajveer 145
    def getAllOrders(self, status, from_date, to_date, warehouse_id):
146
        """
147
        Parameters:
148
         - status
104 ashish 149
         - from_date
150
         - to_date
483 rajveer 151
         - warehouse_id
104 ashish 152
        """
766 rajveer 153
        try:
154
            current_from_date, current_to_date = get_fdate_tdate(from_date, to_date)        
155
            orders = get_all_orders(status, current_from_date, current_to_date, warehouse_id)
156
            return [to_t_order(order) for order in orders]
157
        finally:
158
            close_session()
4133 chandransh 159
 
160
    def getOrdersInBatch(self, statuses, offset, limit, warehouse_id):
161
        """
162
        Returns at most 'limit' orders with the given statuses for the given warehouse starting from the given offset.
163
        Pass the status as null and the limit as 0 to ignore them.
164
 
165
        Parameters:
166
         - statuses
167
         - offset
168
         - limit
169
         - warehouse_id
170
        """
171
        try:
172
            orders = get_orders_in_batch(statuses, offset, limit, warehouse_id)
173
            return [to_t_order(order) for order in orders]
174
        finally:
175
            close_session()
176
 
177
    def getOrderCount(self, statuses, warehouseId):
178
        """
179
        Returns the count of orders with the given statuses assigned to the given warehouse.
180
 
181
        Parameters:
182
         - statuses
183
         - warehouseId
184
        """
185
        try:
186
            return get_order_count(statuses, warehouseId)
187
        finally:
188
            close_session()
189
 
995 varun.gupt 190
    def getOrdersByBillingDate(self, status, start_billing_date, end_billing_date, warehouse_id):
191
        """
192
        Parameters:
193
         - status
194
         - start_billing_date
195
         - end_billing_date
196
         - warehouse_id
197
        """
198
        try:
199
            current_start_billing_date, current_end_billing_date = get_fdate_tdate(start_billing_date, end_billing_date)
200
            orders = get_orders_by_billing_date(status, current_start_billing_date, current_end_billing_date, warehouse_id)
201
            return [to_t_order(order) for order in orders]
202
        finally:
203
            close_session()
1382 varun.gupt 204
 
3451 chandransh 205
    def getOrdersByShippingDate(self, fromShippingDate, toShippingDate, providerId, warehouseId, cod):
3427 chandransh 206
        """
207
        Returns orders for a particular provider and warehouse which were shipped between the given dates.
3451 chandransh 208
        Returned orders comprise of COD orders if cod parameter is true. It comprises of prepaid orders otherwise.
209
        Pass providerId and warehouseId as -1 to ignore both these parameters.
3427 chandransh 210
 
211
        Parameters:
212
         - fromShippingDate
213
         - toShippingDate
214
         - providerId
215
         - warehouseId
3451 chandransh 216
         - cod
3427 chandransh 217
        """
218
        try:
219
            from_shipping_date, to_shipping_date = get_fdate_tdate(fromShippingDate, toShippingDate)
3451 chandransh 220
            orders = get_orders_by_shipping_date(from_shipping_date, to_shipping_date, providerId, warehouseId, cod)
3427 chandransh 221
            return [to_t_order(order) for order in orders]
222
        finally:
223
            close_session()
224
 
1382 varun.gupt 225
    def getReturnableOrdersForCustomer(self, customerId, limit = None):
226
        """
227
        Parameters:
228
         - customerId
229
         - limit
230
        """
231
        try:
232
            return get_returnable_orders_for_customer(customerId, limit)
233
        finally:
234
            close_session()
995 varun.gupt 235
 
1382 varun.gupt 236
    def getCancellableOrdersForCustomer(self, customerId, limit = None):
237
        """
238
        Parameters:
239
         - customerId
240
         - limit
241
        """
242
        try:
243
            return get_cancellable_orders_for_customer(customerId, limit)
244
        finally:
245
            close_session()
246
 
483 rajveer 247
    def changeOrderStatus(self, orderId, status, description):
248
        """
249
        Parameters:
250
         - orderId
251
         - status
252
         - description
253
         - 
254
        """
766 rajveer 255
        try:
256
            return change_order_status(self, orderId, status, description)
257
        finally:
258
            close_session()
921 rajveer 259
 
3064 chandransh 260
    def verifyOrder(self, orderId):
261
        """
262
        Marks the given order as SUBMITTED_FOR_PROCESSING and updates the verified
263
        timestamp. It is intended to be used for COD orders but can be harmlessly
264
        used for all other orders as well.
265
        Throws an exception if no such order exists.
266
 
267
        Parameters:
268
         - orderId
269
        """
270
        try:
271
            return verify_order(self, orderId)
272
        finally:
273
            close_session()
274
 
921 rajveer 275
    def acceptOrder(self, orderId):
276
        """
3064 chandransh 277
        Marks the given order as ACCEPTED and updates the accepted timestamp. If the
278
        given order is not a COD order, it also captures the payment if the same has
279
        not been captured.
280
        Throws an exception if no such order exists.
281
 
921 rajveer 282
        Parameters:
283
         - orderId
284
        """
285
        try:
4285 rajveer 286
            return accept_order(orderId)
921 rajveer 287
        finally:
288
            close_session()
289
 
3014 chandransh 290
    def getOrdersForCustomer(self, customerId, from_date, to_date, statuses):
104 ashish 291
        """
3014 chandransh 292
        Returns list of orders for the given customer created between the given dates and having the given statuses.
293
        Pass and empty list to ignore filtering on statuses.
294
 
104 ashish 295
        Parameters:
296
         - customerId
297
         - from_date
298
         - to_date
3014 chandransh 299
         - statuses
104 ashish 300
        """
766 rajveer 301
        try:
302
            current_from_date, current_to_date = get_fdate_tdate(from_date, to_date)
303
 
3014 chandransh 304
            orders = get_orders_for_customer(customerId, current_from_date, current_to_date, statuses)
766 rajveer 305
            return [to_t_order(order) for order in orders]
306
        finally:
307
            close_session()
1528 ankur.sing 308
 
309
    def getOrderForCustomer(self, orderId, customerId):
310
        """
311
        Returns an order for the order Id. Also checks if the order belongs to the customer whose Id is passed.
312
        Throws exception if either order Id is invalid or order does not below to the customer whose Id is passed.
313
 
314
        Parameters:
315
         - customerId
316
         - orderId
317
        """
318
        try:
319
            return to_t_order(get_order_for_customer(orderId, customerId))
320
        finally:
321
            close_session()
766 rajveer 322
 
483 rajveer 323
    def getOrder(self, id):
324
        """
325
        Parameters:
326
         - id
327
        """
766 rajveer 328
        try:
329
            return to_t_order(get_order(id))
330
        finally:
331
            close_session()
332
 
483 rajveer 333
    def getLineItemsForOrder(self, orderId):
334
        """
335
        Parameters:
336
         - orderId
337
        """
766 rajveer 338
        try:
339
            lineitems = get_line_items_for_order(orderId)
340
            t_lineitems = []
341
            for lineitem in lineitems:
342
                t_lineitems.append(to_t_lineitem(lineitem))
343
            return t_lineitems
344
        finally:
345
            close_session()
1149 chandransh 346
 
4283 anupam.sin 347
    def addBillingDetails(self, orderId, invoice_number, imeiNumber, itemNumber, billedBy, jacketNumber, billingType, vendorId):
494 rajveer 348
        """
2783 chandransh 349
        Adds jacket number and IMEI no. to the order. Doesn't update the IMEI no. if a -1 is supplied.
350
        Also marks the order as billed and sets the billing timestamp.
351
        Return false if it doesn't find the order with the given ID.
1149 chandransh 352
 
353
        Parameters:
354
         - orderId
355
         - jacketNumber
2783 chandransh 356
         - imeiNumber
357
         - itemNumber
358
         - billedBy
1149 chandransh 359
        """
360
        try:
4541 mandeep.dh 361
            add_billing_details(orderId, invoice_number, imeiNumber, itemNumber, billedBy, jacketNumber, billingType, vendorId)
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
 
4454 rajveer 608
    def markOrderDoaRequestReceived(self, orderId):
609
        """
610
        Once user raise the request for a DOA, order status will be changed from DELVIERY_SUCCESS to DOA_REQUEST_RECEIVED
611
 
612
        Parameters:
613
         - orderId
614
        """
615
        try:
616
            return mark_order_doa_request_received(orderId)
617
        finally:
618
            close_session()
619
 
620
    def markOrderDoaRequestAuthorized(self, orderId, isAuthorized):
621
        """
622
        CRM person can authorize or deny the request reised by customer. If he authorizes order will change from DOA_REQUEST_RECEIVED
623
        to DOA_REQUEST_AUTHORIZED. If he denies, status will be changed back to DELVIERY_SUCCESS.
624
 
625
        Parameters:
626
         - orderId
627
         - isAuthorized
628
        """
629
        try:
630
            return mark_order_doa_request_authorized(orderId, isAuthorized)
631
        finally:
632
            close_session()
633
 
4488 rajveer 634
    def markOrderReturnRequestReceived(self, orderId):
635
        """
636
        Once user raise the request for a DOA, order status will be changed from DELVIERY_SUCCESS to RET_REQUEST_RECEIVED
637
 
638
        Parameters:
639
         - orderId
640
        """
641
        try:
642
            return mark_order_return_request_received(orderId)
643
        finally:
644
            close_session()
645
 
646
    def markOrderReturnRequestAuthorized(self, orderId, isAuthorized):
647
        """
648
        CRM person can authorize or deny the request reised by customer. If he authorizes order will change from RET_REQUEST_RECEIVED
649
        to RET_REQUEST_AUTHORIZED. If he denies, status will be changed back to DELVIERY_SUCCESS.
650
 
651
        Parameters:
652
         - orderId
653
         - isAuthorized
654
        """
655
        try:
656
            return mark_order_return_request_authorized(orderId, isAuthorized)
657
        finally:
658
            close_session()
659
 
2536 chandransh 660
    def requestPickupNumber(self, orderId):
104 ashish 661
        """
2536 chandransh 662
        Sends out an email to the account manager of the original courier provider used to ship the order.
4452 rajveer 663
        If the order status was DELIVERY_SUCCESS, it is changed to be DOA_PICKUP_REQUEST_RAISED.
664
        If the order status was DOA_PICKUP_REQUEST_RAISED, it is left unchanged.
2536 chandransh 665
        For any other status, it returns false.
666
        Throws an exception if the order with the given id couldn't be found.
104 ashish 667
 
668
        Parameters:
2536 chandransh 669
         - orderId
104 ashish 670
        """
2536 chandransh 671
        try:
672
            return request_pickup_number(orderId)
673
        finally:
674
            close_session()
483 rajveer 675
 
2536 chandransh 676
    def authorizePickup(self, orderId, pickupNumber):
304 ashish 677
        """
4452 rajveer 678
        If the order status is DOA_PICKUP_REQUEST_RAISED, it does the following
2536 chandransh 679
            1. Sends out an email to the customer with the dispatch advice that he has to print as an attachment.
680
            2. Changes order status to be DOA_PICKUP_AUTHORIZED.
681
            3. Returns true
682
        If the order is any other status, it returns false.
683
        Throws an exception if the order with the given id couldn't be found.
304 ashish 684
 
685
        Parameters:
2536 chandransh 686
         - orderId
687
         - pickupNumber
304 ashish 688
        """
2536 chandransh 689
        try:
690
            return authorize_pickup(orderId, pickupNumber)
691
        finally:
692
            close_session()    
2764 chandransh 693
 
694
    def markDoasAsPickedUp(self, providerId, pickupDetails):
695
        """
696
        Marks all DOA_PICKUP_AUTHORIZED orders of the previous day for a provider as DOA_RETURN_IN_TRANSIT.
697
        Returns a list of orders that were authorized for pickup but did not appear in the pick-up report.
698
 
699
        Parameters:
700
         - providerId
701
         - pickupDetails
702
        """
703
        try:
704
            orders_not_picked_up = mark_doas_as_picked_up(providerId, pickupDetails)
705
            return [to_t_order(order) for order in orders_not_picked_up]
706
        finally:
707
            close_session()
2591 chandransh 708
 
4479 rajveer 709
    def receiveReturn(self, orderId, receiveCondition):
2591 chandransh 710
        """
4452 rajveer 711
        If the order status is DOA_PICKUP_CONFIRMED or DOA_RETURN_IN_TRANSIT, marks the order status as DOA_RECEIVED_PRESTINE and returns true.
2591 chandransh 712
        If the order is in any other state, it returns false.
713
        Throws an exception if the order with the given id couldn't be found.
714
 
715
        Parameters:
716
         - orderId
717
        """
718
        try:
4479 rajveer 719
            return receive_return(orderId, receiveCondition)
2591 chandransh 720
        finally:
721
            close_session()
722
 
723
    def validateDoa(self, orderId, isValid):
724
        """
4452 rajveer 725
        Used to validate the DOA certificate for an order in the DOA_RECEIVED_PRESTINE state. If the certificate is valid,
2609 chandransh 726
        the order state is changed to DOA_CERT_PENDING.
2591 chandransh 727
        If the certificate is invalid, the order state is changed to DOA_CERT_INVALID.
728
        If the order is in any other state, it returns false.
729
        Throws an exception if the order with the given id couldn't be found.
730
 
731
        Parameters:
732
         - orderId
733
         - isValid
734
        """
735
        try:
736
            return validate_doa(orderId, isValid)
737
        finally:
738
            close_session()
2628 chandransh 739
 
4495 rajveer 740
    def validateReturnProduct(self, orderId, isUsable):
741
        """
742
        Parameters:
743
         - orderId
744
         - isUsable
745
        """
746
        try:
747
            return validate_return_product(orderId, isUsable)
748
        finally:
749
            close_session()
750
 
2628 chandransh 751
    def reshipOrder(self, orderId):
752
        """
4484 rajveer 753
        If the order is in RTO_RECEIVED_PRESTINE or DOA_CERT_INVALID state, it does the following:
2628 chandransh 754
            1. Creates a new order for processing in the BILLED state. All billing information is saved.
4484 rajveer 755
            2. Marks the current order as one of the final states RTO_RESHIPPED and DOA_INVALID_RESHIPPED depending on what state the order started in.
2628 chandransh 756
 
757
        If the order is in DOA_CERT_VALID state, it does the following:
758
            1. Creates a new order for processing in the SUBMITTED_FOR_PROCESSING state.
759
            2. Creates a return order for the warehouse executive to return the DOA material.
4452 rajveer 760
            3. Marks the current order as the final DOA_VALID_RESHIPPED state.
2628 chandransh 761
 
762
        Returns the id of the newly created order.
763
 
764
        Throws an exception if the order with the given id couldn't be found.
765
 
766
        Parameters:
767
         - orderId
768
        """
769
        try:
770
            return reship_order(orderId)
771
        finally:
772
            close_session()
773
 
3226 chandransh 774
    def refundOrder(self, orderId, refundedBy, reason):
2628 chandransh 775
        """
4484 rajveer 776
        If the order is in RTO_RECEIVED_PRESTINE, DOA_CERT_VALID or DOA_CERT_INVALID state, it does the following:
2628 chandransh 777
            1. Creates a refund request for batch processing.
778
            2. Creates a return order for the warehouse executive to return the shipped material.
4484 rajveer 779
            3. Marks the current order as RTO_REFUNDED, DOA_VALID_REFUNDED or DOA_INVALID_REFUNDED final states.
2628 chandransh 780
 
781
        If the order is in SUBMITTED_FOR_PROCESSING or INVENTORY_LOW state, it does the following:
782
            1. Creates a refund request for batch processing.
3226 chandransh 783
            2. Cancels the reservation of the item in the warehouse.
784
            3. Marks the current order as the REFUNDED final state.
2628 chandransh 785
 
3226 chandransh 786
        For all COD orders, if the order is in INIT, SUBMITTED_FOR_PROCESSING or INVENTORY_LOW state, it does the following:
787
            1. Cancels the reservation of the item in the warehouse.
788
            2. Marks the current order as CANCELED.
789
 
790
        In all cases, it updates the reason for cancellation or refund and the person who performed the action.
791
 
2628 chandransh 792
        Returns True if it is successful, False otherwise.
793
 
794
        Throws an exception if the order with the given id couldn't be found.
795
 
796
        Parameters:
797
         - orderId
3226 chandransh 798
         - refundedBy
799
         - reason
2628 chandransh 800
        """
801
        try:
3226 chandransh 802
            return refund_order(orderId, refundedBy, reason)
2628 chandransh 803
        finally:
804
            close_session()
805
 
2697 chandransh 806
    def getReturnOrders(self, warehouseId, fromDate, toDate):
807
        """
808
        Get all return orders created between the from and to dates for the given warehouse.
809
        Ignores the warehouse if it is passed as -1.
810
 
811
        Parameters:
812
         - warehouseId
813
         - fromDate
814
         - toDate
815
        """
816
        try:
817
            from_date, to_date = get_fdate_tdate(fromDate, toDate)
818
            return get_return_orders(warehouseId, from_date, to_date)
819
        finally:
820
            close_session()
821
 
2700 chandransh 822
    def getReturnOrder(self, id):
823
        """
824
        Returns the ReturnOrder corresponding to the given id.
825
        Throws an exception if the return order with the given id couldn't be found.
826
 
827
        Parameters:
828
         - id
829
        """
830
        try:
831
            return get_return_order(id)
832
        finally:
833
            close_session()
834
 
2697 chandransh 835
    def processReturn(self, returnOrderId):
836
        """
837
        Marks the return order with the given id as processed. Raises an exception if no such return order exists.
838
 
839
        Parameters:
840
         - returnOrderId
841
        """
842
        try:
843
            process_return(returnOrderId)
844
        finally:
845
            close_session()
2591 chandransh 846
 
2819 chandransh 847
    def createPurchaseOrder(self, warehouseId):
848
        """
849
        Creates a purchase order corresponding to all the pending orders of the given warehouse.
850
        Returns the PO no. of the newly created purchase order.
851
 
852
        Parameters:
853
         - warehouseId
854
        """
855
        try:
856
            return create_purchase_order(warehouseId)
857
        finally:
858
            close_session()
3451 chandransh 859
 
860
    def updateWeight(self, orderId, weight):
861
        """
862
        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.
863
 
864
        Parameters:
865
         - orderId
866
         - weight
867
        """
868
        try:
869
            order = update_weight(orderId, weight)
870
            return to_t_order(order)
871
        finally:
872
            close_session()
873
 
3469 chandransh 874
    def changeItem(self, orderId, itemId):
875
        """
876
        Change the item to be shipped for this order. Also adjusts the reservation in the inventory accordingly.
877
        Currently, it also ensures that only a different color of the given item is shipped.
878
 
879
        Parameters:
880
         - orderId
881
         - itemId
882
        """
883
        try:
884
            order = change_product(orderId, itemId)
885
            return to_t_order(order)
886
        finally:
887
            close_session()
888
 
889
    def shiftToWarehouse(self, orderId, warehouseId):
890
        """
891
        Moves the given order to the given warehouse. Also adjusts the inventory reservations accordingly.
892
 
893
        Parameters:
894
         - orderId
895
         - warehouseId
896
        """
897
        try:
898
            order = change_warehouse(orderId, warehouseId)
899
            return to_t_order(order)
900
        finally:
901
            close_session()
3553 chandransh 902
 
3986 chandransh 903
    def addDelayReason(self, orderId, delayReason, furtherDelay):
3553 chandransh 904
        """
905
        Adds the given delay reason to the given order.
3986 chandransh 906
        Increases the expected delivery time of the given order by the given no. of days.
3553 chandransh 907
        Raises an exception if no order with the given id can be found.
3469 chandransh 908
 
3553 chandransh 909
        Parameters:
910
         - orderId
911
         - delayReason
912
        """
913
        try:
3986 chandransh 914
            return add_delay_reason(orderId, delayReason, furtherDelay)
3553 chandransh 915
        finally:
916
            close_session()
917
 
3956 chandransh 918
    def reconcileCodCollection(self, collectedAmountMap, xferBy, xferTxnId, xferDate):
919
        """
920
        Marks the COD orders with given AWB nos. as having been processed.
921
        Updates the captured amount for the corresponding payment.
922
 
923
        Returns a map of AWBs which were not processed and the associated reason. An AWB is not processed if:
924
        1. There is no order corresponding to an AWB number.
925
        2. The captured amount for a payment exceeds the total payment.
926
        3. The order corresponding to an AWB no. is in a state prior to DELIVERY_SUCCESS.
927
 
928
        Parameters:
929
         - collectedAmountMap
930
         - xferBy
931
         - xferTxnId
932
         - xferDate
933
        """
934
        try:
935
            return reconcile_cod_collection(collectedAmountMap, xferBy, xferTxnId, xferDate)
936
        finally:
937
            close_session()
4008 mandeep.dh 938
 
939
    def getTransactionsRequiringExtraProcessing(self, category):
940
        """
941
        Returns the list of transactions that require some extra processing and
942
        which belong to a particular category. This is currently used by CRM
943
        application.
944
        """
945
        try:
946
            return get_transactions_requiring_extra_processing(category)
947
        finally:
948
            close_session()
949
 
950
    def markTransactionAsProcessed(self, transactionId, category):
951
        """
952
        Marks a particular transaction as processed for a particular category.
953
        It essentially deletes the transaction if it is processed for a particular
954
        category. This is currently used by CRM application.
955
        """
956
        try:
957
            return mark_transaction_as_processed(transactionId, category)
958
        finally:
959
            close_session()
4018 chandransh 960
 
961
    def getItemWiseRiskyOrdersCount(self, ):
962
        """
963
        Returns a map containing the number of risky orders keyed by item id. A risky order
964
        is defined as one whose shipping date is about to expire.
965
        """
966
        try:
967
            return get_item_wise_risky_orders_count()
968
        finally:
969
            close_session()
3956 chandransh 970
 
4247 rajveer 971
    def markOrderCancellationRequestReceived(self, orderId):
972
        """
973
        Mark order as cancellation request received. If customer sends request of cancellation of
974
        a particular order, this method will be called. It will just change status of the order
975
        depending on its current status. It also records the previous status, so that we can move
976
        back to that status if cancellation request is denied.
977
 
978
        Parameters:
979
         - orderId
980
        """
981
        try:
982
            return mark_order_cancellation_request_received(orderId)
983
        finally:
984
            close_session()
985
 
986
 
4258 rajveer 987
    def markTransactionAsPaymentFlagRemoved(self, transactionId):
4247 rajveer 988
        """
4258 rajveer 989
        If we and/or payment gateway has decided to accept the payment, this method needs to be called.
990
        Changed transaction and all orders status to payment accepted.
4247 rajveer 991
 
992
        Parameters:
4258 rajveer 993
         - transactionId
4247 rajveer 994
        """
995
        try:
4259 anupam.sin 996
            return mark_transaction_as_payment_flag_removed(transactionId)
4247 rajveer 997
        finally:
998
            close_session()
4259 anupam.sin 999
 
1000
    def refundTransaction(self, transactionId, refundedBy, reason):
1001
        """
1002
        This method is called when a flagged payment is deemed unserviceable and the corresponding orders
1003
        need to be cancelled
1004
 
1005
        Parameters:
1006
         - transactionId
1007
        """
1008
        try:
1009
            return refund_transaction(transactionId, refundedBy, reason)
1010
        finally:
1011
            close_session()
4247 rajveer 1012
 
1013
    def markOrderCancellationRequestDenied(self, orderId):
1014
        """
1015
        If we decide to not to cancel order, we will move the order ro previous status.
1016
 
1017
        Parameters:
1018
         - orderId
1019
        """
1020
        try:
1021
            return mark_order_cancellation_request_denied(orderId)
1022
        finally:
1023
            close_session()
1024
 
1025
    def markOrderCancellationRequestConfirmed(self, orderId):
1026
        """
1027
        If we decide to to cancel order, CRM will call this method to move the status of order to
1028
        cancellation request confirmed. After this OM will be able to cancel the order.
1029
 
1030
        Parameters:
1031
         - orderId
1032
        """
1033
        try:
1034
            return mark_order_cancellation_request_confirmed(orderId)
1035
        finally:
1036
            close_session()
1037
 
4324 mandeep.dh 1038
    def updateShipmentAddress(self, orderId, addressId):
1039
        """
1040
        Updates shipment address of an order. Delivery and shipping date estimates
1041
        etc. are also updated here.
1042
 
1043
        Throws TransactionServiceException in case address change is not
1044
        possible due to certain reasons such as new pincode in address is
1045
        not serviceable etc.
1046
 
1047
        Parameters:
1048
         - orderId
1049
         - addressId
1050
        """
1051
        try:
1052
            update_shipment_address(orderId, addressId)
1053
        finally:
1054
            close_session()
1055
 
4285 rajveer 1056
    def acceptOrdersForItemId(self, itemId, inventory):
1057
        """
1058
        Marks the orders as ACCEPTED for the given itemId and inventory. It also updates the accepted timestamp. If the
1059
        given order is not a COD order, it also captures the payment if the same has not been captured.
1060
 
1061
        Parameters:
1062
         - itemId
1063
         - inventory
1064
        """
1065
        try:
1066
            return accept_orders_for_item_id(itemId, inventory)
1067
        finally:
1068
            close_session()
1069
 
4303 rajveer 1070
 
1071
 
4369 rajveer 1072
    def markOrdersAsPORaised(self, vendorId, itemId, quantity, estimate, isReminder):
4303 rajveer 1073
        """
1074
        Parameters:
1075
         - vendorId
1076
         - itemId
1077
         - quantity
1078
         - estimate
4369 rajveer 1079
         - isReminder
4303 rajveer 1080
        """
1081
        try:
4369 rajveer 1082
            return mark_orders_as_po_raised(vendorId, itemId, quantity, estimate, isReminder)
4303 rajveer 1083
        finally:
1084
            close_session()
1085
 
4369 rajveer 1086
    def markOrdersAsReversalInitiated(self, vendorId, itemId, quantity, estimate, isReminder):
4303 rajveer 1087
        """
1088
        Parameters:
1089
         - vendorId
1090
         - itemId
1091
         - quantity
1092
         - estimate
4369 rajveer 1093
         - isReminder
4303 rajveer 1094
        """
1095
        try:
4369 rajveer 1096
            return mark_orders_as_reversal_initiated(vendorId, itemId, quantity, estimate, isReminder)
4303 rajveer 1097
        finally:
1098
            close_session()
1099
 
4369 rajveer 1100
    def markOrdersAsNotAvailabke(self, vendorId, itemId, quantity, estimate, isReminder):
4303 rajveer 1101
        """
1102
        Parameters:
1103
         - vendorId
1104
         - itemId
1105
         - quantity
1106
         - estimate
4369 rajveer 1107
         - isReminder
4303 rajveer 1108
        """
1109
        try:
4369 rajveer 1110
            return mark_orders_as_not_available(vendorId, itemId, quantity, estimate, isReminder)
4303 rajveer 1111
        finally:
1112
            close_session()
1113
 
1114
 
4369 rajveer 1115
    def markOrdersAsTimeout(self, vendorId):
1116
        """
1117
        Parameters:
1118
         - vendorId
1119
        """
1120
        try:
1121
            return mark_orders_as_timeout(vendorId)
1122
        finally:
1123
            close_session()
4386 anupam.sin 1124
 
1125
    def getOrderForAwb(self, awb):
1126
        """
1127
        Parameters:
1128
         - AirwayBill Number
1129
        """
1130
        try:
1131
            return to_t_order(get_order_for_awb(awb))
1132
        finally:
1133
            close_session()
4369 rajveer 1134
 
4506 phani.kuma 1135
    def getOrdersForProviderForStatus(self, provider_id, order_status):
1136
        """
1137
        Parameters:
1138
         - provider id
1139
         - order status
1140
        """
1141
        try:
1142
            orders_of_provider_by_status = get_orders_for_provider_for_status(provider_id, order_status)
1143
            return [to_t_order(order) for order in orders_of_provider_by_status if order != None]
1144
        finally:
1145
            close_session()
4369 rajveer 1146
 
2536 chandransh 1147
    def closeSession(self, ):
1148
        close_session()
3376 rajveer 1149
 
1150
    def isAlive(self, ):
1151
        """
1152
        For checking weather service is active alive or not. It also checks connectivity with database
1153
        """
1154
        try:
1155
            return is_alive()
1156
        finally:
4247 rajveer 1157
            close_session()