Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
104 ashish 1
'''
2
Created on 29-Mar-2010
3
 
4
@author: ashish
5
'''
6
from shop2020.model.v1.order.impl import DataService
7
from shop2020.model.v1.order.impl.DataAccessors import create_transaction,\
1382 varun.gupt 8
    create_order, get_new_transaction, get_transactions_for_customer, get_transaction_status,\
9
    get_line_items_for_order, get_transaction, get_transactions_for_shopping_cart_id,\
10
    change_transaction_status, get_orders_for_customer,get_orders_for_transaction, get_order,\
11
    get_returnable_orders_for_customer, get_cancellable_orders_for_customer, get_orders_by_billing_date,\
12
    get_all_orders, change_order_status, get_alerts, set_alert, add_billing_details,\
13
    mark_orders_as_manifested, close_session, accept_order, bill_order, mark_orders_as_picked_up,\
1208 chandransh 14
    mark_orders_as_delivered, mark_orders_as_failed, add_jacket_number,\
1405 ankur.sing 15
    order_outofstock, batch_orders, update_non_delivery_reason, enqueue_transaction_info_email,\
1627 ankur.sing 16
    get_undelivered_orders, get_order_for_customer, get_valid_order_count,\
1886 ankur.sing 17
    get_cust_count_with_successful_txn, get_valid_orders_amount_range,\
2591 chandransh 18
    get_valid_orders, toggle_doa_flag, request_pickup_number, authorize_pickup,\
19
    accept_doa, validate_doa
1405 ankur.sing 20
 
104 ashish 21
from shop2020.model.v1.order.impl.Convertors import to_t_transaction,\
1348 chandransh 22
    to_t_alert, to_t_order, to_t_lineitem
483 rajveer 23
from shop2020.thriftpy.model.v1.order.ttypes import Transaction, OrderStatus,\
24
    LineItem, Order
738 chandransh 25
from shop2020.utils.Utils import to_py_date, get_fdate_tdate
104 ashish 26
 
27
class OrderServiceHandler:
28
 
1249 chandransh 29
    def __init__(self, dbname='transaction'):
483 rajveer 30
        """
104 ashish 31
        Constructor
483 rajveer 32
        """
1249 chandransh 33
        DataService.initialize(dbname)
104 ashish 34
 
35
    def createTransaction(self, transaction):
36
        """
37
        Parameters:
38
         - transaction
39
        """
766 rajveer 40
        try:
41
            return create_transaction(transaction)
42
        finally:
43
            close_session()
44
 
104 ashish 45
    def getTransaction(self, id):
46
        """
47
            Get transaction methods.
48
 
49
        Parameters:
50
         - id
51
        """
766 rajveer 52
        try:
53
            transaction = get_transaction(id)
54
            return to_t_transaction(transaction)
55
        finally:
56
            close_session()
57
 
483 rajveer 58
    def getTransactionsForCustomer(self, customerId, from_date, to_date, status):
104 ashish 59
        """
60
        Parameters:
483 rajveer 61
         - customerId
104 ashish 62
         - from_date
63
         - to_date
483 rajveer 64
         - status
104 ashish 65
        """
766 rajveer 66
        try:
67
            current_from_date, current_to_date = get_fdate_tdate(from_date, to_date)
68
 
69
            transactions = get_transactions_for_customer(customerId, current_from_date, current_to_date, status)
70
            t_transaction = []
71
            for transaction in transactions:
72
                t_transaction.append(to_t_transaction(transaction))
73
            return t_transaction
74
        finally:
75
            close_session()
483 rajveer 76
 
77
    def getTransactionsForShoppingCartId(self, shoppingCartId):
78
        """
79
        Parameters:
80
            - shoppingCartId
81
        """
766 rajveer 82
        try:
83
            transactions = get_transactions_for_shopping_cart_id(shoppingCartId)
84
            t_transaction = []
85
            for transaction in transactions:
86
                t_transaction.append(to_t_transaction(transaction))
87
            return t_transaction
88
        finally:
89
            close_session()
104 ashish 90
 
483 rajveer 91
    def getTransactionStatus(self, transactionId):
104 ashish 92
        """
93
        Parameters:
483 rajveer 94
         - transactionId
95
        """
766 rajveer 96
        try:
97
            return get_transaction_status(transactionId)
98
        finally:
99
            close_session()
100
 
483 rajveer 101
    def changeTransactionStatus(self, transactionId, status, description):
102
        """
103
        Parameters:
104
         - transactionId
104 ashish 105
         - status
483 rajveer 106
         - description
107
        """
766 rajveer 108
        try:
109
            return change_transaction_status(transactionId, status, description)
110
        finally:
111
            close_session()
112
 
1528 ankur.sing 113
    def getOrdersForTransaction(self, transactionId, customerId):
483 rajveer 114
        """
1528 ankur.sing 115
        Returns list of orders for given transaction Id. Also filters based on customer Id so that
116
        only user who owns the transaction can view its order details.
117
 
483 rajveer 118
        Parameters:
119
         - transactionId
1528 ankur.sing 120
         - customerId
483 rajveer 121
        """
766 rajveer 122
        try:
1528 ankur.sing 123
            orders = get_orders_for_transaction(transactionId, customerId)
766 rajveer 124
            return [to_t_order(order) for order in orders]    
125
        finally:
126
            close_session()
127
 
483 rajveer 128
    def getAllOrders(self, status, from_date, to_date, warehouse_id):
129
        """
130
        Parameters:
131
         - status
104 ashish 132
         - from_date
133
         - to_date
483 rajveer 134
         - warehouse_id
104 ashish 135
        """
766 rajveer 136
        try:
137
            current_from_date, current_to_date = get_fdate_tdate(from_date, to_date)        
138
            orders = get_all_orders(status, current_from_date, current_to_date, warehouse_id)
139
            return [to_t_order(order) for order in orders]
140
        finally:
141
            close_session()
142
 
995 varun.gupt 143
    def getOrdersByBillingDate(self, status, start_billing_date, end_billing_date, warehouse_id):
144
        """
145
        Parameters:
146
         - status
147
         - start_billing_date
148
         - end_billing_date
149
         - warehouse_id
150
        """
151
        try:
152
            current_start_billing_date, current_end_billing_date = get_fdate_tdate(start_billing_date, end_billing_date)
153
            orders = get_orders_by_billing_date(status, current_start_billing_date, current_end_billing_date, warehouse_id)
154
            return [to_t_order(order) for order in orders]
155
        finally:
156
            close_session()
1382 varun.gupt 157
 
158
    def getReturnableOrdersForCustomer(self, customerId, limit = None):
159
        """
160
        Parameters:
161
         - customerId
162
         - limit
163
        """
164
        try:
165
            return get_returnable_orders_for_customer(customerId, limit)
166
        finally:
167
            close_session()
995 varun.gupt 168
 
1382 varun.gupt 169
    def getCancellableOrdersForCustomer(self, customerId, limit = None):
170
        """
171
        Parameters:
172
         - customerId
173
         - limit
174
        """
175
        try:
176
            return get_cancellable_orders_for_customer(customerId, limit)
177
        finally:
178
            close_session()
179
 
483 rajveer 180
    def changeOrderStatus(self, orderId, status, description):
181
        """
182
        Parameters:
183
         - orderId
184
         - status
185
         - description
186
         - 
187
        """
766 rajveer 188
        try:
189
            return change_order_status(self, orderId, status, description)
190
        finally:
191
            close_session()
921 rajveer 192
 
193
    def acceptOrder(self, orderId):
194
        """
195
        Parameters:
196
         - orderId
197
        """
198
        try:
199
            return accept_order(self, orderId)
200
        finally:
201
            close_session()
202
 
203
    def billOrder(self, orderId):
204
        """
205
        Parameters:
206
         - orderId
207
        """
208
        try:
209
            return bill_order(self, orderId)
210
        finally:
211
            close_session()
766 rajveer 212
 
483 rajveer 213
    def getOrdersForCustomer(self, customerId, from_date, to_date, status):
104 ashish 214
        """
215
        Parameters:
216
         - customerId
217
         - from_date
218
         - to_date
219
         - status
220
        """
766 rajveer 221
        try:
222
            current_from_date, current_to_date = get_fdate_tdate(from_date, to_date)
223
 
224
            orders = get_orders_for_customer(customerId, current_from_date, current_to_date, status)
225
            return [to_t_order(order) for order in orders]
226
        finally:
227
            close_session()
1528 ankur.sing 228
 
229
    def getOrderForCustomer(self, orderId, customerId):
230
        """
231
        Returns an order for the order Id. Also checks if the order belongs to the customer whose Id is passed.
232
        Throws exception if either order Id is invalid or order does not below to the customer whose Id is passed.
233
 
234
        Parameters:
235
         - customerId
236
         - orderId
237
        """
238
        try:
239
            return to_t_order(get_order_for_customer(orderId, customerId))
240
        finally:
241
            close_session()
766 rajveer 242
 
483 rajveer 243
    def getOrder(self, id):
244
        """
245
        Parameters:
246
         - id
247
        """
766 rajveer 248
        try:
249
            return to_t_order(get_order(id))
250
        finally:
251
            close_session()
252
 
483 rajveer 253
    def getLineItemsForOrder(self, orderId):
254
        """
255
        Parameters:
256
         - orderId
257
        """
766 rajveer 258
        try:
259
            lineitems = get_line_items_for_order(orderId)
260
            t_lineitems = []
261
            for lineitem in lineitems:
262
                t_lineitems.append(to_t_lineitem(lineitem))
263
            return t_lineitems
264
        finally:
265
            close_session()
1149 chandransh 266
 
267
    def addBillingDetails(self, orderId, invoice_number, billed_by):
494 rajveer 268
        """
1149 chandransh 269
        Add billing details such as the bill number and the biller to the Order.
270
 
494 rajveer 271
        Parameters:
272
         - orderId
273
         - invoice_number
274
         - billed_by
275
        """
766 rajveer 276
        try:
1149 chandransh 277
            return add_billing_details(orderId, invoice_number, billed_by)
766 rajveer 278
        finally:
279
            close_session()
1149 chandransh 280
 
281
    def addJacketNumber(self, orderId, jacketNumber):
282
        """
283
        Adds jacket number to the order. Return false if it doesn't find the order with the given ID.
284
 
285
        Parameters:
286
         - orderId
287
         - jacketNumber
288
        """
289
        try:
290
            return add_jacket_number(orderId, jacketNumber)
291
        finally:
292
            close_session()
1220 chandransh 293
 
294
    def batchOrders(self, warehouseId):
295
        """
296
        Create a batch of all the pending orders for the given warehouse.
297
        The returned list is orderd by created_timestamp.
298
        If there are no pending orders, an empty list is returned.
1208 chandransh 299
 
1220 chandransh 300
        Parameters:
301
         - warehouseId
302
        """
303
        try:
304
            pending_orders = batch_orders(warehouseId)
305
            return [to_t_order(order) for order in pending_orders]
306
        finally:
307
            close_session()
308
 
1208 chandransh 309
    def markOrderAsOutOfStock(self, orderId):
310
        """
311
        Mark the given order as out of stock. Throws an exception if the order with the given Id couldn't be found.
312
 
313
 
314
        Parameters:
315
         - orderId
316
        """
317
        try:
318
            return order_outofstock(orderId)
319
        finally:
320
            close_session()
321
 
759 chandransh 322
    def markOrdersAsManifested(self, warehouseId, providerId):
323
        """
324
        Marks all BILLED orders for a warehouse and a provider as SHIPPED_FROM_WH
325
 
326
        Parameters:
327
         - warehouseId
328
         - providerId
329
        """
766 rajveer 330
        try:
331
            return mark_orders_as_manifested(warehouseId, providerId)
332
        finally:
333
            close_session()
1113 chandransh 334
 
335
    def markOrdersAsPickedUp(self, providerId, pickupDetails):
336
        """
337
        Marks all SHIPPED_FROM_WH orders of the previous day for a provider as SHIPPED_TO_LOGISTICS.
338
        Returns a list of orders that were shipped from warehouse but did not appear in the pick-up report.
339
        Raises an exception if we encounter report for an AWB number that we did not ship.
340
 
341
        Parameters:
342
         - providerId
343
         - pickupDetails
344
        """
345
        try:
346
            orders_not_picked_up = mark_orders_as_picked_up(providerId, pickupDetails)
347
            return [to_t_order(order) for order in orders_not_picked_up]
348
        finally:
349
            close_session()
1132 chandransh 350
 
351
    def markOrdersAsDelivered(self, providerId, deliveredOrders):
352
        """
353
        Marks all orders with AWBs in the given map as delivered. Also sets the delivery timestamp and
354
        the name of the receiver.
355
        Raises an exception if we encounter report for an AWB number that we did not ship.
356
 
357
        Parameters:
358
         - providerId
359
         - deliveredOrders
360
        """
361
        try:
362
            mark_orders_as_delivered(providerId, deliveredOrders)
363
        finally:
364
            close_session()
1135 chandransh 365
 
366
    def markOrdersAsFailed(self, providerId, returnedOrders):
367
        """
368
        Mark all orders with AWBs in the given map as failed. Also sets the delivery timestamp.
369
        Raises an exception if we encounter report for an AWB number that we did not ship.
370
 
371
        Parameters:
372
         - providerId
373
         - returnedOrders
374
        """
375
        try:
376
            mark_orders_as_failed(providerId, returnedOrders)
377
        finally:
378
            close_session()
1246 chandransh 379
 
380
    def updateNonDeliveryReason(self, providerId, undeliveredOrders):
381
        """
382
        Update the status description of orders whose AWB numbers are keys of the Map.
383
 
384
        Parameters:
385
         - providerId
386
         - undelivered_orders
387
        """
388
        try:
389
            update_non_delivery_reason(providerId, undeliveredOrders)
390
        finally:
391
            close_session()
1405 ankur.sing 392
 
393
    def getUndeliveredOrders(self, providerId, warehouseId):
394
        """
395
        Returns the list of orders whose delivery time has passed but have not been
396
        delivered yet for the given provider and warehouse. To get a complete list of
397
        undelivered orders, pass them as -1.
398
        Returns an empty list if no such orders exist.
399
 
400
        Parameters:
401
         - providerId
402
         - warehouseId
403
        """
404
        try:
405
            undelivered_orders = get_undelivered_orders(providerId, warehouseId)
406
            return [to_t_order(order) for order in undelivered_orders]
407
        finally:
408
            close_session()
1351 varun.gupt 409
 
1398 varun.gupt 410
    def enqueueTransactionInfoEmail(self, transactionId):
1351 varun.gupt 411
        """
1398 varun.gupt 412
        Save the email containing order details to be sent to customer later by batch job
1351 varun.gupt 413
 
414
        Parameters:
415
         - transactionId
416
        """
417
        try:
1398 varun.gupt 418
            return enqueue_transaction_info_email(transactionId)
1351 varun.gupt 419
        finally:
420
            close_session()
421
 
483 rajveer 422
    def getAlerts(self, orderId, valid):
423
        """
424
        Parameters:
425
         - orderId
426
         - valid
427
        """
766 rajveer 428
        try:
429
            alerts = get_alerts(orderId, valid)
430
 
431
            t_alerts = []
432
 
433
            for alert in alerts:
434
                t_alerts.append(to_t_alert(alert)) 
435
 
436
            return t_alerts
437
        finally:
438
            close_session()
439
 
483 rajveer 440
    def setAlert(self, orderId, unset, type, comment):
441
        """
442
        Parameters:
443
         - orderId
444
         - unset
445
         - type
446
         - comment
447
        """
766 rajveer 448
        try:
449
            set_alert(orderId, unset, type, comment)
450
        finally:
451
            close_session()
1596 ankur.sing 452
 
453
    def getValidOrderCount(self, ):
454
        """
455
        Return the number of valid orders. (OrderStatus >= OrderStatus.SUBMITTED_FOR_PROCESSING)
456
        """
1731 ankur.sing 457
        try:
458
            return get_valid_order_count()
459
        finally:
460
            close_session()
1627 ankur.sing 461
 
462
    def getNoOfCustomersWithSuccessfulTransaction(self, ):
463
        """
464
        Returns the number of distinct customers who have done successful transactions
465
        """
1731 ankur.sing 466
        try:
467
            return get_cust_count_with_successful_txn()
468
        finally:
469
            close_session()
1627 ankur.sing 470
 
1731 ankur.sing 471
 
472
    def getValidOrdersAmountRange(self, ):
1627 ankur.sing 473
        """
1731 ankur.sing 474
        Returns the minimum and maximum amounts of a valid order. (OrderStatus >= OrderStatus.SUBMITTED_FOR_PROCESSING)
475
        List contains two values, first minimum amount and second maximum amount.
1627 ankur.sing 476
        """
1731 ankur.sing 477
        try:
478
            return get_valid_orders_amount_range()
479
        finally:
480
            close_session()
1627 ankur.sing 481
 
1886 ankur.sing 482
    def getValidOrders(self, limit):
483
        """
484
        Returns list of Orders in descending order by Order creation date. List is restricted to limit Orders.
485
        If limit is passed as 0, then all valid Orders are returned.
486
 
487
        Parameters:
488
         - limit
489
        """
490
        try:
491
            return [to_t_order(order) for order in get_valid_orders(limit)]
492
        finally:
493
            close_session()
104 ashish 494
 
2536 chandransh 495
    def toggleDOAFlag(self, orderId):
104 ashish 496
        """
2536 chandransh 497
        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.
498
        Returns the final flag status.
499
        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 500
 
132 ashish 501
        Parameters:
2536 chandransh 502
         - orderId
132 ashish 503
        """
2536 chandransh 504
        try:
505
            return toggle_doa_flag(orderId)
506
        finally:
507
            close_session()
483 rajveer 508
 
2536 chandransh 509
    def requestPickupNumber(self, orderId):
104 ashish 510
        """
2536 chandransh 511
        Sends out an email to the account manager of the original courier provider used to ship the order.
512
        If the order status was DELIVERY_SUCCESS, it is changed to be DOA_PICKUP_REQUESTED.
513
        If the order status was DOA_PICKUP_REQUESTED, it is left unchanged.
514
        For any other status, it returns false.
515
        Throws an exception if the order with the given id couldn't be found.
104 ashish 516
 
517
        Parameters:
2536 chandransh 518
         - orderId
104 ashish 519
        """
2536 chandransh 520
        try:
521
            return request_pickup_number(orderId)
522
        finally:
523
            close_session()
483 rajveer 524
 
2536 chandransh 525
    def authorizePickup(self, orderId, pickupNumber):
304 ashish 526
        """
2536 chandransh 527
        If the order status is DOA_PICKUP_REQUESTED, it does the following
528
            1. Sends out an email to the customer with the dispatch advice that he has to print as an attachment.
529
            2. Changes order status to be DOA_PICKUP_AUTHORIZED.
530
            3. Returns true
531
        If the order is any other status, it returns false.
532
        Throws an exception if the order with the given id couldn't be found.
304 ashish 533
 
534
        Parameters:
2536 chandransh 535
         - orderId
536
         - pickupNumber
304 ashish 537
        """
2536 chandransh 538
        try:
539
            return authorize_pickup(orderId, pickupNumber)
540
        finally:
541
            close_session()    
2591 chandransh 542
 
543
    def acceptDoa(self, orderId):
544
        """
2599 chandransh 545
        If the order status is DOA_RETURN_AUTHORIZED or DOA_RETURN_IN_TRANSIT, marks the order status as DOA_RECEIVED and returns true.
2591 chandransh 546
        If the order is in any other state, it returns false.
547
        Throws an exception if the order with the given id couldn't be found.
548
 
549
        Parameters:
550
         - orderId
551
        """
552
        try:
553
            return accept_doa(orderId)
554
        finally:
555
            close_session()
556
 
557
    def validateDoa(self, orderId, isValid):
558
        """
2599 chandransh 559
        Used to validate the DOA certificate for an order in the DOA_RECEIVED state. If the certificate is valid,
2591 chandransh 560
        the order state is changed to DOA_PENDING.
561
        If the certificate is invalid, the order state is changed to DOA_CERT_INVALID.
562
        If the order is in any other state, it returns false.
563
        Throws an exception if the order with the given id couldn't be found.
564
 
565
        Parameters:
566
         - orderId
567
         - isValid
568
        """
569
        try:
570
            return validate_doa(orderId, isValid)
571
        finally:
572
            close_session()
573
 
2536 chandransh 574
    def closeSession(self, ):
575
        close_session()