Subversion Repositories SmartDukaan

Rev

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