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.

858 lines
34 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 calendar
  5. import datetime
  6. import operator
  7. from odoo import _, api, models
  8. from odoo.tools import float_is_zero
  9. class GeneralLedgerReport(models.AbstractModel):
  10. _name = "report.account_financial_report.general_ledger"
  11. _description = "General Ledger Report"
  12. _inherit = "report.account_financial_report.abstract_report"
  13. def _get_tags_data(self, tags_ids):
  14. tags = self.env["account.analytic.tag"].browse(tags_ids)
  15. tags_data = {}
  16. for tag in tags:
  17. tags_data.update({tag.id: {"name": tag.name}})
  18. return tags_data
  19. def _get_taxes_data(self, taxes_ids):
  20. taxes = self.env["account.tax"].browse(taxes_ids)
  21. taxes_data = {}
  22. for tax in taxes:
  23. taxes_data.update(
  24. {
  25. tax.id: {
  26. "id": tax.id,
  27. "amount": tax.amount,
  28. "amount_type": tax.amount_type,
  29. "display_name": tax.display_name,
  30. }
  31. }
  32. )
  33. if tax.amount_type == "percent" or tax.amount_type == "division":
  34. taxes_data[tax.id]["string"] = "%"
  35. else:
  36. taxes_data[tax.id]["string"] = ""
  37. taxes_data[tax.id]["tax_name"] = (
  38. tax.display_name
  39. + " ("
  40. + str(tax.amount)
  41. + taxes_data[tax.id]["string"]
  42. + ")"
  43. )
  44. return taxes_data
  45. def _get_acc_prt_accounts_ids(self, company_id):
  46. accounts_domain = [
  47. ("company_id", "=", company_id),
  48. ("internal_type", "in", ["receivable", "payable"]),
  49. ]
  50. acc_prt_accounts = self.env["account.account"].search(accounts_domain)
  51. return acc_prt_accounts.ids
  52. def _get_initial_balances_bs_ml_domain(
  53. self, account_ids, company_id, date_from, base_domain, acc_prt=False
  54. ):
  55. accounts_domain = [
  56. ("company_id", "=", company_id),
  57. ("user_type_id.include_initial_balance", "=", True),
  58. ]
  59. if account_ids:
  60. accounts_domain += [("id", "in", account_ids)]
  61. domain = []
  62. domain += base_domain
  63. domain += [("date", "<", date_from)]
  64. accounts = self.env["account.account"].search(accounts_domain)
  65. domain += [("account_id", "in", accounts.ids)]
  66. if acc_prt:
  67. domain += [("account_id.internal_type", "in", ["receivable", "payable"])]
  68. return domain
  69. def _get_initial_balances_pl_ml_domain(
  70. self, account_ids, company_id, date_from, fy_start_date, base_domain
  71. ):
  72. accounts_domain = [
  73. ("company_id", "=", company_id),
  74. ("user_type_id.include_initial_balance", "=", False),
  75. ]
  76. if account_ids:
  77. accounts_domain += [("id", "in", account_ids)]
  78. domain = []
  79. domain += base_domain
  80. domain += [("date", "<", date_from), ("date", ">=", fy_start_date)]
  81. accounts = self.env["account.account"].search(accounts_domain)
  82. domain += [("account_id", "in", accounts.ids)]
  83. return domain
  84. def _get_accounts_initial_balance(self, initial_domain_bs, initial_domain_pl):
  85. gl_initial_acc_bs = self.env["account.move.line"].read_group(
  86. domain=initial_domain_bs,
  87. fields=["account_id", "debit", "credit", "balance", "amount_currency"],
  88. groupby=["account_id"],
  89. )
  90. gl_initial_acc_pl = self.env["account.move.line"].read_group(
  91. domain=initial_domain_pl,
  92. fields=["account_id", "debit", "credit", "balance", "amount_currency"],
  93. groupby=["account_id"],
  94. )
  95. gl_initial_acc = gl_initial_acc_bs + gl_initial_acc_pl
  96. return gl_initial_acc
  97. def _get_initial_balance_fy_pl_ml_domain(
  98. self, account_ids, company_id, fy_start_date, base_domain
  99. ):
  100. accounts_domain = [
  101. ("company_id", "=", company_id),
  102. ("user_type_id.include_initial_balance", "=", False),
  103. ]
  104. if account_ids:
  105. accounts_domain += [("id", "in", account_ids)]
  106. domain = []
  107. domain += base_domain
  108. domain += [("date", "<", fy_start_date)]
  109. accounts = self.env["account.account"].search(accounts_domain)
  110. domain += [("account_id", "in", accounts.ids)]
  111. return domain
  112. def _get_pl_initial_balance(
  113. self, account_ids, company_id, fy_start_date, foreign_currency, base_domain
  114. ):
  115. domain = self._get_initial_balance_fy_pl_ml_domain(
  116. account_ids, company_id, fy_start_date, base_domain
  117. )
  118. initial_balances = self.env["account.move.line"].read_group(
  119. domain=domain,
  120. fields=["account_id", "debit", "credit", "balance", "amount_currency"],
  121. groupby=["account_id"],
  122. )
  123. pl_initial_balance = {
  124. "debit": 0.0,
  125. "credit": 0.0,
  126. "balance": 0.0,
  127. "bal_curr": 0.0,
  128. }
  129. for initial_balance in initial_balances:
  130. pl_initial_balance["debit"] += initial_balance["debit"]
  131. pl_initial_balance["credit"] += initial_balance["credit"]
  132. pl_initial_balance["balance"] += initial_balance["balance"]
  133. pl_initial_balance["bal_curr"] += initial_balance["amount_currency"]
  134. return pl_initial_balance
  135. def _get_initial_balance_data(
  136. self,
  137. account_ids,
  138. partner_ids,
  139. company_id,
  140. date_from,
  141. foreign_currency,
  142. only_posted_moves,
  143. unaffected_earnings_account,
  144. fy_start_date,
  145. analytic_tag_ids,
  146. cost_center_ids,
  147. extra_domain,
  148. ):
  149. # If explicit list of accounts is provided,
  150. # don't include unaffected earnings account
  151. if account_ids:
  152. unaffected_earnings_account = False
  153. base_domain = []
  154. if company_id:
  155. base_domain += [("company_id", "=", company_id)]
  156. if partner_ids:
  157. base_domain += [("partner_id", "in", partner_ids)]
  158. if only_posted_moves:
  159. base_domain += [("move_id.state", "=", "posted")]
  160. else:
  161. base_domain += [("move_id.state", "in", ["posted", "draft"])]
  162. if analytic_tag_ids:
  163. base_domain += [("analytic_tag_ids", "in", analytic_tag_ids)]
  164. if cost_center_ids:
  165. base_domain += [("analytic_account_id", "in", cost_center_ids)]
  166. if extra_domain:
  167. base_domain += extra_domain
  168. initial_domain_bs = self._get_initial_balances_bs_ml_domain(
  169. account_ids, company_id, date_from, base_domain
  170. )
  171. initial_domain_pl = self._get_initial_balances_pl_ml_domain(
  172. account_ids, company_id, date_from, fy_start_date, base_domain
  173. )
  174. gl_initial_acc = self._get_accounts_initial_balance(
  175. initial_domain_bs, initial_domain_pl
  176. )
  177. initial_domain_acc_prt = self._get_initial_balances_bs_ml_domain(
  178. account_ids, company_id, date_from, base_domain, acc_prt=True
  179. )
  180. gl_initial_acc_prt = self.env["account.move.line"].read_group(
  181. domain=initial_domain_acc_prt,
  182. fields=[
  183. "account_id",
  184. "partner_id",
  185. "debit",
  186. "credit",
  187. "balance",
  188. "amount_currency",
  189. ],
  190. groupby=["account_id", "partner_id"],
  191. lazy=False,
  192. )
  193. gen_ld_data = {}
  194. for gl in gl_initial_acc:
  195. acc_id = gl["account_id"][0]
  196. gen_ld_data[acc_id] = {}
  197. gen_ld_data[acc_id]["id"] = acc_id
  198. gen_ld_data[acc_id]["partners"] = False
  199. gen_ld_data[acc_id]["init_bal"] = {}
  200. gen_ld_data[acc_id]["init_bal"]["credit"] = gl["credit"]
  201. gen_ld_data[acc_id]["init_bal"]["debit"] = gl["debit"]
  202. gen_ld_data[acc_id]["init_bal"]["balance"] = gl["balance"]
  203. gen_ld_data[acc_id]["fin_bal"] = {}
  204. gen_ld_data[acc_id]["fin_bal"]["credit"] = gl["credit"]
  205. gen_ld_data[acc_id]["fin_bal"]["debit"] = gl["debit"]
  206. gen_ld_data[acc_id]["fin_bal"]["balance"] = gl["balance"]
  207. gen_ld_data[acc_id]["init_bal"]["bal_curr"] = gl["amount_currency"]
  208. gen_ld_data[acc_id]["fin_bal"]["bal_curr"] = gl["amount_currency"]
  209. partners_data = {}
  210. partners_ids = set()
  211. if gl_initial_acc_prt:
  212. for gl in gl_initial_acc_prt:
  213. if not gl["partner_id"]:
  214. prt_id = 0
  215. prt_name = "Missing Partner"
  216. else:
  217. prt_id = gl["partner_id"][0]
  218. prt_name = gl["partner_id"][1]
  219. prt_name = prt_name._value
  220. if prt_id not in partners_ids:
  221. partners_ids.add(prt_id)
  222. partners_data.update({prt_id: {"id": prt_id, "name": prt_name}})
  223. acc_id = gl["account_id"][0]
  224. gen_ld_data[acc_id][prt_id] = {}
  225. gen_ld_data[acc_id][prt_id]["id"] = prt_id
  226. gen_ld_data[acc_id]["partners"] = True
  227. gen_ld_data[acc_id][prt_id]["init_bal"] = {}
  228. gen_ld_data[acc_id][prt_id]["init_bal"]["credit"] = gl["credit"]
  229. gen_ld_data[acc_id][prt_id]["init_bal"]["debit"] = gl["debit"]
  230. gen_ld_data[acc_id][prt_id]["init_bal"]["balance"] = gl["balance"]
  231. gen_ld_data[acc_id][prt_id]["fin_bal"] = {}
  232. gen_ld_data[acc_id][prt_id]["fin_bal"]["credit"] = gl["credit"]
  233. gen_ld_data[acc_id][prt_id]["fin_bal"]["debit"] = gl["debit"]
  234. gen_ld_data[acc_id][prt_id]["fin_bal"]["balance"] = gl["balance"]
  235. gen_ld_data[acc_id][prt_id]["init_bal"]["bal_curr"] = gl[
  236. "amount_currency"
  237. ]
  238. gen_ld_data[acc_id][prt_id]["fin_bal"]["bal_curr"] = gl[
  239. "amount_currency"
  240. ]
  241. accounts_ids = list(gen_ld_data.keys())
  242. unaffected_id = unaffected_earnings_account
  243. if unaffected_id:
  244. if unaffected_id not in accounts_ids:
  245. accounts_ids.append(unaffected_id)
  246. self._initialize_account(gen_ld_data, unaffected_id, foreign_currency)
  247. pl_initial_balance = self._get_pl_initial_balance(
  248. account_ids, company_id, fy_start_date, foreign_currency, base_domain
  249. )
  250. gen_ld_data[unaffected_id]["init_bal"]["debit"] += pl_initial_balance[
  251. "debit"
  252. ]
  253. gen_ld_data[unaffected_id]["init_bal"]["credit"] += pl_initial_balance[
  254. "credit"
  255. ]
  256. gen_ld_data[unaffected_id]["init_bal"]["balance"] += pl_initial_balance[
  257. "balance"
  258. ]
  259. gen_ld_data[unaffected_id]["fin_bal"]["debit"] += pl_initial_balance[
  260. "debit"
  261. ]
  262. gen_ld_data[unaffected_id]["fin_bal"]["credit"] += pl_initial_balance[
  263. "credit"
  264. ]
  265. gen_ld_data[unaffected_id]["fin_bal"]["balance"] += pl_initial_balance[
  266. "balance"
  267. ]
  268. if foreign_currency:
  269. gen_ld_data[unaffected_id]["init_bal"][
  270. "bal_curr"
  271. ] += pl_initial_balance["bal_curr"]
  272. gen_ld_data[unaffected_id]["fin_bal"]["bal_curr"] += pl_initial_balance[
  273. "bal_curr"
  274. ]
  275. return gen_ld_data, partners_data, partner_ids
  276. @api.model
  277. def _get_move_line_data(self, move_line):
  278. move_line_data = {
  279. "id": move_line["id"],
  280. "date": move_line["date"],
  281. "entry": move_line["move_id"][1],
  282. "entry_id": move_line["move_id"][0],
  283. "journal_id": move_line["journal_id"][0],
  284. "account_id": move_line["account_id"][0],
  285. "partner_id": move_line["partner_id"][0]
  286. if move_line["partner_id"]
  287. else False,
  288. "partner_name": move_line["partner_id"][1]
  289. if move_line["partner_id"]
  290. else "",
  291. "ref": "" if not move_line["ref"] else move_line["ref"],
  292. "name": "" if not move_line["name"] else move_line["name"],
  293. "tax_ids": move_line["tax_ids"],
  294. "debit": move_line["debit"],
  295. "credit": move_line["credit"],
  296. "balance": move_line["balance"],
  297. "bal_curr": move_line["amount_currency"],
  298. "rec_id": move_line["full_reconcile_id"][0]
  299. if move_line["full_reconcile_id"]
  300. else False,
  301. "rec_name": move_line["full_reconcile_id"][1]
  302. if move_line["full_reconcile_id"]
  303. else "",
  304. "tag_ids": move_line["analytic_tag_ids"],
  305. "currency_id": move_line["currency_id"],
  306. "analytic_account": move_line["analytic_account_id"][1]
  307. if move_line["analytic_account_id"]
  308. else "",
  309. "analytic_account_id": move_line["analytic_account_id"][0]
  310. if move_line["analytic_account_id"]
  311. else False,
  312. }
  313. if (
  314. move_line_data["ref"] == move_line_data["name"]
  315. or move_line_data["ref"] == ""
  316. ):
  317. ref_label = move_line_data["name"]
  318. elif move_line_data["name"] == "":
  319. ref_label = move_line_data["ref"]
  320. else:
  321. ref_label = move_line_data["ref"] + str(" - ") + move_line_data["name"]
  322. move_line_data.update({"ref_label": ref_label})
  323. return move_line_data
  324. @api.model
  325. def _get_period_domain(
  326. self,
  327. account_ids,
  328. partner_ids,
  329. company_id,
  330. only_posted_moves,
  331. date_to,
  332. date_from,
  333. analytic_tag_ids,
  334. cost_center_ids,
  335. ):
  336. domain = [
  337. ("display_type", "=", False),
  338. ("date", ">=", date_from),
  339. ("date", "<=", date_to),
  340. ]
  341. if account_ids:
  342. domain += [("account_id", "in", account_ids)]
  343. if company_id:
  344. domain += [("company_id", "=", company_id)]
  345. if partner_ids:
  346. domain += [("partner_id", "in", partner_ids)]
  347. if only_posted_moves:
  348. domain += [("move_id.state", "=", "posted")]
  349. else:
  350. domain += [("move_id.state", "in", ["posted", "draft"])]
  351. if analytic_tag_ids:
  352. domain += [("analytic_tag_ids", "in", analytic_tag_ids)]
  353. if cost_center_ids:
  354. domain += [("analytic_account_id", "in", cost_center_ids)]
  355. return domain
  356. @api.model
  357. def _initialize_partner(self, gen_ld_data, acc_id, prt_id, foreign_currency):
  358. gen_ld_data[acc_id]["partners"] = True
  359. gen_ld_data[acc_id][prt_id] = {}
  360. gen_ld_data[acc_id][prt_id]["id"] = prt_id
  361. gen_ld_data[acc_id][prt_id]["init_bal"] = {}
  362. gen_ld_data[acc_id][prt_id]["init_bal"]["balance"] = 0.0
  363. gen_ld_data[acc_id][prt_id]["init_bal"]["credit"] = 0.0
  364. gen_ld_data[acc_id][prt_id]["init_bal"]["debit"] = 0.0
  365. gen_ld_data[acc_id][prt_id]["fin_bal"] = {}
  366. gen_ld_data[acc_id][prt_id]["fin_bal"]["credit"] = 0.0
  367. gen_ld_data[acc_id][prt_id]["fin_bal"]["debit"] = 0.0
  368. gen_ld_data[acc_id][prt_id]["fin_bal"]["balance"] = 0.0
  369. if foreign_currency:
  370. gen_ld_data[acc_id][prt_id]["init_bal"]["bal_curr"] = 0.0
  371. gen_ld_data[acc_id][prt_id]["fin_bal"]["bal_curr"] = 0.0
  372. return gen_ld_data
  373. def _initialize_account(self, gen_ld_data, acc_id, foreign_currency):
  374. gen_ld_data[acc_id] = {}
  375. gen_ld_data[acc_id]["id"] = acc_id
  376. gen_ld_data[acc_id]["partners"] = False
  377. gen_ld_data[acc_id]["init_bal"] = {}
  378. gen_ld_data[acc_id]["init_bal"]["balance"] = 0.0
  379. gen_ld_data[acc_id]["init_bal"]["credit"] = 0.0
  380. gen_ld_data[acc_id]["init_bal"]["debit"] = 0.0
  381. gen_ld_data[acc_id]["fin_bal"] = {}
  382. gen_ld_data[acc_id]["fin_bal"]["credit"] = 0.0
  383. gen_ld_data[acc_id]["fin_bal"]["debit"] = 0.0
  384. gen_ld_data[acc_id]["fin_bal"]["balance"] = 0.0
  385. if foreign_currency:
  386. gen_ld_data[acc_id]["init_bal"]["bal_curr"] = 0.0
  387. gen_ld_data[acc_id]["fin_bal"]["bal_curr"] = 0.0
  388. return gen_ld_data
  389. def _get_reconciled_after_date_to_ids(self, full_reconcile_ids, date_to):
  390. full_reconcile_ids = list(full_reconcile_ids)
  391. domain = [
  392. ("max_date", ">", date_to),
  393. ("full_reconcile_id", "in", full_reconcile_ids),
  394. ]
  395. fields = ["full_reconcile_id"]
  396. reconciled_after_date_to = self.env["account.partial.reconcile"].search_read(
  397. domain=domain, fields=fields
  398. )
  399. rec_after_date_to_ids = list(
  400. map(operator.itemgetter("full_reconcile_id"), reconciled_after_date_to)
  401. )
  402. rec_after_date_to_ids = [i[0] for i in rec_after_date_to_ids]
  403. return rec_after_date_to_ids
  404. def _get_period_ml_data(
  405. self,
  406. account_ids,
  407. partner_ids,
  408. company_id,
  409. foreign_currency,
  410. only_posted_moves,
  411. date_from,
  412. date_to,
  413. partners_data,
  414. gen_ld_data,
  415. partners_ids,
  416. analytic_tag_ids,
  417. cost_center_ids,
  418. extra_domain,
  419. ):
  420. domain = self._get_period_domain(
  421. account_ids,
  422. partner_ids,
  423. company_id,
  424. only_posted_moves,
  425. date_to,
  426. date_from,
  427. analytic_tag_ids,
  428. cost_center_ids,
  429. )
  430. if extra_domain:
  431. domain += extra_domain
  432. ml_fields = [
  433. "id",
  434. "name",
  435. "date",
  436. "move_id",
  437. "journal_id",
  438. "account_id",
  439. "partner_id",
  440. "debit",
  441. "credit",
  442. "balance",
  443. "currency_id",
  444. "full_reconcile_id",
  445. "tax_ids",
  446. "analytic_tag_ids",
  447. "amount_currency",
  448. "ref",
  449. "name",
  450. "analytic_account_id",
  451. ]
  452. move_lines = self.env["account.move.line"].search_read(
  453. domain=domain, fields=ml_fields
  454. )
  455. journal_ids = set()
  456. full_reconcile_ids = set()
  457. taxes_ids = set()
  458. tags_ids = set()
  459. full_reconcile_data = {}
  460. acc_prt_account_ids = self._get_acc_prt_accounts_ids(company_id)
  461. for move_line in move_lines:
  462. journal_ids.add(move_line["journal_id"][0])
  463. for tax_id in move_line["tax_ids"]:
  464. taxes_ids.add(tax_id)
  465. for analytic_tag_id in move_line["analytic_tag_ids"]:
  466. tags_ids.add(analytic_tag_id)
  467. if move_line["full_reconcile_id"]:
  468. rec_id = move_line["full_reconcile_id"][0]
  469. if rec_id not in full_reconcile_ids:
  470. full_reconcile_data.update(
  471. {
  472. rec_id: {
  473. "id": rec_id,
  474. "name": move_line["full_reconcile_id"][1],
  475. }
  476. }
  477. )
  478. full_reconcile_ids.add(rec_id)
  479. acc_id = move_line["account_id"][0]
  480. ml_id = move_line["id"]
  481. if move_line["partner_id"]:
  482. prt_id = move_line["partner_id"][0]
  483. partner_name = move_line["partner_id"][1]
  484. if acc_id not in gen_ld_data.keys():
  485. gen_ld_data = self._initialize_account(
  486. gen_ld_data, acc_id, foreign_currency
  487. )
  488. if acc_id in acc_prt_account_ids:
  489. if not move_line["partner_id"]:
  490. prt_id = 0
  491. partner_name = "Missing Partner"
  492. partners_ids.append(prt_id)
  493. partners_data.update({prt_id: {"id": prt_id, "name": partner_name}})
  494. if prt_id not in gen_ld_data[acc_id]:
  495. gen_ld_data = self._initialize_partner(
  496. gen_ld_data, acc_id, prt_id, foreign_currency
  497. )
  498. gen_ld_data[acc_id][prt_id][ml_id] = self._get_move_line_data(move_line)
  499. gen_ld_data[acc_id][prt_id]["fin_bal"]["credit"] += move_line["credit"]
  500. gen_ld_data[acc_id][prt_id]["fin_bal"]["debit"] += move_line["debit"]
  501. gen_ld_data[acc_id][prt_id]["fin_bal"]["balance"] += move_line[
  502. "balance"
  503. ]
  504. if foreign_currency:
  505. gen_ld_data[acc_id][prt_id]["fin_bal"]["bal_curr"] += move_line[
  506. "amount_currency"
  507. ]
  508. else:
  509. gen_ld_data[acc_id][ml_id] = self._get_move_line_data(move_line)
  510. gen_ld_data[acc_id]["fin_bal"]["credit"] += move_line["credit"]
  511. gen_ld_data[acc_id]["fin_bal"]["debit"] += move_line["debit"]
  512. gen_ld_data[acc_id]["fin_bal"]["balance"] += move_line["balance"]
  513. if foreign_currency:
  514. gen_ld_data[acc_id]["fin_bal"]["bal_curr"] += move_line[
  515. "amount_currency"
  516. ]
  517. journals_data = self._get_journals_data(list(journal_ids))
  518. accounts_data = self._get_accounts_data(gen_ld_data.keys())
  519. taxes_data = self._get_taxes_data(list(taxes_ids))
  520. tags_data = self._get_tags_data(list(tags_ids))
  521. rec_after_date_to_ids = self._get_reconciled_after_date_to_ids(
  522. full_reconcile_data.keys(), date_to
  523. )
  524. return (
  525. gen_ld_data,
  526. accounts_data,
  527. partners_data,
  528. journals_data,
  529. full_reconcile_data,
  530. taxes_data,
  531. tags_data,
  532. rec_after_date_to_ids,
  533. )
  534. @api.model
  535. def _recalculate_cumul_balance(
  536. self, move_lines, last_cumul_balance, rec_after_date_to_ids
  537. ):
  538. for move_line in move_lines:
  539. move_line["balance"] += last_cumul_balance
  540. last_cumul_balance = move_line["balance"]
  541. if move_line["rec_id"] in rec_after_date_to_ids:
  542. move_line["rec_name"] = "(" + _("future") + ") " + move_line["rec_name"]
  543. return move_lines
  544. def _create_account(self, account, acc_id, gen_led_data, rec_after_date_to_ids):
  545. move_lines = []
  546. for ml_id in gen_led_data[acc_id].keys():
  547. if not isinstance(ml_id, int):
  548. account.update({ml_id: gen_led_data[acc_id][ml_id]})
  549. else:
  550. move_lines += [gen_led_data[acc_id][ml_id]]
  551. move_lines = sorted(move_lines, key=lambda k: (k["date"]))
  552. move_lines = self._recalculate_cumul_balance(
  553. move_lines,
  554. gen_led_data[acc_id]["init_bal"]["balance"],
  555. rec_after_date_to_ids,
  556. )
  557. account.update({"move_lines": move_lines})
  558. return account
  559. def _create_account_not_show_partner(
  560. self, account, acc_id, gen_led_data, rec_after_date_to_ids
  561. ):
  562. move_lines = []
  563. for prt_id in gen_led_data[acc_id].keys():
  564. if not isinstance(prt_id, int):
  565. account.update({prt_id: gen_led_data[acc_id][prt_id]})
  566. else:
  567. for ml_id in gen_led_data[acc_id][prt_id].keys():
  568. if isinstance(ml_id, int):
  569. move_lines += [gen_led_data[acc_id][prt_id][ml_id]]
  570. move_lines = sorted(move_lines, key=lambda k: (k["date"]))
  571. move_lines = self._recalculate_cumul_balance(
  572. move_lines,
  573. gen_led_data[acc_id]["init_bal"]["balance"],
  574. rec_after_date_to_ids,
  575. )
  576. account.update({"move_lines": move_lines, "partners": False})
  577. return account
  578. def _create_general_ledger(
  579. self,
  580. gen_led_data,
  581. accounts_data,
  582. show_partner_details,
  583. rec_after_date_to_ids,
  584. hide_account_at_0,
  585. ):
  586. general_ledger = []
  587. rounding = self.env.company.currency_id.rounding
  588. for acc_id in gen_led_data.keys():
  589. account = {}
  590. account.update(
  591. {
  592. "code": accounts_data[acc_id]["code"],
  593. "name": accounts_data[acc_id]["name"],
  594. "type": "account",
  595. "currency_id": accounts_data[acc_id]["currency_id"],
  596. "centralized": accounts_data[acc_id]["centralized"],
  597. }
  598. )
  599. if not gen_led_data[acc_id]["partners"]:
  600. account = self._create_account(
  601. account, acc_id, gen_led_data, rec_after_date_to_ids
  602. )
  603. if (
  604. hide_account_at_0
  605. and float_is_zero(
  606. gen_led_data[acc_id]["init_bal"]["balance"],
  607. precision_rounding=rounding,
  608. )
  609. and account["move_lines"] == []
  610. ):
  611. continue
  612. else:
  613. if show_partner_details:
  614. list_partner = []
  615. for prt_id in gen_led_data[acc_id].keys():
  616. partner = {}
  617. move_lines = []
  618. if not isinstance(prt_id, int):
  619. account.update({prt_id: gen_led_data[acc_id][prt_id]})
  620. else:
  621. for ml_id in gen_led_data[acc_id][prt_id].keys():
  622. if not isinstance(ml_id, int):
  623. partner.update(
  624. {ml_id: gen_led_data[acc_id][prt_id][ml_id]}
  625. )
  626. else:
  627. move_lines += [gen_led_data[acc_id][prt_id][ml_id]]
  628. move_lines = sorted(move_lines, key=lambda k: (k["date"]))
  629. move_lines = self._recalculate_cumul_balance(
  630. move_lines,
  631. gen_led_data[acc_id][prt_id]["init_bal"]["balance"],
  632. rec_after_date_to_ids,
  633. )
  634. partner.update({"move_lines": move_lines})
  635. if (
  636. hide_account_at_0
  637. and float_is_zero(
  638. gen_led_data[acc_id][prt_id]["init_bal"]["balance"],
  639. precision_rounding=rounding,
  640. )
  641. and partner["move_lines"] == []
  642. ):
  643. continue
  644. list_partner += [partner]
  645. account.update({"list_partner": list_partner})
  646. if (
  647. hide_account_at_0
  648. and float_is_zero(
  649. gen_led_data[acc_id]["init_bal"]["balance"],
  650. precision_rounding=rounding,
  651. )
  652. and account["list_partner"] == []
  653. ):
  654. continue
  655. else:
  656. account = self._create_account_not_show_partner(
  657. account, acc_id, gen_led_data, rec_after_date_to_ids
  658. )
  659. if (
  660. hide_account_at_0
  661. and float_is_zero(
  662. gen_led_data[acc_id]["init_bal"]["balance"],
  663. precision_rounding=rounding,
  664. )
  665. and account["move_lines"] == []
  666. ):
  667. continue
  668. general_ledger += [account]
  669. return general_ledger
  670. @api.model
  671. def _calculate_centralization(self, centralized_ml, move_line, date_to):
  672. jnl_id = move_line["journal_id"]
  673. month = move_line["date"].month
  674. if jnl_id not in centralized_ml.keys():
  675. centralized_ml[jnl_id] = {}
  676. if month not in centralized_ml[jnl_id].keys():
  677. centralized_ml[jnl_id][month] = {}
  678. last_day_month = calendar.monthrange(move_line["date"].year, month)
  679. date = datetime.date(move_line["date"].year, month, last_day_month[1])
  680. if date > date_to:
  681. date = date_to
  682. centralized_ml[jnl_id][month].update(
  683. {
  684. "journal_id": jnl_id,
  685. "ref_label": "Centralized entries",
  686. "date": date,
  687. "debit": 0.0,
  688. "credit": 0.0,
  689. "balance": 0.0,
  690. "bal_curr": 0.0,
  691. "partner_id": False,
  692. "rec_id": 0,
  693. "entry_id": False,
  694. "tax_ids": [],
  695. "full_reconcile_id": False,
  696. "id": False,
  697. "tag_ids": False,
  698. "currency_id": False,
  699. "analytic_account_id": False,
  700. }
  701. )
  702. centralized_ml[jnl_id][month]["debit"] += move_line["debit"]
  703. centralized_ml[jnl_id][month]["credit"] += move_line["credit"]
  704. centralized_ml[jnl_id][month]["balance"] += (
  705. move_line["debit"] - move_line["credit"]
  706. )
  707. centralized_ml[jnl_id][month]["bal_curr"] += move_line["bal_curr"]
  708. return centralized_ml
  709. @api.model
  710. def _get_centralized_ml(self, account, date_to):
  711. centralized_ml = {}
  712. if isinstance(date_to, str):
  713. date_to = datetime.datetime.strptime(date_to, "%Y-%m-%d").date()
  714. if account["partners"]:
  715. for partner in account["list_partner"]:
  716. for move_line in partner["move_lines"]:
  717. centralized_ml = self._calculate_centralization(
  718. centralized_ml, move_line, date_to,
  719. )
  720. else:
  721. for move_line in account["move_lines"]:
  722. centralized_ml = self._calculate_centralization(
  723. centralized_ml, move_line, date_to,
  724. )
  725. list_centralized_ml = []
  726. for jnl_id in centralized_ml.keys():
  727. list_centralized_ml += list(centralized_ml[jnl_id].values())
  728. return list_centralized_ml
  729. def _get_report_values(self, docids, data):
  730. wizard_id = data["wizard_id"]
  731. company = self.env["res.company"].browse(data["company_id"])
  732. company_id = data["company_id"]
  733. date_to = data["date_to"]
  734. date_from = data["date_from"]
  735. partner_ids = data["partner_ids"]
  736. if not partner_ids:
  737. filter_partner_ids = False
  738. else:
  739. filter_partner_ids = True
  740. account_ids = data["account_ids"]
  741. analytic_tag_ids = data["analytic_tag_ids"]
  742. cost_center_ids = data["cost_center_ids"]
  743. show_partner_details = data["show_partner_details"]
  744. hide_account_at_0 = data["hide_account_at_0"]
  745. foreign_currency = data["foreign_currency"]
  746. only_posted_moves = data["only_posted_moves"]
  747. unaffected_earnings_account = data["unaffected_earnings_account"]
  748. fy_start_date = data["fy_start_date"]
  749. extra_domain = data["domain"]
  750. gen_ld_data, partners_data, partners_ids = self._get_initial_balance_data(
  751. account_ids,
  752. partner_ids,
  753. company_id,
  754. date_from,
  755. foreign_currency,
  756. only_posted_moves,
  757. unaffected_earnings_account,
  758. fy_start_date,
  759. analytic_tag_ids,
  760. cost_center_ids,
  761. extra_domain,
  762. )
  763. centralize = data["centralize"]
  764. (
  765. gen_ld_data,
  766. accounts_data,
  767. partners_data,
  768. journals_data,
  769. full_reconcile_data,
  770. taxes_data,
  771. tags_data,
  772. rec_after_date_to_ids,
  773. ) = self._get_period_ml_data(
  774. account_ids,
  775. partner_ids,
  776. company_id,
  777. foreign_currency,
  778. only_posted_moves,
  779. date_from,
  780. date_to,
  781. partners_data,
  782. gen_ld_data,
  783. partners_ids,
  784. analytic_tag_ids,
  785. cost_center_ids,
  786. extra_domain,
  787. )
  788. general_ledger = self._create_general_ledger(
  789. gen_ld_data,
  790. accounts_data,
  791. show_partner_details,
  792. rec_after_date_to_ids,
  793. hide_account_at_0,
  794. )
  795. if centralize:
  796. for account in general_ledger:
  797. if account["centralized"]:
  798. centralized_ml = self._get_centralized_ml(account, date_to)
  799. account["move_lines"] = centralized_ml
  800. account["move_lines"] = self._recalculate_cumul_balance(
  801. account["move_lines"],
  802. gen_ld_data[account["id"]]["init_bal"]["balance"],
  803. rec_after_date_to_ids,
  804. )
  805. if account["partners"]:
  806. account["partners"] = False
  807. del account["list_partner"]
  808. general_ledger = sorted(general_ledger, key=lambda k: k["code"])
  809. return {
  810. "doc_ids": [wizard_id],
  811. "doc_model": "general.ledger.report.wizard",
  812. "docs": self.env["general.ledger.report.wizard"].browse(wizard_id),
  813. "foreign_currency": data["foreign_currency"],
  814. "company_name": company.display_name,
  815. "company_currency": company.currency_id,
  816. "currency_name": company.currency_id.name,
  817. "date_from": data["date_from"],
  818. "date_to": data["date_to"],
  819. "only_posted_moves": data["only_posted_moves"],
  820. "hide_account_at_0": data["hide_account_at_0"],
  821. "show_analytic_tags": data["show_analytic_tags"],
  822. "show_cost_center": data["show_cost_center"],
  823. "general_ledger": general_ledger,
  824. "accounts_data": accounts_data,
  825. "partners_data": partners_data,
  826. "journals_data": journals_data,
  827. "full_reconcile_data": full_reconcile_data,
  828. "taxes_data": taxes_data,
  829. "centralize": centralize,
  830. "tags_data": tags_data,
  831. "filter_partner_ids": filter_partner_ids,
  832. }