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.

351 lines
14 KiB

  1. # Copyright 2019-20 ForgeFlow S.L. (https://www.forgeflow.com)
  2. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
  3. from odoo import api, models
  4. import operator
  5. import itertools
  6. class JournalLedgerReport(models.AbstractModel):
  7. _name = 'report.account_financial_report.journal_ledger'
  8. _description = "Journal Ledger Report"
  9. def _get_journal_ledger_data(self, journal):
  10. return {
  11. 'id': journal.id,
  12. 'name': journal.name,
  13. 'currency_id': journal.currency_id.id,
  14. 'currency_name': journal.currency_id and
  15. journal.currency_id.name or
  16. journal.company_id.currency_id.name,
  17. 'debit': 0.0,
  18. 'credit': 0.0,
  19. }
  20. def _get_journal_ledgers_domain(self, wizard, journal_ids, company):
  21. domain = []
  22. if company:
  23. domain += [('company_id', '=', company.id)]
  24. if journal_ids:
  25. domain += [('id', 'in', journal_ids)]
  26. return domain
  27. def _get_journal_ledgers(self, wizard, journal_ids, company):
  28. journals = self.env['account.journal'].search(
  29. self._get_journal_ledgers_domain(wizard, journal_ids, company),
  30. order='name asc')
  31. journal_ledgers_data = []
  32. for journal in journals:
  33. journal_ledgers_data.append(
  34. self._get_journal_ledger_data(journal))
  35. return journal_ledgers_data
  36. def _get_moves_domain(self, wizard, journal_ids):
  37. domain = [
  38. ('journal_id', 'in', journal_ids),
  39. ('date', '>=', wizard.date_from),
  40. ('date', '<=', wizard.date_to),
  41. ]
  42. if wizard.move_target != 'all':
  43. domain += [('state', '=', wizard.move_target)]
  44. return domain
  45. def _get_moves_order(self, wizard, journal_ids):
  46. search_order = ''
  47. if wizard.sort_option == 'move_name':
  48. search_order = 'name asc'
  49. elif wizard.sort_option == 'date':
  50. search_order = 'date asc, name asc'
  51. return search_order
  52. def _get_moves_data(self, move):
  53. return {
  54. 'move_id': move.id,
  55. 'journal_id': move.journal_id.id,
  56. 'entry': move.name,
  57. }
  58. def _get_moves(self, wizard, journal_ids):
  59. moves = self.env['account.move'].search(
  60. self._get_moves_domain(wizard, journal_ids),
  61. order=self._get_moves_order(wizard, journal_ids))
  62. Moves = []
  63. move_data = {}
  64. for move in moves:
  65. move_data[move.id] = self._get_moves_data(move)
  66. Moves.append(move_data[move.id])
  67. return moves.ids, Moves, move_data
  68. def _get_move_lines_domain(self, move_ids, wizard, journal_ids):
  69. return [
  70. ('move_id', 'in', move_ids),
  71. ]
  72. def _get_move_lines_order(self, move_ids, wizard, journal_ids):
  73. return ''
  74. def _get_move_lines_data(self, ml, wizard, ml_taxes):
  75. base_debit = base_credit = tax_debit = tax_credit = \
  76. base_balance = tax_balance = 0.0
  77. if ml.tax_exigible:
  78. base_debit = ml_taxes and ml.debit or 0.0
  79. base_credit = ml_taxes and ml.credit or 0.0
  80. base_balance = ml_taxes and ml.balance or 0.0
  81. tax_debit = ml.tax_line_id and ml.debit or 0.0
  82. tax_credit = ml.tax_line_id and ml.credit or 0.0
  83. tax_balance = ml.tax_line_id and ml.balance or 0.0
  84. return {
  85. 'move_line_id': ml.id,
  86. 'move_id': ml.move_id.id,
  87. 'date': ml.date,
  88. 'journal_id': ml.journal_id.id,
  89. 'account_id': ml.account_id.id,
  90. 'partner_id': ml.partner_id.id,
  91. 'label': ml.name,
  92. 'debit': ml.debit,
  93. 'credit': ml.credit,
  94. 'company_currency_id': ml.company_currency_id.id,
  95. 'amount_currency': ml.amount_currency,
  96. 'currency_id': ml.currency_id.id,
  97. 'tax_line_id': ml.tax_line_id.id,
  98. 'tax_ids': list(ml_taxes.keys()),
  99. 'base_debit': base_debit,
  100. 'base_credit': base_credit,
  101. 'tax_debit': tax_debit,
  102. 'tax_credit': tax_credit,
  103. 'base_balance': base_balance,
  104. 'tax_balance': tax_balance,
  105. }
  106. def _get_account_data(self, accounts):
  107. data = {}
  108. for account in accounts:
  109. data[account.id] = self._get_account_id_data(account)
  110. return data
  111. def _get_account_id_data(self, account):
  112. return {
  113. 'name': account.name,
  114. 'code': account.code,
  115. 'internal_type': account.internal_type,
  116. }
  117. def _get_partner_data(self, partners):
  118. data = {}
  119. for partner in partners:
  120. data[partner.id] = self._get_partner_id_data(partner)
  121. return data
  122. def _get_partner_id_data(self, partner):
  123. return {
  124. 'name': partner.name,
  125. }
  126. def _get_currency_data(self, currencies):
  127. data = {}
  128. for currency in currencies:
  129. data[currency.id] = self._get_currency_id_data(currency)
  130. return data
  131. def _get_currency_id_data(self, currency):
  132. return {
  133. 'name': currency.name,
  134. }
  135. def _get_tax_line_data(self, taxes):
  136. data = {}
  137. for tax in taxes:
  138. data[tax.id] = self._get_tax_line_id_data(tax)
  139. return data
  140. def _get_tax_line_id_data(self, tax):
  141. return {
  142. 'name': tax.name,
  143. 'description': tax.description,
  144. }
  145. def _get_query_taxes(self):
  146. return """
  147. SELECT aml_at_rel.account_move_line_id, aml_at_rel.account_tax_id,
  148. at.description, at.name
  149. FROM account_move_line_account_tax_rel AS aml_at_rel
  150. LEFT JOIN
  151. account_tax AS at on (at.id = aml_at_rel.account_tax_id)
  152. WHERE account_move_line_id IN %(move_line_ids)s
  153. """
  154. def _get_query_taxes_params(self, move_lines):
  155. return {
  156. 'move_line_ids': tuple(move_lines.ids),
  157. }
  158. def _get_move_lines(self, move_ids, wizard, journal_ids):
  159. move_lines = self.env['account.move.line'].search(
  160. self._get_move_lines_domain(move_ids, wizard, journal_ids),
  161. order=self._get_move_lines_order(move_ids, wizard, journal_ids))
  162. # Get the taxes ids for the move lines
  163. query_taxes_params = self._get_query_taxes_params(move_lines)
  164. query_taxes = self._get_query_taxes()
  165. move_line_ids_taxes_data = {}
  166. self.env.cr.execute(query_taxes,
  167. query_taxes_params)
  168. # Fetch the taxes associated to the move line
  169. for move_line_id, account_tax_id, tax_description, tax_name in \
  170. self.env.cr.fetchall():
  171. if move_line_id not in move_line_ids_taxes_data.keys():
  172. move_line_ids_taxes_data[move_line_id] = {}
  173. move_line_ids_taxes_data[move_line_id][account_tax_id] = {
  174. 'name': tax_name,
  175. 'description': tax_description
  176. }
  177. Move_Lines = {}
  178. accounts = self.env['account.account']
  179. partners = self.env['res.partner']
  180. currencies = self.env['res.currency']
  181. tax_lines = self.env['account.tax']
  182. for ml in move_lines:
  183. if ml.account_id not in accounts:
  184. accounts |= ml.account_id
  185. if ml.partner_id not in partners:
  186. partners |= ml.partner_id
  187. if ml.currency_id not in currencies:
  188. currencies |= ml.currency_id
  189. if ml.tax_line_id not in tax_lines:
  190. tax_lines |= ml.tax_line_id
  191. if ml.move_id.id not in Move_Lines.keys():
  192. Move_Lines[ml.move_id.id] = []
  193. taxes = ml.id in move_line_ids_taxes_data.keys() and \
  194. move_line_ids_taxes_data[ml.id] or {}
  195. Move_Lines[ml.move_id.id].append(self._get_move_lines_data(
  196. ml, wizard, taxes))
  197. account_ids_data = self._get_account_data(accounts)
  198. partner_ids_data = self._get_partner_data(partners)
  199. currency_ids_data = self._get_currency_data(currencies)
  200. tax_line_ids_data = self._get_tax_line_data(tax_lines)
  201. return move_lines.ids, Move_Lines, account_ids_data, \
  202. partner_ids_data, currency_ids_data, tax_line_ids_data, \
  203. move_line_ids_taxes_data
  204. def _get_journal_tax_lines(self, wizard, moves_data):
  205. journals_taxes_data = {}
  206. for move_data in moves_data:
  207. report_move_lines = move_data['report_move_lines']
  208. for report_move_line in report_move_lines:
  209. ml_data = report_move_line
  210. tax_ids = []
  211. if ml_data['tax_line_id']:
  212. tax_ids.append(ml_data['tax_line_id'])
  213. if ml_data['tax_ids']:
  214. tax_ids += ml_data['tax_ids']
  215. tax_ids = list(set(tax_ids))
  216. journal_id = ml_data['journal_id']
  217. if journal_id not in journals_taxes_data.keys():
  218. journals_taxes_data[journal_id] = {}
  219. taxes = self.env['account.tax'].browse(tax_ids)
  220. for tax in taxes:
  221. if tax.id not in journals_taxes_data[journal_id]:
  222. journals_taxes_data[journal_id][tax.id] = {
  223. 'base_debit': 0.0,
  224. 'base_credit': 0.0,
  225. 'base_balance': 0.0,
  226. 'tax_debit': 0.0,
  227. 'tax_credit': 0.0,
  228. 'tax_balance': 0.0,
  229. 'tax_name': tax.name,
  230. 'tax_code': tax.description,
  231. }
  232. field_keys = ['base_debit', 'base_credit', 'base_balance',
  233. 'tax_debit', 'tax_credit', 'tax_balance',
  234. ]
  235. for field_key in field_keys:
  236. journals_taxes_data[journal_id][tax.id][field_key] += \
  237. ml_data[field_key]
  238. journals_taxes_data_2 = {}
  239. for journal_id in journals_taxes_data.keys():
  240. journals_taxes_data_2[journal_id] = []
  241. for tax_id in journals_taxes_data[journal_id].keys():
  242. journals_taxes_data_2[journal_id] += \
  243. [journals_taxes_data[journal_id][tax_id]]
  244. return journals_taxes_data_2
  245. @api.multi
  246. def _get_report_values(self, docids, data):
  247. wizard_id = data['wizard_id']
  248. wizard = self.env['journal.ledger.report.wizard'].browse(wizard_id)
  249. company = self.env['res.company'].browse(data['company_id'])
  250. journal_ids = data['journal_ids']
  251. journal_ledgers_data = self._get_journal_ledgers(
  252. wizard, journal_ids, company)
  253. move_ids, moves_data, move_ids_data = self._get_moves(
  254. wizard, journal_ids)
  255. journal_moves_data = {}
  256. for key, items in itertools.groupby(
  257. moves_data, operator.itemgetter('journal_id')):
  258. if key not in journal_moves_data.keys():
  259. journal_moves_data[key] = []
  260. journal_moves_data[key] += list(items)
  261. move_lines_data = account_ids_data = partner_ids_data = \
  262. currency_ids_data = tax_line_ids_data = \
  263. move_line_ids_taxes_data = {}
  264. if move_ids:
  265. move_line_ids, move_lines_data, account_ids_data, \
  266. partner_ids_data, currency_ids_data, tax_line_ids_data, \
  267. move_line_ids_taxes_data = self._get_move_lines(
  268. move_ids, wizard, journal_ids)
  269. for move_data in moves_data:
  270. move_id = move_data['move_id']
  271. move_data['report_move_lines'] = []
  272. if move_id in move_lines_data.keys():
  273. move_data['report_move_lines'] += move_lines_data[move_id]
  274. journals_taxes_data = {}
  275. if moves_data:
  276. journals_taxes_data = self._get_journal_tax_lines(
  277. wizard, moves_data)
  278. for journal_ledger_data in journal_ledgers_data:
  279. journal_id = journal_ledger_data['id']
  280. journal_ledger_data['tax_lines'] = \
  281. journals_taxes_data.get(journal_id, [])
  282. journal_totals = {}
  283. for move_id in move_lines_data.keys():
  284. for move_line_data in move_lines_data[move_id]:
  285. journal_id = move_line_data['journal_id']
  286. if journal_id not in journal_totals.keys():
  287. journal_totals[journal_id] = {
  288. 'debit': 0.0,
  289. 'credit': 0.0,
  290. }
  291. for item in ['debit', 'credit']:
  292. journal_totals[journal_id][item] += move_line_data[item]
  293. for journal_ledger_data in journal_ledgers_data:
  294. journal_id = journal_ledger_data['id']
  295. if journal_id in journal_moves_data.keys():
  296. journal_ledger_data['report_moves'] = \
  297. journal_moves_data[journal_id]
  298. else:
  299. journal_ledger_data['report_moves'] = []
  300. if journal_id in journal_totals.keys():
  301. for item in ['debit', 'credit']:
  302. journal_ledger_data[item] += \
  303. journal_totals[journal_id][item]
  304. return {
  305. 'doc_ids': [wizard_id],
  306. 'doc_model': 'journal.ledger.report.wizard',
  307. 'docs': self.env['journal.ledger.report.wizard'].browse(wizard_id),
  308. 'group_option': data['group_option'],
  309. 'foreign_currency': data['foreign_currency'],
  310. 'with_account_name': data['with_account_name'],
  311. 'company_name': company.display_name,
  312. 'currency_name': company.currency_id.name,
  313. 'date_from': data['date_from'],
  314. 'date_to': data['date_to'],
  315. 'move_target': data['move_target'],
  316. 'account_ids_data': account_ids_data,
  317. 'partner_ids_data': partner_ids_data,
  318. 'currency_ids_data': currency_ids_data,
  319. 'move_ids_data': move_ids_data,
  320. 'tax_line_data': tax_line_ids_data,
  321. 'move_line_ids_taxes_data': move_line_ids_taxes_data,
  322. 'Journal_Ledgers': journal_ledgers_data,
  323. 'Moves': moves_data,
  324. }