Merge pull request #24805 from alyf-de/improve_taxes_setup
refactor: Setup Taxes and Charges
This commit is contained in:
commit
1e0d22a38f
@ -4,11 +4,8 @@
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from erpnext.regional.united_arab_emirates.setup import make_custom_fields, add_print_formats
|
||||
from erpnext.setup.setup_wizard.operations.taxes_setup import create_sales_tax
|
||||
|
||||
|
||||
def setup(company=None, patch=True):
|
||||
make_custom_fields()
|
||||
add_print_formats()
|
||||
|
||||
if company:
|
||||
create_sales_tax(company)
|
@ -6,7 +6,6 @@ from __future__ import unicode_literals
|
||||
import frappe, os, json
|
||||
from frappe.custom.doctype.custom_field.custom_field import create_custom_fields
|
||||
from frappe.permissions import add_permission, update_permission_property
|
||||
from erpnext.setup.setup_wizard.operations.taxes_setup import create_sales_tax
|
||||
from erpnext.payroll.doctype.gratuity_rule.gratuity_rule import get_gratuity_rule
|
||||
|
||||
def setup(company=None, patch=True):
|
||||
@ -16,9 +15,6 @@ def setup(company=None, patch=True):
|
||||
add_permissions()
|
||||
create_gratuity_rule()
|
||||
|
||||
if company:
|
||||
create_sales_tax(company)
|
||||
|
||||
def make_custom_fields():
|
||||
is_zero_rated = dict(fieldname='is_zero_rated', label='Is Zero Rated',
|
||||
fieldtype='Check', fetch_from='item_code.is_zero_rated', insert_after='description',
|
||||
|
@ -17,6 +17,7 @@ from frappe.utils.nestedset import NestedSet
|
||||
from past.builtins import cmp
|
||||
import functools
|
||||
from erpnext.accounts.doctype.account.account import get_account_currency
|
||||
from erpnext.setup.setup_wizard.operations.taxes_setup import setup_taxes_and_charges
|
||||
|
||||
class Company(NestedSet):
|
||||
nsm_parent_field = 'parent_company'
|
||||
@ -68,11 +69,7 @@ class Company(NestedSet):
|
||||
|
||||
@frappe.whitelist()
|
||||
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
|
||||
})
|
||||
setup_taxes_and_charges(self.name, self.country)
|
||||
|
||||
def validate_default_accounts(self):
|
||||
accounts = [
|
||||
|
@ -481,14 +481,250 @@
|
||||
},
|
||||
|
||||
"Germany": {
|
||||
"Germany VAT 19%": {
|
||||
"account_name": "VAT 19%",
|
||||
"tax_rate": 19.00,
|
||||
"default": 1
|
||||
},
|
||||
"Germany VAT 7%": {
|
||||
"account_name": "VAT 7%",
|
||||
"tax_rate": 7.00
|
||||
"chart_of_accounts": {
|
||||
"SKR04 mit Kontonummern": {
|
||||
"sales_tax_templates": [
|
||||
{
|
||||
"title": "Umsatzsteuer 19%",
|
||||
"taxes": [
|
||||
{
|
||||
"account_head": {
|
||||
"account_name": "Umsatzsteuer 19%",
|
||||
"account_number": "3806",
|
||||
"tax_rate": 19.00
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"title": "Umsatzsteuer 7%",
|
||||
"taxes": [
|
||||
{
|
||||
"account_head": {
|
||||
"account_name": "Umsatzsteuer 7%",
|
||||
"account_number": "3801",
|
||||
"tax_rate": 7.00
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"purchase_tax_templates": [
|
||||
{
|
||||
"title": "Abziehbare Vorsteuer 19%",
|
||||
"taxes": [
|
||||
{
|
||||
"account_head": {
|
||||
"account_name": "Abziehbare Vorsteuer 19%",
|
||||
"account_number": "1406",
|
||||
"root_type": "Asset",
|
||||
"tax_rate": 19.00
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"title": "Abziehbare Vorsteuer 7%",
|
||||
"taxes": [
|
||||
{
|
||||
"account_head": {
|
||||
"account_name": "Abziehbare Vorsteuer 7%",
|
||||
"account_number": "1401",
|
||||
"root_type": "Asset",
|
||||
"tax_rate": 7.00
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"title": "Innergemeinschaftlicher Erwerb 19% Umsatzsteuer und 19% Vorsteuer",
|
||||
"taxes": [
|
||||
{
|
||||
"account_head": {
|
||||
"account_name": "Abziehbare Vorsteuer nach § 13b UStG 19%",
|
||||
"account_number": "1407",
|
||||
"root_type": "Asset",
|
||||
"tax_rate": 19.00
|
||||
},
|
||||
"add_deduct_tax": "Add"
|
||||
},
|
||||
{
|
||||
"account_head": {
|
||||
"account_name": "Umsatzsteuer nach § 13b UStG 19%",
|
||||
"account_number": "3837",
|
||||
"root_type": "Liability",
|
||||
"tax_rate": 19.00
|
||||
},
|
||||
"add_deduct_tax": "Deduct"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
"SKR03 mit Kontonummern": {
|
||||
"sales_tax_templates": [
|
||||
{
|
||||
"title": "Umsatzsteuer 19%",
|
||||
"taxes": [
|
||||
{
|
||||
"account_head": {
|
||||
"account_name": "Umsatzsteuer 19%",
|
||||
"account_number": "1776",
|
||||
"tax_rate": 19.00
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"title": "Umsatzsteuer 7%",
|
||||
"taxes": [
|
||||
{
|
||||
"account_head": {
|
||||
"account_name": "Umsatzsteuer 7%",
|
||||
"account_number": "1771",
|
||||
"tax_rate": 7.00
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"purchase_tax_templates": [
|
||||
{
|
||||
"title": "Abziehbare Vorsteuer 19%",
|
||||
"taxes": [
|
||||
{
|
||||
"account_head": {
|
||||
"account_name": "Abziehbare Vorsteuer 19%",
|
||||
"account_number": "1576",
|
||||
"root_type": "Asset",
|
||||
"tax_rate": 19.00
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"title": "Abziehbare Vorsteuer 7%",
|
||||
"taxes": [
|
||||
{
|
||||
"account_head": {
|
||||
"account_name": "Abziehbare Vorsteuer 7%",
|
||||
"account_number": "1571",
|
||||
"root_type": "Asset",
|
||||
"tax_rate": 7.00
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
"Standard with Numbers": {
|
||||
"sales_tax_templates": [
|
||||
{
|
||||
"title": "Umsatzsteuer 19%",
|
||||
"taxes": [
|
||||
{
|
||||
"account_head": {
|
||||
"account_name": "Umsatzsteuer 19%",
|
||||
"account_number": "2301",
|
||||
"tax_rate": 19.00
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"title": "Umsatzsteuer 7%",
|
||||
"taxes": [
|
||||
{
|
||||
"account_head": {
|
||||
"account_name": "Umsatzsteuer 7%",
|
||||
"account_number": "2302",
|
||||
"tax_rate": 7.00
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"purchase_tax_templates": [
|
||||
{
|
||||
"title": "Abziehbare Vorsteuer 19%",
|
||||
"taxes": [
|
||||
{
|
||||
"account_head": {
|
||||
"account_name": "Abziehbare Vorsteuer 19%",
|
||||
"account_number": "1501",
|
||||
"root_type": "Asset",
|
||||
"tax_rate": 19.00
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"title": "Abziehbare Vorsteuer 7%",
|
||||
"taxes": [
|
||||
{
|
||||
"account_head": {
|
||||
"account_name": "Abziehbare Vorsteuer 7%",
|
||||
"account_number": "1502",
|
||||
"root_type": "Asset",
|
||||
"tax_rate": 7.00
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
"*": {
|
||||
"sales_tax_templates": [
|
||||
{
|
||||
"title": "Umsatzsteuer 19%",
|
||||
"taxes": [
|
||||
{
|
||||
"account_head": {
|
||||
"account_name": "Umsatzsteuer 19%",
|
||||
"tax_rate": 19.00
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"title": "Umsatzsteuer 7%",
|
||||
"taxes": [
|
||||
{
|
||||
"account_head": {
|
||||
"account_name": "Umsatzsteuer 7%",
|
||||
"tax_rate": 7.00
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"purchase_tax_templates": [
|
||||
{
|
||||
"title": "Abziehbare Vorsteuer 19%",
|
||||
"taxes": [
|
||||
{
|
||||
"account_head": {
|
||||
"account_name": "Abziehbare Vorsteuer 19%",
|
||||
"tax_rate": 19.00,
|
||||
"root_type": "Asset"
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"title": "Abziehbare Vorsteuer 7%",
|
||||
"taxes": [
|
||||
{
|
||||
"account_head": {
|
||||
"account_name": "Abziehbare Vorsteuer 7%",
|
||||
"root_type": "Asset",
|
||||
"tax_rate": 7.00
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
@ -580,26 +816,135 @@
|
||||
},
|
||||
|
||||
"India": {
|
||||
"In State GST": {
|
||||
"account_name": ["SGST", "CGST"],
|
||||
"tax_rate": [9.00, 9.00],
|
||||
"default": 1
|
||||
},
|
||||
"Out of State GST": {
|
||||
"account_name": "IGST",
|
||||
"tax_rate": 18.00
|
||||
},
|
||||
"VAT 5%": {
|
||||
"account_name": "VAT 5%",
|
||||
"tax_rate": 5.00
|
||||
},
|
||||
"VAT 4%": {
|
||||
"account_name": "VAT 4%",
|
||||
"tax_rate": 4.00
|
||||
},
|
||||
"VAT 14%": {
|
||||
"account_name": "VAT 14%",
|
||||
"tax_rate": 14.00
|
||||
"chart_of_accounts": {
|
||||
"*": {
|
||||
"item_tax_templates": [
|
||||
{
|
||||
"title": "In State GST",
|
||||
"taxes": [
|
||||
{
|
||||
"tax_type": {
|
||||
"account_name": "SGST",
|
||||
"tax_rate": 9.00
|
||||
}
|
||||
},
|
||||
{
|
||||
"tax_type": {
|
||||
"account_name": "CGST",
|
||||
"tax_rate": 9.00
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"title": "Out of State GST",
|
||||
"taxes": [
|
||||
{
|
||||
"tax_type": {
|
||||
"account_name": "IGST",
|
||||
"tax_rate": 18.00
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"title": "VAT 5%",
|
||||
"taxes": [
|
||||
{
|
||||
"tax_type": {
|
||||
"account_name": "VAT 5%",
|
||||
"tax_rate": 5.00
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"title": "VAT 4%",
|
||||
"taxes": [
|
||||
{
|
||||
"tax_type": {
|
||||
"account_name": "VAT 4%",
|
||||
"tax_rate": 4.00
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"title": "VAT 14%",
|
||||
"taxes": [
|
||||
{
|
||||
"tax_type": {
|
||||
"account_name": "VAT 14%",
|
||||
"tax_rate": 14.00
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"*": [
|
||||
{
|
||||
"title": "In State GST",
|
||||
"taxes": [
|
||||
{
|
||||
"account_head": {
|
||||
"account_name": "SGST",
|
||||
"tax_rate": 9.00
|
||||
}
|
||||
},
|
||||
{
|
||||
"account_head": {
|
||||
"account_name": "CGST",
|
||||
"tax_rate": 9.00
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"title": "Out of State GST",
|
||||
"taxes": [
|
||||
{
|
||||
"account_head": {
|
||||
"account_name": "IGST",
|
||||
"tax_rate": 18.00
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"title": "VAT 5%",
|
||||
"taxes": [
|
||||
{
|
||||
"account_head": {
|
||||
"account_name": "VAT 5%",
|
||||
"tax_rate": 5.00
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"title": "VAT 4%",
|
||||
"taxes": [
|
||||
{
|
||||
"account_head": {
|
||||
"account_name": "VAT 4%",
|
||||
"tax_rate": 4.00
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"title": "VAT 14%",
|
||||
"taxes": [
|
||||
{
|
||||
"account_head": {
|
||||
"account_name": "VAT 14%",
|
||||
"tax_rate": 14.00
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
|
@ -1,123 +1,232 @@
|
||||
# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
|
||||
# Copyright (c) 2021, Frappe Technologies Pvt. Ltd. and Contributors
|
||||
# License: GNU General Public License v3. See license.txt
|
||||
|
||||
from __future__ import unicode_literals
|
||||
import frappe, copy, os, json
|
||||
from frappe.utils import flt
|
||||
from erpnext.accounts.doctype.account.account import RootNotEditable
|
||||
|
||||
def create_sales_tax(args):
|
||||
country_wise_tax = get_country_wise_tax(args.get("country"))
|
||||
if country_wise_tax and len(country_wise_tax) > 0:
|
||||
for sales_tax, tax_data in country_wise_tax.items():
|
||||
make_tax_account_and_template(
|
||||
args.get("company_name"),
|
||||
tax_data.get('account_name'),
|
||||
tax_data.get('tax_rate'), sales_tax)
|
||||
import os
|
||||
import json
|
||||
|
||||
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]
|
||||
import frappe
|
||||
from frappe import _
|
||||
|
||||
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 accounts:
|
||||
make_sales_and_purchase_tax_templates(accounts, template_name)
|
||||
make_item_tax_templates(accounts, template_name)
|
||||
except frappe.NameError:
|
||||
if frappe.message_log: frappe.message_log.pop()
|
||||
except RootNotEditable:
|
||||
pass
|
||||
def setup_taxes_and_charges(company_name: str, country: str):
|
||||
file_path = os.path.join(os.path.dirname(__file__), '..', 'data', 'country_wise_tax.json')
|
||||
with open(file_path, 'r') as json_file:
|
||||
tax_data = json.load(json_file)
|
||||
|
||||
def make_tax_account(company, account_name, tax_rate):
|
||||
tax_group = get_tax_account_group(company)
|
||||
if tax_group:
|
||||
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:
|
||||
if frappe.message_log: frappe.message_log.pop()
|
||||
abbr = frappe.get_cached_value('Company', company, 'abbr')
|
||||
account = '{0} - {1}'.format(account_name, abbr)
|
||||
return frappe.get_doc('Account', account)
|
||||
country_wise_tax = tax_data.get(country)
|
||||
|
||||
def make_sales_and_purchase_tax_templates(accounts, template_name=None):
|
||||
if not template_name:
|
||||
template_name = accounts[0].name
|
||||
if not country_wise_tax:
|
||||
return
|
||||
|
||||
sales_tax_template = {
|
||||
"doctype": "Sales Taxes and Charges Template",
|
||||
"title": template_name,
|
||||
"company": accounts[0].company,
|
||||
'taxes': []
|
||||
if 'chart_of_accounts' not in country_wise_tax:
|
||||
country_wise_tax = simple_to_detailed(country_wise_tax)
|
||||
|
||||
from_detailed_data(company_name, country_wise_tax.get('chart_of_accounts'))
|
||||
|
||||
|
||||
def simple_to_detailed(templates):
|
||||
"""
|
||||
Convert a simple taxes object into a more detailed data structure.
|
||||
|
||||
Example input:
|
||||
|
||||
{
|
||||
"France VAT 20%": {
|
||||
"account_name": "VAT 20%",
|
||||
"tax_rate": 20,
|
||||
"default": 1
|
||||
},
|
||||
"France VAT 10%": {
|
||||
"account_name": "VAT 10%",
|
||||
"tax_rate": 10
|
||||
}
|
||||
}
|
||||
|
||||
for account in accounts:
|
||||
sales_tax_template['taxes'].append({
|
||||
"category": "Total",
|
||||
"charge_type": "On Net Total",
|
||||
"account_head": account.name,
|
||||
"description": "{0} @ {1}".format(account.account_name, account.tax_rate),
|
||||
"rate": account.tax_rate
|
||||
})
|
||||
# Sales
|
||||
frappe.get_doc(copy.deepcopy(sales_tax_template)).insert(ignore_permissions=True)
|
||||
|
||||
# Purchase
|
||||
purchase_tax_template = copy.deepcopy(sales_tax_template)
|
||||
purchase_tax_template["doctype"] = "Purchase Taxes and Charges Template"
|
||||
|
||||
doc = frappe.get_doc(purchase_tax_template)
|
||||
doc.insert(ignore_permissions=True)
|
||||
|
||||
def make_item_tax_templates(accounts, template_name=None):
|
||||
if not template_name:
|
||||
template_name = accounts[0].name
|
||||
|
||||
item_tax_template = {
|
||||
"doctype": "Item Tax Template",
|
||||
"title": template_name,
|
||||
"company": accounts[0].company,
|
||||
'taxes': []
|
||||
"""
|
||||
return {
|
||||
'chart_of_accounts': {
|
||||
'*': {
|
||||
'item_tax_templates': [{
|
||||
'title': title,
|
||||
'taxes': [{
|
||||
'tax_type': {
|
||||
'account_name': data.get('account_name'),
|
||||
'tax_rate': data.get('tax_rate')
|
||||
}
|
||||
}]
|
||||
} for title, data in templates.items()],
|
||||
'*': [{
|
||||
'title': title,
|
||||
'is_default': data.get('default', 0),
|
||||
'taxes': [{
|
||||
'account_head': {
|
||||
'account_name': data.get('account_name'),
|
||||
'tax_rate': data.get('tax_rate')
|
||||
}
|
||||
}]
|
||||
} for title, data in templates.items()]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
for account in accounts:
|
||||
item_tax_template['taxes'].append({
|
||||
"tax_type": account.name,
|
||||
"tax_rate": account.tax_rate
|
||||
})
|
||||
def from_detailed_data(company_name, data):
|
||||
"""Create Taxes and Charges Templates from detailed data."""
|
||||
coa_name = frappe.db.get_value('Company', company_name, 'chart_of_accounts')
|
||||
tax_templates = data.get(coa_name) or data.get('*')
|
||||
sales_tax_templates = tax_templates.get('sales_tax_templates') or tax_templates.get('*')
|
||||
purchase_tax_templates = tax_templates.get('purchase_tax_templates') or tax_templates.get('*')
|
||||
item_tax_templates = tax_templates.get('item_tax_templates') or tax_templates.get('*')
|
||||
|
||||
# Items
|
||||
frappe.get_doc(copy.deepcopy(item_tax_template)).insert(ignore_permissions=True)
|
||||
if sales_tax_templates:
|
||||
for template in sales_tax_templates:
|
||||
make_taxes_and_charges_template(company_name, 'Sales Taxes and Charges Template', template)
|
||||
|
||||
def get_tax_account_group(company):
|
||||
tax_group = frappe.db.get_value("Account",
|
||||
{"account_name": "Duties and Taxes", "is_group": 1, "company": company})
|
||||
if not tax_group:
|
||||
tax_group = frappe.db.get_value("Account", {"is_group": 1, "root_type": "Liability",
|
||||
"account_type": "Tax", "company": company})
|
||||
if purchase_tax_templates:
|
||||
for template in purchase_tax_templates:
|
||||
make_taxes_and_charges_template(company_name, 'Purchase Taxes and Charges Template', template)
|
||||
|
||||
return tax_group
|
||||
if item_tax_templates:
|
||||
for template in item_tax_templates:
|
||||
make_item_tax_template(company_name, template)
|
||||
|
||||
def get_country_wise_tax(country):
|
||||
data = {}
|
||||
with open (os.path.join(os.path.dirname(__file__), "..", "data", "country_wise_tax.json")) as countrywise_tax:
|
||||
data = json.load(countrywise_tax).get(country)
|
||||
|
||||
return data
|
||||
def make_taxes_and_charges_template(company_name, doctype, template):
|
||||
template['company'] = company_name
|
||||
template['doctype'] = doctype
|
||||
|
||||
if frappe.db.exists(doctype, {'title': template.get('title'), 'company': company_name}):
|
||||
return
|
||||
|
||||
for tax_row in template.get('taxes'):
|
||||
account_data = tax_row.get('account_head')
|
||||
tax_row_defaults = {
|
||||
'category': 'Total',
|
||||
'charge_type': 'On Net Total'
|
||||
}
|
||||
|
||||
# if account_head is a dict, search or create the account and get it's name
|
||||
if isinstance(account_data, dict):
|
||||
tax_row_defaults['description'] = '{0} @ {1}'.format(account_data.get('account_name'), account_data.get('tax_rate'))
|
||||
tax_row_defaults['rate'] = account_data.get('tax_rate')
|
||||
account = get_or_create_account(company_name, account_data)
|
||||
tax_row['account_head'] = account.name
|
||||
|
||||
# use the default value if nothing other is specified
|
||||
for fieldname, default_value in tax_row_defaults.items():
|
||||
if fieldname not in tax_row:
|
||||
tax_row[fieldname] = default_value
|
||||
|
||||
return frappe.get_doc(template).insert(ignore_permissions=True)
|
||||
|
||||
|
||||
def make_item_tax_template(company_name, template):
|
||||
"""Create an Item Tax Template.
|
||||
|
||||
This requires a separate method because Item Tax Template is structured
|
||||
differently from Sales and Purchase Tax Templates.
|
||||
"""
|
||||
doctype = 'Item Tax Template'
|
||||
template['company'] = company_name
|
||||
template['doctype'] = doctype
|
||||
|
||||
if frappe.db.exists(doctype, {'title': template.get('title'), 'company': company_name}):
|
||||
return
|
||||
|
||||
for tax_row in template.get('taxes'):
|
||||
account_data = tax_row.get('tax_type')
|
||||
|
||||
# if tax_type is a dict, search or create the account and get it's name
|
||||
if isinstance(account_data, dict):
|
||||
account = get_or_create_account(company_name, account_data)
|
||||
tax_row['tax_type'] = account.name
|
||||
if 'tax_rate' not in tax_row:
|
||||
tax_row['tax_rate'] = account_data.get('tax_rate')
|
||||
|
||||
return frappe.get_doc(template).insert(ignore_permissions=True)
|
||||
|
||||
|
||||
def get_or_create_account(company_name, account):
|
||||
"""
|
||||
Check if account already exists. If not, create it.
|
||||
Return a tax account or None.
|
||||
"""
|
||||
default_root_type = 'Liability'
|
||||
root_type = account.get('root_type', default_root_type)
|
||||
|
||||
existing_accounts = frappe.get_list('Account',
|
||||
filters={
|
||||
'company': company_name,
|
||||
'root_type': root_type
|
||||
},
|
||||
or_filters={
|
||||
'account_name': account.get('account_name'),
|
||||
'account_number': account.get('account_number')
|
||||
}
|
||||
)
|
||||
|
||||
if existing_accounts:
|
||||
return frappe.get_doc('Account', existing_accounts[0].name)
|
||||
|
||||
tax_group = get_or_create_tax_group(company_name, root_type)
|
||||
|
||||
account['doctype'] = 'Account'
|
||||
account['company'] = company_name
|
||||
account['parent_account'] = tax_group
|
||||
account['report_type'] = 'Balance Sheet'
|
||||
account['account_type'] = 'Tax'
|
||||
account['root_type'] = root_type
|
||||
account['is_group'] = 0
|
||||
|
||||
return frappe.get_doc(account).insert(ignore_permissions=True, ignore_mandatory=True)
|
||||
|
||||
|
||||
def get_or_create_tax_group(company_name, root_type):
|
||||
# Look for a group account of type 'Tax'
|
||||
tax_group_name = frappe.db.get_value('Account', {
|
||||
'is_group': 1,
|
||||
'root_type': root_type,
|
||||
'account_type': 'Tax',
|
||||
'company': company_name
|
||||
})
|
||||
|
||||
if tax_group_name:
|
||||
return tax_group_name
|
||||
|
||||
# Look for a group account named 'Duties and Taxes' or 'Tax Assets'
|
||||
account_name = _('Duties and Taxes') if root_type == 'Liability' else _('Tax Assets')
|
||||
tax_group_name = frappe.db.get_value('Account', {
|
||||
'is_group': 1,
|
||||
'root_type': root_type,
|
||||
'account_name': account_name,
|
||||
'company': company_name
|
||||
})
|
||||
|
||||
if tax_group_name:
|
||||
return tax_group_name
|
||||
|
||||
# Create a new group account named 'Duties and Taxes' or 'Tax Assets' just
|
||||
# below the root account
|
||||
root_account = frappe.get_list('Account', {
|
||||
'is_group': 1,
|
||||
'root_type': root_type,
|
||||
'company': company_name,
|
||||
'report_type': 'Balance Sheet',
|
||||
'parent_account': ('is', 'not set')
|
||||
}, limit=1)[0]
|
||||
|
||||
tax_group_account = frappe.get_doc({
|
||||
'doctype': 'Account',
|
||||
'company': company_name,
|
||||
'is_group': 1,
|
||||
'report_type': 'Balance Sheet',
|
||||
'root_type': root_type,
|
||||
'account_type': 'Tax',
|
||||
'account_name': account_name,
|
||||
'parent_account': root_account.name
|
||||
}).insert(ignore_permissions=True)
|
||||
|
||||
tax_group_name = tax_group_account.name
|
||||
|
||||
return tax_group_name
|
||||
|
@ -9,5 +9,4 @@ def complete():
|
||||
'data', 'test_mfg.json'), 'r') as f:
|
||||
data = json.loads(f.read())
|
||||
|
||||
#setup_wizard.create_sales_tax(data)
|
||||
setup_complete(data)
|
||||
|
Loading…
Reference in New Issue
Block a user