diff --git a/erpnext/stock/doctype/batch/batch.py b/erpnext/stock/doctype/batch/batch.py index 931f2435afe..809c7ead4f4 100644 --- a/erpnext/stock/doctype/batch/batch.py +++ b/erpnext/stock/doctype/batch/batch.py @@ -71,9 +71,7 @@ def get_batches_by_oldest(item_code, warehouse): batches = get_batch_qty(item_code = item_code, warehouse = warehouse) batches_dates = [[batch, frappe.get_value('Batch', batch.batch_no, 'expiry_date')] for batch in batches] batches_dates.sort(key=lambda tup: tup[1]) - - sorted_batches = [tup[0] for tup in batches_dates] - return sorted_batches + return batches_dates @frappe.whitelist() def split_batch(batch_no, item_code, warehouse, qty, new_batch_id = None): diff --git a/erpnext/stock/doctype/batch/test_batch.py b/erpnext/stock/doctype/batch/test_batch.py index e63e949bdea..a5dd4ac9d4e 100644 --- a/erpnext/stock/doctype/batch/test_batch.py +++ b/erpnext/stock/doctype/batch/test_batch.py @@ -9,6 +9,7 @@ import unittest from erpnext.stock.doctype.batch.batch import get_batch_qty, UnableToSelectBatchError class TestBatch(unittest.TestCase): + def test_item_has_batch_enabled(self): self.assertRaises(ValidationError, frappe.get_doc({ "doctype": "Batch", @@ -16,14 +17,14 @@ class TestBatch(unittest.TestCase): "item": "_Test Item" }).save) - def make_batch_item(self): + def make_batch_item(self, item_name): from erpnext.stock.doctype.item.test_item import make_item - if not frappe.db.exists('ITEM-BATCH-1'): - make_item('ITEM-BATCH-1', dict(has_batch_no = 1, create_new_batch = 1)) + if not frappe.db.exists(item_name): + make_item(item_name, dict(has_batch_no = 1)) def test_purchase_receipt(self, batch_qty = 100): '''Test automated batch creation from Purchase Receipt''' - self.make_batch_item() + self.make_batch_item('ITEM-BATCH-1') receipt = frappe.get_doc(dict( doctype = 'Purchase Receipt', @@ -47,7 +48,7 @@ class TestBatch(unittest.TestCase): def test_stock_entry_incoming(self): '''Test batch creation via Stock Entry (Production Order)''' - self.make_batch_item() + self.make_batch_item('ITEM-BATCH-1') stock_entry = frappe.get_doc(dict( doctype = 'Stock Entry', @@ -150,4 +151,44 @@ class TestBatch(unittest.TestCase): self.assertEquals(get_batch_qty(receipt.items[0].batch_no, receipt.items[0].warehouse), 78) self.assertEquals(get_batch_qty(new_batch, receipt.items[0].warehouse), 22) + def test_get_batch_qty(self): + '''Test getting batch quantities by batch_numbers, item_code or warehouse''' + from erpnext.stock.doctype.batch.batch import get_batch_qty + self.make_batch_item('ITEM-BATCH-2') + self.make_new_batch_and_entry('ITEM-BATCH-2', 'batch a', '_Test Warehouse - _TC') + self.make_new_batch_and_entry('ITEM-BATCH-2', 'batch b', '_Test Warehouse - _TC') + + self.assertEquals(get_batch_qty(item_code = 'ITEM-BATCH-2', warehouse = '_Test Warehouse - _TC'), + [{'batch_no': u'batch a', 'qty': 90.0}, {'batch_no': u'batch b', 'qty': 90.0}]) + + self.assertEquals(get_batch_qty('batch a', '_Test Warehouse - _TC'), 90) + + def make_new_batch_and_entry(self, item_name, batch_name, warehouse): + '''Make a new stock entry for given target warehouse and batch name of item''' + + if not frappe.db.exists("Batch", batch_name): + batch = frappe.get_doc(dict( + doctype = 'Batch', + item = item_name, + batch_id = batch_name + )).insert(ignore_permissions=True) + batch.submit() + + stock_entry = frappe.get_doc(dict( + doctype = 'Stock Entry', + purpose = 'Material Receipt', + company = '_Test Company', + items = [ + dict( + item_code = item_name, + qty = 90, + t_warehouse = warehouse, + cost_center = 'Main - _TC', + rate = 10, + batch_no = batch_name, + allow_zero_valuation_rate = 1 + ) + ] + )).insert() + stock_entry.submit()