fix: type of transaction validation for the stock entry (backport #40986) (#40992)

* fix: type of transaction validation for the stock entry

(cherry picked from commit 8ad0295f1b)

* chore: fix test case

---------

Co-authored-by: Rohit Waghchaure <rohitw1991@gmail.com>
This commit is contained in:
mergify[bot]
2024-04-13 15:30:58 +05:30
committed by GitHub
parent 9abc71f9c8
commit 4342b891eb
2 changed files with 78 additions and 3 deletions

View File

@@ -9,7 +9,17 @@ import frappe
from frappe import _
from frappe.model.mapper import get_mapped_doc
from frappe.query_builder.functions import Sum
from frappe.utils import cint, comma_or, cstr, flt, format_time, formatdate, getdate, nowdate
from frappe.utils import (
cint,
comma_or,
cstr,
flt,
format_time,
formatdate,
get_link_to_form,
getdate,
nowdate,
)
import erpnext
from erpnext.accounts.general_ledger import process_gl_map
@@ -640,8 +650,8 @@ class StockEntry(StockController):
)
)
work_order_link = frappe.utils.get_link_to_form("Work Order", self.work_order)
job_card_link = frappe.utils.get_link_to_form("Job Card", job_card)
work_order_link = get_link_to_form("Work Order", self.work_order)
job_card_link = get_link_to_form("Job Card", job_card)
frappe.throw(
_(
"Row #{0}: Operation {1} is not completed for {2} qty of finished goods in Work Order {3}. Please update operation status via Job Card {4}."
@@ -1350,9 +1360,24 @@ class StockEntry(StockController):
return finished_item_row
def validate_serial_batch_bundle_type(self, serial_and_batch_bundle):
if (
frappe.db.get_value("Serial and Batch Bundle", serial_and_batch_bundle, "type_of_transaction")
!= "Outward"
):
frappe.throw(
_(
"The Serial and Batch Bundle {0} is not valid for this transaction. The 'Type of Transaction' should be 'Outward' instead of 'Inward' in Serial and Batch Bundle {0}"
).format(get_link_to_form("Serial and Batch Bundle", serial_and_batch_bundle)),
title=_("Invalid Serial and Batch Bundle"),
)
def get_sle_for_source_warehouse(self, sl_entries, finished_item_row):
for d in self.get("items"):
if cstr(d.s_warehouse):
if d.serial_and_batch_bundle and self.docstatus == 1:
self.validate_serial_batch_bundle_type(d.serial_and_batch_bundle)
sle = self.get_sl_entries(
d,
{
@@ -1369,6 +1394,21 @@ class StockEntry(StockController):
):
sle.dependant_sle_voucher_detail_no = finished_item_row.name
if sle.serial_and_batch_bundle and self.docstatus == 2:
bundle_id = frappe.get_cached_value(
"Serial and Batch Bundle",
{
"voucher_detail_no": d.name,
"voucher_no": self.name,
"is_cancelled": 0,
"type_of_transaction": "Outward",
},
"name",
)
if bundle_id:
sle.serial_and_batch_bundle = bundle_id
sl_entries.append(sle)
def make_serial_and_batch_bundle_for_transfer(self):

View File

@@ -1745,6 +1745,41 @@ class TestStockEntry(FrappeTestCase):
self.assertTrue(frappe.db.exists("Serial No", serial_no))
self.assertEqual(frappe.db.get_value("Serial No", serial_no, "status"), "Delivered")
def test_serial_batch_bundle_type_of_transaction(self):
item = make_item(
"Test Use Serial and Batch Item SN Item",
{
"has_batch_no": 1,
"is_stock_item": 1,
"create_new_batch": 1,
"batch_naming_series": "Test-SBBTYT-NNS.#####",
},
).name
se = make_stock_entry(
item_code=item,
qty=2,
target="_Test Warehouse - _TC",
use_serial_batch_fields=1,
)
batch_no = get_batch_from_bundle(se.items[0].serial_and_batch_bundle)
se = make_stock_entry(
item_code=item,
qty=2,
source="_Test Warehouse - _TC",
target="Stores - _TC",
use_serial_batch_fields=0,
batch_no=batch_no,
do_not_submit=True,
)
se.reload()
sbb = se.items[0].serial_and_batch_bundle
frappe.db.set_value("Serial and Batch Bundle", sbb, "type_of_transaction", "Inward")
self.assertRaises(frappe.ValidationError, se.submit)
def make_serialized_item(**args):
args = frappe._dict(args)