Anand Doshi | 825d014 | 2014-07-18 18:05:26 +0530 | [diff] [blame] | 1 | frappe.provide("erpnext.financial_statements"); |
| 2 | |
| 3 | erpnext.financial_statements = { |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 4 | filters: get_filters(), |
| 5 | baseData: null, |
| 6 | formatter: function (value, row, column, data, default_formatter, filter) { |
| 7 | if ( |
| 8 | frappe.query_report.get_filter_value("selected_view") == "Growth" && |
| 9 | data && |
| 10 | column.colIndex >= 3 |
| 11 | ) { |
nitmit | 92649de | 2024-01-27 10:18:35 +0530 | [diff] [blame] | 12 | //Assuming that the first three columns are s.no, account name and the very first year of the accounting values, to calculate the relative percentage values of the successive columns. |
| 13 | const lastAnnualValue = row[column.colIndex - 1].content; |
| 14 | const currentAnnualvalue = data[column.fieldname]; |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 15 | if (currentAnnualvalue == undefined) return "NA"; //making this not applicable for undefined/null values |
nitmit | 92649de | 2024-01-27 10:18:35 +0530 | [diff] [blame] | 16 | let annualGrowth = 0; |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 17 | if (lastAnnualValue == 0 && currentAnnualvalue > 0) { |
nitmit | 92649de | 2024-01-27 10:18:35 +0530 | [diff] [blame] | 18 | //If the previous year value is 0 and the current value is greater than 0 |
| 19 | annualGrowth = 1; |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 20 | } else if (lastAnnualValue > 0) { |
| 21 | annualGrowth = (currentAnnualvalue - lastAnnualValue) / lastAnnualValue; |
nitmit | 92649de | 2024-01-27 10:18:35 +0530 | [diff] [blame] | 22 | } |
| 23 | |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 24 | const growthPercent = Math.round(annualGrowth * 10000) / 100; //calculating the rounded off percentage |
nitmit | 92649de | 2024-01-27 10:18:35 +0530 | [diff] [blame] | 25 | |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 26 | value = $(`<span>${(growthPercent >= 0 ? "+" : "") + growthPercent + "%"}</span>`); |
| 27 | if (growthPercent < 0) { |
nitmit | 92649de | 2024-01-27 10:18:35 +0530 | [diff] [blame] | 28 | value = $(value).addClass("text-danger"); |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 29 | } else { |
nitmit | 92649de | 2024-01-27 10:18:35 +0530 | [diff] [blame] | 30 | value = $(value).addClass("text-success"); |
| 31 | } |
| 32 | value = $(value).wrap("<p></p>").parent().html(); |
| 33 | |
| 34 | return value; |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 35 | } else if (frappe.query_report.get_filter_value("selected_view") == "Margin" && data) { |
| 36 | if (column.fieldname == "account" && data.account_name == __("Income")) { |
nitmit | 92649de | 2024-01-27 10:18:35 +0530 | [diff] [blame] | 37 | //Taking the total income from each column (for all the financial years) as the base (100%) |
| 38 | this.baseData = row; |
| 39 | } |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 40 | if (column.colIndex >= 2) { |
nitmit | 92649de | 2024-01-27 10:18:35 +0530 | [diff] [blame] | 41 | //Assuming that the first two columns are s.no and account name, to calculate the relative percentage values of the successive columns. |
| 42 | const currentAnnualvalue = data[column.fieldname]; |
| 43 | const baseValue = this.baseData[column.colIndex].content; |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 44 | if (currentAnnualvalue == undefined || baseValue <= 0) return "NA"; |
| 45 | const marginPercent = Math.round((currentAnnualvalue / baseValue) * 10000) / 100; |
nitmit | 92649de | 2024-01-27 10:18:35 +0530 | [diff] [blame] | 46 | |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 47 | value = $(`<span>${marginPercent + "%"}</span>`); |
| 48 | if (marginPercent < 0) value = $(value).addClass("text-danger"); |
| 49 | else value = $(value).addClass("text-success"); |
nitmit | 92649de | 2024-01-27 10:18:35 +0530 | [diff] [blame] | 50 | value = $(value).wrap("<p></p>").parent().html(); |
| 51 | return value; |
| 52 | } |
nitmit | 92649de | 2024-01-27 10:18:35 +0530 | [diff] [blame] | 53 | } |
| 54 | |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 55 | if (data && column.fieldname == "account") { |
thefalconx33 | a639f16 | 2019-12-04 09:46:42 +0530 | [diff] [blame] | 56 | value = data.account_name || value; |
Anand Doshi | 825d014 | 2014-07-18 18:05:26 +0530 | [diff] [blame] | 57 | |
Shariq Ansari | 08ed3cd | 2023-12-07 19:39:24 +0530 | [diff] [blame] | 58 | if (filter && filter?.text && filter?.type == "contains") { |
| 59 | if (!value.toLowerCase().includes(filter.text)) { |
| 60 | return value; |
| 61 | } |
| 62 | } |
| 63 | |
Ankush Menat | 2dc95e5 | 2023-09-27 12:34:40 +0530 | [diff] [blame] | 64 | if (data.account) { |
| 65 | column.link_onclick = |
| 66 | "erpnext.financial_statements.open_general_ledger(" + JSON.stringify(data) + ")"; |
| 67 | } |
Faris Ansari | 5986d59 | 2018-07-20 15:11:55 +0530 | [diff] [blame] | 68 | column.is_tree = true; |
Anand Doshi | 825d014 | 2014-07-18 18:05:26 +0530 | [diff] [blame] | 69 | } |
| 70 | |
Faris Ansari | 5986d59 | 2018-07-20 15:11:55 +0530 | [diff] [blame] | 71 | value = default_formatter(value, row, column, data); |
Anand Doshi | cb86d59 | 2014-07-22 19:02:11 +0530 | [diff] [blame] | 72 | |
Diksha Jadhav | 09b8caf | 2020-07-21 17:35:10 +0530 | [diff] [blame] | 73 | if (data && !data.parent_account) { |
Faris Ansari | 5986d59 | 2018-07-20 15:11:55 +0530 | [diff] [blame] | 74 | value = $(`<span>${value}</span>`); |
| 75 | |
Anand Doshi | 5f0459c | 2014-07-21 16:13:06 +0530 | [diff] [blame] | 76 | var $value = $(value).css("font-weight", "bold"); |
Faris Ansari | 5986d59 | 2018-07-20 15:11:55 +0530 | [diff] [blame] | 77 | if (data.warn_if_negative && data[column.fieldname] < 0) { |
Anand Doshi | 5f0459c | 2014-07-21 16:13:06 +0530 | [diff] [blame] | 78 | $value.addClass("text-danger"); |
| 79 | } |
| 80 | |
| 81 | value = $value.wrap("<p></p>").parent().html(); |
Anand Doshi | 825d014 | 2014-07-18 18:05:26 +0530 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | return value; |
| 85 | }, |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 86 | open_general_ledger: function (data) { |
Anand Doshi | 5f0459c | 2014-07-21 16:13:06 +0530 | [diff] [blame] | 87 | if (!data.account) return; |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 88 | let project = $.grep(frappe.query_report.filters, function (e) { |
| 89 | return e.df.fieldname == "project"; |
| 90 | }); |
Anand Doshi | 825d014 | 2014-07-18 18:05:26 +0530 | [diff] [blame] | 91 | |
| 92 | frappe.route_options = { |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 93 | account: data.account, |
| 94 | company: frappe.query_report.get_filter_value("company"), |
| 95 | from_date: data.from_date || data.year_start_date, |
| 96 | to_date: data.to_date || data.year_end_date, |
| 97 | project: project && project.length > 0 ? project[0].$input.val() : "", |
Anand Doshi | 825d014 | 2014-07-18 18:05:26 +0530 | [diff] [blame] | 98 | }; |
FinByz Tech Pvt. Ltd | c52b41d | 2022-10-19 23:14:10 +0530 | [diff] [blame] | 99 | |
| 100 | let report = "General Ledger"; |
| 101 | |
| 102 | if (["Payable", "Receivable"].includes(data.account_type)) { |
| 103 | report = data.account_type == "Payable" ? "Accounts Payable" : "Accounts Receivable"; |
| 104 | frappe.route_options["party_account"] = data.account; |
| 105 | frappe.route_options["report_date"] = data.year_end_date; |
| 106 | } |
| 107 | |
| 108 | frappe.set_route("query-report", report); |
Anand Doshi | cb86d59 | 2014-07-22 19:02:11 +0530 | [diff] [blame] | 109 | }, |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 110 | tree: true, |
| 111 | name_field: "account", |
| 112 | parent_field: "parent_account", |
| 113 | initial_depth: 3, |
| 114 | onload: function (report) { |
Rushabh Mehta | 0525387 | 2016-04-18 19:27:36 +0530 | [diff] [blame] | 115 | // dropdown for links to other financial statements |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 116 | erpnext.financial_statements.filters = get_filters(); |
Rohit Waghchaure | 4275c30 | 2016-08-19 11:48:58 +0530 | [diff] [blame] | 117 | |
Deepesh Garg | 6270607 | 2023-07-16 12:58:42 +0530 | [diff] [blame] | 118 | let fiscal_year = erpnext.utils.get_fiscal_year(frappe.datetime.get_today()); |
Deepesh Garg | 1f3fe59 | 2020-05-28 18:36:21 +0530 | [diff] [blame] | 119 | |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 120 | frappe.model.with_doc("Fiscal Year", fiscal_year, function (r) { |
Deepesh Garg | 1f3fe59 | 2020-05-28 18:36:21 +0530 | [diff] [blame] | 121 | var fy = frappe.model.get_doc("Fiscal Year", fiscal_year); |
| 122 | frappe.query_report.set_filter_value({ |
| 123 | period_start_date: fy.year_start_date, |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 124 | period_end_date: fy.year_end_date, |
Deepesh Garg | 1f3fe59 | 2020-05-28 18:36:21 +0530 | [diff] [blame] | 125 | }); |
| 126 | }); |
| 127 | |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 128 | if (report.page) { |
| 129 | const views_menu = report.page.add_custom_button_group(__("Financial Statements")); |
Shivam Mishra | b6fb132 | 2020-07-09 16:51:36 +0530 | [diff] [blame] | 130 | |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 131 | report.page.add_custom_menu_item(views_menu, __("Balance Sheet"), function () { |
Gursheen Anand | 2486b64 | 2024-01-27 23:30:17 +0530 | [diff] [blame] | 132 | var filters = report.get_values(); |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 133 | frappe.set_route("query-report", "Balance Sheet", { company: filters.company }); |
Gursheen Anand | 2486b64 | 2024-01-27 23:30:17 +0530 | [diff] [blame] | 134 | }); |
Shivam Mishra | b6fb132 | 2020-07-09 16:51:36 +0530 | [diff] [blame] | 135 | |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 136 | report.page.add_custom_menu_item(views_menu, __("Profit and Loss"), function () { |
Gursheen Anand | 2486b64 | 2024-01-27 23:30:17 +0530 | [diff] [blame] | 137 | var filters = report.get_values(); |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 138 | frappe.set_route("query-report", "Profit and Loss Statement", { company: filters.company }); |
Gursheen Anand | 2486b64 | 2024-01-27 23:30:17 +0530 | [diff] [blame] | 139 | }); |
Shivam Mishra | b6fb132 | 2020-07-09 16:51:36 +0530 | [diff] [blame] | 140 | |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 141 | report.page.add_custom_menu_item(views_menu, __("Cash Flow Statement"), function () { |
Gursheen Anand | 2486b64 | 2024-01-27 23:30:17 +0530 | [diff] [blame] | 142 | var filters = report.get_values(); |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 143 | frappe.set_route("query-report", "Cash Flow", { company: filters.company }); |
Gursheen Anand | 2486b64 | 2024-01-27 23:30:17 +0530 | [diff] [blame] | 144 | }); |
| 145 | } |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 146 | }, |
Anand Doshi | 825d014 | 2014-07-18 18:05:26 +0530 | [diff] [blame] | 147 | }; |
Rohit Waghchaure | 4275c30 | 2016-08-19 11:48:58 +0530 | [diff] [blame] | 148 | |
Deepesh Garg | 11ea0b1 | 2020-05-26 19:23:45 +0530 | [diff] [blame] | 149 | function get_filters() { |
deepeshgarg007 | d83cf65 | 2019-05-12 18:34:23 +0530 | [diff] [blame] | 150 | let filters = [ |
Rohit Waghchaure | 4275c30 | 2016-08-19 11:48:58 +0530 | [diff] [blame] | 151 | { |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 152 | fieldname: "company", |
| 153 | label: __("Company"), |
| 154 | fieldtype: "Link", |
| 155 | options: "Company", |
| 156 | default: frappe.defaults.get_user_default("Company"), |
| 157 | reqd: 1, |
Rohit Waghchaure | 4275c30 | 2016-08-19 11:48:58 +0530 | [diff] [blame] | 158 | }, |
| 159 | { |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 160 | fieldname: "finance_book", |
| 161 | label: __("Finance Book"), |
| 162 | fieldtype: "Link", |
| 163 | options: "Finance Book", |
Gaurav Naik | 8cbbdfd | 2018-04-23 03:36:02 +0530 | [diff] [blame] | 164 | }, |
| 165 | { |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 166 | fieldname: "filter_based_on", |
| 167 | label: __("Filter Based On"), |
| 168 | fieldtype: "Select", |
| 169 | options: ["Fiscal Year", "Date Range"], |
| 170 | default: ["Fiscal Year"], |
| 171 | reqd: 1, |
| 172 | on_change: function () { |
| 173 | let filter_based_on = frappe.query_report.get_filter_value("filter_based_on"); |
| 174 | frappe.query_report.toggle_filter_display( |
| 175 | "from_fiscal_year", |
| 176 | filter_based_on === "Date Range" |
| 177 | ); |
| 178 | frappe.query_report.toggle_filter_display("to_fiscal_year", filter_based_on === "Date Range"); |
| 179 | frappe.query_report.toggle_filter_display( |
| 180 | "period_start_date", |
| 181 | filter_based_on === "Fiscal Year" |
| 182 | ); |
| 183 | frappe.query_report.toggle_filter_display( |
| 184 | "period_end_date", |
| 185 | filter_based_on === "Fiscal Year" |
| 186 | ); |
Deepesh Garg | 24a2c9b | 2020-04-07 12:16:25 +0530 | [diff] [blame] | 187 | |
| 188 | frappe.query_report.refresh(); |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 189 | }, |
Deepesh Garg | 24a2c9b | 2020-04-07 12:16:25 +0530 | [diff] [blame] | 190 | }, |
| 191 | { |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 192 | fieldname: "period_start_date", |
| 193 | label: __("Start Date"), |
| 194 | fieldtype: "Date", |
| 195 | reqd: 1, |
| 196 | depends_on: "eval:doc.filter_based_on == 'Date Range'", |
Deepesh Garg | 24a2c9b | 2020-04-07 12:16:25 +0530 | [diff] [blame] | 197 | }, |
| 198 | { |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 199 | fieldname: "period_end_date", |
| 200 | label: __("End Date"), |
| 201 | fieldtype: "Date", |
| 202 | reqd: 1, |
| 203 | depends_on: "eval:doc.filter_based_on == 'Date Range'", |
Deepesh Garg | 24a2c9b | 2020-04-07 12:16:25 +0530 | [diff] [blame] | 204 | }, |
| 205 | { |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 206 | fieldname: "from_fiscal_year", |
| 207 | label: __("Start Year"), |
| 208 | fieldtype: "Link", |
| 209 | options: "Fiscal Year", |
| 210 | reqd: 1, |
| 211 | depends_on: "eval:doc.filter_based_on == 'Fiscal Year'", |
Rohit Waghchaure | 4275c30 | 2016-08-19 11:48:58 +0530 | [diff] [blame] | 212 | }, |
| 213 | { |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 214 | fieldname: "to_fiscal_year", |
| 215 | label: __("End Year"), |
| 216 | fieldtype: "Link", |
| 217 | options: "Fiscal Year", |
| 218 | reqd: 1, |
| 219 | depends_on: "eval:doc.filter_based_on == 'Fiscal Year'", |
Rohit Waghchaure | 4275c30 | 2016-08-19 11:48:58 +0530 | [diff] [blame] | 220 | }, |
| 221 | { |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 222 | fieldname: "periodicity", |
| 223 | label: __("Periodicity"), |
| 224 | fieldtype: "Select", |
| 225 | options: [ |
| 226 | { value: "Monthly", label: __("Monthly") }, |
| 227 | { value: "Quarterly", label: __("Quarterly") }, |
| 228 | { value: "Half-Yearly", label: __("Half-Yearly") }, |
| 229 | { value: "Yearly", label: __("Yearly") }, |
Rohit Waghchaure | 4275c30 | 2016-08-19 11:48:58 +0530 | [diff] [blame] | 230 | ], |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 231 | default: "Yearly", |
| 232 | reqd: 1, |
tundebabzy | c897825 | 2018-02-12 10:34:50 +0100 | [diff] [blame] | 233 | }, |
| 234 | // Note: |
| 235 | // If you are modifying this array such that the presentation_currency object |
| 236 | // is no longer the last object, please make adjustments in cash_flow.js |
| 237 | // accordingly. |
| 238 | { |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 239 | fieldname: "presentation_currency", |
| 240 | label: __("Currency"), |
| 241 | fieldtype: "Select", |
| 242 | options: erpnext.get_presentation_currency_list(), |
deepeshgarg007 | f5b6ee9 | 2019-05-28 12:15:56 +0530 | [diff] [blame] | 243 | }, |
| 244 | { |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 245 | fieldname: "cost_center", |
| 246 | label: __("Cost Center"), |
| 247 | fieldtype: "MultiSelectList", |
| 248 | get_data: function (txt) { |
| 249 | return frappe.db.get_link_options("Cost Center", txt, { |
| 250 | company: frappe.query_report.get_filter_value("company"), |
Gursheen Kaur Anand | 596a14e | 2023-07-12 15:49:17 +0530 | [diff] [blame] | 251 | }); |
| 252 | }, |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 253 | }, |
| 254 | { |
| 255 | fieldname: "project", |
| 256 | label: __("Project"), |
| 257 | fieldtype: "MultiSelectList", |
| 258 | get_data: function (txt) { |
| 259 | return frappe.db.get_link_options("Project", txt, { |
| 260 | company: frappe.query_report.get_filter_value("company"), |
| 261 | }); |
| 262 | }, |
| 263 | }, |
| 264 | ]; |
deepeshgarg007 | d83cf65 | 2019-05-12 18:34:23 +0530 | [diff] [blame] | 265 | |
ruthra kumar | c31ee8e | 2023-11-14 06:44:49 +0530 | [diff] [blame] | 266 | // Dynamically set 'default' values for fiscal year filters |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 267 | let fy_filters = filters.filter((x) => { |
| 268 | return ["from_fiscal_year", "to_fiscal_year"].includes(x.fieldname); |
| 269 | }); |
ruthra kumar | c31ee8e | 2023-11-14 06:44:49 +0530 | [diff] [blame] | 270 | let fiscal_year = erpnext.utils.get_fiscal_year(frappe.datetime.get_today(), false, true); |
| 271 | if (fiscal_year) { |
| 272 | let fy = erpnext.utils.get_fiscal_year(frappe.datetime.get_today(), false, false); |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 273 | fy_filters.forEach((x) => { |
| 274 | x.default = fy; |
| 275 | }); |
ruthra kumar | c31ee8e | 2023-11-14 06:44:49 +0530 | [diff] [blame] | 276 | } |
| 277 | |
deepeshgarg007 | d83cf65 | 2019-05-12 18:34:23 +0530 | [diff] [blame] | 278 | return filters; |
| 279 | } |