Subversion Repositories SmartDukaan

Rev

Rev 4658 | Rev 4709 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
104 ashish 1
'''
2
Created on 29-Mar-2010
3
 
4
@author: ashish
5
'''
6
from shop2020.model.v1.order.impl import DataService
7
from shop2020.model.v1.order.impl.DataAccessors import create_transaction,\
1382 varun.gupt 8
    create_order, get_new_transaction, get_transactions_for_customer, get_transaction_status,\
9
    get_line_items_for_order, get_transaction, get_transactions_for_shopping_cart_id,\
10
    change_transaction_status, get_orders_for_customer,get_orders_for_transaction, get_order,\
11
    get_returnable_orders_for_customer, get_cancellable_orders_for_customer, get_orders_by_billing_date,\
4394 rajveer 12
    get_all_orders, change_order_status, get_alerts, add_alert, add_billing_details,\
4285 rajveer 13
    mark_orders_as_manifested, close_session, accept_order, mark_orders_as_picked_up,\
4283 anupam.sin 14
    mark_orders_as_delivered, mark_orders_as_failed,\
1405 ankur.sing 15
    order_outofstock, batch_orders, update_non_delivery_reason, enqueue_transaction_info_email,\
1627 ankur.sing 16
    get_undelivered_orders, get_order_for_customer, get_valid_order_count,\
1886 ankur.sing 17
    get_cust_count_with_successful_txn, get_valid_orders_amount_range,\
2591 chandransh 18
    get_valid_orders, toggle_doa_flag, request_pickup_number, authorize_pickup,\
2697 chandransh 19
    receive_return, validate_doa, reship_order, refund_order, get_return_orders,\
2819 chandransh 20
    process_return, get_return_order, mark_doas_as_picked_up,\
3451 chandransh 21
    create_purchase_order, verify_order, is_alive, get_orders_by_shipping_date,\
3956 chandransh 22
    update_weight, change_warehouse, change_product, add_delay_reason,\
4008 mandeep.dh 23
    reconcile_cod_collection, get_transactions_requiring_extra_processing,\
4133 chandransh 24
    mark_transaction_as_processed, get_item_wise_risky_orders_count,\
4600 varun.gupt 25
    get_orders_in_batch, get_order_count, get_ebs_settlement_summaries, \
4259 anupam.sin 26
    mark_order_cancellation_request_received,\
27
    mark_order_cancellation_request_denied, refund_transaction,\
28
    mark_order_cancellation_request_confirmed,\
4303 rajveer 29
    mark_transaction_as_payment_flag_removed, accept_orders_for_item_id,\
30
    mark_orders_as_po_raised, mark_orders_as_reversal_initiated,\
4369 rajveer 31
    mark_orders_as_not_available, update_shipment_address,\
4410 rajveer 32
    mark_orders_as_timeout, get_order_for_awb,\
4481 anupam.sin 33
    mark_orders_as_shipped_from_warehouse, mark_alerts_as_seen, \
4488 rajveer 34
    mark_order_doa_request_received, mark_order_doa_request_authorized,\
4600 varun.gupt 35
    mark_order_return_request_received, mark_order_return_request_authorized, add_invoice_number, \
36
    validate_return_product, get_orders_for_provider_for_status, save_ebs_settlement_summary, get_ebs_settlement_date, \
4607 rajveer 37
    save_payment_settlements, mark_ebs_settlement_uploaded, get_billed_orders_for_vendor, get_settlement_for_paymentId,\
4662 rajveer 38
    get_slipped_sipping_date_orders, mark_order_as_lost_in_transit
1405 ankur.sing 39
 
104 ashish 40
from shop2020.model.v1.order.impl.Convertors import to_t_transaction,\
4600 varun.gupt 41
    to_t_alert, to_t_order, to_t_lineitem, to_t_payment_settlement
483 rajveer 42
from shop2020.thriftpy.model.v1.order.ttypes import Transaction, OrderStatus,\
2783 chandransh 43
    LineItem, Order, TransactionServiceException
4600 varun.gupt 44
from shop2020.utils.Utils import to_py_date, get_fdate_tdate, to_java_date
104 ashish 45
 
46
class OrderServiceHandler:
47
 
3187 rajveer 48
    def __init__(self, dbname='transaction', db_hostname='localhost'):
483 rajveer 49
        """
104 ashish 50
        Constructor
483 rajveer 51
        """
3187 rajveer 52
        DataService.initialize(dbname, db_hostname)
104 ashish 53
 
54
    def createTransaction(self, transaction):
55
        """
56
        Parameters:
57
         - transaction
58
        """
766 rajveer 59
        try:
60
            return create_transaction(transaction)
61
        finally:
62
            close_session()
63
 
104 ashish 64
    def getTransaction(self, id):
65
        """
66
            Get transaction methods.
67
 
68
        Parameters:
69
         - id
70
        """
766 rajveer 71
        try:
72
            transaction = get_transaction(id)
73
            return to_t_transaction(transaction)
74
        finally:
75
            close_session()
76
 
483 rajveer 77
    def getTransactionsForCustomer(self, customerId, from_date, to_date, status):
104 ashish 78
        """
79
        Parameters:
483 rajveer 80
         - customerId
104 ashish 81
         - from_date
82
         - to_date
483 rajveer 83
         - status
104 ashish 84
        """
766 rajveer 85
        try:
86
            current_from_date, current_to_date = get_fdate_tdate(from_date, to_date)
87
 
88
            transactions = get_transactions_for_customer(customerId, current_from_date, current_to_date, status)
89
            t_transaction = []
90
            for transaction in transactions:
91
                t_transaction.append(to_t_transaction(transaction))
92
            return t_transaction
93
        finally:
94
            close_session()
483 rajveer 95
 
96
    def getTransactionsForShoppingCartId(self, shoppingCartId):
97
        """
98
        Parameters:
99
            - shoppingCartId
100
        """
766 rajveer 101
        try:
102
            transactions = get_transactions_for_shopping_cart_id(shoppingCartId)
103
            t_transaction = []
104
            for transaction in transactions:
105
                t_transaction.append(to_t_transaction(transaction))
106
            return t_transaction
107
        finally:
108
            close_session()
104 ashish 109
 
483 rajveer 110
    def getTransactionStatus(self, transactionId):
104 ashish 111
        """
112
        Parameters:
483 rajveer 113
         - transactionId
114
        """
766 rajveer 115
        try:
116
            return get_transaction_status(transactionId)
117
        finally:
118
            close_session()
119
 
483 rajveer 120
    def changeTransactionStatus(self, transactionId, status, description):
121
        """
122
        Parameters:
123
         - transactionId
104 ashish 124
         - status
483 rajveer 125
         - description
126
        """
766 rajveer 127
        try:
128
            return change_transaction_status(transactionId, status, description)
129
        finally:
130
            close_session()
131
 
1528 ankur.sing 132
    def getOrdersForTransaction(self, transactionId, customerId):
483 rajveer 133
        """
1528 ankur.sing 134
        Returns list of orders for given transaction Id. Also filters based on customer Id so that
135
        only user who owns the transaction can view its order details.
136
 
483 rajveer 137
        Parameters:
138
         - transactionId
1528 ankur.sing 139
         - customerId
483 rajveer 140
        """
766 rajveer 141
        try:
1528 ankur.sing 142
            orders = get_orders_for_transaction(transactionId, customerId)
766 rajveer 143
            return [to_t_order(order) for order in orders]    
144
        finally:
145
            close_session()
146
 
483 rajveer 147
    def getAllOrders(self, status, from_date, to_date, warehouse_id):
148
        """
149
        Parameters:
150
         - status
104 ashish 151
         - from_date
152
         - to_date
483 rajveer 153
         - warehouse_id
104 ashish 154
        """
766 rajveer 155
        try:
156
            current_from_date, current_to_date = get_fdate_tdate(from_date, to_date)        
157
            orders = get_all_orders(status, current_from_date, current_to_date, warehouse_id)
158
            return [to_t_order(order) for order in orders]
159
        finally:
160
            close_session()
4133 chandransh 161
 
162
    def getOrdersInBatch(self, statuses, offset, limit, warehouse_id):
163
        """
164
        Returns at most 'limit' orders with the given statuses for the given warehouse starting from the given offset.
165
        Pass the status as null and the limit as 0 to ignore them.
166
 
167
        Parameters:
168
         - statuses
169
         - offset
170
         - limit
171
         - warehouse_id
172
        """
173
        try:
174
            orders = get_orders_in_batch(statuses, offset, limit, warehouse_id)
175
            return [to_t_order(order) for order in orders]
176
        finally:
177
            close_session()
178
 
179
    def getOrderCount(self, statuses, warehouseId):
180
        """
181
        Returns the count of orders with the given statuses assigned to the given warehouse.
182
 
183
        Parameters:
184
         - statuses
185
         - warehouseId
186
        """
187
        try:
188
            return get_order_count(statuses, warehouseId)
189
        finally:
190
            close_session()
191
 
995 varun.gupt 192
    def getOrdersByBillingDate(self, status, start_billing_date, end_billing_date, warehouse_id):
193
        """
194
        Parameters:
195
         - status
196
         - start_billing_date
197
         - end_billing_date
198
         - warehouse_id
199
        """
200
        try:
201
            current_start_billing_date, current_end_billing_date = get_fdate_tdate(start_billing_date, end_billing_date)
202
            orders = get_orders_by_billing_date(status, current_start_billing_date, current_end_billing_date, warehouse_id)
203
            return [to_t_order(order) for order in orders]
204
        finally:
205
            close_session()
1382 varun.gupt 206
 
3451 chandransh 207
    def getOrdersByShippingDate(self, fromShippingDate, toShippingDate, providerId, warehouseId, cod):
3427 chandransh 208
        """
209
        Returns orders for a particular provider and warehouse which were shipped between the given dates.
3451 chandransh 210
        Returned orders comprise of COD orders if cod parameter is true. It comprises of prepaid orders otherwise.
211
        Pass providerId and warehouseId as -1 to ignore both these parameters.
3427 chandransh 212
 
213
        Parameters:
214
         - fromShippingDate
215
         - toShippingDate
216
         - providerId
217
         - warehouseId
3451 chandransh 218
         - cod
3427 chandransh 219
        """
220
        try:
221
            from_shipping_date, to_shipping_date = get_fdate_tdate(fromShippingDate, toShippingDate)
3451 chandransh 222
            orders = get_orders_by_shipping_date(from_shipping_date, to_shipping_date, providerId, warehouseId, cod)
3427 chandransh 223
            return [to_t_order(order) for order in orders]
224
        finally:
225
            close_session()
226
 
1382 varun.gupt 227
    def getReturnableOrdersForCustomer(self, customerId, limit = None):
228
        """
229
        Parameters:
230
         - customerId
231
         - limit
232
        """
233
        try:
234
            return get_returnable_orders_for_customer(customerId, limit)
235
        finally:
236
            close_session()
995 varun.gupt 237
 
1382 varun.gupt 238
    def getCancellableOrdersForCustomer(self, customerId, limit = None):
239
        """
240
        Parameters:
241
         - customerId
242
         - limit
243
        """
244
        try:
245
            return get_cancellable_orders_for_customer(customerId, limit)
246
        finally:
247
            close_session()
248
 
483 rajveer 249
    def changeOrderStatus(self, orderId, status, description):
250
        """
251
        Parameters:
252
         - orderId
253
         - status
254
         - description
255
         - 
256
        """
766 rajveer 257
        try:
258
            return change_order_status(self, orderId, status, description)
259
        finally:
260
            close_session()
921 rajveer 261
 
3064 chandransh 262
    def verifyOrder(self, orderId):
263
        """
264
        Marks the given order as SUBMITTED_FOR_PROCESSING and updates the verified
265
        timestamp. It is intended to be used for COD orders but can be harmlessly
266
        used for all other orders as well.
267
        Throws an exception if no such order exists.
268
 
269
        Parameters:
270
         - orderId
271
        """
272
        try:
273
            return verify_order(self, orderId)
274
        finally:
275
            close_session()
276
 
921 rajveer 277
    def acceptOrder(self, orderId):
278
        """
3064 chandransh 279
        Marks the given order as ACCEPTED and updates the accepted timestamp. If the
280
        given order is not a COD order, it also captures the payment if the same has
281
        not been captured.
282
        Throws an exception if no such order exists.
283
 
921 rajveer 284
        Parameters:
285
         - orderId
286
        """
287
        try:
4285 rajveer 288
            return accept_order(orderId)
921 rajveer 289
        finally:
290
            close_session()
291
 
3014 chandransh 292
    def getOrdersForCustomer(self, customerId, from_date, to_date, statuses):
104 ashish 293
        """
3014 chandransh 294
        Returns list of orders for the given customer created between the given dates and having the given statuses.
295
        Pass and empty list to ignore filtering on statuses.
296
 
104 ashish 297
        Parameters:
298
         - customerId
299
         - from_date
300
         - to_date
3014 chandransh 301
         - statuses
104 ashish 302
        """
766 rajveer 303
        try:
304
            current_from_date, current_to_date = get_fdate_tdate(from_date, to_date)
305
 
3014 chandransh 306
            orders = get_orders_for_customer(customerId, current_from_date, current_to_date, statuses)
766 rajveer 307
            return [to_t_order(order) for order in orders]
308
        finally:
309
            close_session()
1528 ankur.sing 310
 
311
    def getOrderForCustomer(self, orderId, customerId):
312
        """
313
        Returns an order for the order Id. Also checks if the order belongs to the customer whose Id is passed.
314
        Throws exception if either order Id is invalid or order does not below to the customer whose Id is passed.
315
 
316
        Parameters:
317
         - customerId
318
         - orderId
319
        """
320
        try:
321
            return to_t_order(get_order_for_customer(orderId, customerId))
322
        finally:
323
            close_session()
766 rajveer 324
 
483 rajveer 325
    def getOrder(self, id):
326
        """
327
        Parameters:
328
         - id
329
        """
766 rajveer 330
        try:
331
            return to_t_order(get_order(id))
332
        finally:
333
            close_session()
334
 
483 rajveer 335
    def getLineItemsForOrder(self, orderId):
336
        """
337
        Parameters:
338
         - orderId
339
        """
766 rajveer 340
        try:
341
            lineitems = get_line_items_for_order(orderId)
342
            t_lineitems = []
343
            for lineitem in lineitems:
344
                t_lineitems.append(to_t_lineitem(lineitem))
345
            return t_lineitems
346
        finally:
347
            close_session()
1149 chandransh 348
 
4658 mandeep.dh 349
    def addBillingDetails(self, orderId, invoice_number, serialNumber, itemNumber, billedBy, jacketNumber, billingType, vendorId):
494 rajveer 350
        """
4658 mandeep.dh 351
        Adds jacket number and serial no. to the order. Doesn't update the serial no. if a -1 is supplied.
2783 chandransh 352
        Also marks the order as billed and sets the billing timestamp.
353
        Return false if it doesn't find the order with the given ID.
1149 chandransh 354
 
355
        Parameters:
356
         - orderId
357
         - jacketNumber
4658 mandeep.dh 358
         - serialNumber
2783 chandransh 359
         - itemNumber
360
         - billedBy
1149 chandransh 361
        """
362
        try:
4658 mandeep.dh 363
            return add_billing_details(orderId, invoice_number, serialNumber, itemNumber, billedBy, jacketNumber, billingType, vendorId)
1149 chandransh 364
        finally:
365
            close_session()
1220 chandransh 366
 
4579 rajveer 367
    def addInvoiceNumber(self, orderId, invoiceNumber):
368
        """
369
        Add the invoice number to the order.
370
 
371
        Parameters:
372
         - orderId
373
         - invoice_number
374
        """
375
        try:
376
            add_invoice_number(orderId, invoiceNumber)
377
        finally:
378
            close_session()
379
 
1220 chandransh 380
    def batchOrders(self, warehouseId):
381
        """
382
        Create a batch of all the pending orders for the given warehouse.
383
        The returned list is orderd by created_timestamp.
384
        If there are no pending orders, an empty list is returned.
1208 chandransh 385
 
1220 chandransh 386
        Parameters:
387
         - warehouseId
388
        """
389
        try:
390
            pending_orders = batch_orders(warehouseId)
391
            return [to_t_order(order) for order in pending_orders]
392
        finally:
393
            close_session()
394
 
1208 chandransh 395
    def markOrderAsOutOfStock(self, orderId):
396
        """
397
        Mark the given order as out of stock. Throws an exception if the order with the given Id couldn't be found.
398
 
399
 
400
        Parameters:
401
         - orderId
402
        """
403
        try:
404
            return order_outofstock(orderId)
405
        finally:
406
            close_session()
407
 
3064 chandransh 408
    def markOrdersAsManifested(self, warehouseId, providerId, cod):
759 chandransh 409
        """
3064 chandransh 410
        Depending on the third parameter, marks either all prepaid or all cod orders BILLED by the
4410 rajveer 411
        given warehouse and were picked up by the given provider as MANIFESTED.
759 chandransh 412
 
413
        Parameters:
414
         - warehouseId
415
         - providerId
3064 chandransh 416
         - cod
759 chandransh 417
        """
766 rajveer 418
        try:
3064 chandransh 419
            return mark_orders_as_manifested(warehouseId, providerId, cod)
766 rajveer 420
        finally:
421
            close_session()
1113 chandransh 422
 
4410 rajveer 423
    def markOrdersAsShippedFromWarehouse(self, warehouseId, providerId, cod):
424
        """
425
        Depending on the third parameter, marks either all prepaid or all cod orders MANIFESTED by the
426
        given warehouse and were picked up by the given provider as SHIPPED_FROM_WH.
427
 
428
        Parameters:
429
         - warehouseId
430
         - providerId
431
         - cod
432
        """
433
        try:
434
            return mark_orders_as_shipped_from_warehouse(warehouseId, providerId, cod)
435
        finally:
436
            close_session()
437
 
1113 chandransh 438
    def markOrdersAsPickedUp(self, providerId, pickupDetails):
439
        """
440
        Marks all SHIPPED_FROM_WH orders of the previous day for a provider as SHIPPED_TO_LOGISTICS.
441
        Returns a list of orders that were shipped from warehouse but did not appear in the pick-up report.
442
        Raises an exception if we encounter report for an AWB number that we did not ship.
443
 
444
        Parameters:
445
         - providerId
446
         - pickupDetails
447
        """
448
        try:
449
            orders_not_picked_up = mark_orders_as_picked_up(providerId, pickupDetails)
450
            return [to_t_order(order) for order in orders_not_picked_up]
451
        finally:
452
            close_session()
1132 chandransh 453
 
454
    def markOrdersAsDelivered(self, providerId, deliveredOrders):
455
        """
456
        Marks all orders with AWBs in the given map as delivered. Also sets the delivery timestamp and
457
        the name of the receiver.
458
        Raises an exception if we encounter report for an AWB number that we did not ship.
459
 
460
        Parameters:
461
         - providerId
462
         - deliveredOrders
463
        """
464
        try:
465
            mark_orders_as_delivered(providerId, deliveredOrders)
466
        finally:
467
            close_session()
1135 chandransh 468
 
469
    def markOrdersAsFailed(self, providerId, returnedOrders):
470
        """
471
        Mark all orders with AWBs in the given map as failed. Also sets the delivery timestamp.
472
        Raises an exception if we encounter report for an AWB number that we did not ship.
473
 
474
        Parameters:
475
         - providerId
476
         - returnedOrders
477
        """
478
        try:
479
            mark_orders_as_failed(providerId, returnedOrders)
480
        finally:
481
            close_session()
1246 chandransh 482
 
483
    def updateNonDeliveryReason(self, providerId, undeliveredOrders):
484
        """
485
        Update the status description of orders whose AWB numbers are keys of the Map.
486
 
487
        Parameters:
488
         - providerId
489
         - undelivered_orders
490
        """
491
        try:
4581 phani.kuma 492
            orders_not_delivered = update_non_delivery_reason(providerId, undeliveredOrders)
493
            return [to_t_order(order) for order in orders_not_delivered]
1246 chandransh 494
        finally:
495
            close_session()
1405 ankur.sing 496
 
497
    def getUndeliveredOrders(self, providerId, warehouseId):
498
        """
499
        Returns the list of orders whose delivery time has passed but have not been
500
        delivered yet for the given provider and warehouse. To get a complete list of
501
        undelivered orders, pass them as -1.
502
        Returns an empty list if no such orders exist.
503
 
504
        Parameters:
505
         - providerId
506
         - warehouseId
507
        """
508
        try:
509
            undelivered_orders = get_undelivered_orders(providerId, warehouseId)
510
            return [to_t_order(order) for order in undelivered_orders]
511
        finally:
512
            close_session()
1351 varun.gupt 513
 
1398 varun.gupt 514
    def enqueueTransactionInfoEmail(self, transactionId):
1351 varun.gupt 515
        """
1398 varun.gupt 516
        Save the email containing order details to be sent to customer later by batch job
1351 varun.gupt 517
 
518
        Parameters:
519
         - transactionId
520
        """
521
        try:
1398 varun.gupt 522
            return enqueue_transaction_info_email(transactionId)
1351 varun.gupt 523
        finally:
524
            close_session()
525
 
4444 rajveer 526
    def getAlerts(self, type, warehouseId, status, timestamp):
483 rajveer 527
        """
528
        Parameters:
4394 rajveer 529
         - type
4444 rajveer 530
         - warehouseId
4394 rajveer 531
         - status
532
         - timestamp
483 rajveer 533
        """
766 rajveer 534
        try:
4444 rajveer 535
            alerts = get_alerts(type, warehouseId, status, timestamp)
766 rajveer 536
 
537
            t_alerts = []
538
            for alert in alerts:
539
                t_alerts.append(to_t_alert(alert)) 
540
 
541
            return t_alerts
542
        finally:
543
            close_session()
4394 rajveer 544
 
4447 rajveer 545
    def addAlert(self, type, warehouseId, description):
483 rajveer 546
        """
547
        Parameters:
548
         - type
4394 rajveer 549
         - description
4447 rajveer 550
         - warehouseId
483 rajveer 551
        """
766 rajveer 552
        try:
4447 rajveer 553
            add_alert(type, warehouseId, description)
766 rajveer 554
        finally:
555
            close_session()
1596 ankur.sing 556
 
4444 rajveer 557
    def markAlertsAsSeen(self, warehouseId):
558
        """
559
        Parameters:
560
         - warehouseId
561
        """
562
        try:
563
            mark_alerts_as_seen(warehouseId)
564
        finally:
565
            close_session()
566
        pass
567
 
1596 ankur.sing 568
    def getValidOrderCount(self, ):
569
        """
570
        Return the number of valid orders. (OrderStatus >= OrderStatus.SUBMITTED_FOR_PROCESSING)
571
        """
1731 ankur.sing 572
        try:
573
            return get_valid_order_count()
574
        finally:
575
            close_session()
1627 ankur.sing 576
 
577
    def getNoOfCustomersWithSuccessfulTransaction(self, ):
578
        """
579
        Returns the number of distinct customers who have done successful transactions
580
        """
1731 ankur.sing 581
        try:
582
            return get_cust_count_with_successful_txn()
583
        finally:
584
            close_session()
1627 ankur.sing 585
 
1731 ankur.sing 586
 
587
    def getValidOrdersAmountRange(self, ):
1627 ankur.sing 588
        """
1731 ankur.sing 589
        Returns the minimum and maximum amounts of a valid order. (OrderStatus >= OrderStatus.SUBMITTED_FOR_PROCESSING)
590
        List contains two values, first minimum amount and second maximum amount.
1627 ankur.sing 591
        """
1731 ankur.sing 592
        try:
593
            return get_valid_orders_amount_range()
594
        finally:
595
            close_session()
1627 ankur.sing 596
 
1886 ankur.sing 597
    def getValidOrders(self, limit):
598
        """
599
        Returns list of Orders in descending order by Order creation date. List is restricted to limit Orders.
600
        If limit is passed as 0, then all valid Orders are returned.
601
 
602
        Parameters:
603
         - limit
604
        """
605
        try:
606
            return [to_t_order(order) for order in get_valid_orders(limit)]
607
        finally:
608
            close_session()
104 ashish 609
 
2536 chandransh 610
    def toggleDOAFlag(self, orderId):
104 ashish 611
        """
2536 chandransh 612
        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.
613
        Returns the final flag status.
614
        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 615
 
132 ashish 616
        Parameters:
2536 chandransh 617
         - orderId
132 ashish 618
        """
2536 chandransh 619
        try:
620
            return toggle_doa_flag(orderId)
621
        finally:
622
            close_session()
483 rajveer 623
 
4454 rajveer 624
    def markOrderDoaRequestReceived(self, orderId):
625
        """
626
        Once user raise the request for a DOA, order status will be changed from DELVIERY_SUCCESS to DOA_REQUEST_RECEIVED
627
 
628
        Parameters:
629
         - orderId
630
        """
631
        try:
632
            return mark_order_doa_request_received(orderId)
633
        finally:
634
            close_session()
635
 
636
    def markOrderDoaRequestAuthorized(self, orderId, isAuthorized):
637
        """
638
        CRM person can authorize or deny the request reised by customer. If he authorizes order will change from DOA_REQUEST_RECEIVED
639
        to DOA_REQUEST_AUTHORIZED. If he denies, status will be changed back to DELVIERY_SUCCESS.
640
 
641
        Parameters:
642
         - orderId
643
         - isAuthorized
644
        """
645
        try:
646
            return mark_order_doa_request_authorized(orderId, isAuthorized)
647
        finally:
648
            close_session()
649
 
4488 rajveer 650
    def markOrderReturnRequestReceived(self, orderId):
651
        """
652
        Once user raise the request for a DOA, order status will be changed from DELVIERY_SUCCESS to RET_REQUEST_RECEIVED
653
 
654
        Parameters:
655
         - orderId
656
        """
657
        try:
658
            return mark_order_return_request_received(orderId)
659
        finally:
660
            close_session()
661
 
662
    def markOrderReturnRequestAuthorized(self, orderId, isAuthorized):
663
        """
664
        CRM person can authorize or deny the request reised by customer. If he authorizes order will change from RET_REQUEST_RECEIVED
665
        to RET_REQUEST_AUTHORIZED. If he denies, status will be changed back to DELVIERY_SUCCESS.
666
 
667
        Parameters:
668
         - orderId
669
         - isAuthorized
670
        """
671
        try:
672
            return mark_order_return_request_authorized(orderId, isAuthorized)
673
        finally:
674
            close_session()
675
 
4579 rajveer 676
    def requestPickupNumber(self, orderId, providerId):
104 ashish 677
        """
2536 chandransh 678
        Sends out an email to the account manager of the original courier provider used to ship the order.
4452 rajveer 679
        If the order status was DELIVERY_SUCCESS, it is changed to be DOA_PICKUP_REQUEST_RAISED.
680
        If the order status was DOA_PICKUP_REQUEST_RAISED, it is left unchanged.
2536 chandransh 681
        For any other status, it returns false.
682
        Throws an exception if the order with the given id couldn't be found.
104 ashish 683
 
684
        Parameters:
2536 chandransh 685
         - orderId
104 ashish 686
        """
2536 chandransh 687
        try:
4579 rajveer 688
            return request_pickup_number(orderId, providerId)
2536 chandransh 689
        finally:
690
            close_session()
483 rajveer 691
 
4602 rajveer 692
    def authorizePickup(self, orderId, pickupNumber, providerId):
304 ashish 693
        """
4452 rajveer 694
        If the order status is DOA_PICKUP_REQUEST_RAISED, it does the following
2536 chandransh 695
            1. Sends out an email to the customer with the dispatch advice that he has to print as an attachment.
696
            2. Changes order status to be DOA_PICKUP_AUTHORIZED.
697
            3. Returns true
698
        If the order is any other status, it returns false.
699
        Throws an exception if the order with the given id couldn't be found.
304 ashish 700
 
701
        Parameters:
2536 chandransh 702
         - orderId
703
         - pickupNumber
304 ashish 704
        """
2536 chandransh 705
        try:
4602 rajveer 706
            return authorize_pickup(orderId, pickupNumber, providerId)
2536 chandransh 707
        finally:
708
            close_session()    
2764 chandransh 709
 
710
    def markDoasAsPickedUp(self, providerId, pickupDetails):
711
        """
712
        Marks all DOA_PICKUP_AUTHORIZED orders of the previous day for a provider as DOA_RETURN_IN_TRANSIT.
713
        Returns a list of orders that were authorized for pickup but did not appear in the pick-up report.
714
 
715
        Parameters:
716
         - providerId
717
         - pickupDetails
718
        """
719
        try:
720
            orders_not_picked_up = mark_doas_as_picked_up(providerId, pickupDetails)
721
            return [to_t_order(order) for order in orders_not_picked_up]
722
        finally:
723
            close_session()
2591 chandransh 724
 
4479 rajveer 725
    def receiveReturn(self, orderId, receiveCondition):
2591 chandransh 726
        """
4452 rajveer 727
        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 728
        If the order is in any other state, it returns false.
729
        Throws an exception if the order with the given id couldn't be found.
730
 
731
        Parameters:
732
         - orderId
733
        """
734
        try:
4479 rajveer 735
            return receive_return(orderId, receiveCondition)
2591 chandransh 736
        finally:
737
            close_session()
738
 
739
    def validateDoa(self, orderId, isValid):
740
        """
4452 rajveer 741
        Used to validate the DOA certificate for an order in the DOA_RECEIVED_PRESTINE state. If the certificate is valid,
2609 chandransh 742
        the order state is changed to DOA_CERT_PENDING.
2591 chandransh 743
        If the certificate is invalid, the order state is changed to DOA_CERT_INVALID.
744
        If the order is in any other state, it returns false.
745
        Throws an exception if the order with the given id couldn't be found.
746
 
747
        Parameters:
748
         - orderId
749
         - isValid
750
        """
751
        try:
752
            return validate_doa(orderId, isValid)
753
        finally:
754
            close_session()
2628 chandransh 755
 
4495 rajveer 756
    def validateReturnProduct(self, orderId, isUsable):
757
        """
758
        Parameters:
759
         - orderId
760
         - isUsable
761
        """
762
        try:
763
            return validate_return_product(orderId, isUsable)
764
        finally:
765
            close_session()
766
 
2628 chandransh 767
    def reshipOrder(self, orderId):
768
        """
4484 rajveer 769
        If the order is in RTO_RECEIVED_PRESTINE or DOA_CERT_INVALID state, it does the following:
2628 chandransh 770
            1. Creates a new order for processing in the BILLED state. All billing information is saved.
4484 rajveer 771
            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 772
 
773
        If the order is in DOA_CERT_VALID state, it does the following:
774
            1. Creates a new order for processing in the SUBMITTED_FOR_PROCESSING state.
775
            2. Creates a return order for the warehouse executive to return the DOA material.
4452 rajveer 776
            3. Marks the current order as the final DOA_VALID_RESHIPPED state.
2628 chandransh 777
 
778
        Returns the id of the newly created order.
779
 
780
        Throws an exception if the order with the given id couldn't be found.
781
 
782
        Parameters:
783
         - orderId
784
        """
785
        try:
786
            return reship_order(orderId)
787
        finally:
788
            close_session()
789
 
3226 chandransh 790
    def refundOrder(self, orderId, refundedBy, reason):
2628 chandransh 791
        """
4484 rajveer 792
        If the order is in RTO_RECEIVED_PRESTINE, DOA_CERT_VALID or DOA_CERT_INVALID state, it does the following:
2628 chandransh 793
            1. Creates a refund request for batch processing.
794
            2. Creates a return order for the warehouse executive to return the shipped material.
4484 rajveer 795
            3. Marks the current order as RTO_REFUNDED, DOA_VALID_REFUNDED or DOA_INVALID_REFUNDED final states.
2628 chandransh 796
 
797
        If the order is in SUBMITTED_FOR_PROCESSING or INVENTORY_LOW state, it does the following:
798
            1. Creates a refund request for batch processing.
3226 chandransh 799
            2. Cancels the reservation of the item in the warehouse.
800
            3. Marks the current order as the REFUNDED final state.
2628 chandransh 801
 
3226 chandransh 802
        For all COD orders, if the order is in INIT, SUBMITTED_FOR_PROCESSING or INVENTORY_LOW state, it does the following:
803
            1. Cancels the reservation of the item in the warehouse.
804
            2. Marks the current order as CANCELED.
805
 
806
        In all cases, it updates the reason for cancellation or refund and the person who performed the action.
807
 
2628 chandransh 808
        Returns True if it is successful, False otherwise.
809
 
810
        Throws an exception if the order with the given id couldn't be found.
811
 
812
        Parameters:
813
         - orderId
3226 chandransh 814
         - refundedBy
815
         - reason
2628 chandransh 816
        """
817
        try:
3226 chandransh 818
            return refund_order(orderId, refundedBy, reason)
2628 chandransh 819
        finally:
820
            close_session()
821
 
2697 chandransh 822
    def getReturnOrders(self, warehouseId, fromDate, toDate):
823
        """
824
        Get all return orders created between the from and to dates for the given warehouse.
825
        Ignores the warehouse if it is passed as -1.
826
 
827
        Parameters:
828
         - warehouseId
829
         - fromDate
830
         - toDate
831
        """
832
        try:
833
            from_date, to_date = get_fdate_tdate(fromDate, toDate)
834
            return get_return_orders(warehouseId, from_date, to_date)
835
        finally:
836
            close_session()
837
 
2700 chandransh 838
    def getReturnOrder(self, id):
839
        """
840
        Returns the ReturnOrder corresponding to the given id.
841
        Throws an exception if the return order with the given id couldn't be found.
842
 
843
        Parameters:
844
         - id
845
        """
846
        try:
847
            return get_return_order(id)
848
        finally:
849
            close_session()
850
 
2697 chandransh 851
    def processReturn(self, returnOrderId):
852
        """
853
        Marks the return order with the given id as processed. Raises an exception if no such return order exists.
854
 
855
        Parameters:
856
         - returnOrderId
857
        """
858
        try:
859
            process_return(returnOrderId)
860
        finally:
861
            close_session()
2591 chandransh 862
 
2819 chandransh 863
    def createPurchaseOrder(self, warehouseId):
864
        """
865
        Creates a purchase order corresponding to all the pending orders of the given warehouse.
866
        Returns the PO no. of the newly created purchase order.
867
 
868
        Parameters:
869
         - warehouseId
870
        """
871
        try:
872
            return create_purchase_order(warehouseId)
873
        finally:
874
            close_session()
3451 chandransh 875
 
876
    def updateWeight(self, orderId, weight):
877
        """
878
        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.
879
 
880
        Parameters:
881
         - orderId
882
         - weight
883
        """
884
        try:
885
            order = update_weight(orderId, weight)
886
            return to_t_order(order)
887
        finally:
888
            close_session()
889
 
3469 chandransh 890
    def changeItem(self, orderId, itemId):
891
        """
892
        Change the item to be shipped for this order. Also adjusts the reservation in the inventory accordingly.
893
        Currently, it also ensures that only a different color of the given item is shipped.
894
 
895
        Parameters:
896
         - orderId
897
         - itemId
898
        """
899
        try:
900
            order = change_product(orderId, itemId)
901
            return to_t_order(order)
902
        finally:
903
            close_session()
904
 
905
    def shiftToWarehouse(self, orderId, warehouseId):
906
        """
907
        Moves the given order to the given warehouse. Also adjusts the inventory reservations accordingly.
908
 
909
        Parameters:
910
         - orderId
911
         - warehouseId
912
        """
913
        try:
914
            order = change_warehouse(orderId, warehouseId)
915
            return to_t_order(order)
916
        finally:
917
            close_session()
3553 chandransh 918
 
4647 rajveer 919
    def addDelayReason(self, orderId, delayReason, furtherDelay, delayReasonText):
3553 chandransh 920
        """
921
        Adds the given delay reason to the given order.
3986 chandransh 922
        Increases the expected delivery time of the given order by the given no. of days.
3553 chandransh 923
        Raises an exception if no order with the given id can be found.
3469 chandransh 924
 
3553 chandransh 925
        Parameters:
926
         - orderId
927
         - delayReason
928
        """
929
        try:
4647 rajveer 930
            return add_delay_reason(orderId, delayReason, furtherDelay, delayReasonText)
3553 chandransh 931
        finally:
932
            close_session()
933
 
3956 chandransh 934
    def reconcileCodCollection(self, collectedAmountMap, xferBy, xferTxnId, xferDate):
935
        """
936
        Marks the COD orders with given AWB nos. as having been processed.
937
        Updates the captured amount for the corresponding payment.
938
 
939
        Returns a map of AWBs which were not processed and the associated reason. An AWB is not processed if:
940
        1. There is no order corresponding to an AWB number.
941
        2. The captured amount for a payment exceeds the total payment.
942
        3. The order corresponding to an AWB no. is in a state prior to DELIVERY_SUCCESS.
943
 
944
        Parameters:
945
         - collectedAmountMap
946
         - xferBy
947
         - xferTxnId
948
         - xferDate
949
        """
950
        try:
951
            return reconcile_cod_collection(collectedAmountMap, xferBy, xferTxnId, xferDate)
952
        finally:
953
            close_session()
4008 mandeep.dh 954
 
955
    def getTransactionsRequiringExtraProcessing(self, category):
956
        """
957
        Returns the list of transactions that require some extra processing and
958
        which belong to a particular category. This is currently used by CRM
959
        application.
960
        """
961
        try:
962
            return get_transactions_requiring_extra_processing(category)
963
        finally:
964
            close_session()
965
 
966
    def markTransactionAsProcessed(self, transactionId, category):
967
        """
968
        Marks a particular transaction as processed for a particular category.
969
        It essentially deletes the transaction if it is processed for a particular
970
        category. This is currently used by CRM application.
971
        """
972
        try:
973
            return mark_transaction_as_processed(transactionId, category)
974
        finally:
975
            close_session()
4018 chandransh 976
 
977
    def getItemWiseRiskyOrdersCount(self, ):
978
        """
979
        Returns a map containing the number of risky orders keyed by item id. A risky order
980
        is defined as one whose shipping date is about to expire.
981
        """
982
        try:
983
            return get_item_wise_risky_orders_count()
984
        finally:
985
            close_session()
3956 chandransh 986
 
4247 rajveer 987
    def markOrderCancellationRequestReceived(self, orderId):
988
        """
989
        Mark order as cancellation request received. If customer sends request of cancellation of
990
        a particular order, this method will be called. It will just change status of the order
991
        depending on its current status. It also records the previous status, so that we can move
992
        back to that status if cancellation request is denied.
993
 
994
        Parameters:
995
         - orderId
996
        """
997
        try:
998
            return mark_order_cancellation_request_received(orderId)
999
        finally:
1000
            close_session()
1001
 
1002
 
4662 rajveer 1003
    def markOrderAsLostInTransit(self, orderId):
1004
        """
1005
        Parameters:
1006
         - orderId
1007
        """
1008
        try:
1009
            return mark_order_as_lost_in_transit(orderId)
1010
        finally:
1011
            close_session()
1012
 
4258 rajveer 1013
    def markTransactionAsPaymentFlagRemoved(self, transactionId):
4247 rajveer 1014
        """
4258 rajveer 1015
        If we and/or payment gateway has decided to accept the payment, this method needs to be called.
1016
        Changed transaction and all orders status to payment accepted.
4247 rajveer 1017
 
1018
        Parameters:
4258 rajveer 1019
         - transactionId
4247 rajveer 1020
        """
1021
        try:
4259 anupam.sin 1022
            return mark_transaction_as_payment_flag_removed(transactionId)
4247 rajveer 1023
        finally:
1024
            close_session()
4259 anupam.sin 1025
 
1026
    def refundTransaction(self, transactionId, refundedBy, reason):
1027
        """
1028
        This method is called when a flagged payment is deemed unserviceable and the corresponding orders
1029
        need to be cancelled
1030
 
1031
        Parameters:
1032
         - transactionId
1033
        """
1034
        try:
1035
            return refund_transaction(transactionId, refundedBy, reason)
1036
        finally:
1037
            close_session()
4247 rajveer 1038
 
1039
    def markOrderCancellationRequestDenied(self, orderId):
1040
        """
1041
        If we decide to not to cancel order, we will move the order ro previous status.
1042
 
1043
        Parameters:
1044
         - orderId
1045
        """
1046
        try:
1047
            return mark_order_cancellation_request_denied(orderId)
1048
        finally:
1049
            close_session()
1050
 
1051
    def markOrderCancellationRequestConfirmed(self, orderId):
1052
        """
1053
        If we decide to to cancel order, CRM will call this method to move the status of order to
1054
        cancellation request confirmed. After this OM will be able to cancel the order.
1055
 
1056
        Parameters:
1057
         - orderId
1058
        """
1059
        try:
1060
            return mark_order_cancellation_request_confirmed(orderId)
1061
        finally:
1062
            close_session()
1063
 
4324 mandeep.dh 1064
    def updateShipmentAddress(self, orderId, addressId):
1065
        """
1066
        Updates shipment address of an order. Delivery and shipping date estimates
1067
        etc. are also updated here.
1068
 
1069
        Throws TransactionServiceException in case address change is not
1070
        possible due to certain reasons such as new pincode in address is
1071
        not serviceable etc.
1072
 
1073
        Parameters:
1074
         - orderId
1075
         - addressId
1076
        """
1077
        try:
1078
            update_shipment_address(orderId, addressId)
1079
        finally:
1080
            close_session()
1081
 
4285 rajveer 1082
    def acceptOrdersForItemId(self, itemId, inventory):
1083
        """
1084
        Marks the orders as ACCEPTED for the given itemId and inventory. It also updates the accepted timestamp. If the
1085
        given order is not a COD order, it also captures the payment if the same has not been captured.
1086
 
1087
        Parameters:
1088
         - itemId
1089
         - inventory
1090
        """
1091
        try:
1092
            return accept_orders_for_item_id(itemId, inventory)
1093
        finally:
1094
            close_session()
1095
 
4303 rajveer 1096
 
1097
 
4369 rajveer 1098
    def markOrdersAsPORaised(self, vendorId, itemId, quantity, estimate, isReminder):
4303 rajveer 1099
        """
1100
        Parameters:
1101
         - vendorId
1102
         - itemId
1103
         - quantity
1104
         - estimate
4369 rajveer 1105
         - isReminder
4303 rajveer 1106
        """
1107
        try:
4369 rajveer 1108
            return mark_orders_as_po_raised(vendorId, itemId, quantity, estimate, isReminder)
4303 rajveer 1109
        finally:
1110
            close_session()
1111
 
4369 rajveer 1112
    def markOrdersAsReversalInitiated(self, vendorId, itemId, quantity, estimate, isReminder):
4303 rajveer 1113
        """
1114
        Parameters:
1115
         - vendorId
1116
         - itemId
1117
         - quantity
1118
         - estimate
4369 rajveer 1119
         - isReminder
4303 rajveer 1120
        """
1121
        try:
4369 rajveer 1122
            return mark_orders_as_reversal_initiated(vendorId, itemId, quantity, estimate, isReminder)
4303 rajveer 1123
        finally:
1124
            close_session()
1125
 
4369 rajveer 1126
    def markOrdersAsNotAvailabke(self, vendorId, itemId, quantity, estimate, isReminder):
4303 rajveer 1127
        """
1128
        Parameters:
1129
         - vendorId
1130
         - itemId
1131
         - quantity
1132
         - estimate
4369 rajveer 1133
         - isReminder
4303 rajveer 1134
        """
1135
        try:
4369 rajveer 1136
            return mark_orders_as_not_available(vendorId, itemId, quantity, estimate, isReminder)
4303 rajveer 1137
        finally:
1138
            close_session()
1139
 
1140
 
4369 rajveer 1141
    def markOrdersAsTimeout(self, vendorId):
1142
        """
1143
        Parameters:
1144
         - vendorId
1145
        """
1146
        try:
1147
            return mark_orders_as_timeout(vendorId)
1148
        finally:
1149
            close_session()
4386 anupam.sin 1150
 
1151
    def getOrderForAwb(self, awb):
1152
        """
1153
        Parameters:
1154
         - AirwayBill Number
1155
        """
1156
        try:
1157
            return to_t_order(get_order_for_awb(awb))
1158
        finally:
1159
            close_session()
4369 rajveer 1160
 
4506 phani.kuma 1161
    def getOrdersForProviderForStatus(self, provider_id, order_status):
1162
        """
1163
        Parameters:
1164
         - provider id
1165
         - order status
1166
        """
1167
        try:
1168
            orders_of_provider_by_status = get_orders_for_provider_for_status(provider_id, order_status)
1169
            return [to_t_order(order) for order in orders_of_provider_by_status if order != None]
1170
        finally:
1171
            close_session()
4600 varun.gupt 1172
 
1173
    def getBilledOrdersForVendor(self, vendorId, billingDateFrom, billingDateTo):
1174
        '''
1175
        Parameters:
1176
         - vendorId
1177
         - billingDateFrom
1178
         - billingDateTo
1179
        '''
1180
        try:
1181
            return [to_t_order(order) for order in get_billed_orders_for_vendor(vendorId, to_py_date(billingDateFrom), to_py_date(billingDateTo))]
1182
        finally:
1183
            close_session()
1184
 
4607 rajveer 1185
    def getSlippedSippingDateOrders(self):
1186
        try:
1187
            return [to_t_order(order) for order in get_slipped_sipping_date_orders()]
1188
        finally:
1189
            close_session()
1190
 
4600 varun.gupt 1191
    def getEBSSettlementSummaries(self):
1192
        try:
1193
            return get_ebs_settlement_summaries()
1194
        finally:
1195
            close_session()
4369 rajveer 1196
 
4600 varun.gupt 1197
    def saveEBSSettlementSummary(self, settlementId, settlementDate, transactionDateFrom, transactionDateTo, amount):
1198
        try:
1199
            save_ebs_settlement_summary(settlementId, to_py_date(settlementDate), to_py_date(transactionDateFrom), to_py_date(transactionDateTo), amount)
1200
        finally:
1201
            close_session()
1202
 
1203
    def getSettlementForPaymentId(self, paymentId):
1204
        '''
1205
        Parameters:
1206
         - paymentId
1207
        '''
1208
        try:
1209
            return to_t_payment_settlement(get_settlement_for_paymentId(paymentId))
1210
        finally:
1211
            close_session()
1212
 
1213
    def getEBSSettlementDate(self, settlementId):
1214
        try:
1215
            return to_java_date(get_ebs_settlement_date(settlementId))
1216
        finally:
1217
            close_session()
1218
 
1219
    def savePaymentSettlements(self, settlementDate, paymentGatewayId, paymentId, serviceTax, otherCharges, netCollection):
1220
        try:
1221
            save_payment_settlements(to_py_date(settlementDate), paymentGatewayId, paymentId, serviceTax, otherCharges, netCollection)
1222
        finally:
1223
            close_session()
1224
 
1225
    def markEBSSettlementUploaded(self, settlementId):
1226
        try:
1227
            mark_ebs_settlement_uploaded(settlementId)
1228
        finally:
1229
            close_session()
1230
 
2536 chandransh 1231
    def closeSession(self, ):
1232
        close_session()
3376 rajveer 1233
 
1234
    def isAlive(self, ):
1235
        """
1236
        For checking weather service is active alive or not. It also checks connectivity with database
1237
        """
1238
        try:
1239
            return is_alive()
1240
        finally:
4247 rajveer 1241
            close_session()