Subversion Repositories SmartDukaan

Rev

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