Subversion Repositories SmartDukaan

Rev

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