blob: 1b10d8ad3a175240d5230e79bcb66f30f442ae1f [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(),
Faris Ansari5986d592018-07-20 15:11:55 +05305 "formatter": function(value, row, column, data, default_formatter) {
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
Ankush Menat2dc95e52023-09-27 12:34:40 +05309 if (data.account) {
10 column.link_onclick =
11 "erpnext.financial_statements.open_general_ledger(" + JSON.stringify(data) + ")";
12 }
Faris Ansari5986d592018-07-20 15:11:55 +053013 column.is_tree = true;
Anand Doshi825d0142014-07-18 18:05:26 +053014 }
15
Faris Ansari5986d592018-07-20 15:11:55 +053016 value = default_formatter(value, row, column, data);
Anand Doshicb86d592014-07-22 19:02:11 +053017
Diksha Jadhav09b8caf2020-07-21 17:35:10 +053018 if (data && !data.parent_account) {
Faris Ansari5986d592018-07-20 15:11:55 +053019 value = $(`<span>${value}</span>`);
20
Anand Doshi5f0459c2014-07-21 16:13:06 +053021 var $value = $(value).css("font-weight", "bold");
Faris Ansari5986d592018-07-20 15:11:55 +053022 if (data.warn_if_negative && data[column.fieldname] < 0) {
Anand Doshi5f0459c2014-07-21 16:13:06 +053023 $value.addClass("text-danger");
24 }
25
26 value = $value.wrap("<p></p>").parent().html();
Anand Doshi825d0142014-07-18 18:05:26 +053027 }
28
29 return value;
30 },
Anand Doshi5f0459c2014-07-21 16:13:06 +053031 "open_general_ledger": function(data) {
32 if (!data.account) return;
FinByz Tech Pvt. Ltdc52b41d2022-10-19 23:14:10 +053033 let project = $.grep(frappe.query_report.filters, function(e){ return e.df.fieldname == 'project'; });
Anand Doshi825d0142014-07-18 18:05:26 +053034
35 frappe.route_options = {
Anand Doshi5f0459c2014-07-21 16:13:06 +053036 "account": data.account,
Faris Ansari9e874af2018-07-18 09:28:44 +053037 "company": frappe.query_report.get_filter_value('company'),
Anand Doshi561e6cd2016-02-16 11:56:53 +053038 "from_date": data.from_date || data.year_start_date,
Ricardo Johann34354232017-01-24 06:42:24 -030039 "to_date": data.to_date || data.year_end_date,
Rohit Waghchaure019501e2017-04-19 17:53:31 +053040 "project": (project && project.length > 0) ? project[0].$input.val() : ""
Anand Doshi825d0142014-07-18 18:05:26 +053041 };
FinByz Tech Pvt. Ltdc52b41d2022-10-19 23:14:10 +053042
43 let report = "General Ledger";
44
45 if (["Payable", "Receivable"].includes(data.account_type)) {
46 report = data.account_type == "Payable" ? "Accounts Payable" : "Accounts Receivable";
47 frappe.route_options["party_account"] = data.account;
48 frappe.route_options["report_date"] = data.year_end_date;
49 }
50
51 frappe.set_route("query-report", report);
Anand Doshicb86d592014-07-22 19:02:11 +053052 },
53 "tree": true,
54 "name_field": "account",
55 "parent_field": "parent_account",
Rushabh Mehta05253872016-04-18 19:27:36 +053056 "initial_depth": 3,
57 onload: function(report) {
58 // dropdown for links to other financial statements
Rohit Waghchaure4275c302016-08-19 11:48:58 +053059 erpnext.financial_statements.filters = get_filters()
60
Deepesh Garg62706072023-07-16 12:58:42 +053061 let fiscal_year = erpnext.utils.get_fiscal_year(frappe.datetime.get_today());
Deepesh Garg1f3fe592020-05-28 18:36:21 +053062
63 frappe.model.with_doc("Fiscal Year", fiscal_year, function(r) {
64 var fy = frappe.model.get_doc("Fiscal Year", fiscal_year);
65 frappe.query_report.set_filter_value({
66 period_start_date: fy.year_start_date,
67 period_end_date: fy.year_end_date
68 });
69 });
70
Shivam Mishrab6fb1322020-07-09 16:51:36 +053071 const views_menu = report.page.add_custom_button_group(__('Financial Statements'));
72
73 report.page.add_custom_menu_item(views_menu, __("Balance Sheet"), function() {
Rushabh Mehta05253872016-04-18 19:27:36 +053074 var filters = report.get_values();
75 frappe.set_route('query-report', 'Balance Sheet', {company: filters.company});
Shivam Mishrab6fb1322020-07-09 16:51:36 +053076 });
77
78 report.page.add_custom_menu_item(views_menu, __("Profit and Loss"), function() {
Rushabh Mehta05253872016-04-18 19:27:36 +053079 var filters = report.get_values();
80 frappe.set_route('query-report', 'Profit and Loss Statement', {company: filters.company});
Shivam Mishrab6fb1322020-07-09 16:51:36 +053081 });
82
83 report.page.add_custom_menu_item(views_menu, __("Cash Flow Statement"), function() {
Rushabh Mehta05253872016-04-18 19:27:36 +053084 var filters = report.get_values();
85 frappe.set_route('query-report', 'Cash Flow', {company: filters.company});
Shivam Mishrab6fb1322020-07-09 16:51:36 +053086 });
Nabin Hait44c1b8e2016-05-20 11:44:08 +053087 }
Anand Doshi825d0142014-07-18 18:05:26 +053088};
Rohit Waghchaure4275c302016-08-19 11:48:58 +053089
Deepesh Garg11ea0b12020-05-26 19:23:45 +053090function get_filters() {
deepeshgarg007d83cf652019-05-12 18:34:23 +053091 let filters = [
Rohit Waghchaure4275c302016-08-19 11:48:58 +053092 {
93 "fieldname":"company",
94 "label": __("Company"),
95 "fieldtype": "Link",
96 "options": "Company",
97 "default": frappe.defaults.get_user_default("Company"),
98 "reqd": 1
99 },
100 {
Gaurav Naik8cbbdfd2018-04-23 03:36:02 +0530101 "fieldname":"finance_book",
102 "label": __("Finance Book"),
103 "fieldtype": "Link",
Nabin Haitb9fed2a2018-05-09 15:10:29 +0530104 "options": "Finance Book"
Gaurav Naik8cbbdfd2018-04-23 03:36:02 +0530105 },
106 {
Deepesh Garg24a2c9b2020-04-07 12:16:25 +0530107 "fieldname":"filter_based_on",
108 "label": __("Filter Based On"),
109 "fieldtype": "Select",
110 "options": ["Fiscal Year", "Date Range"],
111 "default": ["Fiscal Year"],
112 "reqd": 1,
113 on_change: function() {
114 let filter_based_on = frappe.query_report.get_filter_value('filter_based_on');
115 frappe.query_report.toggle_filter_display('from_fiscal_year', filter_based_on === 'Date Range');
116 frappe.query_report.toggle_filter_display('to_fiscal_year', filter_based_on === 'Date Range');
117 frappe.query_report.toggle_filter_display('period_start_date', filter_based_on === 'Fiscal Year');
118 frappe.query_report.toggle_filter_display('period_end_date', filter_based_on === 'Fiscal Year');
119
120 frappe.query_report.refresh();
121 }
122 },
123 {
124 "fieldname":"period_start_date",
125 "label": __("Start Date"),
126 "fieldtype": "Date",
Deepesh Garg3a6894f2021-10-27 19:39:18 +0530127 "reqd": 1,
128 "depends_on": "eval:doc.filter_based_on == 'Date Range'"
Deepesh Garg24a2c9b2020-04-07 12:16:25 +0530129 },
130 {
131 "fieldname":"period_end_date",
132 "label": __("End Date"),
133 "fieldtype": "Date",
Deepesh Garg3a6894f2021-10-27 19:39:18 +0530134 "reqd": 1,
135 "depends_on": "eval:doc.filter_based_on == 'Date Range'"
Deepesh Garg24a2c9b2020-04-07 12:16:25 +0530136 },
137 {
Rohit Waghchaure4275c302016-08-19 11:48:58 +0530138 "fieldname":"from_fiscal_year",
Rohit Waghchaure26b646f2016-08-21 17:14:12 +0530139 "label": __("Start Year"),
Rohit Waghchaure4275c302016-08-19 11:48:58 +0530140 "fieldtype": "Link",
141 "options": "Fiscal Year",
Deepesh Garg3a6894f2021-10-27 19:39:18 +0530142 "reqd": 1,
143 "depends_on": "eval:doc.filter_based_on == 'Fiscal Year'"
Rohit Waghchaure4275c302016-08-19 11:48:58 +0530144 },
145 {
146 "fieldname":"to_fiscal_year",
Rohit Waghchaure26b646f2016-08-21 17:14:12 +0530147 "label": __("End Year"),
Rohit Waghchaure4275c302016-08-19 11:48:58 +0530148 "fieldtype": "Link",
149 "options": "Fiscal Year",
Deepesh Garg3a6894f2021-10-27 19:39:18 +0530150 "reqd": 1,
151 "depends_on": "eval:doc.filter_based_on == 'Fiscal Year'"
Rohit Waghchaure4275c302016-08-19 11:48:58 +0530152 },
153 {
154 "fieldname": "periodicity",
155 "label": __("Periodicity"),
156 "fieldtype": "Select",
157 "options": [
158 { "value": "Monthly", "label": __("Monthly") },
159 { "value": "Quarterly", "label": __("Quarterly") },
160 { "value": "Half-Yearly", "label": __("Half-Yearly") },
161 { "value": "Yearly", "label": __("Yearly") }
162 ],
Nabin Haitf6b784e2018-08-30 18:42:35 +0530163 "default": "Yearly",
Rohit Waghchaure4275c302016-08-19 11:48:58 +0530164 "reqd": 1
tundebabzyc8978252018-02-12 10:34:50 +0100165 },
166 // Note:
167 // If you are modifying this array such that the presentation_currency object
168 // is no longer the last object, please make adjustments in cash_flow.js
169 // accordingly.
170 {
171 "fieldname": "presentation_currency",
172 "label": __("Currency"),
173 "fieldtype": "Select",
174 "options": erpnext.get_presentation_currency_list()
deepeshgarg007f5b6ee92019-05-28 12:15:56 +0530175 },
176 {
Faris Ansari26321072019-06-19 12:15:37 +0530177 "fieldname": "cost_center",
deepeshgarg007f5b6ee92019-05-28 12:15:56 +0530178 "label": __("Cost Center"),
Faris Ansari26321072019-06-19 12:15:37 +0530179 "fieldtype": "MultiSelectList",
180 get_data: function(txt) {
181 return frappe.db.get_link_options('Cost Center', txt, {
182 company: frappe.query_report.get_filter_value("company")
deepeshgarg007f5b6ee92019-05-28 12:15:56 +0530183 });
deepeshgarg007f5b6ee92019-05-28 12:15:56 +0530184 }
Gursheen Kaur Anand596a14e2023-07-12 15:49:17 +0530185 },
186 {
187 "fieldname": "project",
188 "label": __("Project"),
189 "fieldtype": "MultiSelectList",
190 get_data: function(txt) {
191 return frappe.db.get_link_options('Project', txt, {
192 company: frappe.query_report.get_filter_value("company")
193 });
194 },
Rohit Waghchaure4275c302016-08-19 11:48:58 +0530195 }
196 ]
deepeshgarg007d83cf652019-05-12 18:34:23 +0530197
ruthra kumarc31ee8e2023-11-14 06:44:49 +0530198 // Dynamically set 'default' values for fiscal year filters
199 let fy_filters = filters.filter(x=>{return ["from_fiscal_year", "to_fiscal_year"].includes(x.fieldname);})
200 let fiscal_year = erpnext.utils.get_fiscal_year(frappe.datetime.get_today(), false, true);
201 if (fiscal_year) {
202 let fy = erpnext.utils.get_fiscal_year(frappe.datetime.get_today(), false, false);
203 fy_filters.forEach(x=>{x.default = fy;})
204 }
205
deepeshgarg007d83cf652019-05-12 18:34:23 +0530206 return filters;
207}