Subversion Repositories SmartDukaan

Rev

Rev 636 | Rev 669 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
557 chandransh 1
'''
2
Created on 10-May-2010
3
 
4
@author: ashish
5
'''
6
from elixir import *
576 chandransh 7
from shop2020.model.v1.user.impl.Dataservice import Cart, Line, Address, User
557 chandransh 8
from shop2020.thriftpy.model.v1.user.ttypes import CartStatus, LineStatus, ShoppingCartException
9
import datetime
576 chandransh 10
from shop2020.utils.Utils import to_py_date, to_java_date
557 chandransh 11
 
576 chandransh 12
from shop2020.thriftpy.model.v1.order.ttypes import Transaction as TTransaction,\
13
    TransactionStatus as TTransactionStatus, Order as TOrder, LineItem as TLineItem,\
14
    PaymentInfo as TPaymentInfo, OrderStatus 
557 chandransh 15
 
576 chandransh 16
from shop2020.thriftpy.model.v1.catalog.ttypes import Item as TItem
17
from shop2020.thriftpy.logistics.ttypes import LogisticsInfo as TLogisticsInfo
18
 
19
from shop2020.clients.TransactionClient import TransactionClient
20
from shop2020.clients.InventoryClient import InventoryClient
21
from shop2020.clients.LogisticsClient import LogisticsClient
22
from shop2020.model.v1 import user
23
 
557 chandransh 24
def get_cart(user_id):
25
    query = Cart.query.filter_by(user_id=user_id)      
26
    query = query.filter_by(cart_status = CartStatus.ACTIVE)
27
    try:
28
        cart = query.one()
29
        cart.accessed_on = datetime.datetime.now()
30
        session.commit()
31
    except:
32
        return None
33
#        raise ShoppingCartException(101, "Cart does not exist")
34
    return cart
35
 
36
def get_cart_by_id(id):
37
    cart = Cart.get_by(id=id)
38
    return cart
39
 
40
def create_cart(user_id):
41
    cart = get_cart(user_id)
42
    if not cart: 
43
        cart = Cart()
44
        cart.user_id = user_id
45
        cart.created_on = datetime.datetime.now()
46
        cart.updated_on = datetime.datetime.now()
47
        cart.cart_status = CartStatus.ACTIVE
48
        session.commit()
49
 
50
    return cart
51
 
52
def get_cart_by_user_id_and_status(user_id, status):
53
 
54
    query = Cart.query.filter_by(user_id=user_id)
55
    if status:
56
        query = query.filter_by(cart_status=status)
57
    carts = query.all()
58
    return carts
59
 
60
def get_carts_by_status(status):
61
    return Cart.query.filter_by(cart_status=status).all()
62
 
63
 
64
def get_carts_between(start_time, end_time, status):
65
    init_time = to_py_date(start_time)
66
    finish_time = to_py_date(end_time)
67
 
68
    query = Cart.query
69
    if status:
70
        query = query.filter(Cart.cart_status==status)
71
    if init_time:
72
        query = query.filter(Cart.created_on >= init_time)
73
    if finish_time:
74
        query = query.filter(Cart.created_on <= finish_time)
75
 
76
    carts = query.all()
77
    return carts
78
 
643 chandransh 79
def get_line(item_id, cart_id, status, single):
557 chandransh 80
    #get cart first 
81
    try:
82
        found_cart = Cart.get_by(id=cart_id)
83
    except:
84
        raise ShoppingCartException(101, "cart not found ")
643 chandransh 85
    query = Line.query.filter_by(cart = found_cart, item_id = item_id)
557 chandransh 86
 
87
    if status:
88
        query = query.filter_by(line_status = status)
89
    else:
90
        query = query.filter_by(line_status = LineStatus.LINE_ACTIVE)
91
    try:
92
        if single:
93
            return query.one()
94
        else:
95
            return query.all()
96
    except:
97
        return None
98
 
99
def change_cart_status(cart_id, status):
100
    cart = get_cart_by_id(id)
101
    if not cart:
102
        raise ShoppingCartException(101, "no cart attached to this id")
103
    if not status:
104
        raise ShoppingCartException(101, "invalid status")
105
 
106
    cart.cart_status = status
107
 
108
    session.commit()
109
 
110
def add_item_to_cart(cart_id, item_id, quantity):
111
    if not item_id:
112
        raise ShoppingCartException(101, "item_id cannot be null")
113
 
114
    if not quantity:
115
        raise ShoppingCartException(101, "quantity cannot be null")    
643 chandransh 116
 
117
    cart = Cart.get_by(id = cart_id)
557 chandransh 118
    if not cart:
119
        raise ShoppingCartException(101, "no cart attached to this id")
120
 
643 chandransh 121
    current_time = datetime.datetime.now()
122
    cart.updated_on = current_time
123
    line = get_line(item_id, cart_id, None,True)
124
    if line:
125
        #change the quantity only
126
        line.quantity = quantity
127
    else:
128
        line = Line()
129
        line.cart = cart
130
        line.item_id = item_id
131
        line.quantity = quantity
132
        line.created_on = current_time
133
        line.updated_on = current_time
134
        line.line_status = LineStatus.LINE_ACTIVE
557 chandransh 135
    session.commit()
136
 
137
def delete_item_from_cart(cart_id, item_id):
138
    if not item_id:
139
        raise ShoppingCartException(101, "item_id cannot be null")
643 chandransh 140
    item = get_line(item_id, cart_id, None, True)
557 chandransh 141
    item.delete()
142
    session.commit()
143
 
144
def change_item_status(cart_id, item_id, status):
145
    if not status:
146
        raise ShoppingCartException(101, "Status cannot be made null")
643 chandransh 147
    item = get_line(item_id, cart_id, None, True)
557 chandransh 148
    if item:
149
        item.line_status = status
150
        session.commit()
151
    else:
152
        raise ShoppingCartException(101, "Unable to probe the item you desired")
153
 
154
def add_address_to_cart(cart_id, address_id):
155
    if not cart_id:
156
        raise ShoppingCartException(101, "cart id cannot be made null")
157
 
158
    if not address_id:
159
        raise ShoppingCartException(101, "address id cannot be made null")
160
 
161
    cart = get_cart_by_id(cart_id)
162
    if not cart:
163
        raise ShoppingCartException(101, "no cart for this id")
576 chandransh 164
 
165
    address = Address.get_by(id=address_id)
166
    if not address:
167
        raise ShoppingCartException(101, "No address for this id")
168
 
557 chandransh 169
    cart.address_id = address_id
170
    session.commit()
171
 
576 chandransh 172
def commit_cart(cart_id):   
557 chandransh 173
    cart = get_cart_by_id(cart_id)
174
    cart.cart_status = CartStatus.COMMITTED
175
    cart.committed_on = datetime.datetime.now()
576 chandransh 176
 
557 chandransh 177
    #now we have a cart. Need to create a transaction with it
576 chandransh 178
    txn = TTransaction()
179
    txn.paymentInfo = TPaymentInfo()
180
    txn.shoppingCartid = cart_id
181
    txn.customer_id = cart.user_id
182
    txn.createdOn = to_java_date(datetime.datetime.now())
183
    txn.transactionStatus = TTransactionStatus.INIT
184
    txn.statusDescription = "New Order"
557 chandransh 185
 
576 chandransh 186
    txn.orders = create_orders(cart)
187
 
188
    transaction_client = TransactionClient().get_client()
189
    txn_id = transaction_client.createTransaction(txn)
190
    session.commit()
191
 
192
    new_cart = create_cart(cart.user_id)
193
    user = User.get_by(id=cart.user_id)
194
    user.active_cart_id = new_cart.id
195
    session.commit()
196
    return txn_id
197
 
198
def create_orders(cart):
557 chandransh 199
    cart_lines = cart.lines
576 chandransh 200
    orders = []
201
 
557 chandransh 202
    for line in cart_lines:
576 chandransh 203
        if line.line_status == LineStatus.LINE_ACTIVE:
204
            i = 0
205
            while i< line.quantity:
206
                t_line_item = create_line_item(line.item_id)
207
                t_order = create_order(cart.user_id, cart.address_id, t_line_item)
208
                orders.append(t_order)
209
                i += 1
210
    return orders
211
 
212
def create_order(user_id, address_id, t_line_item):
213
    user = User.get_by(id=user_id)
214
    address = Address.get_by(id=address_id)
215
    t_order = TOrder()
557 chandransh 216
 
576 chandransh 217
    t_order.customer_id = user.id
218
    t_order.customer_name = user.name
219
    t_order.customer_email = user.email
220
 
221
    t_order.customer_pincode = address.pin
222
    t_order.customer_address = get_address_string(address)
223
    t_order.customer_mobilenumber = address.phone
224
 
225
    t_order.total_amount = t_line_item.total_price
226
    t_order.total_weight = t_line_item.total_weight
227
    t_order.lineitems = [t_line_item]
228
 
229
    t_order.status = OrderStatus.SUBMITTED_FOR_PROCESSING
230
    t_order.statusDescription = "Submitted to warehouse"
231
    t_order.created_timestamp = to_java_date(datetime.datetime.now())
232
 
233
    logistics_client = LogisticsClient().get_client()
234
    logistics_info = logistics_client.getLogisticsInfo(t_order.customer_pincode, t_line_item.sku_id)
235
 
236
    t_order.logistics_provider_id = logistics_info.provider_id
237
    t_order.airwaybill_no = logistics_info.airway_billno
238
    t_order.tracking_id = t_order.airwaybill_no
239
    t_order.expected_delivery_time = to_java_date(datetime.datetime.now()) + 60*60*1000*logistics_info.delivery_estimate
240
    t_order.warehouse_id = logistics_info.warehouse_id
241
 
242
    return t_order
243
 
636 rajveer 244
def create_line_item(item_id):
576 chandransh 245
    inventory_client = InventoryClient().get_client()
636 rajveer 246
    item = inventory_client.getItem(item_id)
576 chandransh 247
    t_line_item = TLineItem()
636 rajveer 248
    t_line_item.model_name = item.modelName
249
    t_line_item.model_number = item.modelNumber
250
    t_line_item.extra_info = item.featureDescription
251
    t_line_item.brand = item.manufacturerName
252
    t_line_item.sku_id = item.vendorItemId
576 chandransh 253
    t_line_item.quantity = 1
636 rajveer 254
    t_line_item.unit_price = item.sellingPrice
255
    t_line_item.unit_weight = item.weight
256
    t_line_item.total_price = item.sellingPrice
257
    t_line_item.total_weight = item.weight
576 chandransh 258
    return t_line_item
259
 
260
def get_address_string(address):
261
    return address.line_1 + "\n" + address.line_2 + "\nLandmark: " + address.landmark + "\n" + address.city + "\n" + address.state + "\n" + address.country
262
 
263
def validate_cart(cartId):
264
    inventory_client = InventoryClient().get_client()
265
    logistics_client = LogisticsClient().get_client()
563 chandransh 266
    retval = True
557 chandransh 267
    # No need to validate duplicate items since there are only two ways
268
    # to add items to a cart and both of them check whether the item being
269
    # added is a duplicate of an already existing item.
563 chandransh 270
    cart = Cart.get_by(id=cartId)
271
    cart_lines = cart.lines
576 chandransh 272
    customer_pincode = ""
273
    if cart.address_id:
274
        address = Address.get_by(id=cart.address_id)
275
        customer_pincode = address.pin
563 chandransh 276
    for line in cart_lines:
612 chandransh 277
        old_estimate = line.estimate
636 rajveer 278
        item_id = line.item_id
279
        if inventory_client.isActive(item_id):
576 chandransh 280
            if customer_pincode:
281
                #FIXME: Provider ID should be returned by the same call and shouldn't be hard-coded as 1
612 chandransh 282
                try:
643 chandransh 283
                    item_delivery_estimate = logistics_client.getLogisticsEstimation(item_id, customer_pincode).deliveryTime
612 chandransh 284
                except:
643 chandransh 285
                    item_delivery_estimate = 3
612 chandransh 286
                if old_estimate != item_delivery_estimate:
287
                    line.estimate = item_delivery_estimate
288
                    retval = False
576 chandransh 289
        else:
563 chandransh 290
            line.delete()
291
            retval = False
612 chandransh 292
 
293
    session.commit()
563 chandransh 294
    return retval
576 chandransh 295
 
557 chandransh 296
def merge_cart(fromCartId, toCartId):
297
    fromCart = Cart.get_by(id=fromCartId)
298
    toCart = Cart.get_by(id=toCartId)
299
 
300
    old_lines = fromCart.lines
301
    new_lines = toCart.lines
302
 
303
    for line in old_lines:
304
        flag = True
305
        for new_line in new_lines:
306
            if line.item_id == new_line.item_id:
307
                flag = False
576 chandransh 308
        if flag:
557 chandransh 309
            toCart.lines.append(line)
310
 
311
    toCart.updatedOn = datetime.datetime.now() 
312
 
313
    fromCart.expired_on = datetime.datetime.now()
314
    fromCart.cart_status = CartStatus.INACTIVE
643 chandransh 315
    session.commit()