diff --git a/.github/ISSUE_TEMPLATE/bug_report.md b/.github/ISSUE_TEMPLATE/bug_report.md deleted file mode 100644 index c145291b57c..00000000000 --- a/.github/ISSUE_TEMPLATE/bug_report.md +++ /dev/null @@ -1,47 +0,0 @@ ---- -name: Bug report -about: Report a bug encountered while using ERPNext -labels: bug ---- - - - -## Description of the issue - -## Context information (for bug reports) - -**Output of `bench version`** -``` -(paste here) -``` - -## Steps to reproduce the issue - -1. -2. -3. - -### Observed result - -### Expected result - -### Stacktrace / full error message - -``` -(paste here) -``` - -## Additional information - -OS version / distribution, `ERPNext` install method, etc. diff --git a/.github/ISSUE_TEMPLATE/bug_report.yaml b/.github/ISSUE_TEMPLATE/bug_report.yaml new file mode 100644 index 00000000000..8f938112a78 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/bug_report.yaml @@ -0,0 +1,87 @@ +name: Bug Report +description: Report a bug encountered while using ERPNext +labels: ["bug"] + +body: + - type: markdown + attributes: + value: | + Welcome to ERPNext issue tracker! Before creating an issue, please heed the following: + + 1. This tracker should only be used to report bugs and request features / enhancements to ERPNext + - For questions and general support, checkout the [user manual](https://docs.erpnext.com/) or use [forum](https://discuss.erpnext.com) + - For documentation issues, propose edit on [documentation site](https://docs.erpnext.com/) directly. + 2. When making a bug report, make sure you provide all required information. The easier it is for + maintainers to reproduce, the faster it'll be fixed. + 3. If you think you know what the reason for the bug is, share it with us. Maybe put in a PR 😉 + + - type: textarea + id: bug-info + attributes: + label: Information about bug + description: Also tell us, what did you expect to happen? + placeholder: Please provide as much information as possible. + validations: + required: true + + - type: dropdown + id: module + attributes: + label: Module + description: Select affected module of ERPNext. + multiple: true + options: + - accounts + - stock + - buying + - selling + - ecommerce + - manufacturing + - HR + - projects + - support + - assets + - integrations + - quality + - regional + - portal + - agriculture + - education + - non-profit + validations: + required: true + + - type: textarea + id: exact-version + attributes: + label: Version + description: Share exact version number of Frappe and ERPNext you are using. + placeholder: | + Frappe version - + ERPNext Verion - + validations: + required: true + + - type: dropdown + id: install-method + attributes: + label: Installation method + options: + - docker + - easy-install + - manual install + - FrappeCloud + validations: + required: false + + - type: textarea + id: logs + attributes: + label: Relevant log output / Stack trace / Full Error Message. + description: Please copy and paste any relevant log output. This will be automatically formatted. + render: shell + + - type: markdown + attributes: + value: | + By submitting this issue, you agree to follow our [Code of Conduct](https://github.com/frappe/erpnext/blob/develop/CODE_OF_CONDUCT.md) diff --git a/.github/ISSUE_TEMPLATE/feature_request.md b/.github/ISSUE_TEMPLATE/feature_request.md index 6cdad356cd0..418bf3c9417 100644 --- a/.github/ISSUE_TEMPLATE/feature_request.md +++ b/.github/ISSUE_TEMPLATE/feature_request.md @@ -1,7 +1,10 @@ --- name: Feature request about: Suggest an idea to improve ERPNext +title: '' labels: feature-request +assignees: '' + --- \n \n Invoice#: {{doc.name}}\n \u0631\u0642\u0645 \u0627\u0644\u0641\u0627\u062a\u0648\u0631\u0629: {{doc.name}}\n \n \n Invoice Date: {{doc.posting_date}}\n \u062a\u0627\u0631\u064a\u062e \u0627\u0644\u0641\u0627\u062a\u0648\u0631\u0629: {{doc.posting_date}}\n \n \n Date of Supply:{{doc.posting_date}}\n \u062a\u0627\u0631\u064a\u062e \u0627\u0644\u062a\u0648\u0631\u064a\u062f: {{doc.posting_date}}\n \n \n \n \n Supplier:\n \u0627\u0644\u0645\u0648\u0631\u062f:\n \n\t\t{% if (company.tax_id) %}\n \n Supplier Tax Identification Number:\n \u0631\u0642\u0645 \u0627\u0644\u062a\u0639\u0631\u064a\u0641 \u0627\u0644\u0636\u0631\u064a\u0628\u064a \u0644\u0644\u0645\u0648\u0631\u062f:\n \n \n {{ company.tax_id }}\n {{ company.tax_id }}\n \n {% endif %}\n \n {{ company.name }}\n {{ company.company_name_in_arabic }} \n \n \n \n {% if(supplier_address_doc) %}\n \n {{ supplier_address_doc.address_line1}} \n {{ supplier_address_doc.address_in_arabic}} \n \n \n Phone: {{ supplier_address_doc.phone }}\n \u0647\u0627\u062a\u0641: {{ supplier_address_doc.phone }}\n \n \n Email: {{ supplier_address_doc.email_id }}\n \u0628\u0631\u064a\u062f \u0627\u0644\u0643\u062a\u0631\u0648\u0646\u064a: {{ supplier_address_doc.email_id }}\n \n {% endif %}\n \n \n \n CUSTOMER:\n \u0639\u0645\u064a\u0644:\n \n\t\t{% set customer_tax_id = frappe.db.get_value('Customer', doc.customer, 'tax_id') %}\n\t\t{% if customer_tax_id %}\n \n Customer Tax Identification Number:\n \u0631\u0642\u0645 \u0627\u0644\u062a\u0639\u0631\u064a\u0641 \u0627\u0644\u0636\u0631\u064a\u0628\u064a \u0644\u0644\u0639\u0645\u064a\u0644:\n \n \n {{ customer_tax_id }}\n {{ customer_tax_id }}\n \n {% endif %}\n \n {{ doc.customer }}\n {{ doc.customer_name_in_arabic }} \n \n \n {% if(customer_address) %}\n \n {{ customer_address.address_line1}} \n {{ customer_address.address_in_arabic}} \n \n {% endif %}\n \n {% if(customer_shipping_address) %}\n \n SHIPPING ADDRESS:\n \u0639\u0646\u0648\u0627\u0646 \u0627\u0644\u0634\u062d\u0646:\n \n \n \n {{ customer_shipping_address.address_line1}} \n {{ customer_shipping_address.address_in_arabic}} \n \n {% endif %}\n \n\t\t{% if(doc.po_no) %}\n \n OTHER INFORMATION\n \u0645\u0639\u0644\u0648\u0645\u0627\u062a \u0623\u062e\u0631\u0649\n \n \n \n Purchase Order Number: {{ doc.po_no }}\n \u0631\u0642\u0645 \u0623\u0645\u0631 \u0627\u0644\u0634\u0631\u0627\u0621: {{ doc.po_no }}\n \n {% endif %}\n \n \n Payment Due Date: {{ doc.due_date}} \n \u062a\u0627\u0631\u064a\u062e \u0627\u0633\u062a\u062d\u0642\u0627\u0642 \u0627\u0644\u062f\u0641\u0639: {{ doc.due_date}}\n \n \n \n\n \n {% set col = namespace(one = 2, two = 1) %}\n {% set length = doc.taxes | length %}\n {% set length = length / 2 | round %}\n {% set col.one = col.one + length %}\n {% set col.two = col.two + length %}\n \n {%- if(doc.taxes | length % 2 > 0 ) -%}\n {% set col.two = col.two + 1 %}\n {% endif %}\n \n \n {% set total = namespace(amount = 0) %}\n \n \n \n \n \n \n \n \n {% for row in doc.taxes %}\n \n {% endfor %}\n \n \n \n \n \n {%- for item in doc.items -%}\n {% set total.amount = item.amount %}\n \n \n \n \n \n {% for row in doc.taxes %}\n {% set data_object = json.loads(row.item_wise_tax_detail) %}\n \n {% endfor %}\n \n \n {%- endfor -%}\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
Nature of goods or services
\u0637\u0628\u064a\u0639\u0629 \u0627\u0644\u0633\u0644\u0639 \u0623\u0648 \u0627\u0644\u062e\u062f\u0645\u0627\u062a
\n Unit price
\n \u0633\u0639\u0631 \u0627\u0644\u0648\u062d\u062f\u0629\n
\n Quantity
\n \u0627\u0644\u0643\u0645\u064a\u0629\n
\n Taxable Amount
\n \u0627\u0644\u0645\u0628\u0644\u063a \u0627\u0644\u062e\u0627\u0636\u0639 \u0644\u0644\u0636\u0631\u064a\u0628\u0629\n
{{row.description}}\n Total
\n \u0627\u0644\u0645\u062c\u0645\u0648\u0639\n
{{ item.item_code }}{{ item.get_formatted(\"rate\") }}{{ item.qty }}{{ item.get_formatted(\"amount\") }}\n
\n {%- if(data_object[item.item_code][0])-%}\n {{ frappe.format(data_object[item.item_code][0], {'fieldtype': 'Percent'}) }}\n {%- endif -%}\n \n {%- if(data_object[item.item_code][1])-%}\n {{ frappe.format(data_object[item.item_code][1], {'fieldtype': 'Currency'}) }}\n {% set total.amount = total.amount + data_object[item.item_code][1] %}\n {%- endif -%}\n
\n
{{ frappe.format(total.amount, {'fieldtype': 'Currency'}) }}
\n {{ doc.get_formatted(\"total\") }}
\n {{ doc.get_formatted(\"total_taxes_and_charges\") }}\n
\n \u0627\u0644\u0625\u062c\u0645\u0627\u0644\u064a \u0628\u0627\u0633\u062a\u062b\u0646\u0627\u0621 \u0636\u0631\u064a\u0628\u0629 \u0627\u0644\u0642\u064a\u0645\u0629 \u0627\u0644\u0645\u0636\u0627\u0641\u0629\n
\n \u0625\u062c\u0645\u0627\u0644\u064a \u0636\u0631\u064a\u0628\u0629 \u0627\u0644\u0642\u064a\u0645\u0629 \u0627\u0644\u0645\u0636\u0627\u0641\u0629\n
\n Total (Excluding VAT)\n
\n Total VAT\n
\n {{ doc.get_formatted(\"total\") }}
\n {{ doc.get_formatted(\"total_taxes_and_charges\") }}\n
{{ doc.get_formatted(\"grand_total\") }}\n \u0625\u062c\u0645\u0627\u0644\u064a \u0627\u0644\u0645\u0628\u0644\u063a \u0627\u0644\u0645\u0633\u062a\u062d\u0642Total Amount Due{{ doc.get_formatted(\"grand_total\") }}
\n\n\t{%- if doc.terms -%}\n

\n {{doc.terms}}\n

\n\t{%- endif -%}\n\n", + "html": "
\n
\n
\n

TAX INVOICE

\n

\u0641\u0627\u062a\u0648\u0631\u0629 \u0636\u0631\u064a\u0628\u064a\u0629

\n
\n \n \n
\n {% set company = frappe.get_doc(\"Company\", doc.company)%}\n {% if (doc.company_address) %}\n {% set supplier_address_doc = frappe.get_doc('Address', doc.company_address) %}\n {% endif %}\n \n {% if(doc.customer_address) %}\n {% set customer_address = frappe.get_doc('Address', doc.customer_address ) %}\n {% endif %}\n \n {% if(doc.shipping_address_name) %}\n {% set customer_shipping_address = frappe.get_doc('Address', doc.shipping_address_name ) %}\n {% endif %} \n \n \n \n \n \n \n \n \n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n\t\t{% if (company.tax_id) %}\n \n \n \n \n \n \n \n \n {% endif %}\n \n \n \n \n \n \n {% if(supplier_address_doc) %}\n \n \n \n \n \n \n \n \n \n \n \n \n {% endif %}\n \n \n \n \n \n \n\t\t{% set customer_tax_id = frappe.db.get_value('Customer', doc.customer, 'tax_id') %}\n\t\t{% if customer_tax_id %}\n \n \n \n \n \n \n \n \n {% endif %}\n \n \n \n \n \n {% if(customer_address) %}\n \n \n \n \n {% endif %}\n \n {% if(customer_shipping_address) %}\n \n \n \n \n \n \n \n \n \n {% endif %}\n \n\t\t{% if(doc.po_no) %}\n \n \n \n \n \n \n \n \n \n {% endif %}\n \n \n \n \n \n \n
{{ company.name }}{{ company.company_name_in_arabic }}
Invoice#: {{doc.name}}\u0631\u0642\u0645 \u0627\u0644\u0641\u0627\u062a\u0648\u0631\u0629: {{doc.name}}
Invoice Date: {{doc.posting_date}}\u062a\u0627\u0631\u064a\u062e \u0627\u0644\u0641\u0627\u062a\u0648\u0631\u0629: {{doc.posting_date}}
Date of Supply:{{doc.posting_date}}\u062a\u0627\u0631\u064a\u062e \u0627\u0644\u062a\u0648\u0631\u064a\u062f: {{doc.posting_date}}
Supplier:\u0627\u0644\u0645\u0648\u0631\u062f:
Supplier Tax Identification Number:\u0631\u0642\u0645 \u0627\u0644\u062a\u0639\u0631\u064a\u0641 \u0627\u0644\u0636\u0631\u064a\u0628\u064a \u0644\u0644\u0645\u0648\u0631\u062f:
{{ company.tax_id }}{{ company.tax_id }}
{{ company.name }}{{ company.company_name_in_arabic }}
{{ supplier_address_doc.address_line1}} {{ supplier_address_doc.address_in_arabic}}
Phone: {{ supplier_address_doc.phone }}\u0647\u0627\u062a\u0641: {{ supplier_address_doc.phone }}
Email: {{ supplier_address_doc.email_id }}\u0628\u0631\u064a\u062f \u0627\u0644\u0643\u062a\u0631\u0648\u0646\u064a: {{ supplier_address_doc.email_id }}
CUSTOMER:\u0639\u0645\u064a\u0644:
Customer Tax Identification Number:\u0631\u0642\u0645 \u0627\u0644\u062a\u0639\u0631\u064a\u0641 \u0627\u0644\u0636\u0631\u064a\u0628\u064a \u0644\u0644\u0639\u0645\u064a\u0644:
{{ customer_tax_id }}{{ customer_tax_id }}
{{ doc.customer }} {{ doc.customer_name_in_arabic }}
{{ customer_address.address_line1}} {{ customer_address.address_in_arabic}}
SHIPPING ADDRESS:\u0639\u0646\u0648\u0627\u0646 \u0627\u0644\u0634\u062d\u0646:
{{ customer_shipping_address.address_line1}} {{ customer_shipping_address.address_in_arabic}}
OTHER INFORMATION\u0645\u0639\u0644\u0648\u0645\u0627\u062a \u0623\u062e\u0631\u0649
Purchase Order Number: {{ doc.po_no }}\u0631\u0642\u0645 \u0623\u0645\u0631 \u0627\u0644\u0634\u0631\u0627\u0621: {{ doc.po_no }}
Payment Due Date: {{ doc.due_date}} \u062a\u0627\u0631\u064a\u062e \u0627\u0633\u062a\u062d\u0642\u0627\u0642 \u0627\u0644\u062f\u0641\u0639: {{ doc.due_date}}
\n\n \n {% set col = namespace(one = 2, two = 1) %}\n {% set length = doc.taxes | length %}\n {% set length = length / 2 | round %}\n {% set col.one = col.one + length %}\n {% set col.two = col.two + length %}\n \n {%- if(doc.taxes | length % 2 > 0 ) -%}\n {% set col.two = col.two + 1 %}\n {% endif %}\n \n \n {% set total = namespace(amount = 0) %}\n \n \n \n \n \n \n \n \n {% for row in doc.taxes %}\n \n {% endfor %}\n \n \n \n \n \n {%- for item in doc.items -%}\n {% set total.amount = item.amount %}\n \n \n \n \n \n {% for row in doc.taxes %}\n {% set data_object = json.loads(row.item_wise_tax_detail) %}\n {% set key = item.item_code or item.item_name %}\n {% set tax_amount = frappe.utils.flt(data_object[key][1]/doc.conversion_rate, row.precision('tax_amount')) %}\n \n {% endfor %}\n \n \n {%- endfor -%}\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
Nature of goods or services
\u0637\u0628\u064a\u0639\u0629 \u0627\u0644\u0633\u0644\u0639 \u0623\u0648 \u0627\u0644\u062e\u062f\u0645\u0627\u062a
\n Unit price
\n \u0633\u0639\u0631 \u0627\u0644\u0648\u062d\u062f\u0629\n
\n Quantity
\n \u0627\u0644\u0643\u0645\u064a\u0629\n
\n Taxable Amount
\n \u0627\u0644\u0645\u0628\u0644\u063a \u0627\u0644\u062e\u0627\u0636\u0639 \u0644\u0644\u0636\u0631\u064a\u0628\u0629\n
{{row.description}}\n Total
\n \u0627\u0644\u0645\u062c\u0645\u0648\u0639\n
{{ item.item_code or item.item_name }}{{ item.get_formatted(\"rate\") }}{{ item.qty }}{{ item.get_formatted(\"amount\") }}\n
\n {%- if(data_object[key][0])-%}\n {{ frappe.format(data_object[key][0], {'fieldtype': 'Percent'}) }}\n {%- endif -%}\n \n {%- if(data_object[key][1])-%}\n {{ frappe.format_value(tax_amount, currency=doc.currency) }}\n {% set total.amount = total.amount + tax_amount %}\n {%- endif -%}\n
\n
{{ frappe.format_value(frappe.utils.flt(total.amount, doc.precision('total_taxes_and_charges')), currency=doc.currency) }}
\n {{ doc.get_formatted(\"total\") }}
\n {{ doc.get_formatted(\"total_taxes_and_charges\") }}\n
\n \u0627\u0644\u0625\u062c\u0645\u0627\u0644\u064a \u0628\u0627\u0633\u062a\u062b\u0646\u0627\u0621 \u0636\u0631\u064a\u0628\u0629 \u0627\u0644\u0642\u064a\u0645\u0629 \u0627\u0644\u0645\u0636\u0627\u0641\u0629\n
\n \u0625\u062c\u0645\u0627\u0644\u064a \u0636\u0631\u064a\u0628\u0629 \u0627\u0644\u0642\u064a\u0645\u0629 \u0627\u0644\u0645\u0636\u0627\u0641\u0629\n
\n Total (Excluding VAT)\n
\n Total VAT\n
\n {{ doc.get_formatted(\"total\") }}
\n {{ doc.get_formatted(\"total_taxes_and_charges\") }}\n
{{ doc.get_formatted(\"grand_total\") }}\n \u0625\u062c\u0645\u0627\u0644\u064a \u0627\u0644\u0645\u0628\u0644\u063a \u0627\u0644\u0645\u0633\u062a\u062d\u0642Total Amount Due{{ doc.get_formatted(\"grand_total\") }}
\n\n\t{%- if doc.terms -%}\n

\n {{doc.terms}}\n

\n\t{%- endif -%}\n
\n", "idx": 0, "line_breaks": 0, "margin_bottom": 15.0, "margin_left": 15.0, "margin_right": 15.0, "margin_top": 15.0, - "modified": "2021-11-08 09:19:18.660806", + "modified": "2021-12-07 13:43:38.018593", "modified_by": "Administrator", "module": "Regional", "name": "KSA VAT Invoice", diff --git a/erpnext/regional/report/eway_bill/eway_bill.py b/erpnext/regional/report/eway_bill/eway_bill.py index 91a47674d7b..f3fe5e88488 100644 --- a/erpnext/regional/report/eway_bill/eway_bill.py +++ b/erpnext/regional/report/eway_bill/eway_bill.py @@ -106,14 +106,14 @@ def set_address_details(row, special_characters): row.update({'ship_to_state': row.to_state}) def set_taxes(row, filters): - taxes = frappe.get_list("Sales Taxes and Charges", + taxes = frappe.get_all("Sales Taxes and Charges", filters={ 'parent': row.dn_id }, fields=('item_wise_tax_detail', 'account_head')) account_list = ["cgst_account", "sgst_account", "igst_account", "cess_account"] - taxes_list = frappe.get_list("GST Account", + taxes_list = frappe.get_all("GST Account", filters={ "parent": "GST Settings", "company": filters.company diff --git a/erpnext/regional/report/hsn_wise_summary_of_outward_supplies/hsn_wise_summary_of_outward_supplies.py b/erpnext/regional/report/hsn_wise_summary_of_outward_supplies/hsn_wise_summary_of_outward_supplies.py index e03ad374aef..1c1335ebe0b 100644 --- a/erpnext/regional/report/hsn_wise_summary_of_outward_supplies/hsn_wise_summary_of_outward_supplies.py +++ b/erpnext/regional/report/hsn_wise_summary_of_outward_supplies/hsn_wise_summary_of_outward_supplies.py @@ -114,9 +114,11 @@ def get_items(filters): items = frappe.db.sql(""" select - `tabSales Invoice Item`.name, `tabSales Invoice Item`.base_price_list_rate, - `tabSales Invoice Item`.gst_hsn_code, `tabSales Invoice Item`.stock_qty, - `tabSales Invoice Item`.stock_uom, `tabSales Invoice Item`.base_net_amount, + `tabSales Invoice Item`.gst_hsn_code, + `tabSales Invoice Item`.stock_uom, + sum(`tabSales Invoice Item`.stock_qty) as stock_qty, + sum(`tabSales Invoice Item`.base_net_amount) as base_net_amount, + sum(`tabSales Invoice Item`.base_price_list_rate) as base_price_list_rate, `tabSales Invoice Item`.parent, `tabSales Invoice Item`.item_code, `tabGST HSN Code`.description from `tabSales Invoice`, `tabSales Invoice Item`, `tabGST HSN Code` @@ -124,6 +126,8 @@ def get_items(filters): and `tabSales Invoice`.docstatus = 1 and `tabSales Invoice Item`.gst_hsn_code is not NULL and `tabSales Invoice Item`.gst_hsn_code = `tabGST HSN Code`.name %s %s + group by + `tabSales Invoice Item`.parent, `tabSales Invoice Item`.item_code """ % (conditions, match_conditions), filters, as_dict=1) diff --git a/erpnext/regional/report/hsn_wise_summary_of_outward_supplies/test_hsn_wise_summary_of_outward_supplies.py b/erpnext/regional/report/hsn_wise_summary_of_outward_supplies/test_hsn_wise_summary_of_outward_supplies.py new file mode 100644 index 00000000000..86dc458bdb1 --- /dev/null +++ b/erpnext/regional/report/hsn_wise_summary_of_outward_supplies/test_hsn_wise_summary_of_outward_supplies.py @@ -0,0 +1,89 @@ +# Copyright (c) 2021, Frappe Technologies Pvt. Ltd. and contributors +# For license information, please see license.txt + + +from unittest import TestCase + +import frappe + +from erpnext.accounts.doctype.sales_invoice.test_sales_invoice import create_sales_invoice +from erpnext.regional.doctype.gstr_3b_report.test_gstr_3b_report import ( + make_company as setup_company, +) +from erpnext.regional.doctype.gstr_3b_report.test_gstr_3b_report import ( + make_customers as setup_customers, +) +from erpnext.regional.doctype.gstr_3b_report.test_gstr_3b_report import ( + set_account_heads as setup_gst_settings, +) +from erpnext.regional.report.hsn_wise_summary_of_outward_supplies.hsn_wise_summary_of_outward_supplies import ( + execute as run_report, +) +from erpnext.stock.doctype.item.test_item import make_item + + +class TestHSNWiseSummaryReport(TestCase): + @classmethod + def setUpClass(cls): + setup_company() + setup_customers() + setup_gst_settings() + make_item("Golf Car", properties={ "gst_hsn_code": "999900" }) + + @classmethod + def tearDownClass(cls): + frappe.db.rollback() + + def test_hsn_summary_for_invoice_with_duplicate_items(self): + si = create_sales_invoice( + company="_Test Company GST", + customer = "_Test GST Customer", + currency = "INR", + warehouse = "Finished Goods - _GST", + debit_to = "Debtors - _GST", + income_account = "Sales - _GST", + expense_account = "Cost of Goods Sold - _GST", + cost_center = "Main - _GST", + do_not_save=1 + ) + + si.items = [] + si.append("items", { + "item_code": "Golf Car", + "gst_hsn_code": "999900", + "qty": "1", + "rate": "120", + "cost_center": "Main - _GST" + }) + si.append("items", { + "item_code": "Golf Car", + "gst_hsn_code": "999900", + "qty": "1", + "rate": "140", + "cost_center": "Main - _GST" + }) + si.append("taxes", { + "charge_type": "On Net Total", + "account_head": "Output Tax IGST - _GST", + "cost_center": "Main - _GST", + "description": "IGST @ 18.0", + "rate": 18 + }) + si.posting_date = "2020-11-17" + si.submit() + si.reload() + + [columns, data] = run_report(filters=frappe._dict({ + "company": "_Test Company GST", + "gst_hsn_code": "999900", + "company_gstin": si.company_gstin, + "from_date": si.posting_date, + "to_date": si.posting_date + })) + + filtered_rows = list(filter(lambda row: row['gst_hsn_code'] == "999900", data)) + self.assertTrue(filtered_rows) + + hsn_row = filtered_rows[0] + self.assertEquals(hsn_row['stock_qty'], 2.0) + self.assertEquals(hsn_row['total_amount'], 306.8) diff --git a/erpnext/regional/report/vat_audit_report/vat_audit_report.py b/erpnext/regional/report/vat_audit_report/vat_audit_report.py index 5a281a4cbb2..17e50648b3b 100644 --- a/erpnext/regional/report/vat_audit_report/vat_audit_report.py +++ b/erpnext/regional/report/vat_audit_report/vat_audit_report.py @@ -41,7 +41,7 @@ class VATAuditReport(object): return self.columns, self.data def get_sa_vat_accounts(self): - self.sa_vat_accounts = frappe.get_list("South Africa VAT Account", + self.sa_vat_accounts = frappe.get_all("South Africa VAT Account", filters = {"parent": self.filters.company}, pluck="account") if not self.sa_vat_accounts and not frappe.flags.in_test and not frappe.flags.in_migrate: link_to_settings = get_link_to_form("South Africa VAT Settings", "", label="South Africa VAT Settings") diff --git a/erpnext/regional/saudi_arabia/setup.py b/erpnext/regional/saudi_arabia/setup.py index 38a089c6326..2e31c03d5c6 100644 --- a/erpnext/regional/saudi_arabia/setup.py +++ b/erpnext/regional/saudi_arabia/setup.py @@ -3,7 +3,7 @@ import frappe from frappe.permissions import add_permission, update_permission_property -from erpnext.regional.united_arab_emirates.setup import make_custom_fields as uae_custom_fields, add_print_formats +from erpnext.regional.united_arab_emirates.setup import make_custom_fields as uae_custom_fields from erpnext.regional.saudi_arabia.wizard.operations.setup_ksa_vat_setting import create_ksa_vat_setting from frappe.custom.doctype.custom_field.custom_field import create_custom_fields @@ -13,6 +13,16 @@ def setup(company=None, patch=True): add_permissions() make_custom_fields() +def add_print_formats(): + frappe.reload_doc("regional", "print_format", "detailed_tax_invoice", force=True) + frappe.reload_doc("regional", "print_format", "simplified_tax_invoice", force=True) + frappe.reload_doc("regional", "print_format", "tax_invoice", force=True) + frappe.reload_doc("regional", "print_format", "ksa_vat_invoice", force=True) + frappe.reload_doc("regional", "print_format", "ksa_pos_invoice", force=True) + + for d in ('Simplified Tax Invoice', 'Detailed Tax Invoice', 'Tax Invoice', 'KSA VAT Invoice', 'KSA POS Invoice'): + frappe.db.set_value("Print Format", d, "disabled", 0) + def add_permissions(): """Add Permissions for KSA VAT Setting.""" add_permission('KSA VAT Setting', 'All', 0) @@ -33,8 +43,16 @@ def make_custom_fields(): custom_fields = { 'Sales Invoice': [ dict( - fieldname='qr_code', - label='QR Code', + fieldname='ksa_einv_qr', + label='KSA E-Invoicing QR', + fieldtype='Attach Image', + read_only=1, no_copy=1, hidden=1 + ) + ], + 'POS Invoice': [ + dict( + fieldname='ksa_einv_qr', + label='KSA E-Invoicing QR', fieldtype='Attach Image', read_only=1, no_copy=1, hidden=1 ) diff --git a/erpnext/regional/saudi_arabia/utils.py b/erpnext/regional/saudi_arabia/utils.py index cc6c0af7a56..a03c3f0994d 100644 --- a/erpnext/regional/saudi_arabia/utils.py +++ b/erpnext/regional/saudi_arabia/utils.py @@ -1,77 +1,149 @@ import io import os +from base64 import b64encode import frappe +from frappe import _ +from frappe.custom.doctype.custom_field.custom_field import create_custom_fields +from frappe.utils.data import add_to_date, get_time, getdate from pyqrcode import create as qr_create from erpnext import get_region -def create_qr_code(doc, method): - """Create QR Code after inserting Sales Inv - """ - +def create_qr_code(doc, method=None): region = get_region(doc.company) if region not in ['Saudi Arabia']: return - # if QR Code field not present, do nothing - if not hasattr(doc, 'qr_code'): - return + # if QR Code field not present, create it. Invoices without QR are invalid as per law. + if not hasattr(doc, 'ksa_einv_qr'): + create_custom_fields({ + doc.doctype: [ + dict( + fieldname='ksa_einv_qr', + label='KSA E-Invoicing QR', + fieldtype='Attach Image', + read_only=1, no_copy=1, hidden=1 + ) + ] + }) # Don't create QR Code if it already exists - qr_code = doc.get("qr_code") + qr_code = doc.get("ksa_einv_qr") if qr_code and frappe.db.exists({"doctype": "File", "file_url": qr_code}): return - meta = frappe.get_meta('Sales Invoice') + meta = frappe.get_meta(doc.doctype) - for field in meta.get_image_fields(): - if field.fieldname == 'qr_code': - # Creating public url to print format - default_print_format = frappe.db.get_value('Property Setter', dict(property='default_print_format', doc_type=doc.doctype), "value") + if "ksa_einv_qr" in [d.fieldname for d in meta.get_image_fields()]: + ''' TLV conversion for + 1. Seller's Name + 2. VAT Number + 3. Time Stamp + 4. Invoice Amount + 5. VAT Amount + ''' + tlv_array = [] + # Sellers Name - # System Language - language = frappe.get_system_settings('language') + seller_name = frappe.db.get_value( + 'Company', + doc.company, + 'company_name_in_arabic') - # creating qr code for the url - url = f"{ frappe.utils.get_url() }/{ doc.doctype }/{ doc.name }?format={ default_print_format or 'Standard' }&_lang={ language }&key={ doc.get_signature() }" - qr_image = io.BytesIO() - url = qr_create(url, error='L') - url.png(qr_image, scale=2, quiet_zone=1) + if not seller_name: + frappe.throw(_('Arabic name missing for {} in the company document').format(doc.company)) - # making file - filename = f"QR-CODE-{doc.name}.png".replace(os.path.sep, "__") - _file = frappe.get_doc({ - "doctype": "File", - "file_name": filename, - "is_private": 0, - "content": qr_image.getvalue(), - "attached_to_doctype": doc.get("doctype"), - "attached_to_name": doc.get("name"), - "attached_to_field": "qr_code" - }) + tag = bytes([1]).hex() + length = bytes([len(seller_name.encode('utf-8'))]).hex() + value = seller_name.encode('utf-8').hex() + tlv_array.append(''.join([tag, length, value])) - _file.save() + # VAT Number + tax_id = frappe.db.get_value('Company', doc.company, 'tax_id') + if not tax_id: + frappe.throw(_('Tax ID missing for {} in the company document').format(doc.company)) - # assigning to document - doc.db_set('qr_code', _file.file_url) - doc.notify_update() + tag = bytes([2]).hex() + length = bytes([len(tax_id)]).hex() + value = tax_id.encode('utf-8').hex() + tlv_array.append(''.join([tag, length, value])) - break + # Time Stamp + posting_date = getdate(doc.posting_date) + time = get_time(doc.posting_time) + seconds = time.hour * 60 * 60 + time.minute * 60 + time.second + time_stamp = add_to_date(posting_date, seconds=seconds) + time_stamp = time_stamp.strftime('%Y-%m-%dT%H:%M:%SZ') + + tag = bytes([3]).hex() + length = bytes([len(time_stamp)]).hex() + value = time_stamp.encode('utf-8').hex() + tlv_array.append(''.join([tag, length, value])) + + # Invoice Amount + invoice_amount = str(doc.grand_total) + tag = bytes([4]).hex() + length = bytes([len(invoice_amount)]).hex() + value = invoice_amount.encode('utf-8').hex() + tlv_array.append(''.join([tag, length, value])) + + # VAT Amount + vat_amount = str(doc.total_taxes_and_charges) + + tag = bytes([5]).hex() + length = bytes([len(vat_amount)]).hex() + value = vat_amount.encode('utf-8').hex() + tlv_array.append(''.join([tag, length, value])) + + # Joining bytes into one + tlv_buff = ''.join(tlv_array) + + # base64 conversion for QR Code + base64_string = b64encode(bytes.fromhex(tlv_buff)).decode() + + qr_image = io.BytesIO() + url = qr_create(base64_string, error='L') + url.png(qr_image, scale=2, quiet_zone=1) + + name = frappe.generate_hash(doc.name, 5) + + # making file + filename = f"QRCode-{name}.png".replace(os.path.sep, "__") + _file = frappe.get_doc({ + "doctype": "File", + "file_name": filename, + "is_private": 0, + "content": qr_image.getvalue(), + "attached_to_doctype": doc.get("doctype"), + "attached_to_name": doc.get("name"), + "attached_to_field": "ksa_einv_qr" + }) + + _file.save() + + # assigning to document + doc.db_set('ksa_einv_qr', _file.file_url) + doc.notify_update() -def delete_qr_code_file(doc, method): - """Delete QR Code on deleted sales invoice""" - +def delete_qr_code_file(doc, method=None): region = get_region(doc.company) if region not in ['Saudi Arabia']: return - if hasattr(doc, 'qr_code'): - if doc.get('qr_code'): + if hasattr(doc, 'ksa_einv_qr'): + if doc.get('ksa_einv_qr'): file_doc = frappe.get_list('File', { - 'file_url': doc.get('qr_code') + 'file_url': doc.get('ksa_einv_qr') }) if len(file_doc): - frappe.delete_doc('File', file_doc[0].name) \ No newline at end of file + frappe.delete_doc('File', file_doc[0].name) + +def delete_vat_settings_for_company(doc, method=None): + if doc.country != 'Saudi Arabia': + return + + if frappe.db.exists('KSA VAT Setting', doc.name): + frappe.delete_doc('KSA VAT Setting', doc.name) diff --git a/erpnext/restaurant/doctype/restaurant/test_restaurant.js b/erpnext/restaurant/doctype/restaurant/test_restaurant.js deleted file mode 100644 index 8fe4e7b84d5..00000000000 --- a/erpnext/restaurant/doctype/restaurant/test_restaurant.js +++ /dev/null @@ -1,50 +0,0 @@ -/* eslint-disable */ -// rename this file from _test_[name] to test_[name] to activate -// and remove above this line - -QUnit.test("test: Restaurant", function (assert) { - let done = assert.async(); - - // number of asserts - assert.expect(2); - let customer = { - "Test Customer 1": [ - {customer_name: "Test Customer 1"} - ], - "Test Customer 2": [ - {customer_name: "Test Customer 2"} - ] - }; - - frappe.run_serially([ - // insert a new Restaurant - () => frappe.tests.setup_doctype('Customer', customer), - () => { - return frappe.tests.make('Restaurant', [ - // values to be set - {__newname: 'Test Restaurant 1'}, - {company: 'Test Company'}, - {invoice_series_prefix: 'Test-Rest-1-Inv-'}, - {default_customer: 'Test Customer 1'} - ]) - }, - () => frappe.timeout(3), - () => { - assert.equal(cur_frm.doc.company, 'Test Company'); - }, - () => { - return frappe.tests.make('Restaurant', [ - // values to be set - {__newname: 'Test Restaurant 2'}, - {company: 'Test Company'}, - {invoice_series_prefix: 'Test-Rest-3-Inv-'}, - {default_customer: 'Test Customer 2'} - ]); - }, - () => frappe.timeout(3), - () => { - assert.equal(cur_frm.doc.company, 'Test Company'); - }, - () => done() - ]); -}); diff --git a/erpnext/restaurant/doctype/restaurant_menu/test_restaurant_menu.js b/erpnext/restaurant/doctype/restaurant_menu/test_restaurant_menu.js deleted file mode 100644 index f5ab9f09012..00000000000 --- a/erpnext/restaurant/doctype/restaurant_menu/test_restaurant_menu.js +++ /dev/null @@ -1,77 +0,0 @@ -/* eslint-disable */ -// rename this file from _test_[name] to test_[name] to activate -// and remove above this line - -QUnit.test("test: Restaurant Menu", function (assert) { - let done = assert.async(); - - let items = { - "Food Item 1": [ - {item_code: "Food Item 1"}, - {item_group: "Products"}, - {is_stock_item: 1}, - ], - "Food Item 2": [ - {item_code: "Food Item 2"}, - {item_group: "Products"}, - {is_stock_item: 1}, - ], - "Food Item 3": [ - {item_code: "Food Item 3"}, - {item_group: "Products"}, - {is_stock_item: 1}, - ] - }; - - - // number of asserts - assert.expect(0); - - frappe.run_serially([ - // insert a new Restaurant Menu - () => frappe.tests.setup_doctype('Item', items), - () => { - return frappe.tests.make("Restaurant Menu", [ - {__newname: 'Restaurant Menu 1'}, - {restaurant: "Test Restaurant 1"}, - {items: [ - [ - {"item": "Food Item 1"}, - {"rate": 100} - ], - [ - {"item": "Food Item 2"}, - {"rate": 90} - ], - [ - {"item": "Food Item 3"}, - {"rate": 80} - ] - ]} - ]); - }, - () => frappe.timeout(2), - () => { - return frappe.tests.make("Restaurant Menu", [ - {__newname: 'Restaurant Menu 2'}, - {restaurant: "Test Restaurant 2"}, - {items: [ - [ - {"item": "Food Item 1"}, - {"rate": 105} - ], - [ - {"item": "Food Item 3"}, - {"rate": 85} - ] - ]} - ]); - }, - () => frappe.timeout(2), - () => frappe.set_route('Form', 'Restaurant', 'Test Restaurant 1'), - () => cur_frm.set_value('active_menu', 'Restaurant Menu 1'), - () => cur_frm.save(), - () => done() - ]); - -}); diff --git a/erpnext/restaurant/doctype/restaurant_order_entry/test_restaurant_order_entry.js b/erpnext/restaurant/doctype/restaurant_order_entry/test_restaurant_order_entry.js deleted file mode 100644 index fec2a2153be..00000000000 --- a/erpnext/restaurant/doctype/restaurant_order_entry/test_restaurant_order_entry.js +++ /dev/null @@ -1,53 +0,0 @@ -/* eslint-disable */ -// rename this file from _test_[name] to test_[name] to activate -// and remove above this line - -QUnit.test("test: Restaurant Order Entry", function (assert) { - let done = assert.async(); - - // number of asserts - assert.expect(5); - - frappe.run_serially([ - // insert a new Restaurant Order Entry - () => frappe.set_route('Form', 'Restaurant Settings'), - () => cur_frm.set_value('default_customer', 'Test Customer 1'), - () => cur_frm.save(), - () => frappe.set_route('Form', 'Restaurant Order Entry'), - () => frappe.click_button('Clear'), - () => frappe.timeout(2), - () => cur_frm.set_value('restaurant_table', 'Test-Restaurant-1-01'), - () => cur_frm.set_value('add_item', 'Food Item 1'), - () => frappe.timeout(0.5), - () => { - var e = $.Event( "keyup", {which: 13} ); - $('input[data-fieldname="add_item"]').trigger(e); - return frappe.timeout(0.5); - }, - () => cur_frm.set_value('add_item', 'Food Item 1'), - () => { - var e = $.Event( "keyup", {which: 13} ); - $('input[data-fieldname="add_item"]').trigger(e); - return frappe.timeout(0.5); - }, - () => cur_frm.set_value('add_item', 'Food Item 2'), - () => { - var e = $.Event( "keyup", {which: 13} ); - $('input[data-fieldname="add_item"]').trigger(e); - return frappe.timeout(0.5); - }, - () => { - assert.equal(cur_frm.doc.items[0].item, 'Food Item 1'); - assert.equal(cur_frm.doc.items[0].qty, 2); - assert.equal(cur_frm.doc.items[1].item, 'Food Item 2'); - assert.equal(cur_frm.doc.items[1].qty, 1); - }, - () => frappe.click_button('Update'), - () => frappe.timeout(2), - () => { - assert.equal(cur_frm.doc.grand_total, 290); - } - () => done() - ]); - -}); diff --git a/erpnext/restaurant/doctype/restaurant_reservation/test_restaurant_reservation.js b/erpnext/restaurant/doctype/restaurant_reservation/test_restaurant_reservation.js deleted file mode 100644 index eeea5a9f0b2..00000000000 --- a/erpnext/restaurant/doctype/restaurant_reservation/test_restaurant_reservation.js +++ /dev/null @@ -1,27 +0,0 @@ -/* eslint-disable */ -// rename this file from _test_[name] to test_[name] to activate -// and remove above this line - -QUnit.test("test: Restaurant Reservation", function (assert) { - let done = assert.async(); - - // number of asserts - assert.expect(1); - - frappe.run_serially([ - // insert a new Restaurant Reservation - () => frappe.tests.make('Restaurant Reservation', [ - // values to be set - {restaurant: 'Gokul - JP Nagar'}, - {customer_name: 'test customer'}, - {reservation_time: frappe.datetime.now_date() + " 19:00:00"}, - {no_of_people: 4}, - ]), - () => { - assert.equal(cur_frm.doc.reservation_end_time, - frappe.datetime.now_date() + ' 20:00:00'); - }, - () => done() - ]); - -}); diff --git a/erpnext/restaurant/doctype/restaurant_table/test_restaurant_table.js b/erpnext/restaurant/doctype/restaurant_table/test_restaurant_table.js deleted file mode 100644 index 16035f0c892..00000000000 --- a/erpnext/restaurant/doctype/restaurant_table/test_restaurant_table.js +++ /dev/null @@ -1,41 +0,0 @@ -/* eslint-disable */ -// rename this file from _test_[name] to test_[name] to activate -// and remove above this line - -QUnit.test("test: Restaurant Table", function (assert) { - let done = assert.async(); - - // number of asserts - assert.expect(0); - - frappe.run_serially([ - // insert a new Restaurant Table - () => frappe.tests.make('Restaurant Table', [ - // values to be set - {restaurant: 'Test Restaurant 1'}, - {no_of_seats: 4}, - ]), - () => frappe.tests.make('Restaurant Table', [ - // values to be set - {restaurant: 'Test Restaurant 1'}, - {no_of_seats: 5}, - ]), - () => frappe.tests.make('Restaurant Table', [ - // values to be set - {restaurant: 'Test Restaurant 1'}, - {no_of_seats: 2}, - ]), - () => frappe.tests.make('Restaurant Table', [ - // values to be set - {restaurant: 'Test Restaurant 1'}, - {no_of_seats: 2}, - ]), - () => frappe.tests.make('Restaurant Table', [ - // values to be set - {restaurant: 'Test Restaurant 1'}, - {no_of_seats: 6}, - ]), - () => done() - ]); - -}); diff --git a/erpnext/selling/doctype/customer/customer.js b/erpnext/selling/doctype/customer/customer.js index 4b0bbd5a114..107e4a4759d 100644 --- a/erpnext/selling/doctype/customer/customer.js +++ b/erpnext/selling/doctype/customer/customer.js @@ -134,6 +134,12 @@ frappe.ui.form.on("Customer", { frm.trigger("get_customer_group_details"); }, __('Actions')); + if (cint(frappe.defaults.get_default("enable_common_party_accounting"))) { + frm.add_custom_button(__('Link with Supplier'), function () { + frm.trigger('show_party_link_dialog'); + }, __('Actions')); + } + // indicator erpnext.utils.set_party_dashboard_indicators(frm); @@ -158,5 +164,42 @@ frappe.ui.form.on("Customer", { } }); + }, + show_party_link_dialog: function(frm) { + const dialog = new frappe.ui.Dialog({ + title: __('Select a Supplier'), + fields: [{ + fieldtype: 'Link', label: __('Supplier'), + options: 'Supplier', fieldname: 'supplier', reqd: 1 + }], + primary_action: function({ supplier }) { + frappe.call({ + method: 'erpnext.accounts.doctype.party_link.party_link.create_party_link', + args: { + primary_role: 'Customer', + primary_party: frm.doc.name, + secondary_party: supplier + }, + freeze: true, + callback: function() { + dialog.hide(); + frappe.msgprint({ + message: __('Successfully linked to Supplier'), + alert: true + }); + }, + error: function() { + dialog.hide(); + frappe.msgprint({ + message: __('Linking to Supplier Failed. Please try again.'), + title: __('Linking Failed'), + indicator: 'red' + }); + } + }); + }, + primary_action_label: __('Create Link') + }); + dialog.show(); } }); diff --git a/erpnext/selling/doctype/customer/customer.py b/erpnext/selling/doctype/customer/customer.py index d4ad719534b..b7f74df105d 100644 --- a/erpnext/selling/doctype/customer/customer.py +++ b/erpnext/selling/doctype/customer/customer.py @@ -18,7 +18,11 @@ from frappe.model.rename_doc import update_linked_doctypes from frappe.utils import cint, cstr, flt, get_formatted_email, today from frappe.utils.user import get_users_with_role -from erpnext.accounts.party import get_dashboard_info, validate_party_accounts +from erpnext.accounts.party import ( # noqa + get_dashboard_info, + get_timeline_data, + validate_party_accounts, +) from erpnext.utilities.transaction_base import TransactionBase @@ -192,20 +196,19 @@ class Customer(TransactionBase): if not lead.lead_name: frappe.throw(_("Please mention the Lead Name in Lead {0}").format(self.lead_name)) - if lead.company_name: - contact_names = frappe.get_all('Dynamic Link', filters={ - "parenttype":"Contact", - "link_doctype":"Lead", - "link_name":self.lead_name - }, fields=["parent as name"]) + contact_names = frappe.get_all('Dynamic Link', filters={ + "parenttype":"Contact", + "link_doctype":"Lead", + "link_name":self.lead_name + }, fields=["parent as name"]) - for contact_name in contact_names: - contact = frappe.get_doc('Contact', contact_name.get('name')) - if not contact.has_link('Customer', self.name): - contact.append('links', dict(link_doctype='Customer', link_name=self.name)) - contact.save(ignore_permissions=self.flags.ignore_permissions) + for contact_name in contact_names: + contact = frappe.get_doc('Contact', contact_name.get('name')) + if not contact.has_link('Customer', self.name): + contact.append('links', dict(link_doctype='Customer', link_name=self.name)) + contact.save(ignore_permissions=self.flags.ignore_permissions) - else: + if not contact_names: lead.lead_name = lead.lead_name.lstrip().split(" ") lead.first_name = lead.lead_name[0] lead.last_name = " ".join(lead.lead_name[1:]) @@ -463,11 +466,14 @@ def get_customer_list(doctype, txt, searchfield, start, page_len, filters=None): def check_credit_limit(customer, company, ignore_outstanding_sales_order=False, extra_amount=0): + credit_limit = get_credit_limit(customer, company) + if not credit_limit: + return + customer_outstanding = get_customer_outstanding(customer, company, ignore_outstanding_sales_order) if extra_amount > 0: customer_outstanding += flt(extra_amount) - credit_limit = get_credit_limit(customer, company) if credit_limit > 0 and flt(customer_outstanding) > credit_limit: msgprint(_("Credit limit has been crossed for customer {0} ({1}/{2})") .format(customer, customer_outstanding, credit_limit)) diff --git a/erpnext/selling/doctype/customer/test_customer.py b/erpnext/selling/doctype/customer/test_customer.py index 7d6b74d0665..5301fd0524d 100644 --- a/erpnext/selling/doctype/customer/test_customer.py +++ b/erpnext/selling/doctype/customer/test_customer.py @@ -2,8 +2,6 @@ # License: GNU General Public License v3. See license.txt -import unittest - import frappe from frappe.test_runner import make_test_records from frappe.utils import flt @@ -11,7 +9,7 @@ from frappe.utils import flt from erpnext.accounts.party import get_due_date from erpnext.exceptions import PartyDisabled, PartyFrozen from erpnext.selling.doctype.customer.customer import get_credit_limit, get_customer_outstanding -from erpnext.tests.utils import create_test_contact_and_address +from erpnext.tests.utils import ERPNextTestCase, create_test_contact_and_address test_ignore = ["Price List"] test_dependencies = ['Payment Term', 'Payment Terms Template'] @@ -19,7 +17,7 @@ test_records = frappe.get_test_records('Customer') -class TestCustomer(unittest.TestCase): +class TestCustomer(ERPNextTestCase): def setUp(self): if not frappe.get_value('Item', '_Test Item'): make_test_records('Item') diff --git a/erpnext/selling/doctype/party_specific_item/test_party_specific_item.py b/erpnext/selling/doctype/party_specific_item/test_party_specific_item.py index 874a3645929..b951044f332 100644 --- a/erpnext/selling/doctype/party_specific_item/test_party_specific_item.py +++ b/erpnext/selling/doctype/party_specific_item/test_party_specific_item.py @@ -6,6 +6,7 @@ import unittest import frappe from erpnext.controllers.queries import item_query +from erpnext.tests.utils import ERPNextTestCase test_dependencies = ['Item', 'Customer', 'Supplier'] @@ -17,7 +18,7 @@ def create_party_specific_item(**args): psi.based_on_value = args.get('based_on_value') psi.insert() -class TestPartySpecificItem(unittest.TestCase): +class TestPartySpecificItem(ERPNextTestCase): def setUp(self): self.customer = frappe.get_last_doc("Customer") self.supplier = frappe.get_last_doc("Supplier") diff --git a/erpnext/selling/doctype/product_bundle/test_product_bundle.js b/erpnext/selling/doctype/product_bundle/test_product_bundle.js deleted file mode 100644 index 0dc90ec2114..00000000000 --- a/erpnext/selling/doctype/product_bundle/test_product_bundle.js +++ /dev/null @@ -1,35 +0,0 @@ -QUnit.test("test sales order", function(assert) { - assert.expect(4); - let done = assert.async(); - frappe.run_serially([ - () => { - return frappe.tests.make('Product Bundle', [ - {new_item_code: 'Computer'}, - {items: [ - [ - {item_code:'CPU'}, - {qty:1} - ], - [ - {item_code:'Screen'}, - {qty:1} - ], - [ - {item_code:'Keyboard'}, - {qty:1} - ] - ]}, - ]); - }, - () => cur_frm.save(), - () => { - // get_item_details - assert.ok(cur_frm.doc.items[0].item_code=='CPU', "Item Code correct"); - assert.ok(cur_frm.doc.items[1].item_code=='Screen', "Item Code correct"); - assert.ok(cur_frm.doc.items[2].item_code=='Keyboard', "Item Code correct"); - assert.ok(cur_frm.doc.new_item_code == "Computer", "Parent Item correct"); - }, - () => frappe.timeout(0.3), - () => done() - ]); -}); diff --git a/erpnext/selling/doctype/quotation/quotation.json b/erpnext/selling/doctype/quotation/quotation.json index ad788e5c8bf..ee5b0ea760a 100644 --- a/erpnext/selling/doctype/quotation/quotation.json +++ b/erpnext/selling/doctype/quotation/quotation.json @@ -961,9 +961,7 @@ "idx": 82, "is_submittable": 1, "links": [], - "max_attachments": 1, - "migration_hash": "75a86a19f062c2257bcbc8e6e31c7f1e", - "modified": "2021-10-21 12:58:55.514512", + "modified": "2021-11-30 01:33:21.106073", "modified_by": "Administrator", "module": "Selling", "name": "Quotation", diff --git a/erpnext/selling/doctype/quotation/test_quotation.py b/erpnext/selling/doctype/quotation/test_quotation.py index 769e0661b12..4357201d23d 100644 --- a/erpnext/selling/doctype/quotation/test_quotation.py +++ b/erpnext/selling/doctype/quotation/test_quotation.py @@ -1,15 +1,15 @@ # Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors # License: GNU General Public License v3. See license.txt -import unittest - import frappe from frappe.utils import add_days, add_months, flt, getdate, nowdate +from erpnext.tests.utils import ERPNextTestCase + test_dependencies = ["Product Bundle"] -class TestQuotation(unittest.TestCase): +class TestQuotation(ERPNextTestCase): def test_make_quotation_without_terms(self): quotation = make_quotation(do_not_save=1) self.assertFalse(quotation.get('payment_schedule')) @@ -302,6 +302,109 @@ class TestQuotation(unittest.TestCase): enable_calculate_bundle_price(enable=0) + def test_product_bundle_price_calculation_for_multiple_product_bundles_when_calculate_bundle_price_is_checked(self): + from erpnext.selling.doctype.product_bundle.test_product_bundle import make_product_bundle + from erpnext.stock.doctype.item.test_item import make_item + + make_item("_Test Product Bundle 1", {"is_stock_item": 0}) + make_item("_Test Product Bundle 2", {"is_stock_item": 0}) + make_item("_Test Bundle Item 1", {"is_stock_item": 1}) + make_item("_Test Bundle Item 2", {"is_stock_item": 1}) + make_item("_Test Bundle Item 3", {"is_stock_item": 1}) + + make_product_bundle("_Test Product Bundle 1", + ["_Test Bundle Item 1", "_Test Bundle Item 2"]) + make_product_bundle("_Test Product Bundle 2", + ["_Test Bundle Item 2", "_Test Bundle Item 3"]) + + enable_calculate_bundle_price() + + item_list = [ + { + "item_code": "_Test Product Bundle 1", + "warehouse": "", + "qty": 1, + "rate": 400, + "delivered_by_supplier": 1, + "supplier": '_Test Supplier' + }, + { + "item_code": "_Test Product Bundle 2", + "warehouse": "", + "qty": 1, + "rate": 400, + "delivered_by_supplier": 1, + "supplier": '_Test Supplier' + } + ] + + quotation = make_quotation(item_list=item_list, do_not_submit=1) + quotation.packed_items[0].rate = 100 + quotation.packed_items[1].rate = 200 + quotation.packed_items[2].rate = 200 + quotation.packed_items[3].rate = 300 + quotation.save() + + expected_values = [300, 500] + + for item in quotation.items: + self.assertEqual(item.amount, expected_values[item.idx-1]) + + enable_calculate_bundle_price(enable=0) + + def test_packed_items_indices_are_reset_when_product_bundle_is_deleted_from_items_table(self): + from erpnext.selling.doctype.product_bundle.test_product_bundle import make_product_bundle + from erpnext.stock.doctype.item.test_item import make_item + + make_item("_Test Product Bundle 1", {"is_stock_item": 0}) + make_item("_Test Product Bundle 2", {"is_stock_item": 0}) + make_item("_Test Product Bundle 3", {"is_stock_item": 0}) + make_item("_Test Bundle Item 1", {"is_stock_item": 1}) + make_item("_Test Bundle Item 2", {"is_stock_item": 1}) + make_item("_Test Bundle Item 3", {"is_stock_item": 1}) + + make_product_bundle("_Test Product Bundle 1", + ["_Test Bundle Item 1", "_Test Bundle Item 2"]) + make_product_bundle("_Test Product Bundle 2", + ["_Test Bundle Item 2", "_Test Bundle Item 3"]) + make_product_bundle("_Test Product Bundle 3", + ["_Test Bundle Item 3", "_Test Bundle Item 1"]) + + item_list = [ + { + "item_code": "_Test Product Bundle 1", + "warehouse": "", + "qty": 1, + "rate": 400, + "delivered_by_supplier": 1, + "supplier": '_Test Supplier' + }, + { + "item_code": "_Test Product Bundle 2", + "warehouse": "", + "qty": 1, + "rate": 400, + "delivered_by_supplier": 1, + "supplier": '_Test Supplier' + }, + { + "item_code": "_Test Product Bundle 3", + "warehouse": "", + "qty": 1, + "rate": 400, + "delivered_by_supplier": 1, + "supplier": '_Test Supplier' + } + ] + + quotation = make_quotation(item_list=item_list, do_not_submit=1) + del quotation.items[1] + quotation.save() + + for id, item in enumerate(quotation.packed_items): + expected_index = id + 1 + self.assertEqual(item.idx, expected_index) + test_records = frappe.get_test_records('Quotation') def enable_calculate_bundle_price(enable=1): diff --git a/erpnext/selling/doctype/quotation/tests/test_quotation.js b/erpnext/selling/doctype/quotation/tests/test_quotation.js deleted file mode 100644 index ad942fe4976..00000000000 --- a/erpnext/selling/doctype/quotation/tests/test_quotation.js +++ /dev/null @@ -1,58 +0,0 @@ -QUnit.test("test: quotation", function (assert) { - assert.expect(12); - let done = assert.async(); - frappe.run_serially([ - () => { - return frappe.tests.make("Quotation", [ - {customer: "Test Customer 1"}, - {items: [ - [ - {"item_code": "Test Product 1"}, - {"qty": 5} - ]] - }, - {payment_terms_template: '_Test Payment Term Template UI'} - ]); - }, - () => { - // get_item_details - assert.ok(cur_frm.doc.items[0].item_name == "Test Product 1", "Added Test Product 1"); - - // calculate_taxes_and_totals - assert.ok(cur_frm.doc.grand_total === 500, String(cur_frm.doc.grand_total)); - }, - () => cur_frm.set_value("customer_address", "Test1-Billing"), - () => cur_frm.set_value("shipping_address_name", "Test1-Warehouse"), - () => cur_frm.set_value("contact_person", "Contact 1-Test Customer 1"), - () => cur_frm.set_value("currency", "USD"), - () => frappe.timeout(0.3), - () => cur_frm.set_value("selling_price_list", "Test-Selling-USD"), - () => frappe.timeout(0.5), - () => cur_frm.doc.items[0].rate = 200, - () => frappe.timeout(0.3), - () => cur_frm.set_value("tc_name", "Test Term 1"), - () => cur_frm.set_value("payment_schedule", []), - () => frappe.timeout(0.5), - () => cur_frm.save(), - () => { - // Check Address and Contact Info - assert.ok(cur_frm.doc.address_display.includes("Billing Street 1"), "Address Changed"); - assert.ok(cur_frm.doc.shipping_address.includes("Warehouse Street 1"), "Address Changed"); - assert.ok(cur_frm.doc.contact_display == "Contact 1", "Contact info changed"); - - // Check Currency - assert.ok(cur_frm.doc.currency == "USD", "Currency Changed"); - assert.ok(cur_frm.doc.selling_price_list == "Test-Selling-USD", "Price List Changed"); - assert.ok(cur_frm.doc.items[0].rate == 200, "Price Changed Manually"); - assert.equal(cur_frm.doc.total, 1000, "New Total Calculated"); - - // Check Terms and Conditions - assert.ok(cur_frm.doc.tc_name == "Test Term 1", "Terms and Conditions Checked"); - - assert.ok(cur_frm.doc.payment_terms_template, "Payment Terms Template is correct"); - assert.ok(cur_frm.doc.payment_schedule.length > 0, "Payment Term Schedule is not empty"); - - }, - () => done() - ]); -}); diff --git a/erpnext/selling/doctype/quotation/tests/test_quotation_submit_cancel_amend.js b/erpnext/selling/doctype/quotation/tests/test_quotation_submit_cancel_amend.js deleted file mode 100644 index 26a099e4d6d..00000000000 --- a/erpnext/selling/doctype/quotation/tests/test_quotation_submit_cancel_amend.js +++ /dev/null @@ -1,41 +0,0 @@ -QUnit.module('Quotation'); - -QUnit.test("test quotation submit cancel amend", function(assert) { - assert.expect(2); - let done = assert.async(); - frappe.run_serially([ - () => { - return frappe.tests.make('Quotation', [ - {customer: 'Test Customer 1'}, - {items: [ - [ - {'delivery_date': frappe.datetime.add_days(frappe.defaults.get_default("year_end_date"), 1)}, - {'qty': 5}, - {'item_code': 'Test Product 1'} - ] - ]}, - {customer_address: 'Test1-Billing'}, - {shipping_address_name: 'Test1-Shipping'}, - {contact_person: 'Contact 1-Test Customer 1'} - ]); - }, - () => cur_frm.save(), - () => { - // get_item_details - assert.ok(cur_frm.doc.items[0].item_name=='Test Product 1', "Item name correct"); - // get uom details - assert.ok(cur_frm.doc.grand_total== 500, "Grand total correct "); - - }, - () => frappe.tests.click_button('Submit'), - () => frappe.tests.click_button('Yes'), - () => frappe.timeout(1), - () => frappe.tests.click_button('Close'), - () => frappe.tests.click_button('Cancel'), - () => frappe.tests.click_button('Yes'), - () => frappe.timeout(0.5), - () => frappe.tests.click_button('Amend'), - () => cur_frm.save(), - () => done() - ]); -}); diff --git a/erpnext/selling/doctype/quotation/tests/test_quotation_with_discount_on_grand_total.js b/erpnext/selling/doctype/quotation/tests/test_quotation_with_discount_on_grand_total.js deleted file mode 100644 index b59bb0510e8..00000000000 --- a/erpnext/selling/doctype/quotation/tests/test_quotation_with_discount_on_grand_total.js +++ /dev/null @@ -1,43 +0,0 @@ -QUnit.module('Quotation'); - -QUnit.test("test quotation with additional discount in grand total", function(assert) { - assert.expect(2); - let done = assert.async(); - frappe.run_serially([ - () => { - return frappe.tests.make('Quotation', [ - {customer: 'Test Customer 1'}, - {items: [ - [ - {'delivery_date': frappe.datetime.add_days(frappe.defaults.get_default("year_end_date"), 1)}, - {'qty': 5}, - {'item_code': 'Test Product 4'}, - ] - ]}, - {customer_address: 'Test1-Billing'}, - {shipping_address_name: 'Test1-Shipping'}, - {contact_person: 'Contact 1-Test Customer 1'}, - {payment_terms_template: '_Test Payment Term Template UI'} - ]); - }, - () => { - return frappe.tests.set_form_values(cur_frm, [ - {apply_discount_on:'Grand Total'}, - {additional_discount_percentage:10}, - {payment_schedule: []} - ]); - }, - () => cur_frm.save(), - () => { - // get_item_details - assert.ok(cur_frm.doc.items[0].item_name=='Test Product 4', "Item name correct"); - // get grand_total details - assert.ok(cur_frm.doc.grand_total== 450, "Grand total correct "); - - }, - () => frappe.tests.click_button('Submit'), - () => frappe.tests.click_button('Yes'), - () => frappe.timeout(0.3), - () => done() - ]); -}); diff --git a/erpnext/selling/doctype/quotation/tests/test_quotation_with_item_wise_discount.js b/erpnext/selling/doctype/quotation/tests/test_quotation_with_item_wise_discount.js deleted file mode 100644 index f5172fbae2e..00000000000 --- a/erpnext/selling/doctype/quotation/tests/test_quotation_with_item_wise_discount.js +++ /dev/null @@ -1,37 +0,0 @@ -QUnit.module('Quotation'); - -QUnit.test("test quotation with item wise discount", function(assert) { - assert.expect(2); - let done = assert.async(); - frappe.run_serially([ - () => { - return frappe.tests.make('Quotation', [ - {customer: 'Test Customer 1'}, - {items: [ - [ - {'delivery_date': frappe.datetime.add_days(frappe.defaults.get_default("year_end_date"), 1)}, - {'qty': 5}, - {'item_code': 'Test Product 4'}, - {'discount_percentage': 10}, - {'margin_type': 'Percentage'} - ] - ]}, - {customer_address: 'Test1-Billing'}, - {shipping_address_name: 'Test1-Shipping'}, - {contact_person: 'Contact 1-Test Customer 1'} - ]); - }, - () => cur_frm.save(), - () => { - // get_item_details - assert.ok(cur_frm.doc.items[0].item_name=='Test Product 4', "Item name correct"); - // get grand_total details - assert.ok(cur_frm.doc.grand_total== 450, "Grand total correct "); - - }, - () => frappe.tests.click_button('Submit'), - () => frappe.tests.click_button('Yes'), - () => frappe.timeout(0.3), - () => done() - ]); -}); diff --git a/erpnext/selling/doctype/quotation/tests/test_quotation_with_margin.js b/erpnext/selling/doctype/quotation/tests/test_quotation_with_margin.js deleted file mode 100644 index 0d340997ad9..00000000000 --- a/erpnext/selling/doctype/quotation/tests/test_quotation_with_margin.js +++ /dev/null @@ -1,35 +0,0 @@ -QUnit.module('Selling'); - -QUnit.test("test quotation with margin", function(assert) { - assert.expect(3); - let done = assert.async(); - frappe.run_serially([ - () => { - return frappe.tests.make('Quotation', [ - {customer: 'Test Customer 1'}, - {selling_price_list: 'Test-Selling-USD'}, - {currency: 'USD'}, - {items: [ - [ - {'item_code': 'Test Product 4'}, - {'delivery_date': frappe.datetime.add_days(frappe.defaults.get_default("year_end_date"), 1)}, - {'qty': 1}, - {'margin_type': 'Percentage'}, - {'margin_rate_or_amount': 20} - ] - ]} - ]); - }, - () => cur_frm.save(), - () => { - assert.ok(cur_frm.doc.items[0].rate_with_margin == 240, "Margin rate correct"); - assert.ok(cur_frm.doc.items[0].base_rate_with_margin == cur_frm.doc.conversion_rate * 240, "Base margin rate correct"); - assert.ok(cur_frm.doc.total == 240, "Amount correct"); - - }, - () => frappe.tests.click_button('Submit'), - () => frappe.tests.click_button('Yes'), - () => frappe.timeout(0.3), - () => done() - ]); -}); diff --git a/erpnext/selling/doctype/quotation/tests/test_quotation_with_multi_uom.js b/erpnext/selling/doctype/quotation/tests/test_quotation_with_multi_uom.js deleted file mode 100644 index 84be56f4605..00000000000 --- a/erpnext/selling/doctype/quotation/tests/test_quotation_with_multi_uom.js +++ /dev/null @@ -1,38 +0,0 @@ -QUnit.module('Quotation'); - -QUnit.test("test quotation with multi uom", function(assert) { - assert.expect(3); - let done = assert.async(); - frappe.run_serially([ - () => { - return frappe.tests.make('Quotation', [ - {customer: 'Test Customer 1'}, - {items: [ - [ - {'delivery_date': frappe.datetime.add_days(frappe.defaults.get_default("year_end_date"), 1)}, - {'qty': 5}, - {'item_code': 'Test Product 4'}, - {'uom': 'unit'}, - ] - ]}, - {customer_address: 'Test1-Billing'}, - {shipping_address_name: 'Test1-Shipping'}, - {contact_person: 'Contact 1-Test Customer 1'} - ]); - }, - () => cur_frm.save(), - () => { - // get_item_details - assert.ok(cur_frm.doc.items[0].item_name=='Test Product 4', "Item name correct"); - // get uom details - assert.ok(cur_frm.doc.items[0].uom=='Unit', "Multi Uom correct"); - // get grand_total details - assert.ok(cur_frm.doc.grand_total== 5000, "Grand total correct "); - - }, - () => frappe.tests.click_button('Submit'), - () => frappe.tests.click_button('Yes'), - () => frappe.timeout(0.3), - () => done() - ]); -}); diff --git a/erpnext/selling/doctype/quotation/tests/test_quotation_with_shipping_rule.js b/erpnext/selling/doctype/quotation/tests/test_quotation_with_shipping_rule.js deleted file mode 100644 index 17c5dd2b34d..00000000000 --- a/erpnext/selling/doctype/quotation/tests/test_quotation_with_shipping_rule.js +++ /dev/null @@ -1,35 +0,0 @@ -QUnit.module('Quotation'); - -QUnit.test("test quotation with shipping rule", function(assert) { - assert.expect(2); - let done = assert.async(); - frappe.run_serially([ - () => { - return frappe.tests.make('Quotation', [ - {customer: 'Test Customer 1'}, - {items: [ - [ - {'delivery_date': frappe.datetime.add_days(frappe.defaults.get_default("year_end_date"), 1)}, - {'qty': 5}, - {'item_code': 'Test Product 4'}, - ] - ]}, - {customer_address: 'Test1-Billing'}, - {shipping_address_name: 'Test1-Shipping'}, - {contact_person: 'Contact 1-Test Customer 1'}, - {shipping_rule:'Next Day Shipping'} - ]); - }, - () => cur_frm.save(), - () => { - // get_item_details - assert.ok(cur_frm.doc.items[0].item_name=='Test Product 4', "Item name correct"); - // get grand_total details - assert.ok(cur_frm.doc.grand_total== 550, "Grand total correct "); - }, - () => frappe.tests.click_button('Submit'), - () => frappe.tests.click_button('Yes'), - () => frappe.timeout(0.3), - () => done() - ]); -}); diff --git a/erpnext/selling/doctype/quotation/tests/test_quotation_with_taxes_and_charges.js b/erpnext/selling/doctype/quotation/tests/test_quotation_with_taxes_and_charges.js deleted file mode 100644 index 5e21f817573..00000000000 --- a/erpnext/selling/doctype/quotation/tests/test_quotation_with_taxes_and_charges.js +++ /dev/null @@ -1,40 +0,0 @@ -QUnit.module('Quotation'); - -QUnit.test("test quotation with taxes and charges", function(assert) { - assert.expect(3); - let done = assert.async(); - frappe.run_serially([ - () => { - return frappe.tests.make('Quotation', [ - {customer: 'Test Customer 1'}, - {items: [ - [ - {'delivery_date': frappe.datetime.add_days(frappe.defaults.get_default("year_end_date"), 1)}, - {'qty': 5}, - {'item_code': 'Test Product 4'}, - ] - ]}, - {customer_address: 'Test1-Billing'}, - {shipping_address_name: 'Test1-Shipping'}, - {contact_person: 'Contact 1-Test Customer 1'}, - {taxes_and_charges: 'TEST In State GST - FT'}, - {tc_name: 'Test Term 1'}, - {terms: 'This is Test'} - ]); - }, - () => cur_frm.save(), - () => { - // get_item_details - assert.ok(cur_frm.doc.items[0].item_name=='Test Product 4', "Item name correct"); - // get tax details - assert.ok(cur_frm.doc.taxes_and_charges=='TEST In State GST - FT', "Tax details correct"); - // get tax account head details - assert.ok(cur_frm.doc.taxes[0].account_head=='CGST - '+frappe.get_abbr(frappe.defaults.get_default('Company')), " Account Head abbr correct"); - - }, - () => frappe.tests.click_button('Submit'), - () => frappe.tests.click_button('Yes'), - () => frappe.timeout(0.3), - () => done() - ]); -}); diff --git a/erpnext/selling/doctype/sales_order/sales_order.js b/erpnext/selling/doctype/sales_order/sales_order.js index d46c46f90e6..79e9e17e414 100644 --- a/erpnext/selling/doctype/sales_order/sales_order.js +++ b/erpnext/selling/doctype/sales_order/sales_order.js @@ -319,7 +319,7 @@ erpnext.selling.SalesOrderController = class SalesOrderController extends erpnex title: __('Select Items to Manufacture'), fields: fields, primary_action: function() { - var data = d.get_values(); + var data = {items: d.fields_dict.items.grid.get_selected_children()}; me.frm.call({ method: 'make_work_orders', args: { diff --git a/erpnext/selling/doctype/sales_order/sales_order.json b/erpnext/selling/doctype/sales_order/sales_order.json index 7c7ed9a9604..7e99a062439 100644 --- a/erpnext/selling/doctype/sales_order/sales_order.json +++ b/erpnext/selling/doctype/sales_order/sales_order.json @@ -134,6 +134,7 @@ "sales_team_section_break", "sales_partner", "column_break7", + "amount_eligible_for_commission", "commission_rate", "total_commission", "section_break1", @@ -1507,16 +1508,23 @@ "fieldtype": "Small Text", "label": "Dispatch Address", "read_only": 1 + }, + { + "fieldname": "amount_eligible_for_commission", + "fieldtype": "Currency", + "label": "Amount Eligible for Commission", + "read_only": 1 } ], "icon": "fa fa-file-text", "idx": 105, "is_submittable": 1, "links": [], - "modified": "2021-09-28 13:09:51.515542", + "modified": "2021-10-05 12:16:40.775704", "modified_by": "Administrator", "module": "Selling", "name": "Sales Order", + "naming_rule": "By \"Naming Series\" field", "owner": "Administrator", "permissions": [ { diff --git a/erpnext/selling/doctype/sales_order/sales_order.py b/erpnext/selling/doctype/sales_order/sales_order.py index 8cb0f29e9b5..cc951850a4a 100755 --- a/erpnext/selling/doctype/sales_order/sales_order.py +++ b/erpnext/selling/doctype/sales_order/sales_order.py @@ -63,6 +63,8 @@ class SalesOrder(SellingController): if not self.billing_status: self.billing_status = 'Not Billed' if not self.delivery_status: self.delivery_status = 'Not Delivered' + self.reset_default_field_value("set_warehouse", "items", "warehouse") + def validate_po(self): # validate p.o date v/s delivery date if self.po_date and not self.skip_delivery_note: @@ -223,60 +225,15 @@ class SalesOrder(SellingController): check_credit_limit(self.customer, self.company) def check_nextdoc_docstatus(self): - # Checks Delivery Note - submit_dn = frappe.db.sql_list(""" - select t1.name - from `tabDelivery Note` t1,`tabDelivery Note Item` t2 - where t1.name = t2.parent and t2.against_sales_order = %s and t1.docstatus = 1""", self.name) - - if submit_dn: - submit_dn = [get_link_to_form("Delivery Note", dn) for dn in submit_dn] - frappe.throw(_("Delivery Notes {0} must be cancelled before cancelling this Sales Order") - .format(", ".join(submit_dn))) - - # Checks Sales Invoice - submit_rv = frappe.db.sql_list("""select t1.name + linked_invoices = frappe.db.sql_list("""select distinct t1.name from `tabSales Invoice` t1,`tabSales Invoice Item` t2 - where t1.name = t2.parent and t2.sales_order = %s and t1.docstatus < 2""", + where t1.name = t2.parent and t2.sales_order = %s and t1.docstatus = 0""", self.name) - if submit_rv: - submit_rv = [get_link_to_form("Sales Invoice", si) for si in submit_rv] - frappe.throw(_("Sales Invoice {0} must be cancelled before cancelling this Sales Order") - .format(", ".join(submit_rv))) - - #check maintenance schedule - submit_ms = frappe.db.sql_list(""" - select t1.name - from `tabMaintenance Schedule` t1, `tabMaintenance Schedule Item` t2 - where t2.parent=t1.name and t2.sales_order = %s and t1.docstatus = 1""", self.name) - - if submit_ms: - submit_ms = [get_link_to_form("Maintenance Schedule", ms) for ms in submit_ms] - frappe.throw(_("Maintenance Schedule {0} must be cancelled before cancelling this Sales Order") - .format(", ".join(submit_ms))) - - # check maintenance visit - submit_mv = frappe.db.sql_list(""" - select t1.name - from `tabMaintenance Visit` t1, `tabMaintenance Visit Purpose` t2 - where t2.parent=t1.name and t2.prevdoc_docname = %s and t1.docstatus = 1""",self.name) - - if submit_mv: - submit_mv = [get_link_to_form("Maintenance Visit", mv) for mv in submit_mv] - frappe.throw(_("Maintenance Visit {0} must be cancelled before cancelling this Sales Order") - .format(", ".join(submit_mv))) - - # check work order - pro_order = frappe.db.sql_list(""" - select name - from `tabWork Order` - where sales_order = %s and docstatus = 1""", self.name) - - if pro_order: - pro_order = [get_link_to_form("Work Order", po) for po in pro_order] - frappe.throw(_("Work Order {0} must be cancelled before cancelling this Sales Order") - .format(", ".join(pro_order))) + if linked_invoices: + linked_invoices = [get_link_to_form("Sales Invoice", si) for si in linked_invoices] + frappe.throw(_("Sales Invoice {0} must be deleted before cancelling this Sales Order") + .format(", ".join(linked_invoices))) def check_modified_date(self): mod_db = frappe.db.get_value("Sales Order", self.name, "modified") @@ -970,6 +927,7 @@ def make_purchase_order(source_name, selected_items=None, target_doc=None): "supplier", "pricing_rules" ], + "condition": lambda doc: doc.parent_item in items_to_map } }, target_doc, set_missing_values) @@ -1022,6 +980,7 @@ def make_work_orders(items, sales_order, company, project=None): description=i['description'] )).insert() work_order.set_work_order_operations() + work_order.flags.ignore_mandatory = True work_order.save() out.append(work_order) diff --git a/erpnext/selling/doctype/sales_order/test_sales_order.py b/erpnext/selling/doctype/sales_order/test_sales_order.py index 644c0ac905d..42bc0b70f8e 100644 --- a/erpnext/selling/doctype/sales_order/test_sales_order.py +++ b/erpnext/selling/doctype/sales_order/test_sales_order.py @@ -2,7 +2,6 @@ # License: GNU General Public License v3. See license.txt import json -import unittest import frappe import frappe.permissions @@ -10,6 +9,12 @@ from frappe.core.doctype.user_permission.test_user_permission import create_user from frappe.utils import add_days, flt, getdate, nowdate from erpnext.controllers.accounts_controller import update_child_qty_rate +from erpnext.maintenance.doctype.maintenance_schedule.test_maintenance_schedule import ( + make_maintenance_schedule, +) +from erpnext.maintenance.doctype.maintenance_visit.test_maintenance_visit import ( + make_maintenance_visit, +) from erpnext.manufacturing.doctype.blanket_order.test_blanket_order import make_blanket_order from erpnext.selling.doctype.product_bundle.test_product_bundle import make_product_bundle from erpnext.selling.doctype.sales_order.sales_order import ( @@ -22,12 +27,14 @@ from erpnext.selling.doctype.sales_order.sales_order import ( ) from erpnext.stock.doctype.item.test_item import make_item from erpnext.stock.doctype.stock_entry.stock_entry_utils import make_stock_entry +from erpnext.tests.utils import ERPNextTestCase -class TestSalesOrder(unittest.TestCase): +class TestSalesOrder(ERPNextTestCase): @classmethod def setUpClass(cls): + super().setUpClass() cls.unlink_setting = int(frappe.db.get_value("Accounts Settings", "Accounts Settings", "unlink_advance_payment_on_cancelation_of_order")) @@ -36,6 +43,7 @@ class TestSalesOrder(unittest.TestCase): # reset config to previous state frappe.db.set_value("Accounts Settings", "Accounts Settings", "unlink_advance_payment_on_cancelation_of_order", cls.unlink_setting) + super().tearDownClass() def tearDown(self): frappe.set_user("Administrator") @@ -1278,6 +1286,72 @@ class TestSalesOrder(unittest.TestCase): self.assertRaises(frappe.ValidationError, so.cancel) + def test_so_cancellation_after_si_submission(self): + """ + Test to check if Sales Order gets cancelled when linked Sales Invoice has been Submitted + Expected result: Sales Order should not get cancelled + """ + so = make_sales_order() + so.submit() + si = make_sales_invoice(so.name) + si.submit() + + so.load_from_db() + self.assertRaises(frappe.LinkExistsError, so.cancel) + + def test_so_cancellation_after_dn_submission(self): + """ + Test to check if Sales Order gets cancelled when linked Delivery Note has been Submitted + Expected result: Sales Order should not get cancelled + """ + so = make_sales_order() + so.submit() + dn = make_delivery_note(so.name) + dn.submit() + + so.load_from_db() + self.assertRaises(frappe.LinkExistsError, so.cancel) + + def test_so_cancellation_after_maintenance_schedule_submission(self): + """ + Expected result: Sales Order should not get cancelled + """ + so = make_sales_order() + so.submit() + ms = make_maintenance_schedule() + ms.items[0].sales_order = so.name + ms.submit() + + so.load_from_db() + self.assertRaises(frappe.LinkExistsError, so.cancel) + + def test_so_cancellation_after_maintenance_visit_submission(self): + """ + Expected result: Sales Order should not get cancelled + """ + so = make_sales_order() + so.submit() + mv = make_maintenance_visit() + mv.purposes[0].prevdoc_doctype = "Sales Order" + mv.purposes[0].prevdoc_docname = so.name + mv.submit() + + so.load_from_db() + self.assertRaises(frappe.LinkExistsError, so.cancel) + + def test_so_cancellation_after_work_order_submission(self): + """ + Expected result: Sales Order should not get cancelled + """ + from erpnext.manufacturing.doctype.work_order.test_work_order import make_wo_order_test_record + + so = make_sales_order(item_code="_Test FG Item", qty=10) + so.submit() + make_wo_order_test_record(sales_order=so.name) + + so.load_from_db() + self.assertRaises(frappe.LinkExistsError, so.cancel) + def test_payment_terms_are_fetched_when_creating_sales_invoice(self): from erpnext.accounts.doctype.payment_entry.test_payment_entry import ( create_payment_terms_template, diff --git a/erpnext/selling/doctype/sales_order/tests/test_sales_order.js b/erpnext/selling/doctype/sales_order/tests/test_sales_order.js deleted file mode 100644 index c99f9ef2a98..00000000000 --- a/erpnext/selling/doctype/sales_order/tests/test_sales_order.js +++ /dev/null @@ -1,68 +0,0 @@ -QUnit.module('Sales Order'); - -QUnit.test("test sales order", function(assert) { - assert.expect(12); - let done = assert.async(); - frappe.run_serially([ - () => { - return frappe.tests.make('Sales Order', [ - {customer: 'Test Customer 1'}, - {items: [ - [ - {'delivery_date': frappe.datetime.add_days(frappe.defaults.get_default("year_end_date"), 1)}, - {'qty': 5.123}, - {'item_code': 'Test Product 3'}, - ] - ]}, - {customer_address: 'Test1-Billing'}, - {shipping_address_name: 'Test1-Shipping'}, - {contact_person: 'Contact 1-Test Customer 1'}, - {taxes_and_charges: 'TEST In State GST - FT'}, - {tc_name: 'Test Term 1'}, - {terms: 'This is Test'}, - {payment_terms_template: '_Test Payment Term Template UI'} - ]); - }, - () => { - return frappe.tests.set_form_values(cur_frm, [ - {selling_price_list:'Test-Selling-USD'}, - {currency: 'USD'} - ]); - }, - () => frappe.timeout(1.5), - () => { - // get_item_details - assert.ok(cur_frm.doc.items[0].item_name=='Test Product 3', "Item name correct"); - // get tax details - assert.ok(cur_frm.doc.taxes_and_charges=='TEST In State GST - FT', "Tax details correct"); - // get tax account head details - assert.ok(cur_frm.doc.taxes[0].account_head=='CGST - '+frappe.get_abbr(frappe.defaults.get_default('Company')), " Account Head abbr correct"); - }, - () => cur_frm.save(), - () => frappe.timeout(1), - () => cur_frm.print_doc(), - () => frappe.timeout(1), - () => { - // Payment Terms - assert.ok(cur_frm.doc.payment_terms_template, "Payment Terms Template is correct"); - assert.ok(cur_frm.doc.payment_schedule.length > 0, "Payment Term Schedule is not empty"); - - // totals - assert.ok(cur_frm.doc.items[0].price_list_rate==250, "Item 1 price_list_rate"); - assert.ok(cur_frm.doc.net_total== 1280.75, "net total correct "); - assert.ok(cur_frm.doc.base_grand_total== flt(1511.29* cur_frm.doc.conversion_rate, precision('base_grand_total')), String(flt(1511.29* cur_frm.doc.conversion_rate, precision('base_grand_total')) + ' ' + cur_frm.doc.base_grand_total)); - assert.ok(cur_frm.doc.grand_total== 1511.29 , "grand total correct "); - assert.ok(cur_frm.doc.rounded_total== 1511.30, "rounded total correct "); - - // print format - assert.ok($('.btn-print-print').is(':visible'), "Print Format Available"); - frappe.timeout(1); - assert.ok($(".section-break+ .section-break .column-break:nth-child(1) .data-field:nth-child(1) .value").text().includes("Billing Street 1"), "Print Preview Works As Expected"); - }, - () => cur_frm.print_doc(), - () => frappe.tests.click_button('Submit'), - () => frappe.tests.click_button('Yes'), - () => frappe.timeout(0.3), - () => done() - ]); -}); diff --git a/erpnext/selling/doctype/sales_order/tests/test_sales_order_with_bypass_credit_limit_check.js b/erpnext/selling/doctype/sales_order/tests/test_sales_order_with_bypass_credit_limit_check.js deleted file mode 100644 index 79d798b9443..00000000000 --- a/erpnext/selling/doctype/sales_order/tests/test_sales_order_with_bypass_credit_limit_check.js +++ /dev/null @@ -1,58 +0,0 @@ -QUnit.module('Sales Order'); - -QUnit.test("test_sales_order_with_bypass_credit_limit_check", function(assert) { -//#PR : 10861, Author : ashish-greycube & jigneshpshah, Email:mr.ashish.shah@gmail.com - assert.expect(2); - let done = assert.async(); - frappe.run_serially([ - () => frappe.new_doc('Customer'), - () => frappe.timeout(1), - () => frappe.quick_entry.dialog.$wrapper.find('.edit-full').click(), - () => frappe.timeout(1), - () => cur_frm.set_value("customer_name", "Test Customer 10"), - () => cur_frm.add_child('credit_limits', { - 'company': cur_frm.doc.company || '_Test Company' - 'credit_limit': 1000, - 'bypass_credit_limit_check': 1}), - // save form - () => cur_frm.save(), - () => frappe.timeout(1), - - () => frappe.new_doc('Item'), - () => frappe.timeout(1), - () => frappe.quick_entry.dialog.$wrapper.find('.edit-full').click(), - () => frappe.timeout(1), - () => cur_frm.set_value("item_code", "Test Product 10"), - () => cur_frm.set_value("item_group", "Products"), - () => cur_frm.set_value("standard_rate", 100), - // save form - () => cur_frm.save(), - () => frappe.timeout(1), - - () => { - return frappe.tests.make('Sales Order', [ - {customer: 'Test Customer 5'}, - {items: [ - [ - {'delivery_date': frappe.datetime.add_days(frappe.defaults.get_default("year_end_date"), 1)}, - {'qty': 5}, - {'item_code': 'Test Product 10'}, - ] - ]} - - ]); - }, - () => cur_frm.save(), - () => frappe.tests.click_button('Submit'), - () => assert.equal("Confirm", cur_dialog.title,'confirmation for submit'), - () => frappe.tests.click_button('Yes'), - () => frappe.timeout(3), - () => { - - assert.ok(cur_frm.doc.status=="To Deliver and Bill", "It is submited. Credit limit is NOT checked for sales order"); - - - }, - () => done() - ]); -}); diff --git a/erpnext/selling/doctype/sales_order/tests/test_sales_order_with_discount_on_grand_total.js b/erpnext/selling/doctype/sales_order/tests/test_sales_order_with_discount_on_grand_total.js deleted file mode 100644 index de61a6112c1..00000000000 --- a/erpnext/selling/doctype/sales_order/tests/test_sales_order_with_discount_on_grand_total.js +++ /dev/null @@ -1,43 +0,0 @@ -QUnit.module('Sales Order'); - -QUnit.test("test sales order with additional discount in grand total", function(assert) { - assert.expect(2); - let done = assert.async(); - frappe.run_serially([ - () => { - return frappe.tests.make('Sales Order', [ - {customer: 'Test Customer 1'}, - {items: [ - [ - {'delivery_date': frappe.datetime.add_days(frappe.defaults.get_default("year_end_date"), 1)}, - {'qty': 5}, - {'item_code': 'Test Product 4'}, - ] - ]}, - {customer_address: 'Test1-Billing'}, - {shipping_address_name: 'Test1-Shipping'}, - {contact_person: 'Contact 1-Test Customer 1'}, - {payment_terms_template: '_Test Payment Term Template UI'} - ]); - }, - () => { - return frappe.tests.set_form_values(cur_frm, [ - {apply_discount_on:'Grand Total'}, - {additional_discount_percentage:10}, - {payment_schedule: []} - ]); - }, - () => cur_frm.save(), - () => { - // get_item_details - assert.ok(cur_frm.doc.items[0].item_name=='Test Product 4', "Item name correct"); - // get grand_total details - assert.ok(cur_frm.doc.grand_total== 450, "Grand total correct "); - - }, - () => frappe.tests.click_button('Submit'), - () => frappe.tests.click_button('Yes'), - () => frappe.timeout(0.3), - () => done() - ]); -}); diff --git a/erpnext/selling/doctype/sales_order/tests/test_sales_order_with_item_wise_discount.js b/erpnext/selling/doctype/sales_order/tests/test_sales_order_with_item_wise_discount.js deleted file mode 100644 index 2c481083086..00000000000 --- a/erpnext/selling/doctype/sales_order/tests/test_sales_order_with_item_wise_discount.js +++ /dev/null @@ -1,38 +0,0 @@ -QUnit.module('Sales Order'); - -QUnit.test("test sales order", function(assert) { - assert.expect(2); - let done = assert.async(); - frappe.run_serially([ - () => { - return frappe.tests.make('Sales Order', [ - {customer: 'Test Customer 1'}, - {items: [ - [ - {'delivery_date': frappe.datetime.add_days(frappe.defaults.get_default("year_end_date"), 1)}, - {'qty': 5}, - {'item_code': 'Test Product 4'}, - {'discount_percentage': 10}, - {'margin_type': 'Percentage'} - ] - ]}, - {customer_address: 'Test1-Billing'}, - {shipping_address_name: 'Test1-Shipping'}, - {contact_person: 'Contact 1-Test Customer 1'}, - {payment_terms_template: '_Test Payment Term Template UI'} - ]); - }, - () => cur_frm.save(), - () => { - // get_item_details - assert.ok(cur_frm.doc.items[0].item_name=='Test Product 4', "Item name correct"); - // get grand_total details - assert.ok(cur_frm.doc.grand_total== 450, "Grand total correct "); - - }, - () => frappe.tests.click_button('Submit'), - () => frappe.tests.click_button('Yes'), - () => frappe.timeout(0.3), - () => done() - ]); -}); diff --git a/erpnext/selling/doctype/sales_order/tests/test_sales_order_with_margin.js b/erpnext/selling/doctype/sales_order/tests/test_sales_order_with_margin.js deleted file mode 100644 index 9eebfdaf21a..00000000000 --- a/erpnext/selling/doctype/sales_order/tests/test_sales_order_with_margin.js +++ /dev/null @@ -1,37 +0,0 @@ -QUnit.module('Selling'); - -QUnit.test("test sales order with margin", function(assert) { - assert.expect(3); - let done = assert.async(); - frappe.run_serially([ - () => { - return frappe.tests.make('Sales Order', [ - {customer:'Test Customer 1'}, - {selling_price_list: 'Test-Selling-USD'}, - {currency: 'USD'}, - {items: [ - [ - {'item_code': 'Test Product 4'}, - {'delivery_date': frappe.datetime.add_days(frappe.defaults.get_default("year_end_date"), 1)}, - {'qty': 1}, - {'margin_type': 'Amount'}, - {'margin_rate_or_amount': 20} - ] - ]}, - ]); - }, - - () => cur_frm.save(), - () => { - // get_rate_details - assert.ok(cur_frm.doc.items[0].rate_with_margin == 220, "Margin rate correct"); - assert.ok(cur_frm.doc.items[0].base_rate_with_margin == cur_frm.doc.conversion_rate * 220, "Base margin rate correct"); - assert.ok(cur_frm.doc.total == 220, "Amount correct"); - }, - - () => frappe.tests.click_button('Submit'), - () => frappe.tests.click_button('Yes'), - () => frappe.timeout(0.3), - () => done() - ]); -}); diff --git a/erpnext/selling/doctype/sales_order/tests/test_sales_order_with_multi_uom.js b/erpnext/selling/doctype/sales_order/tests/test_sales_order_with_multi_uom.js deleted file mode 100644 index 84301f5a86b..00000000000 --- a/erpnext/selling/doctype/sales_order/tests/test_sales_order_with_multi_uom.js +++ /dev/null @@ -1,38 +0,0 @@ -QUnit.module('Sales Order'); - -QUnit.test("test sales order", function(assert) { - assert.expect(3); - let done = assert.async(); - frappe.run_serially([ - () => { - return frappe.tests.make('Sales Order', [ - {customer: 'Test Customer 1'}, - {items: [ - [ - {'delivery_date': frappe.datetime.add_days(frappe.defaults.get_default("year_end_date"), 1)}, - {'qty': 5}, - {'item_code': 'Test Product 4'}, - {'uom': 'Unit'}, - ] - ]}, - {customer_address: 'Test1-Billing'}, - {shipping_address_name: 'Test1-Shipping'}, - {contact_person: 'Contact 1-Test Customer 1'} - ]); - }, - () => cur_frm.save(), - () => { - // get_item_details - assert.ok(cur_frm.doc.items[0].item_name=='Test Product 4', "Item name correct"); - // get uom details - assert.ok(cur_frm.doc.items[0].uom=='Unit', "Multi Uom correct"); - // get grand_total details - assert.ok(cur_frm.doc.grand_total== 5000, "Grand total correct "); - - }, - () => frappe.tests.click_button('Submit'), - () => frappe.tests.click_button('Yes'), - () => frappe.timeout(0.3), - () => done() - ]); -}); diff --git a/erpnext/selling/doctype/sales_order/tests/test_sales_order_with_multiple_delivery_date.js b/erpnext/selling/doctype/sales_order/tests/test_sales_order_with_multiple_delivery_date.js deleted file mode 100644 index be76c49f845..00000000000 --- a/erpnext/selling/doctype/sales_order/tests/test_sales_order_with_multiple_delivery_date.js +++ /dev/null @@ -1,59 +0,0 @@ -/* eslint-disable */ -// rename this file from _test_[name] to test_[name] to activate -// and remove above this line - -QUnit.test("test: Sales Order", function (assert) { - assert.expect(2); - let done = assert.async(); - let delivery_date = frappe.datetime.add_days(frappe.defaults.get_default("year_end_date"), 1); - - frappe.run_serially([ - // insert a new Sales Order - () => { - return frappe.tests.make('Sales Order', [ - {customer: "Test Customer 1"}, - {delivery_date: delivery_date}, - {order_type: 'Sales'}, - {items: [ - [ - {"item_code": "Test Product 1"}, - {"qty": 5}, - {'rate': 100}, - ]] - } - ]) - }, - () => { - assert.ok(cur_frm.doc.items[0].delivery_date == delivery_date); - }, - () => frappe.timeout(1), - // make SO without delivery date in parent, - // parent delivery date should be set based on final delivery date entered in item - () => { - return frappe.tests.make('Sales Order', [ - {customer: "Test Customer 1"}, - {order_type: 'Sales'}, - {items: [ - [ - {"item_code": "Test Product 1"}, - {"qty": 5}, - {'rate': 100}, - {'delivery_date': delivery_date} - ], - [ - {"item_code": "Test Product 2"}, - {"qty": 5}, - {'rate': 100}, - {'delivery_date': frappe.datetime.add_days(delivery_date, 5)} - ]] - } - ]) - }, - () => cur_frm.save(), - () => frappe.timeout(1), - () => { - assert.ok(cur_frm.doc.delivery_date == frappe.datetime.add_days(delivery_date, 5)); - }, - () => done() - ]); -}); diff --git a/erpnext/selling/doctype/sales_order/tests/test_sales_order_with_pricing_rule.js b/erpnext/selling/doctype/sales_order/tests/test_sales_order_with_pricing_rule.js deleted file mode 100644 index e91fb0143b3..00000000000 --- a/erpnext/selling/doctype/sales_order/tests/test_sales_order_with_pricing_rule.js +++ /dev/null @@ -1,34 +0,0 @@ -QUnit.module('Sales Order'); - -QUnit.test("test sales order with shipping rule", function(assert) { - assert.expect(2); - let done = assert.async(); - frappe.run_serially([ - () => { - return frappe.tests.make('Sales Order', [ - {customer: 'Test Customer 3'}, - {items: [ - [ - {'delivery_date': frappe.datetime.add_days(frappe.defaults.get_default("year_end_date"), 1)}, - {'qty': 5}, - {'item_code': 'Test Product 2'}, - ] - ]}, - {customer_address: 'Test1-Billing'}, - {shipping_address_name: 'Test1-Shipping'}, - {contact_person: 'Contact 1-Test Customer 1'}, - ]); - }, - () => cur_frm.save(), - () => { - // get_item_details - assert.ok(cur_frm.doc.items[0].item_name=='Test Product 2', "Item name correct"); - // get grand_total details - assert.ok(cur_frm.doc.grand_total== 675, "Grand total correct "); - }, - () => frappe.tests.click_button('Submit'), - () => frappe.tests.click_button('Yes'), - () => frappe.timeout(0.3), - () => done() - ]); -}); diff --git a/erpnext/selling/doctype/sales_order/tests/test_sales_order_with_shipping_rule.js b/erpnext/selling/doctype/sales_order/tests/test_sales_order_with_shipping_rule.js deleted file mode 100644 index 7d1211f3215..00000000000 --- a/erpnext/selling/doctype/sales_order/tests/test_sales_order_with_shipping_rule.js +++ /dev/null @@ -1,35 +0,0 @@ -QUnit.module('Sales Order'); - -QUnit.test("test sales order with shipping rule", function(assert) { - assert.expect(2); - let done = assert.async(); - frappe.run_serially([ - () => { - return frappe.tests.make('Sales Order', [ - {customer: 'Test Customer 1'}, - {items: [ - [ - {'delivery_date': frappe.datetime.add_days(frappe.defaults.get_default("year_end_date"), 1)}, - {'qty': 5}, - {'item_code': 'Test Product 4'}, - ] - ]}, - {customer_address: 'Test1-Billing'}, - {shipping_address_name: 'Test1-Shipping'}, - {contact_person: 'Contact 1-Test Customer 1'}, - {shipping_rule:'Next Day Shipping'} - ]); - }, - () => cur_frm.save(), - () => { - // get_item_details - assert.ok(cur_frm.doc.items[0].item_name=='Test Product 4', "Item name correct"); - // get grand_total details - assert.ok(cur_frm.doc.grand_total== 550, "Grand total correct "); - }, - () => frappe.tests.click_button('Submit'), - () => frappe.tests.click_button('Yes'), - () => frappe.timeout(0.3), - () => done() - ]); -}); diff --git a/erpnext/selling/doctype/sales_order/tests/test_sales_order_with_taxes_and_charges.js b/erpnext/selling/doctype/sales_order/tests/test_sales_order_with_taxes_and_charges.js deleted file mode 100644 index a3668ab2afc..00000000000 --- a/erpnext/selling/doctype/sales_order/tests/test_sales_order_with_taxes_and_charges.js +++ /dev/null @@ -1,40 +0,0 @@ -QUnit.module('Sales Order'); - -QUnit.test("test sales order with taxes and charges", function(assert) { - assert.expect(3); - let done = assert.async(); - frappe.run_serially([ - () => { - return frappe.tests.make('Sales Order', [ - {customer: 'Test Customer 1'}, - {items: [ - [ - {'delivery_date': frappe.datetime.add_days(frappe.defaults.get_default("year_end_date"), 1)}, - {'qty': 5}, - {'item_code': 'Test Product 4'}, - ] - ]}, - {customer_address: 'Test1-Billing'}, - {shipping_address_name: 'Test1-Shipping'}, - {contact_person: 'Contact 1-Test Customer 1'}, - {taxes_and_charges: 'TEST In State GST - FT'}, - {tc_name: 'Test Term 1'}, - {terms: 'This is Test'} - ]); - }, - () => cur_frm.save(), - () => { - // get_item_details - assert.ok(cur_frm.doc.items[0].item_name=='Test Product 4', "Item name correct"); - // get tax details - assert.ok(cur_frm.doc.taxes_and_charges=='TEST In State GST - FT', "Tax details correct"); - // get tax account head details - assert.ok(cur_frm.doc.taxes[0].account_head=='CGST - '+frappe.get_abbr(frappe.defaults.get_default('Company')), " Account Head abbr correct"); - - }, - () => frappe.tests.click_button('Submit'), - () => frappe.tests.click_button('Yes'), - () => frappe.timeout(0.3), - () => done() - ]); -}); diff --git a/erpnext/selling/doctype/sales_order/tests/test_sales_order_without_bypass_credit_limit_check.js b/erpnext/selling/doctype/sales_order/tests/test_sales_order_without_bypass_credit_limit_check.js deleted file mode 100644 index 8de39f9aa30..00000000000 --- a/erpnext/selling/doctype/sales_order/tests/test_sales_order_without_bypass_credit_limit_check.js +++ /dev/null @@ -1,62 +0,0 @@ -QUnit.module('Sales Order'); - -QUnit.test("test_sales_order_without_bypass_credit_limit_check", function(assert) { -//#PR : 10861, Author : ashish-greycube & jigneshpshah, Email:mr.ashish.shah@gmail.com - assert.expect(2); - let done = assert.async(); - frappe.run_serially([ - () => frappe.new_doc('Customer'), - () => frappe.timeout(1), - () => frappe.quick_entry.dialog.$wrapper.find('.edit-full').click(), - () => frappe.timeout(1), - () => cur_frm.set_value("customer_name", "Test Customer 11"), - () => cur_frm.add_child('credit_limits', { - 'credit_limit': 1000, - 'company': '_Test Company', - 'bypass_credit_limit_check': 1}), - // save form - () => cur_frm.save(), - () => frappe.timeout(1), - - () => frappe.new_doc('Item'), - () => frappe.timeout(1), - () => frappe.click_link('Edit in full page'), - () => cur_frm.set_value("item_code", "Test Product 11"), - () => cur_frm.set_value("item_group", "Products"), - () => cur_frm.set_value("standard_rate", 100), - // save form - () => cur_frm.save(), - () => frappe.timeout(1), - - () => { - return frappe.tests.make('Sales Order', [ - {customer: 'Test Customer 11'}, - {items: [ - [ - {'delivery_date': frappe.datetime.add_days(frappe.defaults.get_default("year_end_date"), 1)}, - {'qty': 5}, - {'item_code': 'Test Product 11'}, - ] - ]} - - ]); - }, - () => cur_frm.save(), - () => frappe.tests.click_button('Submit'), - () => assert.equal("Confirm", cur_dialog.title,'confirmation for submit'), - () => frappe.tests.click_button('Yes'), - () => frappe.timeout(3), - () => { - - if (cur_dialog.body.innerText.match(/^Credit limit has been crossed for customer.*$/)) - { - /*Match found */ - assert.ok(true, "Credit Limit crossed message received"); - } - - - }, - () => cur_dialog.cancel(), - () => done() - ]); -}); diff --git a/erpnext/selling/doctype/sales_order_item/sales_order_item.json b/erpnext/selling/doctype/sales_order_item/sales_order_item.json index 1e5590e7489..95f6c4e96df 100644 --- a/erpnext/selling/doctype/sales_order_item/sales_order_item.json +++ b/erpnext/selling/doctype/sales_order_item/sales_order_item.json @@ -48,6 +48,7 @@ "pricing_rules", "stock_uom_rate", "is_free_item", + "grant_commission", "section_break_24", "net_rate", "net_amount", @@ -789,15 +790,23 @@ "no_copy": 1, "options": "currency", "read_only": 1 + }, + { + "default": "0", + "fieldname": "grant_commission", + "fieldtype": "Check", + "label": "Grant Commission", + "read_only": 1 } ], "idx": 1, "istable": 1, "links": [], - "modified": "2021-02-23 01:15:05.803091", + "modified": "2021-10-05 12:27:25.014789", "modified_by": "Administrator", "module": "Selling", "name": "Sales Order Item", + "naming_rule": "Random", "owner": "Administrator", "permissions": [], "sort_field": "modified", diff --git a/erpnext/selling/doctype/selling_settings/selling_settings.json b/erpnext/selling/doctype/selling_settings/selling_settings.json index c27f1ea81ad..27bc541d62f 100644 --- a/erpnext/selling/doctype/selling_settings/selling_settings.json +++ b/erpnext/selling/doctype/selling_settings/selling_settings.json @@ -11,11 +11,6 @@ "customer_group", "column_break_4", "territory", - "crm_settings_section", - "campaign_naming_by", - "default_valid_till", - "column_break_9", - "close_opportunity_after_days", "item_price_settings_section", "selling_price_list", "maintain_same_rate_action", @@ -43,13 +38,6 @@ "label": "Customer Naming By", "options": "Customer Name\nNaming Series\nAuto Name" }, - { - "fieldname": "campaign_naming_by", - "fieldtype": "Select", - "in_list_view": 1, - "label": "Campaign Naming By", - "options": "Campaign Name\nNaming Series\nAuto Name" - }, { "fieldname": "customer_group", "fieldtype": "Link", @@ -71,18 +59,6 @@ "label": "Default Price List", "options": "Price List" }, - { - "default": "15", - "description": "Auto close Opportunity after the no. of days mentioned above", - "fieldname": "close_opportunity_after_days", - "fieldtype": "Int", - "label": "Close Opportunity After Days" - }, - { - "fieldname": "default_valid_till", - "fieldtype": "Data", - "label": "Default Quotation Validity Days" - }, { "fieldname": "column_break_5", "fieldtype": "Column Break" @@ -169,15 +145,6 @@ "fieldname": "column_break_4", "fieldtype": "Column Break" }, - { - "fieldname": "crm_settings_section", - "fieldtype": "Section Break", - "label": "CRM Settings" - }, - { - "fieldname": "column_break_9", - "fieldtype": "Column Break" - }, { "fieldname": "item_price_settings_section", "fieldtype": "Section Break", @@ -204,7 +171,7 @@ "index_web_pages_for_search": 1, "issingle": 1, "links": [], - "modified": "2021-09-08 19:38:10.175989", + "modified": "2021-09-13 12:32:17.004404", "modified_by": "Administrator", "module": "Selling", "name": "Selling Settings", diff --git a/erpnext/selling/doctype/selling_settings/selling_settings.py b/erpnext/selling/doctype/selling_settings/selling_settings.py index e7c5e769965..fb86e614b6c 100644 --- a/erpnext/selling/doctype/selling_settings/selling_settings.py +++ b/erpnext/selling/doctype/selling_settings/selling_settings.py @@ -8,7 +8,6 @@ import frappe from frappe.custom.doctype.property_setter.property_setter import make_property_setter from frappe.model.document import Document from frappe.utils import cint -from frappe.utils.nestedset import get_root_of class SellingSettings(Document): @@ -37,9 +36,3 @@ class SellingSettings(Document): editable_bundle_item_rates = cint(self.editable_bundle_item_rates) make_property_setter("Packed Item", "rate", "read_only", not(editable_bundle_item_rates), "Check", validate_fields_for_doctype=False) - - def set_default_customer_group_and_territory(self): - if not self.customer_group: - self.customer_group = get_root_of('Customer Group') - if not self.territory: - self.territory = get_root_of('Territory') diff --git a/erpnext/selling/form_tour/customer/customer.json b/erpnext/selling/form_tour/customer/customer.json new file mode 100644 index 00000000000..1de45b7f5d2 --- /dev/null +++ b/erpnext/selling/form_tour/customer/customer.json @@ -0,0 +1,29 @@ +{ + "creation": "2021-11-23 10:44:13.185982", + "docstatus": 0, + "doctype": "Form Tour", + "idx": 0, + "is_standard": 1, + "modified": "2021-11-23 10:54:09.602358", + "modified_by": "Administrator", + "module": "Selling", + "name": "Customer", + "owner": "Administrator", + "reference_doctype": "Customer", + "save_on_complete": 1, + "steps": [ + { + "description": "Enter the Full Name of the Customer", + "field": "", + "fieldname": "customer_name", + "fieldtype": "Data", + "has_next_condition": 0, + "is_table_field": 0, + "label": "Full Name", + "parent_field": "", + "position": "Left", + "title": "Full Name" + } + ], + "title": "Customer" +} \ No newline at end of file diff --git a/erpnext/selling/form_tour/quotation/quotation.json b/erpnext/selling/form_tour/quotation/quotation.json new file mode 100644 index 00000000000..2a2aa5e63e4 --- /dev/null +++ b/erpnext/selling/form_tour/quotation/quotation.json @@ -0,0 +1,67 @@ +{ + "creation": "2021-11-23 12:00:36.138824", + "docstatus": 0, + "doctype": "Form Tour", + "idx": 0, + "is_standard": 1, + "modified": "2021-11-23 12:02:48.010298", + "modified_by": "Administrator", + "module": "Selling", + "name": "Quotation", + "owner": "Administrator", + "reference_doctype": "Quotation", + "save_on_complete": 1, + "steps": [ + { + "description": "Select a customer or lead for whom this quotation is being prepared. Let's select a Customer.", + "field": "", + "fieldname": "quotation_to", + "fieldtype": "Link", + "has_next_condition": 0, + "is_table_field": 0, + "label": "Quotation To", + "parent_field": "", + "position": "Right", + "title": "Quotation To" + }, + { + "description": "Select a specific Customer to whom this quotation will be sent.", + "field": "", + "fieldname": "party_name", + "fieldtype": "Dynamic Link", + "has_next_condition": 0, + "is_table_field": 0, + "label": "Party", + "parent_field": "", + "position": "Right", + "title": "Party" + }, + { + "child_doctype": "Quotation Item", + "description": "Select an item for which you will be quoting a price.", + "field": "", + "fieldname": "items", + "fieldtype": "Table", + "has_next_condition": 0, + "is_table_field": 0, + "label": "Items", + "parent_field": "", + "parent_fieldname": "items", + "position": "Bottom", + "title": "Items" + }, + { + "description": "You can select pre-populated Sales Taxes and Charges from here.", + "field": "", + "fieldname": "taxes", + "fieldtype": "Table", + "has_next_condition": 0, + "is_table_field": 0, + "label": "Sales Taxes and Charges", + "parent_field": "", + "position": "Bottom", + "title": "Sales Taxes and Charges" + } + ], + "title": "Quotation" +} \ No newline at end of file diff --git a/erpnext/selling/page/point_of_sale/pos_item_cart.js b/erpnext/selling/page/point_of_sale/pos_item_cart.js index 9d8338e5fed..4920584d95e 100644 --- a/erpnext/selling/page/point_of_sale/pos_item_cart.js +++ b/erpnext/selling/page/point_of_sale/pos_item_cart.js @@ -49,11 +49,11 @@ erpnext.PointOfSale.ItemCart = class { this.$component.append( `
-
Item Cart
+
${__('Item Cart')}
-
Item
-
Qty
-
Amount
+
${__('Item')}
+
${__('Quantity')}
+
${__('Amount')}
@@ -78,7 +78,7 @@ erpnext.PointOfSale.ItemCart = class { make_no_items_placeholder() { this.$cart_header.css('display', 'none'); this.$cart_items_wrapper.html( - `
No items in cart
` + `
${__('No items in cart')}
` ); } @@ -98,19 +98,23 @@ erpnext.PointOfSale.ItemCart = class { this.$totals_section.append( `
- ${this.get_discount_icon()} Add Discount + ${this.get_discount_icon()} ${__('Add Discount')} +
+
+
${__('Total Items')}
+
0.00
-
Net Total
+
${__("Net Total")}
0.00
-
Grand Total
+
${__('Grand Total')}
0.00
-
Checkout
-
Edit Cart
` +
${__('Checkout')}
+
${__('Edit Cart')}
` ) this.$add_discount_elem = this.$component.find(".add-discount-wrapper"); @@ -126,10 +130,10 @@ erpnext.PointOfSale.ItemCart = class { }, cols: 5, keys: [ - [ 1, 2, 3, 'Quantity' ], - [ 4, 5, 6, 'Discount' ], - [ 7, 8, 9, 'Rate' ], - [ '.', 0, 'Delete', 'Remove' ] + [ 1, 2, 3, __('Quantity') ], + [ 4, 5, 6, __('Discount') ], + [ 7, 8, 9, __('Rate') ], + [ '.', 0, __('Delete'), __('Remove') ] ], css_classes: [ [ '', '', '', 'col-span-2' ], @@ -142,13 +146,14 @@ erpnext.PointOfSale.ItemCart = class { this.$numpad_section.prepend( `
+
` ) this.$numpad_section.append( - `
Checkout
` + `
${__('Checkout')}
` ) } @@ -386,7 +391,7 @@ erpnext.PointOfSale.ItemCart = class { 'border': '1px dashed var(--gray-500)', 'padding': 'var(--padding-sm) var(--padding-md)' }); - me.$add_discount_elem.html(`${me.get_discount_icon()} Add Discount`); + me.$add_discount_elem.html(`${me.get_discount_icon()} ${__('Add Discount')}`); me.discount_field = undefined; } }, @@ -411,7 +416,7 @@ erpnext.PointOfSale.ItemCart = class { }); this.$add_discount_elem.html( `
- ${this.get_discount_icon()} Additional ${String(discount).bold()}% discount applied + ${this.get_discount_icon()} ${__("Additional")} ${String(discount).bold()}% ${__("discount applied")}
` ); } @@ -445,7 +450,7 @@ erpnext.PointOfSale.ItemCart = class { function get_customer_description() { if (!email_id && !mobile_no) { - return `
Click to add email / phone
`; + return `
${__('Click to add email / phone')}
`; } else if (email_id && !mobile_no) { return `
${email_id}
`; } else if (mobile_no && !email_id) { @@ -470,6 +475,7 @@ erpnext.PointOfSale.ItemCart = class { if (!frm) frm = this.events.get_frm(); this.render_net_total(frm.doc.net_total); + this.render_total_item_qty(frm.doc.items); const grand_total = cint(frappe.sys_defaults.disable_rounded_total) ? frm.doc.grand_total : frm.doc.rounded_total; this.render_grand_total(grand_total); @@ -479,22 +485,37 @@ erpnext.PointOfSale.ItemCart = class { render_net_total(value) { const currency = this.events.get_frm().doc.currency; this.$totals_section.find('.net-total-container').html( - `
Net Total
${format_currency(value, currency)}
` + `
${__('Net Total')}
${format_currency(value, currency)}
` ) this.$numpad_section.find('.numpad-net-total').html( - `
Net Total: ${format_currency(value, currency)}
` + `
${__('Net Total')}: ${format_currency(value, currency)}
` + ); + } + + render_total_item_qty(items) { + var total_item_qty = 0; + items.map((item) => { + total_item_qty = total_item_qty + item.qty; + }); + + this.$totals_section.find('.item-qty-total-container').html( + `
${__('Total Quantity')}
${total_item_qty}
` + ); + + this.$numpad_section.find('.numpad-item-qty-total').html( + `
${__('Total Quantity')}: ${total_item_qty}
` ); } render_grand_total(value) { const currency = this.events.get_frm().doc.currency; this.$totals_section.find('.grand-total-container').html( - `
Grand Total
${format_currency(value, currency)}
` + `
${__('Grand Total')}
${format_currency(value, currency)}
` ) this.$numpad_section.find('.numpad-grand-total').html( - `
Grand Total: ${format_currency(value, currency)}
` + `
${__('Grand Total')}: ${format_currency(value, currency)}
` ); } @@ -502,6 +523,7 @@ erpnext.PointOfSale.ItemCart = class { if (taxes.length) { const currency = this.events.get_frm().doc.currency; const taxes_html = taxes.map(t => { + if (t.tax_amount_after_discount_amount == 0.0) return; const description = /[0-9]+/.test(t.description) ? t.description : `${t.description} @ ${t.rate}%`; return `
${description}
diff --git a/erpnext/selling/page/point_of_sale/pos_item_details.js b/erpnext/selling/page/point_of_sale/pos_item_details.js index ec861d7c531..fb69b63f82a 100644 --- a/erpnext/selling/page/point_of_sale/pos_item_details.js +++ b/erpnext/selling/page/point_of_sale/pos_item_details.js @@ -28,7 +28,7 @@ erpnext.PointOfSale.ItemDetails = class { init_child_components() { this.$component.html( `
-
Item Details
+
${__('Item Details')}
@@ -201,8 +201,9 @@ erpnext.PointOfSale.ItemDetails = class { `
` ); } + const label = __('Auto Fetch Serial Numbers'); this.$form_container.append( - `
Auto Fetch Serial Numbers
` + `
${label}
` ); this.$form_container.find('.serial_no-control').find('textarea').css('height', '6rem'); } diff --git a/erpnext/selling/page/point_of_sale/pos_item_selector.js b/erpnext/selling/page/point_of_sale/pos_item_selector.js index 8352b148acc..496385248c4 100644 --- a/erpnext/selling/page/point_of_sale/pos_item_selector.js +++ b/erpnext/selling/page/point_of_sale/pos_item_selector.js @@ -24,7 +24,7 @@ erpnext.PointOfSale.ItemSelector = class { this.wrapper.append( `
-
All Items
+
${__('All Items')}
diff --git a/erpnext/selling/page/point_of_sale/pos_past_order_list.js b/erpnext/selling/page/point_of_sale/pos_past_order_list.js index e0993e2e342..a0475c70d0d 100644 --- a/erpnext/selling/page/point_of_sale/pos_past_order_list.js +++ b/erpnext/selling/page/point_of_sale/pos_past_order_list.js @@ -16,7 +16,7 @@ erpnext.PointOfSale.PastOrderList = class { this.wrapper.append( `
-
Recent Orders
+
${__('Recent Orders')}
diff --git a/erpnext/selling/page/point_of_sale/pos_past_order_summary.js b/erpnext/selling/page/point_of_sale/pos_past_order_summary.js index dd9e05a0e6d..eeb8523f19c 100644 --- a/erpnext/selling/page/point_of_sale/pos_past_order_summary.js +++ b/erpnext/selling/page/point_of_sale/pos_past_order_summary.js @@ -17,16 +17,16 @@ erpnext.PointOfSale.PastOrderSummary = class { this.wrapper.append( `
- Select an invoice to load summary data + ${__('Select an invoice to load summary data')}
-
Items
+
${__('Items')}
-
Totals
+
${__('Totals')}
-
Payments
+
${__('Payments')}
@@ -82,7 +82,7 @@ erpnext.PointOfSale.PastOrderSummary = class { return `
${doc.customer}
${this.customer_email}
-
Sold by: ${doc.owner}
+
${__('Sold by')}: ${doc.owner}
@@ -121,7 +121,7 @@ erpnext.PointOfSale.PastOrderSummary = class { get_net_total_html(doc) { return `
-
Net Total
+
${__('Net Total')}
${format_currency(doc.net_total, doc.currency)}
`; } @@ -144,14 +144,14 @@ erpnext.PointOfSale.PastOrderSummary = class { get_grand_total_html(doc) { return `
-
Grand Total
+
${__('Grand Total')}
${format_currency(doc.grand_total, doc.currency)}
`; } get_payment_html(doc, payment) { return `
-
${payment.mode_of_payment}
+
${__(payment.mode_of_payment)}
${format_currency(payment.amount, doc.currency)}
`; } @@ -285,8 +285,9 @@ erpnext.PointOfSale.PastOrderSummary = class { if (m.condition) { m.visible_btns.forEach(b => { const class_name = b.split(' ')[0].toLowerCase(); + const btn = __(b); this.$summary_btns.append( - `
${b}
` + `
${btn}
` ); }); } diff --git a/erpnext/selling/page/point_of_sale/pos_payment.js b/erpnext/selling/page/point_of_sale/pos_payment.js index 7ddbf45fdb8..b9b65591dc7 100644 --- a/erpnext/selling/page/point_of_sale/pos_payment.js +++ b/erpnext/selling/page/point_of_sale/pos_payment.js @@ -18,11 +18,11 @@ erpnext.PointOfSale.Payment = class { prepare_dom() { this.wrapper.append( `
- +
- +
@@ -30,7 +30,7 @@ erpnext.PointOfSale.Payment = class {
-
Complete Order
+
${__("Complete Order")}
` ); this.$component = this.wrapper.find('.payment-container'); @@ -518,12 +518,12 @@ erpnext.PointOfSale.Payment = class { this.$totals.html( `
-
Grand Total
+
${__('Grand Total')}
${format_currency(grand_total, currency)}
-
Paid Amount
+
${__('Paid Amount')}
${format_currency(paid_amount, currency)}
diff --git a/erpnext/selling/report/pending_so_items_for_purchase_request/test_pending_so_items_for_purchase_request.py b/erpnext/selling/report/pending_so_items_for_purchase_request/test_pending_so_items_for_purchase_request.py index 9c30afc5b1a..d62915fc66d 100644 --- a/erpnext/selling/report/pending_so_items_for_purchase_request/test_pending_so_items_for_purchase_request.py +++ b/erpnext/selling/report/pending_so_items_for_purchase_request/test_pending_so_items_for_purchase_request.py @@ -2,8 +2,6 @@ # For license information, please see license.txt -import unittest - from frappe.utils import add_months, nowdate from erpnext.selling.doctype.sales_order.sales_order import make_material_request @@ -11,9 +9,10 @@ from erpnext.selling.doctype.sales_order.test_sales_order import make_sales_orde from erpnext.selling.report.pending_so_items_for_purchase_request.pending_so_items_for_purchase_request import ( execute, ) +from erpnext.tests.utils import ERPNextTestCase -class TestPendingSOItemsForPurchaseRequest(unittest.TestCase): +class TestPendingSOItemsForPurchaseRequest(ERPNextTestCase): def test_result_for_partial_material_request(self): so = make_sales_order() mr=make_material_request(so.name) diff --git a/erpnext/selling/report/sales_analytics/test_analytics.py b/erpnext/selling/report/sales_analytics/test_analytics.py index 8ffc5d6d0a7..f56cce2dfdc 100644 --- a/erpnext/selling/report/sales_analytics/test_analytics.py +++ b/erpnext/selling/report/sales_analytics/test_analytics.py @@ -2,15 +2,14 @@ # For license information, please see license.txt -import unittest - import frappe from erpnext.selling.doctype.sales_order.test_sales_order import make_sales_order from erpnext.selling.report.sales_analytics.sales_analytics import execute +from erpnext.tests.utils import ERPNextTestCase -class TestAnalytics(unittest.TestCase): +class TestAnalytics(ERPNextTestCase): def test_sales_analytics(self): frappe.db.sql("delete from `tabSales Order` where company='_Test Company 2'") diff --git a/erpnext/selling/report/sales_order_analysis/sales_order_analysis.py b/erpnext/selling/report/sales_order_analysis/sales_order_analysis.py index 82e5d0ce57d..0c0acc76e39 100644 --- a/erpnext/selling/report/sales_order_analysis/sales_order_analysis.py +++ b/erpnext/selling/report/sales_order_analysis/sales_order_analysis.py @@ -61,6 +61,7 @@ def get_data(conditions, filters): IF(so.status in ('Completed','To Bill'), 0, (SELECT delay_days)) as delay, soi.qty, soi.delivered_qty, (soi.qty - soi.delivered_qty) AS pending_qty, + IF((SELECT pending_qty) = 0, (TO_SECONDS(Max(dn.posting_date))-TO_SECONDS(so.transaction_date)), 0) as time_taken_to_deliver, IFNULL(SUM(sii.qty), 0) as billed_qty, soi.base_amount as amount, (soi.delivered_qty * soi.base_rate) as delivered_qty_amount, @@ -70,9 +71,13 @@ def get_data(conditions, filters): so.company, soi.name FROM `tabSales Order` so, - `tabSales Order Item` soi + (`tabSales Order Item` soi LEFT JOIN `tabSales Invoice Item` sii - ON sii.so_detail = soi.name and sii.docstatus = 1 + ON sii.so_detail = soi.name and sii.docstatus = 1) + LEFT JOIN `tabDelivery Note Item` dni + on dni.so_detail = soi.name + RIGHT JOIN `tabDelivery Note` dn + on dni.parent = dn.name and dn.docstatus = 1 WHERE soi.parent = so.name and so.status not in ('Stopped', 'Closed', 'On Hold') @@ -259,6 +264,12 @@ def get_columns(filters): "fieldname": "delay", "fieldtype": "Data", "width": 100 + }, + { + "label": _("Time Taken to Deliver"), + "fieldname": "time_taken_to_deliver", + "fieldtype": "Duration", + "width": 100 } ]) if not filters.get("group_by_so"): diff --git a/erpnext/selling/sales_common.js b/erpnext/selling/sales_common.js index 20504789aa7..540aca234bd 100644 --- a/erpnext/selling/sales_common.js +++ b/erpnext/selling/sales_common.js @@ -41,6 +41,7 @@ erpnext.selling.SellingController = class SellingController extends erpnext.Tran me.frm.set_query('contact_person', erpnext.queries.contact_query); me.frm.set_query('customer_address', erpnext.queries.address_query); me.frm.set_query('shipping_address_name', erpnext.queries.address_query); + me.frm.set_query('dispatch_address_name', erpnext.queries.dispatch_address_query); if(this.frm.fields_dict.selling_price_list) { @@ -157,25 +158,19 @@ erpnext.selling.SellingController = class SellingController extends erpnext.Tran commission_rate() { this.calculate_commission(); - refresh_field("total_commission"); } total_commission() { - if(this.frm.doc.base_net_total) { - frappe.model.round_floats_in(this.frm.doc, ["base_net_total", "total_commission"]); + frappe.model.round_floats_in(this.frm.doc, ["amount_eligible_for_commission", "total_commission"]); - if(this.frm.doc.base_net_total < this.frm.doc.total_commission) { - var msg = (__("[Error]") + " " + - __(frappe.meta.get_label(this.frm.doc.doctype, "total_commission", - this.frm.doc.name)) + " > " + - __(frappe.meta.get_label(this.frm.doc.doctype, "base_net_total", this.frm.doc.name))); - frappe.msgprint(msg); - throw msg; - } + const { amount_eligible_for_commission } = this.frm.doc; + if(!amount_eligible_for_commission) return; - this.frm.set_value("commission_rate", - flt(this.frm.doc.total_commission * 100.0 / this.frm.doc.base_net_total)); - } + this.frm.set_value( + "commission_rate", flt( + this.frm.doc.total_commission * 100.0 / amount_eligible_for_commission + ) + ); } allocated_percentage(doc, cdt, cdn) { @@ -185,7 +180,7 @@ erpnext.selling.SellingController = class SellingController extends erpnext.Tran sales_person.allocated_percentage = flt(sales_person.allocated_percentage, precision("allocated_percentage", sales_person)); - sales_person.allocated_amount = flt(this.frm.doc.base_net_total * + sales_person.allocated_amount = flt(this.frm.doc.amount_eligible_for_commission * sales_person.allocated_percentage / 100.0, precision("allocated_amount", sales_person)); refresh_field(["allocated_amount"], sales_person); @@ -259,28 +254,39 @@ erpnext.selling.SellingController = class SellingController extends erpnext.Tran } calculate_commission() { - if(this.frm.fields_dict.commission_rate) { - if(this.frm.doc.commission_rate > 100) { - var msg = __(frappe.meta.get_label(this.frm.doc.doctype, "commission_rate", this.frm.doc.name)) + - " " + __("cannot be greater than 100"); - frappe.msgprint(msg); - throw msg; - } + if(!this.frm.fields_dict.commission_rate) return; - this.frm.doc.total_commission = flt(this.frm.doc.base_net_total * this.frm.doc.commission_rate / 100.0, - precision("total_commission")); + if(this.frm.doc.commission_rate > 100) { + this.frm.set_value("commission_rate", 100); + frappe.throw(`${__(frappe.meta.get_label( + this.frm.doc.doctype, "commission_rate", this.frm.doc.name + ))} ${__("cannot be greater than 100")}`); } + + this.frm.doc.amount_eligible_for_commission = this.frm.doc.items.reduce( + (sum, item) => item.grant_commission ? sum + item.base_net_amount : sum, 0 + ) + + this.frm.doc.total_commission = flt( + this.frm.doc.amount_eligible_for_commission * this.frm.doc.commission_rate / 100.0, + precision("total_commission") + ); + + refresh_field(["amount_eligible_for_commission", "total_commission"]); } calculate_contribution() { var me = this; $.each(this.frm.doc.doctype.sales_team || [], function(i, sales_person) { frappe.model.round_floats_in(sales_person); - if(sales_person.allocated_percentage) { - sales_person.allocated_amount = flt( - me.frm.doc.base_net_total * sales_person.allocated_percentage / 100.0, - precision("allocated_amount", sales_person)); - } + if (!sales_person.allocated_percentage) return; + + sales_person.allocated_amount = flt( + me.frm.doc.amount_eligible_for_commission + * sales_person.allocated_percentage + / 100.0, + precision("allocated_amount", sales_person) + ); }); } diff --git a/erpnext/setup/doctype/company/company.js b/erpnext/setup/doctype/company/company.js index 95ca3867ee7..91f60fbd4e2 100644 --- a/erpnext/setup/doctype/company/company.js +++ b/erpnext/setup/doctype/company/company.js @@ -12,6 +12,10 @@ frappe.ui.form.on("Company", { } }); } + + frm.call('check_if_transactions_exist').then(r => { + frm.toggle_enable("default_currency", (!r.message)); + }); }, setup: function(frm) { erpnext.company.setup_queries(frm); @@ -87,9 +91,6 @@ frappe.ui.form.on("Company", { frappe.dynamic_link = {doc: frm.doc, fieldname: 'name', doctype: 'Company'} - frm.toggle_enable("default_currency", (frm.doc.__onload && - !frm.doc.__onload.transactions_exist)); - if (frappe.perm.has_perm("Cost Center", 0, 'read')) { frm.add_custom_button(__('Cost Centers'), function() { frappe.set_route('Tree', 'Cost Center', {'company': frm.doc.name}); diff --git a/erpnext/setup/doctype/company/company.py b/erpnext/setup/doctype/company/company.py index 5ebfa049426..e7397394587 100644 --- a/erpnext/setup/doctype/company/company.py +++ b/erpnext/setup/doctype/company/company.py @@ -22,8 +22,8 @@ class Company(NestedSet): def onload(self): load_address_and_contact(self, "company") - self.get("__onload")["transactions_exist"] = self.check_if_transactions_exist() + @frappe.whitelist() def check_if_transactions_exist(self): exists = False for doctype in ["Sales Invoice", "Delivery Note", "Sales Order", "Quotation", @@ -413,7 +413,7 @@ def install_country_fixtures(company, country): frappe.get_attr(module_name)(company, False) except Exception as e: frappe.log_error() - frappe.throw(_("Failed to setup defaults for country {0}. Please contact support@erpnext.com").format(frappe.bold(country))) + frappe.throw(_("Failed to setup defaults for country {0}. Please contact support.").format(frappe.bold(country))) def update_company_current_month_sales(company): diff --git a/erpnext/setup/doctype/company/test_records.json b/erpnext/setup/doctype/company/test_records.json index 9e55702ddc9..89be607d047 100644 --- a/erpnext/setup/doctype/company/test_records.json +++ b/erpnext/setup/doctype/company/test_records.json @@ -36,7 +36,7 @@ "abbr": "_TC3", "company_name": "_Test Company 3", "is_group": 1, - "country": "India", + "country": "Pakistan", "default_currency": "INR", "doctype": "Company", "domain": "Manufacturing", @@ -49,7 +49,7 @@ "company_name": "_Test Company 4", "parent_company": "_Test Company 3", "is_group": 1, - "country": "India", + "country": "Pakistan", "default_currency": "INR", "doctype": "Company", "domain": "Manufacturing", @@ -61,7 +61,7 @@ "abbr": "_TC5", "company_name": "_Test Company 5", "parent_company": "_Test Company 4", - "country": "India", + "country": "Pakistan", "default_currency": "INR", "doctype": "Company", "domain": "Manufacturing", diff --git a/erpnext/setup/doctype/company/tests/test_company.js b/erpnext/setup/doctype/company/tests/test_company.js deleted file mode 100644 index b568494c84a..00000000000 --- a/erpnext/setup/doctype/company/tests/test_company.js +++ /dev/null @@ -1,25 +0,0 @@ -QUnit.module('setup'); - -QUnit.test("Test: Company [SetUp]", function (assert) { - assert.expect(2); - let done = assert.async(); - - frappe.run_serially([ - // test company creation - () => frappe.set_route("List", "Company", "List"), - () => frappe.new_doc("Company"), - () => frappe.timeout(1), - () => cur_frm.set_value("company_name", "Test Company"), - () => cur_frm.set_value("abbr", "TC"), - () => cur_frm.set_value("domain", "Services"), - () => cur_frm.set_value("default_currency", "INR"), - // save form - () => cur_frm.save(), - () => frappe.timeout(1), - () => assert.equal("Debtors - TC", cur_frm.doc.default_receivable_account, - 'chart of acounts created'), - () => assert.equal("Main - TC", cur_frm.doc.cost_center, - 'chart of cost centers created'), - () => done() - ]); -}); diff --git a/erpnext/setup/doctype/company/tests/test_company_production.js b/erpnext/setup/doctype/company/tests/test_company_production.js deleted file mode 100644 index a4c1e2e7dea..00000000000 --- a/erpnext/setup/doctype/company/tests/test_company_production.js +++ /dev/null @@ -1,19 +0,0 @@ -QUnit.test("Test: Company", function (assert) { - assert.expect(0); - - let done = assert.async(); - - frappe.run_serially([ - // Added company for Work Order testing - () => frappe.set_route("List", "Company"), - () => frappe.new_doc("Company"), - () => frappe.timeout(1), - () => cur_frm.set_value("company_name", "For Testing"), - () => cur_frm.set_value("abbr", "RB"), - () => cur_frm.set_value("default_currency", "INR"), - () => cur_frm.save(), - () => frappe.timeout(1), - - () => done() - ]); -}); diff --git a/erpnext/setup/doctype/item_group/item_group.py b/erpnext/setup/doctype/item_group/item_group.py index 4c42b61f01e..c94b3463fc8 100644 --- a/erpnext/setup/doctype/item_group/item_group.py +++ b/erpnext/setup/doctype/item_group/item_group.py @@ -43,7 +43,6 @@ class ItemGroup(NestedSet, WebsiteGenerator): def on_update(self): NestedSet.on_update(self) invalidate_cache_for(self) - self.validate_name_with_item() self.validate_one_root() self.delete_child_item_groups_key() @@ -67,10 +66,6 @@ class ItemGroup(NestedSet, WebsiteGenerator): WebsiteGenerator.on_trash(self) self.delete_child_item_groups_key() - def validate_name_with_item(self): - if frappe.db.exists("Item", self.name): - frappe.throw(frappe._("An item exists with same name ({0}), please change the item group name or rename the item").format(self.name), frappe.NameError) - def get_context(self, context): context.show_search=True context.page_length = cint(frappe.db.get_single_value('Products Settings', 'products_per_page')) or 6 diff --git a/erpnext/setup/doctype/naming_series/naming_series.py b/erpnext/setup/doctype/naming_series/naming_series.py index 4def6eb9e8a..986b4e87ff0 100644 --- a/erpnext/setup/doctype/naming_series/naming_series.py +++ b/erpnext/setup/doctype/naming_series/naming_series.py @@ -180,11 +180,11 @@ class NamingSeries(Document): prefix = parse_naming_series(parts) return prefix -def set_by_naming_series(doctype, fieldname, naming_series, hide_name_field=True): +def set_by_naming_series(doctype, fieldname, naming_series, hide_name_field=True, make_mandatory=1): from frappe.custom.doctype.property_setter.property_setter import make_property_setter if naming_series: make_property_setter(doctype, "naming_series", "hidden", 0, "Check", validate_fields_for_doctype=False) - make_property_setter(doctype, "naming_series", "reqd", 1, "Check", validate_fields_for_doctype=False) + make_property_setter(doctype, "naming_series", "reqd", make_mandatory, "Check", validate_fields_for_doctype=False) # set values for mandatory try: diff --git a/erpnext/setup/form_tour/company/company.json b/erpnext/setup/form_tour/company/company.json new file mode 100644 index 00000000000..c66abc0a720 --- /dev/null +++ b/erpnext/setup/form_tour/company/company.json @@ -0,0 +1,67 @@ +{ + "creation": "2021-11-24 10:17:18.534917", + "docstatus": 0, + "doctype": "Form Tour", + "first_document": 1, + "idx": 0, + "include_name_field": 0, + "is_standard": 1, + "modified": "2021-11-24 15:38:21.026582", + "modified_by": "Administrator", + "module": "Setup", + "name": "Company", + "owner": "Administrator", + "reference_doctype": "Company", + "save_on_complete": 0, + "steps": [ + { + "description": "This is the default currency for this company.", + "field": "", + "fieldname": "default_currency", + "fieldtype": "Link", + "has_next_condition": 0, + "is_table_field": 0, + "label": "Default Currency", + "parent_field": "", + "position": "Right", + "title": "Default Currency" + }, + { + "description": "Here, you can add multiple addresses of the company", + "field": "", + "fieldname": "company_info", + "fieldtype": "Section Break", + "has_next_condition": 0, + "is_table_field": 0, + "label": "Address & Contact", + "parent_field": "", + "position": "Top", + "title": "Address & Contact" + }, + { + "description": "Here, you can set default Accounts, which will ease the creation of accounting entries.", + "field": "", + "fieldname": "default_settings", + "fieldtype": "Section Break", + "has_next_condition": 0, + "is_table_field": 0, + "label": "Accounts Settings", + "parent_field": "", + "position": "Top", + "title": "Accounts Settings" + }, + { + "description": "This setting is recommended if you wish to track the real-time stock balance in your books of account. This will allow the creation of a General Ledger entry for every stock transaction.", + "field": "", + "fieldname": "enable_perpetual_inventory", + "fieldtype": "Check", + "has_next_condition": 0, + "is_table_field": 0, + "label": "Enable Perpetual Inventory", + "parent_field": "", + "position": "Right", + "title": "Enable Perpetual Inventory" + } + ], + "title": "Company" +} \ No newline at end of file diff --git a/erpnext/setup/module_onboarding/home/home.json b/erpnext/setup/module_onboarding/home/home.json new file mode 100644 index 00000000000..1b2dbc6fea7 --- /dev/null +++ b/erpnext/setup/module_onboarding/home/home.json @@ -0,0 +1,62 @@ +{ + "allow_roles": [ + { + "role": "Accounts Manager" + }, + { + "role": "Stock Manager" + }, + { + "role": "Sales Manager" + }, + { + "role": "Purchase Manager" + }, + { + "role": "Manufacturing Manager" + }, + { + "role": "Item Manager" + } + ], + "creation": "2021-11-22 12:19:15.888642", + "docstatus": 0, + "doctype": "Module Onboarding", + "documentation_url": "https://docs.erpnext.com/docs/v13/user/manual/en/setting-up/company-setup", + "idx": 0, + "is_complete": 0, + "modified": "2021-12-15 14:23:52.460913", + "modified_by": "Administrator", + "module": "Setup", + "name": "Home", + "owner": "Administrator", + "steps": [ + { + "step": "Company Set Up" + }, + { + "step": "Navigation Help" + }, + { + "step": "Data import" + }, + { + "step": "Create an Item" + }, + { + "step": "Create a Customer" + }, + { + "step": "Create a Supplier" + }, + { + "step": "Create a Quotation" + }, + { + "step": "Letterhead" + } + ], + "subtitle": "Company, Item, Customer, Supplier, Navigation Help, Data Import, Letter Head, Quotation", + "success_message": "Masters are all set up!", + "title": "Let's Set Up Some Masters" +} \ No newline at end of file diff --git a/erpnext/setup/onboarding_step/company_set_up/company_set_up.json b/erpnext/setup/onboarding_step/company_set_up/company_set_up.json new file mode 100644 index 00000000000..6f6583231f9 --- /dev/null +++ b/erpnext/setup/onboarding_step/company_set_up/company_set_up.json @@ -0,0 +1,21 @@ +{ + "action": "Create Entry", + "action_label": "Let's review your Company", + "creation": "2021-11-22 11:55:48.931427", + "description": "# Set Up a Company\n\nA company is a legal entity for which you will set up your books of account and create accounting transactions. In ERPNext, you can create multiple companies, and establish relationships (group/subsidiary) among them.\n\nWithin the company master, you can capture various default accounts for that Company and set crucial settings related to the accounting methodology followed for a company.\n", + "docstatus": 0, + "doctype": "Onboarding Step", + "idx": 0, + "is_complete": 0, + "is_single": 0, + "is_skipped": 0, + "modified": "2021-12-15 14:22:18.317423", + "modified_by": "Administrator", + "name": "Company Set Up", + "owner": "Administrator", + "reference_document": "Company", + "show_form_tour": 1, + "show_full_form": 1, + "title": "Set Up a Company", + "validate_action": 1 +} \ No newline at end of file diff --git a/erpnext/setup/onboarding_step/create_a_customer/create_a_customer.json b/erpnext/setup/onboarding_step/create_a_customer/create_a_customer.json new file mode 100644 index 00000000000..f74d745be9c --- /dev/null +++ b/erpnext/setup/onboarding_step/create_a_customer/create_a_customer.json @@ -0,0 +1,21 @@ +{ + "action": "Create Entry", + "action_label": "Let\u2019s create your first Customer", + "creation": "2020-05-14 17:46:41.831517", + "description": "# Create a Customer\n\nThe Customer master is at the heart of your sales transactions. Customers are linked in Quotations, Sales Orders, Invoices, and Payments. Customers can be either numbered or identified by name (you would typically do this based on the number of customers you have).\n\nThrough Customer\u2019s master, you can effectively track essentials like:\n - Customer\u2019s multiple address and contacts\n - Account Receivables\n - Credit Limit and Credit Period\n", + "docstatus": 0, + "doctype": "Onboarding Step", + "idx": 0, + "is_complete": 0, + "is_single": 0, + "is_skipped": 0, + "modified": "2021-12-15 14:20:31.197564", + "modified_by": "Administrator", + "name": "Create a Customer", + "owner": "Administrator", + "reference_document": "Customer", + "show_form_tour": 0, + "show_full_form": 0, + "title": "Manage Customers", + "validate_action": 1 +} \ No newline at end of file diff --git a/erpnext/setup/onboarding_step/create_a_quotation/create_a_quotation.json b/erpnext/setup/onboarding_step/create_a_quotation/create_a_quotation.json new file mode 100644 index 00000000000..8bdb621c0a5 --- /dev/null +++ b/erpnext/setup/onboarding_step/create_a_quotation/create_a_quotation.json @@ -0,0 +1,21 @@ +{ + "action": "Create Entry", + "action_label": "Let\u2019s create your first Quotation", + "creation": "2020-06-01 13:34:58.958641", + "description": "# Create a Quotation\n\nLet\u2019s get started with business transactions by creating your first Quotation. You can create a Quotation for an existing customer or a prospect. It will be an approved document, with items you sell and the proposed price + taxes applied. After completing the instructions, you will get a Quotation in a ready to share print format.", + "docstatus": 0, + "doctype": "Onboarding Step", + "idx": 0, + "is_complete": 0, + "is_single": 0, + "is_skipped": 0, + "modified": "2021-12-15 14:21:31.675330", + "modified_by": "Administrator", + "name": "Create a Quotation", + "owner": "Administrator", + "reference_document": "Quotation", + "show_form_tour": 1, + "show_full_form": 1, + "title": "Create your first Quotation", + "validate_action": 1 +} \ No newline at end of file diff --git a/erpnext/setup/onboarding_step/create_a_supplier/create_a_supplier.json b/erpnext/setup/onboarding_step/create_a_supplier/create_a_supplier.json new file mode 100644 index 00000000000..9574141eaab --- /dev/null +++ b/erpnext/setup/onboarding_step/create_a_supplier/create_a_supplier.json @@ -0,0 +1,21 @@ +{ + "action": "Create Entry", + "action_label": "Let\u2019s create your first Supplier", + "creation": "2020-05-14 22:09:10.043554", + "description": "# Create a Supplier\n\nAlso known as Vendor, is a master at the center of your purchase transactions. Suppliers are linked in Request for Quotation, Purchase Orders, Receipts, and Payments. Suppliers can be either numbered or identified by name.\n\nThrough Supplier\u2019s master, you can effectively track essentials like:\n - Supplier\u2019s multiple address and contacts\n - Account Receivables\n - Credit Limit and Credit Period\n", + "docstatus": 0, + "doctype": "Onboarding Step", + "idx": 0, + "is_complete": 0, + "is_single": 0, + "is_skipped": 0, + "modified": "2021-12-15 14:21:23.518301", + "modified_by": "Administrator", + "name": "Create a Supplier", + "owner": "Administrator", + "reference_document": "Supplier", + "show_form_tour": 0, + "show_full_form": 0, + "title": "Manage Suppliers", + "validate_action": 1 +} \ No newline at end of file diff --git a/erpnext/setup/onboarding_step/create_an_item/create_an_item.json b/erpnext/setup/onboarding_step/create_an_item/create_an_item.json new file mode 100644 index 00000000000..cd29683346c --- /dev/null +++ b/erpnext/setup/onboarding_step/create_an_item/create_an_item.json @@ -0,0 +1,23 @@ +{ + "action": "Create Entry", + "action_label": "Create a new Item", + "creation": "2021-05-17 13:47:18.515052", + "description": "# Create an Item\n\nItem is a product, of a or service offered by your company, or something you buy as a part of your supplies or raw materials.\n\nItems are integral to everything you do in ERPNext - from billing, purchasing to managing inventory. Everything you buy or sell, whether it is a physical product or a service is an Item. Items can be stock, non-stock, variants, serialized, batched, assets etc.\n", + "docstatus": 0, + "doctype": "Onboarding Step", + "form_tour": "Item General", + "idx": 0, + "intro_video_url": "", + "is_complete": 0, + "is_single": 0, + "is_skipped": 0, + "modified": "2021-12-15 14:19:56.297772", + "modified_by": "Administrator", + "name": "Create an Item", + "owner": "Administrator", + "reference_document": "Item", + "show_form_tour": 1, + "show_full_form": 1, + "title": "Manage Items", + "validate_action": 1 +} \ No newline at end of file diff --git a/erpnext/setup/onboarding_step/data_import/data_import.json b/erpnext/setup/onboarding_step/data_import/data_import.json new file mode 100644 index 00000000000..48741dca01c --- /dev/null +++ b/erpnext/setup/onboarding_step/data_import/data_import.json @@ -0,0 +1,21 @@ +{ + "action": "Watch Video", + "action_label": "Learn more about data migration", + "creation": "2021-05-19 05:29:16.809610", + "description": "# Import Data from Spreadsheet\n\nIn ERPNext, you can easily migrate your historical data using spreadsheets. You can use it for migrating not just masters (like Customer, Supplier, Items), but also for transactions like (outstanding invoices, opening stock and accounting entries, etc). If you are migrating from [Tally](https://tallysolutions.com/) or [Quickbooks](https://quickbooks.intuit.com/in/), we got special migration tools for you.", + "docstatus": 0, + "doctype": "Onboarding Step", + "idx": 0, + "is_complete": 0, + "is_single": 0, + "is_skipped": 0, + "modified": "2021-12-15 13:10:57.346422", + "modified_by": "Administrator", + "name": "Data import", + "owner": "Administrator", + "show_form_tour": 0, + "show_full_form": 0, + "title": "Import Data from Spreadsheet", + "validate_action": 1, + "video_url": "https://youtu.be/DQyqeurPI64" +} \ No newline at end of file diff --git a/erpnext/setup/onboarding_step/letterhead/letterhead.json b/erpnext/setup/onboarding_step/letterhead/letterhead.json new file mode 100644 index 00000000000..8e1bb8ce827 --- /dev/null +++ b/erpnext/setup/onboarding_step/letterhead/letterhead.json @@ -0,0 +1,21 @@ +{ + "action": "Create Entry", + "action_label": "Let\u2019s setup your first Letter Head", + "creation": "2021-11-22 12:36:34.583783", + "description": "# Create a Letter Head\n\nA Letter Head contains your organization's name, logo, address, etc which appears at the header and footer portion in documents. You can learn more about Setting up Letter Head in ERPNext here.\n", + "docstatus": 0, + "doctype": "Onboarding Step", + "idx": 0, + "is_complete": 0, + "is_single": 0, + "is_skipped": 0, + "modified": "2021-12-15 14:21:39.037742", + "modified_by": "Administrator", + "name": "Letterhead", + "owner": "Administrator", + "reference_document": "Letter Head", + "show_form_tour": 1, + "show_full_form": 1, + "title": "Setup Your Letterhead", + "validate_action": 1 +} \ No newline at end of file diff --git a/erpnext/setup/onboarding_step/navigation_help/navigation_help.json b/erpnext/setup/onboarding_step/navigation_help/navigation_help.json new file mode 100644 index 00000000000..388853df79d --- /dev/null +++ b/erpnext/setup/onboarding_step/navigation_help/navigation_help.json @@ -0,0 +1,21 @@ +{ + "action": "Watch Video", + "action_label": "Learn about Navigation options", + "creation": "2021-11-22 12:09:52.233872", + "description": "# Navigation in ERPNext\n\nEase of navigating and browsing around the ERPNext is one of our core strengths. In the following video, you will learn how to reach a specific feature in ERPNext via module page or awesome bar\u2019s shortcut.\n", + "docstatus": 0, + "doctype": "Onboarding Step", + "idx": 0, + "is_complete": 0, + "is_single": 0, + "is_skipped": 0, + "modified": "2021-12-15 14:20:55.441678", + "modified_by": "Administrator", + "name": "Navigation Help", + "owner": "Administrator", + "show_form_tour": 0, + "show_full_form": 0, + "title": "How to Navigate in ERPNext", + "validate_action": 1, + "video_url": "https://youtu.be/j60xyNFqX_A" +} \ No newline at end of file diff --git a/erpnext/setup/setup_wizard/data/country_wise_tax.json b/erpnext/setup/setup_wizard/data/country_wise_tax.json index 14b79510c12..91e8eff89fd 100644 --- a/erpnext/setup/setup_wizard/data/country_wise_tax.json +++ b/erpnext/setup/setup_wizard/data/country_wise_tax.json @@ -1178,11 +1178,13 @@ { "title": "Reverse Charge In-State", "is_inter_state": 0, + "is_reverse_charge": 1, "gst_state": "" }, { "title": "Reverse Charge Out-State", "is_inter_state": 1, + "is_reverse_charge": 1, "gst_state": "" }, { diff --git a/erpnext/setup/setup_wizard/operations/defaults_setup.py b/erpnext/setup/setup_wizard/operations/defaults_setup.py index e4b1fa26ae0..ca1f57eb1d4 100644 --- a/erpnext/setup/setup_wizard/operations/defaults_setup.py +++ b/erpnext/setup/setup_wizard/operations/defaults_setup.py @@ -68,6 +68,8 @@ def set_default_settings(args): hr_settings.send_interview_feedback_reminder = 1 hr_settings.feedback_reminder_notification_template = _("Interview Feedback Reminder") + + hr_settings.exit_questionnaire_notification_template = _("Exit Questionnaire Notification") hr_settings.save() def set_no_copy_fields_in_variant_settings(): diff --git a/erpnext/setup/setup_wizard/operations/install_fixtures.py b/erpnext/setup/setup_wizard/operations/install_fixtures.py index 503aeacd015..97d850ba19d 100644 --- a/erpnext/setup/setup_wizard/operations/install_fixtures.py +++ b/erpnext/setup/setup_wizard/operations/install_fixtures.py @@ -278,6 +278,11 @@ def install(country=None): records += [{'doctype': 'Email Template', 'name': _('Interview Feedback Reminder'), 'response': response, 'subject': _('Interview Feedback Reminder'), 'owner': frappe.session.user}] + response = frappe.read_file(os.path.join(base_path, 'exit_interview/exit_questionnaire_notification_template.html')) + + records += [{'doctype': 'Email Template', 'name': _('Exit Questionnaire Notification'), 'response': response, + 'subject': _('Exit Questionnaire Notification'), 'owner': frappe.session.user}] + base_path = frappe.get_app_path("erpnext", "stock", "doctype") response = frappe.read_file(os.path.join(base_path, "delivery_trip/dispatch_notification_template.html")) @@ -303,7 +308,6 @@ def set_more_defaults(): def update_selling_defaults(): selling_settings = frappe.get_doc("Selling Settings") - selling_settings.set_default_customer_group_and_territory() selling_settings.cust_master_name = "Customer Name" selling_settings.so_required = "No" selling_settings.dn_required = "No" diff --git a/erpnext/setup/utils.py b/erpnext/setup/utils.py index 1478007da83..cad4c54d7db 100644 --- a/erpnext/setup/utils.py +++ b/erpnext/setup/utils.py @@ -53,6 +53,7 @@ def before_tests(): frappe.db.set_value("Stock Settings", None, "auto_insert_price_list_rate_if_missing", 0) enable_all_roles_and_domains() + set_defaults_for_tests() frappe.db.commit() @@ -127,6 +128,14 @@ def enable_all_roles_and_domains(): [d.name for d in domains]) add_all_roles_to('Administrator') +def set_defaults_for_tests(): + from frappe.utils.nestedset import get_root_of + + selling_settings = frappe.get_single("Selling Settings") + selling_settings.customer_group = get_root_of("Customer Group") + selling_settings.territory = get_root_of("Territory") + selling_settings.save() + def insert_record(records): for r in records: diff --git a/erpnext/setup/workspace/erpnext_settings/erpnext_settings.json b/erpnext/setup/workspace/erpnext_settings/erpnext_settings.json index 1412acfcead..e47837f2ca4 100644 --- a/erpnext/setup/workspace/erpnext_settings/erpnext_settings.json +++ b/erpnext/setup/workspace/erpnext_settings/erpnext_settings.json @@ -10,7 +10,7 @@ "idx": 0, "label": "ERPNext Settings", "links": [], - "modified": "2021-10-26 21:32:55.323591", + "modified": "2021-11-05 21:32:55.323591", "modified_by": "Administrator", "module": "Setup", "name": "ERPNext Settings", @@ -123,6 +123,13 @@ "label": "Products Settings", "link_to": "Products Settings", "type": "DocType" + }, + { + "doc_view": "", + "icon": "crm", + "label": "CRM Settings", + "link_to": "CRM Settings", + "type": "DocType" } ], "title": "ERPNext Settings" diff --git a/erpnext/setup/workspace/home/home.json b/erpnext/setup/workspace/home/home.json index 4e1ccf9b94f..f9c585c015c 100644 --- a/erpnext/setup/workspace/home/home.json +++ b/erpnext/setup/workspace/home/home.json @@ -1,13 +1,18 @@ { "charts": [], - "content": "[{\"type\":\"header\",\"data\":{\"text\":\"Your Shortcuts\",\"level\":4,\"col\":12}},{\"type\":\"shortcut\",\"data\":{\"shortcut_name\":\"Item\",\"col\":4}},{\"type\":\"shortcut\",\"data\":{\"shortcut_name\":\"Customer\",\"col\":4}},{\"type\":\"shortcut\",\"data\":{\"shortcut_name\":\"Supplier\",\"col\":4}},{\"type\":\"shortcut\",\"data\":{\"shortcut_name\":\"Sales Invoice\",\"col\":4}},{\"type\":\"shortcut\",\"data\":{\"shortcut_name\":\"Leaderboard\",\"col\":4}},{\"type\":\"spacer\",\"data\":{\"col\":12}},{\"type\":\"header\",\"data\":{\"text\":\"Reports & Masters\",\"level\":4,\"col\":12}},{\"type\":\"card\",\"data\":{\"card_name\":\"Accounting\",\"col\":4}},{\"type\":\"card\",\"data\":{\"card_name\":\"Stock\",\"col\":4}},{\"type\":\"card\",\"data\":{\"card_name\":\"Human Resources\",\"col\":4}},{\"type\":\"card\",\"data\":{\"card_name\":\"CRM\",\"col\":4}},{\"type\":\"card\",\"data\":{\"card_name\":\"Data Import and Settings\",\"col\":4}}]", + "content": "[{\"type\":\"onboarding\",\"data\":{\"onboarding_name\":\"Home\",\"col\":12}},{\"type\":\"header\",\"data\":{\"text\":\"Your Shortcuts\\n\\t\\t\\t\\n\\t\\t\\n\\t\\t\\t\\n\\t\\t\\n\\t\\t\\t\\n\\t\\t\",\"level\":4,\"col\":12}},{\"type\":\"shortcut\",\"data\":{\"shortcut_name\":\"Item\",\"col\":4}},{\"type\":\"shortcut\",\"data\":{\"shortcut_name\":\"Customer\",\"col\":4}},{\"type\":\"shortcut\",\"data\":{\"shortcut_name\":\"Supplier\",\"col\":4}},{\"type\":\"shortcut\",\"data\":{\"shortcut_name\":\"Sales Invoice\",\"col\":4}},{\"type\":\"shortcut\",\"data\":{\"shortcut_name\":\"Leaderboard\",\"col\":4}},{\"type\":\"spacer\",\"data\":{\"col\":12}},{\"type\":\"header\",\"data\":{\"text\":\"Reports & Masters\\n\\t\\t\\t\\n\\t\\t\\n\\t\\t\\t\\n\\t\\t\\n\\t\\t\\t\\n\\t\\t\",\"level\":4,\"col\":12}},{\"type\":\"card\",\"data\":{\"card_name\":\"Accounting\",\"col\":4}},{\"type\":\"card\",\"data\":{\"card_name\":\"Stock\",\"col\":4}},{\"type\":\"card\",\"data\":{\"card_name\":\"Human Resources\",\"col\":4}},{\"type\":\"card\",\"data\":{\"card_name\":\"CRM\",\"col\":4}},{\"type\":\"card\",\"data\":{\"card_name\":\"Data Import and Settings\",\"col\":4}}]", "creation": "2020-01-23 13:46:38.833076", + "developer_mode_only": 0, + "disable_user_customization": 0, "docstatus": 0, "doctype": "Workspace", + "extends_another_page": 0, "for_user": "", "hide_custom": 0, "icon": "getting-started", "idx": 0, + "is_default": 0, + "is_standard": 0, "label": "Home", "links": [ { @@ -271,12 +276,14 @@ "type": "Link" } ], - "modified": "2021-08-10 15:33:20.704741", + "modified": "2021-11-22 12:50:15.771366", "modified_by": "Administrator", "module": "Setup", "name": "Home", "owner": "Administrator", "parent_page": "", + "pin_to_bottom": 0, + "pin_to_top": 0, "public": 1, "restrict_to_domain": "", "roles": [], @@ -309,4 +316,4 @@ } ], "title": "Home" -} \ No newline at end of file +} diff --git a/erpnext/startup/boot.py b/erpnext/startup/boot.py index ed8c878ad4a..0da45a54d56 100644 --- a/erpnext/startup/boot.py +++ b/erpnext/startup/boot.py @@ -22,7 +22,7 @@ def boot_session(bootinfo): 'customer_group') bootinfo.sysdefaults.allow_stale = cint(frappe.db.get_single_value('Accounts Settings', 'allow_stale')) - bootinfo.sysdefaults.quotation_valid_till = cint(frappe.db.get_single_value('Selling Settings', + bootinfo.sysdefaults.quotation_valid_till = cint(frappe.db.get_single_value('CRM Settings', 'default_valid_till')) # if no company, show a dialog box to create a new company diff --git a/erpnext/stock/doctype/batch/test_batch.js b/erpnext/stock/doctype/batch/test_batch.js deleted file mode 100644 index 2d2150b8acd..00000000000 --- a/erpnext/stock/doctype/batch/test_batch.js +++ /dev/null @@ -1,22 +0,0 @@ -QUnit.module('Stock'); - -QUnit.test("test Batch", function(assert) { - assert.expect(1); - let done = assert.async(); - frappe.run_serially([ - () => { - return frappe.tests.make('Batch', [ - {batch_id:'TEST-BATCH-001'}, - {item:'Test Product 4'}, - {expiry_date:frappe.datetime.add_days(frappe.datetime.now_date(), 2)}, - ]); - }, - () => cur_frm.save(), - () => { - // get_item_details - assert.ok(cur_frm.doc.batch_id=='TEST-BATCH-001', "Batch Id correct"); - }, - () => frappe.timeout(0.3), - () => done() - ]); -}); diff --git a/erpnext/stock/doctype/bin/bin.py b/erpnext/stock/doctype/bin/bin.py index 48b1cc53967..0ef7ce29230 100644 --- a/erpnext/stock/doctype/bin/bin.py +++ b/erpnext/stock/doctype/bin/bin.py @@ -4,7 +4,9 @@ import frappe from frappe.model.document import Document -from frappe.utils import flt, nowdate +from frappe.query_builder import Case +from frappe.query_builder.functions import Coalesce, Sum +from frappe.utils import flt class Bin(Document): @@ -19,34 +21,42 @@ class Bin(Document): - flt(self.reserved_qty_for_production) - flt(self.reserved_qty_for_sub_contract)) def get_first_sle(self): - sle = frappe.db.sql(""" - select * from `tabStock Ledger Entry` - where item_code = %s - and warehouse = %s - order by timestamp(posting_date, posting_time) asc, creation asc - limit 1 - """, (self.item_code, self.warehouse), as_dict=1) - return sle and sle[0] or None + sle = frappe.qb.DocType("Stock Ledger Entry") + first_sle = ( + frappe.qb.from_(sle) + .select("*") + .where((sle.item_code == self.item_code) & (sle.warehouse == self.warehouse)) + .orderby(sle.posting_date, sle.posting_time, sle.creation) + .limit(1) + ).run(as_dict=True) + + return first_sle and first_sle[0] or None def update_reserved_qty_for_production(self): '''Update qty reserved for production from Production Item tables in open work orders''' - self.reserved_qty_for_production = frappe.db.sql(''' - SELECT - CASE WHEN ifnull(skip_transfer, 0) = 0 THEN - SUM(item.required_qty - item.transferred_qty) - ELSE - SUM(item.required_qty - item.consumed_qty) - END - FROM `tabWork Order` pro, `tabWork Order Item` item - WHERE - item.item_code = %s - and item.parent = pro.name - and pro.docstatus = 1 - and item.source_warehouse = %s - and pro.status not in ("Stopped", "Completed") - and (item.required_qty > item.transferred_qty or item.required_qty > item.consumed_qty) - ''', (self.item_code, self.warehouse))[0][0] + + wo = frappe.qb.DocType("Work Order") + wo_item = frappe.qb.DocType("Work Order Item") + + self.reserved_qty_for_production = ( + frappe.qb + .from_(wo) + .from_(wo_item) + .select(Sum(Case() + .when(wo.skip_transfer == 0, wo_item.required_qty - wo_item.transferred_qty) + .else_(wo_item.required_qty - wo_item.consumed_qty)) + ) + .where( + (wo_item.item_code == self.item_code) + & (wo_item.parent == wo.name) + & (wo.docstatus == 1) + & (wo_item.source_warehouse == self.warehouse) + & (wo.status.notin(["Stopped", "Completed"])) + & ((wo_item.required_qty > wo_item.transferred_qty) + | (wo_item.required_qty > wo_item.consumed_qty)) + ) + ).run()[0][0] or 0.0 self.set_projected_qty() @@ -55,36 +65,53 @@ class Bin(Document): def update_reserved_qty_for_sub_contracting(self): #reserved qty - reserved_qty_for_sub_contract = frappe.db.sql(''' - select ifnull(sum(itemsup.required_qty),0) - from `tabPurchase Order` po, `tabPurchase Order Item Supplied` itemsup - where - itemsup.rm_item_code = %s - and itemsup.parent = po.name - and po.docstatus = 1 - and po.is_subcontracted = 'Yes' - and po.status != 'Closed' - and po.per_received < 100 - and itemsup.reserve_warehouse = %s''', (self.item_code, self.warehouse))[0][0] - #Get Transferred Entries - materials_transferred = frappe.db.sql(""" - select - ifnull(sum(CASE WHEN se.is_return = 1 THEN (transfer_qty * -1) ELSE transfer_qty END),0) - from - `tabStock Entry` se, `tabStock Entry Detail` sed, `tabPurchase Order` po - where - se.docstatus=1 - and se.purpose='Send to Subcontractor' - and ifnull(se.purchase_order, '') !='' - and (sed.item_code = %(item)s or sed.original_item = %(item)s) - and se.name = sed.parent - and se.purchase_order = po.name - and po.docstatus = 1 - and po.is_subcontracted = 'Yes' - and po.status != 'Closed' - and po.per_received < 100 - """, {'item': self.item_code})[0][0] + po = frappe.qb.DocType("Purchase Order") + supplied_item = frappe.qb.DocType("Purchase Order Item Supplied") + + reserved_qty_for_sub_contract = ( + frappe.qb + .from_(po) + .from_(supplied_item) + .select(Sum(Coalesce(supplied_item.required_qty, 0))) + .where( + (supplied_item.rm_item_code == self.item_code) + & (po.name == supplied_item.parent) + & (po.docstatus == 1) + & (po.is_subcontracted == "Yes") + & (po.status != "Closed") + & (po.per_received < 100) + & (supplied_item.reserve_warehouse == self.warehouse) + ) + ).run()[0][0] or 0.0 + + se = frappe.qb.DocType("Stock Entry") + se_item = frappe.qb.DocType("Stock Entry Detail") + + materials_transferred = ( + frappe.qb + .from_(se) + .from_(se_item) + .from_(po) + .select(Sum( + Case() + .when(se.is_return == 1, se_item.transfer_qty * -1) + .else_(se_item.transfer_qty) + )) + .where( + (se.docstatus == 1) + & (se.purpose == "Send to Subcontractor") + & (Coalesce(se.purchase_order, "") != "") + & ((se_item.item_code == self.item_code) + | (se_item.original_item == self.item_code)) + & (se.name == se_item.parent) + & (po.name == se.purchase_order) + & (po.docstatus == 1) + & (po.is_subcontracted == "Yes") + & (po.status != "Closed") + & (po.per_received < 100) + ) + ).run()[0][0] or 0.0 if reserved_qty_for_sub_contract > materials_transferred: reserved_qty_for_sub_contract = reserved_qty_for_sub_contract - materials_transferred @@ -100,47 +127,35 @@ def on_doctype_update(): def update_stock(bin_name, args, allow_negative_stock=False, via_landed_cost_voucher=False): - '''Called from erpnext.stock.utils.update_bin''' + """WARNING: This function is deprecated. Inline this function instead of using it.""" + from erpnext.stock.stock_ledger import repost_current_voucher + + repost_current_voucher(args, allow_negative_stock, via_landed_cost_voucher) update_qty(bin_name, args) - if args.get("actual_qty") or args.get("voucher_type") == "Stock Reconciliation": - from erpnext.stock.stock_ledger import update_entries_after, update_qty_in_future_sle - - if not args.get("posting_date"): - args["posting_date"] = nowdate() - - if args.get("is_cancelled") and via_landed_cost_voucher: - return - - # Reposts only current voucher SL Entries - # Updates valuation rate, stock value, stock queue for current transaction - update_entries_after({ - "item_code": args.get('item_code'), - "warehouse": args.get('warehouse'), - "posting_date": args.get("posting_date"), - "posting_time": args.get("posting_time"), - "voucher_type": args.get("voucher_type"), - "voucher_no": args.get("voucher_no"), - "sle_id": args.get('name'), - "creation": args.get('creation') - }, allow_negative_stock=allow_negative_stock, via_landed_cost_voucher=via_landed_cost_voucher) - - # update qty in future sle and Validate negative qty - update_qty_in_future_sle(args, allow_negative_stock) - def get_bin_details(bin_name): return frappe.db.get_value('Bin', bin_name, ['actual_qty', 'ordered_qty', 'reserved_qty', 'indented_qty', 'planned_qty', 'reserved_qty_for_production', 'reserved_qty_for_sub_contract'], as_dict=1) def update_qty(bin_name, args): - bin_details = get_bin_details(bin_name) + from erpnext.controllers.stock_controller import future_sle_exists - # update the stock values (for current quantities) - if args.get("voucher_type")=="Stock Reconciliation": - actual_qty = args.get('qty_after_transaction') - else: - actual_qty = bin_details.actual_qty + flt(args.get("actual_qty")) + bin_details = get_bin_details(bin_name) + # actual qty is already updated by processing current voucher + actual_qty = bin_details.actual_qty + + # actual qty is not up to date in case of backdated transaction + if future_sle_exists(args): + actual_qty = frappe.db.get_value("Stock Ledger Entry", + filters={ + "item_code": args.get("item_code"), + "warehouse": args.get("warehouse"), + "is_cancelled": 0 + }, + fieldname="qty_after_transaction", + order_by="posting_date desc, posting_time desc, creation desc", + ) or 0.0 ordered_qty = flt(bin_details.ordered_qty) + flt(args.get("ordered_qty")) reserved_qty = flt(bin_details.reserved_qty) + flt(args.get("reserved_qty")) @@ -160,4 +175,4 @@ def update_qty(bin_name, args): 'indented_qty': indented_qty, 'planned_qty': planned_qty, 'projected_qty': projected_qty - }) \ No newline at end of file + }) diff --git a/erpnext/stock/doctype/delivery_note/delivery_note.json b/erpnext/stock/doctype/delivery_note/delivery_note.json index ad1b3b43aee..55a4c956a67 100644 --- a/erpnext/stock/doctype/delivery_note/delivery_note.json +++ b/erpnext/stock/doctype/delivery_note/delivery_note.json @@ -145,6 +145,7 @@ "sales_team_section_break", "sales_partner", "column_break7", + "amount_eligible_for_commission", "commission_rate", "total_commission", "section_break1", @@ -1302,16 +1303,23 @@ "label": "Dispatch Address", "print_hide": 1, "read_only": 1 + }, + { + "fieldname": "amount_eligible_for_commission", + "fieldtype": "Currency", + "label": "Amount Eligible for Commission", + "read_only": 1 } ], "icon": "fa fa-truck", "idx": 146, "is_submittable": 1, "links": [], - "modified": "2021-10-08 14:29:13.428984", + "modified": "2021-10-09 14:29:13.428984", "modified_by": "Administrator", "module": "Stock", "name": "Delivery Note", + "naming_rule": "By \"Naming Series\" field", "owner": "Administrator", "permissions": [ { diff --git a/erpnext/stock/doctype/delivery_note/delivery_note.py b/erpnext/stock/doctype/delivery_note/delivery_note.py index 52684607b4b..70d48a42d72 100644 --- a/erpnext/stock/doctype/delivery_note/delivery_note.py +++ b/erpnext/stock/doctype/delivery_note/delivery_note.py @@ -138,6 +138,7 @@ class DeliveryNote(SellingController): self.update_current_stock() if not self.installation_status: self.installation_status = 'Not Installed' + self.reset_default_field_value("set_warehouse", "items", "warehouse") def validate_with_previous_doc(self): super(DeliveryNote, self).validate_with_previous_doc({ diff --git a/erpnext/stock/doctype/delivery_note/test_delivery_note.js b/erpnext/stock/doctype/delivery_note/test_delivery_note.js deleted file mode 100644 index 76f79894290..00000000000 --- a/erpnext/stock/doctype/delivery_note/test_delivery_note.js +++ /dev/null @@ -1,35 +0,0 @@ -QUnit.module('Stock'); - -QUnit.test("test delivery note", function(assert) { - assert.expect(2); - let done = assert.async(); - frappe.run_serially([ - () => { - return frappe.tests.make('Delivery Note', [ - {customer:'Test Customer 1'}, - {items: [ - [ - {'item_code': 'Test Product 1'}, - {'qty': 5}, - ] - ]}, - {shipping_address_name: 'Test1-Shipping'}, - {contact_person: 'Contact 1-Test Customer 1'}, - {taxes_and_charges: 'TEST In State GST - FT'}, - {tc_name: 'Test Term 1'}, - {transporter_name:'TEST TRANSPORT'}, - {lr_no:'MH-04-FG 1111'} - ]); - }, - () => cur_frm.save(), - () => { - // get_item_details - assert.ok(cur_frm.doc.items[0].item_name=='Test Product 1', "Item name correct"); - assert.ok(cur_frm.doc.grand_total==590, " Grand Total correct"); - }, - () => frappe.tests.click_button('Submit'), - () => frappe.tests.click_button('Yes'), - () => frappe.timeout(0.3), - () => done() - ]); -}); diff --git a/erpnext/stock/doctype/delivery_note/test_delivery_note_with_margin.js b/erpnext/stock/doctype/delivery_note/test_delivery_note_with_margin.js deleted file mode 100644 index 9f1375f563c..00000000000 --- a/erpnext/stock/doctype/delivery_note/test_delivery_note_with_margin.js +++ /dev/null @@ -1,36 +0,0 @@ -QUnit.module('Stock'); - -QUnit.test("test delivery note with margin", function(assert) { - assert.expect(3); - let done = assert.async(); - frappe.run_serially([ - () => { - return frappe.tests.make('Delivery Note', [ - {customer:'Test Customer 1'}, - {selling_price_list: 'Test-Selling-USD'}, - {currency: 'USD'}, - {items: [ - [ - {'item_code': 'Test Product 4'}, - {'qty': 1}, - {'margin_type': 'Amount'}, - {'margin_rate_or_amount': 10} - ] - ]}, - ]); - }, - - () => cur_frm.save(), - () => { - // get_rate_details - assert.ok(cur_frm.doc.items[0].rate_with_margin == 210, "Margin rate correct"); - assert.ok(cur_frm.doc.items[0].base_rate_with_margin == cur_frm.doc.conversion_rate * 210, "Base margin rate correct"); - assert.ok(cur_frm.doc.total == 210, "Amount correct"); - }, - - () => frappe.tests.click_button('Submit'), - () => frappe.tests.click_button('Yes'), - () => frappe.timeout(0.3), - () => done() - ]); -}); diff --git a/erpnext/stock/doctype/delivery_note_item/delivery_note_item.json b/erpnext/stock/doctype/delivery_note_item/delivery_note_item.json index a96c29925e5..51c88bed61d 100644 --- a/erpnext/stock/doctype/delivery_note_item/delivery_note_item.json +++ b/erpnext/stock/doctype/delivery_note_item/delivery_note_item.json @@ -49,6 +49,7 @@ "pricing_rules", "stock_uom_rate", "is_free_item", + "grant_commission", "section_break_25", "net_rate", "net_amount", @@ -753,13 +754,20 @@ "no_copy": 1, "options": "currency", "read_only": 1 + }, + { + "default": "0", + "fieldname": "grant_commission", + "fieldtype": "Check", + "label": "Grant Commission", + "read_only": 1 } ], "idx": 1, "index_web_pages_for_search": 1, "istable": 1, "links": [], - "modified": "2021-10-05 12:12:44.018872", + "modified": "2021-10-06 12:12:44.018872", "modified_by": "Administrator", "module": "Stock", "name": "Delivery Note Item", diff --git a/erpnext/stock/doctype/item/item.json b/erpnext/stock/doctype/item/item.json index 4b314a00a42..29abd45fcc8 100644 --- a/erpnext/stock/doctype/item/item.json +++ b/erpnext/stock/doctype/item/item.json @@ -88,6 +88,7 @@ "sales_details", "sales_uom", "is_sales_item", + "grant_commission", "column_break3", "max_discount", "deferred_revenue", @@ -360,8 +361,7 @@ "fieldname": "valuation_method", "fieldtype": "Select", "label": "Valuation Method", - "options": "\nFIFO\nMoving Average", - "set_only_once": 1 + "options": "\nFIFO\nMoving Average" }, { "depends_on": "is_stock_item", @@ -1020,6 +1020,12 @@ "fieldname": "website_image_alt", "fieldtype": "Data", "label": "Image Description" + }, + { + "default": "1", + "fieldname": "grant_commission", + "fieldtype": "Check", + "label": "Grant Commission" } ], "has_web_view": 1, @@ -1028,8 +1034,7 @@ "image_field": "image", "index_web_pages_for_search": 1, "links": [], - "max_attachments": 1, - "modified": "2021-10-27 21:04:00.324786", + "modified": "2021-12-14 04:13:16.857534", "modified_by": "Administrator", "module": "Stock", "name": "Item", @@ -1097,7 +1102,7 @@ "search_fields": "item_name,description,item_group,customer_code", "show_name_in_global_search": 1, "show_preview_popup": 1, - "sort_field": "idx desc,modified desc", + "sort_field": "modified", "sort_order": "DESC", "title_field": "item_name", "track_changes": 1 diff --git a/erpnext/stock/doctype/item/item.py b/erpnext/stock/doctype/item/item.py index fa42c7d934b..decf522d2f7 100644 --- a/erpnext/stock/doctype/item/item.py +++ b/erpnext/stock/doctype/item/item.py @@ -152,7 +152,6 @@ class Item(WebsiteGenerator): def on_update(self): invalidate_cache_for_item(self) - self.validate_name_with_item_group() self.update_variants() self.update_item_price() self.update_template_item() @@ -223,10 +222,11 @@ class Item(WebsiteGenerator): 'route')) + '/' + self.scrub((self.item_name or self.item_code) + '-' + random_string(5)) def validate_website_image(self): + """Validate if the website image is a public file""" + if frappe.flags.in_import: return - """Validate if the website image is a public file""" auto_set_website_image = False if not self.website_image and self.image: auto_set_website_image = True @@ -256,10 +256,11 @@ class Item(WebsiteGenerator): self.website_image = None def make_thumbnail(self): + """Make a thumbnail of `website_image`""" + if frappe.flags.in_import: return - """Make a thumbnail of `website_image`""" import requests.exceptions if not self.is_new() and self.website_image != frappe.db.get_value(self.doctype, self.name, "website_image"): @@ -628,12 +629,6 @@ class Item(WebsiteGenerator): where item_code = %s and is_cancelled = 0 limit 1""", self.name)) return self._stock_ledger_created - def validate_name_with_item_group(self): - # causes problem with tree build - if frappe.db.exists("Item Group", self.name): - frappe.throw( - _("An Item Group exists with same name, please change the item name or rename the item group")) - def update_item_price(self): frappe.db.sql(""" UPDATE `tabItem Price` @@ -729,7 +724,6 @@ class Item(WebsiteGenerator): def recalculate_bin_qty(self, new_name): from erpnext.stock.stock_balance import repost_stock - frappe.db.auto_commit_on_many_writes = 1 existing_allow_negative_stock = frappe.db.get_value("Stock Settings", None, "allow_negative_stock") frappe.db.set_value("Stock Settings", None, "allow_negative_stock", 1) @@ -743,7 +737,6 @@ class Item(WebsiteGenerator): repost_stock(new_name, warehouse) frappe.db.set_value("Stock Settings", None, "allow_negative_stock", existing_allow_negative_stock) - frappe.db.auto_commit_on_many_writes = 0 @frappe.whitelist() def copy_specification_from_item_group(self): diff --git a/erpnext/stock/doctype/item/test_item.py b/erpnext/stock/doctype/item/test_item.py index 7237178b15b..4028d933341 100644 --- a/erpnext/stock/doctype/item/test_item.py +++ b/erpnext/stock/doctype/item/test_item.py @@ -488,7 +488,7 @@ class TestItem(ERPNextTestCase): item_doc.save() # Check values saved correctly - barcodes = frappe.get_list( + barcodes = frappe.get_all( 'Item Barcode', fields=['barcode', 'barcode_type'], filters={'parent': item_code}) @@ -534,8 +534,6 @@ class TestItem(ERPNextTestCase): def test_index_creation(self): "check if index is getting created in db" - from erpnext.stock.doctype.item.item import on_doctype_update - on_doctype_update() indices = frappe.db.sql("show index from tabItem", as_dict=1) expected_columns = {"item_code", "item_name", "item_group", "route"} diff --git a/erpnext/stock/doctype/item/tests/test_item.js b/erpnext/stock/doctype/item/tests/test_item.js deleted file mode 100644 index 7f7e72d5c0f..00000000000 --- a/erpnext/stock/doctype/item/tests/test_item.js +++ /dev/null @@ -1,121 +0,0 @@ -QUnit.module('stock'); -QUnit.test("test: item", function (assert) { - assert.expect(6); - let done = assert.async(); - let keyboard_cost = 800; - let screen_cost = 4000; - let CPU_cost = 15000; - let scrap_cost = 100; - let no_of_items_to_stock = 100; - let is_stock_item = 1; - frappe.run_serially([ - // test item creation - () => frappe.set_route("List", "Item"), - - // Create a keyboard item - () => frappe.tests.make( - "Item", [ - {item_code: "Keyboard"}, - {item_group: "Products"}, - {is_stock_item: is_stock_item}, - {standard_rate: keyboard_cost}, - {opening_stock: no_of_items_to_stock}, - {default_warehouse: "Stores - FT"} - ] - ), - () => { - assert.ok(cur_frm.doc.item_name.includes('Keyboard'), - 'Item Keyboard created correctly'); - assert.ok(cur_frm.doc.item_code.includes('Keyboard'), - 'item_code for Keyboard set correctly'); - assert.ok(cur_frm.doc.item_group.includes('Products'), - 'item_group for Keyboard set correctly'); - assert.equal(cur_frm.doc.is_stock_item, is_stock_item, - 'is_stock_item for Keyboard set correctly'); - assert.equal(cur_frm.doc.standard_rate, keyboard_cost, - 'standard_rate for Keyboard set correctly'); - assert.equal(cur_frm.doc.opening_stock, no_of_items_to_stock, - 'opening_stock for Keyboard set correctly'); - }, - - // Create a Screen item - () => frappe.tests.make( - "Item", [ - {item_code: "Screen"}, - {item_group: "Products"}, - {is_stock_item: is_stock_item}, - {standard_rate: screen_cost}, - {opening_stock: no_of_items_to_stock}, - {default_warehouse: "Stores - FT"} - ] - ), - - // Create a CPU item - () => frappe.tests.make( - "Item", [ - {item_code: "CPU"}, - {item_group: "Products"}, - {is_stock_item: is_stock_item}, - {standard_rate: CPU_cost}, - {opening_stock: no_of_items_to_stock}, - {default_warehouse: "Stores - FT"} - ] - ), - - // Create a laptop item - () => frappe.tests.make( - "Item", [ - {item_code: "Laptop"}, - {item_group: "Products"}, - {default_warehouse: "Stores - FT"} - ] - ), - () => frappe.tests.make( - "Item", [ - {item_code: "Computer"}, - {item_group: "Products"}, - {is_stock_item: 0}, - ] - ), - - // Create a scrap item - () => frappe.tests.make( - "Item", [ - {item_code: "Scrap item"}, - {item_group: "Products"}, - {is_stock_item: is_stock_item}, - {standard_rate: scrap_cost}, - {opening_stock: no_of_items_to_stock}, - {default_warehouse: "Stores - FT"} - ] - ), - () => frappe.tests.make( - "Item", [ - {item_code: "Test Product 4"}, - {item_group: "Products"}, - {is_stock_item: 1}, - {has_batch_no: 1}, - {create_new_batch: 1}, - {uoms: - [ - [ - {uom:"Unit"}, - {conversion_factor: 10}, - ] - ] - }, - {taxes: - [ - [ - {tax_type:"SGST - "+frappe.get_abbr(frappe.defaults.get_default("Company"))}, - {tax_rate: 0}, - ] - ]}, - {has_serial_no: 1}, - {standard_rate: 100}, - {opening_stock: 100}, - ] - ), - () => done() - ]); -}); diff --git a/erpnext/stock/doctype/item_price/test_item_price.js b/erpnext/stock/doctype/item_price/test_item_price.js deleted file mode 100644 index 49dbaa2051e..00000000000 --- a/erpnext/stock/doctype/item_price/test_item_price.js +++ /dev/null @@ -1,22 +0,0 @@ -QUnit.module('Stock'); - -QUnit.test("test item price", function(assert) { - assert.expect(2); - let done = assert.async(); - frappe.run_serially([ - () => { - return frappe.tests.make('Item Price', [ - {price_list:'Test-Selling-USD'}, - {item_code: 'Test Product 4'}, - {price_list_rate: 200} - ]); - }, - () => cur_frm.save(), - () => { - assert.ok(cur_frm.doc.item_name == 'Test Product 4', "Item name correct"); - assert.ok(cur_frm.doc.price_list_rate == 200, "Price list rate correct"); - }, - () => frappe.timeout(0.3), - () => done() - ]); -}); diff --git a/erpnext/stock/doctype/landed_cost_voucher/landed_cost_voucher.js b/erpnext/stock/doctype/landed_cost_voucher/landed_cost_voucher.js index 433f78adc96..9c1a809f4df 100644 --- a/erpnext/stock/doctype/landed_cost_voucher/landed_cost_voucher.js +++ b/erpnext/stock/doctype/landed_cost_voucher/landed_cost_voucher.js @@ -35,7 +35,7 @@ erpnext.stock.LandedCostVoucher = class LandedCostVoucher extends erpnext.stock. refresh() { var help_content = `

- +

diff --git a/erpnext/stock/doctype/material_request/material_request.py b/erpnext/stock/doctype/material_request/material_request.py index d717c50919f..103e8d6a88c 100644 --- a/erpnext/stock/doctype/material_request/material_request.py +++ b/erpnext/stock/doctype/material_request/material_request.py @@ -80,6 +80,9 @@ class MaterialRequest(BuyingController): # NOTE: Since Item BOM and FG quantities are combined, using current data, it cannot be validated # Though the creation of Material Request from a Production Plan can be rethought to fix this + self.reset_default_field_value("set_warehouse", "items", "warehouse") + self.reset_default_field_value("set_from_warehouse", "items", "from_warehouse") + def set_title(self): '''Set title as comma separated list of items''' if not self.title: diff --git a/erpnext/stock/doctype/material_request/tests/test_material_request.js b/erpnext/stock/doctype/material_request/tests/test_material_request.js deleted file mode 100644 index a2cd03b6495..00000000000 --- a/erpnext/stock/doctype/material_request/tests/test_material_request.js +++ /dev/null @@ -1,39 +0,0 @@ -QUnit.module('Stock'); - -QUnit.test("test material request", function(assert) { - assert.expect(5); - let done = assert.async(); - frappe.run_serially([ - () => { - return frappe.tests.make('Material Request', [ - {items: [ - [ - {'schedule_date': frappe.datetime.add_days(frappe.datetime.nowdate(), 5)}, - {'qty': 5}, - {'item_code': 'Test Product 1'}, - ], - [ - {'schedule_date': frappe.datetime.add_days(frappe.datetime.nowdate(), 6)}, - {'qty': 2}, - {'item_code': 'Test Product 2'}, - ] - ]}, - ]); - }, - () => cur_frm.save(), - () => { - assert.ok(cur_frm.doc.schedule_date == frappe.datetime.add_days(frappe.datetime.now_date(), 5), "Schedule Date correct"); - - // get_item_details - assert.ok(cur_frm.doc.items[0].item_name=='Test Product 1', "Item name correct"); - assert.ok(cur_frm.doc.items[0].schedule_date == frappe.datetime.add_days(frappe.datetime.now_date(), 5), "Schedule Date correct"); - - assert.ok(cur_frm.doc.items[1].item_name=='Test Product 2', "Item name correct"); - assert.ok(cur_frm.doc.items[1].schedule_date == frappe.datetime.add_days(frappe.datetime.now_date(), 6), "Schedule Date correct"); - }, - () => frappe.tests.click_button('Submit'), - () => frappe.tests.click_button('Yes'), - () => frappe.timeout(0.3), - () => done() - ]); -}); diff --git a/erpnext/stock/doctype/material_request/tests/test_material_request_from_bom.js b/erpnext/stock/doctype/material_request/tests/test_material_request_from_bom.js deleted file mode 100644 index 6fb55ae02ac..00000000000 --- a/erpnext/stock/doctype/material_request/tests/test_material_request_from_bom.js +++ /dev/null @@ -1,27 +0,0 @@ -QUnit.module('manufacturing'); - -QUnit.test("test material request get items from BOM", function(assert) { - assert.expect(4); - let done = assert.async(); - frappe.run_serially([ - () => frappe.set_route('Form', 'BOM'), - () => frappe.timeout(3), - () => frappe.click_button('Get Items from BOM'), - () => frappe.timeout(3), - () => { - assert.ok(cur_dialog, 'dialog appeared'); - }, - () => cur_dialog.set_value('bom', 'Laptop'), - () => cur_dialog.set_value('warehouse', 'Laptop Scrap Warehouse'), - () => frappe.click_button('Get Items from BOM'), - () => frappe.timeout(3), - () => { - assert.ok(cur_frm.doc.items[0].item_code, "First row is not empty"); - assert.ok(cur_frm.doc.items[0].item_name, "Item name is not empty"); - assert.equal(cur_frm.doc.items[0].item_name, "Laptop", cur_frm.doc.items[0].item_name); - }, - () => cur_frm.doc.items[0].schedule_date = '2017-12-12', - () => cur_frm.save(), - () => done() - ]); -}); diff --git a/erpnext/stock/doctype/material_request/tests/test_material_request_type_manufacture.js b/erpnext/stock/doctype/material_request/tests/test_material_request_type_manufacture.js deleted file mode 100644 index 137079b9838..00000000000 --- a/erpnext/stock/doctype/material_request/tests/test_material_request_type_manufacture.js +++ /dev/null @@ -1,29 +0,0 @@ -QUnit.module('Stock'); - -QUnit.test("test material request", function(assert) { - assert.expect(1); - let done = assert.async(); - frappe.run_serially([ - () => { - return frappe.tests.make('Material Request', [ - {material_request_type:'Manufacture'}, - {items: [ - [ - {'schedule_date': frappe.datetime.add_days(frappe.datetime.nowdate(), 5)}, - {'qty': 5}, - {'item_code': 'Test Product 1'}, - ] - ]}, - ]); - }, - () => cur_frm.save(), - () => { - // get_item_details - assert.ok(cur_frm.doc.items[0].item_name=='Test Product 1', "Item name correct"); - }, - () => frappe.tests.click_button('Submit'), - () => frappe.tests.click_button('Yes'), - () => frappe.timeout(0.3), - () => done() - ]); -}); diff --git a/erpnext/stock/doctype/material_request/tests/test_material_request_type_material_issue.js b/erpnext/stock/doctype/material_request/tests/test_material_request_type_material_issue.js deleted file mode 100644 index b03a8543c6f..00000000000 --- a/erpnext/stock/doctype/material_request/tests/test_material_request_type_material_issue.js +++ /dev/null @@ -1,29 +0,0 @@ -QUnit.module('Stock'); - -QUnit.test("test material request for issue", function(assert) { - assert.expect(1); - let done = assert.async(); - frappe.run_serially([ - () => { - return frappe.tests.make('Material Request', [ - {material_request_type:'Material Issue'}, - {items: [ - [ - {'schedule_date': frappe.datetime.add_days(frappe.datetime.nowdate(), 5)}, - {'qty': 5}, - {'item_code': 'Test Product 1'}, - ] - ]}, - ]); - }, - () => cur_frm.save(), - () => { - // get_item_details - assert.ok(cur_frm.doc.items[0].item_name=='Test Product 1', "Item name correct"); - }, - () => frappe.tests.click_button('Submit'), - () => frappe.tests.click_button('Yes'), - () => frappe.timeout(0.3), - () => done() - ]); -}); diff --git a/erpnext/stock/doctype/material_request/tests/test_material_request_type_material_transfer.js b/erpnext/stock/doctype/material_request/tests/test_material_request_type_material_transfer.js deleted file mode 100644 index 7c62c2e63a1..00000000000 --- a/erpnext/stock/doctype/material_request/tests/test_material_request_type_material_transfer.js +++ /dev/null @@ -1,29 +0,0 @@ -QUnit.module('Stock'); - -QUnit.test("test material request for transfer", function(assert) { - assert.expect(1); - let done = assert.async(); - frappe.run_serially([ - () => { - return frappe.tests.make('Material Request', [ - {material_request_type:'Manufacture'}, - {items: [ - [ - {'schedule_date': frappe.datetime.add_days(frappe.datetime.nowdate(), 5)}, - {'qty': 5}, - {'item_code': 'Test Product 1'}, - ] - ]}, - ]); - }, - () => cur_frm.save(), - () => { - // get_item_details - assert.ok(cur_frm.doc.items[0].item_name=='Test Product 1', "Item name correct"); - }, - () => frappe.tests.click_button('Submit'), - () => frappe.tests.click_button('Yes'), - () => frappe.timeout(0.3), - () => done() - ]); -}); diff --git a/erpnext/stock/doctype/packed_item/packed_item.py b/erpnext/stock/doctype/packed_item/packed_item.py index 3f73093d673..e4091c40dc4 100644 --- a/erpnext/stock/doctype/packed_item/packed_item.py +++ b/erpnext/stock/doctype/packed_item/packed_item.py @@ -108,9 +108,32 @@ def cleanup_packing_list(doc, parent_items): packed_items = doc.get("packed_items") doc.set("packed_items", []) + for d in packed_items: if d not in delete_list: - doc.append("packed_items", d) + add_item_to_packing_list(doc, d) + +def add_item_to_packing_list(doc, packed_item): + doc.append("packed_items", { + 'parent_item': packed_item.parent_item, + 'item_code': packed_item.item_code, + 'item_name': packed_item.item_name, + 'uom': packed_item.uom, + 'qty': packed_item.qty, + 'rate': packed_item.rate, + 'conversion_factor': packed_item.conversion_factor, + 'description': packed_item.description, + 'warehouse': packed_item.warehouse, + 'batch_no': packed_item.batch_no, + 'actual_batch_qty': packed_item.actual_batch_qty, + 'serial_no': packed_item.serial_no, + 'target_warehouse': packed_item.target_warehouse, + 'actual_qty': packed_item.actual_qty, + 'projected_qty': packed_item.projected_qty, + 'incoming_rate': packed_item.incoming_rate, + 'prevdoc_doctype': packed_item.prevdoc_doctype, + 'parent_detail_docname': packed_item.parent_detail_docname + }) def update_product_bundle_price(doc, parent_items): """Updates the prices of Product Bundles based on the rates of the Items in the bundle.""" @@ -128,7 +151,8 @@ def update_product_bundle_price(doc, parent_items): else: update_parent_item_price(doc, parent_items[parent_items_index][0], bundle_price) - bundle_price = 0 + bundle_item_rate = bundle_item.rate if bundle_item.rate else 0 + bundle_price = bundle_item.qty * bundle_item_rate parent_items_index += 1 # for the last product bundle diff --git a/erpnext/stock/doctype/packing_slip_item/packing_slip_item.json b/erpnext/stock/doctype/packing_slip_item/packing_slip_item.json index 29c4193f9e9..4270839bfdb 100644 --- a/erpnext/stock/doctype/packing_slip_item/packing_slip_item.json +++ b/erpnext/stock/doctype/packing_slip_item/packing_slip_item.json @@ -1,451 +1,140 @@ { - "allow_copy": 0, - "allow_import": 0, - "allow_rename": 0, - "autoname": "hash", - "beta": 0, - "creation": "2013-04-08 13:10:16", - "custom": 0, - "docstatus": 0, - "doctype": "DocType", - "document_type": "Document", - "editable_grid": 1, - "engine": "InnoDB", + "actions": [], + "autoname": "hash", + "creation": "2013-04-08 13:10:16", + "doctype": "DocType", + "document_type": "Document", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "item_code", + "column_break_2", + "item_name", + "batch_no", + "desc_section", + "description", + "quantity_section", + "qty", + "net_weight", + "column_break_10", + "stock_uom", + "weight_uom", + "page_break", + "dn_detail" + ], "fields": [ { - "allow_on_submit": 0, - "bold": 0, - "collapsible": 0, - "columns": 0, - "fieldname": "item_code", - "fieldtype": "Link", - "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": "Item Code", - "length": 0, - "no_copy": 0, - "options": "Item", - "permlevel": 0, - "print_hide": 0, - "print_hide_if_no_value": 0, - "print_width": "100px", - "read_only": 0, - "remember_last_selected_value": 0, - "report_hide": 0, - "reqd": 1, - "search_index": 0, - "set_only_once": 0, - "unique": 0, + "fieldname": "item_code", + "fieldtype": "Link", + "in_global_search": 1, + "in_list_view": 1, + "label": "Item Code", + "options": "Item", + "print_width": "100px", + "reqd": 1, "width": "100px" - }, + }, { - "allow_on_submit": 0, - "bold": 0, - "collapsible": 0, - "columns": 0, - "fieldname": "column_break_2", - "fieldtype": "Column Break", - "hidden": 0, - "ignore_user_permissions": 0, - "ignore_xss_filter": 0, - "in_filter": 0, - "in_global_search": 0, - "in_list_view": 0, - "in_standard_filter": 0, - "length": 0, - "no_copy": 0, - "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, - "unique": 0 - }, + "fieldname": "column_break_2", + "fieldtype": "Column Break" + }, { - "allow_on_submit": 0, - "bold": 0, - "collapsible": 0, - "columns": 0, - "fieldname": "item_name", - "fieldtype": "Data", - "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": "Item Name", - "length": 0, - "no_copy": 0, - "options": "item_code.item_name", - "permlevel": 0, - "print_hide": 0, - "print_hide_if_no_value": 0, - "print_width": "200px", - "read_only": 1, - "remember_last_selected_value": 0, - "report_hide": 0, - "reqd": 0, - "search_index": 0, - "set_only_once": 0, - "unique": 0, + "fetch_from": "item_code.item_name", + "fieldname": "item_name", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Item Name", + "print_width": "200px", + "read_only": 1, "width": "200px" - }, + }, { - "allow_on_submit": 0, - "bold": 0, - "collapsible": 0, - "columns": 0, - "fieldname": "batch_no", - "fieldtype": "Link", - "hidden": 0, - "ignore_user_permissions": 0, - "ignore_xss_filter": 0, - "in_filter": 0, - "in_global_search": 0, - "in_list_view": 0, - "in_standard_filter": 0, - "label": "Batch No", - "length": 0, - "no_copy": 0, - "options": "Batch", - "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, - "unique": 0 - }, + "fieldname": "batch_no", + "fieldtype": "Link", + "label": "Batch No", + "options": "Batch" + }, { - "allow_on_submit": 0, - "bold": 0, - "collapsible": 1, - "columns": 0, - "fieldname": "desc_section", - "fieldtype": "Section Break", - "hidden": 0, - "ignore_user_permissions": 0, - "ignore_xss_filter": 0, - "in_filter": 0, - "in_global_search": 0, - "in_list_view": 0, - "in_standard_filter": 0, - "label": "Description", - "length": 0, - "no_copy": 0, - "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, - "unique": 0 - }, + "collapsible": 1, + "fieldname": "desc_section", + "fieldtype": "Section Break", + "label": "Description" + }, { - "allow_on_submit": 0, - "bold": 0, - "collapsible": 0, - "columns": 0, - "fieldname": "description", - "fieldtype": "Text Editor", - "hidden": 0, - "ignore_user_permissions": 0, - "ignore_xss_filter": 0, - "in_filter": 0, - "in_global_search": 0, - "in_list_view": 0, - "in_standard_filter": 0, - "label": "Description", - "length": 0, - "no_copy": 0, - "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, - "unique": 0 - }, + "fieldname": "description", + "fieldtype": "Text Editor", + "label": "Description" + }, { - "allow_on_submit": 0, - "bold": 0, - "collapsible": 0, - "columns": 0, - "fieldname": "quantity_section", - "fieldtype": "Section Break", - "hidden": 0, - "ignore_user_permissions": 0, - "ignore_xss_filter": 0, - "in_filter": 0, - "in_global_search": 0, - "in_list_view": 0, - "in_standard_filter": 0, - "label": "Quantity", - "length": 0, - "no_copy": 0, - "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, - "unique": 0 - }, + "fieldname": "quantity_section", + "fieldtype": "Section Break", + "label": "Quantity" + }, { - "allow_on_submit": 0, - "bold": 0, - "collapsible": 0, - "columns": 0, - "fieldname": "qty", - "fieldtype": "Float", - "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": "Quantity", - "length": 0, - "no_copy": 0, - "permlevel": 0, - "print_hide": 0, - "print_hide_if_no_value": 0, - "print_width": "100px", - "read_only": 0, - "remember_last_selected_value": 0, - "report_hide": 0, - "reqd": 1, - "search_index": 0, - "set_only_once": 0, - "unique": 0, + "fieldname": "qty", + "fieldtype": "Float", + "in_list_view": 1, + "label": "Quantity", + "print_width": "100px", + "reqd": 1, "width": "100px" - }, + }, { - "allow_on_submit": 0, - "bold": 0, - "collapsible": 0, - "columns": 0, - "fieldname": "net_weight", - "fieldtype": "Float", - "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": "Net Weight", - "length": 0, - "no_copy": 0, - "permlevel": 0, - "print_hide": 0, - "print_hide_if_no_value": 0, - "print_width": "100px", - "read_only": 0, - "remember_last_selected_value": 0, - "report_hide": 0, - "reqd": 0, - "search_index": 0, - "set_only_once": 0, - "unique": 0, + "fieldname": "net_weight", + "fieldtype": "Float", + "in_list_view": 1, + "label": "Net Weight", + "print_width": "100px", "width": "100px" - }, + }, { - "allow_on_submit": 0, - "bold": 0, - "collapsible": 0, - "columns": 0, - "fieldname": "column_break_10", - "fieldtype": "Column Break", - "hidden": 0, - "ignore_user_permissions": 0, - "ignore_xss_filter": 0, - "in_filter": 0, - "in_global_search": 0, - "in_list_view": 0, - "in_standard_filter": 0, - "length": 0, - "no_copy": 0, - "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, - "unique": 0 - }, + "fieldname": "column_break_10", + "fieldtype": "Column Break" + }, { - "allow_on_submit": 0, - "bold": 0, - "collapsible": 0, - "columns": 0, - "fieldname": "stock_uom", - "fieldtype": "Link", - "hidden": 0, - "ignore_user_permissions": 0, - "ignore_xss_filter": 0, - "in_filter": 0, - "in_global_search": 0, - "in_list_view": 0, - "in_standard_filter": 0, - "label": "UOM", - "length": 0, - "no_copy": 0, - "options": "UOM", - "permlevel": 0, - "print_hide": 0, - "print_hide_if_no_value": 0, - "print_width": "100px", - "read_only": 1, - "remember_last_selected_value": 0, - "report_hide": 0, - "reqd": 0, - "search_index": 0, - "set_only_once": 0, - "unique": 0, + "fieldname": "stock_uom", + "fieldtype": "Link", + "label": "UOM", + "options": "UOM", + "print_width": "100px", + "read_only": 1, "width": "100px" - }, + }, { - "allow_on_submit": 0, - "bold": 0, - "collapsible": 0, - "columns": 0, - "fieldname": "weight_uom", - "fieldtype": "Link", - "hidden": 0, - "ignore_user_permissions": 0, - "ignore_xss_filter": 0, - "in_filter": 0, - "in_global_search": 0, - "in_list_view": 0, - "in_standard_filter": 0, - "label": "Weight UOM", - "length": 0, - "no_copy": 0, - "options": "UOM", - "permlevel": 0, - "print_hide": 0, - "print_hide_if_no_value": 0, - "print_width": "100px", - "read_only": 0, - "remember_last_selected_value": 0, - "report_hide": 0, - "reqd": 0, - "search_index": 0, - "set_only_once": 0, - "unique": 0, + "fieldname": "weight_uom", + "fieldtype": "Link", + "label": "Weight UOM", + "options": "UOM", + "print_width": "100px", "width": "100px" - }, + }, { - "allow_on_submit": 1, - "bold": 0, - "collapsible": 0, - "columns": 0, - "fieldname": "page_break", - "fieldtype": "Check", - "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": "Page Break", - "length": 0, - "no_copy": 0, - "permlevel": 0, - "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, - "unique": 0 - }, + "allow_on_submit": 1, + "default": "0", + "fieldname": "page_break", + "fieldtype": "Check", + "in_list_view": 1, + "label": "Page Break" + }, { - "allow_on_submit": 0, - "bold": 0, - "collapsible": 0, - "columns": 0, - "fieldname": "dn_detail", - "fieldtype": "Data", - "hidden": 1, - "ignore_user_permissions": 0, - "ignore_xss_filter": 0, - "in_filter": 0, - "in_global_search": 0, - "in_list_view": 1, - "in_standard_filter": 0, - "label": "DN Detail", - "length": 0, - "no_copy": 0, - "permlevel": 0, - "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, - "unique": 0 + "fieldname": "dn_detail", + "fieldtype": "Data", + "hidden": 1, + "in_list_view": 1, + "label": "DN Detail" } - ], - "hide_heading": 0, - "hide_toolbar": 0, - "idx": 1, - "image_view": 0, - "in_create": 0, - - "is_submittable": 0, - "issingle": 0, - "istable": 1, - "max_attachments": 0, - "modified": "2018-06-01 07:21:58.220980", - "modified_by": "Administrator", - "module": "Stock", - "name": "Packing Slip Item", - "owner": "Administrator", - "permissions": [], - "quick_entry": 0, - "read_only": 0, - "read_only_onload": 0, - "show_name_in_global_search": 0, - "track_changes": 1, - "track_seen": 0 + ], + "idx": 1, + "istable": 1, + "links": [], + "modified": "2021-12-14 01:22:00.715935", + "modified_by": "Administrator", + "module": "Stock", + "name": "Packing Slip Item", + "naming_rule": "Random", + "owner": "Administrator", + "permissions": [], + "sort_field": "modified", + "sort_order": "DESC", + "track_changes": 1 } \ No newline at end of file diff --git a/erpnext/stock/doctype/price_list/test_price_list_uom.js b/erpnext/stock/doctype/price_list/test_price_list_uom.js deleted file mode 100644 index 3896c0e59ea..00000000000 --- a/erpnext/stock/doctype/price_list/test_price_list_uom.js +++ /dev/null @@ -1,58 +0,0 @@ -QUnit.module('Price List'); - -QUnit.test("test price list with uom dependancy", function(assert) { - assert.expect(2); - let done = assert.async(); - frappe.run_serially([ - - () => frappe.set_route('Form', 'Price List', 'Standard Buying'), - () => { - cur_frm.set_value('price_not_uom_dependent','1'); - frappe.timeout(1); - }, - () => cur_frm.save(), - - () => frappe.timeout(1), - - () => { - return frappe.tests.make('Item Price', [ - {price_list:'Standard Buying'}, - {item_code: 'Test Product 3'}, - {price_list_rate: 200} - ]); - }, - - () => cur_frm.save(), - - () => { - return frappe.tests.make('Purchase Order', [ - {supplier: 'Test Supplier'}, - {currency: 'INR'}, - {buying_price_list: 'Standard Buying'}, - {items: [ - [ - {"item_code": 'Test Product 3'}, - {"schedule_date": frappe.datetime.add_days(frappe.datetime.now_date(), 2)}, - {"uom": 'Nos'}, - {"conversion_factor": 3} - ] - ]}, - - ]); - }, - - () => cur_frm.save(), - () => frappe.timeout(0.3), - - () => { - assert.ok(cur_frm.doc.items[0].item_name == 'Test Product 3', "Item code correct"); - assert.ok(cur_frm.doc.items[0].price_list_rate == 200, "Price list rate correct"); - }, - - () => frappe.tests.click_button('Submit'), - () => frappe.tests.click_button('Yes'), - () => frappe.timeout(1), - - () => done() - ]); -}); diff --git a/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py b/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py index 762f45f75f2..c97b306c4e6 100644 --- a/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py +++ b/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py @@ -118,6 +118,10 @@ class PurchaseReceipt(BuyingController): if getdate(self.posting_date) > getdate(nowdate()): throw(_("Posting Date cannot be future date")) + self.reset_default_field_value("set_warehouse", "items", "warehouse") + self.reset_default_field_value("rejected_warehouse", "items", "rejected_warehouse") + self.reset_default_field_value("set_from_warehouse", "items", "from_warehouse") + def validate_cwip_accounts(self): for item in self.get('items'): diff --git a/erpnext/stock/doctype/purchase_receipt/test_purchase_receipt.js b/erpnext/stock/doctype/purchase_receipt/test_purchase_receipt.js deleted file mode 100644 index d1f448536bc..00000000000 --- a/erpnext/stock/doctype/purchase_receipt/test_purchase_receipt.js +++ /dev/null @@ -1,42 +0,0 @@ -QUnit.module('Stock'); - -QUnit.test("test Purchase Receipt", function(assert) { - assert.expect(4); - let done = assert.async(); - frappe.run_serially([ - () => { - return frappe.tests.make('Purchase Receipt', [ - {supplier: 'Test Supplier'}, - {items: [ - [ - {'received_qty': 5}, - {'qty': 4}, - {'item_code': 'Test Product 1'}, - {'uom': 'Nos'}, - {'warehouse':'Stores - '+frappe.get_abbr(frappe.defaults.get_default('Company'))}, - {'rejected_warehouse':'Work In Progress - '+frappe.get_abbr(frappe.defaults.get_default('Company'))}, - ] - ]}, - {taxes_and_charges: 'TEST In State GST - FT'}, - {tc_name: 'Test Term 1'}, - {terms: 'This is Test'} - ]); - }, - () => cur_frm.save(), - () => { - // get_item_details - assert.ok(cur_frm.doc.items[0].item_name=='Test Product 1', "Item name correct"); - // get tax details - assert.ok(cur_frm.doc.taxes_and_charges=='TEST In State GST - FT', "Tax details correct"); - // get tax account head details - assert.ok(cur_frm.doc.taxes[0].account_head=='CGST - '+frappe.get_abbr(frappe.defaults.get_default('Company')), " Account Head abbr correct"); - // grand_total Calculated - assert.ok(cur_frm.doc.grand_total==472, "Grad Total correct"); - - }, - () => frappe.tests.click_button('Submit'), - () => frappe.tests.click_button('Yes'), - () => frappe.timeout(0.3), - () => done() - ]); -}); diff --git a/erpnext/stock/doctype/purchase_receipt/test_purchase_receipt.py b/erpnext/stock/doctype/purchase_receipt/test_purchase_receipt.py index 102730b055f..2909a2d2e74 100644 --- a/erpnext/stock/doctype/purchase_receipt/test_purchase_receipt.py +++ b/erpnext/stock/doctype/purchase_receipt/test_purchase_receipt.py @@ -10,6 +10,7 @@ from frappe.utils import add_days, cint, cstr, flt, today import erpnext from erpnext.accounts.doctype.account.test_account import get_inventory_account +from erpnext.controllers.buying_controller import QtyMismatchError from erpnext.stock.doctype.item.test_item import create_item, make_item from erpnext.stock.doctype.purchase_receipt.purchase_receipt import make_purchase_invoice from erpnext.stock.doctype.serial_no.serial_no import SerialNoDuplicateError, get_serial_nos @@ -22,20 +23,54 @@ class TestPurchaseReceipt(ERPNextTestCase): def setUp(self): frappe.db.set_value("Buying Settings", None, "allow_multiple_items", 1) + def test_purchase_receipt_received_qty(self): + """ + 1. Test if received qty is validated against accepted + rejected + 2. Test if received qty is auto set on save + """ + pr = make_purchase_receipt( + qty=1, + rejected_qty=1, + received_qty=3, + item_code="_Test Item Home Desktop 200", + do_not_save=True + ) + self.assertRaises(QtyMismatchError, pr.save) + + pr.items[0].received_qty = 0 + pr.save() + self.assertEqual(pr.items[0].received_qty, 2) + + # teardown + pr.delete() + def test_reverse_purchase_receipt_sle(self): pr = make_purchase_receipt(qty=0.5, item_code="_Test Item Home Desktop 200") - sl_entry = frappe.db.get_all("Stock Ledger Entry", {"voucher_type": "Purchase Receipt", - "voucher_no": pr.name}, ['actual_qty']) + sl_entry = frappe.db.get_all( + "Stock Ledger Entry", + { + "voucher_type": "Purchase Receipt", + "voucher_no": pr.name + }, + ['actual_qty'] + ) self.assertEqual(len(sl_entry), 1) self.assertEqual(sl_entry[0].actual_qty, 0.5) pr.cancel() - sl_entry_cancelled = frappe.db.get_all("Stock Ledger Entry", {"voucher_type": "Purchase Receipt", - "voucher_no": pr.name}, ['actual_qty'], order_by='creation') + sl_entry_cancelled = frappe.db.get_all( + "Stock Ledger Entry", + { + "voucher_type": "Purchase Receipt", + "voucher_no": pr.name + }, + ['actual_qty'], + order_by='creation' + ) self.assertEqual(len(sl_entry_cancelled), 2) self.assertEqual(sl_entry_cancelled[1].actual_qty, -0.5) @@ -61,8 +96,15 @@ class TestPurchaseReceipt(ERPNextTestCase): }] }).insert() - template = frappe.db.get_value('Payment Terms Template', '_Test Payment Terms Template For Purchase Invoice') - old_template_in_supplier = frappe.db.get_value("Supplier", "_Test Supplier", "payment_terms") + template = frappe.db.get_value( + "Payment Terms Template", + "_Test Payment Terms Template For Purchase Invoice" + ) + old_template_in_supplier = frappe.db.get_value( + "Supplier", + "_Test Supplier", + "payment_terms" + ) frappe.db.set_value("Supplier", "_Test Supplier", "payment_terms", template) pr = make_purchase_receipt(do_not_save=True) @@ -88,30 +130,59 @@ class TestPurchaseReceipt(ERPNextTestCase): # teardown pi.delete() # draft PI pr.cancel() - frappe.db.set_value("Supplier", "_Test Supplier", "payment_terms", old_template_in_supplier) - frappe.get_doc('Payment Terms Template', '_Test Payment Terms Template For Purchase Invoice').delete() + frappe.db.set_value( + "Supplier", + "_Test Supplier", + "payment_terms", + old_template_in_supplier + ) + frappe.get_doc( + "Payment Terms Template", + "_Test Payment Terms Template For Purchase Invoice" + ).delete() def test_purchase_receipt_no_gl_entry(self): from erpnext.stock.doctype.stock_entry.test_stock_entry import make_stock_entry - company = frappe.db.get_value('Warehouse', '_Test Warehouse - _TC', 'company') - - existing_bin_qty, existing_bin_stock_value = frappe.db.get_value("Bin", {"item_code": "_Test Item", - "warehouse": "_Test Warehouse - _TC"}, ["actual_qty", "stock_value"]) + existing_bin_qty, existing_bin_stock_value = frappe.db.get_value( + "Bin", + { + "item_code": "_Test Item", + "warehouse": "_Test Warehouse - _TC" + }, + ["actual_qty", "stock_value"] + ) if existing_bin_qty < 0: - make_stock_entry(item_code="_Test Item", target="_Test Warehouse - _TC", qty=abs(existing_bin_qty)) + make_stock_entry( + item_code="_Test Item", + target="_Test Warehouse - _TC", + qty=abs(existing_bin_qty) + ) pr = make_purchase_receipt() - stock_value_difference = frappe.db.get_value("Stock Ledger Entry", - {"voucher_type": "Purchase Receipt", "voucher_no": pr.name, - "item_code": "_Test Item", "warehouse": "_Test Warehouse - _TC"}, "stock_value_difference") + stock_value_difference = frappe.db.get_value( + "Stock Ledger Entry", + { + "voucher_type": "Purchase Receipt", + "voucher_no": pr.name, + "item_code": "_Test Item", + "warehouse": "_Test Warehouse - _TC" + }, + "stock_value_difference" + ) self.assertEqual(stock_value_difference, 250) - current_bin_stock_value = frappe.db.get_value("Bin", {"item_code": "_Test Item", - "warehouse": "_Test Warehouse - _TC"}, "stock_value") + current_bin_stock_value = frappe.db.get_value( + "Bin", + { + "item_code": "_Test Item", + "warehouse": "_Test Warehouse - _TC" + }, + "stock_value" + ) self.assertEqual(current_bin_stock_value, existing_bin_stock_value + 250) self.assertFalse(get_gl_entries("Purchase Receipt", pr.name)) @@ -133,13 +204,17 @@ class TestPurchaseReceipt(ERPNextTestCase): pr = make_purchase_receipt(item_code=item.name, qty=5, rate=500) - self.assertTrue(frappe.db.get_value('Batch', {'item': item.name, 'reference_name': pr.name})) + self.assertTrue( + frappe.db.get_value('Batch', {'item': item.name, 'reference_name': pr.name}) + ) pr.load_from_db() batch_no = pr.items[0].batch_no pr.cancel() - self.assertFalse(frappe.db.get_value('Batch', {'item': item.name, 'reference_name': pr.name})) + self.assertFalse( + frappe.db.get_value('Batch', {'item': item.name, 'reference_name': pr.name}) + ) self.assertFalse(frappe.db.get_all('Serial No', {'batch_no': batch_no})) def test_duplicate_serial_nos(self): @@ -158,42 +233,78 @@ class TestPurchaseReceipt(ERPNextTestCase): pr = make_purchase_receipt(item_code=item.name, qty=2, rate=500) pr.load_from_db() - serial_nos = frappe.db.get_value('Stock Ledger Entry', - {'voucher_type': 'Purchase Receipt', 'voucher_no': pr.name, 'item_code': item.name}, 'serial_no') + serial_nos = frappe.db.get_value( + "Stock Ledger Entry", + { + "voucher_type": "Purchase Receipt", + "voucher_no": pr.name, + "item_code": item.name + }, + "serial_no" + ) serial_nos = get_serial_nos(serial_nos) self.assertEquals(get_serial_nos(pr.items[0].serial_no), serial_nos) # Then tried to receive same serial nos in difference company - pr_different_company = make_purchase_receipt(item_code=item.name, qty=2, rate=500, - serial_no='\n'.join(serial_nos), company='_Test Company 1', do_not_submit=True, - warehouse = 'Stores - _TC1') + pr_different_company = make_purchase_receipt( + item_code=item.name, + qty=2, + rate=500, + serial_no='\n'.join(serial_nos), + company='_Test Company 1', + do_not_submit=True, + warehouse = 'Stores - _TC1' + ) self.assertRaises(SerialNoDuplicateError, pr_different_company.submit) # Then made delivery note to remove the serial nos from stock - dn = create_delivery_note(item_code=item.name, qty=2, rate = 1500, serial_no='\n'.join(serial_nos)) + dn = create_delivery_note( + item_code=item.name, + qty=2, + rate=1500, + serial_no='\n'.join(serial_nos) + ) dn.load_from_db() self.assertEquals(get_serial_nos(dn.items[0].serial_no), serial_nos) posting_date = add_days(today(), -3) # Try to receive same serial nos again in the same company with backdated. - pr1 = make_purchase_receipt(item_code=item.name, qty=2, rate=500, - posting_date=posting_date, serial_no='\n'.join(serial_nos), do_not_submit=True) + pr1 = make_purchase_receipt( + item_code=item.name, + qty=2, + rate=500, + posting_date=posting_date, + serial_no='\n'.join(serial_nos), + do_not_submit=True + ) self.assertRaises(SerialNoExistsInFutureTransaction, pr1.submit) # Try to receive same serial nos with different company with backdated. - pr2 = make_purchase_receipt(item_code=item.name, qty=2, rate=500, - posting_date=posting_date, serial_no='\n'.join(serial_nos), company='_Test Company 1', do_not_submit=True, - warehouse = 'Stores - _TC1') + pr2 = make_purchase_receipt( + item_code=item.name, + qty=2, + rate=500, + posting_date=posting_date, + serial_no='\n'.join(serial_nos), + company="_Test Company 1", + do_not_submit=True, + warehouse="Stores - _TC1" + ) self.assertRaises(SerialNoExistsInFutureTransaction, pr2.submit) # Receive the same serial nos after the delivery note posting date and time - make_purchase_receipt(item_code=item.name, qty=2, rate=500, serial_no='\n'.join(serial_nos)) + make_purchase_receipt( + item_code=item.name, + qty=2, + rate=500, + serial_no='\n'.join(serial_nos) + ) # Raise the error for backdated deliver note entry cancel self.assertRaises(SerialNoExistsInFutureTransaction, dn.cancel) @@ -236,11 +347,23 @@ class TestPurchaseReceipt(ERPNextTestCase): def test_subcontracting(self): from erpnext.stock.doctype.stock_entry.test_stock_entry import make_stock_entry - frappe.db.set_value("Buying Settings", None, "backflush_raw_materials_of_subcontract_based_on", "BOM") - make_stock_entry(item_code="_Test Item", target="_Test Warehouse 1 - _TC", qty=100, basic_rate=100) - make_stock_entry(item_code="_Test Item Home Desktop 100", target="_Test Warehouse 1 - _TC", - qty=100, basic_rate=100) - pr = make_purchase_receipt(item_code="_Test FG Item", qty=10, rate=500, is_subcontracted="Yes") + frappe.db.set_value( + "Buying Settings", None, + "backflush_raw_materials_of_subcontract_based_on", "BOM" + ) + + make_stock_entry( + item_code="_Test Item", qty=100, + target="_Test Warehouse 1 - _TC", basic_rate=100 + ) + make_stock_entry( + item_code="_Test Item Home Desktop 100", qty=100, + target="_Test Warehouse 1 - _TC", basic_rate=100 + ) + pr = make_purchase_receipt( + item_code="_Test FG Item", qty=10, + rate=500, is_subcontracted="Yes" + ) self.assertEqual(len(pr.get("supplied_items")), 2) rm_supp_cost = sum(d.amount for d in pr.get("supplied_items")) @@ -250,17 +373,33 @@ class TestPurchaseReceipt(ERPNextTestCase): def test_subcontracting_gle_fg_item_rate_zero(self): from erpnext.stock.doctype.stock_entry.test_stock_entry import make_stock_entry - frappe.db.set_value("Buying Settings", None, "backflush_raw_materials_of_subcontract_based_on", "BOM") + frappe.db.set_value( + "Buying Settings", None, + "backflush_raw_materials_of_subcontract_based_on", "BOM" + ) - se1 = make_stock_entry(item_code="_Test Item", target="Work In Progress - TCP1", - qty=100, basic_rate=100, company="_Test Company with perpetual inventory") + se1 = make_stock_entry( + item_code="_Test Item", + target="Work In Progress - TCP1", + qty=100, basic_rate=100, + company="_Test Company with perpetual inventory" + ) - se2 = make_stock_entry(item_code="_Test Item Home Desktop 100", target="Work In Progress - TCP1", - qty=100, basic_rate=100, company="_Test Company with perpetual inventory") + se2 = make_stock_entry( + item_code="_Test Item Home Desktop 100", + target="Work In Progress - TCP1", + qty=100, basic_rate=100, + company="_Test Company with perpetual inventory" + ) - pr = make_purchase_receipt(item_code="_Test FG Item", qty=10, rate=0, is_subcontracted="Yes", - company="_Test Company with perpetual inventory", warehouse='Stores - TCP1', - supplier_warehouse='Work In Progress - TCP1') + pr = make_purchase_receipt( + item_code="_Test FG Item", + qty=10, rate=0, + is_subcontracted="Yes", + company="_Test Company with perpetual inventory", + warehouse="Stores - TCP1", + supplier_warehouse="Work In Progress - TCP1" + ) gl_entries = get_gl_entries("Purchase Receipt", pr.name) @@ -294,13 +433,23 @@ class TestPurchaseReceipt(ERPNextTestCase): po = create_purchase_order(item_code=item_code, qty=1, include_exploded_items=0, is_subcontracted="Yes", supplier_warehouse="_Test Warehouse 1 - _TC") - #stock raw materials in a warehouse before transfer - se1 = make_stock_entry(target="_Test Warehouse - _TC", - item_code = "Test Extra Item 1", qty=10, basic_rate=100) - se2 = make_stock_entry(target="_Test Warehouse - _TC", - item_code = "_Test FG Item", qty=1, basic_rate=100) - se3 = make_stock_entry(target="_Test Warehouse - _TC", - item_code = "Test Extra Item 2", qty=1, basic_rate=100) + # stock raw materials in a warehouse before transfer + make_stock_entry( + target="_Test Warehouse - _TC", + item_code = "Test Extra Item 1", + qty=10, basic_rate=100 + ) + make_stock_entry( + target="_Test Warehouse - _TC", + item_code = "_Test FG Item", + qty=1, basic_rate=100 + ) + make_stock_entry( + target="_Test Warehouse - _TC", + item_code = "Test Extra Item 2", + qty=1, basic_rate=100 + ) + rm_items = [ { "item_code": item_code, @@ -334,11 +483,17 @@ class TestPurchaseReceipt(ERPNextTestCase): def test_serial_no_supplier(self): pr = make_purchase_receipt(item_code="_Test Serialized Item With Series", qty=1) - self.assertEqual(frappe.db.get_value("Serial No", pr.get("items")[0].serial_no, "supplier"), - pr.supplier) + pr_row_1_serial_no = pr.get("items")[0].serial_no + + self.assertEqual( + frappe.db.get_value("Serial No", pr_row_1_serial_no, "supplier"), + pr.supplier + ) pr.cancel() - self.assertFalse(frappe.db.get_value("Serial No", pr.get("items")[0].serial_no, "warehouse")) + self.assertFalse( + frappe.db.get_value("Serial No", pr_row_1_serial_no, "warehouse") + ) def test_rejected_serial_no(self): pr = frappe.copy_doc(test_records[0]) @@ -365,18 +520,33 @@ class TestPurchaseReceipt(ERPNextTestCase): pr.cancel() def test_purchase_return_partial(self): - pr = make_purchase_receipt(company="_Test Company with perpetual inventory", - warehouse = "Stores - TCP1", supplier_warehouse = "Work in Progress - TCP1") + pr = make_purchase_receipt( + company="_Test Company with perpetual inventory", + warehouse = "Stores - TCP1", + supplier_warehouse = "Work in Progress - TCP1" + ) - return_pr = make_purchase_receipt(company="_Test Company with perpetual inventory", - warehouse = "Stores - TCP1", supplier_warehouse = "Work in Progress - TCP1", - is_return=1, return_against=pr.name, qty=-2, do_not_submit=1) + return_pr = make_purchase_receipt( + company="_Test Company with perpetual inventory", + warehouse = "Stores - TCP1", + supplier_warehouse = "Work in Progress - TCP1", + is_return=1, + return_against=pr.name, + qty=-2, + do_not_submit=1 + ) return_pr.items[0].purchase_receipt_item = pr.items[0].name return_pr.submit() # check sle - outgoing_rate = frappe.db.get_value("Stock Ledger Entry", {"voucher_type": "Purchase Receipt", - "voucher_no": return_pr.name}, "outgoing_rate") + outgoing_rate = frappe.db.get_value( + "Stock Ledger Entry", + { + "voucher_type": "Purchase Receipt", + "voucher_no": return_pr.name + }, + "outgoing_rate" + ) self.assertEqual(outgoing_rate, 50) @@ -440,11 +610,21 @@ class TestPurchaseReceipt(ERPNextTestCase): pr.cancel() def test_purchase_return_full(self): - pr = make_purchase_receipt(company="_Test Company with perpetual inventory", warehouse = "Stores - TCP1", - supplier_warehouse = "Work in Progress - TCP1") + pr = make_purchase_receipt( + company="_Test Company with perpetual inventory", + warehouse = "Stores - TCP1", + supplier_warehouse = "Work in Progress - TCP1" + ) - return_pr = make_purchase_receipt(company="_Test Company with perpetual inventory", warehouse = "Stores - TCP1", - supplier_warehouse = "Work in Progress - TCP1", is_return=1, return_against=pr.name, qty=-5, do_not_submit=1) + return_pr = make_purchase_receipt( + company="_Test Company with perpetual inventory", + warehouse = "Stores - TCP1", + supplier_warehouse = "Work in Progress - TCP1", + is_return=1, + return_against=pr.name, + qty=-5, + do_not_submit=1 + ) return_pr.items[0].purchase_receipt_item = pr.items[0].name return_pr.submit() @@ -466,15 +646,41 @@ class TestPurchaseReceipt(ERPNextTestCase): 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 + 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", supplier_warehouse = "Work in Progress - TCP1", received_qty=4, qty=2, rejected_warehouse=rejected_warehouse) + pr = make_purchase_receipt( + company="_Test Company with perpetual inventory", + warehouse = "Stores - TCP1", + supplier_warehouse = "Work in Progress - TCP1", + qty=2, + rejected_qty=2, + rejected_warehouse=rejected_warehouse + ) - return_pr = make_purchase_receipt(company="_Test Company with perpetual inventory", warehouse = "Stores - TCP1", supplier_warehouse = "Work in Progress - TCP1", is_return=1, return_against=pr.name, received_qty = -4, qty=-2, rejected_warehouse=rejected_warehouse) + return_pr = make_purchase_receipt( + company="_Test Company with perpetual inventory", + warehouse = "Stores - TCP1", + supplier_warehouse = "Work in Progress - TCP1", + is_return=1, + return_against=pr.name, + qty=-2, + rejected_qty = -2, + rejected_warehouse=rejected_warehouse + ) - actual_qty = frappe.db.get_value("Stock Ledger Entry", {"voucher_type": "Purchase Receipt", - "voucher_no": return_pr.name, 'warehouse': return_pr.items[0].rejected_warehouse}, "actual_qty") + actual_qty = frappe.db.get_value( + "Stock Ledger Entry", + { + "voucher_type": "Purchase Receipt", + "voucher_no": return_pr.name, + "warehouse": return_pr.items[0].rejected_warehouse + }, + "actual_qty" + ) self.assertEqual(actual_qty, -2) @@ -499,8 +705,13 @@ class TestPurchaseReceipt(ERPNextTestCase): "purchase_document_no": pr.name }) - return_pr = make_purchase_receipt(item_code="_Test Serialized Item With Series", qty=-1, - is_return=1, return_against=pr.name, serial_no=serial_no) + return_pr = make_purchase_receipt( + item_code="_Test Serialized Item With Series", + qty=-1, + is_return=1, + return_against=pr.name, + serial_no=serial_no + ) _check_serial_no_values(serial_no, { "warehouse": "", @@ -522,9 +733,21 @@ class TestPurchaseReceipt(ERPNextTestCase): }) row.db_update() - pr = make_purchase_receipt(item_code=item_code, qty=1, uom="Box", conversion_factor=1.0) - return_pr = make_purchase_receipt(item_code=item_code, qty=-10, uom="Unit", - stock_uom="Box", conversion_factor=0.1, is_return=1, return_against=pr.name) + pr = make_purchase_receipt( + item_code=item_code, + qty=1, + uom="Box", + conversion_factor=1.0 + ) + return_pr = make_purchase_receipt( + item_code=item_code, + qty=-10, + uom="Unit", + stock_uom="Box", + conversion_factor=0.1, + is_return=1, + return_against=pr.name + ) self.assertEqual(abs(return_pr.items[0].stock_qty), 1.0) @@ -540,13 +763,19 @@ class TestPurchaseReceipt(ERPNextTestCase): pr.submit() update_purchase_receipt_status(pr.name, "Closed") - self.assertEqual(frappe.db.get_value("Purchase Receipt", pr.name, "status"), "Closed") + self.assertEqual( + frappe.db.get_value("Purchase Receipt", pr.name, "status"), "Closed" + ) pr.reload() pr.cancel() def test_pr_billing_status(self): - # PO -> PR1 -> PI and PO -> PI and PO -> PR2 + """Flow: + 1. PO -> PR1 -> PI + 2. PO -> PI + 3. PO -> PR2. + """ from erpnext.buying.doctype.purchase_order.purchase_order import ( make_purchase_invoice as make_purchase_invoice_from_po, ) @@ -610,21 +839,39 @@ class TestPurchaseReceipt(ERPNextTestCase): pr_doc = make_purchase_receipt(item_code=item_code, qty=1, serial_no = serial_no) - self.assertEqual(serial_no, frappe.db.get_value("Serial No", - {"purchase_document_type": "Purchase Receipt", "purchase_document_no": pr_doc.name}, "name")) + self.assertEqual( + serial_no, + frappe.db.get_value( + "Serial No", + { + "purchase_document_type": "Purchase Receipt", + "purchase_document_no": pr_doc.name + }, + "name" + ) + ) pr_doc.cancel() - #check for the auto created serial nos + # check for the auto created serial nos item_code = "Test Auto Created Serial No" if not frappe.db.exists("Item", item_code): - item = make_item(item_code, dict(has_serial_no=1, serial_no_series="KLJL.###")) + make_item(item_code, dict(has_serial_no=1, serial_no_series="KLJL.###")) new_pr_doc = make_purchase_receipt(item_code=item_code, qty=1) serial_no = get_serial_nos(new_pr_doc.items[0].serial_no)[0] - self.assertEqual(serial_no, frappe.db.get_value("Serial No", - {"purchase_document_type": "Purchase Receipt", "purchase_document_no": new_pr_doc.name}, "name")) + self.assertEqual( + serial_no, + frappe.db.get_value( + "Serial No", + { + "purchase_document_type": "Purchase Receipt", + "purchase_document_no": new_pr_doc.name + }, + "name" + ) + ) new_pr_doc.cancel() @@ -700,8 +947,12 @@ class TestPurchaseReceipt(ERPNextTestCase): def test_purchase_receipt_cost_center(self): from erpnext.accounts.doctype.cost_center.test_cost_center import create_cost_center + cost_center = "_Test Cost Center for BS Account - TCP1" - create_cost_center(cost_center_name="_Test Cost Center for BS Account", company="_Test Company with perpetual inventory") + create_cost_center( + cost_center_name="_Test Cost Center for BS Account", + company="_Test Company with perpetual inventory" + ) if not frappe.db.exists('Location', 'Test Location'): frappe.get_doc({ @@ -709,10 +960,16 @@ class TestPurchaseReceipt(ERPNextTestCase): 'location_name': 'Test Location' }).insert() - pr = make_purchase_receipt(cost_center=cost_center, company="_Test Company with perpetual inventory", - warehouse = "Stores - TCP1", supplier_warehouse = "Work in Progress - TCP1") + pr = make_purchase_receipt( + cost_center=cost_center, + company="_Test Company with perpetual inventory", + warehouse = "Stores - TCP1", + supplier_warehouse = "Work in Progress - TCP1" + ) - stock_in_hand_account = get_inventory_account(pr.company, pr.get("items")[0].warehouse) + stock_in_hand_account = get_inventory_account( + pr.company, pr.get("items")[0].warehouse + ) gl_entries = get_gl_entries("Purchase Receipt", pr.name) self.assertTrue(gl_entries) @@ -736,9 +993,16 @@ class TestPurchaseReceipt(ERPNextTestCase): 'doctype': 'Location', 'location_name': 'Test Location' }).insert() - pr = make_purchase_receipt(company="_Test Company with perpetual inventory", warehouse = "Stores - TCP1", supplier_warehouse = "Work in Progress - TCP1") - stock_in_hand_account = get_inventory_account(pr.company, pr.get("items")[0].warehouse) + pr = make_purchase_receipt( + company="_Test Company with perpetual inventory", + warehouse = "Stores - TCP1", + supplier_warehouse = "Work in Progress - TCP1" + ) + + stock_in_hand_account = get_inventory_account( + pr.company, pr.get("items")[0].warehouse + ) gl_entries = get_gl_entries("Purchase Receipt", pr.name) self.assertTrue(gl_entries) @@ -766,7 +1030,11 @@ class TestPurchaseReceipt(ERPNextTestCase): po = create_purchase_order() pr = create_pr_against_po(po.name) - pr1 = make_purchase_receipt(is_return=1, return_against=pr.name, qty=-1, do_not_submit=True) + pr1 = make_purchase_receipt( + qty=-1, + is_return=1, return_against=pr.name, + do_not_submit=True + ) pr1.items[0].purchase_order = po.name pr1.items[0].purchase_order_item = po.items[0].name pr1.items[0].purchase_receipt_item = pr.items[0].name @@ -799,7 +1067,11 @@ class TestPurchaseReceipt(ERPNextTestCase): pi1.save() pi1.submit() - pr2 = make_purchase_receipt(is_return=1, return_against=pr1.name, qty=-2, do_not_submit=True) + pr2 = make_purchase_receipt( + qty=-2, + is_return=1, return_against=pr1.name, + do_not_submit=True + ) pr2.items[0].purchase_receipt_item = pr1.items[0].name pr2.submit() @@ -841,14 +1113,22 @@ class TestPurchaseReceipt(ERPNextTestCase): pr1.cancel() def test_stock_transfer_from_purchase_receipt_with_valuation(self): - create_warehouse("_Test Warehouse for Valuation", company="_Test Company with perpetual inventory", - properties={"account": '_Test Account Stock In Hand - TCP1'}) + create_warehouse( + "_Test Warehouse for Valuation", + company="_Test Company with perpetual inventory", + properties={"account": '_Test Account Stock In Hand - TCP1'} + ) - pr1 = make_purchase_receipt(warehouse = '_Test Warehouse for Valuation - TCP1', - company="_Test Company with perpetual inventory") + pr1 = make_purchase_receipt( + warehouse = '_Test Warehouse for Valuation - TCP1', + company="_Test Company with perpetual inventory" + ) - pr = make_purchase_receipt(company="_Test Company with perpetual inventory", - warehouse = "Stores - TCP1", do_not_save=1) + pr = make_purchase_receipt( + company="_Test Company with perpetual inventory", + warehouse = "Stores - TCP1", + do_not_save=1 + ) pr.items[0].from_warehouse = '_Test Warehouse for Valuation - TCP1' pr.supplier_warehouse = '' @@ -930,10 +1210,24 @@ class TestPurchaseReceipt(ERPNextTestCase): } rm_items = [ - {"item_code":item_code,"rm_item_code":"Sub Contracted Raw Material 3","item_name":"_Test Item", - "qty":300,"warehouse":"_Test Warehouse - _TC", "stock_uom":"Nos", "name": po.supplied_items[0].name}, - {"item_code":item_code,"rm_item_code":"Sub Contracted Raw Material 3","item_name":"_Test Item", - "qty":200,"warehouse":"_Test Warehouse - _TC", "stock_uom":"Nos", "name": po.supplied_items[0].name} + { + "item_code":item_code, + "rm_item_code":"Sub Contracted Raw Material 3", + "item_name":"_Test Item", + "qty":300, + "warehouse":"_Test Warehouse - _TC", + "stock_uom":"Nos", + "name": po.supplied_items[0].name + }, + { + "item_code":item_code, + "rm_item_code":"Sub Contracted Raw Material 3", + "item_name":"_Test Item", + "qty":200, + "warehouse":"_Test Warehouse - _TC", + "stock_uom":"Nos", + "name": po.supplied_items[0].name + } ] rm_item_string = json.dumps(rm_items) @@ -943,8 +1237,14 @@ class TestPurchaseReceipt(ERPNextTestCase): se.items[1].batch_no = ste2.items[0].batch_no se.submit() - supplied_qty = frappe.db.get_value("Purchase Order Item Supplied", - {"parent": po.name, "rm_item_code": "Sub Contracted Raw Material 3"}, "supplied_qty") + supplied_qty = frappe.db.get_value( + "Purchase Order Item Supplied", + { + "parent": po.name, + "rm_item_code": "Sub Contracted Raw Material 3" + }, + "supplied_qty" + ) self.assertEqual(supplied_qty, 500.00) @@ -1016,10 +1316,18 @@ class TestPurchaseReceipt(ERPNextTestCase): company = '_Test Company with perpetual inventory' service_item = '_Test Non Stock Item' - before_test_value = frappe.db.get_value('Company', company, 'enable_perpetual_inventory_for_non_stock_items') - frappe.db.set_value('Company', company, 'enable_perpetual_inventory_for_non_stock_items', 1) + before_test_value = frappe.db.get_value( + 'Company', company, 'enable_perpetual_inventory_for_non_stock_items' + ) + frappe.db.set_value( + 'Company', company, + 'enable_perpetual_inventory_for_non_stock_items', 1 + ) srbnb_account = 'Stock Received But Not Billed - TCP1' - frappe.db.set_value('Company', company, 'service_received_but_not_billed', srbnb_account) + frappe.db.set_value( + 'Company', company, + 'service_received_but_not_billed', srbnb_account + ) pr = make_purchase_receipt( company=company, item=service_item, @@ -1051,7 +1359,10 @@ class TestPurchaseReceipt(ERPNextTestCase): self.assertEqual(len(item_one_gl_entry), 1) self.assertEqual(len(item_two_gl_entry), 1) - frappe.db.set_value('Company', company, 'enable_perpetual_inventory_for_non_stock_items', before_test_value) + frappe.db.set_value( + 'Company', company, + 'enable_perpetual_inventory_for_non_stock_items', before_test_value + ) def test_purchase_receipt_with_exchange_rate_difference(self): from erpnext.accounts.doctype.purchase_invoice.purchase_invoice import ( @@ -1076,10 +1387,19 @@ class TestPurchaseReceipt(ERPNextTestCase): pr.submit() # Get exchnage gain and loss account - exchange_gain_loss_account = frappe.db.get_value('Company', pr.company, 'exchange_gain_loss_account') + exchange_gain_loss_account = frappe.db.get_value( + 'Company', pr.company, 'exchange_gain_loss_account' + ) # fetching the latest GL Entry with exchange gain and loss account account - amount = frappe.db.get_value('GL Entry', {'account': exchange_gain_loss_account, 'voucher_no': pr.name}, 'credit') + amount = frappe.db.get_value( + 'GL Entry', + { + 'account': exchange_gain_loss_account, + 'voucher_no': pr.name + }, + 'credit' + ) discrepancy_caused_by_exchange_rate_diff = abs(pi.items[0].base_net_amount - pr.items[0].base_net_amount) self.assertEqual(discrepancy_caused_by_exchange_rate_diff, amount) @@ -1225,8 +1545,8 @@ def make_purchase_receipt(**args): pr.return_against = args.return_against pr.apply_putaway_rule = args.apply_putaway_rule qty = args.qty or 5 - received_qty = args.received_qty or qty - rejected_qty = args.rejected_qty or flt(received_qty) - flt(qty) + rejected_qty = args.rejected_qty or 0 + received_qty = args.received_qty or flt(rejected_qty) + flt(qty) item_code = args.item or args.item_code or "_Test Item" uom = args.uom or frappe.db.get_value("Item", item_code, "stock_uom") or "_Test UOM" @@ -1249,9 +1569,12 @@ def make_purchase_receipt(**args): if args.get_multiple_items: pr.items = [] - for item in get_items(warehouse= args.warehouse, cost_center = args.cost_center or frappe.get_cached_value('Company', pr.company, 'cost_center')): - pr.append("items", item) + company_cost_center = frappe.get_cached_value('Company', pr.company, 'cost_center') + cost_center = args.cost_center or company_cost_center + + for item in get_items(warehouse=args.warehouse, cost_center=cost_center): + pr.append("items", item) if args.get_taxes_and_charges: for tax in get_taxes(): diff --git a/erpnext/stock/doctype/purchase_receipt_item/purchase_receipt_item.json b/erpnext/stock/doctype/purchase_receipt_item/purchase_receipt_item.json index 3efa66e02ed..30ea1c3cadc 100644 --- a/erpnext/stock/doctype/purchase_receipt_item/purchase_receipt_item.json +++ b/erpnext/stock/doctype/purchase_receipt_item/purchase_receipt_item.json @@ -197,6 +197,7 @@ }, { "bold": 1, + "default": "0", "fieldname": "received_qty", "fieldtype": "Float", "label": "Received Quantity", @@ -204,6 +205,7 @@ "oldfieldtype": "Currency", "print_hide": 1, "print_width": "100px", + "read_only": 1, "reqd": 1, "width": "100px" }, @@ -219,8 +221,10 @@ "width": "100px" }, { + "columns": 1, "fieldname": "rejected_qty", "fieldtype": "Float", + "in_list_view": 1, "label": "Rejected Quantity", "oldfieldname": "rejected_qty", "oldfieldtype": "Currency", @@ -327,7 +331,7 @@ }, { "bold": 1, - "columns": 3, + "columns": 2, "fieldname": "rate", "fieldtype": "Currency", "in_list_view": 1, @@ -543,6 +547,7 @@ "fieldname": "stock_qty", "fieldtype": "Float", "label": "Accepted Qty in Stock UOM", + "no_copy": 1, "oldfieldname": "stock_qty", "oldfieldtype": "Currency", "print_hide": 1, @@ -882,7 +887,9 @@ "fieldname": "received_stock_qty", "fieldtype": "Float", "label": "Received Qty in Stock UOM", - "print_hide": 1 + "no_copy": 1, + "print_hide": 1, + "read_only": 1 }, { "depends_on": "eval: doc.uom != doc.stock_uom", @@ -969,10 +976,11 @@ "idx": 1, "istable": 1, "links": [], - "modified": "2021-09-01 16:02:40.338597", + "modified": "2021-11-15 15:46:10.591600", "modified_by": "Administrator", "module": "Stock", "name": "Purchase Receipt Item", + "naming_rule": "Random", "owner": "Administrator", "permissions": [], "quick_entry": 1, diff --git a/erpnext/stock/doctype/quality_inspection/quality_inspection.js b/erpnext/stock/doctype/quality_inspection/quality_inspection.js index d08dc3e8b76..eea28791a9f 100644 --- a/erpnext/stock/doctype/quality_inspection/quality_inspection.js +++ b/erpnext/stock/doctype/quality_inspection/quality_inspection.js @@ -59,7 +59,7 @@ frappe.ui.form.on("Quality Inspection", { }, item_code: function(frm) { - if (frm.doc.item_code) { + if (frm.doc.item_code && !frm.doc.quality_inspection_template) { return frm.call({ method: "get_quality_inspection_template", doc: frm.doc, diff --git a/erpnext/stock/doctype/quality_inspection/quality_inspection.py b/erpnext/stock/doctype/quality_inspection/quality_inspection.py index 913ee1559d4..4e3b80aa761 100644 --- a/erpnext/stock/doctype/quality_inspection/quality_inspection.py +++ b/erpnext/stock/doctype/quality_inspection/quality_inspection.py @@ -18,6 +18,15 @@ class QualityInspection(Document): if not self.readings and self.item_code: self.get_item_specification_details() + if self.inspection_type=="In Process" and self.reference_type=="Job Card": + item_qi_template = frappe.db.get_value("Item", self.item_code, 'quality_inspection_template') + parameters = get_template_details(item_qi_template) + for reading in self.readings: + for d in parameters: + if reading.specification == d.specification: + reading.update(d) + reading.status = "Accepted" + if self.readings: self.inspect_and_set_status() diff --git a/erpnext/stock/doctype/repost_item_valuation/repost_item_valuation.json b/erpnext/stock/doctype/repost_item_valuation/repost_item_valuation.json index a800bf87013..cd7e63b18b2 100644 --- a/erpnext/stock/doctype/repost_item_valuation/repost_item_valuation.json +++ b/erpnext/stock/doctype/repost_item_valuation/repost_item_valuation.json @@ -64,7 +64,7 @@ "in_standard_filter": 1, "label": "Status", "no_copy": 1, - "options": "Queued\nIn Progress\nCompleted\nFailed", + "options": "Queued\nIn Progress\nCompleted\nSkipped\nFailed", "read_only": 1 }, { @@ -177,10 +177,11 @@ "index_web_pages_for_search": 1, "is_submittable": 1, "links": [], - "modified": "2021-07-22 18:59:43.057878", + "modified": "2021-11-24 02:18:10.524560", "modified_by": "Administrator", "module": "Stock", "name": "Repost Item Valuation", + "naming_rule": "Expression (old style)", "owner": "Administrator", "permissions": [ { @@ -197,20 +198,6 @@ "submit": 1, "write": 1 }, - { - "cancel": 1, - "create": 1, - "delete": 1, - "email": 1, - "export": 1, - "print": 1, - "read": 1, - "report": 1, - "role": "Stock User", - "share": 1, - "submit": 1, - "write": 1 - }, { "cancel": 1, "create": 1, @@ -226,7 +213,6 @@ "write": 1 }, { - "cancel": 1, "create": 1, "delete": 1, "email": 1, @@ -234,7 +220,7 @@ "print": 1, "read": 1, "report": 1, - "role": "Accounts User", + "role": "Accounts Manager", "share": 1, "submit": 1, "write": 1 @@ -242,4 +228,4 @@ ], "sort_field": "modified", "sort_order": "DESC" -} \ No newline at end of file +} diff --git a/erpnext/stock/doctype/repost_item_valuation/repost_item_valuation.py b/erpnext/stock/doctype/repost_item_valuation/repost_item_valuation.py index 170aa7f76c1..fb3b355fb74 100644 --- a/erpnext/stock/doctype/repost_item_valuation/repost_item_valuation.py +++ b/erpnext/stock/doctype/repost_item_valuation/repost_item_valuation.py @@ -1,7 +1,6 @@ # Copyright (c) 2020, Frappe Technologies Pvt. Ltd. and contributors # For license information, please see license.txt - import frappe from frappe import _ from frappe.model.document import Document @@ -19,7 +18,7 @@ from erpnext.stock.stock_ledger import repost_future_sle class RepostItemValuation(Document): def validate(self): - self.set_status() + self.set_status(write=False) self.reset_field_values() self.set_company() @@ -27,26 +26,27 @@ class RepostItemValuation(Document): if self.based_on == 'Transaction': self.item_code = None self.warehouse = None - else: - self.voucher_type = None - self.voucher_no = None self.allow_negative_stock = self.allow_negative_stock or \ cint(frappe.db.get_single_value("Stock Settings", "allow_negative_stock")) def set_company(self): - if self.voucher_type and self.voucher_no: + if self.based_on == "Transaction": self.company = frappe.get_cached_value(self.voucher_type, self.voucher_no, "company") elif self.warehouse: self.company = frappe.get_cached_value("Warehouse", self.warehouse, "company") - def set_status(self, status=None): + def set_status(self, status=None, write=True): + status = status or self.status if not status: - status = 'Queued' - self.db_set('status', status) + self.status = 'Queued' + else: + self.status = status + if write: + self.db_set('status', self.status) def on_submit(self): - if not frappe.flags.in_test: + if not frappe.flags.in_test or self.flags.dont_run_in_test or frappe.flags.dont_execute_stock_reposts: return frappe.enqueue(repost, timeout=1800, queue='long', @@ -54,9 +54,42 @@ class RepostItemValuation(Document): @frappe.whitelist() def restart_reposting(self): - self.set_status('Queued') - frappe.enqueue(repost, timeout=1800, queue='long', - job_name='repost_sle', now=True, doc=self) + self.set_status('Queued', write=False) + self.current_index = 0 + self.distinct_item_and_warehouse = None + self.items_to_be_repost = None + self.db_update() + + def deduplicate_similar_repost(self): + """ Deduplicate similar reposts based on item-warehouse-posting combination.""" + if self.based_on != "Item and Warehouse": + return + + filters = { + "item_code": self.item_code, + "warehouse": self.warehouse, + "name": self.name, + "posting_date": self.posting_date, + "posting_time": self.posting_time, + } + + frappe.db.sql(""" + update `tabRepost Item Valuation` + set status = 'Skipped' + WHERE item_code = %(item_code)s + and warehouse = %(warehouse)s + and name != %(name)s + and TIMESTAMP(posting_date, posting_time) > TIMESTAMP(%(posting_date)s, %(posting_time)s) + and docstatus = 1 + and status = 'Queued' + and based_on = 'Item and Warehouse' + """, + filters + ) + +def on_doctype_update(): + frappe.db.add_index("Repost Item Valuation", ["warehouse", "item_code"], "item_warehouse") + def repost(doc): try: @@ -64,7 +97,8 @@ def repost(doc): return doc.set_status('In Progress') - frappe.db.commit() + if not frappe.flags.in_test: + frappe.db.commit() repost_sl_entries(doc) repost_gl_entries(doc) @@ -133,8 +167,10 @@ def repost_entries(): riv_entries = get_repost_item_valuation_entries() for row in riv_entries: - doc = frappe.get_cached_doc('Repost Item Valuation', row.name) - repost(doc) + doc = frappe.get_doc('Repost Item Valuation', row.name) + if doc.status in ('Queued', 'In Progress'): + repost(doc) + doc.deduplicate_similar_repost() riv_entries = get_repost_item_valuation_entries() if riv_entries: diff --git a/erpnext/stock/doctype/repost_item_valuation/test_repost_item_valuation.py b/erpnext/stock/doctype/repost_item_valuation/test_repost_item_valuation.py index c086f938b5d..78b432d564c 100644 --- a/erpnext/stock/doctype/repost_item_valuation/test_repost_item_valuation.py +++ b/erpnext/stock/doctype/repost_item_valuation/test_repost_item_valuation.py @@ -4,10 +4,14 @@ import unittest import frappe +from frappe.utils import nowdate +from erpnext.controllers.stock_controller import create_item_wise_repost_entries +from erpnext.stock.doctype.purchase_receipt.test_purchase_receipt import make_purchase_receipt from erpnext.stock.doctype.repost_item_valuation.repost_item_valuation import ( in_configured_timeslot, ) +from erpnext.stock.utils import PendingRepostingError class TestRepostItemValuation(unittest.TestCase): @@ -70,3 +74,91 @@ class TestRepostItemValuation(unittest.TestCase): in_configured_timeslot(repost_settings, case.get("current_time")), msg=f"Exepcted false from : {case}", ) + + def test_create_item_wise_repost_item_valuation_entries(self): + pr = make_purchase_receipt( + company="_Test Company with perpetual inventory", + warehouse="Stores - TCP1", + get_multiple_items=True, + ) + + rivs = create_item_wise_repost_entries(pr.doctype, pr.name) + self.assertGreaterEqual(len(rivs), 2) + self.assertIn("_Test Item", [d.item_code for d in rivs]) + + for riv in rivs: + self.assertEqual(riv.company, "_Test Company with perpetual inventory") + self.assertEqual(riv.warehouse, "Stores - TCP1") + + def test_deduplication(self): + def _assert_status(doc, status): + doc.load_from_db() + self.assertEqual(doc.status, status) + + riv_args = frappe._dict( + doctype="Repost Item Valuation", + item_code="_Test Item", + warehouse="_Test Warehouse - _TC", + based_on="Item and Warehouse", + voucher_type="Sales Invoice", + voucher_no="SI-1", + posting_date="2021-01-02", + posting_time="00:01:00", + ) + + # new repost without any duplicates + riv1 = frappe.get_doc(riv_args) + riv1.flags.dont_run_in_test = True + riv1.submit() + _assert_status(riv1, "Queued") + self.assertEqual(riv1.voucher_type, "Sales Invoice") # traceability + self.assertEqual(riv1.voucher_no, "SI-1") + + # newer than existing duplicate - riv1 + riv2 = frappe.get_doc(riv_args.update({"posting_date": "2021-01-03"})) + riv2.flags.dont_run_in_test = True + riv2.submit() + riv1.deduplicate_similar_repost() + _assert_status(riv2, "Skipped") + + # older than exisitng duplicate - riv1 + riv3 = frappe.get_doc(riv_args.update({"posting_date": "2021-01-01"})) + riv3.flags.dont_run_in_test = True + riv3.submit() + riv3.deduplicate_similar_repost() + _assert_status(riv3, "Queued") + _assert_status(riv1, "Skipped") + + # unrelated reposts, shouldn't do anything to others. + riv4 = frappe.get_doc(riv_args.update({"warehouse": "Stores - _TC"})) + riv4.flags.dont_run_in_test = True + riv4.submit() + riv4.deduplicate_similar_repost() + _assert_status(riv4, "Queued") + _assert_status(riv3, "Queued") + + # to avoid breaking other tests accidentaly + riv4.set_status("Skipped") + riv3.set_status("Skipped") + + def test_stock_freeze_validation(self): + + today = nowdate() + + riv = frappe.get_doc( + doctype="Repost Item Valuation", + item_code="_Test Item", + warehouse="_Test Warehouse - _TC", + based_on="Item and Warehouse", + posting_date=today, + posting_time="00:01:00", + ) + riv.flags.dont_run_in_test = True # keep it queued + riv.submit() + + stock_settings = frappe.get_doc("Stock Settings") + stock_settings.stock_frozen_upto = today + + self.assertRaises(PendingRepostingError, stock_settings.save) + + riv.set_status("Skipped") diff --git a/erpnext/stock/doctype/serial_no/serial_no.json b/erpnext/stock/doctype/serial_no/serial_no.json index a3d44af4945..6e1e0d461ab 100644 --- a/erpnext/stock/doctype/serial_no/serial_no.json +++ b/erpnext/stock/doctype/serial_no/serial_no.json @@ -1,7 +1,6 @@ { "actions": [], "allow_import": 1, - "allow_rename": 1, "autoname": "field:serial_no", "creation": "2013-05-16 10:59:15", "description": "Distinct unit of an Item", @@ -434,10 +433,11 @@ "icon": "fa fa-barcode", "idx": 1, "links": [], - "modified": "2021-01-08 14:31:15.375996", + "modified": "2021-12-23 10:44:30.299450", "modified_by": "Administrator", "module": "Stock", "name": "Serial No", + "naming_rule": "By fieldname", "owner": "Administrator", "permissions": [ { @@ -476,5 +476,6 @@ "show_name_in_global_search": 1, "sort_field": "modified", "sort_order": "DESC", + "states": [], "track_changes": 1 } \ No newline at end of file diff --git a/erpnext/stock/doctype/serial_no/serial_no.py b/erpnext/stock/doctype/serial_no/serial_no.py index d9d1957c0b1..2947fafe525 100644 --- a/erpnext/stock/doctype/serial_no/serial_no.py +++ b/erpnext/stock/doctype/serial_no/serial_no.py @@ -194,23 +194,6 @@ class SerialNo(StockController): if sle_exists: frappe.throw(_("Cannot delete Serial No {0}, as it is used in stock transactions").format(self.name)) - def before_rename(self, old, new, merge=False): - if merge: - frappe.throw(_("Sorry, Serial Nos cannot be merged")) - - def after_rename(self, old, new, merge=False): - """rename serial_no text fields""" - for dt in frappe.db.sql("""select parent from tabDocField - where fieldname='serial_no' and fieldtype in ('Text', 'Small Text', 'Long Text')"""): - - for item in frappe.db.sql("""select name, serial_no from `tab%s` - where serial_no like %s""" % (dt[0], frappe.db.escape('%' + old + '%'))): - - serial_nos = map(lambda i: new if i.upper()==old.upper() else i, item[1].split('\n')) - frappe.db.sql("""update `tab%s` set serial_no = %s - where name=%s""" % (dt[0], '%s', '%s'), - ('\n'.join(list(serial_nos)), item[0])) - def update_serial_no_reference(self, serial_no=None): last_sle = self.get_last_sle(serial_no) self.set_purchase_details(last_sle.get("purchase_sle")) @@ -342,7 +325,7 @@ def check_serial_no_validity_on_cancel(serial_no, sle): is_stock_reco = sle.voucher_type == "Stock Reconciliation" msg = None - if sr and (actual_qty < 0 or is_stock_reco) and sr.warehouse != sle.warehouse: + if sr and (actual_qty < 0 or is_stock_reco) and (sr.warehouse and sr.warehouse != sle.warehouse): # receipt(inward) is being cancelled msg = _("Cannot cancel {0} {1} as Serial No {2} does not belong to the warehouse {3}").format( sle.voucher_type, doc_link, sr_link, frappe.bold(sle.warehouse)) diff --git a/erpnext/stock/doctype/shipment/test_shipment.py b/erpnext/stock/doctype/shipment/test_shipment.py index 705b2651f65..afe821845ae 100644 --- a/erpnext/stock/doctype/shipment/test_shipment.py +++ b/erpnext/stock/doctype/shipment/test_shipment.py @@ -39,9 +39,9 @@ def create_test_delivery_note(): "description": 'Test delivery note for shipment', "qty": 5, "uom": 'Nos', - "warehouse": 'Stores - SC', + "warehouse": 'Stores - _TC', "rate": item.standard_rate, - "cost_center": 'Main - SC' + "cost_center": 'Main - _TC' } ) delivery_note.insert() @@ -127,13 +127,7 @@ def get_shipment_company_address(company_name): return create_shipment_address(address_title, company_name, 80331) def get_shipment_company(): - company_name = 'Shipment Company' - abbr = 'SC' - companies = frappe.get_all("Company", fields=["name"], filters = {"company_name": company_name}) - if len(companies): - return companies[0] - else: - return create_shipment_company(company_name, abbr) + return frappe.get_doc("Company", "_Test Company") def get_shipment_item(company_name): item_name = 'Testing Shipment item' @@ -182,17 +176,6 @@ def create_customer_contact(fname, lname): customer.insert() return customer - -def create_shipment_company(company_name, abbr): - company = frappe.new_doc("Company") - company.company_name = company_name - company.abbr = abbr - company.default_currency = 'EUR' - company.country = 'Germany' - company.enable_perpetual_inventory = 0 - company.insert() - return company - def create_shipment_customer(customer_name): customer = frappe.new_doc("Customer") customer.customer_name = customer_name @@ -211,12 +194,12 @@ def create_material_receipt(item, company): stock.posting_date = posting_date.strftime("%Y-%m-%d") stock.append('items', { - "t_warehouse": 'Stores - SC', + "t_warehouse": 'Stores - _TC', "item_code": item.name, "qty": 5, "uom": 'Nos', "basic_rate": item.standard_rate, - "cost_center": 'Main - SC' + "cost_center": 'Main - _TC' } ) stock.insert() @@ -233,7 +216,7 @@ def create_shipment_item(item_name, company_name): item.append('item_defaults', { "company": company_name, - "default_warehouse": 'Stores - SC' + "default_warehouse": 'Stores - _TC' } ) item.insert() diff --git a/erpnext/stock/doctype/stock_entry/stock_entry.py b/erpnext/stock/doctype/stock_entry/stock_entry.py index 46c9576a9c8..93e303c9a7f 100644 --- a/erpnext/stock/doctype/stock_entry/stock_entry.py +++ b/erpnext/stock/doctype/stock_entry/stock_entry.py @@ -35,10 +35,16 @@ from erpnext.stock.stock_ledger import NegativeStockError, get_previous_sle, get from erpnext.stock.utils import get_bin, get_incoming_rate -class IncorrectValuationRateError(frappe.ValidationError): pass -class DuplicateEntryForWorkOrderError(frappe.ValidationError): pass -class OperationsNotCompleteError(frappe.ValidationError): pass -class MaxSampleAlreadyRetainedError(frappe.ValidationError): pass +class FinishedGoodError(frappe.ValidationError): + pass +class IncorrectValuationRateError(frappe.ValidationError): + pass +class DuplicateEntryForWorkOrderError(frappe.ValidationError): + pass +class OperationsNotCompleteError(frappe.ValidationError): + pass +class MaxSampleAlreadyRetainedError(frappe.ValidationError): + pass from erpnext.controllers.stock_controller import StockController @@ -103,6 +109,8 @@ class StockEntry(StockController): self.set_actual_qty() self.calculate_rate_and_amount() self.validate_putaway_capacity() + self.reset_default_field_value("from_warehouse", "items", "s_warehouse") + self.reset_default_field_value("to_warehouse", "items", "t_warehouse") def on_submit(self): self.update_stock_ledger() @@ -545,7 +553,7 @@ class StockEntry(StockController): scrap_items_cost = sum([flt(d.basic_amount) for d in self.get("items") if d.is_scrap_item]) # Get raw materials cost from BOM if multiple material consumption entries - if frappe.db.get_single_value("Manufacturing Settings", "material_consumption", cache=True): + if not outgoing_items_cost and frappe.db.get_single_value("Manufacturing Settings", "material_consumption", cache=True): bom_items = self.get_bom_raw_materials(finished_item_qty) outgoing_items_cost = sum([flt(row.qty)*flt(row.rate) for row in bom_items.values()]) @@ -699,6 +707,11 @@ class StockEntry(StockController): finished_item = self.get_finished_item() + if not finished_item and self.purpose == "Manufacture": + # In case of independent Manufacture entry, don't auto set + # user must decide and set + return + for d in self.items: if d.t_warehouse and not d.s_warehouse: if self.purpose=="Repack" or d.item_code == finished_item: @@ -719,38 +732,64 @@ class StockEntry(StockController): return finished_item def validate_finished_goods(self): - """validation: finished good quantity should be same as manufacturing quantity""" - if not self.work_order: return + """ + 1. Check if FG exists + 2. Check if Multiple FG Items are present + 3. Check FG Item and Qty against WO if present + """ + production_item, wo_qty, finished_items = None, 0, [] - production_item, wo_qty = frappe.db.get_value("Work Order", - self.work_order, ["production_item", "qty"]) + wo_details = frappe.db.get_value( + "Work Order", self.work_order, ["production_item", "qty"] + ) + if wo_details: + production_item, wo_qty = wo_details - finished_items = [] for d in self.get('items'): if d.is_finished_item: + if not self.work_order: + finished_items.append(d.item_code) + continue # Independent Manufacture Entry, no WO to match against + if d.item_code != production_item: frappe.throw(_("Finished Item {0} does not match with Work Order {1}") - .format(d.item_code, self.work_order)) + .format(d.item_code, self.work_order) + ) elif flt(d.transfer_qty) > flt(self.fg_completed_qty): - frappe.throw(_("Quantity in row {0} ({1}) must be same as manufactured quantity {2}"). \ - format(d.idx, d.transfer_qty, self.fg_completed_qty)) + frappe.throw(_("Quantity in row {0} ({1}) must be same as manufactured quantity {2}") + .format(d.idx, d.transfer_qty, self.fg_completed_qty) + ) + finished_items.append(d.item_code) if len(set(finished_items)) > 1: - frappe.throw(_("Multiple items cannot be marked as finished item")) + frappe.throw( + msg=_("Multiple items cannot be marked as finished item"), + title=_("Note"), + exc=FinishedGoodError + ) if self.purpose == "Manufacture": if not finished_items: - frappe.throw(_('Finished Good has not set in the stock entry {0}') - .format(self.name)) + frappe.throw( + msg=_("There must be atleast 1 Finished Good in this Stock Entry").format(self.name), + title=_("Missing Finished Good"), + exc=FinishedGoodError + ) - allowance_percentage = flt(frappe.db.get_single_value("Manufacturing Settings", - "overproduction_percentage_for_work_order")) + allowance_percentage = flt( + frappe.db.get_single_value( + "Manufacturing Settings","overproduction_percentage_for_work_order" + ) + ) + allowed_qty = wo_qty + ((allowance_percentage/100) * wo_qty) - allowed_qty = wo_qty + (allowance_percentage/100 * wo_qty) - if self.fg_completed_qty > allowed_qty: - frappe.throw(_("For quantity {0} should not be greater than work order quantity {1}") - .format(flt(self.fg_completed_qty), wo_qty)) + # No work order could mean independent Manufacture entry, if so skip validation + if self.work_order and self.fg_completed_qty > allowed_qty: + frappe.throw( + _("For quantity {0} should not be greater than work order quantity {1}") + .format(flt(self.fg_completed_qty), wo_qty) + ) def update_stock_ledger(self): sl_entries = [] @@ -1462,52 +1501,94 @@ class StockEntry(StockController): return item_dict def get_pro_order_required_items(self, backflush_based_on=None): - item_dict = frappe._dict() - pro_order = frappe.get_doc("Work Order", self.work_order) - if not frappe.db.get_value("Warehouse", pro_order.wip_warehouse, "is_group"): - wip_warehouse = pro_order.wip_warehouse + """ + Gets Work Order Required Items only if Stock Entry purpose is **Material Transferred for Manufacture**. + """ + item_dict, job_card_items = frappe._dict(), [] + work_order = frappe.get_doc("Work Order", self.work_order) + + consider_job_card = work_order.transfer_material_against == "Job Card" and self.get("job_card") + if consider_job_card: + job_card_items = self.get_job_card_item_codes(self.get("job_card")) + + if not frappe.db.get_value("Warehouse", work_order.wip_warehouse, "is_group"): + wip_warehouse = work_order.wip_warehouse else: wip_warehouse = None - for d in pro_order.get("required_items"): - if ( ((flt(d.required_qty) > flt(d.transferred_qty)) or - (backflush_based_on == "Material Transferred for Manufacture")) and - (d.include_item_in_manufacturing or self.purpose != "Material Transfer for Manufacture")): + for d in work_order.get("required_items"): + if consider_job_card and (d.item_code not in job_card_items): + continue + + transfer_pending = flt(d.required_qty) > flt(d.transferred_qty) + can_transfer = transfer_pending or (backflush_based_on == "Material Transferred for Manufacture") + + if not can_transfer: + continue + + if d.include_item_in_manufacturing: item_row = d.as_dict() + item_row["idx"] = len(item_dict) + 1 + + if consider_job_card: + job_card_item = frappe.db.get_value( + "Job Card Item", + { + "item_code": d.item_code, + "parent": self.get("job_card") + } + ) + item_row["job_card_item"] = job_card_item or None + if d.source_warehouse and not frappe.db.get_value("Warehouse", d.source_warehouse, "is_group"): item_row["from_warehouse"] = d.source_warehouse item_row["to_warehouse"] = wip_warehouse if item_row["allow_alternative_item"]: - item_row["allow_alternative_item"] = pro_order.allow_alternative_item + item_row["allow_alternative_item"] = work_order.allow_alternative_item item_dict.setdefault(d.item_code, item_row) return item_dict + def get_job_card_item_codes(self, job_card=None): + if not job_card: + return [] + + job_card_items = frappe.get_all( + "Job Card Item", + filters={ + "parent": job_card + }, + fields=["item_code"], + distinct=True + ) + return [d.item_code for d in job_card_items] + def add_to_stock_entry_detail(self, item_dict, bom_no=None): for d in item_dict: - stock_uom = item_dict[d].get("stock_uom") or frappe.db.get_value("Item", d, "stock_uom") + item_row = item_dict[d] + stock_uom = item_row.get("stock_uom") or frappe.db.get_value("Item", d, "stock_uom") se_child = self.append('items') - se_child.s_warehouse = item_dict[d].get("from_warehouse") - se_child.t_warehouse = item_dict[d].get("to_warehouse") - se_child.item_code = item_dict[d].get('item_code') or cstr(d) - se_child.uom = item_dict[d]["uom"] if item_dict[d].get("uom") else stock_uom + se_child.s_warehouse = item_row.get("from_warehouse") + se_child.t_warehouse = item_row.get("to_warehouse") + se_child.item_code = item_row.get('item_code') or cstr(d) + se_child.uom = item_row["uom"] if item_row.get("uom") else stock_uom se_child.stock_uom = stock_uom - se_child.qty = flt(item_dict[d]["qty"], se_child.precision("qty")) - se_child.allow_alternative_item = item_dict[d].get("allow_alternative_item", 0) - se_child.subcontracted_item = item_dict[d].get("main_item_code") - se_child.cost_center = (item_dict[d].get("cost_center") or - get_default_cost_center(item_dict[d], company = self.company)) - se_child.is_finished_item = item_dict[d].get("is_finished_item", 0) - se_child.is_scrap_item = item_dict[d].get("is_scrap_item", 0) - se_child.is_process_loss = item_dict[d].get("is_process_loss", 0) + se_child.qty = flt(item_row["qty"], se_child.precision("qty")) + se_child.allow_alternative_item = item_row.get("allow_alternative_item", 0) + se_child.subcontracted_item = item_row.get("main_item_code") + se_child.cost_center = (item_row.get("cost_center") or + get_default_cost_center(item_row, company = self.company)) + se_child.is_finished_item = item_row.get("is_finished_item", 0) + se_child.is_scrap_item = item_row.get("is_scrap_item", 0) + se_child.is_process_loss = item_row.get("is_process_loss", 0) for field in ["idx", "po_detail", "original_item", "expense_account", "description", "item_name", "serial_no", "batch_no", "allow_zero_valuation_rate"]: - if item_dict[d].get(field): - se_child.set(field, item_dict[d].get(field)) + if item_row.get(field): + se_child.set(field, item_row.get(field)) if se_child.s_warehouse==None: se_child.s_warehouse = self.from_warehouse @@ -1515,12 +1596,11 @@ class StockEntry(StockController): se_child.t_warehouse = self.to_warehouse # in stock uom - se_child.conversion_factor = flt(item_dict[d].get("conversion_factor")) or 1 - se_child.transfer_qty = flt(item_dict[d]["qty"]*se_child.conversion_factor, se_child.precision("qty")) + se_child.conversion_factor = flt(item_row.get("conversion_factor")) or 1 + se_child.transfer_qty = flt(item_row["qty"]*se_child.conversion_factor, se_child.precision("qty")) - - # to be assigned for finished item - se_child.bom_no = bom_no + se_child.bom_no = bom_no # to be assigned for finished item + se_child.job_card_item = item_row.get("job_card_item") if self.get("job_card") else None def validate_with_material_request(self): for item in self.get("items"): diff --git a/erpnext/stock/doctype/stock_entry/test_stock_entry.py b/erpnext/stock/doctype/stock_entry/test_stock_entry.py index 067946785a9..b874874adfb 100644 --- a/erpnext/stock/doctype/stock_entry/test_stock_entry.py +++ b/erpnext/stock/doctype/stock_entry/test_stock_entry.py @@ -15,7 +15,10 @@ from erpnext.stock.doctype.item.test_item import ( set_item_variant_settings, ) from erpnext.stock.doctype.serial_no.serial_no import * # noqa -from erpnext.stock.doctype.stock_entry.stock_entry import move_sample_to_retention_warehouse +from erpnext.stock.doctype.stock_entry.stock_entry import ( + FinishedGoodError, + move_sample_to_retention_warehouse, +) from erpnext.stock.doctype.stock_entry.stock_entry_utils import make_stock_entry from erpnext.stock.doctype.stock_ledger_entry.stock_ledger_entry import StockFreezeError from erpnext.stock.doctype.stock_reconciliation.stock_reconciliation import ( @@ -24,7 +27,8 @@ from erpnext.stock.doctype.stock_reconciliation.stock_reconciliation import ( from erpnext.stock.doctype.stock_reconciliation.test_stock_reconciliation import ( create_stock_reconciliation, ) -from erpnext.stock.stock_ledger import get_previous_sle +from erpnext.stock.stock_ledger import NegativeStockError, get_previous_sle +from erpnext.tests.utils import ERPNextTestCase, change_settings def get_sle(**args): @@ -38,9 +42,10 @@ def get_sle(**args): order by timestamp(posting_date, posting_time) desc, creation desc limit 1"""% condition, values, as_dict=1) -class TestStockEntry(unittest.TestCase): +class TestStockEntry(ERPNextTestCase): def tearDown(self): frappe.set_user("Administrator") + frappe.db.set_value("Manufacturing Settings", None, "material_consumption", "0") def test_fifo(self): frappe.db.set_value("Stock Settings", None, "allow_negative_stock", 1) @@ -582,6 +587,65 @@ class TestStockEntry(unittest.TestCase): self.assertEqual(fg_cost, flt(rm_cost + bom_operation_cost + work_order.additional_operating_cost, 2)) + def test_work_order_manufacture_with_material_consumption(self): + from erpnext.manufacturing.doctype.work_order.work_order import ( + make_stock_entry as _make_stock_entry, + ) + frappe.db.set_value("Manufacturing Settings", None, "material_consumption", "1") + + bom_no = frappe.db.get_value("BOM", {"item": "_Test FG Item", + "is_default": 1, "docstatus": 1}) + + work_order = frappe.new_doc("Work Order") + work_order.update({ + "company": "_Test Company", + "fg_warehouse": "_Test Warehouse 1 - _TC", + "production_item": "_Test FG Item", + "bom_no": bom_no, + "qty": 1.0, + "stock_uom": "_Test UOM", + "wip_warehouse": "_Test Warehouse - _TC" + }) + work_order.insert() + work_order.submit() + + make_stock_entry(item_code="_Test Item", + target="Stores - _TC", qty=10, basic_rate=5000.0) + make_stock_entry(item_code="_Test Item Home Desktop 100", + target="Stores - _TC", qty=10, basic_rate=1000.0) + + + s = frappe.get_doc(_make_stock_entry(work_order.name, "Material Transfer for Manufacture", 1)) + for d in s.get("items"): + d.s_warehouse = "Stores - _TC" + s.insert() + s.submit() + + # When Stock Entry has RM and FG + s = frappe.get_doc(_make_stock_entry(work_order.name, "Manufacture", 1)) + s.save() + rm_cost = 0 + for d in s.get('items'): + if d.s_warehouse: + rm_cost += d.amount + fg_cost = list(filter(lambda x: x.item_code=="_Test FG Item", s.get("items")))[0].amount + scrap_cost = list(filter(lambda x: x.is_scrap_item, s.get("items")))[0].amount + self.assertEqual(fg_cost, + flt(rm_cost - scrap_cost, 2)) + + # When Stock Entry has only FG + Scrap + s.items.pop(0) + s.items.pop(0) + s.submit() + + rm_cost = 0 + for d in s.get('items'): + if d.s_warehouse: + rm_cost += d.amount + self.assertEqual(rm_cost, 0) + expected_fg_cost = s.get_basic_rate_for_manufactured_item(1) + fg_cost = list(filter(lambda x: x.item_code=="_Test FG Item", s.get("items")))[0].amount + self.assertEqual(flt(fg_cost, 2), flt(expected_fg_cost, 2)) def test_variant_work_order(self): bom_no = frappe.db.get_value("BOM", {"item": "_Test Variant Item", @@ -868,6 +932,115 @@ class TestStockEntry(unittest.TestCase): distributed_costs = [d.additional_cost for d in se.items] self.assertEqual([40.0, 60.0], distributed_costs) + def test_independent_manufacture_entry(self): + "Test FG items and incoming rate calculation in Maniufacture Entry without WO or BOM linked." + se = frappe.get_doc( + doctype="Stock Entry", + purpose="Manufacture", + stock_entry_type="Manufacture", + company="_Test Company", + items=[ + frappe._dict(item_code="_Test Item", qty=1, basic_rate=200, s_warehouse="_Test Warehouse - _TC"), + frappe._dict(item_code="_Test FG Item", qty=4, t_warehouse="_Test Warehouse 1 - _TC") + ] + ) + # SE must have atleast one FG + self.assertRaises(FinishedGoodError, se.save) + + se.items[0].is_finished_item = 1 + se.items[1].is_finished_item = 1 + # SE cannot have multiple FGs + self.assertRaises(FinishedGoodError, se.save) + + se.items[0].is_finished_item = 0 + se.save() + + # Check if FG cost is calculated based on RM total cost + # RM total cost = 200, FG rate = 200/4(FG qty) = 50 + self.assertEqual(se.items[1].basic_rate, 50) + self.assertEqual(se.value_difference, 0.0) + self.assertEqual(se.total_incoming_value, se.total_outgoing_value) + + # teardown + se.delete() + + @change_settings("Stock Settings", {"allow_negative_stock": 0}) + def test_future_negative_sle(self): + # Initialize item, batch, warehouse, opening qty + item_code = '_Test Future Neg Item' + batch_no = '_Test Future Neg Batch' + warehouses = [ + '_Test Future Neg Warehouse Source', + '_Test Future Neg Warehouse Destination' + ] + warehouse_names = initialize_records_for_future_negative_sle_test( + item_code, batch_no, warehouses, + opening_qty=2, posting_date='2021-07-01' + ) + + # Executing an illegal sequence should raise an error + sequence_of_entries = [ + dict(item_code=item_code, + qty=2, + from_warehouse=warehouse_names[0], + to_warehouse=warehouse_names[1], + batch_no=batch_no, + posting_date='2021-07-03', + purpose='Material Transfer'), + dict(item_code=item_code, + qty=2, + from_warehouse=warehouse_names[1], + to_warehouse=warehouse_names[0], + batch_no=batch_no, + posting_date='2021-07-04', + purpose='Material Transfer'), + dict(item_code=item_code, + qty=2, + from_warehouse=warehouse_names[0], + to_warehouse=warehouse_names[1], + batch_no=batch_no, + posting_date='2021-07-02', # Illegal SE + purpose='Material Transfer') + ] + + self.assertRaises(NegativeStockError, create_stock_entries, sequence_of_entries) + + @change_settings("Stock Settings", {"allow_negative_stock": 0}) + def test_future_negative_sle_batch(self): + from erpnext.stock.doctype.batch.test_batch import TestBatch + + # Initialize item, batch, warehouse, opening qty + item_code = '_Test MultiBatch Item' + TestBatch.make_batch_item(item_code) + + batch_nos = [] # store generate batches + warehouse = '_Test Warehouse - _TC' + + se1 = make_stock_entry( + item_code=item_code, + qty=2, + to_warehouse=warehouse, + posting_date='2021-09-01', + purpose='Material Receipt' + ) + batch_nos.append(se1.items[0].batch_no) + se2 = make_stock_entry( + item_code=item_code, + qty=2, + to_warehouse=warehouse, + posting_date='2021-09-03', + purpose='Material Receipt' + ) + batch_nos.append(se2.items[0].batch_no) + + with self.assertRaises(NegativeStockError) as nse: + make_stock_entry(item_code=item_code, + qty=1, + from_warehouse=warehouse, + batch_no=batch_nos[1], + posting_date='2021-09-02', # backdated consumption of 2nd batch + purpose='Material Issue') + def make_serialized_item(**args): args = frappe._dict(args) se = frappe.copy_doc(test_records[0]) @@ -938,3 +1111,31 @@ def get_multiple_items(): ] test_records = frappe.get_test_records('Stock Entry') + +def initialize_records_for_future_negative_sle_test( + item_code, batch_no, warehouses, opening_qty, posting_date): + from erpnext.stock.doctype.batch.test_batch import TestBatch, make_new_batch + from erpnext.stock.doctype.stock_reconciliation.test_stock_reconciliation import ( + create_stock_reconciliation, + ) + from erpnext.stock.doctype.warehouse.test_warehouse import create_warehouse + + TestBatch.make_batch_item(item_code) + make_new_batch(item_code=item_code, batch_id=batch_no) + warehouse_names = [create_warehouse(w) for w in warehouses] + create_stock_reconciliation( + purpose='Opening Stock', + posting_date=posting_date, + posting_time='20:00:20', + item_code=item_code, + warehouse=warehouse_names[0], + valuation_rate=100, + qty=opening_qty, + batch_no=batch_no, + ) + return warehouse_names + + +def create_stock_entries(sequence_of_entries): + for entry_detail in sequence_of_entries: + make_stock_entry(**entry_detail) diff --git a/erpnext/stock/doctype/stock_entry/tests/test_stock_entry_for_manufacture.js b/erpnext/stock/doctype/stock_entry/tests/test_stock_entry_for_manufacture.js deleted file mode 100644 index e51c90cf51f..00000000000 --- a/erpnext/stock/doctype/stock_entry/tests/test_stock_entry_for_manufacture.js +++ /dev/null @@ -1,26 +0,0 @@ -QUnit.module('Stock'); - -QUnit.test("test manufacture from bom", function(assert) { - assert.expect(2); - let done = assert.async(); - frappe.run_serially([ - () => { - return frappe.tests.make("Stock Entry", [ - { purpose: "Manufacture" }, - { from_bom: 1 }, - { bom_no: "BOM-_Test Item - Non Whole UOM-001" }, - { fg_completed_qty: 2 } - ]); - }, - () => cur_frm.save(), - () => frappe.click_button("Update Rate and Availability"), - () => { - assert.ok(cur_frm.doc.items[1] === 0.75, " Finished Item Qty correct"); - assert.ok(cur_frm.doc.items[2] === 0.25, " Process Loss Item Qty correct"); - }, - () => frappe.tests.click_button('Submit'), - () => frappe.tests.click_button('Yes'), - () => frappe.timeout(0.3), - () => done() - ]); -}); diff --git a/erpnext/stock/doctype/stock_entry/tests/test_stock_entry_for_material_issue.js b/erpnext/stock/doctype/stock_entry/tests/test_stock_entry_for_material_issue.js deleted file mode 100644 index a87a7fb7fd8..00000000000 --- a/erpnext/stock/doctype/stock_entry/tests/test_stock_entry_for_material_issue.js +++ /dev/null @@ -1,30 +0,0 @@ -QUnit.module('Stock'); - -QUnit.test("test material request", function(assert) { - assert.expect(2); - let done = assert.async(); - frappe.run_serially([ - () => { - return frappe.tests.make('Stock Entry', [ - {from_warehouse:'Stores - '+frappe.get_abbr(frappe.defaults.get_default('Company'))}, - {items: [ - [ - {'item_code': 'Test Product 1'}, - {'qty': 5}, - ] - ]}, - ]); - }, - () => cur_frm.save(), - () => frappe.click_button('Update Rate and Availability'), - () => { - // get_item_details - assert.ok(cur_frm.doc.items[0].item_name=='Test Product 1', "Item name correct"); - assert.ok(cur_frm.doc.total_outgoing_value==500, " Outgoing Value correct"); - }, - () => frappe.tests.click_button('Submit'), - () => frappe.tests.click_button('Yes'), - () => frappe.timeout(0.3), - () => done() - ]); -}); diff --git a/erpnext/stock/doctype/stock_entry/tests/test_stock_entry_for_material_issue_with_serialize_item.js b/erpnext/stock/doctype/stock_entry/tests/test_stock_entry_for_material_issue_with_serialize_item.js deleted file mode 100644 index cae318d8f2c..00000000000 --- a/erpnext/stock/doctype/stock_entry/tests/test_stock_entry_for_material_issue_with_serialize_item.js +++ /dev/null @@ -1,34 +0,0 @@ -QUnit.module('Stock'); - -QUnit.test("test material issue", function(assert) { - assert.expect(2); - let done = assert.async(); - frappe.run_serially([ - () => { - return frappe.tests.make('Stock Entry', [ - {from_warehouse:'Stores - '+frappe.get_abbr(frappe.defaults.get_default('Company'))}, - {items: [ - [ - {'item_code': 'Test Product 4'}, - {'qty': 1}, - {'batch_no':'TEST-BATCH-001'}, - {'serial_no':'Test-Product-003'}, - {'basic_rate':100}, - ] - ]}, - ]); - }, - () => cur_frm.save(), - () => frappe.click_button('Close'), - () => frappe.click_button('Update Rate and Availability'), - () => { - // get_item_details - assert.ok(cur_frm.doc.items[0].item_name=='Test Product 4', "Item name correct"); - assert.ok(cur_frm.doc.total_outgoing_value==100, " Outgoing Value correct"); - }, - () => frappe.tests.click_button('Submit'), - () => frappe.tests.click_button('Yes'), - () => frappe.timeout(0.3), - () => done() - ]); -}); diff --git a/erpnext/stock/doctype/stock_entry/tests/test_stock_entry_for_material_receipt.js b/erpnext/stock/doctype/stock_entry/tests/test_stock_entry_for_material_receipt.js deleted file mode 100644 index ef0286fe1b9..00000000000 --- a/erpnext/stock/doctype/stock_entry/tests/test_stock_entry_for_material_receipt.js +++ /dev/null @@ -1,31 +0,0 @@ -QUnit.module('Stock'); - -QUnit.test("test material request", function(assert) { - assert.expect(2); - let done = assert.async(); - frappe.run_serially([ - () => { - return frappe.tests.make('Stock Entry', [ - {purpose:'Material Receipt'}, - {to_warehouse:'Stores - '+frappe.get_abbr(frappe.defaults.get_default('Company'))}, - {items: [ - [ - {'item_code': 'Test Product 1'}, - {'qty': 5}, - ] - ]}, - ]); - }, - () => cur_frm.save(), - () => frappe.click_button('Update Rate and Availability'), - () => { - // get_item_details - assert.ok(cur_frm.doc.items[0].item_name=='Test Product 1', "Item name correct"); - assert.ok(cur_frm.doc.total_incoming_value==500, " Incoming Value correct"); - }, - () => frappe.tests.click_button('Submit'), - () => frappe.tests.click_button('Yes'), - () => frappe.timeout(0.3), - () => done() - ]); -}); diff --git a/erpnext/stock/doctype/stock_entry/tests/test_stock_entry_for_material_receipt_for_serialize_item.js b/erpnext/stock/doctype/stock_entry/tests/test_stock_entry_for_material_receipt_for_serialize_item.js deleted file mode 100644 index 54e1ac81211..00000000000 --- a/erpnext/stock/doctype/stock_entry/tests/test_stock_entry_for_material_receipt_for_serialize_item.js +++ /dev/null @@ -1,34 +0,0 @@ -QUnit.module('Stock'); - -QUnit.test("test material receipt", function(assert) { - assert.expect(2); - let done = assert.async(); - frappe.run_serially([ - () => { - return frappe.tests.make('Stock Entry', [ - {purpose:'Material Receipt'}, - {to_warehouse:'Stores - '+frappe.get_abbr(frappe.defaults.get_default('Company'))}, - {items: [ - [ - {'item_code': 'Test Product 4'}, - {'qty': 5}, - {'batch_no':'TEST-BATCH-001'}, - {'serial_no':'Test-Product-001\nTest-Product-002\nTest-Product-003\nTest-Product-004\nTest-Product-005'}, - {'basic_rate':100}, - ] - ]}, - ]); - }, - () => cur_frm.save(), - () => frappe.click_button('Update Rate and Availability'), - () => { - // get_item_details - assert.ok(cur_frm.doc.items[0].item_name=='Test Product 4', "Item name correct"); - assert.ok(cur_frm.doc.total_incoming_value==500, " Incoming Value correct"); - }, - () => frappe.tests.click_button('Submit'), - () => frappe.tests.click_button('Yes'), - () => frappe.timeout(0.3), - () => done() - ]); -}); diff --git a/erpnext/stock/doctype/stock_entry/tests/test_stock_entry_for_material_transfer.js b/erpnext/stock/doctype/stock_entry/tests/test_stock_entry_for_material_transfer.js deleted file mode 100644 index fac0b4b8922..00000000000 --- a/erpnext/stock/doctype/stock_entry/tests/test_stock_entry_for_material_transfer.js +++ /dev/null @@ -1,33 +0,0 @@ -QUnit.module('Stock'); - -QUnit.test("test material request", function(assert) { - assert.expect(3); - let done = assert.async(); - frappe.run_serially([ - () => { - return frappe.tests.make('Stock Entry', [ - {purpose:'Material Transfer'}, - {from_warehouse:'Stores - '+frappe.get_abbr(frappe.defaults.get_default('Company'))}, - {to_warehouse:'Work In Progress - '+frappe.get_abbr(frappe.defaults.get_default('Company'))}, - {items: [ - [ - {'item_code': 'Test Product 1'}, - {'qty': 5}, - ] - ]}, - ]); - }, - () => cur_frm.save(), - () => frappe.click_button('Update Rate and Availability'), - () => { - // get_item_details - assert.ok(cur_frm.doc.items[0].item_name=='Test Product 1', "Item name correct"); - assert.ok(cur_frm.doc.total_outgoing_value==500, " Outgoing Value correct"); - assert.ok(cur_frm.doc.total_incoming_value==500, " Incoming Value correct"); - }, - () => frappe.tests.click_button('Submit'), - () => frappe.tests.click_button('Yes'), - () => frappe.timeout(0.3), - () => done() - ]); -}); diff --git a/erpnext/stock/doctype/stock_entry/tests/test_stock_entry_for_material_transfer_for_manufacture.js b/erpnext/stock/doctype/stock_entry/tests/test_stock_entry_for_material_transfer_for_manufacture.js deleted file mode 100644 index 9f853072709..00000000000 --- a/erpnext/stock/doctype/stock_entry/tests/test_stock_entry_for_material_transfer_for_manufacture.js +++ /dev/null @@ -1,33 +0,0 @@ -QUnit.module('Stock'); - -QUnit.test("test material Transfer to manufacture", function(assert) { - assert.expect(3); - let done = assert.async(); - frappe.run_serially([ - () => { - return frappe.tests.make('Stock Entry', [ - {purpose:'Material Transfer for Manufacture'}, - {from_warehouse:'Stores - '+frappe.get_abbr(frappe.defaults.get_default('Company'))}, - {to_warehouse:'Work In Progress - '+frappe.get_abbr(frappe.defaults.get_default('Company'))}, - {items: [ - [ - {'item_code': 'Test Product 1'}, - {'qty': 1}, - ] - ]}, - ]); - }, - () => cur_frm.save(), - () => frappe.click_button('Update Rate and Availability'), - () => { - // get_item_details - assert.ok(cur_frm.doc.items[0].item_name=='Test Product 1', "Item name correct"); - assert.ok(cur_frm.doc.total_outgoing_value==100, " Outgoing Value correct"); - assert.ok(cur_frm.doc.total_incoming_value==100, " Incoming Value correct"); - }, - () => frappe.tests.click_button('Submit'), - () => frappe.tests.click_button('Yes'), - () => frappe.timeout(0.3), - () => done() - ]); -}); diff --git a/erpnext/stock/doctype/stock_entry/tests/test_stock_entry_for_repack.js b/erpnext/stock/doctype/stock_entry/tests/test_stock_entry_for_repack.js deleted file mode 100644 index 20f119ad617..00000000000 --- a/erpnext/stock/doctype/stock_entry/tests/test_stock_entry_for_repack.js +++ /dev/null @@ -1,41 +0,0 @@ -QUnit.module('Stock'); - -QUnit.test("test repack", function(assert) { - assert.expect(2); - let done = assert.async(); - frappe.run_serially([ - () => { - return frappe.tests.make('Stock Entry', [ - {purpose:'Repack'}, - {items: [ - [ - {'item_code': 'Test Product 1'}, - {'qty': 1}, - {'s_warehouse':'Stores - '+frappe.get_abbr(frappe.defaults.get_default('Company'))}, - ], - [ - {'item_code': 'Test Product 2'}, - {'qty': 1}, - {'s_warehouse':'Stores - '+frappe.get_abbr(frappe.defaults.get_default('Company'))}, - ], - [ - {'item_code': 'Test Product 3'}, - {'qty': 1}, - {'t_warehouse':'Work In Progress - '+frappe.get_abbr(frappe.defaults.get_default('Company'))}, - ], - ]}, - ]); - }, - () => cur_frm.save(), - () => frappe.click_button('Update Rate and Availability'), - () => { - // get_item_details - assert.ok(cur_frm.doc.total_outgoing_value==250, " Outgoing Value correct"); - assert.ok(cur_frm.doc.total_incoming_value==250, " Incoming Value correct"); - }, - () => frappe.tests.click_button('Submit'), - () => frappe.tests.click_button('Yes'), - () => frappe.timeout(0.3), - () => done() - ]); -}); diff --git a/erpnext/stock/doctype/stock_entry/tests/test_stock_entry_for_subcontract.js b/erpnext/stock/doctype/stock_entry/tests/test_stock_entry_for_subcontract.js deleted file mode 100644 index 8243426032d..00000000000 --- a/erpnext/stock/doctype/stock_entry/tests/test_stock_entry_for_subcontract.js +++ /dev/null @@ -1,33 +0,0 @@ -QUnit.module('Stock'); - -QUnit.test("test material Transfer to manufacture", function(assert) { - assert.expect(3); - let done = assert.async(); - frappe.run_serially([ - () => { - return frappe.tests.make('Stock Entry', [ - {purpose:'Send to Subcontractor'}, - {from_warehouse:'Work In Progress - '+frappe.get_abbr(frappe.defaults.get_default('Company'))}, - {to_warehouse:'Finished Goods - '+frappe.get_abbr(frappe.defaults.get_default('Company'))}, - {items: [ - [ - {'item_code': 'Test Product 1'}, - {'qty': 1}, - ] - ]}, - ]); - }, - () => cur_frm.save(), - () => frappe.click_button('Update Rate and Availability'), - () => { - // get_item_details - assert.ok(cur_frm.doc.items[0].item_name=='Test Product 1', "Item name correct"); - assert.ok(cur_frm.doc.total_outgoing_value==100, " Outgoing Value correct"); - assert.ok(cur_frm.doc.total_incoming_value==100, " Incoming Value correct"); - }, - () => frappe.tests.click_button('Submit'), - () => frappe.tests.click_button('Yes'), - () => frappe.timeout(0.3), - () => done() - ]); -}); diff --git a/erpnext/stock/doctype/stock_ledger_entry/stock_ledger_entry.json b/erpnext/stock/doctype/stock_ledger_entry/stock_ledger_entry.json index 2651407d16f..46ce9debf3b 100644 --- a/erpnext/stock/doctype/stock_ledger_entry/stock_ledger_entry.json +++ b/erpnext/stock/doctype/stock_ledger_entry/stock_ledger_entry.json @@ -150,7 +150,7 @@ "fieldtype": "Float", "in_filter": 1, "in_list_view": 1, - "label": "Actual Quantity", + "label": "Qty Change", "oldfieldname": "actual_qty", "oldfieldtype": "Currency", "print_width": "150px", @@ -189,7 +189,7 @@ "fieldname": "qty_after_transaction", "fieldtype": "Float", "in_filter": 1, - "label": "Actual Qty After Transaction", + "label": "Qty After Transaction", "oldfieldname": "bin_aqat", "oldfieldtype": "Currency", "print_width": "150px", @@ -210,7 +210,7 @@ { "fieldname": "stock_value", "fieldtype": "Currency", - "label": "Stock Value", + "label": "Balance Stock Value", "oldfieldname": "stock_value", "oldfieldtype": "Currency", "options": "Company:company:default_currency", @@ -219,14 +219,14 @@ { "fieldname": "stock_value_difference", "fieldtype": "Currency", - "label": "Stock Value Difference", + "label": "Change in Stock Value", "options": "Company:company:default_currency", "read_only": 1 }, { "fieldname": "stock_queue", "fieldtype": "Text", - "label": "Stock Queue (FIFO)", + "label": "FIFO Stock Queue (qty, rate)", "oldfieldname": "fcfs_stack", "oldfieldtype": "Text", "print_hide": 1, @@ -317,10 +317,11 @@ "in_create": 1, "index_web_pages_for_search": 1, "links": [], - "modified": "2021-10-08 13:42:51.857631", + "modified": "2021-12-21 06:25:30.040801", "modified_by": "Administrator", "module": "Stock", "name": "Stock Ledger Entry", + "naming_rule": "Expression (old style)", "owner": "Administrator", "permissions": [ { @@ -338,5 +339,6 @@ } ], "sort_field": "modified", - "sort_order": "DESC" -} + "sort_order": "DESC", + "states": [] +} \ No newline at end of file diff --git a/erpnext/stock/doctype/stock_ledger_entry/stock_ledger_entry.py b/erpnext/stock/doctype/stock_ledger_entry/stock_ledger_entry.py index 93bca7a6947..c53830799d4 100644 --- a/erpnext/stock/doctype/stock_ledger_entry/stock_ledger_entry.py +++ b/erpnext/stock/doctype/stock_ledger_entry/stock_ledger_entry.py @@ -8,7 +8,7 @@ import frappe from frappe import _ from frappe.core.doctype.role.role import get_users from frappe.model.document import Document -from frappe.utils import add_days, cint, flt, formatdate, get_datetime, getdate +from frappe.utils import add_days, cint, formatdate, get_datetime, getdate from erpnext.accounts.utils import get_fiscal_year from erpnext.controllers.item_variant import ItemTemplateCannotHaveStock @@ -43,7 +43,6 @@ class StockLedgerEntry(Document): def on_submit(self): self.check_stock_frozen_date() - self.actual_amt_check() self.calculate_batch_qty() if not self.get("via_landed_cost_voucher"): @@ -57,18 +56,6 @@ class StockLedgerEntry(Document): "sum(actual_qty)") or 0 frappe.db.set_value("Batch", self.batch_no, "batch_qty", batch_qty) - def actual_amt_check(self): - """Validate that qty at warehouse for selected batch is >=0""" - if self.batch_no and not self.get("allow_negative_stock"): - batch_bal_after_transaction = flt(frappe.db.sql("""select sum(actual_qty) - from `tabStock Ledger Entry` - where is_cancelled =0 and warehouse=%s and item_code=%s and batch_no=%s""", - (self.warehouse, self.item_code, self.batch_no))[0][0]) - - if batch_bal_after_transaction < 0: - frappe.throw(_("Stock balance in Batch {0} will become negative {1} for Item {2} at Warehouse {3}") - .format(self.batch_no, batch_bal_after_transaction, self.item_code, self.warehouse)) - def validate_mandatory(self): mandatory = ['warehouse','posting_date','voucher_type','voucher_no','company'] for k in mandatory: diff --git a/erpnext/stock/doctype/stock_reconciliation/stock_reconciliation.json b/erpnext/stock/doctype/stock_reconciliation/stock_reconciliation.json index b7d1497319f..3402972bb89 100644 --- a/erpnext/stock/doctype/stock_reconciliation/stock_reconciliation.json +++ b/erpnext/stock/doctype/stock_reconciliation/stock_reconciliation.json @@ -1,4 +1,5 @@ { + "actions": [], "autoname": "naming_series:", "creation": "2013-03-28 10:35:31", "description": "This tool helps you to update or fix the quantity and valuation of stock in the system. It is typically used to synchronise the system values and what actually exists in your warehouses.", @@ -153,11 +154,12 @@ "icon": "fa fa-upload-alt", "idx": 1, "is_submittable": 1, - "max_attachments": 1, - "modified": "2020-04-08 17:02:47.196206", + "links": [], + "modified": "2021-11-30 01:33:51.437194", "modified_by": "Administrator", "module": "Stock", "name": "Stock Reconciliation", + "naming_rule": "By \"Naming Series\" field", "owner": "Administrator", "permissions": [ { diff --git a/erpnext/stock/doctype/stock_reconciliation/test_stock_reconciliation.js b/erpnext/stock/doctype/stock_reconciliation/test_stock_reconciliation.js deleted file mode 100644 index 666d2c7144f..00000000000 --- a/erpnext/stock/doctype/stock_reconciliation/test_stock_reconciliation.js +++ /dev/null @@ -1,31 +0,0 @@ -QUnit.module('Stock'); - -QUnit.test("test Stock Reconciliation", function(assert) { - assert.expect(1); - let done = assert.async(); - frappe.run_serially([ - () => frappe.set_route('List', 'Stock Reconciliation'), - () => frappe.timeout(1), - () => frappe.click_button('New'), - () => cur_frm.set_value('company','For Testing'), - () => frappe.click_button('Items'), - () => {cur_dialog.set_value('warehouse','Stores - FT'); }, - () => frappe.timeout(0.5), - () => frappe.click_button('Update'), - () => { - cur_frm.doc.items[0].qty = 150; - cur_frm.refresh_fields('items');}, - () => frappe.timeout(0.5), - () => cur_frm.set_value('expense_account','Stock Adjustment - FT'), - () => cur_frm.set_value('cost_center','Main - FT'), - () => cur_frm.save(), - () => { - // get_item_details - assert.ok(cur_frm.doc.expense_account=='Stock Adjustment - FT', "expense_account correct"); - }, - () => frappe.tests.click_button('Submit'), - () => frappe.tests.click_button('Yes'), - () => frappe.timeout(0.3), - () => done() - ]); -}); diff --git a/erpnext/stock/doctype/stock_reconciliation/test_stock_reconciliation.py b/erpnext/stock/doctype/stock_reconciliation/test_stock_reconciliation.py index de89b2b1c4d..c4ddc9e2d6f 100644 --- a/erpnext/stock/doctype/stock_reconciliation/test_stock_reconciliation.py +++ b/erpnext/stock/doctype/stock_reconciliation/test_stock_reconciliation.py @@ -24,11 +24,15 @@ from erpnext.tests.utils import ERPNextTestCase, change_settings class TestStockReconciliation(ERPNextTestCase): @classmethod - def setUpClass(self): + def setUpClass(cls): super().setUpClass() create_batch_or_serial_no_items() frappe.db.set_value("Stock Settings", None, "allow_negative_stock", 1) + def tearDown(self): + frappe.flags.dont_execute_stock_reposts = None + + def test_reco_for_fifo(self): self._test_reco_sle_gle("FIFO") @@ -392,6 +396,41 @@ class TestStockReconciliation(ERPNextTestCase): repost_exists = bool(frappe.db.exists("Repost Item Valuation", {"voucher_no": sr.name})) self.assertFalse(repost_exists, msg="Negative stock validation not working on reco cancellation") + def test_intermediate_sr_bin_update(self): + """Bin should show correct qty even for backdated entries. + + ------------------------------------------- + | creation | Var | Doc | Qty | balance qty + ------------------------------------------- + | 1 | SR | Reco | 10 | 10 (posting date: today+10) + | 3 | SR2 | Reco | 11 | 11 (posting date: today+11) + | 2 | DN | DN | 5 | 6 <-- assert in BIN (posting date: today+12) + """ + from erpnext.stock.doctype.delivery_note.test_delivery_note import create_delivery_note + + # repost will make this test useless, qty should update in realtime without reposts + frappe.flags.dont_execute_stock_reposts = True + frappe.db.rollback() + + item_code = "Backdated-Reco-Cancellation-Item" + warehouse = "_Test Warehouse - _TC" + create_item(item_code) + + sr = create_stock_reconciliation(item_code=item_code, warehouse=warehouse, qty=10, rate=100, + posting_date=add_days(nowdate(), 10)) + + dn = create_delivery_note(item_code=item_code, warehouse=warehouse, qty=5, rate=120, + posting_date=add_days(nowdate(), 12)) + old_bin_qty = frappe.db.get_value("Bin", {"item_code": item_code, "warehouse": warehouse}, "actual_qty") + + sr2 = create_stock_reconciliation(item_code=item_code, warehouse=warehouse, qty=11, rate=100, + posting_date=add_days(nowdate(), 11)) + new_bin_qty = frappe.db.get_value("Bin", {"item_code": item_code, "warehouse": warehouse}, "actual_qty") + + self.assertEqual(old_bin_qty + 1, new_bin_qty) + frappe.db.rollback() + + def test_valid_batch(self): create_batch_item_with_batch("Testing Batch Item 1", "001") create_batch_item_with_batch("Testing Batch Item 2", "002") @@ -399,6 +438,34 @@ class TestStockReconciliation(ERPNextTestCase): , do_not_submit=True) self.assertRaises(frappe.ValidationError, sr.submit) + def test_serial_no_cancellation(self): + + from erpnext.stock.doctype.stock_entry.test_stock_entry import make_stock_entry + item = create_item("Stock-Reco-Serial-Item-9", is_stock_item=1) + if not item.has_serial_no: + item.has_serial_no = 1 + item.serial_no_series = "SRS9.####" + item.save() + + item_code = item.name + warehouse = "_Test Warehouse - _TC" + + se1 = make_stock_entry(item_code=item_code, target=warehouse, qty=10, basic_rate=700) + + serial_nos = get_serial_nos(se1.items[0].serial_no) + # reduce 1 item + serial_nos.pop() + new_serial_nos = "\n".join(serial_nos) + + sr = create_stock_reconciliation(item_code=item.name, warehouse=warehouse, serial_no=new_serial_nos, qty=9) + sr.cancel() + + active_sr_no = frappe.get_all("Serial No", + filters={"item_code": item_code, "warehouse": warehouse, "status": "Active"}) + + self.assertEqual(len(active_sr_no), 10) + + def create_batch_item_with_batch(item_name, batch_id): batch_item_doc = create_item(item_name, is_stock_item=1) if not batch_item_doc.has_batch_no: diff --git a/erpnext/stock/doctype/stock_reposting_settings/stock_reposting_settings.json b/erpnext/stock/doctype/stock_reposting_settings/stock_reposting_settings.json index 24740590037..0facae8d3b8 100644 --- a/erpnext/stock/doctype/stock_reposting_settings/stock_reposting_settings.json +++ b/erpnext/stock/doctype/stock_reposting_settings/stock_reposting_settings.json @@ -1,6 +1,7 @@ { "actions": [], "allow_rename": 1, + "beta": 1, "creation": "2021-10-01 10:56:30.814787", "doctype": "DocType", "editable_grid": 1, @@ -10,7 +11,8 @@ "limit_reposting_timeslot", "start_time", "end_time", - "limits_dont_apply_on" + "limits_dont_apply_on", + "item_based_reposting" ], "fields": [ { @@ -44,12 +46,18 @@ "fieldname": "limit_reposting_timeslot", "fieldtype": "Check", "label": "Limit timeslot for Stock Reposting" + }, + { + "default": "0", + "fieldname": "item_based_reposting", + "fieldtype": "Check", + "label": "Use Item based reposting" } ], "index_web_pages_for_search": 1, "issingle": 1, "links": [], - "modified": "2021-10-01 11:27:28.981594", + "modified": "2021-11-02 01:22:45.155841", "modified_by": "Administrator", "module": "Stock", "name": "Stock Reposting Settings", diff --git a/erpnext/stock/doctype/stock_settings/stock_settings.py b/erpnext/stock/doctype/stock_settings/stock_settings.py index 2dd103d607f..c1293cbf0fa 100644 --- a/erpnext/stock/doctype/stock_settings/stock_settings.py +++ b/erpnext/stock/doctype/stock_settings/stock_settings.py @@ -11,6 +11,8 @@ from frappe.model.document import Document from frappe.utils import cint from frappe.utils.html_utils import clean_html +from erpnext.stock.utils import check_pending_reposting + class StockSettings(Document): def validate(self): @@ -20,7 +22,7 @@ class StockSettings(Document): from erpnext.setup.doctype.naming_series.naming_series import set_by_naming_series set_by_naming_series("Item", "item_code", - self.get("item_naming_by")=="Naming Series", hide_name_field=True) + self.get("item_naming_by")=="Naming Series", hide_name_field=True, make_mandatory=0) stock_frozen_limit = 356 submitted_stock_frozen = self.stock_frozen_upto_days or 0 @@ -36,6 +38,7 @@ class StockSettings(Document): self.validate_warehouses() self.cant_change_valuation_method() self.validate_clean_description_html() + self.validate_pending_reposts() def validate_warehouses(self): warehouse_fields = ["default_warehouse", "sample_retention_warehouse"] @@ -64,6 +67,11 @@ class StockSettings(Document): # changed to text frappe.enqueue('erpnext.stock.doctype.stock_settings.stock_settings.clean_all_descriptions', now=frappe.flags.in_test) + def validate_pending_reposts(self): + if self.stock_frozen_upto: + check_pending_reposting(self.stock_frozen_upto) + + def on_update(self): self.toggle_warehouse_field_for_inter_warehouse_transfer() diff --git a/erpnext/stock/doctype/warehouse/test_warehouse.js b/erpnext/stock/doctype/warehouse/test_warehouse.js deleted file mode 100644 index 850da1ee45f..00000000000 --- a/erpnext/stock/doctype/warehouse/test_warehouse.js +++ /dev/null @@ -1,19 +0,0 @@ -QUnit.test("test: warehouse", function (assert) { - assert.expect(0); - let done = assert.async(); - - frappe.run_serially([ - // test warehouse creation - () => frappe.set_route("List", "Warehouse"), - - // Create a Laptop Scrap Warehouse - () => frappe.tests.make( - "Warehouse", [ - {warehouse_name: "Laptop Scrap Warehouse"}, - {company: "For Testing"} - ] - ), - - () => done() - ]); -}); diff --git a/erpnext/stock/doctype/warehouse/test_warehouse.py b/erpnext/stock/doctype/warehouse/test_warehouse.py index ca92936a1dc..26db2642e4b 100644 --- a/erpnext/stock/doctype/warehouse/test_warehouse.py +++ b/erpnext/stock/doctype/warehouse/test_warehouse.py @@ -33,65 +33,6 @@ class TestWarehouse(ERPNextTestCase): self.assertEqual(p_warehouse.name, child_warehouse.parent_warehouse) self.assertEqual(child_warehouse.is_group, 0) - def test_warehouse_renaming(self): - create_warehouse("Test Warehouse for Renaming 1", company="_Test Company with perpetual inventory") - account = get_inventory_account("_Test Company with perpetual inventory", "Test Warehouse for Renaming 1 - TCP1") - self.assertTrue(frappe.db.get_value("Warehouse", filters={"account": account})) - - # Rename with abbr - if frappe.db.exists("Warehouse", "Test Warehouse for Renaming 2 - TCP1"): - frappe.delete_doc("Warehouse", "Test Warehouse for Renaming 2 - TCP1") - frappe.rename_doc("Warehouse", "Test Warehouse for Renaming 1 - TCP1", "Test Warehouse for Renaming 2 - TCP1") - - self.assertTrue(frappe.db.get_value("Warehouse", - filters={"account": "Test Warehouse for Renaming 1 - TCP1"})) - - # Rename without abbr - if frappe.db.exists("Warehouse", "Test Warehouse for Renaming 3 - TCP1"): - frappe.delete_doc("Warehouse", "Test Warehouse for Renaming 3 - TCP1") - - frappe.rename_doc("Warehouse", "Test Warehouse for Renaming 2 - TCP1", "Test Warehouse for Renaming 3") - - self.assertTrue(frappe.db.get_value("Warehouse", - filters={"account": "Test Warehouse for Renaming 1 - TCP1"})) - - # Another rename with multiple dashes - if frappe.db.exists("Warehouse", "Test - Warehouse - Company - TCP1"): - frappe.delete_doc("Warehouse", "Test - Warehouse - Company - TCP1") - frappe.rename_doc("Warehouse", "Test Warehouse for Renaming 3 - TCP1", "Test - Warehouse - Company") - - def test_warehouse_merging(self): - company = "_Test Company with perpetual inventory" - create_warehouse("Test Warehouse for Merging 1", company=company, - properties={"parent_warehouse": "All Warehouses - TCP1"}) - create_warehouse("Test Warehouse for Merging 2", company=company, - properties={"parent_warehouse": "All Warehouses - TCP1"}) - - make_stock_entry(item_code="_Test Item", target="Test Warehouse for Merging 1 - TCP1", - qty=1, rate=100, company=company) - make_stock_entry(item_code="_Test Item", target="Test Warehouse for Merging 2 - TCP1", - qty=1, rate=100, company=company) - - existing_bin_qty = ( - cint(frappe.db.get_value("Bin", - {"item_code": "_Test Item", "warehouse": "Test Warehouse for Merging 1 - TCP1"}, "actual_qty")) - + cint(frappe.db.get_value("Bin", - {"item_code": "_Test Item", "warehouse": "Test Warehouse for Merging 2 - TCP1"}, "actual_qty")) - ) - - frappe.rename_doc("Warehouse", "Test Warehouse for Merging 1 - TCP1", - "Test Warehouse for Merging 2 - TCP1", merge=True) - - self.assertFalse(frappe.db.exists("Warehouse", "Test Warehouse for Merging 1 - TCP1")) - - bin_qty = frappe.db.get_value("Bin", - {"item_code": "_Test Item", "warehouse": "Test Warehouse for Merging 2 - TCP1"}, "actual_qty") - - self.assertEqual(bin_qty, existing_bin_qty) - - self.assertTrue(frappe.db.get_value("Warehouse", - filters={"account": "Test Warehouse for Merging 2 - TCP1"})) - def test_unlinking_warehouse_from_item_defaults(self): company = "_Test Company" diff --git a/erpnext/stock/doctype/warehouse/warehouse.json b/erpnext/stock/doctype/warehouse/warehouse.json index 9b9093261c2..05076b51a3e 100644 --- a/erpnext/stock/doctype/warehouse/warehouse.json +++ b/erpnext/stock/doctype/warehouse/warehouse.json @@ -1,7 +1,6 @@ { "actions": [], "allow_import": 1, - "allow_rename": 1, "creation": "2013-03-07 18:50:32", "description": "A logical Warehouse against which stock entries are made.", "doctype": "DocType", @@ -245,7 +244,7 @@ "idx": 1, "is_tree": 1, "links": [], - "modified": "2021-04-09 19:54:56.263965", + "modified": "2021-12-03 04:40:06.414630", "modified_by": "Administrator", "module": "Stock", "name": "Warehouse", diff --git a/erpnext/stock/doctype/warehouse/warehouse.py b/erpnext/stock/doctype/warehouse/warehouse.py index b9dbc388805..9cfad86f142 100644 --- a/erpnext/stock/doctype/warehouse/warehouse.py +++ b/erpnext/stock/doctype/warehouse/warehouse.py @@ -10,7 +10,6 @@ from frappe.contacts.address_and_contact import load_address_and_contact from frappe.utils import cint, flt from frappe.utils.nestedset import NestedSet -import erpnext from erpnext.stock import get_warehouse_account @@ -68,57 +67,6 @@ class Warehouse(NestedSet): return frappe.db.sql("""select name from `tabWarehouse` where parent_warehouse = %s limit 1""", self.name) - def before_rename(self, old_name, new_name, merge=False): - super(Warehouse, self).before_rename(old_name, new_name, merge) - - # Add company abbr if not provided - new_warehouse = erpnext.encode_company_abbr(new_name, self.company) - - if merge: - if not frappe.db.exists("Warehouse", new_warehouse): - frappe.throw(_("Warehouse {0} does not exist").format(new_warehouse)) - - if self.company != frappe.db.get_value("Warehouse", new_warehouse, "company"): - frappe.throw(_("Both Warehouse must belong to same Company")) - - return new_warehouse - - def after_rename(self, old_name, new_name, merge=False): - super(Warehouse, self).after_rename(old_name, new_name, merge) - - new_warehouse_name = self.get_new_warehouse_name_without_abbr(new_name) - self.db_set("warehouse_name", new_warehouse_name) - - if merge: - self.recalculate_bin_qty(new_name) - - def get_new_warehouse_name_without_abbr(self, name): - company_abbr = frappe.get_cached_value('Company', self.company, "abbr") - parts = name.rsplit(" - ", 1) - - if parts[-1].lower() == company_abbr.lower(): - name = parts[0] - - return name - - def recalculate_bin_qty(self, new_name): - from erpnext.stock.stock_balance import repost_stock - frappe.db.auto_commit_on_many_writes = 1 - existing_allow_negative_stock = frappe.db.get_value("Stock Settings", None, "allow_negative_stock") - frappe.db.set_value("Stock Settings", None, "allow_negative_stock", 1) - - repost_stock_for_items = frappe.db.sql_list("""select distinct item_code - from tabBin where warehouse=%s""", new_name) - - # Delete all existing bins to avoid duplicate bins for the same item and warehouse - frappe.db.sql("delete from `tabBin` where warehouse=%s", new_name) - - for item_code in repost_stock_for_items: - repost_stock(item_code, new_name) - - frappe.db.set_value("Stock Settings", None, "allow_negative_stock", existing_allow_negative_stock) - frappe.db.auto_commit_on_many_writes = 0 - def convert_to_group_or_ledger(self): if self.is_group: self.convert_to_ledger() diff --git a/erpnext/stock/form_tour/item/item.json b/erpnext/stock/form_tour/item/item.json index 821e91b28d4..5369366edba 100644 --- a/erpnext/stock/form_tour/item/item.json +++ b/erpnext/stock/form_tour/item/item.json @@ -2,15 +2,17 @@ "creation": "2021-08-24 17:56:40.754909", "docstatus": 0, "doctype": "Form Tour", + "first_document": 0, "idx": 0, + "include_name_field": 0, "is_standard": 1, - "modified": "2021-08-24 18:04:50.928431", + "modified": "2021-11-24 17:59:44.559001", "modified_by": "Administrator", "module": "Stock", "name": "Item", "owner": "Administrator", "reference_doctype": "Item", - "save_on_complete": 0, + "save_on_complete": 1, "steps": [ { "description": "Enter code for Asset Item", @@ -36,14 +38,27 @@ "position": "Bottom", "title": "Asset Item Name" }, + { + "description": "Select an Item Group", + "field": "", + "fieldname": "item_group", + "fieldtype": "Link", + "has_next_condition": 0, + "is_table_field": 0, + "label": "Item Group", + "parent_field": "", + "position": "Right", + "title": "Item Group" + }, { "description": "Check this field to make this an Asset Item", "field": "", "fieldname": "is_fixed_asset", "fieldtype": "Check", - "has_next_condition": 0, + "has_next_condition": 1, "is_table_field": 0, "label": "Is Fixed Asset", + "next_step_condition": "eval:doc.is_fixed_asset", "parent_field": "", "position": "Bottom", "title": "Is this a Fixed Asset?" @@ -53,9 +68,10 @@ "field": "", "fieldname": "auto_create_assets", "fieldtype": "Check", - "has_next_condition": 0, + "has_next_condition": 1, "is_table_field": 0, "label": "Auto Create Assets on Purchase", + "next_step_condition": "eval:doc.auto_create_assets", "parent_field": "", "position": "Bottom", "title": "Auto Create Asset on Purchase" @@ -69,7 +85,7 @@ "is_table_field": 0, "label": "Asset Category", "parent_field": "", - "position": "Bottom", + "position": "Left", "title": "Asset Category" }, { @@ -81,9 +97,9 @@ "is_table_field": 0, "label": "Asset Naming Series", "parent_field": "", - "position": "Bottom", + "position": "Left", "title": "Asset Naming Series" } ], "title": "Item" -} +} \ No newline at end of file diff --git a/erpnext/stock/form_tour/item_general/item_general.json b/erpnext/stock/form_tour/item_general/item_general.json new file mode 100644 index 00000000000..b468d270de6 --- /dev/null +++ b/erpnext/stock/form_tour/item_general/item_general.json @@ -0,0 +1,79 @@ +{ + "creation": "2021-12-02 10:37:55.433087", + "docstatus": 0, + "doctype": "Form Tour", + "first_document": 0, + "idx": 0, + "include_name_field": 0, + "is_standard": 1, + "modified": "2021-12-02 10:37:55.433087", + "modified_by": "Administrator", + "module": "Stock", + "name": "Item General", + "owner": "Administrator", + "reference_doctype": "Item", + "save_on_complete": 1, + "steps": [ + { + "description": "Enter code for the Item", + "field": "", + "fieldname": "item_code", + "fieldtype": "Data", + "has_next_condition": 0, + "is_table_field": 0, + "label": "Item Code", + "parent_field": "", + "position": "Right", + "title": "Item Code" + }, + { + "description": "Enter name for the Item", + "field": "", + "fieldname": "item_name", + "fieldtype": "Data", + "has_next_condition": 0, + "is_table_field": 0, + "label": "Item Name", + "parent_field": "", + "position": "Right", + "title": "Item Name" + }, + { + "description": "Select an Item Group", + "field": "", + "fieldname": "item_group", + "fieldtype": "Link", + "has_next_condition": 0, + "is_table_field": 0, + "label": "Item Group", + "parent_field": "", + "position": "Right", + "title": "Item Group" + }, + { + "description": "This is the default measuring unit that you will use for your product. It could be Nos, Kgs, Meters, etc.", + "field": "", + "fieldname": "stock_uom", + "fieldtype": "Link", + "has_next_condition": 0, + "is_table_field": 0, + "label": "Default Unit of Measure", + "parent_field": "", + "position": "Right", + "title": "Default Unit of Measurement" + }, + { + "description": "When creating an Item, entering a value for this field will automatically create an Item Price at the backend. Entering a value after the Item has been saved will not work. In this case, the Item Price is created from any transactions with the Item.", + "field": "", + "fieldname": "standard_rate", + "fieldtype": "Currency", + "has_next_condition": 0, + "is_table_field": 0, + "label": "Standard Selling Rate", + "parent_field": "", + "position": "Left", + "title": "Standard Selling Rate" + } + ], + "title": "Item General" +} \ No newline at end of file diff --git a/erpnext/stock/get_item_details.py b/erpnext/stock/get_item_details.py index e00382bec1a..06f8fa71a94 100644 --- a/erpnext/stock/get_item_details.py +++ b/erpnext/stock/get_item_details.py @@ -299,7 +299,7 @@ def get_basic_details(args, item, overwrite_warehouse=True): "warehouse": warehouse, "income_account": get_default_income_account(args, item_defaults, item_group_defaults, brand_defaults), "expense_account": expense_account or get_default_expense_account(args, item_defaults, item_group_defaults, brand_defaults) , - "discount_account": None or get_default_discount_account(args, item_defaults), + "discount_account": get_default_discount_account(args, item_defaults), "cost_center": get_default_cost_center(args, item_defaults, item_group_defaults, brand_defaults), 'has_serial_no': item.has_serial_no, 'has_batch_no': item.has_batch_no, @@ -317,6 +317,7 @@ def get_basic_details(args, item, overwrite_warehouse=True): "net_rate": 0.0, "net_amount": 0.0, "discount_percentage": 0.0, + "discount_amount": 0.0, "supplier": get_default_supplier(args, item_defaults, item_group_defaults, brand_defaults), "update_stock": args.get("update_stock") if args.get('doctype') in ['Sales Invoice', 'Purchase Invoice'] else 0, "delivered_by_supplier": item.delivered_by_supplier if args.get("doctype") in ["Sales Order", "Sales Invoice"] else 0, @@ -326,7 +327,8 @@ def get_basic_details(args, item, overwrite_warehouse=True): "against_blanket_order": args.get("against_blanket_order"), "bom_no": item.get("default_bom"), "weight_per_unit": args.get("weight_per_unit") or item.get("weight_per_unit"), - "weight_uom": args.get("weight_uom") or item.get("weight_uom") + "weight_uom": args.get("weight_uom") or item.get("weight_uom"), + "grant_commission": item.get("grant_commission") }) if item.get("enable_deferred_revenue") or item.get("enable_deferred_expense"): @@ -1095,7 +1097,7 @@ def apply_price_list(args, as_doc=False): } def apply_price_list_on_item(args): - item_doc = frappe.get_doc("Item", args.item_code) + item_doc = frappe.db.get_value("Item", args.item_code, ['name', 'variant_of'], as_dict=1) item_details = get_price_list_rate(args, item_doc) item_details.update(get_pricing_rule_for_item(args, item_details.price_list_rate)) diff --git a/erpnext/stock/page/warehouse_capacity_summary/warehouse_capacity_summary.html b/erpnext/stock/page/warehouse_capacity_summary/warehouse_capacity_summary.html index de7e38e7d3e..adab4786403 100644 --- a/erpnext/stock/page/warehouse_capacity_summary/warehouse_capacity_summary.html +++ b/erpnext/stock/page/warehouse_capacity_summary/warehouse_capacity_summary.html @@ -1,19 +1,19 @@ {% for d in data %}
-
+ -
+ -
+
{{ d.stock_capacity }}
-
+
{{ d.actual_qty }}
-
+
-
+
{{ d.percent_occupied }}%
{% if can_write %} -
-
{% endif %}
diff --git a/erpnext/stock/page/warehouse_capacity_summary/warehouse_capacity_summary.js b/erpnext/stock/page/warehouse_capacity_summary/warehouse_capacity_summary.js index c0ffdc9d519..ea27dd251da 100644 --- a/erpnext/stock/page/warehouse_capacity_summary/warehouse_capacity_summary.js +++ b/erpnext/stock/page/warehouse_capacity_summary/warehouse_capacity_summary.js @@ -4,7 +4,7 @@ frappe.pages['warehouse-capacity-summary'].on_page_load = function(wrapper) { title: 'Warehouse Capacity Summary', single_column: true }); - page.set_secondary_action('Refresh', () => page.capacity_dashboard.refresh(), 'octicon octicon-sync'); + page.set_secondary_action('Refresh', () => page.capacity_dashboard.refresh(), 'refresh'); page.start = 0; page.company_field = page.add_field({ diff --git a/erpnext/stock/page/warehouse_capacity_summary/warehouse_capacity_summary_header.html b/erpnext/stock/page/warehouse_capacity_summary/warehouse_capacity_summary_header.html index 7ac5e640302..1183ad4496e 100644 --- a/erpnext/stock/page/warehouse_capacity_summary/warehouse_capacity_summary_header.html +++ b/erpnext/stock/page/warehouse_capacity_summary/warehouse_capacity_summary_header.html @@ -1,18 +1,18 @@
-
+
Warehouse
-
+
Item
-
+
Stock Capacity
-
+
Balance Stock Qty
-
+
% Occupied
diff --git a/erpnext/stock/report/itemwise_recommended_reorder_level/itemwise_recommended_reorder_level.py b/erpnext/stock/report/itemwise_recommended_reorder_level/itemwise_recommended_reorder_level.py index 314f1608fa1..3f490653e14 100644 --- a/erpnext/stock/report/itemwise_recommended_reorder_level/itemwise_recommended_reorder_level.py +++ b/erpnext/stock/report/itemwise_recommended_reorder_level/itemwise_recommended_reorder_level.py @@ -48,6 +48,7 @@ def get_item_info(filters): conditions = [get_item_group_condition(filters.get("item_group"))] if filters.get("brand"): conditions.append("item.brand=%(brand)s") + conditions.append("is_stock_item = 1") return frappe.db.sql("""select name, item_name, description, brand, item_group, safety_stock, lead_time_days from `tabItem` item where {}""" diff --git a/erpnext/stock/report/stock_ageing/stock_ageing.py b/erpnext/stock/report/stock_ageing/stock_ageing.py index 0ebe4f903f1..e6dfc97a998 100644 --- a/erpnext/stock/report/stock_ageing/stock_ageing.py +++ b/erpnext/stock/report/stock_ageing/stock_ageing.py @@ -3,6 +3,7 @@ from operator import itemgetter +from typing import Dict, List, Tuple, Union import frappe from frappe import _ @@ -10,19 +11,29 @@ from frappe.utils import cint, date_diff, flt from erpnext.stock.doctype.serial_no.serial_no import get_serial_nos +Filters = frappe._dict -def execute(filters=None): - columns = get_columns(filters) - item_details = get_fifo_queue(filters) +def execute(filters: Filters = None) -> Tuple: to_date = filters["to_date"] - _func = itemgetter(1) + columns = get_columns(filters) + item_details = FIFOSlots(filters).generate() + data = format_report_data(filters, item_details, to_date) + + chart_data = get_chart_data(data, filters) + + return columns, data, None, chart_data + +def format_report_data(filters: Filters, item_details: Dict, to_date: str) -> List[Dict]: + "Returns ordered, formatted data with ranges." + _func = itemgetter(1) data = [] + for item, item_dict in item_details.items(): earliest_age, latest_age = 0, 0 + details = item_dict["details"] fifo_queue = sorted(filter(_func, item_dict["fifo_queue"]), key=_func) - details = item_dict["details"] if not fifo_queue: continue @@ -31,23 +42,22 @@ def execute(filters=None): latest_age = date_diff(to_date, fifo_queue[-1][1]) range1, range2, range3, above_range3 = get_range_age(filters, fifo_queue, to_date, item_dict) - row = [details.name, details.item_name, - details.description, details.item_group, details.brand] + row = [details.name, details.item_name, details.description, + details.item_group, details.brand] if filters.get("show_warehouse_wise_stock"): row.append(details.warehouse) row.extend([item_dict.get("total_qty"), average_age, range1, range2, range3, above_range3, - earliest_age, latest_age, details.stock_uom]) + earliest_age, latest_age, + details.stock_uom]) data.append(row) - chart_data = get_chart_data(data, filters) + return data - return columns, data, None, chart_data - -def get_average_age(fifo_queue, to_date): +def get_average_age(fifo_queue: List, to_date: str) -> float: batch_age = age_qty = total_qty = 0.0 for batch in fifo_queue: batch_age = date_diff(to_date, batch[1]) @@ -61,7 +71,7 @@ def get_average_age(fifo_queue, to_date): return flt(age_qty / total_qty, 2) if total_qty else 0.0 -def get_range_age(filters, fifo_queue, to_date, item_dict): +def get_range_age(filters: Filters, fifo_queue: List, to_date: str, item_dict: Dict) -> Tuple: range1 = range2 = range3 = above_range3 = 0.0 for item in fifo_queue: @@ -79,7 +89,7 @@ def get_range_age(filters, fifo_queue, to_date, item_dict): return range1, range2, range3, above_range3 -def get_columns(filters): +def get_columns(filters: Filters) -> List[Dict]: range_columns = [] setup_ageing_columns(filters, range_columns) columns = [ @@ -164,106 +174,7 @@ def get_columns(filters): return columns -def get_fifo_queue(filters, sle=None): - item_details = {} - transferred_item_details = {} - serial_no_batch_purchase_details = {} - - if sle == None: - sle = get_stock_ledger_entries(filters) - - for d in sle: - key = (d.name, d.warehouse) if filters.get('show_warehouse_wise_stock') else d.name - item_details.setdefault(key, {"details": d, "fifo_queue": []}) - fifo_queue = item_details[key]["fifo_queue"] - - transferred_item_key = (d.voucher_no, d.name, d.warehouse) - transferred_item_details.setdefault(transferred_item_key, []) - - if d.voucher_type == "Stock Reconciliation": - d.actual_qty = flt(d.qty_after_transaction) - flt(item_details[key].get("qty_after_transaction", 0)) - - serial_no_list = get_serial_nos(d.serial_no) if d.serial_no else [] - - if d.actual_qty > 0: - if transferred_item_details.get(transferred_item_key): - batch = transferred_item_details[transferred_item_key][0] - fifo_queue.append(batch) - transferred_item_details[transferred_item_key].pop(0) - else: - if serial_no_list: - for serial_no in serial_no_list: - if serial_no_batch_purchase_details.get(serial_no): - fifo_queue.append([serial_no, serial_no_batch_purchase_details.get(serial_no)]) - else: - serial_no_batch_purchase_details.setdefault(serial_no, d.posting_date) - fifo_queue.append([serial_no, d.posting_date]) - else: - fifo_queue.append([d.actual_qty, d.posting_date]) - else: - if serial_no_list: - fifo_queue[:] = [serial_no for serial_no in fifo_queue if serial_no[0] not in serial_no_list] - else: - qty_to_pop = abs(d.actual_qty) - while qty_to_pop: - batch = fifo_queue[0] if fifo_queue else [0, None] - if 0 < flt(batch[0]) <= qty_to_pop: - # if batch qty > 0 - # not enough or exactly same qty in current batch, clear batch - qty_to_pop -= flt(batch[0]) - transferred_item_details[transferred_item_key].append(fifo_queue.pop(0)) - else: - # all from current batch - batch[0] = flt(batch[0]) - qty_to_pop - transferred_item_details[transferred_item_key].append([qty_to_pop, batch[1]]) - qty_to_pop = 0 - - item_details[key]["qty_after_transaction"] = d.qty_after_transaction - - if "total_qty" not in item_details[key]: - item_details[key]["total_qty"] = d.actual_qty - else: - item_details[key]["total_qty"] += d.actual_qty - - item_details[key]["has_serial_no"] = d.has_serial_no - - return item_details - -def get_stock_ledger_entries(filters): - return frappe.db.sql("""select - item.name, item.item_name, item_group, brand, description, item.stock_uom, item.has_serial_no, - actual_qty, posting_date, voucher_type, voucher_no, serial_no, batch_no, qty_after_transaction, warehouse - from `tabStock Ledger Entry` sle, - (select name, item_name, description, stock_uom, brand, item_group, has_serial_no - from `tabItem` {item_conditions}) item - where item_code = item.name and - company = %(company)s and - posting_date <= %(to_date)s and - is_cancelled != 1 - {sle_conditions} - order by posting_date, posting_time, sle.creation, actual_qty""" #nosec - .format(item_conditions=get_item_conditions(filters), - sle_conditions=get_sle_conditions(filters)), filters, as_dict=True) - -def get_item_conditions(filters): - conditions = [] - if filters.get("item_code"): - conditions.append("item_code=%(item_code)s") - if filters.get("brand"): - conditions.append("brand=%(brand)s") - - return "where {}".format(" and ".join(conditions)) if conditions else "" - -def get_sle_conditions(filters): - conditions = [] - if filters.get("warehouse"): - lft, rgt = frappe.db.get_value('Warehouse', filters.get("warehouse"), ['lft', 'rgt']) - conditions.append("""warehouse in (select wh.name from `tabWarehouse` wh - where wh.lft >= {0} and rgt <= {1})""".format(lft, rgt)) - - return "and {}".format(" and ".join(conditions)) if conditions else "" - -def get_chart_data(data, filters): +def get_chart_data(data: List, filters: Filters) -> Dict: if not data: return [] @@ -294,17 +205,201 @@ def get_chart_data(data, filters): "type" : "bar" } -def setup_ageing_columns(filters, range_columns): - for i, label in enumerate(["0-{range1}".format(range1=filters["range1"]), - "{range1}-{range2}".format(range1=cint(filters["range1"])+ 1, range2=filters["range2"]), - "{range2}-{range3}".format(range2=cint(filters["range2"])+ 1, range3=filters["range3"]), - "{range3}-{above}".format(range3=cint(filters["range3"])+ 1, above=_("Above"))]): - add_column(range_columns, label="Age ("+ label +")", fieldname='range' + str(i+1)) +def setup_ageing_columns(filters: Filters, range_columns: List): + ranges = [ + f"0 - {filters['range1']}", + f"{cint(filters['range1']) + 1} - {cint(filters['range2'])}", + f"{cint(filters['range2']) + 1} - {cint(filters['range3'])}", + f"{cint(filters['range3']) + 1} - {_('Above')}" + ] + for i, label in enumerate(ranges): + fieldname = 'range' + str(i+1) + add_column(range_columns, label=f"Age ({label})",fieldname=fieldname) -def add_column(range_columns, label, fieldname, fieldtype='Float', width=140): +def add_column(range_columns: List, label:str, fieldname: str, fieldtype: str = 'Float', width: int = 140): range_columns.append(dict( label=label, fieldname=fieldname, fieldtype=fieldtype, width=width )) + + +class FIFOSlots: + "Returns FIFO computed slots of inwarded stock as per date." + + def __init__(self, filters: Dict = None , sle: List = None): + self.item_details = {} + self.transferred_item_details = {} + self.serial_no_batch_purchase_details = {} + self.filters = filters + self.sle = sle + + def generate(self) -> Dict: + """ + Returns dict of the foll.g structure: + Key = Item A / (Item A, Warehouse A) + Key: { + 'details' -> Dict: ** item details **, + 'fifo_queue' -> List: ** list of lists containing entries/slots for existing stock, + consumed/updated and maintained via FIFO. ** + } + """ + if self.sle is None: + self.sle = self.__get_stock_ledger_entries() + + for d in self.sle: + key, fifo_queue, transferred_item_key = self.__init_key_stores(d) + + if d.voucher_type == "Stock Reconciliation": + prev_balance_qty = self.item_details[key].get("qty_after_transaction", 0) + d.actual_qty = flt(d.qty_after_transaction) - flt(prev_balance_qty) + + serial_nos = get_serial_nos(d.serial_no) if d.serial_no else [] + + if d.actual_qty > 0: + self.__compute_incoming_stock(d, fifo_queue, transferred_item_key, serial_nos) + else: + self.__compute_outgoing_stock(d, fifo_queue, transferred_item_key, serial_nos) + + self.__update_balances(d, key) + + return self.item_details + + def __init_key_stores(self, row: Dict) -> Tuple: + "Initialise keys and FIFO Queue." + + key = (row.name, row.warehouse) if self.filters.get('show_warehouse_wise_stock') else row.name + self.item_details.setdefault(key, {"details": row, "fifo_queue": []}) + fifo_queue = self.item_details[key]["fifo_queue"] + + transferred_item_key = (row.voucher_no, row.name, row.warehouse) + self.transferred_item_details.setdefault(transferred_item_key, []) + + return key, fifo_queue, transferred_item_key + + def __compute_incoming_stock(self, row: Dict, fifo_queue: List, transfer_key: Tuple, serial_nos: List): + "Update FIFO Queue on inward stock." + + if self.transferred_item_details.get(transfer_key): + # inward/outward from same voucher, item & warehouse + slot = self.transferred_item_details[transfer_key].pop(0) + fifo_queue.append(slot) + else: + if not serial_nos: + if fifo_queue and flt(fifo_queue[0][0]) < 0: + # neutralize negative stock by adding positive stock + fifo_queue[0][0] += flt(row.actual_qty) + fifo_queue[0][1] = row.posting_date + else: + fifo_queue.append([flt(row.actual_qty), row.posting_date]) + return + + for serial_no in serial_nos: + if self.serial_no_batch_purchase_details.get(serial_no): + fifo_queue.append([serial_no, self.serial_no_batch_purchase_details.get(serial_no)]) + else: + self.serial_no_batch_purchase_details.setdefault(serial_no, row.posting_date) + fifo_queue.append([serial_no, row.posting_date]) + + def __compute_outgoing_stock(self, row: Dict, fifo_queue: List, transfer_key: Tuple, serial_nos: List): + "Update FIFO Queue on outward stock." + if serial_nos: + fifo_queue[:] = [serial_no for serial_no in fifo_queue if serial_no[0] not in serial_nos] + return + + qty_to_pop = abs(row.actual_qty) + while qty_to_pop: + slot = fifo_queue[0] if fifo_queue else [0, None] + if 0 < flt(slot[0]) <= qty_to_pop: + # qty to pop >= slot qty + # if +ve and not enough or exactly same balance in current slot, consume whole slot + qty_to_pop -= flt(slot[0]) + self.transferred_item_details[transfer_key].append(fifo_queue.pop(0)) + elif not fifo_queue: + # negative stock, no balance but qty yet to consume + fifo_queue.append([-(qty_to_pop), row.posting_date]) + self.transferred_item_details[transfer_key].append([row.actual_qty, row.posting_date]) + qty_to_pop = 0 + else: + # qty to pop < slot qty, ample balance + # consume actual_qty from first slot + slot[0] = flt(slot[0]) - qty_to_pop + self.transferred_item_details[transfer_key].append([qty_to_pop, slot[1]]) + qty_to_pop = 0 + + def __update_balances(self, row: Dict, key: Union[Tuple, str]): + self.item_details[key]["qty_after_transaction"] = row.qty_after_transaction + + if "total_qty" not in self.item_details[key]: + self.item_details[key]["total_qty"] = row.actual_qty + else: + self.item_details[key]["total_qty"] += row.actual_qty + + self.item_details[key]["has_serial_no"] = row.has_serial_no + + def __get_stock_ledger_entries(self) -> List[Dict]: + sle = frappe.qb.DocType("Stock Ledger Entry") + item = self.__get_item_query() # used as derived table in sle query + + sle_query = ( + frappe.qb.from_(sle).from_(item) + .select( + item.name, item.item_name, item.item_group, + item.brand, item.description, + item.stock_uom, item.has_serial_no, + sle.actual_qty, sle.posting_date, + sle.voucher_type, sle.voucher_no, + sle.serial_no, sle.batch_no, + sle.qty_after_transaction, sle.warehouse + ).where( + (sle.item_code == item.name) + & (sle.company == self.filters.get("company")) + & (sle.posting_date <= self.filters.get("to_date")) + & (sle.is_cancelled != 1) + ) + ) + + if self.filters.get("warehouse"): + sle_query = self.__get_warehouse_conditions(sle, sle_query) + + sle_query = sle_query.orderby( + sle.posting_date, sle.posting_time, sle.creation, sle.actual_qty + ) + + return sle_query.run(as_dict=True) + + def __get_item_query(self) -> str: + item_table = frappe.qb.DocType("Item") + + item = frappe.qb.from_("Item").select( + "name", "item_name", "description", "stock_uom", + "brand", "item_group", "has_serial_no" + ) + + if self.filters.get("item_code"): + item = item.where(item_table.item_code == self.filters.get("item_code")) + + if self.filters.get("brand"): + item = item.where(item_table.brand == self.filters.get("brand")) + + return item + + def __get_warehouse_conditions(self, sle, sle_query) -> str: + warehouse = frappe.qb.DocType("Warehouse") + lft, rgt = frappe.db.get_value( + "Warehouse", + self.filters.get("warehouse"), + ['lft', 'rgt'] + ) + + warehouse_results = ( + frappe.qb.from_(warehouse) + .select("name").where( + (warehouse.lft >= lft) + & (warehouse.rgt <= rgt) + ).run() + ) + warehouse_results = [x[0] for x in warehouse_results] + + return sle_query.where(sle.warehouse.isin(warehouse_results)) diff --git a/erpnext/stock/report/stock_ageing/stock_ageing_fifo_logic.md b/erpnext/stock/report/stock_ageing/stock_ageing_fifo_logic.md new file mode 100644 index 00000000000..5ffe97fd742 --- /dev/null +++ b/erpnext/stock/report/stock_ageing/stock_ageing_fifo_logic.md @@ -0,0 +1,73 @@ +### Concept of FIFO Slots + +Since we need to know age-wise remaining stock, we maintain all the inward entries as slots. So each time stock comes in, a slot is added for the same. + +Eg. For Item A: +---------------------- +Date | Qty | Queue +---------------------- +1st | +50 | [[50, 1-12-2021]] +2nd | +20 | [[50, 1-12-2021], [20, 2-12-2021]] +---------------------- + +Now the queue can tell us the total stock and also how old the stock is. +Here, the balance qty is 70. +50 qty is (today-the 1st) days old +20 qty is (today-the 2nd) days old + +### Calculation of FIFO Slots + +#### Case 1: Outward from sufficient balance qty +---------------------- +Date | Qty | Queue +---------------------- +1st | +50 | [[50, 1-12-2021]] +2nd | -20 | [[30, 1-12-2021]] +2nd | +20 | [[30, 1-12-2021], [20, 2-12-2021]] + +Here after the first entry, while issuing 20 qty: +- **since 20 is lesser than the balance**, **qty_to_pop (20)** is simply consumed from first slot (FIFO consumption) +- Any inward entry after as usual will get its own slot added to the queue + +#### Case 2: Outward from sufficient cumulative (slots) balance qty +---------------------- +Date | Qty | Queue +---------------------- +1st | +50 | [[50, 1-12-2021]] +2nd | +20 | [[50, 1-12-2021], [20, 2-12-2021]] +2nd | -60 | [[10, 2-12-2021]] + +- Consumption happens slot wise. First slot 1 is consumed +- Since **qty_to_pop (60) is greater than slot 1 qty (50)**, the entire slot is consumed and popped +- Now the queue is [[20, 2-12-2021]], and **qty_to_pop=10** (remaining qty to pop) +- It then goes ahead to the next slot and consumes 10 from it +- Now the queue is [[10, 2-12-2021]] + +#### Case 3: Outward from insufficient balance qty +> This case is possible only if **Allow Negative Stock** was enabled at some point/is enabled. + +---------------------- +Date | Qty | Queue +---------------------- +1st | +50 | [[50, 1-12-2021]] +2nd | -60 | [[-10, 1-12-2021]] + +- Since **qty_to_pop (60)** is more than the balance in slot 1, the entire slot is consumed and popped +- Now the queue is **empty**, and **qty_to_pop=10** (remaining qty to pop) +- Since we still have more to consume, we append the balance since 60 is issued from 50 i.e. -10. +- We register this negative value, since the stock issue has caused the balance to become negative + +Now when stock is inwarded: +- Instead of adding a slot we check if there are any negative balances. +- If yes, we keep adding positive stock to it until we make the balance positive. +- Once the balance is positive, the next inward entry will add a new slot in the queue + +Eg: +---------------------- +Date | Qty | Queue +---------------------- +1st | +50 | [[50, 1-12-2021]] +2nd | -60 | [[-10, 1-12-2021]] +3rd | +5 | [[-5, 3-12-2021]] +4th | +10 | [[5, 4-12-2021]] +4th | +20 | [[5, 4-12-2021], [20, 4-12-2021]] \ No newline at end of file diff --git a/erpnext/stock/report/stock_ageing/test_stock_ageing.py b/erpnext/stock/report/stock_ageing/test_stock_ageing.py new file mode 100644 index 00000000000..949bb7c15a8 --- /dev/null +++ b/erpnext/stock/report/stock_ageing/test_stock_ageing.py @@ -0,0 +1,126 @@ +# Copyright (c) 2021, Frappe Technologies Pvt. Ltd. and Contributors +# See license.txt + +import frappe + +from erpnext.stock.report.stock_ageing.stock_ageing import FIFOSlots +from erpnext.tests.utils import ERPNextTestCase + + +class TestStockAgeing(ERPNextTestCase): + def setUp(self) -> None: + self.filters = frappe._dict( + company="_Test Company", + to_date="2021-12-10" + ) + + def test_normal_inward_outward_queue(self): + "Reference: Case 1 in stock_ageing_fifo_logic.md" + sle = [ + frappe._dict( + name="Flask Item", + actual_qty=30, qty_after_transaction=30, + posting_date="2021-12-01", voucher_type="Stock Entry", + voucher_no="001", + has_serial_no=False, serial_no=None + ), + frappe._dict( + name="Flask Item", + actual_qty=20, qty_after_transaction=50, + posting_date="2021-12-02", voucher_type="Stock Entry", + voucher_no="002", + has_serial_no=False, serial_no=None + ), + frappe._dict( + name="Flask Item", + actual_qty=(-10), qty_after_transaction=40, + posting_date="2021-12-03", voucher_type="Stock Entry", + voucher_no="003", + has_serial_no=False, serial_no=None + ) + ] + + slots = FIFOSlots(self.filters, sle).generate() + + self.assertTrue(slots["Flask Item"]["fifo_queue"]) + result = slots["Flask Item"] + queue = result["fifo_queue"] + + self.assertEqual(result["qty_after_transaction"], result["total_qty"]) + self.assertEqual(queue[0][0], 20.0) + + def test_insufficient_balance(self): + "Reference: Case 3 in stock_ageing_fifo_logic.md" + sle = [ + frappe._dict( + name="Flask Item", + actual_qty=(-30), qty_after_transaction=(-30), + posting_date="2021-12-01", voucher_type="Stock Entry", + voucher_no="001", + has_serial_no=False, serial_no=None + ), + frappe._dict( + name="Flask Item", + actual_qty=20, qty_after_transaction=(-10), + posting_date="2021-12-02", voucher_type="Stock Entry", + voucher_no="002", + has_serial_no=False, serial_no=None + ), + frappe._dict( + name="Flask Item", + actual_qty=20, qty_after_transaction=10, + posting_date="2021-12-03", voucher_type="Stock Entry", + voucher_no="003", + has_serial_no=False, serial_no=None + ), + frappe._dict( + name="Flask Item", + actual_qty=10, qty_after_transaction=20, + posting_date="2021-12-03", voucher_type="Stock Entry", + voucher_no="004", + has_serial_no=False, serial_no=None + ) + ] + + slots = FIFOSlots(self.filters, sle).generate() + + result = slots["Flask Item"] + queue = result["fifo_queue"] + + self.assertEqual(result["qty_after_transaction"], result["total_qty"]) + self.assertEqual(queue[0][0], 10.0) + self.assertEqual(queue[1][0], 10.0) + + def test_stock_reconciliation(self): + sle = [ + frappe._dict( + name="Flask Item", + actual_qty=30, qty_after_transaction=30, + posting_date="2021-12-01", voucher_type="Stock Entry", + voucher_no="001", + has_serial_no=False, serial_no=None + ), + frappe._dict( + name="Flask Item", + actual_qty=0, qty_after_transaction=50, + posting_date="2021-12-02", voucher_type="Stock Reconciliation", + voucher_no="002", + has_serial_no=False, serial_no=None + ), + frappe._dict( + name="Flask Item", + actual_qty=(-10), qty_after_transaction=40, + posting_date="2021-12-03", voucher_type="Stock Entry", + voucher_no="003", + has_serial_no=False, serial_no=None + ) + ] + + slots = FIFOSlots(self.filters, sle).generate() + + result = slots["Flask Item"] + queue = result["fifo_queue"] + + self.assertEqual(result["qty_after_transaction"], result["total_qty"]) + self.assertEqual(queue[0][0], 20.0) + self.assertEqual(queue[1][0], 20.0) diff --git a/erpnext/stock/report/stock_balance/stock_balance.py b/erpnext/stock/report/stock_balance/stock_balance.py index c0b89fdd09a..b4f43a7fef1 100644 --- a/erpnext/stock/report/stock_balance/stock_balance.py +++ b/erpnext/stock/report/stock_balance/stock_balance.py @@ -9,7 +9,7 @@ from frappe import _ from frappe.utils import cint, date_diff, flt, getdate import erpnext -from erpnext.stock.report.stock_ageing.stock_ageing import get_average_age, get_fifo_queue +from erpnext.stock.report.stock_ageing.stock_ageing import FIFOSlots, get_average_age from erpnext.stock.report.stock_ledger.stock_ledger import get_item_group_condition from erpnext.stock.utils import add_additional_uom_columns, is_reposting_item_valuation_in_progress @@ -33,7 +33,7 @@ def execute(filters=None): if filters.get('show_stock_ageing_data'): filters['show_warehouse_wise_stock'] = True - item_wise_fifo_queue = get_fifo_queue(filters, sle) + item_wise_fifo_queue = FIFOSlots(filters, sle).generate() # if no stock ledger entry found return if not sle: @@ -167,7 +167,7 @@ def get_stock_ledger_entries(filters, items): sle.company, sle.voucher_type, sle.qty_after_transaction, sle.stock_value_difference, sle.item_code as name, sle.voucher_no, sle.stock_value, sle.batch_no from - `tabStock Ledger Entry` sle force index (posting_sort_index) + `tabStock Ledger Entry` sle where sle.docstatus < 2 %s %s and is_cancelled = 0 order by sle.posting_date, sle.posting_time, sle.creation, sle.actual_qty""" % #nosec diff --git a/erpnext/stock/report/stock_ledger_invariant_check/__init__.py b/erpnext/stock/report/stock_ledger_invariant_check/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/erpnext/stock/report/stock_ledger_invariant_check/stock_ledger_invariant_check.js b/erpnext/stock/report/stock_ledger_invariant_check/stock_ledger_invariant_check.js new file mode 100644 index 00000000000..31f389f236e --- /dev/null +++ b/erpnext/stock/report/stock_ledger_invariant_check/stock_ledger_invariant_check.js @@ -0,0 +1,44 @@ +// Copyright (c) 2016, Frappe Technologies Pvt. Ltd. and contributors +// For license information, please see license.txt +/* eslint-disable */ + +const DIFFERNCE_FIELD_NAMES = [ + "difference_in_qty", + "fifo_qty_diff", + "fifo_value_diff", + "fifo_valuation_diff", + "valuation_diff", + "fifo_difference_diff", + "diff_value_diff" +]; + +frappe.query_reports["Stock Ledger Invariant Check"] = { + "filters": [ + { + "fieldname": "item_code", + "fieldtype": "Link", + "label": "Item", + "mandatory": 1, + "options": "Item", + get_query: function() { + return { + filters: {is_stock_item: 1, has_serial_no: 0} + } + } + }, + { + "fieldname": "warehouse", + "fieldtype": "Link", + "label": "Warehouse", + "mandatory": 1, + "options": "Warehouse", + } + ], + formatter (value, row, column, data, default_formatter) { + value = default_formatter(value, row, column, data); + if (DIFFERNCE_FIELD_NAMES.includes(column.fieldname) && Math.abs(data[column.fieldname]) > 0.001) { + value = "" + value + ""; + } + return value; + }, +}; diff --git a/erpnext/stock/report/stock_ledger_invariant_check/stock_ledger_invariant_check.json b/erpnext/stock/report/stock_ledger_invariant_check/stock_ledger_invariant_check.json new file mode 100644 index 00000000000..d28fe0f62d1 --- /dev/null +++ b/erpnext/stock/report/stock_ledger_invariant_check/stock_ledger_invariant_check.json @@ -0,0 +1,26 @@ +{ + "add_total_row": 0, + "columns": [], + "creation": "2021-12-16 06:31:23.290916", + "disable_prepared_report": 0, + "disabled": 0, + "docstatus": 0, + "doctype": "Report", + "filters": [], + "idx": 0, + "is_standard": "Yes", + "modified": "2021-12-16 09:55:58.341764", + "modified_by": "Administrator", + "module": "Stock", + "name": "Stock Ledger Invariant Check", + "owner": "Administrator", + "prepared_report": 0, + "ref_doctype": "Stock Ledger Entry", + "report_name": "Stock Ledger Invariant Check", + "report_type": "Script Report", + "roles": [ + { + "role": "System Manager" + } + ] +} \ No newline at end of file diff --git a/erpnext/stock/report/stock_ledger_invariant_check/stock_ledger_invariant_check.py b/erpnext/stock/report/stock_ledger_invariant_check/stock_ledger_invariant_check.py new file mode 100644 index 00000000000..48753b0edd4 --- /dev/null +++ b/erpnext/stock/report/stock_ledger_invariant_check/stock_ledger_invariant_check.py @@ -0,0 +1,249 @@ +# Copyright (c) 2021, Frappe Technologies Pvt. Ltd. and contributors +# License: GNU GPL v3. See LICENSE + +import json + +import frappe + +SLE_FIELDS = ( + "name", + "posting_date", + "posting_time", + "creation", + "voucher_type", + "voucher_no", + "actual_qty", + "qty_after_transaction", + "incoming_rate", + "outgoing_rate", + "stock_queue", + "batch_no", + "stock_value", + "stock_value_difference", + "valuation_rate", +) + + +def execute(filters=None): + columns = get_columns() + data = get_data(filters) + return columns, data + + +def get_data(filters): + sles = get_stock_ledger_entries(filters) + return add_invariant_check_fields(sles) + + +def get_stock_ledger_entries(filters): + return frappe.get_all( + "Stock Ledger Entry", + fields=SLE_FIELDS, + filters={ + "item_code": filters.item_code, + "warehouse": filters.warehouse, + "is_cancelled": 0 + }, + order_by="timestamp(posting_date, posting_time), creation", + ) + + +def add_invariant_check_fields(sles): + balance_qty = 0.0 + balance_stock_value = 0.0 + for idx, sle in enumerate(sles): + queue = json.loads(sle.stock_queue) + + fifo_qty = 0.0 + fifo_value = 0.0 + for qty, rate in queue: + fifo_qty += qty + fifo_value += qty * rate + + balance_qty += sle.actual_qty + balance_stock_value += sle.stock_value_difference + if sle.voucher_type == "Stock Reconciliation" and not sle.batch_no: + balance_qty = sle.qty_after_transaction + + sle.fifo_queue_qty = fifo_qty + sle.fifo_stock_value = fifo_value + sle.fifo_valuation_rate = fifo_value / fifo_qty if fifo_qty else None + sle.balance_value_by_qty = ( + sle.stock_value / sle.qty_after_transaction if sle.qty_after_transaction else None + ) + sle.expected_qty_after_transaction = balance_qty + sle.stock_value_from_diff = balance_stock_value + + # set difference fields + sle.difference_in_qty = sle.qty_after_transaction - sle.expected_qty_after_transaction + sle.fifo_qty_diff = sle.qty_after_transaction - fifo_qty + sle.fifo_value_diff = sle.stock_value - fifo_value + sle.fifo_valuation_diff = ( + sle.valuation_rate - sle.fifo_valuation_rate if sle.fifo_valuation_rate else None + ) + sle.valuation_diff = ( + sle.valuation_rate - sle.balance_value_by_qty if sle.balance_value_by_qty else None + ) + sle.diff_value_diff = sle.stock_value_from_diff - sle.stock_value + + if idx > 0: + sle.fifo_stock_diff = sle.fifo_stock_value - sles[idx - 1].fifo_stock_value + sle.fifo_difference_diff = sle.fifo_stock_diff - sle.stock_value_difference + + return sles + + +def get_columns(): + return [ + { + "fieldname": "name", + "fieldtype": "Link", + "label": "Stock Ledger Entry", + "options": "Stock Ledger Entry", + }, + { + "fieldname": "posting_date", + "fieldtype": "Date", + "label": "Posting Date", + }, + { + "fieldname": "posting_time", + "fieldtype": "Time", + "label": "Posting Time", + }, + { + "fieldname": "creation", + "fieldtype": "Datetime", + "label": "Creation", + }, + { + "fieldname": "voucher_type", + "fieldtype": "Link", + "label": "Voucher Type", + "options": "DocType", + }, + { + "fieldname": "voucher_no", + "fieldtype": "Dynamic Link", + "label": "Voucher No", + "options": "voucher_type", + }, + { + "fieldname": "batch_no", + "fieldtype": "Link", + "label": "Batch", + "options": "Batch", + }, + { + "fieldname": "actual_qty", + "fieldtype": "Float", + "label": "Qty Change", + }, + { + "fieldname": "incoming_rate", + "fieldtype": "Float", + "label": "Incoming Rate", + }, + { + "fieldname": "outgoing_rate", + "fieldtype": "Float", + "label": "Outgoing Rate", + }, + { + "fieldname": "qty_after_transaction", + "fieldtype": "Float", + "label": "(A) Qty After Transaction", + }, + { + "fieldname": "expected_qty_after_transaction", + "fieldtype": "Float", + "label": "(B) Expected Qty After Transaction", + }, + { + "fieldname": "difference_in_qty", + "fieldtype": "Float", + "label": "A - B", + }, + { + "fieldname": "stock_queue", + "fieldtype": "Data", + "label": "FIFO Queue", + }, + + { + "fieldname": "fifo_queue_qty", + "fieldtype": "Float", + "label": "(C) Total qty in queue", + }, + { + "fieldname": "fifo_qty_diff", + "fieldtype": "Float", + "label": "A - C", + }, + { + "fieldname": "stock_value", + "fieldtype": "Float", + "label": "(D) Balance Stock Value", + }, + { + "fieldname": "fifo_stock_value", + "fieldtype": "Float", + "label": "(E) Balance Stock Value in Queue", + }, + { + "fieldname": "fifo_value_diff", + "fieldtype": "Float", + "label": "D - E", + }, + { + "fieldname": "stock_value_difference", + "fieldtype": "Float", + "label": "(F) Stock Value Difference", + }, + { + "fieldname": "stock_value_from_diff", + "fieldtype": "Float", + "label": "Balance Stock Value using (F)", + }, + { + "fieldname": "diff_value_diff", + "fieldtype": "Float", + "label": "K - D", + }, + { + "fieldname": "fifo_stock_diff", + "fieldtype": "Float", + "label": "(G) Stock Value difference (FIFO queue)", + }, + { + "fieldname": "fifo_difference_diff", + "fieldtype": "Float", + "label": "F - G", + }, + { + "fieldname": "valuation_rate", + "fieldtype": "Float", + "label": "(H) Valuation Rate", + }, + { + "fieldname": "fifo_valuation_rate", + "fieldtype": "Float", + "label": "(I) Valuation Rate as per FIFO", + }, + + { + "fieldname": "fifo_valuation_diff", + "fieldtype": "Float", + "label": "H - I", + }, + { + "fieldname": "balance_value_by_qty", + "fieldtype": "Float", + "label": "(J) Valuation = Value (D) ÷ Qty (A)", + }, + { + "fieldname": "valuation_diff", + "fieldtype": "Float", + "label": "H - J", + }, + ] diff --git a/erpnext/stock/report/test_reports.py b/erpnext/stock/report/test_reports.py index d7fb5b2bf3f..1dcf863a9d0 100644 --- a/erpnext/stock/report/test_reports.py +++ b/erpnext/stock/report/test_reports.py @@ -41,6 +41,12 @@ REPORT_FILTER_TEST_CASES: List[Tuple[ReportName, ReportFilters]] = [ ("Total Stock Summary", {"group_by": "warehouse",}), ("Batch Item Expiry Status", {}), ("Stock Ageing", {"range1": 30, "range2": 60, "range3": 90, "_optional": True}), + ("Stock Ledger Invariant Check", + { + "warehouse": "_Test Warehouse - _TC", + "item": "_Test Item" + } + ), ] OPTIONAL_FILTERS = { diff --git a/erpnext/stock/report/total_stock_summary/total_stock_summary.js b/erpnext/stock/report/total_stock_summary/total_stock_summary.js index 90648f1b249..88054aaea73 100644 --- a/erpnext/stock/report/total_stock_summary/total_stock_summary.js +++ b/erpnext/stock/report/total_stock_summary/total_stock_summary.js @@ -10,23 +10,8 @@ frappe.query_reports["Total Stock Summary"] = { "fieldtype": "Select", "width": "80", "reqd": 1, - "options": ["", "Warehouse", "Company"], - "change": function() { - let group_by = frappe.query_report.get_filter_value("group_by") - let company_filter = frappe.query_report.get_filter("company") - if (group_by == "Company") { - company_filter.df.reqd = 0; - company_filter.df.hidden = 1; - frappe.query_report.set_filter_value("company", ""); - company_filter.refresh(); - } - else { - company_filter.df.reqd = 1; - company_filter.df.hidden = 0; - company_filter.refresh(); - frappe.query_report.refresh(); - } - } + "options": ["Warehouse", "Company"], + "default": "Warehouse", }, { "fieldname": "company", @@ -34,8 +19,9 @@ frappe.query_reports["Total Stock Summary"] = { "fieldtype": "Link", "width": "80", "options": "Company", + "reqd": 1, "default": frappe.defaults.get_user_default("Company"), - "reqd": 1 + "depends_on": "eval: doc.group_by != 'Company'", }, ] } diff --git a/erpnext/stock/report/total_stock_summary/total_stock_summary.py b/erpnext/stock/report/total_stock_summary/total_stock_summary.py index 7e47b50b856..6f27558b887 100644 --- a/erpnext/stock/report/total_stock_summary/total_stock_summary.py +++ b/erpnext/stock/report/total_stock_summary/total_stock_summary.py @@ -7,8 +7,9 @@ from frappe import _ def execute(filters=None): - if not filters: filters = {} - validate_filters(filters) + + if not filters: + filters = {} columns = get_columns() stock = get_total_stock(filters) @@ -53,9 +54,3 @@ def get_total_stock(filters): ON warehouse.name = ledger.warehouse WHERE ledger.actual_qty != 0 %s""" % (columns, conditions)) - -def validate_filters(filters): - if filters.get("group_by") == 'Company' and \ - filters.get("company"): - - frappe.throw(_("Please set Company filter blank if Group By is 'Company'")) diff --git a/erpnext/stock/report/warehouse_wise_item_balance_age_and_value/warehouse_wise_item_balance_age_and_value.py b/erpnext/stock/report/warehouse_wise_item_balance_age_and_value/warehouse_wise_item_balance_age_and_value.py index d3af5f61e33..22bdb891988 100644 --- a/erpnext/stock/report/warehouse_wise_item_balance_age_and_value/warehouse_wise_item_balance_age_and_value.py +++ b/erpnext/stock/report/warehouse_wise_item_balance_age_and_value/warehouse_wise_item_balance_age_and_value.py @@ -9,7 +9,7 @@ import frappe from frappe import _ from frappe.utils import flt -from erpnext.stock.report.stock_ageing.stock_ageing import get_average_age, get_fifo_queue +from erpnext.stock.report.stock_ageing.stock_ageing import FIFOSlots, get_average_age from erpnext.stock.report.stock_balance.stock_balance import ( get_item_details, get_item_warehouse_map, @@ -33,7 +33,7 @@ def execute(filters=None): item_map = get_item_details(items, sle, filters) iwb_map = get_item_warehouse_map(filters, sle) warehouse_list = get_warehouse_list(filters) - item_ageing = get_fifo_queue(filters) + item_ageing = FIFOSlots(filters).generate() data = [] item_balance = {} item_value = {} @@ -46,8 +46,8 @@ def execute(filters=None): item_balance.setdefault((item, item_map[item]["item_group"]), []) total_stock_value = 0.00 for wh in warehouse_list: - row += [qty_dict.bal_qty] if wh.name in warehouse else [0.00] - total_stock_value += qty_dict.bal_val if wh.name in warehouse else 0.00 + row += [qty_dict.bal_qty] if wh.name == warehouse else [0.00] + total_stock_value += qty_dict.bal_val if wh.name == warehouse else 0.00 item_balance[(item, item_map[item]["item_group"])].append(row) item_value.setdefault((item, item_map[item]["item_group"]),[]) diff --git a/erpnext/stock/stock_ledger.py b/erpnext/stock/stock_ledger.py index 9c4c6761927..107bb23222e 100644 --- a/erpnext/stock/stock_ledger.py +++ b/erpnext/stock/stock_ledger.py @@ -7,29 +7,27 @@ import json import frappe from frappe import _ from frappe.model.meta import get_field_precision -from frappe.utils import cint, cstr, flt, get_link_to_form, getdate, now +from frappe.utils import cint, cstr, flt, get_link_to_form, getdate, now, nowdate import erpnext +from erpnext.stock.doctype.bin.bin import update_qty as update_bin_qty from erpnext.stock.utils import ( get_incoming_outgoing_rate_for_cancel, get_or_make_bin, get_valuation_method, ) +from erpnext.stock.valuation import FIFOValuation -# future reposting class NegativeStockError(frappe.ValidationError): pass class SerialNoExistsInFutureTransaction(frappe.ValidationError): pass _exceptions = frappe.local('stockledger_exceptions') -# _exceptions = [] def make_sl_entries(sl_entries, allow_negative_stock=False, via_landed_cost_voucher=False): from erpnext.controllers.stock_controller import future_sle_exists if sl_entries: - from erpnext.stock.utils import update_bin - cancel = sl_entries[0].get("is_cancelled") if cancel: validate_cancellation(sl_entries) @@ -64,7 +62,38 @@ def make_sl_entries(sl_entries, allow_negative_stock=False, via_landed_cost_vouc # preserve previous_qty_after_transaction for qty reposting args.previous_qty_after_transaction = sle.get("previous_qty_after_transaction") - update_bin(args, allow_negative_stock, via_landed_cost_voucher) + is_stock_item = frappe.get_cached_value('Item', args.get("item_code"), 'is_stock_item') + if is_stock_item: + bin_name = get_or_make_bin(args.get("item_code"), args.get("warehouse")) + repost_current_voucher(args, allow_negative_stock, via_landed_cost_voucher) + update_bin_qty(bin_name, args) + else: + frappe.msgprint(_("Item {0} ignored since it is not a stock item").format(args.get("item_code"))) + +def repost_current_voucher(args, allow_negative_stock=False, via_landed_cost_voucher=False): + if args.get("actual_qty") or args.get("voucher_type") == "Stock Reconciliation": + if not args.get("posting_date"): + args["posting_date"] = nowdate() + + if args.get("is_cancelled") and via_landed_cost_voucher: + return + + # Reposts only current voucher SL Entries + # Updates valuation rate, stock value, stock queue for current transaction + update_entries_after({ + "item_code": args.get('item_code'), + "warehouse": args.get('warehouse'), + "posting_date": args.get("posting_date"), + "posting_time": args.get("posting_time"), + "voucher_type": args.get("voucher_type"), + "voucher_no": args.get("voucher_no"), + "sle_id": args.get('name'), + "creation": args.get('creation') + }, allow_negative_stock=allow_negative_stock, via_landed_cost_voucher=via_landed_cost_voucher) + + # update qty in future sle and Validate negative qty + update_qty_in_future_sle(args, allow_negative_stock) + def get_args_for_future_sle(row): return frappe._dict({ @@ -111,6 +140,7 @@ def validate_cancellation(args): frappe.throw(_("Cannot cancel the transaction. Reposting of item valuation on submission is not completed yet.")) if repost_entry.status == 'Queued': doc = frappe.get_doc("Repost Item Valuation", repost_entry.name) + doc.flags.ignore_permissions = True doc.cancel() doc.delete() @@ -427,9 +457,8 @@ class update_entries_after(object): self.wh_data.qty_after_transaction += flt(sle.actual_qty) self.wh_data.stock_value = flt(self.wh_data.qty_after_transaction) * flt(self.wh_data.valuation_rate) else: - self.get_fifo_values(sle) + self.update_fifo_values(sle) self.wh_data.qty_after_transaction += flt(sle.actual_qty) - self.wh_data.stock_value = sum(flt(batch[0]) * flt(batch[1]) for batch in self.wh_data.stock_queue) # rounding as per precision self.wh_data.stock_value = flt(self.wh_data.stock_value, self.precision) @@ -667,87 +696,39 @@ class update_entries_after(object): sle.voucher_type, sle.voucher_no, self.allow_zero_rate, currency=erpnext.get_company_currency(sle.company), company=sle.company) - def get_fifo_values(self, sle): + def update_fifo_values(self, sle): incoming_rate = flt(sle.incoming_rate) actual_qty = flt(sle.actual_qty) outgoing_rate = flt(sle.outgoing_rate) + fifo_queue = FIFOValuation(self.wh_data.stock_queue) if actual_qty > 0: - if not self.wh_data.stock_queue: - self.wh_data.stock_queue.append([0, 0]) - - # last row has the same rate, just updated the qty - if self.wh_data.stock_queue[-1][1]==incoming_rate: - self.wh_data.stock_queue[-1][0] += actual_qty - else: - # Item has a positive balance qty, add new entry - if self.wh_data.stock_queue[-1][0] > 0: - self.wh_data.stock_queue.append([actual_qty, incoming_rate]) - else: # negative balance qty - qty = self.wh_data.stock_queue[-1][0] + actual_qty - if qty > 0: # new balance qty is positive - self.wh_data.stock_queue[-1] = [qty, incoming_rate] - else: # new balance qty is still negative, maintain same rate - self.wh_data.stock_queue[-1][0] = qty + fifo_queue.add_stock(qty=actual_qty, rate=incoming_rate) else: - qty_to_pop = abs(actual_qty) - while qty_to_pop: - if not self.wh_data.stock_queue: - # Get valuation rate from last sle if exists or from valuation rate field in item master - allow_zero_valuation_rate = self.check_if_allow_zero_valuation_rate(sle.voucher_type, sle.voucher_detail_no) - if not allow_zero_valuation_rate: - _rate = get_valuation_rate(sle.item_code, sle.warehouse, - sle.voucher_type, sle.voucher_no, self.allow_zero_rate, - currency=erpnext.get_company_currency(sle.company), company=sle.company) - else: - _rate = 0 - - self.wh_data.stock_queue.append([0, _rate]) - - index = None - if outgoing_rate > 0: - # Find the entry where rate matched with outgoing rate - for i, v in enumerate(self.wh_data.stock_queue): - if v[1] == outgoing_rate: - index = i - break - - # If no entry found with outgoing rate, collapse stack - 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 + def rate_generator() -> float: + allow_zero_valuation_rate = self.check_if_allow_zero_valuation_rate(sle.voucher_type, sle.voucher_detail_no) + if not allow_zero_valuation_rate: + return get_valuation_rate(sle.item_code, sle.warehouse, + sle.voucher_type, sle.voucher_no, self.allow_zero_rate, + currency=erpnext.get_company_currency(sle.company), company=sle.company) else: - index = 0 + return 0.0 - # select first batch or the batch with same rate - batch = self.wh_data.stock_queue[index] - if qty_to_pop >= batch[0]: - # consume current batch - qty_to_pop = _round_off_if_near_zero(qty_to_pop - batch[0]) - self.wh_data.stock_queue.pop(index) - if not self.wh_data.stock_queue and qty_to_pop: - # stock finished, qty still remains to be withdrawn - # negative stock, keep in as a negative batch - self.wh_data.stock_queue.append([-qty_to_pop, outgoing_rate or batch[1]]) - break + fifo_queue.remove_stock(qty=abs(actual_qty), outgoing_rate=outgoing_rate, rate_generator=rate_generator) - else: - # qty found in current batch - # consume it and exit - batch[0] = batch[0] - qty_to_pop - qty_to_pop = 0 - - stock_value = _round_off_if_near_zero(sum(flt(batch[0]) * flt(batch[1]) for batch in self.wh_data.stock_queue)) - stock_qty = _round_off_if_near_zero(sum(flt(batch[0]) for batch in self.wh_data.stock_queue)) + stock_qty, stock_value = fifo_queue.get_total_stock_and_value() + self.wh_data.stock_queue = fifo_queue.get_state() + self.wh_data.stock_value = stock_value if stock_qty: - self.wh_data.valuation_rate = stock_value / flt(stock_qty) + self.wh_data.valuation_rate = stock_value / stock_qty + if not self.wh_data.stock_queue: self.wh_data.stock_queue.append([0, sle.incoming_rate or sle.outgoing_rate or self.wh_data.valuation_rate]) + + def check_if_allow_zero_valuation_rate(self, voucher_type, voucher_detail_no): ref_item_dt = "" @@ -802,9 +783,9 @@ class update_entries_after(object): def update_bin(self): # update bin for each warehouse for warehouse, data in self.data.items(): - bin_record = get_or_make_bin(self.item_code, warehouse) + bin_name = get_or_make_bin(self.item_code, warehouse) - frappe.db.set_value('Bin', bin_record, { + frappe.db.set_value('Bin', bin_name, { "valuation_rate": data.valuation_rate, "actual_qty": data.qty_after_transaction, "stock_value": data.stock_value @@ -1060,17 +1041,36 @@ def validate_negative_qty_in_future_sle(args, allow_negative_stock=False): allow_negative_stock = cint(allow_negative_stock) \ or cint(frappe.db.get_single_value("Stock Settings", "allow_negative_stock")) - if (args.actual_qty < 0 or args.voucher_type == "Stock Reconciliation") and not allow_negative_stock: - sle = get_future_sle_with_negative_qty(args) - if sle: - message = _("{0} units of {1} needed in {2} on {3} {4} for {5} to complete this transaction.").format( - abs(sle[0]["qty_after_transaction"]), - frappe.get_desk_link('Item', args.item_code), - frappe.get_desk_link('Warehouse', args.warehouse), - sle[0]["posting_date"], sle[0]["posting_time"], - frappe.get_desk_link(sle[0]["voucher_type"], sle[0]["voucher_no"])) + if allow_negative_stock: + return + if not (args.actual_qty < 0 or args.voucher_type == "Stock Reconciliation"): + return + + neg_sle = get_future_sle_with_negative_qty(args) + if neg_sle: + message = _("{0} units of {1} needed in {2} on {3} {4} for {5} to complete this transaction.").format( + abs(neg_sle[0]["qty_after_transaction"]), + frappe.get_desk_link('Item', args.item_code), + frappe.get_desk_link('Warehouse', args.warehouse), + neg_sle[0]["posting_date"], neg_sle[0]["posting_time"], + frappe.get_desk_link(neg_sle[0]["voucher_type"], neg_sle[0]["voucher_no"])) + + frappe.throw(message, NegativeStockError, title='Insufficient Stock') + + + if not args.batch_no: + return + + neg_batch_sle = get_future_sle_with_negative_batch_qty(args) + if neg_batch_sle: + message = _("{0} units of {1} needed in {2} on {3} {4} for {5} to complete this transaction.").format( + abs(neg_batch_sle[0]["cumulative_total"]), + frappe.get_desk_link('Batch', args.batch_no), + frappe.get_desk_link('Warehouse', args.warehouse), + neg_batch_sle[0]["posting_date"], neg_batch_sle[0]["posting_time"], + frappe.get_desk_link(neg_batch_sle[0]["voucher_type"], neg_batch_sle[0]["voucher_no"])) + frappe.throw(message, NegativeStockError, title="Insufficient Stock for Batch") - frappe.throw(message, NegativeStockError, title='Insufficient Stock') def get_future_sle_with_negative_qty(args): return frappe.db.sql(""" @@ -1089,11 +1089,24 @@ def get_future_sle_with_negative_qty(args): limit 1 """, args, as_dict=1) -def _round_off_if_near_zero(number: float, precision: int = 6) -> float: - """ Rounds off the number to zero only if number is close to zero for decimal - specified in precision. Precision defaults to 6. - """ - if flt(number) < (1.0 / (10**precision)): - return 0 - return flt(number) +def get_future_sle_with_negative_batch_qty(args): + return frappe.db.sql(""" + with batch_ledger as ( + select + posting_date, posting_time, voucher_type, voucher_no, + sum(actual_qty) over (order by posting_date, posting_time, creation) as cumulative_total + from `tabStock Ledger Entry` + where + item_code = %(item_code)s + and warehouse = %(warehouse)s + and batch_no=%(batch_no)s + and is_cancelled = 0 + order by posting_date, posting_time, creation + ) + select * from batch_ledger + where + cumulative_total < 0.0 + and timestamp(posting_date, posting_time) >= timestamp(%(posting_date)s, %(posting_time)s) + limit 1 + """, args, as_dict=1) diff --git a/erpnext/stock/tests/test_valuation.py b/erpnext/stock/tests/test_valuation.py new file mode 100644 index 00000000000..85788bac7f8 --- /dev/null +++ b/erpnext/stock/tests/test_valuation.py @@ -0,0 +1,166 @@ +import unittest + +from hypothesis import given +from hypothesis import strategies as st + +from erpnext.stock.valuation import FIFOValuation, _round_off_if_near_zero + +qty_gen = st.floats(min_value=-1e6, max_value=1e6) +value_gen = st.floats(min_value=1, max_value=1e6) +stock_queue_generator = st.lists(st.tuples(qty_gen, value_gen), min_size=10) + + +class TestFifoValuation(unittest.TestCase): + + def setUp(self): + self.queue = FIFOValuation([]) + + def tearDown(self): + qty, value = self.queue.get_total_stock_and_value() + self.assertTotalQty(qty) + self.assertTotalValue(value) + + def assertTotalQty(self, qty): + self.assertAlmostEqual(sum(q for q, _ in self.queue), qty, msg=f"queue: {self.queue}", places=4) + + def assertTotalValue(self, value): + self.assertAlmostEqual(sum(q * r for q, r in self.queue), value, msg=f"queue: {self.queue}", places=2) + + def test_simple_addition(self): + self.queue.add_stock(1, 10) + self.assertTotalQty(1) + + def test_simple_removal(self): + self.queue.add_stock(1, 10) + self.queue.remove_stock(1) + self.assertTotalQty(0) + + def test_merge_new_stock(self): + self.queue.add_stock(1, 10) + self.queue.add_stock(1, 10) + self.assertEqual(self.queue, [[2, 10]]) + + def test_adding_negative_stock_keeps_rate(self): + self.queue = FIFOValuation([[-5.0, 100]]) + self.queue.add_stock(1, 10) + self.assertEqual(self.queue, [[-4, 100]]) + + def test_adding_negative_stock_updates_rate(self): + self.queue = FIFOValuation([[-5.0, 100]]) + self.queue.add_stock(6, 10) + self.assertEqual(self.queue, [[1, 10]]) + + + def test_negative_stock(self): + self.queue.remove_stock(1, 5) + self.assertEqual(self.queue, [[-1, 5]]) + + # XXX + self.queue.remove_stock(1, 10) + self.assertTotalQty(-2) + + self.queue.add_stock(2, 10) + self.assertTotalQty(0) + self.assertTotalValue(0) + + def test_removing_specified_rate(self): + self.queue.add_stock(1, 10) + self.queue.add_stock(1, 20) + + self.queue.remove_stock(1, 20) + self.assertEqual(self.queue, [[1, 10]]) + + + def test_remove_multiple_bins(self): + self.queue.add_stock(1, 10) + self.queue.add_stock(2, 20) + self.queue.add_stock(1, 20) + self.queue.add_stock(5, 20) + + self.queue.remove_stock(4) + self.assertEqual(self.queue, [[5, 20]]) + + + def test_remove_multiple_bins_with_rate(self): + self.queue.add_stock(1, 10) + self.queue.add_stock(2, 20) + self.queue.add_stock(1, 20) + self.queue.add_stock(5, 20) + + self.queue.remove_stock(3, 20) + self.assertEqual(self.queue, [[1, 10], [5, 20]]) + + def test_collapsing_of_queue(self): + self.queue.add_stock(1, 1) + self.queue.add_stock(1, 2) + self.queue.add_stock(1, 3) + self.queue.add_stock(1, 4) + + self.assertTotalValue(10) + + self.queue.remove_stock(3, 1) + # XXX + self.assertEqual(self.queue, [[1, 7]]) + + def test_rounding_off(self): + self.queue.add_stock(1.0, 1.0) + self.queue.remove_stock(1.0 - 1e-9) + self.assertTotalQty(0) + + def test_rounding_off_near_zero(self): + self.assertEqual(_round_off_if_near_zero(0), 0) + self.assertEqual(_round_off_if_near_zero(1), 1) + self.assertEqual(_round_off_if_near_zero(-1), -1) + self.assertEqual(_round_off_if_near_zero(-1e-8), 0) + self.assertEqual(_round_off_if_near_zero(1e-8), 0) + + def test_totals(self): + self.queue.add_stock(1, 10) + self.queue.add_stock(2, 13) + self.queue.add_stock(1, 17) + self.queue.remove_stock(1) + self.queue.remove_stock(1) + self.queue.remove_stock(1) + self.queue.add_stock(5, 17) + self.queue.add_stock(8, 11) + + @given(stock_queue_generator) + def test_fifo_qty_hypothesis(self, stock_queue): + self.queue = FIFOValuation([]) + total_qty = 0 + + for qty, rate in stock_queue: + if qty == 0: + continue + if qty > 0: + self.queue.add_stock(qty, rate) + total_qty += qty + else: + qty = abs(qty) + consumed = self.queue.remove_stock(qty) + self.assertAlmostEqual(qty, sum(q for q, _ in consumed), msg=f"incorrect consumption {consumed}") + total_qty -= qty + self.assertTotalQty(total_qty) + + @given(stock_queue_generator) + def test_fifo_qty_value_nonneg_hypothesis(self, stock_queue): + self.queue = FIFOValuation([]) + total_qty = 0.0 + total_value = 0.0 + + for qty, rate in stock_queue: + # don't allow negative stock + if qty == 0 or total_qty + qty < 0 or abs(qty) < 0.1: + continue + if qty > 0: + self.queue.add_stock(qty, rate) + total_qty += qty + total_value += qty * rate + else: + qty = abs(qty) + consumed = self.queue.remove_stock(qty) + self.assertAlmostEqual(qty, sum(q for q, _ in consumed), msg=f"incorrect consumption {consumed}") + total_qty -= qty + total_value -= sum(q * r for q, r in consumed) + self.assertTotalQty(total_qty) + self.assertTotalValue(total_value) diff --git a/erpnext/stock/utils.py b/erpnext/stock/utils.py index 8031c58b812..3b1ae3b43cd 100644 --- a/erpnext/stock/utils.py +++ b/erpnext/stock/utils.py @@ -12,6 +12,7 @@ import erpnext class InvalidWarehouseCompany(frappe.ValidationError): pass +class PendingRepostingError(frappe.ValidationError): pass def get_stock_value_from_bin(warehouse=None, item_code=None): values = {} @@ -187,7 +188,7 @@ def get_bin(item_code, warehouse): bin_obj.flags.ignore_permissions = True return bin_obj -def get_or_make_bin(item_code, warehouse) -> str: +def get_or_make_bin(item_code: str , warehouse: str) -> str: bin_record = frappe.db.get_value('Bin', {'item_code': item_code, 'warehouse': warehouse}) if not bin_record: @@ -203,11 +204,12 @@ def get_or_make_bin(item_code, warehouse) -> str: return bin_record def update_bin(args, allow_negative_stock=False, via_landed_cost_voucher=False): + """WARNING: This function is deprecated. Inline this function instead of using it.""" from erpnext.stock.doctype.bin.bin import update_stock is_stock_item = frappe.get_cached_value('Item', args.get("item_code"), 'is_stock_item') if is_stock_item: - bin_record = get_or_make_bin(args.get("item_code"), args.get("warehouse")) - update_stock(bin_record, args, allow_negative_stock, via_landed_cost_voucher) + bin_name = get_or_make_bin(args.get("item_code"), args.get("warehouse")) + update_stock(bin_name, args, allow_negative_stock, via_landed_cost_voucher) else: frappe.msgprint(_("Item {0} ignored since it is not a stock item").format(args.get("item_code"))) @@ -416,3 +418,28 @@ def is_reposting_item_valuation_in_progress(): {'docstatus': 1, 'status': ['in', ['Queued','In Progress']]}) if reposting_in_progress: frappe.msgprint(_("Item valuation reposting in progress. Report might show incorrect item valuation."), alert=1) + +def check_pending_reposting(posting_date: str, throw_error: bool = True) -> bool: + """Check if there are pending reposting job till the specified posting date.""" + + filters = { + "docstatus": 1, + "status": ["in", ["Queued","In Progress", "Failed"]], + "posting_date": ["<=", posting_date], + } + + reposting_pending = frappe.db.exists("Repost Item Valuation", filters) + if reposting_pending and throw_error: + msg = _("Stock/Accounts can not be frozen as processing of backdated entries is going on. Please try again later.") + frappe.msgprint(msg, + raise_exception=PendingRepostingError, + title="Stock Reposting Ongoing", + indicator="red", + primary_action={ + "label": _("Show pending entries"), + "client_action": "erpnext.route_to_pending_reposts", + "args": filters, + } + ) + + return bool(reposting_pending) diff --git a/erpnext/stock/valuation.py b/erpnext/stock/valuation.py new file mode 100644 index 00000000000..45c50830995 --- /dev/null +++ b/erpnext/stock/valuation.py @@ -0,0 +1,146 @@ +from typing import Callable, List, NewType, Optional, Tuple + +from frappe.utils import flt + +FifoBin = NewType("FifoBin", List[float]) + +# Indexes of values inside FIFO bin 2-tuple +QTY = 0 +RATE = 1 + + +class FIFOValuation: + """Valuation method where a queue of all the incoming stock is maintained. + + New stock is added at end of the queue. + Qty consumption happens on First In First Out basis. + + Queue is implemented using "bins" of [qty, rate]. + + ref: https://en.wikipedia.org/wiki/FIFO_and_LIFO_accounting + """ + + # specifying the attributes to save resources + # ref: https://docs.python.org/3/reference/datamodel.html#slots + __slots__ = ["queue",] + + def __init__(self, state: Optional[List[FifoBin]]): + self.queue: List[FifoBin] = state if state is not None else [] + + def __repr__(self): + return str(self.queue) + + def __iter__(self): + return iter(self.queue) + + def __eq__(self, other): + if isinstance(other, list): + return self.queue == other + return self.queue == other.queue + + def get_state(self) -> List[FifoBin]: + """Get current state of queue.""" + return self.queue + + def get_total_stock_and_value(self) -> Tuple[float, float]: + total_qty = 0.0 + total_value = 0.0 + + for qty, rate in self.queue: + total_qty += flt(qty) + total_value += flt(qty) * flt(rate) + + return _round_off_if_near_zero(total_qty), _round_off_if_near_zero(total_value) + + def add_stock(self, qty: float, rate: float) -> None: + """Update fifo queue with new stock. + + args: + qty: new quantity to add + rate: incoming rate of new quantity""" + + if not len(self.queue): + self.queue.append([0, 0]) + + # last row has the same rate, merge new bin. + if self.queue[-1][RATE] == rate: + self.queue[-1][QTY] += qty + else: + # Item has a positive balance qty, add new entry + if self.queue[-1][QTY] > 0: + self.queue.append([qty, rate]) + else: # negative balance qty + qty = self.queue[-1][QTY] + qty + if qty > 0: # new balance qty is positive + self.queue[-1] = [qty, rate] + else: # new balance qty is still negative, maintain same rate + self.queue[-1][QTY] = qty + + def remove_stock( + self, qty: float, outgoing_rate: float = 0.0, rate_generator: Callable[[], float] = None + ) -> List[FifoBin]: + """Remove stock from the queue and return popped bins. + + args: + qty: quantity to remove + rate: outgoing rate + rate_generator: function to be called if queue is not found and rate is required. + """ + if not rate_generator: + rate_generator = lambda : 0.0 # noqa + + consumed_bins = [] + while qty: + if not len(self.queue): + # rely on rate generator. + self.queue.append([0, rate_generator()]) + + index = None + if outgoing_rate > 0: + # Find the entry where rate matched with outgoing rate + for idx, fifo_bin in enumerate(self.queue): + if fifo_bin[RATE] == outgoing_rate: + index = idx + break + + # If no entry found with outgoing rate, collapse queue + if index is None: # nosemgrep + new_stock_value = sum(d[QTY] * d[RATE] for d in self.queue) - qty * outgoing_rate + new_stock_qty = sum(d[QTY] for d in self.queue) - qty + self.queue = [[new_stock_qty, new_stock_value / new_stock_qty if new_stock_qty > 0 else outgoing_rate]] + consumed_bins.append([qty, outgoing_rate]) + break + else: + index = 0 + + # select first bin or the bin with same rate + fifo_bin = self.queue[index] + if qty >= fifo_bin[QTY]: + # consume current bin + qty = _round_off_if_near_zero(qty - fifo_bin[QTY]) + to_consume = self.queue.pop(index) + consumed_bins.append(list(to_consume)) + + if not self.queue and qty: + # stock finished, qty still remains to be withdrawn + # negative stock, keep in as a negative bin + self.queue.append([-qty, outgoing_rate or fifo_bin[RATE]]) + consumed_bins.append([qty, outgoing_rate or fifo_bin[RATE]]) + break + else: + # qty found in current bin consume it and exit + fifo_bin[QTY] = _round_off_if_near_zero(fifo_bin[QTY] - qty) + consumed_bins.append([qty, fifo_bin[RATE]]) + qty = 0 + + return consumed_bins + + +def _round_off_if_near_zero(number: float, precision: int = 7) -> float: + """Rounds off the number to zero only if number is close to zero for decimal + specified in precision. Precision defaults to 7. + """ + if abs(0.0 - flt(number)) < (1.0 / (10 ** precision)): + return 0.0 + + return flt(number) diff --git a/erpnext/stock/workspace/stock/stock.json b/erpnext/stock/workspace/stock/stock.json index 9c805150f19..4df27f5dbff 100644 --- a/erpnext/stock/workspace/stock/stock.json +++ b/erpnext/stock/workspace/stock/stock.json @@ -704,59 +704,9 @@ "link_type": "Report", "onboard": 0, "type": "Link" - }, - { - "dependencies": "Stock Ledger Entry", - "hidden": 0, - "is_query_report": 1, - "label": "Stock and Account Value Comparison", - "link_count": 0, - "link_to": "Stock and Account Value Comparison", - "link_type": "Report", - "onboard": 0, - "type": "Link" - }, - { - "hidden": 0, - "is_query_report": 0, - "label": "Incorrect Data Report", - "link_count": 0, - "link_type": "DocType", - "onboard": 0, - "type": "Card Break" - }, - { - "hidden": 0, - "is_query_report": 0, - "label": "Incorrect Serial No Qty and Valuation", - "link_count": 0, - "link_to": "Incorrect Serial No Valuation", - "link_type": "Report", - "onboard": 0, - "type": "Link" - }, - { - "hidden": 0, - "is_query_report": 0, - "label": "Incorrect Balance Qty After Transaction", - "link_count": 0, - "link_to": "Incorrect Balance Qty After Transaction", - "link_type": "Report", - "onboard": 0, - "type": "Link" - }, - { - "hidden": 0, - "is_query_report": 0, - "label": "Stock and Account Value Comparison", - "link_count": 0, - "link_to": "Stock and Account Value Comparison", - "link_type": "Report", - "onboard": 0, - "type": "Link" } ], - "modified": "2021-08-05 12:16:02.361519", + "modified": "2021-11-23 04:34:00.420870", "modified_by": "Administrator", "module": "Stock", "name": "Stock", diff --git a/erpnext/support/doctype/issue/issue.json b/erpnext/support/doctype/issue/issue.json index 14712f89feb..3ff7d02f1ae 100644 --- a/erpnext/support/doctype/issue/issue.json +++ b/erpnext/support/doctype/issue/issue.json @@ -24,12 +24,10 @@ "service_level_section", "service_level_agreement", "response_by", - "response_by_variance", "reset_service_level_agreement", "cb", "agreement_status", "resolution_by", - "resolution_by_variance", "service_level_agreement_creation", "on_hold_since", "total_hold_time", @@ -123,7 +121,6 @@ "search_index": 1 }, { - "default": "Medium", "fieldname": "priority", "fieldtype": "Link", "in_list_view": 1, @@ -318,22 +315,6 @@ "fieldtype": "Check", "label": "Via Customer Portal" }, - { - "depends_on": "eval: doc.service_level_agreement && doc.status != 'Replied';", - "fieldname": "response_by_variance", - "fieldtype": "Duration", - "hide_seconds": 1, - "label": "Response By Variance", - "read_only": 1 - }, - { - "depends_on": "eval: doc.service_level_agreement && doc.status != 'Replied';", - "fieldname": "resolution_by_variance", - "fieldtype": "Duration", - "hide_seconds": 1, - "label": "Resolution By Variance", - "read_only": 1 - }, { "fieldname": "service_level_agreement_creation", "fieldtype": "Datetime", @@ -391,12 +372,12 @@ "read_only": 1 }, { - "default": "Ongoing", + "default": "First Response Due", "depends_on": "eval: doc.service_level_agreement", "fieldname": "agreement_status", "fieldtype": "Select", "label": "Service Level Agreement Status", - "options": "Ongoing\nFulfilled\nFailed", + "options": "First Response Due\nResolution Due\nFulfilled\nFailed", "read_only": 1 }, { @@ -410,10 +391,11 @@ "icon": "fa fa-ticket", "idx": 7, "links": [], - "modified": "2021-06-10 03:22:27.098898", + "modified": "2021-11-24 13:13:10.276630", "modified_by": "Administrator", "module": "Support", "name": "Issue", + "naming_rule": "By \"Naming Series\" field", "owner": "Administrator", "permissions": [ { diff --git a/erpnext/support/doctype/issue/issue.py b/erpnext/support/doctype/issue/issue.py index 0dc3639f1eb..d5e5b782880 100644 --- a/erpnext/support/doctype/issue/issue.py +++ b/erpnext/support/doctype/issue/issue.py @@ -87,11 +87,9 @@ class Issue(Document): if replicated_issue.service_level_agreement: replicated_issue.service_level_agreement_creation = now_datetime() replicated_issue.service_level_agreement = None - replicated_issue.agreement_status = "Ongoing" + replicated_issue.agreement_status = "First Response Due" replicated_issue.response_by = None - replicated_issue.response_by_variance = None replicated_issue.resolution_by = None - replicated_issue.resolution_by_variance = None replicated_issue.reset_issue_metrics() frappe.get_doc(replicated_issue).insert() diff --git a/erpnext/support/doctype/issue/issue_list.js b/erpnext/support/doctype/issue/issue_list.js index e04498e29ee..5bfecb019cc 100644 --- a/erpnext/support/doctype/issue/issue_list.js +++ b/erpnext/support/doctype/issue/issue_list.js @@ -18,7 +18,6 @@ frappe.listview_settings['Issue'] = { }, get_indicator: function(doc) { if (doc.status === 'Open') { - if (!doc.priority) doc.priority = 'Medium'; const color = { 'Low': 'yellow', 'Medium': 'orange', diff --git a/erpnext/support/doctype/issue/test_issue.py b/erpnext/support/doctype/issue/test_issue.py index ab9a444bc34..7a0a5e506fa 100644 --- a/erpnext/support/doctype/issue/test_issue.py +++ b/erpnext/support/doctype/issue/test_issue.py @@ -1,10 +1,10 @@ # Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors and Contributors # See license.txt -import datetime import unittest import frappe +from frappe import _ from frappe.core.doctype.user_permission.test_user_permission import create_user from frappe.utils import flt, get_datetime @@ -83,30 +83,6 @@ class TestIssue(TestSetUp): self.assertEqual(issue.agreement_status, 'Fulfilled') - def test_issue_metrics(self): - creation = get_datetime("2020-03-04 4:00") - - issue = make_issue(creation, index=1) - create_communication(issue.name, "test@example.com", "Received", creation) - - creation = get_datetime("2020-03-04 4:15") - create_communication(issue.name, "test@admin.com", "Sent", creation) - - creation = get_datetime("2020-03-04 5:00") - create_communication(issue.name, "test@example.com", "Received", creation) - - creation = get_datetime("2020-03-04 5:05") - create_communication(issue.name, "test@admin.com", "Sent", creation) - - frappe.flags.current_time = get_datetime("2020-03-04 5:05") - issue.reload() - issue.status = 'Closed' - issue.save() - - self.assertEqual(issue.avg_response_time, 600) - self.assertEqual(issue.resolution_time, 3900) - self.assertEqual(issue.user_resolution_time, 1200) - def test_hold_time_on_replied(self): creation = get_datetime("2020-03-04 4:00") @@ -122,6 +98,7 @@ class TestIssue(TestSetUp): issue.save() self.assertEqual(issue.on_hold_since, frappe.flags.current_time) + self.assertFalse(issue.resolution_by) creation = get_datetime("2020-03-04 5:00") frappe.flags.current_time = get_datetime("2020-03-04 5:00") @@ -142,6 +119,142 @@ class TestIssue(TestSetUp): issue.reload() self.assertEqual(flt(issue.total_hold_time, 2), 2700) + def test_issue_close_after_on_hold(self): + frappe.flags.current_time = get_datetime("2021-11-01 19:00") + + issue = make_issue(frappe.flags.current_time, index=1) + create_communication(issue.name, "test@example.com", "Received", frappe.flags.current_time) + + # send a reply within SLA + frappe.flags.current_time = get_datetime("2021-11-02 11:00") + create_communication(issue.name, "test@admin.com", "Sent", frappe.flags.current_time) + + issue.reload() + issue.status = 'Replied' + issue.save() + + self.assertEqual(issue.on_hold_since, frappe.flags.current_time) + + # close the issue after being on hold for 20 days + frappe.flags.current_time = get_datetime("2021-11-22 01:00") + issue.status = 'Closed' + issue.save() + + self.assertEqual(issue.resolution_by, get_datetime('2021-11-22 06:00:00')) + self.assertEqual(issue.resolution_date, get_datetime('2021-11-22 01:00:00')) + self.assertEqual(issue.agreement_status, 'Fulfilled') + + def test_issue_open_after_closed(self): + + # Created on -> 1 pm, Response Time -> 4 hrs, Resolution Time -> 6 hrs + frappe.flags.current_time = get_datetime("2021-11-01 13:00") + issue = make_issue(frappe.flags.current_time, index=1, issue_type='Critical') # Applies 24hr working time SLA + create_communication(issue.name, "test@example.com", "Received", frappe.flags.current_time) + self.assertEquals(issue.agreement_status, 'First Response Due') + self.assertEquals(issue.response_by, get_datetime("2021-11-01 17:00")) + self.assertEquals(issue.resolution_by, get_datetime("2021-11-01 19:00")) + + # Replied on → 2 pm + frappe.flags.current_time = get_datetime("2021-11-01 14:00") + create_communication(issue.name, "test@admin.com", "Sent", frappe.flags.current_time) + issue.reload() + issue.status = 'Replied' + issue.save() + self.assertEquals(issue.agreement_status, 'Resolution Due') + self.assertEquals(issue.on_hold_since, frappe.flags.current_time) + self.assertEquals(issue.first_responded_on, frappe.flags.current_time) + + # Customer Replied → 3 pm + frappe.flags.current_time = get_datetime("2021-11-01 15:00") + create_communication(issue.name, "test@example.com", "Received", frappe.flags.current_time) + issue.reload() + self.assertEquals(issue.status, 'Open') + # Hold Time + 1 Hrs + self.assertEquals(issue.total_hold_time, 3600) + # Resolution By should increase by one hrs + self.assertEquals(issue.resolution_by, get_datetime("2021-11-01 20:00")) + + # Replied on → 4 pm, Open → 1 hr, Resolution Due → 8 pm + frappe.flags.current_time = get_datetime("2021-11-01 16:00") + create_communication(issue.name, "test@admin.com", "Sent", frappe.flags.current_time) + issue.reload() + issue.status = 'Replied' + issue.save() + self.assertEquals(issue.agreement_status, 'Resolution Due') + + # Customer Closed → 10 pm + frappe.flags.current_time = get_datetime("2021-11-01 22:00") + issue.status = 'Closed' + issue.save() + # Hold Time + 6 Hrs + self.assertEquals(issue.total_hold_time, 3600 + 21600) + # Resolution By should increase by 6 hrs + self.assertEquals(issue.resolution_by, get_datetime("2021-11-02 02:00")) + self.assertEquals(issue.agreement_status, 'Fulfilled') + self.assertEquals(issue.resolution_date, frappe.flags.current_time) + + # Customer Open → 3 am i.e after resolution by is crossed + frappe.flags.current_time = get_datetime("2021-11-02 03:00") + create_communication(issue.name, "test@example.com", "Received", frappe.flags.current_time) + issue.reload() + # Since issue was Resolved, Resolution By should be increased by 5 hrs (3am - 10pm) + self.assertEquals(issue.total_hold_time, 3600 + 21600 + 18000) + # Resolution By should increase by 5 hrs + self.assertEquals(issue.resolution_by, get_datetime("2021-11-02 07:00")) + self.assertEquals(issue.agreement_status, 'Resolution Due') + self.assertFalse(issue.resolution_date) + + # We Closed → 4 am, SLA should be Fulfilled + frappe.flags.current_time = get_datetime("2021-11-02 04:00") + issue.status = 'Closed' + issue.save() + self.assertEquals(issue.resolution_by, get_datetime("2021-11-02 07:00")) + self.assertEquals(issue.agreement_status, 'Fulfilled') + self.assertEquals(issue.resolution_date, frappe.flags.current_time) + + def test_recording_of_assignment_on_first_reponse_failure(self): + from frappe.desk.form.assign_to import add as add_assignment + + frappe.flags.current_time = get_datetime("2021-11-01 19:00") + + issue = make_issue(frappe.flags.current_time, index=1) + create_communication(issue.name, "test@example.com", "Received", frappe.flags.current_time) + add_assignment({ + 'doctype': issue.doctype, + 'name': issue.name, + 'assign_to': ['test@admin.com'] + }) + issue.reload() + + # send a reply failing response SLA + frappe.flags.current_time = get_datetime("2021-11-02 15:00") + create_communication(issue.name, "test@admin.com", "Sent", frappe.flags.current_time) + + # assert if a new timeline item has been added + # to record the assignment + comment = frappe.db.exists('Comment', { + 'reference_doctype': 'Issue', + 'reference_name': issue.name, + 'comment_type': 'Assigned', + 'content': _('First Response SLA Failed by {}').format('test') + }) + self.assertTrue(comment) + + def test_agreement_status_on_response(self): + frappe.flags.current_time = get_datetime("2021-11-01 19:00") + + issue = make_issue(frappe.flags.current_time, index=1) + create_communication(issue.name, "test@example.com", "Received", frappe.flags.current_time) + self.assertTrue(issue.status == 'Open') + + # send a reply within response SLA + frappe.flags.current_time = get_datetime("2021-11-02 11:00") + create_communication(issue.name, "test@admin.com", "Sent", frappe.flags.current_time) + + issue.reload() + self.assertEquals(issue.first_responded_on, frappe.flags.current_time) + self.assertEquals(issue.agreement_status, 'Resolution Due') + class TestFirstResponseTime(TestSetUp): # working hours used in all cases: Mon-Fri, 10am to 6pm # all dates are in the mm-dd-yyyy format @@ -355,12 +468,18 @@ class TestFirstResponseTime(TestSetUp): def create_issue_and_communication(issue_creation, first_responded_on): issue = make_issue(issue_creation, index=1) sender = create_user("test@admin.com") + frappe.flags.current_time = first_responded_on create_communication(issue.name, sender.email, "Sent", first_responded_on) issue.reload() return issue def make_issue(creation=None, customer=None, index=0, priority=None, issue_type=None): + if issue_type and not frappe.db.exists('Issue Type', issue_type): + doc = frappe.new_doc('Issue Type') + doc.name = issue_type + doc.insert() + issue = frappe.get_doc({ "doctype": "Issue", "subject": "Service Level Agreement Issue {0}".format(index), diff --git a/erpnext/support/doctype/service_level_agreement/service_level_agreement.js b/erpnext/support/doctype/service_level_agreement/service_level_agreement.js index ae2080c3b53..bfbffe22ad7 100644 --- a/erpnext/support/doctype/service_level_agreement/service_level_agreement.js +++ b/erpnext/support/doctype/service_level_agreement/service_level_agreement.js @@ -22,10 +22,41 @@ frappe.ui.form.on('Service Level Agreement', { refresh: function(frm) { frm.trigger('fetch_status_fields'); frm.trigger('toggle_resolution_fields'); + frm.trigger('default_service_level_agreement'); + frm.trigger('entity'); + }, + + default_service_level_agreement: function(frm) { + const field = frm.get_field('default_service_level_agreement'); + if (frm.doc.default_service_level_agreement) { + field.set_description(__('SLA will be applied on every {0}', [frm.doc.document_type])); + } else { + field.set_description(__('Enable to apply SLA on every {0}', [frm.doc.document_type])); + } }, document_type: function(frm) { frm.trigger('fetch_status_fields'); + frm.trigger('default_service_level_agreement'); + }, + + entity_type: function(frm) { + frm.set_value('entity', undefined); + }, + + entity: function(frm) { + const field = frm.get_field('entity'); + if (frm.doc.entity) { + const and_descendants = frm.doc.entity_type != 'Customer' ? ' ' + __('or its descendants') : ''; + field.set_description( + __('SLA will be applied if {1} is set as {2}{3}', [ + frm.doc.document_type, frm.doc.entity_type, + frm.doc.entity, and_descendants + ]) + ); + } else { + field.set_description(''); + } }, fetch_status_fields: function(frm) { diff --git a/erpnext/support/doctype/service_level_agreement/service_level_agreement.json b/erpnext/support/doctype/service_level_agreement/service_level_agreement.json index 5f470aad672..1698e2380f7 100644 --- a/erpnext/support/doctype/service_level_agreement/service_level_agreement.json +++ b/erpnext/support/doctype/service_level_agreement/service_level_agreement.json @@ -6,22 +6,17 @@ "editable_grid": 1, "engine": "InnoDB", "field_order": [ - "enabled", - "section_break_2", "document_type", - "default_service_level_agreement", "default_priority", "column_break_2", "service_level", - "holiday_list", - "entity_section", - "entity_type", - "column_break_10", - "entity", + "enabled", "filters_section", - "condition", + "default_service_level_agreement", + "entity_type", + "entity", "column_break_15", - "condition_description", + "condition", "agreement_details_section", "start_date", "column_break_7", @@ -31,8 +26,10 @@ "priorities", "status_details", "sla_fulfilled_on", + "column_break_22", "pause_sla_on", "support_and_resolution_section_break", + "holiday_list", "support_and_resolution" ], "fields": [ @@ -42,7 +39,8 @@ "in_list_view": 1, "in_standard_filter": 1, "label": "Service Level Name", - "reqd": 1 + "reqd": 1, + "set_only_once": 1 }, { "fieldname": "holiday_list", @@ -56,10 +54,10 @@ "fieldtype": "Column Break" }, { - "depends_on": "eval: !doc.default_service_level_agreement", + "depends_on": "eval: doc.document_type", "fieldname": "agreement_details_section", "fieldtype": "Section Break", - "label": "Agreement Details" + "label": "Valid From" }, { "fieldname": "start_date", @@ -72,7 +70,6 @@ "fieldtype": "Column Break" }, { - "depends_on": "eval: !doc.default_service_level_agreement", "fieldname": "end_date", "fieldtype": "Date", "label": "End Date" @@ -80,7 +77,7 @@ { "fieldname": "response_and_resolution_time_section", "fieldtype": "Section Break", - "label": "Response and Resolution Time" + "label": "Response and Resolution" }, { "fieldname": "support_and_resolution_section_break", @@ -90,6 +87,7 @@ { "fieldname": "support_and_resolution", "fieldtype": "Table", + "label": "Working Hours", "options": "Service Day", "reqd": 1 }, @@ -101,10 +99,7 @@ "reqd": 1 }, { - "fieldname": "column_break_10", - "fieldtype": "Column Break" - }, - { + "depends_on": "eval: !doc.default_service_level_agreement", "fieldname": "entity", "fieldtype": "Dynamic Link", "in_list_view": 1, @@ -114,22 +109,12 @@ }, { "depends_on": "eval: !doc.default_service_level_agreement", - "fieldname": "entity_section", - "fieldtype": "Section Break", - "label": "Entity" - }, - { "fieldname": "entity_type", "fieldtype": "Select", "in_standard_filter": 1, "label": "Entity Type", "options": "\nCustomer\nCustomer Group\nTerritory" }, - { - "fieldname": "section_break_2", - "fieldtype": "Section Break", - "hide_border": 1 - }, { "default": "0", "fieldname": "default_service_level_agreement", @@ -152,7 +137,7 @@ { "fieldname": "document_type", "fieldtype": "Link", - "label": "Document Type", + "label": "Apply On", "options": "DocType", "reqd": 1, "set_only_once": 1 @@ -164,6 +149,7 @@ "label": "Enabled" }, { + "depends_on": "document_type", "fieldname": "status_details", "fieldtype": "Section Break", "label": "Status Details" @@ -182,28 +168,31 @@ "label": "Apply SLA for Resolution Time" }, { + "depends_on": "document_type", "fieldname": "filters_section", "fieldtype": "Section Break", - "label": "Assignment Condition" + "label": "Assignment Conditions" }, { "fieldname": "column_break_15", "fieldtype": "Column Break" }, { + "depends_on": "eval: !doc.default_service_level_agreement", + "description": "Simple Python Expression, Example: doc.status == 'Open' and doc.issue_type == 'Bug'", "fieldname": "condition", "fieldtype": "Code", "label": "Condition", - "options": "Python" + "max_height": "7rem", + "options": "PythonExpression" }, { - "fieldname": "condition_description", - "fieldtype": "HTML", - "options": "

Condition Examples:

\n
doc.status==\"Open\"
doc.due_date==nowdate()
doc.total > 40000\n
" + "fieldname": "column_break_22", + "fieldtype": "Column Break" } ], "links": [], - "modified": "2021-10-02 11:32:55.556024", + "modified": "2021-11-26 15:45:33.289911", "modified_by": "Administrator", "module": "Support", "name": "Service Level Agreement", diff --git a/erpnext/support/doctype/service_level_agreement/service_level_agreement.py b/erpnext/support/doctype/service_level_agreement/service_level_agreement.py index 5f8f83d89ba..b3348f1e1e8 100644 --- a/erpnext/support/doctype/service_level_agreement/service_level_agreement.py +++ b/erpnext/support/doctype/service_level_agreement/service_level_agreement.py @@ -10,7 +10,6 @@ from frappe.core.utils import get_parent_doc from frappe.model.document import Document from frappe.utils import ( add_to_date, - cint, get_datetime, get_datetime_str, get_link_to_form, @@ -22,6 +21,7 @@ from frappe.utils import ( time_diff_in_seconds, to_timedelta, ) +from frappe.utils.nestedset import get_ancestors_of from frappe.utils.safe_exec import get_safe_globals from erpnext.support.doctype.issue.issue import get_holidays @@ -248,7 +248,7 @@ def get_active_service_level_agreement_for(doc): customer = doc.get('customer') or_filters.append( - ["Service Level Agreement", "entity", "in", [customer, get_customer_group(customer), get_customer_territory(customer)]] + ["Service Level Agreement", "entity", "in", [customer] + get_customer_group(customer) + get_customer_territory(customer)] ) default_sla_filter = filters + [["Service Level Agreement", "default_service_level_agreement", "=", 1]] @@ -275,11 +275,23 @@ def get_context(doc): return {"doc": doc.as_dict(), "nowdate": nowdate, "frappe": frappe._dict(utils=get_safe_globals().get("frappe").get("utils"))} def get_customer_group(customer): - return frappe.db.get_value("Customer", customer, "customer_group") if customer else None + customer_groups = [] + customer_group = frappe.db.get_value("Customer", customer, "customer_group") if customer else None + if customer_group: + ancestors = get_ancestors_of("Customer Group", customer_group) + customer_groups = [customer_group] + ancestors + + return customer_groups def get_customer_territory(customer): - return frappe.db.get_value("Customer", customer, "territory") if customer else None + customer_territories = [] + customer_territory = frappe.db.get_value("Customer", customer, "territory") if customer else None + if customer_territory: + ancestors = get_ancestors_of("Territory", customer_territory) + customer_territories = [customer_territory] + ancestors + + return customer_territories @frappe.whitelist() @@ -299,7 +311,7 @@ def get_service_level_agreement_filters(doctype, name, customer=None): if customer: # Include SLA with No Entity and Entity Type or_filters.append( - ["Service Level Agreement", "entity", "in", [customer, get_customer_group(customer), get_customer_territory(customer), ""]] + ["Service Level Agreement", "entity", "in", [""] + [customer] + get_customer_group(customer) + get_customer_territory(customer)] ) return { @@ -337,84 +349,135 @@ def set_documents_with_active_service_level_agreement(): def apply(doc, method=None): # Applies SLA to document on validate - if frappe.flags.in_patch or frappe.flags.in_migrate or frappe.flags.in_install or frappe.flags.in_setup_wizard or \ - doc.doctype not in get_documents_with_active_service_level_agreement(): + if ( + frappe.flags.in_patch + or frappe.flags.in_migrate + or frappe.flags.in_install + or frappe.flags.in_setup_wizard + or doc.doctype not in get_documents_with_active_service_level_agreement() + ): return - service_level_agreement = get_active_service_level_agreement_for(doc) + sla = get_active_service_level_agreement_for(doc) - if not service_level_agreement: + if not sla: return - set_sla_properties(doc, service_level_agreement) + process_sla(doc, sla) -def set_sla_properties(doc, service_level_agreement): - if frappe.db.exists(doc.doctype, doc.name): - from_db = frappe.get_doc(doc.doctype, doc.name) - else: - from_db = frappe._dict({}) - - meta = frappe.get_meta(doc.doctype) - - if meta.has_field("customer") and service_level_agreement.customer and doc.get("customer") and \ - not service_level_agreement.customer == doc.get("customer"): - frappe.throw(_("Service Level Agreement {0} is specific to Customer {1}").format(service_level_agreement.name, - service_level_agreement.customer)) - - doc.service_level_agreement = service_level_agreement.name - doc.priority = doc.get("priority") or service_level_agreement.default_priority - priority = get_priority(doc) +def process_sla(doc, sla): if not doc.creation: doc.creation = now_datetime(doc.get("owner")) - - if meta.has_field("service_level_agreement_creation"): + if doc.meta.has_field("service_level_agreement_creation"): doc.service_level_agreement_creation = now_datetime(doc.get("owner")) + doc.service_level_agreement = sla.name + doc.priority = doc.get("priority") or sla.default_priority + + handle_status_change(doc, sla.apply_sla_for_resolution) + update_response_and_resolution_metrics(doc, sla.apply_sla_for_resolution) + update_agreement_status(doc, sla.apply_sla_for_resolution) + + +def handle_status_change(doc, apply_sla_for_resolution): + now_time = frappe.flags.current_time or now_datetime(doc.get("owner")) + prev_status = frappe.db.get_value(doc.doctype, doc.name, 'status') + + hold_statuses = get_hold_statuses(doc.service_level_agreement) + fulfillment_statuses = get_fulfillment_statuses(doc.service_level_agreement) + + def is_hold_status(status): + return status in hold_statuses + + def is_fulfilled_status(status): + return status in fulfillment_statuses + + def is_open_status(status): + return status not in hold_statuses and status not in fulfillment_statuses + + def set_first_response(): + if doc.meta.has_field("first_responded_on") and not doc.get('first_responded_on'): + doc.first_responded_on = now_time + if get_datetime(doc.get('first_responded_on')) > get_datetime(doc.get('response_by')): + record_assigned_users_on_failure(doc) + + def calculate_hold_hours(): + # In case issue was closed and after few days it has been opened + # The hold time should be calculated from resolution_date + + on_hold_since = doc.resolution_date or doc.on_hold_since + if on_hold_since: + current_hold_hours = time_diff_in_seconds(now_time, on_hold_since) + doc.total_hold_time = (doc.total_hold_time or 0) + current_hold_hours + doc.on_hold_since = None + + if ((is_open_status(prev_status) and not is_open_status(doc.status)) or doc.flags.on_first_reply): + set_first_response() + + # Open to Replied + if is_open_status(prev_status) and is_hold_status(doc.status): + # Issue is on hold -> Set on_hold_since + doc.on_hold_since = now_time + reset_expected_response_and_resolution(doc) + + # Replied to Open + if is_hold_status(prev_status) and is_open_status(doc.status): + # Issue was on hold -> Calculate Total Hold Time + calculate_hold_hours() + # Issue is open -> reset resolution_date + reset_resolution_metrics(doc) + + # Open to Closed + if is_open_status(prev_status) and is_fulfilled_status(doc.status): + # Issue is closed -> Set resolution_date + doc.resolution_date = now_time + set_resolution_time(doc) + + # Closed to Open + if is_fulfilled_status(prev_status) and is_open_status(doc.status): + # Issue was closed -> Calculate Total Hold Time from resolution_date + calculate_hold_hours() + # Issue is open -> reset resolution_date + reset_resolution_metrics(doc) + + # Closed to Replied + if is_fulfilled_status(prev_status) and is_hold_status(doc.status): + # Issue was closed -> Calculate Total Hold Time from resolution_date + calculate_hold_hours() + # Issue is on hold -> Set on_hold_since + doc.on_hold_since = now_time + reset_expected_response_and_resolution(doc) + + # Replied to Closed + if is_hold_status(prev_status) and is_fulfilled_status(doc.status): + # Issue was on hold -> Calculate Total Hold Time + calculate_hold_hours() + # Issue is closed -> Set resolution_date + if apply_sla_for_resolution: + doc.resolution_date = now_time + set_resolution_time(doc) + + +def get_fulfillment_statuses(service_level_agreement): + return [entry.status for entry in frappe.db.get_all("SLA Fulfilled On Status", filters={ + "parent": service_level_agreement + }, fields=["status"])] + + +def get_hold_statuses(service_level_agreement): + return [entry.status for entry in frappe.db.get_all("Pause SLA On Status", filters={ + "parent": service_level_agreement + }, fields=["status"])] + + +def update_response_and_resolution_metrics(doc, apply_sla_for_resolution): + priority = get_response_and_resolution_duration(doc) start_date_time = get_datetime(doc.get("service_level_agreement_creation") or doc.creation) - - set_response_by_and_variance(doc, meta, start_date_time, priority) - if service_level_agreement.apply_sla_for_resolution: - set_resolution_by_and_variance(doc, meta, start_date_time, priority) - - update_status(doc, from_db, meta) - - -def update_status(doc, from_db, meta): - if meta.has_field("status"): - if meta.has_field("first_responded_on") and doc.status != "Open" and \ - from_db.status == "Open" and not doc.first_responded_on: - doc.first_responded_on = frappe.flags.current_time or now_datetime(doc.get("owner")) - - if meta.has_field("service_level_agreement") and doc.service_level_agreement: - # mark sla status as fulfilled based on the configuration - fulfillment_statuses = [entry.status for entry in frappe.db.get_all("SLA Fulfilled On Status", filters={ - "parent": doc.service_level_agreement - }, fields=["status"])] - - if doc.status in fulfillment_statuses and from_db.status not in fulfillment_statuses: - apply_sla_for_resolution = frappe.db.get_value("Service Level Agreement", doc.service_level_agreement, - "apply_sla_for_resolution") - - if apply_sla_for_resolution and meta.has_field("resolution_date"): - doc.resolution_date = frappe.flags.current_time or now_datetime(doc.get("owner")) - - if meta.has_field("agreement_status") and from_db.agreement_status == "Ongoing": - set_service_level_agreement_variance(doc.doctype, doc.name) - update_agreement_status(doc, meta) - - if apply_sla_for_resolution: - set_resolution_time(doc, meta) - set_user_resolution_time(doc, meta) - - if doc.status == "Open" and from_db.status != "Open": - # if no date, it should be set as None and not a blank string "", as per mysql strict config - # enable SLA and variance on Reopen - reset_metrics(doc, meta) - set_service_level_agreement_variance(doc.doctype, doc.name) - - handle_hold_time(doc, meta, from_db.status) + set_response_by(doc, start_date_time, priority) + if apply_sla_for_resolution and not doc.get('on_hold_since'): # resolution_by is reset if on hold + set_resolution_by(doc, start_date_time, priority) def get_expected_time_for(parameter, service_level, start_date_time): @@ -485,37 +548,13 @@ def get_support_days(service_level): return support_days -def set_service_level_agreement_variance(doctype, doc=None): +def set_resolution_time(doc): + start_date_time = get_datetime(doc.get("service_level_agreement_creation") or doc.creation) + if doc.meta.has_field("resolution_time"): + doc.resolution_time = time_diff_in_seconds(doc.resolution_date, start_date_time) - filters = {"status": "Open", "agreement_status": "Ongoing"} - - if doc: - filters = {"name": doc} - - for entry in frappe.get_all(doctype, filters=filters): - current_doc = frappe.get_doc(doctype, entry.name) - current_time = frappe.flags.current_time or now_datetime(current_doc.get("owner")) - apply_sla_for_resolution = frappe.db.get_value("Service Level Agreement", current_doc.service_level_agreement, - "apply_sla_for_resolution") - - if not current_doc.first_responded_on: # first_responded_on set when first reply is sent to customer - variance = round(time_diff_in_seconds(current_doc.response_by, current_time), 2) - frappe.db.set_value(current_doc.doctype, current_doc.name, "response_by_variance", variance, update_modified=False) - - if variance < 0: - frappe.db.set_value(current_doc.doctype, current_doc.name, "agreement_status", "Failed", update_modified=False) - - if apply_sla_for_resolution and not current_doc.get("resolution_date"): # resolution_date set when issue has been closed - variance = round(time_diff_in_seconds(current_doc.resolution_by, current_time), 2) - frappe.db.set_value(current_doc.doctype, current_doc.name, "resolution_by_variance", variance, update_modified=False) - - if variance < 0: - frappe.db.set_value(current_doc.doctype, current_doc.name, "agreement_status", "Failed", update_modified=False) - - -def set_user_resolution_time(doc, meta): # total time taken by a user to close the issue apart from wait_time - if not meta.has_field("user_resolution_time"): + if not doc.meta.has_field("user_resolution_time"): return communications = frappe.get_all("Communication", filters={ @@ -531,7 +570,7 @@ def set_user_resolution_time(doc, meta): pending_time.append(wait_time) total_pending_time = sum(pending_time) - resolution_time_in_secs = time_diff_in_seconds(doc.resolution_date, doc.creation) + resolution_time_in_secs = time_diff_in_seconds(doc.resolution_date, start_date_time) doc.user_resolution_time = resolution_time_in_secs - total_pending_time @@ -548,12 +587,12 @@ def change_service_level_agreement_and_priority(self): frappe.msgprint(_("Service Level Agreement has been changed to {0}.").format(self.service_level_agreement)) -def get_priority(doc): - service_level_agreement = frappe.get_doc("Service Level Agreement", doc.service_level_agreement) - priority = service_level_agreement.get_service_level_agreement_priority(doc.priority) +def get_response_and_resolution_duration(doc): + sla = frappe.get_doc("Service Level Agreement", doc.service_level_agreement) + priority = sla.get_service_level_agreement_priority(doc.priority) priority.update({ - "support_and_resolution": service_level_agreement.support_and_resolution, - "holiday_list": service_level_agreement.holiday_list + "support_and_resolution": sla.support_and_resolution, + "holiday_list": sla.holiday_list }) return priority @@ -572,120 +611,99 @@ def reset_service_level_agreement(doc, reason, user): }).insert(ignore_permissions=True) doc.service_level_agreement_creation = now_datetime(doc.get("owner")) - doc.set_response_and_resolution_time(priority=doc.priority, service_level_agreement=doc.service_level_agreement) - doc.agreement_status = "Ongoing" doc.save() -def reset_metrics(doc, meta): - if meta.has_field("resolution_date"): +def reset_resolution_metrics(doc): + if doc.meta.has_field("resolution_date"): doc.resolution_date = None - if not meta.has_field("resolution_time"): + if doc.meta.has_field("resolution_time"): doc.resolution_time = None - if not meta.has_field("user_resolution_time"): + if doc.meta.has_field("user_resolution_time"): doc.user_resolution_time = None - if meta.has_field("agreement_status"): - doc.agreement_status = "Ongoing" - - -def set_resolution_time(doc, meta): - # total time taken from issue creation to closing - if not meta.has_field("resolution_time"): - return - - doc.resolution_time = time_diff_in_seconds(doc.resolution_date, doc.creation) - # called via hooks on communication update -def update_hold_time(doc, status): +def on_communication_update(doc, status): + if doc.communication_type == "Comment": + return + parent = get_parent_doc(doc) if not parent: return - if doc.communication_type == "Comment": + if not parent.meta.has_field('service_level_agreement'): return - status_field = parent.meta.get_field("status") - if status_field: - options = (status_field.options or "").splitlines() + if ( + doc.sent_or_received == "Received" # a reply is received + and parent.get('status') == 'Open' # issue status is set as open from communication.py + and parent.get_doc_before_save() + and parent.get('status') != parent._doc_before_save.get('status') # status changed + ): + # undo the status change in db + # since prev status is fetched from db + frappe.db.set_value( + parent.doctype, parent.name, + 'status', parent._doc_before_save.get('status'), + update_modified=False + ) - # if status has a "Replied" option, then handle hold time - if ("Replied" in options) and doc.sent_or_received == "Received": - meta = frappe.get_meta(parent.doctype) - handle_hold_time(parent, meta, 'Replied') + elif ( + doc.sent_or_received == "Sent" # a reply is sent + and parent.get('first_responded_on') # first_responded_on is set from communication.py + and parent.get_doc_before_save() + and not parent._doc_before_save.get('first_responded_on') # first_responded_on was not set + ): + # reset first_responded_on since it will be handled/set later on + parent.first_responded_on = None + parent.flags.on_first_reply = True + + else: + return + + for_resolution = frappe.db.get_value('Service Level Agreement', parent.service_level_agreement, 'apply_sla_for_resolution') + + handle_status_change(parent, for_resolution) + update_response_and_resolution_metrics(parent, for_resolution) + update_agreement_status(parent, for_resolution) + + parent.save() -def handle_hold_time(doc, meta, status): - if meta.has_field("service_level_agreement") and doc.service_level_agreement: - # set response and resolution variance as None as the issue is on Hold for status as Replied - hold_statuses = [entry.status for entry in frappe.db.get_all("Pause SLA On Status", filters={ - "parent": doc.service_level_agreement - }, fields=["status"])] - - if not hold_statuses: - return - - if meta.has_field("status") and doc.status in hold_statuses and status not in hold_statuses: - apply_hold_status(doc, meta) - - # calculate hold time when status is changed from any hold status to any non-hold status - if meta.has_field("status") and doc.status not in hold_statuses and status in hold_statuses: - reset_hold_status_and_update_hold_time(doc, meta) +def reset_expected_response_and_resolution(doc): + if doc.meta.has_field("first_responded_on") and not doc.get('first_responded_on'): + doc.response_by = None + if doc.meta.has_field("resolution_by") and not doc.get('resolution_date'): + doc.resolution_by = None -def apply_hold_status(doc, meta): - update_values = {'on_hold_since': frappe.flags.current_time or now_datetime(doc.get("owner"))} - - if meta.has_field("first_responded_on") and not doc.first_responded_on: - update_values['response_by'] = None - update_values['response_by_variance'] = 0 - - update_values['resolution_by'] = None - update_values['resolution_by_variance'] = 0 - - doc.db_set(update_values) +def set_response_by(doc, start_date_time, priority): + if doc.meta.has_field("response_by"): + doc.response_by = get_expected_time_for(parameter="response", service_level=priority, start_date_time=start_date_time) + if doc.meta.has_field("total_hold_time") and doc.get('total_hold_time') and not doc.get('first_responded_on'): + doc.response_by = add_to_date(doc.response_by, seconds=round(doc.get('total_hold_time'))) -def reset_hold_status_and_update_hold_time(doc, meta): - hold_time = doc.total_hold_time if meta.has_field("total_hold_time") and doc.total_hold_time else 0 - now_time = frappe.flags.current_time or now_datetime(doc.get("owner")) - last_hold_time = 0 - update_values = {} +def set_resolution_by(doc, start_date_time, priority): + if doc.meta.has_field("resolution_by"): + doc.resolution_by = get_expected_time_for(parameter="resolution", service_level=priority, start_date_time=start_date_time) + if doc.meta.has_field("total_hold_time") and doc.get('total_hold_time'): + doc.resolution_by = add_to_date(doc.resolution_by, seconds=round(doc.get('total_hold_time'))) - if meta.has_field("on_hold_since") and doc.on_hold_since: - # last_hold_time will be added to the sla variables - last_hold_time = time_diff_in_seconds(now_time, doc.on_hold_since) - update_values['total_hold_time'] = hold_time + last_hold_time - # re-calculate SLA variables after issue changes from any hold status to any non-hold status - start_date_time = get_datetime(doc.get("service_level_agreement_creation") or doc.creation) - priority = get_priority(doc) - now_time = frappe.flags.current_time or now_datetime(doc.get("owner")) - - # add hold time to response by variance - if meta.has_field("first_responded_on") and not doc.first_responded_on: - response_by = get_expected_time_for(parameter="response", service_level=priority, start_date_time=start_date_time) - response_by = add_to_date(response_by, seconds=round(last_hold_time)) - response_by_variance = round(time_diff_in_seconds(response_by, now_time)) - - update_values['response_by'] = response_by - update_values['response_by_variance'] = response_by_variance + last_hold_time - - # add hold time to resolution by variance - if frappe.db.get_value("Service Level Agreement", doc.service_level_agreement, "apply_sla_for_resolution"): - resolution_by = get_expected_time_for(parameter="resolution", service_level=priority, start_date_time=start_date_time) - resolution_by = add_to_date(resolution_by, seconds=round(last_hold_time)) - resolution_by_variance = round(time_diff_in_seconds(resolution_by, now_time)) - - update_values['resolution_by'] = resolution_by - update_values['resolution_by_variance'] = resolution_by_variance + last_hold_time - - update_values['on_hold_since'] = None - - doc.db_set(update_values) +def record_assigned_users_on_failure(doc): + assigned_users = doc.get_assigned_users() + if assigned_users: + from frappe.utils import get_fullname + assigned_users = ', '.join((get_fullname(user) for user in assigned_users)) + message = _('First Response SLA Failed by {}').format(assigned_users) + doc.add_comment( + comment_type='Assigned', + text=message + ) def get_service_level_agreement_fields(): @@ -714,17 +732,11 @@ def get_service_level_agreement_fields(): "label": "Response By", "read_only": 1 }, - { - "fieldname": "response_by_variance", - "fieldtype": "Duration", - "hide_seconds": 1, - "label": "Response By Variance", - "read_only": 1 - }, { "fieldname": "first_responded_on", "fieldtype": "Datetime", "label": "First Responded On", + "no_copy": 1, "read_only": 1 }, { @@ -746,11 +758,11 @@ def get_service_level_agreement_fields(): "read_only": 1 }, { - "default": "Ongoing", + "default": "First Response Due", "fieldname": "agreement_status", "fieldtype": "Select", "label": "Service Level Agreement Status", - "options": "Ongoing\nFulfilled\nFailed", + "options": "First Response Due\nResolution Due\nFulfilled\nFailed", "read_only": 1 }, { @@ -759,13 +771,6 @@ def get_service_level_agreement_fields(): "label": "Resolution By", "read_only": 1 }, - { - "fieldname": "resolution_by_variance", - "fieldtype": "Duration", - "hide_seconds": 1, - "label": "Resolution By Variance", - "read_only": 1 - }, { "fieldname": "service_level_agreement_creation", "fieldtype": "Datetime", @@ -786,43 +791,28 @@ def get_service_level_agreement_fields(): def update_agreement_status_on_custom_status(doc): # Update Agreement Fulfilled status using Custom Scripts for Custom Status - - meta = frappe.get_meta(doc.doctype) - now_time = frappe.flags.current_time or now_datetime(doc.get("owner")) - if meta.has_field("first_responded_on") and not doc.first_responded_on: - # first_responded_on set when first reply is sent to customer - doc.response_by_variance = round(time_diff_in_seconds(doc.response_by, now_time), 2) - - if meta.has_field("resolution_date") and not doc.resolution_date: - # resolution_date set when issue has been closed - doc.resolution_by_variance = round(time_diff_in_seconds(doc.resolution_by, now_time), 2) - - if meta.has_field("agreement_status"): - doc.agreement_status = "Fulfilled" if doc.response_by_variance > 0 and doc.resolution_by_variance > 0 else "Failed" + update_agreement_status(doc) -def update_agreement_status(doc, meta): - if meta.has_field("service_level_agreement") and meta.has_field("agreement_status") and \ - doc.service_level_agreement and doc.agreement_status == "Ongoing": - - apply_sla_for_resolution = frappe.db.get_value("Service Level Agreement", doc.service_level_agreement, - "apply_sla_for_resolution") - +def update_agreement_status(doc, apply_sla_for_resolution): + if (doc.meta.has_field("agreement_status")): # if SLA is applied for resolution check for response and resolution, else only response if apply_sla_for_resolution: - if meta.has_field("response_by_variance") and meta.has_field("resolution_by_variance"): - if cint(frappe.db.get_value(doc.doctype, doc.name, "response_by_variance")) < 0 or \ - cint(frappe.db.get_value(doc.doctype, doc.name, "resolution_by_variance")) < 0: - - doc.agreement_status = "Failed" - else: - doc.agreement_status = "Fulfilled" - else: - if meta.has_field("response_by_variance") and \ - cint(frappe.db.get_value(doc.doctype, doc.name, "response_by_variance")) < 0: - doc.agreement_status = "Failed" - else: + if doc.meta.has_field("first_responded_on") and not doc.get('first_responded_on'): + doc.agreement_status = "First Response Due" + elif doc.meta.has_field("resolution_date") and not doc.get('resolution_date'): + doc.agreement_status = "Resolution Due" + elif get_datetime(doc.get('resolution_date')) <= get_datetime(doc.get('resolution_by')): doc.agreement_status = "Fulfilled" + else: + doc.agreement_status = "Failed" + else: + if doc.meta.has_field("first_responded_on") and not doc.get('first_responded_on'): + doc.agreement_status = "First Response Due" + elif get_datetime(doc.get('first_responded_on')) <= get_datetime(doc.get('response_by')): + doc.agreement_status = "Fulfilled" + else: + doc.agreement_status = "Failed" def is_holiday(date, holidays): @@ -835,23 +825,6 @@ def get_time_in_timedelta(time): return datetime.timedelta(hours=time.hour, minutes=time.minute, seconds=time.second) -def set_response_by_and_variance(doc, meta, start_date_time, priority): - if meta.has_field("response_by"): - doc.response_by = get_expected_time_for(parameter="response", service_level=priority, start_date_time=start_date_time) - - if meta.has_field("response_by_variance") and not doc.get('first_responded_on'): - now_time = frappe.flags.current_time or now_datetime(doc.get("owner")) - doc.response_by_variance = round(time_diff_in_seconds(doc.response_by, now_time), 2) - -def set_resolution_by_and_variance(doc, meta, start_date_time, priority): - if meta.has_field("resolution_by"): - doc.resolution_by = get_expected_time_for(parameter="resolution", service_level=priority, start_date_time=start_date_time) - - if meta.has_field("resolution_by_variance") and not doc.get("resolution_date"): - now_time = frappe.flags.current_time or now_datetime(doc.get("owner")) - doc.resolution_by_variance = round(time_diff_in_seconds(doc.resolution_by, now_time), 2) - - def now_datetime(user): dt = convert_utc_to_user_timezone(datetime.utcnow(), user) return dt.replace(tzinfo=None) diff --git a/erpnext/support/doctype/service_level_agreement/test_service_level_agreement.py b/erpnext/support/doctype/service_level_agreement/test_service_level_agreement.py index cfbe7446c0b..b07c862c7b0 100644 --- a/erpnext/support/doctype/service_level_agreement/test_service_level_agreement.py +++ b/erpnext/support/doctype/service_level_agreement/test_service_level_agreement.py @@ -220,42 +220,6 @@ class TestServiceLevelAgreement(unittest.TestCase): lead.reload() self.assertEqual(lead.agreement_status, 'Fulfilled') - def test_changing_of_variance_after_response(self): - # create lead - doctype = "Lead" - lead_sla = create_service_level_agreement( - default_service_level_agreement=1, - holiday_list="__Test Holiday List", - entity_type=None, entity=None, - response_time=14400, - doctype=doctype, - sla_fulfilled_on=[{"status": "Replied"}], - apply_sla_for_resolution=0 - ) - creation = datetime.datetime(2019, 3, 4, 12, 0) - lead = make_lead(creation=creation, index=2) - self.assertEqual(lead.service_level_agreement, lead_sla.name) - - # set lead as replied to set first responded on - frappe.flags.current_time = datetime.datetime(2019, 3, 4, 15, 30) - lead.reload() - lead.status = 'Replied' - lead.save() - lead.reload() - self.assertEqual(lead.agreement_status, 'Fulfilled') - - # check response_by_variance - self.assertEqual(lead.first_responded_on, frappe.flags.current_time) - self.assertEqual(lead.response_by_variance, 1800.0) - - # make a change on the document & - # check response_by_variance is unchanged - frappe.flags.current_time = datetime.datetime(2019, 3, 4, 18, 30) - lead.status = 'Open' - lead.save() - lead.reload() - self.assertEqual(lead.response_by_variance, 1800.0) - def test_service_level_agreement_filters(self): doctype = "Lead" lead_sla = create_service_level_agreement( @@ -295,7 +259,8 @@ def get_service_level_agreement(default_service_level_agreement=None, entity_typ return service_level_agreement def create_service_level_agreement(default_service_level_agreement, holiday_list, response_time, entity_type, - entity, resolution_time=0, doctype="Issue", condition="", sla_fulfilled_on=[], pause_sla_on=[], apply_sla_for_resolution=1): + entity, resolution_time=0, doctype="Issue", condition="", sla_fulfilled_on=[], pause_sla_on=[], apply_sla_for_resolution=1, + service_level=None, start_time="10:00:00", end_time="18:00:00"): make_holiday_list() make_priorities() @@ -312,7 +277,7 @@ def create_service_level_agreement(default_service_level_agreement, holiday_list "doctype": "Service Level Agreement", "enabled": 1, "document_type": doctype, - "service_level": "__Test {} SLA".format(entity_type if entity_type else "Default"), + "service_level": service_level or "__Test {} SLA".format(entity_type if entity_type else "Default"), "default_service_level_agreement": default_service_level_agreement, "condition": condition, "default_priority": "Medium", @@ -345,28 +310,28 @@ def create_service_level_agreement(default_service_level_agreement, holiday_list "support_and_resolution": [ { "workday": "Monday", - "start_time": "10:00:00", - "end_time": "18:00:00", + "start_time": start_time, + "end_time": end_time, }, { "workday": "Tuesday", - "start_time": "10:00:00", - "end_time": "18:00:00", + "start_time": start_time, + "end_time": end_time, }, { "workday": "Wednesday", - "start_time": "10:00:00", - "end_time": "18:00:00", + "start_time": start_time, + "end_time": end_time, }, { "workday": "Thursday", - "start_time": "10:00:00", - "end_time": "18:00:00", + "start_time": start_time, + "end_time": end_time, }, { "workday": "Friday", - "start_time": "10:00:00", - "end_time": "18:00:00", + "start_time": start_time, + "end_time": end_time, } ] }) @@ -386,7 +351,7 @@ def create_service_level_agreement(default_service_level_agreement, holiday_list if sla: frappe.delete_doc("Service Level Agreement", sla, force=1) - return frappe.get_doc(service_level_agreement).insert(ignore_permissions=True) + return frappe.get_doc(service_level_agreement).insert(ignore_permissions=True, ignore_if_duplicate=True) def create_customer(): @@ -443,6 +408,13 @@ def create_service_level_agreements_for_issues(): create_service_level_agreement(default_service_level_agreement=0, holiday_list="__Test Holiday List", entity_type="Territory", entity="_Test SLA Territory", response_time=7200, resolution_time=10800) + create_service_level_agreement( + default_service_level_agreement=0, holiday_list="__Test Holiday List", + entity_type=None, entity=None, response_time=14400, resolution_time=21600, + service_level="24-hour-SLA", start_time="00:00:00", end_time="23:59:59", + condition="doc.issue_type == 'Critical'" + ) + def make_holiday_list(): holiday_list = frappe.db.exists("Holiday List", "__Test Holiday List") if not holiday_list: diff --git a/erpnext/support/report/issue_summary/issue_summary.py b/erpnext/support/report/issue_summary/issue_summary.py index 39a5c407cd4..67fe345d5fe 100644 --- a/erpnext/support/report/issue_summary/issue_summary.py +++ b/erpnext/support/report/issue_summary/issue_summary.py @@ -82,7 +82,8 @@ class IssueSummary(object): self.sla_status_map = { 'SLA Failed': 'failed', 'SLA Fulfilled': 'fulfilled', - 'SLA Ongoing': 'ongoing' + 'First Response Due': 'first_response_due', + 'Resolution Due': 'resolution_due' } for label, fieldname in self.sla_status_map.items(): diff --git a/erpnext/tests/test_init.py b/erpnext/tests/test_init.py index 36a9bf5e37b..61849726efe 100644 --- a/erpnext/tests/test_init.py +++ b/erpnext/tests/test_init.py @@ -8,13 +8,8 @@ test_records = frappe.get_test_records('Company') class TestInit(unittest.TestCase): def test_encode_company_abbr(self): - company = frappe.new_doc("Company") - company.company_name = "New from Existing Company For Test" - company.abbr = "NFECT" - company.default_currency = "INR" - company.save() - abbr = company.abbr + abbr = "NFECT" names = [ "Warehouse Name", "ERPNext Foundation India", "Gold - Member - {a}".format(a=abbr), @@ -32,7 +27,7 @@ class TestInit(unittest.TestCase): ] for i in range(len(names)): - enc_name = encode_company_abbr(names[i], company.name) + enc_name = encode_company_abbr(names[i], abbr=abbr) self.assertTrue( enc_name == expected_names[i], "{enc} is not same as {exp}".format(enc=enc_name, exp=expected_names[i]) diff --git a/erpnext/tests/utils.py b/erpnext/tests/utils.py index 91df5480e35..fbf25948a79 100644 --- a/erpnext/tests/utils.py +++ b/erpnext/tests/utils.py @@ -2,6 +2,7 @@ # License: GNU General Public License v3. See license.txt import copy +import signal import unittest from contextlib import contextmanager from typing import Any, Dict, NewType, Optional @@ -135,3 +136,23 @@ def execute_script_report( report_execute_fn(filter_with_optional_param) return report_data + + +def timeout(seconds=30, error_message="Test timed out."): + """ Timeout decorator to ensure a test doesn't run for too long. + + adapted from https://stackoverflow.com/a/2282656""" + def decorator(func): + def _handle_timeout(signum, frame): + raise Exception(error_message) + + def wrapper(*args, **kwargs): + signal.signal(signal.SIGALRM, _handle_timeout) + signal.alarm(seconds) + try: + result = func(*args, **kwargs) + finally: + signal.alarm(0) + return result + return wrapper + return decorator diff --git a/erpnext/translations/de.csv b/erpnext/translations/de.csv index ca03a787cd1..d46ffb56096 100644 --- a/erpnext/translations/de.csv +++ b/erpnext/translations/de.csv @@ -1847,7 +1847,7 @@ Overdue,Überfällig, Overlap in scoring between {0} and {1},Überlappung beim Scoring zwischen {0} und {1}, Overlapping conditions found between:,Überlagernde Bedingungen gefunden zwischen:, Owner,Besitzer, -PAN,PFANNE, +PAN,PAN, POS,Verkaufsstelle, POS Profile,Verkaufsstellen-Profil, POS Profile is required to use Point-of-Sale,"POS-Profil ist erforderlich, um Point-of-Sale zu verwenden", diff --git a/erpnext/utilities/transaction_base.py b/erpnext/utilities/transaction_base.py index 14b3afa5a72..1d8b3a8db67 100644 --- a/erpnext/utilities/transaction_base.py +++ b/erpnext/utilities/transaction_base.py @@ -162,6 +162,28 @@ class TransactionBase(StatusUpdater): return ret + def reset_default_field_value(self, default_field: str, child_table: str, child_table_field: str): + """ Reset "Set default X" fields on forms to avoid confusion. + + example: + doc = { + "set_from_warehouse": "Warehouse A", + "items": [{"from_warehouse": "warehouse B"}, {"from_warehouse": "warehouse A"}], + } + Since this has dissimilar values in child table, the default field will be erased. + + doc.reset_default_field_value("set_from_warehouse", "items", "from_warehouse") + """ + child_table_values = set() + + for row in self.get(child_table): + child_table_values.add(row.get(child_table_field)) + + if len(child_table_values) > 1: + self.set(default_field, None) + else: + self.set(default_field, list(child_table_values)[0]) + def delete_events(ref_type, ref_name): events = frappe.db.sql_list(""" SELECT distinct `tabEvent`.name