Subversion Repositories SmartDukaan

Rev

Rev 4283 | Rev 4303 | 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,\
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,\
4285 rajveer 29
    mark_transaction_as_payment_flag_removed, accept_orders_for_item_id
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:
4285 rajveer 279
            return accept_order(orderId)
921 rajveer 280
        finally:
281
            close_session()
282
 
3014 chandransh 283
    def getOrdersForCustomer(self, customerId, from_date, to_date, statuses):
104 ashish 284
        """
3014 chandransh 285
        Returns list of orders for the given customer created between the given dates and having the given statuses.
286
        Pass and empty list to ignore filtering on statuses.
287
 
104 ashish 288
        Parameters:
289
         - customerId
290
         - from_date
291
         - to_date
3014 chandransh 292
         - statuses
104 ashish 293
        """
766 rajveer 294
        try:
295
            current_from_date, current_to_date = get_fdate_tdate(from_date, to_date)
296
 
3014 chandransh 297
            orders = get_orders_for_customer(customerId, current_from_date, current_to_date, statuses)
766 rajveer 298
            return [to_t_order(order) for order in orders]
299
        finally:
300
            close_session()
1528 ankur.sing 301
 
302
    def getOrderForCustomer(self, orderId, customerId):
303
        """
304
        Returns an order for the order Id. Also checks if the order belongs to the customer whose Id is passed.
305
        Throws exception if either order Id is invalid or order does not below to the customer whose Id is passed.
306
 
307
        Parameters:
308
         - customerId
309
         - orderId
310
        """
311
        try:
312
            return to_t_order(get_order_for_customer(orderId, customerId))
313
        finally:
314
            close_session()
766 rajveer 315
 
483 rajveer 316
    def getOrder(self, id):
317
        """
318
        Parameters:
319
         - id
320
        """
766 rajveer 321
        try:
322
            return to_t_order(get_order(id))
323
        finally:
324
            close_session()
325
 
483 rajveer 326
    def getLineItemsForOrder(self, orderId):
327
        """
328
        Parameters:
329
         - orderId
330
        """
766 rajveer 331
        try:
332
            lineitems = get_line_items_for_order(orderId)
333
            t_lineitems = []
334
            for lineitem in lineitems:
335
                t_lineitems.append(to_t_lineitem(lineitem))
336
            return t_lineitems
337
        finally:
338
            close_session()
1149 chandransh 339
 
4283 anupam.sin 340
    def addBillingDetails(self, orderId, invoice_number, imeiNumber, itemNumber, billedBy, jacketNumber, billingType, vendorId):
494 rajveer 341
        """
2783 chandransh 342
        Adds jacket number and IMEI no. to the order. Doesn't update the IMEI no. if a -1 is supplied.
343
        Also marks the order as billed and sets the billing timestamp.
344
        Return false if it doesn't find the order with the given ID.
1149 chandransh 345
 
346
        Parameters:
347
         - orderId
348
         - jacketNumber
2783 chandransh 349
         - imeiNumber
350
         - itemNumber
351
         - billedBy
1149 chandransh 352
        """
353
        try:
4285 rajveer 354
            return add_billing_details(orderId, invoice_number, imeiNumber, itemNumber, billedBy, jacketNumber, billingType, vendorId)
2783 chandransh 355
        except TransactionServiceException:
356
            print "ERROR: Couldn't find the order with the given id"
357
            return False
1149 chandransh 358
        finally:
359
            close_session()
1220 chandransh 360
 
361
    def batchOrders(self, warehouseId):
362
        """
363
        Create a batch of all the pending orders for the given warehouse.
364
        The returned list is orderd by created_timestamp.
365
        If there are no pending orders, an empty list is returned.
1208 chandransh 366
 
1220 chandransh 367
        Parameters:
368
         - warehouseId
369
        """
370
        try:
371
            pending_orders = batch_orders(warehouseId)
372
            return [to_t_order(order) for order in pending_orders]
373
        finally:
374
            close_session()
375
 
1208 chandransh 376
    def markOrderAsOutOfStock(self, orderId):
377
        """
378
        Mark the given order as out of stock. Throws an exception if the order with the given Id couldn't be found.
379
 
380
 
381
        Parameters:
382
         - orderId
383
        """
384
        try:
385
            return order_outofstock(orderId)
386
        finally:
387
            close_session()
388
 
3064 chandransh 389
    def markOrdersAsManifested(self, warehouseId, providerId, cod):
759 chandransh 390
        """
3064 chandransh 391
        Depending on the third parameter, marks either all prepaid or all cod orders BILLED by the
392
        given warehouse and were picked up by the given provider as SHIPPED_FROM_WH.
759 chandransh 393
 
394
        Parameters:
395
         - warehouseId
396
         - providerId
3064 chandransh 397
         - cod
759 chandransh 398
        """
766 rajveer 399
        try:
3064 chandransh 400
            return mark_orders_as_manifested(warehouseId, providerId, cod)
766 rajveer 401
        finally:
402
            close_session()
1113 chandransh 403
 
404
    def markOrdersAsPickedUp(self, providerId, pickupDetails):
405
        """
406
        Marks all SHIPPED_FROM_WH orders of the previous day for a provider as SHIPPED_TO_LOGISTICS.
407
        Returns a list of orders that were shipped from warehouse but did not appear in the pick-up report.
408
        Raises an exception if we encounter report for an AWB number that we did not ship.
409
 
410
        Parameters:
411
         - providerId
412
         - pickupDetails
413
        """
414
        try:
415
            orders_not_picked_up = mark_orders_as_picked_up(providerId, pickupDetails)
416
            return [to_t_order(order) for order in orders_not_picked_up]
417
        finally:
418
            close_session()
1132 chandransh 419
 
420
    def markOrdersAsDelivered(self, providerId, deliveredOrders):
421
        """
422
        Marks all orders with AWBs in the given map as delivered. Also sets the delivery timestamp and
423
        the name of the receiver.
424
        Raises an exception if we encounter report for an AWB number that we did not ship.
425
 
426
        Parameters:
427
         - providerId
428
         - deliveredOrders
429
        """
430
        try:
431
            mark_orders_as_delivered(providerId, deliveredOrders)
432
        finally:
433
            close_session()
1135 chandransh 434
 
435
    def markOrdersAsFailed(self, providerId, returnedOrders):
436
        """
437
        Mark all orders with AWBs in the given map as failed. Also sets the delivery timestamp.
438
        Raises an exception if we encounter report for an AWB number that we did not ship.
439
 
440
        Parameters:
441
         - providerId
442
         - returnedOrders
443
        """
444
        try:
445
            mark_orders_as_failed(providerId, returnedOrders)
446
        finally:
447
            close_session()
1246 chandransh 448
 
449
    def updateNonDeliveryReason(self, providerId, undeliveredOrders):
450
        """
451
        Update the status description of orders whose AWB numbers are keys of the Map.
452
 
453
        Parameters:
454
         - providerId
455
         - undelivered_orders
456
        """
457
        try:
458
            update_non_delivery_reason(providerId, undeliveredOrders)
459
        finally:
460
            close_session()
1405 ankur.sing 461
 
462
    def getUndeliveredOrders(self, providerId, warehouseId):
463
        """
464
        Returns the list of orders whose delivery time has passed but have not been
465
        delivered yet for the given provider and warehouse. To get a complete list of
466
        undelivered orders, pass them as -1.
467
        Returns an empty list if no such orders exist.
468
 
469
        Parameters:
470
         - providerId
471
         - warehouseId
472
        """
473
        try:
474
            undelivered_orders = get_undelivered_orders(providerId, warehouseId)
475
            return [to_t_order(order) for order in undelivered_orders]
476
        finally:
477
            close_session()
1351 varun.gupt 478
 
1398 varun.gupt 479
    def enqueueTransactionInfoEmail(self, transactionId):
1351 varun.gupt 480
        """
1398 varun.gupt 481
        Save the email containing order details to be sent to customer later by batch job
1351 varun.gupt 482
 
483
        Parameters:
484
         - transactionId
485
        """
486
        try:
1398 varun.gupt 487
            return enqueue_transaction_info_email(transactionId)
1351 varun.gupt 488
        finally:
489
            close_session()
490
 
483 rajveer 491
    def getAlerts(self, orderId, valid):
492
        """
493
        Parameters:
494
         - orderId
495
         - valid
496
        """
766 rajveer 497
        try:
498
            alerts = get_alerts(orderId, valid)
499
 
500
            t_alerts = []
501
 
502
            for alert in alerts:
503
                t_alerts.append(to_t_alert(alert)) 
504
 
505
            return t_alerts
506
        finally:
507
            close_session()
508
 
483 rajveer 509
    def setAlert(self, orderId, unset, type, comment):
510
        """
511
        Parameters:
512
         - orderId
513
         - unset
514
         - type
515
         - comment
516
        """
766 rajveer 517
        try:
518
            set_alert(orderId, unset, type, comment)
519
        finally:
520
            close_session()
1596 ankur.sing 521
 
522
    def getValidOrderCount(self, ):
523
        """
524
        Return the number of valid orders. (OrderStatus >= OrderStatus.SUBMITTED_FOR_PROCESSING)
525
        """
1731 ankur.sing 526
        try:
527
            return get_valid_order_count()
528
        finally:
529
            close_session()
1627 ankur.sing 530
 
531
    def getNoOfCustomersWithSuccessfulTransaction(self, ):
532
        """
533
        Returns the number of distinct customers who have done successful transactions
534
        """
1731 ankur.sing 535
        try:
536
            return get_cust_count_with_successful_txn()
537
        finally:
538
            close_session()
1627 ankur.sing 539
 
1731 ankur.sing 540
 
541
    def getValidOrdersAmountRange(self, ):
1627 ankur.sing 542
        """
1731 ankur.sing 543
        Returns the minimum and maximum amounts of a valid order. (OrderStatus >= OrderStatus.SUBMITTED_FOR_PROCESSING)
544
        List contains two values, first minimum amount and second maximum amount.
1627 ankur.sing 545
        """
1731 ankur.sing 546
        try:
547
            return get_valid_orders_amount_range()
548
        finally:
549
            close_session()
1627 ankur.sing 550
 
1886 ankur.sing 551
    def getValidOrders(self, limit):
552
        """
553
        Returns list of Orders in descending order by Order creation date. List is restricted to limit Orders.
554
        If limit is passed as 0, then all valid Orders are returned.
555
 
556
        Parameters:
557
         - limit
558
        """
559
        try:
560
            return [to_t_order(order) for order in get_valid_orders(limit)]
561
        finally:
562
            close_session()
104 ashish 563
 
2536 chandransh 564
    def toggleDOAFlag(self, orderId):
104 ashish 565
        """
2536 chandransh 566
        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.
567
        Returns the final flag status.
568
        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 569
 
132 ashish 570
        Parameters:
2536 chandransh 571
         - orderId
132 ashish 572
        """
2536 chandransh 573
        try:
574
            return toggle_doa_flag(orderId)
575
        finally:
576
            close_session()
483 rajveer 577
 
2536 chandransh 578
    def requestPickupNumber(self, orderId):
104 ashish 579
        """
2536 chandransh 580
        Sends out an email to the account manager of the original courier provider used to ship the order.
581
        If the order status was DELIVERY_SUCCESS, it is changed to be DOA_PICKUP_REQUESTED.
582
        If the order status was DOA_PICKUP_REQUESTED, it is left unchanged.
583
        For any other status, it returns false.
584
        Throws an exception if the order with the given id couldn't be found.
104 ashish 585
 
586
        Parameters:
2536 chandransh 587
         - orderId
104 ashish 588
        """
2536 chandransh 589
        try:
590
            return request_pickup_number(orderId)
591
        finally:
592
            close_session()
483 rajveer 593
 
2536 chandransh 594
    def authorizePickup(self, orderId, pickupNumber):
304 ashish 595
        """
2536 chandransh 596
        If the order status is DOA_PICKUP_REQUESTED, it does the following
597
            1. Sends out an email to the customer with the dispatch advice that he has to print as an attachment.
598
            2. Changes order status to be DOA_PICKUP_AUTHORIZED.
599
            3. Returns true
600
        If the order is any other status, it returns false.
601
        Throws an exception if the order with the given id couldn't be found.
304 ashish 602
 
603
        Parameters:
2536 chandransh 604
         - orderId
605
         - pickupNumber
304 ashish 606
        """
2536 chandransh 607
        try:
608
            return authorize_pickup(orderId, pickupNumber)
609
        finally:
610
            close_session()    
2764 chandransh 611
 
612
    def markDoasAsPickedUp(self, providerId, pickupDetails):
613
        """
614
        Marks all DOA_PICKUP_AUTHORIZED orders of the previous day for a provider as DOA_RETURN_IN_TRANSIT.
615
        Returns a list of orders that were authorized for pickup but did not appear in the pick-up report.
616
 
617
        Parameters:
618
         - providerId
619
         - pickupDetails
620
        """
621
        try:
622
            orders_not_picked_up = mark_doas_as_picked_up(providerId, pickupDetails)
623
            return [to_t_order(order) for order in orders_not_picked_up]
624
        finally:
625
            close_session()
2591 chandransh 626
 
2616 chandransh 627
    def receiveReturn(self, orderId):
2591 chandransh 628
        """
2599 chandransh 629
        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 630
        If the order is in any other state, it returns false.
631
        Throws an exception if the order with the given id couldn't be found.
632
 
633
        Parameters:
634
         - orderId
635
        """
636
        try:
2616 chandransh 637
            return receive_return(orderId)
2591 chandransh 638
        finally:
639
            close_session()
640
 
641
    def validateDoa(self, orderId, isValid):
642
        """
2599 chandransh 643
        Used to validate the DOA certificate for an order in the DOA_RECEIVED state. If the certificate is valid,
2609 chandransh 644
        the order state is changed to DOA_CERT_PENDING.
2591 chandransh 645
        If the certificate is invalid, the order state is changed to DOA_CERT_INVALID.
646
        If the order is in any other state, it returns false.
647
        Throws an exception if the order with the given id couldn't be found.
648
 
649
        Parameters:
650
         - orderId
651
         - isValid
652
        """
653
        try:
654
            return validate_doa(orderId, isValid)
655
        finally:
656
            close_session()
2628 chandransh 657
 
658
    def reshipOrder(self, orderId):
659
        """
660
        If the order is in SALES_RET_RECEIVED or DOA_CERT_INVALID state, it does the following:
661
            1. Creates a new order for processing in the BILLED state. All billing information is saved.
662
            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.
663
 
664
        If the order is in DOA_CERT_VALID state, it does the following:
665
            1. Creates a new order for processing in the SUBMITTED_FOR_PROCESSING state.
666
            2. Creates a return order for the warehouse executive to return the DOA material.
667
            3. Marks the current order as the final DOA_RESHIPPED state.
668
 
669
        Returns the id of the newly created order.
670
 
671
        Throws an exception if the order with the given id couldn't be found.
672
 
673
        Parameters:
674
         - orderId
675
        """
676
        try:
677
            return reship_order(orderId)
678
        finally:
679
            close_session()
680
 
3226 chandransh 681
    def refundOrder(self, orderId, refundedBy, reason):
2628 chandransh 682
        """
683
        If the order is in SALES_RET_RECEIVED, DOA_CERT_VALID or DOA_CERT_INVALID state, it does the following:
684
            1. Creates a refund request for batch processing.
685
            2. Creates a return order for the warehouse executive to return the shipped material.
686
            3. Marks the current order as SALES_RET_REFUNDED, DOA_VALID_REFUNDED or DOA_INVALID_REFUNDED final states.
687
 
688
        If the order is in SUBMITTED_FOR_PROCESSING or INVENTORY_LOW state, it does the following:
689
            1. Creates a refund request for batch processing.
3226 chandransh 690
            2. Cancels the reservation of the item in the warehouse.
691
            3. Marks the current order as the REFUNDED final state.
2628 chandransh 692
 
3226 chandransh 693
        For all COD orders, if the order is in INIT, SUBMITTED_FOR_PROCESSING or INVENTORY_LOW state, it does the following:
694
            1. Cancels the reservation of the item in the warehouse.
695
            2. Marks the current order as CANCELED.
696
 
697
        In all cases, it updates the reason for cancellation or refund and the person who performed the action.
698
 
2628 chandransh 699
        Returns True if it is successful, False otherwise.
700
 
701
        Throws an exception if the order with the given id couldn't be found.
702
 
703
        Parameters:
704
         - orderId
3226 chandransh 705
         - refundedBy
706
         - reason
2628 chandransh 707
        """
708
        try:
3226 chandransh 709
            return refund_order(orderId, refundedBy, reason)
2628 chandransh 710
        finally:
711
            close_session()
712
 
2697 chandransh 713
    def getReturnOrders(self, warehouseId, fromDate, toDate):
714
        """
715
        Get all return orders created between the from and to dates for the given warehouse.
716
        Ignores the warehouse if it is passed as -1.
717
 
718
        Parameters:
719
         - warehouseId
720
         - fromDate
721
         - toDate
722
        """
723
        try:
724
            from_date, to_date = get_fdate_tdate(fromDate, toDate)
725
            return get_return_orders(warehouseId, from_date, to_date)
726
        finally:
727
            close_session()
728
 
2700 chandransh 729
    def getReturnOrder(self, id):
730
        """
731
        Returns the ReturnOrder corresponding to the given id.
732
        Throws an exception if the return order with the given id couldn't be found.
733
 
734
        Parameters:
735
         - id
736
        """
737
        try:
738
            return get_return_order(id)
739
        finally:
740
            close_session()
741
 
2697 chandransh 742
    def processReturn(self, returnOrderId):
743
        """
744
        Marks the return order with the given id as processed. Raises an exception if no such return order exists.
745
 
746
        Parameters:
747
         - returnOrderId
748
        """
749
        try:
750
            process_return(returnOrderId)
751
        finally:
752
            close_session()
2591 chandransh 753
 
2819 chandransh 754
    def createPurchaseOrder(self, warehouseId):
755
        """
756
        Creates a purchase order corresponding to all the pending orders of the given warehouse.
757
        Returns the PO no. of the newly created purchase order.
758
 
759
        Parameters:
760
         - warehouseId
761
        """
762
        try:
763
            return create_purchase_order(warehouseId)
764
        finally:
765
            close_session()
3451 chandransh 766
 
767
    def updateWeight(self, orderId, weight):
768
        """
769
        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.
770
 
771
        Parameters:
772
         - orderId
773
         - weight
774
        """
775
        try:
776
            order = update_weight(orderId, weight)
777
            return to_t_order(order)
778
        finally:
779
            close_session()
780
 
3469 chandransh 781
    def changeItem(self, orderId, itemId):
782
        """
783
        Change the item to be shipped for this order. Also adjusts the reservation in the inventory accordingly.
784
        Currently, it also ensures that only a different color of the given item is shipped.
785
 
786
        Parameters:
787
         - orderId
788
         - itemId
789
        """
790
        try:
791
            order = change_product(orderId, itemId)
792
            return to_t_order(order)
793
        finally:
794
            close_session()
795
 
796
    def shiftToWarehouse(self, orderId, warehouseId):
797
        """
798
        Moves the given order to the given warehouse. Also adjusts the inventory reservations accordingly.
799
 
800
        Parameters:
801
         - orderId
802
         - warehouseId
803
        """
804
        try:
805
            order = change_warehouse(orderId, warehouseId)
806
            return to_t_order(order)
807
        finally:
808
            close_session()
3553 chandransh 809
 
3986 chandransh 810
    def addDelayReason(self, orderId, delayReason, furtherDelay):
3553 chandransh 811
        """
812
        Adds the given delay reason to the given order.
3986 chandransh 813
        Increases the expected delivery time of the given order by the given no. of days.
3553 chandransh 814
        Raises an exception if no order with the given id can be found.
3469 chandransh 815
 
3553 chandransh 816
        Parameters:
817
         - orderId
818
         - delayReason
819
        """
820
        try:
3986 chandransh 821
            return add_delay_reason(orderId, delayReason, furtherDelay)
3553 chandransh 822
        finally:
823
            close_session()
824
 
3956 chandransh 825
    def reconcileCodCollection(self, collectedAmountMap, xferBy, xferTxnId, xferDate):
826
        """
827
        Marks the COD orders with given AWB nos. as having been processed.
828
        Updates the captured amount for the corresponding payment.
829
 
830
        Returns a map of AWBs which were not processed and the associated reason. An AWB is not processed if:
831
        1. There is no order corresponding to an AWB number.
832
        2. The captured amount for a payment exceeds the total payment.
833
        3. The order corresponding to an AWB no. is in a state prior to DELIVERY_SUCCESS.
834
 
835
        Parameters:
836
         - collectedAmountMap
837
         - xferBy
838
         - xferTxnId
839
         - xferDate
840
        """
841
        try:
842
            return reconcile_cod_collection(collectedAmountMap, xferBy, xferTxnId, xferDate)
843
        finally:
844
            close_session()
4008 mandeep.dh 845
 
846
    def getTransactionsRequiringExtraProcessing(self, category):
847
        """
848
        Returns the list of transactions that require some extra processing and
849
        which belong to a particular category. This is currently used by CRM
850
        application.
851
        """
852
        try:
853
            return get_transactions_requiring_extra_processing(category)
854
        finally:
855
            close_session()
856
 
857
    def markTransactionAsProcessed(self, transactionId, category):
858
        """
859
        Marks a particular transaction as processed for a particular category.
860
        It essentially deletes the transaction if it is processed for a particular
861
        category. This is currently used by CRM application.
862
        """
863
        try:
864
            return mark_transaction_as_processed(transactionId, category)
865
        finally:
866
            close_session()
4018 chandransh 867
 
868
    def getItemWiseRiskyOrdersCount(self, ):
869
        """
870
        Returns a map containing the number of risky orders keyed by item id. A risky order
871
        is defined as one whose shipping date is about to expire.
872
        """
873
        try:
874
            return get_item_wise_risky_orders_count()
875
        finally:
876
            close_session()
3956 chandransh 877
 
4247 rajveer 878
    def markOrderCancellationRequestReceived(self, orderId):
879
        """
880
        Mark order as cancellation request received. If customer sends request of cancellation of
881
        a particular order, this method will be called. It will just change status of the order
882
        depending on its current status. It also records the previous status, so that we can move
883
        back to that status if cancellation request is denied.
884
 
885
        Parameters:
886
         - orderId
887
        """
888
        try:
889
            return mark_order_cancellation_request_received(orderId)
890
        finally:
891
            close_session()
892
 
893
 
4258 rajveer 894
    def markTransactionAsPaymentFlagRemoved(self, transactionId):
4247 rajveer 895
        """
4258 rajveer 896
        If we and/or payment gateway has decided to accept the payment, this method needs to be called.
897
        Changed transaction and all orders status to payment accepted.
4247 rajveer 898
 
899
        Parameters:
4258 rajveer 900
         - transactionId
4247 rajveer 901
        """
902
        try:
4259 anupam.sin 903
            return mark_transaction_as_payment_flag_removed(transactionId)
4247 rajveer 904
        finally:
905
            close_session()
4259 anupam.sin 906
 
907
    def refundTransaction(self, transactionId, refundedBy, reason):
908
        """
909
        This method is called when a flagged payment is deemed unserviceable and the corresponding orders
910
        need to be cancelled
911
 
912
        Parameters:
913
         - transactionId
914
        """
915
        try:
916
            return refund_transaction(transactionId, refundedBy, reason)
917
        finally:
918
            close_session()
4247 rajveer 919
 
920
    def markOrderCancellationRequestDenied(self, orderId):
921
        """
922
        If we decide to not to cancel order, we will move the order ro previous status.
923
 
924
        Parameters:
925
         - orderId
926
        """
927
        try:
928
            return mark_order_cancellation_request_denied(orderId)
929
        finally:
930
            close_session()
931
 
932
    def markOrderCancellationRequestConfirmed(self, orderId):
933
        """
934
        If we decide to to cancel order, CRM will call this method to move the status of order to
935
        cancellation request confirmed. After this OM will be able to cancel the order.
936
 
937
        Parameters:
938
         - orderId
939
        """
940
        try:
941
            return mark_order_cancellation_request_confirmed(orderId)
942
        finally:
943
            close_session()
944
 
4285 rajveer 945
    def acceptOrdersForItemId(self, itemId, inventory):
946
        """
947
        Marks the orders as ACCEPTED for the given itemId and inventory. It also updates the accepted timestamp. If the
948
        given order is not a COD order, it also captures the payment if the same has not been captured.
949
 
950
        Parameters:
951
         - itemId
952
         - inventory
953
        """
954
        try:
955
            return accept_orders_for_item_id(itemId, inventory)
956
        finally:
957
            close_session()
958
 
2536 chandransh 959
    def closeSession(self, ):
960
        close_session()
3376 rajveer 961
 
962
    def isAlive(self, ):
963
        """
964
        For checking weather service is active alive or not. It also checks connectivity with database
965
        """
966
        try:
967
            return is_alive()
968
        finally:
4247 rajveer 969
            close_session()