Subversion Repositories SmartDukaan

Rev

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

'''
Created on 29-Jul-2011

@author: Chandranshu
'''
from elixir import metadata, setup_all, session
from shop2020.purchase.main.model.LineItem import LineItem
from shop2020.purchase.main.model.Purchase import Purchase
from shop2020.purchase.main.model.PurchaseOrder import PurchaseOrder
from shop2020.purchase.main.model.Supplier import Supplier
from shop2020.thriftpy.purchase.ttypes import PurchaseServiceException
from sqlalchemy import create_engine
import datetime
import logging


logging.basicConfig(level=logging.DEBUG)

class PurchaseServiceHandler:
    '''
    classdocs
    '''

    def __init__(self, dbname='warehouse', db_hostname='localhost',  echoOn=True):
        '''
        Constructor
        '''
        engine = create_engine('mysql://root:shop2020@' + db_hostname + '/' + dbname, pool_recycle=7200)
        metadata.bind = engine
        metadata.bind.echo = echoOn
        setup_all(True)
        
    def createPurchaseOrder(self, tPurchaseOrder):
        """
        Creates a purchase order based on the data in the given purchase order object.
        This method populates a number of missing fields
        
        Parameters:
         - purchaseOrder
        """
        try:
            purchaseOrder = PurchaseOrder(tPurchaseOrder)
            session.commit()
            purchaseOrder.set_po_number()
            session.commit()
            return purchaseOrder.id
        finally:
            self.close_session()

    def getAllPurchaseOrders(self, status):
        """
        Returns a list of all the purchase orders in the given state
        
        Parameters:
         - status
        """
        try:
            pos = PurchaseOrder.query.filter_by(status=status).all()
            return [po.to_thrift_object() for po in pos ]
        finally:
            self.close_session()

    def getPurchaseOrder(self, id):
        """
        Returns the purchase order with the given id. Throws an exception if there is no such purchase order.
        
        Parameters:
         - id
        """
        try:
            purchaseOrder = PurchaseOrder.get_by(id=id)
            if not purchaseOrder:
                raise PurchaseServiceException(101, "No purchase order can be found with id:" + str(id)) 
            return purchaseOrder.to_thrift_object()
        finally:
            self.close_session()

    def getSupplier(self, id):
        """
        Returns the supplier with the given order id. Throws an exception if there is no such supplier.
        
        Parameters:
         - id
        """
        try:
            return Supplier.get_by(id=id).to_thrift_object()
        finally:
            self.close_session()

    def startPurchase(self, purchaseOrderId, invoiceNumber, freightCharges):
        """
        Creates a purchase for the given purchase order.
        Throws an exception if no more purchases are allowed against the given purchase order.
        
        Parameters:
         - purchaseOrderId
         - invoiceNumber
         - freightCharges
        """
        try:
            purchase_order = PurchaseOrder.get_by(id = purchaseOrderId)
            purchase = Purchase(purchase_order, invoiceNumber, freightCharges)
            session.commit()
            return purchase.id
        finally:
            self.close_session()

    def closePurchase(self, purchaseId):
        """
        Marks a purchase as complete and updates the receivedOn time.
        Throws an exception if no such purchase exists.
        
        Parameters:
         - purchaseId
        """
        try:
            purchase = Purchase.get_by(id=purchaseId)
            purchase.receivedOn = datetime.datetime.now()
            session.commit()
        finally:
            self.close_session()

    def getAllPurchases(self, purchaseOrderId, open):
        """
        Returns all open or closed purchases for the given purchase order. Throws an exception if no such purchase order exists
        
        Parameters:
         - purchaseOrderId
         - open
        """
        try:
            if open:
                purchases = Purchase.query.filter_by(purchaseOrder_id = purchaseOrderId, receivedOn = None).all()
            else:
                purchases = Purchase.query.filter_by(purchaseOrder_id = purchaseOrderId).filter(receivedOn != None).all()
            
            return [purchase.to_thrift_object() for purchase in purchases]
        finally:
            self.close_session()

    def getPrice(self, purchaseId, itemId):
        """
        Returns the price at which we bought an item from a supplier

        Parameters:
         - purchaseId
         - itemId
        """
        purchaseOrderId = Purchase.query.filter_by(id = purchaseId).one().purchaseOrder_id
        unitPrice = LineItem.query.filter_by(purchaseOrder_id = purchaseOrderId).filter_by(itemId = itemId).one().unitPrice
        self.close_session()
        return unitPrice

    def close_session(self):
        if session.is_active:
            print "session is active. closing it."
            session.close()

    def isAlive(self, ):
        """
        For checking weather service is active alive or not. It also checks connectivity with database
        """
        try:
            session.query(Supplier.id).limit(1).all()
            return True
        except:
            return False
        finally:
            self.close_session()