Companywise default tax template (#12290)

* Companywise default tax template

* Fix test cases
This commit is contained in:
rohitwaghchaure
2018-01-05 12:42:02 +05:30
committed by Nabin Hait
parent 3f784a7a49
commit bc2c83ee3c
23 changed files with 141 additions and 83 deletions

View File

@@ -34,24 +34,52 @@ frappe.ui.form.on("Company", {
frm.add_custom_button(__('Cost Centers'), function() {
frappe.set_route('Tree', 'Cost Center', {'company': frm.doc.name})
})
}, __("View"));
frm.add_custom_button(__('Chart of Accounts'), function() {
frappe.set_route('Tree', 'Account', {'company': frm.doc.name})
})
}, __("View"));
frm.add_custom_button(__('Sales Tax Template'), function() {
frappe.set_route('List', 'Sales Taxes and Charges Template', {'company': frm.doc.name});
}, __("View"));
frm.add_custom_button(__('Purchase Tax Template'), function() {
frappe.set_route('List', 'Purchase Taxes and Charges Template', {'company': frm.doc.name});
}, __("View"));
frm.add_custom_button(__('Default Tax Template'), function() {
frm.trigger("make_default_tax_template");
}, __("Make"));
frm.page.set_inner_btn_group_as_primary(__("View"));
frm.page.set_inner_btn_group_as_primary(__("Make"));
}
erpnext.company.set_chart_of_accounts_options(frm.doc);
},
make_default_tax_template: function(frm) {
frm.call({
method: "create_default_tax_template",
doc: frm.doc,
freeze: true,
callback: function() {
frappe.msgprint(__("Default tax templates for sales and purchase are created."));
}
})
},
onload_post_render: function(frm) {
if(frm.get_field("delete_company_transactions").$input)
frm.get_field("delete_company_transactions").$input.addClass("btn-danger");
},
country: function(frm) {
erpnext.company.set_chart_of_accounts_options(frm.doc);
},
delete_company_transactions: function(frm) {
frappe.verify_password(function() {
var d = frappe.prompt({

View File

@@ -50,6 +50,13 @@ class Company(Document):
if frappe.db.sql("select abbr from tabCompany where name!=%s and abbr=%s", (self.name, self.abbr)):
frappe.throw(_("Abbreviation already used for another company"))
def create_default_tax_template(self):
from erpnext.setup.setup_wizard.operations.taxes_setup import create_sales_tax
create_sales_tax({
'country': self.country,
'company_name': self.name
})
def validate_default_accounts(self):
for field in ["default_bank_account", "default_cash_account",
"default_receivable_account", "default_payable_account",

View File

@@ -16,38 +16,44 @@ def create_sales_tax(args):
tax_data.get('tax_rate'), sales_tax)
def make_tax_account_and_template(company, account_name, tax_rate, template_name=None):
if not isinstance(account_name, (list, tuple)):
account_name = [account_name]
tax_rate = [tax_rate]
accounts = []
for i, name in enumerate(account_name):
tax_account = make_tax_account(company, account_name[i], tax_rate[i])
if tax_account:
accounts.append(tax_account)
try:
if not isinstance(account_name, (list, tuple)):
account_name = [account_name]
tax_rate = [tax_rate]
accounts = []
for i, name in enumerate(account_name):
tax_account = make_tax_account(company, account_name[i], tax_rate[i])
if tax_account:
accounts.append(tax_account)
if accounts:
make_sales_and_purchase_tax_templates(accounts, template_name)
except frappe.NameError:
pass
frappe.message_log.pop()
except RootNotEditable:
pass
def make_tax_account(company, account_name, tax_rate):
tax_group = get_tax_account_group(company)
if tax_group:
return frappe.get_doc({
"doctype":"Account",
"company": company,
"parent_account": tax_group,
"account_name": account_name,
"is_group": 0,
"report_type": "Balance Sheet",
"root_type": "Liability",
"account_type": "Tax",
"tax_rate": flt(tax_rate) if tax_rate else None
}).insert(ignore_permissions=True, ignore_mandatory=True)
try:
return frappe.get_doc({
"doctype":"Account",
"company": company,
"parent_account": tax_group,
"account_name": account_name,
"is_group": 0,
"report_type": "Balance Sheet",
"root_type": "Liability",
"account_type": "Tax",
"tax_rate": flt(tax_rate) if tax_rate else None
}).insert(ignore_permissions=True, ignore_mandatory=True)
except frappe.NameError:
frappe.message_log.pop()
abbr = frappe.db.get_value('Company', company, 'abbr')
account = '{0} - {1}'.format(account_name, abbr)
return frappe.get_doc('Account', account)
def make_sales_and_purchase_tax_templates(accounts, template_name=None):
if not template_name:
@@ -62,7 +68,7 @@ def make_sales_and_purchase_tax_templates(accounts, template_name=None):
for account in accounts:
sales_tax_template['taxes'].append({
"category": "Valuation and Total",
"category": "Total",
"charge_type": "On Net Total",
"account_head": account.name,
"description": "{0} @ {1}".format(account.account_name, account.tax_rate),