Subversion Repositories SmartDukaan

Rev

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