Subversion Repositories SmartDukaan

Rev

Rev 1217 | Rev 1220 | 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
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
 
1208 chandransh 238
def order_outofstock(orderId):
239
    order = get_order(orderId)
240
    order.status = OrderStatus.INVENTORY_LOW
241
    order.statusDescription = "Low Inventory"
242
    order.outofstock_timestamp = datetime.datetime.now()
243
    session.commit()
244
    return True
483 rajveer 245
 
1149 chandransh 246
def add_billing_details(orderId, invoice_number, billed_by):
494 rajveer 247
    order = Order.get_by(id=orderId)
248
    if not order:
249
        raise TransactionServiceException(101, "No order found for the givem orderid")
250
    order.invoice_number = invoice_number
251
    order.billed_by = billed_by
252
    session.commit()
253
    return True
759 chandransh 254
 
1149 chandransh 255
def add_jacket_number(orderId, jacket_number):
256
    order = Order.get_by(id=orderId)
257
    if not order:
258
        raise TransactionServiceException(101, "No order found for the givem orderid")
259
    order.jacket_number = jacket_number 
260
    session.commit()
261
    return True
262
 
759 chandransh 263
def mark_orders_as_manifested(warehouse_id, provider_id):
264
    try:
986 chandransh 265
        current_timestamp = datetime.datetime.now()
852 chandransh 266
        orders = Order.query.filter_by(warehouse_id = warehouse_id, logistics_provider_id = provider_id, status = OrderStatus.BILLED).all()
759 chandransh 267
        for order in orders:
268
            order.status = OrderStatus.SHIPPED_FROM_WH
987 chandransh 269
            order.statusDescription = "Order shipped from warehouse"
986 chandransh 270
            order.shipping_timestamp = current_timestamp
759 chandransh 271
        session.commit()
272
        return True
273
    except:
274
        return False
1113 chandransh 275
 
276
def mark_orders_as_picked_up(provider_id, pickup_details):
277
    for awb, pickup_timestamp in pickup_details.iteritems():
1135 chandransh 278
        order = Order.query.filter_by(airwaybill_no=awb, logistics_provider_id = provider_id).first()
1113 chandransh 279
        if order == None:
280
            raise TransactionServiceException(102, "No order found for the awb: " + awb)
281
        order.status = OrderStatus.SHIPPED_TO_LOGST
282
        order.statusDescription = "Order picked up by Courier Company"
283
        order.pickup_timestamp = to_py_date(pickup_timestamp)
284
    session.commit()
1217 chandransh 285
 
1132 chandransh 286
    current_time = datetime.datetime.now()
287
    to_datetime = datetime.datetime(current_time.year, current_time.month, current_time.day)
288
    from_datetime = to_datetime - datetime.timedelta(1)
289
    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 290
    return orders_not_picked_up
291
 
1132 chandransh 292
def mark_orders_as_delivered(provider_id, delivered_orders):
293
    for awb, detail in delivered_orders.iteritems():
1135 chandransh 294
        order = Order.query.filter_by(airwaybill_no=awb, logistics_provider_id = provider_id).first()
1132 chandransh 295
        if order == None:
296
            raise TransactionServiceException(103, "No order found for the awb:" + awb)
297
        order.status = OrderStatus.DELIVERY_SUCCESS
298
        order.statusDescription = "Order delivered"
299
        order.delivery_timestamp, order.receiver = detail.split('|')
300
    session.commit()
301
 
1135 chandransh 302
def mark_orders_as_failed(provider_id, returned_orders):
303
    for awb, detail in returned_orders.iteritems():
304
        order = Order.query.filter_by(airwaybill_no=awb, logistics_provider_id = provider_id).first()
305
        if order == None:
306
            raise TransactionServiceException(103, "No order found for the awb:" + awb)
307
        order.status = OrderStatus.FAILED
308
        order.delivery_timestamp, reason = detail.split('|')
309
        order.statusDescription = "Order Returned to Origin:" + reason
310
    session.commit()    
311
 
483 rajveer 312
def get_alerts(order_id, all):
313
    query = Alerts.query.filter_by(order_id=order_id)
314
    if not all:
315
        query = query.filter_by(valid=True)
316
    return query.all()
317
 
318
def set_alert(order_id, unset, type, comment):
319
    #get existing alert
320
    if unset:
321
        query = Alerts.query.filter_by(order_id=order_id)
322
        query = query.filter_by(type=type)
323
        alerts = query.all()
324
        for alert in alerts:
325
            if alert.valid:
326
                alert.valid = False
327
                alert.comment = comment
328
                alert.time_unset = datetime.datetime.now()
329
    else:
330
        alert = Alerts()
331
        alert.order_id = order_id
332
        alert.valid = True
333
        alert.type = type
334
        alert.comment = comment
335
        alert.time_set = datetime.datetime.now()
766 rajveer 336
    session.commit()    
337
 
338
def close_session():
339
    if session.is_active:
340
        print "session is active. closing it."
341
        session.close()