blob: 092f83903eaf6209defbe87d6164203449f2b405 [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" },
Ameya Shenoycc05c0b2017-12-13 18:50:39 +053026 { "label": __("Education"), "value": "Education" },
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +053027 { "label": __("Manufacturing"), "value": "Manufacturing" },
28 { "label": __("Retail"), "value": "Retail" },
Prateeksha Singh95d8fd32017-09-04 11:14:04 +053029 { "label": __("Services"), "value": "Services" },
Ameya Shenoycc05c0b2017-12-13 18:50:39 +053030 { "label": __("Agriculture (beta)"), "value": "Agriculture" },
Ameya Shenoy5c623682017-12-06 18:36:27 +053031 { "label": __("Healthcare (beta)"), "value": "Healthcare" },
Ameya Shenoycc05c0b2017-12-13 18:50:39 +053032 { "label": __("Non Profit (beta)"), "value": "Non Profit" }
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +053033 ], reqd: 1
Rushabh Mehta20038ad2016-07-21 16:01:59 +053034 },
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +053035 ],
Prateeksha Singh95d8fd32017-09-04 11:14:04 +053036 // help: __('Select the nature of your business.'),
Prateeksha Singh73517092017-12-06 13:28:06 +053037 validate: function () {
38 if (this.values.domains.length === 0) {
39 frappe.msgprint(__("Please select at least one domain."));
40 return false;
41 }
42 frappe.setup.domains = this.values.domains;
43 return true;
Rushabh Mehta20038ad2016-07-21 16:01:59 +053044 },
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +053045 },
Rushabh Mehta37b4d752015-11-09 16:53:11 +053046
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +053047 {
48 // Brand
49 name: 'brand',
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +053050 icon: "fa fa-bookmark",
51 title: __("The Brand"),
Prateeksha Singh95d8fd32017-09-04 11:14:04 +053052 // help: __('Upload your letter head and logo. (you can edit them later).'),
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +053053 fields: [
54 {
55 fieldtype: "Attach Image", fieldname: "attach_logo",
56 label: __("Attach Logo"),
57 description: __("100px by 100px"),
Makarand Bauskar1d9fd9a2017-07-26 18:19:41 +053058 is_private: 0,
59 align: 'center'
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +053060 },
61 {
62 fieldname: 'company_name',
Prateeksha Singh73517092017-12-06 13:28:06 +053063 label: frappe.setup.domains.includes('Education') ?
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +053064 __('Institute Name') : __('Company Name'),
Makarand Bauskar1d9fd9a2017-07-26 18:19:41 +053065 fieldtype: 'Data',
66 reqd: 1
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +053067 },
68 {
69 fieldname: 'company_abbr',
Prateeksha Singh73517092017-12-06 13:28:06 +053070 label: frappe.setup.domains.includes('Education') ?
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +053071 __('Institute Abbreviation') : __('Company Abbreviation'),
72 fieldtype: 'Data'
73 }
74 ],
75 onload: function(slide) {
76 this.bind_events(slide);
77 },
78 bind_events: function (slide) {
79 slide.get_input("company_name").on("change", function () {
80 var parts = slide.get_input("company_name").val().split(" ");
81 var abbr = $.map(parts, function (p) { return p ? p.substr(0, 1) : null }).join("");
Makarand Bauskar30f2bcb2017-07-11 12:36:57 +053082 slide.get_field("company_abbr").set_value(abbr.slice(0, 5).toUpperCase());
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +053083 }).val(frappe.boot.sysdefaults.company_name || "").trigger("change");
Rushabh Mehta37b4d752015-11-09 16:53:11 +053084
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +053085 slide.get_input("company_abbr").on("change", function () {
86 if (slide.get_input("company_abbr").val().length > 5) {
Nabin Haite6d65bc2018-02-14 17:44:06 +053087 frappe.msgprint(__("Company Abbreviation cannot have more than 5 characters"));
Makarand Bauskar30f2bcb2017-07-11 12:36:57 +053088 slide.get_field("company_abbr").set_value("");
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +053089 }
90 });
Prateeksha Singh95d8fd32017-09-04 11:14:04 +053091 },
92 validate: function() {
Makarand Bauskarb1bf5022017-10-02 11:38:03 +053093 if ((this.values.company_name || "").toLowerCase() == "company") {
94 frappe.msgprint(__("Company Name cannot be Company"));
95 return false;
96 }
Prateeksha Singh95d8fd32017-09-04 11:14:04 +053097 if (!this.values.company_abbr) {
98 return false;
99 }
Faris Ansari40c15342018-12-06 07:37:32 +0530100 if (this.values.company_abbr.length > 5) {
101 return false;
102 }
Prateeksha Singh95d8fd32017-09-04 11:14:04 +0530103 return true;
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530104 }
105 },
106 {
107 // Organisation
108 name: 'organisation',
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530109 title: __("Your Organization"),
110 icon: "fa fa-building",
Prateeksha Singh73517092017-12-06 13:28:06 +0530111 // help: frappe.setup.domains.includes('Education') ?
Prateeksha Singh95d8fd32017-09-04 11:14:04 +0530112 // __('The name of the institute for which you are setting up this system.') :
113 // __('The name of your company for which you are setting up this system.')),
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530114 fields: [
115 {
116 fieldname: 'company_tagline',
117 label: __('What does it do?'),
118 fieldtype: 'Data',
Prateeksha Singh73517092017-12-06 13:28:06 +0530119 placeholder: frappe.setup.domains.includes('Education') ?
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530120 __('e.g. "Primary School" or "University"') :
121 __('e.g. "Build tools for builders"'),
122 reqd: 1
123 },
124 { fieldname: 'bank_account', label: __('Bank Name'), fieldtype: 'Data', reqd: 1 },
125 {
126 fieldname: 'chart_of_accounts', label: __('Chart of Accounts'),
127 options: "", fieldtype: 'Select'
Anand Doshi1ed82832015-11-16 12:58:14 +0530128 },
Zarrar254ce642018-06-28 14:15:34 +0530129 { fieldname: 'view_coa', label: __('View Chart of Accounts'), fieldtype: 'Button' },
Anand Doshi1ed82832015-11-16 12:58:14 +0530130
strixaluco372a8812017-07-17 14:36:25 +0800131 { fieldtype: "Section Break", label: __('Financial Year') },
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530132 { fieldname: 'fy_start_date', label: __('Start Date'), fieldtype: 'Date', reqd: 1 },
133 { fieldtype: "Column Break" },
134 { fieldname: 'fy_end_date', label: __('End Date'), fieldtype: 'Date', reqd: 1 },
135 ],
136
137 onload: function (slide) {
138 this.load_chart_of_accounts(slide);
139 this.bind_events(slide);
140 this.set_fy_dates(slide);
141 },
142
143 validate: function () {
Zarrar441a1e12018-02-16 14:44:42 +0530144 let me = this;
145 let exist;
146
Faris Ansariaa856c42018-09-18 10:42:04 +0530147 if (!this.validate_fy_dates()) {
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530148 return false;
149 }
Zarrar441a1e12018-02-16 14:44:42 +0530150
151 // Validate bank name
152 if(me.values.bank_account){
153 frappe.call({
154 async: false,
155 method: "erpnext.accounts.doctype.account.chart_of_accounts.chart_of_accounts.validate_bank_account",
156 args: {
157 "coa": me.values.chart_of_accounts,
158 "bank_account": me.values.bank_account
159 },
160 callback: function (r) {
161 if(r.message){
162 exist = r.message;
163 me.get_field("bank_account").set_value("");
Raffael Meyerfbcc3c12020-11-25 04:41:51 +0100164 let message = __('Account {0} already exists. Please enter a different name for your bank account.',
165 [me.values.bank_account]
166 );
167 frappe.msgprint(message);
Zarrar441a1e12018-02-16 14:44:42 +0530168 }
169 }
170 });
171 return !exist; // Return False if exist = true
172 }
173
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530174 return true;
175 },
176
Faris Ansariaa856c42018-09-18 10:42:04 +0530177 validate_fy_dates: function() {
178 // validate fiscal year start and end dates
179 const invalid = this.values.fy_start_date == 'Invalid date' ||
180 this.values.fy_end_date == 'Invalid date';
181 const start_greater_than_end = this.values.fy_start_date > this.values.fy_end_date;
182
183 if (invalid || start_greater_than_end) {
184 frappe.msgprint(__("Please enter valid Financial Year Start and End Dates"));
185 return false;
186 }
187
188 return true;
189 },
190
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530191 set_fy_dates: function (slide) {
192 var country = frappe.wizard.values.country;
193
194 if (country) {
195 var fy = erpnext.setup.fiscal_years[country];
196 var current_year = moment(new Date()).year();
197 var next_year = current_year + 1;
198 if (!fy) {
Makarand Bauskarbf66d7e2017-07-05 17:26:34 +0530199 fy = ["01-01", "12-31"];
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530200 next_year = current_year;
Anand Doshi5c3469a2015-11-24 17:06:54 +0530201 }
202
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530203 var year_start_date = current_year + "-" + fy[0];
204 if (year_start_date > frappe.datetime.get_today()) {
Rushabh Mehta63b06422017-07-05 17:56:04 +0530205 next_year = current_year;
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530206 current_year -= 1;
Anand Doshi5c3469a2015-11-24 17:06:54 +0530207 }
Rushabh Mehta63b06422017-07-05 17:56:04 +0530208 slide.get_field("fy_start_date").set_value(current_year + '-' + fy[0]);
209 slide.get_field("fy_end_date").set_value(next_year + '-' + fy[1]);
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530210 }
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530211 },
Anand Doshi5c3469a2015-11-24 17:06:54 +0530212
Anand Doshi1ed82832015-11-16 12:58:14 +0530213
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530214 load_chart_of_accounts: function (slide) {
215 var country = frappe.wizard.values.country;
Anand Doshi1ed82832015-11-16 12:58:14 +0530216
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530217 if (country) {
218 frappe.call({
219 method: "erpnext.accounts.doctype.account.chart_of_accounts.chart_of_accounts.get_charts_for_country",
Zarrar254ce642018-06-28 14:15:34 +0530220 args: { "country": country, with_standard: true },
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530221 callback: function (r) {
222 if (r.message) {
223 slide.get_input("chart_of_accounts").empty()
224 .add_options(r.message);
Rushabh Mehta37b4d752015-11-09 16:53:11 +0530225 }
Anand Doshi1ed82832015-11-16 12:58:14 +0530226 }
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530227 })
Rushabh Mehta37b4d752015-11-09 16:53:11 +0530228 }
229 },
Rushabh Mehta37b4d752015-11-09 16:53:11 +0530230
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530231 bind_events: function (slide) {
Zarrar254ce642018-06-28 14:15:34 +0530232 let me = this;
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530233 slide.get_input("fy_start_date").on("change", function () {
Rushabh Mehta63b06422017-07-05 17:56:04 +0530234 var start_date = slide.form.fields_dict.fy_start_date.get_value();
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530235 var year_end_date =
Rushabh Mehta63b06422017-07-05 17:56:04 +0530236 frappe.datetime.add_days(frappe.datetime.add_months(start_date, 12), -1);
237 slide.form.fields_dict.fy_end_date.set_value(year_end_date);
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530238 });
Zarrar254ce642018-06-28 14:15:34 +0530239
240 slide.get_input("view_coa").on("click", function() {
241 let chart_template = slide.form.fields_dict.chart_of_accounts.get_value();
242 if(!chart_template) return;
243
244 me.charts_modal(slide, chart_template);
245 });
246 },
247
248 charts_modal: function(slide, chart_template) {
249 let parent = __('All Accounts');
250
251 var dialog = new frappe.ui.Dialog({
252 title: chart_template,
253 fields: [
254 {'fieldname': 'expand_all', 'label': __('Expand All'), 'fieldtype': 'Button',
255 click: function() {
256 // expand all nodes on button click
257 coa_tree.load_children(coa_tree.root_node, true);
258 }
259 },
260 {'fieldname': 'collapse_all', 'label': __('Collapse All'), 'fieldtype': 'Button',
261 click: function() {
262 // collapse all nodes
263 coa_tree.get_all_nodes(coa_tree.root_node.data.value, coa_tree.root_node.is_root)
264 .then(data_list => {
265 data_list.map(d => { coa_tree.toggle_node(coa_tree.nodes[d.parent]); });
266 });
267 }
268 }
269 ]
270 });
271
272 // render tree structure in the dialog modal
273 let coa_tree = new frappe.ui.Tree({
274 parent: $(dialog.body),
275 label: parent,
276 expandable: true,
277 method: 'erpnext.accounts.utils.get_coa',
278 args: {
279 chart: chart_template,
280 parent: parent,
281 doctype: 'Account'
282 },
283 onclick: function(node) {
284 parent = node.value;
285 }
286 });
287
288 // add class to show buttons side by side
Zarrarc1225a82018-07-03 10:35:31 +0530289 const form_container = $(dialog.body).find('form');
290 const buttons = $(form_container).find('.frappe-control');
291 form_container.addClass('flex');
292 buttons.map((index, button) => {
293 $(button).css({"margin-right": "1em"});
294 })
Zarrar254ce642018-06-28 14:15:34 +0530295
296 dialog.show();
297 coa_tree.load_children(coa_tree.root_node, true); // expand all node trigger
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530298 }
Anand Doshi31771382016-07-05 21:34:21 +0530299 }
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530300];
Anand Doshi31771382016-07-05 21:34:21 +0530301
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530302// Source: https://en.wikipedia.org/wiki/Fiscal_year
303// default 1st Jan - 31st Dec
Rushabh Mehta20038ad2016-07-21 16:01:59 +0530304
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530305erpnext.setup.fiscal_years = {
Shreya Shahdb499dc2017-12-12 14:19:35 +0530306 "Afghanistan": ["12-21", "12-20"],
Makarand Bauskarbf66d7e2017-07-05 17:26:34 +0530307 "Australia": ["07-01", "06-30"],
308 "Bangladesh": ["07-01", "06-30"],
309 "Canada": ["04-01", "03-31"],
310 "Costa Rica": ["10-01", "09-30"],
311 "Egypt": ["07-01", "06-30"],
312 "Hong Kong": ["04-01", "03-31"],
313 "India": ["04-01", "03-31"],
314 "Iran": ["06-23", "06-22"],
Makarand Bauskarbf66d7e2017-07-05 17:26:34 +0530315 "Myanmar": ["04-01", "03-31"],
316 "New Zealand": ["04-01", "03-31"],
317 "Pakistan": ["07-01", "06-30"],
318 "Singapore": ["04-01", "03-31"],
319 "South Africa": ["03-01", "02-28"],
320 "Thailand": ["10-01", "09-30"],
321 "United Kingdom": ["04-01", "03-31"],
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530322};