max_leaves_allowed:
frappe.throw(
_(
- "Total allocated leaves are more days than maximum allocation of {0} leave type for employee {1} in the period"
- ).format(self.leave_type, self.employee)
+ "Total allocated leaves are more than maximum allocation allowed for {0} leave type for employee {1} in the period"
+ ).format(self.leave_type, self.employee),
+ OverAllocationError,
)
def on_submit(self):
@@ -84,6 +93,12 @@ class LeaveAllocation(Document):
def on_update_after_submit(self):
if self.has_value_changed("new_leaves_allocated"):
self.validate_against_leave_applications()
+
+ # recalculate total leaves allocated
+ self.total_leaves_allocated = flt(self.unused_leaves) + flt(self.new_leaves_allocated)
+ # run required validations again since total leaves are being updated
+ self.validate_leave_days_and_dates()
+
leaves_to_be_added = self.new_leaves_allocated - self.get_existing_leave_count()
args = {
"leaves": leaves_to_be_added,
@@ -92,6 +107,7 @@ class LeaveAllocation(Document):
"is_carry_forward": 0,
}
create_leave_ledger_entry(self, args, True)
+ self.db_update()
def get_existing_leave_count(self):
ledger_entries = frappe.get_all(
@@ -279,27 +295,27 @@ def get_previous_allocation(from_date, leave_type, employee):
)
-def get_leave_allocation_for_period(employee, leave_type, from_date, to_date):
- leave_allocated = 0
- leave_allocations = frappe.db.sql(
- """
- select employee, leave_type, from_date, to_date, total_leaves_allocated
- from `tabLeave Allocation`
- where employee=%(employee)s and leave_type=%(leave_type)s
- and docstatus=1
- and (from_date between %(from_date)s and %(to_date)s
- or to_date between %(from_date)s and %(to_date)s
- or (from_date < %(from_date)s and to_date > %(to_date)s))
- """,
- {"from_date": from_date, "to_date": to_date, "employee": employee, "leave_type": leave_type},
- as_dict=1,
- )
+def get_leave_allocation_for_period(
+ employee, leave_type, from_date, to_date, exclude_allocation=None
+):
+ from frappe.query_builder.functions import Sum
- if leave_allocations:
- for leave_alloc in leave_allocations:
- leave_allocated += leave_alloc.total_leaves_allocated
-
- return leave_allocated
+ Allocation = frappe.qb.DocType("Leave Allocation")
+ return (
+ frappe.qb.from_(Allocation)
+ .select(Sum(Allocation.total_leaves_allocated).as_("total_allocated_leaves"))
+ .where(
+ (Allocation.employee == employee)
+ & (Allocation.leave_type == leave_type)
+ & (Allocation.docstatus == 1)
+ & (Allocation.name != exclude_allocation)
+ & (
+ (Allocation.from_date.between(from_date, to_date))
+ | (Allocation.to_date.between(from_date, to_date))
+ | ((Allocation.from_date < from_date) & (Allocation.to_date > to_date))
+ )
+ )
+ ).run()[0][0] or 0.0
@frappe.whitelist()
diff --git a/erpnext/hr/doctype/leave_allocation/test_leave_allocation.py b/erpnext/hr/doctype/leave_allocation/test_leave_allocation.py
index a53d4a82ba6..dde52d7ad8e 100644
--- a/erpnext/hr/doctype/leave_allocation/test_leave_allocation.py
+++ b/erpnext/hr/doctype/leave_allocation/test_leave_allocation.py
@@ -1,24 +1,26 @@
import unittest
import frappe
+from frappe.tests.utils import FrappeTestCase
from frappe.utils import add_days, add_months, getdate, nowdate
import erpnext
from erpnext.hr.doctype.employee.test_employee import make_employee
+from erpnext.hr.doctype.leave_allocation.leave_allocation import (
+ BackDatedAllocationError,
+ OverAllocationError,
+)
from erpnext.hr.doctype.leave_ledger_entry.leave_ledger_entry import process_expired_allocation
from erpnext.hr.doctype.leave_type.test_leave_type import create_leave_type
-class TestLeaveAllocation(unittest.TestCase):
- @classmethod
- def setUpClass(cls):
- frappe.db.sql("delete from `tabLeave Period`")
+class TestLeaveAllocation(FrappeTestCase):
+ def setUp(self):
+ frappe.db.delete("Leave Period")
+ frappe.db.delete("Leave Allocation")
- emp_id = make_employee("test_emp_leave_allocation@salary.com")
- cls.employee = frappe.get_doc("Employee", emp_id)
-
- def tearDown(self):
- frappe.db.rollback()
+ emp_id = make_employee("test_emp_leave_allocation@salary.com", company="_Test Company")
+ self.employee = frappe.get_doc("Employee", emp_id)
def test_overlapping_allocation(self):
leaves = [
@@ -65,7 +67,7 @@ class TestLeaveAllocation(unittest.TestCase):
# invalid period
self.assertRaises(frappe.ValidationError, doc.save)
- def test_allocated_leave_days_over_period(self):
+ def test_validation_for_over_allocation(self):
doc = frappe.get_doc(
{
"doctype": "Leave Allocation",
@@ -80,7 +82,135 @@ class TestLeaveAllocation(unittest.TestCase):
)
# allocated leave more than period
- self.assertRaises(frappe.ValidationError, doc.save)
+ self.assertRaises(OverAllocationError, doc.save)
+
+ def test_validation_for_over_allocation_post_submission(self):
+ allocation = frappe.get_doc(
+ {
+ "doctype": "Leave Allocation",
+ "__islocal": 1,
+ "employee": self.employee.name,
+ "employee_name": self.employee.employee_name,
+ "leave_type": "_Test Leave Type",
+ "from_date": getdate("2015-09-1"),
+ "to_date": getdate("2015-09-30"),
+ "new_leaves_allocated": 15,
+ }
+ ).submit()
+ allocation.reload()
+ # allocated leaves more than period after submission
+ allocation.new_leaves_allocated = 35
+ self.assertRaises(OverAllocationError, allocation.save)
+
+ def test_validation_for_over_allocation_based_on_leave_setup(self):
+ frappe.delete_doc_if_exists("Leave Period", "Test Allocation Period")
+ leave_period = frappe.get_doc(
+ dict(
+ name="Test Allocation Period",
+ doctype="Leave Period",
+ from_date=add_months(nowdate(), -6),
+ to_date=add_months(nowdate(), 6),
+ company="_Test Company",
+ is_active=1,
+ )
+ ).insert()
+
+ leave_type = create_leave_type(leave_type_name="_Test Allocation Validation", is_carry_forward=1)
+ leave_type.max_leaves_allowed = 25
+ leave_type.save()
+
+ # 15 leaves allocated in this period
+ allocation = create_leave_allocation(
+ leave_type=leave_type.name,
+ employee=self.employee.name,
+ employee_name=self.employee.employee_name,
+ from_date=leave_period.from_date,
+ to_date=nowdate(),
+ )
+ allocation.submit()
+
+ # trying to allocate additional 15 leaves
+ allocation = create_leave_allocation(
+ leave_type=leave_type.name,
+ employee=self.employee.name,
+ employee_name=self.employee.employee_name,
+ from_date=add_days(nowdate(), 1),
+ to_date=leave_period.to_date,
+ )
+ self.assertRaises(OverAllocationError, allocation.save)
+
+ def test_validation_for_over_allocation_based_on_leave_setup_post_submission(self):
+ frappe.delete_doc_if_exists("Leave Period", "Test Allocation Period")
+ leave_period = frappe.get_doc(
+ dict(
+ name="Test Allocation Period",
+ doctype="Leave Period",
+ from_date=add_months(nowdate(), -6),
+ to_date=add_months(nowdate(), 6),
+ company="_Test Company",
+ is_active=1,
+ )
+ ).insert()
+
+ leave_type = create_leave_type(leave_type_name="_Test Allocation Validation", is_carry_forward=1)
+ leave_type.max_leaves_allowed = 30
+ leave_type.save()
+
+ # 15 leaves allocated
+ allocation = create_leave_allocation(
+ leave_type=leave_type.name,
+ employee=self.employee.name,
+ employee_name=self.employee.employee_name,
+ from_date=leave_period.from_date,
+ to_date=nowdate(),
+ )
+ allocation.submit()
+ allocation.reload()
+
+ # allocate additional 15 leaves
+ allocation = create_leave_allocation(
+ leave_type=leave_type.name,
+ employee=self.employee.name,
+ employee_name=self.employee.employee_name,
+ from_date=add_days(nowdate(), 1),
+ to_date=leave_period.to_date,
+ )
+ allocation.submit()
+ allocation.reload()
+
+ # trying to allocate 25 leaves in 2nd alloc within leave period
+ # total leaves = 40 which is more than `max_leaves_allowed` setting i.e. 30
+ allocation.new_leaves_allocated = 25
+ self.assertRaises(OverAllocationError, allocation.save)
+
+ def test_validate_back_dated_allocation_update(self):
+ leave_type = create_leave_type(leave_type_name="_Test_CF_leave", is_carry_forward=1)
+ leave_type.save()
+
+ # initial leave allocation = 15
+ leave_allocation = create_leave_allocation(
+ employee=self.employee.name,
+ employee_name=self.employee.employee_name,
+ leave_type="_Test_CF_leave",
+ from_date=add_months(nowdate(), -12),
+ to_date=add_months(nowdate(), -1),
+ carry_forward=0,
+ )
+ leave_allocation.submit()
+
+ # new_leaves = 15, carry_forwarded = 10
+ leave_allocation_1 = create_leave_allocation(
+ employee=self.employee.name,
+ employee_name=self.employee.employee_name,
+ leave_type="_Test_CF_leave",
+ carry_forward=1,
+ )
+ leave_allocation_1.submit()
+
+ # try updating initial leave allocation
+ leave_allocation.reload()
+ leave_allocation.new_leaves_allocated = 20
+ self.assertRaises(BackDatedAllocationError, leave_allocation.save)
def test_carry_forward_calculation(self):
leave_type = create_leave_type(leave_type_name="_Test_CF_leave", is_carry_forward=1)
@@ -108,8 +238,10 @@ class TestLeaveAllocation(unittest.TestCase):
carry_forward=1,
)
leave_allocation_1.submit()
+ leave_allocation_1.reload()
self.assertEqual(leave_allocation_1.unused_leaves, 10)
+ self.assertEqual(leave_allocation_1.total_leaves_allocated, 25)
leave_allocation_1.cancel()
@@ -197,9 +329,12 @@ class TestLeaveAllocation(unittest.TestCase):
employee=self.employee.name, employee_name=self.employee.employee_name
)
leave_allocation.submit()
+ leave_allocation.reload()
self.assertTrue(leave_allocation.total_leaves_allocated, 15)
+
leave_allocation.new_leaves_allocated = 40
leave_allocation.submit()
+ leave_allocation.reload()
self.assertTrue(leave_allocation.total_leaves_allocated, 40)
def test_leave_subtraction_after_submit(self):
@@ -207,9 +342,12 @@ class TestLeaveAllocation(unittest.TestCase):
employee=self.employee.name, employee_name=self.employee.employee_name
)
leave_allocation.submit()
+ leave_allocation.reload()
self.assertTrue(leave_allocation.total_leaves_allocated, 15)
+
leave_allocation.new_leaves_allocated = 10
leave_allocation.submit()
+ leave_allocation.reload()
self.assertTrue(leave_allocation.total_leaves_allocated, 10)
def test_validation_against_leave_application_after_submit(self):
diff --git a/erpnext/hr/doctype/leave_application/leave_application.py b/erpnext/hr/doctype/leave_application/leave_application.py
index 9036727f76f..e6fc2e6fc06 100755
--- a/erpnext/hr/doctype/leave_application/leave_application.py
+++ b/erpnext/hr/doctype/leave_application/leave_application.py
@@ -735,9 +735,9 @@ def get_number_of_leave_days(
(Based on the include_holiday setting in Leave Type)"""
number_of_days = 0
if cint(half_day) == 1:
- if from_date == to_date:
+ if getdate(from_date) == getdate(to_date):
number_of_days = 0.5
- elif half_day_date and half_day_date <= to_date:
+ elif half_day_date and getdate(from_date) <= getdate(half_day_date) <= getdate(to_date):
number_of_days = date_diff(to_date, from_date) + 0.5
else:
number_of_days = date_diff(to_date, from_date) + 1
diff --git a/erpnext/hr/doctype/leave_application/test_leave_application.py b/erpnext/hr/doctype/leave_application/test_leave_application.py
index dc8187cf5b7..8924a57708e 100644
--- a/erpnext/hr/doctype/leave_application/test_leave_application.py
+++ b/erpnext/hr/doctype/leave_application/test_leave_application.py
@@ -205,7 +205,12 @@ class TestLeaveApplication(unittest.TestCase):
# creates separate leave ledger entries
frappe.delete_doc_if_exists("Leave Type", "Test Leave Validation", force=1)
leave_type = frappe.get_doc(
- dict(leave_type_name="Test Leave Validation", doctype="Leave Type", allow_negative=True)
+ dict(
+ leave_type_name="Test Leave Validation",
+ doctype="Leave Type",
+ allow_negative=True,
+ include_holiday=True,
+ )
).insert()
employee = get_employee()
@@ -217,8 +222,14 @@ class TestLeaveApplication(unittest.TestCase):
# application across allocations
# CASE 1: from date has no allocation, to date has an allocation / both dates have allocation
+ start_date = add_days(year_start, -10)
application = make_leave_application(
- employee.name, add_days(year_start, -10), add_days(year_start, 3), leave_type.name
+ employee.name,
+ start_date,
+ add_days(year_start, 3),
+ leave_type.name,
+ half_day=1,
+ half_day_date=start_date,
)
# 2 separate leave ledger entries
@@ -827,6 +838,7 @@ class TestLeaveApplication(unittest.TestCase):
leave_type_name="_Test_CF_leave_expiry",
is_carry_forward=1,
expire_carry_forwarded_leaves_after_days=90,
+ include_holiday=True,
)
leave_type.submit()
@@ -839,6 +851,8 @@ class TestLeaveApplication(unittest.TestCase):
leave_type=leave_type.name,
from_date=add_days(nowdate(), -3),
to_date=add_days(nowdate(), 7),
+ half_day=1,
+ half_day_date=add_days(nowdate(), -3),
description="_Test Reason",
company="_Test Company",
docstatus=1,
@@ -854,7 +868,7 @@ class TestLeaveApplication(unittest.TestCase):
self.assertEqual(len(leave_ledger_entry), 2)
self.assertEqual(leave_ledger_entry[0].employee, leave_application.employee)
self.assertEqual(leave_ledger_entry[0].leave_type, leave_application.leave_type)
- self.assertEqual(leave_ledger_entry[0].leaves, -9)
+ self.assertEqual(leave_ledger_entry[0].leaves, -8.5)
self.assertEqual(leave_ledger_entry[1].leaves, -2)
def test_leave_application_creation_after_expiry(self):
diff --git a/erpnext/loan_management/doctype/loan_repayment/loan_repayment.py b/erpnext/loan_management/doctype/loan_repayment/loan_repayment.py
index ce50dd3b38d..6a6eb591629 100644
--- a/erpnext/loan_management/doctype/loan_repayment/loan_repayment.py
+++ b/erpnext/loan_management/doctype/loan_repayment/loan_repayment.py
@@ -585,9 +585,10 @@ def regenerate_repayment_schedule(loan, cancel=0):
balance_amount / len(loan_doc.get("repayment_schedule")) - accrued_entries
)
else:
- if not cancel:
+ repayment_period = loan_doc.repayment_periods - accrued_entries
+ if not cancel and repayment_period > 0:
monthly_repayment_amount = get_monthly_repayment_amount(
- balance_amount, loan_doc.rate_of_interest, loan_doc.repayment_periods - accrued_entries
+ balance_amount, loan_doc.rate_of_interest, repayment_period
)
else:
monthly_repayment_amount = last_repayment_amount
@@ -747,5 +748,8 @@ def calculate_amounts(against_loan, posting_date, payment_type=""):
amounts["payable_principal_amount"] = amounts["pending_principal_amount"]
amounts["interest_amount"] += amounts["unaccrued_interest"]
amounts["payable_amount"] = amounts["payable_principal_amount"] + amounts["interest_amount"]
+ amounts["payable_amount"] = (
+ amounts["payable_principal_amount"] + amounts["interest_amount"] + amounts["penalty_amount"]
+ )
return amounts
diff --git a/erpnext/manufacturing/doctype/bom/bom.py b/erpnext/manufacturing/doctype/bom/bom.py
index e12cd3131cd..f8fcd073951 100644
--- a/erpnext/manufacturing/doctype/bom/bom.py
+++ b/erpnext/manufacturing/doctype/bom/bom.py
@@ -687,15 +687,6 @@ class BOM(WebsiteGenerator):
self.scrap_material_cost = total_sm_cost
self.base_scrap_material_cost = base_total_sm_cost
- def update_new_bom(self, old_bom, new_bom, rate):
- for d in self.get("items"):
- if d.bom_no != old_bom:
- continue
-
- d.bom_no = new_bom
- d.rate = rate
- d.amount = (d.stock_qty or d.qty) * rate
-
def update_exploded_items(self, save=True):
"""Update Flat BOM, following will be correct data"""
self.get_exploded_items()
@@ -1015,7 +1006,7 @@ def get_bom_items_as_dict(
query = query.format(
table="BOM Scrap Item",
where_conditions="",
- select_columns=", bom_item.idx, item.description, is_process_loss",
+ select_columns=", item.description, is_process_loss",
is_stock_item=is_stock_item,
qty_field="stock_qty",
)
@@ -1028,7 +1019,7 @@ def get_bom_items_as_dict(
is_stock_item=is_stock_item,
qty_field="stock_qty" if fetch_qty_in_stock_uom else "qty",
select_columns=""", bom_item.uom, bom_item.conversion_factor, bom_item.source_warehouse,
- bom_item.idx, bom_item.operation, bom_item.include_item_in_manufacturing, bom_item.sourced_by_supplier,
+ bom_item.operation, bom_item.include_item_in_manufacturing, bom_item.sourced_by_supplier,
bom_item.description, bom_item.base_rate as rate """,
)
items = frappe.db.sql(query, {"qty": qty, "bom": bom, "company": company}, as_dict=True)
diff --git a/erpnext/manufacturing/doctype/bom_update_log/__init__.py b/erpnext/manufacturing/doctype/bom_update_log/__init__.py
new file mode 100644
index 00000000000..e69de29bb2d
diff --git a/erpnext/manufacturing/doctype/bom_update_log/bom_update_log.js b/erpnext/manufacturing/doctype/bom_update_log/bom_update_log.js
new file mode 100644
index 00000000000..6da808e26d1
--- /dev/null
+++ b/erpnext/manufacturing/doctype/bom_update_log/bom_update_log.js
@@ -0,0 +1,8 @@
+// Copyright (c) 2022, Frappe Technologies Pvt. Ltd. and contributors
+// For license information, please see license.txt
+
+frappe.ui.form.on('BOM Update Log', {
+ // refresh: function(frm) {
+
+ // }
+});
diff --git a/erpnext/manufacturing/doctype/bom_update_log/bom_update_log.json b/erpnext/manufacturing/doctype/bom_update_log/bom_update_log.json
new file mode 100644
index 00000000000..98c1acb71ce
--- /dev/null
+++ b/erpnext/manufacturing/doctype/bom_update_log/bom_update_log.json
@@ -0,0 +1,109 @@
+{
+ "actions": [],
+ "autoname": "BOM-UPDT-LOG-.#####",
+ "creation": "2022-03-16 14:23:35.210155",
+ "description": "BOM Update Tool Log with job status maintained",
+ "doctype": "DocType",
+ "editable_grid": 1,
+ "engine": "InnoDB",
+ "field_order": [
+ "current_bom",
+ "new_bom",
+ "column_break_3",
+ "update_type",
+ "status",
+ "error_log",
+ "amended_from"
+ ],
+ "fields": [
+ {
+ "fieldname": "current_bom",
+ "fieldtype": "Link",
+ "in_list_view": 1,
+ "in_standard_filter": 1,
+ "label": "Current BOM",
+ "options": "BOM"
+ },
+ {
+ "fieldname": "new_bom",
+ "fieldtype": "Link",
+ "in_list_view": 1,
+ "in_standard_filter": 1,
+ "label": "New BOM",
+ "options": "BOM"
+ },
+ {
+ "fieldname": "column_break_3",
+ "fieldtype": "Column Break"
+ },
+ {
+ "fieldname": "update_type",
+ "fieldtype": "Select",
+ "in_list_view": 1,
+ "label": "Update Type",
+ "options": "Replace BOM\nUpdate Cost"
+ },
+ {
+ "fieldname": "status",
+ "fieldtype": "Select",
+ "label": "Status",
+ "options": "Queued\nIn Progress\nCompleted\nFailed"
+ },
+ {
+ "fieldname": "amended_from",
+ "fieldtype": "Link",
+ "label": "Amended From",
+ "no_copy": 1,
+ "options": "BOM Update Log",
+ "print_hide": 1,
+ "read_only": 1
+ },
+ {
+ "fieldname": "error_log",
+ "fieldtype": "Link",
+ "label": "Error Log",
+ "options": "Error Log"
+ }
+ ],
+ "in_create": 1,
+ "index_web_pages_for_search": 1,
+ "is_submittable": 1,
+ "links": [],
+ "modified": "2022-03-31 12:51:44.885102",
+ "modified_by": "Administrator",
+ "module": "Manufacturing",
+ "name": "BOM Update Log",
+ "naming_rule": "Expression (old style)",
+ "owner": "Administrator",
+ "permissions": [
+ {
+ "create": 1,
+ "delete": 1,
+ "email": 1,
+ "export": 1,
+ "print": 1,
+ "read": 1,
+ "report": 1,
+ "role": "System Manager",
+ "share": 1,
+ "submit": 1,
+ "write": 1
+ },
+ {
+ "create": 1,
+ "email": 1,
+ "export": 1,
+ "print": 1,
+ "read": 1,
+ "report": 1,
+ "role": "Manufacturing Manager",
+ "share": 1,
+ "submit": 1,
+ "write": 1
+ }
+ ],
+ "sort_field": "modified",
+ "sort_order": "DESC",
+ "states": [],
+ "track_changes": 1
+}
\ No newline at end of file
diff --git a/erpnext/manufacturing/doctype/bom_update_log/bom_update_log.py b/erpnext/manufacturing/doctype/bom_update_log/bom_update_log.py
new file mode 100644
index 00000000000..c3df96c99b1
--- /dev/null
+++ b/erpnext/manufacturing/doctype/bom_update_log/bom_update_log.py
@@ -0,0 +1,165 @@
+# Copyright (c) 2022, Frappe Technologies Pvt. Ltd. and contributors
+# For license information, please see license.txt
+from typing import Dict, List, Optional
+
+import frappe
+from frappe import _
+from frappe.model.document import Document
+from frappe.utils import cstr, flt
+from typing_extensions import Literal
+
+from erpnext.manufacturing.doctype.bom_update_tool.bom_update_tool import update_cost
+
+
+class BOMMissingError(frappe.ValidationError):
+ pass
+
+
+class BOMUpdateLog(Document):
+ def validate(self):
+ if self.update_type == "Replace BOM":
+ self.validate_boms_are_specified()
+ self.validate_same_bom()
+ self.validate_bom_items()
+
+ self.status = "Queued"
+
+ def validate_boms_are_specified(self):
+ if self.update_type == "Replace BOM" and not (self.current_bom and self.new_bom):
+ frappe.throw(
+ msg=_("Please mention the Current and New BOM for replacement."),
+ title=_("Mandatory"),
+ exc=BOMMissingError,
+ )
+
+ def validate_same_bom(self):
+ if cstr(self.current_bom) == cstr(self.new_bom):
+ frappe.throw(_("Current BOM and New BOM can not be same"))
+
+ def validate_bom_items(self):
+ current_bom_item = frappe.db.get_value("BOM", self.current_bom, "item")
+ new_bom_item = frappe.db.get_value("BOM", self.new_bom, "item")
+
+ if current_bom_item != new_bom_item:
+ frappe.throw(_("The selected BOMs are not for the same item"))
+
+ def on_submit(self):
+ if frappe.flags.in_test:
+ return
+
+ if self.update_type == "Replace BOM":
+ boms = {"current_bom": self.current_bom, "new_bom": self.new_bom}
+ frappe.enqueue(
+ method="erpnext.manufacturing.doctype.bom_update_log.bom_update_log.run_bom_job",
+ doc=self,
+ boms=boms,
+ timeout=40000,
+ )
+ else:
+ frappe.enqueue(
+ method="erpnext.manufacturing.doctype.bom_update_log.bom_update_log.run_bom_job",
+ doc=self,
+ update_type="Update Cost",
+ timeout=40000,
+ )
+
+
+def replace_bom(boms: Dict) -> None:
+ """Replace current BOM with new BOM in parent BOMs."""
+ current_bom = boms.get("current_bom")
+ new_bom = boms.get("new_bom")
+
+ unit_cost = get_new_bom_unit_cost(new_bom)
+ update_new_bom_in_bom_items(unit_cost, current_bom, new_bom)
+
+ frappe.cache().delete_key("bom_children")
+ parent_boms = get_parent_boms(new_bom)
+
+ for bom in parent_boms:
+ bom_obj = frappe.get_doc("BOM", bom)
+ # this is only used for versioning and we do not want
+ # to make separate db calls by using load_doc_before_save
+ # which proves to be expensive while doing bulk replace
+ bom_obj._doc_before_save = bom_obj
+ bom_obj.update_exploded_items()
+ bom_obj.calculate_cost()
+ bom_obj.update_parent_cost()
+ bom_obj.db_update()
+ if bom_obj.meta.get("track_changes") and not bom_obj.flags.ignore_version:
+ bom_obj.save_version()
+
+
+def update_new_bom_in_bom_items(unit_cost: float, current_bom: str, new_bom: str) -> None:
+ bom_item = frappe.qb.DocType("BOM Item")
+ (
+ frappe.qb.update(bom_item)
+ .set(bom_item.bom_no, new_bom)
+ .set(bom_item.rate, unit_cost)
+ .set(bom_item.amount, (bom_item.stock_qty * unit_cost))
+ .where(
+ (bom_item.bom_no == current_bom) & (bom_item.docstatus < 2) & (bom_item.parenttype == "BOM")
+ )
+ ).run()
+
+
+def get_parent_boms(new_bom: str, bom_list: Optional[List] = None) -> List:
+ bom_list = bom_list or []
+ bom_item = frappe.qb.DocType("BOM Item")
+
+ parents = (
+ frappe.qb.from_(bom_item)
+ .select(bom_item.parent)
+ .where((bom_item.bom_no == new_bom) & (bom_item.docstatus < 2) & (bom_item.parenttype == "BOM"))
+ .run(as_dict=True)
+ )
+
+ for d in parents:
+ if new_bom == d.parent:
+ frappe.throw(_("BOM recursion: {0} cannot be child of {1}").format(new_bom, d.parent))
+
+ bom_list.append(d.parent)
+ get_parent_boms(d.parent, bom_list)
+
+ return list(set(bom_list))
+
+
+def get_new_bom_unit_cost(new_bom: str) -> float:
+ bom = frappe.qb.DocType("BOM")
+ new_bom_unitcost = (
+ frappe.qb.from_(bom).select(bom.total_cost / bom.quantity).where(bom.name == new_bom).run()
+ )
+
+ return flt(new_bom_unitcost[0][0])
+
+
+def run_bom_job(
+ doc: "BOMUpdateLog",
+ boms: Optional[Dict[str, str]] = None,
+ update_type: Literal["Replace BOM", "Update Cost"] = "Replace BOM",
+) -> None:
+ try:
+ doc.db_set("status", "In Progress")
+ if not frappe.flags.in_test:
+ frappe.db.commit()
+
+ frappe.db.auto_commit_on_many_writes = 1
+
+ boms = frappe._dict(boms or {})
+
+ if update_type == "Replace BOM":
+ replace_bom(boms)
+ else:
+ update_cost()
+
+ doc.db_set("status", "Completed")
+
+ except Exception:
+ frappe.db.rollback()
+ error_log = frappe.log_error(message=frappe.get_traceback(), title=_("BOM Update Tool Error"))
+
+ doc.db_set("status", "Failed")
+ doc.db_set("error_log", error_log.name)
+
+ finally:
+ frappe.db.auto_commit_on_many_writes = 0
+ frappe.db.commit() # nosemgrep
diff --git a/erpnext/manufacturing/doctype/bom_update_log/bom_update_log_list.js b/erpnext/manufacturing/doctype/bom_update_log/bom_update_log_list.js
new file mode 100644
index 00000000000..e39b5637c78
--- /dev/null
+++ b/erpnext/manufacturing/doctype/bom_update_log/bom_update_log_list.js
@@ -0,0 +1,13 @@
+frappe.listview_settings['BOM Update Log'] = {
+ add_fields: ["status"],
+ get_indicator: function(doc) {
+ let status_map = {
+ "Queued": "orange",
+ "In Progress": "blue",
+ "Completed": "green",
+ "Failed": "red"
+ };
+
+ return [__(doc.status), status_map[doc.status], "status,=," + doc.status];
+ }
+};
\ No newline at end of file
diff --git a/erpnext/manufacturing/doctype/bom_update_log/test_bom_update_log.py b/erpnext/manufacturing/doctype/bom_update_log/test_bom_update_log.py
new file mode 100644
index 00000000000..47efea961b4
--- /dev/null
+++ b/erpnext/manufacturing/doctype/bom_update_log/test_bom_update_log.py
@@ -0,0 +1,96 @@
+# Copyright (c) 2022, Frappe Technologies Pvt. Ltd. and Contributors
+# See license.txt
+
+import frappe
+from frappe.tests.utils import FrappeTestCase
+
+from erpnext.manufacturing.doctype.bom_update_log.bom_update_log import (
+ BOMMissingError,
+ run_bom_job,
+)
+from erpnext.manufacturing.doctype.bom_update_tool.bom_update_tool import enqueue_replace_bom
+
+test_records = frappe.get_test_records("BOM")
+
+
+class TestBOMUpdateLog(FrappeTestCase):
+ "Test BOM Update Tool Operations via BOM Update Log."
+
+ def setUp(self):
+ bom_doc = frappe.copy_doc(test_records[0])
+ bom_doc.items[1].item_code = "_Test Item"
+ bom_doc.insert()
+
+ self.boms = frappe._dict(
+ current_bom="BOM-_Test Item Home Desktop Manufactured-001",
+ new_bom=bom_doc.name,
+ )
+
+ self.new_bom_doc = bom_doc
+
+ def tearDown(self):
+ frappe.db.rollback()
+
+ if self._testMethodName == "test_bom_update_log_completion":
+ # clear logs and delete BOM created via setUp
+ frappe.db.delete("BOM Update Log")
+ self.new_bom_doc.cancel()
+ self.new_bom_doc.delete()
+
+ # explicitly commit and restore to original state
+ frappe.db.commit() # nosemgrep
+
+ def test_bom_update_log_validate(self):
+ "Test if BOM presence is validated."
+
+ with self.assertRaises(BOMMissingError):
+ enqueue_replace_bom(boms={})
+
+ with self.assertRaises(frappe.ValidationError):
+ enqueue_replace_bom(boms=frappe._dict(current_bom=self.boms.new_bom, new_bom=self.boms.new_bom))
+
+ with self.assertRaises(frappe.ValidationError):
+ enqueue_replace_bom(boms=frappe._dict(current_bom=self.boms.new_bom, new_bom="Dummy BOM"))
+
+ def test_bom_update_log_queueing(self):
+ "Test if BOM Update Log is created and queued."
+
+ log = enqueue_replace_bom(
+ boms=self.boms,
+ )
+
+ self.assertEqual(log.docstatus, 1)
+ self.assertEqual(log.status, "Queued")
+
+ def test_bom_update_log_completion(self):
+ "Test if BOM Update Log handles job completion correctly."
+
+ log = enqueue_replace_bom(
+ boms=self.boms,
+ )
+
+ # Explicitly commits log, new bom (setUp) and replacement impact.
+ # Is run via background jobs IRL
+ run_bom_job(
+ doc=log,
+ boms=self.boms,
+ update_type="Replace BOM",
+ )
+ log.reload()
+
+ self.assertEqual(log.status, "Completed")
+
+ # teardown (undo replace impact) due to commit
+ boms = frappe._dict(
+ current_bom=self.boms.new_bom,
+ new_bom=self.boms.current_bom,
+ )
+ log2 = enqueue_replace_bom(
+ boms=self.boms,
+ )
+ run_bom_job( # Explicitly commits
+ doc=log2,
+ boms=boms,
+ update_type="Replace BOM",
+ )
+ self.assertEqual(log2.status, "Completed")
diff --git a/erpnext/manufacturing/doctype/bom_update_tool/bom_update_tool.js b/erpnext/manufacturing/doctype/bom_update_tool/bom_update_tool.js
index bf5fe2e18de..7ba6517a4fb 100644
--- a/erpnext/manufacturing/doctype/bom_update_tool/bom_update_tool.js
+++ b/erpnext/manufacturing/doctype/bom_update_tool/bom_update_tool.js
@@ -20,30 +20,67 @@ frappe.ui.form.on('BOM Update Tool', {
refresh: function(frm) {
frm.disable_save();
+ frm.events.disable_button(frm, "replace");
+
+ frm.add_custom_button(__("View BOM Update Log"), () => {
+ frappe.set_route("List", "BOM Update Log");
+ });
},
- replace: function(frm) {
+ disable_button: (frm, field, disable=true) => {
+ frm.get_field(field).input.disabled = disable;
+ },
+
+ current_bom: (frm) => {
+ if (frm.doc.current_bom && frm.doc.new_bom) {
+ frm.events.disable_button(frm, "replace", false);
+ }
+ },
+
+ new_bom: (frm) => {
+ if (frm.doc.current_bom && frm.doc.new_bom) {
+ frm.events.disable_button(frm, "replace", false);
+ }
+ },
+
+ replace: (frm) => {
if (frm.doc.current_bom && frm.doc.new_bom) {
frappe.call({
method: "erpnext.manufacturing.doctype.bom_update_tool.bom_update_tool.enqueue_replace_bom",
freeze: true,
args: {
- args: {
+ boms: {
"current_bom": frm.doc.current_bom,
"new_bom": frm.doc.new_bom
}
+ },
+ callback: result => {
+ if (result && result.message && !result.exc) {
+ frm.events.confirm_job_start(frm, result.message);
+ }
}
});
}
},
- update_latest_price_in_all_boms: function() {
+ update_latest_price_in_all_boms: (frm) => {
frappe.call({
method: "erpnext.manufacturing.doctype.bom_update_tool.bom_update_tool.enqueue_update_cost",
freeze: true,
- callback: function() {
- frappe.msgprint(__("Latest price updated in all BOMs"));
+ callback: result => {
+ if (result && result.message && !result.exc) {
+ frm.events.confirm_job_start(frm, result.message);
+ }
}
});
+ },
+
+ confirm_job_start: (frm, log_data) => {
+ let log_link = frappe.utils.get_form_link("BOM Update Log", log_data.name, true);
+ frappe.msgprint({
+ "message": __("BOM Updation is queued and may take a few minutes. Check {0} for progress.", [log_link]),
+ "title": __("BOM Update Initiated"),
+ "indicator": "blue"
+ });
}
});
diff --git a/erpnext/manufacturing/doctype/bom_update_tool/bom_update_tool.py b/erpnext/manufacturing/doctype/bom_update_tool/bom_update_tool.py
index 9f120d175ed..4061c5af7c2 100644
--- a/erpnext/manufacturing/doctype/bom_update_tool/bom_update_tool.py
+++ b/erpnext/manufacturing/doctype/bom_update_tool/bom_update_tool.py
@@ -1,137 +1,71 @@
-# Copyright (c) 2017, Frappe Technologies Pvt. Ltd. and contributors
+# Copyright (c) 2022, Frappe Technologies Pvt. Ltd. and contributors
# For license information, please see license.txt
-
import json
+from typing import TYPE_CHECKING, Dict, Optional, Union
+
+from typing_extensions import Literal
+
+if TYPE_CHECKING:
+ from erpnext.manufacturing.doctype.bom_update_log.bom_update_log import BOMUpdateLog
-import click
import frappe
-from frappe import _
from frappe.model.document import Document
-from frappe.utils import cstr, flt
-from six import string_types
from erpnext.manufacturing.doctype.bom.bom import get_boms_in_bottom_up_order
class BOMUpdateTool(Document):
- def replace_bom(self):
- self.validate_bom()
-
- unit_cost = get_new_bom_unit_cost(self.new_bom)
- self.update_new_bom(unit_cost)
-
- frappe.cache().delete_key("bom_children")
- bom_list = self.get_parent_boms(self.new_bom)
-
- with click.progressbar(bom_list) as bom_list:
- pass
- for bom in bom_list:
- try:
- bom_obj = frappe.get_cached_doc("BOM", bom)
- # this is only used for versioning and we do not want
- # to make separate db calls by using load_doc_before_save
- # which proves to be expensive while doing bulk replace
- bom_obj._doc_before_save = bom_obj
- bom_obj.update_new_bom(self.current_bom, self.new_bom, unit_cost)
- bom_obj.update_exploded_items()
- bom_obj.calculate_cost()
- bom_obj.update_parent_cost()
- bom_obj.db_update()
- if bom_obj.meta.get("track_changes") and not bom_obj.flags.ignore_version:
- bom_obj.save_version()
- except Exception:
- frappe.log_error(frappe.get_traceback())
-
- def validate_bom(self):
- if cstr(self.current_bom) == cstr(self.new_bom):
- frappe.throw(_("Current BOM and New BOM can not be same"))
-
- if frappe.db.get_value("BOM", self.current_bom, "item") != frappe.db.get_value(
- "BOM", self.new_bom, "item"
- ):
- frappe.throw(_("The selected BOMs are not for the same item"))
-
- def update_new_bom(self, unit_cost):
- frappe.db.sql(
- """update `tabBOM Item` set bom_no=%s,
- rate=%s, amount=stock_qty*%s where bom_no = %s and docstatus < 2 and parenttype='BOM'""",
- (self.new_bom, unit_cost, unit_cost, self.current_bom),
- )
-
- def get_parent_boms(self, bom, bom_list=None):
- if bom_list is None:
- bom_list = []
- data = frappe.db.sql(
- """SELECT DISTINCT parent FROM `tabBOM Item`
- WHERE bom_no = %s AND docstatus < 2 AND parenttype='BOM'""",
- bom,
- )
-
- for d in data:
- if self.new_bom == d[0]:
- frappe.throw(_("BOM recursion: {0} cannot be child of {1}").format(bom, self.new_bom))
-
- bom_list.append(d[0])
- self.get_parent_boms(d[0], bom_list)
-
- return list(set(bom_list))
-
-
-def get_new_bom_unit_cost(bom):
- new_bom_unitcost = frappe.db.sql(
- """SELECT `total_cost`/`quantity`
- FROM `tabBOM` WHERE name = %s""",
- bom,
- )
-
- return flt(new_bom_unitcost[0][0]) if new_bom_unitcost else 0
+ pass
@frappe.whitelist()
-def enqueue_replace_bom(args):
- if isinstance(args, string_types):
- args = json.loads(args)
+def enqueue_replace_bom(
+ boms: Optional[Union[Dict, str]] = None, args: Optional[Union[Dict, str]] = None
+) -> "BOMUpdateLog":
+ """Returns a BOM Update Log (that queues a job) for BOM Replacement."""
+ boms = boms or args
+ if isinstance(boms, str):
+ boms = json.loads(boms)
- frappe.enqueue(
- "erpnext.manufacturing.doctype.bom_update_tool.bom_update_tool.replace_bom",
- args=args,
- timeout=40000,
- )
- frappe.msgprint(_("Queued for replacing the BOM. It may take a few minutes."))
+ update_log = create_bom_update_log(boms=boms)
+ return update_log
@frappe.whitelist()
-def enqueue_update_cost():
- frappe.enqueue(
- "erpnext.manufacturing.doctype.bom_update_tool.bom_update_tool.update_cost", timeout=40000
- )
- frappe.msgprint(
- _("Queued for updating latest price in all Bill of Materials. It may take a few minutes.")
- )
+def enqueue_update_cost() -> "BOMUpdateLog":
+ """Returns a BOM Update Log (that queues a job) for BOM Cost Updation."""
+ update_log = create_bom_update_log(update_type="Update Cost")
+ return update_log
-def update_latest_price_in_all_boms():
+def auto_update_latest_price_in_all_boms() -> None:
+ """Called via hooks.py."""
if frappe.db.get_single_value("Manufacturing Settings", "update_bom_costs_automatically"):
update_cost()
-def replace_bom(args):
- frappe.db.auto_commit_on_many_writes = 1
- args = frappe._dict(args)
-
- doc = frappe.get_doc("BOM Update Tool")
- doc.current_bom = args.current_bom
- doc.new_bom = args.new_bom
- doc.replace_bom()
-
- frappe.db.auto_commit_on_many_writes = 0
-
-
-def update_cost():
- frappe.db.auto_commit_on_many_writes = 1
+def update_cost() -> None:
+ """Updates Cost for all BOMs from bottom to top."""
bom_list = get_boms_in_bottom_up_order()
for bom in bom_list:
frappe.get_doc("BOM", bom).update_cost(update_parent=False, from_child_bom=True)
- frappe.db.auto_commit_on_many_writes = 0
+
+def create_bom_update_log(
+ boms: Optional[Dict[str, str]] = None,
+ update_type: Literal["Replace BOM", "Update Cost"] = "Replace BOM",
+) -> "BOMUpdateLog":
+ """Creates a BOM Update Log that handles the background job."""
+
+ boms = boms or {}
+ current_bom = boms.get("current_bom")
+ new_bom = boms.get("new_bom")
+ return frappe.get_doc(
+ {
+ "doctype": "BOM Update Log",
+ "current_bom": current_bom,
+ "new_bom": new_bom,
+ "update_type": update_type,
+ }
+ ).submit()
diff --git a/erpnext/manufacturing/doctype/bom_update_tool/test_bom_update_tool.py b/erpnext/manufacturing/doctype/bom_update_tool/test_bom_update_tool.py
index 57785e58dd0..fae72a0f6f7 100644
--- a/erpnext/manufacturing/doctype/bom_update_tool/test_bom_update_tool.py
+++ b/erpnext/manufacturing/doctype/bom_update_tool/test_bom_update_tool.py
@@ -4,6 +4,7 @@
import frappe
from frappe.tests.utils import FrappeTestCase
+from erpnext.manufacturing.doctype.bom_update_log.bom_update_log import replace_bom
from erpnext.manufacturing.doctype.bom_update_tool.bom_update_tool import update_cost
from erpnext.manufacturing.doctype.production_plan.test_production_plan import make_bom
from erpnext.stock.doctype.item.test_item import create_item
@@ -12,6 +13,8 @@ test_records = frappe.get_test_records("BOM")
class TestBOMUpdateTool(FrappeTestCase):
+ "Test major functions run via BOM Update Tool."
+
def test_replace_bom(self):
current_bom = "BOM-_Test Item Home Desktop Manufactured-001"
@@ -19,18 +22,16 @@ class TestBOMUpdateTool(FrappeTestCase):
bom_doc.items[1].item_code = "_Test Item"
bom_doc.insert()
- update_tool = frappe.get_doc("BOM Update Tool")
- update_tool.current_bom = current_bom
- update_tool.new_bom = bom_doc.name
- update_tool.replace_bom()
+ boms = frappe._dict(current_bom=current_bom, new_bom=bom_doc.name)
+ replace_bom(boms)
self.assertFalse(frappe.db.sql("select name from `tabBOM Item` where bom_no=%s", current_bom))
self.assertTrue(frappe.db.sql("select name from `tabBOM Item` where bom_no=%s", bom_doc.name))
# reverse, as it affects other testcases
- update_tool.current_bom = bom_doc.name
- update_tool.new_bom = current_bom
- update_tool.replace_bom()
+ boms.current_bom = bom_doc.name
+ boms.new_bom = current_bom
+ replace_bom(boms)
def test_bom_cost(self):
for item in ["BOM Cost Test Item 1", "BOM Cost Test Item 2", "BOM Cost Test Item 3"]:
diff --git a/erpnext/manufacturing/doctype/work_order/test_work_order.py b/erpnext/manufacturing/doctype/work_order/test_work_order.py
index 3709c7db101..1e9b3ba1137 100644
--- a/erpnext/manufacturing/doctype/work_order/test_work_order.py
+++ b/erpnext/manufacturing/doctype/work_order/test_work_order.py
@@ -1070,6 +1070,36 @@ class TestWorkOrder(FrappeTestCase):
except frappe.MandatoryError:
self.fail("Batch generation causing failing in Work Order")
+ @change_settings(
+ "Manufacturing Settings",
+ {"backflush_raw_materials_based_on": "Material Transferred for Manufacture"},
+ )
+ def test_manufacture_entry_mapped_idx_with_exploded_bom(self):
+ """Test if WO containing BOM with partial exploded items and scrap items, maps idx correctly."""
+ test_stock_entry.make_stock_entry(
+ item_code="_Test Item",
+ target="_Test Warehouse - _TC",
+ basic_rate=5000.0,
+ qty=2,
+ )
+ test_stock_entry.make_stock_entry(
+ item_code="_Test Item Home Desktop 100",
+ target="_Test Warehouse - _TC",
+ basic_rate=1000.0,
+ qty=2,
+ )
+
+ wo_order = make_wo_order_test_record(
+ qty=1,
+ use_multi_level_bom=1,
+ skip_transfer=1,
+ )
+
+ ste_manu = frappe.get_doc(make_stock_entry(wo_order.name, "Manufacture", 1))
+
+ for index, row in enumerate(ste_manu.get("items"), start=1):
+ self.assertEqual(index, row.idx)
+
def update_job_card(job_card, jc_qty=None):
employee = frappe.db.get_value("Employee", {"status": "Active"}, "name")
diff --git a/erpnext/manufacturing/doctype/work_order/work_order.py b/erpnext/manufacturing/doctype/work_order/work_order.py
index f2586825cc7..7ef39c26aa0 100644
--- a/erpnext/manufacturing/doctype/work_order/work_order.py
+++ b/erpnext/manufacturing/doctype/work_order/work_order.py
@@ -1315,7 +1315,7 @@ def get_serial_nos_for_job_card(row, wo_doc):
used_serial_nos.extend(get_serial_nos(d.serial_no))
serial_nos = sorted(list(set(serial_nos) - set(used_serial_nos)))
- row.serial_no = "\n".join(serial_nos[0 : row.job_card_qty])
+ row.serial_no = "\n".join(serial_nos[0 : cint(row.job_card_qty)])
def validate_operation_data(row):
diff --git a/erpnext/patches.txt b/erpnext/patches.txt
index 50050afd316..98e07783c11 100644
--- a/erpnext/patches.txt
+++ b/erpnext/patches.txt
@@ -357,3 +357,4 @@ erpnext.patches.v13_0.rename_non_profit_fields
erpnext.patches.v13_0.enable_ksa_vat_docs #1
erpnext.patches.v13_0.create_gst_custom_fields_in_quotation
erpnext.patches.v13_0.update_expense_claim_status_for_paid_advances
+erpnext.patches.v13_0.set_return_against_in_pos_invoice_references
diff --git a/erpnext/patches/v12_0/move_item_tax_to_item_tax_template.py b/erpnext/patches/v12_0/move_item_tax_to_item_tax_template.py
index c658dde57c5..35354dade75 100644
--- a/erpnext/patches/v12_0/move_item_tax_to_item_tax_template.py
+++ b/erpnext/patches/v12_0/move_item_tax_to_item_tax_template.py
@@ -113,6 +113,7 @@ def get_item_tax_template(
# if no item tax template found, create one
item_tax_template = frappe.new_doc("Item Tax Template")
item_tax_template.title = make_autoname("Item Tax Template-.####")
+ item_tax_template_name = item_tax_template.title
for tax_type, tax_rate in iteritems(item_tax_map):
account_details = frappe.db.get_value(
@@ -120,6 +121,10 @@ def get_item_tax_template(
)
if account_details:
item_tax_template.company = account_details.company
+ if not item_tax_template_name:
+ # set name once company is set as name is generated from company & title
+ # setting name is required to update `item_tax_templates` dict
+ item_tax_template_name = item_tax_template.set_new_name()
if account_details.account_type not in (
"Tax",
"Chargeable",
@@ -179,8 +184,9 @@ def get_item_tax_template(
if tax_type not in tax_types:
item_tax_template.append("taxes", {"tax_type": tax_type, "tax_rate": tax_rate})
tax_types.append(tax_type)
- item_tax_templates.setdefault(item_tax_template.title, {})
- item_tax_templates[item_tax_template.title][tax_type] = tax_rate
+ item_tax_templates.setdefault(item_tax_template_name, {})
+ item_tax_templates[item_tax_template_name][tax_type] = tax_rate
+
if item_tax_template.get("taxes"):
item_tax_template.save()
return item_tax_template.name
diff --git a/erpnext/patches/v13_0/set_return_against_in_pos_invoice_references.py b/erpnext/patches/v13_0/set_return_against_in_pos_invoice_references.py
new file mode 100644
index 00000000000..6af9617bcee
--- /dev/null
+++ b/erpnext/patches/v13_0/set_return_against_in_pos_invoice_references.py
@@ -0,0 +1,38 @@
+import frappe
+
+
+def execute():
+ """
+ Fetch and Set is_return & return_against from POS Invoice in POS Invoice References table.
+ """
+
+ POSClosingEntry = frappe.qb.DocType("POS Closing Entry")
+ open_pos_closing_entries = (
+ frappe.qb.from_(POSClosingEntry)
+ .select(POSClosingEntry.name)
+ .where(POSClosingEntry.docstatus == 0)
+ .run()
+ )
+ if open_pos_closing_entries:
+ open_pos_closing_entries = [d[0] for d in open_pos_closing_entries]
+
+ if not open_pos_closing_entries:
+ return
+
+ POSInvoiceReference = frappe.qb.DocType("POS Invoice Reference")
+ POSInvoice = frappe.qb.DocType("POS Invoice")
+ pos_invoice_references = (
+ frappe.qb.from_(POSInvoiceReference)
+ .join(POSInvoice)
+ .on(POSInvoiceReference.pos_invoice == POSInvoice.name)
+ .select(POSInvoiceReference.name, POSInvoice.is_return, POSInvoice.return_against)
+ .where(POSInvoiceReference.parent.isin(open_pos_closing_entries))
+ .run(as_dict=True)
+ )
+
+ for row in pos_invoice_references:
+ frappe.db.set_value("POS Invoice Reference", row.name, "is_return", row.is_return)
+ if row.is_return:
+ frappe.db.set_value("POS Invoice Reference", row.name, "return_against", row.return_against)
+ else:
+ frappe.db.set_value("POS Invoice Reference", row.name, "return_against", None)
diff --git a/erpnext/payroll/doctype/additional_salary/additional_salary.py b/erpnext/payroll/doctype/additional_salary/additional_salary.py
index 74b780e9e9d..f57d9d37cf1 100644
--- a/erpnext/payroll/doctype/additional_salary/additional_salary.py
+++ b/erpnext/payroll/doctype/additional_salary/additional_salary.py
@@ -145,6 +145,8 @@ class AdditionalSalary(Document):
@frappe.whitelist()
def get_additional_salaries(employee, start_date, end_date, component_type):
+ from frappe.query_builder import Criterion
+
comp_type = "Earning" if component_type == "earnings" else "Deduction"
additional_sal = frappe.qb.DocType("Additional Salary")
@@ -168,8 +170,23 @@ def get_additional_salaries(employee, start_date, end_date, component_type):
& (additional_sal.type == comp_type)
)
.where(
- additional_sal.payroll_date[start_date:end_date]
- | ((additional_sal.from_date <= end_date) & (additional_sal.to_date >= end_date))
+ Criterion.any(
+ [
+ Criterion.all(
+ [ # is recurring and additional salary dates fall within the payroll period
+ additional_sal.is_recurring == 1,
+ additional_sal.from_date <= end_date,
+ additional_sal.to_date >= end_date,
+ ]
+ ),
+ Criterion.all(
+ [ # is not recurring and additional salary's payroll date falls within the payroll period
+ additional_sal.is_recurring == 0,
+ additional_sal.payroll_date[start_date:end_date],
+ ]
+ ),
+ ]
+ )
)
.run(as_dict=True)
)
diff --git a/erpnext/payroll/doctype/additional_salary/test_additional_salary.py b/erpnext/payroll/doctype/additional_salary/test_additional_salary.py
index 7d5d9e02f34..bd739368a0a 100644
--- a/erpnext/payroll/doctype/additional_salary/test_additional_salary.py
+++ b/erpnext/payroll/doctype/additional_salary/test_additional_salary.py
@@ -4,7 +4,8 @@
import unittest
import frappe
-from frappe.utils import add_days, nowdate
+from frappe.tests.utils import FrappeTestCase
+from frappe.utils import add_days, add_months, nowdate
import erpnext
from erpnext.hr.doctype.employee.test_employee import make_employee
@@ -16,19 +17,10 @@ from erpnext.payroll.doctype.salary_slip.test_salary_slip import (
from erpnext.payroll.doctype.salary_structure.test_salary_structure import make_salary_structure
-class TestAdditionalSalary(unittest.TestCase):
+class TestAdditionalSalary(FrappeTestCase):
def setUp(self):
setup_test()
- def tearDown(self):
- for dt in [
- "Salary Slip",
- "Additional Salary",
- "Salary Structure Assignment",
- "Salary Structure",
- ]:
- frappe.db.sql("delete from `tab%s`" % dt)
-
def test_recurring_additional_salary(self):
amount = 0
salary_component = None
@@ -46,19 +38,66 @@ class TestAdditionalSalary(unittest.TestCase):
if earning.salary_component == "Recurring Salary Component":
amount = earning.amount
salary_component = earning.salary_component
+ break
self.assertEqual(amount, add_sal.amount)
self.assertEqual(salary_component, add_sal.salary_component)
+ def test_non_recurring_additional_salary(self):
+ amount = 0
+ salary_component = None
+ date = nowdate()
-def get_additional_salary(emp_id):
+ emp_id = make_employee("test_additional@salary.com")
+ frappe.db.set_value("Employee", emp_id, "relieving_date", add_days(date, 1800))
+ salary_structure = make_salary_structure(
+ "Test Salary Structure Additional Salary", "Monthly", employee=emp_id
+ )
+ add_sal = get_additional_salary(emp_id, recurring=False, payroll_date=date)
+
+ ss = make_employee_salary_slip(
+ "test_additional@salary.com", "Monthly", salary_structure=salary_structure.name
+ )
+
+ amount, salary_component = None, None
+ for earning in ss.earnings:
+ if earning.salary_component == "Recurring Salary Component":
+ amount = earning.amount
+ salary_component = earning.salary_component
+ break
+
+ self.assertEqual(amount, add_sal.amount)
+ self.assertEqual(salary_component, add_sal.salary_component)
+
+ # should not show up in next months
+ ss.posting_date = add_months(date, 1)
+ ss.start_date = ss.end_date = None
+ ss.earnings = []
+ ss.deductions = []
+ ss.save()
+
+ amount, salary_component = None, None
+ for earning in ss.earnings:
+ if earning.salary_component == "Recurring Salary Component":
+ amount = earning.amount
+ salary_component = earning.salary_component
+ break
+
+ self.assertIsNone(amount)
+ self.assertIsNone(salary_component)
+
+
+def get_additional_salary(emp_id, recurring=True, payroll_date=None):
create_salary_component("Recurring Salary Component")
add_sal = frappe.new_doc("Additional Salary")
add_sal.employee = emp_id
add_sal.salary_component = "Recurring Salary Component"
- add_sal.is_recurring = 1
+
+ add_sal.is_recurring = 1 if recurring else 0
add_sal.from_date = add_days(nowdate(), -50)
add_sal.to_date = add_days(nowdate(), 180)
+ add_sal.payroll_date = payroll_date
+
add_sal.amount = 5000
add_sal.currency = erpnext.get_default_currency()
add_sal.save()
diff --git a/erpnext/payroll/doctype/salary_slip/test_salary_slip.py b/erpnext/payroll/doctype/salary_slip/test_salary_slip.py
index 2a68d9b979a..65dae625b6e 100644
--- a/erpnext/payroll/doctype/salary_slip/test_salary_slip.py
+++ b/erpnext/payroll/doctype/salary_slip/test_salary_slip.py
@@ -1294,7 +1294,16 @@ def create_additional_salary(employee, payroll_period, amount):
return salary_date
-def make_leave_application(employee, from_date, to_date, leave_type, company=None, submit=True):
+def make_leave_application(
+ employee,
+ from_date,
+ to_date,
+ leave_type,
+ company=None,
+ half_day=False,
+ half_day_date=None,
+ submit=True,
+):
leave_application = frappe.get_doc(
dict(
doctype="Leave Application",
@@ -1302,6 +1311,8 @@ def make_leave_application(employee, from_date, to_date, leave_type, company=Non
leave_type=leave_type,
from_date=from_date,
to_date=to_date,
+ half_day=half_day,
+ half_day_date=half_day_date,
company=company or erpnext.get_default_company() or "_Test Company",
status="Approved",
leave_approver="test@example.com",
diff --git a/erpnext/public/js/controllers/taxes_and_totals.js b/erpnext/public/js/controllers/taxes_and_totals.js
index 308f0a4871f..2b1b0e3576b 100644
--- a/erpnext/public/js/controllers/taxes_and_totals.js
+++ b/erpnext/public/js/controllers/taxes_and_totals.js
@@ -271,6 +271,11 @@ erpnext.taxes_and_totals = erpnext.payments.extend({
},
calculate_shipping_charges: function() {
+ // Do not apply shipping rule for POS
+ if (this.frm.doc.is_pos) {
+ return;
+ }
+
frappe.model.round_floats_in(this.frm.doc, ["total", "base_total", "net_total", "base_net_total"]);
if (frappe.meta.get_docfield(this.frm.doc.doctype, "shipping_rule", this.frm.doc.name)) {
return this.shipping_rule();
diff --git a/erpnext/regional/india/e_invoice/einvoice.js b/erpnext/regional/india/e_invoice/einvoice.js
index 348f0c6feed..17b018c65b4 100644
--- a/erpnext/regional/india/e_invoice/einvoice.js
+++ b/erpnext/regional/india/e_invoice/einvoice.js
@@ -105,6 +105,30 @@ erpnext.setup_einvoice_actions = (doctype) => {
},
primary_action_label: __('Submit')
});
+ d.fields_dict.transporter.df.onchange = function () {
+ const transporter = d.fields_dict.transporter.value;
+ if (transporter) {
+ frappe.db.get_value('Supplier', transporter, ['gst_transporter_id', 'supplier_name'])
+ .then(({ message }) => {
+ d.set_value('gst_transporter_id', message.gst_transporter_id);
+ d.set_value('transporter_name', message.supplier_name);
+ });
+ } else {
+ d.set_value('gst_transporter_id', '');
+ d.set_value('transporter_name', '');
+ }
+ };
+ d.fields_dict.driver.df.onchange = function () {
+ const driver = d.fields_dict.driver.value;
+ if (driver) {
+ frappe.db.get_value('Driver', driver, ['full_name'])
+ .then(({ message }) => {
+ d.set_value('driver_name', message.full_name);
+ });
+ } else {
+ d.set_value('driver_name', '');
+ }
+ };
d.show();
};
@@ -153,7 +177,6 @@ const get_ewaybill_fields = (frm) => {
'fieldname': 'gst_transporter_id',
'label': 'GST Transporter ID',
'fieldtype': 'Data',
- 'fetch_from': 'transporter.gst_transporter_id',
'default': frm.doc.gst_transporter_id
},
{
@@ -189,9 +212,9 @@ const get_ewaybill_fields = (frm) => {
'fieldname': 'transporter_name',
'label': 'Transporter Name',
'fieldtype': 'Data',
- 'fetch_from': 'transporter.name',
'read_only': 1,
- 'default': frm.doc.transporter_name
+ 'default': frm.doc.transporter_name,
+ 'depends_on': 'transporter'
},
{
'fieldname': 'mode_of_transport',
@@ -206,7 +229,8 @@ const get_ewaybill_fields = (frm) => {
'fieldtype': 'Data',
'fetch_from': 'driver.full_name',
'read_only': 1,
- 'default': frm.doc.driver_name
+ 'default': frm.doc.driver_name,
+ 'depends_on': 'driver'
},
{
'fieldname': 'lr_date',
diff --git a/erpnext/regional/india/e_invoice/utils.py b/erpnext/regional/india/e_invoice/utils.py
index 990fe25e59f..95cbcd51a9f 100644
--- a/erpnext/regional/india/e_invoice/utils.py
+++ b/erpnext/regional/india/e_invoice/utils.py
@@ -388,7 +388,7 @@ def update_other_charges(
def get_payment_details(invoice):
payee_name = invoice.company
- mode_of_payment = ", ".join([d.mode_of_payment for d in invoice.payments])
+ mode_of_payment = ""
paid_amount = invoice.base_paid_amount
outstanding_amount = invoice.outstanding_amount
diff --git a/erpnext/regional/india/utils.py b/erpnext/regional/india/utils.py
index f6f2f8d4934..4eeb83779b3 100644
--- a/erpnext/regional/india/utils.py
+++ b/erpnext/regional/india/utils.py
@@ -269,6 +269,7 @@ def get_regional_address_details(party_details, doctype, company):
if tax_template_by_category:
party_details["taxes_and_charges"] = tax_template_by_category
+ party_details["taxes"] = get_taxes_and_charges(master_doctype, tax_template_by_category)
return party_details
if not party_details.place_of_supply:
@@ -293,7 +294,7 @@ def get_regional_address_details(party_details, doctype, company):
return party_details
party_details["taxes_and_charges"] = default_tax
- party_details.taxes = get_taxes_and_charges(master_doctype, default_tax)
+ party_details["taxes"] = get_taxes_and_charges(master_doctype, default_tax)
return party_details
diff --git a/erpnext/selling/doctype/selling_settings/selling_settings.json b/erpnext/selling/doctype/selling_settings/selling_settings.json
index a0c1c85dd52..eabfd7d2e22 100644
--- a/erpnext/selling/doctype/selling_settings/selling_settings.json
+++ b/erpnext/selling/doctype/selling_settings/selling_settings.json
@@ -13,6 +13,7 @@
"territory",
"crm_settings_section",
"campaign_naming_by",
+ "contract_naming_by",
"default_valid_till",
"column_break_9",
"close_opportunity_after_days",
@@ -29,7 +30,6 @@
"so_required",
"dn_required",
"sales_update_frequency",
- "column_break_5",
"allow_multiple_items",
"allow_against_multiple_purchase_orders",
"hide_tax_id"
@@ -193,6 +193,12 @@
"fieldname": "sales_transactions_settings_section",
"fieldtype": "Section Break",
"label": "Transaction Settings"
+ },
+ {
+ "fieldname": "contract_naming_by",
+ "fieldtype": "Select",
+ "label": "Contract Naming By",
+ "options": "Party Name\nNaming Series"
}
],
"icon": "fa fa-cog",
@@ -200,7 +206,7 @@
"index_web_pages_for_search": 1,
"issingle": 1,
"links": [],
- "modified": "2021-09-14 22:05:06.139820",
+ "modified": "2022-03-28 12:18:06.768403",
"modified_by": "Administrator",
"module": "Selling",
"name": "Selling Settings",
@@ -219,4 +225,4 @@
"sort_field": "modified",
"sort_order": "DESC",
"track_changes": 1
-}
+}
\ No newline at end of file
diff --git a/erpnext/selling/page/point_of_sale/point_of_sale.py b/erpnext/selling/page/point_of_sale/point_of_sale.py
index 4efb1a03f7e..bf629824ad9 100644
--- a/erpnext/selling/page/point_of_sale/point_of_sale.py
+++ b/erpnext/selling/page/point_of_sale/point_of_sale.py
@@ -8,7 +8,7 @@ import frappe
from frappe.utils.nestedset import get_root_of
from erpnext.accounts.doctype.pos_invoice.pos_invoice import get_stock_availability
-from erpnext.accounts.doctype.pos_profile.pos_profile import get_item_groups
+from erpnext.accounts.doctype.pos_profile.pos_profile import get_child_nodes, get_item_groups
def search_by_term(search_term, warehouse, price_list):
@@ -324,3 +324,17 @@ def set_customer_info(fieldname, customer, value=""):
contact_doc.set("phone_nos", [{"phone": value, "is_primary_mobile_no": 1}])
frappe.db.set_value("Customer", customer, "mobile_no", value)
contact_doc.save()
+
+
+@frappe.whitelist()
+def get_pos_profile_data(pos_profile):
+ pos_profile = frappe.get_doc("POS Profile", pos_profile)
+ pos_profile = pos_profile.as_dict()
+
+ _customer_groups_with_children = []
+ for row in pos_profile.customer_groups:
+ children = get_child_nodes("Customer Group", row.customer_group)
+ _customer_groups_with_children.extend(children)
+
+ pos_profile.customer_groups = _customer_groups_with_children
+ return pos_profile
diff --git a/erpnext/selling/page/point_of_sale/pos_controller.js b/erpnext/selling/page/point_of_sale/pos_controller.js
index ea8459f970b..6974bed4f1f 100644
--- a/erpnext/selling/page/point_of_sale/pos_controller.js
+++ b/erpnext/selling/page/point_of_sale/pos_controller.js
@@ -119,10 +119,15 @@ erpnext.PointOfSale.Controller = class {
this.allow_negative_stock = flt(message.allow_negative_stock) || false;
});
- frappe.db.get_doc("POS Profile", this.pos_profile).then((profile) => {
- Object.assign(this.settings, profile);
- this.settings.customer_groups = profile.customer_groups.map(group => group.customer_group);
- this.make_app();
+ frappe.call({
+ method: "erpnext.selling.page.point_of_sale.point_of_sale.get_pos_profile_data",
+ args: { "pos_profile": this.pos_profile },
+ callback: (res) => {
+ const profile = res.message;
+ Object.assign(this.settings, profile);
+ this.settings.customer_groups = profile.customer_groups.map(group => group.name);
+ this.make_app();
+ }
});
}
@@ -555,7 +560,7 @@ erpnext.PointOfSale.Controller = class {
if (this.item_details.$component.is(':visible'))
this.edit_item_details_of(item_row);
- if (this.check_serial_batch_selection_needed(item_row))
+ if (this.check_serial_batch_selection_needed(item_row) && !this.item_details.$component.is(':visible'))
this.edit_item_details_of(item_row);
}
@@ -704,7 +709,7 @@ erpnext.PointOfSale.Controller = class {
frappe.dom.freeze();
const { doctype, name, current_item } = this.item_details;
- frappe.model.set_value(doctype, name, 'qty', 0)
+ return frappe.model.set_value(doctype, name, 'qty', 0)
.then(() => {
frappe.model.clear_doc(doctype, name);
this.update_cart_html(current_item, true);
@@ -715,7 +720,14 @@ erpnext.PointOfSale.Controller = class {
}
async save_and_checkout() {
- this.frm.is_dirty() && await this.frm.save();
- this.payment.checkout();
+ if (this.frm.is_dirty()) {
+ // only move to payment section if save is successful
+ frappe.route_hooks.after_save = () => this.payment.checkout();
+ return this.frm.save(
+ null, null, null, () => this.cart.toggle_checkout_btn(true) // show checkout button on error
+ );
+ } else {
+ this.payment.checkout();
+ }
}
};
diff --git a/erpnext/selling/page/point_of_sale/pos_item_details.js b/erpnext/selling/page/point_of_sale/pos_item_details.js
index a3ad0025943..1d720f7291a 100644
--- a/erpnext/selling/page/point_of_sale/pos_item_details.js
+++ b/erpnext/selling/page/point_of_sale/pos_item_details.js
@@ -60,12 +60,18 @@ erpnext.PointOfSale.ItemDetails = class {
return item && item.name == this.current_item.name;
}
- toggle_item_details_section(item) {
+ async toggle_item_details_section(item) {
const current_item_changed = !this.compare_with_current_item(item);
// if item is null or highlighted cart item is clicked twice
const hide_item_details = !Boolean(item) || !current_item_changed;
+ if ((!hide_item_details && current_item_changed) || hide_item_details) {
+ // if item details is being closed OR if item details is opened but item is changed
+ // in both cases, if the current item is a serialized item, then validate and remove the item
+ await this.validate_serial_batch_item();
+ }
+
this.events.toggle_item_selector(!hide_item_details);
this.toggle_component(!hide_item_details);
@@ -83,7 +89,6 @@ erpnext.PointOfSale.ItemDetails = class {
this.render_form(item);
this.events.highlight_cart_item(item);
} else {
- this.validate_serial_batch_item();
this.current_item = {};
}
}
@@ -103,11 +108,11 @@ erpnext.PointOfSale.ItemDetails = class {
(serialized && batched && (no_batch_selected || no_serial_selected))) {
frappe.show_alert({
- message: __("Item will be removed since no serial / batch no selected."),
+ message: __("Item is removed since no serial / batch no selected."),
indicator: 'orange'
});
frappe.utils.play_sound("cancel");
- this.events.remove_item_from_cart();
+ return this.events.remove_item_from_cart();
}
}
diff --git a/erpnext/selling/page/point_of_sale/pos_item_selector.js b/erpnext/selling/page/point_of_sale/pos_item_selector.js
index 1177615aee9..b62b27bc4b3 100644
--- a/erpnext/selling/page/point_of_sale/pos_item_selector.js
+++ b/erpnext/selling/page/point_of_sale/pos_item_selector.js
@@ -243,7 +243,7 @@ erpnext.PointOfSale.ItemSelector = class {
value: "+1",
item: { item_code, batch_no, serial_no, uom, rate }
});
- me.set_search_value('');
+ me.search_field.set_focus();
});
this.search_field.$input.on('input', (e) => {
@@ -328,6 +328,7 @@ erpnext.PointOfSale.ItemSelector = class {
add_filtered_item_to_cart() {
this.$items_container.find(".item-wrapper").click();
+ this.set_search_value('');
}
resize_selector(minimize) {
diff --git a/erpnext/selling/page/point_of_sale/pos_payment.js b/erpnext/selling/page/point_of_sale/pos_payment.js
index 1e9f6d7d920..b4ece46e6e1 100644
--- a/erpnext/selling/page/point_of_sale/pos_payment.js
+++ b/erpnext/selling/page/point_of_sale/pos_payment.js
@@ -170,20 +170,24 @@ erpnext.PointOfSale.Payment = class {
});
frappe.ui.form.on('POS Invoice', 'coupon_code', (frm) => {
- if (!frm.doc.ignore_pricing_rule && frm.doc.coupon_code) {
- frappe.run_serially([
- () => frm.doc.ignore_pricing_rule=1,
- () => frm.trigger('ignore_pricing_rule'),
- () => frm.doc.ignore_pricing_rule=0,
- () => frm.trigger('apply_pricing_rule'),
- () => frm.save(),
- () => this.update_totals_section(frm.doc)
- ]);
- } else if (frm.doc.ignore_pricing_rule && frm.doc.coupon_code) {
- frappe.show_alert({
- message: __("Ignore Pricing Rule is enabled. Cannot apply coupon code."),
- indicator: "orange"
- });
+ if (frm.doc.coupon_code && !frm.applying_pos_coupon_code) {
+ if (!frm.doc.ignore_pricing_rule) {
+ frm.applying_pos_coupon_code = true;
+ frappe.run_serially([
+ () => frm.doc.ignore_pricing_rule=1,
+ () => frm.trigger('ignore_pricing_rule'),
+ () => frm.doc.ignore_pricing_rule=0,
+ () => frm.trigger('apply_pricing_rule'),
+ () => frm.save(),
+ () => this.update_totals_section(frm.doc),
+ () => (frm.applying_pos_coupon_code = false)
+ ]);
+ } else if (frm.doc.ignore_pricing_rule) {
+ frappe.show_alert({
+ message: __("Ignore Pricing Rule is enabled. Cannot apply coupon code."),
+ indicator: "orange"
+ });
+ }
}
});
diff --git a/erpnext/setup/doctype/company/test_records.json b/erpnext/setup/doctype/company/test_records.json
index 89be607d047..19b6ef27ac3 100644
--- a/erpnext/setup/doctype/company/test_records.json
+++ b/erpnext/setup/doctype/company/test_records.json
@@ -8,7 +8,8 @@
"domain": "Manufacturing",
"chart_of_accounts": "Standard",
"default_holiday_list": "_Test Holiday List",
- "enable_perpetual_inventory": 0
+ "enable_perpetual_inventory": 0,
+ "allow_account_creation_against_child_company": 1
},
{
"abbr": "_TC1",
diff --git a/erpnext/stock/doctype/delivery_note/delivery_note.py b/erpnext/stock/doctype/delivery_note/delivery_note.py
index 304857dd0f8..7205758a8e4 100644
--- a/erpnext/stock/doctype/delivery_note/delivery_note.py
+++ b/erpnext/stock/doctype/delivery_note/delivery_note.py
@@ -280,8 +280,11 @@ class DeliveryNote(SellingController):
)
if bypass_credit_limit_check_at_sales_order:
- validate_against_credit_limit = True
- extra_amount = self.base_grand_total
+ for d in self.get("items"):
+ if not d.against_sales_invoice:
+ validate_against_credit_limit = True
+ extra_amount = self.base_grand_total
+ break
else:
for d in self.get("items"):
if not (d.against_sales_order or d.against_sales_invoice):
diff --git a/erpnext/stock/doctype/delivery_note_item/delivery_note_item.json b/erpnext/stock/doctype/delivery_note_item/delivery_note_item.json
index f1f5d96e628..e2eb2a4bbb2 100644
--- a/erpnext/stock/doctype/delivery_note_item/delivery_note_item.json
+++ b/erpnext/stock/doctype/delivery_note_item/delivery_note_item.json
@@ -74,6 +74,7 @@
"against_sales_invoice",
"si_detail",
"dn_detail",
+ "pick_list_item",
"section_break_40",
"batch_no",
"serial_no",
@@ -762,13 +763,22 @@
"fieldtype": "Check",
"label": "Grant Commission",
"read_only": 1
+ },
+ {
+ "fieldname": "pick_list_item",
+ "fieldtype": "Data",
+ "hidden": 1,
+ "label": "Pick List Item",
+ "no_copy": 1,
+ "print_hide": 1,
+ "read_only": 1
}
],
"idx": 1,
"index_web_pages_for_search": 1,
"istable": 1,
"links": [],
- "modified": "2022-02-24 14:42:20.211085",
+ "modified": "2022-03-31 18:36:24.671913",
"modified_by": "Administrator",
"module": "Stock",
"name": "Delivery Note Item",
diff --git a/erpnext/stock/doctype/item/item.js b/erpnext/stock/doctype/item/item.js
index 2a2eafbb391..8206e095797 100644
--- a/erpnext/stock/doctype/item/item.js
+++ b/erpnext/stock/doctype/item/item.js
@@ -55,10 +55,15 @@ frappe.ui.form.on("Item", {
if (frm.doc.has_variants) {
frm.set_intro(__("This Item is a Template and cannot be used in transactions. Item attributes will be copied over into the variants unless 'No Copy' is set"), true);
+
frm.add_custom_button(__("Show Variants"), function() {
frappe.set_route("List", "Item", {"variant_of": frm.doc.name});
}, __("View"));
+ frm.add_custom_button(__("Item Variant Settings"), function() {
+ frappe.set_route("Form", "Item Variant Settings");
+ }, __("View"));
+
frm.add_custom_button(__("Variant Details Report"), function() {
frappe.set_route("query-report", "Item Variant Details", {"item": frm.doc.name});
}, __("View"));
@@ -110,6 +115,13 @@ frappe.ui.form.on("Item", {
}
});
}, __('Actions'));
+ } else {
+ frm.add_custom_button(__("Website Item"), function() {
+ frappe.db.get_value("Website Item", {item_code: frm.doc.name}, "name", (d) => {
+ if (!d.name) frappe.throw(__("Website Item not found"));
+ frappe.set_route("Form", "Website Item", d.name);
+ });
+ }, __("View"));
}
erpnext.item.edit_prices_button(frm);
@@ -131,12 +143,6 @@ frappe.ui.form.on("Item", {
frappe.set_route('Form', 'Item', new_item.name);
});
- if(frm.doc.has_variants) {
- frm.add_custom_button(__("Item Variant Settings"), function() {
- frappe.set_route("Form", "Item Variant Settings");
- }, __("View"));
- }
-
const stock_exists = (frm.doc.__onload
&& frm.doc.__onload.stock_exists) ? 1 : 0;
diff --git a/erpnext/stock/doctype/item/item_dashboard.py b/erpnext/stock/doctype/item/item_dashboard.py
index 33acf4bfd8a..3caed02d69b 100644
--- a/erpnext/stock/doctype/item/item_dashboard.py
+++ b/erpnext/stock/doctype/item/item_dashboard.py
@@ -32,5 +32,6 @@ def get_data():
{"label": _("Manufacture"), "items": ["Production Plan", "Work Order", "Item Manufacturer"]},
{"label": _("Traceability"), "items": ["Serial No", "Batch"]},
{"label": _("Move"), "items": ["Stock Entry"]},
+ {"label": _("E-commerce"), "items": ["Website Item"]},
],
}
diff --git a/erpnext/stock/doctype/item_barcode/item_barcode.json b/erpnext/stock/doctype/item_barcode/item_barcode.json
index d89ca55a4f3..eef70c95d05 100644
--- a/erpnext/stock/doctype/item_barcode/item_barcode.json
+++ b/erpnext/stock/doctype/item_barcode/item_barcode.json
@@ -1,109 +1,42 @@
{
- "allow_copy": 0,
- "allow_events_in_timeline": 0,
- "allow_guest_to_view": 0,
- "allow_import": 0,
- "allow_rename": 0,
- "autoname": "field:barcode",
- "beta": 0,
- "creation": "2017-12-09 18:54:50.562438",
- "custom": 0,
- "docstatus": 0,
- "doctype": "DocType",
- "document_type": "",
- "editable_grid": 1,
- "engine": "InnoDB",
+ "actions": [],
+ "autoname": "hash",
+ "creation": "2022-02-11 11:26:22.155183",
+ "doctype": "DocType",
+ "engine": "InnoDB",
+ "field_order": [
+ "barcode",
+ "barcode_type"
+ ],
"fields": [
{
- "allow_bulk_edit": 0,
- "allow_in_quick_entry": 0,
- "allow_on_submit": 0,
- "bold": 0,
- "collapsible": 0,
- "columns": 0,
- "fieldname": "barcode",
- "fieldtype": "Data",
- "hidden": 0,
- "ignore_user_permissions": 0,
- "ignore_xss_filter": 0,
- "in_filter": 0,
- "in_global_search": 1,
- "in_list_view": 1,
- "in_standard_filter": 0,
- "label": "Barcode",
- "length": 0,
- "no_copy": 1,
- "permlevel": 0,
- "precision": "",
- "print_hide": 0,
- "print_hide_if_no_value": 0,
- "read_only": 0,
- "remember_last_selected_value": 0,
- "report_hide": 0,
- "reqd": 0,
- "search_index": 0,
- "set_only_once": 0,
- "translatable": 0,
+ "fieldname": "barcode",
+ "fieldtype": "Data",
+ "in_global_search": 1,
+ "in_list_view": 1,
+ "label": "Barcode",
+ "no_copy": 1,
"unique": 1
- },
+ },
{
- "allow_bulk_edit": 0,
- "allow_in_quick_entry": 0,
- "allow_on_submit": 0,
- "bold": 0,
- "collapsible": 0,
- "columns": 0,
- "fieldname": "barcode_type",
- "fieldtype": "Select",
- "hidden": 0,
- "ignore_user_permissions": 0,
- "ignore_xss_filter": 0,
- "in_filter": 0,
- "in_global_search": 0,
- "in_list_view": 1,
- "in_standard_filter": 0,
- "label": "Barcode Type",
- "length": 0,
- "no_copy": 0,
- "options": "\nEAN\nUPC-A",
- "permlevel": 0,
- "precision": "",
- "print_hide": 0,
- "print_hide_if_no_value": 0,
- "read_only": 0,
- "remember_last_selected_value": 0,
- "report_hide": 0,
- "reqd": 0,
- "search_index": 0,
- "set_only_once": 0,
- "translatable": 0,
- "unique": 0
+ "fieldname": "barcode_type",
+ "fieldtype": "Select",
+ "in_list_view": 1,
+ "label": "Barcode Type",
+ "options": "\nEAN\nUPC-A"
}
- ],
- "has_web_view": 0,
- "hide_heading": 0,
- "hide_toolbar": 0,
- "idx": 0,
- "image_view": 0,
- "in_create": 0,
- "is_submittable": 0,
- "issingle": 0,
- "istable": 1,
- "max_attachments": 0,
- "modified": "2018-11-13 06:03:09.814357",
- "modified_by": "Administrator",
- "module": "Stock",
- "name": "Item Barcode",
- "name_case": "",
- "owner": "Administrator",
- "permissions": [],
- "quick_entry": 1,
- "read_only": 0,
- "read_only_onload": 0,
- "show_name_in_global_search": 0,
- "sort_field": "modified",
- "sort_order": "DESC",
- "track_changes": 1,
- "track_seen": 0,
- "track_views": 0
+ ],
+ "istable": 1,
+ "links": [],
+ "modified": "2022-04-01 05:54:27.314030",
+ "modified_by": "Administrator",
+ "module": "Stock",
+ "name": "Item Barcode",
+ "naming_rule": "Random",
+ "owner": "Administrator",
+ "permissions": [],
+ "sort_field": "modified",
+ "sort_order": "DESC",
+ "states": [],
+ "track_changes": 1
}
\ No newline at end of file
diff --git a/erpnext/stock/doctype/pick_list/pick_list.py b/erpnext/stock/doctype/pick_list/pick_list.py
index 7061ee1eea4..d3476a88f05 100644
--- a/erpnext/stock/doctype/pick_list/pick_list.py
+++ b/erpnext/stock/doctype/pick_list/pick_list.py
@@ -534,6 +534,7 @@ def map_pl_locations(pick_list, item_mapper, delivery_note, sales_order=None):
dn_item = map_child_doc(source_doc, delivery_note, table_mapper)
if dn_item:
+ dn_item.pick_list_item = location.name
dn_item.warehouse = location.warehouse
dn_item.qty = flt(location.picked_qty) / (flt(location.conversion_factor) or 1)
dn_item.batch_no = location.batch_no
diff --git a/erpnext/stock/doctype/pick_list/test_pick_list.py b/erpnext/stock/doctype/pick_list/test_pick_list.py
index 7496b6b1798..ec5011b93d6 100644
--- a/erpnext/stock/doctype/pick_list/test_pick_list.py
+++ b/erpnext/stock/doctype/pick_list/test_pick_list.py
@@ -521,6 +521,8 @@ class TestPickList(FrappeTestCase):
for dn_item in frappe.get_doc("Delivery Note", dn.name).get("items"):
self.assertEqual(dn_item.item_code, "_Test Item")
self.assertEqual(dn_item.against_sales_order, sales_order_1.name)
+ self.assertEqual(dn_item.pick_list_item, pick_list.locations[dn_item.idx - 1].name)
+
for dn in frappe.get_all(
"Delivery Note",
filters={"pick_list": pick_list.name, "customer": "_Test Customer 1"},
diff --git a/erpnext/stock/doctype/purchase_receipt/test_purchase_receipt.py b/erpnext/stock/doctype/purchase_receipt/test_purchase_receipt.py
index 0da89937ed0..65c30de0978 100644
--- a/erpnext/stock/doctype/purchase_receipt/test_purchase_receipt.py
+++ b/erpnext/stock/doctype/purchase_receipt/test_purchase_receipt.py
@@ -663,6 +663,45 @@ class TestPurchaseReceipt(FrappeTestCase):
return_pr.cancel()
pr.cancel()
+ def test_purchase_receipt_for_rejected_gle_without_accepted_warehouse(self):
+ from erpnext.stock.doctype.warehouse.test_warehouse import get_warehouse
+
+ rejected_warehouse = "_Test Rejected Warehouse - TCP1"
+ if not frappe.db.exists("Warehouse", rejected_warehouse):
+ get_warehouse(
+ company="_Test Company with perpetual inventory",
+ abbr=" - TCP1",
+ warehouse_name="_Test Rejected Warehouse",
+ ).name
+
+ pr = make_purchase_receipt(
+ company="_Test Company with perpetual inventory",
+ warehouse="Stores - TCP1",
+ received_qty=2,
+ rejected_qty=2,
+ rejected_warehouse=rejected_warehouse,
+ do_not_save=True,
+ )
+
+ pr.items[0].qty = 0.0
+ pr.items[0].warehouse = ""
+ pr.submit()
+
+ actual_qty = frappe.db.get_value(
+ "Stock Ledger Entry",
+ {
+ "voucher_type": "Purchase Receipt",
+ "voucher_no": pr.name,
+ "warehouse": pr.items[0].rejected_warehouse,
+ "is_cancelled": 0,
+ },
+ "actual_qty",
+ )
+
+ self.assertEqual(actual_qty, 2)
+ self.assertFalse(pr.items[0].warehouse)
+ pr.cancel()
+
def test_purchase_return_for_serialized_items(self):
def _check_serial_no_values(serial_no, field_values):
serial_no = frappe.get_doc("Serial No", serial_no)
diff --git a/erpnext/stock/doctype/serial_no/serial_no.py b/erpnext/stock/doctype/serial_no/serial_no.py
index bc30878789f..6d3969892f2 100644
--- a/erpnext/stock/doctype/serial_no/serial_no.py
+++ b/erpnext/stock/doctype/serial_no/serial_no.py
@@ -820,29 +820,60 @@ def auto_fetch_serial_number(
return sorted([d.get("name") for d in serial_numbers])
+def get_delivered_serial_nos(serial_nos):
+ """
+ Returns serial numbers that delivered from the list of serial numbers
+ """
+ from frappe.query_builder.functions import Coalesce
+
+ SerialNo = frappe.qb.DocType("Serial No")
+ serial_nos = get_serial_nos(serial_nos)
+ query = (
+ frappe.qb.select(SerialNo.name)
+ .from_(SerialNo)
+ .where((SerialNo.name.isin(serial_nos)) & (Coalesce(SerialNo.delivery_document_type, "") != ""))
+ )
+
+ result = query.run()
+ if result and len(result) > 0:
+ delivered_serial_nos = [row[0] for row in result]
+ return delivered_serial_nos
+
+
@frappe.whitelist()
def get_pos_reserved_serial_nos(filters):
if isinstance(filters, str):
filters = json.loads(filters)
- pos_transacted_sr_nos = frappe.db.sql(
- """select item.serial_no as serial_no
- from `tabPOS Invoice` p, `tabPOS Invoice Item` item
- where p.name = item.parent
- and p.consolidated_invoice is NULL
- and p.docstatus = 1
- and item.docstatus = 1
- and item.item_code = %(item_code)s
- and item.warehouse = %(warehouse)s
- and item.serial_no is NOT NULL and item.serial_no != ''
- """,
- filters,
- as_dict=1,
+ POSInvoice = frappe.qb.DocType("POS Invoice")
+ POSInvoiceItem = frappe.qb.DocType("POS Invoice Item")
+ query = (
+ frappe.qb.from_(POSInvoice)
+ .from_(POSInvoiceItem)
+ .select(POSInvoice.is_return, POSInvoiceItem.serial_no)
+ .where(
+ (POSInvoice.name == POSInvoiceItem.parent)
+ & (POSInvoice.docstatus == 1)
+ & (POSInvoiceItem.docstatus == 1)
+ & (POSInvoiceItem.item_code == filters.get("item_code"))
+ & (POSInvoiceItem.warehouse == filters.get("warehouse"))
+ & (POSInvoiceItem.serial_no.isnotnull())
+ & (POSInvoiceItem.serial_no != "")
+ )
)
+ pos_transacted_sr_nos = query.run(as_dict=True)
+
reserved_sr_nos = []
+ returned_sr_nos = []
for d in pos_transacted_sr_nos:
- reserved_sr_nos += get_serial_nos(d.serial_no)
+ if d.is_return == 0:
+ reserved_sr_nos += get_serial_nos(d.serial_no)
+ elif d.is_return == 1:
+ returned_sr_nos += get_serial_nos(d.serial_no)
+
+ for sr_no in returned_sr_nos:
+ reserved_sr_nos.remove(sr_no)
return reserved_sr_nos
diff --git a/erpnext/stock/doctype/stock_entry/stock_entry.js b/erpnext/stock/doctype/stock_entry/stock_entry.js
index 61466cff032..4ec9f1f220f 100644
--- a/erpnext/stock/doctype/stock_entry/stock_entry.js
+++ b/erpnext/stock/doctype/stock_entry/stock_entry.js
@@ -631,7 +631,7 @@ frappe.ui.form.on('Stock Entry Detail', {
// set allow_zero_valuation_rate to 0 if s_warehouse is selected.
let item = frappe.get_doc(cdt, cdn);
if (item.s_warehouse) {
- item.allow_zero_valuation_rate = 0;
+ frappe.model.set_value(cdt, cdn, "allow_zero_valuation_rate", 0);
}
},
diff --git a/erpnext/stock/doctype/stock_entry/stock_entry.py b/erpnext/stock/doctype/stock_entry/stock_entry.py
index bafb138b31d..cc7317a2cd8 100644
--- a/erpnext/stock/doctype/stock_entry/stock_entry.py
+++ b/erpnext/stock/doctype/stock_entry/stock_entry.py
@@ -1383,8 +1383,8 @@ class StockEntry(StockController):
def set_scrap_items(self):
if self.purpose != "Send to Subcontractor" and self.purpose in ["Manufacture", "Repack"]:
scrap_item_dict = self.get_bom_scrap_material(self.fg_completed_qty)
- for item in itervalues(scrap_item_dict):
- item.idx = ""
+
+ for item in scrap_item_dict.values():
if self.pro_doc and self.pro_doc.scrap_warehouse:
item["to_warehouse"] = self.pro_doc.scrap_warehouse
@@ -1900,7 +1900,6 @@ class StockEntry(StockController):
se_child.is_process_loss = item_row.get("is_process_loss", 0)
for field in [
- "idx",
"po_detail",
"original_item",
"expense_account",
diff --git a/erpnext/stock/stock_ledger.py b/erpnext/stock/stock_ledger.py
index 0664a8352b9..b95bcab7149 100644
--- a/erpnext/stock/stock_ledger.py
+++ b/erpnext/stock/stock_ledger.py
@@ -866,16 +866,9 @@ class update_entries_after(object):
index = i
break
- # If no entry found with outgoing rate, collapse stack
+ # If no entry found with outgoing rate, consume as per FIFO
if index is None: # nosemgrep
- new_stock_value = (
- sum((d[0] * d[1] for d in self.wh_data.stock_queue)) - qty_to_pop * outgoing_rate
- )
- new_stock_qty = sum((d[0] for d in self.wh_data.stock_queue)) - qty_to_pop
- self.wh_data.stock_queue = [
- [new_stock_qty, new_stock_value / new_stock_qty if new_stock_qty > 0 else outgoing_rate]
- ]
- break
+ index = 0
else:
index = 0
diff --git a/erpnext/templates/pages/product_search.py b/erpnext/templates/pages/product_search.py
index e5e00ef5a1e..7d473f37b8e 100644
--- a/erpnext/templates/pages/product_search.py
+++ b/erpnext/templates/pages/product_search.py
@@ -1,6 +1,8 @@
# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
# License: GNU General Public License v3. See license.txt
+import json
+
import frappe
from frappe.utils import cint, cstr
from redisearch import AutoCompleter, Client, Query
@@ -9,7 +11,7 @@ from erpnext.e_commerce.redisearch_utils import (
WEBSITE_ITEM_CATEGORY_AUTOCOMPLETE,
WEBSITE_ITEM_INDEX,
WEBSITE_ITEM_NAME_AUTOCOMPLETE,
- is_search_module_loaded,
+ is_redisearch_enabled,
make_key,
)
from erpnext.e_commerce.shopping_cart.product_info import set_product_info_for_website
@@ -74,8 +76,8 @@ def search(query):
def product_search(query, limit=10, fuzzy_search=True):
search_results = {"from_redisearch": True, "results": []}
- if not is_search_module_loaded():
- # Redisearch module not loaded
+ if not is_redisearch_enabled():
+ # Redisearch module not enabled
search_results["from_redisearch"] = False
search_results["results"] = get_product_data(query, 0, limit)
return search_results
@@ -86,6 +88,8 @@ def product_search(query, limit=10, fuzzy_search=True):
red = frappe.cache()
query = clean_up_query(query)
+ # TODO: Check perf/correctness with Suggestions & Query vs only Query
+ # TODO: Use Levenshtein Distance in Query (max=3)
ac = AutoCompleter(make_key(WEBSITE_ITEM_NAME_AUTOCOMPLETE), conn=red)
client = Client(make_key(WEBSITE_ITEM_INDEX), conn=red)
suggestions = ac.get_suggestions(
@@ -121,8 +125,8 @@ def convert_to_dict(redis_search_doc):
def get_category_suggestions(query):
search_results = {"results": []}
- if not is_search_module_loaded():
- # Redisearch module not loaded, query db
+ if not is_redisearch_enabled():
+ # Redisearch module not enabled, query db
categories = frappe.db.get_all(
"Item Group",
filters={"name": ["like", "%{0}%".format(query)], "show_in_website": 1},
@@ -135,8 +139,10 @@ def get_category_suggestions(query):
return search_results
ac = AutoCompleter(make_key(WEBSITE_ITEM_CATEGORY_AUTOCOMPLETE), conn=frappe.cache())
- suggestions = ac.get_suggestions(query, num=10)
+ suggestions = ac.get_suggestions(query, num=10, with_payloads=True)
- search_results["results"] = [s.string for s in suggestions]
+ results = [json.loads(s.payload) for s in suggestions]
+
+ search_results["results"] = results
return search_results