Merge pull request #30592 from frappe/version-13-hotfix

chore: Pre-release for v13.25.0
This commit is contained in:
Deepesh Garg
2022-04-06 15:06:22 +05:30
committed by GitHub
78 changed files with 1708 additions and 542 deletions

View File

@@ -29,6 +29,8 @@ ignore =
B950,
W191,
E124, # closing bracket, irritating while writing QB code
E131, # continuation line unaligned for hanging indent
E123, # closing bracket does not match indentation of opening bracket's line
max-line-length = 200
exclude=.github/helper/semgrep_rules

View File

@@ -204,7 +204,9 @@ class Account(NestedSet):
if not self.account_currency:
self.account_currency = frappe.get_cached_value("Company", self.company, "default_currency")
elif self.account_currency != frappe.db.get_value("Account", self.name, "account_currency"):
gl_currency = frappe.db.get_value("GL Entry", {"account": self.name}, "account_currency")
if gl_currency and self.account_currency != gl_currency:
if frappe.db.get_value("GL Entry", {"account": self.name}):
frappe.throw(_("Currency can not be changed after making entries using some other currency"))

View File

@@ -241,6 +241,28 @@ class TestAccount(unittest.TestCase):
for doc in to_delete:
frappe.delete_doc("Account", doc)
def test_validate_account_currency(self):
from erpnext.accounts.doctype.journal_entry.test_journal_entry import make_journal_entry
if not frappe.db.get_value("Account", "Test Currency Account - _TC"):
acc = frappe.new_doc("Account")
acc.account_name = "Test Currency Account"
acc.parent_account = "Tax Assets - _TC"
acc.company = "_Test Company"
acc.insert()
else:
acc = frappe.get_doc("Account", "Test Currency Account - _TC")
self.assertEqual(acc.account_currency, "INR")
# Make a JV against this account
make_journal_entry(
"Test Currency Account - _TC", "Miscellaneous Expenses - _TC", 100, submit=True
)
acc.account_currency = "USD"
self.assertRaises(frappe.ValidationError, acc.save)
def _make_test_records(verbose=None):
from frappe.test_runner import make_test_objects

View File

@@ -3,6 +3,7 @@
"creation": "2014-08-29 16:02:39.740505",
"doctype": "DocType",
"editable_grid": 1,
"engine": "InnoDB",
"field_order": [
"company",
"account"
@@ -11,6 +12,7 @@
{
"fieldname": "company",
"fieldtype": "Link",
"ignore_user_permissions": 1,
"in_list_view": 1,
"label": "Company",
"options": "Company",
@@ -27,7 +29,7 @@
"index_web_pages_for_search": 1,
"istable": 1,
"links": [],
"modified": "2021-04-07 18:13:08.833822",
"modified": "2022-04-04 12:31:02.994197",
"modified_by": "Administrator",
"module": "Accounts",
"name": "Party Account",
@@ -35,5 +37,6 @@
"permissions": [],
"quick_entry": 1,
"sort_field": "modified",
"sort_order": "DESC"
"sort_order": "DESC",
"states": []
}

View File

@@ -12,7 +12,6 @@ frappe.ui.form.on('Payment Order', {
});
frm.set_df_property('references', 'cannot_add_rows', true);
frm.set_df_property('references', 'cannot_delete_rows', true);
},
refresh: function(frm) {
if (frm.doc.docstatus == 0) {

View File

@@ -264,7 +264,6 @@
"print_hide": 1
},
{
"allow_on_submit": 1,
"default": "0",
"fieldname": "is_return",
"fieldtype": "Check",
@@ -1573,7 +1572,7 @@
"icon": "fa fa-file-text",
"is_submittable": 1,
"links": [],
"modified": "2021-10-05 12:11:53.871828",
"modified": "2022-03-22 13:00:24.166684",
"modified_by": "Administrator",
"module": "Accounts",
"name": "POS Invoice",
@@ -1623,6 +1622,7 @@
"show_name_in_global_search": 1,
"sort_field": "modified",
"sort_order": "DESC",
"states": [],
"timeline_field": "customer",
"title_field": "title",
"track_changes": 1,

View File

@@ -17,7 +17,11 @@ from erpnext.accounts.doctype.sales_invoice.sales_invoice import (
)
from erpnext.accounts.party import get_due_date, get_party_account
from erpnext.stock.doctype.batch.batch import get_batch_qty, get_pos_reserved_batch_qty
from erpnext.stock.doctype.serial_no.serial_no import get_pos_reserved_serial_nos, get_serial_nos
from erpnext.stock.doctype.serial_no.serial_no import (
get_delivered_serial_nos,
get_pos_reserved_serial_nos,
get_serial_nos,
)
class POSInvoice(SalesInvoice):
@@ -179,12 +183,7 @@ class POSInvoice(SalesInvoice):
)
def validate_delivered_serial_nos(self, item):
serial_nos = get_serial_nos(item.serial_no)
delivered_serial_nos = frappe.db.get_list(
"Serial No",
{"item_code": item.item_code, "name": ["in", serial_nos], "sales_invoice": ["is", "set"]},
pluck="name",
)
delivered_serial_nos = get_delivered_serial_nos(item.serial_no)
if delivered_serial_nos:
bold_delivered_serial_nos = frappe.bold(", ".join(delivered_serial_nos))
@@ -212,9 +211,16 @@ class POSInvoice(SalesInvoice):
frappe.throw(error_msg, title=_("Invalid Item"), as_list=True)
def validate_stock_availablility(self):
if self.is_return or self.docstatus != 1:
if self.is_return:
return
if self.docstatus == 0 and not frappe.db.get_value(
"POS Profile", self.pos_profile, "validate_stock_on_save"
):
return
allow_negative_stock = frappe.db.get_single_value("Stock Settings", "allow_negative_stock")
for d in self.get("items"):
is_service_item = not (frappe.db.get_value("Item", d.get("item_code"), "is_stock_item"))
if is_service_item:

View File

@@ -439,6 +439,7 @@ class TestPOSInvoice(unittest.TestCase):
)
si.get("items")[0].serial_no = serial_nos[0]
si.update_stock = 1
si.insert()
si.submit()
@@ -765,6 +766,76 @@ class TestPOSInvoice(unittest.TestCase):
pos_inv.delete()
pr.delete()
def test_delivered_serial_no_case(self):
from erpnext.accounts.doctype.pos_invoice_merge_log.test_pos_invoice_merge_log import (
init_user_and_profile,
)
from erpnext.stock.doctype.delivery_note.test_delivery_note import create_delivery_note
from erpnext.stock.doctype.serial_no.test_serial_no import get_serial_nos
from erpnext.stock.doctype.stock_entry.test_stock_entry import make_serialized_item
frappe.db.savepoint("before_test_delivered_serial_no_case")
try:
se = make_serialized_item()
serial_no = get_serial_nos(se.get("items")[0].serial_no)[0]
dn = create_delivery_note(item_code="_Test Serialized Item With Series", serial_no=serial_no)
delivery_document_no = frappe.db.get_value("Serial No", serial_no, "delivery_document_no")
self.assertEquals(delivery_document_no, dn.name)
init_user_and_profile()
pos_inv = create_pos_invoice(
item_code="_Test Serialized Item With Series",
serial_no=serial_no,
qty=1,
rate=100,
do_not_submit=True,
)
self.assertRaises(frappe.ValidationError, pos_inv.submit)
finally:
frappe.db.rollback(save_point="before_test_delivered_serial_no_case")
frappe.set_user("Administrator")
def test_returned_serial_no_case(self):
from erpnext.accounts.doctype.pos_invoice_merge_log.test_pos_invoice_merge_log import (
init_user_and_profile,
)
from erpnext.stock.doctype.serial_no.serial_no import get_pos_reserved_serial_nos
from erpnext.stock.doctype.serial_no.test_serial_no import get_serial_nos
from erpnext.stock.doctype.stock_entry.test_stock_entry import make_serialized_item
frappe.db.savepoint("before_test_returned_serial_no_case")
try:
se = make_serialized_item()
serial_no = get_serial_nos(se.get("items")[0].serial_no)[0]
init_user_and_profile()
pos_inv = create_pos_invoice(
item_code="_Test Serialized Item With Series",
serial_no=serial_no,
qty=1,
rate=100,
)
pos_return = make_sales_return(pos_inv.name)
pos_return.flags.ignore_validate = True
pos_return.insert()
pos_return.submit()
pos_reserved_serial_nos = get_pos_reserved_serial_nos(
{"item_code": "_Test Serialized Item With Series", "warehouse": "_Test Warehouse - _TC"}
)
self.assertTrue(serial_no not in pos_reserved_serial_nos)
finally:
frappe.db.rollback(save_point="before_test_returned_serial_no_case")
frappe.set_user("Administrator")
def create_pos_invoice(**args):
args = frappe._dict(args)

View File

@@ -5,7 +5,6 @@
import json
import frappe
import six
from frappe import _
from frappe.core.page.background_jobs.background_jobs import get_info
from frappe.model.document import Document
@@ -66,7 +65,9 @@ class POSInvoiceMergeLog(Document):
frappe.throw(msg)
def on_submit(self):
pos_invoice_docs = [frappe.get_doc("POS Invoice", d.pos_invoice) for d in self.pos_invoices]
pos_invoice_docs = [
frappe.get_cached_doc("POS Invoice", d.pos_invoice) for d in self.pos_invoices
]
returns = [d for d in pos_invoice_docs if d.get("is_return") == 1]
sales = [d for d in pos_invoice_docs if d.get("is_return") == 0]
@@ -83,7 +84,9 @@ class POSInvoiceMergeLog(Document):
self.update_pos_invoices(pos_invoice_docs, sales_invoice, credit_note)
def on_cancel(self):
pos_invoice_docs = [frappe.get_doc("POS Invoice", d.pos_invoice) for d in self.pos_invoices]
pos_invoice_docs = [
frappe.get_cached_doc("POS Invoice", d.pos_invoice) for d in self.pos_invoices
]
self.update_pos_invoices(pos_invoice_docs)
self.cancel_linked_invoices()
@@ -282,7 +285,14 @@ def get_all_unconsolidated_invoices():
pos_invoices = frappe.db.get_all(
"POS Invoice",
filters=filters,
fields=["name as pos_invoice", "posting_date", "grand_total", "customer"],
fields=[
"name as pos_invoice",
"posting_date",
"grand_total",
"customer",
"is_return",
"return_against",
],
)
return pos_invoices
@@ -327,19 +337,74 @@ def unconsolidate_pos_invoices(closing_entry):
cancel_merge_logs(merge_logs, closing_entry)
def split_invoices(invoices):
"""
Splits invoices into multiple groups
Use-case:
If a serial no is sold and later it is returned
then split the invoices such that the selling entry is merged first and then the return entry
"""
# Input
# invoices = [
# {'pos_invoice': 'Invoice with SR#1 & SR#2', 'is_return': 0},
# {'pos_invoice': 'Invoice with SR#1', 'is_return': 1},
# {'pos_invoice': 'Invoice with SR#2', 'is_return': 0}
# ]
# Output
# _invoices = [
# [{'pos_invoice': 'Invoice with SR#1 & SR#2', 'is_return': 0}],
# [{'pos_invoice': 'Invoice with SR#1', 'is_return': 1}, {'pos_invoice': 'Invoice with SR#2', 'is_return': 0}],
# ]
_invoices = []
special_invoices = []
pos_return_docs = [
frappe.get_cached_doc("POS Invoice", d.pos_invoice)
for d in invoices
if d.is_return and d.return_against
]
for pos_invoice in pos_return_docs:
for item in pos_invoice.items:
if not item.serial_no:
continue
return_against_is_added = any(
d for d in _invoices if d.pos_invoice == pos_invoice.return_against
)
if return_against_is_added:
break
return_against_is_consolidated = (
frappe.db.get_value("POS Invoice", pos_invoice.return_against, "status", cache=True)
== "Consolidated"
)
if return_against_is_consolidated:
break
pos_invoice_row = [d for d in invoices if d.pos_invoice == pos_invoice.return_against]
_invoices.append(pos_invoice_row)
special_invoices.append(pos_invoice.return_against)
break
_invoices.append([d for d in invoices if d.pos_invoice not in special_invoices])
return _invoices
def create_merge_logs(invoice_by_customer, closing_entry=None):
try:
for customer, invoices in six.iteritems(invoice_by_customer):
merge_log = frappe.new_doc("POS Invoice Merge Log")
merge_log.posting_date = (
getdate(closing_entry.get("posting_date")) if closing_entry else nowdate()
)
merge_log.customer = customer
merge_log.pos_closing_entry = closing_entry.get("name") if closing_entry else None
for customer, invoices in invoice_by_customer.items():
for _invoices in split_invoices(invoices):
merge_log = frappe.new_doc("POS Invoice Merge Log")
merge_log.posting_date = (
getdate(closing_entry.get("posting_date")) if closing_entry else nowdate()
)
merge_log.customer = customer
merge_log.pos_closing_entry = closing_entry.get("name") if closing_entry else None
merge_log.set("pos_invoices", invoices)
merge_log.save(ignore_permissions=True)
merge_log.submit()
merge_log.set("pos_invoices", _invoices)
merge_log.save(ignore_permissions=True)
merge_log.submit()
if closing_entry:
closing_entry.set_status(update=True, status="Submitted")

View File

@@ -391,3 +391,61 @@ class TestPOSInvoiceMergeLog(unittest.TestCase):
frappe.set_user("Administrator")
frappe.db.sql("delete from `tabPOS Profile`")
frappe.db.sql("delete from `tabPOS Invoice`")
def test_serial_no_case_1(self):
"""
Create a POS Invoice with serial no
Create a Return Invoice with serial no
Create a POS Invoice with serial no again
Consolidate the invoices
The first POS Invoice should be consolidated with a separate single Merge Log
The second and third POS Invoice should be consolidated with a single Merge Log
"""
from erpnext.stock.doctype.serial_no.test_serial_no import get_serial_nos
from erpnext.stock.doctype.stock_entry.test_stock_entry import make_serialized_item
frappe.db.sql("delete from `tabPOS Invoice`")
try:
se = make_serialized_item()
serial_no = get_serial_nos(se.get("items")[0].serial_no)[0]
init_user_and_profile()
pos_inv = create_pos_invoice(
item_code="_Test Serialized Item With Series",
serial_no=serial_no,
qty=1,
rate=100,
do_not_submit=1,
)
pos_inv.append("payments", {"mode_of_payment": "Cash", "account": "Cash - _TC", "amount": 100})
pos_inv.submit()
pos_inv_cn = make_sales_return(pos_inv.name)
pos_inv_cn.paid_amount = -100
pos_inv_cn.submit()
pos_inv2 = create_pos_invoice(
item_code="_Test Serialized Item With Series",
serial_no=serial_no,
qty=1,
rate=100,
do_not_submit=1,
)
pos_inv2.append("payments", {"mode_of_payment": "Cash", "account": "Cash - _TC", "amount": 100})
pos_inv2.submit()
consolidate_pos_invoices()
pos_inv.load_from_db()
pos_inv2.load_from_db()
self.assertNotEqual(pos_inv.consolidated_invoice, pos_inv2.consolidated_invoice)
finally:
frappe.set_user("Administrator")
frappe.db.sql("delete from `tabPOS Profile`")
frappe.db.sql("delete from `tabPOS Invoice`")

View File

@@ -9,7 +9,9 @@
"posting_date",
"column_break_3",
"customer",
"grand_total"
"grand_total",
"is_return",
"return_against"
],
"fields": [
{
@@ -48,11 +50,27 @@
"in_list_view": 1,
"label": "Amount",
"reqd": 1
},
{
"default": "0",
"fetch_from": "pos_invoice.is_return",
"fieldname": "is_return",
"fieldtype": "Check",
"label": "Is Return",
"read_only": 1
},
{
"fetch_from": "pos_invoice.return_against",
"fieldname": "return_against",
"fieldtype": "Link",
"label": "Return Against",
"options": "POS Invoice",
"read_only": 1
}
],
"istable": 1,
"links": [],
"modified": "2020-05-29 15:08:42.194979",
"modified": "2022-03-24 13:32:02.366257",
"modified_by": "Administrator",
"module": "Accounts",
"name": "POS Invoice Reference",
@@ -61,5 +79,6 @@
"quick_entry": 1,
"sort_field": "modified",
"sort_order": "DESC",
"states": [],
"track_changes": 1
}

View File

@@ -22,6 +22,7 @@
"hide_images",
"hide_unavailable_items",
"auto_add_item_to_cart",
"validate_stock_on_save",
"column_break_16",
"update_stock",
"ignore_pricing_rule",
@@ -351,6 +352,12 @@
{
"fieldname": "column_break_25",
"fieldtype": "Column Break"
},
{
"default": "0",
"fieldname": "validate_stock_on_save",
"fieldtype": "Check",
"label": "Validate Stock on Save"
}
],
"icon": "icon-cog",
@@ -378,10 +385,11 @@
"link_fieldname": "pos_profile"
}
],
"modified": "2021-10-14 14:17:00.469298",
"modified": "2022-03-21 13:29:28.480533",
"modified_by": "Administrator",
"module": "Accounts",
"name": "POS Profile",
"naming_rule": "Set by user",
"owner": "Administrator",
"permissions": [
{
@@ -404,5 +412,6 @@
}
],
"sort_field": "modified",
"sort_order": "DESC"
"sort_order": "DESC",
"states": []
}

View File

@@ -276,6 +276,8 @@ erpnext.accounts.PurchaseInvoice = erpnext.buying.BuyingController.extend({
if(this.frm.updating_party_details || this.frm.doc.inter_company_invoice_reference)
return;
if (this.frm.doc.__onload && this.frm.doc.__onload.load_after_mapping) return;
erpnext.utils.get_party_details(this.frm, "erpnext.accounts.party.get_party_details",
{
posting_date: this.frm.doc.posting_date,

View File

@@ -245,8 +245,9 @@ class PurchaseInvoice(BuyingController):
def validate_warehouse(self, for_validate=True):
if self.update_stock and for_validate:
stock_items = self.get_stock_items()
for d in self.get("items"):
if not d.warehouse:
if not d.warehouse and d.item_code in stock_items:
frappe.throw(
_(
"Warehouse required at Row No {0}, please set default warehouse for the item {1} for the company {2}"

View File

@@ -281,6 +281,9 @@ erpnext.accounts.SalesInvoiceController = erpnext.selling.SellingController.exte
}
var me = this;
if(this.frm.updating_party_details) return;
if (this.frm.doc.__onload && this.frm.doc.__onload.load_after_mapping) return;
erpnext.utils.get_party_details(this.frm,
"erpnext.accounts.party.get_party_details", {
posting_date: this.frm.doc.posting_date,

View File

@@ -25,7 +25,9 @@ class TaxWithholdingCategory(Document):
def validate_thresholds(self):
for d in self.get("rates"):
if d.cumulative_threshold and d.cumulative_threshold < d.single_threshold:
if (
d.cumulative_threshold and d.single_threshold and d.cumulative_threshold < d.single_threshold
):
frappe.throw(
_("Row #{0}: Cumulative threshold cannot be less than Single Transaction threshold").format(
d.idx

View File

@@ -1,7 +1,8 @@
{%- from "templates/print_formats/standard_macros.html" import add_header, render_field, print_value -%}
{%- set einvoice = json.loads(doc.signed_einvoice) -%}
<div class="page-break">
{% if doc.signed_einvoice %}
{%- set einvoice = json.loads(doc.signed_einvoice) -%}
<div {% if print_settings.repeat_header_footer %} id="header-html" class="hidden-pdf" {% endif %}>
{% if letter_head and not no_letterhead %}
<div class="letter-head">{{ letter_head }}</div>
@@ -170,4 +171,10 @@
</tbody>
</table>
</div>
{% else %}
<div class="text-center" style="color: var(--gray-500); font-size: 14px;">
You must generate IRN before you can preview GST E-Invoice.
</div>
{% endif %}
</div>

View File

@@ -68,7 +68,7 @@ class TestAsset(AssetSetup):
def test_item_exists(self):
asset = create_asset(item_code="MacBook", do_not_save=1)
self.assertRaises(frappe.DoesNotExistError, asset.save)
self.assertRaises(frappe.ValidationError, asset.save)
def test_validate_item(self):
asset = create_asset(item_code="MacBook Pro", do_not_save=1)

View File

@@ -3,19 +3,19 @@
import json
from typing import Dict
import frappe
from frappe import _
from frappe.utils import cint, cstr, flt
from frappe.utils import cint, cstr, flt, getdate
from erpnext.stock.doctype.item.item import get_last_purchase_details, validate_end_of_life
def update_last_purchase_rate(doc, is_submit):
def update_last_purchase_rate(doc, is_submit) -> None:
"""updates last_purchase_rate in item table for each item"""
import frappe.utils
this_purchase_date = frappe.utils.getdate(doc.get("posting_date") or doc.get("transaction_date"))
this_purchase_date = getdate(doc.get("posting_date") or doc.get("transaction_date"))
for d in doc.get("items"):
# get last purchase details
@@ -41,7 +41,7 @@ def update_last_purchase_rate(doc, is_submit):
frappe.db.set_value("Item", d.item_code, "last_purchase_rate", flt(last_purchase_rate))
def validate_for_items(doc):
def validate_for_items(doc) -> None:
items = []
for d in doc.get("items"):
if not d.qty:
@@ -49,40 +49,11 @@ def validate_for_items(doc):
continue
frappe.throw(_("Please enter quantity for Item {0}").format(d.item_code))
# update with latest quantities
bin = frappe.db.sql(
"""select projected_qty from `tabBin` where
item_code = %s and warehouse = %s""",
(d.item_code, d.warehouse),
as_dict=1,
)
f_lst = {
"projected_qty": bin and flt(bin[0]["projected_qty"]) or 0,
"ordered_qty": 0,
"received_qty": 0,
}
if d.doctype in ("Purchase Receipt Item", "Purchase Invoice Item"):
f_lst.pop("received_qty")
for x in f_lst:
if d.meta.get_field(x):
d.set(x, f_lst[x])
item = frappe.db.sql(
"""select is_stock_item,
is_sub_contracted_item, end_of_life, disabled from `tabItem` where name=%s""",
d.item_code,
as_dict=1,
)[0]
set_stock_levels(row=d) # update with latest quantities
item = validate_item_and_get_basic_data(row=d)
validate_stock_item_warehouse(row=d, item=item)
validate_end_of_life(d.item_code, item.end_of_life, item.disabled)
# validate stock item
if item.is_stock_item == 1 and d.qty and not d.warehouse and not d.get("delivered_by_supplier"):
frappe.throw(
_("Warehouse is mandatory for stock Item {0} in row {1}").format(d.item_code, d.idx)
)
items.append(cstr(d.item_code))
if (
@@ -93,7 +64,57 @@ def validate_for_items(doc):
frappe.throw(_("Same item cannot be entered multiple times."))
def check_on_hold_or_closed_status(doctype, docname):
def set_stock_levels(row) -> None:
projected_qty = frappe.db.get_value(
"Bin",
{
"item_code": row.item_code,
"warehouse": row.warehouse,
},
"projected_qty",
)
qty_data = {
"projected_qty": flt(projected_qty),
"ordered_qty": 0,
"received_qty": 0,
}
if row.doctype in ("Purchase Receipt Item", "Purchase Invoice Item"):
qty_data.pop("received_qty")
for field in qty_data:
if row.meta.get_field(field):
row.set(field, qty_data[field])
def validate_item_and_get_basic_data(row) -> Dict:
item = frappe.db.get_values(
"Item",
filters={"name": row.item_code},
fieldname=["is_stock_item", "is_sub_contracted_item", "end_of_life", "disabled"],
as_dict=1,
)
if not item:
frappe.throw(_("Row #{0}: Item {1} does not exist").format(row.idx, frappe.bold(row.item_code)))
return item[0]
def validate_stock_item_warehouse(row, item) -> None:
if (
item.is_stock_item == 1
and row.qty
and not row.warehouse
and not row.get("delivered_by_supplier")
):
frappe.throw(
_("Row #{1}: Warehouse is mandatory for stock Item {0}").format(
frappe.bold(row.item_code), row.idx
)
)
def check_on_hold_or_closed_status(doctype, docname) -> None:
status = frappe.db.get_value(doctype, docname, "status")
if status in ("Closed", "On Hold"):

View File

@@ -1268,17 +1268,11 @@ class AccountsController(TransactionBase):
stock_items = []
item_codes = list(set(item.item_code for item in self.get("items")))
if item_codes:
stock_items = [
r[0]
for r in frappe.db.sql(
"""
select name from `tabItem`
where name in (%s) and is_stock_item=1
"""
% (", ".join((["%s"] * len(item_codes))),),
item_codes,
)
]
stock_items = frappe.db.get_values(
"Item", {"name": ["in", item_codes], "is_stock_item": 1}, as_dict=True, cache=True
)
if stock_items:
stock_items = [d.get("name") for d in stock_items]
return stock_items

View File

@@ -464,7 +464,10 @@ class BuyingController(StockController, Subcontracting):
stock_items = self.get_stock_items()
for d in self.get("items"):
if d.item_code in stock_items and d.warehouse:
if d.item_code not in stock_items:
continue
if d.warehouse:
pr_qty = flt(d.qty) * flt(d.conversion_factor)
if pr_qty:
@@ -489,6 +492,7 @@ class BuyingController(StockController, Subcontracting):
sle = self.get_sl_entries(
d, {"actual_qty": flt(pr_qty), "serial_no": cstr(d.serial_no).strip()}
)
if self.is_return:
outgoing_rate = get_rate_for_return(
self.doctype, self.name, d.item_code, self.return_against, item_row=d
@@ -518,18 +522,18 @@ class BuyingController(StockController, Subcontracting):
sl_entries.append(from_warehouse_sle)
if flt(d.rejected_qty) != 0:
sl_entries.append(
self.get_sl_entries(
d,
{
"warehouse": d.rejected_warehouse,
"actual_qty": flt(d.rejected_qty) * flt(d.conversion_factor),
"serial_no": cstr(d.rejected_serial_no).strip(),
"incoming_rate": 0.0,
},
)
if flt(d.rejected_qty) != 0:
sl_entries.append(
self.get_sl_entries(
d,
{
"warehouse": d.rejected_warehouse,
"actual_qty": flt(d.rejected_qty) * flt(d.conversion_factor),
"serial_no": cstr(d.rejected_serial_no).strip(),
"incoming_rate": 0.0,
},
)
)
self.make_sl_entries_for_supplier_warehouse(sl_entries)
self.make_sl_entries(

View File

@@ -307,6 +307,11 @@ class calculate_taxes_and_totals(object):
self.doc.round_floats_in(self.doc, ["total", "base_total", "net_total", "base_net_total"])
def calculate_shipping_charges(self):
# Do not apply shipping rule for POS
if self.doc.get("is_pos"):
return
if hasattr(self.doc, "shipping_rule") and self.doc.shipping_rule:
shipping_rule = frappe.get_doc("Shipping Rule", self.doc.shipping_rule)
shipping_rule.apply(self.doc)

View File

@@ -2,6 +2,17 @@
// For license information, please see license.txt
frappe.ui.form.on("Contract", {
onload: function(frm) {
frappe.db.get_value(
"Selling Settings",
"Selling Settings",
"contract_naming_by",
(r) => {
frm.toggle_display("naming_series", r.contract_naming_by === "Naming Series");
}
);
},
contract_template: function (frm) {
if (frm.doc.contract_template) {
frappe.call({

View File

@@ -2,11 +2,13 @@
"actions": [],
"allow_import": 1,
"allow_rename": 1,
"autoname": "naming_series:",
"creation": "2018-04-12 06:32:04.582486",
"doctype": "DocType",
"editable_grid": 1,
"engine": "InnoDB",
"field_order": [
"naming_series",
"party_type",
"is_signed",
"cb_party",
@@ -244,11 +246,20 @@
"fieldname": "authorised_by_section",
"fieldtype": "Section Break",
"label": "Authorised By"
},
{
"fieldname": "naming_series",
"fieldtype": "Select",
"label": "Naming Series",
"no_copy": 1,
"options": "CRM-CONTR-.YYYY.-",
"reqd": 1,
"set_only_once": 1
}
],
"is_submittable": 1,
"links": [],
"modified": "2020-12-07 11:15:58.385521",
"modified": "2022-03-28 10:22:11.156658",
"modified_by": "Administrator",
"module": "CRM",
"name": "Contract",

View File

@@ -5,22 +5,32 @@
import frappe
from frappe import _
from frappe.model.document import Document
from frappe.model.naming import set_name_by_naming_series
from frappe.utils import getdate, nowdate
class Contract(Document):
def autoname(self):
name = self.party_name
if frappe.db.get_single_value("Selling Settings", "contract_naming_by") == "Naming Series":
set_name_by_naming_series(self)
if self.contract_template:
name += " - {} Agreement".format(self.contract_template)
else:
name = self.party_name
# If identical, append contract name with the next number in the iteration
if frappe.db.exists("Contract", name):
count = len(frappe.get_all("Contract", filters={"name": ["like", "%{}%".format(name)]}))
name = "{} - {}".format(name, count)
if self.contract_template:
name = f"{name} - {self.contract_template} Agreement"
self.name = _(name)
# If identical, append contract name with the next number in the iteration
if frappe.db.exists("Contract", name):
count = frappe.db.count(
"Contract",
filters={
"name": ("like", f"%{name}%"),
},
)
name = f"{name} - {count}"
self.name = _(name)
def validate(self):
self.validate_dates()

View File

@@ -47,7 +47,7 @@
"item_search_settings_section",
"redisearch_warning",
"search_index_fields",
"show_categories_in_search_autocomplete",
"is_redisearch_enabled",
"is_redisearch_loaded",
"shop_by_category_section",
"slideshow",
@@ -293,6 +293,7 @@
"fieldname": "search_index_fields",
"fieldtype": "Small Text",
"label": "Search Index Fields",
"mandatory_depends_on": "is_redisearch_enabled",
"read_only_depends_on": "eval:!doc.is_redisearch_loaded"
},
{
@@ -301,13 +302,6 @@
"fieldtype": "Section Break",
"label": "Item Search Settings"
},
{
"default": "1",
"fieldname": "show_categories_in_search_autocomplete",
"fieldtype": "Check",
"label": "Show Categories in Search Autocomplete",
"read_only_depends_on": "eval:!doc.is_redisearch_loaded"
},
{
"default": "0",
"fieldname": "is_redisearch_loaded",
@@ -365,12 +359,19 @@
"fieldname": "show_price_in_quotation",
"fieldtype": "Check",
"label": "Show Price in Quotation"
},
{
"default": "0",
"fieldname": "is_redisearch_enabled",
"fieldtype": "Check",
"label": "Enable Redisearch",
"read_only_depends_on": "eval:!doc.is_redisearch_loaded"
}
],
"index_web_pages_for_search": 1,
"issingle": 1,
"links": [],
"modified": "2021-09-02 14:02:44.785824",
"modified": "2022-04-01 18:35:56.106756",
"modified_by": "Administrator",
"module": "E-commerce",
"name": "E Commerce Settings",
@@ -389,5 +390,6 @@
],
"sort_field": "modified",
"sort_order": "DESC",
"states": [],
"track_changes": 1
}

View File

@@ -8,6 +8,7 @@ from frappe.utils import comma_and, flt, unique
from erpnext.e_commerce.redisearch_utils import (
create_website_items_index,
define_autocomplete_dictionary,
get_indexable_web_fields,
is_search_module_loaded,
)
@@ -20,6 +21,8 @@ class ShoppingCartSetupError(frappe.ValidationError):
class ECommerceSettings(Document):
def onload(self):
self.get("__onload").quotation_series = frappe.get_meta("Quotation").get_options("naming_series")
# flag >> if redisearch is installed and loaded
self.is_redisearch_loaded = is_search_module_loaded()
def validate(self):
@@ -33,6 +36,20 @@ class ECommerceSettings(Document):
frappe.clear_document_cache("E Commerce Settings", "E Commerce Settings")
self.is_redisearch_enabled_pre_save = frappe.db.get_single_value(
"E Commerce Settings", "is_redisearch_enabled"
)
def after_save(self):
self.create_redisearch_indexes()
def create_redisearch_indexes(self):
# if redisearch is enabled (value changed) create indexes and dictionary
value_changed = self.is_redisearch_enabled != self.is_redisearch_enabled_pre_save
if self.is_redisearch_loaded and self.is_redisearch_enabled and value_changed:
define_autocomplete_dictionary()
create_website_items_index()
def validate_field_filters(self):
if not (self.enable_field_filters and self.filter_fields):
return

View File

@@ -2,7 +2,7 @@
// For license information, please see license.txt
frappe.ui.form.on('Website Item', {
onload: function(frm) {
onload: (frm) => {
// should never check Private
frm.fields_dict["website_image"].df.is_private = 0;
@@ -13,18 +13,35 @@ frappe.ui.form.on('Website Item', {
});
},
image: function() {
refresh: (frm) => {
frm.add_custom_button(__("Prices"), function() {
frappe.set_route("List", "Item Price", {"item_code": frm.doc.item_code});
}, __("View"));
frm.add_custom_button(__("Stock"), function() {
frappe.route_options = {
"item_code": frm.doc.item_code
};
frappe.set_route("query-report", "Stock Balance");
}, __("View"));
frm.add_custom_button(__("E Commerce Settings"), function() {
frappe.set_route("Form", "E Commerce Settings");
}, __("View"));
},
image: () => {
refresh_field("image_view");
},
copy_from_item_group: function(frm) {
copy_from_item_group: (frm) => {
return frm.call({
doc: frm.doc,
method: "copy_specification_from_item_group"
});
},
set_meta_tags(frm) {
set_meta_tags: (frm) => {
frappe.utils.set_meta_tag(frm.doc.route);
}
});

View File

@@ -1,8 +1,12 @@
# Copyright (c) 2021, Frappe Technologies Pvt. Ltd. and Contributors
# Copyright (c) 2022, Frappe Technologies Pvt. Ltd. and Contributors
# License: GNU General Public License v3. See license.txt
import json
import frappe
from frappe import _
from frappe.utils.redis_wrapper import RedisWrapper
from redis import ResponseError
from redisearch import AutoCompleter, Client, IndexDefinition, Suggestion, TagField, TextField
WEBSITE_ITEM_INDEX = "website_items_index"
@@ -22,6 +26,12 @@ def get_indexable_web_fields():
return [df.fieldname for df in valid_fields]
def is_redisearch_enabled():
"Return True only if redisearch is loaded and enabled."
is_redisearch_enabled = frappe.db.get_single_value("E Commerce Settings", "is_redisearch_enabled")
return is_search_module_loaded() and is_redisearch_enabled
def is_search_module_loaded():
try:
cache = frappe.cache()
@@ -32,14 +42,14 @@ def is_search_module_loaded():
)
return "search" in parsed_output
except Exception:
return False
return False # handling older redis versions
def if_redisearch_loaded(function):
"Decorator to check if Redisearch is loaded."
def if_redisearch_enabled(function):
"Decorator to check if Redisearch is enabled."
def wrapper(*args, **kwargs):
if is_search_module_loaded():
if is_redisearch_enabled():
func = function(*args, **kwargs)
return func
return
@@ -51,22 +61,25 @@ def make_key(key):
return "{0}|{1}".format(frappe.conf.db_name, key).encode("utf-8")
@if_redisearch_loaded
@if_redisearch_enabled
def create_website_items_index():
"Creates Index Definition."
# CREATE index
client = Client(make_key(WEBSITE_ITEM_INDEX), conn=frappe.cache())
# DROP if already exists
try:
client.drop_index()
except Exception:
client.drop_index() # drop if already exists
except ResponseError:
# will most likely raise a ResponseError if index does not exist
# ignore and create index
pass
except Exception:
raise_redisearch_error()
idx_def = IndexDefinition([make_key(WEBSITE_ITEM_KEY_PREFIX)])
# Based on e-commerce settings
# Index fields mentioned in e-commerce settings
idx_fields = frappe.db.get_single_value("E Commerce Settings", "search_index_fields")
idx_fields = idx_fields.split(",") if idx_fields else []
@@ -91,20 +104,20 @@ def to_search_field(field):
return TextField(field)
@if_redisearch_loaded
@if_redisearch_enabled
def insert_item_to_index(website_item_doc):
# Insert item to index
key = get_cache_key(website_item_doc.name)
cache = frappe.cache()
web_item = create_web_item_map(website_item_doc)
for k, v in web_item.items():
super(RedisWrapper, cache).hset(make_key(key), k, v)
for field, value in web_item.items():
super(RedisWrapper, cache).hset(make_key(key), field, value)
insert_to_name_ac(website_item_doc.web_item_name, website_item_doc.name)
@if_redisearch_loaded
@if_redisearch_enabled
def insert_to_name_ac(web_name, doc_name):
ac = AutoCompleter(make_key(WEBSITE_ITEM_NAME_AUTOCOMPLETE), conn=frappe.cache())
ac.add_suggestions(Suggestion(web_name, payload=doc_name))
@@ -114,20 +127,20 @@ def create_web_item_map(website_item_doc):
fields_to_index = get_fields_indexed()
web_item = {}
for f in fields_to_index:
web_item[f] = website_item_doc.get(f) or ""
for field in fields_to_index:
web_item[field] = website_item_doc.get(field) or ""
return web_item
@if_redisearch_loaded
@if_redisearch_enabled
def update_index_for_item(website_item_doc):
# Reinsert to Cache
insert_item_to_index(website_item_doc)
define_autocomplete_dictionary()
@if_redisearch_loaded
@if_redisearch_enabled
def delete_item_from_index(website_item_doc):
cache = frappe.cache()
key = get_cache_key(website_item_doc.name)
@@ -135,13 +148,13 @@ def delete_item_from_index(website_item_doc):
try:
cache.delete(key)
except Exception:
return False
raise_redisearch_error()
delete_from_ac_dict(website_item_doc)
return True
@if_redisearch_loaded
@if_redisearch_enabled
def delete_from_ac_dict(website_item_doc):
"""Removes this items's name from autocomplete dictionary"""
cache = frappe.cache()
@@ -149,40 +162,60 @@ def delete_from_ac_dict(website_item_doc):
name_ac.delete(website_item_doc.web_item_name)
@if_redisearch_loaded
@if_redisearch_enabled
def define_autocomplete_dictionary():
"""Creates an autocomplete search dictionary for `name`.
Also creats autocomplete dictionary for `categories` if
checked in E Commerce Settings"""
"""
Defines/Redefines an autocomplete search dictionary for Website Item Name.
Also creats autocomplete dictionary for Published Item Groups.
"""
cache = frappe.cache()
name_ac = AutoCompleter(make_key(WEBSITE_ITEM_NAME_AUTOCOMPLETE), conn=cache)
cat_ac = AutoCompleter(make_key(WEBSITE_ITEM_CATEGORY_AUTOCOMPLETE), conn=cache)
ac_categories = frappe.db.get_single_value(
"E Commerce Settings", "show_categories_in_search_autocomplete"
)
item_ac = AutoCompleter(make_key(WEBSITE_ITEM_NAME_AUTOCOMPLETE), conn=cache)
item_group_ac = AutoCompleter(make_key(WEBSITE_ITEM_CATEGORY_AUTOCOMPLETE), conn=cache)
# Delete both autocomplete dicts
try:
cache.delete(make_key(WEBSITE_ITEM_NAME_AUTOCOMPLETE))
cache.delete(make_key(WEBSITE_ITEM_CATEGORY_AUTOCOMPLETE))
except Exception:
return False
raise_redisearch_error()
create_items_autocomplete_dict(autocompleter=item_ac)
create_item_groups_autocomplete_dict(autocompleter=item_group_ac)
@if_redisearch_enabled
def create_items_autocomplete_dict(autocompleter):
"Add items as suggestions in Autocompleter."
items = frappe.get_all(
"Website Item", fields=["web_item_name", "item_group"], filters={"published": 1}
)
for item in items:
name_ac.add_suggestions(Suggestion(item.web_item_name))
if ac_categories and item.item_group:
cat_ac.add_suggestions(Suggestion(item.item_group))
return True
autocompleter.add_suggestions(Suggestion(item.web_item_name))
@if_redisearch_loaded
@if_redisearch_enabled
def create_item_groups_autocomplete_dict(autocompleter):
"Add item groups with weightage as suggestions in Autocompleter."
published_item_groups = frappe.get_all(
"Item Group", fields=["name", "route", "weightage"], filters={"show_in_website": 1}
)
if not published_item_groups:
return
for item_group in published_item_groups:
payload = json.dumps({"name": item_group.name, "route": item_group.route})
autocompleter.add_suggestions(
Suggestion(
string=item_group.name,
score=frappe.utils.flt(item_group.weightage) or 1.0,
payload=payload, # additional info that can be retrieved later
)
)
@if_redisearch_enabled
def reindex_all_web_items():
items = frappe.get_all("Website Item", fields=get_fields_indexed(), filters={"published": True})
@@ -191,8 +224,8 @@ def reindex_all_web_items():
web_item = create_web_item_map(item)
key = make_key(get_cache_key(item.name))
for k, v in web_item.items():
super(RedisWrapper, cache).hset(key, k, v)
for field, value in web_item.items():
super(RedisWrapper, cache).hset(key, field, value)
def get_cache_key(name):
@@ -210,7 +243,12 @@ def get_fields_indexed():
return fields_to_index
# TODO: Remove later
# # Figure out a way to run this at startup
define_autocomplete_dictionary()
create_website_items_index()
def raise_redisearch_error():
"Create an Error Log and raise error."
traceback = frappe.get_traceback()
log = frappe.log_error(traceback, frappe._("Redisearch Error"))
log_link = frappe.utils.get_link_to_form("Error Log", log.name)
frappe.throw(
msg=_("Something went wrong. Check {0}").format(log_link), title=_("Redisearch Error")
)

View File

@@ -1,6 +1,6 @@
<div class="web-list-item transaction-list-item">
{% set today = frappe.utils.getdate(frappe.utils.nowdate()) %}
<a href = "{{ doc.route }}/" class="no-underline">
<a href = "{{ doc.route }}" class="no-underline">
<div class="row">
<div class="col-sm-4 bold">
<span class="indicator

View File

@@ -511,7 +511,7 @@ scheduler_events = {
],
"daily_long": [
"erpnext.setup.doctype.email_digest.email_digest.send",
"erpnext.manufacturing.doctype.bom_update_tool.bom_update_tool.update_latest_price_in_all_boms",
"erpnext.manufacturing.doctype.bom_update_tool.bom_update_tool.auto_update_latest_price_in_all_boms",
"erpnext.hr.doctype.leave_ledger_entry.leave_ledger_entry.process_expired_allocation",
"erpnext.hr.utils.generate_leave_encashment",
"erpnext.hr.utils.allocate_earned_leaves",

View File

@@ -39,11 +39,15 @@ class LeaveAllocation(Document):
def validate(self):
self.validate_period()
self.validate_allocation_overlap()
self.validate_back_dated_allocation()
self.set_total_leaves_allocated()
self.validate_total_leaves_allocated()
self.validate_lwp()
set_employee_name(self)
self.set_total_leaves_allocated()
self.validate_leave_days_and_dates()
def validate_leave_days_and_dates(self):
# all validations that should run on save as well as on update after submit
self.validate_back_dated_allocation()
self.validate_total_leaves_allocated()
self.validate_leave_allocation_days()
def validate_leave_allocation_days(self):
@@ -56,14 +60,19 @@ class LeaveAllocation(Document):
leave_allocated = 0
if leave_period:
leave_allocated = get_leave_allocation_for_period(
self.employee, self.leave_type, leave_period[0].from_date, leave_period[0].to_date
self.employee,
self.leave_type,
leave_period[0].from_date,
leave_period[0].to_date,
exclude_allocation=self.name,
)
leave_allocated += flt(self.new_leaves_allocated)
if leave_allocated > 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()

View File

@@ -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):

View File

@@ -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

View File

@@ -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):

View File

@@ -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

View File

@@ -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)

View File

@@ -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) {
// }
});

View File

@@ -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
}

View File

@@ -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

View File

@@ -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];
}
};

View File

@@ -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")

View File

@@ -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"
});
}
});

View File

@@ -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()

View File

@@ -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"]:

View File

@@ -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")

View File

@@ -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):

View File

@@ -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

View File

@@ -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

View File

@@ -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)

View File

@@ -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)
)

View File

@@ -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()

View File

@@ -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",

View File

@@ -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();

View File

@@ -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',

View File

@@ -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

View File

@@ -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

View File

@@ -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
}
}

View File

@@ -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

View File

@@ -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();
}
}
};

View File

@@ -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();
}
}

View File

@@ -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) {

View File

@@ -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"
});
}
}
});

View File

@@ -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",

View File

@@ -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):

View File

@@ -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",

View File

@@ -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;

View File

@@ -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"]},
],
}

View File

@@ -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
}

View File

@@ -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

View File

@@ -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"},

View File

@@ -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)

View File

@@ -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

View File

@@ -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);
}
},

View File

@@ -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",

View File

@@ -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

View File

@@ -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