Tyler Matteson | 53a6492 | 2019-01-17 14:04:01 -0500 | [diff] [blame] | 1 | # Copyright (c) 2018, Frappe Technologies Pvt. Ltd. and Contributors |
| 2 | # License: GNU General Public License v3. See license.txt |
| 3 | |
| 4 | from __future__ import unicode_literals |
| 5 | import frappe |
Subin Tom | 7004944 | 2021-08-31 18:33:16 +0530 | [diff] [blame] | 6 | import os |
| 7 | import json |
| 8 | from frappe.permissions import add_permission, update_permission_property |
Tyler Matteson | 53a6492 | 2019-01-17 14:04:01 -0500 | [diff] [blame] | 9 | from frappe.custom.doctype.custom_field.custom_field import create_custom_fields |
| 10 | |
Tyler Matteson | 53a6492 | 2019-01-17 14:04:01 -0500 | [diff] [blame] | 11 | def setup(company=None, patch=True): |
Subin Tom | 7004944 | 2021-08-31 18:33:16 +0530 | [diff] [blame] | 12 | # Company independent fixtures should be called only once at the first company setup |
| 13 | if frappe.db.count('Company', {'country': 'United States'}) <=1: |
| 14 | setup_company_independent_fixtures(patch=patch) |
| 15 | |
| 16 | def setup_company_independent_fixtures(company=None, patch=True): |
| 17 | add_product_tax_categories() |
Tyler Matteson | 53a6492 | 2019-01-17 14:04:01 -0500 | [diff] [blame] | 18 | make_custom_fields() |
Subin Tom | 7004944 | 2021-08-31 18:33:16 +0530 | [diff] [blame] | 19 | add_permissions() |
| 20 | frappe.enqueue('erpnext.regional.united_states.setup.add_product_tax_categories', now=False) |
Tyler Matteson | 53a6492 | 2019-01-17 14:04:01 -0500 | [diff] [blame] | 21 | add_print_formats() |
Tyler Matteson | 53a6492 | 2019-01-17 14:04:01 -0500 | [diff] [blame] | 22 | |
Subin Tom | 7004944 | 2021-08-31 18:33:16 +0530 | [diff] [blame] | 23 | # Product Tax categories imported from taxjar api |
| 24 | def add_product_tax_categories(): |
| 25 | with open(os.path.join(os.path.dirname(__file__), 'product_tax_category_data.json'), 'r') as f: |
| 26 | tax_categories = json.loads(f.read()) |
| 27 | create_tax_categories(tax_categories['categories']) |
| 28 | |
| 29 | def create_tax_categories(data): |
| 30 | for d in data: |
| 31 | tax_category = frappe.new_doc('Product Tax Category') |
| 32 | tax_category.description = d.get("description") |
| 33 | tax_category.product_tax_code = d.get("product_tax_code") |
| 34 | tax_category.category_name = d.get("name") |
| 35 | try: |
| 36 | tax_category.db_insert() |
| 37 | except frappe.DuplicateEntryError: |
| 38 | pass |
| 39 | |
| 40 | |
Deepesh Garg | 9df4532 | 2020-06-11 21:33:43 +0530 | [diff] [blame] | 41 | def make_custom_fields(update=True): |
Tyler Matteson | 53a6492 | 2019-01-17 14:04:01 -0500 | [diff] [blame] | 42 | custom_fields = { |
| 43 | 'Supplier': [ |
| 44 | dict(fieldname='irs_1099', fieldtype='Check', insert_after='tax_id', |
| 45 | label='Is IRS 1099 reporting required for supplier?') |
vishdha | d3ec1c1 | 2020-03-24 11:31:41 +0530 | [diff] [blame] | 46 | ], |
| 47 | 'Sales Order': [ |
| 48 | dict(fieldname='exempt_from_sales_tax', fieldtype='Check', insert_after='taxes_and_charges', |
| 49 | label='Is customer exempted from sales tax?') |
| 50 | ], |
| 51 | 'Sales Invoice': [ |
| 52 | dict(fieldname='exempt_from_sales_tax', fieldtype='Check', insert_after='taxes_section', |
| 53 | label='Is customer exempted from sales tax?') |
| 54 | ], |
| 55 | 'Customer': [ |
| 56 | dict(fieldname='exempt_from_sales_tax', fieldtype='Check', insert_after='represents_company', |
| 57 | label='Is customer exempted from sales tax?') |
| 58 | ], |
| 59 | 'Quotation': [ |
| 60 | dict(fieldname='exempt_from_sales_tax', fieldtype='Check', insert_after='taxes_and_charges', |
| 61 | label='Is customer exempted from sales tax?') |
Subin Tom | 7004944 | 2021-08-31 18:33:16 +0530 | [diff] [blame] | 62 | ], |
| 63 | 'Sales Invoice Item': [ |
| 64 | dict(fieldname='product_tax_category', fieldtype='Link', insert_after='description', options='Product Tax Category', |
| 65 | label='Product Tax Category', fetch_from='item_code.product_tax_category'), |
Ankush Menat | b147b85 | 2021-09-01 16:45:57 +0530 | [diff] [blame] | 66 | dict(fieldname='tax_collectable', fieldtype='Currency', insert_after='net_amount', |
Subin Tom | 7004944 | 2021-08-31 18:33:16 +0530 | [diff] [blame] | 67 | label='Tax Collectable', read_only=1), |
Ankush Menat | b147b85 | 2021-09-01 16:45:57 +0530 | [diff] [blame] | 68 | dict(fieldname='taxable_amount', fieldtype='Currency', insert_after='tax_collectable', |
Subin Tom | 7004944 | 2021-08-31 18:33:16 +0530 | [diff] [blame] | 69 | label='Taxable Amount', read_only=1) |
| 70 | ], |
| 71 | 'Item': [ |
| 72 | dict(fieldname='product_tax_category', fieldtype='Link', insert_after='item_group', options='Product Tax Category', |
| 73 | label='Product Tax Category') |
Tyler Matteson | 53a6492 | 2019-01-17 14:04:01 -0500 | [diff] [blame] | 74 | ] |
| 75 | } |
Deepesh Garg | 9df4532 | 2020-06-11 21:33:43 +0530 | [diff] [blame] | 76 | create_custom_fields(custom_fields, update=update) |
Tyler Matteson | 53a6492 | 2019-01-17 14:04:01 -0500 | [diff] [blame] | 77 | |
Subin Tom | 7004944 | 2021-08-31 18:33:16 +0530 | [diff] [blame] | 78 | def add_permissions(): |
| 79 | doctype = "Product Tax Category" |
| 80 | for role in ('Accounts Manager', 'Accounts User', 'System Manager','Item Manager', 'Stock Manager'): |
| 81 | add_permission(doctype, role, 0) |
| 82 | update_permission_property(doctype, role, 0, 'write', 1) |
| 83 | update_permission_property(doctype, role, 0, 'create', 1) |
| 84 | |
Tyler Matteson | 53a6492 | 2019-01-17 14:04:01 -0500 | [diff] [blame] | 85 | def add_print_formats(): |
| 86 | frappe.reload_doc("regional", "print_format", "irs_1099_form") |
barredterra | 1521b31 | 2021-03-03 12:33:48 +0100 | [diff] [blame] | 87 | frappe.db.set_value("Print Format", "IRS 1099 Form", "disabled", 0) |