blob: 2173c2d4de8a584790eabd0ba57c0c1fd514b7cc [file] [log] [blame]
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +05301frappe.provide("erpnext.setup");
Rushabh Mehta37b4d752015-11-09 16:53:11 +05302
3frappe.pages['setup-wizard'].on_page_load = function(wrapper) {
Faris Ansariab74ca72017-05-30 12:54:42 +05304 if(frappe.sys_defaults.company) {
Rushabh Mehta37b4d752015-11-09 16:53:11 +05305 frappe.set_route("desk");
6 return;
7 }
Anand Doshi1ed82832015-11-16 12:58:14 +05308};
Rushabh Mehta37b4d752015-11-09 16:53:11 +05309
Prateeksha Singh95d8fd32017-09-04 11:14:04 +053010frappe.setup.on("before_load", function () {
11 erpnext.setup.slides_settings.map(frappe.setup.add_slide);
12});
13
14erpnext.setup.slides_settings = [
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +053015 {
16 // Domain
17 name: 'domain',
Prateeksha Singh73517092017-12-06 13:28:06 +053018 title: __('Select your Domains'),
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +053019 fields: [
20 {
Prateeksha Singh73517092017-12-06 13:28:06 +053021 fieldname: 'domains',
22 label: __('Domains'),
23 fieldtype: 'MultiCheck',
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +053024 options: [
25 { "label": __("Distribution"), "value": "Distribution" },
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +053026 { "label": __("Manufacturing"), "value": "Manufacturing" },
27 { "label": __("Retail"), "value": "Retail" },
Prateeksha Singh95d8fd32017-09-04 11:14:04 +053028 { "label": __("Services"), "value": "Services" },
Ameya Shenoy5c623682017-12-06 18:36:27 +053029 { "label": __("Healthcare (beta)"), "value": "Healthcare" },
Ameya Shenoycc05c0b2017-12-13 18:50:39 +053030 { "label": __("Non Profit (beta)"), "value": "Non Profit" }
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +053031 ], reqd: 1
Rushabh Mehta20038ad2016-07-21 16:01:59 +053032 },
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +053033 ],
Prateeksha Singh95d8fd32017-09-04 11:14:04 +053034 // help: __('Select the nature of your business.'),
Prateeksha Singh73517092017-12-06 13:28:06 +053035 validate: function () {
36 if (this.values.domains.length === 0) {
37 frappe.msgprint(__("Please select at least one domain."));
38 return false;
39 }
40 frappe.setup.domains = this.values.domains;
41 return true;
Rushabh Mehta20038ad2016-07-21 16:01:59 +053042 },
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +053043 },
Rushabh Mehta37b4d752015-11-09 16:53:11 +053044
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +053045 {
46 // Brand
47 name: 'brand',
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +053048 icon: "fa fa-bookmark",
49 title: __("The Brand"),
Prateeksha Singh95d8fd32017-09-04 11:14:04 +053050 // help: __('Upload your letter head and logo. (you can edit them later).'),
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +053051 fields: [
52 {
53 fieldtype: "Attach Image", fieldname: "attach_logo",
54 label: __("Attach Logo"),
55 description: __("100px by 100px"),
Makarand Bauskar1d9fd9a2017-07-26 18:19:41 +053056 is_private: 0,
57 align: 'center'
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +053058 },
59 {
60 fieldname: 'company_name',
Deepesh Garg60b693f2022-04-28 12:02:54 +053061 label: __('Company Name'),
Makarand Bauskar1d9fd9a2017-07-26 18:19:41 +053062 fieldtype: 'Data',
63 reqd: 1
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +053064 },
65 {
66 fieldname: 'company_abbr',
Deepesh Garg60b693f2022-04-28 12:02:54 +053067 label: __('Company Abbreviation'),
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +053068 fieldtype: 'Data'
69 }
70 ],
71 onload: function(slide) {
72 this.bind_events(slide);
73 },
74 bind_events: function (slide) {
75 slide.get_input("company_name").on("change", function () {
76 var parts = slide.get_input("company_name").val().split(" ");
77 var abbr = $.map(parts, function (p) { return p ? p.substr(0, 1) : null }).join("");
Deepesh Garg239733ac2022-02-23 20:41:49 +053078 slide.get_field("company_abbr").set_value(abbr.slice(0, 10).toUpperCase());
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +053079 }).val(frappe.boot.sysdefaults.company_name || "").trigger("change");
Rushabh Mehta37b4d752015-11-09 16:53:11 +053080
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +053081 slide.get_input("company_abbr").on("change", function () {
Deepesh Garg239733ac2022-02-23 20:41:49 +053082 if (slide.get_input("company_abbr").val().length > 10) {
Nabin Haite6d65bc2018-02-14 17:44:06 +053083 frappe.msgprint(__("Company Abbreviation cannot have more than 5 characters"));
Makarand Bauskar30f2bcb2017-07-11 12:36:57 +053084 slide.get_field("company_abbr").set_value("");
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +053085 }
86 });
Prateeksha Singh95d8fd32017-09-04 11:14:04 +053087 },
88 validate: function() {
Makarand Bauskarb1bf5022017-10-02 11:38:03 +053089 if ((this.values.company_name || "").toLowerCase() == "company") {
90 frappe.msgprint(__("Company Name cannot be Company"));
91 return false;
92 }
Prateeksha Singh95d8fd32017-09-04 11:14:04 +053093 if (!this.values.company_abbr) {
94 return false;
95 }
Deepesh Garg239733ac2022-02-23 20:41:49 +053096 if (this.values.company_abbr.length > 10) {
Faris Ansari40c15342018-12-06 07:37:32 +053097 return false;
98 }
Prateeksha Singh95d8fd32017-09-04 11:14:04 +053099 return true;
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530100 }
101 },
102 {
103 // Organisation
104 name: 'organisation',
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530105 title: __("Your Organization"),
106 icon: "fa fa-building",
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530107 fields: [
108 {
109 fieldname: 'company_tagline',
110 label: __('What does it do?'),
111 fieldtype: 'Data',
Deepesh Garg60b693f2022-04-28 12:02:54 +0530112 placeholder: __('e.g. "Build tools for builders"'),
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530113 reqd: 1
114 },
115 { fieldname: 'bank_account', label: __('Bank Name'), fieldtype: 'Data', reqd: 1 },
116 {
117 fieldname: 'chart_of_accounts', label: __('Chart of Accounts'),
118 options: "", fieldtype: 'Select'
Anand Doshi1ed82832015-11-16 12:58:14 +0530119 },
Zarrar254ce642018-06-28 14:15:34 +0530120 { fieldname: 'view_coa', label: __('View Chart of Accounts'), fieldtype: 'Button' },
Rushabh Mehta2cd41bc2020-12-18 13:17:58 +0530121 { fieldname: 'fy_start_date', label: __('Financial Year Begins On'), fieldtype: 'Date', reqd: 1 },
122 // end date should be hidden (auto calculated)
123 { fieldname: 'fy_end_date', label: __('End Date'), fieldtype: 'Date', reqd: 1, hidden: 1 },
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530124 ],
125
126 onload: function (slide) {
127 this.load_chart_of_accounts(slide);
128 this.bind_events(slide);
129 this.set_fy_dates(slide);
130 },
131
132 validate: function () {
Deepesh Garg2908f2e2021-06-26 23:49:32 +0530133 let me = this;
134 let exist;
135
Faris Ansariaa856c42018-09-18 10:42:04 +0530136 if (!this.validate_fy_dates()) {
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530137 return false;
138 }
Zarrar441a1e12018-02-16 14:44:42 +0530139
Deepesh Garg2908f2e2021-06-26 23:49:32 +0530140 // Validate bank name
Ankush Menat4551d7d2021-08-19 13:41:10 +0530141 if(me.values.bank_account) {
Deepesh Garg2908f2e2021-06-26 23:49:32 +0530142 frappe.call({
143 async: false,
144 method: "erpnext.accounts.doctype.account.chart_of_accounts.chart_of_accounts.validate_bank_account",
145 args: {
146 "coa": me.values.chart_of_accounts,
147 "bank_account": me.values.bank_account
148 },
149 callback: function (r) {
150 if(r.message){
151 exist = r.message;
152 me.get_field("bank_account").set_value("");
153 let message = __('Account {0} already exists. Please enter a different name for your bank account.',
154 [me.values.bank_account]
155 );
156 frappe.msgprint(message);
157 }
158 }
159 });
160 return !exist; // Return False if exist = true
161 }
162
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530163 return true;
164 },
165
Faris Ansariaa856c42018-09-18 10:42:04 +0530166 validate_fy_dates: function() {
167 // validate fiscal year start and end dates
168 const invalid = this.values.fy_start_date == 'Invalid date' ||
169 this.values.fy_end_date == 'Invalid date';
170 const start_greater_than_end = this.values.fy_start_date > this.values.fy_end_date;
171
172 if (invalid || start_greater_than_end) {
173 frappe.msgprint(__("Please enter valid Financial Year Start and End Dates"));
174 return false;
175 }
176
177 return true;
178 },
179
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530180 set_fy_dates: function (slide) {
181 var country = frappe.wizard.values.country;
182
183 if (country) {
184 var fy = erpnext.setup.fiscal_years[country];
185 var current_year = moment(new Date()).year();
186 var next_year = current_year + 1;
187 if (!fy) {
Makarand Bauskarbf66d7e2017-07-05 17:26:34 +0530188 fy = ["01-01", "12-31"];
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530189 next_year = current_year;
Anand Doshi5c3469a2015-11-24 17:06:54 +0530190 }
191
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530192 var year_start_date = current_year + "-" + fy[0];
193 if (year_start_date > frappe.datetime.get_today()) {
Rushabh Mehta63b06422017-07-05 17:56:04 +0530194 next_year = current_year;
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530195 current_year -= 1;
Anand Doshi5c3469a2015-11-24 17:06:54 +0530196 }
Rushabh Mehta63b06422017-07-05 17:56:04 +0530197 slide.get_field("fy_start_date").set_value(current_year + '-' + fy[0]);
198 slide.get_field("fy_end_date").set_value(next_year + '-' + fy[1]);
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530199 }
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530200 },
Anand Doshi5c3469a2015-11-24 17:06:54 +0530201
Anand Doshi1ed82832015-11-16 12:58:14 +0530202
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530203 load_chart_of_accounts: function (slide) {
204 var country = frappe.wizard.values.country;
Anand Doshi1ed82832015-11-16 12:58:14 +0530205
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530206 if (country) {
207 frappe.call({
208 method: "erpnext.accounts.doctype.account.chart_of_accounts.chart_of_accounts.get_charts_for_country",
Zarrar254ce642018-06-28 14:15:34 +0530209 args: { "country": country, with_standard: true },
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530210 callback: function (r) {
211 if (r.message) {
212 slide.get_input("chart_of_accounts").empty()
213 .add_options(r.message);
Rushabh Mehta37b4d752015-11-09 16:53:11 +0530214 }
Anand Doshi1ed82832015-11-16 12:58:14 +0530215 }
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530216 })
Rushabh Mehta37b4d752015-11-09 16:53:11 +0530217 }
218 },
Rushabh Mehta37b4d752015-11-09 16:53:11 +0530219
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530220 bind_events: function (slide) {
Zarrar254ce642018-06-28 14:15:34 +0530221 let me = this;
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530222 slide.get_input("fy_start_date").on("change", function () {
Rushabh Mehta63b06422017-07-05 17:56:04 +0530223 var start_date = slide.form.fields_dict.fy_start_date.get_value();
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530224 var year_end_date =
Rushabh Mehta63b06422017-07-05 17:56:04 +0530225 frappe.datetime.add_days(frappe.datetime.add_months(start_date, 12), -1);
226 slide.form.fields_dict.fy_end_date.set_value(year_end_date);
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530227 });
Zarrar254ce642018-06-28 14:15:34 +0530228
229 slide.get_input("view_coa").on("click", function() {
230 let chart_template = slide.form.fields_dict.chart_of_accounts.get_value();
231 if(!chart_template) return;
232
233 me.charts_modal(slide, chart_template);
234 });
235 },
236
237 charts_modal: function(slide, chart_template) {
238 let parent = __('All Accounts');
239
240 var dialog = new frappe.ui.Dialog({
241 title: chart_template,
242 fields: [
243 {'fieldname': 'expand_all', 'label': __('Expand All'), 'fieldtype': 'Button',
244 click: function() {
245 // expand all nodes on button click
246 coa_tree.load_children(coa_tree.root_node, true);
247 }
248 },
249 {'fieldname': 'collapse_all', 'label': __('Collapse All'), 'fieldtype': 'Button',
250 click: function() {
251 // collapse all nodes
252 coa_tree.get_all_nodes(coa_tree.root_node.data.value, coa_tree.root_node.is_root)
253 .then(data_list => {
254 data_list.map(d => { coa_tree.toggle_node(coa_tree.nodes[d.parent]); });
255 });
256 }
257 }
258 ]
259 });
260
261 // render tree structure in the dialog modal
262 let coa_tree = new frappe.ui.Tree({
263 parent: $(dialog.body),
264 label: parent,
265 expandable: true,
266 method: 'erpnext.accounts.utils.get_coa',
267 args: {
268 chart: chart_template,
269 parent: parent,
270 doctype: 'Account'
271 },
272 onclick: function(node) {
273 parent = node.value;
274 }
275 });
276
277 // add class to show buttons side by side
Zarrarc1225a82018-07-03 10:35:31 +0530278 const form_container = $(dialog.body).find('form');
279 const buttons = $(form_container).find('.frappe-control');
280 form_container.addClass('flex');
281 buttons.map((index, button) => {
282 $(button).css({"margin-right": "1em"});
283 })
Zarrar254ce642018-06-28 14:15:34 +0530284
285 dialog.show();
286 coa_tree.load_children(coa_tree.root_node, true); // expand all node trigger
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530287 }
Anand Doshi31771382016-07-05 21:34:21 +0530288 }
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530289];
Anand Doshi31771382016-07-05 21:34:21 +0530290
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530291// Source: https://en.wikipedia.org/wiki/Fiscal_year
292// default 1st Jan - 31st Dec
Rushabh Mehta20038ad2016-07-21 16:01:59 +0530293
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530294erpnext.setup.fiscal_years = {
Shreya Shahdb499dc2017-12-12 14:19:35 +0530295 "Afghanistan": ["12-21", "12-20"],
Makarand Bauskarbf66d7e2017-07-05 17:26:34 +0530296 "Australia": ["07-01", "06-30"],
297 "Bangladesh": ["07-01", "06-30"],
298 "Canada": ["04-01", "03-31"],
299 "Costa Rica": ["10-01", "09-30"],
300 "Egypt": ["07-01", "06-30"],
301 "Hong Kong": ["04-01", "03-31"],
302 "India": ["04-01", "03-31"],
303 "Iran": ["06-23", "06-22"],
Makarand Bauskarbf66d7e2017-07-05 17:26:34 +0530304 "Myanmar": ["04-01", "03-31"],
305 "New Zealand": ["04-01", "03-31"],
306 "Pakistan": ["07-01", "06-30"],
307 "Singapore": ["04-01", "03-31"],
308 "South Africa": ["03-01", "02-28"],
309 "Thailand": ["10-01", "09-30"],
310 "United Kingdom": ["04-01", "03-31"],
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530311};