From be15e16b84f6f2b8bd3cf421c0a7ac06cc599988 Mon Sep 17 00:00:00 2001 From: GangaManoj Date: Mon, 5 Jul 2021 14:58:32 +0530 Subject: [PATCH 01/51] feat(Accounts Settings): Add 'Enable Discount Accounting' checkbox --- .../doctype/accounts_settings/accounts_settings.json | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/erpnext/accounts/doctype/accounts_settings/accounts_settings.json b/erpnext/accounts/doctype/accounts_settings/accounts_settings.json index 703e93c0757c..676c6a8b479a 100644 --- a/erpnext/accounts/doctype/accounts_settings/accounts_settings.json +++ b/erpnext/accounts/doctype/accounts_settings/accounts_settings.json @@ -12,6 +12,7 @@ "role_allowed_to_over_bill", "make_payment_via_journal_entry", "column_break_11", + "enable_discount_accounting", "check_supplier_invoice_uniqueness", "unlink_payment_on_cancellation_of_invoice", "automatically_fetch_payment_terms", @@ -261,6 +262,12 @@ "fieldname": "post_change_gl_entries", "fieldtype": "Check", "label": "Create Ledger Entries for Change Amount" + }, + { + "default": "0", + "fieldname": "enable_discount_accounting", + "fieldtype": "Check", + "label": "Enable Discount Accounting" } ], "icon": "icon-cog", @@ -268,7 +275,7 @@ "index_web_pages_for_search": 1, "issingle": 1, "links": [], - "modified": "2021-06-17 20:26:03.721202", + "modified": "2021-07-05 14:56:19.820731", "modified_by": "Administrator", "module": "Accounts", "name": "Accounts Settings", From 6e0d83d925241f04a9897fa39227624a043fe33f Mon Sep 17 00:00:00 2001 From: GangaManoj Date: Mon, 5 Jul 2021 15:09:05 +0530 Subject: [PATCH 02/51] feat(Sales Invoice): Add 'Discount Account' field in Items table --- .../doctype/sales_invoice_item/sales_invoice_item.json | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/erpnext/accounts/doctype/sales_invoice_item/sales_invoice_item.json b/erpnext/accounts/doctype/sales_invoice_item/sales_invoice_item.json index 8e6952a93c43..b65903bf5e8a 100644 --- a/erpnext/accounts/doctype/sales_invoice_item/sales_invoice_item.json +++ b/erpnext/accounts/doctype/sales_invoice_item/sales_invoice_item.json @@ -63,6 +63,7 @@ "finance_book", "col_break4", "expense_account", + "discount_account", "deferred_revenue", "deferred_revenue_account", "service_stop_date", @@ -821,12 +822,18 @@ "no_copy": 1, "options": "currency", "read_only": 1 + }, + { + "fieldname": "discount_account", + "fieldtype": "Link", + "label": "Discount Account", + "options": "Account" } ], "idx": 1, "istable": 1, "links": [], - "modified": "2021-02-23 01:05:22.123527", + "modified": "2021-07-05 15:07:22.857128", "modified_by": "Administrator", "module": "Accounts", "name": "Sales Invoice Item", From cf7579e29f1bbf58eb13562016188f0552d64c93 Mon Sep 17 00:00:00 2001 From: GangaManoj Date: Tue, 6 Jul 2021 00:36:06 +0530 Subject: [PATCH 03/51] feat: Create GL Entries for discount accounting --- .../doctype/sales_invoice/sales_invoice.py | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py index 55a5b99907b0..15e795135933 100644 --- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py +++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py @@ -845,6 +845,7 @@ def get_gl_entries(self, warehouse_account=None): self.allocate_advance_taxes(gl_entries) self.make_item_gl_entries(gl_entries) + self.make_discount_gl_entries(gl_entries) # merge gl entries before adding pos entries gl_entries = merge_similar_entries(gl_entries) @@ -958,6 +959,40 @@ def make_item_gl_entries(self, gl_entries): erpnext.is_perpetual_inventory_enabled(self.company): gl_entries += super(SalesInvoice, self).get_gl_entries() + def make_discount_gl_entries(self, gl_entries): + enable_discount_accounting = cint(frappe.db.get_single_value('Accounts Settings', 'enable_discount_accounting')) + + if enable_discount_accounting: + for item in self.get("items"): + if item.get('discount_amount') and item.get('discount_account'): + account_currency = get_account_currency(item.discount_account) + gl_entries.append( + self.get_gl_dict({ + "account": item.discount_account, + "against": self.customer, + "debit": flt(item.discount_amount), + "debit_in_account_currency": flt(item.discount_amount), + "cost_center": self.cost_center, + "project": self.project + }, account_currency, item=self) + ) + + income_account = (item.income_account + if (not item.enable_deferred_revenue or self.is_return) + else item.deferred_revenue_account) + + account_currency = get_account_currency(income_account) + gl_entries.append( + self.get_gl_dict({ + "account": income_account, + "against": self.customer, + "credit": flt(item.discount_amount), + "credit_in_account_currency": flt(item.discount_amount), + "cost_center": item.cost_center, + "project": item.project or self.project + }, account_currency, item=item) + ) + def make_loyalty_point_redemption_gle(self, gl_entries): if cint(self.redeem_loyalty_points): gl_entries.append( From b08bb1f1a2e679c3c0c01f4a304937243c84121d Mon Sep 17 00:00:00 2001 From: GangaManoj Date: Tue, 6 Jul 2021 16:28:19 +0530 Subject: [PATCH 04/51] feat: Filter list for Discount Account field in Items table --- erpnext/accounts/doctype/sales_invoice/sales_invoice.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.js b/erpnext/accounts/doctype/sales_invoice/sales_invoice.js index bb5565167022..8672e7b1a699 100644 --- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.js +++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.js @@ -510,6 +510,14 @@ cur_frm.set_query("income_account", "items", function(doc) { } }); +// Discount Account in Details Table +// -------------------------------- +cur_frm.set_query("discount_account", "items", function(doc) { + return{ + query: "erpnext.controllers.queries.get_income_account", + filters: {'company': doc.company} + } +}); // Cost Center in Details Table // ----------------------------- From ef667a9a64b5e4b23551c4b150b4371b6f0e72b7 Mon Sep 17 00:00:00 2001 From: GangaManoj Date: Tue, 6 Jul 2021 16:29:19 +0530 Subject: [PATCH 05/51] feat: Add Default Discount Account field --- erpnext/stock/doctype/item/item.json | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/erpnext/stock/doctype/item/item.json b/erpnext/stock/doctype/item/item.json index 6fed9efa63fb..f1c413e00a0e 100644 --- a/erpnext/stock/doctype/item/item.json +++ b/erpnext/stock/doctype/item/item.json @@ -91,6 +91,7 @@ "is_sales_item", "column_break3", "max_discount", + "default_discount_account", "deferred_revenue", "deferred_revenue_account", "enable_deferred_revenue", @@ -1058,6 +1059,12 @@ "fieldname": "website_image_alt", "fieldtype": "Data", "label": "Image Description" + }, + { + "fieldname": "default_discount_account", + "fieldtype": "Link", + "label": "Default Discount Account", + "options": "Account" } ], "has_web_view": 1, @@ -1067,7 +1074,7 @@ "index_web_pages_for_search": 1, "links": [], "max_attachments": 1, - "modified": "2021-03-18 14:04:38.575519", + "modified": "2021-07-06 00:46:15.878648", "modified_by": "Administrator", "module": "Stock", "name": "Item", @@ -1138,4 +1145,4 @@ "sort_order": "DESC", "title_field": "item_name", "track_changes": 1 -} +} \ No newline at end of file From 657e9b5320d42f2622c0663c358bd08221282de3 Mon Sep 17 00:00:00 2001 From: GangaManoj Date: Tue, 6 Jul 2021 16:51:12 +0530 Subject: [PATCH 06/51] feat: Assign Item's Default Discount Account if present --- erpnext/stock/get_item_details.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/erpnext/stock/get_item_details.py b/erpnext/stock/get_item_details.py index ca174a3f63c0..662ca379096e 100644 --- a/erpnext/stock/get_item_details.py +++ b/erpnext/stock/get_item_details.py @@ -288,6 +288,7 @@ def get_basic_details(args, item, overwrite_warehouse=True): "warehouse": warehouse, "income_account": get_default_income_account(args, item_defaults, item_group_defaults, brand_defaults), "expense_account": expense_account or get_default_expense_account(args, item_defaults, item_group_defaults, brand_defaults) , + "discount_account": None or get_default_discount_account(args, item_defaults), "cost_center": get_default_cost_center(args, item_defaults, item_group_defaults, brand_defaults), 'has_serial_no': item.has_serial_no, 'has_batch_no': item.has_batch_no, @@ -590,6 +591,10 @@ def get_default_expense_account(args, item, item_group, brand): or brand.get("expense_account") or args.expense_account) +def get_default_discount_account(args, item_defaults): + return (item_defaults.default_discount_account + or args.discount_account) + def get_default_deferred_account(args, item, fieldname=None): if item.get("enable_deferred_revenue") or item.get("enable_deferred_expense"): return (item.get(fieldname) From 3ea4f993b799685c4d11ddde003547faaba5da42 Mon Sep 17 00:00:00 2001 From: GangaManoj Date: Tue, 6 Jul 2021 20:22:13 +0530 Subject: [PATCH 07/51] feat: Toggle display for discount accounting fields according to enable_discount_accounting --- .../doctype/accounts_settings/accounts_settings.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/erpnext/accounts/doctype/accounts_settings/accounts_settings.py b/erpnext/accounts/doctype/accounts_settings/accounts_settings.py index ac4a2d6f16de..9e33eb395b76 100644 --- a/erpnext/accounts/doctype/accounts_settings/accounts_settings.py +++ b/erpnext/accounts/doctype/accounts_settings/accounts_settings.py @@ -21,6 +21,7 @@ def validate(self): self.validate_stale_days() self.enable_payment_schedule_in_print() + self.toggle_discount_accounting_fields() def validate_stale_days(self): if not self.allow_stale and cint(self.stale_days) <= 0: @@ -33,3 +34,9 @@ def enable_payment_schedule_in_print(self): for doctype in ("Sales Order", "Sales Invoice", "Purchase Order", "Purchase Invoice"): make_property_setter(doctype, "due_date", "print_hide", show_in_print, "Check", validate_fields_for_doctype=False) make_property_setter(doctype, "payment_schedule", "print_hide", 0 if show_in_print else 1, "Check", validate_fields_for_doctype=False) + + def toggle_discount_accounting_fields(self): + enable_discount_accounting = cint(self.enable_discount_accounting) + + make_property_setter("Sales Invoice Item", "discount_account", "hidden", not(enable_discount_accounting), "Check", validate_fields_for_doctype=False) + make_property_setter("Item", "default_discount_account", "hidden", not(enable_discount_accounting), "Check", validate_fields_for_doctype=False) \ No newline at end of file From 0d5ad3b1bb3933e225fdf98d6b04afae6647e357 Mon Sep 17 00:00:00 2001 From: GangaManoj Date: Mon, 12 Jul 2021 18:56:06 +0530 Subject: [PATCH 08/51] fix: Add description for Enable Discount Accounting checkbox --- .../doctype/accounts_settings/accounts_settings.json | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/erpnext/accounts/doctype/accounts_settings/accounts_settings.json b/erpnext/accounts/doctype/accounts_settings/accounts_settings.json index 676c6a8b479a..49a2afee85f8 100644 --- a/erpnext/accounts/doctype/accounts_settings/accounts_settings.json +++ b/erpnext/accounts/doctype/accounts_settings/accounts_settings.json @@ -12,7 +12,6 @@ "role_allowed_to_over_bill", "make_payment_via_journal_entry", "column_break_11", - "enable_discount_accounting", "check_supplier_invoice_uniqueness", "unlink_payment_on_cancellation_of_invoice", "automatically_fetch_payment_terms", @@ -20,6 +19,7 @@ "book_asset_depreciation_entry_automatically", "unlink_advance_payment_on_cancelation_of_order", "post_change_gl_entries", + "enable_discount_accounting", "tax_settings_section", "determine_address_tax_category_from", "column_break_19", @@ -265,6 +265,7 @@ }, { "default": "0", + "description": "If enabled, additional ledger entries will be made for discounts in a separate Discount Account", "fieldname": "enable_discount_accounting", "fieldtype": "Check", "label": "Enable Discount Accounting" @@ -275,7 +276,7 @@ "index_web_pages_for_search": 1, "issingle": 1, "links": [], - "modified": "2021-07-05 14:56:19.820731", + "modified": "2021-07-12 18:54:29.084958", "modified_by": "Administrator", "module": "Accounts", "name": "Accounts Settings", From b22dba1146b9a5d869b885cb47a1d989ecdd3b64 Mon Sep 17 00:00:00 2001 From: GangaManoj Date: Mon, 12 Jul 2021 19:07:54 +0530 Subject: [PATCH 09/51] fix: Filter Discount Account list --- .../doctype/sales_invoice/sales_invoice.js | 22 +++++++++++-------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.js b/erpnext/accounts/doctype/sales_invoice/sales_invoice.js index 8672e7b1a699..f84b6463979d 100644 --- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.js +++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.js @@ -510,15 +510,6 @@ cur_frm.set_query("income_account", "items", function(doc) { } }); -// Discount Account in Details Table -// -------------------------------- -cur_frm.set_query("discount_account", "items", function(doc) { - return{ - query: "erpnext.controllers.queries.get_income_account", - filters: {'company': doc.company} - } -}); - // Cost Center in Details Table // ----------------------------- cur_frm.fields_dict["items"].grid.get_field("cost_center").get_query = function(doc) { @@ -626,6 +617,19 @@ frappe.ui.form.on('Sales Invoice', { } } + // discount account + frm.fields_dict['items'].grid.get_field('discount_account').get_query = function(doc) { + if (erpnext.is_perpetual_inventory_enabled(doc.company)) { + return { + filters: { + 'report_type': 'Profit and Loss', + 'company': doc.company, + "is_group": 0 + } + } + } + } + frm.fields_dict['items'].grid.get_field('deferred_revenue_account').get_query = function(doc) { return { filters: { From 4fcdc5d129ab3f3655108c2a0bbde4b48f15c3e2 Mon Sep 17 00:00:00 2001 From: GangaManoj Date: Mon, 12 Jul 2021 22:32:38 +0530 Subject: [PATCH 10/51] fix: Copy discount account from first row to all Items --- erpnext/accounts/doctype/sales_invoice/sales_invoice.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.js b/erpnext/accounts/doctype/sales_invoice/sales_invoice.js index f84b6463979d..288e2a7566fe 100644 --- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.js +++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.js @@ -347,7 +347,7 @@ erpnext.accounts.SalesInvoiceController = class SalesInvoiceController extends e items_add(doc, cdt, cdn) { var row = frappe.get_doc(cdt, cdn); - this.frm.script_manager.copy_from_first_row("items", row, ["income_account", "cost_center"]); + this.frm.script_manager.copy_from_first_row("items", row, ["income_account", "discount_account", "cost_center"]); } set_dynamic_labels() { From 0b7d8fb3afb4ffa5662d7e657c357a49fde323b1 Mon Sep 17 00:00:00 2001 From: GangaManoj Date: Tue, 13 Jul 2021 01:37:30 +0530 Subject: [PATCH 11/51] fix: Move Default Discount Account field to Item Defaults --- erpnext/stock/doctype/item/item.json | 9 +- .../doctype/item_default/item_default.json | 548 ++++-------------- erpnext/stock/get_item_details.py | 4 +- 3 files changed, 104 insertions(+), 457 deletions(-) diff --git a/erpnext/stock/doctype/item/item.json b/erpnext/stock/doctype/item/item.json index f1c413e00a0e..f662bbd1c79c 100644 --- a/erpnext/stock/doctype/item/item.json +++ b/erpnext/stock/doctype/item/item.json @@ -91,7 +91,6 @@ "is_sales_item", "column_break3", "max_discount", - "default_discount_account", "deferred_revenue", "deferred_revenue_account", "enable_deferred_revenue", @@ -1059,12 +1058,6 @@ "fieldname": "website_image_alt", "fieldtype": "Data", "label": "Image Description" - }, - { - "fieldname": "default_discount_account", - "fieldtype": "Link", - "label": "Default Discount Account", - "options": "Account" } ], "has_web_view": 1, @@ -1074,7 +1067,7 @@ "index_web_pages_for_search": 1, "links": [], "max_attachments": 1, - "modified": "2021-07-06 00:46:15.878648", + "modified": "2021-07-13 01:29:06.071827", "modified_by": "Administrator", "module": "Stock", "name": "Item", diff --git a/erpnext/stock/doctype/item_default/item_default.json b/erpnext/stock/doctype/item_default/item_default.json index 96b5dfdc8f78..bc171604f43c 100644 --- a/erpnext/stock/doctype/item_default/item_default.json +++ b/erpnext/stock/doctype/item_default/item_default.json @@ -1,464 +1,118 @@ { - "allow_copy": 0, - "allow_events_in_timeline": 0, - "allow_guest_to_view": 0, - "allow_import": 0, - "allow_rename": 0, - "beta": 0, - "creation": "2018-05-03 02:29:24.444341", - "custom": 0, - "docstatus": 0, - "doctype": "DocType", - "document_type": "", - "editable_grid": 1, - "engine": "InnoDB", + "actions": [], + "creation": "2018-05-03 02:29:24.444341", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "company", + "default_warehouse", + "column_break_3", + "default_price_list", + "default_discount_account", + "purchase_defaults", + "buying_cost_center", + "default_supplier", + "column_break_8", + "expense_account", + "selling_defaults", + "selling_cost_center", + "column_break_12", + "income_account" + ], "fields": [ { - "allow_bulk_edit": 0, - "allow_in_quick_entry": 0, - "allow_on_submit": 0, - "bold": 0, - "collapsible": 0, - "columns": 0, - "fieldname": "company", - "fieldtype": "Link", - "hidden": 0, - "ignore_user_permissions": 1, - "ignore_xss_filter": 0, - "in_filter": 0, - "in_global_search": 0, - "in_list_view": 1, - "in_standard_filter": 0, - "label": "Company", - "length": 0, - "no_copy": 0, - "options": "Company", - "permlevel": 0, - "precision": "", - "print_hide": 0, - "print_hide_if_no_value": 0, - "read_only": 0, - "remember_last_selected_value": 0, - "report_hide": 0, - "reqd": 1, - "search_index": 0, - "set_only_once": 0, - "translatable": 0, - "unique": 0 - }, + "fieldname": "company", + "fieldtype": "Link", + "ignore_user_permissions": 1, + "in_list_view": 1, + "label": "Company", + "options": "Company", + "reqd": 1 + }, { - "allow_bulk_edit": 0, - "allow_in_quick_entry": 0, - "allow_on_submit": 0, - "bold": 0, - "collapsible": 0, - "columns": 0, - "fieldname": "default_warehouse", - "fieldtype": "Link", - "hidden": 0, - "ignore_user_permissions": 0, - "ignore_xss_filter": 0, - "in_filter": 0, - "in_global_search": 0, - "in_list_view": 1, - "in_standard_filter": 0, - "label": "Default Warehouse", - "length": 0, - "no_copy": 0, - "options": "Warehouse", - "permlevel": 0, - "precision": "", - "print_hide": 0, - "print_hide_if_no_value": 0, - "read_only": 0, - "remember_last_selected_value": 0, - "report_hide": 0, - "reqd": 0, - "search_index": 0, - "set_only_once": 0, - "translatable": 0, - "unique": 0 - }, + "fieldname": "default_warehouse", + "fieldtype": "Link", + "in_list_view": 1, + "label": "Default Warehouse", + "options": "Warehouse" + }, { - "allow_bulk_edit": 0, - "allow_in_quick_entry": 0, - "allow_on_submit": 0, - "bold": 0, - "collapsible": 0, - "columns": 0, - "fieldname": "column_break_3", - "fieldtype": "Column Break", - "hidden": 0, - "ignore_user_permissions": 0, - "ignore_xss_filter": 0, - "in_filter": 0, - "in_global_search": 0, - "in_list_view": 0, - "in_standard_filter": 0, - "length": 0, - "no_copy": 0, - "permlevel": 0, - "precision": "", - "print_hide": 0, - "print_hide_if_no_value": 0, - "read_only": 0, - "remember_last_selected_value": 0, - "report_hide": 0, - "reqd": 0, - "search_index": 0, - "set_only_once": 0, - "translatable": 0, - "unique": 0 - }, + "fieldname": "column_break_3", + "fieldtype": "Column Break" + }, { - "allow_bulk_edit": 0, - "allow_in_quick_entry": 0, - "allow_on_submit": 0, - "bold": 0, - "collapsible": 0, - "columns": 0, - "fieldname": "default_price_list", - "fieldtype": "Link", - "hidden": 0, - "ignore_user_permissions": 0, - "ignore_xss_filter": 0, - "in_filter": 0, - "in_global_search": 0, - "in_list_view": 1, - "in_standard_filter": 0, - "label": "Default Price List", - "length": 0, - "no_copy": 0, - "options": "Price List", - "permlevel": 0, - "precision": "", - "print_hide": 0, - "print_hide_if_no_value": 0, - "read_only": 0, - "remember_last_selected_value": 0, - "report_hide": 0, - "reqd": 0, - "search_index": 0, - "set_only_once": 0, - "translatable": 0, - "unique": 0 - }, + "fieldname": "default_price_list", + "fieldtype": "Link", + "in_list_view": 1, + "label": "Default Price List", + "options": "Price List" + }, { - "allow_bulk_edit": 0, - "allow_in_quick_entry": 0, - "allow_on_submit": 0, - "bold": 0, - "collapsible": 0, - "columns": 0, - "fieldname": "purchase_defaults", - "fieldtype": "Section Break", - "hidden": 0, - "ignore_user_permissions": 0, - "ignore_xss_filter": 0, - "in_filter": 0, - "in_global_search": 0, - "in_list_view": 0, - "in_standard_filter": 0, - "label": "Purchase Defaults", - "length": 0, - "no_copy": 0, - "permlevel": 0, - "precision": "", - "print_hide": 0, - "print_hide_if_no_value": 0, - "read_only": 0, - "remember_last_selected_value": 0, - "report_hide": 0, - "reqd": 0, - "search_index": 0, - "set_only_once": 0, - "translatable": 0, - "unique": 0 - }, + "fieldname": "purchase_defaults", + "fieldtype": "Section Break", + "label": "Purchase Defaults" + }, { - "allow_bulk_edit": 0, - "allow_in_quick_entry": 0, - "allow_on_submit": 0, - "bold": 0, - "collapsible": 0, - "columns": 0, - "fieldname": "buying_cost_center", - "fieldtype": "Link", - "hidden": 0, - "ignore_user_permissions": 0, - "ignore_xss_filter": 0, - "in_filter": 0, - "in_global_search": 0, - "in_list_view": 0, - "in_standard_filter": 0, - "label": "Default Buying Cost Center", - "length": 0, - "no_copy": 0, - "options": "Cost Center", - "permlevel": 0, - "precision": "", - "print_hide": 0, - "print_hide_if_no_value": 0, - "read_only": 0, - "remember_last_selected_value": 0, - "report_hide": 0, - "reqd": 0, - "search_index": 0, - "set_only_once": 0, - "translatable": 0, - "unique": 0 - }, + "fieldname": "buying_cost_center", + "fieldtype": "Link", + "label": "Default Buying Cost Center", + "options": "Cost Center" + }, { - "allow_bulk_edit": 0, - "allow_in_quick_entry": 0, - "allow_on_submit": 0, - "bold": 0, - "collapsible": 0, - "columns": 0, - "fieldname": "default_supplier", - "fieldtype": "Link", - "hidden": 0, - "ignore_user_permissions": 0, - "ignore_xss_filter": 0, - "in_filter": 0, - "in_global_search": 0, - "in_list_view": 0, - "in_standard_filter": 0, - "label": "Default Supplier", - "length": 0, - "no_copy": 0, - "options": "Supplier", - "permlevel": 0, - "precision": "", - "print_hide": 0, - "print_hide_if_no_value": 0, - "read_only": 0, - "remember_last_selected_value": 0, - "report_hide": 0, - "reqd": 0, - "search_index": 0, - "set_only_once": 0, - "translatable": 0, - "unique": 0 - }, + "fieldname": "default_supplier", + "fieldtype": "Link", + "label": "Default Supplier", + "options": "Supplier" + }, { - "allow_bulk_edit": 0, - "allow_in_quick_entry": 0, - "allow_on_submit": 0, - "bold": 0, - "collapsible": 0, - "columns": 0, - "fieldname": "column_break_8", - "fieldtype": "Column Break", - "hidden": 0, - "ignore_user_permissions": 0, - "ignore_xss_filter": 0, - "in_filter": 0, - "in_global_search": 0, - "in_list_view": 0, - "in_standard_filter": 0, - "length": 0, - "no_copy": 0, - "permlevel": 0, - "precision": "", - "print_hide": 0, - "print_hide_if_no_value": 0, - "read_only": 0, - "remember_last_selected_value": 0, - "report_hide": 0, - "reqd": 0, - "search_index": 0, - "set_only_once": 0, - "translatable": 0, - "unique": 0 - }, + "fieldname": "column_break_8", + "fieldtype": "Column Break" + }, { - "allow_bulk_edit": 0, - "allow_in_quick_entry": 0, - "allow_on_submit": 0, - "bold": 0, - "collapsible": 0, - "columns": 0, - "fieldname": "expense_account", - "fieldtype": "Link", - "hidden": 0, - "ignore_user_permissions": 0, - "ignore_xss_filter": 0, - "in_filter": 0, - "in_global_search": 0, - "in_list_view": 0, - "in_standard_filter": 0, - "label": "Default Expense Account", - "length": 0, - "no_copy": 0, - "options": "Account", - "permlevel": 0, - "precision": "", - "print_hide": 0, - "print_hide_if_no_value": 0, - "read_only": 0, - "remember_last_selected_value": 0, - "report_hide": 0, - "reqd": 0, - "search_index": 0, - "set_only_once": 0, - "translatable": 0, - "unique": 0 - }, + "fieldname": "expense_account", + "fieldtype": "Link", + "label": "Default Expense Account", + "options": "Account" + }, { - "allow_bulk_edit": 0, - "allow_in_quick_entry": 0, - "allow_on_submit": 0, - "bold": 0, - "collapsible": 0, - "columns": 0, - "fieldname": "selling_defaults", - "fieldtype": "Section Break", - "hidden": 0, - "ignore_user_permissions": 0, - "ignore_xss_filter": 0, - "in_filter": 0, - "in_global_search": 0, - "in_list_view": 0, - "in_standard_filter": 0, - "label": "Sales Defaults", - "length": 0, - "no_copy": 0, - "permlevel": 0, - "precision": "", - "print_hide": 0, - "print_hide_if_no_value": 0, - "read_only": 0, - "remember_last_selected_value": 0, - "report_hide": 0, - "reqd": 0, - "search_index": 0, - "set_only_once": 0, - "translatable": 0, - "unique": 0 - }, + "fieldname": "selling_defaults", + "fieldtype": "Section Break", + "label": "Sales Defaults" + }, { - "allow_bulk_edit": 0, - "allow_in_quick_entry": 0, - "allow_on_submit": 0, - "bold": 0, - "collapsible": 0, - "columns": 0, - "fieldname": "selling_cost_center", - "fieldtype": "Link", - "hidden": 0, - "ignore_user_permissions": 0, - "ignore_xss_filter": 0, - "in_filter": 0, - "in_global_search": 0, - "in_list_view": 0, - "in_standard_filter": 0, - "label": "Default Selling Cost Center", - "length": 0, - "no_copy": 0, - "options": "Cost Center", - "permlevel": 0, - "precision": "", - "print_hide": 0, - "print_hide_if_no_value": 0, - "read_only": 0, - "remember_last_selected_value": 0, - "report_hide": 0, - "reqd": 0, - "search_index": 0, - "set_only_once": 0, - "translatable": 0, - "unique": 0 - }, + "fieldname": "selling_cost_center", + "fieldtype": "Link", + "label": "Default Selling Cost Center", + "options": "Cost Center" + }, { - "allow_bulk_edit": 0, - "allow_in_quick_entry": 0, - "allow_on_submit": 0, - "bold": 0, - "collapsible": 0, - "columns": 0, - "fieldname": "column_break_12", - "fieldtype": "Column Break", - "hidden": 0, - "ignore_user_permissions": 0, - "ignore_xss_filter": 0, - "in_filter": 0, - "in_global_search": 0, - "in_list_view": 0, - "in_standard_filter": 0, - "length": 0, - "no_copy": 0, - "permlevel": 0, - "precision": "", - "print_hide": 0, - "print_hide_if_no_value": 0, - "read_only": 0, - "remember_last_selected_value": 0, - "report_hide": 0, - "reqd": 0, - "search_index": 0, - "set_only_once": 0, - "translatable": 0, - "unique": 0 - }, + "fieldname": "column_break_12", + "fieldtype": "Column Break" + }, { - "allow_bulk_edit": 0, - "allow_in_quick_entry": 0, - "allow_on_submit": 0, - "bold": 0, - "collapsible": 0, - "columns": 0, - "fieldname": "income_account", - "fieldtype": "Link", - "hidden": 0, - "ignore_user_permissions": 0, - "ignore_xss_filter": 0, - "in_filter": 0, - "in_global_search": 0, - "in_list_view": 0, - "in_standard_filter": 0, - "label": "Default Income Account", - "length": 0, - "no_copy": 0, - "options": "Account", - "permlevel": 0, - "precision": "", - "print_hide": 0, - "print_hide_if_no_value": 0, - "read_only": 0, - "remember_last_selected_value": 0, - "report_hide": 0, - "reqd": 0, - "search_index": 0, - "set_only_once": 0, - "translatable": 0, - "unique": 0 + "fieldname": "income_account", + "fieldtype": "Link", + "label": "Default Income Account", + "options": "Account" + }, + { + "fieldname": "default_discount_account", + "fieldtype": "Link", + "label": "Default Discount Account", + "options": "Account" } - ], - "has_web_view": 0, - "hide_heading": 0, - "hide_toolbar": 0, - "idx": 0, - "image_view": 0, - "in_create": 0, - "is_submittable": 0, - "issingle": 0, - "istable": 1, - "max_attachments": 0, - "modified": "2018-12-07 11:48:07.638935", - "modified_by": "Administrator", - "module": "Stock", - "name": "Item Default", - "name_case": "", - "owner": "Administrator", - "permissions": [], - "quick_entry": 1, - "read_only": 0, - "read_only_onload": 0, - "show_name_in_global_search": 0, - "sort_field": "modified", - "sort_order": "DESC", - "track_changes": 1, - "track_seen": 0, - "track_views": 0 + ], + "istable": 1, + "links": [], + "modified": "2021-07-13 01:26:03.860065", + "modified_by": "Administrator", + "module": "Stock", + "name": "Item Default", + "owner": "Administrator", + "permissions": [], + "quick_entry": 1, + "sort_field": "modified", + "sort_order": "DESC", + "track_changes": 1 } \ No newline at end of file diff --git a/erpnext/stock/get_item_details.py b/erpnext/stock/get_item_details.py index 662ca379096e..cec485cc1100 100644 --- a/erpnext/stock/get_item_details.py +++ b/erpnext/stock/get_item_details.py @@ -591,8 +591,8 @@ def get_default_expense_account(args, item, item_group, brand): or brand.get("expense_account") or args.expense_account) -def get_default_discount_account(args, item_defaults): - return (item_defaults.default_discount_account +def get_default_discount_account(args, item): + return (item.get("default_discount_account") or args.discount_account) def get_default_deferred_account(args, item, fieldname=None): From b85d3017a11a87cf69383229b25694c175ce30c9 Mon Sep 17 00:00:00 2001 From: GangaManoj Date: Tue, 13 Jul 2021 01:43:41 +0530 Subject: [PATCH 12/51] fix: Filter options for Default Discount Account --- erpnext/stock/doctype/item/item.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/erpnext/stock/doctype/item/item.js b/erpnext/stock/doctype/item/item.js index 45e3c21b27d6..4d8749683c55 100644 --- a/erpnext/stock/doctype/item/item.js +++ b/erpnext/stock/doctype/item/item.js @@ -275,6 +275,17 @@ $.extend(erpnext.item, { } } + frm.fields_dict["item_defaults"].grid.get_field("default_discount_account").get_query = function(doc, cdt, cdn) { + const row = locals[cdt][cdn]; + return { + filters: { + 'report_type': 'Profit and Loss', + 'company': row.company, + "is_group": 0 + } + } + } + frm.fields_dict["item_defaults"].grid.get_field("buying_cost_center").get_query = function(doc, cdt, cdn) { const row = locals[cdt][cdn]; return { From 555852c5ef22ef18b609a8e8a667e42b7f325d19 Mon Sep 17 00:00:00 2001 From: GangaManoj Date: Tue, 13 Jul 2021 02:06:03 +0530 Subject: [PATCH 13/51] fix: Add Discount Account field --- .../purchase_invoice_item/purchase_invoice_item.json | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/erpnext/accounts/doctype/purchase_invoice_item/purchase_invoice_item.json b/erpnext/accounts/doctype/purchase_invoice_item/purchase_invoice_item.json index b39022dd7587..922b567d1521 100644 --- a/erpnext/accounts/doctype/purchase_invoice_item/purchase_invoice_item.json +++ b/erpnext/accounts/doctype/purchase_invoice_item/purchase_invoice_item.json @@ -73,6 +73,7 @@ "manufacturer_part_no", "accounting", "expense_account", + "discount_account", "col_break5", "is_fixed_asset", "asset_location", @@ -849,12 +850,18 @@ "options": "Company:company:default_currency", "print_hide": 1, "read_only": 1 + }, + { + "fieldname": "discount_account", + "fieldtype": "Link", + "label": "Discount Account", + "options": "Account" } ], "idx": 1, "istable": 1, "links": [], - "modified": "2021-06-16 19:43:51.099386", + "modified": "2021-07-13 02:04:37.787882", "modified_by": "Administrator", "module": "Accounts", "name": "Purchase Invoice Item", From 4b8918e8a774d48804e2e3670294c279e0ebfd3e Mon Sep 17 00:00:00 2001 From: GangaManoj Date: Tue, 13 Jul 2021 02:07:46 +0530 Subject: [PATCH 14/51] fix: Display Discount Account only if Enable Discount Accounting is checked --- .../accounts/doctype/accounts_settings/accounts_settings.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/erpnext/accounts/doctype/accounts_settings/accounts_settings.py b/erpnext/accounts/doctype/accounts_settings/accounts_settings.py index 9e33eb395b76..d1abdba37927 100644 --- a/erpnext/accounts/doctype/accounts_settings/accounts_settings.py +++ b/erpnext/accounts/doctype/accounts_settings/accounts_settings.py @@ -38,5 +38,7 @@ def enable_payment_schedule_in_print(self): def toggle_discount_accounting_fields(self): enable_discount_accounting = cint(self.enable_discount_accounting) - make_property_setter("Sales Invoice Item", "discount_account", "hidden", not(enable_discount_accounting), "Check", validate_fields_for_doctype=False) + for doctype in ["Sales Invoice Item", "Purchase Invoice Item"]: + make_property_setter(doctype, "discount_account", "hidden", not(enable_discount_accounting), "Check", validate_fields_for_doctype=False) + make_property_setter("Item", "default_discount_account", "hidden", not(enable_discount_accounting), "Check", validate_fields_for_doctype=False) \ No newline at end of file From 81375aec1f05669503ff57437bd532e10ea9deaa Mon Sep 17 00:00:00 2001 From: GangaManoj Date: Tue, 13 Jul 2021 02:09:40 +0530 Subject: [PATCH 15/51] fix: Copy Discount Account from first row --- erpnext/accounts/doctype/purchase_invoice/purchase_invoice.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.js b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.js index 7562418fd2ff..69c50b452cea 100644 --- a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.js +++ b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.js @@ -365,7 +365,7 @@ erpnext.accounts.PurchaseInvoice = class PurchaseInvoice extends erpnext.buying. items_add(doc, cdt, cdn) { var row = frappe.get_doc(cdt, cdn); this.frm.script_manager.copy_from_first_row("items", row, - ["expense_account", "cost_center", "project"]); + ["expense_account", "discount_account", "cost_center", "project"]); } on_submit() { From 65e2b9fee6406b2981baae5a9d2e90c96504089d Mon Sep 17 00:00:00 2001 From: GangaManoj Date: Tue, 13 Jul 2021 02:14:18 +0530 Subject: [PATCH 16/51] fix: Filter options for Discount Account --- .../doctype/purchase_invoice/purchase_invoice.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.js b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.js index 69c50b452cea..66fa15bc12ec 100644 --- a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.js +++ b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.js @@ -508,6 +508,16 @@ frappe.ui.form.on("Purchase Invoice", { } } } + + frm.fields_dict['items'].grid.get_field('discount_account').get_query = function(doc) { + return { + filters: { + 'report_type': 'Profit and Loss', + 'company': doc.company, + "is_group": 0 + } + } + } }, refresh: function(frm) { From 8f7b0a17534faefc3b28dc3976e5a23e9d4597d3 Mon Sep 17 00:00:00 2001 From: GangaManoj Date: Tue, 13 Jul 2021 03:01:02 +0530 Subject: [PATCH 17/51] fix: Create common function for discount accounting --- .../purchase_invoice/purchase_invoice.py | 1 + .../doctype/sales_invoice/sales_invoice.py | 34 -------------- erpnext/controllers/accounts_controller.py | 45 +++++++++++++++++++ 3 files changed, 46 insertions(+), 34 deletions(-) diff --git a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py index c1cc092554da..fdaa7f091b45 100644 --- a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py +++ b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py @@ -446,6 +446,7 @@ def get_gl_entries(self, warehouse_account=None): self.make_supplier_gl_entry(gl_entries) self.make_item_gl_entries(gl_entries) + self.make_discount_gl_entries(gl_entries) if self.check_asset_cwip_enabled(): self.get_asset_gl_entry(gl_entries) diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py index 15e795135933..02d847031fcf 100644 --- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py +++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py @@ -959,40 +959,6 @@ def make_item_gl_entries(self, gl_entries): erpnext.is_perpetual_inventory_enabled(self.company): gl_entries += super(SalesInvoice, self).get_gl_entries() - def make_discount_gl_entries(self, gl_entries): - enable_discount_accounting = cint(frappe.db.get_single_value('Accounts Settings', 'enable_discount_accounting')) - - if enable_discount_accounting: - for item in self.get("items"): - if item.get('discount_amount') and item.get('discount_account'): - account_currency = get_account_currency(item.discount_account) - gl_entries.append( - self.get_gl_dict({ - "account": item.discount_account, - "against": self.customer, - "debit": flt(item.discount_amount), - "debit_in_account_currency": flt(item.discount_amount), - "cost_center": self.cost_center, - "project": self.project - }, account_currency, item=self) - ) - - income_account = (item.income_account - if (not item.enable_deferred_revenue or self.is_return) - else item.deferred_revenue_account) - - account_currency = get_account_currency(income_account) - gl_entries.append( - self.get_gl_dict({ - "account": income_account, - "against": self.customer, - "credit": flt(item.discount_amount), - "credit_in_account_currency": flt(item.discount_amount), - "cost_center": item.cost_center, - "project": item.project or self.project - }, account_currency, item=item) - ) - def make_loyalty_point_redemption_gle(self, gl_entries): if cint(self.redeem_loyalty_points): gl_entries.append( diff --git a/erpnext/controllers/accounts_controller.py b/erpnext/controllers/accounts_controller.py index 1c086e9edcda..dac7f0bd9e35 100644 --- a/erpnext/controllers/accounts_controller.py +++ b/erpnext/controllers/accounts_controller.py @@ -741,6 +741,51 @@ def update_allocated_advance_taxes_on_cancel(self): tax_map[tax.account_head] -= allocated_amount allocated_tax_map[tax.account_head] -= allocated_amount + def make_discount_gl_entries(self, gl_entries): + enable_discount_accounting = cint(frappe.db.get_single_value('Accounts Settings', 'enable_discount_accounting')) + + if enable_discount_accounting: + for item in self.get("items"): + if item.get('discount_amount') and item.get('discount_account'): + if self.doctype == "Purchase Invoice": + dr_or_cr = "credit" + rev_dr_cr = "debit" + supplier_or_customer = self.supplier + income_or_expense_account = (item.expense_account + if (not item.enable_deferred_expense or self.is_return) + else item.deferred_expense_account) + else: + dr_or_cr = "debit" + rev_dr_cr = "credit" + supplier_or_customer = self.customer + income_or_expense_account = (item.income_account + if (not item.enable_deferred_revenue or self.is_return) + else item.deferred_revenue_account) + + account_currency = get_account_currency(item.discount_account) + gl_entries.append( + self.get_gl_dict({ + "account": item.discount_account, + "against": supplier_or_customer, + dr_or_cr: flt(item.discount_amount), + dr_or_cr + "_in_account_currency": flt(item.discount_amount), + "cost_center": self.cost_center, + "project": self.project + }, account_currency, item=self) + ) + + account_currency = get_account_currency(income_or_expense_account) + gl_entries.append( + self.get_gl_dict({ + "account": income_or_expense_account, + "against": supplier_or_customer, + rev_dr_cr: flt(item.discount_amount), + rev_dr_cr + "_in_account_currency": flt(item.discount_amount), + "cost_center": item.cost_center, + "project": item.project or self.project + }, account_currency, item=item) + ) + def allocate_advance_taxes(self, gl_entries): tax_map = self.get_tax_map() for pe in self.get("advances"): From 2f4c607fc86ac7780491b52f5ef1540a0c8e3598 Mon Sep 17 00:00:00 2001 From: GangaManoj Date: Tue, 13 Jul 2021 17:41:29 +0530 Subject: [PATCH 18/51] fix: Add tests for discount accounting --- .../purchase_invoice/test_purchase_invoice.py | 20 ++++++++++++++++++- .../sales_invoice/test_sales_invoice.py | 15 ++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/erpnext/accounts/doctype/purchase_invoice/test_purchase_invoice.py b/erpnext/accounts/doctype/purchase_invoice/test_purchase_invoice.py index 189260a29daf..1dc048ade025 100644 --- a/erpnext/accounts/doctype/purchase_invoice/test_purchase_invoice.py +++ b/erpnext/accounts/doctype/purchase_invoice/test_purchase_invoice.py @@ -251,6 +251,16 @@ def test_purchase_invoice_with_exchange_rate_difference(self): self.assertEqual(discrepancy_caused_by_exchange_rate_diff, amount) + def test_purchase_invoice_with_discount_accounting_enabled(self): + enable_discount_accounting() + + discount_account = create_account(account_name="Discount Account", + parent_account="Indirect Expenses - _TC", company="_Test Company") + pi = make_purchase_invoice(discount_account=discount_account, discount_amount=100) + + discount_amount = frappe.db.get_value('GL Entry', {'account': discount_account, 'voucher_no': pi.name}, 'credit') + self.assertEqual(discount_amount, 100) + def test_purchase_invoice_change_naming_series(self): pi = frappe.copy_doc(test_records[1]) pi.insert() @@ -1077,6 +1087,11 @@ def unlink_payment_on_cancel_of_invoice(enable=1): accounts_settings.unlink_payment_on_cancellation_of_invoice = enable accounts_settings.save() +def enable_discount_accounting(enable=1): + accounts_settings = frappe.get_doc("Accounts Settings") + accounts_settings.enable_discount_accounting = enable + accounts_settings.save() + def make_purchase_invoice(**args): pi = frappe.new_doc("Purchase Invoice") args = frappe._dict(args) @@ -1099,6 +1114,7 @@ def make_purchase_invoice(**args): pi.return_against = args.return_against pi.is_subcontracted = args.is_subcontracted or "No" pi.supplier_warehouse = args.supplier_warehouse or "_Test Warehouse 1 - _TC" + pi.cost_center = args.cost_center or "_Test Cost Center - _TC" pi.append("items", { "item_code": args.item or args.item_code or "_Test Item", @@ -1107,7 +1123,9 @@ def make_purchase_invoice(**args): "received_qty": args.received_qty or 0, "rejected_qty": args.rejected_qty or 0, "rate": args.rate or 50, - 'expense_account': args.expense_account or '_Test Account Cost for Goods Sold - _TC', + "expense_account": args.expense_account or '_Test Account Cost for Goods Sold - _TC', + "discount_account": args.discount_account or None, + "discount_amount": args.discount_amount or 0, "conversion_factor": 1.0, "serial_no": args.serial_no, "stock_uom": args.uom or "_Test UOM", diff --git a/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py b/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py index fe531d3b227d..d90a00941f83 100644 --- a/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py +++ b/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py @@ -1984,6 +1984,18 @@ def test_item_tax_net_range(self): sales_invoice.save() self.assertEqual(sales_invoice.items[0].item_tax_template, "_Test Account Excise Duty @ 10 - _TC") + def test_sales_invoice_with_discount_accounting_enabled(self): + from erpnext.accounts.doctype.purchase_invoice.test_purchase_invoice import enable_discount_accounting + + enable_discount_accounting() + + discount_account = create_account(account_name="Discount Account", + parent_account="Indirect Expenses - _TC", company="_Test Company") + si = create_sales_invoice(discount_account=discount_account, discount_amount=100) + + discount_amount = frappe.db.get_value('GL Entry', {'account': discount_account, 'voucher_no': si.name}, 'debit') + self.assertEqual(discount_amount, 100) + def get_sales_invoice_for_e_invoice(): si = make_sales_invoice_for_ewaybill() si.naming_series = 'INV-2020-.#####' @@ -2151,6 +2163,7 @@ def create_sales_invoice(**args): si.currency=args.currency or "INR" si.conversion_rate = args.conversion_rate or 1 si.naming_series = args.naming_series or "T-SINV-" + si.cost_center = args.cost_center or "_Test Cost Center - _TC" si.append("items", { "item_code": args.item or args.item_code or "_Test Item", @@ -2164,6 +2177,8 @@ def create_sales_invoice(**args): "rate": args.rate if args.get("rate") is not None else 100, "income_account": args.income_account or "Sales - _TC", "expense_account": args.expense_account or "Cost of Goods Sold - _TC", + "discount_account": args.discount_account or None, + "discount_amount": args.discount_amount or 0, "cost_center": args.cost_center or "_Test Cost Center - _TC", "serial_no": args.serial_no, "conversion_factor": 1 From 4f2bf989667948a736738189ca8ff400902a26b4 Mon Sep 17 00:00:00 2001 From: GangaManoj Date: Thu, 15 Jul 2021 22:01:02 +0530 Subject: [PATCH 19/51] fix: Add Additional Discount Account field --- .../accounts/doctype/sales_invoice/sales_invoice.json | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.json b/erpnext/accounts/doctype/sales_invoice/sales_invoice.json index e7dd6b8a6062..5c09b71cf35c 100644 --- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.json +++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.json @@ -104,6 +104,7 @@ "section_break_49", "apply_discount_on", "base_discount_amount", + "additional_discount_account", "column_break_51", "additional_discount_percentage", "discount_amount", @@ -1966,6 +1967,12 @@ "fieldname": "disable_rounded_total", "fieldtype": "Check", "label": "Disable Rounded Total" + }, + { + "fieldname": "additional_discount_account", + "fieldtype": "Link", + "label": "Additional Discount Account", + "options": "Account" } ], "icon": "fa fa-file-text", @@ -1978,7 +1985,7 @@ "link_fieldname": "consolidated_invoice" } ], - "modified": "2021-05-20 22:48:33.988881", + "modified": "2021-07-15 21:57:17.544279", "modified_by": "Administrator", "module": "Accounts", "name": "Sales Invoice", From 8e96e2d5a24121c74078b214690aeac4e5ff3a7f Mon Sep 17 00:00:00 2001 From: GangaManoj Date: Thu, 15 Jul 2021 22:01:38 +0530 Subject: [PATCH 20/51] fix: Filter options for Additional Discount Account --- .../accounts/doctype/sales_invoice/sales_invoice.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.js b/erpnext/accounts/doctype/sales_invoice/sales_invoice.js index 288e2a7566fe..9beb593b4c08 100644 --- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.js +++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.js @@ -591,6 +591,16 @@ frappe.ui.form.on('Sales Invoice', { }; }); + frm.set_query("additional_discount_account", function() { + return { + filters: { + company: frm.doc.company, + is_group: 0, + root_type: "Profit and Loss", + } + }; + }); + frm.custom_make_buttons = { 'Delivery Note': 'Delivery', 'Sales Invoice': 'Return / Credit Note', From 40412f7e618fd3f36eaa5b33c7805c0acc5c5127 Mon Sep 17 00:00:00 2001 From: GangaManoj Date: Thu, 15 Jul 2021 22:02:43 +0530 Subject: [PATCH 21/51] fix: Only display Additional Discount Account if Enable Discount Accounting is checked --- .../accounts/doctype/accounts_settings/accounts_settings.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/erpnext/accounts/doctype/accounts_settings/accounts_settings.py b/erpnext/accounts/doctype/accounts_settings/accounts_settings.py index d1abdba37927..053f061acc7d 100644 --- a/erpnext/accounts/doctype/accounts_settings/accounts_settings.py +++ b/erpnext/accounts/doctype/accounts_settings/accounts_settings.py @@ -41,4 +41,5 @@ def toggle_discount_accounting_fields(self): for doctype in ["Sales Invoice Item", "Purchase Invoice Item"]: make_property_setter(doctype, "discount_account", "hidden", not(enable_discount_accounting), "Check", validate_fields_for_doctype=False) - make_property_setter("Item", "default_discount_account", "hidden", not(enable_discount_accounting), "Check", validate_fields_for_doctype=False) \ No newline at end of file + make_property_setter("Item", "default_discount_account", "hidden", not(enable_discount_accounting), "Check", validate_fields_for_doctype=False) + make_property_setter("Sales Invoice", "additional_discount_account", "hidden", not(enable_discount_accounting), "Check", validate_fields_for_doctype=False) \ No newline at end of file From 857501cbe1e0ba8e5c6f13d3c7ad29038cdc084e Mon Sep 17 00:00:00 2001 From: GangaManoj Date: Thu, 15 Jul 2021 22:03:46 +0530 Subject: [PATCH 22/51] fix: Make additional GL Entries for discount applied on taxes --- erpnext/controllers/accounts_controller.py | 36 ++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/erpnext/controllers/accounts_controller.py b/erpnext/controllers/accounts_controller.py index dac7f0bd9e35..64ed67c40a68 100644 --- a/erpnext/controllers/accounts_controller.py +++ b/erpnext/controllers/accounts_controller.py @@ -785,6 +785,42 @@ def make_discount_gl_entries(self, gl_entries): "project": item.project or self.project }, account_currency, item=item) ) + + if self.get('discount_amount') and self.get('additional_discount_account'): + self.make_gle_for_additional_discount_applied_on_taxes(gl_entries) + + def make_gle_for_additional_discount_applied_on_taxes(self, gl_entries): + for tax in self.get("taxes"): + if flt(tax.base_tax_amount_after_discount_amount) and flt(tax.base_tax_amount): + account_currency = get_account_currency(tax.account_head) + additional_discount_applied_on_taxes = flt(tax.base_tax_amount) - flt(tax.base_tax_amount_after_discount_amount) + + gl_entries.append( + self.get_gl_dict({ + "account": tax.account_head, + "against": self.customer, + "credit": flt(additional_discount_applied_on_taxes, + tax.precision("tax_amount_after_discount_amount")), + "credit_in_account_currency": (flt(additional_discount_applied_on_taxes, + tax.precision("base_tax_amount_after_discount_amount")) if account_currency==self.company_currency else + flt(additional_discount_applied_on_taxes, tax.precision("tax_amount_after_discount_amount"))), + "cost_center": tax.cost_center + }, account_currency, item=tax) + ) + + gl_entries.append( + self.get_gl_dict({ + "account": self.additional_discount_account, + "against": self.customer, + "debit": flt(additional_discount_applied_on_taxes, + tax.precision("tax_amount_after_discount_amount")), + "debit_in_account_currency": (flt(additional_discount_applied_on_taxes, + tax.precision("base_tax_amount_after_discount_amount")) if account_currency==self.company_currency else + flt(additional_discount_applied_on_taxes, tax.precision("tax_amount_after_discount_amount"))), + "cost_center": tax.cost_center + }, account_currency, item=tax) + ) + def allocate_advance_taxes(self, gl_entries): tax_map = self.get_tax_map() From 6bff653cf0a0c8820967346bfd4ed1566c85c877 Mon Sep 17 00:00:00 2001 From: GangaManoj Date: Fri, 16 Jul 2021 02:18:45 +0530 Subject: [PATCH 23/51] fix: Sider issues --- erpnext/stock/doctype/item/item.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/erpnext/stock/doctype/item/item.js b/erpnext/stock/doctype/item/item.js index 4d8749683c55..77ffa4b71d46 100644 --- a/erpnext/stock/doctype/item/item.js +++ b/erpnext/stock/doctype/item/item.js @@ -283,8 +283,8 @@ $.extend(erpnext.item, { 'company': row.company, "is_group": 0 } - } - } + }; + }; frm.fields_dict["item_defaults"].grid.get_field("buying_cost_center").get_query = function(doc, cdt, cdn) { const row = locals[cdt][cdn]; From b0f21824bc08e1c84aac9cab494d7fdd89df7b86 Mon Sep 17 00:00:00 2001 From: GangaManoj Date: Sat, 17 Jul 2021 17:34:50 +0530 Subject: [PATCH 24/51] fix: Check all expected GL Entries --- .../doctype/purchase_invoice/test_purchase_invoice.py | 11 +++++++++-- .../doctype/sales_invoice/test_sales_invoice.py | 9 +++++++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/erpnext/accounts/doctype/purchase_invoice/test_purchase_invoice.py b/erpnext/accounts/doctype/purchase_invoice/test_purchase_invoice.py index 1dc048ade025..0ac816ec726d 100644 --- a/erpnext/accounts/doctype/purchase_invoice/test_purchase_invoice.py +++ b/erpnext/accounts/doctype/purchase_invoice/test_purchase_invoice.py @@ -252,14 +252,21 @@ def test_purchase_invoice_with_exchange_rate_difference(self): self.assertEqual(discrepancy_caused_by_exchange_rate_diff, amount) def test_purchase_invoice_with_discount_accounting_enabled(self): + from erpnext.accounts.doctype.sales_invoice.test_sales_invoice import check_gl_entries + enable_discount_accounting() discount_account = create_account(account_name="Discount Account", parent_account="Indirect Expenses - _TC", company="_Test Company") pi = make_purchase_invoice(discount_account=discount_account, discount_amount=100) - discount_amount = frappe.db.get_value('GL Entry', {'account': discount_account, 'voucher_no': pi.name}, 'credit') - self.assertEqual(discount_amount, 100) + expected_gle = [ + ["Discount Account - _TC", 0.0, 100.0, nowdate()], + ["_Test Account Cost for Goods Sold - _TC", 350.0, 0.0, nowdate()], + ["Creditors - _TC", 0.0, 250.0, nowdate()] + ] + + check_gl_entries(self, pi.name, expected_gle, nowdate()) def test_purchase_invoice_change_naming_series(self): pi = frappe.copy_doc(test_records[1]) diff --git a/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py b/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py index d90a00941f83..4699732aca09 100644 --- a/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py +++ b/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py @@ -1992,9 +1992,14 @@ def test_sales_invoice_with_discount_accounting_enabled(self): discount_account = create_account(account_name="Discount Account", parent_account="Indirect Expenses - _TC", company="_Test Company") si = create_sales_invoice(discount_account=discount_account, discount_amount=100) + + expected_gle = [ + ["Discount Account - _TC", 100.0, 0.0, nowdate()], + ["Sales - _TC", 0.0, 200.0, nowdate()], + ["Debtors - _TC", 100.0, 0.0, nowdate()] + ] - discount_amount = frappe.db.get_value('GL Entry', {'account': discount_account, 'voucher_no': si.name}, 'debit') - self.assertEqual(discount_amount, 100) + check_gl_entries(self, si.name, expected_gle, nowdate()) def get_sales_invoice_for_e_invoice(): si = make_sales_invoice_for_ewaybill() From 87d448e03943778ad0ddc8e47f0ee9049b39620d Mon Sep 17 00:00:00 2001 From: GangaManoj Date: Sat, 17 Jul 2021 17:40:43 +0530 Subject: [PATCH 25/51] fix: Add Additional Discount Account field --- .../doctype/purchase_invoice/purchase_invoice.json | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.json b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.json index 00ef7d5c184c..96ae828f4645 100644 --- a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.json +++ b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.json @@ -96,6 +96,7 @@ "section_break_44", "apply_discount_on", "base_discount_amount", + "additional_discount_account", "column_break_46", "additional_discount_percentage", "discount_amount", @@ -1377,13 +1378,19 @@ "no_copy": 1, "print_hide": 1, "read_only": 1 + }, + { + "fieldname": "additional_discount_account", + "fieldtype": "Link", + "label": "Additional Discount Account", + "options": "Account" } ], "icon": "fa fa-file-text", "idx": 204, "is_submittable": 1, "links": [], - "modified": "2021-06-15 18:20:56.806195", + "modified": "2021-07-17 17:37:50.570595", "modified_by": "Administrator", "module": "Accounts", "name": "Purchase Invoice", From 7bbc9a886da783f1149db8bed570c16b72c24ab7 Mon Sep 17 00:00:00 2001 From: GangaManoj Date: Sat, 17 Jul 2021 17:41:06 +0530 Subject: [PATCH 26/51] fix: Filter options for Additional Discount Account --- .../doctype/purchase_invoice/purchase_invoice.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.js b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.js index 66fa15bc12ec..435fc1e0c1dd 100644 --- a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.js +++ b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.js @@ -499,6 +499,16 @@ frappe.ui.form.on("Purchase Invoice", { 'Payment Entry': 'Payment' } + frm.set_query("additional_discount_account", function() { + return { + filters: { + company: frm.doc.company, + is_group: 0, + root_type: "Profit and Loss", + } + }; + }); + frm.fields_dict['items'].grid.get_field('deferred_expense_account').get_query = function(doc) { return { filters: { From fa4c03e7a111602fbdf6175f9960bbce0add9a0c Mon Sep 17 00:00:00 2001 From: GangaManoj Date: Sat, 17 Jul 2021 17:42:36 +0530 Subject: [PATCH 27/51] fix: Create ledger entries for discount applied on taxes in make_tax_gl_entries --- .../accounts/doctype/purchase_invoice/purchase_invoice.py | 5 +++++ erpnext/accounts/doctype/sales_invoice/sales_invoice.py | 5 +++++ erpnext/controllers/accounts_controller.py | 3 --- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py index fdaa7f091b45..cdb9bb292491 100644 --- a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py +++ b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py @@ -917,6 +917,11 @@ def make_tax_gl_entries(self, gl_entries): "remarks": self.remarks or "Accounting Entry for Stock" }, item=tax)) + enable_discount_accounting = cint(frappe.db.get_single_value('Accounts Settings', 'enable_discount_accounting')) + + if enable_discount_accounting and self.get('discount_amount') and self.get('additional_discount_account'): + self.make_gle_for_additional_discount_applied_on_taxes(gl_entries) + def make_internal_transfer_gl_entries(self, gl_entries): if self.is_internal_transfer() and flt(self.base_total_taxes_and_charges): account_currency = get_account_currency(self.unrealized_profit_loss_account) diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py index 02d847031fcf..5ebc5e6339b4 100644 --- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py +++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py @@ -901,6 +901,11 @@ def make_tax_gl_entries(self, gl_entries): }, account_currency, item=tax) ) + enable_discount_accounting = cint(frappe.db.get_single_value('Accounts Settings', 'enable_discount_accounting')) + + if enable_discount_accounting and self.get('discount_amount') and self.get('additional_discount_account'): + self.make_gle_for_additional_discount_applied_on_taxes(gl_entries) + def make_internal_transfer_gl_entries(self, gl_entries): if self.is_internal_transfer() and flt(self.base_total_taxes_and_charges): account_currency = get_account_currency(self.unrealized_profit_loss_account) diff --git a/erpnext/controllers/accounts_controller.py b/erpnext/controllers/accounts_controller.py index 64ed67c40a68..700a0e6645bc 100644 --- a/erpnext/controllers/accounts_controller.py +++ b/erpnext/controllers/accounts_controller.py @@ -785,9 +785,6 @@ def make_discount_gl_entries(self, gl_entries): "project": item.project or self.project }, account_currency, item=item) ) - - if self.get('discount_amount') and self.get('additional_discount_account'): - self.make_gle_for_additional_discount_applied_on_taxes(gl_entries) def make_gle_for_additional_discount_applied_on_taxes(self, gl_entries): for tax in self.get("taxes"): From c2ba6897b09d2936fa2768cfb4cb720fafc2a99f Mon Sep 17 00:00:00 2001 From: GangaManoj Date: Sat, 17 Jul 2021 17:45:35 +0530 Subject: [PATCH 28/51] fix: Only display Additional Discount Account if Enable Discount Accounting is checked --- .../accounts/doctype/accounts_settings/accounts_settings.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/erpnext/accounts/doctype/accounts_settings/accounts_settings.py b/erpnext/accounts/doctype/accounts_settings/accounts_settings.py index 053f061acc7d..24b0ec4d4a88 100644 --- a/erpnext/accounts/doctype/accounts_settings/accounts_settings.py +++ b/erpnext/accounts/doctype/accounts_settings/accounts_settings.py @@ -41,5 +41,7 @@ def toggle_discount_accounting_fields(self): for doctype in ["Sales Invoice Item", "Purchase Invoice Item"]: make_property_setter(doctype, "discount_account", "hidden", not(enable_discount_accounting), "Check", validate_fields_for_doctype=False) - make_property_setter("Item", "default_discount_account", "hidden", not(enable_discount_accounting), "Check", validate_fields_for_doctype=False) - make_property_setter("Sales Invoice", "additional_discount_account", "hidden", not(enable_discount_accounting), "Check", validate_fields_for_doctype=False) \ No newline at end of file + for doctype in ["Sales Invoice", "Purchase Invoice"]: + make_property_setter(doctype, "additional_discount_account", "hidden", not(enable_discount_accounting), "Check", validate_fields_for_doctype=False) + + make_property_setter("Item", "default_discount_account", "hidden", not(enable_discount_accounting), "Check", validate_fields_for_doctype=False) \ No newline at end of file From c4d2dc0da336ff1811b0daca07e1386ebeea382f Mon Sep 17 00:00:00 2001 From: GangaManoj Date: Sat, 17 Jul 2021 17:47:20 +0530 Subject: [PATCH 29/51] fix: Remove unnecessary condition --- .../accounts/doctype/sales_invoice/sales_invoice.js | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.js b/erpnext/accounts/doctype/sales_invoice/sales_invoice.js index 9beb593b4c08..3fb20d9d5102 100644 --- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.js +++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.js @@ -629,13 +629,11 @@ frappe.ui.form.on('Sales Invoice', { // discount account frm.fields_dict['items'].grid.get_field('discount_account').get_query = function(doc) { - if (erpnext.is_perpetual_inventory_enabled(doc.company)) { - return { - filters: { - 'report_type': 'Profit and Loss', - 'company': doc.company, - "is_group": 0 - } + return { + filters: { + 'report_type': 'Profit and Loss', + 'company': doc.company, + "is_group": 0 } } } From 99551e9b7f25bb7092112a40b5282e087ecd3ecd Mon Sep 17 00:00:00 2001 From: GangaManoj Date: Sat, 17 Jul 2021 18:45:21 +0530 Subject: [PATCH 30/51] fix: Add test for additional discount applied on taxes --- .../sales_invoice/test_sales_invoice.py | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py b/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py index 4699732aca09..8615963e4adb 100644 --- a/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py +++ b/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py @@ -2001,6 +2001,35 @@ def test_sales_invoice_with_discount_accounting_enabled(self): check_gl_entries(self, si.name, expected_gle, nowdate()) + def test_additional_discount_for_sales_invoice_with_discount_accounting_enabled(self): + from erpnext.accounts.doctype.purchase_invoice.test_purchase_invoice import enable_discount_accounting + + enable_discount_accounting() + additional_discount_account = create_account(account_name="Discount Account", + parent_account="Indirect Expenses - _TC", company="_Test Company") + + si = create_sales_invoice(rate=75000, do_not_save=1) + si.apply_discount_on = "Grand Total" + si.additional_discount_account = additional_discount_account + si.additional_discount_percentage = 10 + si.append("taxes", { + "charge_type": "On Net Total", + "account_head": "CGST - _TC", + "cost_center": "Main - _TC", + "description": "CGST @ 9.0", + "rate": 9 + }) + si.submit() + + expected_gle = [ + ["Sales - _TC", 0.0, 67500.0, nowdate()], + ["Discount Account - _TC", 675.0, 0.0, nowdate()], + ["CGST - _TC", 0.0, 6750.0, nowdate()], + ["Debtors - _TC", 73575.0, 0.0, nowdate()] + ] + + check_gl_entries(self, si.name, expected_gle, nowdate()) + def get_sales_invoice_for_e_invoice(): si = make_sales_invoice_for_ewaybill() si.naming_series = 'INV-2020-.#####' From 228499369cc05f9aefed76f41db6b9faad7457b7 Mon Sep 17 00:00:00 2001 From: GangaManoj Date: Sat, 17 Jul 2021 19:49:16 +0530 Subject: [PATCH 31/51] fix: Switch debit and credit for ledger entries for discount applied on taxes for Purchase Invoice --- erpnext/controllers/accounts_controller.py | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/erpnext/controllers/accounts_controller.py b/erpnext/controllers/accounts_controller.py index 700a0e6645bc..f32f5bdc9540 100644 --- a/erpnext/controllers/accounts_controller.py +++ b/erpnext/controllers/accounts_controller.py @@ -791,14 +791,22 @@ def make_gle_for_additional_discount_applied_on_taxes(self, gl_entries): if flt(tax.base_tax_amount_after_discount_amount) and flt(tax.base_tax_amount): account_currency = get_account_currency(tax.account_head) additional_discount_applied_on_taxes = flt(tax.base_tax_amount) - flt(tax.base_tax_amount_after_discount_amount) + if self.doctype == 'Purchase Invoice': + against = self.supplier + dr_or_cr = "debit" + rev_dr_cr = "credit" + else: + against = self.customer + dr_or_cr = "credit" + rev_dr_cr = "debit" gl_entries.append( self.get_gl_dict({ "account": tax.account_head, - "against": self.customer, - "credit": flt(additional_discount_applied_on_taxes, + "against": against, + dr_or_cr: flt(additional_discount_applied_on_taxes, tax.precision("tax_amount_after_discount_amount")), - "credit_in_account_currency": (flt(additional_discount_applied_on_taxes, + dr_or_cr + "_in_account_currency": (flt(additional_discount_applied_on_taxes, tax.precision("base_tax_amount_after_discount_amount")) if account_currency==self.company_currency else flt(additional_discount_applied_on_taxes, tax.precision("tax_amount_after_discount_amount"))), "cost_center": tax.cost_center @@ -808,17 +816,16 @@ def make_gle_for_additional_discount_applied_on_taxes(self, gl_entries): gl_entries.append( self.get_gl_dict({ "account": self.additional_discount_account, - "against": self.customer, - "debit": flt(additional_discount_applied_on_taxes, + "against": against, + rev_dr_cr: flt(additional_discount_applied_on_taxes, tax.precision("tax_amount_after_discount_amount")), - "debit_in_account_currency": (flt(additional_discount_applied_on_taxes, + rev_dr_cr + "_in_account_currency": (flt(additional_discount_applied_on_taxes, tax.precision("base_tax_amount_after_discount_amount")) if account_currency==self.company_currency else flt(additional_discount_applied_on_taxes, tax.precision("tax_amount_after_discount_amount"))), "cost_center": tax.cost_center }, account_currency, item=tax) ) - def allocate_advance_taxes(self, gl_entries): tax_map = self.get_tax_map() for pe in self.get("advances"): From d6956ff075219293a761ff4c039f7bf476bb6a45 Mon Sep 17 00:00:00 2001 From: GangaManoj Date: Sat, 17 Jul 2021 19:49:42 +0530 Subject: [PATCH 32/51] fix: Add test for additional discount applied on taxes --- .../purchase_invoice/test_purchase_invoice.py | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/erpnext/accounts/doctype/purchase_invoice/test_purchase_invoice.py b/erpnext/accounts/doctype/purchase_invoice/test_purchase_invoice.py index 0ac816ec726d..3a09c96d8250 100644 --- a/erpnext/accounts/doctype/purchase_invoice/test_purchase_invoice.py +++ b/erpnext/accounts/doctype/purchase_invoice/test_purchase_invoice.py @@ -268,6 +268,35 @@ def test_purchase_invoice_with_discount_accounting_enabled(self): check_gl_entries(self, pi.name, expected_gle, nowdate()) + def test_additional_discount_for_purchase_invoice_with_discount_accounting_enabled(self): + from erpnext.accounts.doctype.sales_invoice.test_sales_invoice import check_gl_entries + + enable_discount_accounting() + additional_discount_account = create_account(account_name="Discount Account", + parent_account="Indirect Expenses - _TC", company="_Test Company") + + pi = make_purchase_invoice(qty=1, rate=75000, do_not_save=1) + pi.apply_discount_on = "Grand Total" + pi.additional_discount_account = additional_discount_account + pi.additional_discount_percentage = 10 + pi.append("taxes", { + "charge_type": "On Net Total", + "account_head": "CGST - _TC", + "cost_center": "Main - _TC", + "description": "CGST @ 9.0", + "rate": 9 + }) + pi.submit() + + expected_gle = [ + ["Discount Account - _TC", 0.0, 675.0, nowdate()], + ["CGST - _TC", 6750.0, 0.0, nowdate()], + ["_Test Account Cost for Goods Sold - _TC", 67500.0, 0.0, nowdate()], + ["Creditors - _TC", 0.0, 73575.0, nowdate()] + ] + + check_gl_entries(self, pi.name, expected_gle, nowdate()) + def test_purchase_invoice_change_naming_series(self): pi = frappe.copy_doc(test_records[1]) pi.insert() From 980798c6fd676c240632956fee182d21869c04b1 Mon Sep 17 00:00:00 2001 From: Ganga Manoj Date: Mon, 19 Jul 2021 23:43:36 +0530 Subject: [PATCH 33/51] fix: Use the item's cost centre instead of the Invoice's Co-authored-by: Deepesh Garg <42651287+deepeshgarg007@users.noreply.github.com> --- erpnext/controllers/accounts_controller.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/erpnext/controllers/accounts_controller.py b/erpnext/controllers/accounts_controller.py index aa2fe29bc697..65dbe17ec1dd 100644 --- a/erpnext/controllers/accounts_controller.py +++ b/erpnext/controllers/accounts_controller.py @@ -836,7 +836,7 @@ def make_discount_gl_entries(self, gl_entries): "against": supplier_or_customer, dr_or_cr: flt(item.discount_amount), dr_or_cr + "_in_account_currency": flt(item.discount_amount), - "cost_center": self.cost_center, + "cost_center": item.cost_center, "project": self.project }, account_currency, item=self) ) From 63b7ecd0fed3c8bbfd5766c18d28ab621781fac3 Mon Sep 17 00:00:00 2001 From: Ganga Manoj Date: Mon, 19 Jul 2021 23:44:21 +0530 Subject: [PATCH 34/51] fix: Use the item's project instead of the invoice's Co-authored-by: Deepesh Garg <42651287+deepeshgarg007@users.noreply.github.com> --- erpnext/controllers/accounts_controller.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/erpnext/controllers/accounts_controller.py b/erpnext/controllers/accounts_controller.py index 65dbe17ec1dd..2aac4968a28f 100644 --- a/erpnext/controllers/accounts_controller.py +++ b/erpnext/controllers/accounts_controller.py @@ -837,7 +837,7 @@ def make_discount_gl_entries(self, gl_entries): dr_or_cr: flt(item.discount_amount), dr_or_cr + "_in_account_currency": flt(item.discount_amount), "cost_center": item.cost_center, - "project": self.project + "project": item.project }, account_currency, item=self) ) From 0ea2934cd51e2d0c8af9cc22a8db16d60dd0dd3f Mon Sep 17 00:00:00 2001 From: Ganga Manoj Date: Mon, 19 Jul 2021 23:44:55 +0530 Subject: [PATCH 35/51] fix: GL Entry creation Co-authored-by: Deepesh Garg <42651287+deepeshgarg007@users.noreply.github.com> --- erpnext/controllers/accounts_controller.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/erpnext/controllers/accounts_controller.py b/erpnext/controllers/accounts_controller.py index 2aac4968a28f..9b3336cde50c 100644 --- a/erpnext/controllers/accounts_controller.py +++ b/erpnext/controllers/accounts_controller.py @@ -838,7 +838,7 @@ def make_discount_gl_entries(self, gl_entries): dr_or_cr + "_in_account_currency": flt(item.discount_amount), "cost_center": item.cost_center, "project": item.project - }, account_currency, item=self) + }, account_currency, item=item) ) account_currency = get_account_currency(income_or_expense_account) From 8fc9c13734e033ddb5327b29a3fe175ed5ded823 Mon Sep 17 00:00:00 2001 From: Ganga Manoj Date: Mon, 19 Jul 2021 23:46:38 +0530 Subject: [PATCH 36/51] fix: Filter for additional_discount_account Co-authored-by: Deepesh Garg <42651287+deepeshgarg007@users.noreply.github.com> --- erpnext/accounts/doctype/sales_invoice/sales_invoice.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.js b/erpnext/accounts/doctype/sales_invoice/sales_invoice.js index 3fb20d9d5102..2751b5509cc6 100644 --- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.js +++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.js @@ -596,7 +596,7 @@ frappe.ui.form.on('Sales Invoice', { filters: { company: frm.doc.company, is_group: 0, - root_type: "Profit and Loss", + report_type: "Profit and Loss", } }; }); From 1d830dfd92ca49a00b46915b13558489883d5979 Mon Sep 17 00:00:00 2001 From: Ganga Manoj Date: Mon, 19 Jul 2021 23:47:58 +0530 Subject: [PATCH 37/51] fix: Filter for additional_discount_account Co-authored-by: Deepesh Garg <42651287+deepeshgarg007@users.noreply.github.com> --- erpnext/accounts/doctype/purchase_invoice/purchase_invoice.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.js b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.js index 435fc1e0c1dd..d6bb69bcbf01 100644 --- a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.js +++ b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.js @@ -504,7 +504,7 @@ frappe.ui.form.on("Purchase Invoice", { filters: { company: frm.doc.company, is_group: 0, - root_type: "Profit and Loss", + report_type: "Profit and Loss", } }; }); From 4105e2713828dc6bd204b516dfed0ba9d322871a Mon Sep 17 00:00:00 2001 From: GangaManoj Date: Tue, 20 Jul 2021 03:46:02 +0530 Subject: [PATCH 38/51] fix: Create GL Entries for Additional Discount Account --- .../purchase_invoice/purchase_invoice.py | 11 ++-- .../doctype/sales_invoice/sales_invoice.py | 25 +++++--- erpnext/controllers/accounts_controller.py | 60 ++++++------------- 3 files changed, 39 insertions(+), 57 deletions(-) diff --git a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py index 95ad653c3766..53ad849b1e14 100644 --- a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py +++ b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py @@ -611,7 +611,11 @@ def make_item_gl_entries(self, gl_entries): if (not item.enable_deferred_expense or self.is_return) else item.deferred_expense_account) if not item.is_fixed_asset: - amount = flt(item.base_net_amount, item.precision("base_net_amount")) + if frappe.db.get_single_value('Accounts Settings', 'enable_discount_accounting'): + amount = flt(item.base_amount, item.precision("base_amount")) + else: + amount = flt(item.base_net_amount, item.precision("base_net_amount")) + else: amount = flt(item.base_net_amount + item.item_tax_amount, item.precision("base_net_amount")) @@ -918,11 +922,6 @@ def make_tax_gl_entries(self, gl_entries): "remarks": self.remarks or "Accounting Entry for Stock" }, item=tax)) - enable_discount_accounting = cint(frappe.db.get_single_value('Accounts Settings', 'enable_discount_accounting')) - - if enable_discount_accounting and self.get('discount_amount') and self.get('additional_discount_account'): - self.make_gle_for_additional_discount_applied_on_taxes(gl_entries) - def make_internal_transfer_gl_entries(self, gl_entries): if self.is_internal_transfer() and flt(self.base_total_taxes_and_charges): account_currency = get_account_currency(self.unrealized_profit_loss_account) diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py index 955f223eccbc..7862f82f6f66 100644 --- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py +++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py @@ -902,11 +902,6 @@ def make_tax_gl_entries(self, gl_entries): }, account_currency, item=tax) ) - enable_discount_accounting = cint(frappe.db.get_single_value('Accounts Settings', 'enable_discount_accounting')) - - if enable_discount_accounting and self.get('discount_amount') and self.get('additional_discount_account'): - self.make_gle_for_additional_discount_applied_on_taxes(gl_entries) - def make_internal_transfer_gl_entries(self, gl_entries): if self.is_internal_transfer() and flt(self.base_total_taxes_and_charges): account_currency = get_account_currency(self.unrealized_profit_loss_account) @@ -957,15 +952,17 @@ def make_item_gl_entries(self, gl_entries): income_account = (item.income_account if (not item.enable_deferred_revenue or self.is_return) else item.deferred_revenue_account) + amount, base_amount = self.get_amount_and_base_amount(item) + account_currency = get_account_currency(income_account) gl_entries.append( self.get_gl_dict({ "account": income_account, "against": self.customer, - "credit": flt(item.base_net_amount, item.precision("base_net_amount")), - "credit_in_account_currency": (flt(item.base_net_amount, item.precision("base_net_amount")) + "credit": flt(base_amount, item.precision("base_net_amount")), + "credit_in_account_currency": (flt(base_amount, item.precision("base_net_amount")) if account_currency==self.company_currency - else flt(item.net_amount, item.precision("net_amount"))), + else flt(amount, item.precision("net_amount"))), "cost_center": item.cost_center, "project": item.project or self.project }, account_currency, item=item) @@ -976,6 +973,18 @@ def make_item_gl_entries(self, gl_entries): erpnext.is_perpetual_inventory_enabled(self.company): gl_entries += super(SalesInvoice, self).get_gl_entries() + def get_amount_and_base_amount(self, item): + amount = item.net_amount + base_amount = item.base_net_amount + + enable_discount_accounting = cint(frappe.db.get_single_value('Accounts Settings', 'enable_discount_accounting')) + + if enable_discount_accounting and self.get('discount_amount') and self.get('additional_discount_account'): + amount = item.amount + base_amount = item.base_amount + + return amount, base_amount + def set_asset_status(self, asset): if self.is_return: asset.set_status() diff --git a/erpnext/controllers/accounts_controller.py b/erpnext/controllers/accounts_controller.py index 9b3336cde50c..59879e0df58c 100644 --- a/erpnext/controllers/accounts_controller.py +++ b/erpnext/controllers/accounts_controller.py @@ -812,19 +812,23 @@ def make_discount_gl_entries(self, gl_entries): enable_discount_accounting = cint(frappe.db.get_single_value('Accounts Settings', 'enable_discount_accounting')) if enable_discount_accounting: + if self.doctype == "Purchase Invoice": + dr_or_cr = "credit" + rev_dr_cr = "debit" + supplier_or_customer = self.supplier + + else: + dr_or_cr = "debit" + rev_dr_cr = "credit" + supplier_or_customer = self.customer + for item in self.get("items"): if item.get('discount_amount') and item.get('discount_account'): if self.doctype == "Purchase Invoice": - dr_or_cr = "credit" - rev_dr_cr = "debit" - supplier_or_customer = self.supplier income_or_expense_account = (item.expense_account if (not item.enable_deferred_expense or self.is_return) else item.deferred_expense_account) else: - dr_or_cr = "debit" - rev_dr_cr = "credit" - supplier_or_customer = self.customer income_or_expense_account = (item.income_account if (not item.enable_deferred_revenue or self.is_return) else item.deferred_revenue_account) @@ -853,46 +857,16 @@ def make_discount_gl_entries(self, gl_entries): }, account_currency, item=item) ) - def make_gle_for_additional_discount_applied_on_taxes(self, gl_entries): - for tax in self.get("taxes"): - if flt(tax.base_tax_amount_after_discount_amount) and flt(tax.base_tax_amount): - account_currency = get_account_currency(tax.account_head) - additional_discount_applied_on_taxes = flt(tax.base_tax_amount) - flt(tax.base_tax_amount_after_discount_amount) - if self.doctype == 'Purchase Invoice': - against = self.supplier - dr_or_cr = "debit" - rev_dr_cr = "credit" - else: - against = self.customer - dr_or_cr = "credit" - rev_dr_cr = "debit" - - gl_entries.append( - self.get_gl_dict({ - "account": tax.account_head, - "against": against, - dr_or_cr: flt(additional_discount_applied_on_taxes, - tax.precision("tax_amount_after_discount_amount")), - dr_or_cr + "_in_account_currency": (flt(additional_discount_applied_on_taxes, - tax.precision("base_tax_amount_after_discount_amount")) if account_currency==self.company_currency else - flt(additional_discount_applied_on_taxes, tax.precision("tax_amount_after_discount_amount"))), - "cost_center": tax.cost_center - }, account_currency, item=tax) - ) - + if self.get('discount_amount') and self.get('additional_discount_account'): gl_entries.append( self.get_gl_dict({ "account": self.additional_discount_account, - "against": against, - rev_dr_cr: flt(additional_discount_applied_on_taxes, - tax.precision("tax_amount_after_discount_amount")), - rev_dr_cr + "_in_account_currency": (flt(additional_discount_applied_on_taxes, - tax.precision("base_tax_amount_after_discount_amount")) if account_currency==self.company_currency else - flt(additional_discount_applied_on_taxes, tax.precision("tax_amount_after_discount_amount"))), - "cost_center": tax.cost_center - }, account_currency, item=tax) - ) - + "against": supplier_or_customer, + dr_or_cr: self.discount_amount, + "cost_center": self.cost_center + }, item=self) + ) + def allocate_advance_taxes(self, gl_entries): tax_map = self.get_tax_map() for pe in self.get("advances"): From 59ee6958aa7e67cbe00491093729ff260874efce Mon Sep 17 00:00:00 2001 From: GangaManoj Date: Tue, 20 Jul 2021 03:52:39 +0530 Subject: [PATCH 39/51] fix: Make discount_account mandatory if discount accounting is enabled --- erpnext/accounts/doctype/accounts_settings/accounts_settings.py | 1 + 1 file changed, 1 insertion(+) diff --git a/erpnext/accounts/doctype/accounts_settings/accounts_settings.py b/erpnext/accounts/doctype/accounts_settings/accounts_settings.py index 24b0ec4d4a88..a3a32d5e9750 100644 --- a/erpnext/accounts/doctype/accounts_settings/accounts_settings.py +++ b/erpnext/accounts/doctype/accounts_settings/accounts_settings.py @@ -40,6 +40,7 @@ def toggle_discount_accounting_fields(self): for doctype in ["Sales Invoice Item", "Purchase Invoice Item"]: make_property_setter(doctype, "discount_account", "hidden", not(enable_discount_accounting), "Check", validate_fields_for_doctype=False) + make_property_setter(doctype, "discount_account", "mandatory", enable_discount_accounting, "Check", validate_fields_for_doctype=False) for doctype in ["Sales Invoice", "Purchase Invoice"]: make_property_setter(doctype, "additional_discount_account", "hidden", not(enable_discount_accounting), "Check", validate_fields_for_doctype=False) From 82d147ea625f7e24959a63b47b11138f95fde536 Mon Sep 17 00:00:00 2001 From: GangaManoj Date: Tue, 20 Jul 2021 05:16:33 +0530 Subject: [PATCH 40/51] fix: Tests --- .../purchase_invoice/test_purchase_invoice.py | 43 +++++++++++++------ .../sales_invoice/test_sales_invoice.py | 25 +++++------ 2 files changed, 43 insertions(+), 25 deletions(-) diff --git a/erpnext/accounts/doctype/purchase_invoice/test_purchase_invoice.py b/erpnext/accounts/doctype/purchase_invoice/test_purchase_invoice.py index 87bdb7cd4f04..7c26007b0726 100644 --- a/erpnext/accounts/doctype/purchase_invoice/test_purchase_invoice.py +++ b/erpnext/accounts/doctype/purchase_invoice/test_purchase_invoice.py @@ -252,8 +252,6 @@ def test_purchase_invoice_with_exchange_rate_difference(self): self.assertEqual(discrepancy_caused_by_exchange_rate_diff, amount) def test_purchase_invoice_with_discount_accounting_enabled(self): - from erpnext.accounts.doctype.sales_invoice.test_sales_invoice import check_gl_entries - enable_discount_accounting() discount_account = create_account(account_name="Discount Account", @@ -261,38 +259,45 @@ def test_purchase_invoice_with_discount_accounting_enabled(self): pi = make_purchase_invoice(discount_account=discount_account, discount_amount=100) expected_gle = [ - ["Discount Account - _TC", 0.0, 100.0, nowdate()], ["_Test Account Cost for Goods Sold - _TC", 350.0, 0.0, nowdate()], - ["Creditors - _TC", 0.0, 250.0, nowdate()] + ["Creditors - _TC", 0.0, 250.0, nowdate()], + ["Discount Account - _TC", 0.0, 100.0, nowdate()] ] check_gl_entries(self, pi.name, expected_gle, nowdate()) def test_additional_discount_for_purchase_invoice_with_discount_accounting_enabled(self): - from erpnext.accounts.doctype.sales_invoice.test_sales_invoice import check_gl_entries - enable_discount_accounting() additional_discount_account = create_account(account_name="Discount Account", parent_account="Indirect Expenses - _TC", company="_Test Company") - pi = make_purchase_invoice(qty=1, rate=75000, do_not_save=1) + pi = make_purchase_invoice(qty=1, rate=100, do_not_save=1) pi.apply_discount_on = "Grand Total" pi.additional_discount_account = additional_discount_account - pi.additional_discount_percentage = 10 + pi.additional_discount_percentage = 20 pi.append("taxes", { "charge_type": "On Net Total", "account_head": "CGST - _TC", "cost_center": "Main - _TC", "description": "CGST @ 9.0", - "rate": 9 + "base_tax_amount": 20, + "base_tax_amount_after_discount_amount": 20 }) pi.submit() + # gle = frappe.get_all( + # "GL Entry", + # fields = ['account', 'debit', 'credit', 'posting_date'], + # filters = {'voucher_no': pi.name} + # ) + # for gl in gle: + # print(gl, "\n") + expected_gle = [ - ["Discount Account - _TC", 0.0, 675.0, nowdate()], - ["CGST - _TC", 6750.0, 0.0, nowdate()], - ["_Test Account Cost for Goods Sold - _TC", 67500.0, 0.0, nowdate()], - ["Creditors - _TC", 0.0, 73575.0, nowdate()] + ["CGST - _TC", 20.0, 0.0, nowdate()], + ["Creditors - _TC", 0.0, 96.0, nowdate()], + ["Discount Account - _TC", 0.0, 24.0, nowdate()], + ["_Test Account Cost for Goods Sold - _TC", 100.0, 0.0, nowdate()] ] check_gl_entries(self, pi.name, expected_gle, nowdate()) @@ -1207,6 +1212,18 @@ def test_purchase_invoice_advance_taxes(self): self.assertEqual(expected_gle[i][0], gle.account) self.assertEqual(expected_gle[i][1], gle.amount) +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` + where voucher_type='Purchase Invoice' and voucher_no=%s and posting_date >= %s + order by posting_date asc, account asc""", (voucher_no, posting_date), as_dict=1) + + for i, gle in enumerate(gl_entries): + doc.assertEqual(expected_gle[i][0], gle.account) + doc.assertEqual(expected_gle[i][1], gle.debit) + doc.assertEqual(expected_gle[i][2], gle.credit) + doc.assertEqual(getdate(expected_gle[i][3]), gle.posting_date) + def update_tax_witholding_category(company, account, date): from erpnext.accounts.utils import get_fiscal_year diff --git a/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py b/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py index 00831febac9e..eccc69fdb273 100644 --- a/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py +++ b/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py @@ -2025,12 +2025,12 @@ def test_sales_invoice_with_discount_accounting_enabled(self): si = create_sales_invoice(discount_account=discount_account, discount_amount=100) expected_gle = [ + ["Debtors - _TC", 100.0, 0.0, nowdate()], ["Discount Account - _TC", 100.0, 0.0, nowdate()], - ["Sales - _TC", 0.0, 200.0, nowdate()], - ["Debtors - _TC", 100.0, 0.0, nowdate()] + ["Sales - _TC", 0.0, 200.0, nowdate()] ] - check_gl_entries(self, si.name, expected_gle, nowdate()) + check_gl_entries(self, si.name, expected_gle, add_days(nowdate(), -1)) def test_additional_discount_for_sales_invoice_with_discount_accounting_enabled(self): from erpnext.accounts.doctype.purchase_invoice.test_purchase_invoice import enable_discount_accounting @@ -2039,27 +2039,28 @@ def test_additional_discount_for_sales_invoice_with_discount_accounting_enabled( additional_discount_account = create_account(account_name="Discount Account", parent_account="Indirect Expenses - _TC", company="_Test Company") - si = create_sales_invoice(rate=75000, do_not_save=1) + si = create_sales_invoice(rate=100, do_not_save=1) si.apply_discount_on = "Grand Total" si.additional_discount_account = additional_discount_account - si.additional_discount_percentage = 10 + si.additional_discount_percentage = 20 si.append("taxes", { - "charge_type": "On Net Total", + "charge_type": "Actual", "account_head": "CGST - _TC", "cost_center": "Main - _TC", "description": "CGST @ 9.0", - "rate": 9 + "rate": 0, + "tax_amount": 20 }) si.submit() expected_gle = [ - ["Sales - _TC", 0.0, 67500.0, nowdate()], - ["Discount Account - _TC", 675.0, 0.0, nowdate()], - ["CGST - _TC", 0.0, 6750.0, nowdate()], - ["Debtors - _TC", 73575.0, 0.0, nowdate()] + ["CGST - _TC", 0.0, 20.0, nowdate()], + ["Debtors - _TC", 96.0, 0.0, nowdate()], + ["Discount Account - _TC", 24.0, 0.0, nowdate()], + ["Sales - _TC", 0.0, 100.0, nowdate()] ] - check_gl_entries(self, si.name, expected_gle, nowdate()) + check_gl_entries(self, si.name, expected_gle, add_days(nowdate(), -1)) def get_sales_invoice_for_e_invoice(): si = make_sales_invoice_for_ewaybill() From 46bed5e6cca5eade886d85a9300fc5ebf00de18f Mon Sep 17 00:00:00 2001 From: GangaManoj Date: Tue, 20 Jul 2021 22:03:44 +0530 Subject: [PATCH 41/51] fix: Add mandatory_depends_on property for Discount Account --- .../doctype/accounts_settings/accounts_settings.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/erpnext/accounts/doctype/accounts_settings/accounts_settings.py b/erpnext/accounts/doctype/accounts_settings/accounts_settings.py index a3a32d5e9750..55449132928e 100644 --- a/erpnext/accounts/doctype/accounts_settings/accounts_settings.py +++ b/erpnext/accounts/doctype/accounts_settings/accounts_settings.py @@ -40,9 +40,16 @@ def toggle_discount_accounting_fields(self): for doctype in ["Sales Invoice Item", "Purchase Invoice Item"]: make_property_setter(doctype, "discount_account", "hidden", not(enable_discount_accounting), "Check", validate_fields_for_doctype=False) - make_property_setter(doctype, "discount_account", "mandatory", enable_discount_accounting, "Check", validate_fields_for_doctype=False) + if enable_discount_accounting: + make_property_setter(doctype, "discount_account", "mandatory_depends_on", "eval: doc.discount_amount", "Code", validate_fields_for_doctype=False) + else: + make_property_setter(doctype, "discount_account", "mandatory_depends_on", "", "Code", validate_fields_for_doctype=False) for doctype in ["Sales Invoice", "Purchase Invoice"]: make_property_setter(doctype, "additional_discount_account", "hidden", not(enable_discount_accounting), "Check", validate_fields_for_doctype=False) + if enable_discount_accounting: + make_property_setter(doctype, "additional_discount_account", "mandatory_depends_on", "eval: doc.discount_amount", "Code", validate_fields_for_doctype=False) + else: + make_property_setter(doctype, "additional_discount_account", "mandatory_depends_on", "", "Code", validate_fields_for_doctype=False) make_property_setter("Item", "default_discount_account", "hidden", not(enable_discount_accounting), "Check", validate_fields_for_doctype=False) \ No newline at end of file From b7267f8f5f82dccc237615901e043baaaf58589a Mon Sep 17 00:00:00 2001 From: Deepesh Garg Date: Wed, 21 Jul 2021 15:25:09 +0530 Subject: [PATCH 42/51] fix: Syntax Error --- erpnext/accounts/doctype/sales_invoice/sales_invoice.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.js b/erpnext/accounts/doctype/sales_invoice/sales_invoice.js index 2751b5509cc6..dbc42de583ec 100644 --- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.js +++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.js @@ -348,7 +348,7 @@ erpnext.accounts.SalesInvoiceController = class SalesInvoiceController extends e items_add(doc, cdt, cdn) { var row = frappe.get_doc(cdt, cdn); this.frm.script_manager.copy_from_first_row("items", row, ["income_account", "discount_account", "cost_center"]); - } + }, set_dynamic_labels() { super.set_dynamic_labels(); From 92f7a5a390aa6760e6fa27a867ff289628d504a9 Mon Sep 17 00:00:00 2001 From: Deepesh Garg Date: Wed, 21 Jul 2021 15:25:40 +0530 Subject: [PATCH 43/51] fix: GL For taxes if discount applied on Grand Total --- .../doctype/sales_invoice/sales_invoice.py | 26 +++++++------------ erpnext/controllers/accounts_controller.py | 21 +++++++++++++++ 2 files changed, 31 insertions(+), 16 deletions(-) diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py index 7862f82f6f66..23d3064069c6 100644 --- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py +++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py @@ -886,18 +886,22 @@ def make_customer_gl_entry(self, gl_entries): ) def make_tax_gl_entries(self, gl_entries): + enable_discount_accounting = cint(frappe.db.get_single_value('Accounts Settings', 'enable_discount_accounting')) + for tax in self.get("taxes"): + amount, base_amount = self.get_tax_amounts(tax, enable_discount_accounting) + if flt(tax.base_tax_amount_after_discount_amount): account_currency = get_account_currency(tax.account_head) gl_entries.append( self.get_gl_dict({ "account": tax.account_head, "against": self.customer, - "credit": flt(tax.base_tax_amount_after_discount_amount, + "credit": flt(base_amount, tax.precision("tax_amount_after_discount_amount")), - "credit_in_account_currency": (flt(tax.base_tax_amount_after_discount_amount, + "credit_in_account_currency": (flt(base_amount, tax.precision("base_tax_amount_after_discount_amount")) if account_currency==self.company_currency else - flt(tax.tax_amount_after_discount_amount, tax.precision("tax_amount_after_discount_amount"))), + flt(amount, tax.precision("tax_amount_after_discount_amount"))), "cost_center": tax.cost_center }, account_currency, item=tax) ) @@ -916,6 +920,8 @@ def make_internal_transfer_gl_entries(self, gl_entries): def make_item_gl_entries(self, gl_entries): # income account gl entries + enable_discount_accounting = cint(frappe.db.get_single_value('Accounts Settings', 'enable_discount_accounting')) + for item in self.get("items"): if flt(item.base_net_amount, item.precision("base_net_amount")): if item.is_fixed_asset: @@ -952,7 +958,7 @@ def make_item_gl_entries(self, gl_entries): income_account = (item.income_account if (not item.enable_deferred_revenue or self.is_return) else item.deferred_revenue_account) - amount, base_amount = self.get_amount_and_base_amount(item) + amount, base_amount = self.get_amount_and_base_amount(item, enable_discount_accounting) account_currency = get_account_currency(income_account) gl_entries.append( @@ -973,18 +979,6 @@ def make_item_gl_entries(self, gl_entries): erpnext.is_perpetual_inventory_enabled(self.company): gl_entries += super(SalesInvoice, self).get_gl_entries() - def get_amount_and_base_amount(self, item): - amount = item.net_amount - base_amount = item.base_net_amount - - enable_discount_accounting = cint(frappe.db.get_single_value('Accounts Settings', 'enable_discount_accounting')) - - if enable_discount_accounting and self.get('discount_amount') and self.get('additional_discount_account'): - amount = item.amount - base_amount = item.base_amount - - return amount, base_amount - def set_asset_status(self, asset): if self.is_return: asset.set_status() diff --git a/erpnext/controllers/accounts_controller.py b/erpnext/controllers/accounts_controller.py index 59879e0df58c..3d048c36865b 100644 --- a/erpnext/controllers/accounts_controller.py +++ b/erpnext/controllers/accounts_controller.py @@ -808,6 +808,27 @@ def update_allocated_advance_taxes_on_cancel(self): tax_map[tax.account_head] -= allocated_amount allocated_tax_map[tax.account_head] -= allocated_amount + def get_amount_and_base_amount(self, item, enable_discount_accounting): + amount = item.net_amount + base_amount = item.base_net_amount + + if enable_discount_accounting and self.get('discount_amount') and self.get('additional_discount_account'): + amount = item.amount + base_amount = item.base_amount + + return amount, base_amount + + def get_tax_amounts(self, tax, enable_discount_accounting): + amount = tax.tax_amount_after_discount_amount + base_amount = tax.base_tax_amount_after_discount_amount + + if enable_discount_accounting and self.get('discount_amount') and self.get('additional_discount_account') \ + and self.get('apply_discount_on') == 'Grand Total': + amount = tax.tax_amount + base_amount = tax.base_tax_amount + + return amount, base_amount + def make_discount_gl_entries(self, gl_entries): enable_discount_accounting = cint(frappe.db.get_single_value('Accounts Settings', 'enable_discount_accounting')) From c525b372c6582d09b20b061f8795ee51bf7aed13 Mon Sep 17 00:00:00 2001 From: Deepesh Garg Date: Wed, 21 Jul 2021 22:28:32 +0530 Subject: [PATCH 44/51] fix: Tests --- .../purchase_invoice/test_purchase_invoice.py | 20 +++++++++---------- .../sales_invoice/test_sales_invoice.py | 9 +++++---- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/erpnext/accounts/doctype/purchase_invoice/test_purchase_invoice.py b/erpnext/accounts/doctype/purchase_invoice/test_purchase_invoice.py index 7c26007b0726..9de3012554fe 100644 --- a/erpnext/accounts/doctype/purchase_invoice/test_purchase_invoice.py +++ b/erpnext/accounts/doctype/purchase_invoice/test_purchase_invoice.py @@ -271,17 +271,16 @@ def test_additional_discount_for_purchase_invoice_with_discount_accounting_enabl additional_discount_account = create_account(account_name="Discount Account", parent_account="Indirect Expenses - _TC", company="_Test Company") - pi = make_purchase_invoice(qty=1, rate=100, do_not_save=1) + pi = make_purchase_invoice(qty=1, rate=100, do_not_save=1, parent_cost_center="Main - _TC") pi.apply_discount_on = "Grand Total" pi.additional_discount_account = additional_discount_account pi.additional_discount_percentage = 20 pi.append("taxes", { - "charge_type": "On Net Total", - "account_head": "CGST - _TC", + "charge_type": "Actual", + "account_head": "_Test Account VAT - _TC", "cost_center": "Main - _TC", - "description": "CGST @ 9.0", - "base_tax_amount": 20, - "base_tax_amount_after_discount_amount": 20 + "description": "Test", + "tax_amount": 20 }) pi.submit() @@ -294,10 +293,10 @@ def test_additional_discount_for_purchase_invoice_with_discount_accounting_enabl # print(gl, "\n") expected_gle = [ - ["CGST - _TC", 20.0, 0.0, nowdate()], + ["_Test Account Cost for Goods Sold - _TC", 100.0, 0.0, nowdate()], + ["_Test Account VAT - _TC", 20.0, 0.0, nowdate()], ["Creditors - _TC", 0.0, 96.0, nowdate()], - ["Discount Account - _TC", 0.0, 24.0, nowdate()], - ["_Test Account Cost for Goods Sold - _TC", 100.0, 0.0, nowdate()] + ["Discount Account - _TC", 0.0, 24.0, nowdate()] ] check_gl_entries(self, pi.name, expected_gle, nowdate()) @@ -1218,6 +1217,7 @@ def check_gl_entries(doc, voucher_no, expected_gle, posting_date): where voucher_type='Purchase Invoice' and voucher_no=%s and posting_date >= %s order by posting_date asc, account asc""", (voucher_no, posting_date), as_dict=1) + print(gl_entries) for i, gle in enumerate(gl_entries): doc.assertEqual(expected_gle[i][0], gle.account) doc.assertEqual(expected_gle[i][1], gle.debit) @@ -1281,7 +1281,7 @@ def make_purchase_invoice(**args): pi.return_against = args.return_against pi.is_subcontracted = args.is_subcontracted or "No" pi.supplier_warehouse = args.supplier_warehouse or "_Test Warehouse 1 - _TC" - pi.cost_center = args.cost_center or "_Test Cost Center - _TC" + pi.cost_center = args.parent_cost_center pi.append("items", { "item_code": args.item or args.item_code or "_Test Item", diff --git a/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py b/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py index eccc69fdb273..6ee16f8e78a7 100644 --- a/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py +++ b/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py @@ -2045,16 +2045,17 @@ def test_additional_discount_for_sales_invoice_with_discount_accounting_enabled( si.additional_discount_percentage = 20 si.append("taxes", { "charge_type": "Actual", - "account_head": "CGST - _TC", + "account_head": "_Test Account VAT - _TC", "cost_center": "Main - _TC", - "description": "CGST @ 9.0", + "parent_cost_center": "Main - _TC", + "description": "Test", "rate": 0, "tax_amount": 20 }) si.submit() expected_gle = [ - ["CGST - _TC", 0.0, 20.0, nowdate()], + ["_Test Account VAT - _TC", 0.0, 20.0, nowdate()], ["Debtors - _TC", 96.0, 0.0, nowdate()], ["Discount Account - _TC", 24.0, 0.0, nowdate()], ["Sales - _TC", 0.0, 100.0, nowdate()] @@ -2229,7 +2230,7 @@ def create_sales_invoice(**args): si.currency=args.currency or "INR" si.conversion_rate = args.conversion_rate or 1 si.naming_series = args.naming_series or "T-SINV-" - si.cost_center = args.cost_center or "_Test Cost Center - _TC" + si.cost_center = args.parent_cost_center si.append("items", { "item_code": args.item or args.item_code or "_Test Item", From c677d47a4a62234693d1803355b75608803d7584 Mon Sep 17 00:00:00 2001 From: Deepesh Garg Date: Thu, 22 Jul 2021 10:43:16 +0530 Subject: [PATCH 45/51] fix: Test Cases --- .../doctype/purchase_invoice/test_purchase_invoice.py | 9 --------- .../accounts/doctype/sales_invoice/test_sales_invoice.py | 3 +-- 2 files changed, 1 insertion(+), 11 deletions(-) diff --git a/erpnext/accounts/doctype/purchase_invoice/test_purchase_invoice.py b/erpnext/accounts/doctype/purchase_invoice/test_purchase_invoice.py index 9de3012554fe..ed5c4af1a930 100644 --- a/erpnext/accounts/doctype/purchase_invoice/test_purchase_invoice.py +++ b/erpnext/accounts/doctype/purchase_invoice/test_purchase_invoice.py @@ -284,14 +284,6 @@ def test_additional_discount_for_purchase_invoice_with_discount_accounting_enabl }) pi.submit() - # gle = frappe.get_all( - # "GL Entry", - # fields = ['account', 'debit', 'credit', 'posting_date'], - # filters = {'voucher_no': pi.name} - # ) - # for gl in gle: - # print(gl, "\n") - expected_gle = [ ["_Test Account Cost for Goods Sold - _TC", 100.0, 0.0, nowdate()], ["_Test Account VAT - _TC", 20.0, 0.0, nowdate()], @@ -1217,7 +1209,6 @@ def check_gl_entries(doc, voucher_no, expected_gle, posting_date): where voucher_type='Purchase Invoice' and voucher_no=%s and posting_date >= %s order by posting_date asc, account asc""", (voucher_no, posting_date), as_dict=1) - print(gl_entries) for i, gle in enumerate(gl_entries): doc.assertEqual(expected_gle[i][0], gle.account) doc.assertEqual(expected_gle[i][1], gle.debit) diff --git a/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py b/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py index 6ee16f8e78a7..a55f708ab663 100644 --- a/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py +++ b/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py @@ -2039,7 +2039,7 @@ def test_additional_discount_for_sales_invoice_with_discount_accounting_enabled( additional_discount_account = create_account(account_name="Discount Account", parent_account="Indirect Expenses - _TC", company="_Test Company") - si = create_sales_invoice(rate=100, do_not_save=1) + si = create_sales_invoice(rate=100, parent_cost_center='Main - _TC', do_not_save=1) si.apply_discount_on = "Grand Total" si.additional_discount_account = additional_discount_account si.additional_discount_percentage = 20 @@ -2047,7 +2047,6 @@ def test_additional_discount_for_sales_invoice_with_discount_accounting_enabled( "charge_type": "Actual", "account_head": "_Test Account VAT - _TC", "cost_center": "Main - _TC", - "parent_cost_center": "Main - _TC", "description": "Test", "rate": 0, "tax_amount": 20 From ad7bb316c1dbe630c19d4e0146782e2fca108e6d Mon Sep 17 00:00:00 2001 From: Deepesh Garg Date: Wed, 28 Jul 2021 11:38:44 +0530 Subject: [PATCH 46/51] fix: GL Entries for discount amount with item qty greater than 1 --- erpnext/controllers/accounts_controller.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/erpnext/controllers/accounts_controller.py b/erpnext/controllers/accounts_controller.py index 3d048c36865b..8199b1040f0b 100644 --- a/erpnext/controllers/accounts_controller.py +++ b/erpnext/controllers/accounts_controller.py @@ -845,6 +845,7 @@ def make_discount_gl_entries(self, gl_entries): for item in self.get("items"): if item.get('discount_amount') and item.get('discount_account'): + discount_amount = item.discount_amount * item.qty if self.doctype == "Purchase Invoice": income_or_expense_account = (item.expense_account if (not item.enable_deferred_expense or self.is_return) @@ -859,8 +860,9 @@ def make_discount_gl_entries(self, gl_entries): self.get_gl_dict({ "account": item.discount_account, "against": supplier_or_customer, - dr_or_cr: flt(item.discount_amount), - dr_or_cr + "_in_account_currency": flt(item.discount_amount), + dr_or_cr: flt(discount_amount, item.precision('discount_amount')), + dr_or_cr + "_in_account_currency": flt(discount_amount * self.get('conversion_rate'), + item.precision('discount_amount')), "cost_center": item.cost_center, "project": item.project }, account_currency, item=item) @@ -871,8 +873,9 @@ def make_discount_gl_entries(self, gl_entries): self.get_gl_dict({ "account": income_or_expense_account, "against": supplier_or_customer, - rev_dr_cr: flt(item.discount_amount), - rev_dr_cr + "_in_account_currency": flt(item.discount_amount), + rev_dr_cr: flt(discount_amount, item.precision('discount_amount')), + rev_dr_cr + "_in_account_currency": flt(discount_amount * self.get('conversion_rate'), + item.precision('discount_amount')), "cost_center": item.cost_center, "project": item.project or self.project }, account_currency, item=item) From cb539b7a6a30fe48eb523c3288b889f6805903bc Mon Sep 17 00:00:00 2001 From: Deepesh Garg Date: Thu, 5 Aug 2021 22:38:00 +0530 Subject: [PATCH 47/51] fix: Add discount account handling in Purchase Invoice --- .../purchase_invoice/purchase_invoice.py | 23 ++++++++++--------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py index 53ad849b1e14..76ef23e87815 100644 --- a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py +++ b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py @@ -520,6 +520,8 @@ def make_item_gl_entries(self, gl_entries): and flt(d.base_tax_amount_after_discount_amount)] exchange_rate_map, net_rate_map = get_purchase_document_details(self) + + enable_discount_accounting = cint(frappe.db.get_single_value('Accounts Settings', 'enable_discount_accounting')) for item in self.get("items"): if flt(item.base_net_amount): @@ -611,11 +613,7 @@ def make_item_gl_entries(self, gl_entries): if (not item.enable_deferred_expense or self.is_return) else item.deferred_expense_account) if not item.is_fixed_asset: - if frappe.db.get_single_value('Accounts Settings', 'enable_discount_accounting'): - amount = flt(item.base_amount, item.precision("base_amount")) - else: - amount = flt(item.base_net_amount, item.precision("base_net_amount")) - + dummy, amount = self.get_amount_and_base_amount(item, enable_discount_accounting) else: amount = flt(item.base_net_amount + item.item_tax_amount, item.precision("base_net_amount")) @@ -857,8 +855,11 @@ def make_stock_adjustment_entry(self, gl_entries, item, voucher_wise_stock_value def make_tax_gl_entries(self, gl_entries): # tax table gl entries valuation_tax = {} + enable_discount_accounting = cint(frappe.db.get_single_value('Accounts Settings', 'enable_discount_accounting')) + for tax in self.get("taxes"): - if tax.category in ("Total", "Valuation and Total") and flt(tax.base_tax_amount_after_discount_amount): + amount, base_amount = self.get_tax_amounts(tax, enable_discount_accounting) + if tax.category in ("Total", "Valuation and Total") and flt(base_amount): account_currency = get_account_currency(tax.account_head) dr_or_cr = "debit" if tax.add_deduct_tax == "Add" else "credit" @@ -867,21 +868,21 @@ def make_tax_gl_entries(self, gl_entries): self.get_gl_dict({ "account": tax.account_head, "against": self.supplier, - dr_or_cr: tax.base_tax_amount_after_discount_amount, - dr_or_cr + "_in_account_currency": tax.base_tax_amount_after_discount_amount \ + dr_or_cr: base_amount, + dr_or_cr + "_in_account_currency": base_amount \ if account_currency==self.company_currency \ - else tax.tax_amount_after_discount_amount, + else amount, "cost_center": tax.cost_center }, account_currency, item=tax) ) # accumulate valuation tax - if self.is_opening == "No" and tax.category in ("Valuation", "Valuation and Total") and flt(tax.base_tax_amount_after_discount_amount) \ + if self.is_opening == "No" and tax.category in ("Valuation", "Valuation and Total") and flt(base_amount) \ and not self.is_internal_transfer(): if self.auto_accounting_for_stock and not tax.cost_center: frappe.throw(_("Cost Center is required in row {0} in Taxes table for type {1}").format(tax.idx, _(tax.category))) valuation_tax.setdefault(tax.name, 0) valuation_tax[tax.name] += \ - (tax.add_deduct_tax == "Add" and 1 or -1) * flt(tax.base_tax_amount_after_discount_amount) + (tax.add_deduct_tax == "Add" and 1 or -1) * flt(base_amount) if self.is_opening == "No" and self.negative_expense_to_be_booked and valuation_tax: # credit valuation tax amount in "Expenses Included In Valuation" From 75a832715c09b5e9da28aedee23a4b15b3f483da Mon Sep 17 00:00:00 2001 From: Deepesh Garg Date: Thu, 5 Aug 2021 22:38:24 +0530 Subject: [PATCH 48/51] test: Update test cases for discount accounting --- .../purchase_invoice/test_purchase_invoice.py | 27 ++++++++++--------- .../sales_invoice/test_sales_invoice.py | 24 +++++++++-------- 2 files changed, 28 insertions(+), 23 deletions(-) diff --git a/erpnext/accounts/doctype/purchase_invoice/test_purchase_invoice.py b/erpnext/accounts/doctype/purchase_invoice/test_purchase_invoice.py index ed5c4af1a930..c211e50548a0 100644 --- a/erpnext/accounts/doctype/purchase_invoice/test_purchase_invoice.py +++ b/erpnext/accounts/doctype/purchase_invoice/test_purchase_invoice.py @@ -256,39 +256,41 @@ def test_purchase_invoice_with_discount_accounting_enabled(self): discount_account = create_account(account_name="Discount Account", parent_account="Indirect Expenses - _TC", company="_Test Company") - pi = make_purchase_invoice(discount_account=discount_account, discount_amount=100) + pi = make_purchase_invoice(discount_account=discount_account, rate=45) expected_gle = [ - ["_Test Account Cost for Goods Sold - _TC", 350.0, 0.0, nowdate()], - ["Creditors - _TC", 0.0, 250.0, nowdate()], - ["Discount Account - _TC", 0.0, 100.0, nowdate()] + ["_Test Account Cost for Goods Sold - _TC", 250.0, 0.0, nowdate()], + ["Creditors - _TC", 0.0, 225.0, nowdate()], + ["Discount Account - _TC", 0.0, 25.0, nowdate()] ] check_gl_entries(self, pi.name, expected_gle, nowdate()) + enable_discount_accounting(enable=0) def test_additional_discount_for_purchase_invoice_with_discount_accounting_enabled(self): enable_discount_accounting() additional_discount_account = create_account(account_name="Discount Account", parent_account="Indirect Expenses - _TC", company="_Test Company") - pi = make_purchase_invoice(qty=1, rate=100, do_not_save=1, parent_cost_center="Main - _TC") + pi = make_purchase_invoice(do_not_save=1, parent_cost_center="Main - _TC") pi.apply_discount_on = "Grand Total" pi.additional_discount_account = additional_discount_account - pi.additional_discount_percentage = 20 + pi.additional_discount_percentage = 10 + pi.disable_rounded_total = 1 pi.append("taxes", { - "charge_type": "Actual", + "charge_type": "On Net Total", "account_head": "_Test Account VAT - _TC", "cost_center": "Main - _TC", "description": "Test", - "tax_amount": 20 + "rate": 10 }) pi.submit() expected_gle = [ - ["_Test Account Cost for Goods Sold - _TC", 100.0, 0.0, nowdate()], - ["_Test Account VAT - _TC", 20.0, 0.0, nowdate()], - ["Creditors - _TC", 0.0, 96.0, nowdate()], - ["Discount Account - _TC", 0.0, 24.0, nowdate()] + ["_Test Account Cost for Goods Sold - _TC", 250.0, 0.0, nowdate()], + ["_Test Account VAT - _TC", 25.0, 0.0, nowdate()], + ["Creditors - _TC", 0.0, 247.5, nowdate()], + ["Discount Account - _TC", 0.0, 27.5, nowdate()] ] check_gl_entries(self, pi.name, expected_gle, nowdate()) @@ -1281,6 +1283,7 @@ def make_purchase_invoice(**args): "received_qty": args.received_qty or 0, "rejected_qty": args.rejected_qty or 0, "rate": args.rate or 50, + "price_list_rate": args.price_list_rate or 50, "expense_account": args.expense_account or '_Test Account Cost for Goods Sold - _TC', "discount_account": args.discount_account or None, "discount_amount": args.discount_amount or 0, diff --git a/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py b/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py index a55f708ab663..bde11d2566b3 100644 --- a/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py +++ b/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py @@ -2022,15 +2022,16 @@ def test_sales_invoice_with_discount_accounting_enabled(self): discount_account = create_account(account_name="Discount Account", parent_account="Indirect Expenses - _TC", company="_Test Company") - si = create_sales_invoice(discount_account=discount_account, discount_amount=100) + si = create_sales_invoice(discount_account=discount_account, discount_percentage=10, rate=90) expected_gle = [ - ["Debtors - _TC", 100.0, 0.0, nowdate()], - ["Discount Account - _TC", 100.0, 0.0, nowdate()], - ["Sales - _TC", 0.0, 200.0, nowdate()] + ["Debtors - _TC", 90.0, 0.0, nowdate()], + ["Discount Account - _TC", 10.0, 0.0, nowdate()], + ["Sales - _TC", 0.0, 100.0, nowdate()] ] check_gl_entries(self, si.name, expected_gle, add_days(nowdate(), -1)) + enable_discount_accounting(enable=0) def test_additional_discount_for_sales_invoice_with_discount_accounting_enabled(self): from erpnext.accounts.doctype.purchase_invoice.test_purchase_invoice import enable_discount_accounting @@ -2039,28 +2040,28 @@ def test_additional_discount_for_sales_invoice_with_discount_accounting_enabled( additional_discount_account = create_account(account_name="Discount Account", parent_account="Indirect Expenses - _TC", company="_Test Company") - si = create_sales_invoice(rate=100, parent_cost_center='Main - _TC', do_not_save=1) + si = create_sales_invoice(parent_cost_center='Main - _TC', do_not_save=1) si.apply_discount_on = "Grand Total" si.additional_discount_account = additional_discount_account si.additional_discount_percentage = 20 si.append("taxes", { - "charge_type": "Actual", + "charge_type": "On Net Total", "account_head": "_Test Account VAT - _TC", "cost_center": "Main - _TC", "description": "Test", - "rate": 0, - "tax_amount": 20 + "rate": 10 }) si.submit() expected_gle = [ - ["_Test Account VAT - _TC", 0.0, 20.0, nowdate()], - ["Debtors - _TC", 96.0, 0.0, nowdate()], - ["Discount Account - _TC", 24.0, 0.0, nowdate()], + ["_Test Account VAT - _TC", 0.0, 10.0, nowdate()], + ["Debtors - _TC", 88, 0.0, nowdate()], + ["Discount Account - _TC", 22.0, 0.0, nowdate()], ["Sales - _TC", 0.0, 100.0, nowdate()] ] check_gl_entries(self, si.name, expected_gle, add_days(nowdate(), -1)) + enable_discount_accounting(enable=0) def get_sales_invoice_for_e_invoice(): si = make_sales_invoice_for_ewaybill() @@ -2241,6 +2242,7 @@ def create_sales_invoice(**args): "uom": args.uom or "Nos", "stock_uom": args.uom or "Nos", "rate": args.rate if args.get("rate") is not None else 100, + "price_list_rate": args.price_list_rate if args.get("price_list_rate") is not None else 100, "income_account": args.income_account or "Sales - _TC", "expense_account": args.expense_account or "Cost of Goods Sold - _TC", "discount_account": args.discount_account or None, From 5999760a65db9b8d376126e4bf70feff07bc8b01 Mon Sep 17 00:00:00 2001 From: GangaManoj Date: Fri, 13 Aug 2021 17:11:53 +0530 Subject: [PATCH 49/51] fix: Syntax error --- erpnext/accounts/doctype/sales_invoice/sales_invoice.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.js b/erpnext/accounts/doctype/sales_invoice/sales_invoice.js index dbc42de583ec..2751b5509cc6 100644 --- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.js +++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.js @@ -348,7 +348,7 @@ erpnext.accounts.SalesInvoiceController = class SalesInvoiceController extends e items_add(doc, cdt, cdn) { var row = frappe.get_doc(cdt, cdn); this.frm.script_manager.copy_from_first_row("items", row, ["income_account", "discount_account", "cost_center"]); - }, + } set_dynamic_labels() { super.set_dynamic_labels(); From f977c65e804054a7bbd8b6b3670859821ccd9d1d Mon Sep 17 00:00:00 2001 From: GangaManoj Date: Fri, 13 Aug 2021 17:12:45 +0530 Subject: [PATCH 50/51] fix: Make enable_discount_accounting a class property --- .../doctype/purchase_invoice/purchase_invoice.py | 14 +++++++++----- .../doctype/sales_invoice/sales_invoice.py | 13 ++++++++----- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py index a95f971e00ae..574a353cdf2c 100644 --- a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py +++ b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py @@ -523,8 +523,6 @@ def make_item_gl_entries(self, gl_entries): exchange_rate_map, net_rate_map = get_purchase_document_details(self) - enable_discount_accounting = cint(frappe.db.get_single_value('Accounts Settings', 'enable_discount_accounting')) - for item in self.get("items"): if flt(item.base_net_amount): account_currency = get_account_currency(item.expense_account) @@ -615,7 +613,7 @@ def make_item_gl_entries(self, gl_entries): if (not item.enable_deferred_expense or self.is_return) else item.deferred_expense_account) if not item.is_fixed_asset: - dummy, amount = self.get_amount_and_base_amount(item, enable_discount_accounting) + dummy, amount = self.get_amount_and_base_amount(item, self.enable_discount_accounting) else: amount = flt(item.base_net_amount + item.item_tax_amount, item.precision("base_net_amount")) @@ -857,10 +855,9 @@ def make_stock_adjustment_entry(self, gl_entries, item, voucher_wise_stock_value def make_tax_gl_entries(self, gl_entries): # tax table gl entries valuation_tax = {} - enable_discount_accounting = cint(frappe.db.get_single_value('Accounts Settings', 'enable_discount_accounting')) for tax in self.get("taxes"): - amount, base_amount = self.get_tax_amounts(tax, enable_discount_accounting) + amount, base_amount = self.get_tax_amounts(tax, self.enable_discount_accounting) if tax.category in ("Total", "Valuation and Total") and flt(base_amount): account_currency = get_account_currency(tax.account_head) @@ -925,6 +922,13 @@ def make_tax_gl_entries(self, gl_entries): "remarks": self.remarks or "Accounting Entry for Stock" }, item=tax)) + @property + def enable_discount_accounting(self): + if not hasattr(self, "_enable_discount_accounting"): + self._enable_discount_accounting = cint(frappe.db.get_single_value('Accounts Settings', 'enable_discount_accounting')) + + return self._enable_discount_accounting + def make_internal_transfer_gl_entries(self, gl_entries): if self.is_internal_transfer() and flt(self.base_total_taxes_and_charges): account_currency = get_account_currency(self.unrealized_profit_loss_account) diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py index 1e1fe9001a08..0db426ad5ece 100644 --- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py +++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py @@ -888,10 +888,8 @@ def make_customer_gl_entry(self, gl_entries): ) def make_tax_gl_entries(self, gl_entries): - enable_discount_accounting = cint(frappe.db.get_single_value('Accounts Settings', 'enable_discount_accounting')) - for tax in self.get("taxes"): - amount, base_amount = self.get_tax_amounts(tax, enable_discount_accounting) + amount, base_amount = self.get_tax_amounts(tax, self.enable_discount_accounting) if flt(tax.base_tax_amount_after_discount_amount): account_currency = get_account_currency(tax.account_head) @@ -922,7 +920,6 @@ def make_internal_transfer_gl_entries(self, gl_entries): def make_item_gl_entries(self, gl_entries): # income account gl entries - enable_discount_accounting = cint(frappe.db.get_single_value('Accounts Settings', 'enable_discount_accounting')) for item in self.get("items"): if flt(item.base_net_amount, item.precision("base_net_amount")): @@ -957,7 +954,7 @@ def make_item_gl_entries(self, gl_entries): income_account = (item.income_account if (not item.enable_deferred_revenue or self.is_return) else item.deferred_revenue_account) - amount, base_amount = self.get_amount_and_base_amount(item, enable_discount_accounting) + amount, base_amount = self.get_amount_and_base_amount(item, self.enable_discount_accounting) account_currency = get_account_currency(income_account) gl_entries.append( @@ -1060,6 +1057,12 @@ def sale_was_made_on_original_schedule_date(self, asset, schedule, row, posting_ if orginal_schedule_date == posting_date_of_original_invoice: return True return False + @property + def enable_discount_accounting(self): + if not hasattr(self, "_enable_discount_accounting"): + self._enable_discount_accounting = cint(frappe.db.get_single_value('Accounts Settings', 'enable_discount_accounting')) + + return self._enable_discount_accounting def set_asset_status(self, asset): if self.is_return: From f9356ee64211e61bcb66af98b86b57e52240b085 Mon Sep 17 00:00:00 2001 From: GangaManoj Date: Fri, 13 Aug 2021 17:40:51 +0530 Subject: [PATCH 51/51] fix: Sider issues --- erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py | 4 ++-- erpnext/accounts/doctype/sales_invoice/sales_invoice.py | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py index 574a353cdf2c..c3cb159038b5 100644 --- a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py +++ b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py @@ -868,8 +868,8 @@ def make_tax_gl_entries(self, gl_entries): "account": tax.account_head, "against": self.supplier, dr_or_cr: base_amount, - dr_or_cr + "_in_account_currency": base_amount \ - if account_currency==self.company_currency \ + dr_or_cr + "_in_account_currency": base_amount + if account_currency==self.company_currency else amount, "cost_center": tax.cost_center }, account_currency, item=tax) diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py index 0db426ad5ece..3b8cba2a1e8a 100644 --- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py +++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py @@ -1057,6 +1057,7 @@ def sale_was_made_on_original_schedule_date(self, asset, schedule, row, posting_ if orginal_schedule_date == posting_date_of_original_invoice: return True return False + @property def enable_discount_accounting(self): if not hasattr(self, "_enable_discount_accounting"):