Subversion Repositories SmartDukaan

Rev

Rev 1596 | Rev 1730 | 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
'''
1262 varun.gupt 6
from string import Template
7
from shop2020.thriftpy.utils.ttypes import HelperServiceException, Mail
8
 
104 ashish 9
from shop2020.model.v1.order.impl.DataService import Transaction, LineItem,\
1225 chandransh 10
    Alerts, Order, BatchNoGenerator
104 ashish 11
from shop2020.thriftpy.model.v1.order.ttypes import Transaction as T_Transaction,\
690 chandransh 12
    TransactionServiceException, TransactionStatus, OrderStatus
1565 varun.gupt 13
import datetime 
1479 ankur.sing 14
import time
122 ashish 15
from elixir import *
855 rajveer 16
from shop2020.utils.Utils import to_py_date, to_java_date
1382 varun.gupt 17
from sqlalchemy.sql.expression import and_, or_
1627 ankur.sing 18
from sqlalchemy.sql import func
483 rajveer 19
from shop2020.model.v1 import order
743 rajveer 20
from shop2020.clients.LogisticsClient import LogisticsClient
871 chandransh 21
from shop2020.clients.InventoryClient import InventoryClient
1262 varun.gupt 22
from shop2020.clients.UserClient import UserClient
1351 varun.gupt 23
from shop2020.clients.HelperClient import HelperClient
104 ashish 24
 
25
def get_new_transaction():
26
    transaction = Transaction()
27
    transaction.createdOn = datetime.datetime.now()
738 chandransh 28
    transaction.status = TransactionStatus.INIT
104 ashish 29
    transaction.status_message = "New transaction"
483 rajveer 30
    return transaction
31
 
32
def create_order(t_order):
33
    order = Order()
34
    if t_order.warehouse_id:
35
        order.warehouse_id = t_order.warehouse_id
509 rajveer 36
    if t_order.logistics_provider_id:    
483 rajveer 37
        order.logistics_provider_id = t_order.logistics_provider_id
38
        order.airwaybill_no = t_order.airwaybill_no
39
        order.tracking_id = t_order.tracking_id
40
    if t_order.expected_delivery_time:    
41
        order.expected_delivery_time = to_py_date(t_order.expected_delivery_time)
42
    if t_order.customer_id:    
43
        order.customer_id = t_order.customer_id
44
        order.customer_name = t_order.customer_name
860 rajveer 45
        order.customer_city = t_order.customer_city
46
        order.customer_state = t_order.customer_state
483 rajveer 47
        order.customer_mobilenumber = t_order.customer_mobilenumber
48
        order.customer_pincode = t_order.customer_pincode
738 chandransh 49
        order.customer_address1 = t_order.customer_address1
50
        order.customer_address2 = t_order.customer_address2
483 rajveer 51
        order.customer_email = t_order.customer_email
52
    #new status and status description to be added    
716 rajveer 53
    order.status = t_order.status  
483 rajveer 54
    order.statusDescription = t_order.statusDescription
55
    order.total_amount = t_order.total_amount
56
    order.total_weight = t_order.total_weight
57
    if t_order.created_timestamp:
58
        order.created_timestamp = to_py_date(t_order.created_timestamp)
59
    else:
685 chandransh 60
        order.created_timestamp = datetime.datetime.now()
483 rajveer 61
    return order
62
 
63
def create_transaction(t_transaction):
64
    t_orders = t_transaction.orders
65
    if not t_orders:
66
        raise TransactionServiceException(101, "Orders missing from the transaction")
67
    transaction = get_new_transaction()
68
    transaction.customer_id = t_transaction.customer_id 
69
    transaction.shopping_cart_id = t_transaction.shoppingCartid
70
 
71
    for t_order in t_orders:
72
        order = create_order(t_order)
73
        order.transaction = transaction 
74
        for line_item in t_order.lineitems:
75
            litem = LineItem()
716 rajveer 76
            litem.item_id = line_item.item_id
963 chandransh 77
            litem.productGroup = line_item.productGroup
483 rajveer 78
            litem.brand = line_item.brand
79
            if line_item.model_number:
80
                litem.model_number = line_item.model_number
81
            if line_item.model_name:    
82
                litem.model_name = line_item.model_name
908 chandransh 83
            if line_item.color:
84
                litem.color = line_item.color
483 rajveer 85
            if line_item.extra_info:
86
                litem.extra_info = line_item.extra_info
87
            litem.quantity = line_item.quantity
88
            litem.unit_price = line_item.unit_price
89
            litem.unit_weight = line_item.unit_weight
90
            litem.total_price = line_item.total_price
91
            litem.total_weight = line_item.total_weight
1262 varun.gupt 92
            litem.transfer_price = line_item.transfer_price
483 rajveer 93
            """
94
            if line_item.addedOn:
95
                litem.added_on = to_py_date(line_item.addedOn)
96
            else:
97
                litem.added_on = datetime.datetime.now()
98
            """
99
            litem.order = order
100
    session.commit()
101
    return transaction.id
102
 
103
def get_transaction(transaction_id):
104
    transaction = Transaction.get_by(id=transaction_id)
105
    if not transaction:
106
        raise TransactionServiceException(108, "no such transaction")
107
 
108
    return transaction
109
 
110
def get_transactions_for_customer(customer_id, from_date, to_date, status):
111
    if not customer_id:
112
        raise TransactionServiceException(101, "bad customer id")
113
    query = Transaction.query.filter(Transaction.customer_id == customer_id)
1493 vikas 114
    if status is not None:
483 rajveer 115
        query = query.filter(Transaction.status == status)
116
    if from_date:
117
        query = query.filter(Transaction.createdOn >from_date)
118
    if to_date:
119
        query = query.filter(Transaction.createdOn < to_date)
120
    return query.all()
121
 
122
def get_transactions_for_shopping_cart_id(shopping_cart_id):
123
    transactions = Transaction.query.filter_by(shopping_cart_id=shopping_cart_id).all()
124
    if not transactions:
125
        raise TransactionServiceException(108, "transactions for this shopping cart")
126
    return transactions
127
 
128
def get_transaction_status(transaction_id):
129
    if not transaction_id:
130
        raise TransactionServiceException(101, "bad transaction id")
131
 
132
    transaction = get_transaction(transaction_id)
133
 
134
    return transaction.status
135
 
136
def change_transaction_status(transaction_id, new_status, description):
137
    transaction = get_transaction(transaction_id)
138
    transaction.status = new_status
139
    transaction.status_message = description
690 chandransh 140
    if new_status == TransactionStatus.FAILED:
141
        for order in transaction.orders:
142
            order.status = OrderStatus.PAYMENT_FAILED
143
    elif new_status == TransactionStatus.IN_PROCESS:
144
        for order in transaction.orders:
145
            order.status = OrderStatus.SUBMITTED_FOR_PROCESSING
1219 chandransh 146
            order.statusDescription = "Submitted to warehouse"
743 rajveer 147
            #After we got payment success, we will set logistics info also 
148
            logistics_client = LogisticsClient().get_client()
149
            #FIXME line item is only one now. If multiple will come, need to fix.
871 chandransh 150
            item_id = order.lineitems[0].item_id
151
            logistics_info = logistics_client.getLogisticsInfo(order.customer_pincode, item_id)
743 rajveer 152
 
153
            order.warehouse_id = logistics_info.warehouseId
154
            order.logistics_provider_id = logistics_info.providerId
155
            order.airwaybill_no = logistics_info.airway_billno
156
            order.tracking_id = order.airwaybill_no
855 rajveer 157
            order.expected_delivery_time = datetime.datetime.now() + datetime.timedelta(days=logistics_info.deliveryTime)
871 chandransh 158
 
159
            catalog_client = InventoryClient().get_client()
160
            catalog_client.reserveItemInWarehouse(item_id, logistics_info.warehouseId, order.lineitems[0].quantity)
1351 varun.gupt 161
 
1341 chandransh 162
            item_pricing = catalog_client.getItemPricing(item_id, order.warehouse_id)
163
            order.lineitems[0].transfer_price = item_pricing.transferPrice
1262 varun.gupt 164
 
483 rajveer 165
    session.commit()
166
    return True
167
 
1398 varun.gupt 168
def enqueue_transaction_info_email(transaction_id):
1262 varun.gupt 169
    transaction = get_transaction(transaction_id)
170
 
171
    html_header = """
172
<html>
173
<body>
174
<div>
175
    <p>
176
        Hello,<br /><br />
177
        Thanks for your placing your order with us. Following are the details of your order:
178
    </p>
179
    <div>
180
        Order Date: $order_date
1569 varun.gupt 181
    </div>
1262 varun.gupt 182
    <div>
183
        <table>
1569 varun.gupt 184
        <tr><td colspan="5"><hr /></td></tr>
185
        <tr><td colspan="5" align="left"><b>Order Details</b></td></tr>
186
        <tr><td colspan="5"><hr /></td></tr>
1262 varun.gupt 187
        <tr>
1569 varun.gupt 188
            <th>&nbsp;&nbsp;Order No.&nbsp;&nbsp;</th>
189
            <th>&nbsp;&nbsp;Product&nbsp;&nbsp;</th>
190
            <th>&nbsp;&nbsp;Quantity&nbsp;&nbsp;</th>
191
            <th>&nbsp;&nbsp;Unit Price&nbsp;&nbsp;</th>
192
            <th>&nbsp;&nbsp;Amount&nbsp;&nbsp;</th>
1262 varun.gupt 193
        </tr>
194
    """
1398 varun.gupt 195
    user_email = ""
1262 varun.gupt 196
    total_amount = 0.0
197
    html_table = ""
1569 varun.gupt 198
    order_date = datetime.datetime.now()
1262 varun.gupt 199
 
200
    for order in transaction.orders:
201
        order_date = order.created_timestamp
1398 varun.gupt 202
        user_email = order.customer_email
1569 varun.gupt 203
        html_tr = "<tr><td align='center'>" + str(order.id) + "</td>"
1262 varun.gupt 204
 
205
        for lineitem in order.lineitems:
1276 chandransh 206
            html_tr += "<td>" + str(lineitem) + "</td>"
1569 varun.gupt 207
            html_tr += "<td align='center'>" + str(lineitem.quantity) + "</td>"
208
            html_tr += "<td align='center'>" + str(lineitem.unit_price) + "</td>"
209
            html_tr += "<td align='center'>" + str(lineitem.total_price) + "</td></tr>"
1262 varun.gupt 210
            total_amount += lineitem.total_price
211
            html_table += html_tr
212
    html_footer = """
213
        <tr><td colspan=5>&nbsp;</td></tr>
1565 varun.gupt 214
        <tr><td colspan=5><hr /></td></tr>
1262 varun.gupt 215
        <tr>
216
            <td colspan=4>Total Amount</td>
217
            <td>$total_amount</td>
218
        </tr>
1569 varun.gupt 219
        <tr><td colspan=5><hr /></td></tr>
1262 varun.gupt 220
        </table>
1569 varun.gupt 221
    </div>
1262 varun.gupt 222
    <p>
223
        Best Wishes,<br />
224
        Saholic Team
225
    </p>
226
</div>
227
</body>
228
</html>
229
    """
1398 varun.gupt 230
 
1569 varun.gupt 231
    dt = datetime.datetime.strptime(str(order_date), "%Y-%m-%d %H:%M:%S")
1565 varun.gupt 232
    formated_order_date = dt.strftime("%A, %d. %B %Y %I:%M%p")
233
 
1262 varun.gupt 234
    email_header = Template(html_header).substitute(dict(order_date = formated_order_date))
235
    email_footer = Template(html_footer).substitute(dict(total_amount = total_amount))
236
 
1351 varun.gupt 237
    try:
238
        helper_client = HelperClient().get_client()
1398 varun.gupt 239
        helper_client.saveUserEmailForSending(user_email, "", "Saholic - Order Details", email_header + html_table + email_footer, str(transaction_id), "TransactionInfo")
1351 varun.gupt 240
        return True
241
    except Exception as e:
242
        print e
243
        return False
1262 varun.gupt 244
 
483 rajveer 245
def get_order(order_id):
246
    order = Order.get_by(id=order_id)
247
    if not order:
248
        raise TransactionServiceException(108, "no such order")
249
 
250
    return order
251
 
252
def get_orders_for_customer(customer_id, from_date, to_date, status):
253
    if not customer_id:
254
        raise TransactionServiceException(101, "bad customer id")
255
    query = Order.query.filter(Order.customer_id == customer_id)
256
    if status:
257
        query = query.filter(Order.status == status)
258
    if from_date:
259
        query = query.filter(Order.created_timestamp >from_date)
260
    if to_date:
261
        query = query.filter(Order.created_timestamp < to_date)
262
    return query.all()
263
 
1528 ankur.sing 264
def get_order_for_customer(order_id, customer_id):
265
    if not customer_id:
266
        raise TransactionServiceException(101, "bad customer id")
267
    order = get_order(order_id)
268
    if order.customer_id != customer_id:
269
        raise TransactionServiceException(108, "order: " + str(order_id) + " does not belong to customer: " + str(customer_id))
270
    return order
271
 
483 rajveer 272
def get_all_orders(status, from_date, to_date, warehouse_id):
273
    if not warehouse_id:
274
        raise TransactionServiceException(101, "bad warehouse id")
275
    query = Order.query.filter(Order.warehouse_id == warehouse_id)
276
    if status:
277
        query = query.filter(Order.status == status)
278
    if from_date:
279
        query = query.filter(Order.created_timestamp >from_date)
280
    if to_date:
281
        query = query.filter(Order.created_timestamp < to_date)
282
    return query.all()
283
 
1382 varun.gupt 284
def get_returnable_orders_for_customer(customer_id, limit = None):
285
    if not customer_id:
286
        raise TransactionServiceException(101, "bad customer id")
287
 
288
    query = Order.query.filter(Order.customer_id == customer_id)
289
    query = query.filter(Order.status != OrderStatus.PAYMENT_FAILED)
290
    query = query.filter(Order.status != OrderStatus.REJECTED)
291
    query = query.filter(Order.status > OrderStatus.SHIPPED_FROM_WH)
292
    query = query.filter(or_(Order.status < OrderStatus.DELIVERY_SUCCESS, Order.delivery_timestamp < datetime.datetime.now() + datetime.timedelta(hours = 48)))
293
    orders = query.all()
294
    order_ids = [order.id for order in orders]
295
    return order_ids
296
 
297
def get_cancellable_orders_for_customer(customer_id, limit = None):
298
    if not customer_id:
299
        raise TransactionServiceException(101, "bad customer id")
300
 
301
    query = Order.query.filter(Order.customer_id == customer_id)
302
    query = query.filter(Order.status != OrderStatus.REJECTED)
1483 varun.gupt 303
    query = query.filter(Order.status >= OrderStatus.SUBMITTED_FOR_PROCESSING)
1382 varun.gupt 304
    query = query.filter(Order.status < OrderStatus.BILLED)
305
    orders = query.all()
306
    return [order.id for order in orders]
307
 
995 varun.gupt 308
def get_orders_by_billing_date(status, start_billing_date, end_billing_date, warehouse_id):
309
    if not warehouse_id:
310
        raise TransactionServiceException(101, "bad warehouse id")
311
 
312
    query = Order.query.filter(Order.warehouse_id == warehouse_id)
313
 
314
    if status:
315
        query = query.filter(Order.status == status)
316
 
317
    if start_billing_date:
318
        query = query.filter(Order.billing_timestamp >= start_billing_date)
319
 
320
    if end_billing_date:
321
        query = query.filter(Order.billing_timestamp <= end_billing_date)
322
 
323
    return query.all()
324
 
1528 ankur.sing 325
def get_orders_for_transaction(transaction_id, customer_id):
326
    orders = Order.query.filter_by(transaction_id=transaction_id, customer_id=customer_id).all()
327
 
1208 chandransh 328
    if not orders:
329
        raise TransactionServiceException(101, "No order for the transaction")
330
    return orders
331
 
1405 ankur.sing 332
def get_undelivered_orders(provider_id, warehouse_id):
333
    query = Order.query
334
    if provider_id != -1:
335
        query = query.filter_by(Order.logistics_provider_id == provider_id)
336
    if warehouse_id != -1:
337
        query = query.filter(Order.warehouse_id == warehouse_id)
338
    query = query.filter(and_(Order.status != OrderStatus.DELIVERY_SUCCESS, Order.status != OrderStatus.FAILED, \
339
                              Order.status > OrderStatus.SHIPPED_FROM_WH))
1479 ankur.sing 340
    #Get last midnight time instead of current time as courier company would have sent the 
341
    #delivery records updated at least till midnight and not by the current time.
342
    time_format = "%Y-%m-%d %H:%M:%S.%f"
343
    t = time.strptime(str(datetime.datetime.now()), time_format)
344
    midnightTime = datetime.datetime(*t[:3])
345
    query = query.filter(Order.expected_delivery_time < midnightTime)
1405 ankur.sing 346
    orders = query.all()
347
    return orders
348
 
1208 chandransh 349
def get_line_items_for_order(order_id):
350
    query = LineItem.query.filter(LineItem.order_id == order_id)
351
    return query.all()
352
 
483 rajveer 353
def change_order_status(self, orderId, status, description):
354
    order = get_order(orderId)
355
    order.status = status
356
    order.statusDescription = description
357
    session.commit()
358
    return True
359
 
921 rajveer 360
def accept_order(self, orderId):
361
    order = get_order(orderId)
362
    order.status = OrderStatus.ACCEPTED
363
    order.statusDescription = "Order Accepted"
364
    order.accepted_timestamp = datetime.datetime.now()
365
    session.commit()
366
    return True
367
 
368
def bill_order(self, orderId):
369
    order = get_order(orderId)
370
    order.status = OrderStatus.BILLED
371
    order.statusDescription = "Order Billed"
372
    order.billing_timestamp = datetime.datetime.now()
373
    session.commit()
1208 chandransh 374
    return True
921 rajveer 375
 
1149 chandransh 376
def add_billing_details(orderId, invoice_number, billed_by):
494 rajveer 377
    order = Order.get_by(id=orderId)
378
    if not order:
379
        raise TransactionServiceException(101, "No order found for the givem orderid")
380
    order.invoice_number = invoice_number
381
    order.billed_by = billed_by
382
    session.commit()
383
    return True
759 chandransh 384
 
1149 chandransh 385
def add_jacket_number(orderId, jacket_number):
386
    order = Order.get_by(id=orderId)
387
    if not order:
388
        raise TransactionServiceException(101, "No order found for the givem orderid")
389
    order.jacket_number = jacket_number 
390
    session.commit()
391
    return True
392
 
1220 chandransh 393
def batch_orders(warehouseId):
394
    if not warehouseId:
395
        raise TransactionServiceException(101, "bad warehouse id")
1225 chandransh 396
 
397
    batchno_generator = BatchNoGenerator()
398
    session.commit()
1220 chandransh 399
    query = Order.query.filter_by(warehouse_id=warehouseId)
400
    query = query.filter_by(status=OrderStatus.SUBMITTED_FOR_PROCESSING)
401
    query = query.order_by(Order.created_timestamp)
402
    pending_orders = query.all()
1225 chandransh 403
 
1220 chandransh 404
    serial_no = 1
405
    for order in pending_orders:
1276 chandransh 406
        order.batchNo = batchno_generator.id
1220 chandransh 407
        order.serialNo = serial_no
408
        serial_no += 1
409
    session.commit() 
410
    pending_orders = query.all()
411
    return pending_orders
412
 
413
def order_outofstock(orderId):
414
    order = get_order(orderId)
415
    order.status = OrderStatus.INVENTORY_LOW
416
    order.statusDescription = "Low Inventory"
417
    order.outofstock_timestamp = datetime.datetime.now()
418
    session.commit()
419
    return True
420
 
759 chandransh 421
def mark_orders_as_manifested(warehouse_id, provider_id):
422
    try:
986 chandransh 423
        current_timestamp = datetime.datetime.now()
852 chandransh 424
        orders = Order.query.filter_by(warehouse_id = warehouse_id, logistics_provider_id = provider_id, status = OrderStatus.BILLED).all()
759 chandransh 425
        for order in orders:
426
            order.status = OrderStatus.SHIPPED_FROM_WH
987 chandransh 427
            order.statusDescription = "Order shipped from warehouse"
986 chandransh 428
            order.shipping_timestamp = current_timestamp
759 chandransh 429
        session.commit()
430
        return True
431
    except:
432
        return False
1113 chandransh 433
 
434
def mark_orders_as_picked_up(provider_id, pickup_details):
435
    for awb, pickup_timestamp in pickup_details.iteritems():
1135 chandransh 436
        order = Order.query.filter_by(airwaybill_no=awb, logistics_provider_id = provider_id).first()
1113 chandransh 437
        if order == None:
438
            raise TransactionServiceException(102, "No order found for the awb: " + awb)
439
        order.status = OrderStatus.SHIPPED_TO_LOGST
440
        order.statusDescription = "Order picked up by Courier Company"
1246 chandransh 441
        order.pickup_timestamp = pickup_timestamp
1113 chandransh 442
    session.commit()
1217 chandransh 443
 
1132 chandransh 444
    current_time = datetime.datetime.now()
445
    to_datetime = datetime.datetime(current_time.year, current_time.month, current_time.day)
1246 chandransh 446
    from_datetime = to_datetime - datetime.timedelta(2)
1132 chandransh 447
    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 448
    return orders_not_picked_up
449
 
1132 chandransh 450
def mark_orders_as_delivered(provider_id, delivered_orders):
451
    for awb, detail in delivered_orders.iteritems():
1135 chandransh 452
        order = Order.query.filter_by(airwaybill_no=awb, logistics_provider_id = provider_id).first()
1132 chandransh 453
        if order == None:
454
            raise TransactionServiceException(103, "No order found for the awb:" + awb)
455
        order.status = OrderStatus.DELIVERY_SUCCESS
456
        order.statusDescription = "Order delivered"
457
        order.delivery_timestamp, order.receiver = detail.split('|')
458
    session.commit()
459
 
1135 chandransh 460
def mark_orders_as_failed(provider_id, returned_orders):
461
    for awb, detail in returned_orders.iteritems():
462
        order = Order.query.filter_by(airwaybill_no=awb, logistics_provider_id = provider_id).first()
463
        if order == None:
464
            raise TransactionServiceException(103, "No order found for the awb:" + awb)
465
        order.status = OrderStatus.FAILED
466
        order.delivery_timestamp, reason = detail.split('|')
467
        order.statusDescription = "Order Returned to Origin:" + reason
468
    session.commit()    
469
 
1246 chandransh 470
def update_non_delivery_reason(provider_id, undelivered_orders):
471
    for awb, reason in undelivered_orders.iteritems():
472
        order = Order.query.filter_by(airwaybill_no=awb, logistics_provider_id = provider_id).first()
473
        if order == None:
474
            raise TransactionServiceException(103, "No order found for the awb:" + awb)
475
        order.statusDescription = reason
476
    session.commit()
477
 
483 rajveer 478
def get_alerts(order_id, all):
479
    query = Alerts.query.filter_by(order_id=order_id)
480
    if not all:
481
        query = query.filter_by(valid=True)
482
    return query.all()
483
 
484
def set_alert(order_id, unset, type, comment):
485
    #get existing alert
486
    if unset:
487
        query = Alerts.query.filter_by(order_id=order_id)
488
        query = query.filter_by(type=type)
489
        alerts = query.all()
490
        for alert in alerts:
491
            if alert.valid:
492
                alert.valid = False
493
                alert.comment = comment
494
                alert.time_unset = datetime.datetime.now()
495
    else:
496
        alert = Alerts()
497
        alert.order_id = order_id
498
        alert.valid = True
499
        alert.type = type
500
        alert.comment = comment
501
        alert.time_set = datetime.datetime.now()
766 rajveer 502
    session.commit()    
503
 
1596 ankur.sing 504
def get_valid_order_count():
505
    return Order.query.filter(Order.status >= OrderStatus.SUBMITTED_FOR_PROCESSING).count()
506
 
1627 ankur.sing 507
def get_cust_count_with_successful_txn():
508
    return session.query(Order.customer_id).filter(Order.status >= OrderStatus.SUBMITTED_FOR_PROCESSING).distinct().count()
509
 
510
def get_max_valid_order_amount():
511
    orders = Order.query.filter(Order.status >= OrderStatus.SUBMITTED_FOR_PROCESSING).all()
512
    order_amounts = [o.total_amount for o in orders]
513
    return max(order_amounts)
514
 
515
def get_min_valid_order_amount():
516
    orders = Order.query.filter(Order.status >= OrderStatus.SUBMITTED_FOR_PROCESSING).all()
517
    order_amounts = [o.total_amount for o in orders]
518
    return min(order_amounts)
519
 
766 rajveer 520
def close_session():
521
    if session.is_active:
522
        print "session is active. closing it."
523
        session.close()