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.

794 lines
32 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. class GeneralLedgerReport(models.AbstractModel):
  9. _name = "report.account_financial_report.general_ledger"
  10. _description = "General Ledger Report"
  11. def _get_accounts_data(self, account_ids):
  12. accounts = self.env["account.account"].browse(account_ids)
  13. accounts_data = {}
  14. for account in accounts:
  15. accounts_data.update(
  16. {
  17. account.id: {
  18. "id": account.id,
  19. "code": account.code,
  20. "name": account.name,
  21. "group_id": account.group_id.id,
  22. "currency_id": account.currency_id or False,
  23. "currency_name": account.currency_id.name,
  24. "centralized": account.centralized,
  25. }
  26. }
  27. )
  28. return accounts_data
  29. def _get_journals_data(self, journals_ids):
  30. journals = self.env["account.journal"].browse(journals_ids)
  31. journals_data = {}
  32. for journal in journals:
  33. journals_data.update({journal.id: {"id": journal.id, "code": journal.code}})
  34. return journals_data
  35. def _get_tags_data(self, tags_ids):
  36. tags = self.env["account.analytic.tag"].browse(tags_ids)
  37. tags_data = {}
  38. for tag in tags:
  39. tags_data.update({tag.id: {"name": tag.name}})
  40. return tags_data
  41. def _get_taxes_data(self, taxes_ids):
  42. taxes = self.env["account.tax"].browse(taxes_ids)
  43. taxes_data = {}
  44. for tax in taxes:
  45. taxes_data.update(
  46. {
  47. tax.id: {
  48. "id": tax.id,
  49. "amount": tax.amount,
  50. "amount_type": tax.amount_type,
  51. "display_name": tax.display_name,
  52. }
  53. }
  54. )
  55. if tax.amount_type == "percent" or tax.amount_type == "division":
  56. taxes_data[tax.id]["string"] = "%"
  57. else:
  58. taxes_data[tax.id]["string"] = ""
  59. taxes_data[tax.id]["tax_name"] = (
  60. tax.display_name
  61. + " ("
  62. + str(tax.amount)
  63. + taxes_data[tax.id]["string"]
  64. + ")"
  65. )
  66. return taxes_data
  67. def _get_acc_prt_accounts_ids(self, company_id):
  68. accounts_domain = [
  69. ("company_id", "=", company_id),
  70. ("internal_type", "in", ["receivable", "payable"]),
  71. ]
  72. acc_prt_accounts = self.env["account.account"].search(accounts_domain)
  73. return acc_prt_accounts.ids
  74. def _get_initial_balances_bs_ml_domain(
  75. self, account_ids, company_id, date_from, base_domain, acc_prt=False
  76. ):
  77. accounts_domain = [
  78. ("company_id", "=", company_id),
  79. ("user_type_id.include_initial_balance", "=", True),
  80. ]
  81. if account_ids:
  82. accounts_domain += [("id", "in", account_ids)]
  83. domain = []
  84. domain += base_domain
  85. domain += [("date", "<", date_from)]
  86. accounts = self.env["account.account"].search(accounts_domain)
  87. domain += [("account_id", "in", accounts.ids)]
  88. if acc_prt:
  89. domain += [("account_id.internal_type", "in", ["receivable", "payable"])]
  90. return domain
  91. def _get_initial_balances_pl_ml_domain(
  92. self, account_ids, company_id, date_from, fy_start_date, base_domain
  93. ):
  94. accounts_domain = [
  95. ("company_id", "=", company_id),
  96. ("user_type_id.include_initial_balance", "=", False),
  97. ]
  98. if account_ids:
  99. accounts_domain += [("id", "in", account_ids)]
  100. domain = []
  101. domain += base_domain
  102. domain += [("date", "<", date_from), ("date", ">=", fy_start_date)]
  103. accounts = self.env["account.account"].search(accounts_domain)
  104. domain += [("account_id", "in", accounts.ids)]
  105. return domain
  106. def _get_accounts_initial_balance(self, initial_domain_bs, initial_domain_pl):
  107. gl_initial_acc_bs = self.env["account.move.line"].read_group(
  108. domain=initial_domain_bs,
  109. fields=["account_id", "debit", "credit", "balance", "amount_currency"],
  110. groupby=["account_id"],
  111. )
  112. gl_initial_acc_pl = self.env["account.move.line"].read_group(
  113. domain=initial_domain_pl,
  114. fields=["account_id", "debit", "credit", "balance", "amount_currency"],
  115. groupby=["account_id"],
  116. )
  117. gl_initial_acc = gl_initial_acc_bs + gl_initial_acc_pl
  118. return gl_initial_acc
  119. def _get_initial_balance_fy_pl_ml_domain(
  120. self, account_ids, company_id, fy_start_date, base_domain
  121. ):
  122. accounts_domain = [
  123. ("company_id", "=", company_id),
  124. ("user_type_id.include_initial_balance", "=", False),
  125. ]
  126. if account_ids:
  127. accounts_domain += [("id", "in", account_ids)]
  128. domain = []
  129. domain += base_domain
  130. domain += [("date", "<", fy_start_date)]
  131. accounts = self.env["account.account"].search(accounts_domain)
  132. domain += [("account_id", "in", accounts.ids)]
  133. return domain
  134. def _get_pl_initial_balance(
  135. self, account_ids, company_id, fy_start_date, foreign_currency, base_domain
  136. ):
  137. domain = self._get_initial_balance_fy_pl_ml_domain(
  138. account_ids, company_id, fy_start_date, base_domain
  139. )
  140. initial_balances = self.env["account.move.line"].read_group(
  141. domain=domain,
  142. fields=["account_id", "debit", "credit", "balance", "amount_currency"],
  143. groupby=["account_id"],
  144. )
  145. pl_initial_balance = {
  146. "debit": 0.0,
  147. "credit": 0.0,
  148. "balance": 0.0,
  149. "bal_curr": 0.0,
  150. }
  151. for initial_balance in initial_balances:
  152. pl_initial_balance["debit"] += initial_balance["debit"]
  153. pl_initial_balance["credit"] += initial_balance["credit"]
  154. pl_initial_balance["balance"] += initial_balance["balance"]
  155. pl_initial_balance["bal_curr"] += initial_balance["amount_currency"]
  156. return pl_initial_balance
  157. def _get_initial_balance_data(
  158. self,
  159. account_ids,
  160. partner_ids,
  161. company_id,
  162. date_from,
  163. foreign_currency,
  164. only_posted_moves,
  165. hide_account_at_0,
  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. }
  312. if (
  313. move_line_data["ref"] == move_line_data["name"]
  314. or move_line_data["ref"] == ""
  315. ):
  316. ref_label = move_line_data["name"]
  317. elif move_line_data["name"] == "":
  318. ref_label = move_line_data["ref"]
  319. else:
  320. ref_label = move_line_data["ref"] + str(" - ") + move_line_data["name"]
  321. move_line_data.update({"ref_label": ref_label})
  322. return move_line_data
  323. @api.model
  324. def _get_period_domain(
  325. self,
  326. account_ids,
  327. partner_ids,
  328. company_id,
  329. only_posted_moves,
  330. date_to,
  331. date_from,
  332. analytic_tag_ids,
  333. cost_center_ids,
  334. ):
  335. domain = [
  336. ("display_type", "=", False),
  337. ("date", ">=", date_from),
  338. ("date", "<=", date_to),
  339. ]
  340. if account_ids:
  341. domain += [("account_id", "in", account_ids)]
  342. if company_id:
  343. domain += [("company_id", "=", company_id)]
  344. if partner_ids:
  345. domain += [("partner_id", "in", partner_ids)]
  346. if only_posted_moves:
  347. domain += [("move_id.state", "=", "posted")]
  348. if analytic_tag_ids:
  349. domain += [("analytic_tag_ids", "in", analytic_tag_ids)]
  350. if cost_center_ids:
  351. domain += [("analytic_account_id", "in", cost_center_ids)]
  352. return domain
  353. @api.model
  354. def _initialize_partner(self, gen_ld_data, acc_id, prt_id, foreign_currency):
  355. gen_ld_data[acc_id]["partners"] = True
  356. gen_ld_data[acc_id][prt_id] = {}
  357. gen_ld_data[acc_id][prt_id]["id"] = prt_id
  358. gen_ld_data[acc_id][prt_id]["init_bal"] = {}
  359. gen_ld_data[acc_id][prt_id]["init_bal"]["balance"] = 0.0
  360. gen_ld_data[acc_id][prt_id]["init_bal"]["credit"] = 0.0
  361. gen_ld_data[acc_id][prt_id]["init_bal"]["debit"] = 0.0
  362. gen_ld_data[acc_id][prt_id]["fin_bal"] = {}
  363. gen_ld_data[acc_id][prt_id]["fin_bal"]["credit"] = 0.0
  364. gen_ld_data[acc_id][prt_id]["fin_bal"]["debit"] = 0.0
  365. gen_ld_data[acc_id][prt_id]["fin_bal"]["balance"] = 0.0
  366. if foreign_currency:
  367. gen_ld_data[acc_id][prt_id]["init_bal"]["bal_curr"] = 0.0
  368. gen_ld_data[acc_id][prt_id]["fin_bal"]["bal_curr"] = 0.0
  369. return gen_ld_data
  370. def _initialize_account(self, gen_ld_data, acc_id, foreign_currency):
  371. gen_ld_data[acc_id] = {}
  372. gen_ld_data[acc_id]["id"] = acc_id
  373. gen_ld_data[acc_id]["partners"] = False
  374. gen_ld_data[acc_id]["init_bal"] = {}
  375. gen_ld_data[acc_id]["init_bal"]["balance"] = 0.0
  376. gen_ld_data[acc_id]["init_bal"]["credit"] = 0.0
  377. gen_ld_data[acc_id]["init_bal"]["debit"] = 0.0
  378. gen_ld_data[acc_id]["fin_bal"] = {}
  379. gen_ld_data[acc_id]["fin_bal"]["credit"] = 0.0
  380. gen_ld_data[acc_id]["fin_bal"]["debit"] = 0.0
  381. gen_ld_data[acc_id]["fin_bal"]["balance"] = 0.0
  382. if foreign_currency:
  383. gen_ld_data[acc_id]["init_bal"]["bal_curr"] = 0.0
  384. gen_ld_data[acc_id]["fin_bal"]["bal_curr"] = 0.0
  385. return gen_ld_data
  386. def _get_reconciled_after_date_to_ids(self, full_reconcile_ids, date_to):
  387. full_reconcile_ids = list(full_reconcile_ids)
  388. domain = [
  389. ("max_date", ">", date_to),
  390. ("full_reconcile_id", "in", full_reconcile_ids),
  391. ]
  392. fields = ["full_reconcile_id"]
  393. reconciled_after_date_to = self.env["account.partial.reconcile"].search_read(
  394. domain=domain, fields=fields
  395. )
  396. rec_after_date_to_ids = list(
  397. map(operator.itemgetter("full_reconcile_id"), reconciled_after_date_to)
  398. )
  399. rec_after_date_to_ids = [i[0] for i in rec_after_date_to_ids]
  400. return rec_after_date_to_ids
  401. def _get_period_ml_data(
  402. self,
  403. account_ids,
  404. partner_ids,
  405. company_id,
  406. foreign_currency,
  407. only_posted_moves,
  408. hide_account_at_0,
  409. date_from,
  410. date_to,
  411. partners_data,
  412. gen_ld_data,
  413. partners_ids,
  414. centralize,
  415. analytic_tag_ids,
  416. cost_center_ids,
  417. ):
  418. domain = self._get_period_domain(
  419. account_ids,
  420. partner_ids,
  421. company_id,
  422. only_posted_moves,
  423. date_to,
  424. date_from,
  425. analytic_tag_ids,
  426. cost_center_ids,
  427. )
  428. ml_fields = [
  429. "id",
  430. "name",
  431. "date",
  432. "move_id",
  433. "journal_id",
  434. "account_id",
  435. "partner_id",
  436. "debit",
  437. "credit",
  438. "balance",
  439. "currency_id",
  440. "full_reconcile_id",
  441. "tax_ids",
  442. "analytic_tag_ids",
  443. "amount_currency",
  444. "ref",
  445. "name",
  446. ]
  447. move_lines = self.env["account.move.line"].search_read(
  448. domain=domain, fields=ml_fields
  449. )
  450. journal_ids = set()
  451. full_reconcile_ids = set()
  452. taxes_ids = set()
  453. tags_ids = set()
  454. full_reconcile_data = {}
  455. acc_prt_account_ids = self._get_acc_prt_accounts_ids(company_id)
  456. for move_line in move_lines:
  457. journal_ids.add(move_line["journal_id"][0])
  458. for tax_id in move_line["tax_ids"]:
  459. taxes_ids.add(tax_id)
  460. for analytic_tag_id in move_line["analytic_tag_ids"]:
  461. tags_ids.add(analytic_tag_id)
  462. if move_line["full_reconcile_id"]:
  463. rec_id = move_line["full_reconcile_id"][0]
  464. if rec_id not in full_reconcile_ids:
  465. full_reconcile_data.update(
  466. {
  467. rec_id: {
  468. "id": rec_id,
  469. "name": move_line["full_reconcile_id"][1],
  470. }
  471. }
  472. )
  473. full_reconcile_ids.add(rec_id)
  474. acc_id = move_line["account_id"][0]
  475. ml_id = move_line["id"]
  476. if move_line["partner_id"]:
  477. prt_id = move_line["partner_id"][0]
  478. partner_name = move_line["partner_id"][1]
  479. if acc_id not in gen_ld_data.keys():
  480. gen_ld_data = self._initialize_account(
  481. gen_ld_data, acc_id, foreign_currency
  482. )
  483. if acc_id in acc_prt_account_ids:
  484. if not move_line["partner_id"]:
  485. prt_id = 0
  486. partner_name = "Missing Partner"
  487. partners_ids.append(prt_id)
  488. partners_data.update({prt_id: {"id": prt_id, "name": partner_name}})
  489. if prt_id not in gen_ld_data[acc_id]:
  490. gen_ld_data = self._initialize_partner(
  491. gen_ld_data, acc_id, prt_id, foreign_currency
  492. )
  493. gen_ld_data[acc_id][prt_id][ml_id] = self._get_move_line_data(move_line)
  494. gen_ld_data[acc_id][prt_id]["fin_bal"]["credit"] += move_line["credit"]
  495. gen_ld_data[acc_id][prt_id]["fin_bal"]["debit"] += move_line["debit"]
  496. gen_ld_data[acc_id][prt_id]["fin_bal"]["balance"] += move_line[
  497. "balance"
  498. ]
  499. if foreign_currency:
  500. gen_ld_data[acc_id][prt_id]["fin_bal"]["bal_curr"] += move_line[
  501. "amount_currency"
  502. ]
  503. else:
  504. gen_ld_data[acc_id][ml_id] = self._get_move_line_data(move_line)
  505. gen_ld_data[acc_id]["fin_bal"]["credit"] += move_line["credit"]
  506. gen_ld_data[acc_id]["fin_bal"]["debit"] += move_line["debit"]
  507. gen_ld_data[acc_id]["fin_bal"]["balance"] += move_line["balance"]
  508. if foreign_currency:
  509. gen_ld_data[acc_id]["fin_bal"]["bal_curr"] += move_line[
  510. "amount_currency"
  511. ]
  512. journals_data = self._get_journals_data(list(journal_ids))
  513. accounts_data = self._get_accounts_data(gen_ld_data.keys())
  514. taxes_data = self._get_taxes_data(list(taxes_ids))
  515. tags_data = self._get_tags_data(list(tags_ids))
  516. rec_after_date_to_ids = self._get_reconciled_after_date_to_ids(
  517. full_reconcile_data.keys(), date_to
  518. )
  519. return (
  520. gen_ld_data,
  521. accounts_data,
  522. partners_data,
  523. journals_data,
  524. full_reconcile_data,
  525. taxes_data,
  526. tags_data,
  527. rec_after_date_to_ids,
  528. )
  529. @api.model
  530. def _recalculate_cumul_balance(
  531. self, move_lines, last_cumul_balance, rec_after_date_to_ids
  532. ):
  533. for move_line in move_lines:
  534. move_line["balance"] += last_cumul_balance
  535. last_cumul_balance = move_line["balance"]
  536. if move_line["rec_id"] in rec_after_date_to_ids:
  537. move_line["rec_name"] = "(" + _("future") + ") " + move_line["rec_name"]
  538. return move_lines
  539. @api.model
  540. def _create_general_ledger(
  541. self, gen_led_data, accounts_data, show_partner_details, rec_after_date_to_ids
  542. ):
  543. general_ledger = []
  544. for acc_id in gen_led_data.keys():
  545. account = {}
  546. account.update(
  547. {
  548. "code": accounts_data[acc_id]["code"],
  549. "name": accounts_data[acc_id]["name"],
  550. "type": "account",
  551. "currency_id": accounts_data[acc_id]["currency_id"],
  552. "centralized": accounts_data[acc_id]["centralized"],
  553. }
  554. )
  555. if not gen_led_data[acc_id]["partners"]:
  556. move_lines = []
  557. for ml_id in gen_led_data[acc_id].keys():
  558. if not isinstance(ml_id, int):
  559. account.update({ml_id: gen_led_data[acc_id][ml_id]})
  560. else:
  561. move_lines += [gen_led_data[acc_id][ml_id]]
  562. move_lines = sorted(move_lines, key=lambda k: (k["date"]))
  563. move_lines = self._recalculate_cumul_balance(
  564. move_lines,
  565. gen_led_data[acc_id]["init_bal"]["balance"],
  566. rec_after_date_to_ids,
  567. )
  568. account.update({"move_lines": move_lines})
  569. else:
  570. if show_partner_details:
  571. list_partner = []
  572. for prt_id in gen_led_data[acc_id].keys():
  573. partner = {}
  574. move_lines = []
  575. if not isinstance(prt_id, int):
  576. account.update({prt_id: gen_led_data[acc_id][prt_id]})
  577. else:
  578. for ml_id in gen_led_data[acc_id][prt_id].keys():
  579. if not isinstance(ml_id, int):
  580. partner.update(
  581. {ml_id: gen_led_data[acc_id][prt_id][ml_id]}
  582. )
  583. else:
  584. move_lines += [gen_led_data[acc_id][prt_id][ml_id]]
  585. move_lines = sorted(move_lines, key=lambda k: (k["date"]))
  586. move_lines = self._recalculate_cumul_balance(
  587. move_lines,
  588. gen_led_data[acc_id][prt_id]["init_bal"]["balance"],
  589. rec_after_date_to_ids,
  590. )
  591. partner.update({"move_lines": move_lines})
  592. list_partner += [partner]
  593. account.update({"list_partner": list_partner})
  594. else:
  595. move_lines = []
  596. for prt_id in gen_led_data[acc_id].keys():
  597. if not isinstance(prt_id, int):
  598. account.update({prt_id: gen_led_data[acc_id][prt_id]})
  599. else:
  600. for ml_id in gen_led_data[acc_id][prt_id].keys():
  601. if isinstance(ml_id, int):
  602. move_lines += [gen_led_data[acc_id][prt_id][ml_id]]
  603. move_lines = sorted(move_lines, key=lambda k: (k["date"]))
  604. move_lines = self._recalculate_cumul_balance(
  605. move_lines,
  606. gen_led_data[acc_id]["init_bal"]["balance"],
  607. rec_after_date_to_ids,
  608. )
  609. account.update({"move_lines": move_lines, "partners": False})
  610. general_ledger += [account]
  611. return general_ledger
  612. @api.model
  613. def _calculate_centralization(self, centralized_ml, move_line, date_to):
  614. jnl_id = move_line["journal_id"]
  615. month = move_line["date"].month
  616. if jnl_id not in centralized_ml.keys():
  617. centralized_ml[jnl_id] = {}
  618. if month not in centralized_ml[jnl_id].keys():
  619. centralized_ml[jnl_id][month] = {}
  620. last_day_month = calendar.monthrange(move_line["date"].year, month)
  621. date = datetime.date(move_line["date"].year, month, last_day_month[1])
  622. if date > date_to:
  623. date = date_to
  624. centralized_ml[jnl_id][month].update(
  625. {
  626. "journal_id": jnl_id,
  627. "ref_label": "Centralized entries",
  628. "date": date,
  629. "debit": 0.0,
  630. "credit": 0.0,
  631. "balance": 0.0,
  632. "bal_curr": 0.0,
  633. "partner_id": False,
  634. "rec_id": 0,
  635. "entry_id": False,
  636. "tax_ids": [],
  637. "full_reconcile_id": False,
  638. "id": False,
  639. "tag_ids": False,
  640. "currency_id": False,
  641. }
  642. )
  643. centralized_ml[jnl_id][month]["debit"] += move_line["debit"]
  644. centralized_ml[jnl_id][month]["credit"] += move_line["credit"]
  645. centralized_ml[jnl_id][month]["balance"] += (
  646. move_line["debit"] - move_line["credit"]
  647. )
  648. centralized_ml[jnl_id][month]["bal_curr"] += move_line["bal_curr"]
  649. return centralized_ml
  650. @api.model
  651. def _get_centralized_ml(self, account, date_to):
  652. centralized_ml = {}
  653. if isinstance(date_to, str):
  654. date_to = datetime.datetime.strptime(date_to, "%Y-%m-%d").date()
  655. if account["partners"]:
  656. for partner in account["list_partner"]:
  657. for move_line in partner["move_lines"]:
  658. centralized_ml = self._calculate_centralization(
  659. centralized_ml, move_line, date_to,
  660. )
  661. else:
  662. for move_line in account["move_lines"]:
  663. centralized_ml = self._calculate_centralization(
  664. centralized_ml, move_line, date_to,
  665. )
  666. list_centralized_ml = []
  667. for jnl_id in centralized_ml.keys():
  668. list_centralized_ml += list(centralized_ml[jnl_id].values())
  669. return list_centralized_ml
  670. def _get_report_values(self, docids, data):
  671. wizard_id = data["wizard_id"]
  672. company = self.env["res.company"].browse(data["company_id"])
  673. company_id = data["company_id"]
  674. date_to = data["date_to"]
  675. date_from = data["date_from"]
  676. partner_ids = data["partner_ids"]
  677. if not partner_ids:
  678. filter_partner_ids = False
  679. else:
  680. filter_partner_ids = True
  681. account_ids = data["account_ids"]
  682. analytic_tag_ids = data["analytic_tag_ids"]
  683. cost_center_ids = data["cost_center_ids"]
  684. show_partner_details = data["show_partner_details"]
  685. hide_account_at_0 = data["hide_account_at_0"]
  686. foreign_currency = data["foreign_currency"]
  687. only_posted_moves = data["only_posted_moves"]
  688. unaffected_earnings_account = data["unaffected_earnings_account"]
  689. fy_start_date = data["fy_start_date"]
  690. gen_ld_data, partners_data, partners_ids = self._get_initial_balance_data(
  691. account_ids,
  692. partner_ids,
  693. company_id,
  694. date_from,
  695. foreign_currency,
  696. only_posted_moves,
  697. hide_account_at_0,
  698. unaffected_earnings_account,
  699. fy_start_date,
  700. analytic_tag_ids,
  701. cost_center_ids,
  702. )
  703. centralize = data["centralize"]
  704. (
  705. gen_ld_data,
  706. accounts_data,
  707. partners_data,
  708. journals_data,
  709. full_reconcile_data,
  710. taxes_data,
  711. tags_data,
  712. rec_after_date_to_ids,
  713. ) = self._get_period_ml_data(
  714. account_ids,
  715. partner_ids,
  716. company_id,
  717. foreign_currency,
  718. only_posted_moves,
  719. hide_account_at_0,
  720. date_from,
  721. date_to,
  722. partners_data,
  723. gen_ld_data,
  724. partners_ids,
  725. centralize,
  726. analytic_tag_ids,
  727. cost_center_ids,
  728. )
  729. general_ledger = self._create_general_ledger(
  730. gen_ld_data, accounts_data, show_partner_details, rec_after_date_to_ids
  731. )
  732. if centralize:
  733. for account in general_ledger:
  734. if account["centralized"]:
  735. centralized_ml = self._get_centralized_ml(account, date_to)
  736. account["move_lines"] = centralized_ml
  737. account["move_lines"] = self._recalculate_cumul_balance(
  738. account["move_lines"],
  739. gen_ld_data[account["id"]]["init_bal"]["balance"],
  740. rec_after_date_to_ids,
  741. )
  742. if account["partners"]:
  743. account["partners"] = False
  744. del account["list_partner"]
  745. general_ledger = sorted(general_ledger, key=lambda k: k["code"])
  746. return {
  747. "doc_ids": [wizard_id],
  748. "doc_model": "general.ledger.report.wizard",
  749. "docs": self.env["general.ledger.report.wizard"].browse(wizard_id),
  750. "foreign_currency": data["foreign_currency"],
  751. "company_name": company.display_name,
  752. "company_currency": company.currency_id,
  753. "currency_name": company.currency_id.name,
  754. "date_from": data["date_from"],
  755. "date_to": data["date_to"],
  756. "only_posted_moves": data["only_posted_moves"],
  757. "hide_account_at_0": data["hide_account_at_0"],
  758. "show_analytic_tags": data["show_analytic_tags"],
  759. "general_ledger": general_ledger,
  760. "accounts_data": accounts_data,
  761. "partners_data": partners_data,
  762. "journals_data": journals_data,
  763. "full_reconcile_data": full_reconcile_data,
  764. "taxes_data": taxes_data,
  765. "centralize": centralize,
  766. "tags_data": tags_data,
  767. "filter_partner_ids": filter_partner_ids,
  768. }