Subversion Repositories SmartDukaan

Rev

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