Prateeksha Singh | 5e4c8ec | 2017-07-03 18:23:57 +0530 | [diff] [blame] | 1 | frappe.provide("erpnext.setup"); |
Rushabh Mehta | 37b4d75 | 2015-11-09 16:53:11 +0530 | [diff] [blame] | 2 | |
| 3 | frappe.pages['setup-wizard'].on_page_load = function(wrapper) { |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 4 | if(frappe.sys_defaults.company) { |
Rushabh Mehta | 37b4d75 | 2015-11-09 16:53:11 +0530 | [diff] [blame] | 5 | frappe.set_route("desk"); |
| 6 | return; |
| 7 | } |
Anand Doshi | 1ed8283 | 2015-11-16 12:58:14 +0530 | [diff] [blame] | 8 | }; |
Rushabh Mehta | 37b4d75 | 2015-11-09 16:53:11 +0530 | [diff] [blame] | 9 | |
Prateeksha Singh | 95d8fd3 | 2017-09-04 11:14:04 +0530 | [diff] [blame] | 10 | frappe.setup.on("before_load", function () { |
| 11 | erpnext.setup.slides_settings.map(frappe.setup.add_slide); |
| 12 | }); |
| 13 | |
| 14 | erpnext.setup.slides_settings = [ |
Prateeksha Singh | 5e4c8ec | 2017-07-03 18:23:57 +0530 | [diff] [blame] | 15 | { |
Deepesh Garg | 3598bcc | 2023-01-27 14:40:39 +0530 | [diff] [blame^] | 16 | // Organization |
| 17 | name: 'organization', |
| 18 | title: __("Setup your organization"), |
| 19 | icon: "fa fa-building", |
Prateeksha Singh | 5e4c8ec | 2017-07-03 18:23:57 +0530 | [diff] [blame] | 20 | fields: [ |
| 21 | { |
Prateeksha Singh | 5e4c8ec | 2017-07-03 18:23:57 +0530 | [diff] [blame] | 22 | fieldname: 'company_name', |
Deepesh Garg | 60b693f | 2022-04-28 12:02:54 +0530 | [diff] [blame] | 23 | label: __('Company Name'), |
Makarand Bauskar | 1d9fd9a | 2017-07-26 18:19:41 +0530 | [diff] [blame] | 24 | fieldtype: 'Data', |
| 25 | reqd: 1 |
Prateeksha Singh | 5e4c8ec | 2017-07-03 18:23:57 +0530 | [diff] [blame] | 26 | }, |
| 27 | { |
| 28 | fieldname: 'company_abbr', |
Deepesh Garg | 60b693f | 2022-04-28 12:02:54 +0530 | [diff] [blame] | 29 | label: __('Company Abbreviation'), |
Prateeksha Singh | 5e4c8ec | 2017-07-03 18:23:57 +0530 | [diff] [blame] | 30 | fieldtype: 'Data', |
Deepesh Garg | 3598bcc | 2023-01-27 14:40:39 +0530 | [diff] [blame^] | 31 | hidden: 1 |
Prateeksha Singh | 5e4c8ec | 2017-07-03 18:23:57 +0530 | [diff] [blame] | 32 | }, |
Prateeksha Singh | 5e4c8ec | 2017-07-03 18:23:57 +0530 | [diff] [blame] | 33 | { |
| 34 | fieldname: 'chart_of_accounts', label: __('Chart of Accounts'), |
| 35 | options: "", fieldtype: 'Select' |
Anand Doshi | 1ed8283 | 2015-11-16 12:58:14 +0530 | [diff] [blame] | 36 | }, |
Zarrar | 254ce64 | 2018-06-28 14:15:34 +0530 | [diff] [blame] | 37 | { fieldname: 'view_coa', label: __('View Chart of Accounts'), fieldtype: 'Button' }, |
Rushabh Mehta | 2cd41bc | 2020-12-18 13:17:58 +0530 | [diff] [blame] | 38 | { fieldname: 'fy_start_date', label: __('Financial Year Begins On'), fieldtype: 'Date', reqd: 1 }, |
| 39 | // end date should be hidden (auto calculated) |
| 40 | { fieldname: 'fy_end_date', label: __('End Date'), fieldtype: 'Date', reqd: 1, hidden: 1 }, |
Prateeksha Singh | 5e4c8ec | 2017-07-03 18:23:57 +0530 | [diff] [blame] | 41 | ], |
| 42 | |
| 43 | onload: function (slide) { |
Prateeksha Singh | 5e4c8ec | 2017-07-03 18:23:57 +0530 | [diff] [blame] | 44 | this.bind_events(slide); |
Deepesh Garg | 3598bcc | 2023-01-27 14:40:39 +0530 | [diff] [blame^] | 45 | this.load_chart_of_accounts(slide); |
Prateeksha Singh | 5e4c8ec | 2017-07-03 18:23:57 +0530 | [diff] [blame] | 46 | this.set_fy_dates(slide); |
| 47 | }, |
Prateeksha Singh | 5e4c8ec | 2017-07-03 18:23:57 +0530 | [diff] [blame] | 48 | validate: function () { |
Faris Ansari | aa856c4 | 2018-09-18 10:42:04 +0530 | [diff] [blame] | 49 | if (!this.validate_fy_dates()) { |
Prateeksha Singh | 5e4c8ec | 2017-07-03 18:23:57 +0530 | [diff] [blame] | 50 | return false; |
| 51 | } |
Zarrar | 441a1e1 | 2018-02-16 14:44:42 +0530 | [diff] [blame] | 52 | |
Deepesh Garg | 3598bcc | 2023-01-27 14:40:39 +0530 | [diff] [blame^] | 53 | if ((this.values.company_name || "").toLowerCase() == "company") { |
| 54 | frappe.msgprint(__("Company Name cannot be Company")); |
| 55 | return false; |
| 56 | } |
| 57 | if (!this.values.company_abbr) { |
| 58 | return false; |
| 59 | } |
| 60 | if (this.values.company_abbr.length > 10) { |
| 61 | return false; |
Deepesh Garg | 2908f2e | 2021-06-26 23:49:32 +0530 | [diff] [blame] | 62 | } |
| 63 | |
Prateeksha Singh | 5e4c8ec | 2017-07-03 18:23:57 +0530 | [diff] [blame] | 64 | return true; |
| 65 | }, |
| 66 | |
Faris Ansari | aa856c4 | 2018-09-18 10:42:04 +0530 | [diff] [blame] | 67 | validate_fy_dates: function() { |
| 68 | // validate fiscal year start and end dates |
| 69 | const invalid = this.values.fy_start_date == 'Invalid date' || |
| 70 | this.values.fy_end_date == 'Invalid date'; |
| 71 | const start_greater_than_end = this.values.fy_start_date > this.values.fy_end_date; |
| 72 | |
| 73 | if (invalid || start_greater_than_end) { |
| 74 | frappe.msgprint(__("Please enter valid Financial Year Start and End Dates")); |
| 75 | return false; |
| 76 | } |
| 77 | |
| 78 | return true; |
| 79 | }, |
| 80 | |
Prateeksha Singh | 5e4c8ec | 2017-07-03 18:23:57 +0530 | [diff] [blame] | 81 | set_fy_dates: function (slide) { |
| 82 | var country = frappe.wizard.values.country; |
| 83 | |
| 84 | if (country) { |
Deepesh Garg | 3598bcc | 2023-01-27 14:40:39 +0530 | [diff] [blame^] | 85 | let fy = erpnext.setup.fiscal_years[country]; |
| 86 | let current_year = moment(new Date()).year(); |
| 87 | let next_year = current_year + 1; |
Prateeksha Singh | 5e4c8ec | 2017-07-03 18:23:57 +0530 | [diff] [blame] | 88 | if (!fy) { |
Makarand Bauskar | bf66d7e | 2017-07-05 17:26:34 +0530 | [diff] [blame] | 89 | fy = ["01-01", "12-31"]; |
Prateeksha Singh | 5e4c8ec | 2017-07-03 18:23:57 +0530 | [diff] [blame] | 90 | next_year = current_year; |
Anand Doshi | 5c3469a | 2015-11-24 17:06:54 +0530 | [diff] [blame] | 91 | } |
| 92 | |
Deepesh Garg | 3598bcc | 2023-01-27 14:40:39 +0530 | [diff] [blame^] | 93 | let year_start_date = current_year + "-" + fy[0]; |
Prateeksha Singh | 5e4c8ec | 2017-07-03 18:23:57 +0530 | [diff] [blame] | 94 | if (year_start_date > frappe.datetime.get_today()) { |
Rushabh Mehta | 63b0642 | 2017-07-05 17:56:04 +0530 | [diff] [blame] | 95 | next_year = current_year; |
Prateeksha Singh | 5e4c8ec | 2017-07-03 18:23:57 +0530 | [diff] [blame] | 96 | current_year -= 1; |
Anand Doshi | 5c3469a | 2015-11-24 17:06:54 +0530 | [diff] [blame] | 97 | } |
Rushabh Mehta | 63b0642 | 2017-07-05 17:56:04 +0530 | [diff] [blame] | 98 | slide.get_field("fy_start_date").set_value(current_year + '-' + fy[0]); |
| 99 | slide.get_field("fy_end_date").set_value(next_year + '-' + fy[1]); |
Prateeksha Singh | 5e4c8ec | 2017-07-03 18:23:57 +0530 | [diff] [blame] | 100 | } |
Prateeksha Singh | 5e4c8ec | 2017-07-03 18:23:57 +0530 | [diff] [blame] | 101 | }, |
Anand Doshi | 5c3469a | 2015-11-24 17:06:54 +0530 | [diff] [blame] | 102 | |
Anand Doshi | 1ed8283 | 2015-11-16 12:58:14 +0530 | [diff] [blame] | 103 | |
Prateeksha Singh | 5e4c8ec | 2017-07-03 18:23:57 +0530 | [diff] [blame] | 104 | load_chart_of_accounts: function (slide) { |
Deepesh Garg | 3598bcc | 2023-01-27 14:40:39 +0530 | [diff] [blame^] | 105 | let country = frappe.wizard.values.country; |
Anand Doshi | 1ed8283 | 2015-11-16 12:58:14 +0530 | [diff] [blame] | 106 | |
Prateeksha Singh | 5e4c8ec | 2017-07-03 18:23:57 +0530 | [diff] [blame] | 107 | if (country) { |
| 108 | frappe.call({ |
| 109 | method: "erpnext.accounts.doctype.account.chart_of_accounts.chart_of_accounts.get_charts_for_country", |
Zarrar | 254ce64 | 2018-06-28 14:15:34 +0530 | [diff] [blame] | 110 | args: { "country": country, with_standard: true }, |
Prateeksha Singh | 5e4c8ec | 2017-07-03 18:23:57 +0530 | [diff] [blame] | 111 | callback: function (r) { |
| 112 | if (r.message) { |
| 113 | slide.get_input("chart_of_accounts").empty() |
| 114 | .add_options(r.message); |
Rushabh Mehta | 37b4d75 | 2015-11-09 16:53:11 +0530 | [diff] [blame] | 115 | } |
Anand Doshi | 1ed8283 | 2015-11-16 12:58:14 +0530 | [diff] [blame] | 116 | } |
Prateeksha Singh | 5e4c8ec | 2017-07-03 18:23:57 +0530 | [diff] [blame] | 117 | }) |
Rushabh Mehta | 37b4d75 | 2015-11-09 16:53:11 +0530 | [diff] [blame] | 118 | } |
| 119 | }, |
Rushabh Mehta | 37b4d75 | 2015-11-09 16:53:11 +0530 | [diff] [blame] | 120 | |
Prateeksha Singh | 5e4c8ec | 2017-07-03 18:23:57 +0530 | [diff] [blame] | 121 | bind_events: function (slide) { |
Zarrar | 254ce64 | 2018-06-28 14:15:34 +0530 | [diff] [blame] | 122 | let me = this; |
Prateeksha Singh | 5e4c8ec | 2017-07-03 18:23:57 +0530 | [diff] [blame] | 123 | slide.get_input("fy_start_date").on("change", function () { |
Rushabh Mehta | 63b0642 | 2017-07-05 17:56:04 +0530 | [diff] [blame] | 124 | var start_date = slide.form.fields_dict.fy_start_date.get_value(); |
Prateeksha Singh | 5e4c8ec | 2017-07-03 18:23:57 +0530 | [diff] [blame] | 125 | var year_end_date = |
Rushabh Mehta | 63b0642 | 2017-07-05 17:56:04 +0530 | [diff] [blame] | 126 | frappe.datetime.add_days(frappe.datetime.add_months(start_date, 12), -1); |
| 127 | slide.form.fields_dict.fy_end_date.set_value(year_end_date); |
Prateeksha Singh | 5e4c8ec | 2017-07-03 18:23:57 +0530 | [diff] [blame] | 128 | }); |
Zarrar | 254ce64 | 2018-06-28 14:15:34 +0530 | [diff] [blame] | 129 | |
| 130 | slide.get_input("view_coa").on("click", function() { |
| 131 | let chart_template = slide.form.fields_dict.chart_of_accounts.get_value(); |
| 132 | if(!chart_template) return; |
| 133 | |
| 134 | me.charts_modal(slide, chart_template); |
| 135 | }); |
Deepesh Garg | 3598bcc | 2023-01-27 14:40:39 +0530 | [diff] [blame^] | 136 | |
| 137 | slide.get_input("company_name").on("change", function () { |
| 138 | let parts = slide.get_input("company_name").val().split(" "); |
| 139 | let abbr = $.map(parts, function (p) { return p ? p.substr(0, 1) : null }).join(""); |
| 140 | slide.get_field("company_abbr").set_value(abbr.slice(0, 10).toUpperCase()); |
| 141 | }).val(frappe.boot.sysdefaults.company_name || "").trigger("change"); |
| 142 | |
| 143 | slide.get_input("company_abbr").on("change", function () { |
| 144 | if (slide.get_input("company_abbr").val().length > 10) { |
| 145 | frappe.msgprint(__("Company Abbreviation cannot have more than 5 characters")); |
| 146 | slide.get_field("company_abbr").set_value(""); |
| 147 | } |
| 148 | }); |
Zarrar | 254ce64 | 2018-06-28 14:15:34 +0530 | [diff] [blame] | 149 | }, |
| 150 | |
| 151 | charts_modal: function(slide, chart_template) { |
| 152 | let parent = __('All Accounts'); |
| 153 | |
Deepesh Garg | 3598bcc | 2023-01-27 14:40:39 +0530 | [diff] [blame^] | 154 | let dialog = new frappe.ui.Dialog({ |
Zarrar | 254ce64 | 2018-06-28 14:15:34 +0530 | [diff] [blame] | 155 | title: chart_template, |
| 156 | fields: [ |
| 157 | {'fieldname': 'expand_all', 'label': __('Expand All'), 'fieldtype': 'Button', |
| 158 | click: function() { |
| 159 | // expand all nodes on button click |
| 160 | coa_tree.load_children(coa_tree.root_node, true); |
| 161 | } |
| 162 | }, |
| 163 | {'fieldname': 'collapse_all', 'label': __('Collapse All'), 'fieldtype': 'Button', |
| 164 | click: function() { |
| 165 | // collapse all nodes |
| 166 | coa_tree.get_all_nodes(coa_tree.root_node.data.value, coa_tree.root_node.is_root) |
| 167 | .then(data_list => { |
| 168 | data_list.map(d => { coa_tree.toggle_node(coa_tree.nodes[d.parent]); }); |
| 169 | }); |
| 170 | } |
| 171 | } |
| 172 | ] |
| 173 | }); |
| 174 | |
| 175 | // render tree structure in the dialog modal |
| 176 | let coa_tree = new frappe.ui.Tree({ |
| 177 | parent: $(dialog.body), |
| 178 | label: parent, |
| 179 | expandable: true, |
| 180 | method: 'erpnext.accounts.utils.get_coa', |
| 181 | args: { |
| 182 | chart: chart_template, |
| 183 | parent: parent, |
| 184 | doctype: 'Account' |
| 185 | }, |
| 186 | onclick: function(node) { |
| 187 | parent = node.value; |
| 188 | } |
| 189 | }); |
| 190 | |
| 191 | // add class to show buttons side by side |
Zarrar | c1225a8 | 2018-07-03 10:35:31 +0530 | [diff] [blame] | 192 | const form_container = $(dialog.body).find('form'); |
| 193 | const buttons = $(form_container).find('.frappe-control'); |
| 194 | form_container.addClass('flex'); |
| 195 | buttons.map((index, button) => { |
| 196 | $(button).css({"margin-right": "1em"}); |
| 197 | }) |
Zarrar | 254ce64 | 2018-06-28 14:15:34 +0530 | [diff] [blame] | 198 | |
| 199 | dialog.show(); |
| 200 | coa_tree.load_children(coa_tree.root_node, true); // expand all node trigger |
Prateeksha Singh | 5e4c8ec | 2017-07-03 18:23:57 +0530 | [diff] [blame] | 201 | } |
Anand Doshi | 3177138 | 2016-07-05 21:34:21 +0530 | [diff] [blame] | 202 | } |
Prateeksha Singh | 5e4c8ec | 2017-07-03 18:23:57 +0530 | [diff] [blame] | 203 | ]; |
Anand Doshi | 3177138 | 2016-07-05 21:34:21 +0530 | [diff] [blame] | 204 | |
Prateeksha Singh | 5e4c8ec | 2017-07-03 18:23:57 +0530 | [diff] [blame] | 205 | // Source: https://en.wikipedia.org/wiki/Fiscal_year |
| 206 | // default 1st Jan - 31st Dec |
Rushabh Mehta | 20038ad | 2016-07-21 16:01:59 +0530 | [diff] [blame] | 207 | |
Prateeksha Singh | 5e4c8ec | 2017-07-03 18:23:57 +0530 | [diff] [blame] | 208 | erpnext.setup.fiscal_years = { |
Shreya Shah | db499dc | 2017-12-12 14:19:35 +0530 | [diff] [blame] | 209 | "Afghanistan": ["12-21", "12-20"], |
Makarand Bauskar | bf66d7e | 2017-07-05 17:26:34 +0530 | [diff] [blame] | 210 | "Australia": ["07-01", "06-30"], |
| 211 | "Bangladesh": ["07-01", "06-30"], |
| 212 | "Canada": ["04-01", "03-31"], |
| 213 | "Costa Rica": ["10-01", "09-30"], |
| 214 | "Egypt": ["07-01", "06-30"], |
| 215 | "Hong Kong": ["04-01", "03-31"], |
| 216 | "India": ["04-01", "03-31"], |
| 217 | "Iran": ["06-23", "06-22"], |
Makarand Bauskar | bf66d7e | 2017-07-05 17:26:34 +0530 | [diff] [blame] | 218 | "Myanmar": ["04-01", "03-31"], |
| 219 | "New Zealand": ["04-01", "03-31"], |
| 220 | "Pakistan": ["07-01", "06-30"], |
| 221 | "Singapore": ["04-01", "03-31"], |
| 222 | "South Africa": ["03-01", "02-28"], |
| 223 | "Thailand": ["10-01", "09-30"], |
| 224 | "United Kingdom": ["04-01", "03-31"], |
Prateeksha Singh | 5e4c8ec | 2017-07-03 18:23:57 +0530 | [diff] [blame] | 225 | }; |