Subversion Repositories SmartDukaan

Rev

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