| 1 |
# pylint: disable=protected-access |
| 2 |
# Part of Odoo. See LICENSE file for full copyright and licensing details. |
| 3 |
import logging |
| 4 |
|
| 5 |
from dateutil.relativedelta import relativedelta |
| 6 |
from datetime import datetime |
| 7 |
from odoo import models, fields, api |
| 8 |
|
| 9 |
_logger = logging.getLogger(__name__) |
| 10 |
|
| 11 |
|
| 12 |
class L10n_ArPaymentRegisterWithholding(models.TransientModel): |
| 13 |
_name = 'l10n_ar.payment.register.withholding' |
| 14 |
_description = 'Payment register withholding lines' |
| 15 |
_check_company_auto = True |
| 16 |
|
| 17 |
payment_register_id = fields.Many2one('account.payment.register', required=True, ondelete='cascade') |
| 18 |
company_id = fields.Many2one(related='payment_register_id.company_id') |
| 19 |
currency_id = fields.Many2one(related='payment_register_id.currency_id') |
| 20 |
name = fields.Char(string='Number') |
| 21 |
tax_id = fields.Many2one( |
| 22 |
'account.tax', check_company=True, required=True, |
| 23 |
domain="[('l10n_ar_withholding_payment_type', '=', parent.partner_type)]") |
| 24 |
withholding_sequence_id = fields.Many2one(related='tax_id.l10n_ar_withholding_sequence_id') |
| 25 |
base_amount = fields.Monetary(required=True, compute='_compute_base_amount', store=True, readonly=False) |
| 26 |
amount = fields.Monetary(required=True, compute='_compute_amount', store=True, readonly=False) |
| 27 |
|
| 28 |
def _tax_compute_all_helper(self): |
| 29 |
self.ensure_one() |
| 30 |
# Computes the withholding tax amount provided a base and a tax |
| 31 |
# It is equivalent to: amount = self.base * self.tax_id.amount / 100 |
| 32 |
|
| 33 |
# if it is earnings withholding, then we accumulate the tax base for the period |
| 34 |
if self.tax_id.l10n_ar_tax_type in ['earnings', 'earnings_scale']: |
| 35 |
to_date = self.payment_register_id.payment_date or datetime.date.today() |
| 36 |
from_date = to_date + relativedelta(day=1) |
| 37 |
# We search for the payments in the same month of the same regimen and the same code. |
| 38 |
domain_same_period_withholdings = [ |
| 39 |
('company_id', 'child_of', self.tax_id.company_id.id), |
| 40 |
('parent_state', '=', 'posted'), |
| 41 |
('tax_line_id.l10n_ar_code', '=', self.tax_id.l10n_ar_code), |
| 42 |
('tax_line_id.l10n_ar_tax_type', 'in', ['earnings', 'earnings_scale']), |
| 43 |
('partner_id', '=', self.payment_register_id.partner_id.commercial_partner_id.id), |
| 44 |
('date', '<=', to_date), ('date', '>=', from_date)] |
| 45 |
if same_period_partner_withholdings := self.env['account.move.line'].sudo()._read_group(domain_same_period_withholdings, ['partner_id'], ['balance:sum']): |
| 46 |
same_period_withholdings = abs(same_period_partner_withholdings[0][1]) |
| 47 |
else: |
| 48 |
same_period_withholdings = 0.0 |
| 49 |
domain_same_period_base = [ |
| 50 |
('company_id', 'child_of', self.tax_id.company_id.id), |
| 51 |
('parent_state', '=', 'posted'), |
| 52 |
('tax_ids.l10n_ar_code', '=', self.tax_id.l10n_ar_code), |
| 53 |
('tax_ids.l10n_ar_tax_type', 'in', ['earnings', 'earnings_scale']), |
| 54 |
('partner_id', '=', self.payment_register_id.partner_id.commercial_partner_id.id), |
| 55 |
('date', '<=', to_date), ('date', '>=', from_date)] |
| 56 |
if same_period_partner_base := self.env['account.move.line'].sudo()._read_group(domain_same_period_base, ['partner_id'], ['balance:sum']): |
| 57 |
same_period_base = abs(same_period_partner_base[0][1]) |
| 58 |
else: |
| 59 |
same_period_base = 0.0 |
| 60 |
net_amount = self.base_amount + same_period_base |
| 61 |
else: |
| 62 |
net_amount = self.base_amount |
| 63 |
net_amount = max(0, net_amount - self.tax_id.l10n_ar_non_taxable_amount) |
| 64 |
taxes_res = self.tax_id.compute_all( |
| 65 |
net_amount, |
| 66 |
currency=self.payment_register_id.currency_id, |
| 67 |
quantity=1.0, |
| 68 |
product=False, |
| 69 |
partner=False, |
| 70 |
is_refund=False, |
| 71 |
rounding_method='round_per_line', |
| 72 |
) |
| 73 |
tax_amount = taxes_res['taxes'][0]['amount'] |
| 74 |
tax_account_id = taxes_res['taxes'][0]['account_id'] |
| 75 |
tax_repartition_line_id = taxes_res['taxes'][0]['tax_repartition_line_id'] |
| 76 |
|
| 77 |
if self.tax_id.l10n_ar_tax_type in ['earnings', 'earnings_scale']: |
| 78 |
# if it is earnings scale we calculate according to the scale. |
| 79 |
if self.tax_id.l10n_ar_tax_type == 'earnings_scale': |
| 80 |
escala = self.env['l10n_ar.earnings.scale.line'].search([ |
| 81 |
('scale_id', '=', self.tax_id.l10n_ar_scale_id.id), |
| 82 |
('excess_amount', '<=', net_amount), |
| 83 |
('to_amount', '>', net_amount), |
| 84 |
], limit=1) |
| 85 |
tax_amount = ((net_amount - escala.excess_amount) * escala.percentage / 100) + escala.fixed_amount |
| 86 |
# deduct withholdings from the same period |
| 87 |
tax_amount -= same_period_withholdings |
| 88 |
|
| 89 |
l10n_ar_minimum_threshold = self.tax_id.l10n_ar_minimum_threshold |
| 90 |
if l10n_ar_minimum_threshold > tax_amount: |
| 91 |
tax_amount = 0.0 |
| 92 |
return tax_amount, tax_account_id, tax_repartition_line_id |
| 93 |
|
| 94 |
@api.depends('base_amount', 'tax_id') |
| 95 |
def _compute_amount(self): |
| 96 |
for line in self: |
| 97 |
if not line.tax_id: |
| 98 |
line.amount = 0.0 |
| 99 |
else: |
| 100 |
line.amount = line._tax_compute_all_helper()[0] |
| 101 |
|
| 102 |
@api.depends('payment_register_id.amount', 'tax_id') |
| 103 |
def _compute_base_amount(self): |
| 104 |
for wth in self: |
| 105 |
if wth.tax_id.l10n_ar_tax_type == 'iibb_total': |
| 106 |
wth.base_amount = wth.payment_register_id.amount |
| 107 |
else: |
| 108 |
wth.base_amount = wth.payment_register_id.amount * sum(wth.payment_register_id.line_ids.mapped('move_id.amount_untaxed')) / sum(wth.payment_register_id.line_ids.mapped("move_id.amount_total")) |