Subversion Repositories SmartDukaan

Rev

Rev 4712 | Rev 4741 | 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,\
4715 varun.gupt 35
    mark_order_return_request_received, mark_order_return_request_authorized, add_invoice_number, get_reshipped_order_ids, \
4600 varun.gupt 36
    validate_return_product, get_orders_for_provider_for_status, save_ebs_settlement_summary, get_ebs_settlement_date, \
4715 varun.gupt 37
    save_payment_settlements, mark_ebs_settlement_uploaded, get_billed_orders_for_vendor, get_settlement_for_paymentId, \
38
    get_settlements_by_date, get_slipped_sipping_date_orders, mark_order_as_lost_in_transit,\
4712 rajveer 39
    get_cancelled_orders, mark_order_as_delivered
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
 
4712 rajveer 470
    def markOrderAsDelivered(self, orderId, deliveryTimestamp, receiver):
471
        """
472
        Attributes:
473
         - orderId
474
         - deliveryTimestamp
475
         - receiver
476
        """
477
        try:
478
            mark_order_as_delivered(orderId, to_py_date(deliveryTimestamp), receiver)
479
        finally:
480
            close_session()
481
 
482
 
1135 chandransh 483
    def markOrdersAsFailed(self, providerId, returnedOrders):
484
        """
485
        Mark all orders with AWBs in the given map as failed. Also sets the delivery timestamp.
486
        Raises an exception if we encounter report for an AWB number that we did not ship.
487
 
488
        Parameters:
489
         - providerId
490
         - returnedOrders
491
        """
492
        try:
493
            mark_orders_as_failed(providerId, returnedOrders)
494
        finally:
495
            close_session()
1246 chandransh 496
 
497
    def updateNonDeliveryReason(self, providerId, undeliveredOrders):
498
        """
499
        Update the status description of orders whose AWB numbers are keys of the Map.
500
 
501
        Parameters:
502
         - providerId
503
         - undelivered_orders
504
        """
505
        try:
4581 phani.kuma 506
            orders_not_delivered = update_non_delivery_reason(providerId, undeliveredOrders)
507
            return [to_t_order(order) for order in orders_not_delivered]
1246 chandransh 508
        finally:
509
            close_session()
1405 ankur.sing 510
 
511
    def getUndeliveredOrders(self, providerId, warehouseId):
512
        """
513
        Returns the list of orders whose delivery time has passed but have not been
514
        delivered yet for the given provider and warehouse. To get a complete list of
515
        undelivered orders, pass them as -1.
516
        Returns an empty list if no such orders exist.
517
 
518
        Parameters:
519
         - providerId
520
         - warehouseId
521
        """
522
        try:
523
            undelivered_orders = get_undelivered_orders(providerId, warehouseId)
524
            return [to_t_order(order) for order in undelivered_orders]
525
        finally:
526
            close_session()
1351 varun.gupt 527
 
1398 varun.gupt 528
    def enqueueTransactionInfoEmail(self, transactionId):
1351 varun.gupt 529
        """
1398 varun.gupt 530
        Save the email containing order details to be sent to customer later by batch job
1351 varun.gupt 531
 
532
        Parameters:
533
         - transactionId
534
        """
535
        try:
1398 varun.gupt 536
            return enqueue_transaction_info_email(transactionId)
1351 varun.gupt 537
        finally:
538
            close_session()
539
 
4444 rajveer 540
    def getAlerts(self, type, warehouseId, status, timestamp):
483 rajveer 541
        """
542
        Parameters:
4394 rajveer 543
         - type
4444 rajveer 544
         - warehouseId
4394 rajveer 545
         - status
546
         - timestamp
483 rajveer 547
        """
766 rajveer 548
        try:
4444 rajveer 549
            alerts = get_alerts(type, warehouseId, status, timestamp)
766 rajveer 550
 
551
            t_alerts = []
552
            for alert in alerts:
553
                t_alerts.append(to_t_alert(alert)) 
554
 
555
            return t_alerts
556
        finally:
557
            close_session()
4394 rajveer 558
 
4447 rajveer 559
    def addAlert(self, type, warehouseId, description):
483 rajveer 560
        """
561
        Parameters:
562
         - type
4394 rajveer 563
         - description
4447 rajveer 564
         - warehouseId
483 rajveer 565
        """
766 rajveer 566
        try:
4447 rajveer 567
            add_alert(type, warehouseId, description)
766 rajveer 568
        finally:
569
            close_session()
1596 ankur.sing 570
 
4444 rajveer 571
    def markAlertsAsSeen(self, warehouseId):
572
        """
573
        Parameters:
574
         - warehouseId
575
        """
576
        try:
577
            mark_alerts_as_seen(warehouseId)
578
        finally:
579
            close_session()
580
        pass
581
 
1596 ankur.sing 582
    def getValidOrderCount(self, ):
583
        """
584
        Return the number of valid orders. (OrderStatus >= OrderStatus.SUBMITTED_FOR_PROCESSING)
585
        """
1731 ankur.sing 586
        try:
587
            return get_valid_order_count()
588
        finally:
589
            close_session()
1627 ankur.sing 590
 
591
    def getNoOfCustomersWithSuccessfulTransaction(self, ):
592
        """
593
        Returns the number of distinct customers who have done successful transactions
594
        """
1731 ankur.sing 595
        try:
596
            return get_cust_count_with_successful_txn()
597
        finally:
598
            close_session()
1627 ankur.sing 599
 
1731 ankur.sing 600
 
601
    def getValidOrdersAmountRange(self, ):
1627 ankur.sing 602
        """
1731 ankur.sing 603
        Returns the minimum and maximum amounts of a valid order. (OrderStatus >= OrderStatus.SUBMITTED_FOR_PROCESSING)
604
        List contains two values, first minimum amount and second maximum amount.
1627 ankur.sing 605
        """
1731 ankur.sing 606
        try:
607
            return get_valid_orders_amount_range()
608
        finally:
609
            close_session()
1627 ankur.sing 610
 
1886 ankur.sing 611
    def getValidOrders(self, limit):
612
        """
613
        Returns list of Orders in descending order by Order creation date. List is restricted to limit Orders.
614
        If limit is passed as 0, then all valid Orders are returned.
615
 
616
        Parameters:
617
         - limit
618
        """
619
        try:
620
            return [to_t_order(order) for order in get_valid_orders(limit)]
621
        finally:
622
            close_session()
104 ashish 623
 
2536 chandransh 624
    def toggleDOAFlag(self, orderId):
104 ashish 625
        """
2536 chandransh 626
        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.
627
        Returns the final flag status.
628
        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 629
 
132 ashish 630
        Parameters:
2536 chandransh 631
         - orderId
132 ashish 632
        """
2536 chandransh 633
        try:
634
            return toggle_doa_flag(orderId)
635
        finally:
636
            close_session()
483 rajveer 637
 
4454 rajveer 638
    def markOrderDoaRequestReceived(self, orderId):
639
        """
640
        Once user raise the request for a DOA, order status will be changed from DELVIERY_SUCCESS to DOA_REQUEST_RECEIVED
641
 
642
        Parameters:
643
         - orderId
644
        """
645
        try:
646
            return mark_order_doa_request_received(orderId)
647
        finally:
648
            close_session()
649
 
650
    def markOrderDoaRequestAuthorized(self, orderId, isAuthorized):
651
        """
652
        CRM person can authorize or deny the request reised by customer. If he authorizes order will change from DOA_REQUEST_RECEIVED
653
        to DOA_REQUEST_AUTHORIZED. If he denies, status will be changed back to DELVIERY_SUCCESS.
654
 
655
        Parameters:
656
         - orderId
657
         - isAuthorized
658
        """
659
        try:
660
            return mark_order_doa_request_authorized(orderId, isAuthorized)
661
        finally:
662
            close_session()
663
 
4488 rajveer 664
    def markOrderReturnRequestReceived(self, orderId):
665
        """
666
        Once user raise the request for a DOA, order status will be changed from DELVIERY_SUCCESS to RET_REQUEST_RECEIVED
667
 
668
        Parameters:
669
         - orderId
670
        """
671
        try:
672
            return mark_order_return_request_received(orderId)
673
        finally:
674
            close_session()
675
 
676
    def markOrderReturnRequestAuthorized(self, orderId, isAuthorized):
677
        """
678
        CRM person can authorize or deny the request reised by customer. If he authorizes order will change from RET_REQUEST_RECEIVED
679
        to RET_REQUEST_AUTHORIZED. If he denies, status will be changed back to DELVIERY_SUCCESS.
680
 
681
        Parameters:
682
         - orderId
683
         - isAuthorized
684
        """
685
        try:
686
            return mark_order_return_request_authorized(orderId, isAuthorized)
687
        finally:
688
            close_session()
689
 
4579 rajveer 690
    def requestPickupNumber(self, orderId, providerId):
104 ashish 691
        """
2536 chandransh 692
        Sends out an email to the account manager of the original courier provider used to ship the order.
4452 rajveer 693
        If the order status was DELIVERY_SUCCESS, it is changed to be DOA_PICKUP_REQUEST_RAISED.
694
        If the order status was DOA_PICKUP_REQUEST_RAISED, it is left unchanged.
2536 chandransh 695
        For any other status, it returns false.
696
        Throws an exception if the order with the given id couldn't be found.
104 ashish 697
 
698
        Parameters:
2536 chandransh 699
         - orderId
104 ashish 700
        """
2536 chandransh 701
        try:
4579 rajveer 702
            return request_pickup_number(orderId, providerId)
2536 chandransh 703
        finally:
704
            close_session()
483 rajveer 705
 
4602 rajveer 706
    def authorizePickup(self, orderId, pickupNumber, providerId):
304 ashish 707
        """
4452 rajveer 708
        If the order status is DOA_PICKUP_REQUEST_RAISED, it does the following
2536 chandransh 709
            1. Sends out an email to the customer with the dispatch advice that he has to print as an attachment.
710
            2. Changes order status to be DOA_PICKUP_AUTHORIZED.
711
            3. Returns true
712
        If the order is any other status, it returns false.
713
        Throws an exception if the order with the given id couldn't be found.
304 ashish 714
 
715
        Parameters:
2536 chandransh 716
         - orderId
717
         - pickupNumber
304 ashish 718
        """
2536 chandransh 719
        try:
4602 rajveer 720
            return authorize_pickup(orderId, pickupNumber, providerId)
2536 chandransh 721
        finally:
722
            close_session()    
2764 chandransh 723
 
724
    def markDoasAsPickedUp(self, providerId, pickupDetails):
725
        """
726
        Marks all DOA_PICKUP_AUTHORIZED orders of the previous day for a provider as DOA_RETURN_IN_TRANSIT.
727
        Returns a list of orders that were authorized for pickup but did not appear in the pick-up report.
728
 
729
        Parameters:
730
         - providerId
731
         - pickupDetails
732
        """
733
        try:
734
            orders_not_picked_up = mark_doas_as_picked_up(providerId, pickupDetails)
735
            return [to_t_order(order) for order in orders_not_picked_up]
736
        finally:
737
            close_session()
2591 chandransh 738
 
4479 rajveer 739
    def receiveReturn(self, orderId, receiveCondition):
2591 chandransh 740
        """
4452 rajveer 741
        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 742
        If the order is in any other state, it returns false.
743
        Throws an exception if the order with the given id couldn't be found.
744
 
745
        Parameters:
746
         - orderId
747
        """
748
        try:
4479 rajveer 749
            return receive_return(orderId, receiveCondition)
2591 chandransh 750
        finally:
751
            close_session()
752
 
753
    def validateDoa(self, orderId, isValid):
754
        """
4452 rajveer 755
        Used to validate the DOA certificate for an order in the DOA_RECEIVED_PRESTINE state. If the certificate is valid,
2609 chandransh 756
        the order state is changed to DOA_CERT_PENDING.
2591 chandransh 757
        If the certificate is invalid, the order state is changed to DOA_CERT_INVALID.
758
        If the order is in any other state, it returns false.
759
        Throws an exception if the order with the given id couldn't be found.
760
 
761
        Parameters:
762
         - orderId
763
         - isValid
764
        """
765
        try:
766
            return validate_doa(orderId, isValid)
767
        finally:
768
            close_session()
2628 chandransh 769
 
4495 rajveer 770
    def validateReturnProduct(self, orderId, isUsable):
771
        """
772
        Parameters:
773
         - orderId
774
         - isUsable
775
        """
776
        try:
777
            return validate_return_product(orderId, isUsable)
778
        finally:
779
            close_session()
780
 
2628 chandransh 781
    def reshipOrder(self, orderId):
782
        """
4484 rajveer 783
        If the order is in RTO_RECEIVED_PRESTINE or DOA_CERT_INVALID state, it does the following:
2628 chandransh 784
            1. Creates a new order for processing in the BILLED state. All billing information is saved.
4484 rajveer 785
            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 786
 
787
        If the order is in DOA_CERT_VALID state, it does the following:
788
            1. Creates a new order for processing in the SUBMITTED_FOR_PROCESSING state.
789
            2. Creates a return order for the warehouse executive to return the DOA material.
4452 rajveer 790
            3. Marks the current order as the final DOA_VALID_RESHIPPED state.
2628 chandransh 791
 
792
        Returns the id of the newly created order.
793
 
794
        Throws an exception if the order with the given id couldn't be found.
795
 
796
        Parameters:
797
         - orderId
798
        """
799
        try:
800
            return reship_order(orderId)
801
        finally:
802
            close_session()
803
 
3226 chandransh 804
    def refundOrder(self, orderId, refundedBy, reason):
2628 chandransh 805
        """
4484 rajveer 806
        If the order is in RTO_RECEIVED_PRESTINE, DOA_CERT_VALID or DOA_CERT_INVALID state, it does the following:
2628 chandransh 807
            1. Creates a refund request for batch processing.
808
            2. Creates a return order for the warehouse executive to return the shipped material.
4484 rajveer 809
            3. Marks the current order as RTO_REFUNDED, DOA_VALID_REFUNDED or DOA_INVALID_REFUNDED final states.
2628 chandransh 810
 
811
        If the order is in SUBMITTED_FOR_PROCESSING or INVENTORY_LOW state, it does the following:
812
            1. Creates a refund request for batch processing.
3226 chandransh 813
            2. Cancels the reservation of the item in the warehouse.
814
            3. Marks the current order as the REFUNDED final state.
2628 chandransh 815
 
3226 chandransh 816
        For all COD orders, if the order is in INIT, SUBMITTED_FOR_PROCESSING or INVENTORY_LOW state, it does the following:
817
            1. Cancels the reservation of the item in the warehouse.
818
            2. Marks the current order as CANCELED.
819
 
820
        In all cases, it updates the reason for cancellation or refund and the person who performed the action.
821
 
2628 chandransh 822
        Returns True if it is successful, False otherwise.
823
 
824
        Throws an exception if the order with the given id couldn't be found.
825
 
826
        Parameters:
827
         - orderId
3226 chandransh 828
         - refundedBy
829
         - reason
2628 chandransh 830
        """
831
        try:
3226 chandransh 832
            return refund_order(orderId, refundedBy, reason)
2628 chandransh 833
        finally:
834
            close_session()
835
 
2697 chandransh 836
    def getReturnOrders(self, warehouseId, fromDate, toDate):
837
        """
838
        Get all return orders created between the from and to dates for the given warehouse.
839
        Ignores the warehouse if it is passed as -1.
840
 
841
        Parameters:
842
         - warehouseId
843
         - fromDate
844
         - toDate
845
        """
846
        try:
847
            from_date, to_date = get_fdate_tdate(fromDate, toDate)
848
            return get_return_orders(warehouseId, from_date, to_date)
849
        finally:
850
            close_session()
851
 
2700 chandransh 852
    def getReturnOrder(self, id):
853
        """
854
        Returns the ReturnOrder corresponding to the given id.
855
        Throws an exception if the return order with the given id couldn't be found.
856
 
857
        Parameters:
858
         - id
859
        """
860
        try:
861
            return get_return_order(id)
862
        finally:
863
            close_session()
864
 
2697 chandransh 865
    def processReturn(self, returnOrderId):
866
        """
867
        Marks the return order with the given id as processed. Raises an exception if no such return order exists.
868
 
869
        Parameters:
870
         - returnOrderId
871
        """
872
        try:
873
            process_return(returnOrderId)
874
        finally:
875
            close_session()
2591 chandransh 876
 
2819 chandransh 877
    def createPurchaseOrder(self, warehouseId):
878
        """
879
        Creates a purchase order corresponding to all the pending orders of the given warehouse.
880
        Returns the PO no. of the newly created purchase order.
881
 
882
        Parameters:
883
         - warehouseId
884
        """
885
        try:
886
            return create_purchase_order(warehouseId)
887
        finally:
888
            close_session()
3451 chandransh 889
 
890
    def updateWeight(self, orderId, weight):
891
        """
892
        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.
893
 
894
        Parameters:
895
         - orderId
896
         - weight
897
        """
898
        try:
899
            order = update_weight(orderId, weight)
900
            return to_t_order(order)
901
        finally:
902
            close_session()
903
 
3469 chandransh 904
    def changeItem(self, orderId, itemId):
905
        """
906
        Change the item to be shipped for this order. Also adjusts the reservation in the inventory accordingly.
907
        Currently, it also ensures that only a different color of the given item is shipped.
908
 
909
        Parameters:
910
         - orderId
911
         - itemId
912
        """
913
        try:
914
            order = change_product(orderId, itemId)
915
            return to_t_order(order)
916
        finally:
917
            close_session()
918
 
919
    def shiftToWarehouse(self, orderId, warehouseId):
920
        """
921
        Moves the given order to the given warehouse. Also adjusts the inventory reservations accordingly.
922
 
923
        Parameters:
924
         - orderId
925
         - warehouseId
926
        """
927
        try:
928
            order = change_warehouse(orderId, warehouseId)
929
            return to_t_order(order)
930
        finally:
931
            close_session()
3553 chandransh 932
 
4647 rajveer 933
    def addDelayReason(self, orderId, delayReason, furtherDelay, delayReasonText):
3553 chandransh 934
        """
935
        Adds the given delay reason to the given order.
3986 chandransh 936
        Increases the expected delivery time of the given order by the given no. of days.
3553 chandransh 937
        Raises an exception if no order with the given id can be found.
3469 chandransh 938
 
3553 chandransh 939
        Parameters:
940
         - orderId
941
         - delayReason
942
        """
943
        try:
4647 rajveer 944
            return add_delay_reason(orderId, delayReason, furtherDelay, delayReasonText)
3553 chandransh 945
        finally:
946
            close_session()
947
 
3956 chandransh 948
    def reconcileCodCollection(self, collectedAmountMap, xferBy, xferTxnId, xferDate):
949
        """
950
        Marks the COD orders with given AWB nos. as having been processed.
951
        Updates the captured amount for the corresponding payment.
952
 
953
        Returns a map of AWBs which were not processed and the associated reason. An AWB is not processed if:
954
        1. There is no order corresponding to an AWB number.
955
        2. The captured amount for a payment exceeds the total payment.
956
        3. The order corresponding to an AWB no. is in a state prior to DELIVERY_SUCCESS.
957
 
958
        Parameters:
959
         - collectedAmountMap
960
         - xferBy
961
         - xferTxnId
962
         - xferDate
963
        """
964
        try:
965
            return reconcile_cod_collection(collectedAmountMap, xferBy, xferTxnId, xferDate)
966
        finally:
967
            close_session()
4008 mandeep.dh 968
 
969
    def getTransactionsRequiringExtraProcessing(self, category):
970
        """
971
        Returns the list of transactions that require some extra processing and
972
        which belong to a particular category. This is currently used by CRM
973
        application.
974
        """
975
        try:
976
            return get_transactions_requiring_extra_processing(category)
977
        finally:
978
            close_session()
979
 
980
    def markTransactionAsProcessed(self, transactionId, category):
981
        """
982
        Marks a particular transaction as processed for a particular category.
983
        It essentially deletes the transaction if it is processed for a particular
984
        category. This is currently used by CRM application.
985
        """
986
        try:
987
            return mark_transaction_as_processed(transactionId, category)
988
        finally:
989
            close_session()
4018 chandransh 990
 
991
    def getItemWiseRiskyOrdersCount(self, ):
992
        """
993
        Returns a map containing the number of risky orders keyed by item id. A risky order
994
        is defined as one whose shipping date is about to expire.
995
        """
996
        try:
997
            return get_item_wise_risky_orders_count()
998
        finally:
999
            close_session()
3956 chandransh 1000
 
4247 rajveer 1001
    def markOrderCancellationRequestReceived(self, orderId):
1002
        """
1003
        Mark order as cancellation request received. If customer sends request of cancellation of
1004
        a particular order, this method will be called. It will just change status of the order
1005
        depending on its current status. It also records the previous status, so that we can move
1006
        back to that status if cancellation request is denied.
1007
 
1008
        Parameters:
1009
         - orderId
1010
        """
1011
        try:
1012
            return mark_order_cancellation_request_received(orderId)
1013
        finally:
1014
            close_session()
1015
 
1016
 
4662 rajveer 1017
    def markOrderAsLostInTransit(self, orderId):
1018
        """
1019
        Parameters:
1020
         - orderId
1021
        """
1022
        try:
1023
            return mark_order_as_lost_in_transit(orderId)
1024
        finally:
1025
            close_session()
1026
 
4258 rajveer 1027
    def markTransactionAsPaymentFlagRemoved(self, transactionId):
4247 rajveer 1028
        """
4258 rajveer 1029
        If we and/or payment gateway has decided to accept the payment, this method needs to be called.
1030
        Changed transaction and all orders status to payment accepted.
4247 rajveer 1031
 
1032
        Parameters:
4258 rajveer 1033
         - transactionId
4247 rajveer 1034
        """
1035
        try:
4259 anupam.sin 1036
            return mark_transaction_as_payment_flag_removed(transactionId)
4247 rajveer 1037
        finally:
1038
            close_session()
4259 anupam.sin 1039
 
1040
    def refundTransaction(self, transactionId, refundedBy, reason):
1041
        """
1042
        This method is called when a flagged payment is deemed unserviceable and the corresponding orders
1043
        need to be cancelled
1044
 
1045
        Parameters:
1046
         - transactionId
1047
        """
1048
        try:
1049
            return refund_transaction(transactionId, refundedBy, reason)
1050
        finally:
1051
            close_session()
4247 rajveer 1052
 
1053
    def markOrderCancellationRequestDenied(self, orderId):
1054
        """
1055
        If we decide to not to cancel order, we will move the order ro previous status.
1056
 
1057
        Parameters:
1058
         - orderId
1059
        """
1060
        try:
1061
            return mark_order_cancellation_request_denied(orderId)
1062
        finally:
1063
            close_session()
1064
 
1065
    def markOrderCancellationRequestConfirmed(self, orderId):
1066
        """
1067
        If we decide to to cancel order, CRM will call this method to move the status of order to
1068
        cancellation request confirmed. After this OM will be able to cancel the order.
1069
 
1070
        Parameters:
1071
         - orderId
1072
        """
1073
        try:
1074
            return mark_order_cancellation_request_confirmed(orderId)
1075
        finally:
1076
            close_session()
1077
 
4324 mandeep.dh 1078
    def updateShipmentAddress(self, orderId, addressId):
1079
        """
1080
        Updates shipment address of an order. Delivery and shipping date estimates
1081
        etc. are also updated here.
1082
 
1083
        Throws TransactionServiceException in case address change is not
1084
        possible due to certain reasons such as new pincode in address is
1085
        not serviceable etc.
1086
 
1087
        Parameters:
1088
         - orderId
1089
         - addressId
1090
        """
1091
        try:
1092
            update_shipment_address(orderId, addressId)
1093
        finally:
1094
            close_session()
1095
 
4285 rajveer 1096
    def acceptOrdersForItemId(self, itemId, inventory):
1097
        """
1098
        Marks the orders as ACCEPTED for the given itemId and inventory. It also updates the accepted timestamp. If the
1099
        given order is not a COD order, it also captures the payment if the same has not been captured.
1100
 
1101
        Parameters:
1102
         - itemId
1103
         - inventory
1104
        """
1105
        try:
1106
            return accept_orders_for_item_id(itemId, inventory)
1107
        finally:
1108
            close_session()
1109
 
4303 rajveer 1110
 
1111
 
4369 rajveer 1112
    def markOrdersAsPORaised(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_po_raised(vendorId, itemId, quantity, estimate, isReminder)
4303 rajveer 1123
        finally:
1124
            close_session()
1125
 
4369 rajveer 1126
    def markOrdersAsReversalInitiated(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_reversal_initiated(vendorId, itemId, quantity, estimate, isReminder)
4303 rajveer 1137
        finally:
1138
            close_session()
1139
 
4369 rajveer 1140
    def markOrdersAsNotAvailabke(self, vendorId, itemId, quantity, estimate, isReminder):
4303 rajveer 1141
        """
1142
        Parameters:
1143
         - vendorId
1144
         - itemId
1145
         - quantity
1146
         - estimate
4369 rajveer 1147
         - isReminder
4303 rajveer 1148
        """
1149
        try:
4369 rajveer 1150
            return mark_orders_as_not_available(vendorId, itemId, quantity, estimate, isReminder)
4303 rajveer 1151
        finally:
1152
            close_session()
1153
 
1154
 
4369 rajveer 1155
    def markOrdersAsTimeout(self, vendorId):
1156
        """
1157
        Parameters:
1158
         - vendorId
1159
        """
1160
        try:
1161
            return mark_orders_as_timeout(vendorId)
1162
        finally:
1163
            close_session()
4386 anupam.sin 1164
 
1165
    def getOrderForAwb(self, awb):
1166
        """
1167
        Parameters:
1168
         - AirwayBill Number
1169
        """
1170
        try:
1171
            return to_t_order(get_order_for_awb(awb))
1172
        finally:
1173
            close_session()
4369 rajveer 1174
 
4506 phani.kuma 1175
    def getOrdersForProviderForStatus(self, provider_id, order_status):
1176
        """
1177
        Parameters:
1178
         - provider id
1179
         - order status
1180
        """
1181
        try:
1182
            orders_of_provider_by_status = get_orders_for_provider_for_status(provider_id, order_status)
1183
            return [to_t_order(order) for order in orders_of_provider_by_status if order != None]
1184
        finally:
1185
            close_session()
4600 varun.gupt 1186
 
1187
    def getBilledOrdersForVendor(self, vendorId, billingDateFrom, billingDateTo):
1188
        '''
1189
        Parameters:
1190
         - vendorId
1191
         - billingDateFrom
1192
         - billingDateTo
1193
        '''
1194
        try:
1195
            return [to_t_order(order) for order in get_billed_orders_for_vendor(vendorId, to_py_date(billingDateFrom), to_py_date(billingDateTo))]
1196
        finally:
1197
            close_session()
1198
 
4607 rajveer 1199
    def getSlippedSippingDateOrders(self):
1200
        try:
1201
            return [to_t_order(order) for order in get_slipped_sipping_date_orders()]
1202
        finally:
1203
            close_session()
1204
 
4709 rajveer 1205
    def getCancelledOrders(self, cancelDateFrom, cancelDateTo):
1206
        try:
1207
            return [to_t_order(order) for order in get_cancelled_orders(to_py_date(cancelDateFrom), to_py_date(cancelDateTo))]
1208
        finally:
1209
            close_session()
1210
 
4600 varun.gupt 1211
    def getEBSSettlementSummaries(self):
1212
        try:
1213
            return get_ebs_settlement_summaries()
1214
        finally:
1215
            close_session()
4369 rajveer 1216
 
4600 varun.gupt 1217
    def saveEBSSettlementSummary(self, settlementId, settlementDate, transactionDateFrom, transactionDateTo, amount):
1218
        try:
1219
            save_ebs_settlement_summary(settlementId, to_py_date(settlementDate), to_py_date(transactionDateFrom), to_py_date(transactionDateTo), amount)
1220
        finally:
1221
            close_session()
1222
 
1223
    def getSettlementForPaymentId(self, paymentId):
1224
        '''
1225
        Parameters:
1226
         - paymentId
1227
        '''
1228
        try:
1229
            return to_t_payment_settlement(get_settlement_for_paymentId(paymentId))
1230
        finally:
1231
            close_session()
1232
 
1233
    def getEBSSettlementDate(self, settlementId):
1234
        try:
1235
            return to_java_date(get_ebs_settlement_date(settlementId))
1236
        finally:
1237
            close_session()
1238
 
1239
    def savePaymentSettlements(self, settlementDate, paymentGatewayId, paymentId, serviceTax, otherCharges, netCollection):
1240
        try:
1241
            save_payment_settlements(to_py_date(settlementDate), paymentGatewayId, paymentId, serviceTax, otherCharges, netCollection)
1242
        finally:
1243
            close_session()
1244
 
1245
    def markEBSSettlementUploaded(self, settlementId):
1246
        try:
1247
            mark_ebs_settlement_uploaded(settlementId)
1248
        finally:
1249
            close_session()
1250
 
4715 varun.gupt 1251
    def getSettlementsByDate(self, settlementDateFrom, settlementDateTo, isRefund):
1252
        try:
1253
            settlements = get_settlements_by_date(to_py_date(settlementDateFrom), to_py_date(settlementDateTo), isRefund)
1254
            return [to_t_payment_settlement(settlement) for settlement in settlements]
1255
        finally:
1256
            close_session()
1257
 
1258
    def getReshippedOrderIds(self, orderIds):
1259
        try:
1260
            return get_reshipped_order_ids(orderIds)
1261
        finally:
1262
            close_session()
1263
 
2536 chandransh 1264
    def closeSession(self, ):
1265
        close_session()
3376 rajveer 1266
 
1267
    def isAlive(self, ):
1268
        """
1269
        For checking weather service is active alive or not. It also checks connectivity with database
1270
        """
1271
        try:
1272
            return is_alive()
1273
        finally:
4247 rajveer 1274
            close_session()