Subversion Repositories SmartDukaan

Rev

Rev 3553 | Rev 3986 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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