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.

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