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