Subversion Repositories SmartDukaan

Rev

Rev 1135 | Rev 1208 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

'''
Created on 29-Mar-2010

@author: ashish
'''
from shop2020.model.v1.order.impl import DataService
from shop2020.model.v1.order.impl.DataAccessors import create_transaction,\
create_order, get_new_transaction, get_transactions_for_customer, get_transaction_status,\
 get_line_items_for_order, get_transaction, get_transactions_for_shopping_cart_id,\
 change_transaction_status, get_orders_for_customer,get_orders_for_transaction, get_order,\
 get_all_orders, change_order_status, get_alerts, set_alert, add_billing_details,\
    mark_orders_as_manifested, close_session, accept_order, bill_order,\
    get_orders_by_billing_date, mark_orders_as_picked_up,\
    mark_orders_as_delivered, mark_orders_as_failed, add_jacket_number
from shop2020.model.v1.order.impl.Convertors import to_t_transaction,\
    to_t_lineitem, to_t_alert, to_t_order, to_t_lineitem
from shop2020.thriftpy.model.v1.order.ttypes import Transaction, OrderStatus,\
    LineItem, Order
from shop2020.utils.Utils import to_py_date, get_fdate_tdate

class OrderServiceHandler:
    
    def __init__(self):
        """
        Constructor
        """
        DataService.initialize()
        
    def createTransaction(self, transaction):
        """
        Parameters:
         - transaction
        """
        try:
            return create_transaction(transaction)
        finally:
            close_session()
        
    def getTransaction(self, id):
        """
            Get transaction methods.
        
        Parameters:
         - id
        """
        try:
            transaction = get_transaction(id)
            return to_t_transaction(transaction)
        finally:
            close_session()
            
    def getTransactionsForCustomer(self, customerId, from_date, to_date, status):
        """
        Parameters:
         - customerId
         - from_date
         - to_date
         - status
        """
        try:
            current_from_date, current_to_date = get_fdate_tdate(from_date, to_date)
                    
            transactions = get_transactions_for_customer(customerId, current_from_date, current_to_date, status)
            t_transaction = []
            for transaction in transactions:
                t_transaction.append(to_t_transaction(transaction))
            return t_transaction
        finally:
            close_session()

    def getTransactionsForShoppingCartId(self, shoppingCartId):
        """
        Parameters:
            - shoppingCartId
        """
        try:
            transactions = get_transactions_for_shopping_cart_id(shoppingCartId)
            t_transaction = []
            for transaction in transactions:
                t_transaction.append(to_t_transaction(transaction))
            return t_transaction
        finally:
            close_session()
    
    def getTransactionStatus(self, transactionId):
        """
        Parameters:
         - transactionId
        """
        try:
            return get_transaction_status(transactionId)
        finally:
            close_session()
                
    def changeTransactionStatus(self, transactionId, status, description):
        """
        Parameters:
         - transactionId
         - status
         - description
        """
        try:
            return change_transaction_status(transactionId, status, description)
        finally:
            close_session()
            
    def getOrdersForTransaction(self, transactionId):
        """
        Parameters:
         - transactionId
        """
        try:
            orders = get_orders_for_transaction(transactionId)
            return [to_t_order(order) for order in orders]    
        finally:
            close_session()
            
    def getAllOrders(self, status, from_date, to_date, warehouse_id):
        """
        Parameters:
         - status
         - from_date
         - to_date
         - warehouse_id
        """
        try:
            current_from_date, current_to_date = get_fdate_tdate(from_date, to_date)        
            orders = get_all_orders(status, current_from_date, current_to_date, warehouse_id)
            return [to_t_order(order) for order in orders]
        finally:
            close_session()
            
    def getOrdersByBillingDate(self, status, start_billing_date, end_billing_date, warehouse_id):
        """
        Parameters:
         - status
         - start_billing_date
         - end_billing_date
         - warehouse_id
        """
        try:
            current_start_billing_date, current_end_billing_date = get_fdate_tdate(start_billing_date, end_billing_date)
            orders = get_orders_by_billing_date(status, current_start_billing_date, current_end_billing_date, warehouse_id)
            return [to_t_order(order) for order in orders]
        finally:
            close_session()
            
    def changeOrderStatus(self, orderId, status, description):
        """
        Parameters:
         - orderId
         - status
         - description
         - 
        """
        try:
            return change_order_status(self, orderId, status, description)
        finally:
            close_session()

    def acceptOrder(self, orderId):
        """
        Parameters:
         - orderId
        """
        try:
            return accept_order(self, orderId)
        finally:
            close_session()
    
    def billOrder(self, orderId):
        """
        Parameters:
         - orderId
        """
        try:
            return bill_order(self, orderId)
        finally:
            close_session()
            
    def getOrdersForCustomer(self, customerId, from_date, to_date, status):
        """
        Parameters:
         - customerId
         - from_date
         - to_date
         - status
        """
        try:
            current_from_date, current_to_date = get_fdate_tdate(from_date, to_date)
            
            orders = get_orders_for_customer(customerId, current_from_date, current_to_date, status)
            return [to_t_order(order) for order in orders]
        finally:
            close_session()
            
    def getOrder(self, id):
        """
        Parameters:
         - id
        """
        try:
            return to_t_order(get_order(id))
        finally:
            close_session()
            
    def getLineItemsForOrder(self, orderId):
        """
        Parameters:
         - orderId
        """
        try:
            lineitems = get_line_items_for_order(orderId)
            t_lineitems = []
            for lineitem in lineitems:
                t_lineitems.append(to_t_lineitem(lineitem))
            return t_lineitems
        finally:
            close_session()

    def addBillingDetails(self, orderId, invoice_number, billed_by):
        """
        Add billing details such as the bill number and the biller to the Order.
        
        Parameters:
         - orderId
         - invoice_number
         - billed_by
        """
        try:
            return add_billing_details(orderId, invoice_number, billed_by)
        finally:
            close_session()

    def addJacketNumber(self, orderId, jacketNumber):
        """
        Adds jacket number to the order. Return false if it doesn't find the order with the given ID.
        
        Parameters:
         - orderId
         - jacketNumber
        """
        try:
            return add_jacket_number(orderId, jacketNumber)
        finally:
            close_session()
            
    def markOrdersAsManifested(self, warehouseId, providerId):
        """
        Marks all BILLED orders for a warehouse and a provider as SHIPPED_FROM_WH
        
        Parameters:
         - warehouseId
         - providerId
        """
        try:
            return mark_orders_as_manifested(warehouseId, providerId)
        finally:
            close_session()

    def markOrdersAsPickedUp(self, providerId, pickupDetails):
        """
        Marks all SHIPPED_FROM_WH orders of the previous day for a provider as SHIPPED_TO_LOGISTICS.
        Returns a list of orders that were shipped from warehouse but did not appear in the pick-up report.
        Raises an exception if we encounter report for an AWB number that we did not ship.
        
        Parameters:
         - providerId
         - pickupDetails
        """
        try:
            orders_not_picked_up = mark_orders_as_picked_up(providerId, pickupDetails)
            return [to_t_order(order) for order in orders_not_picked_up]
        finally:
            close_session()

    def markOrdersAsDelivered(self, providerId, deliveredOrders):
        """
        Marks all orders with AWBs in the given map as delivered. Also sets the delivery timestamp and
        the name of the receiver.
        Raises an exception if we encounter report for an AWB number that we did not ship.
        
        Parameters:
         - providerId
         - deliveredOrders
        """
        try:
            mark_orders_as_delivered(providerId, deliveredOrders)
        finally:
            close_session()

    def markOrdersAsFailed(self, providerId, returnedOrders):
        """
        Mark all orders with AWBs in the given map as failed. Also sets the delivery timestamp.
        Raises an exception if we encounter report for an AWB number that we did not ship.
        
        Parameters:
         - providerId
         - returnedOrders
        """
        try:
            mark_orders_as_failed(providerId, returnedOrders)
        finally:
            close_session()
            
    def getAlerts(self, orderId, valid):
        """
        Parameters:
         - orderId
         - valid
        """
        try:
            alerts = get_alerts(orderId, valid)
            
            t_alerts = []
            
            for alert in alerts:
                t_alerts.append(to_t_alert(alert)) 
            
            return t_alerts
        finally:
            close_session()
            
    def setAlert(self, orderId, unset, type, comment):
        """
        Parameters:
         - orderId
         - unset
         - type
         - comment
        """
        try:
            set_alert(orderId, unset, type, comment)
        finally:
            close_session()
            
    def closeSession(self, ):
        close_session()
    
    
    
    '''
if __name__ == "__main__":
    order = OrderServiceHandler()
    t = order.getTransaction(4)
    print t
    
    
    these methods commented right now. may be used later
    def getAllTransactions(self, status, from_date, to_date, warehouse_id):
        """
        Parameters:
         - status
         - from_date
         - to_date
        """
        current_status, current_from_date, current_to_date = get_status_fdate_tdate(status, from_date, to_date)
        
        transactions = get_all_transactions(current_status, current_from_date, current_to_date, warehouse_id)
        t_transaction = []
        for transaction in transactions:
            t_transaction.append(to_t_transaction(transaction))
        return t_transaction

    def getTransactionsForShipmentStatus(self, status, from_date, to_date):
        """
        Parameters:
         - status
         - from_date
         - to_date
        """
        current_status, current_from_date, current_to_date = get_status_fdate_tdate(status, from_date, to_date)
            
        transactions = get_transactions_for_shipment_status(current_status, current_from_date, current_to_date)
        t_transaction = []
        for transaction in transactions:
            t_transaction.append(to_t_transaction(transaction))
        return t_transaction

    def getTransactionsForCustomerAndShipmentStatus(self, customerId, from_date, to_date, status):
        """
        Parameters:
         - customerId
         - from_date
         - to_date
         - status
        """
        current_status, current_from_date, current_to_date = get_status_fdate_tdate(status, from_date, to_date)
      
        transactions = get_transactions_for_customer_shipment(customerId, current_from_date, current_to_date, current_status)
        t_transaction = []
        for transaction in transactions:
            t_transaction.append(to_t_transaction(transaction))
        return t_transaction

    def getOrderInfo(self, transactionId):
        """
            Get and set individual objects in it
        *
        
        Parameters:
         - transactionId
        """
        lineitems = get_line_items(transactionId)
        order_info = OrderInfo()
        order_info.lineitems = []
        for lineitem in lineitems:
            order_info.lineitems.append(to_t_lineitem(lineitem))
            order_info.id = lineitem.transaction.id
        
        return order_info
    
    def getShippingInfo(self, transactionId):
        """
        Parameters:
         - transactionId
        """
        shipments = get_shipments(transactionId)
        shipment_info = ShipmentInfo()
        shipment_info.shipments = []
        for shipment in shipments:
            shipment_info.shipments.append(to_t_shipment(shipment))
            shipment_info.id = shipment.id
        return shipment_info
    def getBillingInfo(self, transactionId):
        """
        Parameters:
         - transactionId
        """
        billings = get_billings(transactionId)
        billing_info = BillingInfo()
        billing_info.billings = []
        if billings:
            for billing in billings:
                billing_info.billings.append(to_t_billing(billing))
                billing_info.id = billing.line_item.transaction.id
        return billing_info
    
    def addBilling(self, transactionId, billing):
        """
        Parameters:
         - tranactionId
         - billing
        """
        return set_billing(transactionId, billing)
    
    def setShippingTracker(self, transactionId, shippingId, trackingId, airwayBillNo, provider):
        """
        Parameters:
         - transactionId
         - shippingId
         - trackingId
         - airwayBillNo
         - provider
        """
        return set_shipping_tracker_info(transactionId, shippingId, trackingId, airwayBillNo, provider)
    
    def setShippingDate(self, transactionId, shippingId, timestamp):
        """
        Parameters:
         - transactionId
         - shippingId
         - timestamp
        """
        return set_shipping_date(transactionId, shippingId, to_py_date(timestamp))
    
    def setDeliveryDate(self, transactionId, shippingid, timestamp):
        """
        Parameters:
         - transactionId
         - shippingid
         - timestamp
        """
        return set_delivery_date(transactionId, shippingid, timestamp)

    def getAlerts(self, transactionId, valid):
        """
        Parameters:
         - transactionId
         - valid
        """
        alerts = get_alerts(transactionId, valid)
        
        t_alerts = []
        
        for alert in alerts:
            t_alerts.append(to_t_alert(alert)) 
        
        return t_alerts

    def setAlert(self, transactionId, unset, type, comment):
        """
        Parameters:
         - transactionId
         - unset
         - type
         - comment
        """
        set_alert(transactionId, unset, type, comment)

    def getExtraInfo(self, transaction_id):
        """
        Parameters:
         - transaction_id
        """
        extra_info = get_extra_info(transaction_id)
        
        return to_t_extra_item_info(extra_info, transaction_id)
        

    def setExtraInfo(self, transaction_id, sku_id, model, colour, vendor):
        """
        Parameters:
         - transaction_id
         - sku_id
         - model
         - colour
         - vendor
        """
        set_extra_info(transaction_id, sku_id, model, vendor, colour)

    '''