Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
4503 mandeep.dh 1
'''
2
Created on 29-Jul-2011
3
 
4
@author: Chandranshu
5
'''
6
from elixir import metadata, setup_all, session
7
from shop2020.purchase.main.model.LineItem import LineItem
8
from shop2020.purchase.main.model.Purchase import Purchase
9
from shop2020.purchase.main.model.PurchaseOrder import PurchaseOrder
10
from shop2020.purchase.main.model.Supplier import Supplier
11
from shop2020.thriftpy.purchase.ttypes import PurchaseServiceException
12
from sqlalchemy import create_engine
13
import datetime
14
import logging
15
 
16
 
17
logging.basicConfig(level=logging.DEBUG)
18
 
19
class PurchaseServiceHandler:
20
    '''
21
    classdocs
22
    '''
23
 
24
    def __init__(self, dbname='warehouse', db_hostname='localhost',  echoOn=True):
25
        '''
26
        Constructor
27
        '''
28
        engine = create_engine('mysql://root:shop2020@' + db_hostname + '/' + dbname, pool_recycle=7200)
29
        metadata.bind = engine
30
        metadata.bind.echo = echoOn
31
        setup_all(True)
32
 
33
    def createPurchaseOrder(self, tPurchaseOrder):
34
        """
35
        Creates a purchase order based on the data in the given purchase order object.
36
        This method populates a number of missing fields
37
 
38
        Parameters:
39
         - purchaseOrder
40
        """
41
        try:
42
            purchaseOrder = PurchaseOrder(tPurchaseOrder)
43
            session.commit()
44
            purchaseOrder.set_po_number()
45
            session.commit()
46
            return purchaseOrder.id
47
        finally:
48
            self.close_session()
49
 
50
    def getAllPurchaseOrders(self, status):
51
        """
52
        Returns a list of all the purchase orders in the given state
53
 
54
        Parameters:
55
         - status
56
        """
57
        try:
58
            pos = PurchaseOrder.query.filter_by(status=status).all()
59
            return [po.to_thrift_object() for po in pos ]
60
        finally:
61
            self.close_session()
62
 
63
    def getPurchaseOrder(self, id):
64
        """
65
        Returns the purchase order with the given id. Throws an exception if there is no such purchase order.
66
 
67
        Parameters:
68
         - id
69
        """
70
        try:
71
            purchaseOrder = PurchaseOrder.get_by(id=id)
72
            if not purchaseOrder:
73
                raise PurchaseServiceException(101, "No purchase order can be found with id:" + str(id)) 
74
            return purchaseOrder.to_thrift_object()
75
        finally:
76
            self.close_session()
77
 
78
    def getSupplier(self, id):
79
        """
80
        Returns the supplier with the given order id. Throws an exception if there is no such supplier.
81
 
82
        Parameters:
83
         - id
84
        """
85
        try:
86
            return Supplier.get_by(id=id).to_thrift_object()
87
        finally:
88
            self.close_session()
89
 
90
    def startPurchase(self, purchaseOrderId, invoiceNumber, freightCharges):
91
        """
92
        Creates a purchase for the given purchase order.
93
        Throws an exception if no more purchases are allowed against the given purchase order.
94
 
95
        Parameters:
96
         - purchaseOrderId
97
         - invoiceNumber
98
         - freightCharges
99
        """
100
        try:
101
            purchase_order = PurchaseOrder.get_by(id = purchaseOrderId)
102
            purchase = Purchase(purchase_order, invoiceNumber, freightCharges)
103
            session.commit()
104
            return purchase.id
105
        finally:
106
            self.close_session()
107
 
108
    def closePurchase(self, purchaseId):
109
        """
110
        Marks a purchase as complete and updates the receivedOn time.
111
        Throws an exception if no such purchase exists.
112
 
113
        Parameters:
114
         - purchaseId
115
        """
116
        try:
117
            purchase = Purchase.get_by(id=purchaseId)
118
            purchase.receivedOn = datetime.datetime.now()
119
            session.commit()
120
        finally:
121
            self.close_session()
122
 
123
    def getAllPurchases(self, purchaseOrderId, open):
124
        """
125
        Returns all open or closed purchases for the given purchase order. Throws an exception if no such purchase order exists
126
 
127
        Parameters:
128
         - purchaseOrderId
129
         - open
130
        """
131
        try:
132
            if open:
133
                purchases = Purchase.query.filter_by(purchaseOrder_id = purchaseOrderId, receivedOn = None).all()
134
            else:
135
                purchases = Purchase.query.filter_by(purchaseOrder_id = purchaseOrderId).filter(receivedOn != None).all()
136
 
137
            return [purchase.to_thrift_object() for purchase in purchases]
138
        finally:
139
            self.close_session()
140
 
141
    def getPrice(self, purchaseId, itemId):
142
        """
143
        Returns the price at which we bought an item from a supplier
144
 
145
        Parameters:
146
         - purchaseId
147
         - itemId
148
        """
149
        purchaseOrderId = Purchase.query.filter_by(id = purchaseId).one().purchaseOrder_id
4547 mandeep.dh 150
        return LineItem.query.filter_by(purchaseOrder_id = purchaseOrderId).filter_by(itemId = itemId).one().unitPrice
4503 mandeep.dh 151
 
152
    def close_session(self):
153
        if session.is_active:
154
            print "session is active. closing it."
155
            session.close()
156
 
157
    def isAlive(self, ):
158
        """
159
        For checking weather service is active alive or not. It also checks connectivity with database
160
        """
161
        try:
162
            session.query(Supplier.id).limit(1).all()
163
            return True
164
        except:
165
            return False
166
        finally:
167
            self.close_session()
168