Subversion Repositories SmartDukaan

Rev

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