diff --git a/brewman/brewman/models/voucher.py b/brewman/brewman/models/voucher.py index 783505fc..a680dab7 100644 --- a/brewman/brewman/models/voucher.py +++ b/brewman/brewman/models/voucher.py @@ -240,10 +240,12 @@ class Batch(Base): return self.quantity_remaining * self.rate * (1 + self.tax) * (1 - self.discount) @classmethod - def list(cls, name, include_nil): + def list(cls, name, include_nil, date): query = DBSession.query(Batch).join(Batch.product) if not include_nil: query = query.filter(cls.quantity_remaining > 0) + if date is not None: + query = query.filter(cls.name <= date) for item in name.split(): query = query.filter(Product.name.ilike('%' + item + '%')) return query.order_by(Product.name).order_by(cls.name).all() diff --git a/brewman/brewman/static/scripts/issue.js b/brewman/brewman/static/scripts/issue.js index 65c9aa86..cf0f09e3 100644 --- a/brewman/brewman/static/scripts/issue.js +++ b/brewman/brewman/static/scripts/issue.js @@ -225,12 +225,18 @@ var IssueCtrl = ['$scope', '$routeParams', '$location', '$q', 'dateFilter', 'vou $scope.batches = function ($viewValue) { var deferred = $q.defer(); - Batch.autocomplete({term: $viewValue, count: 20}, function (result) { + var params = {term: $viewValue, count: 20}; + if (angular.isDate($scope.voucher.Date)) { + params.date = dateFilter($scope.voucher.Date, 'dd-MMM-yyyy'); + } else { + params.date = $scope.voucher.Date; + } + Batch.autocomplete(params, function (result) { deferred.resolve(result); }); return deferred.promise; - } -}] + }; +}]; IssueCtrl.resolve = { voucher: ['$q', '$route', 'Voucher', function ($q, $route, Voucher) { diff --git a/brewman/brewman/static/scripts/purchase-return.js b/brewman/brewman/static/scripts/purchase-return.js index 417864df..55a9fcd3 100644 --- a/brewman/brewman/static/scripts/purchase-return.js +++ b/brewman/brewman/static/scripts/purchase-return.js @@ -163,15 +163,21 @@ var PurchaseReturnCtrl = ['$scope', '$location', '$q', 'dateFilter', '$modal', ' deferred.resolve(result); }); return deferred.promise; - } + }; $scope.batches = function ($viewValue) { var deferred = $q.defer(); - Batch.autocomplete({term: $viewValue, count: 20}, function (result) { + var params = {term: $viewValue, count: 20}; + if (angular.isDate($scope.voucher.Date)) { + params.date = dateFilter($scope.voucher.Date, 'dd-MMM-yyyy'); + } else { + params.date = $scope.voucher.Date; + } + Batch.autocomplete(params, function (result) { deferred.resolve(result); }); return deferred.promise; - } -}] + }; +}]; PurchaseReturnCtrl.resolve = { voucher: ['$q', '$route', 'Voucher', function ($q, $route, Voucher) { diff --git a/brewman/brewman/views/services/batch.py b/brewman/brewman/views/services/batch.py index e5b87a91..de084a90 100644 --- a/brewman/brewman/views/services/batch.py +++ b/brewman/brewman/views/services/batch.py @@ -1,3 +1,4 @@ +import datetime from pyramid.view import view_config from brewman.models.voucher import Batch @@ -8,8 +9,10 @@ def batch_term(request): filter = filter if filter is not None and filter is not '' else None count = request.GET.get('count', None) count = None if count is None or count == '' else int(count) + date = request.GET.get('date', None) + date = None if count is None or count == '' else datetime.datetime.strptime(date, '%d-%b-%Y') list = [] - for index, item in enumerate(Batch.list(filter, include_nil=False)): + for index, item in enumerate(Batch.list(filter, include_nil=False, date=date)): text = "{0} ({1}) {2:.2f}@{3:.2f} from {4}".format(item.product.name, item.product.units, item.quantity_remaining, item.rate, item.name.strftime('%d-%b-%Y')) list.append({'BatchID': item.id, 'Name': text, 'QuantityRemaining': item.quantity_remaining, diff --git a/brewman/brewman/views/services/voucher/issue.py b/brewman/brewman/views/services/voucher/issue.py index d0ad885b..e4f88483 100644 --- a/brewman/brewman/views/services/voucher/issue.py +++ b/brewman/brewman/views/services/voucher/issue.py @@ -41,7 +41,8 @@ def issue_create_inventory(voucher, item, batch_consumed): raise ValidationError("Quantity of {0} cannot be zero".format(item.product.name)) if batch_consumed == True and quantity > batch.quantity_remaining: raise ValidationError("Quantity available is {0} only".format(batch.quantity_remaining)) - + if batch.name < voucher.date: + raise ValidationError("Batch of {0} was purchased after the issue date".format(item.product.name)) if batch_consumed is None: pass elif batch_consumed: @@ -116,6 +117,8 @@ def issue_update_inventory(voucher, newInventories, batch_consumed): if batch_consumed == 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)) + if item.batch.name < voucher.date: + raise ValidationError("Batch of {0} was purchased after the issue date".format(item.product.name)) if batch_consumed is None: pass diff --git a/brewman/brewman/views/services/voucher/purchase.py b/brewman/brewman/views/services/voucher/purchase.py index 15249894..0f138d64 100644 --- a/brewman/brewman/views/services/voucher/purchase.py +++ b/brewman/brewman/views/services/voucher/purchase.py @@ -10,6 +10,7 @@ from brewman.models.voucher import Voucher, VoucherType, Batch, Inventory, Journ __author__ = 'tanshu' + def purchase_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(json['Type'])) @@ -37,16 +38,15 @@ def purchase_create_inventory(voucher, item, date): tax = round(Decimal(item['Tax']), 5) discount = round(Decimal(item['Discount']), 5) batch = Batch(name=date, product_id=product.id, quantity_remaining=quantity, rate=rate, tax=tax, - discount=discount) + discount=discount) DBSession.add(batch) inventory = Inventory(id=inventory_id, product_id=product.id, batch=batch, quantity=quantity, rate=rate, tax=tax, - discount=discount) + discount=discount) product.price = rate voucher.inventories.append(inventory) DBSession.add(inventory) - def purchase_create_journals(inventories, ledgerID): otherLedger = LedgerBase.by_id(uuid.UUID(ledgerID)) journals = dict() @@ -59,9 +59,9 @@ def purchase_create_journals(inventories, ledgerID): journals[ledger.id].amount += item_amount else: journals[ledger.id] = Journal(debit=1, cost_center_id=ledger.costcenter_id, ledger_id=ledger.id, - amount=item_amount) + amount=item_amount) journals[otherLedger.id] = Journal(debit=-1, cost_center_id=otherLedger.costcenter_id, ledger_id=otherLedger.id, - amount=amount) + amount=amount) return list(journals.values()) @@ -103,7 +103,11 @@ def purchase_update_inventory(voucher, newInventories): rate = round(Decimal(i['Rate']), 2) discount = round(Decimal(i['Discount']), 5) tax = round(Decimal(i['Tax']), 5) - item.batch.name = voucher.date + if voucher.date != item.batch.name: + item.batch.name = voucher.date + if voucher.date < item.batch.date: + # TODO: check for issued products which might have been in a back date + pass item.rate = rate item.batch.rate = rate item.discount = discount @@ -131,9 +135,9 @@ def purchase_update_inventory(voucher, newInventories): tax = round(Decimal(i['Tax']), 5) discount = round(Decimal(i['Discount']), 5) batch = Batch(name=voucher.date, product_id=product.id, quantity_remaining=new_quantity, rate=rate, tax=tax, - discount=discount) + discount=discount) inventory = Inventory(id=None, product_id=product.id, batch=batch, quantity=new_quantity, rate=rate, tax=tax, - discount=discount) + discount=discount) inventory.voucher_id = voucher.id DBSession.add(batch) inventory.batch_id = batch.id @@ -154,9 +158,9 @@ def purchase_update_journals(voucher, journals): journals[ledger.id].amount += item.amount else: journals[ledger.id] = Journal(debit=1, cost_center_id=ledger.costcenter_id, ledger_id=ledger.id, - amount=item.amount) + amount=item.amount) journals[otherLedger.id] = Journal(debit=-1, cost_center_id=otherLedger.costcenter_id, ledger_id=otherLedger.id, - amount=amount) + amount=amount) for i in range(len(voucher.journals), 0, -1): item = voucher.journals[i - 1] if item.ledger_id in journals: diff --git a/brewman/brewman/views/services/voucher/purchase_return.py b/brewman/brewman/views/services/voucher/purchase_return.py index 18ab05c7..212d7937 100644 --- a/brewman/brewman/views/services/voucher/purchase_return.py +++ b/brewman/brewman/views/services/voucher/purchase_return.py @@ -34,6 +34,8 @@ def purchase_return_create_inventory(voucher, item): raise ValidationError("Quantity of {0} cannot be zero".format(item.product.name)) if quantity > batch.quantity_remaining: raise ValidationError("Quantity available is {0} only".format(batch.quantity_remaining)) + if batch.name < voucher.date: + raise ValidationError("Batch of {0} was purchased after the issue date".format(item.product.name)) batch.quantity_remaining -= quantity @@ -91,6 +93,8 @@ def purchase_return_update_inventory(voucher, newInventories, date): if quantity - item.quantity > item.batch.quantity_remaining: raise ValidationError("Maximum quantity available for {0} is {1}".format(item.product.full_name, item.quantity + item.batch.quantity_remaining)) + if item.batch.name < voucher.date: + raise ValidationError("Batch of {0} was purchased after the issue date".format(item.product.name)) item.batch.quantity_remaining -= (quantity - item.quantity) item.quantity = quantity newInventories.remove(i)