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.

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