[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
This commit is contained in:
Nabin Hait 2017-08-16 15:04:18 +05:30 committed by Makarand Bauskar
parent f35710db15
commit 64c32dcd59
5 changed files with 39 additions and 18 deletions

View File

@ -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([{

View File

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

View File

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

View File

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

View File

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