Rushabh Mehta | b3c8f44 | 2017-06-21 17:22:38 +0530 | [diff] [blame] | 1 | # Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors |
| 2 | # License: GNU General Public License v3. See license.txt |
| 3 | |
| 4 | from __future__ import unicode_literals |
| 5 | |
| 6 | import frappe, os, json |
| 7 | from frappe.custom.doctype.custom_field.custom_field import create_custom_field |
| 8 | from frappe.permissions import add_permission |
| 9 | from erpnext.regional.india import states |
| 10 | |
Rushabh Mehta | 0165927 | 2017-06-27 18:05:17 +0530 | [diff] [blame] | 11 | def setup(company=None, patch=True): |
Rushabh Mehta | b3c8f44 | 2017-06-21 17:22:38 +0530 | [diff] [blame] | 12 | make_custom_fields() |
Rushabh Mehta | b3c8f44 | 2017-06-21 17:22:38 +0530 | [diff] [blame] | 13 | add_permissions() |
Rushabh Mehta | 919a74a | 2017-06-22 16:37:04 +0530 | [diff] [blame] | 14 | add_custom_roles_for_reports() |
Rushabh Mehta | b3c8f44 | 2017-06-21 17:22:38 +0530 | [diff] [blame] | 15 | add_hsn_codes() |
Rushabh Mehta | 919a74a | 2017-06-22 16:37:04 +0530 | [diff] [blame] | 16 | update_address_template() |
Rushabh Mehta | 0165927 | 2017-06-27 18:05:17 +0530 | [diff] [blame] | 17 | if not patch: |
| 18 | make_fixtures() |
Rushabh Mehta | 919a74a | 2017-06-22 16:37:04 +0530 | [diff] [blame] | 19 | |
| 20 | def update_address_template(): |
| 21 | with open(os.path.join(os.path.dirname(__file__), 'address_template.html'), 'r') as f: |
| 22 | html = f.read() |
| 23 | |
| 24 | address_template = frappe.db.get_value('Address Template', 'India') |
| 25 | if address_template: |
| 26 | frappe.db.set_value('Address Template', 'India', 'template', html) |
| 27 | else: |
| 28 | # make new html template for India |
Rushabh Mehta | 9b09ff2 | 2017-06-27 12:17:39 +0530 | [diff] [blame] | 29 | frappe.get_doc(dict( |
Rushabh Mehta | 919a74a | 2017-06-22 16:37:04 +0530 | [diff] [blame] | 30 | doctype='Address Template', |
| 31 | country='India', |
| 32 | template=html |
Rushabh Mehta | 9b09ff2 | 2017-06-27 12:17:39 +0530 | [diff] [blame] | 33 | )).insert() |
Rushabh Mehta | b3c8f44 | 2017-06-21 17:22:38 +0530 | [diff] [blame] | 34 | |
| 35 | def add_hsn_codes(): |
Rushabh Mehta | 919a74a | 2017-06-22 16:37:04 +0530 | [diff] [blame] | 36 | if frappe.db.count('GST HSN Code') > 100: |
| 37 | return |
| 38 | |
Rushabh Mehta | b3c8f44 | 2017-06-21 17:22:38 +0530 | [diff] [blame] | 39 | with open(os.path.join(os.path.dirname(__file__), 'hsn_code_data.json'), 'r') as f: |
| 40 | hsn_codes = json.loads(f.read()) |
| 41 | |
Rushabh Mehta | 919a74a | 2017-06-22 16:37:04 +0530 | [diff] [blame] | 42 | frappe.db.commit() |
Rushabh Mehta | b3c8f44 | 2017-06-21 17:22:38 +0530 | [diff] [blame] | 43 | frappe.db.sql('truncate `tabGST HSN Code`') |
| 44 | |
| 45 | for d in hsn_codes: |
| 46 | hsn_code = frappe.new_doc('GST HSN Code') |
| 47 | hsn_code.update(d) |
| 48 | hsn_code.name = hsn_code.hsn_code |
| 49 | hsn_code.db_insert() |
| 50 | |
Rushabh Mehta | 919a74a | 2017-06-22 16:37:04 +0530 | [diff] [blame] | 51 | frappe.db.commit() |
| 52 | |
| 53 | def add_custom_roles_for_reports(): |
| 54 | for report_name in ('GST Sales Register', 'GST Purchase Register', |
| 55 | 'GST Itemised Sales Register', 'GST Itemised Purchase Register'): |
| 56 | |
Rushabh Mehta | 919a74a | 2017-06-22 16:37:04 +0530 | [diff] [blame] | 57 | if not frappe.db.get_value('Custom Role', dict(report=report_name)): |
| 58 | frappe.get_doc(dict( |
| 59 | doctype='Custom Role', |
| 60 | report=report_name, |
| 61 | roles= [ |
| 62 | dict(role='Accounts User'), |
| 63 | dict(role='Accounts Manager') |
| 64 | ] |
| 65 | )).insert() |
| 66 | |
Rushabh Mehta | b3c8f44 | 2017-06-21 17:22:38 +0530 | [diff] [blame] | 67 | def add_permissions(): |
Rushabh Mehta | 00ae424 | 2017-06-27 17:31:41 +0530 | [diff] [blame] | 68 | for doctype in ('GST HSN Code', 'GST Settings'): |
Rushabh Mehta | b3c8f44 | 2017-06-21 17:22:38 +0530 | [diff] [blame] | 69 | add_permission(doctype, 'Accounts Manager', 0) |
| 70 | add_permission(doctype, 'All', 0) |
| 71 | |
| 72 | def make_custom_fields(): |
| 73 | custom_fields = { |
| 74 | 'Address': [ |
Rushabh Mehta | 919a74a | 2017-06-22 16:37:04 +0530 | [diff] [blame] | 75 | dict(fieldname='gstin', label='Party GSTIN', fieldtype='Data', |
| 76 | insert_after='fax'), |
Rushabh Mehta | b3c8f44 | 2017-06-21 17:22:38 +0530 | [diff] [blame] | 77 | dict(fieldname='gst_state', label='GST State', fieldtype='Select', |
| 78 | options='\n'.join(states), insert_after='gstin') |
| 79 | ], |
| 80 | 'Purchase Invoice': [ |
| 81 | dict(fieldname='supplier_gstin', label='Supplier GSTIN', |
Rushabh Mehta | 919a74a | 2017-06-22 16:37:04 +0530 | [diff] [blame] | 82 | fieldtype='Data', insert_after='supplier_address', |
| 83 | options='supplier_address.gstin'), |
Rushabh Mehta | b3c8f44 | 2017-06-21 17:22:38 +0530 | [diff] [blame] | 84 | dict(fieldname='company_gstin', label='Company GSTIN', |
Rushabh Mehta | 919a74a | 2017-06-22 16:37:04 +0530 | [diff] [blame] | 85 | fieldtype='Data', insert_after='shipping_address', |
| 86 | options='shipping_address.gstin'), |
Rushabh Mehta | b3c8f44 | 2017-06-21 17:22:38 +0530 | [diff] [blame] | 87 | ], |
| 88 | 'Sales Invoice': [ |
| 89 | dict(fieldname='customer_gstin', label='Customer GSTIN', |
Rushabh Mehta | 919a74a | 2017-06-22 16:37:04 +0530 | [diff] [blame] | 90 | fieldtype='Data', insert_after='shipping_address', |
| 91 | options='shipping_address_name.gstin'), |
Rushabh Mehta | b3c8f44 | 2017-06-21 17:22:38 +0530 | [diff] [blame] | 92 | dict(fieldname='company_gstin', label='Company GSTIN', |
Rushabh Mehta | 919a74a | 2017-06-22 16:37:04 +0530 | [diff] [blame] | 93 | fieldtype='Data', insert_after='company_address', |
| 94 | options='company_address.gstin'), |
Rushabh Mehta | b3c8f44 | 2017-06-21 17:22:38 +0530 | [diff] [blame] | 95 | ], |
| 96 | 'Item': [ |
| 97 | dict(fieldname='gst_hsn_code', label='GST HSN Code', |
| 98 | fieldtype='Link', options='GST HSN Code', insert_after='item_group'), |
| 99 | ], |
| 100 | 'Sales Invoice Item': [ |
| 101 | dict(fieldname='gst_hsn_code', label='GST HSN Code', |
Rushabh Mehta | 919a74a | 2017-06-22 16:37:04 +0530 | [diff] [blame] | 102 | fieldtype='Data', options='item_code.gst_hsn_code', |
| 103 | insert_after='income_account'), |
Rushabh Mehta | b3c8f44 | 2017-06-21 17:22:38 +0530 | [diff] [blame] | 104 | ], |
| 105 | 'Purchase Invoice Item': [ |
| 106 | dict(fieldname='gst_hsn_code', label='GST HSN Code', |
Rushabh Mehta | 919a74a | 2017-06-22 16:37:04 +0530 | [diff] [blame] | 107 | fieldtype='Data', options='item_code.gst_hsn_code', |
| 108 | insert_after='expense_account'), |
Rushabh Mehta | b3c8f44 | 2017-06-21 17:22:38 +0530 | [diff] [blame] | 109 | ] |
| 110 | } |
| 111 | |
| 112 | for doctype, fields in custom_fields.items(): |
| 113 | for df in fields: |
| 114 | create_custom_field(doctype, df) |
| 115 | |
| 116 | def make_fixtures(): |
| 117 | docs = [ |
| 118 | {'doctype': 'Salary Component', 'salary_component': 'Professional Tax', 'description': 'Professional Tax', 'type': 'Deduction'}, |
| 119 | {'doctype': 'Salary Component', 'salary_component': 'Provident Fund', 'description': 'Provident fund', 'type': 'Deduction'}, |
| 120 | {'doctype': 'Salary Component', 'salary_component': 'House Rent Allowance', 'description': 'House Rent Allowance', 'type': 'Earning'}, |
| 121 | {'doctype': 'Salary Component', 'salary_component': 'Basic', 'description': 'Basic', 'type': 'Earning'}, |
| 122 | {'doctype': 'Salary Component', 'salary_component': 'Arrear', 'description': 'Arrear', 'type': 'Earning'}, |
| 123 | {'doctype': 'Salary Component', 'salary_component': 'Leave Encashment', 'description': 'Leave Encashment', 'type': 'Earning'} |
| 124 | ] |
| 125 | |
| 126 | for d in docs: |
| 127 | try: |
| 128 | doc = frappe.get_doc(d) |
| 129 | doc.flags.ignore_permissions = True |
| 130 | doc.insert() |
| 131 | except frappe.NameError: |
| 132 | pass |