diff --git a/.flake8 b/.flake8
index 56c9b9a3699..5735456ae7d 100644
--- a/.flake8
+++ b/.flake8
@@ -28,6 +28,7 @@ ignore =
B007,
B950,
W191,
+ E124, # closing bracket, irritating while writing QB code
max-line-length = 200
exclude=.github/helper/semgrep_rules
diff --git a/.github/workflows/patch.yml b/.github/workflows/patch.yml
index 8bb44555206..8d29057b487 100644
--- a/.github/workflows/patch.yml
+++ b/.github/workflows/patch.yml
@@ -5,9 +5,14 @@ on:
paths-ignore:
- '**.js'
- '**.md'
+ types: [opened, unlabeled, synchronize, reopened]
workflow_dispatch:
+concurrency:
+ group: patch-mariadb-v13-${{ github.event.number }}
+ cancel-in-progress: true
+
jobs:
test:
runs-on: ubuntu-18.04
@@ -25,6 +30,11 @@ jobs:
options: --health-cmd="mysqladmin ping" --health-interval=5s --health-timeout=2s --health-retries=3
steps:
+ - name: Check for merge conficts label
+ if: ${{ contains(github.event.pull_request.labels.*.name, 'conflicts') }}
+ run: |
+ echo "Remove merge conflicts and remove conflict label to run CI"
+ exit 1
- name: Clone
uses: actions/checkout@v2
diff --git a/.github/workflows/server-tests.yml b/.github/workflows/server-tests.yml
index 6d7324d623b..1c9743c5700 100644
--- a/.github/workflows/server-tests.yml
+++ b/.github/workflows/server-tests.yml
@@ -5,6 +5,7 @@ on:
paths-ignore:
- '**.js'
- '**.md'
+ types: [opened, unlabeled, synchronize, reopened]
workflow_dispatch:
push:
branches: [ develop ]
@@ -12,6 +13,10 @@ on:
- '**.js'
- '**.md'
+concurrency:
+ group: server-mariadb-v13-${{ github.event.number }}
+ cancel-in-progress: true
+
jobs:
test:
runs-on: ubuntu-18.04
@@ -35,6 +40,12 @@ jobs:
options: --health-cmd="mysqladmin ping" --health-interval=5s --health-timeout=2s --health-retries=3
steps:
+ - name: Check for merge conficts label
+ if: ${{ contains(github.event.pull_request.labels.*.name, 'conflicts') }}
+ run: |
+ echo "Remove merge conflicts and remove conflict label to run CI"
+ exit 1
+
- name: Clone
uses: actions/checkout@v2
@@ -89,39 +100,8 @@ jobs:
run: bash ${GITHUB_WORKSPACE}/.github/helper/install.sh
- name: Run Tests
- run: cd ~/frappe-bench/ && bench --site test_site run-parallel-tests --app erpnext --use-orchestrator --with-coverage
+ run: cd ~/frappe-bench/ && bench --site test_site run-parallel-tests --app erpnext --use-orchestrator
env:
TYPE: server
CI_BUILD_ID: ${{ github.run_id }}
ORCHESTRATOR_URL: http://test-orchestrator.frappe.io
-
- - name: Upload Coverage Data
- run: |
- cp ~/frappe-bench/sites/.coverage ${GITHUB_WORKSPACE}
- cd ${GITHUB_WORKSPACE}
- pip3 install coverage==5.5
- pip3 install coveralls==3.0.1
- coveralls
- env:
- GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- COVERALLS_FLAG_NAME: run-${{ matrix.container }}
- COVERALLS_SERVICE_NAME: ${{ github.event_name == 'pull_request' && 'github' || 'github-actions' }}
- COVERALLS_PARALLEL: true
-
- coveralls:
- name: Coverage Wrap Up
- needs: test
- container: python:3-slim
- runs-on: ubuntu-18.04
- steps:
- - name: Clone
- uses: actions/checkout@v2
-
- - name: Coveralls Finished
- run: |
- cd ${GITHUB_WORKSPACE}
- pip3 install coverage==5.5
- pip3 install coveralls==3.0.1
- coveralls --finish
- env:
- GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
diff --git a/.github/workflows/ui-tests.yml b/.github/workflows/ui-tests.yml
index 5459e86123d..9f142bd2c2f 100644
--- a/.github/workflows/ui-tests.yml
+++ b/.github/workflows/ui-tests.yml
@@ -6,6 +6,10 @@ on:
- '**.md'
workflow_dispatch:
+concurrency:
+ group: ui-v13-${{ github.event.number }}
+ cancel-in-progress: true
+
jobs:
test:
runs-on: ubuntu-18.04
diff --git a/erpnext/__init__.py b/erpnext/__init__.py
index c1369aaf8b9..10299871af6 100644
--- a/erpnext/__init__.py
+++ b/erpnext/__init__.py
@@ -5,7 +5,7 @@ import frappe
from erpnext.hooks import regional_overrides
-__version__ = '13.19.0'
+__version__ = '13.20.0'
def get_default_company(user=None):
'''Get default company for user'''
diff --git a/erpnext/accounts/doctype/opening_invoice_creation_tool/opening_invoice_creation_tool.py b/erpnext/accounts/doctype/opening_invoice_creation_tool/opening_invoice_creation_tool.py
index a8d7bf7a0e7..00eecd3a4f4 100644
--- a/erpnext/accounts/doctype/opening_invoice_creation_tool/opening_invoice_creation_tool.py
+++ b/erpnext/accounts/doctype/opening_invoice_creation_tool/opening_invoice_creation_tool.py
@@ -135,7 +135,7 @@ class OpeningInvoiceCreationTool(Document):
default_uom = frappe.db.get_single_value("Stock Settings", "stock_uom") or _("Nos")
rate = flt(row.outstanding_amount) / flt(row.qty)
- return frappe._dict({
+ item_dict = frappe._dict({
"uom": default_uom,
"rate": rate or 0.0,
"qty": row.qty,
@@ -146,6 +146,13 @@ class OpeningInvoiceCreationTool(Document):
"cost_center": cost_center
})
+ for dimension in get_accounting_dimensions():
+ item_dict.update({
+ dimension: row.get(dimension)
+ })
+
+ return item_dict
+
item = get_item_dict()
invoice = frappe._dict({
@@ -166,7 +173,7 @@ class OpeningInvoiceCreationTool(Document):
accounting_dimension = get_accounting_dimensions()
for dimension in accounting_dimension:
invoice.update({
- dimension: item.get(dimension)
+ dimension: self.get(dimension) or item.get(dimension)
})
return invoice
diff --git a/erpnext/accounts/doctype/opening_invoice_creation_tool/test_opening_invoice_creation_tool.py b/erpnext/accounts/doctype/opening_invoice_creation_tool/test_opening_invoice_creation_tool.py
index b5aae9845b6..6700e9b975d 100644
--- a/erpnext/accounts/doctype/opening_invoice_creation_tool/test_opening_invoice_creation_tool.py
+++ b/erpnext/accounts/doctype/opening_invoice_creation_tool/test_opening_invoice_creation_tool.py
@@ -7,21 +7,26 @@ import frappe
from frappe.cache_manager import clear_doctype_cache
from frappe.custom.doctype.property_setter.property_setter import make_property_setter
+from erpnext.accounts.doctype.accounting_dimension.test_accounting_dimension import (
+ create_dimension,
+ disable_dimension,
+)
from erpnext.accounts.doctype.opening_invoice_creation_tool.opening_invoice_creation_tool import (
get_temporary_opening_account,
)
-test_dependencies = ["Customer", "Supplier"]
+test_dependencies = ["Customer", "Supplier", "Accounting Dimension"]
class TestOpeningInvoiceCreationTool(unittest.TestCase):
def setUp(self):
if not frappe.db.exists("Company", "_Test Opening Invoice Company"):
make_company()
+ create_dimension()
- def make_invoices(self, invoice_type="Sales", company=None, party_1=None, party_2=None, invoice_number=None):
+ def make_invoices(self, invoice_type="Sales", company=None, party_1=None, party_2=None, invoice_number=None, department=None):
doc = frappe.get_single("Opening Invoice Creation Tool")
args = get_opening_invoice_creation_dict(invoice_type=invoice_type, company=company,
- party_1=party_1, party_2=party_2, invoice_number=invoice_number)
+ party_1=party_1, party_2=party_2, invoice_number=invoice_number, department=department)
doc.update(args)
return doc.make_invoices()
@@ -106,6 +111,19 @@ class TestOpeningInvoiceCreationTool(unittest.TestCase):
doc = frappe.get_doc('Sales Invoice', inv)
doc.cancel()
+ def test_opening_invoice_with_accounting_dimension(self):
+ invoices = self.make_invoices(invoice_type="Sales", company="_Test Opening Invoice Company", department='Sales - _TOIC')
+
+ expected_value = {
+ "keys": ["customer", "outstanding_amount", "status", "department"],
+ 0: ["_Test Customer", 300, "Overdue", "Sales - _TOIC"],
+ 1: ["_Test Customer 1", 250, "Overdue", "Sales - _TOIC"],
+ }
+ self.check_expected_values(invoices, expected_value, invoice_type="Sales")
+
+ def tearDown(self):
+ disable_dimension()
+
def get_opening_invoice_creation_dict(**args):
party = "Customer" if args.get("invoice_type", "Sales") == "Sales" else "Supplier"
company = args.get("company", "_Test Company")
diff --git a/erpnext/accounts/doctype/pricing_rule/test_pricing_rule.py b/erpnext/accounts/doctype/pricing_rule/test_pricing_rule.py
index e9a018ec46c..6571e1674c2 100644
--- a/erpnext/accounts/doctype/pricing_rule/test_pricing_rule.py
+++ b/erpnext/accounts/doctype/pricing_rule/test_pricing_rule.py
@@ -630,6 +630,26 @@ class TestPricingRule(unittest.TestCase):
for doc in [si, si1]:
doc.delete()
+ def test_multiple_pricing_rules_with_min_qty(self):
+ make_pricing_rule(discount_percentage=20, selling=1, priority=1, min_qty=4,
+ apply_multiple_pricing_rules=1, title="_Test Pricing Rule with Min Qty - 1")
+ make_pricing_rule(discount_percentage=10, selling=1, priority=2, min_qty=4,
+ apply_multiple_pricing_rules=1, title="_Test Pricing Rule with Min Qty - 2")
+
+ si = create_sales_invoice(do_not_submit=True, customer="_Test Customer 1", qty=1, currency="USD")
+ item = si.items[0]
+ item.stock_qty = 1
+ si.save()
+ self.assertFalse(item.discount_percentage)
+ item.qty = 5
+ item.stock_qty = 5
+ si.save()
+ self.assertEqual(item.discount_percentage, 30)
+ si.delete()
+
+ frappe.delete_doc_if_exists("Pricing Rule", "_Test Pricing Rule with Min Qty - 1")
+ frappe.delete_doc_if_exists("Pricing Rule", "_Test Pricing Rule with Min Qty - 2")
+
test_dependencies = ["Campaign"]
def make_pricing_rule(**args):
diff --git a/erpnext/accounts/doctype/pricing_rule/utils.py b/erpnext/accounts/doctype/pricing_rule/utils.py
index 02bfc9defd7..7792590c9c7 100644
--- a/erpnext/accounts/doctype/pricing_rule/utils.py
+++ b/erpnext/accounts/doctype/pricing_rule/utils.py
@@ -73,7 +73,7 @@ def sorted_by_priority(pricing_rules, args, doc=None):
for key in sorted(pricing_rule_dict):
pricing_rules_list.extend(pricing_rule_dict.get(key))
- return pricing_rules_list or pricing_rules
+ return pricing_rules_list
def filter_pricing_rule_based_on_condition(pricing_rules, doc=None):
filtered_pricing_rules = []
diff --git a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py
index c5cc05749a7..165f6000ce7 100644
--- a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py
+++ b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py
@@ -543,6 +543,11 @@ class PurchaseInvoice(BuyingController):
if d.category in ('Valuation', 'Total and Valuation')
and flt(d.base_tax_amount_after_discount_amount)]
+ provisional_accounting_for_non_stock_items = cint(frappe.db.get_value('Company', self.company,
+ 'enable_provisional_accounting_for_non_stock_items'))
+
+ purchase_receipt_doc_map = {}
+
for item in self.get("items"):
if flt(item.base_net_amount):
account_currency = get_account_currency(item.expense_account)
@@ -637,19 +642,23 @@ class PurchaseInvoice(BuyingController):
else:
amount = flt(item.base_net_amount + item.item_tax_amount, item.precision("base_net_amount"))
- auto_accounting_for_non_stock_items = cint(frappe.db.get_value('Company', self.company, 'enable_perpetual_inventory_for_non_stock_items'))
-
- if auto_accounting_for_non_stock_items:
- service_received_but_not_billed_account = self.get_company_default("service_received_but_not_billed")
-
+ if provisional_accounting_for_non_stock_items:
if item.purchase_receipt:
+ provisional_account = self.get_company_default("default_provisional_account")
+ purchase_receipt_doc = purchase_receipt_doc_map.get(item.purchase_receipt)
+
+ if not purchase_receipt_doc:
+ purchase_receipt_doc = frappe.get_doc("Purchase Receipt", item.purchase_receipt)
+ purchase_receipt_doc_map[item.purchase_receipt] = purchase_receipt_doc
+
# Post reverse entry for Stock-Received-But-Not-Billed if it is booked in Purchase Receipt
expense_booked_in_pr = frappe.db.get_value('GL Entry', {'is_cancelled': 0,
'voucher_type': 'Purchase Receipt', 'voucher_no': item.purchase_receipt, 'voucher_detail_no': item.pr_detail,
- 'account':service_received_but_not_billed_account}, ['name'])
+ 'account':provisional_account}, ['name'])
if expense_booked_in_pr:
- expense_account = service_received_but_not_billed_account
+ # Intentionally passing purchase invoice item to handle partial billing
+ purchase_receipt_doc.add_provisional_gl_entry(item, gl_entries, self.posting_date, reverse=1)
if not self.is_internal_transfer():
gl_entries.append(self.get_gl_dict({
diff --git a/erpnext/accounts/doctype/purchase_invoice/test_purchase_invoice.py b/erpnext/accounts/doctype/purchase_invoice/test_purchase_invoice.py
index d01baebe636..1d2dcdf2776 100644
--- a/erpnext/accounts/doctype/purchase_invoice/test_purchase_invoice.py
+++ b/erpnext/accounts/doctype/purchase_invoice/test_purchase_invoice.py
@@ -11,12 +11,17 @@ from frappe.utils import add_days, cint, flt, getdate, nowdate, today
import erpnext
from erpnext.accounts.doctype.account.test_account import create_account, get_inventory_account
from erpnext.accounts.doctype.payment_entry.payment_entry import get_payment_entry
+from erpnext.buying.doctype.purchase_order.purchase_order import get_mapped_purchase_invoice
+from erpnext.buying.doctype.purchase_order.test_purchase_order import create_purchase_order
from erpnext.buying.doctype.supplier.test_supplier import create_supplier
from erpnext.controllers.accounts_controller import get_payment_terms
from erpnext.controllers.buying_controller import QtyMismatchError
from erpnext.exceptions import InvalidCurrency
from erpnext.projects.doctype.project.test_project import make_project
from erpnext.stock.doctype.item.test_item import create_item
+from erpnext.stock.doctype.purchase_receipt.purchase_receipt import (
+ make_purchase_invoice as create_purchase_invoice_from_receipt,
+)
from erpnext.stock.doctype.purchase_receipt.test_purchase_receipt import (
get_taxes,
make_purchase_receipt,
@@ -1124,8 +1129,6 @@ class TestPurchaseInvoice(unittest.TestCase):
def test_purchase_invoice_advance_taxes(self):
from erpnext.accounts.doctype.payment_entry.payment_entry import get_payment_entry
- from erpnext.buying.doctype.purchase_order.purchase_order import get_mapped_purchase_invoice
- from erpnext.buying.doctype.purchase_order.test_purchase_order import create_purchase_order
# create a new supplier to test
supplier = create_supplier(supplier_name = '_Test TDS Advance Supplier',
@@ -1198,6 +1201,45 @@ class TestPurchaseInvoice(unittest.TestCase):
payment_entry.load_from_db()
self.assertEqual(payment_entry.taxes[0].allocated_amount, 0)
+ def test_provisional_accounting_entry(self):
+ item = create_item("_Test Non Stock Item", is_stock_item=0)
+ provisional_account = create_account(account_name="Provision Account",
+ parent_account="Current Liabilities - _TC", company="_Test Company")
+
+ company = frappe.get_doc('Company', '_Test Company')
+ company.enable_provisional_accounting_for_non_stock_items = 1
+ company.default_provisional_account = provisional_account
+ company.save()
+
+ pr = make_purchase_receipt(item_code="_Test Non Stock Item", posting_date=add_days(nowdate(), -2))
+
+ pi = create_purchase_invoice_from_receipt(pr.name)
+ pi.set_posting_time = 1
+ pi.posting_date = add_days(pr.posting_date, -1)
+ pi.items[0].expense_account = 'Cost of Goods Sold - _TC'
+ pi.save()
+ pi.submit()
+
+ # Check GLE for Purchase Invoice
+ expected_gle = [
+ ['Cost of Goods Sold - _TC', 250, 0, add_days(pr.posting_date, -1)],
+ ['Creditors - _TC', 0, 250, add_days(pr.posting_date, -1)]
+ ]
+
+ check_gl_entries(self, pi.name, expected_gle, pi.posting_date)
+
+ expected_gle_for_purchase_receipt = [
+ ["Provision Account - _TC", 250, 0, pr.posting_date],
+ ["_Test Account Cost for Goods Sold - _TC", 0, 250, pr.posting_date],
+ ["Provision Account - _TC", 0, 250, pi.posting_date],
+ ["_Test Account Cost for Goods Sold - _TC", 250, 0, pi.posting_date]
+ ]
+
+ check_gl_entries(self, pr.name, expected_gle_for_purchase_receipt, pr.posting_date)
+
+ company.enable_provisional_accounting_for_non_stock_items = 0
+ company.save()
+
def check_gl_entries(doc, voucher_no, expected_gle, posting_date):
gl_entries = frappe.db.sql("""select account, debit, credit, posting_date
from `tabGL Entry`
diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py
index 772e8c4e872..9077ee73b3a 100644
--- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py
+++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py
@@ -45,6 +45,7 @@ from erpnext.setup.doctype.company.company import update_company_current_month_s
from erpnext.stock.doctype.batch.batch import set_batch_nos
from erpnext.stock.doctype.delivery_note.delivery_note import update_billed_amount_based_on_so
from erpnext.stock.doctype.serial_no.serial_no import get_delivery_note_serial_no, get_serial_nos
+from erpnext.stock.utils import calculate_mapped_packed_items_return
form_grid_templates = {
"items": "templates/form_grid/item_grid.html"
@@ -745,8 +746,11 @@ class SalesInvoice(SellingController):
def update_packing_list(self):
if cint(self.update_stock) == 1:
- from erpnext.stock.doctype.packed_item.packed_item import make_packing_list
- make_packing_list(self)
+ if cint(self.is_return) and self.return_against:
+ calculate_mapped_packed_items_return(self)
+ else:
+ from erpnext.stock.doctype.packed_item.packed_item import make_packing_list
+ make_packing_list(self)
else:
self.set('packed_items', [])
diff --git a/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py b/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py
index 3d70a95ad9a..99a6ade8f4a 100644
--- a/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py
+++ b/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py
@@ -2177,9 +2177,9 @@ class TestSalesInvoice(unittest.TestCase):
asset.load_from_db()
expected_values = [
- ["2020-06-30", 1311.48, 1311.48],
- ["2021-06-30", 20000.0, 21311.48],
- ["2021-09-30", 5041.1, 26352.58]
+ ["2020-06-30", 1366.12, 1366.12],
+ ["2021-06-30", 20000.0, 21366.12],
+ ["2021-09-30", 5041.1, 26407.22]
]
for i, schedule in enumerate(asset.schedules):
@@ -2227,12 +2227,12 @@ class TestSalesInvoice(unittest.TestCase):
asset.load_from_db()
expected_values = [
- ["2020-06-30", 1311.48, 1311.48, True],
- ["2021-06-30", 20000.0, 21311.48, True],
- ["2022-06-30", 20000.0, 41311.48, False],
- ["2023-06-30", 20000.0, 61311.48, False],
- ["2024-06-30", 20000.0, 81311.48, False],
- ["2025-06-06", 18688.52, 100000.0, False]
+ ["2020-06-30", 1366.12, 1366.12, True],
+ ["2021-06-30", 20000.0, 21366.12, True],
+ ["2022-06-30", 20000.0, 41366.12, False],
+ ["2023-06-30", 20000.0, 61366.12, False],
+ ["2024-06-30", 20000.0, 81366.12, False],
+ ["2025-06-06", 18633.88, 100000.0, False]
]
for i, schedule in enumerate(asset.schedules):
diff --git a/erpnext/accounts/doctype/shipping_rule/shipping_rule.py b/erpnext/accounts/doctype/shipping_rule/shipping_rule.py
index 7e5129911e4..792e7d21a78 100644
--- a/erpnext/accounts/doctype/shipping_rule/shipping_rule.py
+++ b/erpnext/accounts/doctype/shipping_rule/shipping_rule.py
@@ -71,7 +71,8 @@ class ShippingRule(Document):
if doc.currency != doc.company_currency:
shipping_amount = flt(shipping_amount / doc.conversion_rate, 2)
- self.add_shipping_rule_to_tax_table(doc, shipping_amount)
+ if shipping_amount:
+ self.add_shipping_rule_to_tax_table(doc, shipping_amount)
def get_shipping_amount_from_rules(self, value):
for condition in self.get("conditions"):
diff --git a/erpnext/accounts/party.py b/erpnext/accounts/party.py
index a1c34a87ba8..4f4f180e542 100644
--- a/erpnext/accounts/party.py
+++ b/erpnext/accounts/party.py
@@ -59,7 +59,7 @@ def _get_party_details(party=None, account=None, party_type="Customer", company=
frappe.throw(_("Not permitted for {0}").format(party), frappe.PermissionError)
party = frappe.get_doc(party_type, party)
- currency = party.default_currency if party.get("default_currency") else get_company_currency(company)
+ currency = party.get("default_currency") or currency or get_company_currency(company)
party_address, shipping_address = set_address_details(party_details, party, party_type, doctype, company, party_address, company_address, shipping_address)
set_contact_details(party_details, party, party_type)
diff --git a/erpnext/accounts/report/deferred_revenue_and_expense/test_deferred_revenue_and_expense.py b/erpnext/accounts/report/deferred_revenue_and_expense/test_deferred_revenue_and_expense.py
index 1de6fb68241..86eb2134fe8 100644
--- a/erpnext/accounts/report/deferred_revenue_and_expense/test_deferred_revenue_and_expense.py
+++ b/erpnext/accounts/report/deferred_revenue_and_expense/test_deferred_revenue_and_expense.py
@@ -17,10 +17,42 @@ from erpnext.stock.doctype.item.test_item import create_item
class TestDeferredRevenueAndExpense(unittest.TestCase):
@classmethod
def setUpClass(self):
- clear_old_entries()
+ clear_accounts_and_items()
create_company()
+ self.maxDiff = None
+
+ def clear_old_entries(self):
+ sinv = qb.DocType("Sales Invoice")
+ sinv_item = qb.DocType("Sales Invoice Item")
+ pinv = qb.DocType("Purchase Invoice")
+ pinv_item = qb.DocType("Purchase Invoice Item")
+
+ # delete existing invoices with deferred items
+ deferred_invoices = (
+ qb.from_(sinv)
+ .join(sinv_item)
+ .on(sinv.name == sinv_item.parent)
+ .select(sinv.name)
+ .where(sinv_item.enable_deferred_revenue == 1)
+ .run()
+ )
+ if deferred_invoices:
+ qb.from_(sinv).delete().where(sinv.name.isin(deferred_invoices)).run()
+
+ deferred_invoices = (
+ qb.from_(pinv)
+ .join(pinv_item)
+ .on(pinv.name == pinv_item.parent)
+ .select(pinv.name)
+ .where(pinv_item.enable_deferred_expense == 1)
+ .run()
+ )
+ if deferred_invoices:
+ qb.from_(pinv).delete().where(pinv.name.isin(deferred_invoices)).run()
def test_deferred_revenue(self):
+ self.clear_old_entries()
+
# created deferred expense accounts, if not found
deferred_revenue_account = create_account(
account_name="Deferred Revenue",
@@ -108,6 +140,8 @@ class TestDeferredRevenueAndExpense(unittest.TestCase):
self.assertEqual(report.period_total, expected)
def test_deferred_expense(self):
+ self.clear_old_entries()
+
# created deferred expense accounts, if not found
deferred_expense_account = create_account(
account_name="Deferred Expense",
@@ -198,6 +232,91 @@ class TestDeferredRevenueAndExpense(unittest.TestCase):
]
self.assertEqual(report.period_total, expected)
+ def test_zero_months(self):
+ self.clear_old_entries()
+ # created deferred expense accounts, if not found
+ deferred_revenue_account = create_account(
+ account_name="Deferred Revenue",
+ parent_account="Current Liabilities - _CD",
+ company="_Test Company DR",
+ )
+
+ acc_settings = frappe.get_doc("Accounts Settings", "Accounts Settings")
+ acc_settings.book_deferred_entries_based_on = "Months"
+ acc_settings.save()
+
+ customer = frappe.new_doc("Customer")
+ customer.customer_name = "_Test Customer DR"
+ customer.type = "Individual"
+ customer.insert()
+
+ item = create_item(
+ "_Test Internet Subscription",
+ is_stock_item=0,
+ warehouse="All Warehouses - _CD",
+ company="_Test Company DR",
+ )
+ item.enable_deferred_revenue = 1
+ item.deferred_revenue_account = deferred_revenue_account
+ item.no_of_months = 0
+ item.save()
+
+ si = create_sales_invoice(
+ item=item.name,
+ company="_Test Company DR",
+ customer="_Test Customer DR",
+ debit_to="Debtors - _CD",
+ posting_date="2021-05-01",
+ parent_cost_center="Main - _CD",
+ cost_center="Main - _CD",
+ do_not_submit=True,
+ rate=300,
+ price_list_rate=300,
+ )
+ si.items[0].enable_deferred_revenue = 1
+ si.items[0].deferred_revenue_account = deferred_revenue_account
+ si.items[0].income_account = "Sales - _CD"
+ si.save()
+ si.submit()
+
+ pda = frappe.get_doc(
+ dict(
+ doctype="Process Deferred Accounting",
+ posting_date=nowdate(),
+ start_date="2021-05-01",
+ end_date="2021-08-01",
+ type="Income",
+ company="_Test Company DR",
+ )
+ )
+ pda.insert()
+ pda.submit()
+
+ # execute report
+ fiscal_year = frappe.get_doc("Fiscal Year", frappe.defaults.get_user_default("fiscal_year"))
+ self.filters = frappe._dict(
+ {
+ "company": frappe.defaults.get_user_default("Company"),
+ "filter_based_on": "Date Range",
+ "period_start_date": "2021-05-01",
+ "period_end_date": "2021-08-01",
+ "from_fiscal_year": fiscal_year.year,
+ "to_fiscal_year": fiscal_year.year,
+ "periodicity": "Monthly",
+ "type": "Revenue",
+ "with_upcoming_postings": False,
+ }
+ )
+
+ report = Deferred_Revenue_and_Expense_Report(filters=self.filters)
+ report.run()
+ expected = [
+ {"key": "may_2021", "total": 300.0, "actual": 300.0},
+ {"key": "jun_2021", "total": 0, "actual": 0},
+ {"key": "jul_2021", "total": 0, "actual": 0},
+ {"key": "aug_2021", "total": 0, "actual": 0},
+ ]
+ self.assertEqual(report.period_total, expected)
def create_company():
company = frappe.db.exists("Company", "_Test Company DR")
@@ -209,15 +328,11 @@ def create_company():
company.insert()
-def clear_old_entries():
+def clear_accounts_and_items():
item = qb.DocType("Item")
account = qb.DocType("Account")
customer = qb.DocType("Customer")
supplier = qb.DocType("Supplier")
- sinv = qb.DocType("Sales Invoice")
- sinv_item = qb.DocType("Sales Invoice Item")
- pinv = qb.DocType("Purchase Invoice")
- pinv_item = qb.DocType("Purchase Invoice Item")
qb.from_(account).delete().where(
(account.account_name == "Deferred Revenue")
@@ -228,26 +343,3 @@ def clear_old_entries():
).run()
qb.from_(customer).delete().where(customer.customer_name == "_Test Customer DR").run()
qb.from_(supplier).delete().where(supplier.supplier_name == "_Test Furniture Supplier").run()
-
- # delete existing invoices with deferred items
- deferred_invoices = (
- qb.from_(sinv)
- .join(sinv_item)
- .on(sinv.name == sinv_item.parent)
- .select(sinv.name)
- .where(sinv_item.enable_deferred_revenue == 1)
- .run()
- )
- if deferred_invoices:
- qb.from_(sinv).delete().where(sinv.name.isin(deferred_invoices)).run()
-
- deferred_invoices = (
- qb.from_(pinv)
- .join(pinv_item)
- .on(pinv.name == pinv_item.parent)
- .select(pinv.name)
- .where(pinv_item.enable_deferred_expense == 1)
- .run()
- )
- if deferred_invoices:
- qb.from_(pinv).delete().where(pinv.name.isin(deferred_invoices)).run()
diff --git a/erpnext/assets/doctype/asset/asset.py b/erpnext/assets/doctype/asset/asset.py
index a57a3e281d3..84929b5b4c2 100644
--- a/erpnext/assets/doctype/asset/asset.py
+++ b/erpnext/assets/doctype/asset/asset.py
@@ -186,83 +186,85 @@ class Asset(AccountsController):
if not self.available_for_use_date:
return
- for d in self.get('finance_books'):
- self.validate_asset_finance_books(d)
+ start = self.clear_depreciation_schedule()
- start = self.clear_depreciation_schedule()
+ for finance_book in self.get('finance_books'):
+ self.validate_asset_finance_books(finance_book)
# value_after_depreciation - current Asset value
- if self.docstatus == 1 and d.value_after_depreciation:
- value_after_depreciation = flt(d.value_after_depreciation)
+ if self.docstatus == 1 and finance_book.value_after_depreciation:
+ value_after_depreciation = flt(finance_book.value_after_depreciation)
else:
value_after_depreciation = (flt(self.gross_purchase_amount) -
flt(self.opening_accumulated_depreciation))
- d.value_after_depreciation = value_after_depreciation
+ finance_book.value_after_depreciation = value_after_depreciation
- number_of_pending_depreciations = cint(d.total_number_of_depreciations) - \
+ number_of_pending_depreciations = cint(finance_book.total_number_of_depreciations) - \
cint(self.number_of_depreciations_booked)
- has_pro_rata = self.check_is_pro_rata(d)
+ has_pro_rata = self.check_is_pro_rata(finance_book)
if has_pro_rata:
number_of_pending_depreciations += 1
skip_row = False
- for n in range(start, number_of_pending_depreciations):
+
+ for n in range(start[finance_book.idx-1], number_of_pending_depreciations):
# If depreciation is already completed (for double declining balance)
if skip_row: continue
- depreciation_amount = get_depreciation_amount(self, value_after_depreciation, d)
+ depreciation_amount = get_depreciation_amount(self, value_after_depreciation, finance_book)
if not has_pro_rata or n < cint(number_of_pending_depreciations) - 1:
- schedule_date = add_months(d.depreciation_start_date,
- n * cint(d.frequency_of_depreciation))
+ schedule_date = add_months(finance_book.depreciation_start_date,
+ n * cint(finance_book.frequency_of_depreciation))
# schedule date will be a year later from start date
# so monthly schedule date is calculated by removing 11 months from it
- monthly_schedule_date = add_months(schedule_date, - d.frequency_of_depreciation + 1)
+ monthly_schedule_date = add_months(schedule_date, - finance_book.frequency_of_depreciation + 1)
# if asset is being sold
if date_of_sale:
- from_date = self.get_from_date(d.finance_book)
- depreciation_amount, days, months = self.get_pro_rata_amt(d, depreciation_amount,
+ from_date = self.get_from_date(finance_book.finance_book)
+ depreciation_amount, days, months = self.get_pro_rata_amt(finance_book, depreciation_amount,
from_date, date_of_sale)
if depreciation_amount > 0:
self.append("schedules", {
"schedule_date": date_of_sale,
"depreciation_amount": depreciation_amount,
- "depreciation_method": d.depreciation_method,
- "finance_book": d.finance_book,
- "finance_book_id": d.idx
+ "depreciation_method": finance_book.depreciation_method,
+ "finance_book": finance_book.finance_book,
+ "finance_book_id": finance_book.idx
})
break
# For first row
if has_pro_rata and not self.opening_accumulated_depreciation and n==0:
- depreciation_amount, days, months = self.get_pro_rata_amt(d, depreciation_amount,
- self.available_for_use_date, d.depreciation_start_date)
+ from_date = add_days(self.available_for_use_date, -1) # needed to calc depr amount for available_for_use_date too
+ depreciation_amount, days, months = self.get_pro_rata_amt(finance_book, depreciation_amount,
+ from_date, finance_book.depreciation_start_date)
# For first depr schedule date will be the start date
# so monthly schedule date is calculated by removing month difference between use date and start date
- monthly_schedule_date = add_months(d.depreciation_start_date, - months + 1)
+ monthly_schedule_date = add_months(finance_book.depreciation_start_date, - months + 1)
# For last row
elif has_pro_rata and n == cint(number_of_pending_depreciations) - 1:
if not self.flags.increase_in_asset_life:
# In case of increase_in_asset_life, the self.to_date is already set on asset_repair submission
self.to_date = add_months(self.available_for_use_date,
- (n + self.number_of_depreciations_booked) * cint(d.frequency_of_depreciation))
+ (n + self.number_of_depreciations_booked) * cint(finance_book.frequency_of_depreciation))
depreciation_amount_without_pro_rata = depreciation_amount
- depreciation_amount, days, months = self.get_pro_rata_amt(d,
+ depreciation_amount, days, months = self.get_pro_rata_amt(finance_book,
depreciation_amount, schedule_date, self.to_date)
depreciation_amount = self.get_adjusted_depreciation_amount(depreciation_amount_without_pro_rata,
- depreciation_amount, d.finance_book)
+ depreciation_amount, finance_book.finance_book)
monthly_schedule_date = add_months(schedule_date, 1)
schedule_date = add_days(schedule_date, days)
@@ -273,10 +275,10 @@ class Asset(AccountsController):
self.precision("gross_purchase_amount"))
# Adjust depreciation amount in the last period based on the expected value after useful life
- if d.expected_value_after_useful_life and ((n == cint(number_of_pending_depreciations) - 1
- and value_after_depreciation != d.expected_value_after_useful_life)
- or value_after_depreciation < d.expected_value_after_useful_life):
- depreciation_amount += (value_after_depreciation - d.expected_value_after_useful_life)
+ if finance_book.expected_value_after_useful_life and ((n == cint(number_of_pending_depreciations) - 1
+ and value_after_depreciation != finance_book.expected_value_after_useful_life)
+ or value_after_depreciation < finance_book.expected_value_after_useful_life):
+ depreciation_amount += (value_after_depreciation - finance_book.expected_value_after_useful_life)
skip_row = True
if depreciation_amount > 0:
@@ -286,7 +288,7 @@ class Asset(AccountsController):
# In pro rata case, for first and last depreciation, month range would be different
month_range = months \
if (has_pro_rata and n==0) or (has_pro_rata and n == cint(number_of_pending_depreciations) - 1) \
- else d.frequency_of_depreciation
+ else finance_book.frequency_of_depreciation
for r in range(month_range):
if (has_pro_rata and n == 0):
@@ -312,27 +314,52 @@ class Asset(AccountsController):
self.append("schedules", {
"schedule_date": date,
"depreciation_amount": amount,
- "depreciation_method": d.depreciation_method,
- "finance_book": d.finance_book,
- "finance_book_id": d.idx
+ "depreciation_method": finance_book.depreciation_method,
+ "finance_book": finance_book.finance_book,
+ "finance_book_id": finance_book.idx
})
else:
self.append("schedules", {
"schedule_date": schedule_date,
"depreciation_amount": depreciation_amount,
- "depreciation_method": d.depreciation_method,
- "finance_book": d.finance_book,
- "finance_book_id": d.idx
+ "depreciation_method": finance_book.depreciation_method,
+ "finance_book": finance_book.finance_book,
+ "finance_book_id": finance_book.idx
})
- # used when depreciation schedule needs to be modified due to increase in asset life
+ # depreciation schedules need to be cleared before modification due to increase in asset life/asset sales
+ # JE: Journal Entry, FB: Finance Book
def clear_depreciation_schedule(self):
- start = 0
- for n in range(len(self.schedules)):
- if not self.schedules[n].journal_entry:
- del self.schedules[n:]
- start = n
- break
+ start = []
+ num_of_depreciations_completed = 0
+ depr_schedule = []
+
+ for schedule in self.get('schedules'):
+
+ # to update start when there are JEs linked with all the schedule rows corresponding to an FB
+ if len(start) == (int(schedule.finance_book_id) - 2):
+ start.append(num_of_depreciations_completed)
+ num_of_depreciations_completed = 0
+
+ # to ensure that start will only be updated once for each FB
+ if len(start) == (int(schedule.finance_book_id) - 1):
+ if schedule.journal_entry:
+ num_of_depreciations_completed += 1
+ depr_schedule.append(schedule)
+ else:
+ start.append(num_of_depreciations_completed)
+ num_of_depreciations_completed = 0
+
+ # to update start when all the schedule rows corresponding to the last FB are linked with JEs
+ if len(start) == (len(self.finance_books) - 1):
+ start.append(num_of_depreciations_completed)
+
+ # when the Depreciation Schedule is being created for the first time
+ if start == []:
+ start = [0] * len(self.finance_books)
+ else:
+ self.schedules = depr_schedule
+
return start
def get_from_date(self, finance_book):
@@ -349,7 +376,9 @@ class Asset(AccountsController):
if from_date:
return from_date
- return self.available_for_use_date
+
+ # since depr for available_for_use_date is not yet booked
+ return add_days(self.available_for_use_date, -1)
# if it returns True, depreciation_amount will not be equal for the first and last rows
def check_is_pro_rata(self, row):
diff --git a/erpnext/assets/doctype/asset/test_asset.py b/erpnext/assets/doctype/asset/test_asset.py
index ba4dbee72da..0ddfb6c1c02 100644
--- a/erpnext/assets/doctype/asset/test_asset.py
+++ b/erpnext/assets/doctype/asset/test_asset.py
@@ -207,9 +207,9 @@ class TestAsset(AssetSetup):
self.assertEqual(frappe.db.get_value("Asset", asset.name, "status"), "Sold")
expected_gle = (
- ("_Test Accumulated Depreciations - _TC", 20392.16, 0.0),
+ ("_Test Accumulated Depreciations - _TC", 20490.2, 0.0),
("_Test Fixed Asset - _TC", 0.0, 100000.0),
- ("_Test Gain/Loss on Asset Disposal - _TC", 54607.84, 0.0),
+ ("_Test Gain/Loss on Asset Disposal - _TC", 54509.8, 0.0),
("Debtors - _TC", 25000.0, 0.0)
)
@@ -491,10 +491,10 @@ class TestDepreciationMethods(AssetSetup):
)
expected_schedules = [
- ["2030-12-31", 27534.25, 27534.25],
- ["2031-12-31", 30000.0, 57534.25],
- ["2032-12-31", 30000.0, 87534.25],
- ["2033-01-30", 2465.75, 90000.0]
+ ['2030-12-31', 27616.44, 27616.44],
+ ['2031-12-31', 30000.0, 57616.44],
+ ['2032-12-31', 30000.0, 87616.44],
+ ['2033-01-30', 2383.56, 90000.0]
]
schedules = [[cstr(d.schedule_date), flt(d.depreciation_amount, 2), flt(d.accumulated_depreciation_amount, 2)]
@@ -544,10 +544,10 @@ class TestDepreciationMethods(AssetSetup):
self.assertEqual(asset.finance_books[0].rate_of_depreciation, 50.0)
expected_schedules = [
- ["2030-12-31", 28493.15, 28493.15],
- ["2031-12-31", 35753.43, 64246.58],
- ["2032-12-31", 17876.71, 82123.29],
- ["2033-06-06", 5376.71, 87500.0]
+ ['2030-12-31', 28630.14, 28630.14],
+ ['2031-12-31', 35684.93, 64315.07],
+ ['2032-12-31', 17842.47, 82157.54],
+ ['2033-06-06', 5342.46, 87500.0]
]
schedules = [[cstr(d.schedule_date), flt(d.depreciation_amount, 2), flt(d.accumulated_depreciation_amount, 2)]
@@ -580,10 +580,10 @@ class TestDepreciationMethods(AssetSetup):
self.assertEqual(asset.finance_books[0].rate_of_depreciation, 50.0)
expected_schedules = [
- ["2030-12-31", 11780.82, 11780.82],
- ["2031-12-31", 44109.59, 55890.41],
- ["2032-12-31", 22054.8, 77945.21],
- ["2033-07-12", 9554.79, 87500.0]
+ ["2030-12-31", 11849.32, 11849.32],
+ ["2031-12-31", 44075.34, 55924.66],
+ ["2032-12-31", 22037.67, 77962.33],
+ ["2033-07-12", 9537.67, 87500.0]
]
schedules = [[cstr(d.schedule_date), flt(d.depreciation_amount, 2), flt(d.accumulated_depreciation_amount, 2)]
@@ -642,7 +642,7 @@ class TestDepreciationBasics(AssetSetup):
asset = create_asset(
item_code = "Macbook Pro",
calculate_depreciation = 1,
- available_for_use_date = getdate("2019-12-31"),
+ available_for_use_date = getdate("2020-01-01"),
total_number_of_depreciations = 3,
expected_value_after_useful_life = 10000,
depreciation_start_date = getdate("2020-07-01"),
@@ -653,7 +653,7 @@ class TestDepreciationBasics(AssetSetup):
["2020-07-01", 15000, 15000],
["2021-07-01", 30000, 45000],
["2022-07-01", 30000, 75000],
- ["2022-12-31", 15000, 90000]
+ ["2023-01-01", 15000, 90000]
]
for i, schedule in enumerate(asset.schedules):
@@ -976,6 +976,82 @@ class TestDepreciationBasics(AssetSetup):
self.assertEqual(len(asset.schedules), 1)
+ def test_clear_depreciation_schedule_for_multiple_finance_books(self):
+ asset = create_asset(
+ item_code = "Macbook Pro",
+ available_for_use_date = "2019-12-31",
+ do_not_save = 1
+ )
+
+ asset.calculate_depreciation = 1
+ asset.append("finance_books", {
+ "depreciation_method": "Straight Line",
+ "frequency_of_depreciation": 1,
+ "total_number_of_depreciations": 3,
+ "expected_value_after_useful_life": 10000,
+ "depreciation_start_date": "2020-01-31"
+ })
+ asset.append("finance_books", {
+ "depreciation_method": "Straight Line",
+ "frequency_of_depreciation": 1,
+ "total_number_of_depreciations": 6,
+ "expected_value_after_useful_life": 10000,
+ "depreciation_start_date": "2020-01-31"
+ })
+ asset.append("finance_books", {
+ "depreciation_method": "Straight Line",
+ "frequency_of_depreciation": 12,
+ "total_number_of_depreciations": 3,
+ "expected_value_after_useful_life": 10000,
+ "depreciation_start_date": "2020-12-31"
+ })
+ asset.submit()
+
+ post_depreciation_entries(date="2020-04-01")
+ asset.load_from_db()
+
+ asset.clear_depreciation_schedule()
+
+ self.assertEqual(len(asset.schedules), 6)
+
+ for schedule in asset.schedules:
+ if schedule.idx <= 3:
+ self.assertEqual(schedule.finance_book_id, "1")
+ else:
+ self.assertEqual(schedule.finance_book_id, "2")
+
+ def test_depreciation_schedules_are_set_up_for_multiple_finance_books(self):
+ asset = create_asset(
+ item_code = "Macbook Pro",
+ available_for_use_date = "2019-12-31",
+ do_not_save = 1
+ )
+
+ asset.calculate_depreciation = 1
+ asset.append("finance_books", {
+ "depreciation_method": "Straight Line",
+ "frequency_of_depreciation": 12,
+ "total_number_of_depreciations": 3,
+ "expected_value_after_useful_life": 10000,
+ "depreciation_start_date": "2020-12-31"
+ })
+ asset.append("finance_books", {
+ "depreciation_method": "Straight Line",
+ "frequency_of_depreciation": 12,
+ "total_number_of_depreciations": 6,
+ "expected_value_after_useful_life": 10000,
+ "depreciation_start_date": "2020-12-31"
+ })
+ asset.save()
+
+ self.assertEqual(len(asset.schedules), 9)
+
+ for schedule in asset.schedules:
+ if schedule.idx <= 3:
+ self.assertEqual(schedule.finance_book_id, 1)
+ else:
+ self.assertEqual(schedule.finance_book_id, 2)
+
def test_depreciation_entry_cancellation(self):
asset = create_asset(
item_code = "Macbook Pro",
diff --git a/erpnext/change_log/v13/v13_20_0.md b/erpnext/change_log/v13/v13_20_0.md
new file mode 100644
index 00000000000..10c8fc70d20
--- /dev/null
+++ b/erpnext/change_log/v13/v13_20_0.md
@@ -0,0 +1,32 @@
+## Version 13.20.0 Release Notes
+
+### Features & Enhancements
+
+- Provisional accounting for expenses ([#29451](https://github.com/frappe/erpnext/pull/29451))
+
+### Fixes
+
+- Incorrect number of items fetched while creating delivery note ([#29454](https://github.com/frappe/erpnext/pull/29454))
+- Incorrect raw materials quantity in manufacture stock entry ([#29419](https://github.com/frappe/erpnext/pull/29419))
+- Refactored the update_serial_no function for old Maintenance Visits ([#28843](https://github.com/frappe/erpnext/pull/28843))
+- Ignore empty customer/supplier in item query ([#29610](https://github.com/frappe/erpnext/pull/29610))
+- The "Bypass Credit Limit Check" from customer has not fetched in the Customer Credit Balance report ([#29367](https://github.com/frappe/erpnext/pull/29367))
+- Reset conversion facture after changing the Stock UOM ([#29062](https://github.com/frappe/erpnext/pull/29062))
+- Cart Items rendering issue ([#29398](https://github.com/frappe/erpnext/pull/29398))
+- Honour 'include holidays' setting while marking attendance for leave application ([#29425](https://github.com/frappe/erpnext/pull/29425))
+- Cost of poor quality report time filters not working ([#28958](https://github.com/frappe/erpnext/pull/28958))
+- Incorrect packing items getting fetched on Sales Return / Credit Note ([#28607](https://github.com/frappe/erpnext/pull/28607))
+- Regenerate packing items on newly mapped doc ([#29642](https://github.com/frappe/erpnext/pull/29642))
+- From Time and To Time not updated in drag and drop action for Course Schedule ([#29114](https://github.com/frappe/erpnext/pull/29114))
+- Employee: set user image and validate user id only if user data is found ([#29452](https://github.com/frappe/erpnext/pull/29452))
+- Clear Depreciation Schedule before modification ([#28507](https://github.com/frappe/erpnext/pull/28507))
+- Fixed shopping cart qty badge ([#29077](https://github.com/frappe/erpnext/pull/29077))
+- Fetch "transfer material against" from BOM ([#29435](https://github.com/frappe/erpnext/pull/29435))
+- Cart & Popup Logic of Item variant without Website Item ([#29383](https://github.com/frappe/erpnext/pull/29383))
+- Timesheets: calculate to time based on from time and hours ([#28589](https://github.com/frappe/erpnext/pull/28589))
+- Dynamically compute BOM Level ([#29522](https://github.com/frappe/erpnext/pull/29522))
+- Contact duplication on converting lead to customer ([#29337](https://github.com/frappe/erpnext/pull/29337))
+- Fixed populate practitioner selected in form to check availability popup ([#29405](https://github.com/frappe/erpnext/pull/29405))
+- Compute batch ledger in python ([#29324](https://github.com/frappe/erpnext/pull/29324))
+- Incorrect packing list for recurring items & code cleanup ([#29456](https://github.com/frappe/erpnext/pull/29456))
+- Opening invoice creation tool can fetch multiple accounting dimension ([#29407](https://github.com/frappe/erpnext/pull/29407))
\ No newline at end of file
diff --git a/erpnext/controllers/queries.py b/erpnext/controllers/queries.py
index ef5ee36a758..9324f07ec97 100644
--- a/erpnext/controllers/queries.py
+++ b/erpnext/controllers/queries.py
@@ -249,6 +249,9 @@ def item_query(doctype, txt, searchfield, start, page_len, filters, as_dict=Fals
del filters['customer']
else:
del filters['supplier']
+ else:
+ filters.pop('customer', None)
+ filters.pop('supplier', None)
description_cond = ''
diff --git a/erpnext/controllers/stock_controller.py b/erpnext/controllers/stock_controller.py
index f22669b2555..8d17683953e 100644
--- a/erpnext/controllers/stock_controller.py
+++ b/erpnext/controllers/stock_controller.py
@@ -40,7 +40,10 @@ class StockController(AccountsController):
if self.docstatus == 2:
make_reverse_gl_entries(voucher_type=self.doctype, voucher_no=self.name)
- if cint(erpnext.is_perpetual_inventory_enabled(self.company)):
+ provisional_accounting_for_non_stock_items = \
+ cint(frappe.db.get_value('Company', self.company, 'enable_provisional_accounting_for_non_stock_items'))
+
+ if cint(erpnext.is_perpetual_inventory_enabled(self.company)) or provisional_accounting_for_non_stock_items:
warehouse_account = get_warehouse_account_map(self.company)
if self.docstatus==1:
@@ -77,17 +80,17 @@ class StockController(AccountsController):
.format(d.idx, get_link_to_form("Batch", d.get("batch_no"))))
def clean_serial_nos(self):
+ from erpnext.stock.doctype.serial_no.serial_no import clean_serial_no_string
+
for row in self.get("items"):
if hasattr(row, "serial_no") and row.serial_no:
- # replace commas by linefeed
- row.serial_no = row.serial_no.replace(",", "\n")
+ # remove extra whitespace and store one serial no on each line
+ row.serial_no = clean_serial_no_string(row.serial_no)
- # strip preceeding and succeeding spaces for each SN
- # (SN could have valid spaces in between e.g. SN - 123 - 2021)
- serial_no_list = row.serial_no.split("\n")
- serial_no_list = [sn.strip() for sn in serial_no_list]
-
- row.serial_no = "\n".join(serial_no_list)
+ for row in self.get('packed_items') or []:
+ if hasattr(row, "serial_no") and row.serial_no:
+ # remove extra whitespace and store one serial no on each line
+ row.serial_no = clean_serial_no_string(row.serial_no)
def get_gl_entries(self, warehouse_account=None, default_expense_account=None,
default_cost_center=None):
@@ -256,11 +259,7 @@ class StockController(AccountsController):
for d in self.items:
if not d.batch_no: continue
- serial_nos = [sr.name for sr in frappe.get_all("Serial No",
- {'batch_no': d.batch_no, 'status': 'Inactive'})]
-
- if serial_nos:
- frappe.db.set_value("Serial No", { 'name': ['in', serial_nos] }, "batch_no", None)
+ frappe.db.set_value("Serial No", {"batch_no": d.batch_no, "status": "Inactive"}, "batch_no", None)
d.batch_no = None
d.db_set("batch_no", None)
diff --git a/erpnext/controllers/tests/test_queries.py b/erpnext/controllers/tests/test_queries.py
index 908d78c15bf..60d1733021c 100644
--- a/erpnext/controllers/tests/test_queries.py
+++ b/erpnext/controllers/tests/test_queries.py
@@ -56,6 +56,12 @@ class TestQueries(unittest.TestCase):
bundled_stock_items = query(txt="_test product bundle item 5", filters={"is_stock_item": 1})
self.assertEqual(len(bundled_stock_items), 0)
+ # empty customer/supplier should be stripped of instead of failure
+ query(txt="", filters={"customer": None})
+ query(txt="", filters={"customer": ""})
+ query(txt="", filters={"supplier": None})
+ query(txt="", filters={"supplier": ""})
+
def test_bom_qury(self):
query = add_default_params(queries.bom, "BOM")
diff --git a/erpnext/education/api.py b/erpnext/education/api.py
index d9013b08161..636b948a1cc 100644
--- a/erpnext/education/api.py
+++ b/erpnext/education/api.py
@@ -201,8 +201,8 @@ def get_course_schedule_events(start, end, filters=None):
conditions = get_event_conditions("Course Schedule", filters)
data = frappe.db.sql("""select name, course, color,
- timestamp(schedule_date, from_time) as from_datetime,
- timestamp(schedule_date, to_time) as to_datetime,
+ timestamp(schedule_date, from_time) as from_time,
+ timestamp(schedule_date, to_time) as to_time,
room, student_group, 0 as 'allDay'
from `tabCourse Schedule`
where ( schedule_date between %(start)s and %(end)s )
diff --git a/erpnext/education/doctype/course_schedule/course_schedule.py b/erpnext/education/doctype/course_schedule/course_schedule.py
index 335b6d28d0c..335cec43527 100644
--- a/erpnext/education/doctype/course_schedule/course_schedule.py
+++ b/erpnext/education/doctype/course_schedule/course_schedule.py
@@ -3,6 +3,8 @@
# For license information, please see license.txt
+from datetime import datetime
+
import frappe
from frappe import _
from frappe.model.document import Document
@@ -30,6 +32,14 @@ class CourseSchedule(Document):
if self.from_time > self.to_time:
frappe.throw(_("From Time cannot be greater than To Time."))
+ """Handles specicfic case to update schedule date in calendar """
+ if isinstance(self.from_time, str):
+ try:
+ datetime_obj = datetime.strptime(self.from_time, '%Y-%m-%d %H:%M:%S')
+ self.schedule_date = datetime_obj
+ except ValueError:
+ pass
+
def validate_overlap(self):
"""Validates overlap for Student Group, Instructor, Room"""
@@ -47,4 +57,4 @@ class CourseSchedule(Document):
validate_overlap_for(self, "Assessment Plan", "student_group")
validate_overlap_for(self, "Assessment Plan", "room")
- validate_overlap_for(self, "Assessment Plan", "supervisor", self.instructor)
+ validate_overlap_for(self, "Assessment Plan", "supervisor", self.instructor)
\ No newline at end of file
diff --git a/erpnext/education/doctype/course_schedule/course_schedule_calendar.js b/erpnext/education/doctype/course_schedule/course_schedule_calendar.js
index 803527e5480..cacd539b224 100644
--- a/erpnext/education/doctype/course_schedule/course_schedule_calendar.js
+++ b/erpnext/education/doctype/course_schedule/course_schedule_calendar.js
@@ -1,11 +1,10 @@
frappe.views.calendar["Course Schedule"] = {
field_map: {
- // from_datetime and to_datetime don't exist as docfields but are used in onload
- "start": "from_datetime",
- "end": "to_datetime",
+ "start": "from_time",
+ "end": "to_time",
"id": "name",
"title": "course",
- "allDay": "allDay"
+ "allDay": "allDay",
},
gantt: false,
order_by: "schedule_date",
diff --git a/erpnext/education/doctype/course_schedule/test_course_schedule.py b/erpnext/education/doctype/course_schedule/test_course_schedule.py
index a7324195557..56149affcea 100644
--- a/erpnext/education/doctype/course_schedule/test_course_schedule.py
+++ b/erpnext/education/doctype/course_schedule/test_course_schedule.py
@@ -6,6 +6,7 @@ import unittest
import frappe
from frappe.utils import to_timedelta, today
+from frappe.utils.data import add_to_date
from erpnext.education.utils import OverlapError
@@ -39,6 +40,11 @@ class TestCourseSchedule(unittest.TestCase):
make_course_schedule_test_record(from_time= cs1.from_time, to_time= cs1.to_time,
student_group="Course-TC102-2014-2015 (_Test Academic Term)", instructor="_Test Instructor 2", room=frappe.get_all("Room")[1].name)
+ def test_update_schedule_date(self):
+ doc = make_course_schedule_test_record(schedule_date= add_to_date(today(), days=1))
+ doc.schedule_date = add_to_date(doc.schedule_date, days=1)
+ doc.save()
+
def make_course_schedule_test_record(**args):
args = frappe._dict(args)
diff --git a/erpnext/healthcare/doctype/lab_test/lab_test.json b/erpnext/healthcare/doctype/lab_test/lab_test.json
index ac61fea3ad7..cdf27aa06b3 100644
--- a/erpnext/healthcare/doctype/lab_test/lab_test.json
+++ b/erpnext/healthcare/doctype/lab_test/lab_test.json
@@ -99,7 +99,6 @@
"search_index": 1
},
{
- "fetch_from": "inpatient_record.patient",
"fieldname": "patient",
"fieldtype": "Link",
"ignore_user_permissions": 1,
@@ -559,7 +558,7 @@
],
"is_submittable": 1,
"links": [],
- "modified": "2020-11-30 11:04:17.195848",
+ "modified": "2022-01-20 12:37:07.943153",
"modified_by": "Administrator",
"module": "Healthcare",
"name": "Lab Test",
diff --git a/erpnext/healthcare/doctype/patient_appointment/patient_appointment.js b/erpnext/healthcare/doctype/patient_appointment/patient_appointment.js
index bba001c9c0e..ba862783caa 100644
--- a/erpnext/healthcare/doctype/patient_appointment/patient_appointment.js
+++ b/erpnext/healthcare/doctype/patient_appointment/patient_appointment.js
@@ -337,9 +337,13 @@ let check_and_set_availability = function(frm) {
});
d.fields_dict['department'].df.onchange = () => {
- d.set_values({
- 'practitioner': ''
- });
+ if (d.get_value('department') == frm.doc.department) {
+ d.set_value('practitioner', frm.doc.practitioner);
+ } else {
+ d.set_value('practitioner', '');
+ d.fields_dict.available_slots.html('');
+ d.get_primary_btn().attr('disabled', true);
+ }
let department = d.get_value('department');
if (department) {
d.fields_dict.practitioner.get_query = function() {
@@ -426,7 +430,8 @@ let check_and_set_availability = function(frm) {
slot_details.forEach((slot_info) => {
slot_html += `
- ${__('Practitioner Schedule:')} ${slot_info.slot_name}
+ ${slot_info.practitioner_name}
+ ${__('Schedule:')} ${slot_info.slot_name}
${__('Service Unit:')} ${slot_info.service_unit} `;
if (slot_info.service_unit_capacity) {
diff --git a/erpnext/healthcare/doctype/patient_appointment/patient_appointment.py b/erpnext/healthcare/doctype/patient_appointment/patient_appointment.py
index 1e4608f84e0..c4f253a062f 100755
--- a/erpnext/healthcare/doctype/patient_appointment/patient_appointment.py
+++ b/erpnext/healthcare/doctype/patient_appointment/patient_appointment.py
@@ -388,7 +388,8 @@ def get_available_slots(practitioner_doc, date):
fields=['name', 'appointment_time', 'duration', 'status'])
slot_details.append({'slot_name': slot_name, 'service_unit': schedule_entry.service_unit, 'avail_slot': available_slots,
- 'appointments': appointments, 'allow_overlap': allow_overlap, 'service_unit_capacity': service_unit_capacity})
+ 'appointments': appointments, 'allow_overlap': allow_overlap, 'service_unit_capacity': service_unit_capacity,
+ 'practitioner_name': practitioner_doc.practitioner_name})
return slot_details
diff --git a/erpnext/healthcare/doctype/sample_collection/sample_collection.json b/erpnext/healthcare/doctype/sample_collection/sample_collection.json
index 83383e34457..f8525f7e14b 100644
--- a/erpnext/healthcare/doctype/sample_collection/sample_collection.json
+++ b/erpnext/healthcare/doctype/sample_collection/sample_collection.json
@@ -66,7 +66,6 @@
"search_index": 1
},
{
- "fetch_from": "inpatient_record.patient",
"fieldname": "patient",
"fieldtype": "Link",
"hide_days": 1,
@@ -224,7 +223,7 @@
],
"is_submittable": 1,
"links": [],
- "modified": "2020-07-30 16:53:13.076104",
+ "modified": "2022-01-20 12:38:55.382621",
"modified_by": "Administrator",
"module": "Healthcare",
"name": "Sample Collection",
diff --git a/erpnext/healthcare/doctype/vital_signs/vital_signs.json b/erpnext/healthcare/doctype/vital_signs/vital_signs.json
index 15ab5047bc4..a945032c7e0 100644
--- a/erpnext/healthcare/doctype/vital_signs/vital_signs.json
+++ b/erpnext/healthcare/doctype/vital_signs/vital_signs.json
@@ -51,7 +51,6 @@
"read_only": 1
},
{
- "fetch_from": "inpatient_record.patient",
"fieldname": "patient",
"fieldtype": "Link",
"ignore_user_permissions": 1,
@@ -259,7 +258,7 @@
],
"is_submittable": 1,
"links": [],
- "modified": "2020-05-17 22:23:24.632286",
+ "modified": "2022-01-20 12:30:07.515185",
"modified_by": "Administrator",
"module": "Healthcare",
"name": "Vital Signs",
diff --git a/erpnext/hr/doctype/employee/employee.py b/erpnext/hr/doctype/employee/employee.py
index 88e5ca9d4c5..8a2950696af 100755
--- a/erpnext/hr/doctype/employee/employee.py
+++ b/erpnext/hr/doctype/employee/employee.py
@@ -68,12 +68,18 @@ class Employee(NestedSet):
self.employee_name = ' '.join(filter(lambda x: x, [self.first_name, self.middle_name, self.last_name]))
def validate_user_details(self):
- data = frappe.db.get_value('User',
- self.user_id, ['enabled', 'user_image'], as_dict=1)
- if data.get("user_image") and self.image == '':
- self.image = data.get("user_image")
- self.validate_for_enabled_user_id(data.get("enabled", 0))
- self.validate_duplicate_user_id()
+ if self.user_id:
+ data = frappe.db.get_value("User",
+ self.user_id, ["enabled", "user_image"], as_dict=1)
+
+ if not data:
+ self.user_id = None
+ return
+
+ if data.get("user_image") and self.image == "":
+ self.image = data.get("user_image")
+ self.validate_for_enabled_user_id(data.get("enabled", 0))
+ self.validate_duplicate_user_id()
def update_nsm_model(self):
frappe.utils.nestedset.update_nsm(self)
diff --git a/erpnext/hr/doctype/leave_application/leave_application.py b/erpnext/hr/doctype/leave_application/leave_application.py
index 1dc5b31461e..70250f5bcf8 100755
--- a/erpnext/hr/doctype/leave_application/leave_application.py
+++ b/erpnext/hr/doctype/leave_application/leave_application.py
@@ -22,6 +22,7 @@ from erpnext.hr.doctype.employee.employee import get_holiday_list_for_employee
from erpnext.hr.doctype.leave_block_list.leave_block_list import get_applicable_block_dates
from erpnext.hr.doctype.leave_ledger_entry.leave_ledger_entry import create_leave_ledger_entry
from erpnext.hr.utils import (
+ get_holiday_dates_for_employee,
get_leave_period,
set_employee_name,
share_doc_with_approver,
@@ -159,33 +160,57 @@ class LeaveApplication(Document):
.format(formatdate(future_allocation[0].from_date), future_allocation[0].name))
def update_attendance(self):
- if self.status == "Approved":
- for dt in daterange(getdate(self.from_date), getdate(self.to_date)):
- date = dt.strftime("%Y-%m-%d")
- status = "Half Day" if self.half_day_date and getdate(date) == getdate(self.half_day_date) else "On Leave"
- attendance_name = frappe.db.exists('Attendance', dict(employee = self.employee,
- attendance_date = date, docstatus = ('!=', 2)))
+ if self.status != "Approved":
+ return
+ holiday_dates = []
+ if not frappe.db.get_value("Leave Type", self.leave_type, "include_holiday"):
+ holiday_dates = get_holiday_dates_for_employee(self.employee, self.from_date, self.to_date)
+
+ for dt in daterange(getdate(self.from_date), getdate(self.to_date)):
+ date = dt.strftime("%Y-%m-%d")
+ attendance_name = frappe.db.exists("Attendance", dict(employee = self.employee,
+ attendance_date = date, docstatus = ('!=', 2)))
+
+ # don't mark attendance for holidays
+ # if leave type does not include holidays within leaves as leaves
+ if date in holiday_dates:
if attendance_name:
- # update existing attendance, change absent to on leave
- doc = frappe.get_doc('Attendance', attendance_name)
- if doc.status != status:
- doc.db_set('status', status)
- doc.db_set('leave_type', self.leave_type)
- doc.db_set('leave_application', self.name)
- else:
- # make new attendance and submit it
- doc = frappe.new_doc("Attendance")
- doc.employee = self.employee
- doc.employee_name = self.employee_name
- doc.attendance_date = date
- doc.company = self.company
- doc.leave_type = self.leave_type
- doc.leave_application = self.name
- doc.status = status
- doc.flags.ignore_validate = True
- doc.insert(ignore_permissions=True)
- doc.submit()
+ # cancel and delete existing attendance for holidays
+ attendance = frappe.get_doc("Attendance", attendance_name)
+ attendance.flags.ignore_permissions = True
+ if attendance.docstatus == 1:
+ attendance.cancel()
+ frappe.delete_doc("Attendance", attendance_name, force=1)
+ continue
+
+ self.create_or_update_attendance(attendance_name, date)
+
+ def create_or_update_attendance(self, attendance_name, date):
+ status = "Half Day" if self.half_day_date and getdate(date) == getdate(self.half_day_date) else "On Leave"
+
+ if attendance_name:
+ # update existing attendance, change absent to on leave
+ doc = frappe.get_doc('Attendance', attendance_name)
+ if doc.status != status:
+ doc.db_set({
+ 'status': status,
+ 'leave_type': self.leave_type,
+ 'leave_application': self.name
+ })
+ else:
+ # make new attendance and submit it
+ doc = frappe.new_doc("Attendance")
+ doc.employee = self.employee
+ doc.employee_name = self.employee_name
+ doc.attendance_date = date
+ doc.company = self.company
+ doc.leave_type = self.leave_type
+ doc.leave_application = self.name
+ doc.status = status
+ doc.flags.ignore_validate = True
+ doc.insert(ignore_permissions=True)
+ doc.submit()
def cancel_attendance(self):
if self.docstatus == 2:
diff --git a/erpnext/hr/doctype/leave_application/test_leave_application.py b/erpnext/hr/doctype/leave_application/test_leave_application.py
index f73d3e52da1..0d2e3989e3e 100644
--- a/erpnext/hr/doctype/leave_application/test_leave_application.py
+++ b/erpnext/hr/doctype/leave_application/test_leave_application.py
@@ -5,7 +5,16 @@ import unittest
import frappe
from frappe.permissions import clear_user_permissions_for_doctype
-from frappe.utils import add_days, add_months, getdate, nowdate
+from frappe.utils import (
+ add_days,
+ add_months,
+ get_first_day,
+ get_last_day,
+ get_year_ending,
+ get_year_start,
+ getdate,
+ nowdate,
+)
from erpnext.hr.doctype.employee.test_employee import make_employee
from erpnext.hr.doctype.leave_allocation.test_leave_allocation import create_leave_allocation
@@ -19,6 +28,10 @@ from erpnext.hr.doctype.leave_policy_assignment.leave_policy_assignment import (
create_assignment_for_multiple_employees,
)
from erpnext.hr.doctype.leave_type.test_leave_type import create_leave_type
+from erpnext.payroll.doctype.salary_slip.test_salary_slip import (
+ make_holiday_list,
+ make_leave_application,
+)
test_dependencies = ["Leave Allocation", "Leave Block List", "Employee"]
@@ -61,13 +74,15 @@ class TestLeaveApplication(unittest.TestCase):
for dt in ["Leave Application", "Leave Allocation", "Salary Slip", "Leave Ledger Entry"]:
frappe.db.sql("DELETE FROM `tab%s`" % dt) #nosec
+ frappe.set_user("Administrator")
+
@classmethod
def setUpClass(cls):
set_leave_approver()
frappe.db.sql("delete from tabAttendance where employee='_T-Employee-00001'")
def tearDown(self):
- frappe.set_user("Administrator")
+ frappe.db.rollback()
def _clear_roles(self):
frappe.db.sql("""delete from `tabHas Role` where parent in
@@ -106,6 +121,72 @@ class TestLeaveApplication(unittest.TestCase):
for d in ('2018-01-01', '2018-01-02', '2018-01-03'):
self.assertTrue(getdate(d) in dates)
+ def test_attendance_for_include_holidays(self):
+ # Case 1: leave type with 'Include holidays within leaves as leaves' enabled
+ frappe.delete_doc_if_exists("Leave Type", "Test Include Holidays", force=1)
+ leave_type = frappe.get_doc(dict(
+ leave_type_name="Test Include Holidays",
+ doctype="Leave Type",
+ include_holiday=True
+ )).insert()
+
+ date = getdate()
+ make_allocation_record(leave_type=leave_type.name, from_date=get_year_start(date), to_date=get_year_ending(date))
+
+ holiday_list = make_holiday_list()
+ frappe.db.set_value("Company", "_Test Company", "default_holiday_list", holiday_list)
+ first_sunday = get_first_sunday(holiday_list)
+
+ leave_application = make_leave_application("_T-Employee-00001", first_sunday, add_days(first_sunday, 3), leave_type.name)
+ leave_application.reload()
+ self.assertEqual(leave_application.total_leave_days, 4)
+ self.assertEqual(frappe.db.count('Attendance', {'leave_application': leave_application.name}), 4)
+
+ leave_application.cancel()
+
+ def test_attendance_update_for_exclude_holidays(self):
+ # Case 2: leave type with 'Include holidays within leaves as leaves' disabled
+ frappe.delete_doc_if_exists("Leave Type", "Test Do Not Include Holidays", force=1)
+ leave_type = frappe.get_doc(dict(
+ leave_type_name="Test Do Not Include Holidays",
+ doctype="Leave Type",
+ include_holiday=False
+ )).insert()
+
+ date = getdate()
+ make_allocation_record(leave_type=leave_type.name, from_date=get_year_start(date), to_date=get_year_ending(date))
+
+ holiday_list = make_holiday_list()
+ frappe.db.set_value("Company", "_Test Company", "default_holiday_list", holiday_list)
+ first_sunday = get_first_sunday(holiday_list)
+
+ # already marked attendance on a holiday should be deleted in this case
+ config = {
+ "doctype": "Attendance",
+ "employee": "_T-Employee-00001",
+ "status": "Present"
+ }
+ attendance_on_holiday = frappe.get_doc(config)
+ attendance_on_holiday.attendance_date = first_sunday
+ attendance_on_holiday.save()
+
+ # already marked attendance on a non-holiday should be updated
+ attendance = frappe.get_doc(config)
+ attendance.attendance_date = add_days(first_sunday, 3)
+ attendance.save()
+
+ leave_application = make_leave_application("_T-Employee-00001", first_sunday, add_days(first_sunday, 3), leave_type.name)
+ leave_application.reload()
+ # holiday should be excluded while marking attendance
+ self.assertEqual(leave_application.total_leave_days, 3)
+ self.assertEqual(frappe.db.count("Attendance", {"leave_application": leave_application.name}), 3)
+
+ # attendance on holiday deleted
+ self.assertFalse(frappe.db.exists("Attendance", attendance_on_holiday.name))
+
+ # attendance on non-holiday updated
+ self.assertEqual(frappe.db.get_value("Attendance", attendance.name, "status"), "On Leave")
+
def test_block_list(self):
self._clear_roles()
@@ -241,7 +322,13 @@ class TestLeaveApplication(unittest.TestCase):
leave_period = get_leave_period()
today = nowdate()
holiday_list = 'Test Holiday List for Optional Holiday'
- optional_leave_date = add_days(today, 7)
+ employee = get_employee()
+
+ default_holiday_list = make_holiday_list()
+ frappe.db.set_value("Company", "_Test Company", "default_holiday_list", default_holiday_list)
+ first_sunday = get_first_sunday(default_holiday_list)
+
+ optional_leave_date = add_days(first_sunday, 1)
if not frappe.db.exists('Holiday List', holiday_list):
frappe.get_doc(dict(
@@ -253,7 +340,6 @@ class TestLeaveApplication(unittest.TestCase):
dict(holiday_date = optional_leave_date, description = 'Test')
]
)).insert()
- employee = get_employee()
frappe.db.set_value('Leave Period', leave_period.name, 'optional_holiday_list', holiday_list)
leave_type = 'Test Optional Type'
@@ -266,7 +352,7 @@ class TestLeaveApplication(unittest.TestCase):
allocate_leaves(employee, leave_period, leave_type, 10)
- date = add_days(today, 6)
+ date = add_days(first_sunday, 2)
leave_application = frappe.get_doc(dict(
doctype = 'Leave Application',
@@ -636,13 +722,13 @@ def create_carry_forwarded_allocation(employee, leave_type):
carry_forward=1)
leave_allocation.submit()
-def make_allocation_record(employee=None, leave_type=None):
+def make_allocation_record(employee=None, leave_type=None, from_date=None, to_date=None):
allocation = frappe.get_doc({
"doctype": "Leave Allocation",
"employee": employee or "_T-Employee-00001",
"leave_type": leave_type or "_Test Leave Type",
- "from_date": "2013-01-01",
- "to_date": "2019-12-31",
+ "from_date": from_date or "2013-01-01",
+ "to_date": to_date or "2019-12-31",
"new_leaves_allocated": 30
})
@@ -691,3 +777,16 @@ def allocate_leaves(employee, leave_period, leave_type, new_leaves_allocated, el
}).insert()
allocate_leave.submit()
+
+
+def get_first_sunday(holiday_list):
+ month_start_date = get_first_day(nowdate())
+ month_end_date = get_last_day(nowdate())
+ first_sunday = frappe.db.sql("""
+ select holiday_date from `tabHoliday`
+ where parent = %s
+ and holiday_date between %s and %s
+ order by holiday_date
+ """, (holiday_list, month_start_date, month_end_date))[0][0]
+
+ return first_sunday
\ No newline at end of file
diff --git a/erpnext/loan_management/doctype/loan/loan.js b/erpnext/loan_management/doctype/loan/loan.js
index f9c201ab603..940a1bbc000 100644
--- a/erpnext/loan_management/doctype/loan/loan.js
+++ b/erpnext/loan_management/doctype/loan/loan.js
@@ -46,7 +46,7 @@ frappe.ui.form.on('Loan', {
});
});
- $.each(["payment_account", "loan_account"], function (i, field) {
+ $.each(["payment_account", "loan_account", "disbursement_account"], function (i, field) {
frm.set_query(field, function () {
return {
"filters": {
@@ -88,6 +88,10 @@ frappe.ui.form.on('Loan', {
frm.add_custom_button(__('Loan Write Off'), function() {
frm.trigger("make_loan_write_off_entry");
},__('Create'));
+
+ frm.add_custom_button(__('Loan Refund'), function() {
+ frm.trigger("make_loan_refund");
+ },__('Create'));
}
}
frm.trigger("toggle_fields");
@@ -155,6 +159,21 @@ frappe.ui.form.on('Loan', {
})
},
+ make_loan_refund: function(frm) {
+ frappe.call({
+ args: {
+ "loan": frm.doc.name
+ },
+ method: "erpnext.loan_management.doctype.loan.loan.make_refund_jv",
+ callback: function (r) {
+ if (r.message) {
+ let doc = frappe.model.sync(r.message)[0];
+ frappe.set_route("Form", doc.doctype, doc.name);
+ }
+ }
+ })
+ },
+
request_loan_closure: function(frm) {
frappe.confirm(__("Do you really want to close this loan"),
function() {
diff --git a/erpnext/loan_management/doctype/loan/loan.json b/erpnext/loan_management/doctype/loan/loan.json
index fe94e2cadd6..dd723f38bdf 100644
--- a/erpnext/loan_management/doctype/loan/loan.json
+++ b/erpnext/loan_management/doctype/loan/loan.json
@@ -2,7 +2,7 @@
"actions": [],
"allow_import": 1,
"autoname": "ACC-LOAN-.YYYY.-.#####",
- "creation": "2019-08-29 17:29:18.176786",
+ "creation": "2022-01-25 10:30:02.294967",
"doctype": "DocType",
"document_type": "Document",
"editable_grid": 1,
@@ -34,6 +34,7 @@
"is_term_loan",
"account_info",
"mode_of_payment",
+ "disbursement_account",
"payment_account",
"column_break_9",
"loan_account",
@@ -356,12 +357,21 @@
"fieldtype": "Date",
"label": "Closure Date",
"read_only": 1
+ },
+ {
+ "fetch_from": "loan_type.disbursement_account",
+ "fieldname": "disbursement_account",
+ "fieldtype": "Link",
+ "label": "Disbursement Account",
+ "options": "Account",
+ "read_only": 1,
+ "reqd": 1
}
],
"index_web_pages_for_search": 1,
"is_submittable": 1,
"links": [],
- "modified": "2021-10-20 08:28:16.796105",
+ "modified": "2022-01-25 16:29:16.325501",
"modified_by": "Administrator",
"module": "Loan Management",
"name": "Loan",
@@ -391,5 +401,6 @@
"search_fields": "posting_date",
"sort_field": "creation",
"sort_order": "DESC",
+ "states": [],
"track_changes": 1
}
diff --git a/erpnext/loan_management/doctype/loan/loan.py b/erpnext/loan_management/doctype/loan/loan.py
index 94e0c55a775..f3914d51286 100644
--- a/erpnext/loan_management/doctype/loan/loan.py
+++ b/erpnext/loan_management/doctype/loan/loan.py
@@ -11,6 +11,7 @@ from frappe.utils import add_months, flt, get_last_day, getdate, now_datetime, n
from six import string_types
import erpnext
+from erpnext.accounts.doctype.journal_entry.journal_entry import get_payment_entry
from erpnext.controllers.accounts_controller import AccountsController
from erpnext.loan_management.doctype.loan_repayment.loan_repayment import calculate_amounts
from erpnext.loan_management.doctype.loan_security_unpledge.loan_security_unpledge import (
@@ -234,17 +235,15 @@ def request_loan_closure(loan, posting_date=None):
loan_type = frappe.get_value('Loan', loan, 'loan_type')
write_off_limit = frappe.get_value('Loan Type', loan_type, 'write_off_amount')
- # checking greater than 0 as there may be some minor precision error
- if not pending_amount:
- frappe.db.set_value('Loan', loan, 'status', 'Loan Closure Requested')
- elif pending_amount < write_off_limit:
+ if pending_amount and abs(pending_amount) < write_off_limit:
# Auto create loan write off and update status as loan closure requested
write_off = make_loan_write_off(loan)
write_off.submit()
- frappe.db.set_value('Loan', loan, 'status', 'Loan Closure Requested')
- else:
+ elif pending_amount > 0:
frappe.throw(_("Cannot close loan as there is an outstanding of {0}").format(pending_amount))
+ frappe.db.set_value('Loan', loan, 'status', 'Loan Closure Requested')
+
@frappe.whitelist()
def get_loan_application(loan_application):
loan = frappe.get_doc("Loan Application", loan_application)
@@ -401,4 +400,39 @@ def add_single_month(date):
if getdate(date) == get_last_day(date):
return get_last_day(add_months(date, 1))
else:
- return add_months(date, 1)
\ No newline at end of file
+ return add_months(date, 1)
+
+@frappe.whitelist()
+def make_refund_jv(loan, amount=0, reference_number=None, reference_date=None, submit=0):
+ loan_details = frappe.db.get_value('Loan', loan, ['applicant_type', 'applicant',
+ 'loan_account', 'payment_account', 'posting_date', 'company', 'name',
+ 'total_payment', 'total_principal_paid'], as_dict=1)
+
+ loan_details.doctype = 'Loan'
+ loan_details[loan_details.applicant_type.lower()] = loan_details.applicant
+
+ if not amount:
+ amount = flt(loan_details.total_principal_paid - loan_details.total_payment)
+
+ if amount < 0:
+ frappe.throw(_('No excess amount pending for refund'))
+
+ refund_jv = get_payment_entry(loan_details, {
+ "party_type": loan_details.applicant_type,
+ "party_account": loan_details.loan_account,
+ "amount_field_party": 'debit_in_account_currency',
+ "amount_field_bank": 'credit_in_account_currency',
+ "amount": amount,
+ "bank_account": loan_details.payment_account
+ })
+
+ if reference_number:
+ refund_jv.cheque_no = reference_number
+
+ if reference_date:
+ refund_jv.cheque_date = reference_date
+
+ if submit:
+ refund_jv.submit()
+
+ return refund_jv
\ No newline at end of file
diff --git a/erpnext/loan_management/doctype/loan/test_loan.py b/erpnext/loan_management/doctype/loan/test_loan.py
index 1676c218c87..5ebb2e1bdce 100644
--- a/erpnext/loan_management/doctype/loan/test_loan.py
+++ b/erpnext/loan_management/doctype/loan/test_loan.py
@@ -42,16 +42,17 @@ class TestLoan(unittest.TestCase):
create_loan_type("Personal Loan", 500000, 8.4,
is_term_loan=1,
mode_of_payment='Cash',
+ disbursement_account='Disbursement Account - _TC',
payment_account='Payment Account - _TC',
loan_account='Loan Account - _TC',
interest_income_account='Interest Income Account - _TC',
penalty_income_account='Penalty Income Account - _TC')
- create_loan_type("Stock Loan", 2000000, 13.5, 25, 1, 5, 'Cash', 'Payment Account - _TC', 'Loan Account - _TC',
- 'Interest Income Account - _TC', 'Penalty Income Account - _TC')
+ create_loan_type("Stock Loan", 2000000, 13.5, 25, 1, 5, 'Cash', 'Disbursement Account - _TC',
+ 'Payment Account - _TC', 'Loan Account - _TC', 'Interest Income Account - _TC', 'Penalty Income Account - _TC')
- create_loan_type("Demand Loan", 2000000, 13.5, 25, 0, 5, 'Cash', 'Payment Account - _TC', 'Loan Account - _TC',
- 'Interest Income Account - _TC', 'Penalty Income Account - _TC')
+ create_loan_type("Demand Loan", 2000000, 13.5, 25, 0, 5, 'Cash', 'Disbursement Account - _TC',
+ 'Payment Account - _TC', 'Loan Account - _TC', 'Interest Income Account - _TC', 'Penalty Income Account - _TC')
create_loan_security_type()
create_loan_security()
@@ -679,6 +680,29 @@ class TestLoan(unittest.TestCase):
loan.load_from_db()
self.assertEqual(loan.status, "Loan Closure Requested")
+ def test_loan_repayment_against_partially_disbursed_loan(self):
+ pledge = [{
+ "loan_security": "Test Security 1",
+ "qty": 4000.00
+ }]
+
+ loan_application = create_loan_application('_Test Company', self.applicant2, 'Demand Loan', pledge)
+ create_pledge(loan_application)
+
+ loan = create_demand_loan(self.applicant2, "Demand Loan", loan_application, posting_date='2019-10-01')
+ loan.submit()
+
+ first_date = '2019-10-01'
+ last_date = '2019-10-30'
+
+ make_loan_disbursement_entry(loan.name, loan.loan_amount/2, disbursement_date=first_date)
+
+ loan.load_from_db()
+
+ self.assertEqual(loan.status, "Partially Disbursed")
+ create_repayment_entry(loan.name, self.applicant2, add_days(last_date, 5),
+ flt(loan.loan_amount/3))
+
def test_loan_amount_write_off(self):
pledge = [{
"loan_security": "Test Security 1",
@@ -790,6 +814,18 @@ def create_loan_accounts():
"account_type": "Bank",
}).insert(ignore_permissions=True)
+ if not frappe.db.exists("Account", "Disbursement Account - _TC"):
+ frappe.get_doc({
+ "doctype": "Account",
+ "company": "_Test Company",
+ "account_name": "Disbursement Account",
+ "root_type": "Asset",
+ "report_type": "Balance Sheet",
+ "currency": "INR",
+ "parent_account": "Bank Accounts - _TC",
+ "account_type": "Bank",
+ }).insert(ignore_permissions=True)
+
if not frappe.db.exists("Account", "Interest Income Account - _TC"):
frappe.get_doc({
"doctype": "Account",
@@ -815,7 +851,7 @@ def create_loan_accounts():
}).insert(ignore_permissions=True)
def create_loan_type(loan_name, maximum_loan_amount, rate_of_interest, penalty_interest_rate=None, is_term_loan=None, grace_period_in_days=None,
- mode_of_payment=None, payment_account=None, loan_account=None, interest_income_account=None, penalty_income_account=None,
+ mode_of_payment=None, disbursement_account=None, payment_account=None, loan_account=None, interest_income_account=None, penalty_income_account=None,
repayment_method=None, repayment_periods=None):
if not frappe.db.exists("Loan Type", loan_name):
@@ -829,6 +865,7 @@ def create_loan_type(loan_name, maximum_loan_amount, rate_of_interest, penalty_i
"penalty_interest_rate": penalty_interest_rate,
"grace_period_in_days": grace_period_in_days,
"mode_of_payment": mode_of_payment,
+ "disbursement_account": disbursement_account,
"payment_account": payment_account,
"loan_account": loan_account,
"interest_income_account": interest_income_account,
diff --git a/erpnext/loan_management/doctype/loan_application/test_loan_application.py b/erpnext/loan_management/doctype/loan_application/test_loan_application.py
index d367e92ac49..640709c095f 100644
--- a/erpnext/loan_management/doctype/loan_application/test_loan_application.py
+++ b/erpnext/loan_management/doctype/loan_application/test_loan_application.py
@@ -15,7 +15,7 @@ from erpnext.payroll.doctype.salary_structure.test_salary_structure import (
class TestLoanApplication(unittest.TestCase):
def setUp(self):
create_loan_accounts()
- create_loan_type("Home Loan", 500000, 9.2, 0, 1, 0, 'Cash', 'Payment Account - _TC', 'Loan Account - _TC',
+ create_loan_type("Home Loan", 500000, 9.2, 0, 1, 0, 'Cash', 'Disbursement Account - _TC', 'Payment Account - _TC', 'Loan Account - _TC',
'Interest Income Account - _TC', 'Penalty Income Account - _TC', 'Repay Over Number of Periods', 18)
self.applicant = make_employee("kate_loan@loan.com", "_Test Company")
make_salary_structure("Test Salary Structure Loan", "Monthly", employee=self.applicant, currency='INR')
diff --git a/erpnext/loan_management/doctype/loan_disbursement/loan_disbursement.py b/erpnext/loan_management/doctype/loan_disbursement/loan_disbursement.py
index e2d758b1b90..df3aadfb18d 100644
--- a/erpnext/loan_management/doctype/loan_disbursement/loan_disbursement.py
+++ b/erpnext/loan_management/doctype/loan_disbursement/loan_disbursement.py
@@ -122,7 +122,7 @@ class LoanDisbursement(AccountsController):
gle_map.append(
self.get_gl_dict({
"account": loan_details.loan_account,
- "against": loan_details.payment_account,
+ "against": loan_details.disbursement_account,
"debit": self.disbursed_amount,
"debit_in_account_currency": self.disbursed_amount,
"against_voucher_type": "Loan",
@@ -137,7 +137,7 @@ class LoanDisbursement(AccountsController):
gle_map.append(
self.get_gl_dict({
- "account": loan_details.payment_account,
+ "account": loan_details.disbursement_account,
"against": loan_details.loan_account,
"credit": self.disbursed_amount,
"credit_in_account_currency": self.disbursed_amount,
diff --git a/erpnext/loan_management/doctype/loan_disbursement/test_loan_disbursement.py b/erpnext/loan_management/doctype/loan_disbursement/test_loan_disbursement.py
index 94ec84ea5db..10be750b449 100644
--- a/erpnext/loan_management/doctype/loan_disbursement/test_loan_disbursement.py
+++ b/erpnext/loan_management/doctype/loan_disbursement/test_loan_disbursement.py
@@ -44,8 +44,8 @@ class TestLoanDisbursement(unittest.TestCase):
def setUp(self):
create_loan_accounts()
- create_loan_type("Demand Loan", 2000000, 13.5, 25, 0, 5, 'Cash', 'Payment Account - _TC', 'Loan Account - _TC',
- 'Interest Income Account - _TC', 'Penalty Income Account - _TC')
+ create_loan_type("Demand Loan", 2000000, 13.5, 25, 0, 5, 'Cash', 'Disbursement Account - _TC',
+ 'Payment Account - _TC', 'Loan Account - _TC', 'Interest Income Account - _TC', 'Penalty Income Account - _TC')
create_loan_security_type()
create_loan_security()
diff --git a/erpnext/loan_management/doctype/loan_interest_accrual/test_loan_interest_accrual.py b/erpnext/loan_management/doctype/loan_interest_accrual/test_loan_interest_accrual.py
index 46aaaad9fd2..e8c77506fcb 100644
--- a/erpnext/loan_management/doctype/loan_interest_accrual/test_loan_interest_accrual.py
+++ b/erpnext/loan_management/doctype/loan_interest_accrual/test_loan_interest_accrual.py
@@ -30,8 +30,8 @@ class TestLoanInterestAccrual(unittest.TestCase):
def setUp(self):
create_loan_accounts()
- create_loan_type("Demand Loan", 2000000, 13.5, 25, 0, 5, 'Cash', 'Payment Account - _TC', 'Loan Account - _TC',
- 'Interest Income Account - _TC', 'Penalty Income Account - _TC')
+ create_loan_type("Demand Loan", 2000000, 13.5, 25, 0, 5, 'Cash', 'Disbursement Account - _TC',
+ 'Payment Account - _TC', 'Loan Account - _TC', 'Interest Income Account - _TC', 'Penalty Income Account - _TC')
create_loan_security_type()
create_loan_security()
diff --git a/erpnext/loan_management/doctype/loan_repayment/loan_repayment.py b/erpnext/loan_management/doctype/loan_repayment/loan_repayment.py
index 65099da49b4..56ee2c0225c 100644
--- a/erpnext/loan_management/doctype/loan_repayment/loan_repayment.py
+++ b/erpnext/loan_management/doctype/loan_repayment/loan_repayment.py
@@ -126,7 +126,7 @@ class LoanRepayment(AccountsController):
def update_paid_amount(self):
loan = frappe.get_value("Loan", self.against_loan, ['total_amount_paid', 'total_principal_paid',
- 'status', 'is_secured_loan', 'total_payment', 'loan_amount', 'total_interest_payable',
+ 'status', 'is_secured_loan', 'total_payment', 'loan_amount', 'disbursed_amount', 'total_interest_payable',
'written_off_amount'], as_dict=1)
loan.update({
@@ -154,7 +154,7 @@ class LoanRepayment(AccountsController):
def mark_as_unpaid(self):
loan = frappe.get_value("Loan", self.against_loan, ['total_amount_paid', 'total_principal_paid',
- 'status', 'is_secured_loan', 'total_payment', 'loan_amount', 'total_interest_payable',
+ 'status', 'is_secured_loan', 'total_payment', 'loan_amount', 'disbursed_amount', 'total_interest_payable',
'written_off_amount'], as_dict=1)
no_of_repayments = len(self.repayment_details)
diff --git a/erpnext/loan_management/doctype/loan_type/loan_type.js b/erpnext/loan_management/doctype/loan_type/loan_type.js
index 04c89c45499..9f9137cfbcd 100644
--- a/erpnext/loan_management/doctype/loan_type/loan_type.js
+++ b/erpnext/loan_management/doctype/loan_type/loan_type.js
@@ -15,7 +15,7 @@ frappe.ui.form.on('Loan Type', {
});
});
- $.each(["payment_account", "loan_account"], function (i, field) {
+ $.each(["payment_account", "loan_account", "disbursement_account"], function (i, field) {
frm.set_query(field, function () {
return {
"filters": {
diff --git a/erpnext/loan_management/doctype/loan_type/loan_type.json b/erpnext/loan_management/doctype/loan_type/loan_type.json
index c0a5d2cda12..00337e4b4c3 100644
--- a/erpnext/loan_management/doctype/loan_type/loan_type.json
+++ b/erpnext/loan_management/doctype/loan_type/loan_type.json
@@ -19,9 +19,10 @@
"description",
"account_details_section",
"mode_of_payment",
+ "disbursement_account",
"payment_account",
- "loan_account",
"column_break_12",
+ "loan_account",
"interest_income_account",
"penalty_income_account",
"amended_from"
@@ -79,7 +80,7 @@
{
"fieldname": "payment_account",
"fieldtype": "Link",
- "label": "Payment Account",
+ "label": "Repayment Account",
"options": "Account",
"reqd": 1
},
@@ -149,15 +150,23 @@
"fieldtype": "Currency",
"label": "Auto Write Off Amount ",
"options": "Company:company:default_currency"
+ },
+ {
+ "fieldname": "disbursement_account",
+ "fieldtype": "Link",
+ "label": "Disbursement Account",
+ "options": "Account",
+ "reqd": 1
}
],
"index_web_pages_for_search": 1,
"is_submittable": 1,
"links": [],
- "modified": "2021-04-19 18:10:57.368490",
+ "modified": "2022-01-25 16:23:57.009349",
"modified_by": "Administrator",
"module": "Loan Management",
"name": "Loan Type",
+ "naming_rule": "By fieldname",
"owner": "Administrator",
"permissions": [
{
@@ -181,5 +190,6 @@
}
],
"sort_field": "modified",
- "sort_order": "DESC"
+ "sort_order": "DESC",
+ "states": []
}
\ No newline at end of file
diff --git a/erpnext/manufacturing/doctype/bom/bom.json b/erpnext/manufacturing/doctype/bom/bom.json
index 218ac64d8da..0b441969400 100644
--- a/erpnext/manufacturing/doctype/bom/bom.json
+++ b/erpnext/manufacturing/doctype/bom/bom.json
@@ -37,7 +37,6 @@
"inspection_required",
"quality_inspection_template",
"column_break_31",
- "bom_level",
"section_break_33",
"items",
"scrap_section",
@@ -522,13 +521,6 @@
"fieldname": "column_break_31",
"fieldtype": "Column Break"
},
- {
- "default": "0",
- "fieldname": "bom_level",
- "fieldtype": "Int",
- "label": "BOM Level",
- "read_only": 1
- },
{
"fieldname": "section_break_33",
"fieldtype": "Section Break",
@@ -540,7 +532,7 @@
"image_field": "image",
"is_submittable": 1,
"links": [],
- "modified": "2021-11-18 13:04:16.271975",
+ "modified": "2022-01-30 21:27:54.727298",
"modified_by": "Administrator",
"module": "Manufacturing",
"name": "BOM",
@@ -577,5 +569,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/manufacturing/doctype/bom/bom.py b/erpnext/manufacturing/doctype/bom/bom.py
index f75038eab34..b97dcab632f 100644
--- a/erpnext/manufacturing/doctype/bom/bom.py
+++ b/erpnext/manufacturing/doctype/bom/bom.py
@@ -149,14 +149,13 @@ class BOM(WebsiteGenerator):
self.set_bom_material_details()
self.set_bom_scrap_items_detail()
self.validate_materials()
+ self.validate_transfer_against()
self.set_routing_operations()
self.validate_operations()
self.calculate_cost()
self.update_stock_qty()
self.validate_scrap_items()
self.update_cost(update_parent=False, from_child_bom=True, update_hour_rate = False, save=False)
- self.set_bom_level()
-
def get_context(self, context):
context.parents = [{'name': 'boms', 'title': _('All BOMs') }]
@@ -682,6 +681,12 @@ class BOM(WebsiteGenerator):
if act_pbom and act_pbom[0][0]:
frappe.throw(_("Cannot deactivate or cancel BOM as it is linked with other BOMs"))
+ def validate_transfer_against(self):
+ if not self.with_operations:
+ self.transfer_material_against = "Work Order"
+ if not self.transfer_material_against and not self.is_new():
+ frappe.throw(_("Setting {} is required").format(self.meta.get_label("transfer_material_against")), title=_("Missing value"))
+
def set_routing_operations(self):
if self.routing and self.with_operations and not self.operations:
self.get_routing()
@@ -697,7 +702,6 @@ class BOM(WebsiteGenerator):
if not d.batch_size or d.batch_size <= 0:
d.batch_size = 1
-
def validate_scrap_items(self):
for item in self.scrap_items:
msg = ""
@@ -728,20 +732,6 @@ class BOM(WebsiteGenerator):
"""Get a complete tree representation preserving order of child items."""
return BOMTree(self.name)
- def set_bom_level(self, update=False):
- levels = []
-
- self.bom_level = 0
- for row in self.items:
- if row.bom_no:
- levels.append(frappe.get_cached_value("BOM", row.bom_no, "bom_level") or 0)
-
- if levels:
- self.bom_level = max(levels) + 1
-
- if update:
- self.db_set("bom_level", self.bom_level)
-
def get_bom_item_rate(args, bom_doc):
if bom_doc.rm_cost_as_per == 'Valuation Rate':
rate = get_valuation_rate(args) * (args.get("conversion_factor") or 1)
diff --git a/erpnext/manufacturing/doctype/bom/test_bom.py b/erpnext/manufacturing/doctype/bom/test_bom.py
index 2f9804d1d4a..bfafacdfb57 100644
--- a/erpnext/manufacturing/doctype/bom/test_bom.py
+++ b/erpnext/manufacturing/doctype/bom/test_bom.py
@@ -356,6 +356,36 @@ class TestBOM(ERPNextTestCase):
self.assertTrue(0 < len(filtered) <= 3, msg="Item filtering showing excessive results")
+ def test_valid_transfer_defaults(self):
+ bom_with_op = frappe.db.get_value("BOM", {"item": "_Test FG Item 2", "with_operations": 1, "is_active": 1})
+ bom = frappe.copy_doc(frappe.get_doc("BOM", bom_with_op), ignore_no_copy=False)
+
+ # test defaults
+ bom.docstatus = 0
+ bom.transfer_material_against = None
+ bom.insert()
+ self.assertEqual(bom.transfer_material_against, "Work Order")
+
+ bom.reload()
+ bom.transfer_material_against = None
+ with self.assertRaises(frappe.ValidationError):
+ bom.save()
+ bom.reload()
+
+ # test saner default
+ bom.transfer_material_against = "Job Card"
+ bom.with_operations = 0
+ bom.save()
+ self.assertEqual(bom.transfer_material_against, "Work Order")
+
+ # test no value on existing doc
+ bom.transfer_material_against = None
+ bom.with_operations = 0
+ bom.save()
+ self.assertEqual(bom.transfer_material_against, "Work Order")
+ bom.delete()
+
+
def get_default_bom(item_code="_Test FG Item 2"):
return frappe.db.get_value("BOM", {"item": item_code, "is_active": 1, "is_default": 1})
diff --git a/erpnext/manufacturing/doctype/production_plan/production_plan.py b/erpnext/manufacturing/doctype/production_plan/production_plan.py
index e903e9baf84..f06624fe92c 100644
--- a/erpnext/manufacturing/doctype/production_plan/production_plan.py
+++ b/erpnext/manufacturing/doctype/production_plan/production_plan.py
@@ -560,9 +560,11 @@ class ProductionPlan(Document):
get_sub_assembly_items(row.bom_no, bom_data, row.planned_qty)
self.set_sub_assembly_items_based_on_level(row, bom_data, manufacturing_type)
- def set_sub_assembly_items_based_on_level(self, row, bom_data, manufacturing_type=None):
- bom_data = sorted(bom_data, key = lambda i: i.bom_level)
+ self.sub_assembly_items.sort(key= lambda d: d.bom_level, reverse=True)
+ for idx, row in enumerate(self.sub_assembly_items, start=1):
+ row.idx = idx
+ def set_sub_assembly_items_based_on_level(self, row, bom_data, manufacturing_type=None):
for data in bom_data:
data.qty = data.stock_qty
data.production_plan_item = row.name
@@ -949,6 +951,7 @@ def get_materials_from_other_locations(item, warehouses, new_mr_items, company):
warehouses, item.get("quantity"), company, ignore_validation=True)
required_qty = item.get("quantity")
+ # get available material by transferring to production warehouse
for d in locations:
if required_qty <=0: return
@@ -959,12 +962,14 @@ def get_materials_from_other_locations(item, warehouses, new_mr_items, company):
new_dict.update({
"quantity": quantity,
"material_request_type": "Material Transfer",
+ "uom": new_dict.get("stock_uom"), # internal transfer should be in stock UOM
"from_warehouse": d.get("warehouse")
})
required_qty -= quantity
new_mr_items.append(new_dict)
+ # raise purchase request for remaining qty
if required_qty:
stock_uom, purchase_uom = frappe.db.get_value(
'Item',
@@ -1002,9 +1007,6 @@ def get_sub_assembly_items(bom_no, bom_data, to_produce_qty, indent=0):
for d in data:
if d.expandable:
parent_item_code = frappe.get_cached_value("BOM", bom_no, "item")
- bom_level = (frappe.get_cached_value("BOM", d.value, "bom_level")
- if d.value else 0)
-
stock_qty = (d.stock_qty / d.parent_bom_qty) * flt(to_produce_qty)
bom_data.append(frappe._dict({
'parent_item_code': parent_item_code,
@@ -1015,7 +1017,7 @@ def get_sub_assembly_items(bom_no, bom_data, to_produce_qty, indent=0):
'uom': d.stock_uom,
'bom_no': d.value,
'is_sub_contracted_item': d.is_sub_contracted_item,
- 'bom_level': bom_level,
+ 'bom_level': indent,
'indent': indent,
'stock_qty': stock_qty
}))
diff --git a/erpnext/manufacturing/doctype/production_plan/test_production_plan.py b/erpnext/manufacturing/doctype/production_plan/test_production_plan.py
index 2febc1e23c0..21a126b2a79 100644
--- a/erpnext/manufacturing/doctype/production_plan/test_production_plan.py
+++ b/erpnext/manufacturing/doctype/production_plan/test_production_plan.py
@@ -347,6 +347,45 @@ class TestProductionPlan(ERPNextTestCase):
frappe.db.rollback()
+ def test_subassmebly_sorting(self):
+ """ Test subassembly sorting in case of multiple items with nested BOMs"""
+ from erpnext.manufacturing.doctype.bom.test_bom import create_nested_bom
+
+ prefix = "_TestLevel_"
+ boms = {
+ "Assembly": {
+ "SubAssembly1": {"ChildPart1": {}, "ChildPart2": {},},
+ "SubAssembly2": {"ChildPart3": {}},
+ "SubAssembly3": {"SubSubAssy1": {"ChildPart4": {}}},
+ "ChildPart5": {},
+ "ChildPart6": {},
+ "SubAssembly4": {"SubSubAssy2": {"ChildPart7": {}}},
+ },
+ "MegaDeepAssy": {
+ "SecretSubassy": {"SecretPart": {"VerySecret" : { "SuperSecret": {"Classified": {}}}},},
+ # ^ assert that this is
+ # first item in subassy table
+ }
+ }
+ create_nested_bom(boms, prefix=prefix)
+
+ items = [prefix + item_code for item_code in boms.keys()]
+ plan = create_production_plan(item_code=items[0], do_not_save=True)
+ plan.append("po_items", {
+ 'use_multi_level_bom': 1,
+ 'item_code': items[1],
+ 'bom_no': frappe.db.get_value('Item', items[1], 'default_bom'),
+ 'planned_qty': 1,
+ 'planned_start_date': now_datetime()
+ })
+ plan.get_sub_assembly_items()
+
+ bom_level_order = [d.bom_level for d in plan.sub_assembly_items]
+ self.assertEqual(bom_level_order, sorted(bom_level_order, reverse=True))
+ # lowest most level of subassembly should be first
+ self.assertIn("SuperSecret", plan.sub_assembly_items[0].production_item)
+
+
def create_production_plan(**args):
args = frappe._dict(args)
diff --git a/erpnext/manufacturing/doctype/production_plan_sub_assembly_item/production_plan_sub_assembly_item.json b/erpnext/manufacturing/doctype/production_plan_sub_assembly_item/production_plan_sub_assembly_item.json
index 657ee35a852..45ea26c3a8a 100644
--- a/erpnext/manufacturing/doctype/production_plan_sub_assembly_item/production_plan_sub_assembly_item.json
+++ b/erpnext/manufacturing/doctype/production_plan_sub_assembly_item/production_plan_sub_assembly_item.json
@@ -102,7 +102,6 @@
},
{
"columns": 1,
- "fetch_from": "bom_no.bom_level",
"fieldname": "bom_level",
"fieldtype": "Int",
"in_list_view": 1,
@@ -189,7 +188,7 @@
"index_web_pages_for_search": 1,
"istable": 1,
"links": [],
- "modified": "2021-06-28 20:10:56.296410",
+ "modified": "2022-01-30 21:31:10.527559",
"modified_by": "Administrator",
"module": "Manufacturing",
"name": "Production Plan Sub Assembly Item",
@@ -198,5 +197,6 @@
"quick_entry": 1,
"sort_field": "modified",
"sort_order": "DESC",
+ "states": [],
"track_changes": 1
}
\ No newline at end of file
diff --git a/erpnext/manufacturing/doctype/work_order/test_work_order.py b/erpnext/manufacturing/doctype/work_order/test_work_order.py
index 1c9cd0cbf4d..f3beabddcf9 100644
--- a/erpnext/manufacturing/doctype/work_order/test_work_order.py
+++ b/erpnext/manufacturing/doctype/work_order/test_work_order.py
@@ -870,6 +870,57 @@ class TestWorkOrder(ERPNextTestCase):
close_work_order(wo_order, "Closed")
self.assertEqual(wo_order.get('status'), "Closed")
+ def test_partial_manufacture_entries(self):
+ cancel_stock_entry = []
+
+ frappe.db.set_value("Manufacturing Settings", None,
+ "backflush_raw_materials_based_on", "Material Transferred for Manufacture")
+
+ wo_order = make_wo_order_test_record(planned_start_date=now(), qty=100)
+ ste1 = test_stock_entry.make_stock_entry(item_code="_Test Item",
+ target="_Test Warehouse - _TC", qty=120, basic_rate=5000.0)
+
+ ste2 = test_stock_entry.make_stock_entry(item_code="_Test Item Home Desktop 100",
+ target="_Test Warehouse - _TC", qty=240, basic_rate=1000.0)
+
+ cancel_stock_entry.extend([ste1.name, ste2.name])
+
+ sm = frappe.get_doc(make_stock_entry(wo_order.name, "Material Transfer for Manufacture", 100))
+ for row in sm.get('items'):
+ if row.get('item_code') == '_Test Item':
+ row.qty = 110
+
+ sm.submit()
+ cancel_stock_entry.append(sm.name)
+
+ s = frappe.get_doc(make_stock_entry(wo_order.name, "Manufacture", 90))
+ for row in s.get('items'):
+ if row.get('item_code') == '_Test Item':
+ self.assertEqual(row.get('qty'), 100)
+
+ s.submit()
+ cancel_stock_entry.append(s.name)
+
+ s1 = frappe.get_doc(make_stock_entry(wo_order.name, "Manufacture", 5))
+ for row in s1.get('items'):
+ if row.get('item_code') == '_Test Item':
+ self.assertEqual(row.get('qty'), 5)
+ s1.submit()
+ cancel_stock_entry.append(s1.name)
+
+ s2 = frappe.get_doc(make_stock_entry(wo_order.name, "Manufacture", 5))
+ for row in s2.get('items'):
+ if row.get('item_code') == '_Test Item':
+ self.assertEqual(row.get('qty'), 5)
+
+ cancel_stock_entry.reverse()
+ for ste in cancel_stock_entry:
+ doc = frappe.get_doc("Stock Entry", ste)
+ doc.cancel()
+
+ frappe.db.set_value("Manufacturing Settings", None,
+ "backflush_raw_materials_based_on", "BOM")
+
def update_job_card(job_card, jc_qty=None):
employee = frappe.db.get_value('Employee', {'status': 'Active'}, 'name')
diff --git a/erpnext/manufacturing/doctype/work_order/work_order.json b/erpnext/manufacturing/doctype/work_order/work_order.json
index 12cd58f418b..9452a63d70b 100644
--- a/erpnext/manufacturing/doctype/work_order/work_order.json
+++ b/erpnext/manufacturing/doctype/work_order/work_order.json
@@ -333,12 +333,13 @@
"options": "fa fa-wrench"
},
{
- "default": "Work Order",
"depends_on": "operations",
+ "fetch_from": "bom_no.transfer_material_against",
+ "fetch_if_empty": 1,
"fieldname": "transfer_material_against",
"fieldtype": "Select",
"label": "Transfer Material Against",
- "options": "Work Order\nJob Card"
+ "options": "\nWork Order\nJob Card"
},
{
"fieldname": "operations",
@@ -574,7 +575,7 @@
"image_field": "image",
"is_submittable": 1,
"links": [],
- "modified": "2021-11-08 17:36:07.016300",
+ "modified": "2022-01-24 21:18:12.160114",
"modified_by": "Administrator",
"module": "Manufacturing",
"name": "Work Order",
@@ -607,6 +608,7 @@
],
"sort_field": "modified",
"sort_order": "ASC",
+ "states": [],
"title_field": "production_item",
"track_changes": 1,
"track_seen": 1
diff --git a/erpnext/manufacturing/doctype/work_order/work_order.py b/erpnext/manufacturing/doctype/work_order/work_order.py
index 0090f4d04ee..b12e157390f 100644
--- a/erpnext/manufacturing/doctype/work_order/work_order.py
+++ b/erpnext/manufacturing/doctype/work_order/work_order.py
@@ -31,6 +31,7 @@ from erpnext.stock.doctype.batch.batch import make_batch
from erpnext.stock.doctype.item.item import get_item_defaults, validate_end_of_life
from erpnext.stock.doctype.serial_no.serial_no import (
auto_make_serial_nos,
+ clean_serial_no_string,
get_auto_serial_nos,
get_serial_nos,
)
@@ -65,6 +66,7 @@ class WorkOrder(Document):
self.validate_warehouse_belongs_to_company()
self.calculate_operating_cost()
self.validate_qty()
+ self.validate_transfer_against()
self.validate_operation_time()
self.status = self.get_status()
@@ -72,6 +74,7 @@ class WorkOrder(Document):
self.set_required_items(reset_only_qty = len(self.get("required_items")))
+
def validate_sales_order(self):
if self.sales_order:
self.check_sales_order_on_hold_or_close()
@@ -356,6 +359,7 @@ class WorkOrder(Document):
frappe.delete_doc("Batch", row.name)
def make_serial_nos(self, args):
+ self.serial_no = clean_serial_no_string(self.serial_no)
serial_no_series = frappe.get_cached_value("Item", self.production_item, "serial_no_series")
if serial_no_series:
self.serial_no = get_auto_serial_nos(serial_no_series, self.qty)
@@ -621,6 +625,16 @@ class WorkOrder(Document):
if not self.qty > 0:
frappe.throw(_("Quantity to Manufacture must be greater than 0."))
+ def validate_transfer_against(self):
+ if not self.docstatus == 1:
+ # let user configure operations until they're ready to submit
+ return
+ if not self.operations:
+ self.transfer_material_against = "Work Order"
+ if not self.transfer_material_against:
+ frappe.throw(_("Setting {} is required").format(self.meta.get_label("transfer_material_against")), title=_("Missing value"))
+
+
def validate_operation_time(self):
for d in self.operations:
if not d.time_in_mins > 0:
diff --git a/erpnext/manufacturing/report/bom_explorer/bom_explorer.py b/erpnext/manufacturing/report/bom_explorer/bom_explorer.py
index 25de2e03797..19a80ab4076 100644
--- a/erpnext/manufacturing/report/bom_explorer/bom_explorer.py
+++ b/erpnext/manufacturing/report/bom_explorer/bom_explorer.py
@@ -26,8 +26,7 @@ def get_exploded_items(bom, data, indent=0, qty=1):
'item_code': item.item_code,
'item_name': item.item_name,
'indent': indent,
- 'bom_level': (frappe.get_cached_value("BOM", item.bom_no, "bom_level")
- if item.bom_no else ""),
+ 'bom_level': indent,
'bom': item.bom_no,
'qty': item.qty * qty,
'uom': item.uom,
@@ -73,7 +72,7 @@ def get_columns():
},
{
"label": "BOM Level",
- "fieldtype": "Data",
+ "fieldtype": "Int",
"fieldname": "bom_level",
"width": 100
},
diff --git a/erpnext/manufacturing/report/bom_operations_time/bom_operations_time.js b/erpnext/manufacturing/report/bom_operations_time/bom_operations_time.js
index 7468e34020c..0eb22a22f73 100644
--- a/erpnext/manufacturing/report/bom_operations_time/bom_operations_time.js
+++ b/erpnext/manufacturing/report/bom_operations_time/bom_operations_time.js
@@ -4,6 +4,39 @@
frappe.query_reports["BOM Operations Time"] = {
"filters": [
-
+ {
+ "fieldname": "item_code",
+ "label": __("Item Code"),
+ "fieldtype": "Link",
+ "width": "100",
+ "options": "Item",
+ "get_query": () =>{
+ return {
+ filters: { "disabled": 0, "is_stock_item": 1 }
+ }
+ }
+ },
+ {
+ "fieldname": "bom_id",
+ "label": __("BOM ID"),
+ "fieldtype": "MultiSelectList",
+ "width": "100",
+ "options": "BOM",
+ "get_data": function(txt) {
+ return frappe.db.get_link_options("BOM", txt);
+ },
+ "get_query": () =>{
+ return {
+ filters: { "docstatus": 1, "is_active": 1, "with_operations": 1 }
+ }
+ }
+ },
+ {
+ "fieldname": "workstation",
+ "label": __("Workstation"),
+ "fieldtype": "Link",
+ "width": "100",
+ "options": "Workstation"
+ },
]
};
diff --git a/erpnext/manufacturing/report/bom_operations_time/bom_operations_time.json b/erpnext/manufacturing/report/bom_operations_time/bom_operations_time.json
index 665c5b9f79e..8162017ca81 100644
--- a/erpnext/manufacturing/report/bom_operations_time/bom_operations_time.json
+++ b/erpnext/manufacturing/report/bom_operations_time/bom_operations_time.json
@@ -1,14 +1,16 @@
{
- "add_total_row": 0,
+ "add_total_row": 1,
+ "columns": [],
"creation": "2020-03-03 01:41:20.862521",
"disable_prepared_report": 0,
"disabled": 0,
"docstatus": 0,
"doctype": "Report",
+ "filters": [],
"idx": 0,
"is_standard": "Yes",
"letter_head": "",
- "modified": "2020-03-03 01:41:20.862521",
+ "modified": "2022-01-20 14:21:47.771591",
"modified_by": "Administrator",
"module": "Manufacturing",
"name": "BOM Operations Time",
diff --git a/erpnext/manufacturing/report/bom_operations_time/bom_operations_time.py b/erpnext/manufacturing/report/bom_operations_time/bom_operations_time.py
index e7a818abd5d..eda9eb9d701 100644
--- a/erpnext/manufacturing/report/bom_operations_time/bom_operations_time.py
+++ b/erpnext/manufacturing/report/bom_operations_time/bom_operations_time.py
@@ -12,19 +12,15 @@ def execute(filters=None):
return columns, data
def get_data(filters):
- data = []
+ bom_wise_data = {}
+ bom_data, report_data = [], []
- bom_data = []
- for d in frappe.db.sql("""
- SELECT
- bom.name, bom.item, bom.item_name, bom.uom,
- bomps.operation, bomps.workstation, bomps.time_in_mins
- FROM `tabBOM` bom, `tabBOM Operation` bomps
- WHERE
- bom.docstatus = 1 and bom.is_active = 1 and bom.name = bomps.parent
- """, as_dict=1):
+ bom_operation_data = get_filtered_data(filters)
+
+ for d in bom_operation_data:
row = get_args()
if d.name not in bom_data:
+ bom_wise_data[d.name] = []
bom_data.append(d.name)
row.update(d)
else:
@@ -34,14 +30,49 @@ def get_data(filters):
"time_in_mins": d.time_in_mins
})
- data.append(row)
+ # maintain BOM wise data for grouping such as:
+ # {"BOM A": [{Row1}, {Row2}], "BOM B": ...}
+ bom_wise_data[d.name].append(row)
used_as_subassembly_items = get_bom_count(bom_data)
- for d in data:
- d.used_as_subassembly_items = used_as_subassembly_items.get(d.name, 0)
+ for d in bom_wise_data:
+ for row in bom_wise_data[d]:
+ row.used_as_subassembly_items = used_as_subassembly_items.get(row.name, 0)
+ report_data.append(row)
- return data
+ return report_data
+
+def get_filtered_data(filters):
+ bom = frappe.qb.DocType("BOM")
+ bom_ops = frappe.qb.DocType("BOM Operation")
+
+ bom_ops_query = (
+ frappe.qb.from_(bom)
+ .join(bom_ops).on(bom.name == bom_ops.parent)
+ .select(
+ bom.name, bom.item, bom.item_name, bom.uom,
+ bom_ops.operation, bom_ops.workstation, bom_ops.time_in_mins
+ ).where(
+ (bom.docstatus == 1)
+ & (bom.is_active == 1)
+ )
+ )
+
+ if filters.get("item_code"):
+ bom_ops_query = bom_ops_query.where(bom.item == filters.get("item_code"))
+
+ if filters.get("bom_id"):
+ bom_ops_query = bom_ops_query.where(bom.name.isin(filters.get("bom_id")))
+
+ if filters.get("workstation"):
+ bom_ops_query = bom_ops_query.where(
+ bom_ops.workstation == filters.get("workstation")
+ )
+
+ bom_operation_data = bom_ops_query.run(as_dict=True)
+
+ return bom_operation_data
def get_bom_count(bom_data):
data = frappe.get_all("BOM Item",
@@ -68,13 +99,13 @@ def get_columns(filters):
"options": "BOM",
"fieldname": "name",
"fieldtype": "Link",
- "width": 140
+ "width": 220
}, {
- "label": _("BOM Item Code"),
+ "label": _("Item Code"),
"options": "Item",
"fieldname": "item",
"fieldtype": "Link",
- "width": 140
+ "width": 150
}, {
"label": _("Item Name"),
"fieldname": "item_name",
@@ -85,13 +116,13 @@ def get_columns(filters):
"options": "UOM",
"fieldname": "uom",
"fieldtype": "Link",
- "width": 140
+ "width": 100
}, {
"label": _("Operation"),
"options": "Operation",
"fieldname": "operation",
"fieldtype": "Link",
- "width": 120
+ "width": 140
}, {
"label": _("Workstation"),
"options": "Workstation",
@@ -101,11 +132,11 @@ def get_columns(filters):
}, {
"label": _("Time (In Mins)"),
"fieldname": "time_in_mins",
- "fieldtype": "Int",
- "width": 140
+ "fieldtype": "Float",
+ "width": 120
}, {
"label": _("Sub-assembly BOM Count"),
"fieldname": "used_as_subassembly_items",
"fieldtype": "Int",
- "width": 180
+ "width": 200
}]
diff --git a/erpnext/manufacturing/report/cost_of_poor_quality_report/cost_of_poor_quality_report.js b/erpnext/manufacturing/report/cost_of_poor_quality_report/cost_of_poor_quality_report.js
index 97e7e0a7d20..72eed5e0d7c 100644
--- a/erpnext/manufacturing/report/cost_of_poor_quality_report/cost_of_poor_quality_report.js
+++ b/erpnext/manufacturing/report/cost_of_poor_quality_report/cost_of_poor_quality_report.js
@@ -17,14 +17,12 @@ frappe.query_reports["Cost of Poor Quality Report"] = {
fieldname:"from_date",
fieldtype: "Datetime",
default: frappe.datetime.convert_to_system_tz(frappe.datetime.add_months(frappe.datetime.now_datetime(), -1)),
- reqd: 1
},
{
label: __("To Date"),
fieldname:"to_date",
fieldtype: "Datetime",
default: frappe.datetime.now_datetime(),
- reqd: 1,
},
{
label: __("Job Card"),
diff --git a/erpnext/manufacturing/report/cost_of_poor_quality_report/cost_of_poor_quality_report.py b/erpnext/manufacturing/report/cost_of_poor_quality_report/cost_of_poor_quality_report.py
index 77418235b07..88b21170e8b 100644
--- a/erpnext/manufacturing/report/cost_of_poor_quality_report/cost_of_poor_quality_report.py
+++ b/erpnext/manufacturing/report/cost_of_poor_quality_report/cost_of_poor_quality_report.py
@@ -3,46 +3,65 @@
import frappe
from frappe import _
-from frappe.utils import flt
def execute(filters=None):
- columns, data = [], []
+ return get_columns(filters), get_data(filters)
- columns = get_columns(filters)
- data = get_data(filters)
-
- return columns, data
def get_data(report_filters):
data = []
operations = frappe.get_all("Operation", filters = {"is_corrective_operation": 1})
if operations:
- operations = [d.name for d in operations]
- fields = ["production_item as item_code", "item_name", "work_order", "operation",
- "workstation", "total_time_in_mins", "name", "hour_rate", "serial_no", "batch_no"]
+ if report_filters.get('operation'):
+ operations = [report_filters.get('operation')]
+ else:
+ operations = [d.name for d in operations]
- filters = get_filters(report_filters, operations)
+ job_card = frappe.qb.DocType("Job Card")
- job_cards = frappe.get_all("Job Card", fields = fields,
- filters = filters)
+ operating_cost = ((job_card.hour_rate) * (job_card.total_time_in_mins) / 60.0).as_('operating_cost')
+ item_code = (job_card.production_item).as_('item_code')
- for row in job_cards:
- row.operating_cost = flt(row.hour_rate) * (flt(row.total_time_in_mins) / 60.0)
- data.append(row)
+ query = (frappe.qb
+ .from_(job_card)
+ .select(job_card.name, job_card.work_order, item_code, job_card.item_name,
+ job_card.operation, job_card.serial_no, job_card.batch_no,
+ job_card.workstation, job_card.total_time_in_mins, job_card.hour_rate,
+ operating_cost)
+ .where(
+ (job_card.docstatus == 1)
+ & (job_card.is_corrective_job_card == 1))
+ .groupby(job_card.name)
+ )
+ query = append_filters(query, report_filters, operations, job_card)
+ data = query.run(as_dict=True)
return data
-def get_filters(report_filters, operations):
- filters = {"docstatus": 1, "operation": ("in", operations), "is_corrective_job_card": 1}
- for field in ["name", "work_order", "operation", "workstation", "company", "serial_no", "batch_no", "production_item"]:
- if report_filters.get(field):
- if field != 'serial_no':
- filters[field] = report_filters.get(field)
- else:
- filters[field] = ('like', '% {} %'.format(report_filters.get(field)))
+def append_filters(query, report_filters, operations, job_card):
+ """Append optional filters to query builder. """
- return filters
+ for field in ("name", "work_order", "operation", "workstation",
+ "company", "serial_no", "batch_no", "production_item"):
+ if report_filters.get(field):
+ if field == 'serial_no':
+ query = query.where(job_card[field].like('%{}%'.format(report_filters.get(field))))
+ elif field == 'operation':
+ query = query.where(job_card[field].isin(operations))
+ else:
+ query = query.where(job_card[field] == report_filters.get(field))
+
+ if report_filters.get('from_date') or report_filters.get('to_date'):
+ job_card_time_log = frappe.qb.DocType("Job Card Time Log")
+
+ query = query.join(job_card_time_log).on(job_card.name == job_card_time_log.parent)
+ if report_filters.get('from_date'):
+ query = query.where(job_card_time_log.from_time >= report_filters.get('from_date'))
+ if report_filters.get('to_date'):
+ query = query.where(job_card_time_log.to_time <= report_filters.get('to_date'))
+
+ return query
def get_columns(filters):
return [
diff --git a/erpnext/manufacturing/report/production_plan_summary/production_plan_summary.py b/erpnext/manufacturing/report/production_plan_summary/production_plan_summary.py
index 55b1a3f2f9a..aaa231466fd 100644
--- a/erpnext/manufacturing/report/production_plan_summary/production_plan_summary.py
+++ b/erpnext/manufacturing/report/production_plan_summary/production_plan_summary.py
@@ -48,7 +48,7 @@ def get_production_plan_item_details(filters, data, order_details):
"qty": row.planned_qty,
"document_type": "Work Order",
"document_name": work_order or "",
- "bom_level": frappe.get_cached_value("BOM", row.bom_no, "bom_level"),
+ "bom_level": 0,
"produced_qty": order_details.get((work_order, row.item_code), {}).get("produced_qty", 0),
"pending_qty": flt(row.planned_qty) - flt(order_details.get((work_order, row.item_code), {}).get("produced_qty", 0))
})
diff --git a/erpnext/manufacturing/report/production_planning_report/production_planning_report.py b/erpnext/manufacturing/report/production_planning_report/production_planning_report.py
index 8368db6374b..e1e7225e057 100644
--- a/erpnext/manufacturing/report/production_planning_report/production_planning_report.py
+++ b/erpnext/manufacturing/report/production_planning_report/production_planning_report.py
@@ -172,10 +172,15 @@ class ProductionPlanReport(object):
self.purchase_details = {}
- for d in frappe.get_all("Purchase Order Item",
+ purchased_items = frappe.get_all("Purchase Order Item",
fields=["item_code", "min(schedule_date) as arrival_date", "qty as arrival_qty", "warehouse"],
- filters = {"item_code": ("in", self.item_codes), "warehouse": ("in", self.warehouses)},
- group_by = "item_code, warehouse"):
+ filters={
+ "item_code": ("in", self.item_codes),
+ "warehouse": ("in", self.warehouses),
+ "docstatus": 1,
+ },
+ group_by = "item_code, warehouse")
+ for d in purchased_items:
key = (d.item_code, d.warehouse)
if key not in self.purchase_details:
self.purchase_details.setdefault(key, d)
diff --git a/erpnext/manufacturing/report/test_reports.py b/erpnext/manufacturing/report/test_reports.py
index 1de472659eb..9f51ded6c77 100644
--- a/erpnext/manufacturing/report/test_reports.py
+++ b/erpnext/manufacturing/report/test_reports.py
@@ -18,7 +18,7 @@ REPORT_FILTER_TEST_CASES: List[Tuple[ReportName, ReportFilters]] = [
("BOM Operations Time", {}),
("BOM Stock Calculated", {"bom": frappe.get_last_doc("BOM").name, "qty_to_make": 2}),
("BOM Stock Report", {"bom": frappe.get_last_doc("BOM").name, "qty_to_produce": 2}),
- ("Cost of Poor Quality Report", {}),
+ ("Cost of Poor Quality Report", {"item": "_Test Item", "serial_no": "00"}),
("Downtime Analysis", {}),
(
"Exponential Smoothing Forecasting",
diff --git a/erpnext/patches.txt b/erpnext/patches.txt
index 7110a7e308e..0a7eb33f66b 100644
--- a/erpnext/patches.txt
+++ b/erpnext/patches.txt
@@ -1,4 +1,5 @@
erpnext.patches.v12_0.update_is_cancelled_field
+erpnext.patches.v13_0.add_bin_unique_constraint
erpnext.patches.v11_0.rename_production_order_to_work_order
erpnext.patches.v11_0.refactor_naming_series
erpnext.patches.v11_0.refactor_autoname_naming
@@ -291,7 +292,6 @@ erpnext.patches.v13_0.set_training_event_attendance
erpnext.patches.v13_0.rename_issue_status_hold_to_on_hold
erpnext.patches.v13_0.bill_for_rejected_quantity_in_purchase_invoice
erpnext.patches.v13_0.update_job_card_details
-erpnext.patches.v13_0.update_level_in_bom #1234sswef
erpnext.patches.v13_0.create_gst_payment_entry_fields #27-11-2021
erpnext.patches.v13_0.add_missing_fg_item_for_stock_entry
erpnext.patches.v13_0.update_subscription_status_in_memberships
@@ -337,8 +337,14 @@ erpnext.patches.v13_0.item_naming_series_not_mandatory
erpnext.patches.v13_0.update_category_in_ltds_certificate
erpnext.patches.v13_0.create_ksa_vat_custom_fields
erpnext.patches.v13_0.rename_ksa_qr_field
+erpnext.patches.v13_0.wipe_serial_no_field_for_0_qty
erpnext.patches.v13_0.disable_ksa_print_format_for_others # 16-12-2021
erpnext.patches.v13_0.update_tax_category_for_rcm
erpnext.patches.v13_0.convert_to_website_item_in_item_card_group_template
erpnext.patches.v13_0.agriculture_deprecation_warning
erpnext.patches.v13_0.update_maintenance_schedule_field_in_visit
+erpnext.patches.v13_0.hospitality_deprecation_warning
+erpnext.patches.v13_0.delete_bank_reconciliation_detail
+erpnext.patches.v13_0.update_sane_transfer_against
+erpnext.patches.v13_0.enable_provisional_accounting
+erpnext.patches.v13_0.update_disbursement_account
\ No newline at end of file
diff --git a/erpnext/patches/v12_0/update_is_cancelled_field.py b/erpnext/patches/v12_0/update_is_cancelled_field.py
index 18787848dfd..06b6673a5d2 100644
--- a/erpnext/patches/v12_0/update_is_cancelled_field.py
+++ b/erpnext/patches/v12_0/update_is_cancelled_field.py
@@ -3,14 +3,28 @@ import frappe
def execute():
- try:
- frappe.db.sql("UPDATE `tabStock Ledger Entry` SET is_cancelled = 0 where is_cancelled in ('', NULL, 'No')")
- frappe.db.sql("UPDATE `tabSerial No` SET is_cancelled = 0 where is_cancelled in ('', NULL, 'No')")
+ #handle type casting for is_cancelled field
+ module_doctypes = (
+ ('stock', 'Stock Ledger Entry'),
+ ('stock', 'Serial No'),
+ ('accounts', 'GL Entry')
+ )
- frappe.db.sql("UPDATE `tabStock Ledger Entry` SET is_cancelled = 1 where is_cancelled = 'Yes'")
- frappe.db.sql("UPDATE `tabSerial No` SET is_cancelled = 1 where is_cancelled = 'Yes'")
+ for module, doctype in module_doctypes:
+ if (not frappe.db.has_column(doctype, "is_cancelled")
+ or frappe.db.get_column_type(doctype, "is_cancelled").lower() == "int(1)"
+ ):
+ continue
- frappe.reload_doc("stock", "doctype", "stock_ledger_entry")
- frappe.reload_doc("stock", "doctype", "serial_no")
- except Exception:
- pass
+ frappe.db.sql("""
+ UPDATE `tab{doctype}`
+ SET is_cancelled = 0
+ where is_cancelled in ('', NULL, 'No')"""
+ .format(doctype=doctype))
+ frappe.db.sql("""
+ UPDATE `tab{doctype}`
+ SET is_cancelled = 1
+ where is_cancelled = 'Yes'"""
+ .format(doctype=doctype))
+
+ frappe.reload_doc(module, "doctype", frappe.scrub(doctype))
diff --git a/erpnext/patches/v13_0/add_bin_unique_constraint.py b/erpnext/patches/v13_0/add_bin_unique_constraint.py
new file mode 100644
index 00000000000..57fbaae9d8d
--- /dev/null
+++ b/erpnext/patches/v13_0/add_bin_unique_constraint.py
@@ -0,0 +1,63 @@
+import frappe
+
+from erpnext.stock.stock_balance import (
+ get_balance_qty_from_sle,
+ get_indented_qty,
+ get_ordered_qty,
+ get_planned_qty,
+ get_reserved_qty,
+)
+from erpnext.stock.utils import get_bin
+
+
+def execute():
+ delete_broken_bins()
+ delete_and_patch_duplicate_bins()
+
+def delete_broken_bins():
+ # delete useless bins
+ frappe.db.sql("delete from `tabBin` where item_code is null or warehouse is null")
+
+def delete_and_patch_duplicate_bins():
+
+ duplicate_bins = frappe.db.sql("""
+ SELECT
+ item_code, warehouse, count(*) as bin_count
+ FROM
+ tabBin
+ GROUP BY
+ item_code, warehouse
+ HAVING
+ bin_count > 1
+ """, as_dict=1)
+
+ for duplicate_bin in duplicate_bins:
+ item_code = duplicate_bin.item_code
+ warehouse = duplicate_bin.warehouse
+ existing_bins = frappe.get_list("Bin",
+ filters={
+ "item_code": item_code,
+ "warehouse": warehouse
+ },
+ fields=["name"],
+ order_by="creation",)
+
+ # keep last one
+ existing_bins.pop()
+
+ for broken_bin in existing_bins:
+ frappe.delete_doc("Bin", broken_bin.name)
+
+ qty_dict = {
+ "reserved_qty": get_reserved_qty(item_code, warehouse),
+ "indented_qty": get_indented_qty(item_code, warehouse),
+ "ordered_qty": get_ordered_qty(item_code, warehouse),
+ "planned_qty": get_planned_qty(item_code, warehouse),
+ "actual_qty": get_balance_qty_from_sle(item_code, warehouse)
+ }
+
+ bin = get_bin(item_code, warehouse)
+ bin.update(qty_dict)
+ bin.update_reserved_qty_for_production()
+ bin.update_reserved_qty_for_sub_contracting()
+ bin.db_update()
diff --git a/erpnext/patches/v13_0/delete_bank_reconciliation_detail.py b/erpnext/patches/v13_0/delete_bank_reconciliation_detail.py
new file mode 100644
index 00000000000..75953b0e304
--- /dev/null
+++ b/erpnext/patches/v13_0/delete_bank_reconciliation_detail.py
@@ -0,0 +1,13 @@
+# Copyright (c) 2019, Frappe and Contributors
+# License: GNU General Public License v3. See license.txt
+
+
+import frappe
+
+
+def execute():
+
+ if frappe.db.exists('DocType', 'Bank Reconciliation Detail') and \
+ frappe.db.exists('DocType', 'Bank Clearance Detail'):
+
+ frappe.delete_doc("DocType", 'Bank Reconciliation Detail', force=1)
diff --git a/erpnext/patches/v13_0/delete_old_sales_reports.py b/erpnext/patches/v13_0/delete_old_sales_reports.py
index c597fe86457..e6eba0a6085 100644
--- a/erpnext/patches/v13_0/delete_old_sales_reports.py
+++ b/erpnext/patches/v13_0/delete_old_sales_reports.py
@@ -12,6 +12,7 @@ def execute():
for report in reports_to_delete:
if frappe.db.exists("Report", report):
+ delete_links_from_desktop_icons(report)
delete_auto_email_reports(report)
check_and_delete_linked_reports(report)
@@ -22,3 +23,9 @@ def delete_auto_email_reports(report):
auto_email_reports = frappe.db.get_values("Auto Email Report", {"report": report}, ["name"])
for auto_email_report in auto_email_reports:
frappe.delete_doc("Auto Email Report", auto_email_report[0])
+
+def delete_links_from_desktop_icons(report):
+ """ Check for one or multiple Desktop Icons and delete """
+ desktop_icons = frappe.db.get_values("Desktop Icon", {"_report": report}, ["name"])
+ for desktop_icon in desktop_icons:
+ frappe.delete_doc("Desktop Icon", desktop_icon[0])
\ No newline at end of file
diff --git a/erpnext/patches/v13_0/enable_provisional_accounting.py b/erpnext/patches/v13_0/enable_provisional_accounting.py
new file mode 100644
index 00000000000..85bbaed89df
--- /dev/null
+++ b/erpnext/patches/v13_0/enable_provisional_accounting.py
@@ -0,0 +1,22 @@
+import frappe
+
+
+def execute():
+ if not frappe.get_meta("Company").has_field("enable_perpetual_inventory_for_non_stock_items"):
+ return
+
+ frappe.reload_doc("setup", "doctype", "company")
+
+ company = frappe.qb.DocType("Company")
+
+ frappe.qb.update(
+ company
+ ).set(
+ company.enable_provisional_accounting_for_non_stock_items, company.enable_perpetual_inventory_for_non_stock_items
+ ).set(
+ company.default_provisional_account, company.service_received_but_not_billed
+ ).where(
+ company.enable_perpetual_inventory_for_non_stock_items == 1
+ ).where(
+ company.service_received_but_not_billed.isnotnull()
+ ).run()
\ No newline at end of file
diff --git a/erpnext/patches/v13_0/hospitality_deprecation_warning.py b/erpnext/patches/v13_0/hospitality_deprecation_warning.py
new file mode 100644
index 00000000000..9f9cf54f693
--- /dev/null
+++ b/erpnext/patches/v13_0/hospitality_deprecation_warning.py
@@ -0,0 +1,10 @@
+import click
+
+
+def execute():
+
+ click.secho(
+ "Hospitality Domain is moved to a separate app and will be removed from ERPNext in version-14.\n"
+ "When upgrading to ERPNext version-14, please install the app to continue using the Agriculture domain: https://github.com/frappe/hospitality",
+ fg="yellow",
+ )
diff --git a/erpnext/patches/v13_0/make_homepage_products_website_items.py b/erpnext/patches/v13_0/make_homepage_products_website_items.py
index bb0630aafdc..3ca20e2da86 100644
--- a/erpnext/patches/v13_0/make_homepage_products_website_items.py
+++ b/erpnext/patches/v13_0/make_homepage_products_website_items.py
@@ -13,4 +13,6 @@ def execute():
row.item_code = web_item
homepage.flags.ignore_mandatory = True
+ homepage.flags.ignore_links = True
+
homepage.save()
\ No newline at end of file
diff --git a/erpnext/patches/v13_0/setup_fields_for_80g_certificate_and_donation.py b/erpnext/patches/v13_0/setup_fields_for_80g_certificate_and_donation.py
index 7a2a2539670..2d35ea34587 100644
--- a/erpnext/patches/v13_0/setup_fields_for_80g_certificate_and_donation.py
+++ b/erpnext/patches/v13_0/setup_fields_for_80g_certificate_and_donation.py
@@ -5,6 +5,9 @@ from erpnext.regional.india.setup import make_custom_fields
def execute():
if frappe.get_all('Company', filters = {'country': 'India'}):
+ frappe.reload_doc('accounts', 'doctype', 'POS Invoice')
+ frappe.reload_doc('accounts', 'doctype', 'POS Invoice Item')
+
make_custom_fields()
if not frappe.db.exists('Party Type', 'Donor'):
diff --git a/erpnext/patches/v13_0/update_actual_start_and_end_date_in_wo.py b/erpnext/patches/v13_0/update_actual_start_and_end_date_in_wo.py
index 10ecd093069..9993063e485 100644
--- a/erpnext/patches/v13_0/update_actual_start_and_end_date_in_wo.py
+++ b/erpnext/patches/v13_0/update_actual_start_and_end_date_in_wo.py
@@ -38,4 +38,4 @@ def execute():
jc.production_item = wo.production_item, jc.item_name = wo.item_name
WHERE
jc.work_order = wo.name and IFNULL(jc.production_item, "") = ""
- """)
+ """)
\ No newline at end of file
diff --git a/erpnext/patches/v13_0/update_disbursement_account.py b/erpnext/patches/v13_0/update_disbursement_account.py
new file mode 100644
index 00000000000..c56fa8fdc62
--- /dev/null
+++ b/erpnext/patches/v13_0/update_disbursement_account.py
@@ -0,0 +1,22 @@
+import frappe
+
+
+def execute():
+
+ frappe.reload_doc("loan_management", "doctype", "loan_type")
+ frappe.reload_doc("loan_management", "doctype", "loan")
+
+ loan_type = frappe.qb.DocType("Loan Type")
+ loan = frappe.qb.DocType("Loan")
+
+ frappe.qb.update(
+ loan_type
+ ).set(
+ loan_type.disbursement_account, loan_type.payment_account
+ ).run()
+
+ frappe.qb.update(
+ loan
+ ).set(
+ loan.disbursement_account, loan.payment_account
+ ).run()
\ No newline at end of file
diff --git a/erpnext/patches/v13_0/update_level_in_bom.py b/erpnext/patches/v13_0/update_level_in_bom.py
deleted file mode 100644
index 499412ee270..00000000000
--- a/erpnext/patches/v13_0/update_level_in_bom.py
+++ /dev/null
@@ -1,31 +0,0 @@
-# Copyright (c) 2020, Frappe and Contributors
-# License: GNU General Public License v3. See license.txt
-
-
-import frappe
-
-
-def execute():
- for document in ["bom", "bom_item", "bom_explosion_item"]:
- frappe.reload_doc('manufacturing', 'doctype', document)
-
- frappe.db.sql(" update `tabBOM` set bom_level = 0 where docstatus = 1")
-
- bom_list = frappe.db.sql_list("""select name from `tabBOM` bom
- where docstatus=1 and is_active=1 and not exists(select bom_no from `tabBOM Item`
- where parent=bom.name and ifnull(bom_no, '')!='')""")
-
- count = 0
- while(count < len(bom_list)):
- for parent_bom in get_parent_boms(bom_list[count]):
- bom_doc = frappe.get_cached_doc("BOM", parent_bom)
- bom_doc.set_bom_level(update=True)
- bom_list.append(parent_bom)
- count += 1
-
-def get_parent_boms(bom_no):
- return frappe.db.sql_list("""
- select distinct bom_item.parent from `tabBOM Item` bom_item
- where bom_item.bom_no = %s and bom_item.docstatus=1 and bom_item.parenttype='BOM'
- and exists(select bom.name from `tabBOM` bom where bom.name=bom_item.parent and bom.is_active=1)
- """, bom_no)
diff --git a/erpnext/patches/v13_0/update_sane_transfer_against.py b/erpnext/patches/v13_0/update_sane_transfer_against.py
new file mode 100644
index 00000000000..a163d385843
--- /dev/null
+++ b/erpnext/patches/v13_0/update_sane_transfer_against.py
@@ -0,0 +1,11 @@
+import frappe
+
+
+def execute():
+ bom = frappe.qb.DocType("BOM")
+
+ (frappe.qb
+ .update(bom)
+ .set(bom.transfer_material_against, "Work Order")
+ .where(bom.with_operations == 0)
+ ).run()
diff --git a/erpnext/patches/v13_0/wipe_serial_no_field_for_0_qty.py b/erpnext/patches/v13_0/wipe_serial_no_field_for_0_qty.py
new file mode 100644
index 00000000000..e43a8bad8ea
--- /dev/null
+++ b/erpnext/patches/v13_0/wipe_serial_no_field_for_0_qty.py
@@ -0,0 +1,18 @@
+import frappe
+
+
+def execute():
+
+ doctype = "Stock Reconciliation Item"
+
+ if not frappe.db.has_column(doctype, "current_serial_no"):
+ # nothing to fix if column doesn't exist
+ return
+
+ sr_item = frappe.qb.DocType(doctype)
+
+ (frappe.qb
+ .update(sr_item)
+ .set(sr_item.current_serial_no, None)
+ .where(sr_item.current_qty == 0)
+ ).run()
diff --git a/erpnext/payroll/doctype/payroll_entry/test_payroll_entry.py b/erpnext/payroll/doctype/payroll_entry/test_payroll_entry.py
index c6f38972880..56a8bf727f2 100644
--- a/erpnext/payroll/doctype/payroll_entry/test_payroll_entry.py
+++ b/erpnext/payroll/doctype/payroll_entry/test_payroll_entry.py
@@ -197,6 +197,7 @@ class TestPayrollEntry(unittest.TestCase):
create_loan_type("Car Loan", 500000, 8.4,
is_term_loan=1,
mode_of_payment='Cash',
+ disbursement_account='Disbursement Account - _TC',
payment_account='Payment Account - _TC',
loan_account='Loan Account - _TC',
interest_income_account='Interest Income Account - _TC',
diff --git a/erpnext/payroll/doctype/salary_slip/test_salary_slip.py b/erpnext/payroll/doctype/salary_slip/test_salary_slip.py
index 0977561c9e6..ff573b5bdd4 100644
--- a/erpnext/payroll/doctype/salary_slip/test_salary_slip.py
+++ b/erpnext/payroll/doctype/salary_slip/test_salary_slip.py
@@ -374,6 +374,7 @@ class TestSalarySlip(unittest.TestCase):
create_loan_type("Car Loan", 500000, 8.4,
is_term_loan=1,
mode_of_payment='Cash',
+ disbursement_account='Disbursement Account - _TC',
payment_account='Payment Account - _TC',
loan_account='Loan Account - _TC',
interest_income_account='Interest Income Account - _TC',
@@ -999,6 +1000,8 @@ def make_leave_application(employee, from_date, to_date, leave_type, company=Non
))
leave_application.submit()
+ return leave_application
+
def setup_test():
make_earning_salary_component(setup=True, company_list=["_Test Company"])
make_deduction_salary_component(setup=True, company_list=["_Test Company"])
diff --git a/erpnext/projects/doctype/timesheet/test_timesheet.py b/erpnext/projects/doctype/timesheet/test_timesheet.py
index 148d8ba29c2..989bcd1670d 100644
--- a/erpnext/projects/doctype/timesheet/test_timesheet.py
+++ b/erpnext/projects/doctype/timesheet/test_timesheet.py
@@ -5,7 +5,7 @@ import datetime
import unittest
import frappe
-from frappe.utils import add_months, now_datetime, nowdate
+from frappe.utils import add_months, add_to_date, now_datetime, nowdate
from erpnext.accounts.doctype.sales_invoice.test_sales_invoice import create_sales_invoice
from erpnext.hr.doctype.employee.test_employee import make_employee
@@ -151,6 +151,27 @@ class TestTimesheet(unittest.TestCase):
settings.ignore_employee_time_overlap = initial_setting
settings.save()
+ def test_to_time(self):
+ emp = make_employee("test_employee_6@salary.com")
+ from_time = now_datetime()
+
+ timesheet = frappe.new_doc("Timesheet")
+ timesheet.employee = emp
+ timesheet.append(
+ 'time_logs',
+ {
+ "billable": 1,
+ "activity_type": "_Test Activity Type",
+ "from_time": from_time,
+ "hours": 2,
+ "company": "_Test Company"
+ }
+ )
+ timesheet.save()
+
+ to_time = timesheet.time_logs[0].to_time
+ self.assertEqual(to_time, add_to_date(from_time, hours=2, as_datetime=True))
+
def make_salary_structure_for_timesheet(employee, company=None):
salary_structure_name = "Timesheet Salary Structure Test"
diff --git a/erpnext/projects/doctype/timesheet/timesheet.py b/erpnext/projects/doctype/timesheet/timesheet.py
index e92785e06cf..dd0b5f90f4d 100644
--- a/erpnext/projects/doctype/timesheet/timesheet.py
+++ b/erpnext/projects/doctype/timesheet/timesheet.py
@@ -7,7 +7,7 @@ import json
import frappe
from frappe import _
from frappe.model.document import Document
-from frappe.utils import flt, getdate, time_diff_in_hours
+from frappe.utils import add_to_date, flt, getdate, time_diff_in_hours
from erpnext.controllers.queries import get_match_cond
from erpnext.hr.utils import validate_active_employee
@@ -136,10 +136,19 @@ class Timesheet(Document):
def validate_time_logs(self):
for data in self.get('time_logs'):
+ self.set_to_time(data)
self.validate_overlap(data)
self.set_project(data)
self.validate_project(data)
+ def set_to_time(self, data):
+ if not (data.from_time and data.hours):
+ return
+
+ _to_time = add_to_date(data.from_time, hours=data.hours, as_datetime=True)
+ if data.to_time != _to_time:
+ data.to_time = _to_time
+
def validate_overlap(self, data):
settings = frappe.get_single('Projects Settings')
self.validate_overlap_for("user", data, self.user, settings.ignore_user_time_overlap)
diff --git a/erpnext/public/js/controllers/buying.js b/erpnext/public/js/controllers/buying.js
index 613f93cc3f3..93169d972e4 100644
--- a/erpnext/public/js/controllers/buying.js
+++ b/erpnext/public/js/controllers/buying.js
@@ -441,7 +441,7 @@ erpnext.buying.get_items_from_product_bundle = function(frm) {
type: "GET",
method: "erpnext.stock.doctype.packed_item.packed_item.get_items_from_product_bundle",
args: {
- args: {
+ row: {
item_code: args.product_bundle,
quantity: args.quantity,
parenttype: frm.doc.doctype,
diff --git a/erpnext/public/js/utils/serial_no_batch_selector.js b/erpnext/public/js/utils/serial_no_batch_selector.js
index b5d3981ba7f..16e3fa0abd1 100644
--- a/erpnext/public/js/utils/serial_no_batch_selector.js
+++ b/erpnext/public/js/utils/serial_no_batch_selector.js
@@ -590,6 +590,6 @@ function check_can_calculate_pending_qty(me) {
&& doc.fg_completed_qty
&& erpnext.stock.bom
&& erpnext.stock.bom.name === doc.bom_no;
- const itemChecks = !!item;
+ const itemChecks = !!item && !item.allow_alternative_item;
return docChecks && itemChecks;
}
diff --git a/erpnext/public/scss/shopping_cart.scss b/erpnext/public/scss/shopping_cart.scss
index 429f4ca35df..b743504a527 100644
--- a/erpnext/public/scss/shopping_cart.scss
+++ b/erpnext/public/scss/shopping_cart.scss
@@ -590,7 +590,6 @@ body.product-page {
top: -10px;
left: -12px;
background: var(--red-600);
- width: 16px;
align-items: center;
height: 16px;
font-size: 10px;
diff --git a/erpnext/regional/india/utils.py b/erpnext/regional/india/utils.py
index 2287714a008..0126b090fca 100644
--- a/erpnext/regional/india/utils.py
+++ b/erpnext/regional/india/utils.py
@@ -221,6 +221,7 @@ def get_regional_address_details(party_details, doctype, company):
if not party_details.place_of_supply: return party_details
if not party_details.company_gstin: return party_details
+ if not party_details.supplier_gstin: return party_details
if ((doctype in ("Sales Invoice", "Delivery Note", "Sales Order") and party_details.company_gstin
and party_details.company_gstin[:2] != party_details.place_of_supply[:2]) or (doctype in ("Purchase Invoice",
diff --git a/erpnext/selling/doctype/customer/customer.py b/erpnext/selling/doctype/customer/customer.py
index c86e18ab7aa..df871491422 100644
--- a/erpnext/selling/doctype/customer/customer.py
+++ b/erpnext/selling/doctype/customer/customer.py
@@ -142,7 +142,7 @@ class Customer(TransactionBase):
self.update_lead_status()
if self.flags.is_new_doc:
- self.create_lead_address_contact()
+ self.link_lead_address_and_contact()
self.update_customer_groups()
@@ -176,63 +176,25 @@ class Customer(TransactionBase):
if self.lead_name:
frappe.db.set_value("Lead", self.lead_name, "status", "Converted")
- def create_lead_address_contact(self):
+ def link_lead_address_and_contact(self):
if self.lead_name:
- # assign lead address to customer (if already not set)
- address_names = frappe.get_all('Dynamic Link', filters={
- "parenttype":"Address",
- "link_doctype":"Lead",
- "link_name":self.lead_name
- }, fields=["parent as name"])
+ # assign lead address and contact to customer (if already not set)
+ linked_contacts_and_addresses = frappe.get_all(
+ "Dynamic Link",
+ filters=[
+ ["parenttype", "in", ["Contact", "Address"]],
+ ["link_doctype", "=", "Lead"],
+ ["link_name", "=", self.lead_name],
+ ],
+ fields=["parent as name", "parenttype as doctype"],
+ )
- for address_name in address_names:
- address = frappe.get_doc('Address', address_name.get('name'))
- if not address.has_link('Customer', self.name):
- address.append('links', dict(link_doctype='Customer', link_name=self.name))
- address.save(ignore_permissions=self.flags.ignore_permissions)
+ for row in linked_contacts_and_addresses:
+ linked_doc = frappe.get_doc(row.doctype, row.name)
+ if not linked_doc.has_link('Customer', self.name):
+ linked_doc.append('links', dict(link_doctype='Customer', link_name=self.name))
+ linked_doc.save(ignore_permissions=self.flags.ignore_permissions)
- lead = frappe.db.get_value("Lead", self.lead_name, ["organization_lead", "lead_name", "email_id", "phone", "mobile_no", "gender", "salutation"], as_dict=True)
-
- if not lead.lead_name:
- frappe.throw(_("Please mention the Lead Name in Lead {0}").format(self.lead_name))
-
- if lead.organization_lead:
- 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)
-
- else:
- lead.lead_name = lead.lead_name.lstrip().split(" ")
- lead.first_name = lead.lead_name[0]
- lead.last_name = " ".join(lead.lead_name[1:])
-
- # create contact from lead
- contact = frappe.new_doc('Contact')
- contact.first_name = lead.first_name
- contact.last_name = lead.last_name
- contact.gender = lead.gender
- contact.salutation = lead.salutation
- contact.email_id = lead.email_id
- contact.phone = lead.phone
- contact.mobile_no = lead.mobile_no
- contact.is_primary_contact = 1
- contact.append('links', dict(link_doctype='Customer', link_name=self.name))
- if lead.email_id:
- contact.append('email_ids', dict(email_id=lead.email_id, is_primary=1))
- if lead.mobile_no:
- contact.append('phone_nos', dict(phone=lead.mobile_no, is_primary_mobile_no=1))
- contact.flags.ignore_permissions = self.flags.ignore_permissions
- contact.autoname()
- if not frappe.db.exists("Contact", contact.name):
- contact.insert()
def validate_name_with_customer_group(self):
if frappe.db.exists("Customer Group", self.name):
diff --git a/erpnext/selling/doctype/sales_order/sales_order.js b/erpnext/selling/doctype/sales_order/sales_order.js
index 886ed071716..2d5bb2013f2 100644
--- a/erpnext/selling/doctype/sales_order/sales_order.js
+++ b/erpnext/selling/doctype/sales_order/sales_order.js
@@ -457,12 +457,8 @@ erpnext.selling.SalesOrderController = erpnext.selling.SellingController.extend(
make_delivery_note_based_on_delivery_date: function() {
var me = this;
- var delivery_dates = [];
- $.each(this.frm.doc.items || [], function(i, d) {
- if(!delivery_dates.includes(d.delivery_date)) {
- delivery_dates.push(d.delivery_date);
- }
- });
+ var delivery_dates = this.frm.doc.items.map(i => i.delivery_date);
+ delivery_dates = [ ...new Set(delivery_dates) ];
var item_grid = this.frm.fields_dict["items"].grid;
if(!item_grid.get_selected().length && delivery_dates.length > 1) {
@@ -500,14 +496,7 @@ erpnext.selling.SalesOrderController = erpnext.selling.SellingController.extend(
if(!dates) return;
- $.each(dates, function(i, d) {
- $.each(item_grid.grid_rows || [], function(j, row) {
- if(row.doc.delivery_date == d) {
- row.doc.__checked = 1;
- }
- });
- })
- me.make_delivery_note();
+ me.make_delivery_note(dates);
dialog.hide();
});
dialog.show();
@@ -516,10 +505,13 @@ erpnext.selling.SalesOrderController = erpnext.selling.SellingController.extend(
}
},
- make_delivery_note: function() {
+ make_delivery_note: function(delivery_dates) {
frappe.model.open_mapped_doc({
method: "erpnext.selling.doctype.sales_order.sales_order.make_delivery_note",
- frm: this.frm
+ frm: this.frm,
+ args: {
+ delivery_dates
+ }
})
},
diff --git a/erpnext/selling/doctype/sales_order/sales_order.py b/erpnext/selling/doctype/sales_order/sales_order.py
index 658691548f1..8336a143617 100755
--- a/erpnext/selling/doctype/sales_order/sales_order.py
+++ b/erpnext/selling/doctype/sales_order/sales_order.py
@@ -611,6 +611,13 @@ def make_delivery_note(source_name, target_doc=None, skip_item_mapping=False):
}
if not skip_item_mapping:
+ def condition(doc):
+ # make_mapped_doc sets js `args` into `frappe.flags.args`
+ if frappe.flags.args and frappe.flags.args.delivery_dates:
+ if cstr(doc.delivery_date) not in frappe.flags.args.delivery_dates:
+ return False
+ return abs(doc.delivered_qty) < abs(doc.qty) and doc.delivered_by_supplier!=1
+
mapper["Sales Order Item"] = {
"doctype": "Delivery Note Item",
"field_map": {
@@ -619,7 +626,7 @@ def make_delivery_note(source_name, target_doc=None, skip_item_mapping=False):
"parent": "against_sales_order",
},
"postprocess": update_item,
- "condition": lambda doc: abs(doc.delivered_qty) < abs(doc.qty) and doc.delivered_by_supplier!=1
+ "condition": condition
}
target_doc = get_mapped_doc("Sales Order", source_name, mapper, target_doc, set_missing_values)
diff --git a/erpnext/selling/report/customer_credit_balance/customer_credit_balance.py b/erpnext/selling/report/customer_credit_balance/customer_credit_balance.py
index 777b02ca66d..dd49f1355d2 100644
--- a/erpnext/selling/report/customer_credit_balance/customer_credit_balance.py
+++ b/erpnext/selling/report/customer_credit_balance/customer_credit_balance.py
@@ -23,19 +23,24 @@ def execute(filters=None):
row = []
outstanding_amt = get_customer_outstanding(d.name, filters.get("company"),
- ignore_outstanding_sales_order=d.bypass_credit_limit_check_at_sales_order)
+ ignore_outstanding_sales_order=d.bypass_credit_limit_check)
credit_limit = get_credit_limit(d.name, filters.get("company"))
bal = flt(credit_limit) - flt(outstanding_amt)
if customer_naming_type == "Naming Series":
- row = [d.name, d.customer_name, credit_limit, outstanding_amt, bal,
- d.bypass_credit_limit_check, d.is_frozen,
- d.disabled]
+ row = [
+ d.name, d.customer_name, credit_limit,
+ outstanding_amt, bal, d.bypass_credit_limit_check,
+ d.is_frozen, d.disabled
+ ]
else:
- row = [d.name, credit_limit, outstanding_amt, bal,
- d.bypass_credit_limit_check_at_sales_order, d.is_frozen, d.disabled]
+ row = [
+ d.name, credit_limit, outstanding_amt, bal,
+ d.bypass_credit_limit_check, d.is_frozen,
+ d.disabled
+ ]
if credit_limit:
data.append(row)
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 c530d30c0c2..001095588ba 100644
--- a/erpnext/selling/report/sales_order_analysis/sales_order_analysis.py
+++ b/erpnext/selling/report/sales_order_analysis/sales_order_analysis.py
@@ -80,7 +80,7 @@ def get_data(conditions, filters):
and so.docstatus = 1
{conditions}
GROUP BY soi.name
- ORDER BY so.transaction_date ASC
+ ORDER BY so.transaction_date ASC, soi.item_code ASC
""".format(conditions=conditions), filters, as_dict=1)
return data
diff --git a/erpnext/setup/doctype/company/company.json b/erpnext/setup/doctype/company/company.json
index dae64e4ad65..84bc6afa4d5 100644
--- a/erpnext/setup/doctype/company/company.json
+++ b/erpnext/setup/doctype/company/company.json
@@ -3,7 +3,7 @@
"allow_import": 1,
"allow_rename": 1,
"autoname": "field:company_name",
- "creation": "2013-04-10 08:35:39",
+ "creation": "2022-01-25 10:29:55.938239",
"description": "Legal Entity / Subsidiary with a separate Chart of Accounts belonging to the Organization.",
"doctype": "DocType",
"document_type": "Setup",
@@ -66,12 +66,12 @@
"payment_terms",
"auto_accounting_for_stock_settings",
"enable_perpetual_inventory",
- "enable_perpetual_inventory_for_non_stock_items",
+ "enable_provisional_accounting_for_non_stock_items",
"default_inventory_account",
"stock_adjustment_account",
"column_break_32",
"stock_received_but_not_billed",
- "service_received_but_not_billed",
+ "default_provisional_account",
"expenses_included_in_valuation",
"fixed_asset_defaults",
"accumulated_depreciation_account",
@@ -692,20 +692,6 @@
"label": "Default Buying Terms",
"options": "Terms and Conditions"
},
- {
- "fieldname": "service_received_but_not_billed",
- "fieldtype": "Link",
- "ignore_user_permissions": 1,
- "label": "Service Received But Not Billed",
- "no_copy": 1,
- "options": "Account"
- },
- {
- "default": "0",
- "fieldname": "enable_perpetual_inventory_for_non_stock_items",
- "fieldtype": "Check",
- "label": "Enable Perpetual Inventory For Non Stock Items"
- },
{
"fieldname": "default_in_transit_warehouse",
"fieldtype": "Link",
@@ -735,6 +721,25 @@
"fieldtype": "Link",
"label": "Repair and Maintenance Account",
"options": "Account"
+ },
+ {
+ "fieldname": "section_break_28",
+ "fieldtype": "Section Break",
+ "label": "Chart of Accounts"
+ },
+ {
+ "default": "0",
+ "fieldname": "enable_provisional_accounting_for_non_stock_items",
+ "fieldtype": "Check",
+ "label": "Enable Provisional Accounting For Non Stock Items"
+ },
+ {
+ "fieldname": "default_provisional_account",
+ "fieldtype": "Link",
+ "ignore_user_permissions": 1,
+ "label": "Default Provisional Account",
+ "no_copy": 1,
+ "options": "Account"
}
],
"icon": "fa fa-building",
@@ -742,7 +747,7 @@
"image_field": "company_logo",
"is_tree": 1,
"links": [],
- "modified": "2021-12-02 14:52:08.187233",
+ "modified": "2022-01-25 10:33:16.826067",
"modified_by": "Administrator",
"module": "Setup",
"name": "Company",
@@ -802,5 +807,6 @@
"show_name_in_global_search": 1,
"sort_field": "modified",
"sort_order": "ASC",
+ "states": [],
"track_changes": 1
}
\ No newline at end of file
diff --git a/erpnext/setup/doctype/company/company.py b/erpnext/setup/doctype/company/company.py
index 142fe04b6f2..3347935234c 100644
--- a/erpnext/setup/doctype/company/company.py
+++ b/erpnext/setup/doctype/company/company.py
@@ -11,6 +11,7 @@ import frappe.defaults
from frappe import _
from frappe.cache_manager import clear_defaults_cache
from frappe.contacts.address_and_contact import load_address_and_contact
+from frappe.custom.doctype.property_setter.property_setter import make_property_setter
from frappe.utils import cint, formatdate, get_timestamp, today
from frappe.utils.nestedset import NestedSet
from past.builtins import cmp
@@ -47,7 +48,7 @@ class Company(NestedSet):
self.validate_currency()
self.validate_coa_input()
self.validate_perpetual_inventory()
- self.validate_perpetual_inventory_for_non_stock_items()
+ self.validate_provisional_account_for_non_stock_items()
self.check_country_change()
self.check_parent_changed()
self.set_chart_of_accounts()
@@ -189,11 +190,14 @@ class Company(NestedSet):
frappe.msgprint(_("Set default inventory account for perpetual inventory"),
alert=True, indicator='orange')
- def validate_perpetual_inventory_for_non_stock_items(self):
+ def validate_provisional_account_for_non_stock_items(self):
if not self.get("__islocal"):
- if cint(self.enable_perpetual_inventory_for_non_stock_items) == 1 and not self.service_received_but_not_billed:
- frappe.throw(_("Set default {0} account for perpetual inventory for non stock items").format(
- frappe.bold('Service Received But Not Billed')))
+ if cint(self.enable_provisional_accounting_for_non_stock_items) == 1 and not self.default_provisional_account:
+ frappe.throw(_("Set default {0} account for non stock items").format(
+ frappe.bold('Provisional Account')))
+
+ make_property_setter("Purchase Receipt", "provisional_expense_account", "hidden",
+ not self.enable_provisional_accounting_for_non_stock_items, "Check", validate_fields_for_doctype=False)
def check_country_change(self):
frappe.flags.country_change = False
diff --git a/erpnext/stock/doctype/bin/bin.json b/erpnext/stock/doctype/bin/bin.json
index 8e79f0e5552..56dc71c57e1 100644
--- a/erpnext/stock/doctype/bin/bin.json
+++ b/erpnext/stock/doctype/bin/bin.json
@@ -33,6 +33,7 @@
"oldfieldtype": "Link",
"options": "Warehouse",
"read_only": 1,
+ "reqd": 1,
"search_index": 1
},
{
@@ -46,6 +47,7 @@
"oldfieldtype": "Link",
"options": "Item",
"read_only": 1,
+ "reqd": 1,
"search_index": 1
},
{
@@ -169,10 +171,11 @@
"idx": 1,
"in_create": 1,
"links": [],
- "modified": "2021-03-30 23:09:39.572776",
+ "modified": "2022-01-30 17:04:54.715288",
"modified_by": "Administrator",
"module": "Stock",
"name": "Bin",
+ "naming_rule": "Expression (old style)",
"owner": "Administrator",
"permissions": [
{
@@ -200,5 +203,6 @@
"quick_entry": 1,
"search_fields": "item_code,warehouse",
"sort_field": "modified",
- "sort_order": "ASC"
+ "sort_order": "ASC",
+ "states": []
}
\ No newline at end of file
diff --git a/erpnext/stock/doctype/bin/bin.py b/erpnext/stock/doctype/bin/bin.py
index 11ff359b483..1d874cd06fb 100644
--- a/erpnext/stock/doctype/bin/bin.py
+++ b/erpnext/stock/doctype/bin/bin.py
@@ -96,7 +96,7 @@ class Bin(Document):
self.db_set('projected_qty', self.projected_qty)
def on_doctype_update():
- frappe.db.add_index("Bin", ["item_code", "warehouse"])
+ frappe.db.add_unique("Bin", ["item_code", "warehouse"], constraint_name="unique_item_warehouse")
def update_stock(bin_name, args, allow_negative_stock=False, via_landed_cost_voucher=False):
diff --git a/erpnext/stock/doctype/bin/test_bin.py b/erpnext/stock/doctype/bin/test_bin.py
index 9c390d94b4e..250126c6b98 100644
--- a/erpnext/stock/doctype/bin/test_bin.py
+++ b/erpnext/stock/doctype/bin/test_bin.py
@@ -1,9 +1,36 @@
# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
# See license.txt
-import unittest
+import frappe
-# test_records = frappe.get_test_records('Bin')
+from erpnext.stock.doctype.item.test_item import make_item
+from erpnext.stock.utils import _create_bin
+from erpnext.tests.utils import ERPNextTestCase
-class TestBin(unittest.TestCase):
- pass
+
+class TestBin(ERPNextTestCase):
+
+
+ def test_concurrent_inserts(self):
+ """ Ensure no duplicates are possible in case of concurrent inserts"""
+ item_code = "_TestConcurrentBin"
+ make_item(item_code)
+ warehouse = "_Test Warehouse - _TC"
+
+ bin1 = frappe.get_doc(doctype="Bin", item_code=item_code, warehouse=warehouse)
+ bin1.insert()
+
+ bin2 = frappe.get_doc(doctype="Bin", item_code=item_code, warehouse=warehouse)
+ with self.assertRaises(frappe.UniqueValidationError):
+ bin2.insert()
+
+ # util method should handle it
+ bin = _create_bin(item_code, warehouse)
+ self.assertEqual(bin.item_code, item_code)
+
+ frappe.db.rollback()
+
+ def test_index_exists(self):
+ indexes = frappe.db.sql("show index from tabBin where Non_unique = 0", as_dict=1)
+ if not any(index.get("Key_name") == "unique_item_warehouse" for index in indexes):
+ self.fail(f"Expected unique index on item-warehouse")
diff --git a/erpnext/stock/doctype/delivery_note/delivery_note.py b/erpnext/stock/doctype/delivery_note/delivery_note.py
index 70d48a42d72..d1e22440b96 100644
--- a/erpnext/stock/doctype/delivery_note/delivery_note.py
+++ b/erpnext/stock/doctype/delivery_note/delivery_note.py
@@ -14,6 +14,7 @@ from erpnext.controllers.accounts_controller import get_taxes_and_charges
from erpnext.controllers.selling_controller import SellingController
from erpnext.stock.doctype.batch.batch import set_batch_nos
from erpnext.stock.doctype.serial_no.serial_no import get_delivery_note_serial_no
+from erpnext.stock.utils import calculate_mapped_packed_items_return
form_grid_templates = {
"items": "templates/form_grid/item_grid.html"
@@ -128,8 +129,12 @@ class DeliveryNote(SellingController):
self.validate_uom_is_integer("uom", "qty")
self.validate_with_previous_doc()
- from erpnext.stock.doctype.packed_item.packed_item import make_packing_list
- make_packing_list(self)
+ # Keeps mapped packed_items in case product bundle is updated.
+ if self.is_return and self.return_against:
+ calculate_mapped_packed_items_return(self)
+ else:
+ from erpnext.stock.doctype.packed_item.packed_item import make_packing_list
+ make_packing_list(self)
if self._action != 'submit' and not self.is_return:
set_batch_nos(self, 'warehouse', throw=True)
diff --git a/erpnext/stock/doctype/delivery_note/test_delivery_note.py b/erpnext/stock/doctype/delivery_note/test_delivery_note.py
index 4f89a19f3c7..bd18e788ba6 100644
--- a/erpnext/stock/doctype/delivery_note/test_delivery_note.py
+++ b/erpnext/stock/doctype/delivery_note/test_delivery_note.py
@@ -386,8 +386,7 @@ class TestDeliveryNote(ERPNextTestCase):
self.assertEqual(actual_qty, 25)
# return bundled item
- dn1 = create_delivery_note(item_code='_Test Product Bundle Item', is_return=1,
- return_against=dn.name, qty=-2, rate=500, company=company, warehouse="Stores - TCP1", expense_account="Cost of Goods Sold - TCP1", cost_center="Main - TCP1")
+ dn1 = create_return_delivery_note(source_name=dn.name, rate=500, qty=-2)
# qty after return
actual_qty = get_qty_after_transaction(warehouse="Stores - TCP1")
@@ -823,6 +822,15 @@ class TestDeliveryNote(ERPNextTestCase):
automatically_fetch_payment_terms(enable=0)
+def create_return_delivery_note(**args):
+ args = frappe._dict(args)
+ from erpnext.controllers.sales_and_purchase_return import make_return_doc
+ doc = make_return_doc("Delivery Note", args.source_name, None)
+ doc.items[0].rate = args.rate
+ doc.items[0].qty = args.qty
+ doc.submit()
+ return doc
+
def create_delivery_note(**args):
dn = frappe.new_doc("Delivery Note")
args = frappe._dict(args)
diff --git a/erpnext/stock/doctype/item/item.js b/erpnext/stock/doctype/item/item.js
index e346ea87214..da371d968c9 100644
--- a/erpnext/stock/doctype/item/item.js
+++ b/erpnext/stock/doctype/item/item.js
@@ -380,8 +380,7 @@ $.extend(erpnext.item, {
// Show Stock Levels only if is_stock_item
if (frm.doc.is_stock_item) {
frappe.require('assets/js/item-dashboard.min.js', function() {
- frm.dashboard.parent.find('.stock-levels').remove();
- const section = frm.dashboard.add_section('', __("Stock Levels"), 'stock-levels');
+ const section = frm.dashboard.add_section('', __("Stock Levels"));
erpnext.item.item_dashboard = new erpnext.stock.ItemDashboard({
parent: section,
item_code: frm.doc.name,
diff --git a/erpnext/stock/doctype/item/item.py b/erpnext/stock/doctype/item/item.py
index 9f3d9569f9f..557e07bf0a2 100644
--- a/erpnext/stock/doctype/item/item.py
+++ b/erpnext/stock/doctype/item/item.py
@@ -219,18 +219,20 @@ class Item(Document):
self.item_code))
def add_default_uom_in_conversion_factor_table(self):
- uom_conv_list = [d.uom for d in self.get("uoms")]
- if self.stock_uom not in uom_conv_list:
- ch = self.append('uoms', {})
- ch.uom = self.stock_uom
- ch.conversion_factor = 1
+ if not self.is_new() and self.has_value_changed("stock_uom"):
+ self.uoms = []
+ frappe.msgprint(
+ _("Successfully changed Stock UOM, please redefine conversion factors for new UOM."),
+ alert=True,
+ )
- to_remove = []
- for d in self.get("uoms"):
- if d.conversion_factor == 1 and d.uom != self.stock_uom:
- to_remove.append(d)
+ uoms_list = [d.uom for d in self.get("uoms")]
- [self.remove(d) for d in to_remove]
+ if self.stock_uom not in uoms_list:
+ self.append("uoms", {
+ "uom": self.stock_uom,
+ "conversion_factor": 1
+ })
def update_website_item(self):
"""Update Website Item if change in Item impacts it."""
@@ -347,14 +349,6 @@ class Item(Document):
frappe.throw(_("Barcode {0} is not a valid {1} code").format(
item_barcode.barcode, item_barcode.barcode_type), InvalidBarcode)
- if item_barcode.barcode != item_barcode.name:
- # if barcode is getting updated , the row name has to reset.
- # Delete previous old row doc and re-enter row as if new to reset name in db.
- item_barcode.set("__islocal", True)
- item_barcode_entry_name = item_barcode.name
- item_barcode.name = None
- frappe.delete_doc("Item Barcode", item_barcode_entry_name)
-
def validate_warehouse_for_reorder(self):
'''Validate Reorder level table for duplicate and conditional mandatory'''
warehouse = []
diff --git a/erpnext/stock/doctype/item/test_item.py b/erpnext/stock/doctype/item/test_item.py
index e191f0a3293..150cead465b 100644
--- a/erpnext/stock/doctype/item/test_item.py
+++ b/erpnext/stock/doctype/item/test_item.py
@@ -573,6 +573,16 @@ class TestItem(ERPNextTestCase):
except frappe.ValidationError as e:
self.fail(f"UoM change not allowed even though no SLE / BIN with positive qty exists: {e}")
+ def test_erasure_of_old_conversions(self):
+ item = create_item("_item change uom")
+ item.stock_uom = "Gram"
+ item.append("uoms", frappe._dict(uom="Box", conversion_factor=2))
+ item.save()
+ item.reload()
+ item.stock_uom = "Nos"
+ item.save()
+ self.assertEqual(len(item.uoms), 1)
+
def test_validate_stock_item(self):
self.assertRaises(frappe.ValidationError, validate_is_stock_item, "_Test Non Stock Item")
diff --git a/erpnext/stock/doctype/packed_item/packed_item.json b/erpnext/stock/doctype/packed_item/packed_item.json
index 830d5469bf0..d2d47897658 100644
--- a/erpnext/stock/doctype/packed_item/packed_item.json
+++ b/erpnext/stock/doctype/packed_item/packed_item.json
@@ -218,8 +218,6 @@
"label": "Conversion Factor"
},
{
- "fetch_from": "item_code.valuation_rate",
- "fetch_if_empty": 1,
"fieldname": "rate",
"fieldtype": "Currency",
"in_list_view": 1,
@@ -232,7 +230,7 @@
"index_web_pages_for_search": 1,
"istable": 1,
"links": [],
- "modified": "2021-09-01 15:10:29.646399",
+ "modified": "2022-01-28 16:03:30.780111",
"modified_by": "Administrator",
"module": "Stock",
"name": "Packed Item",
@@ -240,5 +238,6 @@
"permissions": [],
"sort_field": "modified",
"sort_order": "DESC",
+ "states": [],
"track_changes": 1
}
\ No newline at end of file
diff --git a/erpnext/stock/doctype/packed_item/packed_item.py b/erpnext/stock/doctype/packed_item/packed_item.py
index e4091c40dc4..07c2f1f0dd3 100644
--- a/erpnext/stock/doctype/packed_item/packed_item.py
+++ b/erpnext/stock/doctype/packed_item/packed_item.py
@@ -8,187 +8,253 @@ import json
import frappe
from frappe.model.document import Document
-from frappe.utils import cstr, flt
+from frappe.utils import flt
-from erpnext.stock.get_item_details import get_item_details
+from erpnext.stock.get_item_details import get_item_details, get_price_list_rate
class PackedItem(Document):
pass
-def get_product_bundle_items(item_code):
- return frappe.db.sql("""select t1.item_code, t1.qty, t1.uom, t1.description
- from `tabProduct Bundle Item` t1, `tabProduct Bundle` t2
- where t2.new_item_code=%s and t1.parent = t2.name order by t1.idx""", item_code, as_dict=1)
-
-def get_packing_item_details(item, company):
- return frappe.db.sql("""
- select i.item_name, i.is_stock_item, i.description, i.stock_uom, id.default_warehouse
- from `tabItem` i LEFT JOIN `tabItem Default` id ON id.parent=i.name and id.company=%s
- where i.name = %s""",
- (company, item), as_dict = 1)[0]
-
-def get_bin_qty(item, warehouse):
- det = frappe.db.sql("""select actual_qty, projected_qty from `tabBin`
- where item_code = %s and warehouse = %s""", (item, warehouse), as_dict = 1)
- return det and det[0] or frappe._dict()
-
-def update_packing_list_item(doc, packing_item_code, qty, main_item_row, description):
- if doc.amended_from:
- old_packed_items_map = get_old_packed_item_details(doc.packed_items)
- else:
- old_packed_items_map = False
- item = get_packing_item_details(packing_item_code, doc.company)
-
- # check if exists
- exists = 0
- for d in doc.get("packed_items"):
- if d.parent_item == main_item_row.item_code and d.item_code == packing_item_code:
- if d.parent_detail_docname != main_item_row.name:
- d.parent_detail_docname = main_item_row.name
-
- pi, exists = d, 1
- break
-
- if not exists:
- pi = doc.append('packed_items', {})
-
- pi.parent_item = main_item_row.item_code
- pi.item_code = packing_item_code
- pi.item_name = item.item_name
- pi.parent_detail_docname = main_item_row.name
- pi.uom = item.stock_uom
- pi.qty = flt(qty)
- pi.conversion_factor = main_item_row.conversion_factor
- if description and not pi.description:
- pi.description = description
- if not pi.warehouse and not doc.amended_from:
- pi.warehouse = (main_item_row.warehouse if ((doc.get('is_pos') or item.is_stock_item \
- or not item.default_warehouse) and main_item_row.warehouse) else item.default_warehouse)
- if not pi.batch_no and not doc.amended_from:
- pi.batch_no = cstr(main_item_row.get("batch_no"))
- if not pi.target_warehouse:
- pi.target_warehouse = main_item_row.get("target_warehouse")
- bin = get_bin_qty(packing_item_code, pi.warehouse)
- pi.actual_qty = flt(bin.get("actual_qty"))
- pi.projected_qty = flt(bin.get("projected_qty"))
- if old_packed_items_map and old_packed_items_map.get((packing_item_code, main_item_row.item_code)):
- pi.batch_no = old_packed_items_map.get((packing_item_code, main_item_row.item_code))[0].batch_no
- pi.serial_no = old_packed_items_map.get((packing_item_code, main_item_row.item_code))[0].serial_no
- pi.warehouse = old_packed_items_map.get((packing_item_code, main_item_row.item_code))[0].warehouse
def make_packing_list(doc):
- """make packing list for Product Bundle item"""
- if doc.get("_action") and doc._action == "update_after_submit": return
-
- parent_items = []
- for d in doc.get("items"):
- if frappe.db.get_value("Product Bundle", {"new_item_code": d.item_code}):
- for i in get_product_bundle_items(d.item_code):
- update_packing_list_item(doc, i.item_code, flt(i.qty)*flt(d.stock_qty), d, i.description)
-
- if [d.item_code, d.name] not in parent_items:
- parent_items.append([d.item_code, d.name])
-
- cleanup_packing_list(doc, parent_items)
-
- if frappe.db.get_single_value("Selling Settings", "editable_bundle_item_rates"):
- update_product_bundle_price(doc, parent_items)
-
-def cleanup_packing_list(doc, parent_items):
- """Remove all those child items which are no longer present in main item table"""
- delete_list = []
- for d in doc.get("packed_items"):
- if [d.parent_item, d.parent_detail_docname] not in parent_items:
- # mark for deletion from doclist
- delete_list.append(d)
-
- if not delete_list:
- return doc
-
- packed_items = doc.get("packed_items")
- doc.set("packed_items", [])
-
- for d in packed_items:
- if d not in delete_list:
- 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."""
-
- if not doc.get('items'):
+ "Make/Update packing list for Product Bundle Item."
+ if doc.get("_action") and doc._action == "update_after_submit":
return
- parent_items_index = 0
- bundle_price = 0
+ parent_items_price, reset = {}, False
+ set_price_from_children = frappe.db.get_single_value("Selling Settings", "editable_bundle_item_rates")
- for bundle_item in doc.get("packed_items"):
- if parent_items[parent_items_index][0] == bundle_item.parent_item:
- bundle_item_rate = bundle_item.rate if bundle_item.rate else 0
- bundle_price += bundle_item.qty * bundle_item_rate
- else:
- update_parent_item_price(doc, parent_items[parent_items_index][0], bundle_price)
+ stale_packed_items_table = get_indexed_packed_items_table(doc)
- bundle_item_rate = bundle_item.rate if bundle_item.rate else 0
- bundle_price = bundle_item.qty * bundle_item_rate
- parent_items_index += 1
+ reset = reset_packing_list(doc)
- # for the last product bundle
- if doc.get("packed_items"):
- update_parent_item_price(doc, parent_items[parent_items_index][0], bundle_price)
+ for item_row in doc.get("items"):
+ if frappe.db.exists("Product Bundle", {"new_item_code": item_row.item_code}):
+ for bundle_item in get_product_bundle_items(item_row.item_code):
+ pi_row = add_packed_item_row(
+ doc=doc, packing_item=bundle_item,
+ main_item_row=item_row, packed_items_table=stale_packed_items_table,
+ reset=reset
+ )
+ item_data = get_packed_item_details(bundle_item.item_code, doc.company)
+ update_packed_item_basic_data(item_row, pi_row, bundle_item, item_data)
+ update_packed_item_stock_data(item_row, pi_row, bundle_item, item_data, doc)
+ update_packed_item_price_data(pi_row, item_data, doc)
+ update_packed_item_from_cancelled_doc(item_row, bundle_item, pi_row, doc)
-def update_parent_item_price(doc, parent_item_code, bundle_price):
- parent_item_doc = doc.get('items', {'item_code': parent_item_code})[0]
+ if set_price_from_children: # create/update bundle item wise price dict
+ update_product_bundle_rate(parent_items_price, pi_row)
- current_parent_item_price = parent_item_doc.amount
- if current_parent_item_price != bundle_price:
- parent_item_doc.amount = bundle_price
- update_parent_item_rate(parent_item_doc, bundle_price)
+ if parent_items_price:
+ set_product_bundle_rate_amount(doc, parent_items_price) # set price in bundle item
-def update_parent_item_rate(parent_item_doc, bundle_price):
- parent_item_doc.rate = bundle_price/parent_item_doc.qty
+def get_indexed_packed_items_table(doc):
+ """
+ Create dict from stale packed items table like:
+ {(Parent Item 1, Bundle Item 1, ae4b5678): {...}, (key): {value}}
-@frappe.whitelist()
-def get_items_from_product_bundle(args):
- args = json.loads(args)
- items = []
- bundled_items = get_product_bundle_items(args["item_code"])
- for item in bundled_items:
- args.update({
- "item_code": item.item_code,
- "qty": flt(args["quantity"]) * flt(item.qty)
- })
- items.append(get_item_details(args))
+ Use: to quickly retrieve/check if row existed in table instead of looping n times
+ """
+ indexed_table = {}
+ for packed_item in doc.get("packed_items"):
+ key = (packed_item.parent_item, packed_item.item_code, packed_item.parent_detail_docname)
+ indexed_table[key] = packed_item
- return items
+ return indexed_table
+
+def reset_packing_list(doc):
+ "Conditionally reset the table and return if it was reset or not."
+ reset_table = False
+ doc_before_save = doc.get_doc_before_save()
+
+ if doc_before_save:
+ # reset table if:
+ # 1. items were deleted
+ # 2. if bundle item replaced by another item (same no. of items but different items)
+ # we maintain list to track recurring item rows as well
+ items_before_save = [item.item_code for item in doc_before_save.get("items")]
+ items_after_save = [item.item_code for item in doc.get("items")]
+ reset_table = items_before_save != items_after_save
+ else:
+ # reset: if via Update Items OR
+ # if new mapped doc with packed items set (SO -> DN)
+ # (cannot determine action)
+ reset_table = True
+
+ if reset_table:
+ doc.set("packed_items", [])
+ return reset_table
+
+def get_product_bundle_items(item_code):
+ product_bundle = frappe.qb.DocType("Product Bundle")
+ product_bundle_item = frappe.qb.DocType("Product Bundle Item")
+
+ query = (
+ frappe.qb.from_(product_bundle_item)
+ .join(product_bundle).on(product_bundle_item.parent == product_bundle.name)
+ .select(
+ product_bundle_item.item_code,
+ product_bundle_item.qty,
+ product_bundle_item.uom,
+ product_bundle_item.description
+ ).where(
+ product_bundle.new_item_code == item_code
+ ).orderby(
+ product_bundle_item.idx
+ )
+ )
+ return query.run(as_dict=True)
+
+def add_packed_item_row(doc, packing_item, main_item_row, packed_items_table, reset):
+ """Add and return packed item row.
+ doc: Transaction document
+ packing_item (dict): Packed Item details
+ main_item_row (dict): Items table row corresponding to packed item
+ packed_items_table (dict): Packed Items table before save (indexed)
+ reset (bool): State if table is reset or preserved as is
+ """
+ exists, pi_row = False, {}
+
+ # check if row already exists in packed items table
+ key = (main_item_row.item_code, packing_item.item_code, main_item_row.name)
+ if packed_items_table.get(key):
+ pi_row, exists = packed_items_table.get(key), True
+
+ if not exists:
+ pi_row = doc.append('packed_items', {})
+ elif reset: # add row if row exists but table is reset
+ pi_row.idx, pi_row.name = None, None
+ pi_row = doc.append('packed_items', pi_row)
+
+ return pi_row
+
+def get_packed_item_details(item_code, company):
+ item = frappe.qb.DocType("Item")
+ item_default = frappe.qb.DocType("Item Default")
+ query = (
+ frappe.qb.from_(item)
+ .left_join(item_default)
+ .on(
+ (item_default.parent == item.name)
+ & (item_default.company == company)
+ ).select(
+ item.item_name, item.is_stock_item,
+ item.description, item.stock_uom,
+ item.valuation_rate,
+ item_default.default_warehouse
+ ).where(
+ item.name == item_code
+ )
+ )
+ return query.run(as_dict=True)[0]
+
+def update_packed_item_basic_data(main_item_row, pi_row, packing_item, item_data):
+ pi_row.parent_item = main_item_row.item_code
+ pi_row.parent_detail_docname = main_item_row.name
+ pi_row.item_code = packing_item.item_code
+ pi_row.item_name = item_data.item_name
+ pi_row.uom = item_data.stock_uom
+ pi_row.qty = flt(packing_item.qty) * flt(main_item_row.stock_qty)
+ pi_row.conversion_factor = main_item_row.conversion_factor
+
+ if not pi_row.description:
+ pi_row.description = packing_item.get("description")
+
+def update_packed_item_stock_data(main_item_row, pi_row, packing_item, item_data, doc):
+ # TODO batch_no, actual_batch_qty, incoming_rate
+ if not pi_row.warehouse and not doc.amended_from:
+ fetch_warehouse = (doc.get('is_pos') or item_data.is_stock_item or not item_data.default_warehouse)
+ pi_row.warehouse = (main_item_row.warehouse if (fetch_warehouse and main_item_row.warehouse)
+ else item_data.default_warehouse)
+
+ if not pi_row.target_warehouse:
+ pi_row.target_warehouse = main_item_row.get("target_warehouse")
+
+ bin = get_packed_item_bin_qty(packing_item.item_code, pi_row.warehouse)
+ pi_row.actual_qty = flt(bin.get("actual_qty"))
+ pi_row.projected_qty = flt(bin.get("projected_qty"))
+
+def update_packed_item_price_data(pi_row, item_data, doc):
+ "Set price as per price list or from the Item master."
+ if pi_row.rate:
+ return
+
+ item_doc = frappe.get_cached_doc("Item", pi_row.item_code)
+ row_data = pi_row.as_dict().copy()
+ row_data.update({
+ "company": doc.get("company"),
+ "price_list": doc.get("selling_price_list"),
+ "currency": doc.get("currency")
+ })
+ rate = get_price_list_rate(row_data, item_doc).get("price_list_rate")
+
+ pi_row.rate = rate or item_data.get("valuation_rate") or 0.0
+
+def update_packed_item_from_cancelled_doc(main_item_row, packing_item, pi_row, doc):
+ "Update packed item row details from cancelled doc into amended doc."
+ prev_doc_packed_items_map = None
+ if doc.amended_from:
+ prev_doc_packed_items_map = get_cancelled_doc_packed_item_details(doc.packed_items)
+
+ if prev_doc_packed_items_map and prev_doc_packed_items_map.get((packing_item.item_code, main_item_row.item_code)):
+ prev_doc_row = prev_doc_packed_items_map.get((packing_item.item_code, main_item_row.item_code))
+ pi_row.batch_no = prev_doc_row[0].batch_no
+ pi_row.serial_no = prev_doc_row[0].serial_no
+ pi_row.warehouse = prev_doc_row[0].warehouse
+
+def get_packed_item_bin_qty(item, warehouse):
+ bin_data = frappe.db.get_values(
+ "Bin",
+ fieldname=["actual_qty", "projected_qty"],
+ filters={"item_code": item, "warehouse": warehouse},
+ as_dict=True
+ )
+
+ return bin_data[0] if bin_data else {}
+
+def get_cancelled_doc_packed_item_details(old_packed_items):
+ prev_doc_packed_items_map = {}
+ for items in old_packed_items:
+ prev_doc_packed_items_map.setdefault((items.item_code ,items.parent_item), []).append(items.as_dict())
+ return prev_doc_packed_items_map
+
+def update_product_bundle_rate(parent_items_price, pi_row):
+ """
+ Update the price dict of Product Bundles based on the rates of the Items in the bundle.
+
+ Stucture:
+ {(Bundle Item 1, ae56fgji): 150.0, (Bundle Item 2, bc78fkjo): 200.0}
+ """
+ key = (pi_row.parent_item, pi_row.parent_detail_docname)
+ rate = parent_items_price.get(key)
+ if not rate:
+ parent_items_price[key] = 0.0
+
+ parent_items_price[key] += flt(pi_row.rate)
+
+def set_product_bundle_rate_amount(doc, parent_items_price):
+ "Set cumulative rate and amount in bundle item."
+ for item in doc.get("items"):
+ bundle_rate = parent_items_price.get((item.item_code, item.name))
+ if bundle_rate and bundle_rate != item.rate:
+ item.rate = bundle_rate
+ item.amount = flt(bundle_rate * item.qty)
def on_doctype_update():
frappe.db.add_index("Packed Item", ["item_code", "warehouse"])
-def get_old_packed_item_details(old_packed_items):
- old_packed_items_map = {}
- for items in old_packed_items:
- old_packed_items_map.setdefault((items.item_code ,items.parent_item), []).append(items.as_dict())
- return old_packed_items_map
+
+@frappe.whitelist()
+def get_items_from_product_bundle(row):
+ row, items = json.loads(row), []
+
+ bundled_items = get_product_bundle_items(row["item_code"])
+ for item in bundled_items:
+ row.update({
+ "item_code": item.item_code,
+ "qty": flt(row["quantity"]) * flt(item.qty)
+ })
+ items.append(get_item_details(row))
+
+ return items
diff --git a/erpnext/stock/doctype/packed_item/test_packed_item.py b/erpnext/stock/doctype/packed_item/test_packed_item.py
new file mode 100644
index 00000000000..5cbaa1ea669
--- /dev/null
+++ b/erpnext/stock/doctype/packed_item/test_packed_item.py
@@ -0,0 +1,127 @@
+# Copyright (c) 2022, Frappe Technologies Pvt. Ltd. and Contributors
+# License: GNU General Public License v3. See license.txt
+
+from erpnext.selling.doctype.product_bundle.test_product_bundle import make_product_bundle
+from erpnext.selling.doctype.sales_order.sales_order import make_delivery_note
+from erpnext.selling.doctype.sales_order.test_sales_order import make_sales_order
+from erpnext.stock.doctype.item.test_item import make_item
+from erpnext.tests.utils import ERPNextTestCase, change_settings
+
+
+class TestPackedItem(ERPNextTestCase):
+ "Test impact on Packed Items table in various scenarios."
+ @classmethod
+ def setUpClass(cls) -> None:
+ make_item("_Test Product Bundle X", {"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 Normal Stock Item", {"is_stock_item": 1})
+
+ make_product_bundle(
+ "_Test Product Bundle X",
+ ["_Test Bundle Item 1", "_Test Bundle Item 2"],
+ qty=2
+ )
+
+ def test_adding_bundle_item(self):
+ "Test impact on packed items if bundle item row is added."
+ so = make_sales_order(item_code = "_Test Product Bundle X", qty=1,
+ do_not_submit=True)
+
+ self.assertEqual(so.items[0].qty, 1)
+ self.assertEqual(len(so.packed_items), 2)
+ self.assertEqual(so.packed_items[0].item_code, "_Test Bundle Item 1")
+ self.assertEqual(so.packed_items[0].qty, 2)
+
+ def test_updating_bundle_item(self):
+ "Test impact on packed items if bundle item row is updated."
+ so = make_sales_order(item_code = "_Test Product Bundle X", qty=1,
+ do_not_submit=True)
+
+ so.items[0].qty = 2 # change qty
+ so.save()
+
+ self.assertEqual(so.packed_items[0].qty, 4)
+ self.assertEqual(so.packed_items[1].qty, 4)
+
+ # change item code to non bundle item
+ so.items[0].item_code = "_Test Normal Stock Item"
+ so.save()
+
+ self.assertEqual(len(so.packed_items), 0)
+
+ def test_recurring_bundle_item(self):
+ "Test impact on packed items if same bundle item is added and removed."
+ so_items = []
+ for qty in [2, 4, 6, 8]:
+ so_items.append({
+ "item_code": "_Test Product Bundle X",
+ "qty": qty,
+ "rate": 400,
+ "warehouse": "_Test Warehouse - _TC"
+ })
+
+ # create SO with recurring bundle item
+ so = make_sales_order(item_list=so_items, do_not_submit=True)
+
+ # check alternate rows for qty
+ self.assertEqual(len(so.packed_items), 8)
+ self.assertEqual(so.packed_items[1].item_code, "_Test Bundle Item 2")
+ self.assertEqual(so.packed_items[1].qty, 4)
+ self.assertEqual(so.packed_items[3].qty, 8)
+ self.assertEqual(so.packed_items[5].qty, 12)
+ self.assertEqual(so.packed_items[7].qty, 16)
+
+ # delete intermediate row (2nd)
+ del so.items[1]
+ so.save()
+
+ # check alternate rows for qty
+ self.assertEqual(len(so.packed_items), 6)
+ self.assertEqual(so.packed_items[1].qty, 4)
+ self.assertEqual(so.packed_items[3].qty, 12)
+ self.assertEqual(so.packed_items[5].qty, 16)
+
+ # delete last row
+ del so.items[2]
+ so.save()
+
+ # check alternate rows for qty
+ self.assertEqual(len(so.packed_items), 4)
+ self.assertEqual(so.packed_items[1].qty, 4)
+ self.assertEqual(so.packed_items[3].qty, 12)
+
+ @change_settings("Selling Settings", {"editable_bundle_item_rates": 1})
+ def test_bundle_item_cumulative_price(self):
+ "Test if Bundle Item rate is cumulative from packed items."
+ so = make_sales_order(item_code = "_Test Product Bundle X", qty=2,
+ do_not_submit=True)
+
+ so.packed_items[0].rate = 150
+ so.packed_items[1].rate = 200
+ so.save()
+
+ self.assertEqual(so.items[0].rate, 350)
+ self.assertEqual(so.items[0].amount, 700)
+
+ def test_newly_mapped_doc_packed_items(self):
+ "Test impact on packed items in newly mapped DN from SO."
+ so_items = []
+ for qty in [2, 4]:
+ so_items.append({
+ "item_code": "_Test Product Bundle X",
+ "qty": qty,
+ "rate": 400,
+ "warehouse": "_Test Warehouse - _TC"
+ })
+
+ # create SO with recurring bundle item
+ so = make_sales_order(item_list=so_items)
+
+ dn = make_delivery_note(so.name)
+ dn.items[1].qty = 3 # change second row qty for inserting doc
+ dn.save()
+
+ self.assertEqual(len(dn.packed_items), 4)
+ self.assertEqual(dn.packed_items[2].qty, 6)
+ self.assertEqual(dn.packed_items[3].qty, 6)
\ No newline at end of file
diff --git a/erpnext/stock/doctype/purchase_receipt/purchase_receipt.json b/erpnext/stock/doctype/purchase_receipt/purchase_receipt.json
index 112ddedac29..b54a90eed35 100755
--- a/erpnext/stock/doctype/purchase_receipt/purchase_receipt.json
+++ b/erpnext/stock/doctype/purchase_receipt/purchase_receipt.json
@@ -106,6 +106,8 @@
"terms",
"bill_no",
"bill_date",
+ "accounting_details_section",
+ "provisional_expense_account",
"more_info",
"project",
"status",
@@ -1144,16 +1146,30 @@
"label": "Represents Company",
"options": "Company",
"read_only": 1
+ },
+ {
+ "collapsible": 1,
+ "fieldname": "accounting_details_section",
+ "fieldtype": "Section Break",
+ "label": "Accounting Details"
+ },
+ {
+ "fieldname": "provisional_expense_account",
+ "fieldtype": "Link",
+ "hidden": 1,
+ "label": "Provisional Expense Account",
+ "options": "Account"
}
],
"icon": "fa fa-truck",
"idx": 261,
"is_submittable": 1,
"links": [],
- "modified": "2021-09-28 13:11:10.181328",
+ "modified": "2022-02-01 11:40:52.690984",
"modified_by": "Administrator",
"module": "Stock",
"name": "Purchase Receipt",
+ "naming_rule": "By \"Naming Series\" field",
"owner": "Administrator",
"permissions": [
{
@@ -1214,6 +1230,7 @@
"show_name_in_global_search": 1,
"sort_field": "modified",
"sort_order": "DESC",
+ "states": [],
"timeline_field": "supplier",
"title_field": "title",
"track_changes": 1
diff --git a/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py b/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py
index e7e9e9c1d7f..47c81280f6b 100644
--- a/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py
+++ b/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py
@@ -9,6 +9,7 @@ from frappe.model.mapper import get_mapped_doc
from frappe.utils import cint, flt, getdate, nowdate
from six import iteritems
+import erpnext
from erpnext.accounts.utils import get_account_currency
from erpnext.assets.doctype.asset.asset import get_asset_account, is_cwip_accounting_enabled
from erpnext.assets.doctype.asset_category.asset_category import get_asset_category_account
@@ -113,6 +114,7 @@ class PurchaseReceipt(BuyingController):
self.validate_uom_is_integer("uom", ["qty", "received_qty"])
self.validate_uom_is_integer("stock_uom", "stock_qty")
self.validate_cwip_accounts()
+ self.validate_provisional_expense_account()
self.check_on_hold_or_closed_status()
@@ -134,6 +136,15 @@ class PurchaseReceipt(BuyingController):
company = self.company)
break
+ def validate_provisional_expense_account(self):
+ provisional_accounting_for_non_stock_items = \
+ cint(frappe.db.get_value('Company', self.company, 'enable_provisional_accounting_for_non_stock_items'))
+
+ if provisional_accounting_for_non_stock_items:
+ default_provisional_account = self.get_company_default("default_provisional_account")
+ if not self.provisional_expense_account:
+ self.provisional_expense_account = default_provisional_account
+
def validate_with_previous_doc(self):
super(PurchaseReceipt, self).validate_with_previous_doc({
"Purchase Order": {
@@ -255,13 +266,15 @@ class PurchaseReceipt(BuyingController):
return process_gl_map(gl_entries)
def make_item_gl_entries(self, gl_entries, warehouse_account=None):
- stock_rbnb = self.get_company_default("stock_received_but_not_billed")
- landed_cost_entries = get_item_account_wise_additional_cost(self.name)
- expenses_included_in_valuation = self.get_company_default("expenses_included_in_valuation")
- auto_accounting_for_non_stock_items = cint(frappe.db.get_value('Company', self.company, 'enable_perpetual_inventory_for_non_stock_items'))
+ if erpnext.is_perpetual_inventory_enabled(self.company):
+ stock_rbnb = self.get_company_default("stock_received_but_not_billed")
+ landed_cost_entries = get_item_account_wise_additional_cost(self.name)
+ expenses_included_in_valuation = self.get_company_default("expenses_included_in_valuation")
warehouse_with_no_account = []
stock_items = self.get_stock_items()
+ provisional_accounting_for_non_stock_items = \
+ cint(frappe.db.get_value('Company', self.company, 'enable_provisional_accounting_for_non_stock_items'))
for d in self.get("items"):
if d.item_code in stock_items and flt(d.valuation_rate) and flt(d.qty):
@@ -384,43 +397,58 @@ class PurchaseReceipt(BuyingController):
elif d.warehouse not in warehouse_with_no_account or \
d.rejected_warehouse not in warehouse_with_no_account:
warehouse_with_no_account.append(d.warehouse)
- elif d.item_code not in stock_items and not d.is_fixed_asset and flt(d.qty) and auto_accounting_for_non_stock_items:
- service_received_but_not_billed_account = self.get_company_default("service_received_but_not_billed")
- credit_currency = get_account_currency(service_received_but_not_billed_account)
- debit_currency = get_account_currency(d.expense_account)
- remarks = self.get("remarks") or _("Accounting Entry for Service")
-
- self.add_gl_entry(
- gl_entries=gl_entries,
- account=service_received_but_not_billed_account,
- cost_center=d.cost_center,
- debit=0.0,
- credit=d.amount,
- remarks=remarks,
- against_account=d.expense_account,
- account_currency=credit_currency,
- project=d.project,
- voucher_detail_no=d.name, item=d)
-
- self.add_gl_entry(
- gl_entries=gl_entries,
- account=d.expense_account,
- cost_center=d.cost_center,
- debit=d.amount,
- credit=0.0,
- remarks=remarks,
- against_account=service_received_but_not_billed_account,
- account_currency = debit_currency,
- project=d.project,
- voucher_detail_no=d.name,
- item=d)
+ elif d.item_code not in stock_items and not d.is_fixed_asset and flt(d.qty) and provisional_accounting_for_non_stock_items:
+ self.add_provisional_gl_entry(d, gl_entries, self.posting_date)
if warehouse_with_no_account:
frappe.msgprint(_("No accounting entries for the following warehouses") + ": \n" +
"\n".join(warehouse_with_no_account))
+ def add_provisional_gl_entry(self, item, gl_entries, posting_date, reverse=0):
+ provisional_expense_account = self.get('provisional_expense_account')
+ credit_currency = get_account_currency(provisional_expense_account)
+ debit_currency = get_account_currency(item.expense_account)
+ expense_account = item.expense_account
+ remarks = self.get("remarks") or _("Accounting Entry for Service")
+ multiplication_factor = 1
+
+ if reverse:
+ multiplication_factor = -1
+ expense_account = frappe.db.get_value('Purchase Receipt Item', {'name': item.get('pr_detail')}, ['expense_account'])
+
+ self.add_gl_entry(
+ gl_entries=gl_entries,
+ account=provisional_expense_account,
+ cost_center=item.cost_center,
+ debit=0.0,
+ credit=multiplication_factor * item.amount,
+ remarks=remarks,
+ against_account=expense_account,
+ account_currency=credit_currency,
+ project=item.project,
+ voucher_detail_no=item.name,
+ item=item,
+ posting_date=posting_date)
+
+ self.add_gl_entry(
+ gl_entries=gl_entries,
+ account=expense_account,
+ cost_center=item.cost_center,
+ debit=multiplication_factor * item.amount,
+ credit=0.0,
+ remarks=remarks,
+ against_account=provisional_expense_account,
+ account_currency = debit_currency,
+ project=item.project,
+ voucher_detail_no=item.name,
+ item=item,
+ posting_date=posting_date)
+
def make_tax_gl_entries(self, gl_entries):
- expenses_included_in_valuation = self.get_company_default("expenses_included_in_valuation")
+
+ if erpnext.is_perpetual_inventory_enabled(self.company):
+ expenses_included_in_valuation = self.get_company_default("expenses_included_in_valuation")
+
negative_expense_to_be_booked = sum([flt(d.item_tax_amount) for d in self.get('items')])
# Cost center-wise amount breakup for other charges included for valuation
valuation_tax = {}
@@ -477,7 +505,8 @@ class PurchaseReceipt(BuyingController):
def add_gl_entry(self, gl_entries, account, cost_center, debit, credit, remarks, against_account,
debit_in_account_currency=None, credit_in_account_currency=None, account_currency=None,
- project=None, voucher_detail_no=None, item=None):
+ project=None, voucher_detail_no=None, item=None, posting_date=None):
+
gl_entry = {
"account": account,
"cost_center": cost_center,
@@ -496,6 +525,9 @@ class PurchaseReceipt(BuyingController):
if credit_in_account_currency:
gl_entry.update({"credit_in_account_currency": credit_in_account_currency})
+ if posting_date:
+ gl_entry.update({"posting_date": posting_date})
+
gl_entries.append(self.get_gl_dict(gl_entry, item=item))
def get_asset_gl_entry(self, gl_entries):
@@ -524,6 +556,7 @@ class PurchaseReceipt(BuyingController):
# debit cwip account
debit_in_account_currency = (base_asset_amount
if cwip_account_currency == self.company_currency else asset_amount)
+
self.add_gl_entry(
gl_entries=gl_entries,
account=cwip_account,
@@ -539,6 +572,7 @@ class PurchaseReceipt(BuyingController):
# credit arbnb account
credit_in_account_currency = (base_asset_amount
if asset_rbnb_currency == self.company_currency else asset_amount)
+
self.add_gl_entry(
gl_entries=gl_entries,
account=arbnb_account,
diff --git a/erpnext/stock/doctype/purchase_receipt/test_purchase_receipt.py b/erpnext/stock/doctype/purchase_receipt/test_purchase_receipt.py
index 6774dafb68c..556535317d4 100644
--- a/erpnext/stock/doctype/purchase_receipt/test_purchase_receipt.py
+++ b/erpnext/stock/doctype/purchase_receipt/test_purchase_receipt.py
@@ -1331,58 +1331,6 @@ class TestPurchaseReceipt(ERPNextTestCase):
self.assertEqual(pr.status, "To Bill")
self.assertAlmostEqual(pr.per_billed, 50.0, places=2)
- def test_service_item_purchase_with_perpetual_inventory(self):
- 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
- )
- srbnb_account = 'Stock Received But Not Billed - TCP1'
- frappe.db.set_value(
- 'Company', company,
- 'service_received_but_not_billed', srbnb_account
- )
-
- pr = make_purchase_receipt(
- company=company, item=service_item,
- warehouse='Finished Goods - TCP1', do_not_save=1
- )
- item_row_with_diff_rate = frappe.copy_doc(pr.items[0])
- item_row_with_diff_rate.rate = 100
- pr.append('items', item_row_with_diff_rate)
-
- pr.save()
- pr.submit()
-
- item_one_gl_entry = frappe.db.get_all("GL Entry", {
- 'voucher_type': pr.doctype,
- 'voucher_no': pr.name,
- 'account': srbnb_account,
- 'voucher_detail_no': pr.items[0].name
- }, pluck="name")
-
- item_two_gl_entry = frappe.db.get_all("GL Entry", {
- 'voucher_type': pr.doctype,
- 'voucher_no': pr.name,
- 'account': srbnb_account,
- 'voucher_detail_no': pr.items[1].name
- }, pluck="name")
-
- # check if the entries are not merged into one
- # seperate entries should be made since voucher_detail_no is different
- 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
- )
-
def test_payment_terms_are_fetched_when_creating_purchase_invoice(self):
from erpnext.accounts.doctype.payment_entry.test_payment_entry import (
create_payment_terms_template,
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 30ea1c3cadc..e5994b2dd48 100644
--- a/erpnext/stock/doctype/purchase_receipt_item/purchase_receipt_item.json
+++ b/erpnext/stock/doctype/purchase_receipt_item/purchase_receipt_item.json
@@ -976,7 +976,7 @@
"idx": 1,
"istable": 1,
"links": [],
- "modified": "2021-11-15 15:46:10.591600",
+ "modified": "2022-02-01 11:32:27.980524",
"modified_by": "Administrator",
"module": "Stock",
"name": "Purchase Receipt Item",
@@ -985,5 +985,6 @@
"permissions": [],
"quick_entry": 1,
"sort_field": "modified",
- "sort_order": "DESC"
+ "sort_order": "DESC",
+ "states": []
}
\ No newline at end of file
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 cd7e63b18b2..0ba97d59a14 100644
--- a/erpnext/stock/doctype/repost_item_valuation/repost_item_valuation.json
+++ b/erpnext/stock/doctype/repost_item_valuation/repost_item_valuation.json
@@ -1,7 +1,7 @@
{
"actions": [],
"autoname": "REPOST-ITEM-VAL-.######",
- "creation": "2020-10-22 22:27:07.742161",
+ "creation": "2022-01-11 15:03:38.273179",
"doctype": "DocType",
"editable_grid": 1,
"engine": "InnoDB",
@@ -129,7 +129,7 @@
"reqd": 1
},
{
- "default": "0",
+ "default": "1",
"fieldname": "allow_negative_stock",
"fieldtype": "Check",
"label": "Allow Negative Stock"
@@ -177,7 +177,7 @@
"index_web_pages_for_search": 1,
"is_submittable": 1,
"links": [],
- "modified": "2021-11-24 02:18:10.524560",
+ "modified": "2022-01-18 10:57:33.450907",
"modified_by": "Administrator",
"module": "Stock",
"name": "Repost Item Valuation",
@@ -227,5 +227,6 @@
}
],
"sort_field": "modified",
- "sort_order": "DESC"
-}
+ "sort_order": "DESC",
+ "states": []
+}
\ 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 5ad8f443203..3b76301b4cc 100644
--- a/erpnext/stock/doctype/repost_item_valuation/repost_item_valuation.py
+++ b/erpnext/stock/doctype/repost_item_valuation/repost_item_valuation.py
@@ -27,8 +27,7 @@ class RepostItemValuation(Document):
self.item_code = None
self.warehouse = None
- self.allow_negative_stock = self.allow_negative_stock or \
- cint(frappe.db.get_single_value("Stock Settings", "allow_negative_stock"))
+ self.allow_negative_stock = 1
def set_company(self):
if self.based_on == "Transaction":
diff --git a/erpnext/stock/doctype/serial_no/serial_no.py b/erpnext/stock/doctype/serial_no/serial_no.py
index 3b325b80295..e300d46db83 100644
--- a/erpnext/stock/doctype/serial_no/serial_no.py
+++ b/erpnext/stock/doctype/serial_no/serial_no.py
@@ -484,6 +484,13 @@ def get_serial_nos(serial_no):
return [s.strip() for s in cstr(serial_no).strip().upper().replace(',', '\n').split('\n')
if s.strip()]
+def clean_serial_no_string(serial_no: str) -> str:
+ if not serial_no:
+ return ""
+
+ serial_no_list = get_serial_nos(serial_no)
+ return "\n".join(serial_no_list)
+
def update_args_for_serial_no(serial_no_doc, serial_no, args, is_new=False):
for field in ["item_code", "work_order", "company", "batch_no", "supplier", "location"]:
if args.get(field):
diff --git a/erpnext/stock/doctype/stock_entry/stock_entry.py b/erpnext/stock/doctype/stock_entry/stock_entry.py
index 7472c95ecf5..a5bf2397411 100644
--- a/erpnext/stock/doctype/stock_entry/stock_entry.py
+++ b/erpnext/stock/doctype/stock_entry/stock_entry.py
@@ -1446,14 +1446,15 @@ class StockEntry(StockController):
qty = req_qty_each * flt(self.fg_completed_qty)
elif backflushed_materials.get(item.item_code):
+ precision = frappe.get_precision("Stock Entry Detail", "qty")
for d in backflushed_materials.get(item.item_code):
- if d.get(item.warehouse):
+ if d.get(item.warehouse) > 0:
if (qty > req_qty):
- qty = (qty/trans_qty) * flt(self.fg_completed_qty)
+ qty = ((flt(qty, precision) - flt(d.get(item.warehouse), precision))
+ / (flt(trans_qty, precision) - flt(produced_qty, precision))
+ ) * flt(self.fg_completed_qty)
- if consumed_qty and frappe.db.get_single_value("Manufacturing Settings",
- "material_consumption"):
- qty -= consumed_qty
+ d[item.warehouse] -= qty
if cint(frappe.get_cached_value('UOM', item.stock_uom, 'must_be_whole_number')):
qty = frappe.utils.ceil(qty)
@@ -1673,6 +1674,8 @@ class StockEntry(StockController):
for d in self.get("items"):
item_code = d.get('original_item') or d.get('item_code')
reserve_warehouse = item_wh.get(item_code)
+ if not (reserve_warehouse and item_code):
+ continue
stock_bin = get_bin(item_code, reserve_warehouse)
stock_bin.update_reserved_qty_for_sub_contracting()
diff --git a/erpnext/stock/doctype/stock_ledger_entry/test_stock_ledger_entry.py b/erpnext/stock/doctype/stock_ledger_entry/test_stock_ledger_entry.py
index cafbd7581ce..a1030d54964 100644
--- a/erpnext/stock/doctype/stock_ledger_entry/test_stock_ledger_entry.py
+++ b/erpnext/stock/doctype/stock_ledger_entry/test_stock_ledger_entry.py
@@ -5,7 +5,10 @@ import frappe
from frappe.core.page.permission_manager.permission_manager import reset
from frappe.utils import add_days, today
-from erpnext.stock.doctype.delivery_note.test_delivery_note import create_delivery_note
+from erpnext.stock.doctype.delivery_note.test_delivery_note import (
+ create_delivery_note,
+ create_return_delivery_note,
+)
from erpnext.stock.doctype.item.test_item import make_item
from erpnext.stock.doctype.landed_cost_voucher.test_landed_cost_voucher import (
create_landed_cost_voucher,
@@ -232,8 +235,7 @@ class TestStockLedgerEntry(ERPNextTestCase):
self.assertEqual(outgoing_rate, 100)
# Return Entry: Qty = -2, Rate = 150
- return_dn = create_delivery_note(is_return=1, return_against=dn.name, item_code=bundled_item, qty=-2, rate=150,
- company=company, warehouse="Stores - _TC", expense_account="Cost of Goods Sold - _TC", cost_center="Main - _TC")
+ return_dn = create_return_delivery_note(source_name=dn.name, rate=150, qty=-2)
# check incoming rate for Return entry
incoming_rate, stock_value_difference = frappe.db.get_value("Stock Ledger Entry",
diff --git a/erpnext/stock/doctype/stock_reconciliation/test_stock_reconciliation.py b/erpnext/stock/doctype/stock_reconciliation/test_stock_reconciliation.py
index 1984004df83..3bdf7e21af7 100644
--- a/erpnext/stock/doctype/stock_reconciliation/test_stock_reconciliation.py
+++ b/erpnext/stock/doctype/stock_reconciliation/test_stock_reconciliation.py
@@ -6,7 +6,7 @@
import frappe
-from frappe.utils import add_days, flt, nowdate, nowtime, random_string
+from frappe.utils import add_days, cstr, flt, nowdate, nowtime, random_string
from erpnext.accounts.utils import get_stock_and_account_balance
from erpnext.stock.doctype.item.test_item import create_item
@@ -439,8 +439,8 @@ class TestStockReconciliation(ERPNextTestCase):
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
@@ -466,6 +466,31 @@ class TestStockReconciliation(ERPNextTestCase):
self.assertEqual(len(active_sr_no), 10)
+ def test_serial_no_creation_and_inactivation(self):
+ item = create_item("_TestItemCreatedWithStockReco", is_stock_item=1)
+ if not item.has_serial_no:
+ item.has_serial_no = 1
+ item.save()
+
+ item_code = item.name
+ warehouse = "_Test Warehouse - _TC"
+
+ sr = create_stock_reconciliation(item_code=item.name, warehouse=warehouse,
+ serial_no="SR-CREATED-SR-NO", qty=1, do_not_submit=True, rate=100)
+ sr.save()
+ self.assertEqual(cstr(sr.items[0].current_serial_no), "")
+ sr.submit()
+
+ active_sr_no = frappe.get_all("Serial No",
+ filters={"item_code": item_code, "warehouse": warehouse, "status": "Active"})
+ self.assertEqual(len(active_sr_no), 1)
+
+ 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), 0)
+
+
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/report/stock_ledger/stock_ledger.js b/erpnext/stock/report/stock_ledger/stock_ledger.js
index fe2417bba7e..ef7c2cc7d9e 100644
--- a/erpnext/stock/report/stock_ledger/stock_ledger.js
+++ b/erpnext/stock/report/stock_ledger/stock_ledger.js
@@ -86,10 +86,10 @@ frappe.query_reports["Stock Ledger"] = {
],
"formatter": function (value, row, column, data, default_formatter) {
value = default_formatter(value, row, column, data);
- if (column.fieldname == "out_qty" && data.out_qty < 0) {
+ if (column.fieldname == "out_qty" && data && data.out_qty < 0) {
value = "" + value + "";
}
- else if (column.fieldname == "in_qty" && data.in_qty > 0) {
+ else if (column.fieldname == "in_qty" && data && data.in_qty > 0) {
value = "" + value + "";
}
diff --git a/erpnext/stock/utils.py b/erpnext/stock/utils.py
index 4a8c97fb10a..b8bdf39301e 100644
--- a/erpnext/stock/utils.py
+++ b/erpnext/stock/utils.py
@@ -104,7 +104,7 @@ def get_stock_balance(item_code, warehouse, posting_date=None, posting_time=None
serial_nos = get_serial_nos_data_after_transactions(args)
return ((last_entry.qty_after_transaction, last_entry.valuation_rate, serial_nos)
- if last_entry else (0.0, 0.0, 0.0))
+ if last_entry else (0.0, 0.0, None))
else:
return (last_entry.qty_after_transaction, last_entry.valuation_rate) if last_entry else (0.0, 0.0)
else:
@@ -177,13 +177,7 @@ def get_latest_stock_balance():
def get_bin(item_code, warehouse):
bin = frappe.db.get_value("Bin", {"item_code": item_code, "warehouse": warehouse})
if not bin:
- bin_obj = frappe.get_doc({
- "doctype": "Bin",
- "item_code": item_code,
- "warehouse": warehouse,
- })
- bin_obj.flags.ignore_permissions = 1
- bin_obj.insert()
+ bin_obj = _create_bin(item_code, warehouse)
else:
bin_obj = frappe.get_doc('Bin', bin, for_update=True)
bin_obj.flags.ignore_permissions = True
@@ -193,16 +187,24 @@ 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:
- bin_obj = frappe.get_doc({
- "doctype": "Bin",
- "item_code": item_code,
- "warehouse": warehouse,
- })
+ bin_obj = _create_bin(item_code, warehouse)
+ bin_record = bin_obj.name
+ return bin_record
+
+def _create_bin(item_code, warehouse):
+ """Create a bin and take care of concurrent inserts."""
+
+ bin_creation_savepoint = "create_bin"
+ try:
+ frappe.db.savepoint(bin_creation_savepoint)
+ bin_obj = frappe.get_doc(doctype="Bin", item_code=item_code, warehouse=warehouse)
bin_obj.flags.ignore_permissions = 1
bin_obj.insert()
- bin_record = bin_obj.name
+ except frappe.UniqueValidationError:
+ frappe.db.rollback(save_point=bin_creation_savepoint) # preserve transaction in postgres
+ bin_obj = frappe.get_last_doc("Bin", {"item_code": item_code, "warehouse": warehouse})
- return bin_record
+ return bin_obj
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."""
@@ -420,6 +422,19 @@ def is_reposting_item_valuation_in_progress():
if reposting_in_progress:
frappe.msgprint(_("Item valuation reposting in progress. Report might show incorrect item valuation."), alert=1)
+
+def calculate_mapped_packed_items_return(return_doc):
+ parent_items = set([item.parent_item for item in return_doc.packed_items])
+ against_doc = frappe.get_doc(return_doc.doctype, return_doc.return_against)
+
+ for original_bundle, returned_bundle in zip(against_doc.items, return_doc.items):
+ if original_bundle.item_code in parent_items:
+ for returned_packed_item, original_packed_item in zip(return_doc.packed_items, against_doc.packed_items):
+ if returned_packed_item.parent_item == original_bundle.item_code:
+ returned_packed_item.parent_detail_docname = returned_bundle.name
+ returned_packed_item.qty = (original_packed_item.qty / original_bundle.qty) * returned_bundle.qty
+
+
def check_pending_reposting(posting_date: str, throw_error: bool = True) -> bool:
"""Check if there are pending reposting job till the specified posting date."""
diff --git a/erpnext/tests/utils.py b/erpnext/tests/utils.py
index fbf25948a79..2378a54311f 100644
--- a/erpnext/tests/utils.py
+++ b/erpnext/tests/utils.py
@@ -92,6 +92,8 @@ def change_settings(doctype, settings_dict):
for key, value in settings_dict.items():
setattr(settings, key, value)
settings.save()
+ # singles are cached by default, clear to avoid flake
+ frappe.db.value_cache[settings] = {}
yield # yield control to calling function
finally: