fix: travis

This commit is contained in:
Rohit Waghchaure 2023-03-31 09:03:54 +05:30
parent f704eb7581
commit d3ceb07936
29 changed files with 1238 additions and 744 deletions

View File

@ -703,6 +703,9 @@ class GrossProfitGenerator(object):
} }
) )
if row.serial_and_batch_bundle:
args.update({"serial_and_batch_bundle": row.serial_and_batch_bundle})
average_buying_rate = get_incoming_rate(args) average_buying_rate = get_incoming_rate(args)
self.average_buying_rate[item_code] = flt(average_buying_rate) self.average_buying_rate[item_code] = flt(average_buying_rate)
@ -805,7 +808,7 @@ class GrossProfitGenerator(object):
`tabSales Invoice Item`.delivery_note, `tabSales Invoice Item`.stock_qty as qty, `tabSales Invoice Item`.delivery_note, `tabSales Invoice Item`.stock_qty as qty,
`tabSales Invoice Item`.base_net_rate, `tabSales Invoice Item`.base_net_amount, `tabSales Invoice Item`.base_net_rate, `tabSales Invoice Item`.base_net_amount,
`tabSales Invoice Item`.name as "item_row", `tabSales Invoice`.is_return, `tabSales Invoice Item`.name as "item_row", `tabSales Invoice`.is_return,
`tabSales Invoice Item`.cost_center `tabSales Invoice Item`.cost_center, `tabSales Invoice Item`.serial_and_batch_bundle
{sales_person_cols} {sales_person_cols}
{payment_term_cols} {payment_term_cols}
from from

View File

@ -92,7 +92,7 @@ class BuyingController(SubcontractingController):
return return
for item in self.get("items"): for item in self.get("items"):
if item.get(field) and not item.serial_and_batch_bundle: if item.get(field) and not item.serial_and_batch_bundle and bundle_ids.get(item.get(field)):
item.serial_and_batch_bundle = self.make_package_for_transfer( item.serial_and_batch_bundle = self.make_package_for_transfer(
bundle_ids.get(item.get(field)), bundle_ids.get(item.get(field)),
item.from_warehouse, item.from_warehouse,
@ -557,6 +557,7 @@ class BuyingController(SubcontractingController):
if self.get("is_old_subcontracting_flow"): if self.get("is_old_subcontracting_flow"):
self.make_sl_entries_for_supplier_warehouse(sl_entries) self.make_sl_entries_for_supplier_warehouse(sl_entries)
self.make_sl_entries( self.make_sl_entries(
sl_entries, sl_entries,
allow_negative_stock=allow_negative_stock, allow_negative_stock=allow_negative_stock,

View File

@ -409,6 +409,7 @@ def make_return_doc(doctype: str, source_name: str, target_doc=None):
"type_of_transaction": type_of_transaction, "type_of_transaction": type_of_transaction,
"serial_and_batch_bundle": source_doc.serial_and_batch_bundle, "serial_and_batch_bundle": source_doc.serial_and_batch_bundle,
"returned_against": source_doc.name, "returned_against": source_doc.name,
"item_code": source_doc.item_code,
} }
) )
@ -431,6 +432,7 @@ def make_return_doc(doctype: str, source_name: str, target_doc=None):
"type_of_transaction": type_of_transaction, "type_of_transaction": type_of_transaction,
"serial_and_batch_bundle": source_doc.rejected_serial_and_batch_bundle, "serial_and_batch_bundle": source_doc.rejected_serial_and_batch_bundle,
"returned_against": source_doc.name, "returned_against": source_doc.name,
"item_code": source_doc.item_code,
} }
) )

View File

@ -302,7 +302,8 @@ class SellingController(StockController):
"item_code": p.item_code, "item_code": p.item_code,
"qty": flt(p.qty), "qty": flt(p.qty),
"uom": p.uom, "uom": p.uom,
"serial_and_batch_bundle": p.serial_and_batch_bundle, "serial_and_batch_bundle": p.serial_and_batch_bundle
or get_serial_and_batch_bundle(p, self),
"name": d.name, "name": d.name,
"target_warehouse": p.target_warehouse, "target_warehouse": p.target_warehouse,
"company": self.company, "company": self.company,
@ -338,6 +339,7 @@ class SellingController(StockController):
} }
) )
) )
return il return il
def has_product_bundle(self, item_code): def has_product_bundle(self, item_code):
@ -511,6 +513,7 @@ class SellingController(StockController):
"actual_qty": -1 * flt(item_row.qty), "actual_qty": -1 * flt(item_row.qty),
"incoming_rate": item_row.incoming_rate, "incoming_rate": item_row.incoming_rate,
"recalculate_rate": cint(self.is_return), "recalculate_rate": cint(self.is_return),
"serial_and_batch_bundle": item_row.serial_and_batch_bundle,
}, },
) )
if item_row.target_warehouse and not cint(self.is_return): if item_row.target_warehouse and not cint(self.is_return):
@ -674,3 +677,40 @@ def set_default_income_account_for_item(obj):
if d.item_code: if d.item_code:
if getattr(d, "income_account", None): if getattr(d, "income_account", None):
set_item_default(d.item_code, obj.company, "income_account", d.income_account) set_item_default(d.item_code, obj.company, "income_account", d.income_account)
def get_serial_and_batch_bundle(child, parent):
from erpnext.stock.serial_batch_bundle import SerialBatchCreation
if not frappe.db.get_single_value(
"Stock Settings", "auto_create_serial_and_batch_bundle_for_outward"
):
return
item_details = frappe.db.get_value(
"Item", child.item_code, ["has_serial_no", "has_batch_no"], as_dict=1
)
if not item_details.has_serial_no and not item_details.has_batch_no:
return
sn_doc = SerialBatchCreation(
{
"item_code": child.item_code,
"warehouse": child.warehouse,
"voucher_type": parent.doctype,
"voucher_no": parent.name,
"voucher_detail_no": child.name,
"posting_date": parent.posting_date,
"posting_time": parent.posting_time,
"qty": child.qty,
"type_of_transaction": "Outward" if child.qty > 0 else "Inward",
"company": parent.company,
"do_not_submit": "True",
}
)
doc = sn_doc.make_serial_and_batch_bundle()
child.db_set("serial_and_batch_bundle", doc.name)
return doc.name

View File

@ -372,15 +372,26 @@ class StockController(AccountsController):
row.db_set("serial_and_batch_bundle", None) row.db_set("serial_and_batch_bundle", None)
def set_serial_and_batch_bundle(self, table_name=None): def set_serial_and_batch_bundle(self, table_name=None, ignore_validate=False):
if not table_name: if not table_name:
table_name = "items" table_name = "items"
QTY_FIELD = {
"serial_and_batch_bundle": "qty",
"current_serial_and_batch_bundle": "current_qty",
"rejected_serial_and_batch_bundle": "rejected_qty",
}
for row in self.get(table_name): for row in self.get(table_name):
if row.get("serial_and_batch_bundle"): for field in [
frappe.get_doc( "serial_and_batch_bundle",
"Serial and Batch Bundle", row.serial_and_batch_bundle "current_serial_and_batch_bundle",
).set_serial_and_batch_values(self, row) "rejected_serial_and_batch_bundle",
]:
if row.get(field):
frappe.get_doc("Serial and Batch Bundle", row.get(field)).set_serial_and_batch_values(
self, row, qty_field=QTY_FIELD[field]
)
def make_package_for_transfer( def make_package_for_transfer(
self, serial_and_batch_bundle, warehouse, type_of_transaction=None, do_not_submit=None self, serial_and_batch_bundle, warehouse, type_of_transaction=None, do_not_submit=None
@ -410,11 +421,7 @@ class StockController(AccountsController):
bundle_doc.calculate_qty_and_amount() bundle_doc.calculate_qty_and_amount()
bundle_doc.flags.ignore_permissions = True bundle_doc.flags.ignore_permissions = True
bundle_doc.save(ignore_permissions=True)
if not do_not_submit:
bundle_doc.submit()
else:
bundle_doc.save(ignore_permissions=True)
return bundle_doc.name return bundle_doc.name

View File

@ -53,7 +53,7 @@ class SubcontractingController(StockController):
self.create_raw_materials_supplied() self.create_raw_materials_supplied()
for table_field in ["items", "supplied_items"]: for table_field in ["items", "supplied_items"]:
if self.get(table_field): if self.get(table_field):
self.set_total_in_words(table_field) self.set_serial_and_batch_bundle(table_field)
else: else:
super(SubcontractingController, self).validate() super(SubcontractingController, self).validate()

View File

@ -33,7 +33,7 @@ from erpnext.manufacturing.doctype.manufacturing_settings.manufacturing_settings
) )
from erpnext.stock.doctype.batch.batch import make_batch from erpnext.stock.doctype.batch.batch import make_batch
from erpnext.stock.doctype.item.item import get_item_defaults, validate_end_of_life from erpnext.stock.doctype.item.item import get_item_defaults, validate_end_of_life
from erpnext.stock.doctype.serial_no.serial_no import get_auto_serial_nos, get_serial_nos from erpnext.stock.doctype.serial_no.serial_no import get_available_serial_nos, get_serial_nos
from erpnext.stock.stock_balance import get_planned_qty, update_bin_qty from erpnext.stock.stock_balance import get_planned_qty, update_bin_qty
from erpnext.stock.utils import get_bin, get_latest_stock_qty, validate_warehouse_company from erpnext.stock.utils import get_bin, get_latest_stock_qty, validate_warehouse_company
from erpnext.utilities.transaction_base import validate_uom_is_integer from erpnext.utilities.transaction_base import validate_uom_is_integer
@ -450,7 +450,7 @@ class WorkOrder(Document):
serial_nos = [] serial_nos = []
if item_details.serial_no_series: if item_details.serial_no_series:
serial_nos = get_auto_serial_nos(item_details.serial_no_series, self.qty) serial_nos = get_available_serial_nos(item_details.serial_no_series, self.qty)
if not serial_nos: if not serial_nos:
return return

View File

@ -37,7 +37,7 @@ class DeprecatedSerialNoValuation:
incoming_values = 0.0 incoming_values = 0.0
for d in all_serial_nos: for d in all_serial_nos:
if d.company == self.sle.company: if d.company == self.sle.company:
self.serial_no_incoming_rate[d.name] = flt(d.purchase_rate) self.serial_no_incoming_rate[d.name] += flt(d.purchase_rate)
incoming_values += flt(d.purchase_rate) incoming_values += flt(d.purchase_rate)
# Get rate for serial nos which has been transferred to other company # Get rate for serial nos which has been transferred to other company
@ -49,6 +49,7 @@ class DeprecatedSerialNoValuation:
from `tabStock Ledger Entry` from `tabStock Ledger Entry`
where where
company = %s company = %s
and serial_and_batch_bundle IS NULL
and actual_qty > 0 and actual_qty > 0
and is_cancelled = 0 and is_cancelled = 0
and (serial_no = %s and (serial_no = %s
@ -62,7 +63,7 @@ class DeprecatedSerialNoValuation:
(self.sle.company, serial_no, serial_no + "\n%", "%\n" + serial_no, "%\n" + serial_no + "\n%"), (self.sle.company, serial_no, serial_no + "\n%", "%\n" + serial_no, "%\n" + serial_no + "\n%"),
) )
self.serial_no_incoming_rate[serial_no] = flt(incoming_rate[0][0]) if incoming_rate else 0 self.serial_no_incoming_rate[serial_no] += flt(incoming_rate[0][0]) if incoming_rate else 0
incoming_values += self.serial_no_incoming_rate[serial_no] incoming_values += self.serial_no_incoming_rate[serial_no]
return incoming_values return incoming_values

View File

@ -47,6 +47,8 @@ frappe.ui.form.on('Batch', {
return; return;
} }
debugger
const section = frm.dashboard.add_section('', __("Stock Levels")); const section = frm.dashboard.add_section('', __("Stock Levels"));
// sort by qty // sort by qty

View File

@ -9,7 +9,7 @@ from frappe import _
from frappe.model.document import Document from frappe.model.document import Document
from frappe.model.naming import make_autoname, revert_series_if_last from frappe.model.naming import make_autoname, revert_series_if_last
from frappe.query_builder.functions import CurDate, Sum from frappe.query_builder.functions import CurDate, Sum
from frappe.utils import cint, flt, get_link_to_form from frappe.utils import cint, flt, get_link_to_form, nowtime, today
from frappe.utils.data import add_days from frappe.utils.data import add_days
from frappe.utils.jinja import render_template from frappe.utils.jinja import render_template
@ -184,13 +184,15 @@ def get_batch_qty(
) )
batchwise_qty = defaultdict(float) batchwise_qty = defaultdict(float)
kwargs = frappe._dict({ kwargs = frappe._dict(
"item_code": item_code, {
"warehouse": warehouse, "item_code": item_code,
"posting_date": posting_date, "warehouse": warehouse,
"posting_time": posting_time, "posting_date": posting_date,
"batch_no": batch_no "posting_time": posting_time,
}) "batch_no": batch_no,
}
)
batches = get_auto_batch_nos(kwargs) batches = get_auto_batch_nos(kwargs)
@ -216,13 +218,37 @@ def get_batches_by_oldest(item_code, warehouse):
@frappe.whitelist() @frappe.whitelist()
def split_batch(batch_no, item_code, warehouse, qty, new_batch_id=None): def split_batch(batch_no, item_code, warehouse, qty, new_batch_id=None):
"""Split the batch into a new batch""" """Split the batch into a new batch"""
batch = frappe.get_doc(dict(doctype="Batch", item=item_code, batch_id=new_batch_id)).insert() batch = frappe.get_doc(dict(doctype="Batch", item=item_code, batch_id=new_batch_id)).insert()
qty = flt(qty)
company = frappe.db.get_value( company = frappe.db.get_value("Warehouse", warehouse, "company")
"Stock Ledger Entry",
dict(item_code=item_code, batch_no=batch_no, warehouse=warehouse), from_bundle_id = make_batch_bundle(
["company"], frappe._dict(
{
"item_code": item_code,
"warehouse": warehouse,
"batches": frappe._dict({batch_no: qty}),
"company": company,
"type_of_transaction": "Outward",
"qty": qty,
}
)
)
to_bundle_id = make_batch_bundle(
frappe._dict(
{
"item_code": item_code,
"warehouse": warehouse,
"batches": frappe._dict({batch.name: qty}),
"company": company,
"type_of_transaction": "Inward",
"qty": qty,
}
)
) )
stock_entry = frappe.get_doc( stock_entry = frappe.get_doc(
@ -231,8 +257,12 @@ def split_batch(batch_no, item_code, warehouse, qty, new_batch_id=None):
purpose="Repack", purpose="Repack",
company=company, company=company,
items=[ items=[
dict(item_code=item_code, qty=float(qty or 0), s_warehouse=warehouse, batch_no=batch_no), dict(
dict(item_code=item_code, qty=float(qty or 0), t_warehouse=warehouse, batch_no=batch.name), item_code=item_code, qty=qty, s_warehouse=warehouse, serial_and_batch_bundle=from_bundle_id
),
dict(
item_code=item_code, qty=qty, t_warehouse=warehouse, serial_and_batch_bundle=to_bundle_id
),
], ],
) )
) )
@ -243,6 +273,29 @@ def split_batch(batch_no, item_code, warehouse, qty, new_batch_id=None):
return batch.name return batch.name
def make_batch_bundle(kwargs):
from erpnext.stock.serial_batch_bundle import SerialBatchCreation
return (
SerialBatchCreation(
{
"item_code": kwargs.item_code,
"warehouse": kwargs.warehouse,
"posting_date": today(),
"posting_time": nowtime(),
"voucher_type": "Stock Entry",
"qty": flt(kwargs.qty),
"type_of_transaction": kwargs.type_of_transaction,
"company": kwargs.company,
"batches": kwargs.batches,
"do_not_submit": True,
}
)
.make_serial_and_batch_bundle()
.name
)
def get_batches(item_code, warehouse, qty=1, throw=False, serial_no=None): def get_batches(item_code, warehouse, qty=1, throw=False, serial_no=None):
from erpnext.stock.doctype.serial_no.serial_no import get_serial_nos from erpnext.stock.doctype.serial_no.serial_no import get_serial_nos

View File

@ -7,7 +7,7 @@ def get_data():
"transactions": [ "transactions": [
{"label": _("Buy"), "items": ["Purchase Invoice", "Purchase Receipt"]}, {"label": _("Buy"), "items": ["Purchase Invoice", "Purchase Receipt"]},
{"label": _("Sell"), "items": ["Sales Invoice", "Delivery Note"]}, {"label": _("Sell"), "items": ["Sales Invoice", "Delivery Note"]},
{"label": _("Move"), "items": ["Stock Entry"]}, {"label": _("Move"), "items": ["Serial and Batch Bundle"]},
{"label": _("Quality"), "items": ["Quality Inspection"]}, {"label": _("Quality"), "items": ["Quality Inspection"]},
], ],
} }

View File

@ -10,12 +10,15 @@ from frappe.utils import cint, flt
from frappe.utils.data import add_to_date, getdate from frappe.utils.data import add_to_date, getdate
from erpnext.accounts.doctype.purchase_invoice.test_purchase_invoice import make_purchase_invoice from erpnext.accounts.doctype.purchase_invoice.test_purchase_invoice import make_purchase_invoice
from erpnext.stock.doctype.batch.batch import get_batch_no, get_batch_qty from erpnext.stock.doctype.batch.batch import get_batch_qty
from erpnext.stock.doctype.item.test_item import make_item from erpnext.stock.doctype.item.test_item import make_item
from erpnext.stock.doctype.purchase_receipt.test_purchase_receipt import make_purchase_receipt from erpnext.stock.doctype.purchase_receipt.test_purchase_receipt import make_purchase_receipt
from erpnext.stock.doctype.serial_and_batch_bundle.serial_and_batch_bundle import ( from erpnext.stock.doctype.serial_and_batch_bundle.serial_and_batch_bundle import (
BatchNegativeStockError, BatchNegativeStockError,
) )
from erpnext.stock.doctype.serial_and_batch_bundle.test_serial_and_batch_bundle import (
get_batch_from_bundle,
)
from erpnext.stock.doctype.stock_entry.stock_entry_utils import make_stock_entry from erpnext.stock.doctype.stock_entry.stock_entry_utils import make_stock_entry
from erpnext.stock.get_item_details import get_item_details from erpnext.stock.get_item_details import get_item_details
from erpnext.stock.serial_batch_bundle import SerialBatchCreation from erpnext.stock.serial_batch_bundle import SerialBatchCreation
@ -96,13 +99,37 @@ class TestBatch(FrappeTestCase):
receipt = self.test_purchase_receipt(batch_qty) receipt = self.test_purchase_receipt(batch_qty)
item_code = "ITEM-BATCH-1" item_code = "ITEM-BATCH-1"
batch_no = get_batch_from_bundle(receipt.items[0].serial_and_batch_bundle)
bundle_id = (
SerialBatchCreation(
{
"item_code": item_code,
"warehouse": receipt.items[0].warehouse,
"actual_qty": batch_qty,
"voucher_type": "Stock Entry",
"batches": frappe._dict({batch_no: batch_qty}),
"type_of_transaction": "Outward",
"company": receipt.company,
}
)
.make_serial_and_batch_bundle()
.name
)
delivery_note = frappe.get_doc( delivery_note = frappe.get_doc(
dict( dict(
doctype="Delivery Note", doctype="Delivery Note",
customer="_Test Customer", customer="_Test Customer",
company=receipt.company, company=receipt.company,
items=[ items=[
dict(item_code=item_code, qty=batch_qty, rate=10, warehouse=receipt.items[0].warehouse) dict(
item_code=item_code,
qty=batch_qty,
rate=10,
warehouse=receipt.items[0].warehouse,
serial_and_batch_bundle=bundle_id,
)
], ],
) )
).insert() ).insert()
@ -113,8 +140,8 @@ class TestBatch(FrappeTestCase):
# shipped from FEFO batch # shipped from FEFO batch
self.assertEqual( self.assertEqual(
get_batch_no(delivery_note.items[0].serial_and_batch_bundle), get_batch_from_bundle(delivery_note.items[0].serial_and_batch_bundle),
get_batch_no(receipt.items[0].serial_and_batch_bundle), batch_no,
) )
def test_batch_negative_stock_error(self): def test_batch_negative_stock_error(self):
@ -130,7 +157,7 @@ class TestBatch(FrappeTestCase):
"voucher_type": "Delivery Note", "voucher_type": "Delivery Note",
"qty": 5000, "qty": 5000,
"avg_rate": 10, "avg_rate": 10,
"batches": frappe._dict({batch_no: 90}), "batches": frappe._dict({batch_no: 5000}),
"type_of_transaction": "Outward", "type_of_transaction": "Outward",
"company": receipt.company, "company": receipt.company,
} }
@ -145,6 +172,24 @@ class TestBatch(FrappeTestCase):
receipt = self.test_purchase_receipt(batch_qty) receipt = self.test_purchase_receipt(batch_qty)
item_code = "ITEM-BATCH-1" item_code = "ITEM-BATCH-1"
batch_no = get_batch_from_bundle(receipt.items[0].serial_and_batch_bundle)
bundle_id = (
SerialBatchCreation(
{
"item_code": item_code,
"warehouse": receipt.items[0].warehouse,
"actual_qty": batch_qty,
"voucher_type": "Stock Entry",
"batches": frappe._dict({batch_no: batch_qty}),
"type_of_transaction": "Outward",
"company": receipt.company,
}
)
.make_serial_and_batch_bundle()
.name
)
stock_entry = frappe.get_doc( stock_entry = frappe.get_doc(
dict( dict(
doctype="Stock Entry", doctype="Stock Entry",
@ -155,6 +200,7 @@ class TestBatch(FrappeTestCase):
item_code=item_code, item_code=item_code,
qty=batch_qty, qty=batch_qty,
s_warehouse=receipt.items[0].warehouse, s_warehouse=receipt.items[0].warehouse,
serial_and_batch_bundle=bundle_id,
) )
], ],
) )
@ -163,10 +209,11 @@ class TestBatch(FrappeTestCase):
stock_entry.set_stock_entry_type() stock_entry.set_stock_entry_type()
stock_entry.insert() stock_entry.insert()
stock_entry.submit() stock_entry.submit()
stock_entry.load_from_db()
self.assertEqual( self.assertEqual(
get_batch_no(stock_entry.items[0].serial_and_batch_bundle), get_batch_from_bundle(stock_entry.items[0].serial_and_batch_bundle),
get_batch_no(receipt.items[0].serial_and_batch_bundle), get_batch_from_bundle(receipt.items[0].serial_and_batch_bundle),
) )
def test_batch_split(self): def test_batch_split(self):
@ -174,11 +221,11 @@ class TestBatch(FrappeTestCase):
receipt = self.test_purchase_receipt() receipt = self.test_purchase_receipt()
from erpnext.stock.doctype.batch.batch import split_batch from erpnext.stock.doctype.batch.batch import split_batch
new_batch = split_batch( batch_no = get_batch_from_bundle(receipt.items[0].serial_and_batch_bundle)
receipt.items[0].batch_no, "ITEM-BATCH-1", receipt.items[0].warehouse, 22
)
self.assertEqual(get_batch_qty(receipt.items[0].batch_no, receipt.items[0].warehouse), 78) new_batch = split_batch(batch_no, "ITEM-BATCH-1", receipt.items[0].warehouse, 22)
self.assertEqual(get_batch_qty(batch_no, receipt.items[0].warehouse), 78)
self.assertEqual(get_batch_qty(new_batch, receipt.items[0].warehouse), 22) self.assertEqual(get_batch_qty(new_batch, receipt.items[0].warehouse), 22)
def test_get_batch_qty(self): def test_get_batch_qty(self):
@ -189,7 +236,10 @@ class TestBatch(FrappeTestCase):
self.assertEqual( self.assertEqual(
get_batch_qty(item_code="ITEM-BATCH-2", warehouse="_Test Warehouse - _TC"), get_batch_qty(item_code="ITEM-BATCH-2", warehouse="_Test Warehouse - _TC"),
[{"batch_no": "batch a", "qty": 90.0}, {"batch_no": "batch b", "qty": 90.0}], [
{"batch_no": "batch a", "qty": 90.0, "warehouse": "_Test Warehouse - _TC"},
{"batch_no": "batch b", "qty": 90.0, "warehouse": "_Test Warehouse - _TC"},
],
) )
self.assertEqual(get_batch_qty("batch a", "_Test Warehouse - _TC"), 90) self.assertEqual(get_batch_qty("batch a", "_Test Warehouse - _TC"), 90)
@ -389,7 +439,7 @@ class TestBatch(FrappeTestCase):
self.make_batch_item(item_code) self.make_batch_item(item_code)
se = make_stock_entry(item_code=item_code, qty=100, rate=10, target="_Test Warehouse - _TC") se = make_stock_entry(item_code=item_code, qty=100, rate=10, target="_Test Warehouse - _TC")
batch_no = se.items[0].batch_no batch_no = get_batch_from_bundle(se.items[0].serial_and_batch_bundle)
batch = frappe.get_doc("Batch", batch_no) batch = frappe.get_doc("Batch", batch_no)
expiry_date = add_to_date(batch.manufacturing_date, days=30) expiry_date = add_to_date(batch.manufacturing_date, days=30)
@ -418,14 +468,17 @@ class TestBatch(FrappeTestCase):
pr_1 = make_purchase_receipt(item_code=item_code, qty=1, batch_no=manually_created_batch) pr_1 = make_purchase_receipt(item_code=item_code, qty=1, batch_no=manually_created_batch)
pr_2 = make_purchase_receipt(item_code=item_code, qty=1) pr_2 = make_purchase_receipt(item_code=item_code, qty=1)
self.assertNotEqual(pr_1.items[0].batch_no, pr_2.items[0].batch_no) pr_1.load_from_db()
self.assertEqual("BATCHEXISTING002", pr_2.items[0].batch_no) pr_2.load_from_db()
self.assertNotEqual(
get_batch_from_bundle(pr_1.items[0].serial_and_batch_bundle),
get_batch_from_bundle(pr_2.items[0].serial_and_batch_bundle),
)
def get_batch_from_bundle(bundle): self.assertEqual(
batches = get_batch_no(bundle) "BATCHEXISTING002", get_batch_from_bundle(pr_2.items[0].serial_and_batch_bundle)
)
return list(batches.keys())[0]
def create_batch(item_code, rate, create_item_price_for_batch): def create_batch(item_code, rate, create_item_price_for_batch):

View File

@ -23,7 +23,11 @@ from erpnext.stock.doctype.delivery_note.delivery_note import (
) )
from erpnext.stock.doctype.item.test_item import make_item from erpnext.stock.doctype.item.test_item import make_item
from erpnext.stock.doctype.purchase_receipt.test_purchase_receipt import get_gl_entries from erpnext.stock.doctype.purchase_receipt.test_purchase_receipt import get_gl_entries
from erpnext.stock.doctype.serial_no.serial_no import SerialNoWarehouseError, get_serial_nos from erpnext.stock.doctype.serial_and_batch_bundle.test_serial_and_batch_bundle import (
get_batch_from_bundle,
get_serial_nos_from_bundle,
make_serial_batch_bundle,
)
from erpnext.stock.doctype.stock_entry.test_stock_entry import ( from erpnext.stock.doctype.stock_entry.test_stock_entry import (
get_qty_after_transaction, get_qty_after_transaction,
make_serialized_item, make_serialized_item,
@ -135,42 +139,6 @@ class TestDeliveryNote(FrappeTestCase):
dn.cancel() dn.cancel()
def test_serialized(self):
se = make_serialized_item()
serial_no = get_serial_nos(se.get("items")[0].serial_no)[0]
dn = create_delivery_note(item_code="_Test Serialized Item With Series", serial_no=serial_no)
self.check_serial_no_values(serial_no, {"warehouse": "", "delivery_document_no": dn.name})
si = make_sales_invoice(dn.name)
si.insert(ignore_permissions=True)
self.assertEqual(dn.items[0].serial_no, si.items[0].serial_no)
dn.cancel()
self.check_serial_no_values(
serial_no, {"warehouse": "_Test Warehouse - _TC", "delivery_document_no": ""}
)
def test_serialized_partial_sales_invoice(self):
se = make_serialized_item()
serial_no = get_serial_nos(se.get("items")[0].serial_no)
serial_no = "\n".join(serial_no)
dn = create_delivery_note(
item_code="_Test Serialized Item With Series", qty=2, serial_no=serial_no
)
si = make_sales_invoice(dn.name)
si.items[0].qty = 1
si.submit()
self.assertEqual(si.items[0].qty, 1)
si = make_sales_invoice(dn.name)
si.submit()
self.assertEqual(si.items[0].qty, len(get_serial_nos(si.items[0].serial_no)))
def test_serialize_status(self): def test_serialize_status(self):
from frappe.model.naming import make_autoname from frappe.model.naming import make_autoname
@ -178,16 +146,28 @@ class TestDeliveryNote(FrappeTestCase):
{ {
"doctype": "Serial No", "doctype": "Serial No",
"item_code": "_Test Serialized Item With Series", "item_code": "_Test Serialized Item With Series",
"serial_no": make_autoname("SR", "Serial No"), "serial_no": make_autoname("SRDD", "Serial No"),
} }
) )
serial_no.save() serial_no.save()
dn = create_delivery_note( bundle_id = make_serial_batch_bundle(
item_code="_Test Serialized Item With Series", serial_no=serial_no.name, do_not_submit=True frappe._dict(
{
"item_code": "_Test Serialized Item With Series",
"warehouse": "_Test Warehouse - _TC",
"qty": -1,
"voucher_type": "Delivery Note",
"serial_nos": [serial_no.name],
"posting_date": today(),
"posting_time": nowtime(),
"type_of_transaction": "Outward",
"do_not_save": True,
}
)
) )
self.assertRaises(SerialNoWarehouseError, dn.submit) self.assertRaises(frappe.ValidationError, bundle_id.make_serial_and_batch_bundle)
def check_serial_no_values(self, serial_no, field_values): def check_serial_no_values(self, serial_no, field_values):
serial_no = frappe.get_doc("Serial No", serial_no) serial_no = frappe.get_doc("Serial No", serial_no)
@ -532,13 +512,13 @@ class TestDeliveryNote(FrappeTestCase):
def test_return_for_serialized_items(self): def test_return_for_serialized_items(self):
se = make_serialized_item() se = make_serialized_item()
serial_no = get_serial_nos(se.get("items")[0].serial_no)[0] serial_no = [get_serial_nos_from_bundle(se.get("items")[0].serial_and_batch_bundle)[0]]
dn = create_delivery_note( dn = create_delivery_note(
item_code="_Test Serialized Item With Series", rate=500, serial_no=serial_no item_code="_Test Serialized Item With Series", rate=500, serial_no=serial_no
) )
self.check_serial_no_values(serial_no, {"warehouse": "", "delivery_document_no": dn.name}) self.check_serial_no_values(serial_no, {"warehouse": ""})
# return entry # return entry
dn1 = create_delivery_note( dn1 = create_delivery_note(
@ -550,23 +530,17 @@ class TestDeliveryNote(FrappeTestCase):
serial_no=serial_no, serial_no=serial_no,
) )
self.check_serial_no_values( self.check_serial_no_values(serial_no, {"warehouse": "_Test Warehouse - _TC"})
serial_no, {"warehouse": "_Test Warehouse - _TC", "delivery_document_no": ""}
)
dn1.cancel() dn1.cancel()
self.check_serial_no_values(serial_no, {"warehouse": "", "delivery_document_no": dn.name}) self.check_serial_no_values(serial_no, {"warehouse": ""})
dn.cancel() dn.cancel()
self.check_serial_no_values( self.check_serial_no_values(
serial_no, serial_no,
{ {"warehouse": "_Test Warehouse - _TC"},
"warehouse": "_Test Warehouse - _TC",
"delivery_document_no": "",
"purchase_document_no": se.name,
},
) )
def test_delivery_of_bundled_items_to_target_warehouse(self): def test_delivery_of_bundled_items_to_target_warehouse(self):
@ -964,16 +938,11 @@ class TestDeliveryNote(FrappeTestCase):
item_code=batched_item.name, target="_Test Warehouse - _TC", qty=10, basic_rate=42 item_code=batched_item.name, target="_Test Warehouse - _TC", qty=10, basic_rate=42
) )
try: dn = create_delivery_note(item_code=batched_bundle.name, qty=1)
dn = create_delivery_note(item_code=batched_bundle.name, qty=1) dn.load_from_db()
except frappe.ValidationError as e:
if "batch" in str(e).lower():
self.fail("Batch numbers not getting added to bundled items in DN.")
raise e
self.assertTrue( batch_no = get_batch_from_bundle(dn.items[0].serial_and_batch_bundle)
"TESTBATCH" in dn.packed_items[0].batch_no, "Batch number not added in packed item" self.assertTrue(batch_no)
)
def test_payment_terms_are_fetched_when_creating_sales_invoice(self): def test_payment_terms_are_fetched_when_creating_sales_invoice(self):
from erpnext.accounts.doctype.payment_entry.test_payment_entry import ( from erpnext.accounts.doctype.payment_entry.test_payment_entry import (
@ -1167,10 +1136,11 @@ class TestDeliveryNote(FrappeTestCase):
pi = make_purchase_receipt(qty=1, item_code=item.name) pi = make_purchase_receipt(qty=1, item_code=item.name)
dn = create_delivery_note(qty=1, item_code=item.name, batch_no=pi.items[0].batch_no) pr_batch_no = get_batch_from_bundle(pi.items[0].serial_and_batch_bundle)
dn = create_delivery_note(qty=1, item_code=item.name, batch_no=pr_batch_no)
dn.load_from_db() dn.load_from_db()
batch_no = dn.items[0].batch_no batch_no = get_batch_from_bundle(dn.items[0].serial_and_batch_bundle)
self.assertTrue(batch_no) self.assertTrue(batch_no)
frappe.db.set_value("Batch", batch_no, "expiry_date", add_days(today(), -1)) frappe.db.set_value("Batch", batch_no, "expiry_date", add_days(today(), -1))
@ -1241,6 +1211,32 @@ def create_delivery_note(**args):
dn.is_return = args.is_return dn.is_return = args.is_return
dn.return_against = args.return_against dn.return_against = args.return_against
bundle_id = None
if args.get("batch_no") or args.get("serial_no"):
type_of_transaction = args.type_of_transaction or "Outward"
qty = args.get("qty") or 1
qty *= -1 if type_of_transaction == "Outward" else 1
batches = {}
if args.get("batch_no"):
batches = frappe._dict({args.batch_no: qty})
bundle_id = make_serial_batch_bundle(
frappe._dict(
{
"item_code": args.item or args.item_code or "_Test Item",
"warehouse": args.warehouse or "_Test Warehouse - _TC",
"qty": qty,
"batches": batches,
"voucher_type": "Delivery Note",
"serial_nos": args.serial_no,
"posting_date": dn.posting_date,
"posting_time": dn.posting_time,
"type_of_transaction": type_of_transaction,
}
)
).name
dn.append( dn.append(
"items", "items",
{ {
@ -1249,11 +1245,10 @@ def create_delivery_note(**args):
"qty": args.qty or 1, "qty": args.qty or 1,
"rate": args.rate if args.get("rate") is not None else 100, "rate": args.rate if args.get("rate") is not None else 100,
"conversion_factor": 1.0, "conversion_factor": 1.0,
"serial_and_batch_bundle": bundle_id,
"allow_zero_valuation_rate": args.allow_zero_valuation_rate or 1, "allow_zero_valuation_rate": args.allow_zero_valuation_rate or 1,
"expense_account": args.expense_account or "Cost of Goods Sold - _TC", "expense_account": args.expense_account or "Cost of Goods Sold - _TC",
"cost_center": args.cost_center or "_Test Cost Center - _TC", "cost_center": args.cost_center or "_Test Cost Center - _TC",
"serial_no": args.serial_no,
"batch_no": args.batch_no or None,
"target_warehouse": args.target_warehouse, "target_warehouse": args.target_warehouse,
}, },
) )

View File

@ -3,7 +3,7 @@
import frappe import frappe
from frappe.tests.utils import FrappeTestCase, change_settings from frappe.tests.utils import FrappeTestCase, change_settings
from frappe.utils import add_days, cint, cstr, flt, today from frappe.utils import add_days, cint, cstr, flt, nowtime, today
from pypika import functions as fn from pypika import functions as fn
import erpnext import erpnext
@ -11,7 +11,16 @@ from erpnext.accounts.doctype.account.test_account import get_inventory_account
from erpnext.controllers.buying_controller import QtyMismatchError from erpnext.controllers.buying_controller import QtyMismatchError
from erpnext.stock.doctype.item.test_item import create_item, make_item from erpnext.stock.doctype.item.test_item import create_item, make_item
from erpnext.stock.doctype.purchase_receipt.purchase_receipt import make_purchase_invoice from erpnext.stock.doctype.purchase_receipt.purchase_receipt import make_purchase_invoice
from erpnext.stock.doctype.serial_no.serial_no import SerialNoDuplicateError, get_serial_nos from erpnext.stock.doctype.serial_and_batch_bundle.serial_and_batch_bundle import (
SerialNoDuplicateError,
SerialNoExistsInFutureTransactionError,
)
from erpnext.stock.doctype.serial_and_batch_bundle.test_serial_and_batch_bundle import (
get_batch_from_bundle,
get_serial_nos_from_bundle,
make_serial_batch_bundle,
)
from erpnext.stock.doctype.serial_no.serial_no import get_serial_nos
from erpnext.stock.doctype.warehouse.test_warehouse import create_warehouse from erpnext.stock.doctype.warehouse.test_warehouse import create_warehouse
from erpnext.stock.stock_ledger import SerialNoExistsInFutureTransaction from erpnext.stock.stock_ledger import SerialNoExistsInFutureTransaction
@ -184,14 +193,11 @@ class TestPurchaseReceipt(FrappeTestCase):
self.assertTrue(frappe.db.get_value("Batch", {"item": item.name, "reference_name": pr.name})) self.assertTrue(frappe.db.get_value("Batch", {"item": item.name, "reference_name": pr.name}))
pr.load_from_db() pr.load_from_db()
batch_no = pr.items[0].batch_no
pr.cancel() pr.cancel()
self.assertFalse(frappe.db.get_value("Batch", {"item": item.name, "reference_name": pr.name}))
self.assertFalse(frappe.db.get_all("Serial No", {"batch_no": batch_no}))
def test_duplicate_serial_nos(self): def test_duplicate_serial_nos(self):
from erpnext.stock.doctype.delivery_note.test_delivery_note import create_delivery_note from erpnext.stock.doctype.delivery_note.test_delivery_note import create_delivery_note
from erpnext.stock.serial_batch_bundle import SerialBatchCreation
item = frappe.db.exists("Item", {"item_name": "Test Serialized Item 123"}) item = frappe.db.exists("Item", {"item_name": "Test Serialized Item 123"})
if not item: if not item:
@ -206,67 +212,86 @@ class TestPurchaseReceipt(FrappeTestCase):
pr = make_purchase_receipt(item_code=item.name, qty=2, rate=500) pr = make_purchase_receipt(item_code=item.name, qty=2, rate=500)
pr.load_from_db() pr.load_from_db()
serial_nos = frappe.db.get_value( bundle_id = frappe.db.get_value(
"Stock Ledger Entry", "Stock Ledger Entry",
{"voucher_type": "Purchase Receipt", "voucher_no": pr.name, "item_code": item.name}, {"voucher_type": "Purchase Receipt", "voucher_no": pr.name, "item_code": item.name},
"serial_no", "serial_and_batch_bundle",
) )
serial_nos = get_serial_nos(serial_nos) serial_nos = get_serial_nos_from_bundle(bundle_id)
self.assertEquals(get_serial_nos(pr.items[0].serial_no), serial_nos) self.assertEquals(get_serial_nos_from_bundle(pr.items[0].serial_and_batch_bundle), serial_nos)
# Then tried to receive same serial nos in difference company bundle_id = make_serial_batch_bundle(
pr_different_company = make_purchase_receipt( frappe._dict(
item_code=item.name, {
qty=2, "item_code": item.item_code,
rate=500, "warehouse": "_Test Warehouse 2 - _TC1",
serial_no="\n".join(serial_nos), "company": "_Test Company 1",
company="_Test Company 1", "qty": 2,
do_not_submit=True, "voucher_type": "Purchase Receipt",
warehouse="Stores - _TC1", "serial_nos": serial_nos,
"posting_date": today(),
"posting_time": nowtime(),
"do_not_save": True,
}
)
) )
self.assertRaises(SerialNoDuplicateError, pr_different_company.submit) self.assertRaises(SerialNoDuplicateError, bundle_id.make_serial_and_batch_bundle)
# Then made delivery note to remove the serial nos from stock # Then made delivery note to remove the serial nos from stock
dn = create_delivery_note(item_code=item.name, qty=2, rate=1500, serial_no="\n".join(serial_nos)) dn = create_delivery_note(item_code=item.name, qty=2, rate=1500, serial_no=serial_nos)
dn.load_from_db() dn.load_from_db()
self.assertEquals(get_serial_nos(dn.items[0].serial_no), serial_nos) self.assertEquals(get_serial_nos_from_bundle(dn.items[0].serial_and_batch_bundle), serial_nos)
posting_date = add_days(today(), -3) posting_date = add_days(today(), -3)
# Try to receive same serial nos again in the same company with backdated. # Try to receive same serial nos again in the same company with backdated.
pr1 = make_purchase_receipt( bundle_id = make_serial_batch_bundle(
item_code=item.name, frappe._dict(
qty=2, {
rate=500, "item_code": item.item_code,
posting_date=posting_date, "warehouse": "_Test Warehouse - _TC",
serial_no="\n".join(serial_nos), "company": "_Test Company",
do_not_submit=True, "qty": 2,
"rate": 500,
"voucher_type": "Purchase Receipt",
"serial_nos": serial_nos,
"posting_date": posting_date,
"posting_time": nowtime(),
"do_not_save": True,
}
)
) )
self.assertRaises(SerialNoExistsInFutureTransaction, pr1.submit) self.assertRaises(SerialNoExistsInFutureTransactionError, bundle_id.make_serial_and_batch_bundle)
# Try to receive same serial nos with different company with backdated. # Try to receive same serial nos with different company with backdated.
pr2 = make_purchase_receipt( bundle_id = make_serial_batch_bundle(
item_code=item.name, frappe._dict(
qty=2, {
rate=500, "item_code": item.item_code,
posting_date=posting_date, "warehouse": "_Test Warehouse 2 - _TC1",
serial_no="\n".join(serial_nos), "company": "_Test Company 1",
company="_Test Company 1", "qty": 2,
do_not_submit=True, "rate": 500,
warehouse="Stores - _TC1", "voucher_type": "Purchase Receipt",
"serial_nos": serial_nos,
"posting_date": posting_date,
"posting_time": nowtime(),
"do_not_save": True,
}
)
) )
self.assertRaises(SerialNoExistsInFutureTransaction, pr2.submit) self.assertRaises(SerialNoExistsInFutureTransactionError, bundle_id.make_serial_and_batch_bundle)
# Receive the same serial nos after the delivery note posting date and time # Receive the same serial nos after the delivery note posting date and time
make_purchase_receipt(item_code=item.name, qty=2, rate=500, serial_no="\n".join(serial_nos)) make_purchase_receipt(item_code=item.name, qty=2, rate=500, serial_no=serial_nos)
# Raise the error for backdated deliver note entry cancel # Raise the error for backdated deliver note entry cancel
self.assertRaises(SerialNoExistsInFutureTransaction, dn.cancel) # self.assertRaises(SerialNoExistsInFutureTransactionError, dn.cancel)
def test_purchase_receipt_gl_entry(self): def test_purchase_receipt_gl_entry(self):
pr = make_purchase_receipt( pr = make_purchase_receipt(
@ -307,11 +332,13 @@ class TestPurchaseReceipt(FrappeTestCase):
pr.cancel() pr.cancel()
self.assertTrue(get_gl_entries("Purchase Receipt", pr.name)) self.assertTrue(get_gl_entries("Purchase Receipt", pr.name))
def test_serial_no_supplier(self): def test_serial_no_warehouse(self):
pr = make_purchase_receipt(item_code="_Test Serialized Item With Series", qty=1) pr = make_purchase_receipt(item_code="_Test Serialized Item With Series", qty=1)
pr_row_1_serial_no = pr.get("items")[0].serial_no pr_row_1_serial_no = get_serial_nos_from_bundle(pr.get("items")[0].serial_and_batch_bundle)[0]
self.assertEqual(frappe.db.get_value("Serial No", pr_row_1_serial_no, "supplier"), pr.supplier) self.assertEqual(
frappe.db.get_value("Serial No", pr_row_1_serial_no, "warehouse"), pr.get("items")[0].warehouse
)
pr.cancel() pr.cancel()
self.assertFalse(frappe.db.get_value("Serial No", pr_row_1_serial_no, "warehouse")) self.assertFalse(frappe.db.get_value("Serial No", pr_row_1_serial_no, "warehouse"))
@ -325,15 +352,18 @@ class TestPurchaseReceipt(FrappeTestCase):
pr.get("items")[0].rejected_warehouse = "_Test Rejected Warehouse - _TC" pr.get("items")[0].rejected_warehouse = "_Test Rejected Warehouse - _TC"
pr.insert() pr.insert()
pr.submit() pr.submit()
pr.load_from_db()
accepted_serial_nos = pr.get("items")[0].serial_no.split("\n") accepted_serial_nos = get_serial_nos_from_bundle(pr.get("items")[0].serial_and_batch_bundle)
self.assertEqual(len(accepted_serial_nos), 3) self.assertEqual(len(accepted_serial_nos), 3)
for serial_no in accepted_serial_nos: for serial_no in accepted_serial_nos:
self.assertEqual( self.assertEqual(
frappe.db.get_value("Serial No", serial_no, "warehouse"), pr.get("items")[0].warehouse frappe.db.get_value("Serial No", serial_no, "warehouse"), pr.get("items")[0].warehouse
) )
rejected_serial_nos = pr.get("items")[0].rejected_serial_no.split("\n") rejected_serial_nos = get_serial_nos_from_bundle(
pr.get("items")[0].rejected_serial_and_batch_bundle
)
self.assertEqual(len(rejected_serial_nos), 2) self.assertEqual(len(rejected_serial_nos), 2)
for serial_no in rejected_serial_nos: for serial_no in rejected_serial_nos:
self.assertEqual( self.assertEqual(
@ -556,23 +586,21 @@ class TestPurchaseReceipt(FrappeTestCase):
pr = make_purchase_receipt(item_code="_Test Serialized Item With Series", qty=1) pr = make_purchase_receipt(item_code="_Test Serialized Item With Series", qty=1)
serial_no = get_serial_nos(pr.get("items")[0].serial_no)[0] serial_no = get_serial_nos_from_bundle(pr.get("items")[0].serial_and_batch_bundle)[0]
_check_serial_no_values( _check_serial_no_values(serial_no, {"warehouse": "_Test Warehouse - _TC"})
serial_no, {"warehouse": "_Test Warehouse - _TC", "purchase_document_no": pr.name}
)
return_pr = make_purchase_receipt( return_pr = make_purchase_receipt(
item_code="_Test Serialized Item With Series", item_code="_Test Serialized Item With Series",
qty=-1, qty=-1,
is_return=1, is_return=1,
return_against=pr.name, return_against=pr.name,
serial_no=serial_no, serial_no=[serial_no],
) )
_check_serial_no_values( _check_serial_no_values(
serial_no, serial_no,
{"warehouse": "", "purchase_document_no": pr.name, "delivery_document_no": return_pr.name}, {"warehouse": ""},
) )
return_pr.cancel() return_pr.cancel()
@ -677,20 +705,23 @@ class TestPurchaseReceipt(FrappeTestCase):
item_code = "Test Manual Created Serial No" item_code = "Test Manual Created Serial No"
if not frappe.db.exists("Item", item_code): if not frappe.db.exists("Item", item_code):
item = make_item(item_code, dict(has_serial_no=1)) make_item(item_code, dict(has_serial_no=1))
serial_no = ["12903812901"]
if not frappe.db.exists("Serial No", serial_no[0]):
frappe.get_doc(
{"doctype": "Serial No", "item_code": item_code, "serial_no": serial_no[0]}
).insert()
serial_no = "12903812901"
pr_doc = make_purchase_receipt(item_code=item_code, qty=1, serial_no=serial_no) pr_doc = make_purchase_receipt(item_code=item_code, qty=1, serial_no=serial_no)
pr_doc.load_from_db()
self.assertEqual( bundle_id = pr_doc.items[0].serial_and_batch_bundle
serial_no, self.assertEqual(serial_no[0], get_serial_nos_from_bundle(bundle_id)[0])
frappe.db.get_value(
"Serial No",
{"purchase_document_type": "Purchase Receipt", "purchase_document_no": pr_doc.name},
"name",
),
)
voucher_no = frappe.db.get_value("Serial and Batch Bundle", bundle_id, "voucher_no")
self.assertEqual(voucher_no, pr_doc.name)
pr_doc.cancel() pr_doc.cancel()
# check for the auto created serial nos # check for the auto created serial nos
@ -699,16 +730,15 @@ class TestPurchaseReceipt(FrappeTestCase):
make_item(item_code, dict(has_serial_no=1, serial_no_series="KLJL.###")) make_item(item_code, dict(has_serial_no=1, serial_no_series="KLJL.###"))
new_pr_doc = make_purchase_receipt(item_code=item_code, qty=1) new_pr_doc = make_purchase_receipt(item_code=item_code, qty=1)
new_pr_doc.load_from_db()
serial_no = get_serial_nos(new_pr_doc.items[0].serial_no)[0] bundle_id = new_pr_doc.items[0].serial_and_batch_bundle
self.assertEqual( serial_no = get_serial_nos_from_bundle(bundle_id)[0]
serial_no, self.assertTrue(serial_no)
frappe.db.get_value(
"Serial No", voucher_no = frappe.db.get_value("Serial and Batch Bundle", bundle_id, "voucher_no")
{"purchase_document_type": "Purchase Receipt", "purchase_document_no": new_pr_doc.name},
"name", self.assertEqual(voucher_no, new_pr_doc.name)
),
)
new_pr_doc.cancel() new_pr_doc.cancel()
@ -1491,7 +1521,7 @@ class TestPurchaseReceipt(FrappeTestCase):
) )
pi.load_from_db() pi.load_from_db()
batch_no = pi.items[0].batch_no batch_no = get_batch_from_bundle(pi.items[0].serial_and_batch_bundle)
self.assertTrue(batch_no) self.assertTrue(batch_no)
frappe.db.set_value("Batch", batch_no, "expiry_date", add_days(today(), -1)) frappe.db.set_value("Batch", batch_no, "expiry_date", add_days(today(), -1))
@ -1917,6 +1947,30 @@ def make_purchase_receipt(**args):
item_code = args.item or args.item_code or "_Test Item" item_code = args.item or args.item_code or "_Test Item"
uom = args.uom or frappe.db.get_value("Item", item_code, "stock_uom") or "_Test UOM" uom = args.uom or frappe.db.get_value("Item", item_code, "stock_uom") or "_Test UOM"
bundle_id = None
if args.get("batch_no") or args.get("serial_no"):
batches = {}
if args.get("batch_no"):
batches = frappe._dict({args.batch_no: qty})
serial_nos = args.get("serial_no") or []
bundle_id = make_serial_batch_bundle(
frappe._dict(
{
"item_code": item_code,
"warehouse": args.warehouse or "_Test Warehouse - _TC",
"qty": qty,
"batches": batches,
"voucher_type": "Purchase Receipt",
"serial_nos": serial_nos,
"posting_date": args.posting_date or today(),
"posting_time": args.posting_time,
}
)
).name
pr.append( pr.append(
"items", "items",
{ {
@ -1931,8 +1985,7 @@ def make_purchase_receipt(**args):
"rate": args.rate if args.rate != None else 50, "rate": args.rate if args.rate != None else 50,
"conversion_factor": args.conversion_factor or 1.0, "conversion_factor": args.conversion_factor or 1.0,
"stock_qty": flt(qty) * (flt(args.conversion_factor) or 1.0), "stock_qty": flt(qty) * (flt(args.conversion_factor) or 1.0),
"serial_no": args.serial_no, "serial_and_batch_bundle": bundle_id,
"batch_no": args.batch_no,
"stock_uom": args.stock_uom or "_Test UOM", "stock_uom": args.stock_uom or "_Test UOM",
"uom": uom, "uom": uom,
"cost_center": args.cost_center "cost_center": args.cost_center
@ -1958,6 +2011,9 @@ def make_purchase_receipt(**args):
pr.insert() pr.insert()
if not args.do_not_submit: if not args.do_not_submit:
pr.submit() pr.submit()
pr.load_from_db()
return pr return pr

View File

@ -12,6 +12,7 @@ from frappe.query_builder.functions import CombineDatetime, Sum
from frappe.utils import add_days, cint, flt, get_link_to_form, nowtime, today from frappe.utils import add_days, cint, flt, get_link_to_form, nowtime, today
from erpnext.stock.serial_batch_bundle import BatchNoValuation, SerialNoValuation from erpnext.stock.serial_batch_bundle import BatchNoValuation, SerialNoValuation
from erpnext.stock.serial_batch_bundle import get_serial_nos as get_serial_nos_from_bundle
class SerialNoExistsInFutureTransactionError(frappe.ValidationError): class SerialNoExistsInFutureTransactionError(frappe.ValidationError):
@ -22,6 +23,14 @@ class BatchNegativeStockError(frappe.ValidationError):
pass pass
class SerialNoDuplicateError(frappe.ValidationError):
pass
class SerialNoWarehouseError(frappe.ValidationError):
pass
class SerialandBatchBundle(Document): class SerialandBatchBundle(Document):
def validate(self): def validate(self):
self.validate_serial_and_batch_no() self.validate_serial_and_batch_no()
@ -30,38 +39,66 @@ class SerialandBatchBundle(Document):
if self.type_of_transaction == "Maintenance": if self.type_of_transaction == "Maintenance":
return return
self.validate_serial_nos_duplicate()
self.check_future_entries_exists() self.check_future_entries_exists()
self.validate_serial_nos_inventory()
self.set_is_outward() self.set_is_outward()
self.validate_qty_and_stock_value_difference() self.calculate_total_qty()
self.calculate_qty_and_amount()
self.set_warehouse() self.set_warehouse()
self.set_incoming_rate() self.set_incoming_rate()
self.calculate_qty_and_amount()
def validate_serial_nos_inventory(self): def validate_serial_nos_inventory(self):
if not (self.has_serial_no and self.type_of_transaction == "Outward"): if not (self.has_serial_no and self.type_of_transaction == "Outward"):
return return
serial_nos = [d.serial_no for d in self.entries if d.serial_no] serial_nos = [d.serial_no for d in self.entries if d.serial_no]
serial_no_warehouse = frappe._dict( available_serial_nos = get_available_serial_nos(
frappe.get_all( frappe._dict({"item_code": self.item_code, "warehouse": self.warehouse})
"Serial No",
filters={"name": ("in", serial_nos)},
fields=["name", "warehouse"],
as_list=1,
)
) )
serial_no_warehouse = {}
for data in available_serial_nos:
if data.serial_no not in serial_nos:
continue
serial_no_warehouse[data.serial_no] = data.warehouse
for serial_no in serial_nos: for serial_no in serial_nos:
if ( if (
not serial_no_warehouse.get(serial_no) or serial_no_warehouse.get(serial_no) != self.warehouse not serial_no_warehouse.get(serial_no) or serial_no_warehouse.get(serial_no) != self.warehouse
): ):
self.throw_error_message( self.throw_error_message(
f"Serial No {bold(serial_no)} is not present in the warehouse {bold(self.warehouse)}." f"Serial No {bold(serial_no)} is not present in the warehouse {bold(self.warehouse)}.",
SerialNoWarehouseError,
) )
def throw_error_message(self, message): def validate_serial_nos_duplicate(self):
frappe.throw(_(message), title=_("Error")) if self.voucher_type in ["Stock Reconciliation", "Stock Entry"] and self.docstatus != 1:
return
if not (self.has_serial_no and self.type_of_transaction == "Inward"):
return
serial_nos = [d.serial_no for d in self.entries if d.serial_no]
available_serial_nos = get_available_serial_nos(
frappe._dict(
{
"item_code": self.item_code,
"posting_date": self.posting_date,
"posting_time": self.posting_time,
}
)
)
for data in available_serial_nos:
if data.serial_no in serial_nos:
self.throw_error_message(
f"Serial No {bold(data.serial_no)} is already present in the warehouse {bold(data.warehouse)}.",
SerialNoDuplicateError,
)
def throw_error_message(self, message, exception=frappe.ValidationError):
frappe.throw(_(message), exception, title=_("Error"))
def set_incoming_rate(self, row=None, save=False): def set_incoming_rate(self, row=None, save=False):
if self.type_of_transaction == "Outward": if self.type_of_transaction == "Outward":
@ -69,24 +106,25 @@ class SerialandBatchBundle(Document):
else: else:
self.set_incoming_rate_for_inward_transaction(row, save) self.set_incoming_rate_for_inward_transaction(row, save)
def validate_qty_and_stock_value_difference(self): def calculate_total_qty(self, save=True):
if self.type_of_transaction != "Outward": self.total_qty = 0.0
return
for d in self.entries: for d in self.entries:
if d.qty and d.qty > 0: d.qty = abs(d.qty) if d.qty else 0
d.stock_value_difference = abs(d.stock_value_difference) if d.stock_value_difference else 0
if self.type_of_transaction == "Outward":
d.qty *= -1 d.qty *= -1
if d.stock_value_difference and d.stock_value_difference > 0:
d.stock_value_difference *= -1 d.stock_value_difference *= -1
self.total_qty += flt(d.qty)
if save:
self.db_set("total_qty", self.total_qty)
def get_serial_nos(self): def get_serial_nos(self):
return [d.serial_no for d in self.entries if d.serial_no] return [d.serial_no for d in self.entries if d.serial_no]
def set_incoming_rate_for_outward_transaction(self, row=None, save=False): def set_incoming_rate_for_outward_transaction(self, row=None, save=False):
sle = self.get_sle_for_outward_transaction(row) sle = self.get_sle_for_outward_transaction()
if not sle.actual_qty and sle.qty:
sle.actual_qty = sle.qty
if self.has_serial_no: if self.has_serial_no:
sn_obj = SerialNoValuation( sn_obj = SerialNoValuation(
@ -107,7 +145,9 @@ class SerialandBatchBundle(Document):
if self.has_serial_no: if self.has_serial_no:
d.incoming_rate = abs(sn_obj.serial_no_incoming_rate.get(d.serial_no, 0.0)) d.incoming_rate = abs(sn_obj.serial_no_incoming_rate.get(d.serial_no, 0.0))
else: else:
d.incoming_rate = abs(sn_obj.batch_avg_rate.get(d.batch_no)) if sn_obj.batch_avg_rate.get(d.batch_no):
d.incoming_rate = abs(sn_obj.batch_avg_rate.get(d.batch_no))
available_qty = flt(sn_obj.available_qty.get(d.batch_no)) + flt(d.qty) available_qty = flt(sn_obj.available_qty.get(d.batch_no)) + flt(d.qty)
self.validate_negative_batch(d.batch_no, available_qty) self.validate_negative_batch(d.batch_no, available_qty)
@ -128,8 +168,8 @@ class SerialandBatchBundle(Document):
frappe.throw(_(msg), BatchNegativeStockError) frappe.throw(_(msg), BatchNegativeStockError)
def get_sle_for_outward_transaction(self, row): def get_sle_for_outward_transaction(self):
return frappe._dict( sle = frappe._dict(
{ {
"posting_date": self.posting_date, "posting_date": self.posting_date,
"posting_time": self.posting_time, "posting_time": self.posting_time,
@ -140,9 +180,19 @@ class SerialandBatchBundle(Document):
"company": self.company, "company": self.company,
"serial_nos": [row.serial_no for row in self.entries if row.serial_no], "serial_nos": [row.serial_no for row in self.entries if row.serial_no],
"batch_nos": {row.batch_no: row for row in self.entries if row.batch_no}, "batch_nos": {row.batch_no: row for row in self.entries if row.batch_no},
"voucher_type": self.voucher_type,
} }
) )
if self.docstatus == 1:
sle["voucher_no"] = self.voucher_no
if not sle.actual_qty:
self.calculate_total_qty()
sle.actual_qty = self.total_qty
return sle
def set_incoming_rate_for_inward_transaction(self, row=None, save=False): def set_incoming_rate_for_inward_transaction(self, row=None, save=False):
valuation_field = "valuation_rate" valuation_field = "valuation_rate"
if self.voucher_type in ["Sales Invoice", "Delivery Note"]: if self.voucher_type in ["Sales Invoice", "Delivery Note"]:
@ -155,10 +205,9 @@ class SerialandBatchBundle(Document):
rate = frappe.db.get_value(self.child_table, self.voucher_detail_no, valuation_field) rate = frappe.db.get_value(self.child_table, self.voucher_detail_no, valuation_field)
for d in self.entries: for d in self.entries:
if self.voucher_type in ["Stock Reconciliation", "Stock Entry"] and d.incoming_rate: if not rate or (
continue flt(rate, precision) == flt(d.incoming_rate, precision) and d.stock_value_difference
):
if not rate or flt(rate, precision) == flt(d.incoming_rate, precision):
continue continue
d.incoming_rate = flt(rate, precision) d.incoming_rate = flt(rate, precision)
@ -170,7 +219,7 @@ class SerialandBatchBundle(Document):
{"incoming_rate": d.incoming_rate, "stock_value_difference": d.stock_value_difference} {"incoming_rate": d.incoming_rate, "stock_value_difference": d.stock_value_difference}
) )
def set_serial_and_batch_values(self, parent, row): def set_serial_and_batch_values(self, parent, row, qty_field=None):
values_to_set = {} values_to_set = {}
if not self.voucher_no or self.voucher_no != row.parent: if not self.voucher_no or self.voucher_no != row.parent:
values_to_set["voucher_no"] = row.parent values_to_set["voucher_no"] = row.parent
@ -194,10 +243,14 @@ class SerialandBatchBundle(Document):
if values_to_set: if values_to_set:
self.db_set(values_to_set) self.db_set(values_to_set)
# self.validate_voucher_no() self.calculate_total_qty(save=True)
self.set_incoming_rate(save=True, row=row)
# If user has changed the rate in the child table
if self.docstatus == 0:
self.set_incoming_rate(save=True, row=row)
self.calculate_qty_and_amount(save=True) self.calculate_qty_and_amount(save=True)
self.validate_quantity(row) self.validate_quantity(row, qty_field=qty_field)
self.set_warranty_expiry_date() self.set_warranty_expiry_date()
def set_warranty_expiry_date(self): def set_warranty_expiry_date(self):
@ -292,15 +345,17 @@ class SerialandBatchBundle(Document):
frappe.throw(_(msg), title=_(title), exc=SerialNoExistsInFutureTransactionError) frappe.throw(_(msg), title=_(title), exc=SerialNoExistsInFutureTransactionError)
def validate_quantity(self, row): def validate_quantity(self, row, qty_field=None):
if not qty_field:
qty_field = "qty"
precision = row.precision precision = row.precision
qty_field = "qty"
if self.voucher_type in ["Subcontracting Receipt"]: if self.voucher_type in ["Subcontracting Receipt"]:
qty_field = "consumed_qty" qty_field = "consumed_qty"
if abs(flt(self.total_qty, precision)) - abs(flt(row.get(qty_field), precision)) > 0.01: if abs(abs(flt(self.total_qty, precision)) - abs(flt(row.get(qty_field), precision))) > 0.01:
self.throw_error_message( self.throw_error_message(
f"Total quantity {self.total_qty} in the Serial and Batch Bundle {self.name} does not match with the Item {self.item_code} in the {self.voucher_type} # {self.voucher_no}" f"Total quantity {abs(self.total_qty)} in the Serial and Batch Bundle {bold(self.name)} does not match with the quantity {abs(row.get(qty_field))} for the Item {bold(self.item_code)} in the {self.voucher_type} # {self.voucher_no}"
) )
def set_is_outward(self): def set_is_outward(self):
@ -324,7 +379,8 @@ class SerialandBatchBundle(Document):
self.avg_rate = 0.0 self.avg_rate = 0.0
for row in self.entries: for row in self.entries:
rate = flt(row.incoming_rate) or flt(row.outgoing_rate) rate = flt(row.incoming_rate)
row.stock_value_difference = flt(row.qty) * rate
self.total_amount += flt(row.qty) * rate self.total_amount += flt(row.qty) * rate
self.total_qty += flt(row.qty) self.total_qty += flt(row.qty)
@ -361,6 +417,51 @@ class SerialandBatchBundle(Document):
msg = f"The Item {self.item_code} does not have Serial No or Batch No" msg = f"The Item {self.item_code} does not have Serial No or Batch No"
frappe.throw(_(msg)) frappe.throw(_(msg))
serial_nos = []
batch_nos = []
for row in self.entries:
if row.serial_no:
serial_nos.append(row.serial_no)
if row.batch_no and not row.serial_no:
batch_nos.append(row.batch_no)
if serial_nos:
self.validate_incorrect_serial_nos(serial_nos)
elif batch_nos:
self.validate_incorrect_batch_nos(batch_nos)
def validate_incorrect_serial_nos(self, serial_nos):
if self.voucher_type == "Stock Entry" and self.voucher_no:
if frappe.get_cached_value("Stock Entry", self.voucher_no, "purpose") == "Repack":
return
incorrect_serial_nos = frappe.get_all(
"Serial No",
filters={"name": ("in", serial_nos), "item_code": ("!=", self.item_code)},
fields=["name"],
)
if incorrect_serial_nos:
incorrect_serial_nos = ", ".join([d.name for d in incorrect_serial_nos])
self.throw_error_message(
f"Serial Nos {bold(incorrect_serial_nos)} does not belong to Item {bold(self.item_code)}"
)
def validate_incorrect_batch_nos(self, batch_nos):
incorrect_batch_nos = frappe.get_all(
"Batch", filters={"name": ("in", batch_nos), "item": ("!=", self.item_code)}, fields=["name"]
)
if incorrect_batch_nos:
incorrect_batch_nos = ", ".join([d.name for d in incorrect_batch_nos])
self.throw_error_message(
f"Batch Nos {bold(incorrect_batch_nos)} does not belong to Item {bold(self.item_code)}"
)
def validate_duplicate_serial_and_batch_no(self): def validate_duplicate_serial_and_batch_no(self):
serial_nos = [] serial_nos = []
batch_nos = [] batch_nos = []
@ -406,13 +507,30 @@ class SerialandBatchBundle(Document):
return table return table
def delink_refernce_from_voucher(self): def delink_refernce_from_voucher(self):
or_filters = {"serial_and_batch_bundle": self.name}
fields = ["name", "serial_and_batch_bundle"]
if self.voucher_type == "Stock Reconciliation":
fields = ["name", "current_serial_and_batch_bundle", "serial_and_batch_bundle"]
or_filters["current_serial_and_batch_bundle"] = self.name
elif self.voucher_type == "Purchase Receipt":
fields = ["name", "rejected_serial_and_batch_bundle", "serial_and_batch_bundle"]
or_filters["rejected_serial_and_batch_bundle"] = self.name
vouchers = frappe.get_all( vouchers = frappe.get_all(
self.child_table, self.child_table,
fields=["name"], fields=fields,
filters={"serial_and_batch_bundle": self.name, "docstatus": 0}, filters={"docstatus": 0},
or_filters=or_filters,
) )
for voucher in vouchers: for voucher in vouchers:
if voucher.get("current_serial_and_batch_bundle"):
frappe.db.set_value(self.child_table, voucher.name, "current_serial_and_batch_bundle", None)
elif voucher.get("rejected_serial_and_batch_bundle"):
frappe.db.set_value(self.child_table, voucher.name, "rejected_serial_and_batch_bundle", None)
frappe.db.set_value(self.child_table, voucher.name, "serial_and_batch_bundle", None) frappe.db.set_value(self.child_table, voucher.name, "serial_and_batch_bundle", None)
def delink_reference_from_batch(self): def delink_reference_from_batch(self):
@ -425,6 +543,9 @@ class SerialandBatchBundle(Document):
for batch in batches: for batch in batches:
frappe.db.set_value("Batch", batch.name, {"reference_name": None, "reference_doctype": None}) frappe.db.set_value("Batch", batch.name, {"reference_name": None, "reference_doctype": None})
def on_submit(self):
self.validate_serial_nos_inventory()
def on_cancel(self): def on_cancel(self):
self.validate_voucher_no_docstatus() self.validate_voucher_no_docstatus()
@ -628,14 +749,14 @@ def get_serial_and_batch_ledger(**kwargs):
def get_auto_data(**kwargs): def get_auto_data(**kwargs):
kwargs = frappe._dict(kwargs) kwargs = frappe._dict(kwargs)
if cint(kwargs.has_serial_no): if cint(kwargs.has_serial_no):
return get_auto_serial_nos(kwargs) return get_available_serial_nos(kwargs)
elif cint(kwargs.has_batch_no): elif cint(kwargs.has_batch_no):
return get_auto_batch_nos(kwargs) return get_auto_batch_nos(kwargs)
def get_auto_serial_nos(kwargs): def get_available_serial_nos(kwargs):
fields = ["name as serial_no"] fields = ["name as serial_no", "warehouse"]
if kwargs.has_batch_no: if kwargs.has_batch_no:
fields.append("batch_no") fields.append("batch_no")
@ -645,21 +766,59 @@ def get_auto_serial_nos(kwargs):
elif kwargs.based_on == "Expiry": elif kwargs.based_on == "Expiry":
order_by = "amc_expiry_date asc" order_by = "amc_expiry_date asc"
filters = {"item_code": kwargs.item_code, "warehouse": ("is", "set")}
if kwargs.warehouse:
filters["warehouse"] = kwargs.warehouse
ignore_serial_nos = get_reserved_serial_nos_for_pos(kwargs) ignore_serial_nos = get_reserved_serial_nos_for_pos(kwargs)
if kwargs.get("posting_date"):
if kwargs.get("posting_time") is None:
kwargs.posting_time = nowtime()
filters["name"] = ("in", get_serial_nos_based_on_posting_date(kwargs, ignore_serial_nos))
elif ignore_serial_nos:
filters["name"] = ("not in", ignore_serial_nos)
return frappe.get_all( return frappe.get_all(
"Serial No", "Serial No",
fields=fields, fields=fields,
filters={ filters=filters,
"item_code": kwargs.item_code, limit=cint(kwargs.qty) or 10000000,
"warehouse": kwargs.warehouse,
"name": ("not in", ignore_serial_nos),
},
limit=cint(kwargs.qty),
order_by=order_by, order_by=order_by,
) )
def get_serial_nos_based_on_posting_date(kwargs, ignore_serial_nos):
from erpnext.stock.doctype.serial_no.serial_no import get_serial_nos
serial_nos = set()
data = get_stock_ledgers_for_serial_nos(kwargs)
for d in data:
if d.serial_and_batch_bundle:
sns = get_serial_nos_from_bundle(d.serial_and_batch_bundle)
if d.actual_qty > 0:
serial_nos.update(sns)
else:
serial_nos.difference_update(sns)
elif d.serial_no:
sns = get_serial_nos(d.serial_no)
if d.actual_qty > 0:
serial_nos.update(sns)
else:
serial_nos.difference_update(sns)
serial_nos = list(serial_nos)
for serial_no in ignore_serial_nos:
if serial_no in serial_nos:
serial_nos.remove(serial_no)
return serial_nos
def get_reserved_serial_nos_for_pos(kwargs): def get_reserved_serial_nos_for_pos(kwargs):
from erpnext.stock.doctype.serial_no.serial_no import get_serial_nos from erpnext.stock.doctype.serial_no.serial_no import get_serial_nos
@ -696,15 +855,14 @@ def get_auto_batch_nos(kwargs):
qty = flt(kwargs.qty) qty = flt(kwargs.qty)
batches = []
stock_ledgers_batches = get_stock_ledgers_batches(kwargs) stock_ledgers_batches = get_stock_ledgers_batches(kwargs)
if stock_ledgers_batches: if stock_ledgers_batches:
update_available_batches(available_batches, stock_ledgers_batches) update_available_batches(available_batches, stock_ledgers_batches)
if not qty: if not qty:
return batches return available_batches
batches = []
for batch in available_batches: for batch in available_batches:
if qty > 0: if qty > 0:
batch_qty = flt(batch.qty) batch_qty = flt(batch.qty)
@ -736,8 +894,8 @@ def get_auto_batch_nos(kwargs):
def update_available_batches(available_batches, reserved_batches): def update_available_batches(available_batches, reserved_batches):
for batch in available_batches: for batch in available_batches:
if batch.batch_no in reserved_batches: if batch.batch_no and batch.batch_no in reserved_batches:
available_batches[batch.batch_no] -= reserved_batches[batch.batch_no] batch.qty -= reserved_batches[batch.batch_no]
def get_available_batches(kwargs): def get_available_batches(kwargs):
@ -757,6 +915,7 @@ def get_available_batches(kwargs):
Sum(batch_ledger.qty).as_("qty"), Sum(batch_ledger.qty).as_("qty"),
) )
.where(((batch_table.expiry_date >= today()) | (batch_table.expiry_date.isnull()))) .where(((batch_table.expiry_date >= today()) | (batch_table.expiry_date.isnull())))
.where(stock_ledger_entry.is_cancelled == 0)
.groupby(batch_ledger.batch_no) .groupby(batch_ledger.batch_no)
) )
@ -781,9 +940,9 @@ def get_available_batches(kwargs):
if kwargs.get("batch_no"): if kwargs.get("batch_no"):
if isinstance(kwargs.batch_no, list): if isinstance(kwargs.batch_no, list):
query = query.where(batch_ledger.name.isin(kwargs.batch_no)) query = query.where(batch_ledger.batch_no.isin(kwargs.batch_no))
else: else:
query = query.where(batch_ledger.name == kwargs.batch_no) query = query.where(batch_ledger.batch_no == kwargs.batch_no)
if kwargs.based_on == "LIFO": if kwargs.based_on == "LIFO":
query = query.orderby(batch_table.creation, order=frappe.qb.desc) query = query.orderby(batch_table.creation, order=frappe.qb.desc)
@ -874,18 +1033,39 @@ def get_ledgers_from_serial_batch_bundle(**kwargs) -> List[frappe._dict]:
return query.run(as_dict=True) return query.run(as_dict=True)
def get_available_serial_nos(item_code, warehouse): def get_stock_ledgers_for_serial_nos(kwargs):
filters = { stock_ledger_entry = frappe.qb.DocType("Stock Ledger Entry")
"item_code": item_code,
"warehouse": ("is", "set"),
}
fields = ["name as serial_no", "warehouse", "batch_no"] query = (
frappe.qb.from_(stock_ledger_entry)
.select(
stock_ledger_entry.actual_qty,
stock_ledger_entry.serial_no,
stock_ledger_entry.serial_and_batch_bundle,
)
.where((stock_ledger_entry.is_cancelled == 0))
)
if warehouse: if kwargs.get("posting_date"):
filters["warehouse"] = warehouse if kwargs.get("posting_time") is None:
kwargs.posting_time = nowtime()
return frappe.get_all("Serial No", filters=filters, fields=fields) timestamp_condition = CombineDatetime(
stock_ledger_entry.posting_date, stock_ledger_entry.posting_time
) <= CombineDatetime(kwargs.posting_date, kwargs.posting_time)
query = query.where(timestamp_condition)
for field in ["warehouse", "item_code", "serial_no"]:
if not kwargs.get(field):
continue
if isinstance(kwargs.get(field), list):
query = query.where(stock_ledger_entry[field].isin(kwargs.get(field)))
else:
query = query.where(stock_ledger_entry[field] == kwargs.get(field))
return query.run(as_dict=True)
def get_stock_ledgers_batches(kwargs): def get_stock_ledgers_batches(kwargs):
@ -899,7 +1079,7 @@ def get_stock_ledgers_batches(kwargs):
Sum(stock_ledger_entry.actual_qty).as_("qty"), Sum(stock_ledger_entry.actual_qty).as_("qty"),
stock_ledger_entry.batch_no, stock_ledger_entry.batch_no,
) )
.where((stock_ledger_entry.is_cancelled == 0)) .where((stock_ledger_entry.is_cancelled == 0) & (stock_ledger_entry.batch_no.isnotnull()))
.groupby(stock_ledger_entry.batch_no, stock_ledger_entry.warehouse) .groupby(stock_ledger_entry.batch_no, stock_ledger_entry.warehouse)
) )

View File

@ -4,6 +4,45 @@
# import frappe # import frappe
from frappe.tests.utils import FrappeTestCase from frappe.tests.utils import FrappeTestCase
from erpnext.stock.serial_batch_bundle import get_batch_nos, get_serial_nos
class TestSerialandBatchBundle(FrappeTestCase): class TestSerialandBatchBundle(FrappeTestCase):
pass pass
def get_batch_from_bundle(bundle):
batches = get_batch_nos(bundle)
return list(batches.keys())[0]
def get_serial_nos_from_bundle(bundle):
return sorted(get_serial_nos(bundle))
def make_serial_batch_bundle(kwargs):
from erpnext.stock.serial_batch_bundle import SerialBatchCreation
sb = SerialBatchCreation(
{
"item_code": kwargs.item_code,
"warehouse": kwargs.warehouse,
"voucher_type": kwargs.voucher_type,
"voucher_no": kwargs.voucher_no,
"posting_date": kwargs.posting_date,
"posting_time": kwargs.posting_time,
"qty": kwargs.qty,
"avg_rate": kwargs.rate,
"batches": kwargs.batches,
"serial_nos": kwargs.serial_nos,
"type_of_transaction": "Inward" if kwargs.qty > 0 else "Outward",
"company": kwargs.company or "_Test Company",
"do_not_submit": kwargs.do_not_submit,
}
)
if not kwargs.get("do_not_save"):
return sb.make_serial_and_batch_bundle()
return sb

View File

@ -5,7 +5,6 @@
"editable_grid": 1, "editable_grid": 1,
"engine": "InnoDB", "engine": "InnoDB",
"field_order": [ "field_order": [
"item_code",
"serial_no", "serial_no",
"batch_no", "batch_no",
"column_break_2", "column_break_2",
@ -28,7 +27,8 @@
"in_standard_filter": 1, "in_standard_filter": 1,
"label": "Serial No", "label": "Serial No",
"mandatory_depends_on": "eval:parent.has_serial_no == 1", "mandatory_depends_on": "eval:parent.has_serial_no == 1",
"options": "Serial No" "options": "Serial No",
"search_index": 1
}, },
{ {
"depends_on": "eval:parent.has_batch_no == 1", "depends_on": "eval:parent.has_batch_no == 1",
@ -38,7 +38,8 @@
"in_standard_filter": 1, "in_standard_filter": 1,
"label": "Batch No", "label": "Batch No",
"mandatory_depends_on": "eval:parent.has_batch_no == 1", "mandatory_depends_on": "eval:parent.has_batch_no == 1",
"options": "Batch" "options": "Batch",
"search_index": 1
}, },
{ {
"default": "1", "default": "1",
@ -52,7 +53,8 @@
"fieldtype": "Link", "fieldtype": "Link",
"in_list_view": 1, "in_list_view": 1,
"label": "Warehouse", "label": "Warehouse",
"options": "Warehouse" "options": "Warehouse",
"search_index": 1
}, },
{ {
"fieldname": "column_break_2", "fieldname": "column_break_2",
@ -83,13 +85,6 @@
"fieldname": "column_break_8", "fieldname": "column_break_8",
"fieldtype": "Column Break" "fieldtype": "Column Break"
}, },
{
"fieldname": "item_code",
"fieldtype": "Link",
"label": "Item Code",
"options": "Item",
"read_only": 1
},
{ {
"fieldname": "stock_value_difference", "fieldname": "stock_value_difference",
"fieldtype": "Float", "fieldtype": "Float",
@ -114,7 +109,7 @@
"index_web_pages_for_search": 1, "index_web_pages_for_search": 1,
"istable": 1, "istable": 1,
"links": [], "links": [],
"modified": "2023-03-29 12:13:55.455738", "modified": "2023-03-31 11:18:59.809486",
"modified_by": "Administrator", "modified_by": "Administrator",
"module": "Stock", "module": "Stock",
"name": "Serial and Batch Entry", "name": "Serial and Batch Entry",

View File

@ -107,7 +107,7 @@ class SerialNo(StockController):
) )
def get_auto_serial_nos(serial_no_series, qty) -> List[str]: def get_available_serial_nos(serial_no_series, qty) -> List[str]:
serial_nos = [] serial_nos = []
for i in range(cint(qty)): for i in range(cint(qty)):
serial_nos.append(get_new_serial_number(serial_no_series)) serial_nos.append(get_new_serial_number(serial_no_series))
@ -315,10 +315,10 @@ def fetch_serial_numbers(filters, qty, do_not_include=None):
def get_serial_nos_for_outward(kwargs): def get_serial_nos_for_outward(kwargs):
from erpnext.stock.doctype.serial_and_batch_bundle.serial_and_batch_bundle import ( from erpnext.stock.doctype.serial_and_batch_bundle.serial_and_batch_bundle import (
get_auto_serial_nos, get_available_serial_nos,
) )
serial_nos = get_auto_serial_nos(kwargs) serial_nos = get_available_serial_nos(kwargs)
if not serial_nos: if not serial_nos:
return [] return []

View File

@ -1263,6 +1263,7 @@ class StockEntry(StockController):
"incoming_rate": flt(d.valuation_rate), "incoming_rate": flt(d.valuation_rate),
}, },
) )
if cstr(d.s_warehouse) or (finished_item_row and d.name == finished_item_row.name): if cstr(d.s_warehouse) or (finished_item_row and d.name == finished_item_row.name):
sle.recalculate_rate = 1 sle.recalculate_rate = 1
@ -2398,6 +2399,11 @@ class StockEntry(StockController):
@frappe.whitelist() @frappe.whitelist()
def move_sample_to_retention_warehouse(company, items): def move_sample_to_retention_warehouse(company, items):
from erpnext.stock.doctype.serial_and_batch_bundle.test_serial_and_batch_bundle import (
get_batch_from_bundle,
)
from erpnext.stock.serial_batch_bundle import SerialBatchCreation
if isinstance(items, str): if isinstance(items, str):
items = json.loads(items) items = json.loads(items)
retention_warehouse = frappe.db.get_single_value("Stock Settings", "sample_retention_warehouse") retention_warehouse = frappe.db.get_single_value("Stock Settings", "sample_retention_warehouse")
@ -2406,20 +2412,25 @@ def move_sample_to_retention_warehouse(company, items):
stock_entry.purpose = "Material Transfer" stock_entry.purpose = "Material Transfer"
stock_entry.set_stock_entry_type() stock_entry.set_stock_entry_type()
for item in items: for item in items:
if item.get("sample_quantity") and item.get("batch_no"): if item.get("sample_quantity") and item.get("serial_and_batch_bundle"):
batch_no = get_batch_from_bundle(item.get("serial_and_batch_bundle"))
sample_quantity = validate_sample_quantity( sample_quantity = validate_sample_quantity(
item.get("item_code"), item.get("item_code"),
item.get("sample_quantity"), item.get("sample_quantity"),
item.get("transfer_qty") or item.get("qty"), item.get("transfer_qty") or item.get("qty"),
item.get("batch_no"), batch_no,
) )
if sample_quantity: if sample_quantity:
sample_serial_nos = "" cls_obj = SerialBatchCreation(
if item.get("serial_no"): {
serial_nos = (item.get("serial_no")).split() "type_of_transaction": "Outward",
if serial_nos and len(serial_nos) > item.get("sample_quantity"): "serial_and_batch_bundle": item.get("serial_and_batch_bundle"),
serial_no_list = serial_nos[: -(len(serial_nos) - item.get("sample_quantity"))] "item_code": item.get("item_code"),
sample_serial_nos = "\n".join(serial_no_list) }
)
cls_obj.duplicate_package()
stock_entry.append( stock_entry.append(
"items", "items",
@ -2432,8 +2443,7 @@ def move_sample_to_retention_warehouse(company, items):
"uom": item.get("uom"), "uom": item.get("uom"),
"stock_uom": item.get("stock_uom"), "stock_uom": item.get("stock_uom"),
"conversion_factor": item.get("conversion_factor") or 1.0, "conversion_factor": item.get("conversion_factor") or 1.0,
"serial_no": sample_serial_nos, "serial_and_batch_bundle": cls_obj.serial_and_batch_bundle,
"batch_no": item.get("batch_no"),
}, },
) )
if stock_entry.get("items"): if stock_entry.get("items"):

View File

@ -133,8 +133,12 @@ def make_stock_entry(**args):
serial_number = args.serial_no serial_number = args.serial_no
bundle_id = None bundle_id = None
if not args.serial_no and args.qty and args.batch_no: if args.serial_no or args.batch_no or args.batches:
batches = frappe._dict({args.batch_no: args.qty}) batches = frappe._dict({})
if args.batch_no:
batches = frappe._dict({args.batch_no: args.qty})
elif args.batches:
batches = args.batches
bundle_id = ( bundle_id = (
SerialBatchCreation( SerialBatchCreation(
@ -144,8 +148,13 @@ def make_stock_entry(**args):
"voucher_type": "Stock Entry", "voucher_type": "Stock Entry",
"total_qty": args.qty * (-1 if args.source else 1), "total_qty": args.qty * (-1 if args.source else 1),
"batches": batches, "batches": batches,
"serial_nos": args.serial_no,
"type_of_transaction": "Outward" if args.source else "Inward", "type_of_transaction": "Outward" if args.source else "Inward",
"company": s.company, "company": s.company,
"posting_date": s.posting_date,
"posting_time": s.posting_time,
"rate": args.rate or args.basic_rate,
"do_not_submit": True,
} }
) )
.make_serial_and_batch_bundle() .make_serial_and_batch_bundle()
@ -178,6 +187,6 @@ def make_stock_entry(**args):
if not args.do_not_submit: if not args.do_not_submit:
s.submit() s.submit()
s.load_from_db() s.load_from_db()
return s return s

View File

@ -14,12 +14,13 @@ from erpnext.stock.doctype.item.test_item import (
make_item_variant, make_item_variant,
set_item_variant_settings, set_item_variant_settings,
) )
from erpnext.stock.doctype.serial_no.serial_no import * # noqa from erpnext.stock.doctype.serial_and_batch_bundle.test_serial_and_batch_bundle import (
from erpnext.stock.doctype.stock_entry.stock_entry import ( get_batch_from_bundle,
FinishedGoodError, get_serial_nos_from_bundle,
make_stock_in_entry, make_serial_batch_bundle,
move_sample_to_retention_warehouse,
) )
from erpnext.stock.doctype.serial_no.serial_no import * # noqa
from erpnext.stock.doctype.stock_entry.stock_entry import FinishedGoodError, make_stock_in_entry
from erpnext.stock.doctype.stock_entry.stock_entry_utils import make_stock_entry from erpnext.stock.doctype.stock_entry.stock_entry_utils import make_stock_entry
from erpnext.stock.doctype.stock_ledger_entry.stock_ledger_entry import StockFreezeError from erpnext.stock.doctype.stock_ledger_entry.stock_ledger_entry import StockFreezeError
from erpnext.stock.doctype.stock_reconciliation.stock_reconciliation import ( from erpnext.stock.doctype.stock_reconciliation.stock_reconciliation import (
@ -28,6 +29,7 @@ from erpnext.stock.doctype.stock_reconciliation.stock_reconciliation import (
from erpnext.stock.doctype.stock_reconciliation.test_stock_reconciliation import ( from erpnext.stock.doctype.stock_reconciliation.test_stock_reconciliation import (
create_stock_reconciliation, create_stock_reconciliation,
) )
from erpnext.stock.serial_batch_bundle import SerialBatchCreation
from erpnext.stock.stock_ledger import NegativeStockError, get_previous_sle from erpnext.stock.stock_ledger import NegativeStockError, get_previous_sle
@ -549,28 +551,47 @@ class TestStockEntry(FrappeTestCase):
def test_serial_no_not_reqd(self): def test_serial_no_not_reqd(self):
se = frappe.copy_doc(test_records[0]) se = frappe.copy_doc(test_records[0])
se.get("items")[0].serial_no = "ABCD" se.get("items")[0].serial_no = "ABCD"
se.set_stock_entry_type()
se.insert() bundle_id = make_serial_batch_bundle(
self.assertRaises(SerialNoNotRequiredError, se.submit) frappe._dict(
{
"item_code": se.get("items")[0].item_code,
"warehouse": se.get("items")[0].t_warehouse,
"company": se.company,
"qty": 2,
"voucher_type": "Stock Entry",
"serial_nos": ["ABCD"],
"posting_date": se.posting_date,
"posting_time": se.posting_time,
"do_not_save": True,
}
)
)
self.assertRaises(frappe.ValidationError, bundle_id.make_serial_and_batch_bundle)
def test_serial_no_reqd(self): def test_serial_no_reqd(self):
se = frappe.copy_doc(test_records[0]) se = frappe.copy_doc(test_records[0])
se.get("items")[0].item_code = "_Test Serialized Item" se.get("items")[0].item_code = "_Test Serialized Item"
se.get("items")[0].qty = 2 se.get("items")[0].qty = 2
se.get("items")[0].transfer_qty = 2 se.get("items")[0].transfer_qty = 2
se.set_stock_entry_type()
se.insert()
self.assertRaises(SerialNoRequiredError, se.submit)
def test_serial_no_qty_more(self): bundle_id = make_serial_batch_bundle(
se = frappe.copy_doc(test_records[0]) frappe._dict(
se.get("items")[0].item_code = "_Test Serialized Item" {
se.get("items")[0].qty = 2 "item_code": se.get("items")[0].item_code,
se.get("items")[0].serial_no = "ABCD\nEFGH\nXYZ" "warehouse": se.get("items")[0].t_warehouse,
se.get("items")[0].transfer_qty = 2 "company": se.company,
se.set_stock_entry_type() "qty": 2,
se.insert() "voucher_type": "Stock Entry",
self.assertRaises(SerialNoQtyError, se.submit) "posting_date": se.posting_date,
"posting_time": se.posting_time,
"do_not_save": True,
}
)
)
self.assertRaises(frappe.ValidationError, bundle_id.make_serial_and_batch_bundle)
def test_serial_no_qty_less(self): def test_serial_no_qty_less(self):
se = frappe.copy_doc(test_records[0]) se = frappe.copy_doc(test_records[0])
@ -578,91 +599,85 @@ class TestStockEntry(FrappeTestCase):
se.get("items")[0].qty = 2 se.get("items")[0].qty = 2
se.get("items")[0].serial_no = "ABCD" se.get("items")[0].serial_no = "ABCD"
se.get("items")[0].transfer_qty = 2 se.get("items")[0].transfer_qty = 2
se.set_stock_entry_type()
se.insert() bundle_id = make_serial_batch_bundle(
self.assertRaises(SerialNoQtyError, se.submit) frappe._dict(
{
"item_code": se.get("items")[0].item_code,
"warehouse": se.get("items")[0].t_warehouse,
"company": se.company,
"qty": 2,
"serial_nos": ["ABCD"],
"voucher_type": "Stock Entry",
"posting_date": se.posting_date,
"posting_time": se.posting_time,
"do_not_save": True,
}
)
)
self.assertRaises(frappe.ValidationError, bundle_id.make_serial_and_batch_bundle)
def test_serial_no_transfer_in(self): def test_serial_no_transfer_in(self):
serial_nos = ["ABCD1", "EFGH1"]
for serial_no in serial_nos:
if not frappe.db.exists("Serial No", serial_no):
doc = frappe.new_doc("Serial No")
doc.serial_no = serial_no
doc.item_code = "_Test Serialized Item"
doc.insert(ignore_permissions=True)
se = frappe.copy_doc(test_records[0]) se = frappe.copy_doc(test_records[0])
se.get("items")[0].item_code = "_Test Serialized Item" se.get("items")[0].item_code = "_Test Serialized Item"
se.get("items")[0].qty = 2 se.get("items")[0].qty = 2
se.get("items")[0].serial_no = "ABCD\nEFGH"
se.get("items")[0].transfer_qty = 2 se.get("items")[0].transfer_qty = 2
se.set_stock_entry_type() se.set_stock_entry_type()
se.get("items")[0].serial_and_batch_bundle = make_serial_batch_bundle(
frappe._dict(
{
"item_code": se.get("items")[0].item_code,
"warehouse": se.get("items")[0].t_warehouse,
"company": se.company,
"qty": 2,
"voucher_type": "Stock Entry",
"serial_nos": serial_nos,
"posting_date": se.posting_date,
"posting_time": se.posting_time,
"do_not_submit": True,
}
)
)
se.insert() se.insert()
se.submit() se.submit()
self.assertTrue(frappe.db.exists("Serial No", "ABCD")) self.assertTrue(frappe.db.get_value("Serial No", "ABCD1", "warehouse"))
self.assertTrue(frappe.db.exists("Serial No", "EFGH")) self.assertTrue(frappe.db.get_value("Serial No", "EFGH1", "warehouse"))
se.cancel() se.cancel()
self.assertFalse(frappe.db.get_value("Serial No", "ABCD", "warehouse")) self.assertFalse(frappe.db.get_value("Serial No", "ABCD1", "warehouse"))
def test_serial_no_not_exists(self):
frappe.db.sql("delete from `tabSerial No` where name in ('ABCD', 'EFGH')")
make_serialized_item(target_warehouse="_Test Warehouse 1 - _TC")
se = frappe.copy_doc(test_records[0])
se.purpose = "Material Issue"
se.get("items")[0].item_code = "_Test Serialized Item With Series"
se.get("items")[0].qty = 2
se.get("items")[0].s_warehouse = "_Test Warehouse 1 - _TC"
se.get("items")[0].t_warehouse = None
se.get("items")[0].serial_no = "ABCD\nEFGH"
se.get("items")[0].transfer_qty = 2
se.set_stock_entry_type()
se.insert()
self.assertRaises(SerialNoNotExistsError, se.submit)
def test_serial_duplicate(self):
se, serial_nos = self.test_serial_by_series()
se = frappe.copy_doc(test_records[0])
se.get("items")[0].item_code = "_Test Serialized Item With Series"
se.get("items")[0].qty = 1
se.get("items")[0].serial_no = serial_nos[0]
se.get("items")[0].transfer_qty = 1
se.set_stock_entry_type()
se.insert()
self.assertRaises(SerialNoDuplicateError, se.submit)
def test_serial_by_series(self): def test_serial_by_series(self):
se = make_serialized_item() se = make_serialized_item()
serial_nos = get_serial_nos(se.get("items")[0].serial_no) serial_nos = get_serial_nos_from_bundle(se.get("items")[0].serial_and_batch_bundle)
self.assertTrue(frappe.db.exists("Serial No", serial_nos[0])) self.assertTrue(frappe.db.exists("Serial No", serial_nos[0]))
self.assertTrue(frappe.db.exists("Serial No", serial_nos[1])) self.assertTrue(frappe.db.exists("Serial No", serial_nos[1]))
return se, serial_nos return se, serial_nos
def test_serial_item_error(self):
se, serial_nos = self.test_serial_by_series()
if not frappe.db.exists("Serial No", "ABCD"):
make_serialized_item(item_code="_Test Serialized Item", serial_no="ABCD\nEFGH")
se = frappe.copy_doc(test_records[0])
se.purpose = "Material Transfer"
se.get("items")[0].item_code = "_Test Serialized Item"
se.get("items")[0].qty = 1
se.get("items")[0].transfer_qty = 1
se.get("items")[0].serial_no = serial_nos[0]
se.get("items")[0].s_warehouse = "_Test Warehouse - _TC"
se.get("items")[0].t_warehouse = "_Test Warehouse 1 - _TC"
se.set_stock_entry_type()
se.insert()
self.assertRaises(SerialNoItemError, se.submit)
def test_serial_move(self): def test_serial_move(self):
se = make_serialized_item() se = make_serialized_item()
serial_no = get_serial_nos(se.get("items")[0].serial_no)[0] serial_no = get_serial_nos_from_bundle(se.get("items")[0].serial_and_batch_bundle)[0]
se = frappe.copy_doc(test_records[0]) se = frappe.copy_doc(test_records[0])
se.purpose = "Material Transfer" se.purpose = "Material Transfer"
se.get("items")[0].item_code = "_Test Serialized Item With Series" se.get("items")[0].item_code = "_Test Serialized Item With Series"
se.get("items")[0].qty = 1 se.get("items")[0].qty = 1
se.get("items")[0].transfer_qty = 1 se.get("items")[0].transfer_qty = 1
se.get("items")[0].serial_no = serial_no se.get("items")[0].serial_no = [serial_no]
se.get("items")[0].s_warehouse = "_Test Warehouse - _TC" se.get("items")[0].s_warehouse = "_Test Warehouse - _TC"
se.get("items")[0].t_warehouse = "_Test Warehouse 1 - _TC" se.get("items")[0].t_warehouse = "_Test Warehouse 1 - _TC"
se.set_stock_entry_type() se.set_stock_entry_type()
@ -677,29 +692,12 @@ class TestStockEntry(FrappeTestCase):
frappe.db.get_value("Serial No", serial_no, "warehouse"), "_Test Warehouse - _TC" frappe.db.get_value("Serial No", serial_no, "warehouse"), "_Test Warehouse - _TC"
) )
def test_serial_warehouse_error(self):
make_serialized_item(target_warehouse="_Test Warehouse 1 - _TC")
t = make_serialized_item()
serial_nos = get_serial_nos(t.get("items")[0].serial_no)
se = frappe.copy_doc(test_records[0])
se.purpose = "Material Transfer"
se.get("items")[0].item_code = "_Test Serialized Item With Series"
se.get("items")[0].qty = 1
se.get("items")[0].transfer_qty = 1
se.get("items")[0].serial_no = serial_nos[0]
se.get("items")[0].s_warehouse = "_Test Warehouse 1 - _TC"
se.get("items")[0].t_warehouse = "_Test Warehouse - _TC"
se.set_stock_entry_type()
se.insert()
self.assertRaises(SerialNoWarehouseError, se.submit)
def test_serial_cancel(self): def test_serial_cancel(self):
se, serial_nos = self.test_serial_by_series() se, serial_nos = self.test_serial_by_series()
se.load_from_db()
se.cancel() se.cancel()
serial_no = get_serial_nos(se.get("items")[0].serial_no)[0] serial_no = get_serial_nos_from_bundle(se.get("items")[0].serial_and_batch_bundle)[0]
self.assertFalse(frappe.db.get_value("Serial No", serial_no, "warehouse")) self.assertFalse(frappe.db.get_value("Serial No", serial_no, "warehouse"))
def test_serial_batch_item_stock_entry(self): def test_serial_batch_item_stock_entry(self):
@ -726,8 +724,8 @@ class TestStockEntry(FrappeTestCase):
se = make_stock_entry( se = make_stock_entry(
item_code=item.item_code, target="_Test Warehouse - _TC", qty=1, basic_rate=100 item_code=item.item_code, target="_Test Warehouse - _TC", qty=1, basic_rate=100
) )
batch_no = se.items[0].batch_no batch_no = get_batch_from_bundle(se.items[0].serial_and_batch_bundle)
serial_no = get_serial_nos(se.items[0].serial_no)[0] serial_no = get_serial_nos_from_bundle(se.items[0].serial_and_batch_bundle)[0]
batch_qty = get_batch_qty(batch_no, "_Test Warehouse - _TC", item.item_code) batch_qty = get_batch_qty(batch_no, "_Test Warehouse - _TC", item.item_code)
batch_in_serial_no = frappe.db.get_value("Serial No", serial_no, "batch_no") batch_in_serial_no = frappe.db.get_value("Serial No", serial_no, "batch_no")
@ -738,10 +736,7 @@ class TestStockEntry(FrappeTestCase):
se.cancel() se.cancel()
batch_in_serial_no = frappe.db.get_value("Serial No", serial_no, "batch_no") batch_in_serial_no = frappe.db.get_value("Serial No", serial_no, "batch_no")
self.assertEqual(batch_in_serial_no, None) self.assertEqual(frappe.db.get_value("Serial No", serial_no, "warehouse"), None)
self.assertEqual(frappe.db.get_value("Serial No", serial_no, "status"), "Inactive")
self.assertEqual(frappe.db.exists("Batch", batch_no), None)
def test_serial_batch_item_qty_deduction(self): def test_serial_batch_item_qty_deduction(self):
""" """
@ -768,8 +763,8 @@ class TestStockEntry(FrappeTestCase):
se1 = make_stock_entry( se1 = make_stock_entry(
item_code=item.item_code, target="_Test Warehouse - _TC", qty=1, basic_rate=100 item_code=item.item_code, target="_Test Warehouse - _TC", qty=1, basic_rate=100
) )
batch_no = se1.items[0].batch_no batch_no = get_batch_from_bundle(se1.items[0].serial_and_batch_bundle)
serial_no1 = get_serial_nos(se1.items[0].serial_no)[0] serial_no1 = get_serial_nos_from_bundle(se1.items[0].serial_and_batch_bundle)[0]
# Check Source (Origin) Document of Batch # Check Source (Origin) Document of Batch
self.assertEqual(frappe.db.get_value("Batch", batch_no, "reference_name"), se1.name) self.assertEqual(frappe.db.get_value("Batch", batch_no, "reference_name"), se1.name)
@ -781,7 +776,7 @@ class TestStockEntry(FrappeTestCase):
basic_rate=100, basic_rate=100,
batch_no=batch_no, batch_no=batch_no,
) )
serial_no2 = get_serial_nos(se2.items[0].serial_no)[0] serial_no2 = get_serial_nos_from_bundle(se2.items[0].serial_and_batch_bundle)[0]
batch_qty = get_batch_qty(batch_no, "_Test Warehouse - _TC", item.item_code) batch_qty = get_batch_qty(batch_no, "_Test Warehouse - _TC", item.item_code)
self.assertEqual(batch_qty, 2) self.assertEqual(batch_qty, 2)
@ -798,7 +793,7 @@ class TestStockEntry(FrappeTestCase):
# Check if Serial No from Stock Entry 2 is Unlinked and Inactive # Check if Serial No from Stock Entry 2 is Unlinked and Inactive
self.assertEqual(frappe.db.get_value("Serial No", serial_no2, "batch_no"), None) self.assertEqual(frappe.db.get_value("Serial No", serial_no2, "batch_no"), None)
self.assertEqual(frappe.db.get_value("Serial No", serial_no2, "status"), "Inactive") self.assertEqual(frappe.db.get_value("Serial No", serial_no2, "warehouse"), None)
def test_warehouse_company_validation(self): def test_warehouse_company_validation(self):
company = frappe.db.get_value("Warehouse", "_Test Warehouse 2 - _TC1", "company") company = frappe.db.get_value("Warehouse", "_Test Warehouse 2 - _TC1", "company")
@ -1004,7 +999,7 @@ class TestStockEntry(FrappeTestCase):
def test_same_serial_nos_in_repack_or_manufacture_entries(self): def test_same_serial_nos_in_repack_or_manufacture_entries(self):
s1 = make_serialized_item(target_warehouse="_Test Warehouse - _TC") s1 = make_serialized_item(target_warehouse="_Test Warehouse - _TC")
serial_nos = s1.get("items")[0].serial_no serial_nos = get_serial_nos_from_bundle(s1.get("items")[0].serial_and_batch_bundle)
s2 = make_stock_entry( s2 = make_stock_entry(
item_code="_Test Serialized Item With Series", item_code="_Test Serialized Item With Series",
@ -1016,6 +1011,26 @@ class TestStockEntry(FrappeTestCase):
do_not_save=True, do_not_save=True,
) )
cls_obj = SerialBatchCreation(
{
"type_of_transaction": "Inward",
"serial_and_batch_bundle": s2.items[0].serial_and_batch_bundle,
"item_code": "_Test Serialized Item",
}
)
cls_obj.duplicate_package()
bundle_id = cls_obj.serial_and_batch_bundle
doc = frappe.get_doc("Serial and Batch Bundle", bundle_id)
doc.db_set(
{
"item_code": "_Test Serialized Item",
"warehouse": "_Test Warehouse - _TC",
}
)
doc.load_from_db()
s2.append( s2.append(
"items", "items",
{ {
@ -1026,90 +1041,90 @@ class TestStockEntry(FrappeTestCase):
"expense_account": "Stock Adjustment - _TC", "expense_account": "Stock Adjustment - _TC",
"conversion_factor": 1.0, "conversion_factor": 1.0,
"cost_center": "_Test Cost Center - _TC", "cost_center": "_Test Cost Center - _TC",
"serial_no": serial_nos, "serial_and_batch_bundle": bundle_id,
}, },
) )
s2.submit() s2.submit()
s2.cancel() s2.cancel()
def test_retain_sample(self): # def test_retain_sample(self):
from erpnext.stock.doctype.batch.batch import get_batch_qty # from erpnext.stock.doctype.batch.batch import get_batch_qty
from erpnext.stock.doctype.warehouse.test_warehouse import create_warehouse # from erpnext.stock.doctype.warehouse.test_warehouse import create_warehouse
create_warehouse("Test Warehouse for Sample Retention") # create_warehouse("Test Warehouse for Sample Retention")
frappe.db.set_value( # frappe.db.set_value(
"Stock Settings", # "Stock Settings",
None, # None,
"sample_retention_warehouse", # "sample_retention_warehouse",
"Test Warehouse for Sample Retention - _TC", # "Test Warehouse for Sample Retention - _TC",
) # )
test_item_code = "Retain Sample Item" # test_item_code = "Retain Sample Item"
if not frappe.db.exists("Item", test_item_code): # if not frappe.db.exists("Item", test_item_code):
item = frappe.new_doc("Item") # item = frappe.new_doc("Item")
item.item_code = test_item_code # item.item_code = test_item_code
item.item_name = "Retain Sample Item" # item.item_name = "Retain Sample Item"
item.description = "Retain Sample Item" # item.description = "Retain Sample Item"
item.item_group = "All Item Groups" # item.item_group = "All Item Groups"
item.is_stock_item = 1 # item.is_stock_item = 1
item.has_batch_no = 1 # item.has_batch_no = 1
item.create_new_batch = 1 # item.create_new_batch = 1
item.retain_sample = 1 # item.retain_sample = 1
item.sample_quantity = 4 # item.sample_quantity = 4
item.save() # item.save()
receipt_entry = frappe.new_doc("Stock Entry") # receipt_entry = frappe.new_doc("Stock Entry")
receipt_entry.company = "_Test Company" # receipt_entry.company = "_Test Company"
receipt_entry.purpose = "Material Receipt" # receipt_entry.purpose = "Material Receipt"
receipt_entry.append( # receipt_entry.append(
"items", # "items",
{ # {
"item_code": test_item_code, # "item_code": test_item_code,
"t_warehouse": "_Test Warehouse - _TC", # "t_warehouse": "_Test Warehouse - _TC",
"qty": 40, # "qty": 40,
"basic_rate": 12, # "basic_rate": 12,
"cost_center": "_Test Cost Center - _TC", # "cost_center": "_Test Cost Center - _TC",
"sample_quantity": 4, # "sample_quantity": 4,
}, # },
) # )
receipt_entry.set_stock_entry_type() # receipt_entry.set_stock_entry_type()
receipt_entry.insert() # receipt_entry.insert()
receipt_entry.submit() # receipt_entry.submit()
retention_data = move_sample_to_retention_warehouse( # retention_data = move_sample_to_retention_warehouse(
receipt_entry.company, receipt_entry.get("items") # receipt_entry.company, receipt_entry.get("items")
) # )
retention_entry = frappe.new_doc("Stock Entry") # retention_entry = frappe.new_doc("Stock Entry")
retention_entry.company = retention_data.company # retention_entry.company = retention_data.company
retention_entry.purpose = retention_data.purpose # retention_entry.purpose = retention_data.purpose
retention_entry.append( # retention_entry.append(
"items", # "items",
{ # {
"item_code": test_item_code, # "item_code": test_item_code,
"t_warehouse": "Test Warehouse for Sample Retention - _TC", # "t_warehouse": "Test Warehouse for Sample Retention - _TC",
"s_warehouse": "_Test Warehouse - _TC", # "s_warehouse": "_Test Warehouse - _TC",
"qty": 4, # "qty": 4,
"basic_rate": 12, # "basic_rate": 12,
"cost_center": "_Test Cost Center - _TC", # "cost_center": "_Test Cost Center - _TC",
"batch_no": receipt_entry.get("items")[0].batch_no, # "batch_no": get_batch_from_bundle(receipt_entry.get("items")[0].serial_and_batch_bundle),
}, # },
) # )
retention_entry.set_stock_entry_type() # retention_entry.set_stock_entry_type()
retention_entry.insert() # retention_entry.insert()
retention_entry.submit() # retention_entry.submit()
qty_in_usable_warehouse = get_batch_qty( # qty_in_usable_warehouse = get_batch_qty(
receipt_entry.get("items")[0].batch_no, "_Test Warehouse - _TC", "_Test Item" # get_batch_from_bundle(receipt_entry.get("items")[0].serial_and_batch_bundle), "_Test Warehouse - _TC", "_Test Item"
) # )
qty_in_retention_warehouse = get_batch_qty( # qty_in_retention_warehouse = get_batch_qty(
receipt_entry.get("items")[0].batch_no, # get_batch_from_bundle(receipt_entry.get("items")[0].serial_and_batch_bundle),
"Test Warehouse for Sample Retention - _TC", # "Test Warehouse for Sample Retention - _TC",
"_Test Item", # "_Test Item",
) # )
self.assertEqual(qty_in_usable_warehouse, 36) # self.assertEqual(qty_in_usable_warehouse, 36)
self.assertEqual(qty_in_retention_warehouse, 4) # self.assertEqual(qty_in_retention_warehouse, 4)
def test_quality_check(self): def test_quality_check(self):
item_code = "_Test Item For QC" item_code = "_Test Item For QC"
@ -1403,7 +1418,7 @@ class TestStockEntry(FrappeTestCase):
posting_date="2021-09-01", posting_date="2021-09-01",
purpose="Material Receipt", purpose="Material Receipt",
) )
batch_nos.append(se1.items[0].batch_no) batch_nos.append(get_batch_from_bundle(se1.items[0].serial_and_batch_bundle))
se2 = make_stock_entry( se2 = make_stock_entry(
item_code=item_code, item_code=item_code,
qty=2, qty=2,
@ -1411,9 +1426,9 @@ class TestStockEntry(FrappeTestCase):
posting_date="2021-09-03", posting_date="2021-09-03",
purpose="Material Receipt", purpose="Material Receipt",
) )
batch_nos.append(se2.items[0].batch_no) batch_nos.append(get_batch_from_bundle(se2.items[0].serial_and_batch_bundle))
with self.assertRaises(NegativeStockError) as nse: with self.assertRaises(frappe.ValidationError) as nse:
make_stock_entry( make_stock_entry(
item_code=item_code, item_code=item_code,
qty=1, qty=1,
@ -1434,8 +1449,6 @@ class TestStockEntry(FrappeTestCase):
""" """
from erpnext.stock.doctype.batch.test_batch import TestBatch from erpnext.stock.doctype.batch.test_batch import TestBatch
batch_nos = []
item_code = "_TestMultibatchFifo" item_code = "_TestMultibatchFifo"
TestBatch.make_batch_item(item_code) TestBatch.make_batch_item(item_code)
warehouse = "_Test Warehouse - _TC" warehouse = "_Test Warehouse - _TC"
@ -1452,18 +1465,25 @@ class TestStockEntry(FrappeTestCase):
) )
receipt.save() receipt.save()
receipt.submit() receipt.submit()
batch_nos.extend(row.batch_no for row in receipt.items) receipt.load_from_db()
batches = frappe._dict(
{get_batch_from_bundle(row.serial_and_batch_bundle): row.qty for row in receipt.items}
)
self.assertEqual(receipt.value_difference, 30) self.assertEqual(receipt.value_difference, 30)
issue = make_stock_entry( issue = make_stock_entry(
item_code=item_code, qty=1, from_warehouse=warehouse, purpose="Material Issue", do_not_save=True item_code=item_code,
qty=2,
from_warehouse=warehouse,
purpose="Material Issue",
do_not_save=True,
batches=batches,
) )
issue.append("items", frappe.copy_doc(issue.items[0], ignore_no_copy=False))
for row, batch_no in zip(issue.items, batch_nos):
row.batch_no = batch_no
issue.save() issue.save()
issue.submit() issue.submit()
issue.reload() # reload because reposting current voucher updates rate issue.reload() # reload because reposting current voucher updates rate
self.assertEqual(issue.value_difference, -30) self.assertEqual(issue.value_difference, -30)
@ -1745,10 +1765,31 @@ def make_serialized_item(**args):
if args.company: if args.company:
se.company = args.company se.company = args.company
if args.target_warehouse:
se.get("items")[0].t_warehouse = args.target_warehouse
se.get("items")[0].item_code = args.item_code or "_Test Serialized Item With Series" se.get("items")[0].item_code = args.item_code or "_Test Serialized Item With Series"
if args.serial_no: if args.serial_no:
se.get("items")[0].serial_no = args.serial_no serial_nos = args.serial_no
if isinstance(serial_nos, str):
serial_nos = [serial_nos]
se.get("items")[0].serial_and_batch_bundle = make_serial_batch_bundle(
frappe._dict(
{
"item_code": se.get("items")[0].item_code,
"warehouse": se.get("items")[0].t_warehouse,
"company": se.company,
"qty": 2,
"voucher_type": "Stock Entry",
"serial_nos": serial_nos,
"posting_date": today(),
"posting_time": nowtime(),
"do_not_submit": True,
}
)
)
if args.cost_center: if args.cost_center:
se.get("items")[0].cost_center = args.cost_center se.get("items")[0].cost_center = args.cost_center
@ -1759,9 +1800,6 @@ def make_serialized_item(**args):
se.get("items")[0].qty = 2 se.get("items")[0].qty = 2
se.get("items")[0].transfer_qty = 2 se.get("items")[0].transfer_qty = 2
if args.target_warehouse:
se.get("items")[0].t_warehouse = args.target_warehouse
se.set_stock_entry_type() se.set_stock_entry_type()
se.insert() se.insert()
se.submit() se.submit()

View File

@ -104,13 +104,6 @@ class StockLedgerEntry(Document):
if item_detail.has_serial_no or item_detail.has_batch_no: if item_detail.has_serial_no or item_detail.has_batch_no:
if not self.serial_and_batch_bundle: if not self.serial_and_batch_bundle:
self.throw_error_message(f"Serial No / Batch No are mandatory for Item {self.item_code}") self.throw_error_message(f"Serial No / Batch No are mandatory for Item {self.item_code}")
else:
bundle_data = frappe.get_cached_value(
"Serial and Batch Bundle", self.serial_and_batch_bundle, ["item_code", "docstatus"], as_dict=1
)
if bundle_data.docstatus != 1:
self.submit_serial_and_batch_bundle()
if self.serial_and_batch_bundle and not (item_detail.has_serial_no or item_detail.has_batch_no): if self.serial_and_batch_bundle and not (item_detail.has_serial_no or item_detail.has_batch_no):
self.throw_error_message(f"Serial No and Batch No are not allowed for Item {self.item_code}") self.throw_error_message(f"Serial No and Batch No are not allowed for Item {self.item_code}")
@ -118,10 +111,6 @@ class StockLedgerEntry(Document):
def throw_error_message(self, message, exception=frappe.ValidationError): def throw_error_message(self, message, exception=frappe.ValidationError):
frappe.throw(_(message), exception) frappe.throw(_(message), exception)
def submit_serial_and_batch_bundle(self):
doc = frappe.get_doc("Serial and Batch Bundle", self.serial_and_batch_bundle)
doc.submit()
def check_stock_frozen_date(self): def check_stock_frozen_date(self):
stock_settings = frappe.get_cached_doc("Stock Settings") stock_settings = frappe.get_cached_doc("Stock Settings")

View File

@ -5,6 +5,10 @@ frappe.provide("erpnext.stock");
frappe.provide("erpnext.accounts.dimensions"); frappe.provide("erpnext.accounts.dimensions");
frappe.ui.form.on("Stock Reconciliation", { frappe.ui.form.on("Stock Reconciliation", {
setup(frm) {
frm.ignore_doctypes_on_cancel_all = ['Serial and Batch Bundle'];
},
onload: function(frm) { onload: function(frm) {
frm.add_fetch("item_code", "item_name", "item_name"); frm.add_fetch("item_code", "item_name", "item_name");

View File

@ -11,9 +11,8 @@ from frappe.utils import cint, cstr, flt
import erpnext import erpnext
from erpnext.accounts.utils import get_company_default from erpnext.accounts.utils import get_company_default
from erpnext.controllers.stock_controller import StockController from erpnext.controllers.stock_controller import StockController
from erpnext.stock.doctype.batch.batch import get_batch_qty from erpnext.stock.doctype.batch.batch import get_available_batches, get_batch_qty
from erpnext.stock.doctype.serial_and_batch_bundle.serial_and_batch_bundle import ( from erpnext.stock.doctype.serial_and_batch_bundle.serial_and_batch_bundle import (
get_auto_batch_nos,
get_available_serial_nos, get_available_serial_nos,
) )
from erpnext.stock.doctype.serial_no.serial_no import get_serial_nos from erpnext.stock.doctype.serial_no.serial_no import get_serial_nos
@ -56,7 +55,7 @@ class StockReconciliation(StockController):
self.validate_reserved_stock() self.validate_reserved_stock()
def on_update(self): def on_update(self):
self.set_serial_and_batch_bundle() self.set_serial_and_batch_bundle(ignore_validate=True)
def on_submit(self): def on_submit(self):
self.update_stock_ledger() self.update_stock_ledger()
@ -83,9 +82,10 @@ class StockReconciliation(StockController):
"Item", item.item_code, ["has_serial_no", "has_batch_no"], as_dict=1 "Item", item.item_code, ["has_serial_no", "has_batch_no"], as_dict=1
) )
if ( if not (item_details.has_serial_no or item_details.has_batch_no):
item_details.has_serial_no or item_details.has_batch_no continue
) and not item.current_serial_and_batch_bundle:
if not item.current_serial_and_batch_bundle:
serial_and_batch_bundle = frappe.get_doc( serial_and_batch_bundle = frappe.get_doc(
{ {
"doctype": "Serial and Batch Bundle", "doctype": "Serial and Batch Bundle",
@ -94,46 +94,67 @@ class StockReconciliation(StockController):
"posting_date": self.posting_date, "posting_date": self.posting_date,
"posting_time": self.posting_time, "posting_time": self.posting_time,
"voucher_type": self.doctype, "voucher_type": self.doctype,
"voucher_no": self.name,
"type_of_transaction": "Outward", "type_of_transaction": "Outward",
} }
) )
else:
serial_and_batch_bundle = frappe.get_doc(
"Serial and Batch Bundle", item.current_serial_and_batch_bundle
)
if item_details.has_serial_no: serial_and_batch_bundle.set("entries", [])
serial_nos_details = get_available_serial_nos(item.item_code, item.warehouse)
for serial_no_row in serial_nos_details: if item_details.has_serial_no:
serial_and_batch_bundle.append( serial_nos_details = get_available_serial_nos(
"entries", frappe._dict(
{ {
"serial_no": serial_no_row.serial_no, "item_code": item.item_code,
"qty": -1, "warehouse": item.warehouse,
"warehouse": serial_no_row.warehouse, "posting_date": self.posting_date,
"batch_no": serial_no_row.batch_no, "posting_time": self.posting_time,
}, }
) )
)
if item_details.has_batch_no: for serial_no_row in serial_nos_details:
batch_nos_details = get_auto_batch_nos( serial_and_batch_bundle.append(
frappe._dict( "entries",
{ {
"item_code": item.item_code, "serial_no": serial_no_row.serial_no,
"warehouse": item.warehouse, "qty": -1,
} "warehouse": serial_no_row.warehouse,
) "batch_no": serial_no_row.batch_no,
},
) )
for batch_no, qty in batch_nos_details.items(): if item_details.has_batch_no:
serial_and_batch_bundle.append( batch_nos_details = get_available_batches(
"entries", frappe._dict(
{ {
"batch_no": batch_no, "item_code": item.item_code,
"qty": qty * -1, "warehouse": item.warehouse,
"warehouse": item.warehouse, "posting_date": self.posting_date,
}, "posting_time": self.posting_time,
) }
)
)
item.current_serial_and_batch_bundle = serial_and_batch_bundle.save().name for batch_no, qty in batch_nos_details.items():
serial_and_batch_bundle.append(
"entries",
{
"batch_no": batch_no,
"qty": qty * -1,
"warehouse": item.warehouse,
},
)
if not serial_and_batch_bundle.entries:
continue
item.current_serial_and_batch_bundle = serial_and_batch_bundle.save().name
item.current_qty = abs(serial_and_batch_bundle.total_qty)
item.current_valuation_rate = abs(serial_and_batch_bundle.avg_rate)
def set_new_serial_and_batch_bundle(self): def set_new_serial_and_batch_bundle(self):
for item in self.items: for item in self.items:
@ -302,16 +323,6 @@ class StockReconciliation(StockController):
validate_end_of_life(item_code, item.end_of_life, item.disabled) validate_end_of_life(item_code, item.end_of_life, item.disabled)
validate_is_stock_item(item_code, item.is_stock_item) validate_is_stock_item(item_code, item.is_stock_item)
# item should not be serialized
if item.has_serial_no and not row.serial_no and not item.serial_no_series:
raise frappe.ValidationError(
_("Serial no(s) required for serialized item {0}").format(item_code)
)
# item managed batch-wise not allowed
if item.has_batch_no and not row.batch_no and not item.create_new_batch:
raise frappe.ValidationError(_("Batch no is required for batched item {0}").format(item_code))
# docstatus should be < 2 # docstatus should be < 2
validate_cancelled_item(item_code, item.docstatus) validate_cancelled_item(item_code, item.docstatus)
@ -364,8 +375,6 @@ class StockReconciliation(StockController):
from erpnext.stock.stock_ledger import get_previous_sle from erpnext.stock.stock_ledger import get_previous_sle
sl_entries = [] sl_entries = []
has_serial_no = False
has_batch_no = False
for row in self.items: for row in self.items:
item = frappe.get_cached_value( item = frappe.get_cached_value(
"Item", row.item_code, ["has_serial_no", "has_batch_no"], as_dict=1 "Item", row.item_code, ["has_serial_no", "has_batch_no"], as_dict=1
@ -412,18 +421,11 @@ class StockReconciliation(StockController):
sl_entries.append(self.get_sle_for_items(row)) sl_entries.append(self.get_sle_for_items(row))
if sl_entries: if sl_entries:
if has_serial_no: allow_negative_stock = cint(
sl_entries = self.merge_similar_item_serial_nos(sl_entries) frappe.db.get_single_value("Stock Settings", "allow_negative_stock")
)
allow_negative_stock = False
if has_batch_no:
allow_negative_stock = True
self.make_sl_entries(sl_entries, allow_negative_stock=allow_negative_stock) self.make_sl_entries(sl_entries, allow_negative_stock=allow_negative_stock)
if has_serial_no and sl_entries:
self.update_valuation_rate_for_serial_no()
def get_sle_for_serialized_items(self, row, sl_entries): def get_sle_for_serialized_items(self, row, sl_entries):
if row.current_serial_and_batch_bundle: if row.current_serial_and_batch_bundle:
args = self.get_sle_for_items(row) args = self.get_sle_for_items(row)
@ -437,18 +439,16 @@ class StockReconciliation(StockController):
sl_entries.append(args) sl_entries.append(args)
if row.current_serial_and_batch_bundle: args = self.get_sle_for_items(row)
args = self.get_sle_for_items(row) args.update(
args.update( {
{ "actual_qty": row.qty,
"actual_qty": frappe.get_cached_value( "incoming_rate": row.valuation_rate,
"Serial and Batch Bundle", row.serial_and_batch_bundle, "total_qty" "serial_and_batch_bundle": row.serial_and_batch_bundle,
), }
"serial_and_batch_bundle": row.current_serial_and_batch_bundle, )
}
)
sl_entries.append(args) sl_entries.append(args)
def update_valuation_rate_for_serial_no(self): def update_valuation_rate_for_serial_no(self):
for d in self.items: for d in self.items:
@ -493,17 +493,19 @@ class StockReconciliation(StockController):
if not row.batch_no: if not row.batch_no:
data.qty_after_transaction = flt(row.qty, row.precision("qty")) data.qty_after_transaction = flt(row.qty, row.precision("qty"))
if self.docstatus == 2 and not row.batch_no: if self.docstatus == 2:
if row.current_qty: if row.current_qty:
data.actual_qty = -1 * row.current_qty data.actual_qty = -1 * row.current_qty
data.qty_after_transaction = flt(row.current_qty) data.qty_after_transaction = flt(row.current_qty)
data.previous_qty_after_transaction = flt(row.qty) data.previous_qty_after_transaction = flt(row.qty)
data.valuation_rate = flt(row.current_valuation_rate) data.valuation_rate = flt(row.current_valuation_rate)
data.serial_and_batch_bundle = row.current_serial_and_batch_bundle
data.stock_value = data.qty_after_transaction * data.valuation_rate data.stock_value = data.qty_after_transaction * data.valuation_rate
data.stock_value_difference = -1 * flt(row.amount_difference) data.stock_value_difference = -1 * flt(row.amount_difference)
else: else:
data.actual_qty = row.qty data.actual_qty = row.qty
data.qty_after_transaction = 0.0 data.qty_after_transaction = 0.0
data.serial_and_batch_bundle = row.serial_and_batch_bundle
data.valuation_rate = flt(row.valuation_rate) data.valuation_rate = flt(row.valuation_rate)
data.stock_value_difference = -1 * flt(row.amount_difference) data.stock_value_difference = -1 * flt(row.amount_difference)
@ -516,15 +518,7 @@ class StockReconciliation(StockController):
has_serial_no = False has_serial_no = False
for row in self.items: for row in self.items:
if row.serial_no or row.batch_no or row.current_serial_no: sl_entries.append(self.get_sle_for_items(row))
has_serial_no = True
serial_nos = ""
if row.current_serial_no:
serial_nos = get_serial_nos(row.current_serial_no)
sl_entries.append(self.get_sle_for_items(row, serial_nos))
else:
sl_entries.append(self.get_sle_for_items(row))
if sl_entries: if sl_entries:
if has_serial_no: if has_serial_no:

View File

@ -12,6 +12,11 @@ from frappe.utils import add_days, cstr, flt, nowdate, nowtime, random_string
from erpnext.accounts.utils import get_stock_and_account_balance from erpnext.accounts.utils import get_stock_and_account_balance
from erpnext.stock.doctype.item.test_item import create_item from erpnext.stock.doctype.item.test_item import create_item
from erpnext.stock.doctype.purchase_receipt.test_purchase_receipt import make_purchase_receipt from erpnext.stock.doctype.purchase_receipt.test_purchase_receipt import make_purchase_receipt
from erpnext.stock.doctype.serial_and_batch_bundle.test_serial_and_batch_bundle import (
get_batch_from_bundle,
get_serial_nos_from_bundle,
make_serial_batch_bundle,
)
from erpnext.stock.doctype.serial_no.serial_no import get_serial_nos from erpnext.stock.doctype.serial_no.serial_no import get_serial_nos
from erpnext.stock.doctype.stock_reconciliation.stock_reconciliation import ( from erpnext.stock.doctype.stock_reconciliation.stock_reconciliation import (
EmptyStockReconciliationItemsError, EmptyStockReconciliationItemsError,
@ -165,7 +170,8 @@ class TestStockReconciliation(FrappeTestCase, StockTestMixin):
args = { args = {
"item_code": serial_item_code, "item_code": serial_item_code,
"warehouse": serial_warehouse, "warehouse": serial_warehouse,
"posting_date": nowdate(), "qty": -5,
"posting_date": add_days(sr.posting_date, 1),
"posting_time": nowtime(), "posting_time": nowtime(),
"serial_and_batch_bundle": sr.items[0].serial_and_batch_bundle, "serial_and_batch_bundle": sr.items[0].serial_and_batch_bundle,
} }
@ -176,19 +182,18 @@ class TestStockReconciliation(FrappeTestCase, StockTestMixin):
to_delete_records.append(sr.name) to_delete_records.append(sr.name)
sr = create_stock_reconciliation( sr = create_stock_reconciliation(
item_code=serial_item_code, warehouse=serial_warehouse, qty=5, rate=300 item_code=serial_item_code, warehouse=serial_warehouse, qty=5, rate=300, serial_no=serial_nos
) )
serial_nos1 = frappe.get_doc( sn_doc = frappe.get_doc("Serial and Batch Bundle", sr.items[0].serial_and_batch_bundle)
"Serial and Batch Bundle", sr.items[0].serial_and_batch_bundle
).get_serial_nos()
self.assertEqual(len(serial_nos1), 5) self.assertEqual(len(sn_doc.get_serial_nos()), 5)
args = { args = {
"item_code": serial_item_code, "item_code": serial_item_code,
"warehouse": serial_warehouse, "warehouse": serial_warehouse,
"posting_date": nowdate(), "qty": -5,
"posting_date": add_days(sr.posting_date, 1),
"posting_time": nowtime(), "posting_time": nowtime(),
"serial_and_batch_bundle": sr.items[0].serial_and_batch_bundle, "serial_and_batch_bundle": sr.items[0].serial_and_batch_bundle,
} }
@ -203,66 +208,32 @@ class TestStockReconciliation(FrappeTestCase, StockTestMixin):
stock_doc = frappe.get_doc("Stock Reconciliation", d) stock_doc = frappe.get_doc("Stock Reconciliation", d)
stock_doc.cancel() stock_doc.cancel()
def test_stock_reco_for_merge_serialized_item(self):
to_delete_records = []
# Add new serial nos
serial_item_code = "Stock-Reco-Serial-Item-2"
serial_warehouse = "_Test Warehouse for Stock Reco1 - _TC"
sr = create_stock_reconciliation(
item_code=serial_item_code,
serial_no=random_string(6),
warehouse=serial_warehouse,
qty=1,
rate=100,
do_not_submit=True,
purpose="Opening Stock",
)
for i in range(3):
sr.append(
"items",
{
"item_code": serial_item_code,
"warehouse": serial_warehouse,
"qty": 1,
"valuation_rate": 100,
"serial_no": random_string(6),
},
)
sr.save()
sr.submit()
sle_entries = frappe.get_all(
"Stock Ledger Entry", filters={"voucher_no": sr.name}, fields=["name", "incoming_rate"]
)
self.assertEqual(len(sle_entries), 1)
self.assertEqual(sle_entries[0].incoming_rate, 100)
to_delete_records.append(sr.name)
to_delete_records.reverse()
for d in to_delete_records:
stock_doc = frappe.get_doc("Stock Reconciliation", d)
stock_doc.cancel()
def test_stock_reco_for_batch_item(self): def test_stock_reco_for_batch_item(self):
to_delete_records = [] to_delete_records = []
# Add new serial nos # Add new serial nos
item_code = "Stock-Reco-batch-Item-1" item_code = "Stock-Reco-batch-Item-123"
warehouse = "_Test Warehouse for Stock Reco2 - _TC" warehouse = "_Test Warehouse for Stock Reco2 - _TC"
self.make_item(
item_code,
frappe._dict(
{
"is_stock_item": 1,
"has_batch_no": 1,
"create_new_batch": 1,
"batch_number_series": "SRBI123-.#####",
}
),
)
sr = create_stock_reconciliation( sr = create_stock_reconciliation(
item_code=item_code, warehouse=warehouse, qty=5, rate=200, do_not_save=1 item_code=item_code, warehouse=warehouse, qty=5, rate=200, do_not_save=1
) )
sr.save() sr.save()
sr.submit() sr.submit()
sr.load_from_db()
batch_no = sr.items[0].serial_and_batch_bundle batch_no = get_batch_from_bundle(sr.items[0].serial_and_batch_bundle)
self.assertTrue(batch_no) self.assertTrue(batch_no)
to_delete_records.append(sr.name) to_delete_records.append(sr.name)
@ -275,7 +246,7 @@ class TestStockReconciliation(FrappeTestCase, StockTestMixin):
"warehouse": warehouse, "warehouse": warehouse,
"posting_date": nowdate(), "posting_date": nowdate(),
"posting_time": nowtime(), "posting_time": nowtime(),
"batch_no": batch_no, "serial_and_batch_bundle": sr1.items[0].serial_and_batch_bundle,
} }
valuation_rate = get_incoming_rate(args) valuation_rate = get_incoming_rate(args)
@ -308,16 +279,15 @@ class TestStockReconciliation(FrappeTestCase, StockTestMixin):
sr = create_stock_reconciliation(item_code=item.item_code, warehouse=warehouse, qty=1, rate=100) sr = create_stock_reconciliation(item_code=item.item_code, warehouse=warehouse, qty=1, rate=100)
batch_no = sr.items[0].batch_no batch_no = get_batch_from_bundle(sr.items[0].serial_and_batch_bundle)
serial_nos = get_serial_nos(sr.items[0].serial_no) serial_nos = get_serial_nos_from_bundle(sr.items[0].serial_and_batch_bundle)
self.assertEqual(len(serial_nos), 1) self.assertEqual(len(serial_nos), 1)
self.assertEqual(frappe.db.get_value("Serial No", serial_nos[0], "batch_no"), batch_no) self.assertEqual(frappe.db.get_value("Serial No", serial_nos[0], "batch_no"), batch_no)
sr.cancel() sr.cancel()
self.assertEqual(frappe.db.get_value("Serial No", serial_nos[0], "status"), "Inactive") self.assertEqual(frappe.db.get_value("Serial No", serial_nos[0], "warehouse"), None)
self.assertEqual(frappe.db.exists("Batch", batch_no), None)
def test_stock_reco_for_serial_and_batch_item_with_future_dependent_entry(self): def test_stock_reco_for_serial_and_batch_item_with_future_dependent_entry(self):
""" """
@ -344,13 +314,13 @@ class TestStockReconciliation(FrappeTestCase, StockTestMixin):
stock_reco = create_stock_reconciliation( stock_reco = create_stock_reconciliation(
item_code=item.item_code, warehouse=warehouse, qty=1, rate=100 item_code=item.item_code, warehouse=warehouse, qty=1, rate=100
) )
batch_no = stock_reco.items[0].batch_no batch_no = get_batch_from_bundle(stock_reco.items[0].serial_and_batch_bundle)
reco_serial_no = get_serial_nos(stock_reco.items[0].serial_no)[0] reco_serial_no = get_serial_nos_from_bundle(stock_reco.items[0].serial_and_batch_bundle)[0]
stock_entry = make_stock_entry( stock_entry = make_stock_entry(
item_code=item.item_code, target=warehouse, qty=1, basic_rate=100, batch_no=batch_no item_code=item.item_code, target=warehouse, qty=1, basic_rate=100, batch_no=batch_no
) )
serial_no_2 = get_serial_nos(stock_entry.items[0].serial_no)[0] serial_no_2 = get_serial_nos_from_bundle(stock_entry.items[0].serial_and_batch_bundle)[0]
# Check Batch qty after 2 transactions # Check Batch qty after 2 transactions
batch_qty = get_batch_qty(batch_no, warehouse, item.item_code) batch_qty = get_batch_qty(batch_no, warehouse, item.item_code)
@ -369,7 +339,7 @@ class TestStockReconciliation(FrappeTestCase, StockTestMixin):
# Check if Serial No from Stock Entry is Unlinked and Inactive # Check if Serial No from Stock Entry is Unlinked and Inactive
self.assertEqual(frappe.db.get_value("Serial No", serial_no_2, "batch_no"), None) self.assertEqual(frappe.db.get_value("Serial No", serial_no_2, "batch_no"), None)
self.assertEqual(frappe.db.get_value("Serial No", serial_no_2, "status"), "Inactive") self.assertEqual(frappe.db.get_value("Serial No", serial_no_2, "warehouse"), None)
stock_reco.cancel() stock_reco.cancel()
@ -584,10 +554,24 @@ class TestStockReconciliation(FrappeTestCase, StockTestMixin):
def test_valid_batch(self): def test_valid_batch(self):
create_batch_item_with_batch("Testing Batch Item 1", "001") create_batch_item_with_batch("Testing Batch Item 1", "001")
create_batch_item_with_batch("Testing Batch Item 2", "002") create_batch_item_with_batch("Testing Batch Item 2", "002")
sr = create_stock_reconciliation(
item_code="Testing Batch Item 1", qty=1, rate=100, batch_no="002", do_not_submit=True doc = frappe.get_doc(
{
"doctype": "Serial and Batch Bundle",
"item_code": "Testing Batch Item 1",
"warehouse": "_Test Warehouse - _TC",
"voucher_type": "Stock Reconciliation",
"entries": [
{
"batch_no": "002",
"qty": 1,
"incoming_rate": 100,
}
],
}
) )
self.assertRaises(frappe.ValidationError, sr.submit)
self.assertRaises(frappe.ValidationError, doc.save)
def test_serial_no_cancellation(self): def test_serial_no_cancellation(self):
from erpnext.stock.doctype.stock_entry.test_stock_entry import make_stock_entry from erpnext.stock.doctype.stock_entry.test_stock_entry import make_stock_entry
@ -595,18 +579,17 @@ class TestStockReconciliation(FrappeTestCase, StockTestMixin):
item = create_item("Stock-Reco-Serial-Item-9", is_stock_item=1) item = create_item("Stock-Reco-Serial-Item-9", is_stock_item=1)
if not item.has_serial_no: if not item.has_serial_no:
item.has_serial_no = 1 item.has_serial_no = 1
item.serial_no_series = "SRS9.####" item.serial_no_series = "PSRS9.####"
item.save() item.save()
item_code = item.name item_code = item.name
warehouse = "_Test Warehouse - _TC" warehouse = "_Test Warehouse - _TC"
se1 = make_stock_entry(item_code=item_code, target=warehouse, qty=10, basic_rate=700) se1 = make_stock_entry(item_code=item_code, target=warehouse, qty=10, basic_rate=700)
serial_nos = get_serial_nos_from_bundle(se1.items[0].serial_and_batch_bundle)
serial_nos = get_serial_nos(se1.items[0].serial_no)
# reduce 1 item # reduce 1 item
serial_nos.pop() serial_nos.pop()
new_serial_nos = "\n".join(serial_nos) new_serial_nos = serial_nos
sr = create_stock_reconciliation( sr = create_stock_reconciliation(
item_code=item.name, warehouse=warehouse, serial_no=new_serial_nos, qty=9 item_code=item.name, warehouse=warehouse, serial_no=new_serial_nos, qty=9
@ -628,10 +611,19 @@ class TestStockReconciliation(FrappeTestCase, StockTestMixin):
item_code = item.name item_code = item.name
warehouse = "_Test Warehouse - _TC" warehouse = "_Test Warehouse - _TC"
if not frappe.db.exists("Serial No", "SR-CREATED-SR-NO"):
frappe.get_doc(
{
"doctype": "Serial No",
"item_code": item_code,
"serial_no": "SR-CREATED-SR-NO",
}
).insert()
sr = create_stock_reconciliation( sr = create_stock_reconciliation(
item_code=item.name, item_code=item.name,
warehouse=warehouse, warehouse=warehouse,
serial_no="SR-CREATED-SR-NO", serial_no=["SR-CREATED-SR-NO"],
qty=1, qty=1,
do_not_submit=True, do_not_submit=True,
rate=100, rate=100,
@ -900,6 +892,31 @@ def create_stock_reconciliation(**args):
or frappe.get_cached_value("Cost Center", filters={"is_group": 0, "company": sr.company}) or frappe.get_cached_value("Cost Center", filters={"is_group": 0, "company": sr.company})
) )
bundle_id = None
if args.batch_no or args.serial_no:
batches = frappe._dict({})
if args.batch_no:
batches[args.batch_no] = args.qty
bundle_id = make_serial_batch_bundle(
frappe._dict(
{
"item_code": args.item_code or "_Test Item",
"warehouse": args.warehouse or "_Test Warehouse - _TC",
"qty": args.qty,
"voucher_type": "Stock Reconciliation",
"batches": batches,
"rate": args.rate,
"serial_nos": args.serial_no,
"posting_date": sr.posting_date,
"posting_time": sr.posting_time,
"type_of_transaction": "Inward" if args.qty > 0 else "Outward",
"company": args.company or "_Test Company",
"do_not_submit": True,
}
)
)
sr.append( sr.append(
"items", "items",
{ {
@ -907,8 +924,7 @@ def create_stock_reconciliation(**args):
"warehouse": args.warehouse or "_Test Warehouse - _TC", "warehouse": args.warehouse or "_Test Warehouse - _TC",
"qty": args.qty, "qty": args.qty,
"valuation_rate": args.rate, "valuation_rate": args.rate,
"serial_no": args.serial_no, "serial_and_batch_bundle": bundle_id,
"batch_no": args.batch_no,
}, },
) )
@ -919,6 +935,9 @@ def create_stock_reconciliation(**args):
sr.submit() sr.submit()
except EmptyStockReconciliationItemsError: except EmptyStockReconciliationItemsError:
pass pass
sr.load_from_db()
return sr return sr

View File

@ -40,9 +40,8 @@
"section_break_7", "section_break_7",
"auto_create_serial_and_batch_bundle_for_outward", "auto_create_serial_and_batch_bundle_for_outward",
"pick_serial_and_batch_based_on", "pick_serial_and_batch_based_on",
"section_break_plhx",
"disable_serial_no_and_batch_selector",
"column_break_mhzc", "column_break_mhzc",
"disable_serial_no_and_batch_selector",
"use_naming_series", "use_naming_series",
"naming_series_prefix", "naming_series_prefix",
"stock_planning_tab", "stock_planning_tab",

View File

@ -5,7 +5,7 @@ import frappe
from frappe import _, bold from frappe import _, bold
from frappe.model.naming import make_autoname from frappe.model.naming import make_autoname
from frappe.query_builder.functions import CombineDatetime, Sum from frappe.query_builder.functions import CombineDatetime, Sum
from frappe.utils import cint, flt, now, today from frappe.utils import cint, flt, now, nowtime, today
from erpnext.stock.deprecated_serial_batch import ( from erpnext.stock.deprecated_serial_batch import (
DeprecatedBatchNoValuation, DeprecatedBatchNoValuation,
@ -181,6 +181,13 @@ class SerialBatchBundle:
if not self.sle.serial_and_batch_bundle: if not self.sle.serial_and_batch_bundle:
return return
docstatus = frappe.get_cached_value(
"Serial and Batch Bundle", self.sle.serial_and_batch_bundle, "docstatus"
)
if docstatus != 1:
self.submit_serial_and_batch_bundle()
if self.item_details.has_serial_no == 1: if self.item_details.has_serial_no == 1:
self.set_warehouse_and_status_in_serial_nos() self.set_warehouse_and_status_in_serial_nos()
@ -194,8 +201,13 @@ class SerialBatchBundle:
if self.item_details.has_batch_no == 1: if self.item_details.has_batch_no == 1:
self.update_batch_qty() self.update_batch_qty()
def submit_serial_and_batch_bundle(self):
doc = frappe.get_doc("Serial and Batch Bundle", self.sle.serial_and_batch_bundle)
doc.flags.ignore_voucher_validation = True
doc.submit()
def set_warehouse_and_status_in_serial_nos(self): def set_warehouse_and_status_in_serial_nos(self):
serial_nos = get_serial_nos(self.sle.serial_and_batch_bundle, check_outward=False) serial_nos = get_serial_nos(self.sle.serial_and_batch_bundle)
warehouse = self.warehouse if self.sle.actual_qty > 0 else None warehouse = self.warehouse if self.sle.actual_qty > 0 else None
if not serial_nos: if not serial_nos:
@ -239,15 +251,12 @@ class SerialBatchBundle:
) )
) )
for batch_no, qty in batches_qty.items(): for batch_no in batches:
frappe.db.set_value("Batch", batch_no, "batch_qty", qty) frappe.db.set_value("Batch", batch_no, "batch_qty", batches_qty.get(batch_no, 0))
def get_serial_nos(serial_and_batch_bundle, check_outward=True): def get_serial_nos(serial_and_batch_bundle):
filters = {"parent": serial_and_batch_bundle} filters = {"parent": serial_and_batch_bundle}
if check_outward:
filters["is_outward"] = 1
entries = frappe.get_all("Serial and Batch Entry", fields=["serial_no"], filters=filters) entries = frappe.get_all("Serial and Batch Entry", fields=["serial_no"], filters=filters)
return [d.serial_no for d in entries] return [d.serial_no for d in entries]
@ -262,7 +271,7 @@ class SerialNoValuation(DeprecatedSerialNoValuation):
self.calculate_valuation_rate() self.calculate_valuation_rate()
def calculate_stock_value_change(self): def calculate_stock_value_change(self):
if self.sle.actual_qty > 0: if flt(self.sle.actual_qty) > 0:
self.stock_value_change = frappe.get_cached_value( self.stock_value_change = frappe.get_cached_value(
"Serial and Batch Bundle", self.sle.serial_and_batch_bundle, "total_amount" "Serial and Batch Bundle", self.sle.serial_and_batch_bundle, "total_amount"
) )
@ -274,63 +283,52 @@ class SerialNoValuation(DeprecatedSerialNoValuation):
self.stock_value_change = 0.0 self.stock_value_change = 0.0
for ledger in entries: for ledger in entries:
self.stock_value_change += ledger.incoming_rate * -1 self.stock_value_change += ledger.incoming_rate
self.serial_no_incoming_rate[ledger.serial_no] = ledger.incoming_rate self.serial_no_incoming_rate[ledger.serial_no] += ledger.incoming_rate
self.calculate_stock_value_from_deprecarated_ledgers() self.calculate_stock_value_from_deprecarated_ledgers()
def get_serial_no_ledgers(self): def get_serial_no_ledgers(self):
serial_nos = self.get_serial_nos() serial_nos = self.get_serial_nos()
bundle = frappe.qb.DocType("Serial and Batch Bundle")
bundle_child = frappe.qb.DocType("Serial and Batch Entry")
subquery = f""" query = (
SELECT frappe.qb.from_(bundle)
MAX( .inner_join(bundle_child)
TIMESTAMP( .on(bundle.name == bundle_child.parent)
parent.posting_date, parent.posting_time .select(
) bundle.name,
), child.name, child.serial_no, child.warehouse bundle_child.serial_no,
FROM (bundle_child.incoming_rate * bundle_child.qty).as_("incoming_rate"),
`tabSerial and Batch Bundle` as parent, )
`tabSerial and Batch Entry` as child .where(
WHERE (bundle.is_cancelled == 0)
parent.name = child.parent & (bundle.docstatus == 1)
AND child.serial_no IN ({', '.join([frappe.db.escape(s) for s in serial_nos])}) & (bundle_child.serial_no.isin(serial_nos))
AND child.is_outward = 0 & (bundle.type_of_transaction != "Maintenance")
AND parent.docstatus = 1 & (bundle.item_code == self.sle.item_code)
AND parent.type_of_transaction != 'Maintenance' & (bundle_child.warehouse == self.sle.warehouse)
AND parent.is_cancelled = 0 )
AND child.warehouse = {frappe.db.escape(self.sle.warehouse)} .orderby(bundle.posting_date, bundle.posting_time, bundle.creation)
AND parent.item_code = {frappe.db.escape(self.sle.item_code)}
AND (
parent.posting_date < '{self.sle.posting_date}'
OR (
parent.posting_date = '{self.sle.posting_date}'
AND parent.posting_time <= '{self.sle.posting_time}'
)
)
GROUP BY
child.serial_no
"""
return frappe.db.sql(
f"""
SELECT
ledger.serial_no, ledger.incoming_rate, ledger.warehouse
FROM
`tabSerial and Batch Entry` AS ledger,
({subquery}) AS SubQuery
WHERE
ledger.name = SubQuery.name
AND ledger.serial_no = SubQuery.serial_no
AND ledger.warehouse = SubQuery.warehouse
GROUP BY
ledger.serial_no
Order By
ledger.creation
""",
as_dict=1,
) )
# Important to exclude the current voucher
if self.sle.voucher_type == "Stock Reconciliation" and self.sle.voucher_no:
query = query.where(bundle.voucher_no != self.sle.voucher_no)
if self.sle.posting_date:
if self.sle.posting_time is None:
self.sle.posting_time = nowtime()
timestamp_condition = CombineDatetime(
bundle.posting_date, bundle.posting_time
) <= CombineDatetime(self.sle.posting_date, self.sle.posting_time)
query = query.where(timestamp_condition)
return query.run(as_dict=True)
def get_serial_nos(self): def get_serial_nos(self):
if self.sle.get("serial_nos"): if self.sle.get("serial_nos"):
return self.sle.serial_nos return self.sle.serial_nos
@ -422,7 +420,7 @@ class BatchNoValuation(DeprecatedBatchNoValuation):
if self.sle.posting_date and self.sle.posting_time: if self.sle.posting_date and self.sle.posting_time:
timestamp_condition = CombineDatetime( timestamp_condition = CombineDatetime(
parent.posting_date, parent.posting_time parent.posting_date, parent.posting_time
) < CombineDatetime(self.sle.posting_date, self.sle.posting_time) ) <= CombineDatetime(self.sle.posting_date, self.sle.posting_time)
query = ( query = (
frappe.qb.from_(parent) frappe.qb.from_(parent)
@ -444,8 +442,9 @@ class BatchNoValuation(DeprecatedBatchNoValuation):
.groupby(child.batch_no) .groupby(child.batch_no)
) )
if self.sle.serial_and_batch_bundle: # Important to exclude the current voucher
query = query.where(child.parent != self.sle.serial_and_batch_bundle) if self.sle.voucher_no:
query = query.where(parent.voucher_no != self.sle.voucher_no)
if timestamp_condition: if timestamp_condition:
query = query.where(timestamp_condition) query = query.where(timestamp_condition)
@ -478,11 +477,11 @@ class BatchNoValuation(DeprecatedBatchNoValuation):
return get_batch_nos(self.sle.serial_and_batch_bundle) return get_batch_nos(self.sle.serial_and_batch_bundle)
def set_stock_value_difference(self): def set_stock_value_difference(self):
if not self.sle.serial_and_batch_bundle:
return
self.stock_value_change = 0 self.stock_value_change = 0
for batch_no, ledger in self.batch_nos.items(): for batch_no, ledger in self.batch_nos.items():
if not self.available_qty[batch_no]:
continue
self.batch_avg_rate[batch_no] = ( self.batch_avg_rate[batch_no] = (
self.stock_value_differece[batch_no] / self.available_qty[batch_no] self.stock_value_differece[batch_no] / self.available_qty[batch_no]
) )
@ -507,8 +506,18 @@ class BatchNoValuation(DeprecatedBatchNoValuation):
self.wh_data.valuation_rate = self.wh_data.stock_value / self.wh_data.qty_after_transaction self.wh_data.valuation_rate = self.wh_data.stock_value / self.wh_data.qty_after_transaction
def get_incoming_rate(self): def get_incoming_rate(self):
if not self.sle.actual_qty:
self.sle.actual_qty = self.get_actual_qty()
return abs(flt(self.stock_value_change) / flt(self.sle.actual_qty)) return abs(flt(self.stock_value_change) / flt(self.sle.actual_qty))
def get_actual_qty(self):
total_qty = 0.0
for batch_no in self.available_qty:
total_qty += self.available_qty[batch_no]
return total_qty
def get_batch_nos(serial_and_batch_bundle): def get_batch_nos(serial_and_batch_bundle):
entries = frappe.get_all( entries = frappe.get_all(
@ -635,8 +644,9 @@ class SerialBatchCreation:
id = self.serial_and_batch_bundle id = self.serial_and_batch_bundle
package = frappe.get_doc("Serial and Batch Bundle", id) package = frappe.get_doc("Serial and Batch Bundle", id)
new_package = frappe.copy_doc(package) new_package = frappe.copy_doc(package)
new_package.docstatus = 0
new_package.type_of_transaction = self.type_of_transaction new_package.type_of_transaction = self.type_of_transaction
new_package.returned_against = self.returned_against new_package.returned_against = self.get("returned_against")
new_package.save() new_package.save()
self.serial_and_batch_bundle = new_package.name self.serial_and_batch_bundle = new_package.name
@ -650,7 +660,7 @@ class SerialBatchCreation:
if self.type_of_transaction == "Outward": if self.type_of_transaction == "Outward":
self.set_auto_serial_batch_entries_for_outward() self.set_auto_serial_batch_entries_for_outward()
elif self.type_of_transaction == "Inward" and not self.get("batches"): elif self.type_of_transaction == "Inward":
self.set_auto_serial_batch_entries_for_inward() self.set_auto_serial_batch_entries_for_inward()
self.set_serial_batch_entries(doc) self.set_serial_batch_entries(doc)
@ -670,7 +680,7 @@ class SerialBatchCreation:
{ {
"item_code": self.item_code, "item_code": self.item_code,
"warehouse": self.warehouse, "warehouse": self.warehouse,
"qty": abs(self.actual_qty), "qty": abs(self.actual_qty) if self.actual_qty else 0,
"based_on": frappe.db.get_single_value("Stock Settings", "pick_serial_and_batch_based_on"), "based_on": frappe.db.get_single_value("Stock Settings", "pick_serial_and_batch_based_on"),
} }
) )
@ -681,6 +691,11 @@ class SerialBatchCreation:
self.batches = get_available_batches(kwargs) self.batches = get_available_batches(kwargs)
def set_auto_serial_batch_entries_for_inward(self): def set_auto_serial_batch_entries_for_inward(self):
if (self.get("batches") and self.has_batch_no) or (
self.get("serial_nos") and self.has_serial_no
):
return
self.batch_no = None self.batch_no = None
if self.has_batch_no: if self.has_batch_no:
self.batch_no = self.create_batch() self.batch_no = self.create_batch()
@ -746,6 +761,10 @@ class SerialBatchCreation:
sr_nos = [] sr_nos = []
serial_nos_details = [] serial_nos_details = []
if not self.serial_no_series:
msg = f"Please set Serial No Series in the item {self.item_code} or create Serial and Batch Bundle manually."
frappe.throw(_(msg))
for i in range(abs(cint(self.actual_qty))): for i in range(abs(cint(self.actual_qty))):
serial_no = make_autoname(self.serial_no_series, "Serial No") serial_no = make_autoname(self.serial_no_series, "Serial No")
sr_nos.append(serial_no) sr_nos.append(serial_no)

View File

@ -27,7 +27,6 @@ from erpnext.stock.doctype.bin.bin import update_qty as update_bin_qty
from erpnext.stock.doctype.stock_reservation_entry.stock_reservation_entry import ( from erpnext.stock.doctype.stock_reservation_entry.stock_reservation_entry import (
get_sre_reserved_qty_for_item_and_warehouse as get_reserved_stock, get_sre_reserved_qty_for_item_and_warehouse as get_reserved_stock,
) )
from erpnext.stock.serial_batch_bundle import BatchNoValuation, SerialNoValuation
from erpnext.stock.utils import ( from erpnext.stock.utils import (
get_incoming_outgoing_rate_for_cancel, get_incoming_outgoing_rate_for_cancel,
get_or_make_bin, get_or_make_bin,
@ -692,22 +691,7 @@ class update_entries_after(object):
sle.outgoing_rate = get_incoming_rate_for_inter_company_transfer(sle) sle.outgoing_rate = get_incoming_rate_for_inter_company_transfer(sle)
if sle.serial_and_batch_bundle: if sle.serial_and_batch_bundle:
if frappe.get_cached_value("Item", sle.item_code, "has_serial_no"): self.calculate_valuation_for_serial_batch_bundle(sle)
SerialNoValuation(
sle=sle,
sle_self=self,
wh_data=self.wh_data,
warehouse=sle.warehouse,
item_code=sle.item_code,
)
else:
BatchNoValuation(
sle=sle,
sle_self=self,
wh_data=self.wh_data,
warehouse=sle.warehouse,
item_code=sle.item_code,
)
else: else:
if sle.voucher_type == "Stock Reconciliation" and not sle.batch_no: if sle.voucher_type == "Stock Reconciliation" and not sle.batch_no:
# assert # assert
@ -759,6 +743,18 @@ class update_entries_after(object):
elif current_qty == 0: elif current_qty == 0:
sle.is_cancelled = 1 sle.is_cancelled = 1
def calculate_valuation_for_serial_batch_bundle(self, sle):
doc = frappe.get_cached_doc("Serial and Batch Bundle", sle.serial_and_batch_bundle)
doc.set_incoming_rate(save=True)
doc.calculate_qty_and_amount(save=True)
self.wh_data.stock_value = round_off_if_near_zero(self.wh_data.stock_value + doc.total_amount)
self.wh_data.qty_after_transaction += doc.total_qty
if self.wh_data.qty_after_transaction:
self.wh_data.valuation_rate = self.wh_data.stock_value / self.wh_data.qty_after_transaction
def validate_negative_stock(self, sle): def validate_negative_stock(self, sle):
""" """
validate negative stock for entries current datetime onwards validate negative stock for entries current datetime onwards
@ -1425,6 +1421,8 @@ def get_valuation_rate(
serial_and_batch_bundle=None, serial_and_batch_bundle=None,
): ):
from erpnext.stock.serial_batch_bundle import BatchNoValuation
if not company: if not company:
company = frappe.get_cached_value("Warehouse", warehouse, "company") company = frappe.get_cached_value("Warehouse", warehouse, "company")

View File

@ -262,7 +262,7 @@ def get_incoming_rate(args, raise_error_if_no_rate=True):
if isinstance(args, dict): if isinstance(args, dict):
args = frappe._dict(args) args = frappe._dict(args)
if item_details.has_serial_no and args.get("serial_and_batch_bundle"): if item_details and item_details.has_serial_no and args.get("serial_and_batch_bundle"):
args.actual_qty = args.qty args.actual_qty = args.qty
sn_obj = SerialNoValuation( sn_obj = SerialNoValuation(
sle=args, sle=args,
@ -272,7 +272,7 @@ def get_incoming_rate(args, raise_error_if_no_rate=True):
in_rate = sn_obj.get_incoming_rate() in_rate = sn_obj.get_incoming_rate()
elif item_details.has_batch_no and args.get("serial_and_batch_bundle"): elif item_details and item_details.has_batch_no and args.get("serial_and_batch_bundle"):
args.actual_qty = args.qty args.actual_qty = args.qty
batch_obj = BatchNoValuation( batch_obj = BatchNoValuation(
sle=args, sle=args,
@ -307,7 +307,6 @@ def get_incoming_rate(args, raise_error_if_no_rate=True):
currency=erpnext.get_company_currency(args.get("company")), currency=erpnext.get_company_currency(args.get("company")),
company=args.get("company"), company=args.get("company"),
raise_error_if_no_rate=raise_error_if_no_rate, raise_error_if_no_rate=raise_error_if_no_rate,
batch_no=args.get("batch_no"),
) )
return flt(in_rate) return flt(in_rate)
@ -455,17 +454,6 @@ def update_included_uom_in_report(columns, result, include_uom, conversion_facto
row[key] = value row[key] = value
def get_available_serial_nos(args):
return frappe.db.sql(
""" SELECT name from `tabSerial No`
WHERE item_code = %(item_code)s and warehouse = %(warehouse)s
and timestamp(purchase_date, purchase_time) <= timestamp(%(posting_date)s, %(posting_time)s)
""",
args,
as_dict=1,
)
def add_additional_uom_columns(columns, result, include_uom, conversion_factors): def add_additional_uom_columns(columns, result, include_uom, conversion_factors):
if not include_uom or not conversion_factors: if not include_uom or not conversion_factors:
return return