Subversion Repositories SmartDukaan

Rev

Rev 1627 | Rev 1886 | 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,\
1731 ankur.sing 17
    get_cust_count_with_successful_txn, get_valid_orders_amount_range
1405 ankur.sing 18
 
104 ashish 19
from shop2020.model.v1.order.impl.Convertors import to_t_transaction,\
1348 chandransh 20
    to_t_alert, to_t_order, to_t_lineitem
483 rajveer 21
from shop2020.thriftpy.model.v1.order.ttypes import Transaction, OrderStatus,\
22
    LineItem, Order
738 chandransh 23
from shop2020.utils.Utils import to_py_date, get_fdate_tdate
104 ashish 24
 
25
class OrderServiceHandler:
26
 
1249 chandransh 27
    def __init__(self, dbname='transaction'):
483 rajveer 28
        """
104 ashish 29
        Constructor
483 rajveer 30
        """
1249 chandransh 31
        DataService.initialize(dbname)
104 ashish 32
 
33
    def createTransaction(self, transaction):
34
        """
35
        Parameters:
36
         - transaction
37
        """
766 rajveer 38
        try:
39
            return create_transaction(transaction)
40
        finally:
41
            close_session()
42
 
104 ashish 43
    def getTransaction(self, id):
44
        """
45
            Get transaction methods.
46
 
47
        Parameters:
48
         - id
49
        """
766 rajveer 50
        try:
51
            transaction = get_transaction(id)
52
            return to_t_transaction(transaction)
53
        finally:
54
            close_session()
55
 
483 rajveer 56
    def getTransactionsForCustomer(self, customerId, from_date, to_date, status):
104 ashish 57
        """
58
        Parameters:
483 rajveer 59
         - customerId
104 ashish 60
         - from_date
61
         - to_date
483 rajveer 62
         - status
104 ashish 63
        """
766 rajveer 64
        try:
65
            current_from_date, current_to_date = get_fdate_tdate(from_date, to_date)
66
 
67
            transactions = get_transactions_for_customer(customerId, current_from_date, current_to_date, status)
68
            t_transaction = []
69
            for transaction in transactions:
70
                t_transaction.append(to_t_transaction(transaction))
71
            return t_transaction
72
        finally:
73
            close_session()
483 rajveer 74
 
75
    def getTransactionsForShoppingCartId(self, shoppingCartId):
76
        """
77
        Parameters:
78
            - shoppingCartId
79
        """
766 rajveer 80
        try:
81
            transactions = get_transactions_for_shopping_cart_id(shoppingCartId)
82
            t_transaction = []
83
            for transaction in transactions:
84
                t_transaction.append(to_t_transaction(transaction))
85
            return t_transaction
86
        finally:
87
            close_session()
104 ashish 88
 
483 rajveer 89
    def getTransactionStatus(self, transactionId):
104 ashish 90
        """
91
        Parameters:
483 rajveer 92
         - transactionId
93
        """
766 rajveer 94
        try:
95
            return get_transaction_status(transactionId)
96
        finally:
97
            close_session()
98
 
483 rajveer 99
    def changeTransactionStatus(self, transactionId, status, description):
100
        """
101
        Parameters:
102
         - transactionId
104 ashish 103
         - status
483 rajveer 104
         - description
105
        """
766 rajveer 106
        try:
107
            return change_transaction_status(transactionId, status, description)
108
        finally:
109
            close_session()
110
 
1528 ankur.sing 111
    def getOrdersForTransaction(self, transactionId, customerId):
483 rajveer 112
        """
1528 ankur.sing 113
        Returns list of orders for given transaction Id. Also filters based on customer Id so that
114
        only user who owns the transaction can view its order details.
115
 
483 rajveer 116
        Parameters:
117
         - transactionId
1528 ankur.sing 118
         - customerId
483 rajveer 119
        """
766 rajveer 120
        try:
1528 ankur.sing 121
            orders = get_orders_for_transaction(transactionId, customerId)
766 rajveer 122
            return [to_t_order(order) for order in orders]    
123
        finally:
124
            close_session()
125
 
483 rajveer 126
    def getAllOrders(self, status, from_date, to_date, warehouse_id):
127
        """
128
        Parameters:
129
         - status
104 ashish 130
         - from_date
131
         - to_date
483 rajveer 132
         - warehouse_id
104 ashish 133
        """
766 rajveer 134
        try:
135
            current_from_date, current_to_date = get_fdate_tdate(from_date, to_date)        
136
            orders = get_all_orders(status, current_from_date, current_to_date, warehouse_id)
137
            return [to_t_order(order) for order in orders]
138
        finally:
139
            close_session()
140
 
995 varun.gupt 141
    def getOrdersByBillingDate(self, status, start_billing_date, end_billing_date, warehouse_id):
142
        """
143
        Parameters:
144
         - status
145
         - start_billing_date
146
         - end_billing_date
147
         - warehouse_id
148
        """
149
        try:
150
            current_start_billing_date, current_end_billing_date = get_fdate_tdate(start_billing_date, end_billing_date)
151
            orders = get_orders_by_billing_date(status, current_start_billing_date, current_end_billing_date, warehouse_id)
152
            return [to_t_order(order) for order in orders]
153
        finally:
154
            close_session()
1382 varun.gupt 155
 
156
    def getReturnableOrdersForCustomer(self, customerId, limit = None):
157
        """
158
        Parameters:
159
         - customerId
160
         - limit
161
        """
162
        try:
163
            return get_returnable_orders_for_customer(customerId, limit)
164
        finally:
165
            close_session()
995 varun.gupt 166
 
1382 varun.gupt 167
    def getCancellableOrdersForCustomer(self, customerId, limit = None):
168
        """
169
        Parameters:
170
         - customerId
171
         - limit
172
        """
173
        try:
174
            return get_cancellable_orders_for_customer(customerId, limit)
175
        finally:
176
            close_session()
177
 
483 rajveer 178
    def changeOrderStatus(self, orderId, status, description):
179
        """
180
        Parameters:
181
         - orderId
182
         - status
183
         - description
184
         - 
185
        """
766 rajveer 186
        try:
187
            return change_order_status(self, orderId, status, description)
188
        finally:
189
            close_session()
921 rajveer 190
 
191
    def acceptOrder(self, orderId):
192
        """
193
        Parameters:
194
         - orderId
195
        """
196
        try:
197
            return accept_order(self, orderId)
198
        finally:
199
            close_session()
200
 
201
    def billOrder(self, orderId):
202
        """
203
        Parameters:
204
         - orderId
205
        """
206
        try:
207
            return bill_order(self, orderId)
208
        finally:
209
            close_session()
766 rajveer 210
 
483 rajveer 211
    def getOrdersForCustomer(self, customerId, from_date, to_date, status):
104 ashish 212
        """
213
        Parameters:
214
         - customerId
215
         - from_date
216
         - to_date
217
         - status
218
        """
766 rajveer 219
        try:
220
            current_from_date, current_to_date = get_fdate_tdate(from_date, to_date)
221
 
222
            orders = get_orders_for_customer(customerId, current_from_date, current_to_date, status)
223
            return [to_t_order(order) for order in orders]
224
        finally:
225
            close_session()
1528 ankur.sing 226
 
227
    def getOrderForCustomer(self, orderId, customerId):
228
        """
229
        Returns an order for the order Id. Also checks if the order belongs to the customer whose Id is passed.
230
        Throws exception if either order Id is invalid or order does not below to the customer whose Id is passed.
231
 
232
        Parameters:
233
         - customerId
234
         - orderId
235
        """
236
        try:
237
            return to_t_order(get_order_for_customer(orderId, customerId))
238
        finally:
239
            close_session()
766 rajveer 240
 
483 rajveer 241
    def getOrder(self, id):
242
        """
243
        Parameters:
244
         - id
245
        """
766 rajveer 246
        try:
247
            return to_t_order(get_order(id))
248
        finally:
249
            close_session()
250
 
483 rajveer 251
    def getLineItemsForOrder(self, orderId):
252
        """
253
        Parameters:
254
         - orderId
255
        """
766 rajveer 256
        try:
257
            lineitems = get_line_items_for_order(orderId)
258
            t_lineitems = []
259
            for lineitem in lineitems:
260
                t_lineitems.append(to_t_lineitem(lineitem))
261
            return t_lineitems
262
        finally:
263
            close_session()
1149 chandransh 264
 
265
    def addBillingDetails(self, orderId, invoice_number, billed_by):
494 rajveer 266
        """
1149 chandransh 267
        Add billing details such as the bill number and the biller to the Order.
268
 
494 rajveer 269
        Parameters:
270
         - orderId
271
         - invoice_number
272
         - billed_by
273
        """
766 rajveer 274
        try:
1149 chandransh 275
            return add_billing_details(orderId, invoice_number, billed_by)
766 rajveer 276
        finally:
277
            close_session()
1149 chandransh 278
 
279
    def addJacketNumber(self, orderId, jacketNumber):
280
        """
281
        Adds jacket number to the order. Return false if it doesn't find the order with the given ID.
282
 
283
        Parameters:
284
         - orderId
285
         - jacketNumber
286
        """
287
        try:
288
            return add_jacket_number(orderId, jacketNumber)
289
        finally:
290
            close_session()
1220 chandransh 291
 
292
    def batchOrders(self, warehouseId):
293
        """
294
        Create a batch of all the pending orders for the given warehouse.
295
        The returned list is orderd by created_timestamp.
296
        If there are no pending orders, an empty list is returned.
1208 chandransh 297
 
1220 chandransh 298
        Parameters:
299
         - warehouseId
300
        """
301
        try:
302
            pending_orders = batch_orders(warehouseId)
303
            return [to_t_order(order) for order in pending_orders]
304
        finally:
305
            close_session()
306
 
1208 chandransh 307
    def markOrderAsOutOfStock(self, orderId):
308
        """
309
        Mark the given order as out of stock. Throws an exception if the order with the given Id couldn't be found.
310
 
311
 
312
        Parameters:
313
         - orderId
314
        """
315
        try:
316
            return order_outofstock(orderId)
317
        finally:
318
            close_session()
319
 
759 chandransh 320
    def markOrdersAsManifested(self, warehouseId, providerId):
321
        """
322
        Marks all BILLED orders for a warehouse and a provider as SHIPPED_FROM_WH
323
 
324
        Parameters:
325
         - warehouseId
326
         - providerId
327
        """
766 rajveer 328
        try:
329
            return mark_orders_as_manifested(warehouseId, providerId)
330
        finally:
331
            close_session()
1113 chandransh 332
 
333
    def markOrdersAsPickedUp(self, providerId, pickupDetails):
334
        """
335
        Marks all SHIPPED_FROM_WH orders of the previous day for a provider as SHIPPED_TO_LOGISTICS.
336
        Returns a list of orders that were shipped from warehouse but did not appear in the pick-up report.
337
        Raises an exception if we encounter report for an AWB number that we did not ship.
338
 
339
        Parameters:
340
         - providerId
341
         - pickupDetails
342
        """
343
        try:
344
            orders_not_picked_up = mark_orders_as_picked_up(providerId, pickupDetails)
345
            return [to_t_order(order) for order in orders_not_picked_up]
346
        finally:
347
            close_session()
1132 chandransh 348
 
349
    def markOrdersAsDelivered(self, providerId, deliveredOrders):
350
        """
351
        Marks all orders with AWBs in the given map as delivered. Also sets the delivery timestamp and
352
        the name of the receiver.
353
        Raises an exception if we encounter report for an AWB number that we did not ship.
354
 
355
        Parameters:
356
         - providerId
357
         - deliveredOrders
358
        """
359
        try:
360
            mark_orders_as_delivered(providerId, deliveredOrders)
361
        finally:
362
            close_session()
1135 chandransh 363
 
364
    def markOrdersAsFailed(self, providerId, returnedOrders):
365
        """
366
        Mark all orders with AWBs in the given map as failed. Also sets the delivery timestamp.
367
        Raises an exception if we encounter report for an AWB number that we did not ship.
368
 
369
        Parameters:
370
         - providerId
371
         - returnedOrders
372
        """
373
        try:
374
            mark_orders_as_failed(providerId, returnedOrders)
375
        finally:
376
            close_session()
1246 chandransh 377
 
378
    def updateNonDeliveryReason(self, providerId, undeliveredOrders):
379
        """
380
        Update the status description of orders whose AWB numbers are keys of the Map.
381
 
382
        Parameters:
383
         - providerId
384
         - undelivered_orders
385
        """
386
        try:
387
            update_non_delivery_reason(providerId, undeliveredOrders)
388
        finally:
389
            close_session()
1405 ankur.sing 390
 
391
    def getUndeliveredOrders(self, providerId, warehouseId):
392
        """
393
        Returns the list of orders whose delivery time has passed but have not been
394
        delivered yet for the given provider and warehouse. To get a complete list of
395
        undelivered orders, pass them as -1.
396
        Returns an empty list if no such orders exist.
397
 
398
        Parameters:
399
         - providerId
400
         - warehouseId
401
        """
402
        try:
403
            undelivered_orders = get_undelivered_orders(providerId, warehouseId)
404
            return [to_t_order(order) for order in undelivered_orders]
405
        finally:
406
            close_session()
1351 varun.gupt 407
 
1398 varun.gupt 408
    def enqueueTransactionInfoEmail(self, transactionId):
1351 varun.gupt 409
        """
1398 varun.gupt 410
        Save the email containing order details to be sent to customer later by batch job
1351 varun.gupt 411
 
412
        Parameters:
413
         - transactionId
414
        """
415
        try:
1398 varun.gupt 416
            return enqueue_transaction_info_email(transactionId)
1351 varun.gupt 417
        finally:
418
            close_session()
419
 
483 rajveer 420
    def getAlerts(self, orderId, valid):
421
        """
422
        Parameters:
423
         - orderId
424
         - valid
425
        """
766 rajveer 426
        try:
427
            alerts = get_alerts(orderId, valid)
428
 
429
            t_alerts = []
430
 
431
            for alert in alerts:
432
                t_alerts.append(to_t_alert(alert)) 
433
 
434
            return t_alerts
435
        finally:
436
            close_session()
437
 
483 rajveer 438
    def setAlert(self, orderId, unset, type, comment):
439
        """
440
        Parameters:
441
         - orderId
442
         - unset
443
         - type
444
         - comment
445
        """
766 rajveer 446
        try:
447
            set_alert(orderId, unset, type, comment)
448
        finally:
449
            close_session()
1596 ankur.sing 450
 
451
    def getValidOrderCount(self, ):
452
        """
453
        Return the number of valid orders. (OrderStatus >= OrderStatus.SUBMITTED_FOR_PROCESSING)
454
        """
1731 ankur.sing 455
        try:
456
            return get_valid_order_count()
457
        finally:
458
            close_session()
1627 ankur.sing 459
 
460
    def getNoOfCustomersWithSuccessfulTransaction(self, ):
461
        """
462
        Returns the number of distinct customers who have done successful transactions
463
        """
1731 ankur.sing 464
        try:
465
            return get_cust_count_with_successful_txn()
466
        finally:
467
            close_session()
1627 ankur.sing 468
 
1731 ankur.sing 469
 
470
    def getValidOrdersAmountRange(self, ):
1627 ankur.sing 471
        """
1731 ankur.sing 472
        Returns the minimum and maximum amounts of a valid order. (OrderStatus >= OrderStatus.SUBMITTED_FOR_PROCESSING)
473
        List contains two values, first minimum amount and second maximum amount.
1627 ankur.sing 474
        """
1731 ankur.sing 475
        try:
476
            return get_valid_orders_amount_range()
477
        finally:
478
            close_session()
1627 ankur.sing 479
 
766 rajveer 480
 
481
    def closeSession(self, ):
482
        close_session()
104 ashish 483
 
766 rajveer 484
 
485
 
486
    '''
483 rajveer 487
if __name__ == "__main__":
488
    order = OrderServiceHandler()
489
    t = order.getTransaction(4)
490
    print t
491
 
766 rajveer 492
 
483 rajveer 493
    these methods commented right now. may be used later
494
    def getAllTransactions(self, status, from_date, to_date, warehouse_id):
104 ashish 495
        """
496
        Parameters:
483 rajveer 497
         - status
104 ashish 498
         - from_date
499
         - to_date
500
        """
501
        current_status, current_from_date, current_to_date = get_status_fdate_tdate(status, from_date, to_date)
483 rajveer 502
 
503
        transactions = get_all_transactions(current_status, current_from_date, current_to_date, warehouse_id)
104 ashish 504
        t_transaction = []
505
        for transaction in transactions:
506
            t_transaction.append(to_t_transaction(transaction))
507
        return t_transaction
483 rajveer 508
 
509
    def getTransactionsForShipmentStatus(self, status, from_date, to_date):
132 ashish 510
        """
511
        Parameters:
483 rajveer 512
         - status
513
         - from_date
514
         - to_date
132 ashish 515
        """
483 rajveer 516
        current_status, current_from_date, current_to_date = get_status_fdate_tdate(status, from_date, to_date)
517
 
518
        transactions = get_transactions_for_shipment_status(current_status, current_from_date, current_to_date)
132 ashish 519
        t_transaction = []
520
        for transaction in transactions:
521
            t_transaction.append(to_t_transaction(transaction))
522
        return t_transaction
483 rajveer 523
 
524
    def getTransactionsForCustomerAndShipmentStatus(self, customerId, from_date, to_date, status):
104 ashish 525
        """
526
        Parameters:
483 rajveer 527
         - customerId
528
         - from_date
529
         - to_date
104 ashish 530
         - status
531
        """
483 rajveer 532
        current_status, current_from_date, current_to_date = get_status_fdate_tdate(status, from_date, to_date)
533
 
534
        transactions = get_transactions_for_customer_shipment(customerId, current_from_date, current_to_date, current_status)
535
        t_transaction = []
536
        for transaction in transactions:
537
            t_transaction.append(to_t_transaction(transaction))
538
        return t_transaction
539
 
104 ashish 540
    def getOrderInfo(self, transactionId):
541
        """
542
            Get and set individual objects in it
543
        *
544
 
545
        Parameters:
546
         - transactionId
547
        """
548
        lineitems = get_line_items(transactionId)
549
        order_info = OrderInfo()
550
        order_info.lineitems = []
551
        for lineitem in lineitems:
552
            order_info.lineitems.append(to_t_lineitem(lineitem))
553
            order_info.id = lineitem.transaction.id
554
 
555
        return order_info
556
 
557
    def getShippingInfo(self, transactionId):
558
        """
559
        Parameters:
560
         - transactionId
561
        """
562
        shipments = get_shipments(transactionId)
563
        shipment_info = ShipmentInfo()
564
        shipment_info.shipments = []
565
        for shipment in shipments:
566
            shipment_info.shipments.append(to_t_shipment(shipment))
567
            shipment_info.id = shipment.id
568
        return shipment_info
569
    def getBillingInfo(self, transactionId):
570
        """
571
        Parameters:
572
         - transactionId
573
        """
574
        billings = get_billings(transactionId)
575
        billing_info = BillingInfo()
576
        billing_info.billings = []
577
        if billings:
578
            for billing in billings:
579
                billing_info.billings.append(to_t_billing(billing))
580
                billing_info.id = billing.line_item.transaction.id
581
        return billing_info
582
 
583
    def addBilling(self, transactionId, billing):
584
        """
585
        Parameters:
586
         - tranactionId
587
         - billing
588
        """
589
        return set_billing(transactionId, billing)
590
 
591
    def setShippingTracker(self, transactionId, shippingId, trackingId, airwayBillNo, provider):
592
        """
593
        Parameters:
594
         - transactionId
595
         - shippingId
596
         - trackingId
597
         - airwayBillNo
598
         - provider
599
        """
600
        return set_shipping_tracker_info(transactionId, shippingId, trackingId, airwayBillNo, provider)
601
 
602
    def setShippingDate(self, transactionId, shippingId, timestamp):
603
        """
604
        Parameters:
605
         - transactionId
606
         - shippingId
607
         - timestamp
608
        """
609
        return set_shipping_date(transactionId, shippingId, to_py_date(timestamp))
610
 
611
    def setDeliveryDate(self, transactionId, shippingid, timestamp):
612
        """
613
        Parameters:
614
         - transactionId
615
         - shippingid
616
         - timestamp
617
        """
618
        return set_delivery_date(transactionId, shippingid, timestamp)
483 rajveer 619
 
304 ashish 620
    def getAlerts(self, transactionId, valid):
621
        """
622
        Parameters:
623
         - transactionId
624
         - valid
625
        """
626
        alerts = get_alerts(transactionId, valid)
627
 
628
        t_alerts = []
629
 
630
        for alert in alerts:
631
            t_alerts.append(to_t_alert(alert)) 
632
 
633
        return t_alerts
634
 
635
    def setAlert(self, transactionId, unset, type, comment):
636
        """
637
        Parameters:
638
         - transactionId
639
         - unset
640
         - type
641
         - comment
642
        """
643
        set_alert(transactionId, unset, type, comment)
644
 
645
    def getExtraInfo(self, transaction_id):
646
        """
647
        Parameters:
648
         - transaction_id
649
        """
650
        extra_info = get_extra_info(transaction_id)
651
 
652
        return to_t_extra_item_info(extra_info, transaction_id)
653
 
654
 
655
    def setExtraInfo(self, transaction_id, sku_id, model, colour, vendor):
656
        """
657
        Parameters:
658
         - transaction_id
659
         - sku_id
660
         - model
661
         - colour
662
         - vendor
663
        """
664
        set_extra_info(transaction_id, sku_id, model, vendor, colour)
665
 
483 rajveer 666
    '''
104 ashish 667