Subversion Repositories SmartDukaan

Rev

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