Subversion Repositories SmartDukaan

Rev

Rev 1208 | Rev 1246 | 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,\
1220 chandransh 15
    order_outofstock, batch_orders
104 ashish 16
from shop2020.model.v1.order.impl.Convertors import to_t_transaction,\
483 rajveer 17
    to_t_lineitem, to_t_alert, to_t_order, to_t_lineitem
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
 
24
    def __init__(self):
483 rajveer 25
        """
104 ashish 26
        Constructor
483 rajveer 27
        """
104 ashish 28
        DataService.initialize()
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()
766 rajveer 334
 
483 rajveer 335
    def getAlerts(self, orderId, valid):
336
        """
337
        Parameters:
338
         - orderId
339
         - valid
340
        """
766 rajveer 341
        try:
342
            alerts = get_alerts(orderId, valid)
343
 
344
            t_alerts = []
345
 
346
            for alert in alerts:
347
                t_alerts.append(to_t_alert(alert)) 
348
 
349
            return t_alerts
350
        finally:
351
            close_session()
352
 
483 rajveer 353
    def setAlert(self, orderId, unset, type, comment):
354
        """
355
        Parameters:
356
         - orderId
357
         - unset
358
         - type
359
         - comment
360
        """
766 rajveer 361
        try:
362
            set_alert(orderId, unset, type, comment)
363
        finally:
364
            close_session()
365
 
366
    def closeSession(self, ):
367
        close_session()
104 ashish 368
 
766 rajveer 369
 
370
 
371
    '''
483 rajveer 372
if __name__ == "__main__":
373
    order = OrderServiceHandler()
374
    t = order.getTransaction(4)
375
    print t
376
 
766 rajveer 377
 
483 rajveer 378
    these methods commented right now. may be used later
379
    def getAllTransactions(self, status, from_date, to_date, warehouse_id):
104 ashish 380
        """
381
        Parameters:
483 rajveer 382
         - status
104 ashish 383
         - from_date
384
         - to_date
385
        """
386
        current_status, current_from_date, current_to_date = get_status_fdate_tdate(status, from_date, to_date)
483 rajveer 387
 
388
        transactions = get_all_transactions(current_status, current_from_date, current_to_date, warehouse_id)
104 ashish 389
        t_transaction = []
390
        for transaction in transactions:
391
            t_transaction.append(to_t_transaction(transaction))
392
        return t_transaction
483 rajveer 393
 
394
    def getTransactionsForShipmentStatus(self, status, from_date, to_date):
132 ashish 395
        """
396
        Parameters:
483 rajveer 397
         - status
398
         - from_date
399
         - to_date
132 ashish 400
        """
483 rajveer 401
        current_status, current_from_date, current_to_date = get_status_fdate_tdate(status, from_date, to_date)
402
 
403
        transactions = get_transactions_for_shipment_status(current_status, current_from_date, current_to_date)
132 ashish 404
        t_transaction = []
405
        for transaction in transactions:
406
            t_transaction.append(to_t_transaction(transaction))
407
        return t_transaction
483 rajveer 408
 
409
    def getTransactionsForCustomerAndShipmentStatus(self, customerId, from_date, to_date, status):
104 ashish 410
        """
411
        Parameters:
483 rajveer 412
         - customerId
413
         - from_date
414
         - to_date
104 ashish 415
         - status
416
        """
483 rajveer 417
        current_status, current_from_date, current_to_date = get_status_fdate_tdate(status, from_date, to_date)
418
 
419
        transactions = get_transactions_for_customer_shipment(customerId, current_from_date, current_to_date, current_status)
420
        t_transaction = []
421
        for transaction in transactions:
422
            t_transaction.append(to_t_transaction(transaction))
423
        return t_transaction
424
 
104 ashish 425
    def getOrderInfo(self, transactionId):
426
        """
427
            Get and set individual objects in it
428
        *
429
 
430
        Parameters:
431
         - transactionId
432
        """
433
        lineitems = get_line_items(transactionId)
434
        order_info = OrderInfo()
435
        order_info.lineitems = []
436
        for lineitem in lineitems:
437
            order_info.lineitems.append(to_t_lineitem(lineitem))
438
            order_info.id = lineitem.transaction.id
439
 
440
        return order_info
441
 
442
    def getShippingInfo(self, transactionId):
443
        """
444
        Parameters:
445
         - transactionId
446
        """
447
        shipments = get_shipments(transactionId)
448
        shipment_info = ShipmentInfo()
449
        shipment_info.shipments = []
450
        for shipment in shipments:
451
            shipment_info.shipments.append(to_t_shipment(shipment))
452
            shipment_info.id = shipment.id
453
        return shipment_info
454
    def getBillingInfo(self, transactionId):
455
        """
456
        Parameters:
457
         - transactionId
458
        """
459
        billings = get_billings(transactionId)
460
        billing_info = BillingInfo()
461
        billing_info.billings = []
462
        if billings:
463
            for billing in billings:
464
                billing_info.billings.append(to_t_billing(billing))
465
                billing_info.id = billing.line_item.transaction.id
466
        return billing_info
467
 
468
    def addBilling(self, transactionId, billing):
469
        """
470
        Parameters:
471
         - tranactionId
472
         - billing
473
        """
474
        return set_billing(transactionId, billing)
475
 
476
    def setShippingTracker(self, transactionId, shippingId, trackingId, airwayBillNo, provider):
477
        """
478
        Parameters:
479
         - transactionId
480
         - shippingId
481
         - trackingId
482
         - airwayBillNo
483
         - provider
484
        """
485
        return set_shipping_tracker_info(transactionId, shippingId, trackingId, airwayBillNo, provider)
486
 
487
    def setShippingDate(self, transactionId, shippingId, timestamp):
488
        """
489
        Parameters:
490
         - transactionId
491
         - shippingId
492
         - timestamp
493
        """
494
        return set_shipping_date(transactionId, shippingId, to_py_date(timestamp))
495
 
496
    def setDeliveryDate(self, transactionId, shippingid, timestamp):
497
        """
498
        Parameters:
499
         - transactionId
500
         - shippingid
501
         - timestamp
502
        """
503
        return set_delivery_date(transactionId, shippingid, timestamp)
483 rajveer 504
 
304 ashish 505
    def getAlerts(self, transactionId, valid):
506
        """
507
        Parameters:
508
         - transactionId
509
         - valid
510
        """
511
        alerts = get_alerts(transactionId, valid)
512
 
513
        t_alerts = []
514
 
515
        for alert in alerts:
516
            t_alerts.append(to_t_alert(alert)) 
517
 
518
        return t_alerts
519
 
520
    def setAlert(self, transactionId, unset, type, comment):
521
        """
522
        Parameters:
523
         - transactionId
524
         - unset
525
         - type
526
         - comment
527
        """
528
        set_alert(transactionId, unset, type, comment)
529
 
530
    def getExtraInfo(self, transaction_id):
531
        """
532
        Parameters:
533
         - transaction_id
534
        """
535
        extra_info = get_extra_info(transaction_id)
536
 
537
        return to_t_extra_item_info(extra_info, transaction_id)
538
 
539
 
540
    def setExtraInfo(self, transaction_id, sku_id, model, colour, vendor):
541
        """
542
        Parameters:
543
         - transaction_id
544
         - sku_id
545
         - model
546
         - colour
547
         - vendor
548
        """
549
        set_extra_info(transaction_id, sku_id, model, vendor, colour)
550
 
483 rajveer 551
    '''
104 ashish 552