Fix: On update issue, it will update price / discount / tax from the batch (if changed)

This commit is contained in:
Amritanshu 2014-04-08 13:13:24 +05:30
parent 69071182a3
commit d2e08d09cc
1 changed files with 15 additions and 10 deletions

View File

@ -2,13 +2,14 @@ import datetime
from decimal import Decimal from decimal import Decimal
import uuid import uuid
from brewman.models import DBSession from brewman.models import DBSession
from brewman.models.master import Product, CostCenter, LedgerBase from brewman.models.master import CostCenter, LedgerBase
from brewman.models.operations import journals_valid, inventory_valid from brewman.models.operations import journals_valid, inventory_valid
from brewman.models.validation_exception import ValidationError from brewman.models.validation_exception import ValidationError
from brewman.models.voucher import Voucher, VoucherType, Batch, Inventory, Journal from brewman.models.voucher import Voucher, VoucherType, Batch, Inventory, Journal
__author__ = 'tanshu' __author__ = 'tanshu'
def issue_create_voucher(json, user): def issue_create_voucher(json, user):
dt = datetime.datetime.strptime(json['Date'], '%d-%b-%Y') dt = datetime.datetime.strptime(json['Date'], '%d-%b-%Y')
voucher = Voucher(date=dt, narration=json['Narration'], user_id=user.id, type=VoucherType.by_name('Issue')) voucher = Voucher(date=dt, narration=json['Narration'], user_id=user.id, type=VoucherType.by_name('Issue'))
@ -105,16 +106,16 @@ def issue_update_inventory(voucher, newInventories, batch_consumed):
for j in range(len(newInventories), 0, -1): for j in range(len(newInventories), 0, -1):
i = newInventories[j - 1] i = newInventories[j - 1]
if 'InventoryID' in i and item.id == uuid.UUID(i['InventoryID']): if 'InventoryID' in i and item.id == uuid.UUID(i['InventoryID']):
product = Product.by_id(uuid.UUID(i['Product']['ProductID'])) batch = Batch.by_id(uuid.UUID(i['Batch']['BatchID']))
found = True found = True
if item.product_id != product.id: if item.batch_id != batch.id:
raise ValidationError('Product cannot be changed') raise ValidationError('Product / Batch cannot be changed')
new_quantity = round(Decimal(i['Quantity']), 2) new_quantity = round(Decimal(i['Quantity']), 2)
old_quantity = round(Decimal(item.quantity), 2) old_quantity = round(Decimal(item.quantity), 2)
quantity_remaining = round(Decimal(item.batch.quantity_remaining), 2) quantity_remaining = round(Decimal(item.batch.quantity_remaining), 2)
if new_quantity <= 0: if new_quantity <= 0:
raise ValidationError("Quantity of {0} cannot be zero".format(item.product.name)) raise ValidationError("Quantity of {0} cannot be zero".format(item.product.name))
if batch_consumed == True and new_quantity - old_quantity > quantity_remaining: if batch_consumed is True and new_quantity - old_quantity > quantity_remaining:
raise ValidationError("Maximum quantity available for {0} is {1}".format(item.product.full_name, raise ValidationError("Maximum quantity available for {0} is {1}".format(item.product.full_name,
old_quantity + quantity_remaining)) old_quantity + quantity_remaining))
if item.batch.name > voucher.date: if item.batch.name > voucher.date:
@ -128,6 +129,10 @@ def issue_update_inventory(voucher, newInventories, batch_consumed):
item.batch.quantity_remaining += (new_quantity - old_quantity) item.batch.quantity_remaining += (new_quantity - old_quantity)
item.quantity = new_quantity item.quantity = new_quantity
item.rate = batch.rate
item.tax = batch.tax
item.discount = batch.discount
newInventories.remove(i) newInventories.remove(i)
break break
if not found: if not found: