| 1 |
# Part of Odoo. See LICENSE file for full copyright and licensing details. |
| 2 |
import logging |
| 3 |
|
| 4 |
from odoo import models, fields, api, Command, _ |
| 5 |
from odoo.exceptions import ValidationError |
| 6 |
from odoo.exceptions import UserError |
| 7 |
from datetime import datetime |
| 8 |
|
| 9 |
_logger = logging.getLogger(__name__) |
| 10 |
|
| 11 |
|
| 12 |
class AccountPaymentRegister(models.TransientModel): |
| 13 |
_inherit = 'account.payment.register' |
| 14 |
|
| 15 |
l10n_ar_withholding_ids = fields.One2many( |
| 16 |
'l10n_ar.payment.register.withholding', 'payment_register_id', string="Withholdings", |
| 17 |
compute="_compute_l10n_ar_withholding_ids", readonly=False, store=True) |
| 18 |
l10n_ar_net_amount = fields.Monetary(compute='_compute_l10n_ar_net_amount', readonly=True, help="Net amount after withholdings") |
| 19 |
l10n_ar_adjustment_warning = fields.Boolean(compute="_compute_l10n_ar_adjustment_warning") |
| 20 |
|
| 21 |
@api.depends('can_edit_wizard', 'source_amount', 'source_amount_currency', 'source_currency_id', 'company_id', 'currency_id', 'payment_date', 'installments_mode', 'l10n_latam_move_check_ids.amount', 'l10n_latam_new_check_ids.amount', 'payment_method_code') |
| 22 |
def _compute_amount(self): |
| 23 |
super()._compute_amount() |
| 24 |
for wizard in self: |
| 25 |
checks = wizard.l10n_latam_new_check_ids if wizard.filtered(lambda x: x._is_latam_check_payment(check_subtype='new_check')) else wizard.l10n_latam_move_check_ids |
| 26 |
checks_amount = sum(checks.mapped('amount')) |
| 27 |
if not wizard.currency_id.is_zero(checks_amount) and wizard.currency_id.compare_amounts(checks_amount, wizard.l10n_ar_net_amount) != 0: |
| 28 |
if wizard.partner_type == 'supplier': |
| 29 |
original_amount = wizard.amount |
| 30 |
f_delta = checks_amount - wizard.l10n_ar_net_amount |
| 31 |
if f_delta < 0: |
| 32 |
# Removing withholdings can result in an overshoot of the initial amount |
| 33 |
wizard.amount = checks_amount |
| 34 |
f_delta = checks_amount - wizard.l10n_ar_net_amount |
| 35 |
d = f_delta |
| 36 |
f_previous = wizard.l10n_ar_net_amount |
| 37 |
wizard.amount += d |
| 38 |
wizard._compute_l10n_ar_net_amount() |
| 39 |
for i in range(201): |
| 40 |
f_delta = checks_amount - wizard.l10n_ar_net_amount |
| 41 |
if wizard.currency_id.is_zero(f_delta): |
| 42 |
break |
| 43 |
der = ((wizard.l10n_ar_net_amount - f_previous) / d) if abs(d) >= 0.01 else 1.0 |
| 44 |
if wizard.currency_id.is_zero(der): |
| 45 |
i = 200 |
| 46 |
break |
| 47 |
d = max(f_delta / der, 0.01) |
| 48 |
f_previous = wizard.l10n_ar_net_amount |
| 49 |
wizard.amount += d |
| 50 |
wizard._compute_l10n_ar_net_amount() |
| 51 |
if i == 200: |
| 52 |
# Adjustment failed, resetting |
| 53 |
wizard.amount = original_amount |
| 54 |
|
| 55 |
@api.depends('amount', 'l10n_latam_move_check_ids', 'l10n_latam_new_check_ids', 'payment_method_code') |
| 56 |
def _compute_l10n_ar_adjustment_warning(self): |
| 57 |
for wizard in self: |
| 58 |
checks = wizard.l10n_latam_new_check_ids if wizard.filtered(lambda x: x._is_latam_check_payment(check_subtype='new_check')) else wizard.l10n_latam_move_check_ids |
| 59 |
checks_amount = sum(checks.mapped('amount')) |
| 60 |
wizard.l10n_ar_adjustment_warning = not wizard.currency_id.is_zero(checks_amount) and wizard.currency_id.compare_amounts(checks_amount, wizard.l10n_ar_net_amount) != 0 |
| 61 |
|
| 62 |
@api.depends('amount', 'l10n_ar_withholding_ids.amount') |
| 63 |
def _compute_l10n_ar_net_amount(self): |
| 64 |
for rec in self: |
| 65 |
rec.l10n_ar_net_amount = rec.amount - sum(rec.l10n_ar_withholding_ids.mapped('amount')) |
| 66 |
|
| 67 |
def _create_payment_vals_from_wizard(self, batch_result): |
| 68 |
payment_vals = super()._create_payment_vals_from_wizard(batch_result) |
| 69 |
|
| 70 |
if not self.l10n_ar_withholding_ids: |
| 71 |
return payment_vals # Nothing to do if we are not working with withholding taxes. |
| 72 |
|
| 73 |
payment_vals['amount'] = self.l10n_ar_net_amount |
| 74 |
conversion_rate = self._get_conversion_rate() |
| 75 |
sign = 1 |
| 76 |
if self.partner_type == 'supplier': |
| 77 |
sign = -1 |
| 78 |
for line in self.l10n_ar_withholding_ids: |
| 79 |
if not line.name: |
| 80 |
if line.tax_id.l10n_ar_withholding_sequence_id: |
| 81 |
line.name = line.tax_id.l10n_ar_withholding_sequence_id.next_by_id() |
| 82 |
else: |
| 83 |
raise UserError(_('Please enter withholding number for tax %s') % line.tax_id.name) |
| 84 |
dummy, account_id, tax_repartition_line_id = line._tax_compute_all_helper() |
| 85 |
balance = self.company_currency_id.round(line.amount * conversion_rate) |
| 86 |
# create withholding amount applied move line only if amount != 0 |
| 87 |
payment_vals['write_off_line_vals'].append({ |
| 88 |
'currency_id': self.currency_id.id, |
| 89 |
'name': line.name, |
| 90 |
'account_id': account_id, |
| 91 |
'amount_currency': sign * line.amount, |
| 92 |
'balance': sign * balance, |
| 93 |
'tax_base_amount': sign * line.base_amount, |
| 94 |
'tax_repartition_line_id': tax_repartition_line_id, |
| 95 |
}) |
| 96 |
|
| 97 |
for base_amount in list(set(self.l10n_ar_withholding_ids.mapped('base_amount'))): |
| 98 |
withholding_lines = self.l10n_ar_withholding_ids.filtered(lambda x: x.base_amount == base_amount) |
| 99 |
nice_base_label = ','.join(withholding_lines.mapped('name')) |
| 100 |
account_id = self.company_id.l10n_ar_tax_base_account_id.id |
| 101 |
base_amount = sign * base_amount |
| 102 |
cc_base_amount = self.company_currency_id.round(base_amount * conversion_rate) |
| 103 |
payment_vals['write_off_line_vals'].append({ |
| 104 |
'currency_id': self.currency_id.id, |
| 105 |
'name': nice_base_label, |
| 106 |
'tax_ids': [Command.set(withholding_lines.mapped('tax_id').ids)], |
| 107 |
'account_id': account_id, |
| 108 |
'balance': cc_base_amount, |
| 109 |
'amount_currency': base_amount, |
| 110 |
}) |
| 111 |
payment_vals['write_off_line_vals'].append({ |
| 112 |
'currency_id': self.currency_id.id, # Counterpart 0 operation |
| 113 |
'name': nice_base_label, |
| 114 |
'account_id': account_id, |
| 115 |
'balance': -cc_base_amount, |
| 116 |
'amount_currency': -base_amount, |
| 117 |
}) |
| 118 |
|
| 119 |
return payment_vals |
| 120 |
|
| 121 |
def _get_conversion_rate(self): |
| 122 |
self.ensure_one() |
| 123 |
if self.currency_id != self.company_id.currency_id: |
| 124 |
return self.env['res.currency']._get_conversion_rate( |
| 125 |
self.currency_id, |
| 126 |
self.company_id.currency_id, |
| 127 |
self.company_id, |
| 128 |
self.payment_date, |
| 129 |
) |
| 130 |
return 1.0 |
| 131 |
|
| 132 |
@api.depends('partner_id', 'payment_date') |
| 133 |
def _compute_l10n_ar_withholding_ids(self): |
| 134 |
for wizard in self: |
| 135 |
date = wizard.payment_date or fields.Date.context_today(self) |
| 136 |
partner_taxes = self.env['l10n_ar.partner.tax'].search([ |
| 137 |
*self.env['l10n_ar.partner.tax']._check_company_domain(wizard.company_id), |
| 138 |
'|', ('from_date', '>=', date), ('from_date', '=', False), |
| 139 |
'|', ('to_date', '<=', date), ('to_date', '=', False), |
| 140 |
('partner_id', '=', wizard.partner_id.commercial_partner_id.id), |
| 141 |
('tax_id.l10n_ar_withholding_payment_type', '=', wizard.partner_type) |
| 142 |
]) |
| 143 |
wizard.l10n_ar_withholding_ids = [Command.clear()] + [Command.create({'tax_id': x.tax_id.id}) for x in partner_taxes] |
| 144 |
|
| 145 |
def action_create_payments(self): |
| 146 |
if self.l10n_ar_withholding_ids and not self.payment_method_line_id.payment_account_id: |
| 147 |
raise ValidationError(_("A payment cannot have withholding if the payment method has no outstanding accounts")) |
| 148 |
return super().action_create_payments() |