mirror of
https://github.com/frappe/erpnext.git
synced 2026-06-01 03:09:09 +00:00
fix: set batch qty in batch master and test cases
This commit is contained in:
@@ -5,5 +5,7 @@ def execute():
|
|||||||
frappe.reload_doc("stock", "doctype", "batch")
|
frappe.reload_doc("stock", "doctype", "batch")
|
||||||
|
|
||||||
for batch in frappe.get_all("Batch", fields=["name", "batch_id"]):
|
for batch in frappe.get_all("Batch", fields=["name", "batch_id"]):
|
||||||
batch_qty = frappe.db.get_value("Stock Ledger Entry", {"docstatus": 1, "batch_no": batch.batch_id}, "sum(actual_qty)") or 0.0
|
batch_qty = frappe.db.get_value("Stock Ledger Entry",
|
||||||
|
{"docstatus": 1, "batch_no": batch.batch_id, "is_cancelled": "No"},
|
||||||
|
"sum(actual_qty)") or 0.0
|
||||||
frappe.db.set_value("Batch", batch.name, "batch_qty", batch_qty, update_modified=False)
|
frappe.db.set_value("Batch", batch.name, "batch_qty", batch_qty, update_modified=False)
|
||||||
|
|||||||
@@ -111,15 +111,11 @@ class Batch(Document):
|
|||||||
|
|
||||||
def validate(self):
|
def validate(self):
|
||||||
self.item_has_batch_enabled()
|
self.item_has_batch_enabled()
|
||||||
self.calculate_batch_qty()
|
|
||||||
|
|
||||||
def item_has_batch_enabled(self):
|
def item_has_batch_enabled(self):
|
||||||
if frappe.db.get_value("Item", self.item, "has_batch_no") == 0:
|
if frappe.db.get_value("Item", self.item, "has_batch_no") == 0:
|
||||||
frappe.throw(_("The selected item cannot have Batch"))
|
frappe.throw(_("The selected item cannot have Batch"))
|
||||||
|
|
||||||
def calculate_batch_qty(self):
|
|
||||||
self.batch_qty = frappe.db.get_value("Stock Ledger Entry", {"docstatus": 1, "batch_no": self.batch_id}, "sum(actual_qty)")
|
|
||||||
|
|
||||||
def before_save(self):
|
def before_save(self):
|
||||||
has_expiry_date, shelf_life_in_days = frappe.db.get_value('Item', self.item, ['has_expiry_date', 'shelf_life_in_days'])
|
has_expiry_date, shelf_life_in_days = frappe.db.get_value('Item', self.item, ['has_expiry_date', 'shelf_life_in_days'])
|
||||||
if not self.expiry_date and has_expiry_date and shelf_life_in_days:
|
if not self.expiry_date and has_expiry_date and shelf_life_in_days:
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ from frappe.exceptions import ValidationError
|
|||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
from erpnext.stock.doctype.batch.batch import get_batch_qty, UnableToSelectBatchError, get_batch_no
|
from erpnext.stock.doctype.batch.batch import get_batch_qty, UnableToSelectBatchError, get_batch_no
|
||||||
from frappe.utils import cint
|
from frappe.utils import cint, flt
|
||||||
from erpnext.stock.doctype.purchase_receipt.test_purchase_receipt import set_perpetual_inventory
|
from erpnext.stock.doctype.purchase_receipt.test_purchase_receipt import set_perpetual_inventory
|
||||||
|
|
||||||
class TestBatch(unittest.TestCase):
|
class TestBatch(unittest.TestCase):
|
||||||
@@ -35,12 +35,13 @@ class TestBatch(unittest.TestCase):
|
|||||||
receipt = frappe.get_doc(dict(
|
receipt = frappe.get_doc(dict(
|
||||||
doctype='Purchase Receipt',
|
doctype='Purchase Receipt',
|
||||||
supplier='_Test Supplier',
|
supplier='_Test Supplier',
|
||||||
|
company='_Test Company',
|
||||||
items=[
|
items=[
|
||||||
dict(
|
dict(
|
||||||
item_code='ITEM-BATCH-1',
|
item_code='ITEM-BATCH-1',
|
||||||
qty=batch_qty,
|
qty=batch_qty,
|
||||||
rate=10,
|
rate=10,
|
||||||
warehouse= 'Stores - WP'
|
warehouse= 'Stores - _TC'
|
||||||
)
|
)
|
||||||
]
|
]
|
||||||
)).insert()
|
)).insert()
|
||||||
@@ -175,6 +176,18 @@ class TestBatch(unittest.TestCase):
|
|||||||
|
|
||||||
self.assertEqual(get_batch_qty('batch a', '_Test Warehouse - _TC'), 90)
|
self.assertEqual(get_batch_qty('batch a', '_Test Warehouse - _TC'), 90)
|
||||||
|
|
||||||
|
def test_total_batch_qty(self):
|
||||||
|
self.make_batch_item('ITEM-BATCH-3')
|
||||||
|
existing_batch_qty = flt(frappe.db.get_value("Batch", "B100", "batch_qty"))
|
||||||
|
stock_entry = self.make_new_batch_and_entry('ITEM-BATCH-3', 'B100', '_Test Warehouse - _TC')
|
||||||
|
|
||||||
|
current_batch_qty = flt(frappe.db.get_value("Batch", "B100", "batch_qty"))
|
||||||
|
self.assertEqual(current_batch_qty, existing_batch_qty + 90)
|
||||||
|
|
||||||
|
stock_entry.cancel()
|
||||||
|
current_batch_qty = flt(frappe.db.get_value("Batch", "B100", "batch_qty"))
|
||||||
|
self.assertEqual(current_batch_qty, existing_batch_qty)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def make_new_batch_and_entry(cls, item_name, batch_name, warehouse):
|
def make_new_batch_and_entry(cls, item_name, batch_name, warehouse):
|
||||||
'''Make a new stock entry for given target warehouse and batch name of item'''
|
'''Make a new stock entry for given target warehouse and batch name of item'''
|
||||||
@@ -208,6 +221,8 @@ class TestBatch(unittest.TestCase):
|
|||||||
stock_entry.insert()
|
stock_entry.insert()
|
||||||
stock_entry.submit()
|
stock_entry.submit()
|
||||||
|
|
||||||
|
return stock_entry
|
||||||
|
|
||||||
def test_batch_name_with_naming_series(self):
|
def test_batch_name_with_naming_series(self):
|
||||||
stock_settings = frappe.get_single('Stock Settings')
|
stock_settings = frappe.get_single('Stock Settings')
|
||||||
use_naming_series = cint(stock_settings.use_naming_series)
|
use_naming_series = cint(stock_settings.use_naming_series)
|
||||||
|
|||||||
@@ -43,12 +43,11 @@ class StockLedgerEntry(Document):
|
|||||||
from erpnext.stock.doctype.serial_no.serial_no import process_serial_no
|
from erpnext.stock.doctype.serial_no.serial_no import process_serial_no
|
||||||
process_serial_no(self)
|
process_serial_no(self)
|
||||||
|
|
||||||
def on_cancel(self):
|
|
||||||
self.calculate_batch_qty()
|
|
||||||
|
|
||||||
def calculate_batch_qty(self):
|
def calculate_batch_qty(self):
|
||||||
if self.batch_no:
|
if self.batch_no:
|
||||||
batch_qty = frappe.db.get_value("Stock Ledger Entry", {"docstatus": 1, "batch_no": self.batch_no}, "sum(actual_qty)")
|
batch_qty = frappe.db.get_value("Stock Ledger Entry",
|
||||||
|
{"docstatus": 1, "batch_no": self.batch_no, "is_cancelled": "No"},
|
||||||
|
"sum(actual_qty)") or 0
|
||||||
frappe.db.set_value("Batch", self.batch_no, "batch_qty", batch_qty)
|
frappe.db.set_value("Batch", self.batch_no, "batch_qty", batch_qty)
|
||||||
|
|
||||||
#check for item quantity available in stock
|
#check for item quantity available in stock
|
||||||
|
|||||||
Reference in New Issue
Block a user