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.

708 lines
32 KiB

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