Merge branch 'version-13-hotfix' into item-tax-templates
diff --git a/erpnext/setup/setup_wizard/data/country_wise_tax.json b/erpnext/setup/setup_wizard/data/country_wise_tax.json
index 5876488..ec9a6d6 100644
--- a/erpnext/setup/setup_wizard/data/country_wise_tax.json
+++ b/erpnext/setup/setup_wizard/data/country_wise_tax.json
@@ -481,37 +481,42 @@
 	},
 
 	"Germany": {
+		"tax_categories": [
+			"Umsatzsteuer",
+			"Vorsteuer"
+		],
 		"chart_of_accounts": {
 			"SKR04 mit Kontonummern": {
 				"sales_tax_templates": [
 					{
-						"title": "Umsatzsteuer 19%",
+						"title": "Umsatzsteuer",
+						"tax_category": "Umsatzsteuer",
+						"is_default": 1,
 						"taxes": [
 							{
 								"account_head": {
 									"account_name": "Umsatzsteuer 19%",
 									"account_number": "3806",
 									"tax_rate": 19.00
-								}
-							}
-						]
-					},
-					{
-						"title": "Umsatzsteuer 7%",
-						"taxes": [
+								},
+								"rate": 0.00
+							},
 							{
 								"account_head": {
 									"account_name": "Umsatzsteuer 7%",
 									"account_number": "3801",
 									"tax_rate": 7.00
-								}
+								},
+								"rate": 0.00
 							}
 						]
 					}
 				],
 				"purchase_tax_templates": [
 					{
-						"title": "Abziehbare Vorsteuer 19%",
+						"title": "Vorsteuer",
+						"tax_category": "Vorsteuer",
+						"is_default": 1,
 						"taxes": [
 							{
 								"account_head": {
@@ -519,20 +524,17 @@
 									"account_number": "1406",
 									"root_type": "Asset",
 									"tax_rate": 19.00
-								}
-							}
-						]
-					},
-					{
-						"title": "Abziehbare Vorsteuer 7%",
-						"taxes": [
+								},
+								"rate": 0.00
+							},
 							{
 								"account_head": {
 									"account_name": "Abziehbare Vorsteuer 7%",
 									"account_number": "1401",
 									"root_type": "Asset",
 									"tax_rate": 7.00
-								}
+								},
+								"rate": 0.00
 							}
 						]
 					},
@@ -559,19 +561,26 @@
 							}
 						]
 					}
-				]
-			},
-			"SKR03 mit Kontonummern": {
-				"sales_tax_templates": [
+				],
+				"item_tax_templates": [
 					{
 						"title": "Umsatzsteuer 19%",
 						"taxes": [
 							{
-								"account_head": {
+								"tax_type": {
 									"account_name": "Umsatzsteuer 19%",
-									"account_number": "1776",
+									"account_number": "3806",
 									"tax_rate": 19.00
-								}
+								},
+								"tax_rate": 19.00
+							},
+							{
+								"tax_type": {
+									"account_name": "Umsatzsteuer 7%",
+									"account_number": "3801",
+									"tax_rate": 7.00
+								},
+								"tax_rate": 0.00
 							}
 						]
 					},
@@ -579,18 +588,102 @@
 						"title": "Umsatzsteuer 7%",
 						"taxes": [
 							{
+								"tax_type": {
+									"account_name": "Umsatzsteuer 19%",
+									"account_number": "3806",
+									"tax_rate": 19.00
+								},
+								"tax_rate": 0.00
+							},
+							{
+								"tax_type": {
+									"account_name": "Umsatzsteuer 7%",
+									"account_number": "3801",
+									"tax_rate": 7.00
+								},
+								"tax_rate": 7.00
+							}
+						]
+					},
+					{
+						"title": "Vorsteuer 19%",
+						"taxes": [
+							{
+								"tax_type": {
+									"account_name": "Abziehbare Vorsteuer 19%",
+									"account_number": "1406",
+									"root_type": "Asset",
+									"tax_rate": 19.00
+								},
+								"tax_rate": 19.00
+							},
+							{
+								"tax_type": {
+									"account_name": "Abziehbare Vorsteuer 7%",
+									"account_number": "1401",
+									"root_type": "Asset",
+									"tax_rate": 7.00
+								},
+								"tax_rate": 0.00
+							}
+						]
+					},
+					{
+						"title": "Vorsteuer 7%",
+						"taxes": [
+							{
+								"tax_type": {
+									"account_name": "Abziehbare Vorsteuer 19%",
+									"account_number": "1406",
+									"root_type": "Asset",
+									"tax_rate": 19.00
+								},
+								"tax_rate": 0.00
+							},
+							{
+								"tax_type": {
+									"account_name": "Abziehbare Vorsteuer 7%",
+									"account_number": "1401",
+									"root_type": "Asset",
+									"tax_rate": 7.00
+								},
+								"tax_rate": 7.00
+							}
+						]
+					}
+				]
+			},
+			"SKR03 mit Kontonummern": {
+				"sales_tax_templates": [
+					{
+						"title": "Umsatzsteuer",
+						"tax_category": "Umsatzsteuer",
+						"is_default": 1,
+						"taxes": [
+							{
+								"account_head": {
+									"account_name": "Umsatzsteuer 19%",
+									"account_number": "1776",
+									"tax_rate": 19.00
+								},
+								"rate": 0.00
+							},
+							{
 								"account_head": {
 									"account_name": "Umsatzsteuer 7%",
 									"account_number": "1771",
 									"tax_rate": 7.00
-								}
+								},
+								"rate": 0.00
 							}
 						]
 					}
 				],
 				"purchase_tax_templates": [
 					{
-						"title": "Abziehbare Vorsteuer 19%",
+						"title": "Vorsteuer",
+						"tax_category": "Vorsteuer",
+						"is_default": 1,
 						"taxes": [
 							{
 								"account_head": {
@@ -598,20 +691,107 @@
 									"account_number": "1576",
 									"root_type": "Asset",
 									"tax_rate": 19.00
-								}
-							}
-						]
-					},
-					{
-						"title": "Abziehbare Vorsteuer 7%",
-						"taxes": [
+								},
+								"rate": 0.00
+							},
 							{
 								"account_head": {
 									"account_name": "Abziehbare Vorsteuer 7%",
 									"account_number": "1571",
 									"root_type": "Asset",
 									"tax_rate": 7.00
-								}
+								},
+								"rate": 0.00
+							}
+						]
+					}
+				],
+				"item_tax_templates": [
+					{
+						"title": "Umsatzsteuer 19%",
+						"taxes": [
+							{
+								"tax_type": {
+									"account_name": "Umsatzsteuer 19%",
+									"account_number": "1776",
+									"tax_rate": 19.00
+								},
+								"tax_rate": 19.00
+							},
+							{
+								"tax_type": {
+									"account_name": "Umsatzsteuer 7%",
+									"account_number": "1771",
+									"tax_rate": 7.00
+								},
+								"tax_rate": 0.00
+							}
+						]
+					},
+					{
+						"title": "Umsatzsteuer 7%",
+						"taxes": [
+							{
+								"tax_type": {
+									"account_name": "Umsatzsteuer 19%",
+									"account_number": "1776",
+									"tax_rate": 19.00
+								},
+								"tax_rate": 0.00
+							},
+							{
+								"tax_type": {
+									"account_name": "Umsatzsteuer 7%",
+									"account_number": "1771",
+									"tax_rate": 7.00
+								},
+								"tax_rate": 7.00
+							}
+						]
+					},
+					{
+						"title": "Vorsteuer 19%",
+						"taxes": [
+							{
+								"tax_type": {
+									"account_name": "Abziehbare Vorsteuer 19%",
+									"account_number": "1576",
+									"root_type": "Asset",
+									"tax_rate": 19.00
+								},
+								"tax_rate": 19.00
+							},
+							{
+								"tax_type": {
+									"account_name": "Abziehbare Vorsteuer 7%",
+									"account_number": "1571",
+									"root_type": "Asset",
+									"tax_rate": 7.00
+								},
+								"tax_rate": 0.00
+							}
+						]
+					},
+					{
+						"title": "Vorsteuer 7%",
+						"taxes": [
+							{
+								"tax_type": {
+									"account_name": "Abziehbare Vorsteuer 19%",
+									"account_number": "1576",
+									"root_type": "Asset",
+									"tax_rate": 19.00
+								},
+								"tax_rate": 0.00
+							},
+							{
+								"tax_type": {
+									"account_name": "Abziehbare Vorsteuer 7%",
+									"account_number": "1571",
+									"root_type": "Asset",
+									"tax_rate": 7.00
+								},
+								"tax_rate": 7.00
 							}
 						]
 					}
@@ -620,33 +800,34 @@
 			"Standard with Numbers": {
 				"sales_tax_templates": [
 					{
-						"title": "Umsatzsteuer 19%",
+						"title": "Umsatzsteuer",
+						"tax_category": "Umsatzsteuer",
+						"is_default": 1,
 						"taxes": [
 							{
 								"account_head": {
 									"account_name": "Umsatzsteuer 19%",
 									"account_number": "2301",
 									"tax_rate": 19.00
-								}
-							}
-						]
-					},
-					{
-						"title": "Umsatzsteuer 7%",
-						"taxes": [
+								},
+								"rate": 0.00
+							},
 							{
 								"account_head": {
 									"account_name": "Umsatzsteuer 7%",
 									"account_number": "2302",
 									"tax_rate": 7.00
-								}
+								},
+								"rate": 0.00
 							}
 						]
 					}
 				],
 				"purchase_tax_templates": [
 					{
-						"title": "Abziehbare Vorsteuer 19%",
+						"title": "Vorsteuer",
+						"tax_category": "Vorsteuer",
+						"is_default": 1,
 						"taxes": [
 							{
 								"account_head": {
@@ -654,20 +835,107 @@
 									"account_number": "1501",
 									"root_type": "Asset",
 									"tax_rate": 19.00
-								}
-							}
-						]
-					},
-					{
-						"title": "Abziehbare Vorsteuer 7%",
-						"taxes": [
+								},
+								"rate": 0.00
+							},
 							{
 								"account_head": {
 									"account_name": "Abziehbare Vorsteuer 7%",
 									"account_number": "1502",
 									"root_type": "Asset",
 									"tax_rate": 7.00
-								}
+								},
+								"rate": 0.00
+							}
+						]
+					}
+				],
+				"item_tax_templates": [
+					{
+						"title": "Umsatzsteuer 19%",
+						"taxes": [
+							{
+								"tax_type": {
+									"account_name": "Umsatzsteuer 19%",
+									"account_number": "2301",
+									"tax_rate": 19.00
+								},
+								"tax_rate": 19.00
+							},
+							{
+								"tax_type": {
+									"account_name": "Umsatzsteuer 7%",
+									"account_number": "2302",
+									"tax_rate": 7.00
+								},
+								"tax_rate": 0.00
+							}
+						]
+					},
+					{
+						"title": "Umsatzsteuer 7%",
+						"taxes": [
+							{
+								"tax_type": {
+									"account_name": "Umsatzsteuer 19%",
+									"account_number": "2301",
+									"tax_rate": 19.00
+								},
+								"tax_rate": 0.00
+							},
+							{
+								"tax_type": {
+									"account_name": "Umsatzsteuer 7%",
+									"account_number": "2302",
+									"tax_rate": 7.00
+								},
+								"tax_rate": 7.00
+							}
+						]
+					},
+					{
+						"title": "Vorsteuer 19%",
+						"taxes": [
+							{
+								"tax_type": {
+									"account_name": "Abziehbare Vorsteuer 19%",
+									"account_number": "1501",
+									"root_type": "Asset",
+									"tax_rate": 19.00
+								},
+								"tax_rate": 19.00
+							},
+							{
+								"tax_type": {
+									"account_name": "Abziehbare Vorsteuer 7%",
+									"account_number": "1502",
+									"root_type": "Asset",
+									"tax_rate": 7.00
+								},
+								"tax_rate": 0.00
+							}
+						]
+					},
+					{
+						"title": "Vorsteuer 7%",
+						"taxes": [
+							{
+								"tax_type": {
+									"account_name": "Abziehbare Vorsteuer 19%",
+									"account_number": "1501",
+									"root_type": "Asset",
+									"tax_rate": 19.00
+								},
+								"tax_rate": 0.00
+							},
+							{
+								"tax_type": {
+									"account_name": "Abziehbare Vorsteuer 7%",
+									"account_number": "1502",
+									"root_type": "Asset",
+									"tax_rate": 7.00
+								},
+								"tax_rate": 7.00
 							}
 						]
 					}
@@ -676,13 +944,69 @@
 			"*": {
 				"sales_tax_templates": [
 					{
-						"title": "Umsatzsteuer 19%",
+						"title": "Umsatzsteuer",
+						"tax_category": "Umsatzsteuer",
+						"is_default": 1,
 						"taxes": [
 							{
 								"account_head": {
 									"account_name": "Umsatzsteuer 19%",
 									"tax_rate": 19.00
-								}
+								},
+								"rate": 0.00
+							},
+							{
+								"account_head": {
+									"account_name": "Umsatzsteuer 7%",
+									"tax_rate": 7.00
+								},
+								"rate": 0.00
+							}
+						]
+					}
+				],
+				"purchase_tax_templates": [
+					{
+						"title": "Vorsteuer 19%",
+						"tax_category": "Vorsteuer",
+						"is_default": 1,
+						"taxes": [
+							{
+								"account_head": {
+									"account_name": "Abziehbare Vorsteuer 19%",
+									"tax_rate": 19.00,
+									"root_type": "Asset"
+								},
+								"rate": 0.00
+							},
+							{
+								"account_head": {
+									"account_name": "Abziehbare Vorsteuer 7%",
+									"root_type": "Asset",
+									"tax_rate": 7.00
+								},
+								"rate": 0.00
+							}
+						]
+					}
+				],
+				"item_tax_templates": [
+					{
+						"title": "Umsatzsteuer 19%",
+						"taxes": [
+							{
+								"tax_type": {
+									"account_name": "Umsatzsteuer 19%",
+									"tax_rate": 19.00
+								},
+								"tax_rate": 19.00
+							},
+							{
+								"tax_type": {
+									"account_name": "Umsatzsteuer 7%",
+									"tax_rate": 7.00
+								},
+								"tax_rate": 0.00
 							}
 						]
 					},
@@ -690,36 +1014,60 @@
 						"title": "Umsatzsteuer 7%",
 						"taxes": [
 							{
-								"account_head": {
+								"tax_type": {
+									"account_name": "Umsatzsteuer 19%",
+									"tax_rate": 19.00
+								},
+								"tax_rate": 0.00
+							},
+							{
+								"tax_type": {
 									"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"
-								}
+								},
+								"tax_rate": 7.00
 							}
 						]
 					},
 					{
-						"title": "Abziehbare Vorsteuer 7%",
+						"title": "Vorsteuer 19%",
 						"taxes": [
 							{
-								"account_head": {
+								"tax_type": {
+									"account_name": "Abziehbare Vorsteuer 19%",
+									"root_type": "Asset",
+									"tax_rate": 19.00
+								},
+								"tax_rate": 19.00
+							},
+							{
+								"tax_type": {
 									"account_name": "Abziehbare Vorsteuer 7%",
 									"root_type": "Asset",
 									"tax_rate": 7.00
-								}
+								},
+								"tax_rate": 0.00
+							}
+						]
+					},
+					{
+						"title": "Vorsteuer 7%",
+						"taxes": [
+							{
+								"tax_type": {
+									"account_name": "Abziehbare Vorsteuer 19%",
+									"root_type": "Asset",
+									"tax_rate": 19.00
+								},
+								"tax_rate": 0.00
+							},
+							{
+								"tax_type": {
+									"account_name": "Abziehbare Vorsteuer 7%",
+									"root_type": "Asset",
+									"tax_rate": 7.00
+								},
+								"tax_rate": 7.00
 							}
 						]
 					}
diff --git a/erpnext/setup/setup_wizard/operations/taxes_setup.py b/erpnext/setup/setup_wizard/operations/taxes_setup.py
index 5019837..f4fe18e 100644
--- a/erpnext/setup/setup_wizard/operations/taxes_setup.py
+++ b/erpnext/setup/setup_wizard/operations/taxes_setup.py
@@ -11,6 +11,9 @@
 
 
 def setup_taxes_and_charges(company_name: str, country: str):
+	if not frappe.db.exists('Company', company_name):
+		frappe.throw(_('Company {} does not exist yet. Taxes setup aborted.').format(company_name))
+
 	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)
@@ -23,7 +26,7 @@
 	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'))
+	from_detailed_data(company_name, country_wise_tax)
 
 
 def simple_to_detailed(templates):
@@ -74,10 +77,16 @@
 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('*')
+	coa_data = data.get('chart_of_accounts', {})
+	tax_templates = coa_data.get(coa_name) or coa_data.get('*', {})
+	tax_categories = data.get('tax_categories')
+	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('*', {})
+
+	if tax_categories:
+		for tax_category in tax_categories:
+			make_tax_catgory(tax_category)
 
 	if sales_tax_templates:
 		for template in sales_tax_templates:
@@ -233,3 +242,14 @@
 	tax_group_name = tax_group_account.name
 
 	return tax_group_name
+
+
+def make_tax_catgory(tax_category):
+	doctype = 'Tax Category'
+	if isinstance(tax_category, str):
+		tax_category = {'title': tax_category}
+
+	tax_category['doctype'] = doctype
+	if not frappe.db.exists(doctype, tax_category['title']):
+		doc = frappe.get_doc(tax_category)
+		doc.insert(ignore_permissions=True)