Subversion Repositories SmartDukaan

Rev

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