Subversion Repositories SmartDukaan

Rev

Rev 1405 | Rev 1596 | 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,\
1528 ankur.sing 16
    get_undelivered_orders, get_order_for_customer
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()
449
 
450
    def closeSession(self, ):
451
        close_session()
104 ashish 452
 
766 rajveer 453
 
454
 
455
    '''
483 rajveer 456
if __name__ == "__main__":
457
    order = OrderServiceHandler()
458
    t = order.getTransaction(4)
459
    print t
460
 
766 rajveer 461
 
483 rajveer 462
    these methods commented right now. may be used later
463
    def getAllTransactions(self, status, from_date, to_date, warehouse_id):
104 ashish 464
        """
465
        Parameters:
483 rajveer 466
         - status
104 ashish 467
         - from_date
468
         - to_date
469
        """
470
        current_status, current_from_date, current_to_date = get_status_fdate_tdate(status, from_date, to_date)
483 rajveer 471
 
472
        transactions = get_all_transactions(current_status, current_from_date, current_to_date, warehouse_id)
104 ashish 473
        t_transaction = []
474
        for transaction in transactions:
475
            t_transaction.append(to_t_transaction(transaction))
476
        return t_transaction
483 rajveer 477
 
478
    def getTransactionsForShipmentStatus(self, status, from_date, to_date):
132 ashish 479
        """
480
        Parameters:
483 rajveer 481
         - status
482
         - from_date
483
         - to_date
132 ashish 484
        """
483 rajveer 485
        current_status, current_from_date, current_to_date = get_status_fdate_tdate(status, from_date, to_date)
486
 
487
        transactions = get_transactions_for_shipment_status(current_status, current_from_date, current_to_date)
132 ashish 488
        t_transaction = []
489
        for transaction in transactions:
490
            t_transaction.append(to_t_transaction(transaction))
491
        return t_transaction
483 rajveer 492
 
493
    def getTransactionsForCustomerAndShipmentStatus(self, customerId, from_date, to_date, status):
104 ashish 494
        """
495
        Parameters:
483 rajveer 496
         - customerId
497
         - from_date
498
         - to_date
104 ashish 499
         - status
500
        """
483 rajveer 501
        current_status, current_from_date, current_to_date = get_status_fdate_tdate(status, from_date, to_date)
502
 
503
        transactions = get_transactions_for_customer_shipment(customerId, current_from_date, current_to_date, current_status)
504
        t_transaction = []
505
        for transaction in transactions:
506
            t_transaction.append(to_t_transaction(transaction))
507
        return t_transaction
508
 
104 ashish 509
    def getOrderInfo(self, transactionId):
510
        """
511
            Get and set individual objects in it
512
        *
513
 
514
        Parameters:
515
         - transactionId
516
        """
517
        lineitems = get_line_items(transactionId)
518
        order_info = OrderInfo()
519
        order_info.lineitems = []
520
        for lineitem in lineitems:
521
            order_info.lineitems.append(to_t_lineitem(lineitem))
522
            order_info.id = lineitem.transaction.id
523
 
524
        return order_info
525
 
526
    def getShippingInfo(self, transactionId):
527
        """
528
        Parameters:
529
         - transactionId
530
        """
531
        shipments = get_shipments(transactionId)
532
        shipment_info = ShipmentInfo()
533
        shipment_info.shipments = []
534
        for shipment in shipments:
535
            shipment_info.shipments.append(to_t_shipment(shipment))
536
            shipment_info.id = shipment.id
537
        return shipment_info
538
    def getBillingInfo(self, transactionId):
539
        """
540
        Parameters:
541
         - transactionId
542
        """
543
        billings = get_billings(transactionId)
544
        billing_info = BillingInfo()
545
        billing_info.billings = []
546
        if billings:
547
            for billing in billings:
548
                billing_info.billings.append(to_t_billing(billing))
549
                billing_info.id = billing.line_item.transaction.id
550
        return billing_info
551
 
552
    def addBilling(self, transactionId, billing):
553
        """
554
        Parameters:
555
         - tranactionId
556
         - billing
557
        """
558
        return set_billing(transactionId, billing)
559
 
560
    def setShippingTracker(self, transactionId, shippingId, trackingId, airwayBillNo, provider):
561
        """
562
        Parameters:
563
         - transactionId
564
         - shippingId
565
         - trackingId
566
         - airwayBillNo
567
         - provider
568
        """
569
        return set_shipping_tracker_info(transactionId, shippingId, trackingId, airwayBillNo, provider)
570
 
571
    def setShippingDate(self, transactionId, shippingId, timestamp):
572
        """
573
        Parameters:
574
         - transactionId
575
         - shippingId
576
         - timestamp
577
        """
578
        return set_shipping_date(transactionId, shippingId, to_py_date(timestamp))
579
 
580
    def setDeliveryDate(self, transactionId, shippingid, timestamp):
581
        """
582
        Parameters:
583
         - transactionId
584
         - shippingid
585
         - timestamp
586
        """
587
        return set_delivery_date(transactionId, shippingid, timestamp)
483 rajveer 588
 
304 ashish 589
    def getAlerts(self, transactionId, valid):
590
        """
591
        Parameters:
592
         - transactionId
593
         - valid
594
        """
595
        alerts = get_alerts(transactionId, valid)
596
 
597
        t_alerts = []
598
 
599
        for alert in alerts:
600
            t_alerts.append(to_t_alert(alert)) 
601
 
602
        return t_alerts
603
 
604
    def setAlert(self, transactionId, unset, type, comment):
605
        """
606
        Parameters:
607
         - transactionId
608
         - unset
609
         - type
610
         - comment
611
        """
612
        set_alert(transactionId, unset, type, comment)
613
 
614
    def getExtraInfo(self, transaction_id):
615
        """
616
        Parameters:
617
         - transaction_id
618
        """
619
        extra_info = get_extra_info(transaction_id)
620
 
621
        return to_t_extra_item_info(extra_info, transaction_id)
622
 
623
 
624
    def setExtraInfo(self, transaction_id, sku_id, model, colour, vendor):
625
        """
626
        Parameters:
627
         - transaction_id
628
         - sku_id
629
         - model
630
         - colour
631
         - vendor
632
        """
633
        set_extra_info(transaction_id, sku_id, model, vendor, colour)
634
 
483 rajveer 635
    '''
104 ashish 636