Subversion Repositories SmartDukaan

Rev

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