Subversion Repositories SmartDukaan

Rev

Rev 1225 | Rev 1262 | 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.DataService import Transaction, LineItem,\
1225 chandransh 7
    Alerts, Order, BatchNoGenerator
104 ashish 8
from shop2020.thriftpy.model.v1.order.ttypes import Transaction as T_Transaction,\
690 chandransh 9
    TransactionServiceException, TransactionStatus, OrderStatus
104 ashish 10
import datetime
122 ashish 11
from elixir import *
855 rajveer 12
from shop2020.utils.Utils import to_py_date, to_java_date
104 ashish 13
from sqlalchemy.sql.expression import and_
483 rajveer 14
from shop2020.model.v1 import order
743 rajveer 15
from shop2020.clients.LogisticsClient import LogisticsClient
871 chandransh 16
from shop2020.clients.InventoryClient import InventoryClient
104 ashish 17
 
18
def get_new_transaction():
19
    transaction = Transaction()
20
    transaction.createdOn = datetime.datetime.now()
738 chandransh 21
    transaction.status = TransactionStatus.INIT
104 ashish 22
    transaction.status_message = "New transaction"
483 rajveer 23
    return transaction
24
 
25
def create_order(t_order):
26
    order = Order()
27
    if t_order.warehouse_id:
28
        order.warehouse_id = t_order.warehouse_id
509 rajveer 29
    if t_order.logistics_provider_id:    
483 rajveer 30
        order.logistics_provider_id = t_order.logistics_provider_id
31
        order.airwaybill_no = t_order.airwaybill_no
32
        order.tracking_id = t_order.tracking_id
33
    if t_order.expected_delivery_time:    
34
        order.expected_delivery_time = to_py_date(t_order.expected_delivery_time)
35
    if t_order.customer_id:    
36
        order.customer_id = t_order.customer_id
37
        order.customer_name = t_order.customer_name
860 rajveer 38
        order.customer_city = t_order.customer_city
39
        order.customer_state = t_order.customer_state
483 rajveer 40
        order.customer_mobilenumber = t_order.customer_mobilenumber
41
        order.customer_pincode = t_order.customer_pincode
738 chandransh 42
        order.customer_address1 = t_order.customer_address1
43
        order.customer_address2 = t_order.customer_address2
483 rajveer 44
        order.customer_email = t_order.customer_email
45
    #new status and status description to be added    
716 rajveer 46
    order.status = t_order.status  
483 rajveer 47
    order.statusDescription = t_order.statusDescription
48
    order.total_amount = t_order.total_amount
49
    order.total_weight = t_order.total_weight
50
    if t_order.created_timestamp:
51
        order.created_timestamp = to_py_date(t_order.created_timestamp)
52
    else:
685 chandransh 53
        order.created_timestamp = datetime.datetime.now()
483 rajveer 54
    return order
55
 
56
def create_transaction(t_transaction):
57
    t_orders = t_transaction.orders
58
    if not t_orders:
59
        raise TransactionServiceException(101, "Orders missing from the transaction")
60
    transaction = get_new_transaction()
61
    transaction.customer_id = t_transaction.customer_id 
62
    transaction.shopping_cart_id = t_transaction.shoppingCartid
63
 
64
    for t_order in t_orders:
65
        order = create_order(t_order)
66
        order.transaction = transaction 
67
        for line_item in t_order.lineitems:
68
            litem = LineItem()
716 rajveer 69
            litem.item_id = line_item.item_id
963 chandransh 70
            litem.productGroup = line_item.productGroup
483 rajveer 71
            litem.brand = line_item.brand
72
            if line_item.model_number:
73
                litem.model_number = line_item.model_number
74
            if line_item.model_name:    
75
                litem.model_name = line_item.model_name
908 chandransh 76
            if line_item.color:
77
                litem.color = line_item.color
483 rajveer 78
            if line_item.extra_info:
79
                litem.extra_info = line_item.extra_info
80
            litem.quantity = line_item.quantity
81
            litem.unit_price = line_item.unit_price
82
            litem.unit_weight = line_item.unit_weight
83
            litem.total_price = line_item.total_price
84
            litem.total_weight = line_item.total_weight
85
            """
86
            if line_item.addedOn:
87
                litem.added_on = to_py_date(line_item.addedOn)
88
            else:
89
                litem.added_on = datetime.datetime.now()
90
            """
91
            litem.order = order
92
    session.commit()
93
    return transaction.id
94
 
95
def get_transaction(transaction_id):
96
    transaction = Transaction.get_by(id=transaction_id)
97
    if not transaction:
98
        raise TransactionServiceException(108, "no such transaction")
99
 
100
    return transaction
101
 
102
def get_transactions_for_customer(customer_id, from_date, to_date, status):
103
    if not customer_id:
104
        raise TransactionServiceException(101, "bad customer id")
105
    query = Transaction.query.filter(Transaction.customer_id == customer_id)
106
    if status:
107
        query = query.filter(Transaction.status == status)
108
    if from_date:
109
        query = query.filter(Transaction.createdOn >from_date)
110
    if to_date:
111
        query = query.filter(Transaction.createdOn < to_date)
112
    return query.all()
113
 
114
def get_transactions_for_shopping_cart_id(shopping_cart_id):
115
    transactions = Transaction.query.filter_by(shopping_cart_id=shopping_cart_id).all()
116
    if not transactions:
117
        raise TransactionServiceException(108, "transactions for this shopping cart")
118
    return transactions
119
 
120
def get_transaction_status(transaction_id):
121
    if not transaction_id:
122
        raise TransactionServiceException(101, "bad transaction id")
123
 
124
    transaction = get_transaction(transaction_id)
125
 
126
    return transaction.status
127
 
128
def change_transaction_status(transaction_id, new_status, description):
129
    transaction = get_transaction(transaction_id)
130
    transaction.status = new_status
131
    transaction.status_message = description
690 chandransh 132
    if new_status == TransactionStatus.FAILED:
133
        for order in transaction.orders:
134
            order.status = OrderStatus.PAYMENT_FAILED
135
    elif new_status == TransactionStatus.IN_PROCESS:
136
        for order in transaction.orders:
137
            order.status = OrderStatus.SUBMITTED_FOR_PROCESSING
1219 chandransh 138
            order.statusDescription = "Submitted to warehouse"
743 rajveer 139
            #After we got payment success, we will set logistics info also 
140
            logistics_client = LogisticsClient().get_client()
141
            #FIXME line item is only one now. If multiple will come, need to fix.
871 chandransh 142
            item_id = order.lineitems[0].item_id
143
            logistics_info = logistics_client.getLogisticsInfo(order.customer_pincode, item_id)
743 rajveer 144
 
145
            order.warehouse_id = logistics_info.warehouseId
146
            order.logistics_provider_id = logistics_info.providerId
147
            order.airwaybill_no = logistics_info.airway_billno
148
            order.tracking_id = order.airwaybill_no
855 rajveer 149
            order.expected_delivery_time = datetime.datetime.now() + datetime.timedelta(days=logistics_info.deliveryTime)
871 chandransh 150
 
151
            catalog_client = InventoryClient().get_client()
152
            catalog_client.reserveItemInWarehouse(item_id, logistics_info.warehouseId, order.lineitems[0].quantity)
483 rajveer 153
    session.commit()
154
    return True
155
 
156
 
157
def get_order(order_id):
158
    order = Order.get_by(id=order_id)
159
    if not order:
160
        raise TransactionServiceException(108, "no such order")
161
 
162
    return order
163
 
164
def get_orders_for_customer(customer_id, from_date, to_date, status):
165
    if not customer_id:
166
        raise TransactionServiceException(101, "bad customer id")
167
    query = Order.query.filter(Order.customer_id == customer_id)
168
    if status:
169
        query = query.filter(Order.status == status)
170
    if from_date:
171
        query = query.filter(Order.created_timestamp >from_date)
172
    if to_date:
173
        query = query.filter(Order.created_timestamp < to_date)
174
    return query.all()
175
 
176
def get_all_orders(status, from_date, to_date, warehouse_id):
177
    if not warehouse_id:
178
        raise TransactionServiceException(101, "bad warehouse id")
179
    query = Order.query.filter(Order.warehouse_id == warehouse_id)
180
    if status:
181
        query = query.filter(Order.status == status)
182
    if from_date:
183
        query = query.filter(Order.created_timestamp >from_date)
184
    if to_date:
185
        query = query.filter(Order.created_timestamp < to_date)
186
    return query.all()
187
 
995 varun.gupt 188
def get_orders_by_billing_date(status, start_billing_date, end_billing_date, warehouse_id):
189
    if not warehouse_id:
190
        raise TransactionServiceException(101, "bad warehouse id")
191
 
192
    query = Order.query.filter(Order.warehouse_id == warehouse_id)
193
 
194
    if status:
195
        query = query.filter(Order.status == status)
196
 
197
    if start_billing_date:
198
        query = query.filter(Order.billing_timestamp >= start_billing_date)
199
 
200
    if end_billing_date:
201
        query = query.filter(Order.billing_timestamp <= end_billing_date)
202
 
203
    return query.all()
204
 
1208 chandransh 205
def get_orders_for_transaction(transaction_id):
206
    orders = Order.query.filter_by(transaction_id=transaction_id).all()
207
    if not orders:
208
        raise TransactionServiceException(101, "No order for the transaction")
209
    return orders
210
 
211
def get_line_items_for_order(order_id):
212
    query = LineItem.query.filter(LineItem.order_id == order_id)
213
    return query.all()
214
 
483 rajveer 215
def change_order_status(self, orderId, status, description):
216
    order = get_order(orderId)
217
    order.status = status
218
    order.statusDescription = description
219
    session.commit()
220
    return True
221
 
921 rajveer 222
def accept_order(self, orderId):
223
    order = get_order(orderId)
224
    order.status = OrderStatus.ACCEPTED
225
    order.statusDescription = "Order Accepted"
226
    order.accepted_timestamp = datetime.datetime.now()
227
    session.commit()
228
    return True
229
 
230
def bill_order(self, orderId):
231
    order = get_order(orderId)
232
    order.status = OrderStatus.BILLED
233
    order.statusDescription = "Order Billed"
234
    order.billing_timestamp = datetime.datetime.now()
235
    session.commit()
1208 chandransh 236
    return True
921 rajveer 237
 
1149 chandransh 238
def add_billing_details(orderId, invoice_number, billed_by):
494 rajveer 239
    order = Order.get_by(id=orderId)
240
    if not order:
241
        raise TransactionServiceException(101, "No order found for the givem orderid")
242
    order.invoice_number = invoice_number
243
    order.billed_by = billed_by
244
    session.commit()
245
    return True
759 chandransh 246
 
1149 chandransh 247
def add_jacket_number(orderId, jacket_number):
248
    order = Order.get_by(id=orderId)
249
    if not order:
250
        raise TransactionServiceException(101, "No order found for the givem orderid")
251
    order.jacket_number = jacket_number 
252
    session.commit()
253
    return True
254
 
1220 chandransh 255
def batch_orders(warehouseId):
256
    if not warehouseId:
257
        raise TransactionServiceException(101, "bad warehouse id")
1225 chandransh 258
 
259
    batchno_generator = BatchNoGenerator()
260
    session.commit()
1220 chandransh 261
    query = Order.query.filter_by(warehouse_id=warehouseId)
262
    query = query.filter_by(status=OrderStatus.SUBMITTED_FOR_PROCESSING)
263
    query = query.order_by(Order.created_timestamp)
264
    pending_orders = query.all()
1225 chandransh 265
 
1220 chandransh 266
    serial_no = 1
267
    for order in pending_orders:
1225 chandransh 268
        order.batchNo = batchno_generator.id #FIXME: Batch numbers should be read from a batch number generator
1220 chandransh 269
        order.serialNo = serial_no
270
        serial_no += 1
271
    session.commit() 
272
    pending_orders = query.all()
273
    return pending_orders
274
 
275
def order_outofstock(orderId):
276
    order = get_order(orderId)
277
    order.status = OrderStatus.INVENTORY_LOW
278
    order.statusDescription = "Low Inventory"
279
    order.outofstock_timestamp = datetime.datetime.now()
280
    session.commit()
281
    return True
282
 
759 chandransh 283
def mark_orders_as_manifested(warehouse_id, provider_id):
284
    try:
986 chandransh 285
        current_timestamp = datetime.datetime.now()
852 chandransh 286
        orders = Order.query.filter_by(warehouse_id = warehouse_id, logistics_provider_id = provider_id, status = OrderStatus.BILLED).all()
759 chandransh 287
        for order in orders:
288
            order.status = OrderStatus.SHIPPED_FROM_WH
987 chandransh 289
            order.statusDescription = "Order shipped from warehouse"
986 chandransh 290
            order.shipping_timestamp = current_timestamp
759 chandransh 291
        session.commit()
292
        return True
293
    except:
294
        return False
1113 chandransh 295
 
296
def mark_orders_as_picked_up(provider_id, pickup_details):
297
    for awb, pickup_timestamp in pickup_details.iteritems():
1135 chandransh 298
        order = Order.query.filter_by(airwaybill_no=awb, logistics_provider_id = provider_id).first()
1113 chandransh 299
        if order == None:
300
            raise TransactionServiceException(102, "No order found for the awb: " + awb)
301
        order.status = OrderStatus.SHIPPED_TO_LOGST
302
        order.statusDescription = "Order picked up by Courier Company"
1246 chandransh 303
        order.pickup_timestamp = pickup_timestamp
1113 chandransh 304
    session.commit()
1217 chandransh 305
 
1132 chandransh 306
    current_time = datetime.datetime.now()
307
    to_datetime = datetime.datetime(current_time.year, current_time.month, current_time.day)
1246 chandransh 308
    from_datetime = to_datetime - datetime.timedelta(2)
1132 chandransh 309
    orders_not_picked_up = Order.query.filter_by(logistics_provider_id = provider_id).filter_by(status=OrderStatus.SHIPPED_FROM_WH).filter(Order.shipping_timestamp >= from_datetime).filter(Order.shipping_timestamp <= to_datetime).all()
1113 chandransh 310
    return orders_not_picked_up
311
 
1132 chandransh 312
def mark_orders_as_delivered(provider_id, delivered_orders):
313
    for awb, detail in delivered_orders.iteritems():
1135 chandransh 314
        order = Order.query.filter_by(airwaybill_no=awb, logistics_provider_id = provider_id).first()
1132 chandransh 315
        if order == None:
316
            raise TransactionServiceException(103, "No order found for the awb:" + awb)
317
        order.status = OrderStatus.DELIVERY_SUCCESS
318
        order.statusDescription = "Order delivered"
319
        order.delivery_timestamp, order.receiver = detail.split('|')
320
    session.commit()
321
 
1135 chandransh 322
def mark_orders_as_failed(provider_id, returned_orders):
323
    for awb, detail in returned_orders.iteritems():
324
        order = Order.query.filter_by(airwaybill_no=awb, logistics_provider_id = provider_id).first()
325
        if order == None:
326
            raise TransactionServiceException(103, "No order found for the awb:" + awb)
327
        order.status = OrderStatus.FAILED
328
        order.delivery_timestamp, reason = detail.split('|')
329
        order.statusDescription = "Order Returned to Origin:" + reason
330
    session.commit()    
331
 
1246 chandransh 332
def update_non_delivery_reason(provider_id, undelivered_orders):
333
    for awb, reason in undelivered_orders.iteritems():
334
        order = Order.query.filter_by(airwaybill_no=awb, logistics_provider_id = provider_id).first()
335
        if order == None:
336
            raise TransactionServiceException(103, "No order found for the awb:" + awb)
337
        order.statusDescription = reason
338
    session.commit()
339
 
483 rajveer 340
def get_alerts(order_id, all):
341
    query = Alerts.query.filter_by(order_id=order_id)
342
    if not all:
343
        query = query.filter_by(valid=True)
344
    return query.all()
345
 
346
def set_alert(order_id, unset, type, comment):
347
    #get existing alert
348
    if unset:
349
        query = Alerts.query.filter_by(order_id=order_id)
350
        query = query.filter_by(type=type)
351
        alerts = query.all()
352
        for alert in alerts:
353
            if alert.valid:
354
                alert.valid = False
355
                alert.comment = comment
356
                alert.time_unset = datetime.datetime.now()
357
    else:
358
        alert = Alerts()
359
        alert.order_id = order_id
360
        alert.valid = True
361
        alert.type = type
362
        alert.comment = comment
363
        alert.time_set = datetime.datetime.now()
766 rajveer 364
    session.commit()    
365
 
366
def close_session():
367
    if session.is_active:
368
        print "session is active. closing it."
369
        session.close()