Subversion Repositories SmartDukaan

Rev

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