|
2 | 2 | # For copyright and license notices, see __manifest__.py file in module root |
3 | 3 | # directory |
4 | 4 | ############################################################################## |
| 5 | +from odoo.addons.l10n_ar_reports.report.account_ar_vat_line import AccountArVatLine |
| 6 | + |
| 7 | +from odoo import api |
| 8 | + |
5 | 9 | from . import models |
6 | 10 | from . import wizards |
| 11 | + |
| 12 | + |
| 13 | +def monkey_patches(): |
| 14 | +# monkey patch |
| 15 | + @api.model |
| 16 | + def _ar_vat_line_build_query(self, tables='account_move_line', where_clause='', where_params=None, |
| 17 | + column_group_key='', tax_types=('sale', 'purchase')): |
| 18 | + """Returns the SQL Select query fetching account_move_lines info in order to build the pivot view for the VAT summary. |
| 19 | + This method is also meant to be used outside this model, which is the reason why it gives the opportunity to |
| 20 | + provide a few parameters, for which the defaults are used in this model. |
| 21 | +
|
| 22 | + The query is used to build the VAT book report""" |
| 23 | + if where_params is None: |
| 24 | + where_params = [] |
| 25 | + |
| 26 | + query = f""" |
| 27 | + WITH tax_lines AS ( |
| 28 | + SELECT |
| 29 | + aml.id AS move_line_id, |
| 30 | + aml.move_id, |
| 31 | + ntg.l10n_ar_vat_afip_code AS vat_code, |
| 32 | + ntg.l10n_ar_tribute_afip_code AS tribute_code, |
| 33 | + nt.type_tax_use AS type_tax_use, |
| 34 | + aml.balance |
| 35 | + FROM account_move_line aml |
| 36 | + LEFT JOIN account_tax nt ON aml.tax_line_id = nt.id |
| 37 | + LEFT JOIN account_tax_group ntg ON nt.tax_group_id = ntg.id |
| 38 | + WHERE aml.tax_line_id IS NOT NULL |
| 39 | + ), |
| 40 | + base_lines AS ( |
| 41 | + SELECT |
| 42 | + aml.id AS move_line_id, |
| 43 | + aml.move_id, |
| 44 | + MAX(btg.l10n_ar_vat_afip_code) AS vat_code, -- MAX is used to avoid duplicates when multiple taxes exist on a line |
| 45 | + MAX(bt.type_tax_use) AS type_tax_use, |
| 46 | + aml.balance |
| 47 | + FROM account_move_line aml |
| 48 | + JOIN account_move_line_account_tax_rel amltr ON aml.id = amltr.account_move_line_id |
| 49 | + JOIN account_tax bt ON amltr.account_tax_id = bt.id |
| 50 | + JOIN account_tax_group btg ON bt.tax_group_id = btg.id |
| 51 | + GROUP BY aml.id, aml.move_id, aml.balance |
| 52 | + ) |
| 53 | + SELECT |
| 54 | + %s AS column_group_key, |
| 55 | + account_move.id, |
| 56 | + (CASE WHEN lit.l10n_ar_afip_code = '80' THEN rp.vat ELSE NULL END) AS cuit, |
| 57 | + art.name AS afip_responsibility_type_name, |
| 58 | + rp.name AS partner_name, |
| 59 | + COALESCE(tax.type_tax_use, base.type_tax_use) AS tax_type, |
| 60 | + account_move.id AS move_id, |
| 61 | + account_move.move_type, |
| 62 | + account_move.date, |
| 63 | + account_move.invoice_date, |
| 64 | + account_move.partner_id, |
| 65 | + account_move.journal_id, |
| 66 | + account_move.name AS move_name, |
| 67 | + account_move.l10n_ar_afip_responsibility_type_id AS afip_responsibility_type_id, |
| 68 | + account_move.l10n_latam_document_type_id AS document_type_id, |
| 69 | + account_move.state, |
| 70 | + account_move.company_id, |
| 71 | + SUM(CASE WHEN base.vat_code IN ('4', '5', '6', '8', '9') THEN base.balance ELSE 0 END) AS taxed, |
| 72 | + SUM(CASE WHEN base.vat_code = '4' THEN base.balance ELSE 0 END) AS base_10, |
| 73 | + SUM(CASE WHEN tax.vat_code = '4' THEN tax.balance ELSE 0 END) AS vat_10, |
| 74 | + SUM(CASE WHEN base.vat_code = '5' THEN base.balance ELSE 0 END) AS base_21, |
| 75 | + SUM(CASE WHEN tax.vat_code = '5' THEN tax.balance ELSE 0 END) AS vat_21, |
| 76 | + SUM(CASE WHEN base.vat_code = '6' THEN base.balance ELSE 0 END) AS base_27, |
| 77 | + SUM(CASE WHEN tax.vat_code = '6' THEN tax.balance ELSE 0 END) AS vat_27, |
| 78 | + SUM(CASE WHEN base.vat_code = '8' THEN base.balance ELSE 0 END) AS base_5, |
| 79 | + SUM(CASE WHEN tax.vat_code = '8' THEN tax.balance ELSE 0 END) AS vat_5, |
| 80 | + SUM(CASE WHEN base.vat_code = '9' THEN base.balance ELSE 0 END) AS base_25, |
| 81 | + SUM(CASE WHEN tax.vat_code = '9' THEN tax.balance ELSE 0 END) AS vat_25, |
| 82 | + SUM(CASE WHEN base.vat_code IN ('0', '1', '2', '3', '7') THEN base.balance ELSE 0 END) AS not_taxed, |
| 83 | + SUM(CASE WHEN tax.tribute_code = '06' THEN tax.balance ELSE 0 END) AS vat_per, |
| 84 | + SUM(CASE WHEN tax.vat_code IS NULL AND tax.tribute_code != '06' THEN tax.balance ELSE 0 END) AS other_taxes, |
| 85 | + SUM(account_move_line.balance) AS total |
| 86 | + FROM |
| 87 | + {tables} |
| 88 | + JOIN |
| 89 | + account_move ON account_move_line.move_id = account_move.id |
| 90 | + LEFT JOIN |
| 91 | + tax_lines tax ON tax.move_line_id = account_move_line.id |
| 92 | + LEFT JOIN |
| 93 | + base_lines base ON base.move_line_id = account_move_line.id |
| 94 | + LEFT JOIN |
| 95 | + res_partner rp ON rp.id = account_move.commercial_partner_id |
| 96 | + LEFT JOIN |
| 97 | + l10n_latam_identification_type lit ON rp.l10n_latam_identification_type_id = lit.id |
| 98 | + LEFT JOIN |
| 99 | + l10n_ar_afip_responsibility_type art ON account_move.l10n_ar_afip_responsibility_type_id = art.id |
| 100 | + WHERE |
| 101 | + (account_move_line.tax_line_id IS NOT NULL OR base.vat_code IS NOT NULL) |
| 102 | + AND (tax.type_tax_use IN %s OR base.type_tax_use IN %s) |
| 103 | + {where_clause} |
| 104 | + GROUP BY |
| 105 | + account_move.id, art.name, rp.id, rp.name, rp.vat, lit.id, |
| 106 | + account_move.move_type, |
| 107 | + account_move.date, |
| 108 | + account_move.invoice_date, |
| 109 | + account_move.partner_id, |
| 110 | + account_move.journal_id, |
| 111 | + account_move.name, |
| 112 | + account_move.l10n_ar_afip_responsibility_type_id, |
| 113 | + account_move.l10n_latam_document_type_id, |
| 114 | + account_move.state, |
| 115 | + account_move.company_id, |
| 116 | + COALESCE(tax.type_tax_use, base.type_tax_use) |
| 117 | +
|
| 118 | + ORDER BY |
| 119 | + account_move.invoice_date, account_move.name""" |
| 120 | + return query, [column_group_key, tax_types, tax_types, *where_params] |
| 121 | + |
| 122 | + AccountArVatLine._ar_vat_line_build_query = _ar_vat_line_build_query |
0 commit comments