Subversion Repositories SmartDukaan

Rev

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