Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

154 строки
4.8 KiB

  1. import frappe
  2. from frappe.utils import flt, formatdate, get_datetime_str
  3. from erpnext import get_company_currency, get_default_company
  4. from erpnext.accounts.doctype.fiscal_year.fiscal_year import get_from_and_to_date
  5. from erpnext.setup.utils import get_exchange_rate
  6. __exchange_rates = {}
  7. def get_currency(filters):
  8. """
  9. Returns a dictionary containing currency information. The keys of the dict are
  10. - company: The company for which we are fetching currency information. if no
  11. company is specified, it will fallback to the default company.
  12. - company currency: The functional currency of the said company.
  13. - presentation currency: The presentation currency to use. Only currencies that
  14. have been used for transactions will be allowed.
  15. - report date: The report date.
  16. :param filters: Report filters
  17. :type filters: dict
  18. :return: str - Currency
  19. """
  20. company = get_appropriate_company(filters)
  21. company_currency = get_company_currency(company)
  22. presentation_currency = (
  23. filters["presentation_currency"] if filters.get("presentation_currency") else company_currency
  24. )
  25. report_date = filters.get("to_date") or filters.get("period_end_date")
  26. if not report_date:
  27. fiscal_year_to_date = get_from_and_to_date(filters.get("to_fiscal_year"))["to_date"]
  28. report_date = formatdate(get_datetime_str(fiscal_year_to_date), "dd-MM-yyyy")
  29. currency_map = dict(
  30. company=company,
  31. company_currency=company_currency,
  32. presentation_currency=presentation_currency,
  33. report_date=report_date,
  34. )
  35. return currency_map
  36. def convert(value, from_, to, date):
  37. """
  38. convert `value` from `from_` to `to` on `date`
  39. :param value: Amount to be converted
  40. :param from_: Currency of `value`
  41. :param to: Currency to convert to
  42. :param date: exchange rate as at this date
  43. :return: Result of converting `value`
  44. """
  45. rate = get_rate_as_at(date, from_, to)
  46. converted_value = flt(value) / (rate or 1)
  47. return converted_value
  48. def get_rate_as_at(date, from_currency, to_currency):
  49. """
  50. Gets exchange rate as at `date` for `from_currency` - `to_currency` exchange rate.
  51. This calls `get_exchange_rate` so that we can get the correct exchange rate as per
  52. the user's Accounts Settings.
  53. It is made efficient by memoising results to `__exchange_rates`
  54. :param date: exchange rate as at this date
  55. :param from_currency: Base currency
  56. :param to_currency: Quote currency
  57. :return: Retrieved exchange rate
  58. """
  59. rate = __exchange_rates.get("{0}-{1}@{2}".format(from_currency, to_currency, date))
  60. if not rate:
  61. rate = get_exchange_rate(from_currency, to_currency, date) or 1
  62. __exchange_rates["{0}-{1}@{2}".format(from_currency, to_currency, date)] = rate
  63. return rate
  64. def convert_to_presentation_currency(gl_entries, currency_info, company):
  65. """
  66. Take a list of GL Entries and change the 'debit' and 'credit' values to currencies
  67. in `currency_info`.
  68. :param gl_entries:
  69. :param currency_info:
  70. :return:
  71. """
  72. converted_gl_list = []
  73. presentation_currency = currency_info["presentation_currency"]
  74. company_currency = currency_info["company_currency"]
  75. account_currencies = list(set(entry["account_currency"] for entry in gl_entries))
  76. for entry in gl_entries:
  77. account = entry["account"]
  78. debit = flt(entry["debit"])
  79. credit = flt(entry["credit"])
  80. debit_in_account_currency = flt(entry["debit_in_account_currency"])
  81. credit_in_account_currency = flt(entry["credit_in_account_currency"])
  82. account_currency = entry["account_currency"]
  83. if len(account_currencies) == 1 and account_currency == presentation_currency:
  84. entry["debit"] = debit_in_account_currency
  85. entry["credit"] = credit_in_account_currency
  86. else:
  87. date = currency_info["report_date"]
  88. converted_debit_value = convert(debit, presentation_currency, company_currency, date)
  89. converted_credit_value = convert(credit, presentation_currency, company_currency, date)
  90. if entry.get("debit"):
  91. entry["debit"] = converted_debit_value
  92. if entry.get("credit"):
  93. entry["credit"] = converted_credit_value
  94. converted_gl_list.append(entry)
  95. return converted_gl_list
  96. def get_appropriate_company(filters):
  97. if filters.get("company"):
  98. company = filters["company"]
  99. else:
  100. company = get_default_company()
  101. return company
  102. @frappe.whitelist()
  103. def get_invoiced_item_gross_margin(
  104. sales_invoice=None, item_code=None, company=None, with_item_data=False
  105. ):
  106. from erpnext.accounts.report.gross_profit.gross_profit import GrossProfitGenerator
  107. sales_invoice = sales_invoice or frappe.form_dict.get("sales_invoice")
  108. item_code = item_code or frappe.form_dict.get("item_code")
  109. company = company or frappe.get_cached_value("Sales Invoice", sales_invoice, "company")
  110. filters = {
  111. "sales_invoice": sales_invoice,
  112. "item_code": item_code,
  113. "company": company,
  114. "group_by": "Invoice",
  115. }
  116. gross_profit_data = GrossProfitGenerator(filters)
  117. result = gross_profit_data.grouped_data
  118. if not with_item_data:
  119. result = sum(d.gross_profit for d in result)
  120. return result