blob: ba200ef168179bc17105ba3e9cfc4f5f5ae85300 [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 {
Deepesh Garg3598bcc2023-01-27 14:40:39 +053016 // Organization
17 name: 'organization',
18 title: __("Setup your organization"),
19 icon: "fa fa-building",
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +053020 fields: [
21 {
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +053022 fieldname: 'company_name',
Deepesh Garg60b693f2022-04-28 12:02:54 +053023 label: __('Company Name'),
Makarand Bauskar1d9fd9a2017-07-26 18:19:41 +053024 fieldtype: 'Data',
25 reqd: 1
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +053026 },
Bernd Oliver Sünderhaufbc8d05d2023-07-30 19:04:03 +020027 { fieldtype: "Column Break" },
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +053028 {
29 fieldname: 'company_abbr',
Deepesh Garg60b693f2022-04-28 12:02:54 +053030 label: __('Company Abbreviation'),
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +053031 fieldtype: 'Data',
Bernd Oliver Sünderhaufbc8d05d2023-07-30 19:04:03 +020032 reqd: 1
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +053033 },
Bernd Oliver Sünderhaufbc8d05d2023-07-30 19:04:03 +020034 { fieldtype: "Section Break" },
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +053035 {
36 fieldname: 'chart_of_accounts', label: __('Chart of Accounts'),
37 options: "", fieldtype: 'Select'
Anand Doshi1ed82832015-11-16 12:58:14 +053038 },
Zarrar254ce642018-06-28 14:15:34 +053039 { fieldname: 'view_coa', label: __('View Chart of Accounts'), fieldtype: 'Button' },
Rushabh Mehta2cd41bc2020-12-18 13:17:58 +053040 { fieldname: 'fy_start_date', label: __('Financial Year Begins On'), fieldtype: 'Date', reqd: 1 },
41 // end date should be hidden (auto calculated)
42 { fieldname: 'fy_end_date', label: __('End Date'), fieldtype: 'Date', reqd: 1, hidden: 1 },
Ankush Menat3a21c902023-08-10 15:48:57 +053043 { fieldtype: "Section Break" },
44 {
45 fieldname: 'setup_demo',
46 label: __('Generate Demo Data for Exploration'),
47 fieldtype: 'Check',
48 description: 'If checked, we will create demo data for you to explore the system. This demo data can be erased later.'},
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +053049 ],
50
51 onload: function (slide) {
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +053052 this.bind_events(slide);
Deepesh Garg3598bcc2023-01-27 14:40:39 +053053 this.load_chart_of_accounts(slide);
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +053054 this.set_fy_dates(slide);
55 },
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +053056 validate: function () {
Faris Ansariaa856c42018-09-18 10:42:04 +053057 if (!this.validate_fy_dates()) {
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +053058 return false;
59 }
Zarrar441a1e12018-02-16 14:44:42 +053060
Deepesh Garg3598bcc2023-01-27 14:40:39 +053061 if ((this.values.company_name || "").toLowerCase() == "company") {
62 frappe.msgprint(__("Company Name cannot be Company"));
63 return false;
64 }
65 if (!this.values.company_abbr) {
66 return false;
67 }
68 if (this.values.company_abbr.length > 10) {
69 return false;
Deepesh Garg2908f2e2021-06-26 23:49:32 +053070 }
71
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +053072 return true;
73 },
74
Faris Ansariaa856c42018-09-18 10:42:04 +053075 validate_fy_dates: function() {
76 // validate fiscal year start and end dates
77 const invalid = this.values.fy_start_date == 'Invalid date' ||
78 this.values.fy_end_date == 'Invalid date';
79 const start_greater_than_end = this.values.fy_start_date > this.values.fy_end_date;
80
81 if (invalid || start_greater_than_end) {
82 frappe.msgprint(__("Please enter valid Financial Year Start and End Dates"));
83 return false;
84 }
85
86 return true;
87 },
88
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +053089 set_fy_dates: function (slide) {
90 var country = frappe.wizard.values.country;
91
92 if (country) {
Deepesh Garg3598bcc2023-01-27 14:40:39 +053093 let fy = erpnext.setup.fiscal_years[country];
94 let current_year = moment(new Date()).year();
95 let next_year = current_year + 1;
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +053096 if (!fy) {
Makarand Bauskarbf66d7e2017-07-05 17:26:34 +053097 fy = ["01-01", "12-31"];
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +053098 next_year = current_year;
Anand Doshi5c3469a2015-11-24 17:06:54 +053099 }
100
Deepesh Garg3598bcc2023-01-27 14:40:39 +0530101 let year_start_date = current_year + "-" + fy[0];
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530102 if (year_start_date > frappe.datetime.get_today()) {
Rushabh Mehta63b06422017-07-05 17:56:04 +0530103 next_year = current_year;
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530104 current_year -= 1;
Anand Doshi5c3469a2015-11-24 17:06:54 +0530105 }
Rushabh Mehta63b06422017-07-05 17:56:04 +0530106 slide.get_field("fy_start_date").set_value(current_year + '-' + fy[0]);
107 slide.get_field("fy_end_date").set_value(next_year + '-' + fy[1]);
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530108 }
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530109 },
Anand Doshi5c3469a2015-11-24 17:06:54 +0530110
Anand Doshi1ed82832015-11-16 12:58:14 +0530111
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530112 load_chart_of_accounts: function (slide) {
Deepesh Garg3598bcc2023-01-27 14:40:39 +0530113 let country = frappe.wizard.values.country;
Anand Doshi1ed82832015-11-16 12:58:14 +0530114
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530115 if (country) {
116 frappe.call({
117 method: "erpnext.accounts.doctype.account.chart_of_accounts.chart_of_accounts.get_charts_for_country",
Zarrar254ce642018-06-28 14:15:34 +0530118 args: { "country": country, with_standard: true },
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530119 callback: function (r) {
120 if (r.message) {
121 slide.get_input("chart_of_accounts").empty()
122 .add_options(r.message);
Rushabh Mehta37b4d752015-11-09 16:53:11 +0530123 }
Anand Doshi1ed82832015-11-16 12:58:14 +0530124 }
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530125 })
Rushabh Mehta37b4d752015-11-09 16:53:11 +0530126 }
127 },
Rushabh Mehta37b4d752015-11-09 16:53:11 +0530128
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530129 bind_events: function (slide) {
Zarrar254ce642018-06-28 14:15:34 +0530130 let me = this;
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530131 slide.get_input("fy_start_date").on("change", function () {
Rushabh Mehta63b06422017-07-05 17:56:04 +0530132 var start_date = slide.form.fields_dict.fy_start_date.get_value();
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530133 var year_end_date =
Rushabh Mehta63b06422017-07-05 17:56:04 +0530134 frappe.datetime.add_days(frappe.datetime.add_months(start_date, 12), -1);
135 slide.form.fields_dict.fy_end_date.set_value(year_end_date);
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530136 });
Zarrar254ce642018-06-28 14:15:34 +0530137
138 slide.get_input("view_coa").on("click", function() {
139 let chart_template = slide.form.fields_dict.chart_of_accounts.get_value();
140 if(!chart_template) return;
141
142 me.charts_modal(slide, chart_template);
143 });
Deepesh Garg3598bcc2023-01-27 14:40:39 +0530144
Bernd Oliver Sünderhaufbc8d05d2023-07-30 19:04:03 +0200145 slide.get_input("company_name").on("input", function () {
Deepesh Garg3598bcc2023-01-27 14:40:39 +0530146 let parts = slide.get_input("company_name").val().split(" ");
147 let abbr = $.map(parts, function (p) { return p ? p.substr(0, 1) : null }).join("");
148 slide.get_field("company_abbr").set_value(abbr.slice(0, 10).toUpperCase());
149 }).val(frappe.boot.sysdefaults.company_name || "").trigger("change");
150
151 slide.get_input("company_abbr").on("change", function () {
Bernd Oliver Sünderhaufbc8d05d2023-07-30 19:04:03 +0200152 let abbr = slide.get_input("company_abbr").val();
153 if (abbr.length > 10) {
Deepesh Garg3598bcc2023-01-27 14:40:39 +0530154 frappe.msgprint(__("Company Abbreviation cannot have more than 5 characters"));
Bernd Oliver Sünderhaufbc8d05d2023-07-30 19:04:03 +0200155 abbr = abbr.slice(0, 10);
Deepesh Garg3598bcc2023-01-27 14:40:39 +0530156 }
Bernd Oliver Sünderhaufbc8d05d2023-07-30 19:04:03 +0200157 slide.get_field("company_abbr").set_value(abbr);
158 }).val(frappe.boot.sysdefaults.company_abbr || "").trigger("change");
Zarrar254ce642018-06-28 14:15:34 +0530159 },
160
161 charts_modal: function(slide, chart_template) {
162 let parent = __('All Accounts');
163
Deepesh Garg3598bcc2023-01-27 14:40:39 +0530164 let dialog = new frappe.ui.Dialog({
Zarrar254ce642018-06-28 14:15:34 +0530165 title: chart_template,
166 fields: [
167 {'fieldname': 'expand_all', 'label': __('Expand All'), 'fieldtype': 'Button',
168 click: function() {
169 // expand all nodes on button click
170 coa_tree.load_children(coa_tree.root_node, true);
171 }
172 },
173 {'fieldname': 'collapse_all', 'label': __('Collapse All'), 'fieldtype': 'Button',
174 click: function() {
175 // collapse all nodes
176 coa_tree.get_all_nodes(coa_tree.root_node.data.value, coa_tree.root_node.is_root)
177 .then(data_list => {
178 data_list.map(d => { coa_tree.toggle_node(coa_tree.nodes[d.parent]); });
179 });
180 }
181 }
182 ]
183 });
184
185 // render tree structure in the dialog modal
186 let coa_tree = new frappe.ui.Tree({
187 parent: $(dialog.body),
188 label: parent,
189 expandable: true,
190 method: 'erpnext.accounts.utils.get_coa',
191 args: {
192 chart: chart_template,
193 parent: parent,
194 doctype: 'Account'
195 },
196 onclick: function(node) {
197 parent = node.value;
198 }
199 });
200
201 // add class to show buttons side by side
Zarrarc1225a82018-07-03 10:35:31 +0530202 const form_container = $(dialog.body).find('form');
203 const buttons = $(form_container).find('.frappe-control');
204 form_container.addClass('flex');
205 buttons.map((index, button) => {
206 $(button).css({"margin-right": "1em"});
207 })
Zarrar254ce642018-06-28 14:15:34 +0530208
209 dialog.show();
210 coa_tree.load_children(coa_tree.root_node, true); // expand all node trigger
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530211 }
Anand Doshi31771382016-07-05 21:34:21 +0530212 }
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530213];
Anand Doshi31771382016-07-05 21:34:21 +0530214
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530215// Source: https://en.wikipedia.org/wiki/Fiscal_year
216// default 1st Jan - 31st Dec
Rushabh Mehta20038ad2016-07-21 16:01:59 +0530217
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530218erpnext.setup.fiscal_years = {
Shreya Shahdb499dc2017-12-12 14:19:35 +0530219 "Afghanistan": ["12-21", "12-20"],
Makarand Bauskarbf66d7e2017-07-05 17:26:34 +0530220 "Australia": ["07-01", "06-30"],
221 "Bangladesh": ["07-01", "06-30"],
222 "Canada": ["04-01", "03-31"],
223 "Costa Rica": ["10-01", "09-30"],
224 "Egypt": ["07-01", "06-30"],
225 "Hong Kong": ["04-01", "03-31"],
226 "India": ["04-01", "03-31"],
227 "Iran": ["06-23", "06-22"],
Makarand Bauskarbf66d7e2017-07-05 17:26:34 +0530228 "Myanmar": ["04-01", "03-31"],
229 "New Zealand": ["04-01", "03-31"],
230 "Pakistan": ["07-01", "06-30"],
231 "Singapore": ["04-01", "03-31"],
232 "South Africa": ["03-01", "02-28"],
233 "Thailand": ["10-01", "09-30"],
234 "United Kingdom": ["04-01", "03-31"],
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530235};