| 4503 |
mandeep.dh |
1 |
'''
|
|
|
2 |
Created on 29-Jul-2011
|
|
|
3 |
|
|
|
4 |
@author: Chandranshu
|
|
|
5 |
'''
|
|
|
6 |
from elixir.entity import Entity
|
|
|
7 |
from elixir.fields import Field
|
|
|
8 |
from elixir.options import using_options, using_table_options
|
|
|
9 |
from elixir.relationships import OneToMany
|
|
|
10 |
from shop2020.purchase.main.model.LineItem import LineItem
|
|
|
11 |
from shop2020.thriftpy.purchase.ttypes import PurchaseOrder as TPurchaseOrder, \
|
| 9925 |
amar.kumar |
12 |
POStatus
|
| 4503 |
mandeep.dh |
13 |
from shop2020.utils.Utils import to_java_date
|
| 6821 |
amar.kumar |
14 |
from sqlalchemy.types import Integer, String, Float, DateTime, Enum
|
| 4503 |
mandeep.dh |
15 |
import datetime
|
|
|
16 |
import time
|
|
|
17 |
|
|
|
18 |
|
|
|
19 |
|
|
|
20 |
|
|
|
21 |
class PurchaseOrder(Entity):
|
|
|
22 |
'''
|
|
|
23 |
classdocs
|
|
|
24 |
'''
|
|
|
25 |
id = Field(Integer, primary_key=True, autoincrement=True)
|
|
|
26 |
poNumber = Field(String(20))
|
|
|
27 |
supplierId = Field(Integer)
|
| 20025 |
amit.gupta |
28 |
buyerId = Field(Integer)
|
| 9925 |
amar.kumar |
29 |
warehouseId = Field(Integer)
|
| 20025 |
amit.gupta |
30 |
warehouseAddressId = Field(Integer)
|
| 9925 |
amar.kumar |
31 |
shippingWarehouseId = Field(Integer)
|
| 20025 |
amit.gupta |
32 |
shippingWarehouseAddressId = Field(Integer)
|
| 4503 |
mandeep.dh |
33 |
status = Field(Integer)
|
|
|
34 |
createdAt = Field(DateTime)
|
|
|
35 |
updatedAt = Field(DateTime)
|
|
|
36 |
totalCost = Field(Float)
|
|
|
37 |
freightCharges = Field(Float)
|
|
|
38 |
realizedCost = Field(Float)
|
|
|
39 |
realizedFreightCharges = Field(Float)
|
| 6821 |
amar.kumar |
40 |
type = Field(Enum('REAL', 'VIRTUAL'), default = 'REAL', server_default='REAL')
|
| 9416 |
amar.kumar |
41 |
taxType = Field(Integer)
|
| 4503 |
mandeep.dh |
42 |
lineitems = OneToMany("LineItem")
|
|
|
43 |
purchases = OneToMany("Purchase")
|
|
|
44 |
using_options(shortnames=True)
|
|
|
45 |
using_table_options(mysql_engine="InnoDB")
|
|
|
46 |
|
|
|
47 |
def __init__(self, t_purchase_order):
|
|
|
48 |
'''
|
|
|
49 |
Constructor
|
|
|
50 |
'''
|
|
|
51 |
#Generate the poNumber
|
|
|
52 |
self.supplierId = t_purchase_order.supplierId
|
|
|
53 |
self.warehouseId = t_purchase_order.warehouseId
|
| 9925 |
amar.kumar |
54 |
if t_purchase_order.shippingWarehouseId:
|
|
|
55 |
self.shippingWarehouseId = t_purchase_order.shippingWarehouseId
|
|
|
56 |
else:
|
|
|
57 |
self.shippingWarehouseId = t_purchase_order.warehouseId
|
| 4503 |
mandeep.dh |
58 |
|
|
|
59 |
#TODO: This should be INIT. Category manager should review and mark it as ready.
|
|
|
60 |
self.status = POStatus.READY
|
|
|
61 |
self.createdAt = datetime.datetime.now()
|
|
|
62 |
self.totalCost = t_purchase_order.totalCost
|
|
|
63 |
self.freightCharges = t_purchase_order.freightCharges or 0
|
|
|
64 |
self.realizedCost = 0
|
|
|
65 |
self.realizedFreightCharges = 0
|
| 6762 |
amar.kumar |
66 |
if t_purchase_order.lineitems:
|
| 6857 |
amar.kumar |
67 |
for t_lineitem in t_purchase_order.lineitems:
|
| 12866 |
manish.sha |
68 |
if t_lineitem.quantity == 0:
|
| 12864 |
manish.sha |
69 |
t_purchase_order.lineitems.pop(t_purchase_order.lineitems.index(t_lineitem))
|
| 6762 |
amar.kumar |
70 |
self.lineitems = [LineItem(self, t_lineitem) for t_lineitem in t_purchase_order.lineitems]
|
| 6821 |
amar.kumar |
71 |
self.totalCost = sum([t_lineitem.quantity * t_lineitem.unitPrice for t_lineitem in t_purchase_order.lineitems])
|
|
|
72 |
self.type = t_purchase_order.type
|
| 9416 |
amar.kumar |
73 |
self.poNumber = t_purchase_order.poNumber
|
| 20025 |
amit.gupta |
74 |
self.taxType = t_purchase_order.taxType
|
|
|
75 |
self.buyerId = t_purchase_order.buyerId
|
|
|
76 |
self.warehouseAddressId = t_purchase_order.warehouseAddressId
|
|
|
77 |
self.shippingWarehouseAddressId = t_purchase_order.shippingWarehouseAddressId
|
|
|
78 |
|
| 4503 |
mandeep.dh |
79 |
|
|
|
80 |
def to_thrift_object(self):
|
|
|
81 |
t_purchase_order = TPurchaseOrder()
|
|
|
82 |
t_purchase_order.id = self.id
|
|
|
83 |
t_purchase_order.poNumber = self.poNumber
|
|
|
84 |
t_purchase_order.supplierId = self.supplierId
|
| 7410 |
amar.kumar |
85 |
t_purchase_order.warehouseId = self.warehouseId
|
| 20025 |
amit.gupta |
86 |
t_purchase_order.warehouseAddressId = self.warehouseAddressId
|
| 9925 |
amar.kumar |
87 |
t_purchase_order.shippingWarehouseId = self.shippingWarehouseId
|
| 20071 |
amit.gupta |
88 |
t_purchase_order.shippingWarehouseAddressId = self.shippingWarehouseAddressId
|
| 4503 |
mandeep.dh |
89 |
t_purchase_order.status = self.status
|
|
|
90 |
t_purchase_order.createdAt = to_java_date(self.createdAt)
|
|
|
91 |
if self.updatedAt:
|
|
|
92 |
t_purchase_order.updatedAt = to_java_date(self.updatedAt)
|
|
|
93 |
t_purchase_order.totalCost = self.totalCost
|
|
|
94 |
t_purchase_order.freightCharges = self.freightCharges
|
|
|
95 |
t_purchase_order.realizedCost = self.realizedCost
|
|
|
96 |
t_purchase_order.realizedFreightCharges = self.realizedFreightCharges
|
|
|
97 |
|
|
|
98 |
t_purchase_order.lineitems = [lineitem.to_thrift_object() for lineitem in self.lineitems]
|
| 9416 |
amar.kumar |
99 |
t_purchase_order.taxType = self.taxType
|
| 20025 |
amit.gupta |
100 |
t_purchase_order.buyerId = self.buyerId
|
| 4503 |
mandeep.dh |
101 |
return t_purchase_order
|
|
|
102 |
|
|
|
103 |
def set_po_number(self):
|
| 20025 |
amit.gupta |
104 |
#Changed from SORL to PO after discussion from Rajneesh
|
|
|
105 |
self.poNumber = "PO/" + time.strftime("%m-%y") + "/" + str(self.id)
|