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.

610 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. from operator import itemgetter
  6. from natsort import natsorted
  7. import calendar
  8. import datetime
  9. class GeneralLedgerReport(models.AbstractModel):
  10. _name = 'report.account_financial_report.general_ledger'
  11. _description = "General Ledger Report"
  12. def _get_accounts_data(self, account_ids):
  13. accounts = self.env['account.account'].browse(account_ids)
  14. accounts_data = {}
  15. for account in accounts:
  16. accounts_data.update({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. 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. if prt_id not in partners_ids:
  205. partners_ids.add(prt_id)
  206. partners_data.update({
  207. prt_id: {'id': prt_id, 'name': prt_name}
  208. })
  209. acc_id = gl['account_id'][0]
  210. gen_ld_data[acc_id][prt_id] = {}
  211. gen_ld_data[acc_id][prt_id]['id'] = prt_id
  212. gen_ld_data[acc_id]['partners'] = True
  213. gen_ld_data[acc_id][prt_id]['init_bal'] = {}
  214. gen_ld_data[acc_id][prt_id][
  215. 'init_bal']['credit'] = gl['credit']
  216. gen_ld_data[acc_id][prt_id][
  217. 'init_bal']['debit'] = gl['debit']
  218. gen_ld_data[acc_id][prt_id][
  219. 'init_bal']['balance'] = gl['balance']
  220. gen_ld_data[acc_id][prt_id]['fin_bal'] = {}
  221. gen_ld_data[acc_id][prt_id][
  222. 'fin_bal']['credit'] = gl['credit']
  223. gen_ld_data[acc_id][prt_id][
  224. 'fin_bal']['debit'] = gl['debit']
  225. gen_ld_data[acc_id][prt_id][
  226. 'fin_bal']['balance'] = gl['balance']
  227. gen_ld_data[acc_id][prt_id]['init_bal'][
  228. 'bal_curr'] = gl['amount_currency']
  229. gen_ld_data[acc_id][prt_id]['fin_bal'][
  230. 'bal_curr'] = gl['amount_currency']
  231. accounts_ids = list(gen_ld_data.keys())
  232. unaffected_id = unaffected_earnings_account
  233. if unaffected_id not in accounts_ids:
  234. accounts_ids.append(unaffected_id)
  235. self._initialize_account(
  236. gen_ld_data, unaffected_id, foreign_currency
  237. )
  238. pl_initial_balance = self._get_pl_initial_balance(
  239. account_ids, company_id, fy_start_date,
  240. foreign_currency, base_domain
  241. )
  242. gen_ld_data[unaffected_id]['init_bal']['debit'] += \
  243. pl_initial_balance['debit']
  244. gen_ld_data[unaffected_id]['init_bal']['credit'] += \
  245. pl_initial_balance['credit']
  246. gen_ld_data[unaffected_id]['init_bal']['balance'] += \
  247. pl_initial_balance['balance']
  248. gen_ld_data[unaffected_id]['fin_bal']['debit'] += \
  249. pl_initial_balance['debit']
  250. gen_ld_data[unaffected_id]['fin_bal']['credit'] += \
  251. pl_initial_balance['credit']
  252. gen_ld_data[unaffected_id]['fin_bal']['balance'] += \
  253. pl_initial_balance['balance']
  254. if foreign_currency:
  255. gen_ld_data[unaffected_id]['init_bal']['bal_curr'] += \
  256. pl_initial_balance['bal_curr']
  257. gen_ld_data[unaffected_id]['fin_bal']['bal_curr'] += \
  258. pl_initial_balance['bal_curr']
  259. return gen_ld_data, partners_data, partner_ids
  260. @api.model
  261. def _get_move_line_data(self, move_line):
  262. move_line_data = {
  263. 'id': move_line['id'],
  264. 'date': move_line['date'],
  265. 'entry': move_line['move_id'][1],
  266. 'entry_id': move_line['move_id'][0],
  267. 'journal_id': move_line['journal_id'][0],
  268. 'account_id': move_line['account_id'][0],
  269. 'partner_id': move_line['partner_id'][0] if
  270. move_line['partner_id'] else False,
  271. 'partner_name': move_line['partner_id'][1] if
  272. move_line['partner_id'] else "",
  273. 'ref': move_line['name'],
  274. 'tax_ids': move_line['tax_ids'],
  275. 'debit': move_line['debit'],
  276. 'credit': move_line['credit'],
  277. 'balance': move_line['balance'],
  278. 'bal_curr': move_line['amount_currency'],
  279. 'rec_id': move_line['full_reconcile_id'][0] if
  280. move_line['full_reconcile_id'] else False,
  281. 'rec_name': move_line['full_reconcile_id'][1] if
  282. move_line['full_reconcile_id'] else "",
  283. 'tag_ids': move_line['analytic_tag_ids'],
  284. 'currency_id': move_line['currency_id'],
  285. }
  286. return move_line_data
  287. @api.model
  288. def _get_period_domain(
  289. self, account_ids, partner_ids, company_id, only_posted_moves,
  290. date_to, date_from, analytic_tag_ids, cost_center_ids):
  291. domain = [('date', '>=', date_from), ('date', '<=', date_to)]
  292. if account_ids:
  293. domain += [('account_id', 'in', account_ids)]
  294. if company_id:
  295. domain += [('company_id', '=', company_id)]
  296. if partner_ids:
  297. domain += [('partner_id', 'in', partner_ids)]
  298. if only_posted_moves:
  299. domain += [('move_id.state', '=', 'posted')]
  300. if analytic_tag_ids:
  301. domain += [('analytic_tag_ids', 'in', analytic_tag_ids)]
  302. if cost_center_ids:
  303. domain += [('analytic_account_id', 'in', cost_center_ids)]
  304. return domain
  305. @api.model
  306. def _initialize_partner(self, gen_ld_data, acc_id, prt_id,
  307. foreign_currency):
  308. gen_ld_data[acc_id]['partners'] = True
  309. gen_ld_data[acc_id][prt_id] = {}
  310. gen_ld_data[acc_id][prt_id]['id'] = prt_id
  311. gen_ld_data[acc_id][prt_id]['init_bal'] = {}
  312. gen_ld_data[acc_id][prt_id]['init_bal']['balance'] = 0.0
  313. gen_ld_data[acc_id][prt_id]['init_bal']['credit'] = 0.0
  314. gen_ld_data[acc_id][prt_id]['init_bal']['debit'] = 0.0
  315. gen_ld_data[acc_id][prt_id]['fin_bal'] = {}
  316. gen_ld_data[acc_id][prt_id]['fin_bal']['credit'] = 0.0
  317. gen_ld_data[acc_id][prt_id]['fin_bal']['debit'] = 0.0
  318. gen_ld_data[acc_id][prt_id]['fin_bal']['balance'] = 0.0
  319. if foreign_currency:
  320. gen_ld_data[acc_id][prt_id]['init_bal']['bal_curr'] = 0.0
  321. gen_ld_data[acc_id][prt_id]['fin_bal']['bal_curr'] = 0.0
  322. return gen_ld_data
  323. def _initialize_account(self, gen_ld_data, acc_id, foreign_currency):
  324. gen_ld_data[acc_id] = {}
  325. gen_ld_data[acc_id]['id'] = acc_id
  326. gen_ld_data[acc_id]['partners'] = False
  327. gen_ld_data[acc_id]['init_bal'] = {}
  328. gen_ld_data[acc_id]['init_bal']['balance'] = 0.0
  329. gen_ld_data[acc_id]['init_bal']['credit'] = 0.0
  330. gen_ld_data[acc_id]['init_bal']['debit'] = 0.0
  331. gen_ld_data[acc_id]['fin_bal'] = {}
  332. gen_ld_data[acc_id]['fin_bal']['credit'] = 0.0
  333. gen_ld_data[acc_id]['fin_bal']['debit'] = 0.0
  334. gen_ld_data[acc_id]['fin_bal']['balance'] = 0.0
  335. if foreign_currency:
  336. gen_ld_data[acc_id]['init_bal']['bal_curr'] = 0.0
  337. gen_ld_data[acc_id]['fin_bal']['bal_curr'] = 0.0
  338. return gen_ld_data
  339. def _get_period_ml_data(
  340. self, account_ids, partner_ids, company_id, foreign_currency,
  341. only_posted_moves, hide_account_at_0, date_from, date_to,
  342. partners_data, gen_ld_data, partners_ids, centralize,
  343. analytic_tag_ids, cost_center_ids):
  344. domain = self._get_period_domain(account_ids, partner_ids,
  345. company_id, only_posted_moves,
  346. date_to, date_from,
  347. analytic_tag_ids, cost_center_ids)
  348. ml_fields = [
  349. 'id', 'name', 'date', 'move_id', 'journal_id', 'account_id',
  350. 'partner_id', 'debit', 'credit', 'balance', 'currency_id',
  351. 'full_reconcile_id', 'tax_ids', 'analytic_tag_ids',
  352. 'amount_currency']
  353. move_lines = self.env['account.move.line'].search_read(
  354. domain=domain,
  355. fields=ml_fields)
  356. journal_ids = set()
  357. full_reconcile_ids = set()
  358. taxes_ids = set()
  359. tags_ids = set()
  360. full_reconcile_data = {}
  361. acc_prt_account_ids = self._get_acc_prt_accounts_ids(company_id)
  362. for move_line in move_lines:
  363. journal_ids.add(move_line['journal_id'][0])
  364. for tax_id in move_line['tax_ids']:
  365. taxes_ids.add(tax_id)
  366. for analytic_tag_id in move_line['analytic_tag_ids']:
  367. tags_ids.add(analytic_tag_id)
  368. if move_line['full_reconcile_id']:
  369. rec_id = move_line['full_reconcile_id'][0]
  370. if rec_id not in full_reconcile_ids:
  371. full_reconcile_data.update({
  372. rec_id: {
  373. 'id': rec_id,
  374. 'name': move_line['full_reconcile_id'][1]}
  375. })
  376. full_reconcile_ids.add(rec_id)
  377. acc_id = move_line['account_id'][0]
  378. ml_id = move_line['id']
  379. if move_line['partner_id']:
  380. prt_id = move_line['partner_id'][0]
  381. partner_name = move_line['partner_id'][1]
  382. if acc_id not in gen_ld_data.keys():
  383. gen_ld_data = self._initialize_account(gen_ld_data, acc_id,
  384. foreign_currency)
  385. if acc_id in acc_prt_account_ids:
  386. if not move_line['partner_id']:
  387. prt_id = 0
  388. partner_name = 'Missing Partner'
  389. if gen_ld_data:
  390. if prt_id not in gen_ld_data[acc_id]:
  391. if prt_id not in partners_ids:
  392. partners_ids.append(prt_id)
  393. partners_data.update({
  394. prt_id: {'id': prt_id,
  395. 'name': partner_name}
  396. })
  397. gen_ld_data = self._initialize_partner(
  398. gen_ld_data, acc_id, prt_id, foreign_currency
  399. )
  400. else:
  401. partners_ids.append(prt_id)
  402. partners_data.update({
  403. prt_id: {'id': prt_id,
  404. 'name': partner_name}
  405. })
  406. gen_ld_data = self._initialize_partner(
  407. gen_ld_data, acc_id, prt_id, foreign_currency
  408. )
  409. gen_ld_data[acc_id][prt_id][ml_id] = \
  410. self._get_move_line_data(move_line)
  411. gen_ld_data[acc_id][prt_id]['fin_bal']['credit'] += \
  412. move_line['credit']
  413. gen_ld_data[acc_id][prt_id]['fin_bal']['debit'] += \
  414. move_line['debit']
  415. gen_ld_data[acc_id][prt_id]['fin_bal']['balance'] += \
  416. move_line['balance']
  417. if foreign_currency:
  418. gen_ld_data[acc_id][prt_id]['fin_bal']['bal_curr'] += \
  419. move_line['amount_currency']
  420. else:
  421. gen_ld_data[acc_id][ml_id] = self._get_move_line_data(move_line)
  422. gen_ld_data[acc_id]['fin_bal']['credit'] += \
  423. move_line['credit']
  424. gen_ld_data[acc_id]['fin_bal']['debit'] += \
  425. move_line['debit']
  426. gen_ld_data[acc_id]['fin_bal']['balance'] += \
  427. move_line['balance']
  428. if foreign_currency:
  429. gen_ld_data[acc_id]['fin_bal']['bal_curr'] += \
  430. move_line['amount_currency']
  431. journals_data = self._get_journals_data(list(journal_ids))
  432. accounts_data = self._get_accounts_data(gen_ld_data.keys())
  433. taxes_data = self._get_taxes_data(list(taxes_ids))
  434. tags_data = self._get_tags_data(list(tags_ids))
  435. return gen_ld_data, accounts_data, partners_data, journals_data, \
  436. full_reconcile_data, taxes_data, tags_data
  437. @api.model
  438. def _create_general_ledger(self, gen_led_data, accounts_data):
  439. general_ledger = []
  440. for acc_id in gen_led_data.keys():
  441. account = {}
  442. account.update({
  443. 'code': accounts_data[acc_id]['code'],
  444. 'name': accounts_data[acc_id]['name'],
  445. 'type': 'account',
  446. 'currency_id': accounts_data[acc_id]['currency_id'],
  447. })
  448. if not gen_led_data[acc_id]['partners']:
  449. move_lines = []
  450. for ml_id in gen_led_data[acc_id].keys():
  451. if not isinstance(ml_id, int):
  452. account.update({ml_id: gen_led_data[acc_id][ml_id]})
  453. else:
  454. move_lines += [gen_led_data[acc_id][ml_id]]
  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. partner.update({'move_lines': move_lines})
  472. list_partner += [partner]
  473. account.update({'list_partner': list_partner})
  474. general_ledger += [account]
  475. return general_ledger
  476. @api.model
  477. def _get_centralized_ml(self, partners, date_to):
  478. centralized_ml = {}
  479. if isinstance(date_to, str):
  480. date_to = datetime.datetime.strptime(date_to, '%Y-%m-%d').date()
  481. for partner in partners:
  482. for move_line in partner['move_lines']:
  483. jnl_id = move_line['journal_id']
  484. month = move_line['date'].month
  485. if jnl_id not in centralized_ml.keys():
  486. centralized_ml[jnl_id] = {}
  487. if month not in centralized_ml[jnl_id].keys():
  488. centralized_ml[jnl_id][month] = {}
  489. last_day_month = \
  490. calendar.monthrange(move_line['date'].year, month)
  491. date = datetime.date(
  492. move_line['date'].year,
  493. month,
  494. last_day_month[1])
  495. if date > date_to:
  496. date = date_to
  497. centralized_ml[jnl_id][month].update({
  498. 'journal_id': jnl_id,
  499. 'ref': 'Centralized entries',
  500. 'date': date,
  501. 'debit': 0.0,
  502. 'credit': 0.0,
  503. 'balance': 0.0,
  504. 'bal_curr': 0.0,
  505. 'partner_id': False,
  506. 'rec_id': 0,
  507. 'entry_id': False,
  508. 'tax_ids': [],
  509. 'full_reconcile_id': False,
  510. 'id': False,
  511. 'tag_ids': False,
  512. 'currency_id': False,
  513. })
  514. centralized_ml[jnl_id][month]['debit'] += move_line['debit']
  515. centralized_ml[jnl_id][month]['credit'] += move_line['credit']
  516. centralized_ml[jnl_id][month]['balance'] += move_line['balance']
  517. centralized_ml[jnl_id][month]['bal_curr'] += move_line['bal_curr']
  518. list_centralized_ml = []
  519. for jnl_id in centralized_ml.keys():
  520. list_centralized_ml += list(centralized_ml[jnl_id].values())
  521. return list_centralized_ml
  522. @api.multi
  523. def _get_report_values(self, docids, data):
  524. wizard_id = data['wizard_id']
  525. company = self.env['res.company'].browse(data['company_id'])
  526. company_id = data['company_id']
  527. date_to = data['date_to']
  528. date_from = data['date_from']
  529. partner_ids = data['partner_ids']
  530. if not partner_ids:
  531. filter_partner_ids = False
  532. else:
  533. filter_partner_ids = True
  534. account_ids = data['account_ids']
  535. analytic_tag_ids = data['analytic_tag_ids']
  536. cost_center_ids = data['cost_center_ids']
  537. hide_account_at_0 = data['hide_account_at_0']
  538. foreign_currency = data['foreign_currency']
  539. only_posted_moves = data['only_posted_moves']
  540. unaffected_earnings_account = data['unaffected_earnings_account']
  541. fy_start_date = data['fy_start_date']
  542. gen_ld_data, partners_data, partners_ids = \
  543. self._get_initial_balance_data(
  544. account_ids, partner_ids, company_id, date_from,
  545. foreign_currency, only_posted_moves, hide_account_at_0,
  546. unaffected_earnings_account, fy_start_date, analytic_tag_ids,
  547. cost_center_ids)
  548. centralize = data['centralize']
  549. gen_ld_data, accounts_data, partners_data, journals_data, \
  550. full_reconcile_data, taxes_data, tags_data = \
  551. self._get_period_ml_data(
  552. account_ids, partner_ids, company_id, foreign_currency,
  553. only_posted_moves, hide_account_at_0, date_from, date_to,
  554. partners_data, gen_ld_data, partners_ids,
  555. centralize, analytic_tag_ids, cost_center_ids)
  556. general_ledger = self._create_general_ledger(gen_ld_data, accounts_data)
  557. if centralize:
  558. for account in general_ledger:
  559. if account['partners']:
  560. centralized_ml = self._get_centralized_ml(
  561. account['list_partner'], date_to)
  562. account['move_lines'] = centralized_ml
  563. account['partners'] = False
  564. del account['list_partner']
  565. general_ledger = natsorted(general_ledger, key=itemgetter('code'))
  566. return {
  567. 'doc_ids': [wizard_id],
  568. 'doc_model': 'general.ledger.report.wizard',
  569. 'docs': self.env['general.ledger.report.wizard'].browse(wizard_id),
  570. 'foreign_currency': data['foreign_currency'],
  571. 'company_name': company.display_name,
  572. 'company_currency': company.currency_id,
  573. 'currency_name': company.currency_id.name,
  574. 'date_from': data['date_from'],
  575. 'date_to': data['date_to'],
  576. 'only_posted_moves': data['only_posted_moves'],
  577. 'hide_account_at_0': data['hide_account_at_0'],
  578. 'show_analytic_tags': data['show_analytic_tags'],
  579. 'general_ledger': general_ledger,
  580. 'accounts_data': accounts_data,
  581. 'partners_data': partners_data,
  582. 'journals_data': journals_data,
  583. 'full_reconcile_data': full_reconcile_data,
  584. 'taxes_data': taxes_data,
  585. 'centralize': centralize,
  586. 'tags_data': tags_data,
  587. 'filter_partner_ids': filter_partner_ids,
  588. }