blob: 7a7e937ea455710b7be2dd65e1157f8dfeeca599 [file] [log] [blame]
Rushabh Mehtab3c8f442017-06-21 17:22:38 +05301# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
2# License: GNU General Public License v3. See license.txt
3
4from __future__ import unicode_literals
5
6import frappe, os, json
7from frappe.custom.doctype.custom_field.custom_field import create_custom_field
8from frappe.permissions import add_permission
9from erpnext.regional.india import states
10
Rushabh Mehta01659272017-06-27 18:05:17 +053011def setup(company=None, patch=True):
Rushabh Mehtab3c8f442017-06-21 17:22:38 +053012 make_custom_fields()
Rushabh Mehtab3c8f442017-06-21 17:22:38 +053013 add_permissions()
Rushabh Mehta919a74a2017-06-22 16:37:04 +053014 add_custom_roles_for_reports()
Nabin Hait1a609312017-07-13 12:16:04 +053015 add_hsn_sac_codes()
Nabin Hait852cb642017-07-05 12:58:19 +053016 add_print_formats()
Rushabh Mehta01659272017-06-27 18:05:17 +053017 if not patch:
Nabin Hait1a609312017-07-13 12:16:04 +053018 update_address_template()
Rushabh Mehta01659272017-06-27 18:05:17 +053019 make_fixtures()
Rushabh Mehta919a74a2017-06-22 16:37:04 +053020
21def update_address_template():
22 with open(os.path.join(os.path.dirname(__file__), 'address_template.html'), 'r') as f:
23 html = f.read()
24
25 address_template = frappe.db.get_value('Address Template', 'India')
26 if address_template:
27 frappe.db.set_value('Address Template', 'India', 'template', html)
28 else:
29 # make new html template for India
Rushabh Mehta9b09ff22017-06-27 12:17:39 +053030 frappe.get_doc(dict(
Rushabh Mehta919a74a2017-06-22 16:37:04 +053031 doctype='Address Template',
32 country='India',
33 template=html
Rushabh Mehta9b09ff22017-06-27 12:17:39 +053034 )).insert()
Rushabh Mehtab3c8f442017-06-21 17:22:38 +053035
Nabin Hait1a609312017-07-13 12:16:04 +053036def add_hsn_sac_codes():
37 # HSN codes
Rushabh Mehtab3c8f442017-06-21 17:22:38 +053038 with open(os.path.join(os.path.dirname(__file__), 'hsn_code_data.json'), 'r') as f:
39 hsn_codes = json.loads(f.read())
40
Nabin Hait1a609312017-07-13 12:16:04 +053041 create_hsn_codes(hsn_codes, code_field="hsn_code")
42
43 # SAC Codes
44 with open(os.path.join(os.path.dirname(__file__), 'sac_code_data.json'), 'r') as f:
45 sac_codes = json.loads(f.read())
46 create_hsn_codes(sac_codes, code_field="sac_code")
47
48def create_hsn_codes(data, code_field):
49 for d in data:
50 if not frappe.db.exists("GST HSN Code", d[code_field]):
51 hsn_code = frappe.new_doc('GST HSN Code')
52 hsn_code.description = d["description"]
53 hsn_code.hsn_code = d[code_field]
54 hsn_code.name = d[code_field]
55 hsn_code.db_insert()
Rushabh Mehtab3c8f442017-06-21 17:22:38 +053056
Rushabh Mehta919a74a2017-06-22 16:37:04 +053057 frappe.db.commit()
58
59def add_custom_roles_for_reports():
60 for report_name in ('GST Sales Register', 'GST Purchase Register',
61 'GST Itemised Sales Register', 'GST Itemised Purchase Register'):
62
Rushabh Mehta919a74a2017-06-22 16:37:04 +053063 if not frappe.db.get_value('Custom Role', dict(report=report_name)):
64 frappe.get_doc(dict(
65 doctype='Custom Role',
66 report=report_name,
67 roles= [
68 dict(role='Accounts User'),
69 dict(role='Accounts Manager')
70 ]
71 )).insert()
72
Rushabh Mehtab3c8f442017-06-21 17:22:38 +053073def add_permissions():
Rushabh Mehta00ae4242017-06-27 17:31:41 +053074 for doctype in ('GST HSN Code', 'GST Settings'):
Rushabh Mehtab3c8f442017-06-21 17:22:38 +053075 add_permission(doctype, 'Accounts Manager', 0)
76 add_permission(doctype, 'All', 0)
77
Nabin Hait852cb642017-07-05 12:58:19 +053078def add_print_formats():
79 frappe.reload_doc("regional", "print_format", "gst_tax_invoice")
80
Rushabh Mehtab3c8f442017-06-21 17:22:38 +053081def make_custom_fields():
Nabin Haitf3f0dfe2017-07-06 14:49:34 +053082 hsn_sac_field = dict(fieldname='gst_hsn_code', label='HSN/SAC',
Nabin Haitb962fc12017-07-17 18:02:31 +053083 fieldtype='Data', options='item_code.gst_hsn_code', insert_after='description', print_hide=1)
Nabin Haitf3f0dfe2017-07-06 14:49:34 +053084
Rushabh Mehtab3c8f442017-06-21 17:22:38 +053085 custom_fields = {
86 'Address': [
Rushabh Mehta919a74a2017-06-22 16:37:04 +053087 dict(fieldname='gstin', label='Party GSTIN', fieldtype='Data',
88 insert_after='fax'),
Rushabh Mehtab3c8f442017-06-21 17:22:38 +053089 dict(fieldname='gst_state', label='GST State', fieldtype='Select',
Nabin Haitf3f0dfe2017-07-06 14:49:34 +053090 options='\n'.join(states), insert_after='gstin'),
91 dict(fieldname='gst_state_number', label='GST State Number',
Nabin Hait1b363362017-07-07 14:05:33 +053092 fieldtype='Int', insert_after='gst_state', read_only=1),
Rushabh Mehtab3c8f442017-06-21 17:22:38 +053093 ],
94 'Purchase Invoice': [
95 dict(fieldname='supplier_gstin', label='Supplier GSTIN',
Rushabh Mehta919a74a2017-06-22 16:37:04 +053096 fieldtype='Data', insert_after='supplier_address',
Nabin Hait852cb642017-07-05 12:58:19 +053097 options='supplier_address.gstin', print_hide=1),
Rushabh Mehtab3c8f442017-06-21 17:22:38 +053098 dict(fieldname='company_gstin', label='Company GSTIN',
Rushabh Mehta919a74a2017-06-22 16:37:04 +053099 fieldtype='Data', insert_after='shipping_address',
Nabin Hait852cb642017-07-05 12:58:19 +0530100 options='shipping_address.gstin', print_hide=1),
Rushabh Mehtab3c8f442017-06-21 17:22:38 +0530101 ],
102 'Sales Invoice': [
103 dict(fieldname='customer_gstin', label='Customer GSTIN',
Rushabh Mehta919a74a2017-06-22 16:37:04 +0530104 fieldtype='Data', insert_after='shipping_address',
Nabin Hait852cb642017-07-05 12:58:19 +0530105 options='shipping_address_name.gstin', print_hide=1),
Rushabh Mehtab3c8f442017-06-21 17:22:38 +0530106 dict(fieldname='company_gstin', label='Company GSTIN',
Rushabh Mehta919a74a2017-06-22 16:37:04 +0530107 fieldtype='Data', insert_after='company_address',
Nabin Hait852cb642017-07-05 12:58:19 +0530108 options='company_address.gstin', print_hide=1),
Nabin Haitf3f0dfe2017-07-06 14:49:34 +0530109 dict(fieldname='invoice_copy', label='Invoice Copy',
Nabin Hait9c421612017-07-20 13:32:01 +0530110 fieldtype='Select', insert_after='select_print_heading', print_hide=1, allow_on_submit=1,
111 options='Original for Recipient\nDuplicate for Transporter\nDuplicate for Supplier\nTriplicate for Supplier')
Rushabh Mehtab3c8f442017-06-21 17:22:38 +0530112 ],
113 'Item': [
Nabin Haitf3f0dfe2017-07-06 14:49:34 +0530114 dict(fieldname='gst_hsn_code', label='HSN/SAC',
Rushabh Mehtab3c8f442017-06-21 17:22:38 +0530115 fieldtype='Link', options='GST HSN Code', insert_after='item_group'),
116 ],
Nabin Hait9c421612017-07-20 13:32:01 +0530117 'Quotation Item': [hsn_sac_field],
118 'Supplier Quotation Item': [hsn_sac_field],
Nabin Haitf3f0dfe2017-07-06 14:49:34 +0530119 'Sales Order Item': [hsn_sac_field],
120 'Delivery Note Item': [hsn_sac_field],
121 'Sales Invoice Item': [hsn_sac_field],
122 'Purchase Order Item': [hsn_sac_field],
123 'Purchase Receipt Item': [hsn_sac_field],
124 'Purchase Invoice Item': [hsn_sac_field]
Rushabh Mehtab3c8f442017-06-21 17:22:38 +0530125 }
126
127 for doctype, fields in custom_fields.items():
128 for df in fields:
Nabin Hait1a609312017-07-13 12:16:04 +0530129 field = frappe.db.get_value("Custom Field", {"dt": doctype, "fieldname": df["fieldname"]})
130 if not field:
131 create_custom_field(doctype, df)
132 else:
133 custom_field = frappe.get_doc("Custom Field", field)
134 custom_field.update(df)
135 custom_field.save()
Nabin Hait9c421612017-07-20 13:32:01 +0530136
Rushabh Mehtab3c8f442017-06-21 17:22:38 +0530137def make_fixtures():
138 docs = [
139 {'doctype': 'Salary Component', 'salary_component': 'Professional Tax', 'description': 'Professional Tax', 'type': 'Deduction'},
140 {'doctype': 'Salary Component', 'salary_component': 'Provident Fund', 'description': 'Provident fund', 'type': 'Deduction'},
141 {'doctype': 'Salary Component', 'salary_component': 'House Rent Allowance', 'description': 'House Rent Allowance', 'type': 'Earning'},
142 {'doctype': 'Salary Component', 'salary_component': 'Basic', 'description': 'Basic', 'type': 'Earning'},
143 {'doctype': 'Salary Component', 'salary_component': 'Arrear', 'description': 'Arrear', 'type': 'Earning'},
144 {'doctype': 'Salary Component', 'salary_component': 'Leave Encashment', 'description': 'Leave Encashment', 'type': 'Earning'}
145 ]
146
147 for d in docs:
148 try:
149 doc = frappe.get_doc(d)
150 doc.flags.ignore_permissions = True
151 doc.insert()
152 except frappe.NameError:
153 pass