Subversion Repositories SmartDukaan

Rev

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