Subversion Repositories SmartDukaan

Rev

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