Skip to content

Commit

Permalink
#220 added workaround checks for order items when their budget items …
Browse files Browse the repository at this point in the history
…have been deleted. Quantities and costs remain in the order, but the name displays as 'Item Deleted'
  • Loading branch information
nvernooy committed Dec 19, 2015
1 parent 20fde3e commit 57d5f59
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 34 deletions.
40 changes: 33 additions & 7 deletions src/optimate.app/optimate/app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1560,7 +1560,7 @@ def __repr__(self):


class Supplier(Base):
"""A table containing the data relavent to a supplier of Optimate
"""A table containing the data relevant to a supplier of Optimate
"""
__tablename__ = 'Supplier'
ID = Column(Integer, primary_key=True)
Expand Down Expand Up @@ -1630,7 +1630,7 @@ def __repr__(self):


class Order(Base):
""" A table containing the data relavent to an order of Optimate
""" A table containing the data relevant to an order of Optimate
"""
__tablename__ = 'Order'
ID = Column(Integer, primary_key=True)
Expand Down Expand Up @@ -1791,18 +1791,44 @@ def Discount(self, discount):
float(self.Subtotal)*self.Discount/100.0
) * (1 + self.VAT/100.0)

@property
def Name(self):
""" Return the BudgetItem Name, if it exists
"""
if self.BudgetItem:
return self.BudgetItem.Name
else:
return 'Item Deleted'

@property
def Unit(self):
""" Return the BudgetItem Unit, if it exists
"""
if self.BudgetItem:
return self.BudgetItem.Unit
else:
return ''

def dict(self):
""" Override the dict function
"""
vatcost = Decimal(float(self.Total) - (float(self.Subtotal)
- float(self.Subtotal)*self.Discount/100)
).quantize(Decimal('.01'))
return {'Name': self.BudgetItem.Name,
'ParentName': self.BudgetItem.Parent.Name,
'ID': self.BudgetItemID,
'id': self.BudgetItemID,

if self.BudgetItem:
parentname = self.BudgetItem.Parent.Name
checkedid = self.BudgetItemID
else:
parentname = ''
checkedid = 'DELETED' + str(self.ID)

return {'Name': self.Name,
'ParentName': parentname,
'ID': checkedid,
'id': checkedid,
'Quantity': self.Quantity,
'Unit': self.BudgetItem.Unit,
'Unit': self.Unit,
'Rate': str(self.Rate),
'VAT': self.VAT,
'Subtotal': str(self.Subtotal),
Expand Down
12 changes: 9 additions & 3 deletions src/optimate.app/optimate/app/reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,10 @@ def order(request):
for orderitem in order.OrderItems:
# get the resource id
data = orderitem.dict()
data['ResourceID'] = orderitem.BudgetItem.ResourceID
if orderitem.BudgetItem:
data['ResourceID'] = orderitem.BudgetItem.ResourceID
else:
data['ResourceID'] = 'DELETED' + str(orderitem.ID)
orderitems.append(data)

# sort by resourceid
Expand Down Expand Up @@ -1580,7 +1583,10 @@ def excelorder(request):
for orderitem in order.OrderItems:
# get the resource id
data = orderitem.dict()
data['ResourceID'] = orderitem.BudgetItem.ResourceID
if orderitem.BudgetItem:
data['ResourceID'] = orderitem.BudgetItem.ResourceID
else:
data['ResourceID'] = 'DELETED' + str(orderitem.ID)
orderitems.append(data)

# sort by resourceid
Expand Down Expand Up @@ -2320,7 +2326,7 @@ def excelcashflow(request):
for item in rootitems:
data = item.dict()
budgettotal = item.BudgetGroup.Total
if item.Total > 0:
if budgettotal > 0:
data['PercentageComplete'] = float(item.Total/budgettotal)*100
else:
data['PercentageComplete'] = 0
Expand Down
55 changes: 31 additions & 24 deletions src/optimate.app/optimate/app/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,7 @@ def node_budgetgroups(request):
childrenlist.append(data)
# get data and append parents valuation items to parent list
else:
# check if the bg has child budget groups
# check if the budgetgroup has child budget groups
level = '1'
children = DBSession.query(BudgetGroup
).filter_by(ParentID=bg.ID).first()
Expand Down Expand Up @@ -2023,8 +2023,8 @@ def orderview(request):
# update the budgetitem ordered amounts
for orderitem in deletethis.OrderItems:
if orderitem.BudgetItem:
orderitem.BudgetItem.Ordered = (orderitem.BudgetItem.Ordered -
orderitem.Total)
orderitem.BudgetItem.Ordered = (orderitem.BudgetItem.Ordered
- orderitem.Total)
DBSession.delete(deletethis)
transaction.commit()
except IntegrityError:
Expand Down Expand Up @@ -2085,7 +2085,8 @@ def orderview(request):
neworder.resetTotal()
# update the budgetitem ordered amounts
for orderitem in neworder.OrderItems:
orderitem.BudgetItem.Ordered += orderitem.Total
if orderitem.BudgetItem:
orderitem.BudgetItem.Ordered += orderitem.Total
return neworder.dict()

# if the method is put, edit an existing order
Expand Down Expand Up @@ -2154,7 +2155,8 @@ def orderview(request):
# update the budget item ordered amount
bi = DBSession.query(BudgetItem).filter_by(
ID=budgetitem['ID']).first()
bi.Ordered +=neworderitem.Total
if bi:
bi.Ordered +=neworderitem.Total
else:
# otherwise update the item and remove the id from the list
orderitemid = iddict[budgetitem['ID']]
Expand All @@ -2174,12 +2176,14 @@ def orderview(request):
# update the budget item ordered amount
bi = DBSession.query(BudgetItem).filter_by(
ID=budgetitem['ID']).first()
bi.Ordered = bi.Ordered - oldtotal + orderitem.Total
if bi:
bi.Ordered = bi.Ordered - oldtotal + orderitem.Total
del iddict[budgetitem['ID']]
# delete the leftover id's and update the ordered total
for oldid in iddict.values():
deletethis = DBSession.query(OrderItem).filter_by(ID=oldid).first()
deletethis.BudgetItem.Ordered -= deletethis.Total
if deletethis.BudgetItem:
deletethis.BudgetItem.Ordered -= deletethis.Total
qry = DBSession.delete(deletethis)

transaction.commit()
Expand Down Expand Up @@ -2831,11 +2835,12 @@ def invoiceview(request):
ordertotal = order.Total
invoicetotal = deletethis.Total
for orderitem in order.OrderItems:
if ordertotal > 0:
proportion = orderitem.Total/ordertotal
orderitem.BudgetItem.Invoiced -= invoicetotal * proportion
else:
orderitem.BudgetItem.Invoiced = 0
if orderitem.BudgetItem:
if ordertotal > 0:
proportion = orderitem.Total/ordertotal
orderitem.BudgetItem.Invoiced -= invoicetotal * proportion
else:
orderitem.BudgetItem.Invoiced = 0

DBSession.delete(deletethis)
transaction.commit()
Expand Down Expand Up @@ -2867,12 +2872,13 @@ def invoiceview(request):
ordertotal = order.Total

for orderitem in order.OrderItems:
if ordertotal > 0:
proportion = orderitem.Total/ordertotal
orderitem.BudgetItem.Invoiced += invoicetotal * proportion
else:
if not orderitem.BudgetItem.Invoiced:
orderitem.BudgetItem.Invoiced = 0
if orderitem.BudgetItem:
if ordertotal > 0:
proportion = orderitem.Total/ordertotal
orderitem.BudgetItem.Invoiced += invoicetotal * proportion
else:
if not orderitem.BudgetItem.Invoiced:
orderitem.BudgetItem.Invoiced = 0

newinvoice = Invoice(OrderID=orderid,
InvoiceNumber=request.json_body['InvoiceNumber'],
Expand Down Expand Up @@ -2917,12 +2923,13 @@ def invoiceview(request):
if oldtotal != newtotal:
order = DBSession.query(Order).filter_by(ID=invoice.OrderID).first()
for orderitem in order.OrderItems:
if order.Total > 0:
proportion = orderitem.Total/order.Total
difference = oldtotal * proportion - newtotal * proportion
orderitem.BudgetItem.Invoiced += difference
else:
orderitem.BudgetItem.Invoiced = 0
if orderitem.BudgetItem:
if order.Total > 0:
proportion = orderitem.Total/order.Total
difference = oldtotal * proportion - newtotal * proportion
orderitem.BudgetItem.Invoiced += difference
else:
orderitem.BudgetItem.Invoiced = 0
transaction.commit()
# return the edited invoice
invoice = DBSession.query(Invoice).filter_by(
Expand Down

0 comments on commit 57d5f59

Please sign in to comment.