diff --git a/brewman/views/services/voucher/issue.py b/brewman/views/services/voucher/issue.py index cd93baf0..41ba8311 100644 --- a/brewman/views/services/voucher/issue.py +++ b/brewman/views/services/voucher/issue.py @@ -2,13 +2,14 @@ import datetime from decimal import Decimal import uuid 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.validation_exception import ValidationError from brewman.models.voucher import Voucher, VoucherType, Batch, Inventory, Journal __author__ = 'tanshu' + def issue_create_voucher(json, user): 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')) @@ -51,7 +52,7 @@ def issue_create_inventory(voucher, item, batch_consumed): batch.quantity_remaining += quantity 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) DBSession.add(item) @@ -61,9 +62,9 @@ def issue_create_journals(inventories, source, destination): for item in inventories: amount += item.amount 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), - cost_center_id=destination)] + cost_center_id=destination)] 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): i = newInventories[j - 1] 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 - if item.product_id != product.id: - raise ValidationError('Product cannot be changed') + if item.batch_id != batch.id: + raise ValidationError('Product / Batch cannot be changed') new_quantity = round(Decimal(i['Quantity']), 2) old_quantity = round(Decimal(item.quantity), 2) quantity_remaining = round(Decimal(item.batch.quantity_remaining), 2) if new_quantity <= 0: 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, - old_quantity + quantity_remaining)) + old_quantity + quantity_remaining)) if item.batch.name > voucher.date: 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.quantity = new_quantity + item.rate = batch.rate + item.tax = batch.tax + item.discount = batch.discount + newInventories.remove(i) break if not found: @@ -139,7 +144,7 @@ def issue_update_inventory(voucher, newInventories, batch_consumed): if item.batch.quantity_remaining < item.quantity: raise ValidationError( "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 DBSession.delete(item) voucher.inventories.remove(item)