Subversion Repositories SmartDukaan

Rev

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