blob: 9288f515cd47a4c46d9bd8aa0630bbe18157df9b [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 {
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +053016 // Brand
17 name: 'brand',
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +053018 icon: "fa fa-bookmark",
19 title: __("The Brand"),
Prateeksha Singh95d8fd32017-09-04 11:14:04 +053020 // help: __('Upload your letter head and logo. (you can edit them later).'),
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +053021 fields: [
22 {
23 fieldtype: "Attach Image", fieldname: "attach_logo",
24 label: __("Attach Logo"),
25 description: __("100px by 100px"),
Makarand Bauskar1d9fd9a2017-07-26 18:19:41 +053026 is_private: 0,
27 align: 'center'
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +053028 },
29 {
30 fieldname: 'company_name',
Deepesh Garg60b693f2022-04-28 12:02:54 +053031 label: __('Company Name'),
Makarand Bauskar1d9fd9a2017-07-26 18:19:41 +053032 fieldtype: 'Data',
33 reqd: 1
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +053034 },
35 {
36 fieldname: 'company_abbr',
Deepesh Garg60b693f2022-04-28 12:02:54 +053037 label: __('Company Abbreviation'),
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +053038 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 Garg239733ac2022-02-23 20:41:49 +053048 slide.get_field("company_abbr").set_value(abbr.slice(0, 10).toUpperCase());
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +053049 }).val(frappe.boot.sysdefaults.company_name || "").trigger("change");
Rushabh Mehta37b4d752015-11-09 16:53:11 +053050
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +053051 slide.get_input("company_abbr").on("change", function () {
Deepesh Garg239733ac2022-02-23 20:41:49 +053052 if (slide.get_input("company_abbr").val().length > 10) {
Nabin Haite6d65bc2018-02-14 17:44:06 +053053 frappe.msgprint(__("Company Abbreviation cannot have more than 5 characters"));
Makarand Bauskar30f2bcb2017-07-11 12:36:57 +053054 slide.get_field("company_abbr").set_value("");
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +053055 }
56 });
Prateeksha Singh95d8fd32017-09-04 11:14:04 +053057 },
58 validate: function() {
Makarand Bauskarb1bf5022017-10-02 11:38:03 +053059 if ((this.values.company_name || "").toLowerCase() == "company") {
60 frappe.msgprint(__("Company Name cannot be Company"));
61 return false;
62 }
Prateeksha Singh95d8fd32017-09-04 11:14:04 +053063 if (!this.values.company_abbr) {
64 return false;
65 }
Deepesh Garg239733ac2022-02-23 20:41:49 +053066 if (this.values.company_abbr.length > 10) {
Faris Ansari40c15342018-12-06 07:37:32 +053067 return false;
68 }
Prateeksha Singh95d8fd32017-09-04 11:14:04 +053069 return true;
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +053070 }
71 },
72 {
73 // Organisation
74 name: 'organisation',
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +053075 title: __("Your Organization"),
76 icon: "fa fa-building",
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +053077 fields: [
78 {
79 fieldname: 'company_tagline',
80 label: __('What does it do?'),
81 fieldtype: 'Data',
Deepesh Garg60b693f2022-04-28 12:02:54 +053082 placeholder: __('e.g. "Build tools for builders"'),
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +053083 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 Doshi1ed82832015-11-16 12:58:14 +053089 },
Zarrar254ce642018-06-28 14:15:34 +053090 { fieldname: 'view_coa', label: __('View Chart of Accounts'), fieldtype: 'Button' },
Rushabh Mehta2cd41bc2020-12-18 13:17:58 +053091 { 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 Singh5e4c8ec2017-07-03 18:23:57 +053094 ],
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 Garg2908f2e2021-06-26 23:49:32 +0530103 let me = this;
104 let exist;
105
Faris Ansariaa856c42018-09-18 10:42:04 +0530106 if (!this.validate_fy_dates()) {
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530107 return false;
108 }
Zarrar441a1e12018-02-16 14:44:42 +0530109
Deepesh Garg2908f2e2021-06-26 23:49:32 +0530110 // Validate bank name
Ankush Menat4551d7d2021-08-19 13:41:10 +0530111 if(me.values.bank_account) {
Deepesh Garg2908f2e2021-06-26 23:49:32 +0530112 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 Singh5e4c8ec2017-07-03 18:23:57 +0530133 return true;
134 },
135
Faris Ansariaa856c42018-09-18 10:42:04 +0530136 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 Singh5e4c8ec2017-07-03 18:23:57 +0530150 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 Bauskarbf66d7e2017-07-05 17:26:34 +0530158 fy = ["01-01", "12-31"];
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530159 next_year = current_year;
Anand Doshi5c3469a2015-11-24 17:06:54 +0530160 }
161
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530162 var year_start_date = current_year + "-" + fy[0];
163 if (year_start_date > frappe.datetime.get_today()) {
Rushabh Mehta63b06422017-07-05 17:56:04 +0530164 next_year = current_year;
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530165 current_year -= 1;
Anand Doshi5c3469a2015-11-24 17:06:54 +0530166 }
Rushabh Mehta63b06422017-07-05 17:56:04 +0530167 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 Singh5e4c8ec2017-07-03 18:23:57 +0530169 }
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530170 },
Anand Doshi5c3469a2015-11-24 17:06:54 +0530171
Anand Doshi1ed82832015-11-16 12:58:14 +0530172
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530173 load_chart_of_accounts: function (slide) {
174 var country = frappe.wizard.values.country;
Anand Doshi1ed82832015-11-16 12:58:14 +0530175
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530176 if (country) {
177 frappe.call({
178 method: "erpnext.accounts.doctype.account.chart_of_accounts.chart_of_accounts.get_charts_for_country",
Zarrar254ce642018-06-28 14:15:34 +0530179 args: { "country": country, with_standard: true },
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530180 callback: function (r) {
181 if (r.message) {
182 slide.get_input("chart_of_accounts").empty()
183 .add_options(r.message);
Rushabh Mehta37b4d752015-11-09 16:53:11 +0530184 }
Anand Doshi1ed82832015-11-16 12:58:14 +0530185 }
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530186 })
Rushabh Mehta37b4d752015-11-09 16:53:11 +0530187 }
188 },
Rushabh Mehta37b4d752015-11-09 16:53:11 +0530189
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530190 bind_events: function (slide) {
Zarrar254ce642018-06-28 14:15:34 +0530191 let me = this;
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530192 slide.get_input("fy_start_date").on("change", function () {
Rushabh Mehta63b06422017-07-05 17:56:04 +0530193 var start_date = slide.form.fields_dict.fy_start_date.get_value();
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530194 var year_end_date =
Rushabh Mehta63b06422017-07-05 17:56:04 +0530195 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 Singh5e4c8ec2017-07-03 18:23:57 +0530197 });
Zarrar254ce642018-06-28 14:15:34 +0530198
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
Zarrarc1225a82018-07-03 10:35:31 +0530248 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 })
Zarrar254ce642018-06-28 14:15:34 +0530254
255 dialog.show();
256 coa_tree.load_children(coa_tree.root_node, true); // expand all node trigger
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530257 }
Anand Doshi31771382016-07-05 21:34:21 +0530258 }
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530259];
Anand Doshi31771382016-07-05 21:34:21 +0530260
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530261// Source: https://en.wikipedia.org/wiki/Fiscal_year
262// default 1st Jan - 31st Dec
Rushabh Mehta20038ad2016-07-21 16:01:59 +0530263
Prateeksha Singh5e4c8ec2017-07-03 18:23:57 +0530264erpnext.setup.fiscal_years = {
Shreya Shahdb499dc2017-12-12 14:19:35 +0530265 "Afghanistan": ["12-21", "12-20"],
Makarand Bauskarbf66d7e2017-07-05 17:26:34 +0530266 "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 Bauskarbf66d7e2017-07-05 17:26:34 +0530274 "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 Singh5e4c8ec2017-07-03 18:23:57 +0530281};