Subversion Repositories SmartDukaan

Rev

Rev 3376 | Rev 3451 | 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,\
3427 chandransh 21
    create_purchase_order, verify_order, is_alive, get_orders_by_shipping_date
1405 ankur.sing 22
 
104 ashish 23
from shop2020.model.v1.order.impl.Convertors import to_t_transaction,\
1348 chandransh 24
    to_t_alert, to_t_order, to_t_lineitem
483 rajveer 25
from shop2020.thriftpy.model.v1.order.ttypes import Transaction, OrderStatus,\
2783 chandransh 26
    LineItem, Order, TransactionServiceException
738 chandransh 27
from shop2020.utils.Utils import to_py_date, get_fdate_tdate
104 ashish 28
 
29
class OrderServiceHandler:
30
 
3187 rajveer 31
    def __init__(self, dbname='transaction', db_hostname='localhost'):
483 rajveer 32
        """
104 ashish 33
        Constructor
483 rajveer 34
        """
3187 rajveer 35
        DataService.initialize(dbname, db_hostname)
104 ashish 36
 
37
    def createTransaction(self, transaction):
38
        """
39
        Parameters:
40
         - transaction
41
        """
766 rajveer 42
        try:
43
            return create_transaction(transaction)
44
        finally:
45
            close_session()
46
 
104 ashish 47
    def getTransaction(self, id):
48
        """
49
            Get transaction methods.
50
 
51
        Parameters:
52
         - id
53
        """
766 rajveer 54
        try:
55
            transaction = get_transaction(id)
56
            return to_t_transaction(transaction)
57
        finally:
58
            close_session()
59
 
483 rajveer 60
    def getTransactionsForCustomer(self, customerId, from_date, to_date, status):
104 ashish 61
        """
62
        Parameters:
483 rajveer 63
         - customerId
104 ashish 64
         - from_date
65
         - to_date
483 rajveer 66
         - status
104 ashish 67
        """
766 rajveer 68
        try:
69
            current_from_date, current_to_date = get_fdate_tdate(from_date, to_date)
70
 
71
            transactions = get_transactions_for_customer(customerId, current_from_date, current_to_date, status)
72
            t_transaction = []
73
            for transaction in transactions:
74
                t_transaction.append(to_t_transaction(transaction))
75
            return t_transaction
76
        finally:
77
            close_session()
483 rajveer 78
 
79
    def getTransactionsForShoppingCartId(self, shoppingCartId):
80
        """
81
        Parameters:
82
            - shoppingCartId
83
        """
766 rajveer 84
        try:
85
            transactions = get_transactions_for_shopping_cart_id(shoppingCartId)
86
            t_transaction = []
87
            for transaction in transactions:
88
                t_transaction.append(to_t_transaction(transaction))
89
            return t_transaction
90
        finally:
91
            close_session()
104 ashish 92
 
483 rajveer 93
    def getTransactionStatus(self, transactionId):
104 ashish 94
        """
95
        Parameters:
483 rajveer 96
         - transactionId
97
        """
766 rajveer 98
        try:
99
            return get_transaction_status(transactionId)
100
        finally:
101
            close_session()
102
 
483 rajveer 103
    def changeTransactionStatus(self, transactionId, status, description):
104
        """
105
        Parameters:
106
         - transactionId
104 ashish 107
         - status
483 rajveer 108
         - description
109
        """
766 rajveer 110
        try:
111
            return change_transaction_status(transactionId, status, description)
112
        finally:
113
            close_session()
114
 
1528 ankur.sing 115
    def getOrdersForTransaction(self, transactionId, customerId):
483 rajveer 116
        """
1528 ankur.sing 117
        Returns list of orders for given transaction Id. Also filters based on customer Id so that
118
        only user who owns the transaction can view its order details.
119
 
483 rajveer 120
        Parameters:
121
         - transactionId
1528 ankur.sing 122
         - customerId
483 rajveer 123
        """
766 rajveer 124
        try:
1528 ankur.sing 125
            orders = get_orders_for_transaction(transactionId, customerId)
766 rajveer 126
            return [to_t_order(order) for order in orders]    
127
        finally:
128
            close_session()
129
 
483 rajveer 130
    def getAllOrders(self, status, from_date, to_date, warehouse_id):
131
        """
132
        Parameters:
133
         - status
104 ashish 134
         - from_date
135
         - to_date
483 rajveer 136
         - warehouse_id
104 ashish 137
        """
766 rajveer 138
        try:
139
            current_from_date, current_to_date = get_fdate_tdate(from_date, to_date)        
140
            orders = get_all_orders(status, current_from_date, current_to_date, warehouse_id)
141
            return [to_t_order(order) for order in orders]
142
        finally:
143
            close_session()
144
 
995 varun.gupt 145
    def getOrdersByBillingDate(self, status, start_billing_date, end_billing_date, warehouse_id):
146
        """
147
        Parameters:
148
         - status
149
         - start_billing_date
150
         - end_billing_date
151
         - warehouse_id
152
        """
153
        try:
154
            current_start_billing_date, current_end_billing_date = get_fdate_tdate(start_billing_date, end_billing_date)
155
            orders = get_orders_by_billing_date(status, current_start_billing_date, current_end_billing_date, warehouse_id)
156
            return [to_t_order(order) for order in orders]
157
        finally:
158
            close_session()
1382 varun.gupt 159
 
3427 chandransh 160
    def getOrdersByShippingDate(self, fromShippingDate, toShippingDate, providerId, warehouseId):
161
        """
162
        Returns orders for a particular provider and warehouse which were shipped between the given dates.
163
        Pass providerId and warehouseId as -1 to ignore both the parameters.
164
 
165
        Parameters:
166
         - fromShippingDate
167
         - toShippingDate
168
         - providerId
169
         - warehouseId
170
        """
171
        try:
172
            from_shipping_date, to_shipping_date = get_fdate_tdate(fromShippingDate, toShippingDate)
173
            orders = get_orders_by_shipping_date(from_shipping_date, to_shipping_date, providerId, warehouseId)
174
            return [to_t_order(order) for order in orders]
175
        finally:
176
            close_session()
177
 
1382 varun.gupt 178
    def getReturnableOrdersForCustomer(self, customerId, limit = None):
179
        """
180
        Parameters:
181
         - customerId
182
         - limit
183
        """
184
        try:
185
            return get_returnable_orders_for_customer(customerId, limit)
186
        finally:
187
            close_session()
995 varun.gupt 188
 
1382 varun.gupt 189
    def getCancellableOrdersForCustomer(self, customerId, limit = None):
190
        """
191
        Parameters:
192
         - customerId
193
         - limit
194
        """
195
        try:
196
            return get_cancellable_orders_for_customer(customerId, limit)
197
        finally:
198
            close_session()
199
 
483 rajveer 200
    def changeOrderStatus(self, orderId, status, description):
201
        """
202
        Parameters:
203
         - orderId
204
         - status
205
         - description
206
         - 
207
        """
766 rajveer 208
        try:
209
            return change_order_status(self, orderId, status, description)
210
        finally:
211
            close_session()
921 rajveer 212
 
3064 chandransh 213
    def verifyOrder(self, orderId):
214
        """
215
        Marks the given order as SUBMITTED_FOR_PROCESSING and updates the verified
216
        timestamp. It is intended to be used for COD orders but can be harmlessly
217
        used for all other orders as well.
218
        Throws an exception if no such order exists.
219
 
220
        Parameters:
221
         - orderId
222
        """
223
        try:
224
            return verify_order(self, orderId)
225
        finally:
226
            close_session()
227
 
921 rajveer 228
    def acceptOrder(self, orderId):
229
        """
3064 chandransh 230
        Marks the given order as ACCEPTED and updates the accepted timestamp. If the
231
        given order is not a COD order, it also captures the payment if the same has
232
        not been captured.
233
        Throws an exception if no such order exists.
234
 
921 rajveer 235
        Parameters:
236
         - orderId
237
        """
238
        try:
239
            return accept_order(self, orderId)
240
        finally:
241
            close_session()
242
 
243
    def billOrder(self, orderId):
244
        """
245
        Parameters:
246
         - orderId
247
        """
248
        try:
249
            return bill_order(self, orderId)
250
        finally:
251
            close_session()
766 rajveer 252
 
3014 chandransh 253
    def getOrdersForCustomer(self, customerId, from_date, to_date, statuses):
104 ashish 254
        """
3014 chandransh 255
        Returns list of orders for the given customer created between the given dates and having the given statuses.
256
        Pass and empty list to ignore filtering on statuses.
257
 
104 ashish 258
        Parameters:
259
         - customerId
260
         - from_date
261
         - to_date
3014 chandransh 262
         - statuses
104 ashish 263
        """
766 rajveer 264
        try:
265
            current_from_date, current_to_date = get_fdate_tdate(from_date, to_date)
266
 
3014 chandransh 267
            orders = get_orders_for_customer(customerId, current_from_date, current_to_date, statuses)
766 rajveer 268
            return [to_t_order(order) for order in orders]
269
        finally:
270
            close_session()
1528 ankur.sing 271
 
272
    def getOrderForCustomer(self, orderId, customerId):
273
        """
274
        Returns an order for the order Id. Also checks if the order belongs to the customer whose Id is passed.
275
        Throws exception if either order Id is invalid or order does not below to the customer whose Id is passed.
276
 
277
        Parameters:
278
         - customerId
279
         - orderId
280
        """
281
        try:
282
            return to_t_order(get_order_for_customer(orderId, customerId))
283
        finally:
284
            close_session()
766 rajveer 285
 
483 rajveer 286
    def getOrder(self, id):
287
        """
288
        Parameters:
289
         - id
290
        """
766 rajveer 291
        try:
292
            return to_t_order(get_order(id))
293
        finally:
294
            close_session()
295
 
483 rajveer 296
    def getLineItemsForOrder(self, orderId):
297
        """
298
        Parameters:
299
         - orderId
300
        """
766 rajveer 301
        try:
302
            lineitems = get_line_items_for_order(orderId)
303
            t_lineitems = []
304
            for lineitem in lineitems:
305
                t_lineitems.append(to_t_lineitem(lineitem))
306
            return t_lineitems
307
        finally:
308
            close_session()
1149 chandransh 309
 
310
    def addBillingDetails(self, orderId, invoice_number, billed_by):
494 rajveer 311
        """
1149 chandransh 312
        Add billing details such as the bill number and the biller to the Order.
313
 
494 rajveer 314
        Parameters:
315
         - orderId
316
         - invoice_number
317
         - billed_by
318
        """
766 rajveer 319
        try:
1149 chandransh 320
            return add_billing_details(orderId, invoice_number, billed_by)
766 rajveer 321
        finally:
322
            close_session()
1149 chandransh 323
 
2842 chandransh 324
    def addJacketNumber(self, orderId, jacketNumber, imeiNumber, itemNumber, billedBy, billingType):
1149 chandransh 325
        """
2783 chandransh 326
        Adds jacket number and IMEI no. to the order. Doesn't update the IMEI no. if a -1 is supplied.
327
        Also marks the order as billed and sets the billing timestamp.
328
        Return false if it doesn't find the order with the given ID.
1149 chandransh 329
 
330
        Parameters:
331
         - orderId
332
         - jacketNumber
2783 chandransh 333
         - imeiNumber
334
         - itemNumber
335
         - billedBy
1149 chandransh 336
        """
337
        try:
2842 chandransh 338
            return add_jacket_number(self, orderId, jacketNumber, imeiNumber, itemNumber, billedBy, billingType)
2783 chandransh 339
        except TransactionServiceException:
340
            print "ERROR: Couldn't find the order with the given id"
341
            return False
1149 chandransh 342
        finally:
343
            close_session()
1220 chandransh 344
 
345
    def batchOrders(self, warehouseId):
346
        """
347
        Create a batch of all the pending orders for the given warehouse.
348
        The returned list is orderd by created_timestamp.
349
        If there are no pending orders, an empty list is returned.
1208 chandransh 350
 
1220 chandransh 351
        Parameters:
352
         - warehouseId
353
        """
354
        try:
355
            pending_orders = batch_orders(warehouseId)
356
            return [to_t_order(order) for order in pending_orders]
357
        finally:
358
            close_session()
359
 
1208 chandransh 360
    def markOrderAsOutOfStock(self, orderId):
361
        """
362
        Mark the given order as out of stock. Throws an exception if the order with the given Id couldn't be found.
363
 
364
 
365
        Parameters:
366
         - orderId
367
        """
368
        try:
369
            return order_outofstock(orderId)
370
        finally:
371
            close_session()
372
 
3064 chandransh 373
    def markOrdersAsManifested(self, warehouseId, providerId, cod):
759 chandransh 374
        """
3064 chandransh 375
        Depending on the third parameter, marks either all prepaid or all cod orders BILLED by the
376
        given warehouse and were picked up by the given provider as SHIPPED_FROM_WH.
759 chandransh 377
 
378
        Parameters:
379
         - warehouseId
380
         - providerId
3064 chandransh 381
         - cod
759 chandransh 382
        """
766 rajveer 383
        try:
3064 chandransh 384
            return mark_orders_as_manifested(warehouseId, providerId, cod)
766 rajveer 385
        finally:
386
            close_session()
1113 chandransh 387
 
388
    def markOrdersAsPickedUp(self, providerId, pickupDetails):
389
        """
390
        Marks all SHIPPED_FROM_WH orders of the previous day for a provider as SHIPPED_TO_LOGISTICS.
391
        Returns a list of orders that were shipped from warehouse but did not appear in the pick-up report.
392
        Raises an exception if we encounter report for an AWB number that we did not ship.
393
 
394
        Parameters:
395
         - providerId
396
         - pickupDetails
397
        """
398
        try:
399
            orders_not_picked_up = mark_orders_as_picked_up(providerId, pickupDetails)
400
            return [to_t_order(order) for order in orders_not_picked_up]
401
        finally:
402
            close_session()
1132 chandransh 403
 
404
    def markOrdersAsDelivered(self, providerId, deliveredOrders):
405
        """
406
        Marks all orders with AWBs in the given map as delivered. Also sets the delivery timestamp and
407
        the name of the receiver.
408
        Raises an exception if we encounter report for an AWB number that we did not ship.
409
 
410
        Parameters:
411
         - providerId
412
         - deliveredOrders
413
        """
414
        try:
415
            mark_orders_as_delivered(providerId, deliveredOrders)
416
        finally:
417
            close_session()
1135 chandransh 418
 
419
    def markOrdersAsFailed(self, providerId, returnedOrders):
420
        """
421
        Mark all orders with AWBs in the given map as failed. Also sets the delivery timestamp.
422
        Raises an exception if we encounter report for an AWB number that we did not ship.
423
 
424
        Parameters:
425
         - providerId
426
         - returnedOrders
427
        """
428
        try:
429
            mark_orders_as_failed(providerId, returnedOrders)
430
        finally:
431
            close_session()
1246 chandransh 432
 
433
    def updateNonDeliveryReason(self, providerId, undeliveredOrders):
434
        """
435
        Update the status description of orders whose AWB numbers are keys of the Map.
436
 
437
        Parameters:
438
         - providerId
439
         - undelivered_orders
440
        """
441
        try:
442
            update_non_delivery_reason(providerId, undeliveredOrders)
443
        finally:
444
            close_session()
1405 ankur.sing 445
 
446
    def getUndeliveredOrders(self, providerId, warehouseId):
447
        """
448
        Returns the list of orders whose delivery time has passed but have not been
449
        delivered yet for the given provider and warehouse. To get a complete list of
450
        undelivered orders, pass them as -1.
451
        Returns an empty list if no such orders exist.
452
 
453
        Parameters:
454
         - providerId
455
         - warehouseId
456
        """
457
        try:
458
            undelivered_orders = get_undelivered_orders(providerId, warehouseId)
459
            return [to_t_order(order) for order in undelivered_orders]
460
        finally:
461
            close_session()
1351 varun.gupt 462
 
1398 varun.gupt 463
    def enqueueTransactionInfoEmail(self, transactionId):
1351 varun.gupt 464
        """
1398 varun.gupt 465
        Save the email containing order details to be sent to customer later by batch job
1351 varun.gupt 466
 
467
        Parameters:
468
         - transactionId
469
        """
470
        try:
1398 varun.gupt 471
            return enqueue_transaction_info_email(transactionId)
1351 varun.gupt 472
        finally:
473
            close_session()
474
 
483 rajveer 475
    def getAlerts(self, orderId, valid):
476
        """
477
        Parameters:
478
         - orderId
479
         - valid
480
        """
766 rajveer 481
        try:
482
            alerts = get_alerts(orderId, valid)
483
 
484
            t_alerts = []
485
 
486
            for alert in alerts:
487
                t_alerts.append(to_t_alert(alert)) 
488
 
489
            return t_alerts
490
        finally:
491
            close_session()
492
 
483 rajveer 493
    def setAlert(self, orderId, unset, type, comment):
494
        """
495
        Parameters:
496
         - orderId
497
         - unset
498
         - type
499
         - comment
500
        """
766 rajveer 501
        try:
502
            set_alert(orderId, unset, type, comment)
503
        finally:
504
            close_session()
1596 ankur.sing 505
 
506
    def getValidOrderCount(self, ):
507
        """
508
        Return the number of valid orders. (OrderStatus >= OrderStatus.SUBMITTED_FOR_PROCESSING)
509
        """
1731 ankur.sing 510
        try:
511
            return get_valid_order_count()
512
        finally:
513
            close_session()
1627 ankur.sing 514
 
515
    def getNoOfCustomersWithSuccessfulTransaction(self, ):
516
        """
517
        Returns the number of distinct customers who have done successful transactions
518
        """
1731 ankur.sing 519
        try:
520
            return get_cust_count_with_successful_txn()
521
        finally:
522
            close_session()
1627 ankur.sing 523
 
1731 ankur.sing 524
 
525
    def getValidOrdersAmountRange(self, ):
1627 ankur.sing 526
        """
1731 ankur.sing 527
        Returns the minimum and maximum amounts of a valid order. (OrderStatus >= OrderStatus.SUBMITTED_FOR_PROCESSING)
528
        List contains two values, first minimum amount and second maximum amount.
1627 ankur.sing 529
        """
1731 ankur.sing 530
        try:
531
            return get_valid_orders_amount_range()
532
        finally:
533
            close_session()
1627 ankur.sing 534
 
1886 ankur.sing 535
    def getValidOrders(self, limit):
536
        """
537
        Returns list of Orders in descending order by Order creation date. List is restricted to limit Orders.
538
        If limit is passed as 0, then all valid Orders are returned.
539
 
540
        Parameters:
541
         - limit
542
        """
543
        try:
544
            return [to_t_order(order) for order in get_valid_orders(limit)]
545
        finally:
546
            close_session()
104 ashish 547
 
2536 chandransh 548
    def toggleDOAFlag(self, orderId):
104 ashish 549
        """
2536 chandransh 550
        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.
551
        Returns the final flag status.
552
        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 553
 
132 ashish 554
        Parameters:
2536 chandransh 555
         - orderId
132 ashish 556
        """
2536 chandransh 557
        try:
558
            return toggle_doa_flag(orderId)
559
        finally:
560
            close_session()
483 rajveer 561
 
2536 chandransh 562
    def requestPickupNumber(self, orderId):
104 ashish 563
        """
2536 chandransh 564
        Sends out an email to the account manager of the original courier provider used to ship the order.
565
        If the order status was DELIVERY_SUCCESS, it is changed to be DOA_PICKUP_REQUESTED.
566
        If the order status was DOA_PICKUP_REQUESTED, it is left unchanged.
567
        For any other status, it returns false.
568
        Throws an exception if the order with the given id couldn't be found.
104 ashish 569
 
570
        Parameters:
2536 chandransh 571
         - orderId
104 ashish 572
        """
2536 chandransh 573
        try:
574
            return request_pickup_number(orderId)
575
        finally:
576
            close_session()
483 rajveer 577
 
2536 chandransh 578
    def authorizePickup(self, orderId, pickupNumber):
304 ashish 579
        """
2536 chandransh 580
        If the order status is DOA_PICKUP_REQUESTED, it does the following
581
            1. Sends out an email to the customer with the dispatch advice that he has to print as an attachment.
582
            2. Changes order status to be DOA_PICKUP_AUTHORIZED.
583
            3. Returns true
584
        If the order is any other status, it returns false.
585
        Throws an exception if the order with the given id couldn't be found.
304 ashish 586
 
587
        Parameters:
2536 chandransh 588
         - orderId
589
         - pickupNumber
304 ashish 590
        """
2536 chandransh 591
        try:
592
            return authorize_pickup(orderId, pickupNumber)
593
        finally:
594
            close_session()    
2764 chandransh 595
 
596
    def markDoasAsPickedUp(self, providerId, pickupDetails):
597
        """
598
        Marks all DOA_PICKUP_AUTHORIZED orders of the previous day for a provider as DOA_RETURN_IN_TRANSIT.
599
        Returns a list of orders that were authorized for pickup but did not appear in the pick-up report.
600
 
601
        Parameters:
602
         - providerId
603
         - pickupDetails
604
        """
605
        try:
606
            orders_not_picked_up = mark_doas_as_picked_up(providerId, pickupDetails)
607
            return [to_t_order(order) for order in orders_not_picked_up]
608
        finally:
609
            close_session()
2591 chandransh 610
 
2616 chandransh 611
    def receiveReturn(self, orderId):
2591 chandransh 612
        """
2599 chandransh 613
        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 614
        If the order is in any other state, it returns false.
615
        Throws an exception if the order with the given id couldn't be found.
616
 
617
        Parameters:
618
         - orderId
619
        """
620
        try:
2616 chandransh 621
            return receive_return(orderId)
2591 chandransh 622
        finally:
623
            close_session()
624
 
625
    def validateDoa(self, orderId, isValid):
626
        """
2599 chandransh 627
        Used to validate the DOA certificate for an order in the DOA_RECEIVED state. If the certificate is valid,
2609 chandransh 628
        the order state is changed to DOA_CERT_PENDING.
2591 chandransh 629
        If the certificate is invalid, the order state is changed to DOA_CERT_INVALID.
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
         - isValid
636
        """
637
        try:
638
            return validate_doa(orderId, isValid)
639
        finally:
640
            close_session()
2628 chandransh 641
 
642
    def reshipOrder(self, orderId):
643
        """
644
        If the order is in SALES_RET_RECEIVED or DOA_CERT_INVALID state, it does the following:
645
            1. Creates a new order for processing in the BILLED state. All billing information is saved.
646
            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.
647
 
648
        If the order is in DOA_CERT_VALID state, it does the following:
649
            1. Creates a new order for processing in the SUBMITTED_FOR_PROCESSING state.
650
            2. Creates a return order for the warehouse executive to return the DOA material.
651
            3. Marks the current order as the final DOA_RESHIPPED state.
652
 
653
        Returns the id of the newly created order.
654
 
655
        Throws an exception if the order with the given id couldn't be found.
656
 
657
        Parameters:
658
         - orderId
659
        """
660
        try:
661
            return reship_order(orderId)
662
        finally:
663
            close_session()
664
 
3226 chandransh 665
    def refundOrder(self, orderId, refundedBy, reason):
2628 chandransh 666
        """
667
        If the order is in SALES_RET_RECEIVED, DOA_CERT_VALID or DOA_CERT_INVALID state, it does the following:
668
            1. Creates a refund request for batch processing.
669
            2. Creates a return order for the warehouse executive to return the shipped material.
670
            3. Marks the current order as SALES_RET_REFUNDED, DOA_VALID_REFUNDED or DOA_INVALID_REFUNDED final states.
671
 
672
        If the order is in SUBMITTED_FOR_PROCESSING or INVENTORY_LOW state, it does the following:
673
            1. Creates a refund request for batch processing.
3226 chandransh 674
            2. Cancels the reservation of the item in the warehouse.
675
            3. Marks the current order as the REFUNDED final state.
2628 chandransh 676
 
3226 chandransh 677
        For all COD orders, if the order is in INIT, SUBMITTED_FOR_PROCESSING or INVENTORY_LOW state, it does the following:
678
            1. Cancels the reservation of the item in the warehouse.
679
            2. Marks the current order as CANCELED.
680
 
681
        In all cases, it updates the reason for cancellation or refund and the person who performed the action.
682
 
2628 chandransh 683
        Returns True if it is successful, False otherwise.
684
 
685
        Throws an exception if the order with the given id couldn't be found.
686
 
687
        Parameters:
688
         - orderId
3226 chandransh 689
         - refundedBy
690
         - reason
2628 chandransh 691
        """
692
        try:
3226 chandransh 693
            return refund_order(orderId, refundedBy, reason)
2628 chandransh 694
        finally:
695
            close_session()
696
 
2697 chandransh 697
    def getReturnOrders(self, warehouseId, fromDate, toDate):
698
        """
699
        Get all return orders created between the from and to dates for the given warehouse.
700
        Ignores the warehouse if it is passed as -1.
701
 
702
        Parameters:
703
         - warehouseId
704
         - fromDate
705
         - toDate
706
        """
707
        try:
708
            from_date, to_date = get_fdate_tdate(fromDate, toDate)
709
            return get_return_orders(warehouseId, from_date, to_date)
710
        finally:
711
            close_session()
712
 
2700 chandransh 713
    def getReturnOrder(self, id):
714
        """
715
        Returns the ReturnOrder corresponding to the given id.
716
        Throws an exception if the return order with the given id couldn't be found.
717
 
718
        Parameters:
719
         - id
720
        """
721
        try:
722
            return get_return_order(id)
723
        finally:
724
            close_session()
725
 
2697 chandransh 726
    def processReturn(self, returnOrderId):
727
        """
728
        Marks the return order with the given id as processed. Raises an exception if no such return order exists.
729
 
730
        Parameters:
731
         - returnOrderId
732
        """
733
        try:
734
            process_return(returnOrderId)
735
        finally:
736
            close_session()
2591 chandransh 737
 
2819 chandransh 738
    def createPurchaseOrder(self, warehouseId):
739
        """
740
        Creates a purchase order corresponding to all the pending orders of the given warehouse.
741
        Returns the PO no. of the newly created purchase order.
742
 
743
        Parameters:
744
         - warehouseId
745
        """
746
        try:
747
            return create_purchase_order(warehouseId)
748
        finally:
749
            close_session()
750
 
2536 chandransh 751
    def closeSession(self, ):
752
        close_session()
3376 rajveer 753
 
754
    def isAlive(self, ):
755
        """
756
        For checking weather service is active alive or not. It also checks connectivity with database
757
        """
758
        try:
759
            return is_alive()
760
        finally:
761
            close_session()