Subversion Repositories SmartDukaan

Rev

Rev 6019 | Rev 6048 | 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
6000 mandeep.dh 7
from elixir import session
8
from shop2020.model.v1.order.impl.Convertors import to_t_transaction, to_t_alert, \
9
    to_t_order, to_t_lineitem, to_t_payment_settlement, to_t_verification_agent, \
10
    to_t_attribute
11
from shop2020.model.v1.order.impl.DataAccessors import create_transaction, \
12
    get_transactions_for_customer, get_transaction_status, get_line_items_for_order, \
13
    get_transaction, get_transactions_for_shopping_cart_id, \
14
    change_transaction_status, get_orders_for_customer, get_orders_for_transaction, \
15
    get_order, get_returnable_orders_for_customer, \
16
    get_cancellable_orders_for_customer, get_orders_by_billing_date, get_all_orders, \
17
    change_order_status, get_alerts, add_alert, add_billing_details, close_session, \
18
    accept_order, mark_orders_as_picked_up, mark_orders_as_delivered, \
19
    mark_orders_as_rto, order_outofstock, batch_orders, update_non_delivery_reason, \
20
    enqueue_transaction_info_email, get_undelivered_orders, get_order_for_customer, \
21
    get_valid_order_count, get_cust_count_with_successful_txn, \
22
    get_valid_orders_amount_range, get_valid_orders, toggle_doa_flag, \
23
    request_pickup_number, authorize_pickup, receive_return, validate_doa, \
24
    reship_order, refund_order, get_return_orders, process_return, get_return_order, \
25
    mark_doas_as_picked_up, verify_order, is_alive, get_orders_by_shipping_date, \
26
    update_weight, change_warehouse, change_product, add_delay_reason, \
27
    reconcile_cod_collection, get_transactions_requiring_extra_processing, \
28
    mark_transaction_as_processed, get_item_wise_risky_orders_count, \
29
    accept_order_for_item, get_orders_in_batch, get_order_count, \
30
    get_ebs_settlement_summaries, mark_order_cancellation_request_received, \
31
    mark_order_cancellation_request_denied, refund_transaction, \
32
    mark_order_cancellation_request_confirmed, \
33
    mark_transaction_as_payment_flag_removed, accept_orders_for_item_id, \
34
    mark_orders_as_po_raised, mark_orders_as_reversal_initiated, \
35
    mark_orders_as_not_available, update_shipment_address, mark_orders_as_timeout, \
36
    get_order_for_awb, mark_orders_as_shipped_from_warehouse, mark_alerts_as_seen, \
37
    mark_order_doa_request_received, mark_order_doa_request_authorized, \
38
    mark_order_return_request_received, mark_order_return_request_authorized, \
39
    add_invoice_number, get_reshipped_order_ids, validate_return_product, \
40
    get_orders_for_provider_for_status, save_ebs_settlement_summary, \
41
    get_ebs_settlement_date, save_payment_settlements, mark_ebs_settlement_uploaded, \
42
    get_billed_orders_for_vendor, get_settlement_for_Prepaid, \
43
    get_settlements_by_date, get_slipped_sipping_date_orders, \
44
    mark_order_as_lost_in_transit, get_cancelled_orders, mark_order_as_delivered, \
45
    mark_return_orders_as_picked_up, update_orders_as_PORaised, \
46
    get_order_distribution_by_status, get_orders_not_met_expected_delivery_date, \
47
    mark_orders_as_local_connected, mark_orders_as_destinationCityReached, \
48
    mark_orders_as_firstDeliveryAttempted, get_non_delivered_orders_by_courier, \
49
    get_orders_not_local_connected, get_doas_not_picked_up, \
50
    get_return_orders_not_picked_up, get_orders_not_picked_up, get_rto_orders, \
51
    get_order_list, get_order_ids_for_status, update_orders_as_paid_to_vendor, \
52
    update_COD_agent, get_refunded_orders_marked_paid, get_settlement_for_Cod, \
53
    get_order_list_for_vendor, update_order_only_as_paid_to_vendor, \
54
    get_all_verification_agents, get_all_attributes_for_order_id, \
55
    set_order_attribute_for_transaction, get_billed_orders, get_all_return_orders, \
56
    mark_order_as_received_at_store, get_receive_pending_orders, \
57
    get_received_at_store_orders, mark_orders_as_returned_from_store, \
6019 rajveer 58
    set_order_attributes, get_orders_collection_at_store, get_order_attribute_value,\
6031 rajveer 59
    change_jacket_number, mark_order_as_rto_in_transit, get_recharge_order,\
60
    get_recharge_orders, update_recharge_order_status, activate_recharge_txn,\
61
    get_user_wallet, get_user_wallet_history
6000 mandeep.dh 62
from shop2020.model.v1.order.impl.model.DTHRechargeOrder import DTHRechargeOrder
63
from shop2020.model.v1.order.impl.model.MobileRechargeOrder import \
64
    MobileRechargeOrder
65
from shop2020.thriftpy.model.v1.order.ttypes import TransactionServiceException, \
66
    RechargeType
67
from shop2020.utils.Utils import to_py_date, get_fdate_tdate, to_java_date
6031 rajveer 68
from shop2020.model.v1.order.impl.model.DigitalTransaction import DigitalTransaction
69
import datetime
1405 ankur.sing 70
 
5527 anupam.sin 71
 
104 ashish 72
 
73
class OrderServiceHandler:
74
 
3187 rajveer 75
    def __init__(self, dbname='transaction', db_hostname='localhost'):
483 rajveer 76
        """
104 ashish 77
        Constructor
483 rajveer 78
        """
3187 rajveer 79
        DataService.initialize(dbname, db_hostname)
104 ashish 80
 
81
    def createTransaction(self, transaction):
82
        """
83
        Parameters:
84
         - transaction
85
        """
766 rajveer 86
        try:
87
            return create_transaction(transaction)
88
        finally:
89
            close_session()
90
 
104 ashish 91
    def getTransaction(self, id):
92
        """
93
            Get transaction methods.
94
 
95
        Parameters:
96
         - id
97
        """
766 rajveer 98
        try:
99
            transaction = get_transaction(id)
100
            return to_t_transaction(transaction)
101
        finally:
102
            close_session()
103
 
483 rajveer 104
    def getTransactionsForCustomer(self, customerId, from_date, to_date, status):
104 ashish 105
        """
106
        Parameters:
483 rajveer 107
         - customerId
104 ashish 108
         - from_date
109
         - to_date
483 rajveer 110
         - status
104 ashish 111
        """
766 rajveer 112
        try:
113
            current_from_date, current_to_date = get_fdate_tdate(from_date, to_date)
114
 
115
            transactions = get_transactions_for_customer(customerId, current_from_date, current_to_date, status)
116
            t_transaction = []
117
            for transaction in transactions:
118
                t_transaction.append(to_t_transaction(transaction))
119
            return t_transaction
120
        finally:
121
            close_session()
483 rajveer 122
 
123
    def getTransactionsForShoppingCartId(self, shoppingCartId):
124
        """
125
        Parameters:
126
            - shoppingCartId
127
        """
766 rajveer 128
        try:
129
            transactions = get_transactions_for_shopping_cart_id(shoppingCartId)
130
            t_transaction = []
131
            for transaction in transactions:
132
                t_transaction.append(to_t_transaction(transaction))
133
            return t_transaction
134
        finally:
135
            close_session()
104 ashish 136
 
483 rajveer 137
    def getTransactionStatus(self, transactionId):
104 ashish 138
        """
139
        Parameters:
483 rajveer 140
         - transactionId
141
        """
766 rajveer 142
        try:
143
            return get_transaction_status(transactionId)
144
        finally:
145
            close_session()
146
 
5527 anupam.sin 147
    def changeTransactionStatus(self, transactionId, status, description, pickUp, orderType):
483 rajveer 148
        """
149
        Parameters:
150
         - transactionId
104 ashish 151
         - status
483 rajveer 152
         - description
5387 rajveer 153
         - selfPickup
483 rajveer 154
        """
766 rajveer 155
        try:
5527 anupam.sin 156
            return change_transaction_status(transactionId, status, description, pickUp, orderType)
766 rajveer 157
        finally:
158
            close_session()
159
 
1528 ankur.sing 160
    def getOrdersForTransaction(self, transactionId, customerId):
483 rajveer 161
        """
1528 ankur.sing 162
        Returns list of orders for given transaction Id. Also filters based on customer Id so that
163
        only user who owns the transaction can view its order details.
164
 
483 rajveer 165
        Parameters:
166
         - transactionId
1528 ankur.sing 167
         - customerId
483 rajveer 168
        """
766 rajveer 169
        try:
1528 ankur.sing 170
            orders = get_orders_for_transaction(transactionId, customerId)
766 rajveer 171
            return [to_t_order(order) for order in orders]    
172
        finally:
173
            close_session()
174
 
4801 anupam.sin 175
    def getAllOrders(self, statuses, from_date, to_date, warehouse_id):
483 rajveer 176
        """
177
        Parameters:
178
         - status
104 ashish 179
         - from_date
180
         - to_date
483 rajveer 181
         - warehouse_id
104 ashish 182
        """
766 rajveer 183
        try:
184
            current_from_date, current_to_date = get_fdate_tdate(from_date, to_date)        
4801 anupam.sin 185
            orders = get_all_orders(statuses, current_from_date, current_to_date, warehouse_id)
766 rajveer 186
            return [to_t_order(order) for order in orders]
187
        finally:
188
            close_session()
4133 chandransh 189
 
190
    def getOrdersInBatch(self, statuses, offset, limit, warehouse_id):
191
        """
192
        Returns at most 'limit' orders with the given statuses for the given warehouse starting from the given offset.
193
        Pass the status as null and the limit as 0 to ignore them.
194
 
195
        Parameters:
196
         - statuses
197
         - offset
198
         - limit
199
         - warehouse_id
200
        """
201
        try:
202
            orders = get_orders_in_batch(statuses, offset, limit, warehouse_id)
203
            return [to_t_order(order) for order in orders]
204
        finally:
205
            close_session()
206
 
207
    def getOrderCount(self, statuses, warehouseId):
208
        """
209
        Returns the count of orders with the given statuses assigned to the given warehouse.
210
 
211
        Parameters:
212
         - statuses
213
         - warehouseId
214
        """
215
        try:
216
            return get_order_count(statuses, warehouseId)
217
        finally:
218
            close_session()
219
 
995 varun.gupt 220
    def getOrdersByBillingDate(self, status, start_billing_date, end_billing_date, warehouse_id):
221
        """
222
        Parameters:
223
         - status
224
         - start_billing_date
225
         - end_billing_date
226
         - warehouse_id
227
        """
228
        try:
229
            current_start_billing_date, current_end_billing_date = get_fdate_tdate(start_billing_date, end_billing_date)
230
            orders = get_orders_by_billing_date(status, current_start_billing_date, current_end_billing_date, warehouse_id)
231
            return [to_t_order(order) for order in orders]
232
        finally:
233
            close_session()
1382 varun.gupt 234
 
3451 chandransh 235
    def getOrdersByShippingDate(self, fromShippingDate, toShippingDate, providerId, warehouseId, cod):
3427 chandransh 236
        """
237
        Returns orders for a particular provider and warehouse which were shipped between the given dates.
3451 chandransh 238
        Returned orders comprise of COD orders if cod parameter is true. It comprises of prepaid orders otherwise.
239
        Pass providerId and warehouseId as -1 to ignore both these parameters.
3427 chandransh 240
 
241
        Parameters:
242
         - fromShippingDate
243
         - toShippingDate
244
         - providerId
245
         - warehouseId
3451 chandransh 246
         - cod
3427 chandransh 247
        """
248
        try:
249
            from_shipping_date, to_shipping_date = get_fdate_tdate(fromShippingDate, toShippingDate)
3451 chandransh 250
            orders = get_orders_by_shipping_date(from_shipping_date, to_shipping_date, providerId, warehouseId, cod)
3427 chandransh 251
            return [to_t_order(order) for order in orders]
252
        finally:
253
            close_session()
254
 
1382 varun.gupt 255
    def getReturnableOrdersForCustomer(self, customerId, limit = None):
256
        """
257
        Parameters:
258
         - customerId
259
         - limit
260
        """
261
        try:
262
            return get_returnable_orders_for_customer(customerId, limit)
263
        finally:
264
            close_session()
995 varun.gupt 265
 
1382 varun.gupt 266
    def getCancellableOrdersForCustomer(self, customerId, limit = None):
267
        """
268
        Parameters:
269
         - customerId
270
         - limit
271
        """
272
        try:
273
            return get_cancellable_orders_for_customer(customerId, limit)
274
        finally:
275
            close_session()
276
 
483 rajveer 277
    def changeOrderStatus(self, orderId, status, description):
278
        """
279
        Parameters:
280
         - orderId
281
         - status
282
         - description
283
         - 
284
        """
766 rajveer 285
        try:
286
            return change_order_status(self, orderId, status, description)
287
        finally:
288
            close_session()
921 rajveer 289
 
3064 chandransh 290
    def verifyOrder(self, orderId):
291
        """
292
        Marks the given order as SUBMITTED_FOR_PROCESSING and updates the verified
293
        timestamp. It is intended to be used for COD orders but can be harmlessly
294
        used for all other orders as well.
295
        Throws an exception if no such order exists.
296
 
297
        Parameters:
298
         - orderId
299
        """
300
        try:
301
            return verify_order(self, orderId)
302
        finally:
303
            close_session()
304
 
921 rajveer 305
    def acceptOrder(self, orderId):
306
        """
3064 chandransh 307
        Marks the given order as ACCEPTED and updates the accepted timestamp. If the
308
        given order is not a COD order, it also captures the payment if the same has
309
        not been captured.
310
        Throws an exception if no such order exists.
311
 
921 rajveer 312
        Parameters:
313
         - orderId
314
        """
315
        try:
4285 rajveer 316
            return accept_order(orderId)
921 rajveer 317
        finally:
318
            close_session()
319
 
3014 chandransh 320
    def getOrdersForCustomer(self, customerId, from_date, to_date, statuses):
104 ashish 321
        """
3014 chandransh 322
        Returns list of orders for the given customer created between the given dates and having the given statuses.
323
        Pass and empty list to ignore filtering on statuses.
324
 
104 ashish 325
        Parameters:
326
         - customerId
327
         - from_date
328
         - to_date
3014 chandransh 329
         - statuses
104 ashish 330
        """
766 rajveer 331
        try:
332
            current_from_date, current_to_date = get_fdate_tdate(from_date, to_date)
333
 
3014 chandransh 334
            orders = get_orders_for_customer(customerId, current_from_date, current_to_date, statuses)
766 rajveer 335
            return [to_t_order(order) for order in orders]
336
        finally:
337
            close_session()
1528 ankur.sing 338
 
339
    def getOrderForCustomer(self, orderId, customerId):
340
        """
341
        Returns an order for the order Id. Also checks if the order belongs to the customer whose Id is passed.
342
        Throws exception if either order Id is invalid or order does not below to the customer whose Id is passed.
343
 
344
        Parameters:
345
         - customerId
346
         - orderId
347
        """
348
        try:
349
            return to_t_order(get_order_for_customer(orderId, customerId))
350
        finally:
351
            close_session()
766 rajveer 352
 
483 rajveer 353
    def getOrder(self, id):
354
        """
355
        Parameters:
356
         - id
357
        """
766 rajveer 358
        try:
359
            return to_t_order(get_order(id))
360
        finally:
361
            close_session()
4999 phani.kuma 362
 
363
    def getOrderList(self, order_ids):
364
        """
365
        Parameters:
366
         - order_ids
367
        """
368
        try:
369
            orders = get_order_list(order_ids)
370
            return [to_t_order(order) for order in orders]
371
        finally:
372
            close_session()
373
 
5386 phani.kuma 374
    def getOrderListForVendor(self, order_ids, vendorId):
375
        """
376
        Parameters:
377
         - order_ids
378
         - vendorId
379
        """
380
        try:
381
            orders = get_order_list_for_vendor(order_ids, vendorId)
382
            return [to_t_order(order) for order in orders]
383
        finally:
384
            close_session()
385
 
483 rajveer 386
    def getLineItemsForOrder(self, orderId):
387
        """
388
        Parameters:
389
         - orderId
390
        """
766 rajveer 391
        try:
392
            lineitems = get_line_items_for_order(orderId)
393
            t_lineitems = []
394
            for lineitem in lineitems:
395
                t_lineitems.append(to_t_lineitem(lineitem))
396
            return t_lineitems
397
        finally:
398
            close_session()
1149 chandransh 399
 
4763 rajveer 400
    def addBillingDetails(self, orderId, invoice_number, serialNumber, itemNumber, billedBy, jacketNumber, billingType, vendorId, authorize):
494 rajveer 401
        """
4658 mandeep.dh 402
        Adds jacket number and serial no. to the order. Doesn't update the serial no. if a -1 is supplied.
2783 chandransh 403
        Also marks the order as billed and sets the billing timestamp.
404
        Return false if it doesn't find the order with the given ID.
1149 chandransh 405
 
406
        Parameters:
407
         - orderId
408
         - jacketNumber
4658 mandeep.dh 409
         - serialNumber
2783 chandransh 410
         - itemNumber
411
         - billedBy
1149 chandransh 412
        """
413
        try:
4763 rajveer 414
            return add_billing_details(orderId, invoice_number, serialNumber, itemNumber, billedBy, jacketNumber, billingType, vendorId, authorize)
1149 chandransh 415
        finally:
416
            close_session()
1220 chandransh 417
 
4763 rajveer 418
    def addInvoiceNumber(self, orderId, invoiceNumber, color):
4579 rajveer 419
        """
420
        Add the invoice number to the order.
421
 
422
        Parameters:
423
         - orderId
424
         - invoice_number
425
        """
426
        try:
4763 rajveer 427
            add_invoice_number(orderId, invoiceNumber, color)
4579 rajveer 428
        finally:
429
            close_session()
430
 
1220 chandransh 431
    def batchOrders(self, warehouseId):
432
        """
433
        Create a batch of all the pending orders for the given warehouse.
434
        The returned list is orderd by created_timestamp.
435
        If there are no pending orders, an empty list is returned.
1208 chandransh 436
 
1220 chandransh 437
        Parameters:
438
         - warehouseId
439
        """
440
        try:
441
            pending_orders = batch_orders(warehouseId)
442
            return [to_t_order(order) for order in pending_orders]
443
        finally:
444
            close_session()
445
 
1208 chandransh 446
    def markOrderAsOutOfStock(self, orderId):
447
        """
448
        Mark the given order as out of stock. Throws an exception if the order with the given Id couldn't be found.
449
 
450
 
451
        Parameters:
452
         - orderId
453
        """
454
        try:
455
            return order_outofstock(orderId)
456
        finally:
457
            close_session()
458
 
4910 phani.kuma 459
    def markOrdersAsShippedFromWarehouse(self, warehouseId, providerId, cod, orderIds):
759 chandransh 460
        """
3064 chandransh 461
        Depending on the third parameter, marks either all prepaid or all cod orders BILLED by the
4910 phani.kuma 462
        given warehouse and were picked up by the given provider as SHIPPED_FROM_WH.
759 chandransh 463
 
464
        Parameters:
465
         - warehouseId
466
         - providerId
3064 chandransh 467
         - cod
759 chandransh 468
        """
766 rajveer 469
        try:
4910 phani.kuma 470
            return mark_orders_as_shipped_from_warehouse(warehouseId, providerId, cod, orderIds)
766 rajveer 471
        finally:
472
            close_session()
1113 chandransh 473
 
5713 rajveer 474
    def markOrdersAsReturnedFromStore(self, providerId, orderIds, awbs):
5676 rajveer 475
        """
476
        Parameters:
477
         - providerId
478
         - orderIds
5713 rajveer 479
         - awbs
5676 rajveer 480
        """
481
        try:
5713 rajveer 482
            return mark_orders_as_returned_from_store(providerId, orderIds, awbs)
5676 rajveer 483
        finally:
484
            close_session()
485
 
486
    def setOrderAttributes(self, orderId, attributes):
487
        """
488
        sets attributes for an order
489
 
490
        Parameters:
491
         - orderId
492
         - attributes
493
        """
494
        try:
495
            return set_order_attributes(orderId, attributes)
496
        finally:
497
            close_session()
498
 
499
 
4910 phani.kuma 500
    def markOrdersAsPickedUp(self, providerId, pickupDetails):
4410 rajveer 501
        """
4910 phani.kuma 502
        Marks all SHIPPED_FROM_WH orders of the previous day for a provider as SHIPPED_TO_LOGISTICS.
503
        Raises an exception if we encounter report for an AWB number that we did not ship.
4410 rajveer 504
 
505
        Parameters:
506
         - providerId
4910 phani.kuma 507
         - pickupDetails
4410 rajveer 508
        """
509
        try:
4910 phani.kuma 510
            mark_orders_as_picked_up(providerId, pickupDetails)
4410 rajveer 511
        finally:
512
            close_session()
513
 
4910 phani.kuma 514
    def getOrdersNotPickedUp(self, providerId):
1113 chandransh 515
        """
516
        Returns a list of orders that were shipped from warehouse but did not appear in the pick-up report.
517
 
518
        Parameters:
519
         - providerId
520
        """
521
        try:
4910 phani.kuma 522
            orders_not_picked_up = get_orders_not_picked_up(providerId)
1113 chandransh 523
            return [to_t_order(order) for order in orders_not_picked_up]
524
        finally:
525
            close_session()
1132 chandransh 526
 
527
    def markOrdersAsDelivered(self, providerId, deliveredOrders):
528
        """
529
        Marks all orders with AWBs in the given map as delivered. Also sets the delivery timestamp and
530
        the name of the receiver.
531
        Raises an exception if we encounter report for an AWB number that we did not ship.
532
 
533
        Parameters:
534
         - providerId
535
         - deliveredOrders
536
        """
537
        try:
538
            mark_orders_as_delivered(providerId, deliveredOrders)
539
        finally:
540
            close_session()
1135 chandransh 541
 
4712 rajveer 542
    def markOrderAsDelivered(self, orderId, deliveryTimestamp, receiver):
543
        """
544
        Attributes:
545
         - orderId
546
         - deliveryTimestamp
547
         - receiver
548
        """
549
        try:
550
            mark_order_as_delivered(orderId, to_py_date(deliveryTimestamp), receiver)
551
        finally:
552
            close_session()
553
 
554
 
5555 rajveer 555
    def markOrderAsReceivedAtStore(self, orderId, deliveryTimestamp):
556
        """
557
        Attributes:
558
         - orderId
559
         - deliveryTimestamp
560
        """
561
        try:
562
            mark_order_as_received_at_store(orderId, to_py_date(deliveryTimestamp))
563
        finally:
564
            close_session()
565
 
566
    def getReceivePendingOrders(self, storeId):
567
        """
568
        Attributes:
569
         - storeId
570
        """
571
        try:
572
            orders = get_receive_pending_orders(storeId)
573
            return [to_t_order(order) for order in orders]
574
        finally:
575
            close_session()
576
 
577
    def getReceivedAtStoreOrders(self, storeId):
578
        """
579
        Attributes:
580
         - storeId
581
        """
582
        try:
583
            orders = get_received_at_store_orders(storeId)
584
            return [to_t_order(order) for order in orders]
585
        finally:
586
            close_session()
5713 rajveer 587
 
588
    def getOrdersCollectionAtStore(self, storeId, fromDate, toDate, onlyCod):
589
        """
590
        Parameters:
591
         - storeId
592
         - fromDate
593
         - toDate
594
         - onlyCod
595
        """
596
        try:
597
            orders = get_orders_collection_at_store(storeId, to_py_date(fromDate), to_py_date(toDate), onlyCod)
598
            return [to_t_order(order) for order in orders]
599
        finally:
600
            close_session()
601
 
4910 phani.kuma 602
    def markAsRTOrders(self, providerId, returnedOrders):
1135 chandransh 603
        """
4910 phani.kuma 604
        Mark all orders with AWBs in the given map as RTO. Also sets the delivery timestamp.
1135 chandransh 605
        Raises an exception if we encounter report for an AWB number that we did not ship.
606
 
607
        Parameters:
608
         - providerId
609
         - returnedOrders
610
        """
611
        try:
4910 phani.kuma 612
            mark_orders_as_rto(providerId, returnedOrders)
1135 chandransh 613
        finally:
614
            close_session()
1246 chandransh 615
 
4910 phani.kuma 616
    def getRTOrders(self, providerId):
617
        """
618
        Returns a list of orders that were returned by courier.
619
 
620
        Parameters:
621
         - providerId
622
        """
623
        try:
624
            rto_orders = get_rto_orders(providerId)
625
            return [to_t_order(order) for order in rto_orders]
626
        finally:
627
            close_session()
628
 
1246 chandransh 629
    def updateNonDeliveryReason(self, providerId, undeliveredOrders):
630
        """
631
        Update the status description of orders whose AWB numbers are keys of the Map.
632
 
633
        Parameters:
634
         - providerId
635
         - undelivered_orders
636
        """
637
        try:
4910 phani.kuma 638
            update_non_delivery_reason(providerId, undeliveredOrders)
639
        finally:
640
            close_session()
641
 
642
    def getNonDeliveredOrdersbyCourier(self, providerId):
643
        """
644
        Returns a list of orders that were picked up or shipped four days ago but did not get delivered.
645
 
646
        Parameters:
647
         - providerId
648
        """
649
        try:
650
            orders_not_delivered = get_non_delivered_orders_by_courier(providerId)
4581 phani.kuma 651
            return [to_t_order(order) for order in orders_not_delivered]
1246 chandransh 652
        finally:
653
            close_session()
1405 ankur.sing 654
 
4910 phani.kuma 655
    def markOrdersAsLocalConnected(self, providerId, local_connected_orders):
656
        """
657
        Mark all orders with AWBs in the given map as local connected. Also sets the local connected timestamp.
658
 
659
        Parameters:
660
         - providerId
661
         - local_connected_orders
662
        """
663
        try:
664
            mark_orders_as_local_connected(providerId, local_connected_orders)
665
        finally:
666
            close_session()
667
 
668
    def getOrdersNotLocalConnected(self, providerId):
669
        """
670
        Returns a list of orders that were picked up or shipped but pending local connection.
671
 
672
        Parameters:
673
         - providerId
674
        """
675
        try:
676
            orders_pending_local_connection = get_orders_not_local_connected(providerId)
677
            return [to_t_order(order) for order in orders_pending_local_connection]
678
        finally:
679
            close_session()
680
 
681
    def markOrdersAsDestinationCityReached(self, providerId, destination_city_reached_orders):
682
        """
683
        Mark all orders with AWBs in the given map as reached destination city. Also sets the reached destination timestamp.
684
 
685
        Parameters:
686
         - providerId
687
         - destination_city_reached_orders
688
        """
689
        try:
690
            mark_orders_as_destinationCityReached(providerId, destination_city_reached_orders)
691
        finally:
692
            close_session()
693
 
694
    def markOrdersAsFirstDeliveryAttempted(self, providerId, first_atdl_orders):
695
        """
696
        Mark all orders with AWBs in the given map as first delivery attempt made. Also sets the first delivery attempted timestamp.
697
 
698
        Parameters:
699
         - providerId
700
         - first_atdl_orders
701
        """
702
        try:
703
            mark_orders_as_firstDeliveryAttempted(providerId, first_atdl_orders)
704
        finally:
705
            close_session()
706
 
4783 phani.kuma 707
    def getUndeliveredOrdersExpectedDeliveryDateNotMet(self):
708
        """
709
        Returns the list of orders whose expected delivery date has passed but have not been
710
        delivered yet.
711
        Returns an empty list if no such orders exist.
712
        """
713
        try:
714
            orders_not_delivered = get_orders_not_met_expected_delivery_date()
715
            return [to_t_order(order) for order in orders_not_delivered]
716
        finally:
717
            close_session()
718
 
1405 ankur.sing 719
    def getUndeliveredOrders(self, providerId, warehouseId):
720
        """
721
        Returns the list of orders whose delivery time has passed but have not been
722
        delivered yet for the given provider and warehouse. To get a complete list of
723
        undelivered orders, pass them as -1.
724
        Returns an empty list if no such orders exist.
725
 
726
        Parameters:
727
         - providerId
728
         - warehouseId
729
        """
730
        try:
731
            undelivered_orders = get_undelivered_orders(providerId, warehouseId)
732
            return [to_t_order(order) for order in undelivered_orders]
733
        finally:
734
            close_session()
1351 varun.gupt 735
 
1398 varun.gupt 736
    def enqueueTransactionInfoEmail(self, transactionId):
1351 varun.gupt 737
        """
1398 varun.gupt 738
        Save the email containing order details to be sent to customer later by batch job
1351 varun.gupt 739
 
740
        Parameters:
741
         - transactionId
742
        """
743
        try:
1398 varun.gupt 744
            return enqueue_transaction_info_email(transactionId)
1351 varun.gupt 745
        finally:
746
            close_session()
747
 
4444 rajveer 748
    def getAlerts(self, type, warehouseId, status, timestamp):
483 rajveer 749
        """
750
        Parameters:
4394 rajveer 751
         - type
4444 rajveer 752
         - warehouseId
4394 rajveer 753
         - status
754
         - timestamp
483 rajveer 755
        """
766 rajveer 756
        try:
4444 rajveer 757
            alerts = get_alerts(type, warehouseId, status, timestamp)
766 rajveer 758
 
759
            t_alerts = []
760
            for alert in alerts:
761
                t_alerts.append(to_t_alert(alert)) 
762
 
763
            return t_alerts
764
        finally:
765
            close_session()
4394 rajveer 766
 
4447 rajveer 767
    def addAlert(self, type, warehouseId, description):
483 rajveer 768
        """
769
        Parameters:
770
         - type
4394 rajveer 771
         - description
4447 rajveer 772
         - warehouseId
483 rajveer 773
        """
766 rajveer 774
        try:
4447 rajveer 775
            add_alert(type, warehouseId, description)
766 rajveer 776
        finally:
777
            close_session()
1596 ankur.sing 778
 
4444 rajveer 779
    def markAlertsAsSeen(self, warehouseId):
780
        """
781
        Parameters:
782
         - warehouseId
783
        """
784
        try:
785
            mark_alerts_as_seen(warehouseId)
786
        finally:
787
            close_session()
788
        pass
789
 
1596 ankur.sing 790
    def getValidOrderCount(self, ):
791
        """
792
        Return the number of valid orders. (OrderStatus >= OrderStatus.SUBMITTED_FOR_PROCESSING)
793
        """
1731 ankur.sing 794
        try:
795
            return get_valid_order_count()
796
        finally:
797
            close_session()
1627 ankur.sing 798
 
799
    def getNoOfCustomersWithSuccessfulTransaction(self, ):
800
        """
801
        Returns the number of distinct customers who have done successful transactions
802
        """
1731 ankur.sing 803
        try:
804
            return get_cust_count_with_successful_txn()
805
        finally:
806
            close_session()
1627 ankur.sing 807
 
1731 ankur.sing 808
 
809
    def getValidOrdersAmountRange(self, ):
1627 ankur.sing 810
        """
1731 ankur.sing 811
        Returns the minimum and maximum amounts of a valid order. (OrderStatus >= OrderStatus.SUBMITTED_FOR_PROCESSING)
812
        List contains two values, first minimum amount and second maximum amount.
1627 ankur.sing 813
        """
1731 ankur.sing 814
        try:
815
            return get_valid_orders_amount_range()
816
        finally:
817
            close_session()
1627 ankur.sing 818
 
5874 rajveer 819
    def getValidOrders(self, limit, onlyStore):
1886 ankur.sing 820
        """
821
        Returns list of Orders in descending order by Order creation date. List is restricted to limit Orders.
822
        If limit is passed as 0, then all valid Orders are returned.
823
 
824
        Parameters:
825
         - limit
5874 rajveer 826
         - onlyStore
1886 ankur.sing 827
        """
828
        try:
5874 rajveer 829
            return [to_t_order(order) for order in get_valid_orders(limit, onlyStore)]
1886 ankur.sing 830
        finally:
831
            close_session()
104 ashish 832
 
2536 chandransh 833
    def toggleDOAFlag(self, orderId):
104 ashish 834
        """
2536 chandransh 835
        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.
836
        Returns the final flag status.
837
        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 838
 
132 ashish 839
        Parameters:
2536 chandransh 840
         - orderId
132 ashish 841
        """
2536 chandransh 842
        try:
843
            return toggle_doa_flag(orderId)
844
        finally:
845
            close_session()
483 rajveer 846
 
4454 rajveer 847
    def markOrderDoaRequestReceived(self, orderId):
848
        """
849
        Once user raise the request for a DOA, order status will be changed from DELVIERY_SUCCESS to DOA_REQUEST_RECEIVED
850
 
851
        Parameters:
852
         - orderId
853
        """
854
        try:
855
            return mark_order_doa_request_received(orderId)
856
        finally:
857
            close_session()
858
 
859
    def markOrderDoaRequestAuthorized(self, orderId, isAuthorized):
860
        """
861
        CRM person can authorize or deny the request reised by customer. If he authorizes order will change from DOA_REQUEST_RECEIVED
862
        to DOA_REQUEST_AUTHORIZED. If he denies, status will be changed back to DELVIERY_SUCCESS.
863
 
864
        Parameters:
865
         - orderId
866
         - isAuthorized
867
        """
868
        try:
869
            return mark_order_doa_request_authorized(orderId, isAuthorized)
870
        finally:
871
            close_session()
872
 
4488 rajveer 873
    def markOrderReturnRequestReceived(self, orderId):
874
        """
875
        Once user raise the request for a DOA, order status will be changed from DELVIERY_SUCCESS to RET_REQUEST_RECEIVED
876
 
877
        Parameters:
878
         - orderId
879
        """
880
        try:
881
            return mark_order_return_request_received(orderId)
882
        finally:
883
            close_session()
884
 
885
    def markOrderReturnRequestAuthorized(self, orderId, isAuthorized):
886
        """
887
        CRM person can authorize or deny the request reised by customer. If he authorizes order will change from RET_REQUEST_RECEIVED
888
        to RET_REQUEST_AUTHORIZED. If he denies, status will be changed back to DELVIERY_SUCCESS.
889
 
890
        Parameters:
891
         - orderId
892
         - isAuthorized
893
        """
894
        try:
895
            return mark_order_return_request_authorized(orderId, isAuthorized)
896
        finally:
897
            close_session()
898
 
4579 rajveer 899
    def requestPickupNumber(self, orderId, providerId):
104 ashish 900
        """
2536 chandransh 901
        Sends out an email to the account manager of the original courier provider used to ship the order.
4452 rajveer 902
        If the order status was DELIVERY_SUCCESS, it is changed to be DOA_PICKUP_REQUEST_RAISED.
903
        If the order status was DOA_PICKUP_REQUEST_RAISED, it is left unchanged.
2536 chandransh 904
        For any other status, it returns false.
905
        Throws an exception if the order with the given id couldn't be found.
104 ashish 906
 
907
        Parameters:
2536 chandransh 908
         - orderId
104 ashish 909
        """
2536 chandransh 910
        try:
4579 rajveer 911
            return request_pickup_number(orderId, providerId)
2536 chandransh 912
        finally:
913
            close_session()
483 rajveer 914
 
4602 rajveer 915
    def authorizePickup(self, orderId, pickupNumber, providerId):
304 ashish 916
        """
4452 rajveer 917
        If the order status is DOA_PICKUP_REQUEST_RAISED, it does the following
2536 chandransh 918
            1. Sends out an email to the customer with the dispatch advice that he has to print as an attachment.
919
            2. Changes order status to be DOA_PICKUP_AUTHORIZED.
920
            3. Returns true
921
        If the order is any other status, it returns false.
922
        Throws an exception if the order with the given id couldn't be found.
304 ashish 923
 
924
        Parameters:
2536 chandransh 925
         - orderId
926
         - pickupNumber
304 ashish 927
        """
2536 chandransh 928
        try:
4602 rajveer 929
            return authorize_pickup(orderId, pickupNumber, providerId)
2536 chandransh 930
        finally:
931
            close_session()    
2764 chandransh 932
 
933
    def markDoasAsPickedUp(self, providerId, pickupDetails):
934
        """
935
        Marks all DOA_PICKUP_AUTHORIZED orders of the previous day for a provider as DOA_RETURN_IN_TRANSIT.
936
 
937
        Parameters:
938
         - providerId
939
         - pickupDetails
940
        """
941
        try:
4910 phani.kuma 942
            mark_doas_as_picked_up(providerId, pickupDetails)
943
        finally:
944
            close_session()
945
 
946
    def getDoasNotPickedUp(self, providerId):
947
        """
948
        Returns a list of orders that were authorized for pickup but did not appear in the pick-up report.
949
 
950
        Parameters:
951
         - providerId
952
        """
953
        try:
954
            orders_not_picked_up = get_doas_not_picked_up(providerId)
2764 chandransh 955
            return [to_t_order(order) for order in orders_not_picked_up]
956
        finally:
957
            close_session()
4910 phani.kuma 958
 
4741 phani.kuma 959
    def markReturnOrdersAsPickedUp(self, providerId, pickupDetails):
960
        """
961
        Marks all RET_PICKUP_CONFIRMED orders of the previous day for a provider as RET_RETURN_IN_TRANSIT.
962
 
963
        Parameters:
964
         - providerId
965
         - pickupDetails
966
        """
967
        try:
4910 phani.kuma 968
            mark_return_orders_as_picked_up(providerId, pickupDetails)
969
        finally:
970
            close_session()
971
 
972
    def getReturnOrdersNotPickedUp(self, providerId):
973
        """
974
        Returns a list of orders that were authorized for pickup but did not appear in the pick-up report.
975
 
976
        Parameters:
977
         - providerId
978
        """
979
        try:
980
            orders_not_picked_up = get_return_orders_not_picked_up(providerId)
4741 phani.kuma 981
            return [to_t_order(order) for order in orders_not_picked_up]
982
        finally:
983
            close_session()
4910 phani.kuma 984
 
4479 rajveer 985
    def receiveReturn(self, orderId, receiveCondition):
2591 chandransh 986
        """
4452 rajveer 987
        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 988
        If the order is in any other state, it returns false.
989
        Throws an exception if the order with the given id couldn't be found.
990
 
991
        Parameters:
992
         - orderId
993
        """
994
        try:
4479 rajveer 995
            return receive_return(orderId, receiveCondition)
2591 chandransh 996
        finally:
997
            close_session()
998
 
999
    def validateDoa(self, orderId, isValid):
1000
        """
4452 rajveer 1001
        Used to validate the DOA certificate for an order in the DOA_RECEIVED_PRESTINE state. If the certificate is valid,
2609 chandransh 1002
        the order state is changed to DOA_CERT_PENDING.
2591 chandransh 1003
        If the certificate is invalid, the order state is changed to DOA_CERT_INVALID.
1004
        If the order is in any other state, it returns false.
1005
        Throws an exception if the order with the given id couldn't be found.
1006
 
1007
        Parameters:
1008
         - orderId
1009
         - isValid
1010
        """
1011
        try:
1012
            return validate_doa(orderId, isValid)
1013
        finally:
1014
            close_session()
2628 chandransh 1015
 
4495 rajveer 1016
    def validateReturnProduct(self, orderId, isUsable):
1017
        """
1018
        Parameters:
1019
         - orderId
1020
         - isUsable
1021
        """
1022
        try:
1023
            return validate_return_product(orderId, isUsable)
1024
        finally:
1025
            close_session()
1026
 
2628 chandransh 1027
    def reshipOrder(self, orderId):
1028
        """
4484 rajveer 1029
        If the order is in RTO_RECEIVED_PRESTINE or DOA_CERT_INVALID state, it does the following:
2628 chandransh 1030
            1. Creates a new order for processing in the BILLED state. All billing information is saved.
4484 rajveer 1031
            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 1032
 
1033
        If the order is in DOA_CERT_VALID state, it does the following:
1034
            1. Creates a new order for processing in the SUBMITTED_FOR_PROCESSING state.
1035
            2. Creates a return order for the warehouse executive to return the DOA material.
4452 rajveer 1036
            3. Marks the current order as the final DOA_VALID_RESHIPPED state.
2628 chandransh 1037
 
1038
        Returns the id of the newly created order.
1039
 
1040
        Throws an exception if the order with the given id couldn't be found.
1041
 
1042
        Parameters:
1043
         - orderId
1044
        """
1045
        try:
1046
            return reship_order(orderId)
1047
        finally:
1048
            close_session()
1049
 
3226 chandransh 1050
    def refundOrder(self, orderId, refundedBy, reason):
2628 chandransh 1051
        """
4484 rajveer 1052
        If the order is in RTO_RECEIVED_PRESTINE, DOA_CERT_VALID or DOA_CERT_INVALID state, it does the following:
2628 chandransh 1053
            1. Creates a refund request for batch processing.
1054
            2. Creates a return order for the warehouse executive to return the shipped material.
4484 rajveer 1055
            3. Marks the current order as RTO_REFUNDED, DOA_VALID_REFUNDED or DOA_INVALID_REFUNDED final states.
2628 chandransh 1056
 
1057
        If the order is in SUBMITTED_FOR_PROCESSING or INVENTORY_LOW state, it does the following:
1058
            1. Creates a refund request for batch processing.
3226 chandransh 1059
            2. Cancels the reservation of the item in the warehouse.
1060
            3. Marks the current order as the REFUNDED final state.
2628 chandransh 1061
 
3226 chandransh 1062
        For all COD orders, if the order is in INIT, SUBMITTED_FOR_PROCESSING or INVENTORY_LOW state, it does the following:
1063
            1. Cancels the reservation of the item in the warehouse.
1064
            2. Marks the current order as CANCELED.
1065
 
1066
        In all cases, it updates the reason for cancellation or refund and the person who performed the action.
1067
 
2628 chandransh 1068
        Returns True if it is successful, False otherwise.
1069
 
1070
        Throws an exception if the order with the given id couldn't be found.
1071
 
1072
        Parameters:
1073
         - orderId
3226 chandransh 1074
         - refundedBy
1075
         - reason
2628 chandransh 1076
        """
1077
        try:
3226 chandransh 1078
            return refund_order(orderId, refundedBy, reason)
2628 chandransh 1079
        finally:
1080
            close_session()
1081
 
2697 chandransh 1082
    def getReturnOrders(self, warehouseId, fromDate, toDate):
1083
        """
1084
        Get all return orders created between the from and to dates for the given warehouse.
1085
        Ignores the warehouse if it is passed as -1.
1086
 
1087
        Parameters:
1088
         - warehouseId
1089
         - fromDate
1090
         - toDate
1091
        """
1092
        try:
1093
            from_date, to_date = get_fdate_tdate(fromDate, toDate)
1094
            return get_return_orders(warehouseId, from_date, to_date)
1095
        finally:
1096
            close_session()
1097
 
5481 phani.kuma 1098
    def getAllReturnOrders(self, onlyNotProcessed, fromDate, toDate):
1099
        """
1100
        Get all return orders created between the from and to dates for the given warehouse.
1101
        Ignores the warehouse if it is passed as -1.
1102
 
1103
        Parameters:
1104
         - warehouseId
1105
         - fromDate
1106
         - toDate
1107
        """
1108
        try:
1109
            from_date, to_date = get_fdate_tdate(fromDate, toDate)
1110
            return get_all_return_orders(onlyNotProcessed, from_date, to_date)
1111
        finally:
1112
            close_session()
1113
 
2700 chandransh 1114
    def getReturnOrder(self, id):
1115
        """
1116
        Returns the ReturnOrder corresponding to the given id.
1117
        Throws an exception if the return order with the given id couldn't be found.
1118
 
1119
        Parameters:
1120
         - id
1121
        """
1122
        try:
1123
            return get_return_order(id)
1124
        finally:
1125
            close_session()
1126
 
2697 chandransh 1127
    def processReturn(self, returnOrderId):
1128
        """
1129
        Marks the return order with the given id as processed. Raises an exception if no such return order exists.
1130
 
1131
        Parameters:
1132
         - returnOrderId
1133
        """
1134
        try:
1135
            process_return(returnOrderId)
1136
        finally:
1137
            close_session()
4757 mandeep.dh 1138
 
1139
    def updateOrdersAsPORaised(self, itemIdQuantityMap, purchaseOrderId, warehouseId):
2819 chandransh 1140
        """
4757 mandeep.dh 1141
        Updates orders as PO raised. Also updates purchase order id in orders. Pass a map of items mapped to
1142
        the quantities for which the PO is raised.
1143
 
2819 chandransh 1144
        Parameters:
4757 mandeep.dh 1145
         - itemIdQuantityMap
1146
         - purchaseOrderId
2819 chandransh 1147
         - warehouseId
1148
        """
1149
        try:
4757 mandeep.dh 1150
            update_orders_as_PORaised(itemIdQuantityMap, purchaseOrderId, warehouseId)
2819 chandransh 1151
        finally:
1152
            close_session()
4757 mandeep.dh 1153
 
3451 chandransh 1154
    def updateWeight(self, orderId, weight):
1155
        """
1156
        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.
1157
 
1158
        Parameters:
1159
         - orderId
1160
         - weight
1161
        """
1162
        try:
1163
            order = update_weight(orderId, weight)
1164
            return to_t_order(order)
1165
        finally:
1166
            close_session()
1167
 
3469 chandransh 1168
    def changeItem(self, orderId, itemId):
1169
        """
1170
        Change the item to be shipped for this order. Also adjusts the reservation in the inventory accordingly.
1171
        Currently, it also ensures that only a different color of the given item is shipped.
1172
 
1173
        Parameters:
1174
         - orderId
1175
         - itemId
1176
        """
1177
        try:
1178
            order = change_product(orderId, itemId)
1179
            return to_t_order(order)
1180
        finally:
1181
            close_session()
1182
 
1183
    def shiftToWarehouse(self, orderId, warehouseId):
1184
        """
1185
        Moves the given order to the given warehouse. Also adjusts the inventory reservations accordingly.
1186
 
1187
        Parameters:
1188
         - orderId
1189
         - warehouseId
1190
        """
1191
        try:
1192
            order = change_warehouse(orderId, warehouseId)
1193
            return to_t_order(order)
1194
        finally:
1195
            close_session()
3553 chandransh 1196
 
4647 rajveer 1197
    def addDelayReason(self, orderId, delayReason, furtherDelay, delayReasonText):
3553 chandransh 1198
        """
1199
        Adds the given delay reason to the given order.
3986 chandransh 1200
        Increases the expected delivery time of the given order by the given no. of days.
3553 chandransh 1201
        Raises an exception if no order with the given id can be found.
3469 chandransh 1202
 
3553 chandransh 1203
        Parameters:
1204
         - orderId
1205
         - delayReason
1206
        """
1207
        try:
4647 rajveer 1208
            return add_delay_reason(orderId, delayReason, furtherDelay, delayReasonText)
3553 chandransh 1209
        finally:
1210
            close_session()
1211
 
3956 chandransh 1212
    def reconcileCodCollection(self, collectedAmountMap, xferBy, xferTxnId, xferDate):
1213
        """
1214
        Marks the COD orders with given AWB nos. as having been processed.
1215
        Updates the captured amount for the corresponding payment.
1216
 
1217
        Returns a map of AWBs which were not processed and the associated reason. An AWB is not processed if:
1218
        1. There is no order corresponding to an AWB number.
1219
        2. The captured amount for a payment exceeds the total payment.
1220
        3. The order corresponding to an AWB no. is in a state prior to DELIVERY_SUCCESS.
1221
 
1222
        Parameters:
1223
         - collectedAmountMap
1224
         - xferBy
1225
         - xferTxnId
1226
         - xferDate
1227
        """
1228
        try:
1229
            return reconcile_cod_collection(collectedAmountMap, xferBy, xferTxnId, xferDate)
1230
        finally:
1231
            close_session()
4008 mandeep.dh 1232
 
1233
    def getTransactionsRequiringExtraProcessing(self, category):
1234
        """
1235
        Returns the list of transactions that require some extra processing and
1236
        which belong to a particular category. This is currently used by CRM
1237
        application.
1238
        """
1239
        try:
1240
            return get_transactions_requiring_extra_processing(category)
1241
        finally:
1242
            close_session()
1243
 
1244
    def markTransactionAsProcessed(self, transactionId, category):
1245
        """
1246
        Marks a particular transaction as processed for a particular category.
1247
        It essentially deletes the transaction if it is processed for a particular
1248
        category. This is currently used by CRM application.
1249
        """
1250
        try:
1251
            return mark_transaction_as_processed(transactionId, category)
1252
        finally:
1253
            close_session()
4018 chandransh 1254
 
1255
    def getItemWiseRiskyOrdersCount(self, ):
1256
        """
1257
        Returns a map containing the number of risky orders keyed by item id. A risky order
1258
        is defined as one whose shipping date is about to expire.
1259
        """
1260
        try:
1261
            return get_item_wise_risky_orders_count()
1262
        finally:
1263
            close_session()
3956 chandransh 1264
 
4247 rajveer 1265
    def markOrderCancellationRequestReceived(self, orderId):
1266
        """
1267
        Mark order as cancellation request received. If customer sends request of cancellation of
1268
        a particular order, this method will be called. It will just change status of the order
1269
        depending on its current status. It also records the previous status, so that we can move
1270
        back to that status if cancellation request is denied.
1271
 
1272
        Parameters:
1273
         - orderId
1274
        """
1275
        try:
1276
            return mark_order_cancellation_request_received(orderId)
1277
        finally:
1278
            close_session()
1279
 
1280
 
4662 rajveer 1281
    def markOrderAsLostInTransit(self, orderId):
1282
        """
1283
        Parameters:
1284
         - orderId
1285
        """
1286
        try:
1287
            return mark_order_as_lost_in_transit(orderId)
1288
        finally:
1289
            close_session()
1290
 
4258 rajveer 1291
    def markTransactionAsPaymentFlagRemoved(self, transactionId):
4247 rajveer 1292
        """
4258 rajveer 1293
        If we and/or payment gateway has decided to accept the payment, this method needs to be called.
1294
        Changed transaction and all orders status to payment accepted.
4247 rajveer 1295
 
1296
        Parameters:
4258 rajveer 1297
         - transactionId
4247 rajveer 1298
        """
1299
        try:
4259 anupam.sin 1300
            return mark_transaction_as_payment_flag_removed(transactionId)
4247 rajveer 1301
        finally:
1302
            close_session()
4259 anupam.sin 1303
 
1304
    def refundTransaction(self, transactionId, refundedBy, reason):
1305
        """
1306
        This method is called when a flagged payment is deemed unserviceable and the corresponding orders
1307
        need to be cancelled
1308
 
1309
        Parameters:
1310
         - transactionId
1311
        """
1312
        try:
1313
            return refund_transaction(transactionId, refundedBy, reason)
1314
        finally:
1315
            close_session()
4247 rajveer 1316
 
1317
    def markOrderCancellationRequestDenied(self, orderId):
1318
        """
1319
        If we decide to not to cancel order, we will move the order ro previous status.
1320
 
1321
        Parameters:
1322
         - orderId
1323
        """
1324
        try:
1325
            return mark_order_cancellation_request_denied(orderId)
1326
        finally:
1327
            close_session()
1328
 
1329
    def markOrderCancellationRequestConfirmed(self, orderId):
1330
        """
1331
        If we decide to to cancel order, CRM will call this method to move the status of order to
1332
        cancellation request confirmed. After this OM will be able to cancel the order.
1333
 
1334
        Parameters:
1335
         - orderId
1336
        """
1337
        try:
1338
            return mark_order_cancellation_request_confirmed(orderId)
1339
        finally:
1340
            close_session()
1341
 
4324 mandeep.dh 1342
    def updateShipmentAddress(self, orderId, addressId):
1343
        """
1344
        Updates shipment address of an order. Delivery and shipping date estimates
1345
        etc. are also updated here.
1346
 
1347
        Throws TransactionServiceException in case address change is not
1348
        possible due to certain reasons such as new pincode in address is
1349
        not serviceable etc.
1350
 
1351
        Parameters:
1352
         - orderId
1353
         - addressId
1354
        """
1355
        try:
1356
            update_shipment_address(orderId, addressId)
1357
        finally:
1358
            close_session()
1359
 
4285 rajveer 1360
    def acceptOrdersForItemId(self, itemId, inventory):
1361
        """
1362
        Marks the orders as ACCEPTED for the given itemId and inventory. It also updates the accepted timestamp. If the
1363
        given order is not a COD order, it also captures the payment if the same has not been captured.
1364
 
1365
        Parameters:
1366
         - itemId
1367
         - inventory
1368
        """
1369
        try:
1370
            return accept_orders_for_item_id(itemId, inventory)
1371
        finally:
1372
            close_session()
1373
 
4303 rajveer 1374
 
1375
 
4369 rajveer 1376
    def markOrdersAsPORaised(self, vendorId, itemId, quantity, estimate, isReminder):
4303 rajveer 1377
        """
1378
        Parameters:
1379
         - vendorId
1380
         - itemId
1381
         - quantity
1382
         - estimate
4369 rajveer 1383
         - isReminder
4303 rajveer 1384
        """
1385
        try:
4369 rajveer 1386
            return mark_orders_as_po_raised(vendorId, itemId, quantity, estimate, isReminder)
4303 rajveer 1387
        finally:
1388
            close_session()
1389
 
4369 rajveer 1390
    def markOrdersAsReversalInitiated(self, vendorId, itemId, quantity, estimate, isReminder):
4303 rajveer 1391
        """
1392
        Parameters:
1393
         - vendorId
1394
         - itemId
1395
         - quantity
1396
         - estimate
4369 rajveer 1397
         - isReminder
4303 rajveer 1398
        """
1399
        try:
4369 rajveer 1400
            return mark_orders_as_reversal_initiated(vendorId, itemId, quantity, estimate, isReminder)
4303 rajveer 1401
        finally:
1402
            close_session()
1403
 
4369 rajveer 1404
    def markOrdersAsNotAvailabke(self, vendorId, itemId, quantity, estimate, isReminder):
4303 rajveer 1405
        """
1406
        Parameters:
1407
         - vendorId
1408
         - itemId
1409
         - quantity
1410
         - estimate
4369 rajveer 1411
         - isReminder
4303 rajveer 1412
        """
1413
        try:
4369 rajveer 1414
            return mark_orders_as_not_available(vendorId, itemId, quantity, estimate, isReminder)
4303 rajveer 1415
        finally:
1416
            close_session()
1417
 
1418
 
4369 rajveer 1419
    def markOrdersAsTimeout(self, vendorId):
1420
        """
1421
        Parameters:
1422
         - vendorId
1423
        """
1424
        try:
1425
            return mark_orders_as_timeout(vendorId)
1426
        finally:
1427
            close_session()
4386 anupam.sin 1428
 
1429
    def getOrderForAwb(self, awb):
1430
        """
1431
        Parameters:
1432
         - AirwayBill Number
1433
        """
1434
        try:
1435
            return to_t_order(get_order_for_awb(awb))
1436
        finally:
1437
            close_session()
4369 rajveer 1438
 
4910 phani.kuma 1439
    def getOrdersForProviderForStatus(self, provider_id, order_status_list):
4506 phani.kuma 1440
        """
1441
        Parameters:
1442
         - provider id
1443
         - order status
1444
        """
1445
        try:
4910 phani.kuma 1446
            orders_of_provider_by_status = get_orders_for_provider_for_status(provider_id, order_status_list)
4506 phani.kuma 1447
            return [to_t_order(order) for order in orders_of_provider_by_status if order != None]
1448
        finally:
1449
            close_session()
4600 varun.gupt 1450
 
1451
    def getBilledOrdersForVendor(self, vendorId, billingDateFrom, billingDateTo):
1452
        '''
1453
        Parameters:
1454
         - vendorId
1455
         - billingDateFrom
1456
         - billingDateTo
1457
        '''
1458
        try:
1459
            return [to_t_order(order) for order in get_billed_orders_for_vendor(vendorId, to_py_date(billingDateFrom), to_py_date(billingDateTo))]
1460
        finally:
1461
            close_session()
1462
 
4607 rajveer 1463
    def getSlippedSippingDateOrders(self):
1464
        try:
1465
            return [to_t_order(order) for order in get_slipped_sipping_date_orders()]
1466
        finally:
1467
            close_session()
1468
 
4709 rajveer 1469
    def getCancelledOrders(self, cancelDateFrom, cancelDateTo):
1470
        try:
1471
            return [to_t_order(order) for order in get_cancelled_orders(to_py_date(cancelDateFrom), to_py_date(cancelDateTo))]
1472
        finally:
1473
            close_session()
1474
 
4600 varun.gupt 1475
    def getEBSSettlementSummaries(self):
1476
        try:
1477
            return get_ebs_settlement_summaries()
1478
        finally:
1479
            close_session()
4369 rajveer 1480
 
4600 varun.gupt 1481
    def saveEBSSettlementSummary(self, settlementId, settlementDate, transactionDateFrom, transactionDateTo, amount):
1482
        try:
1483
            save_ebs_settlement_summary(settlementId, to_py_date(settlementDate), to_py_date(transactionDateFrom), to_py_date(transactionDateTo), amount)
1484
        finally:
1485
            close_session()
1486
 
5386 phani.kuma 1487
    def getSettlementForPrepaid(self, referenceId, isRefund):
4600 varun.gupt 1488
        '''
1489
        Parameters:
5189 varun.gupt 1490
         - referenceId
1491
         - isRefund
4600 varun.gupt 1492
        '''
1493
        try:
5386 phani.kuma 1494
            return to_t_payment_settlement(get_settlement_for_Prepaid(referenceId, isRefund))
4600 varun.gupt 1495
        finally:
1496
            close_session()
5386 phani.kuma 1497
 
1498
    def getSettlementForCod(self, orderId, isRefund):
1499
        '''
1500
        Parameters:
1501
         - orderId
1502
         - isRefund
1503
        '''
1504
        try:
1505
            return to_t_payment_settlement(get_settlement_for_Cod(orderId, isRefund))
1506
        finally:
1507
            close_session()
1508
 
4600 varun.gupt 1509
    def getEBSSettlementDate(self, settlementId):
1510
        try:
1511
            return to_java_date(get_ebs_settlement_date(settlementId))
1512
        finally:
1513
            close_session()
1514
 
4905 varun.gupt 1515
    def savePaymentSettlements(self, settlementDate, paymentGatewayId, referenceId, serviceTax, otherCharges, netCollection):
4600 varun.gupt 1516
        try:
4905 varun.gupt 1517
            save_payment_settlements(to_py_date(settlementDate), paymentGatewayId, referenceId, serviceTax, otherCharges, netCollection)
4600 varun.gupt 1518
        finally:
1519
            close_session()
1520
 
1521
    def markEBSSettlementUploaded(self, settlementId):
1522
        try:
1523
            mark_ebs_settlement_uploaded(settlementId)
1524
        finally:
1525
            close_session()
1526
 
4715 varun.gupt 1527
    def getSettlementsByDate(self, settlementDateFrom, settlementDateTo, isRefund):
1528
        try:
1529
            settlements = get_settlements_by_date(to_py_date(settlementDateFrom), to_py_date(settlementDateTo), isRefund)
1530
            return [to_t_payment_settlement(settlement) for settlement in settlements]
1531
        finally:
1532
            close_session()
1533
 
1534
    def getReshippedOrderIds(self, orderIds):
1535
        try:
1536
            return get_reshipped_order_ids(orderIds)
1537
        finally:
1538
            close_session()
1539
 
5481 phani.kuma 1540
    def getBilledOrders(self, vendorId, onlyVendorNotPaid, billingDateFrom, billingDateTo):
4875 varun.gupt 1541
        try:
5481 phani.kuma 1542
            from_date, to_date = get_fdate_tdate(billingDateFrom, billingDateTo)
1543
            return [to_t_order(order) for order in get_billed_orders(vendorId, onlyVendorNotPaid, from_date, to_date)]
4875 varun.gupt 1544
        finally:
1545
            close_session()
1546
 
5062 varun.gupt 1547
    def getStatusDistributionOfOrders(self, startDate, endDate):
1548
        try:
5067 varun.gupt 1549
            distribution = {}
1550
 
1551
            for status, count in get_order_distribution_by_status(to_py_date(startDate), to_py_date(endDate)):
1552
                distribution[status] = count
1553
 
1554
            return distribution
5062 varun.gupt 1555
        finally:
1556
            close_session()
1557
 
5067 varun.gupt 1558
    def getOrderIdsForStatus(self, status, startDatetime, endDatetime):
1559
        try:
1560
            orders = get_order_ids_for_status(status, to_py_date(startDatetime), to_py_date(endDatetime))
1561
            return [order.id for order in orders]
1562
        finally:
1563
            close_session()
1564
 
5099 varun.gupt 1565
    def updateOrderAsPaidToVendor(self, orderId):
1566
        try:
1567
            update_orders_as_paid_to_vendor(orderId)
1568
        finally:
1569
            close_session()
1570
 
5348 anupam.sin 1571
    def updateCODAgent(self, agentEmailId, orderId):
1572
        try:
1573
            update_COD_agent(agentEmailId, orderId)
1574
        finally:
1575
            close_session()
1576
 
5386 phani.kuma 1577
    def updateOrderOnlyAsPaidToVendor(self, orderId):
1578
        try:
1579
            update_order_only_as_paid_to_vendor(orderId)
1580
        finally:
1581
            close_session()
1582
 
5208 varun.gupt 1583
    def getRefundedOrdersMarkedPaid(self):
1584
        try:
1585
            return [to_t_order(order) for order in get_refunded_orders_marked_paid()]
1586
        finally:
1587
            close_session()
5447 anupam.sin 1588
 
1589
    def getAllVerificationAgents(self, minOrderId, maxOrderId):
1590
        try:
1591
            return [to_t_verification_agent(codAgent) for codAgent in get_all_verification_agents(minOrderId, maxOrderId)]
1592
        finally:
5527 anupam.sin 1593
            close_session()
1594
 
1595
    def getAllAttributesForOrderId(self, orderId):
1596
        try:
1597
            return [to_t_attribute(attribute) for attribute in get_all_attributes_for_order_id(orderId)]
1598
        finally:
1599
            close_session()
1600
 
1601
    def setOrderAttributeForTransaction(self, transactionId, attribute):
1602
        try:
1603
            set_order_attribute_for_transaction(transactionId, attribute)                    
1604
        finally:
1605
            close_session()
5593 mandeep.dh 1606
 
1607
    def acceptOrderForItem(self, itemId, quantity, fulfilmentWarehouseId, billingWarehouseId):
1608
        """
1609
        Accepts appropriate order for an item in a given billingWarehouse. Usually
1610
        invoked while scanning IN of items.
1611
 
1612
        Parameters:
1613
         - itemId
1614
         - quantity
1615
         - fulfilmentWarehouseId
1616
         - billingWarehouseId
1617
        """
1618
        try:
1619
            accept_order_for_item(itemId, quantity, fulfilmentWarehouseId, billingWarehouseId)
1620
        finally:
1621
            close_session()
1622
 
5833 rajveer 1623
    def getOrderAttributeValue(self, orderId, attributeName):
1624
        """
1625
        Parameters:
1626
         - orderId
1627
         - attributeName
1628
        """
1629
        try:
6019 rajveer 1630
            return get_order_attribute_value(orderId, attributeName)
5833 rajveer 1631
        finally:
1632
            close_session()
1633
 
6019 rajveer 1634
    def changeJacketNumber(self, orderId, jacketNumber):
1635
        """
1636
        Parameters:
1637
         - orderId
1638
         - jacketNumber
1639
        """
1640
        try:
1641
            return change_jacket_number(orderId, jacketNumber)
1642
        finally:
1643
            close_session()
1644
 
1645
    def markOrderAsRtoInTransit(self, orderId):
1646
        """
1647
        Parameters:
1648
         - orderId
1649
        """
1650
        try:
1651
            return mark_order_as_rto_in_transit(orderId)
1652
        finally:
1653
            close_session()        
1654
 
6000 mandeep.dh 1655
    def createRechargeOrder(self, thriftRechargeOrder):
1656
        """
1657
        Parameters:
1658
         - thriftRechargeOrder
1659
        """
6031 rajveer 1660
        try:
1661
            if thriftRechargeOrder is None or thriftRechargeOrder.rechargeType is None:
1662
                raise TransactionServiceException(102, 'Order or the type is Null')
1663
 
1664
            rechargeOrder = None
1665
            if thriftRechargeOrder.rechargeType == RechargeType.MOBILE:
1666
                rechargeOrder = MobileRechargeOrder()
1667
            elif thriftRechargeOrder.rechargeType == RechargeType.DTH:
1668
                rechargeOrder = DTHRechargeOrder()
1669
 
1670
            if rechargeOrder is None:
1671
                raise TransactionServiceException(102, 'Unsupported recharge type')
6000 mandeep.dh 1672
 
6031 rajveer 1673
            transaction = DigitalTransaction()
1674
            transaction.createdOn = datetime.datetime.now()
1675
            transaction.userId = rechargeOrder.userId
1676
            transaction.orders = [rechargeOrder]
1677
 
6000 mandeep.dh 1678
            rechargeOrder.from_thrift_object(thriftRechargeOrder)
1679
            session.commit()
6031 rajveer 1680
            return rechargeOrder.to_thrift_object()
6000 mandeep.dh 1681
        except:
1682
            raise TransactionServiceException(103, 'Invalid values passed')
1683
        finally:
1684
            self.closeSession()
1685
 
6031 rajveer 1686
    def getRechargeOrder(self, rechargeOrderId):
1687
        """
1688
        Parameters:
1689
         - rechargeOrderId
1690
        """
1691
        try:
1692
            return get_recharge_order(rechargeOrderId)
1693
        finally:
1694
            self.closeSession()
1695
 
1696
    def getRechargeOrders(self, userId):
1697
        """
1698
        Parameters:
1699
         - userId
1700
        """
1701
        try:
1702
            return [order.to_thrift_object() for order in get_recharge_orders(userId)]
1703
        finally:
1704
            self.closeSession()
1705
 
6000 mandeep.dh 1706
    def updateRechargeOrderStatus(self, rechargeOrderId, rechargeOrderStatus):
1707
        """
1708
        Parameters:
1709
         - rechargeOrderId
1710
         - rechargeOrderStatus
1711
        """
6031 rajveer 1712
        try:
1713
            return update_recharge_order_status(rechargeOrderId, rechargeOrderStatus)
1714
        finally:
1715
            self.closeSession()
1716
 
1717
    def activateRechargeTxn(self, rechargeOrderId):
6000 mandeep.dh 1718
        """
1719
        Parameters:
6031 rajveer 1720
         - rechargeOrderId
6000 mandeep.dh 1721
        """
6031 rajveer 1722
        try:
1723
            return True
1724
            activate_recharge_txn(rechargeOrderId)
1725
        finally:
1726
            self.closeSession()
6000 mandeep.dh 1727
 
6031 rajveer 1728
    def getUserWallet(self, userId):
6000 mandeep.dh 1729
        """
1730
        Parameters:
6031 rajveer 1731
         - userId
6000 mandeep.dh 1732
        """
6031 rajveer 1733
        try:
1734
            return get_user_wallet(userId).to_thrift_object()
1735
        finally:
1736
            self.closeSession()
6000 mandeep.dh 1737
 
6031 rajveer 1738
    def getUserWalletHistory(self, userId):
6000 mandeep.dh 1739
        """
1740
        Parameters:
6031 rajveer 1741
         - userId
6000 mandeep.dh 1742
        """
6031 rajveer 1743
        try:
1744
            return [wallet.to_thrift_object() for wallet in get_user_wallet_history(userId)]
1745
        finally:
1746
            self.closeSession()
6000 mandeep.dh 1747
 
6031 rajveer 1748
 
2536 chandransh 1749
    def closeSession(self, ):
1750
        close_session()
3376 rajveer 1751
 
1752
    def isAlive(self, ):
1753
        """
5447 anupam.sin 1754
        For checking whether service is alive or not. It also checks connectivity with database
3376 rajveer 1755
        """
1756
        try:
1757
            return is_alive()
1758
        finally:
4247 rajveer 1759
            close_session()