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 | { |
Prateeksha Singh | 5e4c8ec | 2017-07-03 18:23:57 +0530 | [diff] [blame] | 16 | // Brand |
| 17 | name: 'brand', |
Prateeksha Singh | 5e4c8ec | 2017-07-03 18:23:57 +0530 | [diff] [blame] | 18 | icon: "fa fa-bookmark", |
| 19 | title: __("The Brand"), |
Prateeksha Singh | 95d8fd3 | 2017-09-04 11:14:04 +0530 | [diff] [blame] | 20 | // help: __('Upload your letter head and logo. (you can edit them later).'), |
Prateeksha Singh | 5e4c8ec | 2017-07-03 18:23:57 +0530 | [diff] [blame] | 21 | fields: [ |
| 22 | { |
| 23 | fieldtype: "Attach Image", fieldname: "attach_logo", |
| 24 | label: __("Attach Logo"), |
| 25 | description: __("100px by 100px"), |
Makarand Bauskar | 1d9fd9a | 2017-07-26 18:19:41 +0530 | [diff] [blame] | 26 | is_private: 0, |
| 27 | align: 'center' |
Prateeksha Singh | 5e4c8ec | 2017-07-03 18:23:57 +0530 | [diff] [blame] | 28 | }, |
| 29 | { |
| 30 | fieldname: 'company_name', |
Deepesh Garg | 60b693f | 2022-04-28 12:02:54 +0530 | [diff] [blame] | 31 | label: __('Company Name'), |
Makarand Bauskar | 1d9fd9a | 2017-07-26 18:19:41 +0530 | [diff] [blame] | 32 | fieldtype: 'Data', |
| 33 | reqd: 1 |
Prateeksha Singh | 5e4c8ec | 2017-07-03 18:23:57 +0530 | [diff] [blame] | 34 | }, |
| 35 | { |
| 36 | fieldname: 'company_abbr', |
Deepesh Garg | 60b693f | 2022-04-28 12:02:54 +0530 | [diff] [blame] | 37 | label: __('Company Abbreviation'), |
Prateeksha Singh | 5e4c8ec | 2017-07-03 18:23:57 +0530 | [diff] [blame] | 38 | fieldtype: 'Data' |
| 39 | } |
| 40 | ], |
| 41 | onload: function(slide) { |
| 42 | this.bind_events(slide); |
| 43 | }, |
| 44 | bind_events: function (slide) { |
| 45 | slide.get_input("company_name").on("change", function () { |
| 46 | var parts = slide.get_input("company_name").val().split(" "); |
| 47 | var abbr = $.map(parts, function (p) { return p ? p.substr(0, 1) : null }).join(""); |
Deepesh Garg | 239733ac | 2022-02-23 20:41:49 +0530 | [diff] [blame] | 48 | slide.get_field("company_abbr").set_value(abbr.slice(0, 10).toUpperCase()); |
Prateeksha Singh | 5e4c8ec | 2017-07-03 18:23:57 +0530 | [diff] [blame] | 49 | }).val(frappe.boot.sysdefaults.company_name || "").trigger("change"); |
Rushabh Mehta | 37b4d75 | 2015-11-09 16:53:11 +0530 | [diff] [blame] | 50 | |
Prateeksha Singh | 5e4c8ec | 2017-07-03 18:23:57 +0530 | [diff] [blame] | 51 | slide.get_input("company_abbr").on("change", function () { |
Deepesh Garg | 239733ac | 2022-02-23 20:41:49 +0530 | [diff] [blame] | 52 | if (slide.get_input("company_abbr").val().length > 10) { |
Nabin Hait | e6d65bc | 2018-02-14 17:44:06 +0530 | [diff] [blame] | 53 | frappe.msgprint(__("Company Abbreviation cannot have more than 5 characters")); |
Makarand Bauskar | 30f2bcb | 2017-07-11 12:36:57 +0530 | [diff] [blame] | 54 | slide.get_field("company_abbr").set_value(""); |
Prateeksha Singh | 5e4c8ec | 2017-07-03 18:23:57 +0530 | [diff] [blame] | 55 | } |
| 56 | }); |
Prateeksha Singh | 95d8fd3 | 2017-09-04 11:14:04 +0530 | [diff] [blame] | 57 | }, |
| 58 | validate: function() { |
Makarand Bauskar | b1bf502 | 2017-10-02 11:38:03 +0530 | [diff] [blame] | 59 | if ((this.values.company_name || "").toLowerCase() == "company") { |
| 60 | frappe.msgprint(__("Company Name cannot be Company")); |
| 61 | return false; |
| 62 | } |
Prateeksha Singh | 95d8fd3 | 2017-09-04 11:14:04 +0530 | [diff] [blame] | 63 | if (!this.values.company_abbr) { |
| 64 | return false; |
| 65 | } |
Deepesh Garg | 239733ac | 2022-02-23 20:41:49 +0530 | [diff] [blame] | 66 | if (this.values.company_abbr.length > 10) { |
Faris Ansari | 40c1534 | 2018-12-06 07:37:32 +0530 | [diff] [blame] | 67 | return false; |
| 68 | } |
Prateeksha Singh | 95d8fd3 | 2017-09-04 11:14:04 +0530 | [diff] [blame] | 69 | return true; |
Prateeksha Singh | 5e4c8ec | 2017-07-03 18:23:57 +0530 | [diff] [blame] | 70 | } |
| 71 | }, |
| 72 | { |
| 73 | // Organisation |
| 74 | name: 'organisation', |
Prateeksha Singh | 5e4c8ec | 2017-07-03 18:23:57 +0530 | [diff] [blame] | 75 | title: __("Your Organization"), |
| 76 | icon: "fa fa-building", |
Prateeksha Singh | 5e4c8ec | 2017-07-03 18:23:57 +0530 | [diff] [blame] | 77 | fields: [ |
| 78 | { |
| 79 | fieldname: 'company_tagline', |
| 80 | label: __('What does it do?'), |
| 81 | fieldtype: 'Data', |
Deepesh Garg | 60b693f | 2022-04-28 12:02:54 +0530 | [diff] [blame] | 82 | placeholder: __('e.g. "Build tools for builders"'), |
Prateeksha Singh | 5e4c8ec | 2017-07-03 18:23:57 +0530 | [diff] [blame] | 83 | reqd: 1 |
| 84 | }, |
| 85 | { fieldname: 'bank_account', label: __('Bank Name'), fieldtype: 'Data', reqd: 1 }, |
| 86 | { |
| 87 | fieldname: 'chart_of_accounts', label: __('Chart of Accounts'), |
| 88 | options: "", fieldtype: 'Select' |
Anand Doshi | 1ed8283 | 2015-11-16 12:58:14 +0530 | [diff] [blame] | 89 | }, |
Zarrar | 254ce64 | 2018-06-28 14:15:34 +0530 | [diff] [blame] | 90 | { fieldname: 'view_coa', label: __('View Chart of Accounts'), fieldtype: 'Button' }, |
Rushabh Mehta | 2cd41bc | 2020-12-18 13:17:58 +0530 | [diff] [blame] | 91 | { fieldname: 'fy_start_date', label: __('Financial Year Begins On'), fieldtype: 'Date', reqd: 1 }, |
| 92 | // end date should be hidden (auto calculated) |
| 93 | { 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] | 94 | ], |
| 95 | |
| 96 | onload: function (slide) { |
| 97 | this.load_chart_of_accounts(slide); |
| 98 | this.bind_events(slide); |
| 99 | this.set_fy_dates(slide); |
| 100 | }, |
| 101 | |
| 102 | validate: function () { |
Deepesh Garg | 2908f2e | 2021-06-26 23:49:32 +0530 | [diff] [blame] | 103 | let me = this; |
| 104 | let exist; |
| 105 | |
Faris Ansari | aa856c4 | 2018-09-18 10:42:04 +0530 | [diff] [blame] | 106 | if (!this.validate_fy_dates()) { |
Prateeksha Singh | 5e4c8ec | 2017-07-03 18:23:57 +0530 | [diff] [blame] | 107 | return false; |
| 108 | } |
Zarrar | 441a1e1 | 2018-02-16 14:44:42 +0530 | [diff] [blame] | 109 | |
Deepesh Garg | 2908f2e | 2021-06-26 23:49:32 +0530 | [diff] [blame] | 110 | // Validate bank name |
Ankush Menat | 4551d7d | 2021-08-19 13:41:10 +0530 | [diff] [blame] | 111 | if(me.values.bank_account) { |
Deepesh Garg | 2908f2e | 2021-06-26 23:49:32 +0530 | [diff] [blame] | 112 | frappe.call({ |
| 113 | async: false, |
| 114 | method: "erpnext.accounts.doctype.account.chart_of_accounts.chart_of_accounts.validate_bank_account", |
| 115 | args: { |
| 116 | "coa": me.values.chart_of_accounts, |
| 117 | "bank_account": me.values.bank_account |
| 118 | }, |
| 119 | callback: function (r) { |
| 120 | if(r.message){ |
| 121 | exist = r.message; |
| 122 | me.get_field("bank_account").set_value(""); |
| 123 | let message = __('Account {0} already exists. Please enter a different name for your bank account.', |
| 124 | [me.values.bank_account] |
| 125 | ); |
| 126 | frappe.msgprint(message); |
| 127 | } |
| 128 | } |
| 129 | }); |
| 130 | return !exist; // Return False if exist = true |
| 131 | } |
| 132 | |
Prateeksha Singh | 5e4c8ec | 2017-07-03 18:23:57 +0530 | [diff] [blame] | 133 | return true; |
| 134 | }, |
| 135 | |
Faris Ansari | aa856c4 | 2018-09-18 10:42:04 +0530 | [diff] [blame] | 136 | validate_fy_dates: function() { |
| 137 | // validate fiscal year start and end dates |
| 138 | const invalid = this.values.fy_start_date == 'Invalid date' || |
| 139 | this.values.fy_end_date == 'Invalid date'; |
| 140 | const start_greater_than_end = this.values.fy_start_date > this.values.fy_end_date; |
| 141 | |
| 142 | if (invalid || start_greater_than_end) { |
| 143 | frappe.msgprint(__("Please enter valid Financial Year Start and End Dates")); |
| 144 | return false; |
| 145 | } |
| 146 | |
| 147 | return true; |
| 148 | }, |
| 149 | |
Prateeksha Singh | 5e4c8ec | 2017-07-03 18:23:57 +0530 | [diff] [blame] | 150 | set_fy_dates: function (slide) { |
| 151 | var country = frappe.wizard.values.country; |
| 152 | |
| 153 | if (country) { |
| 154 | var fy = erpnext.setup.fiscal_years[country]; |
| 155 | var current_year = moment(new Date()).year(); |
| 156 | var next_year = current_year + 1; |
| 157 | if (!fy) { |
Makarand Bauskar | bf66d7e | 2017-07-05 17:26:34 +0530 | [diff] [blame] | 158 | fy = ["01-01", "12-31"]; |
Prateeksha Singh | 5e4c8ec | 2017-07-03 18:23:57 +0530 | [diff] [blame] | 159 | next_year = current_year; |
Anand Doshi | 5c3469a | 2015-11-24 17:06:54 +0530 | [diff] [blame] | 160 | } |
| 161 | |
Prateeksha Singh | 5e4c8ec | 2017-07-03 18:23:57 +0530 | [diff] [blame] | 162 | var year_start_date = current_year + "-" + fy[0]; |
| 163 | if (year_start_date > frappe.datetime.get_today()) { |
Rushabh Mehta | 63b0642 | 2017-07-05 17:56:04 +0530 | [diff] [blame] | 164 | next_year = current_year; |
Prateeksha Singh | 5e4c8ec | 2017-07-03 18:23:57 +0530 | [diff] [blame] | 165 | current_year -= 1; |
Anand Doshi | 5c3469a | 2015-11-24 17:06:54 +0530 | [diff] [blame] | 166 | } |
Rushabh Mehta | 63b0642 | 2017-07-05 17:56:04 +0530 | [diff] [blame] | 167 | slide.get_field("fy_start_date").set_value(current_year + '-' + fy[0]); |
| 168 | slide.get_field("fy_end_date").set_value(next_year + '-' + fy[1]); |
Prateeksha Singh | 5e4c8ec | 2017-07-03 18:23:57 +0530 | [diff] [blame] | 169 | } |
Prateeksha Singh | 5e4c8ec | 2017-07-03 18:23:57 +0530 | [diff] [blame] | 170 | }, |
Anand Doshi | 5c3469a | 2015-11-24 17:06:54 +0530 | [diff] [blame] | 171 | |
Anand Doshi | 1ed8283 | 2015-11-16 12:58:14 +0530 | [diff] [blame] | 172 | |
Prateeksha Singh | 5e4c8ec | 2017-07-03 18:23:57 +0530 | [diff] [blame] | 173 | load_chart_of_accounts: function (slide) { |
| 174 | var country = frappe.wizard.values.country; |
Anand Doshi | 1ed8283 | 2015-11-16 12:58:14 +0530 | [diff] [blame] | 175 | |
Prateeksha Singh | 5e4c8ec | 2017-07-03 18:23:57 +0530 | [diff] [blame] | 176 | if (country) { |
| 177 | frappe.call({ |
| 178 | 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] | 179 | args: { "country": country, with_standard: true }, |
Prateeksha Singh | 5e4c8ec | 2017-07-03 18:23:57 +0530 | [diff] [blame] | 180 | callback: function (r) { |
| 181 | if (r.message) { |
| 182 | slide.get_input("chart_of_accounts").empty() |
| 183 | .add_options(r.message); |
Rushabh Mehta | 37b4d75 | 2015-11-09 16:53:11 +0530 | [diff] [blame] | 184 | } |
Anand Doshi | 1ed8283 | 2015-11-16 12:58:14 +0530 | [diff] [blame] | 185 | } |
Prateeksha Singh | 5e4c8ec | 2017-07-03 18:23:57 +0530 | [diff] [blame] | 186 | }) |
Rushabh Mehta | 37b4d75 | 2015-11-09 16:53:11 +0530 | [diff] [blame] | 187 | } |
| 188 | }, |
Rushabh Mehta | 37b4d75 | 2015-11-09 16:53:11 +0530 | [diff] [blame] | 189 | |
Prateeksha Singh | 5e4c8ec | 2017-07-03 18:23:57 +0530 | [diff] [blame] | 190 | bind_events: function (slide) { |
Zarrar | 254ce64 | 2018-06-28 14:15:34 +0530 | [diff] [blame] | 191 | let me = this; |
Prateeksha Singh | 5e4c8ec | 2017-07-03 18:23:57 +0530 | [diff] [blame] | 192 | slide.get_input("fy_start_date").on("change", function () { |
Rushabh Mehta | 63b0642 | 2017-07-05 17:56:04 +0530 | [diff] [blame] | 193 | var start_date = slide.form.fields_dict.fy_start_date.get_value(); |
Prateeksha Singh | 5e4c8ec | 2017-07-03 18:23:57 +0530 | [diff] [blame] | 194 | var year_end_date = |
Rushabh Mehta | 63b0642 | 2017-07-05 17:56:04 +0530 | [diff] [blame] | 195 | frappe.datetime.add_days(frappe.datetime.add_months(start_date, 12), -1); |
| 196 | slide.form.fields_dict.fy_end_date.set_value(year_end_date); |
Prateeksha Singh | 5e4c8ec | 2017-07-03 18:23:57 +0530 | [diff] [blame] | 197 | }); |
Zarrar | 254ce64 | 2018-06-28 14:15:34 +0530 | [diff] [blame] | 198 | |
| 199 | slide.get_input("view_coa").on("click", function() { |
| 200 | let chart_template = slide.form.fields_dict.chart_of_accounts.get_value(); |
| 201 | if(!chart_template) return; |
| 202 | |
| 203 | me.charts_modal(slide, chart_template); |
| 204 | }); |
| 205 | }, |
| 206 | |
| 207 | charts_modal: function(slide, chart_template) { |
| 208 | let parent = __('All Accounts'); |
| 209 | |
| 210 | var dialog = new frappe.ui.Dialog({ |
| 211 | title: chart_template, |
| 212 | fields: [ |
| 213 | {'fieldname': 'expand_all', 'label': __('Expand All'), 'fieldtype': 'Button', |
| 214 | click: function() { |
| 215 | // expand all nodes on button click |
| 216 | coa_tree.load_children(coa_tree.root_node, true); |
| 217 | } |
| 218 | }, |
| 219 | {'fieldname': 'collapse_all', 'label': __('Collapse All'), 'fieldtype': 'Button', |
| 220 | click: function() { |
| 221 | // collapse all nodes |
| 222 | coa_tree.get_all_nodes(coa_tree.root_node.data.value, coa_tree.root_node.is_root) |
| 223 | .then(data_list => { |
| 224 | data_list.map(d => { coa_tree.toggle_node(coa_tree.nodes[d.parent]); }); |
| 225 | }); |
| 226 | } |
| 227 | } |
| 228 | ] |
| 229 | }); |
| 230 | |
| 231 | // render tree structure in the dialog modal |
| 232 | let coa_tree = new frappe.ui.Tree({ |
| 233 | parent: $(dialog.body), |
| 234 | label: parent, |
| 235 | expandable: true, |
| 236 | method: 'erpnext.accounts.utils.get_coa', |
| 237 | args: { |
| 238 | chart: chart_template, |
| 239 | parent: parent, |
| 240 | doctype: 'Account' |
| 241 | }, |
| 242 | onclick: function(node) { |
| 243 | parent = node.value; |
| 244 | } |
| 245 | }); |
| 246 | |
| 247 | // add class to show buttons side by side |
Zarrar | c1225a8 | 2018-07-03 10:35:31 +0530 | [diff] [blame] | 248 | const form_container = $(dialog.body).find('form'); |
| 249 | const buttons = $(form_container).find('.frappe-control'); |
| 250 | form_container.addClass('flex'); |
| 251 | buttons.map((index, button) => { |
| 252 | $(button).css({"margin-right": "1em"}); |
| 253 | }) |
Zarrar | 254ce64 | 2018-06-28 14:15:34 +0530 | [diff] [blame] | 254 | |
| 255 | dialog.show(); |
| 256 | 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] | 257 | } |
Anand Doshi | 3177138 | 2016-07-05 21:34:21 +0530 | [diff] [blame] | 258 | } |
Prateeksha Singh | 5e4c8ec | 2017-07-03 18:23:57 +0530 | [diff] [blame] | 259 | ]; |
Anand Doshi | 3177138 | 2016-07-05 21:34:21 +0530 | [diff] [blame] | 260 | |
Prateeksha Singh | 5e4c8ec | 2017-07-03 18:23:57 +0530 | [diff] [blame] | 261 | // Source: https://en.wikipedia.org/wiki/Fiscal_year |
| 262 | // default 1st Jan - 31st Dec |
Rushabh Mehta | 20038ad | 2016-07-21 16:01:59 +0530 | [diff] [blame] | 263 | |
Prateeksha Singh | 5e4c8ec | 2017-07-03 18:23:57 +0530 | [diff] [blame] | 264 | erpnext.setup.fiscal_years = { |
Shreya Shah | db499dc | 2017-12-12 14:19:35 +0530 | [diff] [blame] | 265 | "Afghanistan": ["12-21", "12-20"], |
Makarand Bauskar | bf66d7e | 2017-07-05 17:26:34 +0530 | [diff] [blame] | 266 | "Australia": ["07-01", "06-30"], |
| 267 | "Bangladesh": ["07-01", "06-30"], |
| 268 | "Canada": ["04-01", "03-31"], |
| 269 | "Costa Rica": ["10-01", "09-30"], |
| 270 | "Egypt": ["07-01", "06-30"], |
| 271 | "Hong Kong": ["04-01", "03-31"], |
| 272 | "India": ["04-01", "03-31"], |
| 273 | "Iran": ["06-23", "06-22"], |
Makarand Bauskar | bf66d7e | 2017-07-05 17:26:34 +0530 | [diff] [blame] | 274 | "Myanmar": ["04-01", "03-31"], |
| 275 | "New Zealand": ["04-01", "03-31"], |
| 276 | "Pakistan": ["07-01", "06-30"], |
| 277 | "Singapore": ["04-01", "03-31"], |
| 278 | "South Africa": ["03-01", "02-28"], |
| 279 | "Thailand": ["10-01", "09-30"], |
| 280 | "United Kingdom": ["04-01", "03-31"], |
Prateeksha Singh | 5e4c8ec | 2017-07-03 18:23:57 +0530 | [diff] [blame] | 281 | }; |