You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

378 lines
15 KiB

  1. # © 2016 Julien Coux (Camptocamp)
  2. # Copyright 2020 ForgeFlow S.L. (https://www.forgeflow.com)
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. import operator
  5. from datetime import date, datetime, timedelta
  6. from odoo import api, models
  7. from odoo.tools import float_is_zero
  8. class AgedPartnerBalanceReport(models.AbstractModel):
  9. _name = "report.account_financial_report.aged_partner_balance"
  10. _description = "Aged Partner Balance Report"
  11. _inherit = "report.account_financial_report.abstract_report"
  12. @api.model
  13. def _initialize_account(self, ag_pb_data, acc_id):
  14. ag_pb_data[acc_id] = {}
  15. ag_pb_data[acc_id]["id"] = acc_id
  16. ag_pb_data[acc_id]["residual"] = 0.0
  17. ag_pb_data[acc_id]["current"] = 0.0
  18. ag_pb_data[acc_id]["30_days"] = 0.0
  19. ag_pb_data[acc_id]["60_days"] = 0.0
  20. ag_pb_data[acc_id]["90_days"] = 0.0
  21. ag_pb_data[acc_id]["120_days"] = 0.0
  22. ag_pb_data[acc_id]["older"] = 0.0
  23. return ag_pb_data
  24. @api.model
  25. def _initialize_partner(self, ag_pb_data, acc_id, prt_id):
  26. ag_pb_data[acc_id][prt_id] = {}
  27. ag_pb_data[acc_id][prt_id]["id"] = acc_id
  28. ag_pb_data[acc_id][prt_id]["residual"] = 0.0
  29. ag_pb_data[acc_id][prt_id]["current"] = 0.0
  30. ag_pb_data[acc_id][prt_id]["30_days"] = 0.0
  31. ag_pb_data[acc_id][prt_id]["60_days"] = 0.0
  32. ag_pb_data[acc_id][prt_id]["90_days"] = 0.0
  33. ag_pb_data[acc_id][prt_id]["120_days"] = 0.0
  34. ag_pb_data[acc_id][prt_id]["older"] = 0.0
  35. ag_pb_data[acc_id][prt_id]["move_lines"] = []
  36. return ag_pb_data
  37. @api.model
  38. def _calculate_amounts(
  39. self, ag_pb_data, acc_id, prt_id, residual, due_date, date_at_object
  40. ):
  41. ag_pb_data[acc_id]["residual"] += residual
  42. ag_pb_data[acc_id][prt_id]["residual"] += residual
  43. today = date_at_object
  44. if not due_date or today <= due_date:
  45. ag_pb_data[acc_id]["current"] += residual
  46. ag_pb_data[acc_id][prt_id]["current"] += residual
  47. elif today <= due_date + timedelta(days=30):
  48. ag_pb_data[acc_id]["30_days"] += residual
  49. ag_pb_data[acc_id][prt_id]["30_days"] += residual
  50. elif today <= due_date + timedelta(days=60):
  51. ag_pb_data[acc_id]["60_days"] += residual
  52. ag_pb_data[acc_id][prt_id]["60_days"] += residual
  53. elif today <= due_date + timedelta(days=90):
  54. ag_pb_data[acc_id]["90_days"] += residual
  55. ag_pb_data[acc_id][prt_id]["90_days"] += residual
  56. elif today <= due_date + timedelta(days=120):
  57. ag_pb_data[acc_id]["120_days"] += residual
  58. ag_pb_data[acc_id][prt_id]["120_days"] += residual
  59. else:
  60. ag_pb_data[acc_id]["older"] += residual
  61. ag_pb_data[acc_id][prt_id]["older"] += residual
  62. return ag_pb_data
  63. def _get_account_partial_reconciled(self, company_id, date_at_object):
  64. domain = [("max_date", ">", date_at_object), ("company_id", "=", company_id)]
  65. fields = ["debit_move_id", "credit_move_id", "amount"]
  66. accounts_partial_reconcile = self.env["account.partial.reconcile"].search_read(
  67. domain=domain, fields=fields
  68. )
  69. debit_amount = {}
  70. credit_amount = {}
  71. for account_partial_reconcile_data in accounts_partial_reconcile:
  72. debit_move_id = account_partial_reconcile_data["debit_move_id"][0]
  73. credit_move_id = account_partial_reconcile_data["credit_move_id"][0]
  74. if debit_move_id not in debit_amount.keys():
  75. debit_amount[debit_move_id] = 0.0
  76. debit_amount[debit_move_id] += account_partial_reconcile_data["amount"]
  77. if credit_move_id not in credit_amount.keys():
  78. credit_amount[credit_move_id] = 0.0
  79. credit_amount[credit_move_id] += account_partial_reconcile_data["amount"]
  80. account_partial_reconcile_data.update(
  81. {"debit_move_id": debit_move_id, "credit_move_id": credit_move_id}
  82. )
  83. return accounts_partial_reconcile, debit_amount, credit_amount
  84. def _get_move_lines_data(
  85. self,
  86. company_id,
  87. account_ids,
  88. partner_ids,
  89. date_at_object,
  90. date_from,
  91. only_posted_moves,
  92. show_move_line_details,
  93. ):
  94. domain = self._get_move_lines_domain_not_reconciled(
  95. company_id, account_ids, partner_ids, only_posted_moves, date_from
  96. )
  97. ml_fields = [
  98. "id",
  99. "name",
  100. "date",
  101. "move_id",
  102. "journal_id",
  103. "account_id",
  104. "partner_id",
  105. "amount_residual",
  106. "date_maturity",
  107. "ref",
  108. "reconciled",
  109. ]
  110. move_lines = self.env["account.move.line"].search_read(
  111. domain=domain, fields=ml_fields
  112. )
  113. journals_ids = set()
  114. partners_ids = set()
  115. partners_data = {}
  116. ag_pb_data = {}
  117. if date_at_object < date.today():
  118. (
  119. acc_partial_rec,
  120. debit_amount,
  121. credit_amount,
  122. ) = self._get_account_partial_reconciled(company_id, date_at_object)
  123. if acc_partial_rec:
  124. ml_ids = list(map(operator.itemgetter("id"), move_lines))
  125. debit_ids = list(
  126. map(operator.itemgetter("debit_move_id"), acc_partial_rec)
  127. )
  128. credit_ids = list(
  129. map(operator.itemgetter("credit_move_id"), acc_partial_rec)
  130. )
  131. move_lines = self._recalculate_move_lines(
  132. move_lines,
  133. debit_ids,
  134. credit_ids,
  135. debit_amount,
  136. credit_amount,
  137. ml_ids,
  138. account_ids,
  139. company_id,
  140. partner_ids,
  141. only_posted_moves,
  142. )
  143. move_lines = [
  144. move_line
  145. for move_line in move_lines
  146. if move_line["date"] <= date_at_object
  147. and not float_is_zero(move_line["amount_residual"], precision_digits=2)
  148. ]
  149. for move_line in move_lines:
  150. journals_ids.add(move_line["journal_id"][0])
  151. acc_id = move_line["account_id"][0]
  152. if move_line["partner_id"]:
  153. prt_id = move_line["partner_id"][0]
  154. prt_name = move_line["partner_id"][1]
  155. else:
  156. prt_id = 0
  157. prt_name = ""
  158. if prt_id not in partners_ids:
  159. partners_data.update({prt_id: {"id": prt_id, "name": prt_name}})
  160. partners_ids.add(prt_id)
  161. if acc_id not in ag_pb_data.keys():
  162. ag_pb_data = self._initialize_account(ag_pb_data, acc_id)
  163. if prt_id not in ag_pb_data[acc_id]:
  164. ag_pb_data = self._initialize_partner(ag_pb_data, acc_id, prt_id)
  165. move_line_data = {}
  166. if show_move_line_details:
  167. if move_line["ref"] == move_line["name"]:
  168. if move_line["ref"]:
  169. ref_label = move_line["ref"]
  170. else:
  171. ref_label = ""
  172. elif not move_line["ref"]:
  173. ref_label = move_line["name"]
  174. elif not move_line["name"]:
  175. ref_label = move_line["ref"]
  176. else:
  177. ref_label = move_line["ref"] + str(" - ") + move_line["name"]
  178. move_line_data.update(
  179. {
  180. "date": move_line["date"],
  181. "entry": move_line["move_id"][1],
  182. "jnl_id": move_line["journal_id"][0],
  183. "acc_id": acc_id,
  184. "partner": prt_name,
  185. "ref_label": ref_label,
  186. "due_date": move_line["date_maturity"],
  187. "residual": move_line["amount_residual"],
  188. }
  189. )
  190. ag_pb_data[acc_id][prt_id]["move_lines"].append(move_line_data)
  191. ag_pb_data = self._calculate_amounts(
  192. ag_pb_data,
  193. acc_id,
  194. prt_id,
  195. move_line["amount_residual"],
  196. move_line["date_maturity"],
  197. date_at_object,
  198. )
  199. journals_data = self._get_journals_data(list(journals_ids))
  200. accounts_data = self._get_accounts_data(ag_pb_data.keys())
  201. return ag_pb_data, accounts_data, partners_data, journals_data
  202. @api.model
  203. def _compute_maturity_date(self, ml, date_at_object):
  204. ml.update(
  205. {
  206. "current": 0.0,
  207. "30_days": 0.0,
  208. "60_days": 0.0,
  209. "90_days": 0.0,
  210. "120_days": 0.0,
  211. "older": 0.0,
  212. }
  213. )
  214. due_date = ml["due_date"]
  215. amount = ml["residual"]
  216. today = date_at_object
  217. if not due_date or today <= due_date:
  218. ml["current"] += amount
  219. elif today <= due_date + timedelta(days=30):
  220. ml["30_days"] += amount
  221. elif today <= due_date + timedelta(days=60):
  222. ml["60_days"] += amount
  223. elif today <= due_date + timedelta(days=90):
  224. ml["90_days"] += amount
  225. elif today <= due_date + timedelta(days=120):
  226. ml["120_days"] += amount
  227. else:
  228. ml["older"] += amount
  229. def _create_account_list(
  230. self,
  231. ag_pb_data,
  232. accounts_data,
  233. partners_data,
  234. journals_data,
  235. show_move_line_details,
  236. date_at_oject,
  237. ):
  238. aged_partner_data = []
  239. for account in accounts_data.values():
  240. acc_id = account["id"]
  241. account.update(
  242. {
  243. "residual": ag_pb_data[acc_id]["residual"],
  244. "current": ag_pb_data[acc_id]["current"],
  245. "30_days": ag_pb_data[acc_id]["30_days"],
  246. "60_days": ag_pb_data[acc_id]["60_days"],
  247. "90_days": ag_pb_data[acc_id]["90_days"],
  248. "120_days": ag_pb_data[acc_id]["120_days"],
  249. "older": ag_pb_data[acc_id]["older"],
  250. "partners": [],
  251. }
  252. )
  253. for prt_id in ag_pb_data[acc_id]:
  254. if isinstance(prt_id, int):
  255. partner = {
  256. "name": partners_data[prt_id]["name"],
  257. "residual": ag_pb_data[acc_id][prt_id]["residual"],
  258. "current": ag_pb_data[acc_id][prt_id]["current"],
  259. "30_days": ag_pb_data[acc_id][prt_id]["30_days"],
  260. "60_days": ag_pb_data[acc_id][prt_id]["60_days"],
  261. "90_days": ag_pb_data[acc_id][prt_id]["90_days"],
  262. "120_days": ag_pb_data[acc_id][prt_id]["120_days"],
  263. "older": ag_pb_data[acc_id][prt_id]["older"],
  264. }
  265. if show_move_line_details:
  266. move_lines = []
  267. for ml in ag_pb_data[acc_id][prt_id]["move_lines"]:
  268. ml.update(
  269. {
  270. "journal": journals_data[ml["jnl_id"]]["code"],
  271. "account": accounts_data[ml["acc_id"]]["code"],
  272. }
  273. )
  274. self._compute_maturity_date(ml, date_at_oject)
  275. move_lines.append(ml)
  276. move_lines = sorted(move_lines, key=lambda k: (k["date"]))
  277. partner.update({"move_lines": move_lines})
  278. account["partners"].append(partner)
  279. aged_partner_data.append(account)
  280. return aged_partner_data
  281. @api.model
  282. def _calculate_percent(self, aged_partner_data):
  283. for account in aged_partner_data:
  284. if abs(account["residual"]) > 0.01:
  285. total = account["residual"]
  286. account.update(
  287. {
  288. "percent_current": abs(
  289. round((account["current"] / total) * 100, 2)
  290. ),
  291. "percent_30_days": abs(
  292. round((account["30_days"] / total) * 100, 2)
  293. ),
  294. "percent_60_days": abs(
  295. round((account["60_days"] / total) * 100, 2)
  296. ),
  297. "percent_90_days": abs(
  298. round((account["90_days"] / total) * 100, 2)
  299. ),
  300. "percent_120_days": abs(
  301. round((account["120_days"] / total) * 100, 2)
  302. ),
  303. "percent_older": abs(
  304. round((account["older"] / total) * 100, 2)
  305. ),
  306. }
  307. )
  308. else:
  309. account.update(
  310. {
  311. "percent_current": 0.0,
  312. "percent_30_days": 0.0,
  313. "percent_60_days": 0.0,
  314. "percent_90_days": 0.0,
  315. "percent_120_days": 0.0,
  316. "percent_older": 0.0,
  317. }
  318. )
  319. return aged_partner_data
  320. def _get_report_values(self, docids, data):
  321. wizard_id = data["wizard_id"]
  322. company = self.env["res.company"].browse(data["company_id"])
  323. company_id = data["company_id"]
  324. account_ids = data["account_ids"]
  325. partner_ids = data["partner_ids"]
  326. date_at = data["date_at"]
  327. date_at_object = datetime.strptime(date_at, "%Y-%m-%d").date()
  328. date_from = data["date_from"]
  329. only_posted_moves = data["only_posted_moves"]
  330. show_move_line_details = data["show_move_line_details"]
  331. (
  332. ag_pb_data,
  333. accounts_data,
  334. partners_data,
  335. journals_data,
  336. ) = self._get_move_lines_data(
  337. company_id,
  338. account_ids,
  339. partner_ids,
  340. date_at_object,
  341. date_from,
  342. only_posted_moves,
  343. show_move_line_details,
  344. )
  345. aged_partner_data = self._create_account_list(
  346. ag_pb_data,
  347. accounts_data,
  348. partners_data,
  349. journals_data,
  350. show_move_line_details,
  351. date_at_object,
  352. )
  353. aged_partner_data = self._calculate_percent(aged_partner_data)
  354. return {
  355. "doc_ids": [wizard_id],
  356. "doc_model": "open.items.report.wizard",
  357. "docs": self.env["open.items.report.wizard"].browse(wizard_id),
  358. "company_name": company.display_name,
  359. "company_currency": company.currency_id,
  360. "currency_name": company.currency_id.name,
  361. "date_at": date_at,
  362. "only_posted_moves": only_posted_moves,
  363. "aged_partner_balance": aged_partner_data,
  364. "show_move_lines_details": show_move_line_details,
  365. }