mirror of
https://github.com/frappe/erpnext.git
synced 2026-05-26 08:24:47 +00:00
Merge pull request #28560 from frappe/mergify/bp/version-13-hotfix/pr-28124
refactor: item-warehouse based reposting (backport #28124)
This commit is contained in:
@@ -17,7 +17,7 @@ from erpnext.accounts.general_ledger import (
|
|||||||
from erpnext.accounts.utils import get_fiscal_year
|
from erpnext.accounts.utils import get_fiscal_year
|
||||||
from erpnext.controllers.accounts_controller import AccountsController
|
from erpnext.controllers.accounts_controller import AccountsController
|
||||||
from erpnext.stock import get_warehouse_account_map
|
from erpnext.stock import get_warehouse_account_map
|
||||||
from erpnext.stock.stock_ledger import get_valuation_rate
|
from erpnext.stock.stock_ledger import get_items_to_be_repost, get_valuation_rate
|
||||||
|
|
||||||
|
|
||||||
class QualityInspectionRequiredError(frappe.ValidationError): pass
|
class QualityInspectionRequiredError(frappe.ValidationError): pass
|
||||||
@@ -544,7 +544,12 @@ class StockController(AccountsController):
|
|||||||
"company": self.company
|
"company": self.company
|
||||||
})
|
})
|
||||||
if future_sle_exists(args):
|
if future_sle_exists(args):
|
||||||
create_repost_item_valuation_entry(args)
|
item_based_reposting = cint(frappe.db.get_single_value("Stock Reposting Settings", "item_based_reposting"))
|
||||||
|
if item_based_reposting:
|
||||||
|
create_item_wise_repost_entries(voucher_type=self.doctype, voucher_no=self.name)
|
||||||
|
else:
|
||||||
|
create_repost_item_valuation_entry(args)
|
||||||
|
|
||||||
|
|
||||||
@frappe.whitelist()
|
@frappe.whitelist()
|
||||||
def make_quality_inspections(doctype, docname, items):
|
def make_quality_inspections(doctype, docname, items):
|
||||||
@@ -679,3 +684,35 @@ def create_repost_item_valuation_entry(args):
|
|||||||
repost_entry.flags.ignore_permissions = True
|
repost_entry.flags.ignore_permissions = True
|
||||||
repost_entry.save()
|
repost_entry.save()
|
||||||
repost_entry.submit()
|
repost_entry.submit()
|
||||||
|
|
||||||
|
|
||||||
|
def create_item_wise_repost_entries(voucher_type, voucher_no, allow_zero_rate=False):
|
||||||
|
"""Using a voucher create repost item valuation records for all item-warehouse pairs."""
|
||||||
|
|
||||||
|
stock_ledger_entries = get_items_to_be_repost(voucher_type, voucher_no)
|
||||||
|
|
||||||
|
distinct_item_warehouses = set()
|
||||||
|
repost_entries = []
|
||||||
|
|
||||||
|
for sle in stock_ledger_entries:
|
||||||
|
item_wh = (sle.item_code, sle.warehouse)
|
||||||
|
if item_wh in distinct_item_warehouses:
|
||||||
|
continue
|
||||||
|
distinct_item_warehouses.add(item_wh)
|
||||||
|
|
||||||
|
repost_entry = frappe.new_doc("Repost Item Valuation")
|
||||||
|
repost_entry.based_on = "Item and Warehouse"
|
||||||
|
repost_entry.voucher_type = voucher_type
|
||||||
|
repost_entry.voucher_no = voucher_no
|
||||||
|
|
||||||
|
repost_entry.item_code = sle.item_code
|
||||||
|
repost_entry.warehouse = sle.warehouse
|
||||||
|
repost_entry.posting_date = sle.posting_date
|
||||||
|
repost_entry.posting_time = sle.posting_time
|
||||||
|
repost_entry.allow_zero_rate = allow_zero_rate
|
||||||
|
repost_entry.flags.ignore_links = True
|
||||||
|
repost_entry.flags.ignore_permissions = True
|
||||||
|
repost_entry.submit()
|
||||||
|
repost_entries.append(repost_entry)
|
||||||
|
|
||||||
|
return repost_entries
|
||||||
|
|||||||
@@ -64,7 +64,7 @@
|
|||||||
"in_standard_filter": 1,
|
"in_standard_filter": 1,
|
||||||
"label": "Status",
|
"label": "Status",
|
||||||
"no_copy": 1,
|
"no_copy": 1,
|
||||||
"options": "Queued\nIn Progress\nCompleted\nFailed",
|
"options": "Queued\nIn Progress\nCompleted\nSkipped\nFailed",
|
||||||
"read_only": 1
|
"read_only": 1
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -177,7 +177,7 @@
|
|||||||
"index_web_pages_for_search": 1,
|
"index_web_pages_for_search": 1,
|
||||||
"is_submittable": 1,
|
"is_submittable": 1,
|
||||||
"links": [],
|
"links": [],
|
||||||
"modified": "2021-11-18 02:18:10.524560",
|
"modified": "2021-11-24 02:18:10.524560",
|
||||||
"modified_by": "Administrator",
|
"modified_by": "Administrator",
|
||||||
"module": "Stock",
|
"module": "Stock",
|
||||||
"name": "Repost Item Valuation",
|
"name": "Repost Item Valuation",
|
||||||
@@ -228,4 +228,4 @@
|
|||||||
],
|
],
|
||||||
"sort_field": "modified",
|
"sort_field": "modified",
|
||||||
"sort_order": "DESC"
|
"sort_order": "DESC"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
# Copyright (c) 2020, Frappe Technologies Pvt. Ltd. and contributors
|
# Copyright (c) 2020, Frappe Technologies Pvt. Ltd. and contributors
|
||||||
# For license information, please see license.txt
|
# For license information, please see license.txt
|
||||||
|
|
||||||
|
|
||||||
import frappe
|
import frappe
|
||||||
from frappe import _
|
from frappe import _
|
||||||
from frappe.model.document import Document
|
from frappe.model.document import Document
|
||||||
@@ -19,7 +18,7 @@ from erpnext.stock.stock_ledger import repost_future_sle
|
|||||||
|
|
||||||
class RepostItemValuation(Document):
|
class RepostItemValuation(Document):
|
||||||
def validate(self):
|
def validate(self):
|
||||||
self.set_status()
|
self.set_status(write=False)
|
||||||
self.reset_field_values()
|
self.reset_field_values()
|
||||||
self.set_company()
|
self.set_company()
|
||||||
|
|
||||||
@@ -27,26 +26,27 @@ class RepostItemValuation(Document):
|
|||||||
if self.based_on == 'Transaction':
|
if self.based_on == 'Transaction':
|
||||||
self.item_code = None
|
self.item_code = None
|
||||||
self.warehouse = None
|
self.warehouse = None
|
||||||
else:
|
|
||||||
self.voucher_type = None
|
|
||||||
self.voucher_no = None
|
|
||||||
|
|
||||||
self.allow_negative_stock = self.allow_negative_stock or \
|
self.allow_negative_stock = self.allow_negative_stock or \
|
||||||
cint(frappe.db.get_single_value("Stock Settings", "allow_negative_stock"))
|
cint(frappe.db.get_single_value("Stock Settings", "allow_negative_stock"))
|
||||||
|
|
||||||
def set_company(self):
|
def set_company(self):
|
||||||
if self.voucher_type and self.voucher_no:
|
if self.based_on == "Transaction":
|
||||||
self.company = frappe.get_cached_value(self.voucher_type, self.voucher_no, "company")
|
self.company = frappe.get_cached_value(self.voucher_type, self.voucher_no, "company")
|
||||||
elif self.warehouse:
|
elif self.warehouse:
|
||||||
self.company = frappe.get_cached_value("Warehouse", self.warehouse, "company")
|
self.company = frappe.get_cached_value("Warehouse", self.warehouse, "company")
|
||||||
|
|
||||||
def set_status(self, status=None):
|
def set_status(self, status=None, write=True):
|
||||||
|
status = status or self.status
|
||||||
if not status:
|
if not status:
|
||||||
status = 'Queued'
|
self.status = 'Queued'
|
||||||
self.db_set('status', status)
|
else:
|
||||||
|
self.status = status
|
||||||
|
if write:
|
||||||
|
self.db_set('status', self.status)
|
||||||
|
|
||||||
def on_submit(self):
|
def on_submit(self):
|
||||||
if not frappe.flags.in_test:
|
if not frappe.flags.in_test or self.flags.dont_run_in_test:
|
||||||
return
|
return
|
||||||
|
|
||||||
frappe.enqueue(repost, timeout=1800, queue='long',
|
frappe.enqueue(repost, timeout=1800, queue='long',
|
||||||
@@ -58,6 +58,37 @@ class RepostItemValuation(Document):
|
|||||||
frappe.enqueue(repost, timeout=1800, queue='long',
|
frappe.enqueue(repost, timeout=1800, queue='long',
|
||||||
job_name='repost_sle', now=True, doc=self)
|
job_name='repost_sle', now=True, doc=self)
|
||||||
|
|
||||||
|
def deduplicate_similar_repost(self):
|
||||||
|
""" Deduplicate similar reposts based on item-warehouse-posting combination."""
|
||||||
|
if self.based_on != "Item and Warehouse":
|
||||||
|
return
|
||||||
|
|
||||||
|
filters = {
|
||||||
|
"item_code": self.item_code,
|
||||||
|
"warehouse": self.warehouse,
|
||||||
|
"name": self.name,
|
||||||
|
"posting_date": self.posting_date,
|
||||||
|
"posting_time": self.posting_time,
|
||||||
|
}
|
||||||
|
|
||||||
|
frappe.db.sql("""
|
||||||
|
update `tabRepost Item Valuation`
|
||||||
|
set status = 'Skipped'
|
||||||
|
WHERE item_code = %(item_code)s
|
||||||
|
and warehouse = %(warehouse)s
|
||||||
|
and name != %(name)s
|
||||||
|
and TIMESTAMP(posting_date, posting_time) > TIMESTAMP(%(posting_date)s, %(posting_time)s)
|
||||||
|
and docstatus = 1
|
||||||
|
and status = 'Queued'
|
||||||
|
and based_on = 'Item and Warehouse'
|
||||||
|
""",
|
||||||
|
filters
|
||||||
|
)
|
||||||
|
|
||||||
|
def on_doctype_update():
|
||||||
|
frappe.db.add_index("Repost Item Valuation", ["warehouse", "item_code"], "item_warehouse")
|
||||||
|
|
||||||
|
|
||||||
def repost(doc):
|
def repost(doc):
|
||||||
try:
|
try:
|
||||||
if not frappe.db.exists("Repost Item Valuation", doc.name):
|
if not frappe.db.exists("Repost Item Valuation", doc.name):
|
||||||
@@ -134,7 +165,9 @@ def repost_entries():
|
|||||||
|
|
||||||
for row in riv_entries:
|
for row in riv_entries:
|
||||||
doc = frappe.get_doc('Repost Item Valuation', row.name)
|
doc = frappe.get_doc('Repost Item Valuation', row.name)
|
||||||
repost(doc)
|
if doc.status in ('Queued', 'In Progress'):
|
||||||
|
doc.deduplicate_similar_repost()
|
||||||
|
repost(doc)
|
||||||
|
|
||||||
riv_entries = get_repost_item_valuation_entries()
|
riv_entries = get_repost_item_valuation_entries()
|
||||||
if riv_entries:
|
if riv_entries:
|
||||||
|
|||||||
@@ -5,6 +5,8 @@ import unittest
|
|||||||
|
|
||||||
import frappe
|
import frappe
|
||||||
|
|
||||||
|
from erpnext.controllers.stock_controller import create_item_wise_repost_entries
|
||||||
|
from erpnext.stock.doctype.purchase_receipt.test_purchase_receipt import make_purchase_receipt
|
||||||
from erpnext.stock.doctype.repost_item_valuation.repost_item_valuation import (
|
from erpnext.stock.doctype.repost_item_valuation.repost_item_valuation import (
|
||||||
in_configured_timeslot,
|
in_configured_timeslot,
|
||||||
)
|
)
|
||||||
@@ -70,3 +72,69 @@ class TestRepostItemValuation(unittest.TestCase):
|
|||||||
in_configured_timeslot(repost_settings, case.get("current_time")),
|
in_configured_timeslot(repost_settings, case.get("current_time")),
|
||||||
msg=f"Exepcted false from : {case}",
|
msg=f"Exepcted false from : {case}",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def test_create_item_wise_repost_item_valuation_entries(self):
|
||||||
|
pr = make_purchase_receipt(
|
||||||
|
company="_Test Company with perpetual inventory",
|
||||||
|
warehouse="Stores - TCP1",
|
||||||
|
get_multiple_items=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
rivs = create_item_wise_repost_entries(pr.doctype, pr.name)
|
||||||
|
self.assertGreaterEqual(len(rivs), 2)
|
||||||
|
self.assertIn("_Test Item", [d.item_code for d in rivs])
|
||||||
|
|
||||||
|
for riv in rivs:
|
||||||
|
self.assertEqual(riv.company, "_Test Company with perpetual inventory")
|
||||||
|
self.assertEqual(riv.warehouse, "Stores - TCP1")
|
||||||
|
|
||||||
|
def test_deduplication(self):
|
||||||
|
def _assert_status(doc, status):
|
||||||
|
doc.load_from_db()
|
||||||
|
self.assertEqual(doc.status, status)
|
||||||
|
|
||||||
|
riv_args = frappe._dict(
|
||||||
|
doctype="Repost Item Valuation",
|
||||||
|
item_code="_Test Item",
|
||||||
|
warehouse="_Test Warehouse - _TC",
|
||||||
|
based_on="Item and Warehouse",
|
||||||
|
voucher_type="Sales Invoice",
|
||||||
|
voucher_no="SI-1",
|
||||||
|
posting_date="2021-01-02",
|
||||||
|
posting_time="00:01:00",
|
||||||
|
)
|
||||||
|
|
||||||
|
# new repost without any duplicates
|
||||||
|
riv1 = frappe.get_doc(riv_args)
|
||||||
|
riv1.flags.dont_run_in_test = True
|
||||||
|
riv1.submit()
|
||||||
|
_assert_status(riv1, "Queued")
|
||||||
|
self.assertEqual(riv1.voucher_type, "Sales Invoice") # traceability
|
||||||
|
self.assertEqual(riv1.voucher_no, "SI-1")
|
||||||
|
|
||||||
|
# newer than existing duplicate - riv1
|
||||||
|
riv2 = frappe.get_doc(riv_args.update({"posting_date": "2021-01-03"}))
|
||||||
|
riv2.flags.dont_run_in_test = True
|
||||||
|
riv2.submit()
|
||||||
|
riv1.deduplicate_similar_repost()
|
||||||
|
_assert_status(riv2, "Skipped")
|
||||||
|
|
||||||
|
# older than exisitng duplicate - riv1
|
||||||
|
riv3 = frappe.get_doc(riv_args.update({"posting_date": "2021-01-01"}))
|
||||||
|
riv3.flags.dont_run_in_test = True
|
||||||
|
riv3.submit()
|
||||||
|
riv3.deduplicate_similar_repost()
|
||||||
|
_assert_status(riv3, "Queued")
|
||||||
|
_assert_status(riv1, "Skipped")
|
||||||
|
|
||||||
|
# unrelated reposts, shouldn't do anything to others.
|
||||||
|
riv4 = frappe.get_doc(riv_args.update({"warehouse": "Stores - _TC"}))
|
||||||
|
riv4.flags.dont_run_in_test = True
|
||||||
|
riv4.submit()
|
||||||
|
riv4.deduplicate_similar_repost()
|
||||||
|
_assert_status(riv4, "Queued")
|
||||||
|
_assert_status(riv3, "Queued")
|
||||||
|
|
||||||
|
# to avoid breaking other tests accidentaly
|
||||||
|
riv4.set_status("Skipped")
|
||||||
|
riv3.set_status("Skipped")
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
{
|
{
|
||||||
"actions": [],
|
"actions": [],
|
||||||
"allow_rename": 1,
|
"allow_rename": 1,
|
||||||
|
"beta": 1,
|
||||||
"creation": "2021-10-01 10:56:30.814787",
|
"creation": "2021-10-01 10:56:30.814787",
|
||||||
"doctype": "DocType",
|
"doctype": "DocType",
|
||||||
"editable_grid": 1,
|
"editable_grid": 1,
|
||||||
@@ -10,7 +11,8 @@
|
|||||||
"limit_reposting_timeslot",
|
"limit_reposting_timeslot",
|
||||||
"start_time",
|
"start_time",
|
||||||
"end_time",
|
"end_time",
|
||||||
"limits_dont_apply_on"
|
"limits_dont_apply_on",
|
||||||
|
"item_based_reposting"
|
||||||
],
|
],
|
||||||
"fields": [
|
"fields": [
|
||||||
{
|
{
|
||||||
@@ -44,12 +46,18 @@
|
|||||||
"fieldname": "limit_reposting_timeslot",
|
"fieldname": "limit_reposting_timeslot",
|
||||||
"fieldtype": "Check",
|
"fieldtype": "Check",
|
||||||
"label": "Limit timeslot for Stock Reposting"
|
"label": "Limit timeslot for Stock Reposting"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"default": "0",
|
||||||
|
"fieldname": "item_based_reposting",
|
||||||
|
"fieldtype": "Check",
|
||||||
|
"label": "Use Item based reposting"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"index_web_pages_for_search": 1,
|
"index_web_pages_for_search": 1,
|
||||||
"issingle": 1,
|
"issingle": 1,
|
||||||
"links": [],
|
"links": [],
|
||||||
"modified": "2021-10-01 11:27:28.981594",
|
"modified": "2021-11-02 01:22:45.155841",
|
||||||
"modified_by": "Administrator",
|
"modified_by": "Administrator",
|
||||||
"module": "Stock",
|
"module": "Stock",
|
||||||
"name": "Stock Reposting Settings",
|
"name": "Stock Reposting Settings",
|
||||||
|
|||||||
Reference in New Issue
Block a user