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 __future__ import unicode_literals
|
||||||
|
|
||||||
from erpnext.regional.united_arab_emirates.setup import make_custom_fields, add_print_formats
|
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):
|
def setup(company=None, patch=True):
|
||||||
make_custom_fields()
|
make_custom_fields()
|
||||||
add_print_formats()
|
add_print_formats()
|
||||||
|
|
||||||
if company:
|
|
||||||
create_sales_tax(company)
|
|
@ -6,7 +6,6 @@ from __future__ import unicode_literals
|
|||||||
import frappe, os, json
|
import frappe, os, json
|
||||||
from frappe.custom.doctype.custom_field.custom_field import create_custom_fields
|
from frappe.custom.doctype.custom_field.custom_field import create_custom_fields
|
||||||
from frappe.permissions import add_permission, update_permission_property
|
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
|
from erpnext.payroll.doctype.gratuity_rule.gratuity_rule import get_gratuity_rule
|
||||||
|
|
||||||
def setup(company=None, patch=True):
|
def setup(company=None, patch=True):
|
||||||
@ -16,9 +15,6 @@ def setup(company=None, patch=True):
|
|||||||
add_permissions()
|
add_permissions()
|
||||||
create_gratuity_rule()
|
create_gratuity_rule()
|
||||||
|
|
||||||
if company:
|
|
||||||
create_sales_tax(company)
|
|
||||||
|
|
||||||
def make_custom_fields():
|
def make_custom_fields():
|
||||||
is_zero_rated = dict(fieldname='is_zero_rated', label='Is Zero Rated',
|
is_zero_rated = dict(fieldname='is_zero_rated', label='Is Zero Rated',
|
||||||
fieldtype='Check', fetch_from='item_code.is_zero_rated', insert_after='description',
|
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
|
from past.builtins import cmp
|
||||||
import functools
|
import functools
|
||||||
from erpnext.accounts.doctype.account.account import get_account_currency
|
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):
|
class Company(NestedSet):
|
||||||
nsm_parent_field = 'parent_company'
|
nsm_parent_field = 'parent_company'
|
||||||
@ -68,11 +69,7 @@ class Company(NestedSet):
|
|||||||
|
|
||||||
@frappe.whitelist()
|
@frappe.whitelist()
|
||||||
def create_default_tax_template(self):
|
def create_default_tax_template(self):
|
||||||
from erpnext.setup.setup_wizard.operations.taxes_setup import create_sales_tax
|
setup_taxes_and_charges(self.name, self.country)
|
||||||
create_sales_tax({
|
|
||||||
'country': self.country,
|
|
||||||
'company_name': self.name
|
|
||||||
})
|
|
||||||
|
|
||||||
def validate_default_accounts(self):
|
def validate_default_accounts(self):
|
||||||
accounts = [
|
accounts = [
|
||||||
|
@ -481,14 +481,250 @@
|
|||||||
},
|
},
|
||||||
|
|
||||||
"Germany": {
|
"Germany": {
|
||||||
"Germany VAT 19%": {
|
"chart_of_accounts": {
|
||||||
"account_name": "VAT 19%",
|
"SKR04 mit Kontonummern": {
|
||||||
"tax_rate": 19.00,
|
"sales_tax_templates": [
|
||||||
"default": 1
|
{
|
||||||
},
|
"title": "Umsatzsteuer 19%",
|
||||||
"Germany VAT 7%": {
|
"taxes": [
|
||||||
"account_name": "VAT 7%",
|
{
|
||||||
"tax_rate": 7.00
|
"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": {
|
"India": {
|
||||||
"In State GST": {
|
"chart_of_accounts": {
|
||||||
"account_name": ["SGST", "CGST"],
|
"*": {
|
||||||
"tax_rate": [9.00, 9.00],
|
"item_tax_templates": [
|
||||||
"default": 1
|
{
|
||||||
},
|
"title": "In State GST",
|
||||||
"Out of State GST": {
|
"taxes": [
|
||||||
"account_name": "IGST",
|
{
|
||||||
"tax_rate": 18.00
|
"tax_type": {
|
||||||
},
|
"account_name": "SGST",
|
||||||
"VAT 5%": {
|
"tax_rate": 9.00
|
||||||
"account_name": "VAT 5%",
|
}
|
||||||
"tax_rate": 5.00
|
},
|
||||||
},
|
{
|
||||||
"VAT 4%": {
|
"tax_type": {
|
||||||
"account_name": "VAT 4%",
|
"account_name": "CGST",
|
||||||
"tax_rate": 4.00
|
"tax_rate": 9.00
|
||||||
},
|
}
|
||||||
"VAT 14%": {
|
}
|
||||||
"account_name": "VAT 14%",
|
]
|
||||||
"tax_rate": 14.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
|
# License: GNU General Public License v3. See license.txt
|
||||||
|
|
||||||
from __future__ import unicode_literals
|
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):
|
import os
|
||||||
country_wise_tax = get_country_wise_tax(args.get("country"))
|
import json
|
||||||
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)
|
|
||||||
|
|
||||||
def make_tax_account_and_template(company, account_name, tax_rate, template_name=None):
|
import frappe
|
||||||
if not isinstance(account_name, (list, tuple)):
|
from frappe import _
|
||||||
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:
|
def setup_taxes_and_charges(company_name: str, country: str):
|
||||||
if accounts:
|
file_path = os.path.join(os.path.dirname(__file__), '..', 'data', 'country_wise_tax.json')
|
||||||
make_sales_and_purchase_tax_templates(accounts, template_name)
|
with open(file_path, 'r') as json_file:
|
||||||
make_item_tax_templates(accounts, template_name)
|
tax_data = json.load(json_file)
|
||||||
except frappe.NameError:
|
|
||||||
if frappe.message_log: frappe.message_log.pop()
|
|
||||||
except RootNotEditable:
|
|
||||||
pass
|
|
||||||
|
|
||||||
def make_tax_account(company, account_name, tax_rate):
|
country_wise_tax = tax_data.get(country)
|
||||||
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)
|
|
||||||
|
|
||||||
def make_sales_and_purchase_tax_templates(accounts, template_name=None):
|
if not country_wise_tax:
|
||||||
if not template_name:
|
return
|
||||||
template_name = accounts[0].name
|
|
||||||
|
|
||||||
sales_tax_template = {
|
if 'chart_of_accounts' not in country_wise_tax:
|
||||||
"doctype": "Sales Taxes and Charges Template",
|
country_wise_tax = simple_to_detailed(country_wise_tax)
|
||||||
"title": template_name,
|
|
||||||
"company": accounts[0].company,
|
from_detailed_data(company_name, country_wise_tax.get('chart_of_accounts'))
|
||||||
'taxes': []
|
|
||||||
|
|
||||||
|
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:
|
return {
|
||||||
sales_tax_template['taxes'].append({
|
'chart_of_accounts': {
|
||||||
"category": "Total",
|
'*': {
|
||||||
"charge_type": "On Net Total",
|
'item_tax_templates': [{
|
||||||
"account_head": account.name,
|
'title': title,
|
||||||
"description": "{0} @ {1}".format(account.account_name, account.tax_rate),
|
'taxes': [{
|
||||||
"rate": account.tax_rate
|
'tax_type': {
|
||||||
})
|
'account_name': data.get('account_name'),
|
||||||
# Sales
|
'tax_rate': data.get('tax_rate')
|
||||||
frappe.get_doc(copy.deepcopy(sales_tax_template)).insert(ignore_permissions=True)
|
}
|
||||||
|
}]
|
||||||
# Purchase
|
} for title, data in templates.items()],
|
||||||
purchase_tax_template = copy.deepcopy(sales_tax_template)
|
'*': [{
|
||||||
purchase_tax_template["doctype"] = "Purchase Taxes and Charges Template"
|
'title': title,
|
||||||
|
'is_default': data.get('default', 0),
|
||||||
doc = frappe.get_doc(purchase_tax_template)
|
'taxes': [{
|
||||||
doc.insert(ignore_permissions=True)
|
'account_head': {
|
||||||
|
'account_name': data.get('account_name'),
|
||||||
def make_item_tax_templates(accounts, template_name=None):
|
'tax_rate': data.get('tax_rate')
|
||||||
if not template_name:
|
}
|
||||||
template_name = accounts[0].name
|
}]
|
||||||
|
} for title, data in templates.items()]
|
||||||
item_tax_template = {
|
}
|
||||||
"doctype": "Item Tax Template",
|
}
|
||||||
"title": template_name,
|
|
||||||
"company": accounts[0].company,
|
|
||||||
'taxes': []
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
for account in accounts:
|
def from_detailed_data(company_name, data):
|
||||||
item_tax_template['taxes'].append({
|
"""Create Taxes and Charges Templates from detailed data."""
|
||||||
"tax_type": account.name,
|
coa_name = frappe.db.get_value('Company', company_name, 'chart_of_accounts')
|
||||||
"tax_rate": account.tax_rate
|
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
|
if sales_tax_templates:
|
||||||
frappe.get_doc(copy.deepcopy(item_tax_template)).insert(ignore_permissions=True)
|
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):
|
if purchase_tax_templates:
|
||||||
tax_group = frappe.db.get_value("Account",
|
for template in purchase_tax_templates:
|
||||||
{"account_name": "Duties and Taxes", "is_group": 1, "company": company})
|
make_taxes_and_charges_template(company_name, 'Purchase Taxes and Charges Template', template)
|
||||||
if not tax_group:
|
|
||||||
tax_group = frappe.db.get_value("Account", {"is_group": 1, "root_type": "Liability",
|
|
||||||
"account_type": "Tax", "company": company})
|
|
||||||
|
|
||||||
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', 'test_mfg.json'), 'r') as f:
|
||||||
data = json.loads(f.read())
|
data = json.loads(f.read())
|
||||||
|
|
||||||
#setup_wizard.create_sales_tax(data)
|
|
||||||
setup_complete(data)
|
setup_complete(data)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user