Subversion Repositories SmartDukaan

Rev

Rev 2850 | Rev 2966 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2850 Rev 2851
Line 10... Line 10...
10
import time
10
import time
11
from datetime import date
11
from datetime import date
12
from textwrap import dedent
12
from textwrap import dedent
13
from string import Template
13
from string import Template
14
 
14
 
15
from sqlalchemy.sql.expression import and_, or_, desc, not_
15
from sqlalchemy.sql.expression import and_, or_, desc, not_, distinct
16
from sqlalchemy.sql import func
16
from sqlalchemy.sql import func
17
 
17
 
18
from elixir import *
18
from elixir import *
19
 
19
 
20
from reportlab.pdfgen.canvas import Canvas
20
from reportlab.pdfgen.canvas import Canvas
Line 591... Line 591...
591
        alert.comment = comment
591
        alert.comment = comment
592
        alert.time_set = datetime.datetime.now()
592
        alert.time_set = datetime.datetime.now()
593
    session.commit()    
593
    session.commit()    
594
 
594
 
595
def get_valid_order_count():
595
def get_valid_order_count():
-
 
596
    '''
-
 
597
    Returns the number of orders which we processed. The reshipped orders are not
-
 
598
    counted since there'll always be a new compensating order present.
-
 
599
    '''
596
    return Order.query.filter(Order.status >= OrderStatus.SUBMITTED_FOR_PROCESSING).filter(not_(Order.status.in_( (OrderStatus.SALES_RET_RESHIPPED, OrderStatus.DOA_INVALID_RESHIPPED, OrderStatus.DOA_RESHIPPED) ))).count()
600
    return Order.query.filter(Order.status >= OrderStatus.SUBMITTED_FOR_PROCESSING).filter(not_(Order.status.in_((OrderStatus.SALES_RET_RESHIPPED, OrderStatus.DOA_INVALID_RESHIPPED, OrderStatus.DOA_RESHIPPED)))).count()
597
 
601
 
598
def get_cust_count_with_successful_txn():
602
def get_cust_count_with_successful_txn():
-
 
603
    '''
-
 
604
    Returns the number of distinct customers who have done successful transactions.
-
 
605
    It uses the sqlalchemy func module to construct an efficient query.
-
 
606
    '''
599
    return session.query(Order.customer_id).filter(Order.status >= OrderStatus.SUBMITTED_FOR_PROCESSING).distinct().count()
607
    return session.query(func.count(distinct(Order.customer_id))).filter(Order.status >= OrderStatus.SUBMITTED_FOR_PROCESSING).scalar()
600
 
608
 
601
def get_valid_orders_amount_range():
609
def get_valid_orders_amount_range():
-
 
610
    '''
602
    orders = Order.query.filter(Order.status >= OrderStatus.SUBMITTED_FOR_PROCESSING).all()
611
    Returns a list containing the minimum and maximum amount across all
603
    order_amounts = [o.total_amount for o in orders]
612
    orders placed with us. No need to consider reshippped orders for min/max
604
    return [min(order_amounts), max(order_amounts)]
613
    amount of orders.
-
 
614
    '''
-
 
615
    return session.query(func.min(Order.total_amount), func.max(Order.total_amount)).filter(Order.status >= OrderStatus.SUBMITTED_FOR_PROCESSING).one()
605
 
616
 
606
def get_valid_orders(limit):
617
def get_valid_orders(limit):
-
 
618
    '''
-
 
619
    Returns the last limit number of valid orders placed with us. Ignored the
-
 
620
    limit if it's passed as 0. Raises an excpetion if the supplied limit is 
-
 
621
    less than 0. 
-
 
622
    '''
-
 
623
    if limit < 0:
-
 
624
        raise TransactionServiceException(101, "Invalid argument limit " + limit)
607
    query = Order.query.filter(Order.status >= OrderStatus.SUBMITTED_FOR_PROCESSING)
625
    query = Order.query.filter(Order.status >= OrderStatus.SUBMITTED_FOR_PROCESSING)
608
    query = query.order_by(desc(Order.created_timestamp))
626
    query = query.order_by(desc(Order.created_timestamp))
609
    if limit > 0:
627
    if limit != 0:
610
        query = query.limit(limit)
628
        query = query.limit(limit)
611
    elif limit < 0:
-
 
612
        raise TransactionServiceException(101, "Invalid argument limit " + limit)
-
 
613
    return query.all()
629
    return query.all()
614
 
630
 
615
def get_next_invoice_number():
631
def get_next_invoice_number():
616
    entity_id = InvoiceIDGenerator.query.with_lockmode("update").one()
632
    entity_id = InvoiceIDGenerator.query.with_lockmode("update").one()
617
    invoice_number = entity_id.id + 1
633
    invoice_number = entity_id.id + 1