Subversion Repositories SmartDukaan

Rev

Rev 1135 | Rev 1208 | 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
 
483 rajveer 204
def change_order_status(self, orderId, status, description):
205
    order = get_order(orderId)
206
    order.status = status
207
    order.statusDescription = description
208
    session.commit()
209
    return True
210
 
921 rajveer 211
def accept_order(self, orderId):
212
    order = get_order(orderId)
213
    order.status = OrderStatus.ACCEPTED
214
    order.statusDescription = "Order Accepted"
215
    order.accepted_timestamp = datetime.datetime.now()
216
    session.commit()
217
    return True
218
 
219
def bill_order(self, orderId):
220
    order = get_order(orderId)
221
    order.status = OrderStatus.BILLED
222
    order.statusDescription = "Order Billed"
223
    order.billing_timestamp = datetime.datetime.now()
224
    session.commit()
225
    return True    
226
 
483 rajveer 227
def get_orders_for_transaction(transaction_id):
228
    orders = Order.query.filter_by(transaction_id=transaction_id).all()
229
    if not orders:
230
        raise TransactionServiceException(101, "No order for the transaction")
231
    return orders
232
 
233
def get_line_items_for_order(order_id):
234
    query = LineItem.query.filter(LineItem.order_id == order_id)
235
    return query.all()
236
 
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
 
759 chandransh 255
def mark_orders_as_manifested(warehouse_id, provider_id):
256
    try:
986 chandransh 257
        current_timestamp = datetime.datetime.now()
852 chandransh 258
        orders = Order.query.filter_by(warehouse_id = warehouse_id, logistics_provider_id = provider_id, status = OrderStatus.BILLED).all()
759 chandransh 259
        for order in orders:
260
            order.status = OrderStatus.SHIPPED_FROM_WH
987 chandransh 261
            order.statusDescription = "Order shipped from warehouse"
986 chandransh 262
            order.shipping_timestamp = current_timestamp
759 chandransh 263
        session.commit()
264
        return True
265
    except:
266
        return False
1113 chandransh 267
 
268
def mark_orders_as_picked_up(provider_id, pickup_details):
269
    for awb, pickup_timestamp in pickup_details.iteritems():
1135 chandransh 270
        order = Order.query.filter_by(airwaybill_no=awb, logistics_provider_id = provider_id).first()
1113 chandransh 271
        if order == None:
272
            raise TransactionServiceException(102, "No order found for the awb: " + awb)
273
        order.status = OrderStatus.SHIPPED_TO_LOGST
274
        order.statusDescription = "Order picked up by Courier Company"
275
        order.pickup_timestamp = to_py_date(pickup_timestamp)
276
    session.commit()
277
    #FIXME: Add a check for shipping timestamp to be of yesterday
1132 chandransh 278
    current_time = datetime.datetime.now()
279
    to_datetime = datetime.datetime(current_time.year, current_time.month, current_time.day)
280
    from_datetime = to_datetime - datetime.timedelta(1)
281
    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 282
    return orders_not_picked_up
283
 
1132 chandransh 284
def mark_orders_as_delivered(provider_id, delivered_orders):
285
    for awb, detail in delivered_orders.iteritems():
1135 chandransh 286
        order = Order.query.filter_by(airwaybill_no=awb, logistics_provider_id = provider_id).first()
1132 chandransh 287
        if order == None:
288
            raise TransactionServiceException(103, "No order found for the awb:" + awb)
289
        order.status = OrderStatus.DELIVERY_SUCCESS
290
        order.statusDescription = "Order delivered"
291
        order.delivery_timestamp, order.receiver = detail.split('|')
292
    session.commit()
293
 
1135 chandransh 294
def mark_orders_as_failed(provider_id, returned_orders):
295
    for awb, detail in returned_orders.iteritems():
296
        order = Order.query.filter_by(airwaybill_no=awb, logistics_provider_id = provider_id).first()
297
        if order == None:
298
            raise TransactionServiceException(103, "No order found for the awb:" + awb)
299
        order.status = OrderStatus.FAILED
300
        order.delivery_timestamp, reason = detail.split('|')
301
        order.statusDescription = "Order Returned to Origin:" + reason
302
    session.commit()    
303
 
483 rajveer 304
def get_alerts(order_id, all):
305
    query = Alerts.query.filter_by(order_id=order_id)
306
    if not all:
307
        query = query.filter_by(valid=True)
308
    return query.all()
309
 
310
def set_alert(order_id, unset, type, comment):
311
    #get existing alert
312
    if unset:
313
        query = Alerts.query.filter_by(order_id=order_id)
314
        query = query.filter_by(type=type)
315
        alerts = query.all()
316
        for alert in alerts:
317
            if alert.valid:
318
                alert.valid = False
319
                alert.comment = comment
320
                alert.time_unset = datetime.datetime.now()
321
    else:
322
        alert = Alerts()
323
        alert.order_id = order_id
324
        alert.valid = True
325
        alert.type = type
326
        alert.comment = comment
327
        alert.time_set = datetime.datetime.now()
766 rajveer 328
    session.commit()    
329
 
330
def close_session():
331
    if session.is_active:
332
        print "session is active. closing it."
333
        session.close()