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.

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