Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 

285 wiersze
7.8 KiB

  1. # Copyright (c) 2013, Frappe Technologies Pvt. Ltd. and contributors
  2. # For license information, please see license.txt
  3. import frappe
  4. from frappe import _
  5. from frappe.utils import cint, cstr
  6. from erpnext.accounts.report.financial_statements import (
  7. get_columns,
  8. get_cost_centers_with_children,
  9. get_data,
  10. get_filtered_list_for_consolidated_report,
  11. get_period_list,
  12. )
  13. from erpnext.accounts.report.profit_and_loss_statement.profit_and_loss_statement import (
  14. get_net_profit_loss,
  15. )
  16. from erpnext.accounts.utils import get_fiscal_year
  17. def execute(filters=None):
  18. if cint(frappe.db.get_single_value("Accounts Settings", "use_custom_cash_flow")):
  19. from erpnext.accounts.report.cash_flow.custom_cash_flow import execute as execute_custom
  20. return execute_custom(filters=filters)
  21. period_list = get_period_list(
  22. filters.from_fiscal_year,
  23. filters.to_fiscal_year,
  24. filters.period_start_date,
  25. filters.period_end_date,
  26. filters.filter_based_on,
  27. filters.periodicity,
  28. company=filters.company,
  29. )
  30. cash_flow_accounts = get_cash_flow_accounts()
  31. # compute net profit / loss
  32. income = get_data(
  33. filters.company,
  34. "Income",
  35. "Credit",
  36. period_list,
  37. filters=filters,
  38. accumulated_values=filters.accumulated_values,
  39. ignore_closing_entries=True,
  40. ignore_accumulated_values_for_fy=True,
  41. )
  42. expense = get_data(
  43. filters.company,
  44. "Expense",
  45. "Debit",
  46. period_list,
  47. filters=filters,
  48. accumulated_values=filters.accumulated_values,
  49. ignore_closing_entries=True,
  50. ignore_accumulated_values_for_fy=True,
  51. )
  52. net_profit_loss = get_net_profit_loss(income, expense, period_list, filters.company)
  53. data = []
  54. summary_data = {}
  55. company_currency = frappe.get_cached_value("Company", filters.company, "default_currency")
  56. for cash_flow_account in cash_flow_accounts:
  57. section_data = []
  58. data.append(
  59. {
  60. "account_name": cash_flow_account["section_header"],
  61. "parent_account": None,
  62. "indent": 0.0,
  63. "account": cash_flow_account["section_header"],
  64. }
  65. )
  66. if len(data) == 1:
  67. # add first net income in operations section
  68. if net_profit_loss:
  69. net_profit_loss.update(
  70. {"indent": 1, "parent_account": cash_flow_accounts[0]["section_header"]}
  71. )
  72. data.append(net_profit_loss)
  73. section_data.append(net_profit_loss)
  74. for account in cash_flow_account["account_types"]:
  75. account_data = get_account_type_based_data(
  76. filters.company, account["account_type"], period_list, filters.accumulated_values, filters
  77. )
  78. account_data.update(
  79. {
  80. "account_name": account["label"],
  81. "account": account["label"],
  82. "indent": 1,
  83. "parent_account": cash_flow_account["section_header"],
  84. "currency": company_currency,
  85. }
  86. )
  87. data.append(account_data)
  88. section_data.append(account_data)
  89. add_total_row_account(
  90. data,
  91. section_data,
  92. cash_flow_account["section_footer"],
  93. period_list,
  94. company_currency,
  95. summary_data,
  96. filters,
  97. )
  98. add_total_row_account(
  99. data, data, _("Net Change in Cash"), period_list, company_currency, summary_data, filters
  100. )
  101. columns = get_columns(
  102. filters.periodicity, period_list, filters.accumulated_values, filters.company
  103. )
  104. chart = get_chart_data(columns, data)
  105. report_summary = get_report_summary(summary_data, company_currency)
  106. return columns, data, None, chart, report_summary
  107. def get_cash_flow_accounts():
  108. operation_accounts = {
  109. "section_name": "Operations",
  110. "section_footer": _("Net Cash from Operations"),
  111. "section_header": _("Cash Flow from Operations"),
  112. "account_types": [
  113. {"account_type": "Depreciation", "label": _("Depreciation")},
  114. {"account_type": "Receivable", "label": _("Net Change in Accounts Receivable")},
  115. {"account_type": "Payable", "label": _("Net Change in Accounts Payable")},
  116. {"account_type": "Stock", "label": _("Net Change in Inventory")},
  117. ],
  118. }
  119. investing_accounts = {
  120. "section_name": "Investing",
  121. "section_footer": _("Net Cash from Investing"),
  122. "section_header": _("Cash Flow from Investing"),
  123. "account_types": [{"account_type": "Fixed Asset", "label": _("Net Change in Fixed Asset")}],
  124. }
  125. financing_accounts = {
  126. "section_name": "Financing",
  127. "section_footer": _("Net Cash from Financing"),
  128. "section_header": _("Cash Flow from Financing"),
  129. "account_types": [{"account_type": "Equity", "label": _("Net Change in Equity")}],
  130. }
  131. # combine all cash flow accounts for iteration
  132. return [operation_accounts, investing_accounts, financing_accounts]
  133. def get_account_type_based_data(company, account_type, period_list, accumulated_values, filters):
  134. data = {}
  135. total = 0
  136. for period in period_list:
  137. start_date = get_start_date(period, accumulated_values, company)
  138. filters.start_date = start_date
  139. filters.end_date = period["to_date"]
  140. filters.account_type = account_type
  141. amount = get_account_type_based_gl_data(company, filters)
  142. if amount and account_type == "Depreciation":
  143. amount *= -1
  144. total += amount
  145. data.setdefault(period["key"], amount)
  146. data["total"] = total
  147. return data
  148. def get_account_type_based_gl_data(company, filters=None):
  149. cond = ""
  150. filters = frappe._dict(filters or {})
  151. if filters.include_default_book_entries:
  152. company_fb = frappe.db.get_value("Company", company, "default_finance_book")
  153. cond = """ AND (finance_book in (%s, %s, '') OR finance_book IS NULL)
  154. """ % (
  155. frappe.db.escape(filters.finance_book),
  156. frappe.db.escape(company_fb),
  157. )
  158. else:
  159. cond = " AND (finance_book in (%s, '') OR finance_book IS NULL)" % (
  160. frappe.db.escape(cstr(filters.finance_book))
  161. )
  162. if filters.get("cost_center"):
  163. filters.cost_center = get_cost_centers_with_children(filters.cost_center)
  164. cond += " and cost_center in %(cost_center)s"
  165. gl_sum = frappe.db.sql_list(
  166. """
  167. select sum(credit) - sum(debit)
  168. from `tabGL Entry`
  169. where company=%(company)s and posting_date >= %(start_date)s and posting_date <= %(end_date)s
  170. and voucher_type != 'Period Closing Voucher'
  171. and account in ( SELECT name FROM tabAccount WHERE account_type = %(account_type)s) {cond}
  172. """.format(
  173. cond=cond
  174. ),
  175. filters,
  176. )
  177. return gl_sum[0] if gl_sum and gl_sum[0] else 0
  178. def get_start_date(period, accumulated_values, company):
  179. if not accumulated_values and period.get("from_date"):
  180. return period["from_date"]
  181. start_date = period["year_start_date"]
  182. if accumulated_values:
  183. start_date = get_fiscal_year(period.to_date, company=company)[1]
  184. return start_date
  185. def add_total_row_account(
  186. out, data, label, period_list, currency, summary_data, filters, consolidated=False
  187. ):
  188. total_row = {
  189. "account_name": "'" + _("{0}").format(label) + "'",
  190. "account": "'" + _("{0}").format(label) + "'",
  191. "currency": currency,
  192. }
  193. summary_data[label] = 0
  194. # from consolidated financial statement
  195. if filters.get("accumulated_in_group_company"):
  196. period_list = get_filtered_list_for_consolidated_report(filters, period_list)
  197. for row in data:
  198. if row.get("parent_account"):
  199. for period in period_list:
  200. key = period if consolidated else period["key"]
  201. total_row.setdefault(key, 0.0)
  202. total_row[key] += row.get(key, 0.0)
  203. summary_data[label] += row.get(key)
  204. total_row.setdefault("total", 0.0)
  205. total_row["total"] += row["total"]
  206. out.append(total_row)
  207. out.append({})
  208. def get_report_summary(summary_data, currency):
  209. report_summary = []
  210. for label, value in summary_data.items():
  211. report_summary.append(
  212. {"value": value, "label": label, "datatype": "Currency", "currency": currency}
  213. )
  214. return report_summary
  215. def get_chart_data(columns, data):
  216. labels = [d.get("label") for d in columns[2:]]
  217. datasets = [
  218. {
  219. "name": account.get("account").replace("'", ""),
  220. "values": [account.get(d.get("fieldname")) for d in columns[2:]],
  221. }
  222. for account in data
  223. if account.get("parent_account") == None and account.get("currency")
  224. ]
  225. datasets = datasets[:-1]
  226. chart = {"data": {"labels": labels, "datasets": datasets}, "type": "bar"}
  227. chart["fieldtype"] = "Currency"
  228. return chart