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.

857 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. def _get_accounts_data(self, account_ids):
  13. accounts = self.env["account.account"].browse(account_ids)
  14. accounts_data = {}
  15. for account in accounts:
  16. accounts_data.update(
  17. {
  18. account.id: {
  19. "id": account.id,
  20. "code": account.code,
  21. "name": account.name,
  22. "group_id": account.group_id.id,
  23. "currency_id": account.currency_id or False,
  24. "currency_name": account.currency_id.name,
  25. "centralized": account.centralized,
  26. }
  27. }
  28. )
  29. return accounts_data
  30. def _get_journals_data(self, journals_ids):
  31. journals = self.env["account.journal"].browse(journals_ids)
  32. journals_data = {}
  33. for journal in journals:
  34. journals_data.update({journal.id: {"id": journal.id, "code": journal.code}})
  35. return journals_data
  36. def _get_tags_data(self, tags_ids):
  37. tags = self.env["account.analytic.tag"].browse(tags_ids)
  38. tags_data = {}
  39. for tag in tags:
  40. tags_data.update({tag.id: {"name": tag.name}})
  41. return tags_data
  42. def _get_taxes_data(self, taxes_ids):
  43. taxes = self.env["account.tax"].browse(taxes_ids)
  44. taxes_data = {}
  45. for tax in taxes:
  46. taxes_data.update(
  47. {
  48. tax.id: {
  49. "id": tax.id,
  50. "amount": tax.amount,
  51. "amount_type": tax.amount_type,
  52. "display_name": tax.display_name,
  53. }
  54. }
  55. )
  56. if tax.amount_type == "percent" or tax.amount_type == "division":
  57. taxes_data[tax.id]["string"] = "%"
  58. else:
  59. taxes_data[tax.id]["string"] = ""
  60. taxes_data[tax.id]["tax_name"] = (
  61. tax.display_name
  62. + " ("
  63. + str(tax.amount)
  64. + taxes_data[tax.id]["string"]
  65. + ")"
  66. )
  67. return taxes_data
  68. def _get_acc_prt_accounts_ids(self, company_id):
  69. accounts_domain = [
  70. ("company_id", "=", company_id),
  71. ("internal_type", "in", ["receivable", "payable"]),
  72. ]
  73. acc_prt_accounts = self.env["account.account"].search(accounts_domain)
  74. return acc_prt_accounts.ids
  75. def _get_initial_balances_bs_ml_domain(
  76. self, account_ids, company_id, date_from, base_domain, acc_prt=False
  77. ):
  78. accounts_domain = [
  79. ("company_id", "=", company_id),
  80. ("user_type_id.include_initial_balance", "=", True),
  81. ]
  82. if account_ids:
  83. accounts_domain += [("id", "in", account_ids)]
  84. domain = []
  85. domain += base_domain
  86. domain += [("date", "<", date_from)]
  87. accounts = self.env["account.account"].search(accounts_domain)
  88. domain += [("account_id", "in", accounts.ids)]
  89. if acc_prt:
  90. domain += [("account_id.internal_type", "in", ["receivable", "payable"])]
  91. return domain
  92. def _get_initial_balances_pl_ml_domain(
  93. self, account_ids, company_id, date_from, fy_start_date, base_domain
  94. ):
  95. accounts_domain = [
  96. ("company_id", "=", company_id),
  97. ("user_type_id.include_initial_balance", "=", False),
  98. ]
  99. if account_ids:
  100. accounts_domain += [("id", "in", account_ids)]
  101. domain = []
  102. domain += base_domain
  103. domain += [("date", "<", date_from), ("date", ">=", fy_start_date)]
  104. accounts = self.env["account.account"].search(accounts_domain)
  105. domain += [("account_id", "in", accounts.ids)]
  106. return domain
  107. def _get_accounts_initial_balance(self, initial_domain_bs, initial_domain_pl):
  108. gl_initial_acc_bs = self.env["account.move.line"].read_group(
  109. domain=initial_domain_bs,
  110. fields=["account_id", "debit", "credit", "balance", "amount_currency"],
  111. groupby=["account_id"],
  112. )
  113. gl_initial_acc_pl = self.env["account.move.line"].read_group(
  114. domain=initial_domain_pl,
  115. fields=["account_id", "debit", "credit", "balance", "amount_currency"],
  116. groupby=["account_id"],
  117. )
  118. gl_initial_acc = gl_initial_acc_bs + gl_initial_acc_pl
  119. return gl_initial_acc
  120. def _get_initial_balance_fy_pl_ml_domain(
  121. self, account_ids, company_id, fy_start_date, base_domain
  122. ):
  123. accounts_domain = [
  124. ("company_id", "=", company_id),
  125. ("user_type_id.include_initial_balance", "=", False),
  126. ]
  127. if account_ids:
  128. accounts_domain += [("id", "in", account_ids)]
  129. domain = []
  130. domain += base_domain
  131. domain += [("date", "<", fy_start_date)]
  132. accounts = self.env["account.account"].search(accounts_domain)
  133. domain += [("account_id", "in", accounts.ids)]
  134. return domain
  135. def _get_pl_initial_balance(
  136. self, account_ids, company_id, fy_start_date, foreign_currency, base_domain
  137. ):
  138. domain = self._get_initial_balance_fy_pl_ml_domain(
  139. account_ids, company_id, fy_start_date, base_domain
  140. )
  141. initial_balances = self.env["account.move.line"].read_group(
  142. domain=domain,
  143. fields=["account_id", "debit", "credit", "balance", "amount_currency"],
  144. groupby=["account_id"],
  145. )
  146. pl_initial_balance = {
  147. "debit": 0.0,
  148. "credit": 0.0,
  149. "balance": 0.0,
  150. "bal_curr": 0.0,
  151. }
  152. for initial_balance in initial_balances:
  153. pl_initial_balance["debit"] += initial_balance["debit"]
  154. pl_initial_balance["credit"] += initial_balance["credit"]
  155. pl_initial_balance["balance"] += initial_balance["balance"]
  156. pl_initial_balance["bal_curr"] += initial_balance["amount_currency"]
  157. return pl_initial_balance
  158. def _get_initial_balance_data(
  159. self,
  160. account_ids,
  161. partner_ids,
  162. company_id,
  163. date_from,
  164. foreign_currency,
  165. only_posted_moves,
  166. unaffected_earnings_account,
  167. fy_start_date,
  168. analytic_tag_ids,
  169. cost_center_ids,
  170. ):
  171. base_domain = []
  172. if company_id:
  173. base_domain += [("company_id", "=", company_id)]
  174. if partner_ids:
  175. base_domain += [("partner_id", "in", partner_ids)]
  176. if only_posted_moves:
  177. base_domain += [("move_id.state", "=", "posted")]
  178. if analytic_tag_ids:
  179. base_domain += [("analytic_tag_ids", "in", analytic_tag_ids)]
  180. if cost_center_ids:
  181. base_domain += [("analytic_account_id", "in", cost_center_ids)]
  182. initial_domain_bs = self._get_initial_balances_bs_ml_domain(
  183. account_ids, company_id, date_from, base_domain
  184. )
  185. initial_domain_pl = self._get_initial_balances_pl_ml_domain(
  186. account_ids, company_id, date_from, fy_start_date, base_domain
  187. )
  188. gl_initial_acc = self._get_accounts_initial_balance(
  189. initial_domain_bs, initial_domain_pl
  190. )
  191. initial_domain_acc_prt = self._get_initial_balances_bs_ml_domain(
  192. account_ids, company_id, date_from, base_domain, acc_prt=True
  193. )
  194. gl_initial_acc_prt = self.env["account.move.line"].read_group(
  195. domain=initial_domain_acc_prt,
  196. fields=[
  197. "account_id",
  198. "partner_id",
  199. "debit",
  200. "credit",
  201. "balance",
  202. "amount_currency",
  203. ],
  204. groupby=["account_id", "partner_id"],
  205. lazy=False,
  206. )
  207. gen_ld_data = {}
  208. for gl in gl_initial_acc:
  209. acc_id = gl["account_id"][0]
  210. gen_ld_data[acc_id] = {}
  211. gen_ld_data[acc_id]["id"] = acc_id
  212. gen_ld_data[acc_id]["partners"] = False
  213. gen_ld_data[acc_id]["init_bal"] = {}
  214. gen_ld_data[acc_id]["init_bal"]["credit"] = gl["credit"]
  215. gen_ld_data[acc_id]["init_bal"]["debit"] = gl["debit"]
  216. gen_ld_data[acc_id]["init_bal"]["balance"] = gl["balance"]
  217. gen_ld_data[acc_id]["fin_bal"] = {}
  218. gen_ld_data[acc_id]["fin_bal"]["credit"] = gl["credit"]
  219. gen_ld_data[acc_id]["fin_bal"]["debit"] = gl["debit"]
  220. gen_ld_data[acc_id]["fin_bal"]["balance"] = gl["balance"]
  221. gen_ld_data[acc_id]["init_bal"]["bal_curr"] = gl["amount_currency"]
  222. gen_ld_data[acc_id]["fin_bal"]["bal_curr"] = gl["amount_currency"]
  223. partners_data = {}
  224. partners_ids = set()
  225. if gl_initial_acc_prt:
  226. for gl in gl_initial_acc_prt:
  227. if not gl["partner_id"]:
  228. prt_id = 0
  229. prt_name = "Missing Partner"
  230. else:
  231. prt_id = gl["partner_id"][0]
  232. prt_name = gl["partner_id"][1]
  233. prt_name = prt_name._value
  234. if prt_id not in partners_ids:
  235. partners_ids.add(prt_id)
  236. partners_data.update({prt_id: {"id": prt_id, "name": prt_name}})
  237. acc_id = gl["account_id"][0]
  238. gen_ld_data[acc_id][prt_id] = {}
  239. gen_ld_data[acc_id][prt_id]["id"] = prt_id
  240. gen_ld_data[acc_id]["partners"] = True
  241. gen_ld_data[acc_id][prt_id]["init_bal"] = {}
  242. gen_ld_data[acc_id][prt_id]["init_bal"]["credit"] = gl["credit"]
  243. gen_ld_data[acc_id][prt_id]["init_bal"]["debit"] = gl["debit"]
  244. gen_ld_data[acc_id][prt_id]["init_bal"]["balance"] = gl["balance"]
  245. gen_ld_data[acc_id][prt_id]["fin_bal"] = {}
  246. gen_ld_data[acc_id][prt_id]["fin_bal"]["credit"] = gl["credit"]
  247. gen_ld_data[acc_id][prt_id]["fin_bal"]["debit"] = gl["debit"]
  248. gen_ld_data[acc_id][prt_id]["fin_bal"]["balance"] = gl["balance"]
  249. gen_ld_data[acc_id][prt_id]["init_bal"]["bal_curr"] = gl[
  250. "amount_currency"
  251. ]
  252. gen_ld_data[acc_id][prt_id]["fin_bal"]["bal_curr"] = gl[
  253. "amount_currency"
  254. ]
  255. accounts_ids = list(gen_ld_data.keys())
  256. unaffected_id = unaffected_earnings_account
  257. if unaffected_id not in accounts_ids:
  258. accounts_ids.append(unaffected_id)
  259. self._initialize_account(gen_ld_data, unaffected_id, foreign_currency)
  260. pl_initial_balance = self._get_pl_initial_balance(
  261. account_ids, company_id, fy_start_date, foreign_currency, base_domain
  262. )
  263. gen_ld_data[unaffected_id]["init_bal"]["debit"] += pl_initial_balance["debit"]
  264. gen_ld_data[unaffected_id]["init_bal"]["credit"] += pl_initial_balance["credit"]
  265. gen_ld_data[unaffected_id]["init_bal"]["balance"] += pl_initial_balance[
  266. "balance"
  267. ]
  268. gen_ld_data[unaffected_id]["fin_bal"]["debit"] += pl_initial_balance["debit"]
  269. gen_ld_data[unaffected_id]["fin_bal"]["credit"] += pl_initial_balance["credit"]
  270. gen_ld_data[unaffected_id]["fin_bal"]["balance"] += pl_initial_balance[
  271. "balance"
  272. ]
  273. if foreign_currency:
  274. gen_ld_data[unaffected_id]["init_bal"]["bal_curr"] += pl_initial_balance[
  275. "bal_curr"
  276. ]
  277. gen_ld_data[unaffected_id]["fin_bal"]["bal_curr"] += pl_initial_balance[
  278. "bal_curr"
  279. ]
  280. return gen_ld_data, partners_data, partner_ids
  281. @api.model
  282. def _get_move_line_data(self, move_line):
  283. move_line_data = {
  284. "id": move_line["id"],
  285. "date": move_line["date"],
  286. "entry": move_line["move_id"][1],
  287. "entry_id": move_line["move_id"][0],
  288. "journal_id": move_line["journal_id"][0],
  289. "account_id": move_line["account_id"][0],
  290. "partner_id": move_line["partner_id"][0]
  291. if move_line["partner_id"]
  292. else False,
  293. "partner_name": move_line["partner_id"][1]
  294. if move_line["partner_id"]
  295. else "",
  296. "ref": "" if not move_line["ref"] else move_line["ref"],
  297. "name": "" if not move_line["name"] else move_line["name"],
  298. "tax_ids": move_line["tax_ids"],
  299. "debit": move_line["debit"],
  300. "credit": move_line["credit"],
  301. "balance": move_line["balance"],
  302. "bal_curr": move_line["amount_currency"],
  303. "rec_id": move_line["full_reconcile_id"][0]
  304. if move_line["full_reconcile_id"]
  305. else False,
  306. "rec_name": move_line["full_reconcile_id"][1]
  307. if move_line["full_reconcile_id"]
  308. else "",
  309. "tag_ids": move_line["analytic_tag_ids"],
  310. "currency_id": move_line["currency_id"],
  311. "analytic_account": move_line["analytic_account_id"][1]
  312. if move_line["analytic_account_id"]
  313. else "",
  314. "analytic_account_id": move_line["analytic_account_id"][0]
  315. if move_line["analytic_account_id"]
  316. else False,
  317. }
  318. if (
  319. move_line_data["ref"] == move_line_data["name"]
  320. or move_line_data["ref"] == ""
  321. ):
  322. ref_label = move_line_data["name"]
  323. elif move_line_data["name"] == "":
  324. ref_label = move_line_data["ref"]
  325. else:
  326. ref_label = move_line_data["ref"] + str(" - ") + move_line_data["name"]
  327. move_line_data.update({"ref_label": ref_label})
  328. return move_line_data
  329. @api.model
  330. def _get_period_domain(
  331. self,
  332. account_ids,
  333. partner_ids,
  334. company_id,
  335. only_posted_moves,
  336. date_to,
  337. date_from,
  338. analytic_tag_ids,
  339. cost_center_ids,
  340. ):
  341. domain = [
  342. ("display_type", "=", False),
  343. ("date", ">=", date_from),
  344. ("date", "<=", date_to),
  345. ]
  346. if account_ids:
  347. domain += [("account_id", "in", account_ids)]
  348. if company_id:
  349. domain += [("company_id", "=", company_id)]
  350. if partner_ids:
  351. domain += [("partner_id", "in", partner_ids)]
  352. if only_posted_moves:
  353. domain += [("move_id.state", "=", "posted")]
  354. if analytic_tag_ids:
  355. domain += [("analytic_tag_ids", "in", analytic_tag_ids)]
  356. if cost_center_ids:
  357. domain += [("analytic_account_id", "in", cost_center_ids)]
  358. return domain
  359. @api.model
  360. def _initialize_partner(self, gen_ld_data, acc_id, prt_id, foreign_currency):
  361. gen_ld_data[acc_id]["partners"] = True
  362. gen_ld_data[acc_id][prt_id] = {}
  363. gen_ld_data[acc_id][prt_id]["id"] = prt_id
  364. gen_ld_data[acc_id][prt_id]["init_bal"] = {}
  365. gen_ld_data[acc_id][prt_id]["init_bal"]["balance"] = 0.0
  366. gen_ld_data[acc_id][prt_id]["init_bal"]["credit"] = 0.0
  367. gen_ld_data[acc_id][prt_id]["init_bal"]["debit"] = 0.0
  368. gen_ld_data[acc_id][prt_id]["fin_bal"] = {}
  369. gen_ld_data[acc_id][prt_id]["fin_bal"]["credit"] = 0.0
  370. gen_ld_data[acc_id][prt_id]["fin_bal"]["debit"] = 0.0
  371. gen_ld_data[acc_id][prt_id]["fin_bal"]["balance"] = 0.0
  372. if foreign_currency:
  373. gen_ld_data[acc_id][prt_id]["init_bal"]["bal_curr"] = 0.0
  374. gen_ld_data[acc_id][prt_id]["fin_bal"]["bal_curr"] = 0.0
  375. return gen_ld_data
  376. def _initialize_account(self, gen_ld_data, acc_id, foreign_currency):
  377. gen_ld_data[acc_id] = {}
  378. gen_ld_data[acc_id]["id"] = acc_id
  379. gen_ld_data[acc_id]["partners"] = False
  380. gen_ld_data[acc_id]["init_bal"] = {}
  381. gen_ld_data[acc_id]["init_bal"]["balance"] = 0.0
  382. gen_ld_data[acc_id]["init_bal"]["credit"] = 0.0
  383. gen_ld_data[acc_id]["init_bal"]["debit"] = 0.0
  384. gen_ld_data[acc_id]["fin_bal"] = {}
  385. gen_ld_data[acc_id]["fin_bal"]["credit"] = 0.0
  386. gen_ld_data[acc_id]["fin_bal"]["debit"] = 0.0
  387. gen_ld_data[acc_id]["fin_bal"]["balance"] = 0.0
  388. if foreign_currency:
  389. gen_ld_data[acc_id]["init_bal"]["bal_curr"] = 0.0
  390. gen_ld_data[acc_id]["fin_bal"]["bal_curr"] = 0.0
  391. return gen_ld_data
  392. def _get_reconciled_after_date_to_ids(self, full_reconcile_ids, date_to):
  393. full_reconcile_ids = list(full_reconcile_ids)
  394. domain = [
  395. ("max_date", ">", date_to),
  396. ("full_reconcile_id", "in", full_reconcile_ids),
  397. ]
  398. fields = ["full_reconcile_id"]
  399. reconciled_after_date_to = self.env["account.partial.reconcile"].search_read(
  400. domain=domain, fields=fields
  401. )
  402. rec_after_date_to_ids = list(
  403. map(operator.itemgetter("full_reconcile_id"), reconciled_after_date_to)
  404. )
  405. rec_after_date_to_ids = [i[0] for i in rec_after_date_to_ids]
  406. return rec_after_date_to_ids
  407. def _get_period_ml_data(
  408. self,
  409. account_ids,
  410. partner_ids,
  411. company_id,
  412. foreign_currency,
  413. only_posted_moves,
  414. date_from,
  415. date_to,
  416. partners_data,
  417. gen_ld_data,
  418. partners_ids,
  419. analytic_tag_ids,
  420. cost_center_ids,
  421. ):
  422. domain = self._get_period_domain(
  423. account_ids,
  424. partner_ids,
  425. company_id,
  426. only_posted_moves,
  427. date_to,
  428. date_from,
  429. analytic_tag_ids,
  430. cost_center_ids,
  431. )
  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.user.company_id.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. gen_ld_data, partners_data, partners_ids = self._get_initial_balance_data(
  750. account_ids,
  751. partner_ids,
  752. company_id,
  753. date_from,
  754. foreign_currency,
  755. only_posted_moves,
  756. unaffected_earnings_account,
  757. fy_start_date,
  758. analytic_tag_ids,
  759. cost_center_ids,
  760. )
  761. centralize = data["centralize"]
  762. (
  763. gen_ld_data,
  764. accounts_data,
  765. partners_data,
  766. journals_data,
  767. full_reconcile_data,
  768. taxes_data,
  769. tags_data,
  770. rec_after_date_to_ids,
  771. ) = self._get_period_ml_data(
  772. account_ids,
  773. partner_ids,
  774. company_id,
  775. foreign_currency,
  776. only_posted_moves,
  777. date_from,
  778. date_to,
  779. partners_data,
  780. gen_ld_data,
  781. partners_ids,
  782. analytic_tag_ids,
  783. cost_center_ids,
  784. )
  785. general_ledger = self._create_general_ledger(
  786. gen_ld_data,
  787. accounts_data,
  788. show_partner_details,
  789. rec_after_date_to_ids,
  790. hide_account_at_0,
  791. )
  792. if centralize:
  793. for account in general_ledger:
  794. if account["centralized"]:
  795. centralized_ml = self._get_centralized_ml(account, date_to)
  796. account["move_lines"] = centralized_ml
  797. account["move_lines"] = self._recalculate_cumul_balance(
  798. account["move_lines"],
  799. gen_ld_data[account["id"]]["init_bal"]["balance"],
  800. rec_after_date_to_ids,
  801. )
  802. if account["partners"]:
  803. account["partners"] = False
  804. del account["list_partner"]
  805. general_ledger = sorted(general_ledger, key=lambda k: k["code"])
  806. return {
  807. "doc_ids": [wizard_id],
  808. "doc_model": "general.ledger.report.wizard",
  809. "docs": self.env["general.ledger.report.wizard"].browse(wizard_id),
  810. "foreign_currency": data["foreign_currency"],
  811. "company_name": company.display_name,
  812. "company_currency": company.currency_id,
  813. "currency_name": company.currency_id.name,
  814. "date_from": data["date_from"],
  815. "date_to": data["date_to"],
  816. "only_posted_moves": data["only_posted_moves"],
  817. "hide_account_at_0": data["hide_account_at_0"],
  818. "show_analytic_tags": data["show_analytic_tags"],
  819. "show_cost_center": data["show_cost_center"],
  820. "general_ledger": general_ledger,
  821. "accounts_data": accounts_data,
  822. "partners_data": partners_data,
  823. "journals_data": journals_data,
  824. "full_reconcile_data": full_reconcile_data,
  825. "taxes_data": taxes_data,
  826. "centralize": centralize,
  827. "tags_data": tags_data,
  828. "filter_partner_ids": filter_partner_ids,
  829. }