Subversion Repositories SmartDukaan

Rev

Rev 1149 | Rev 1217 | 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,\
685 chandransh 7
    Alerts, Order
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
743 rajveer 138
            #After we got payment success, we will set logistics info also 
139
            logistics_client = LogisticsClient().get_client()
140
            #FIXME line item is only one now. If multiple will come, need to fix.
871 chandransh 141
            item_id = order.lineitems[0].item_id
142
            logistics_info = logistics_client.getLogisticsInfo(order.customer_pincode, item_id)
743 rajveer 143
 
144
            order.warehouse_id = logistics_info.warehouseId
145
            order.logistics_provider_id = logistics_info.providerId
146
            order.airwaybill_no = logistics_info.airway_billno
147
            order.tracking_id = order.airwaybill_no
855 rajveer 148
            order.expected_delivery_time = datetime.datetime.now() + datetime.timedelta(days=logistics_info.deliveryTime)
871 chandransh 149
 
150
            catalog_client = InventoryClient().get_client()
151
            catalog_client.reserveItemInWarehouse(item_id, logistics_info.warehouseId, order.lineitems[0].quantity)
483 rajveer 152
    session.commit()
153
    return True
154
 
155
 
156
def get_order(order_id):
157
    order = Order.get_by(id=order_id)
158
    if not order:
159
        raise TransactionServiceException(108, "no such order")
160
 
161
    return order
162
 
163
def get_orders_for_customer(customer_id, from_date, to_date, status):
164
    if not customer_id:
165
        raise TransactionServiceException(101, "bad customer id")
166
    query = Order.query.filter(Order.customer_id == customer_id)
167
    if status:
168
        query = query.filter(Order.status == status)
169
    if from_date:
170
        query = query.filter(Order.created_timestamp >from_date)
171
    if to_date:
172
        query = query.filter(Order.created_timestamp < to_date)
173
    return query.all()
174
 
175
def get_all_orders(status, from_date, to_date, warehouse_id):
176
    if not warehouse_id:
177
        raise TransactionServiceException(101, "bad warehouse id")
178
    query = Order.query.filter(Order.warehouse_id == warehouse_id)
179
    if status:
180
        query = query.filter(Order.status == status)
181
    if from_date:
182
        query = query.filter(Order.created_timestamp >from_date)
183
    if to_date:
184
        query = query.filter(Order.created_timestamp < to_date)
185
    return query.all()
186
 
995 varun.gupt 187
def get_orders_by_billing_date(status, start_billing_date, end_billing_date, warehouse_id):
188
    if not warehouse_id:
189
        raise TransactionServiceException(101, "bad warehouse id")
190
 
191
    query = Order.query.filter(Order.warehouse_id == warehouse_id)
192
 
193
    if status:
194
        query = query.filter(Order.status == status)
195
 
196
    if start_billing_date:
197
        query = query.filter(Order.billing_timestamp >= start_billing_date)
198
 
199
    if end_billing_date:
200
        query = query.filter(Order.billing_timestamp <= end_billing_date)
201
 
202
    return query.all()
203
 
1208 chandransh 204
def get_orders_for_transaction(transaction_id):
205
    orders = Order.query.filter_by(transaction_id=transaction_id).all()
206
    if not orders:
207
        raise TransactionServiceException(101, "No order for the transaction")
208
    return orders
209
 
210
def get_line_items_for_order(order_id):
211
    query = LineItem.query.filter(LineItem.order_id == order_id)
212
    return query.all()
213
 
483 rajveer 214
def change_order_status(self, orderId, status, description):
215
    order = get_order(orderId)
216
    order.status = status
217
    order.statusDescription = description
218
    session.commit()
219
    return True
220
 
921 rajveer 221
def accept_order(self, orderId):
222
    order = get_order(orderId)
223
    order.status = OrderStatus.ACCEPTED
224
    order.statusDescription = "Order Accepted"
225
    order.accepted_timestamp = datetime.datetime.now()
226
    session.commit()
227
    return True
228
 
229
def bill_order(self, orderId):
230
    order = get_order(orderId)
231
    order.status = OrderStatus.BILLED
232
    order.statusDescription = "Order Billed"
233
    order.billing_timestamp = datetime.datetime.now()
234
    session.commit()
1208 chandransh 235
    return True
921 rajveer 236
 
1208 chandransh 237
def order_outofstock(orderId):
238
    order = get_order(orderId)
239
    order.status = OrderStatus.INVENTORY_LOW
240
    order.statusDescription = "Low Inventory"
241
    order.outofstock_timestamp = datetime.datetime.now()
242
    session.commit()
243
    return True
483 rajveer 244
 
1149 chandransh 245
def add_billing_details(orderId, invoice_number, billed_by):
494 rajveer 246
    order = Order.get_by(id=orderId)
247
    if not order:
248
        raise TransactionServiceException(101, "No order found for the givem orderid")
249
    order.invoice_number = invoice_number
250
    order.billed_by = billed_by
251
    session.commit()
252
    return True
759 chandransh 253
 
1149 chandransh 254
def add_jacket_number(orderId, jacket_number):
255
    order = Order.get_by(id=orderId)
256
    if not order:
257
        raise TransactionServiceException(101, "No order found for the givem orderid")
258
    order.jacket_number = jacket_number 
259
    session.commit()
260
    return True
261
 
759 chandransh 262
def mark_orders_as_manifested(warehouse_id, provider_id):
263
    try:
986 chandransh 264
        current_timestamp = datetime.datetime.now()
852 chandransh 265
        orders = Order.query.filter_by(warehouse_id = warehouse_id, logistics_provider_id = provider_id, status = OrderStatus.BILLED).all()
759 chandransh 266
        for order in orders:
267
            order.status = OrderStatus.SHIPPED_FROM_WH
987 chandransh 268
            order.statusDescription = "Order shipped from warehouse"
986 chandransh 269
            order.shipping_timestamp = current_timestamp
759 chandransh 270
        session.commit()
271
        return True
272
    except:
273
        return False
1113 chandransh 274
 
275
def mark_orders_as_picked_up(provider_id, pickup_details):
276
    for awb, pickup_timestamp in pickup_details.iteritems():
1135 chandransh 277
        order = Order.query.filter_by(airwaybill_no=awb, logistics_provider_id = provider_id).first()
1113 chandransh 278
        if order == None:
279
            raise TransactionServiceException(102, "No order found for the awb: " + awb)
280
        order.status = OrderStatus.SHIPPED_TO_LOGST
281
        order.statusDescription = "Order picked up by Courier Company"
282
        order.pickup_timestamp = to_py_date(pickup_timestamp)
283
    session.commit()
284
    #FIXME: Add a check for shipping timestamp to be of yesterday
1132 chandransh 285
    current_time = datetime.datetime.now()
286
    to_datetime = datetime.datetime(current_time.year, current_time.month, current_time.day)
287
    from_datetime = to_datetime - datetime.timedelta(1)
288
    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 289
    return orders_not_picked_up
290
 
1132 chandransh 291
def mark_orders_as_delivered(provider_id, delivered_orders):
292
    for awb, detail in delivered_orders.iteritems():
1135 chandransh 293
        order = Order.query.filter_by(airwaybill_no=awb, logistics_provider_id = provider_id).first()
1132 chandransh 294
        if order == None:
295
            raise TransactionServiceException(103, "No order found for the awb:" + awb)
296
        order.status = OrderStatus.DELIVERY_SUCCESS
297
        order.statusDescription = "Order delivered"
298
        order.delivery_timestamp, order.receiver = detail.split('|')
299
    session.commit()
300
 
1135 chandransh 301
def mark_orders_as_failed(provider_id, returned_orders):
302
    for awb, detail in returned_orders.iteritems():
303
        order = Order.query.filter_by(airwaybill_no=awb, logistics_provider_id = provider_id).first()
304
        if order == None:
305
            raise TransactionServiceException(103, "No order found for the awb:" + awb)
306
        order.status = OrderStatus.FAILED
307
        order.delivery_timestamp, reason = detail.split('|')
308
        order.statusDescription = "Order Returned to Origin:" + reason
309
    session.commit()    
310
 
483 rajveer 311
def get_alerts(order_id, all):
312
    query = Alerts.query.filter_by(order_id=order_id)
313
    if not all:
314
        query = query.filter_by(valid=True)
315
    return query.all()
316
 
317
def set_alert(order_id, unset, type, comment):
318
    #get existing alert
319
    if unset:
320
        query = Alerts.query.filter_by(order_id=order_id)
321
        query = query.filter_by(type=type)
322
        alerts = query.all()
323
        for alert in alerts:
324
            if alert.valid:
325
                alert.valid = False
326
                alert.comment = comment
327
                alert.time_unset = datetime.datetime.now()
328
    else:
329
        alert = Alerts()
330
        alert.order_id = order_id
331
        alert.valid = True
332
        alert.type = type
333
        alert.comment = comment
334
        alert.time_set = datetime.datetime.now()
766 rajveer 335
    session.commit()    
336
 
337
def close_session():
338
    if session.is_active:
339
        print "session is active. closing it."
340
        session.close()