blob: 17341d19d9cfc3896214b359aa6de334c96044ee [file] [log] [blame]
Anand Doshi825d0142014-07-18 18:05:26 +05301frappe.provide("erpnext.financial_statements");
2
3erpnext.financial_statements = {
Rohit Waghchaure4275c302016-08-19 11:48:58 +05304 "filters": get_filters(),
Shariq Ansari08ed3cd2023-12-07 19:39:24 +05305 "formatter": function(value, row, column, data, default_formatter, filter) {
Diksha Jadhav09b8caf2020-07-21 17:35:10 +05306 if (data && column.fieldname=="account") {
thefalconx33a639f162019-12-04 09:46:42 +05307 value = data.account_name || value;
Anand Doshi825d0142014-07-18 18:05:26 +05308
Shariq Ansari08ed3cd2023-12-07 19:39:24 +05309 if (filter && filter?.text && filter?.type == "contains") {
10 if (!value.toLowerCase().includes(filter.text)) {
11 return value;
12 }
13 }
14
Ankush Menat2dc95e52023-09-27 12:34:40 +053015 if (data.account) {
16 column.link_onclick =
17 "erpnext.financial_statements.open_general_ledger(" + JSON.stringify(data) + ")";
18 }
Faris Ansari5986d592018-07-20 15:11:55 +053019 column.is_tree = true;
Anand Doshi825d0142014-07-18 18:05:26 +053020 }
21
Faris Ansari5986d592018-07-20 15:11:55 +053022 value = default_formatter(value, row, column, data);
Anand Doshicb86d592014-07-22 19:02:11 +053023
Diksha Jadhav09b8caf2020-07-21 17:35:10 +053024 if (data && !data.parent_account) {
Faris Ansari5986d592018-07-20 15:11:55 +053025 value = $(`<span>${value}</span>`);
26
Anand Doshi5f0459c2014-07-21 16:13:06 +053027 var $value = $(value).css("font-weight", "bold");
Faris Ansari5986d592018-07-20 15:11:55 +053028 if (data.warn_if_negative && data[column.fieldname] < 0) {
Anand Doshi5f0459c2014-07-21 16:13:06 +053029 $value.addClass("text-danger");
30 }
31
32 value = $value.wrap("<p></p>").parent().html();
Anand Doshi825d0142014-07-18 18:05:26 +053033 }
34
35 return value;
36 },
Anand Doshi5f0459c2014-07-21 16:13:06 +053037 "open_general_ledger": function(data) {
38 if (!data.account) return;
FinByz Tech Pvt. Ltdc52b41d2022-10-19 23:14:10 +053039 let project = $.grep(frappe.query_report.filters, function(e){ return e.df.fieldname == 'project'; });
Anand Doshi825d0142014-07-18 18:05:26 +053040
41 frappe.route_options = {
Anand Doshi5f0459c2014-07-21 16:13:06 +053042 "account": data.account,
Faris Ansari9e874af2018-07-18 09:28:44 +053043 "company": frappe.query_report.get_filter_value('company'),
Anand Doshi561e6cd2016-02-16 11:56:53 +053044 "from_date": data.from_date || data.year_start_date,
Ricardo Johann34354232017-01-24 06:42:24 -030045 "to_date": data.to_date || data.year_end_date,
Rohit Waghchaure019501e2017-04-19 17:53:31 +053046 "project": (project && project.length > 0) ? project[0].$input.val() : ""
Anand Doshi825d0142014-07-18 18:05:26 +053047 };
FinByz Tech Pvt. Ltdc52b41d2022-10-19 23:14:10 +053048
49 let report = "General Ledger";
50
51 if (["Payable", "Receivable"].includes(data.account_type)) {
52 report = data.account_type == "Payable" ? "Accounts Payable" : "Accounts Receivable";
53 frappe.route_options["party_account"] = data.account;
54 frappe.route_options["report_date"] = data.year_end_date;
55 }
56
57 frappe.set_route("query-report", report);
Anand Doshicb86d592014-07-22 19:02:11 +053058 },
59 "tree": true,
60 "name_field": "account",
61 "parent_field": "parent_account",
Rushabh Mehta05253872016-04-18 19:27:36 +053062 "initial_depth": 3,
63 onload: function(report) {
64 // dropdown for links to other financial statements
Rohit Waghchaure4275c302016-08-19 11:48:58 +053065 erpnext.financial_statements.filters = get_filters()
66
Deepesh Garg62706072023-07-16 12:58:42 +053067 let fiscal_year = erpnext.utils.get_fiscal_year(frappe.datetime.get_today());
Deepesh Garg1f3fe592020-05-28 18:36:21 +053068
69 frappe.model.with_doc("Fiscal Year", fiscal_year, function(r) {
70 var fy = frappe.model.get_doc("Fiscal Year", fiscal_year);
71 frappe.query_report.set_filter_value({
72 period_start_date: fy.year_start_date,
73 period_end_date: fy.year_end_date
74 });
75 });
76
Shivam Mishrab6fb1322020-07-09 16:51:36 +053077 const views_menu = report.page.add_custom_button_group(__('Financial Statements'));
78
79 report.page.add_custom_menu_item(views_menu, __("Balance Sheet"), function() {
Rushabh Mehta05253872016-04-18 19:27:36 +053080 var filters = report.get_values();
81 frappe.set_route('query-report', 'Balance Sheet', {company: filters.company});
Shivam Mishrab6fb1322020-07-09 16:51:36 +053082 });
83
84 report.page.add_custom_menu_item(views_menu, __("Profit and Loss"), function() {
Rushabh Mehta05253872016-04-18 19:27:36 +053085 var filters = report.get_values();
86 frappe.set_route('query-report', 'Profit and Loss Statement', {company: filters.company});
Shivam Mishrab6fb1322020-07-09 16:51:36 +053087 });
88
89 report.page.add_custom_menu_item(views_menu, __("Cash Flow Statement"), function() {
Rushabh Mehta05253872016-04-18 19:27:36 +053090 var filters = report.get_values();
91 frappe.set_route('query-report', 'Cash Flow', {company: filters.company});
Shivam Mishrab6fb1322020-07-09 16:51:36 +053092 });
Nabin Hait44c1b8e2016-05-20 11:44:08 +053093 }
Anand Doshi825d0142014-07-18 18:05:26 +053094};
Rohit Waghchaure4275c302016-08-19 11:48:58 +053095
Deepesh Garg11ea0b12020-05-26 19:23:45 +053096function get_filters() {
deepeshgarg007d83cf652019-05-12 18:34:23 +053097 let filters = [
Rohit Waghchaure4275c302016-08-19 11:48:58 +053098 {
99 "fieldname":"company",
100 "label": __("Company"),
101 "fieldtype": "Link",
102 "options": "Company",
103 "default": frappe.defaults.get_user_default("Company"),
104 "reqd": 1
105 },
106 {
Gaurav Naik8cbbdfd2018-04-23 03:36:02 +0530107 "fieldname":"finance_book",
108 "label": __("Finance Book"),
109 "fieldtype": "Link",
Nabin Haitb9fed2a2018-05-09 15:10:29 +0530110 "options": "Finance Book"
Gaurav Naik8cbbdfd2018-04-23 03:36:02 +0530111 },
112 {
Deepesh Garg24a2c9b2020-04-07 12:16:25 +0530113 "fieldname":"filter_based_on",
114 "label": __("Filter Based On"),
115 "fieldtype": "Select",
116 "options": ["Fiscal Year", "Date Range"],
117 "default": ["Fiscal Year"],
118 "reqd": 1,
119 on_change: function() {
120 let filter_based_on = frappe.query_report.get_filter_value('filter_based_on');
121 frappe.query_report.toggle_filter_display('from_fiscal_year', filter_based_on === 'Date Range');
122 frappe.query_report.toggle_filter_display('to_fiscal_year', filter_based_on === 'Date Range');
123 frappe.query_report.toggle_filter_display('period_start_date', filter_based_on === 'Fiscal Year');
124 frappe.query_report.toggle_filter_display('period_end_date', filter_based_on === 'Fiscal Year');
125
126 frappe.query_report.refresh();
127 }
128 },
129 {
130 "fieldname":"period_start_date",
131 "label": __("Start Date"),
132 "fieldtype": "Date",
Deepesh Garg3a6894f2021-10-27 19:39:18 +0530133 "reqd": 1,
134 "depends_on": "eval:doc.filter_based_on == 'Date Range'"
Deepesh Garg24a2c9b2020-04-07 12:16:25 +0530135 },
136 {
137 "fieldname":"period_end_date",
138 "label": __("End Date"),
139 "fieldtype": "Date",
Deepesh Garg3a6894f2021-10-27 19:39:18 +0530140 "reqd": 1,
141 "depends_on": "eval:doc.filter_based_on == 'Date Range'"
Deepesh Garg24a2c9b2020-04-07 12:16:25 +0530142 },
143 {
Rohit Waghchaure4275c302016-08-19 11:48:58 +0530144 "fieldname":"from_fiscal_year",
Rohit Waghchaure26b646f2016-08-21 17:14:12 +0530145 "label": __("Start Year"),
Rohit Waghchaure4275c302016-08-19 11:48:58 +0530146 "fieldtype": "Link",
147 "options": "Fiscal Year",
Deepesh Garg3a6894f2021-10-27 19:39:18 +0530148 "reqd": 1,
149 "depends_on": "eval:doc.filter_based_on == 'Fiscal Year'"
Rohit Waghchaure4275c302016-08-19 11:48:58 +0530150 },
151 {
152 "fieldname":"to_fiscal_year",
Rohit Waghchaure26b646f2016-08-21 17:14:12 +0530153 "label": __("End Year"),
Rohit Waghchaure4275c302016-08-19 11:48:58 +0530154 "fieldtype": "Link",
155 "options": "Fiscal Year",
Deepesh Garg3a6894f2021-10-27 19:39:18 +0530156 "reqd": 1,
157 "depends_on": "eval:doc.filter_based_on == 'Fiscal Year'"
Rohit Waghchaure4275c302016-08-19 11:48:58 +0530158 },
159 {
160 "fieldname": "periodicity",
161 "label": __("Periodicity"),
162 "fieldtype": "Select",
163 "options": [
164 { "value": "Monthly", "label": __("Monthly") },
165 { "value": "Quarterly", "label": __("Quarterly") },
166 { "value": "Half-Yearly", "label": __("Half-Yearly") },
167 { "value": "Yearly", "label": __("Yearly") }
168 ],
Nabin Haitf6b784e2018-08-30 18:42:35 +0530169 "default": "Yearly",
Rohit Waghchaure4275c302016-08-19 11:48:58 +0530170 "reqd": 1
tundebabzyc8978252018-02-12 10:34:50 +0100171 },
172 // Note:
173 // If you are modifying this array such that the presentation_currency object
174 // is no longer the last object, please make adjustments in cash_flow.js
175 // accordingly.
176 {
177 "fieldname": "presentation_currency",
178 "label": __("Currency"),
179 "fieldtype": "Select",
180 "options": erpnext.get_presentation_currency_list()
deepeshgarg007f5b6ee92019-05-28 12:15:56 +0530181 },
182 {
Faris Ansari26321072019-06-19 12:15:37 +0530183 "fieldname": "cost_center",
deepeshgarg007f5b6ee92019-05-28 12:15:56 +0530184 "label": __("Cost Center"),
Faris Ansari26321072019-06-19 12:15:37 +0530185 "fieldtype": "MultiSelectList",
186 get_data: function(txt) {
187 return frappe.db.get_link_options('Cost Center', txt, {
188 company: frappe.query_report.get_filter_value("company")
deepeshgarg007f5b6ee92019-05-28 12:15:56 +0530189 });
deepeshgarg007f5b6ee92019-05-28 12:15:56 +0530190 }
Gursheen Kaur Anand596a14e2023-07-12 15:49:17 +0530191 },
192 {
193 "fieldname": "project",
194 "label": __("Project"),
195 "fieldtype": "MultiSelectList",
196 get_data: function(txt) {
197 return frappe.db.get_link_options('Project', txt, {
198 company: frappe.query_report.get_filter_value("company")
199 });
200 },
Rohit Waghchaure4275c302016-08-19 11:48:58 +0530201 }
202 ]
deepeshgarg007d83cf652019-05-12 18:34:23 +0530203
ruthra kumarc31ee8e2023-11-14 06:44:49 +0530204 // Dynamically set 'default' values for fiscal year filters
205 let fy_filters = filters.filter(x=>{return ["from_fiscal_year", "to_fiscal_year"].includes(x.fieldname);})
206 let fiscal_year = erpnext.utils.get_fiscal_year(frappe.datetime.get_today(), false, true);
207 if (fiscal_year) {
208 let fy = erpnext.utils.get_fiscal_year(frappe.datetime.get_today(), false, false);
209 fy_filters.forEach(x=>{x.default = fy;})
210 }
211
deepeshgarg007d83cf652019-05-12 18:34:23 +0530212 return filters;
213}