From 64c32dcd59843d44135f1cdcbec2c5633c611924 Mon Sep 17 00:00:00 2001 From: Nabin Hait Date: Wed, 16 Aug 2017 15:04:18 +0530 Subject: [PATCH] [fix] Update monthly sales value on Company (#10417) * [fix] Update monthly sales value on cancellation of invoice, also update modified datestamp * Company monthly sales: patches and test cases --- .../doctype/sales_invoice/sales_invoice.py | 12 +++++++- .../sales_invoice/test_sales_invoice.py | 11 ++++++++ erpnext/patches.txt | 2 +- erpnext/setup/doctype/company/company.py | 28 ++++++++----------- .../company/delete_company_transactions.py | 4 +++ 5 files changed, 39 insertions(+), 18 deletions(-) diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py index 9dfacbdb5e..96d617e865 100644 --- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py +++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py @@ -19,6 +19,7 @@ from erpnext.accounts.doctype.asset.depreciation \ import get_disposal_account_and_cost_center, get_gl_entries_on_asset_disposal from erpnext.stock.doctype.batch.batch import set_batch_nos from erpnext.stock.doctype.serial_no.serial_no import get_serial_nos, get_delivery_note_serial_no +from erpnext.setup.doctype.company.company import update_company_current_month_sales form_grid_templates = { "items": "templates/form_grid/item_grid.html" @@ -140,7 +141,7 @@ class SalesInvoice(SellingController): self.update_time_sheet(self.name) - frappe.enqueue('erpnext.setup.doctype.company.company.update_company_current_month_sales', company=self.company) + self.update_current_month_sales() def validate_pos_paid_amount(self): if len(self.payments) == 0 and self.is_pos: @@ -178,6 +179,15 @@ class SalesInvoice(SellingController): self.make_gl_entries_on_cancel() frappe.db.set(self, 'status', 'Cancelled') + self.update_current_month_sales() + + def update_current_month_sales(self): + if frappe.flags.in_test: + update_company_current_month_sales(self.company) + else: + frappe.enqueue('erpnext.setup.doctype.company.company.update_company_current_month_sales', + company=self.company) + def update_status_updater_args(self): if cint(self.update_stock): self.status_updater.extend([{ diff --git a/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py b/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py index 806b489da4..e0a453c5c9 100644 --- a/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py +++ b/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py @@ -1272,6 +1272,17 @@ class TestSalesInvoice(unittest.TestCase): si.insert() return si + def test_company_monthly_sales(self): + existing_current_month_sales = frappe.db.get_value("Company", "_Test Company", "total_monthly_sales") + + si = create_sales_invoice() + current_month_sales = frappe.db.get_value("Company", "_Test Company", "total_monthly_sales") + self.assertEqual(current_month_sales, existing_current_month_sales + si.base_grand_total) + + si.cancel() + current_month_sales = frappe.db.get_value("Company", "_Test Company", "total_monthly_sales") + self.assertEqual(current_month_sales, existing_current_month_sales) + def create_sales_invoice(**args): si = frappe.new_doc("Sales Invoice") args = frappe._dict(args) diff --git a/erpnext/patches.txt b/erpnext/patches.txt index 60caf0fec1..6b660e1e6d 100644 --- a/erpnext/patches.txt +++ b/erpnext/patches.txt @@ -421,7 +421,7 @@ erpnext.patches.v8_1.removed_report_support_hours erpnext.patches.v8_1.add_indexes_in_transaction_doctypes erpnext.patches.v8_3.set_restrict_to_domain_for_module_def erpnext.patches.v8_1.update_expense_claim_status -erpnext.patches.v8_3.update_company_total_sales +erpnext.patches.v8_3.update_company_total_sales #2017-08-16 erpnext.patches.v8_4.make_scorecard_records erpnext.patches.v8_1.set_delivery_date_in_so_item #2017-07-28 erpnext.patches.v8_5.fix_tax_breakup_for_non_invoice_docs diff --git a/erpnext/setup/doctype/company/company.py b/erpnext/setup/doctype/company/company.py index 29630eeb84..b945ee4104 100644 --- a/erpnext/setup/doctype/company/company.py +++ b/erpnext/setup/doctype/company/company.py @@ -5,7 +5,7 @@ from __future__ import unicode_literals import frappe, os from frappe import _ -from frappe.utils import cint +from frappe.utils import cint, today, formatdate import frappe.defaults @@ -312,39 +312,35 @@ def get_name_with_abbr(name, company): return " - ".join(parts) def update_company_current_month_sales(company): - from frappe.utils import today, formatdate current_month_year = formatdate(today(), "MM-yyyy") - results = frappe.db.sql((''' + results = frappe.db.sql(''' select sum(base_grand_total) as total, date_format(posting_date, '%m-%Y') as month_year from `tabSales Invoice` where - date_format(posting_date, '%m-%Y')="{0}" and - company = "{1}" + date_format(posting_date, '%m-%Y')="{0}" + and docstatus = 1 + and company = "{1}" group by - month_year; - ''').format(current_month_year, frappe.db.escape(company)), as_dict = True) + month_year + '''.format(current_month_year, frappe.db.escape(company)), as_dict = True) monthly_total = results[0]['total'] if len(results) > 0 else 0 - frappe.db.sql((''' - update tabCompany set total_monthly_sales = %s where name=%s - '''), (monthly_total, frappe.db.escape(company))) + frappe.db.set_value("Company", company, "total_monthly_sales", monthly_total) frappe.db.commit() - def update_company_monthly_sales(company): '''Cache past year monthly sales of every company based on sales invoices''' from frappe.utils.goal import get_monthly_results import json - filter_str = "company = '{0}' and status != 'Draft'".format(frappe.db.escape(company)) - month_to_value_dict = get_monthly_results("Sales Invoice", "base_grand_total", "posting_date", filter_str, "sum") + filter_str = "company = '{0}' and status != 'Draft' and docstatus=1".format(frappe.db.escape(company)) + month_to_value_dict = get_monthly_results("Sales Invoice", "base_grand_total", + "posting_date", filter_str, "sum") - frappe.db.sql((''' - update tabCompany set sales_monthly_history = %s where name=%s - '''), (json.dumps(month_to_value_dict), frappe.db.escape(company))) + frappe.db.set_value("Company", company, "sales_monthly_history", json.dumps(month_to_value_dict)) frappe.db.commit() def cache_companies_monthly_sales_history(): diff --git a/erpnext/setup/doctype/company/delete_company_transactions.py b/erpnext/setup/doctype/company/delete_company_transactions.py index f1fb0cf96e..d975a9ee8d 100644 --- a/erpnext/setup/doctype/company/delete_company_transactions.py +++ b/erpnext/setup/doctype/company/delete_company_transactions.py @@ -27,6 +27,10 @@ def delete_company_transactions(company_name): "Purchase Taxes and Charges Template", "POS Profile", 'BOM'): delete_for_doctype(doctype, company_name) + # reset company values + doc.total_monthly_sales = 0 + doc.sales_monthly_history = None + doc.save() # Clear notification counts clear_notifications()