Subversion Repositories SmartDukaan

Rev

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