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'))
@ -51,7 +52,7 @@ def issue_create_inventory(voucher, item, batch_consumed):
batch.quantity_remaining += quantity batch.quantity_remaining += quantity
item = Inventory(id=inventory_id, product_id=batch.product.id, quantity=quantity, rate=batch.rate, tax=batch.tax, item = Inventory(id=inventory_id, product_id=batch.product.id, quantity=quantity, rate=batch.rate, tax=batch.tax,
discount=batch.discount, batch=batch) discount=batch.discount, batch=batch)
voucher.inventories.append(item) voucher.inventories.append(item)
DBSession.add(item) DBSession.add(item)
@ -61,9 +62,9 @@ def issue_create_journals(inventories, source, destination):
for item in inventories: for item in inventories:
amount += item.amount amount += item.amount
return [Journal(debit=-1, ledger_id=LedgerBase.all_purchases(), amount=round(amount, 2), return [Journal(debit=-1, ledger_id=LedgerBase.all_purchases(), amount=round(amount, 2),
cost_center_id=source), cost_center_id=source),
Journal(debit=1, ledger_id=LedgerBase.all_purchases(), amount=round(amount, 2), Journal(debit=1, ledger_id=LedgerBase.all_purchases(), amount=round(amount, 2),
cost_center_id=destination)] cost_center_id=destination)]
def issue_update_voucher(voucher, json, user): def issue_update_voucher(voucher, json, user):
@ -105,18 +106,18 @@ 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:
raise ValidationError("Batch of {0} was purchased after the issue date".format(item.product.name)) raise ValidationError("Batch of {0} was purchased after the issue date".format(item.product.name))
@ -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:
@ -139,7 +144,7 @@ def issue_update_inventory(voucher, newInventories, batch_consumed):
if item.batch.quantity_remaining < item.quantity: if item.batch.quantity_remaining < item.quantity:
raise ValidationError( raise ValidationError(
"Product {0} cannot be removed, minimum quantity is {1}".format(item.product.name, "Product {0} cannot be removed, minimum quantity is {1}".format(item.product.name,
item.batch.quantity_remaining)) item.batch.quantity_remaining))
item.batch.quantity_remaining -= item.quantity item.batch.quantity_remaining -= item.quantity
DBSession.delete(item) DBSession.delete(item)
voucher.inventories.remove(item) voucher.inventories.remove(item)