blob: a067ec0f4a47016681b6a1464348bbba03da1945 [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 },
27 {
28 fieldname: 'company_abbr',
Deepesh Garg60b693f2022-04-28 12:02:54 +053029 label: __('Company Abbreviation'),
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +053030 fieldtype: 'Data',
Deepesh Garg3598bcc2023-01-27 14:40:39 +053031 hidden: 1
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +053032 },
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +053033 {
34 fieldname: 'chart_of_accounts', label: __('Chart of Accounts'),
35 options: "", fieldtype: 'Select'
Anand Doshi1ed82832015-11-16 12:58:14 +053036 },
Zarrar254ce642018-06-28 14:15:34 +053037 { fieldname: 'view_coa', label: __('View Chart of Accounts'), fieldtype: 'Button' },
Rushabh Mehta2cd41bc2020-12-18 13:17:58 +053038 { 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 },
Deepesh Gargbb5387f2023-07-07 10:49:56 +053041 { fieldname: 'setup_demo', label: __('Generate dummy data for demo'), fieldtype: 'Check'},
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +053042 ],
43
44 onload: function (slide) {
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +053045 this.bind_events(slide);
Deepesh Garg3598bcc2023-01-27 14:40:39 +053046 this.load_chart_of_accounts(slide);
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +053047 this.set_fy_dates(slide);
48 },
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +053049 validate: function () {
Faris Ansariaa856c42018-09-18 10:42:04 +053050 if (!this.validate_fy_dates()) {
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +053051 return false;
52 }
Zarrar441a1e12018-02-16 14:44:42 +053053
Deepesh Garg3598bcc2023-01-27 14:40:39 +053054 if ((this.values.company_name || "").toLowerCase() == "company") {
55 frappe.msgprint(__("Company Name cannot be Company"));
56 return false;
57 }
58 if (!this.values.company_abbr) {
59 return false;
60 }
61 if (this.values.company_abbr.length > 10) {
62 return false;
Deepesh Garg2908f2e2021-06-26 23:49:32 +053063 }
64
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +053065 return true;
66 },
67
Faris Ansariaa856c42018-09-18 10:42:04 +053068 validate_fy_dates: function() {
69 // validate fiscal year start and end dates
70 const invalid = this.values.fy_start_date == 'Invalid date' ||
71 this.values.fy_end_date == 'Invalid date';
72 const start_greater_than_end = this.values.fy_start_date > this.values.fy_end_date;
73
74 if (invalid || start_greater_than_end) {
75 frappe.msgprint(__("Please enter valid Financial Year Start and End Dates"));
76 return false;
77 }
78
79 return true;
80 },
81
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +053082 set_fy_dates: function (slide) {
83 var country = frappe.wizard.values.country;
84
85 if (country) {
Deepesh Garg3598bcc2023-01-27 14:40:39 +053086 let fy = erpnext.setup.fiscal_years[country];
87 let current_year = moment(new Date()).year();
88 let next_year = current_year + 1;
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +053089 if (!fy) {
Makarand Bauskarbf66d7e2017-07-05 17:26:34 +053090 fy = ["01-01", "12-31"];
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +053091 next_year = current_year;
Anand Doshi5c3469a2015-11-24 17:06:54 +053092 }
93
Deepesh Garg3598bcc2023-01-27 14:40:39 +053094 let year_start_date = current_year + "-" + fy[0];
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +053095 if (year_start_date > frappe.datetime.get_today()) {
Rushabh Mehta63b06422017-07-05 17:56:04 +053096 next_year = current_year;
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +053097 current_year -= 1;
Anand Doshi5c3469a2015-11-24 17:06:54 +053098 }
Rushabh Mehta63b06422017-07-05 17:56:04 +053099 slide.get_field("fy_start_date").set_value(current_year + '-' + fy[0]);
100 slide.get_field("fy_end_date").set_value(next_year + '-' + fy[1]);
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530101 }
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530102 },
Anand Doshi5c3469a2015-11-24 17:06:54 +0530103
Anand Doshi1ed82832015-11-16 12:58:14 +0530104
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530105 load_chart_of_accounts: function (slide) {
Deepesh Garg3598bcc2023-01-27 14:40:39 +0530106 let country = frappe.wizard.values.country;
Anand Doshi1ed82832015-11-16 12:58:14 +0530107
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530108 if (country) {
109 frappe.call({
110 method: "erpnext.accounts.doctype.account.chart_of_accounts.chart_of_accounts.get_charts_for_country",
Zarrar254ce642018-06-28 14:15:34 +0530111 args: { "country": country, with_standard: true },
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530112 callback: function (r) {
113 if (r.message) {
114 slide.get_input("chart_of_accounts").empty()
115 .add_options(r.message);
Rushabh Mehta37b4d752015-11-09 16:53:11 +0530116 }
Anand Doshi1ed82832015-11-16 12:58:14 +0530117 }
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530118 })
Rushabh Mehta37b4d752015-11-09 16:53:11 +0530119 }
120 },
Rushabh Mehta37b4d752015-11-09 16:53:11 +0530121
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530122 bind_events: function (slide) {
Zarrar254ce642018-06-28 14:15:34 +0530123 let me = this;
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530124 slide.get_input("fy_start_date").on("change", function () {
Rushabh Mehta63b06422017-07-05 17:56:04 +0530125 var start_date = slide.form.fields_dict.fy_start_date.get_value();
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530126 var year_end_date =
Rushabh Mehta63b06422017-07-05 17:56:04 +0530127 frappe.datetime.add_days(frappe.datetime.add_months(start_date, 12), -1);
128 slide.form.fields_dict.fy_end_date.set_value(year_end_date);
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530129 });
Zarrar254ce642018-06-28 14:15:34 +0530130
131 slide.get_input("view_coa").on("click", function() {
132 let chart_template = slide.form.fields_dict.chart_of_accounts.get_value();
133 if(!chart_template) return;
134
135 me.charts_modal(slide, chart_template);
136 });
Deepesh Garg3598bcc2023-01-27 14:40:39 +0530137
138 slide.get_input("company_name").on("change", function () {
139 let parts = slide.get_input("company_name").val().split(" ");
140 let abbr = $.map(parts, function (p) { return p ? p.substr(0, 1) : null }).join("");
141 slide.get_field("company_abbr").set_value(abbr.slice(0, 10).toUpperCase());
142 }).val(frappe.boot.sysdefaults.company_name || "").trigger("change");
143
144 slide.get_input("company_abbr").on("change", function () {
145 if (slide.get_input("company_abbr").val().length > 10) {
146 frappe.msgprint(__("Company Abbreviation cannot have more than 5 characters"));
147 slide.get_field("company_abbr").set_value("");
148 }
149 });
Zarrar254ce642018-06-28 14:15:34 +0530150 },
151
152 charts_modal: function(slide, chart_template) {
153 let parent = __('All Accounts');
154
Deepesh Garg3598bcc2023-01-27 14:40:39 +0530155 let dialog = new frappe.ui.Dialog({
Zarrar254ce642018-06-28 14:15:34 +0530156 title: chart_template,
157 fields: [
158 {'fieldname': 'expand_all', 'label': __('Expand All'), 'fieldtype': 'Button',
159 click: function() {
160 // expand all nodes on button click
161 coa_tree.load_children(coa_tree.root_node, true);
162 }
163 },
164 {'fieldname': 'collapse_all', 'label': __('Collapse All'), 'fieldtype': 'Button',
165 click: function() {
166 // collapse all nodes
167 coa_tree.get_all_nodes(coa_tree.root_node.data.value, coa_tree.root_node.is_root)
168 .then(data_list => {
169 data_list.map(d => { coa_tree.toggle_node(coa_tree.nodes[d.parent]); });
170 });
171 }
172 }
173 ]
174 });
175
176 // render tree structure in the dialog modal
177 let coa_tree = new frappe.ui.Tree({
178 parent: $(dialog.body),
179 label: parent,
180 expandable: true,
181 method: 'erpnext.accounts.utils.get_coa',
182 args: {
183 chart: chart_template,
184 parent: parent,
185 doctype: 'Account'
186 },
187 onclick: function(node) {
188 parent = node.value;
189 }
190 });
191
192 // add class to show buttons side by side
Zarrarc1225a82018-07-03 10:35:31 +0530193 const form_container = $(dialog.body).find('form');
194 const buttons = $(form_container).find('.frappe-control');
195 form_container.addClass('flex');
196 buttons.map((index, button) => {
197 $(button).css({"margin-right": "1em"});
198 })
Zarrar254ce642018-06-28 14:15:34 +0530199
200 dialog.show();
201 coa_tree.load_children(coa_tree.root_node, true); // expand all node trigger
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530202 }
Anand Doshi31771382016-07-05 21:34:21 +0530203 }
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530204];
Anand Doshi31771382016-07-05 21:34:21 +0530205
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530206// Source: https://en.wikipedia.org/wiki/Fiscal_year
207// default 1st Jan - 31st Dec
Rushabh Mehta20038ad2016-07-21 16:01:59 +0530208
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530209erpnext.setup.fiscal_years = {
Shreya Shahdb499dc2017-12-12 14:19:35 +0530210 "Afghanistan": ["12-21", "12-20"],
Makarand Bauskarbf66d7e2017-07-05 17:26:34 +0530211 "Australia": ["07-01", "06-30"],
212 "Bangladesh": ["07-01", "06-30"],
213 "Canada": ["04-01", "03-31"],
214 "Costa Rica": ["10-01", "09-30"],
215 "Egypt": ["07-01", "06-30"],
216 "Hong Kong": ["04-01", "03-31"],
217 "India": ["04-01", "03-31"],
218 "Iran": ["06-23", "06-22"],
Makarand Bauskarbf66d7e2017-07-05 17:26:34 +0530219 "Myanmar": ["04-01", "03-31"],
220 "New Zealand": ["04-01", "03-31"],
221 "Pakistan": ["07-01", "06-30"],
222 "Singapore": ["04-01", "03-31"],
223 "South Africa": ["03-01", "02-28"],
224 "Thailand": ["10-01", "09-30"],
225 "United Kingdom": ["04-01", "03-31"],
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530226};