| 1 |
# pylint: disable=protected-access |
| 2 |
# Part of Odoo. See LICENSE file for full copyright and licensing details. |
| 3 |
import logging |
| 4 |
import stdnum |
| 5 |
|
| 6 |
from odoo import models, fields, api, Command, _ |
| 7 |
from odoo.exceptions import UserError, ValidationError |
| 8 |
from odoo.tools import index_exists |
| 9 |
|
| 10 |
|
| 11 |
_logger = logging.getLogger(__name__) |
| 12 |
|
| 13 |
|
| 14 |
class L10n_LatamCheck(models.Model): |
| 15 |
_name = 'l10n_latam.check' |
| 16 |
_description = 'Account payment check' |
| 17 |
_check_company_auto = True |
| 18 |
_inherit = ['mail.thread', 'mail.activity.mixin'] |
| 19 |
|
| 20 |
payment_id = fields.Many2one( |
| 21 |
'account.payment', |
| 22 |
required=True, |
| 23 |
ondelete='cascade', |
| 24 |
) |
| 25 |
operation_ids = fields.Many2many( |
| 26 |
comodel_name='account.payment', |
| 27 |
relation='l10n_latam_check_account_payment_rel', |
| 28 |
column1="check_id", |
| 29 |
column2="payment_id", |
| 30 |
readonly=True, |
| 31 |
check_company=True, |
| 32 |
) |
| 33 |
current_journal_id = fields.Many2one( |
| 34 |
comodel_name='account.journal', |
| 35 |
compute='_compute_current_journal', store=True, |
| 36 |
) |
| 37 |
name = fields.Char(string='Number') |
| 38 |
bank_id = fields.Many2one( |
| 39 |
comodel_name='res.bank', |
| 40 |
compute='_compute_bank_id', store=True, readonly=False, |
| 41 |
) |
| 42 |
issuer_vat = fields.Char( |
| 43 |
compute='_compute_issuer_vat', store=True, readonly=False, |
| 44 |
) |
| 45 |
payment_date = fields.Date(readonly=False, required=True) |
| 46 |
amount = fields.Monetary() |
| 47 |
outstanding_line_id = fields.Many2one('account.move.line', readonly=True, check_company=True) |
| 48 |
issue_state = fields.Selection( |
| 49 |
selection=[('handed', 'Handed'), ('debited', 'Debited'), ('voided', 'Voided')], |
| 50 |
compute='_compute_issue_state', |
| 51 |
store=True |
| 52 |
) |
| 53 |
# fields from payment |
| 54 |
payment_method_code = fields.Char(related='payment_id.payment_method_code') |
| 55 |
partner_id = fields.Many2one(related='payment_id.partner_id') |
| 56 |
original_journal_id = fields.Many2one(related='payment_id.journal_id') |
| 57 |
company_id = fields.Many2one(related='payment_id.company_id', store=True) |
| 58 |
currency_id = fields.Many2one(related='payment_id.currency_id') |
| 59 |
payment_method_line_id = fields.Many2one( |
| 60 |
related='payment_id.payment_method_line_id', |
| 61 |
store=True, |
| 62 |
) |
| 63 |
|
| 64 |
# issue_state is used to know that is an own check and also that is posted |
| 65 |
_unique = models.UniqueIndex("(name, payment_method_line_id) WHERE outstanding_line_id IS NOT NULL") |
| 66 |
|
| 67 |
@api.onchange('name') |
| 68 |
def _onchange_name(self): |
| 69 |
if self.name: |
| 70 |
self.name = self.name.zfill(8) |
| 71 |
|
| 72 |
def _prepare_void_move_vals(self): |
| 73 |
return { |
| 74 |
'ref': 'Void check', |
| 75 |
'journal_id': self.outstanding_line_id.move_id.journal_id.id, |
| 76 |
'line_ids': [ |
| 77 |
Command.create({ |
| 78 |
'name': "Void check %s" % self.outstanding_line_id.name, |
| 79 |
'date_maturity': self.outstanding_line_id.date_maturity, |
| 80 |
'amount_currency': self.outstanding_line_id.amount_currency, |
| 81 |
'currency_id': self.outstanding_line_id.currency_id.id, |
| 82 |
'debit': self.outstanding_line_id.debit, |
| 83 |
'credit': self.outstanding_line_id.credit, |
| 84 |
'partner_id': self.outstanding_line_id.partner_id.id, |
| 85 |
'account_id': self.payment_id.destination_account_id.id, |
| 86 |
}), |
| 87 |
Command.create({ |
| 88 |
'name': "Void check %s" % self.outstanding_line_id.name, |
| 89 |
'date_maturity': self.outstanding_line_id.date_maturity, |
| 90 |
'amount_currency': -self.outstanding_line_id.amount_currency, |
| 91 |
'currency_id': self.outstanding_line_id.currency_id.id, |
| 92 |
'debit': -self.outstanding_line_id.debit, |
| 93 |
'credit': -self.outstanding_line_id.credit, |
| 94 |
'partner_id': self.outstanding_line_id.partner_id.id, |
| 95 |
'account_id': self.outstanding_line_id.account_id.id, |
| 96 |
}), |
| 97 |
], |
| 98 |
} |
| 99 |
|
| 100 |
@api.depends('outstanding_line_id.amount_residual') |
| 101 |
def _compute_issue_state(self): |
| 102 |
for rec in self: |
| 103 |
if not rec.outstanding_line_id: |
| 104 |
rec.issue_state = False |
| 105 |
elif rec.amount and not rec.outstanding_line_id.amount_residual: |
| 106 |
if any( |
| 107 |
line.account_id.account_type in ['liability_payable', 'asset_receivable'] |
| 108 |
for line in rec.outstanding_line_id.matched_debit_ids.debit_move_id.move_id.line_ids |
| 109 |
): |
| 110 |
rec.issue_state = 'voided' |
| 111 |
else: |
| 112 |
rec.issue_state = 'debited' |
| 113 |
else: |
| 114 |
rec.issue_state = 'handed' |
| 115 |
|
| 116 |
def action_void(self): |
| 117 |
for rec in self.filtered('outstanding_line_id'): |
| 118 |
void_move = rec.env['account.move'].create(rec._prepare_void_move_vals()) |
| 119 |
void_move.action_post() |
| 120 |
(void_move.line_ids[1] + rec.outstanding_line_id).reconcile() |
| 121 |
|
| 122 |
def _get_last_operation(self): |
| 123 |
self.ensure_one() |
| 124 |
return (self.payment_id + self.operation_ids).filtered( |
| 125 |
lambda x: x.state not in ['draft', 'canceled']).sorted(key=lambda payment: (payment.date, payment.write_date, payment._origin.id))[-1:] |
| 126 |
|
| 127 |
@api.depends('payment_id.state', 'operation_ids.state') |
| 128 |
def _compute_current_journal(self): |
| 129 |
for rec in self: |
| 130 |
last_operation = rec._get_last_operation() |
| 131 |
if not last_operation: |
| 132 |
rec.current_journal_id = False |
| 133 |
continue |
| 134 |
if last_operation.payment_type == 'inbound': |
| 135 |
rec.current_journal_id = last_operation.journal_id |
| 136 |
else: |
| 137 |
rec.current_journal_id = False |
| 138 |
|
| 139 |
def button_open_payment(self): |
| 140 |
self.ensure_one() |
| 141 |
return self.payment_id._get_records_action() |
| 142 |
|
| 143 |
def button_open_check_operations(self): |
| 144 |
''' Redirect the user to the invoice(s) paid by this payment. |
| 145 |
:return: An action on account.move. |
| 146 |
''' |
| 147 |
self.ensure_one() |
| 148 |
operations = ((self.operation_ids + self.payment_id).filtered(lambda x: x.state not in ['draft', 'canceled'])) |
| 149 |
action = { |
| 150 |
'name': _("Check Operations"), |
| 151 |
'type': 'ir.actions.act_window', |
| 152 |
'res_model': 'account.payment', |
| 153 |
'views': [ |
| 154 |
(self.env.ref('l10n_latam_check.view_account_third_party_check_operations_tree').id, 'list'), |
| 155 |
(False, 'form') |
| 156 |
], |
| 157 |
'context': {'create': False}, |
| 158 |
'domain': [('id', 'in', operations.ids)], |
| 159 |
} |
| 160 |
return action |
| 161 |
|
| 162 |
def action_show_reconciled_move(self): |
| 163 |
self.ensure_one() |
| 164 |
move = self._get_reconciled_move() |
| 165 |
return move._get_records_action() |
| 166 |
|
| 167 |
def action_show_journal_entry(self): |
| 168 |
self.ensure_one() |
| 169 |
return self.outstanding_line_id.move_id._get_records_action() |
| 170 |
|
| 171 |
def _get_reconciled_move(self): |
| 172 |
reconciled_line = self.outstanding_line_id.full_reconcile_id.reconciled_line_ids - self.outstanding_line_id |
| 173 |
return (reconciled_line.move_id.line_ids - reconciled_line).mapped('move_id') |
| 174 |
|
| 175 |
@api.constrains('amount') |
| 176 |
def _constrains_min_amount(self): |
| 177 |
min_amount_error = self.filtered(lambda x: x.amount <= 0) |
| 178 |
if min_amount_error: |
| 179 |
raise ValidationError(_('The amount of the check must be greater than 0')) |
| 180 |
|
| 181 |
@api.depends('payment_method_line_id.code', 'payment_id.partner_id') |
| 182 |
def _compute_bank_id(self): |
| 183 |
new_third_party_checks = self.filtered(lambda x: x.payment_method_line_id.code == 'new_third_party_checks') |
| 184 |
for rec in new_third_party_checks: |
| 185 |
rec.bank_id = rec.partner_id.bank_ids[:1].bank_id |
| 186 |
(self - new_third_party_checks).bank_id = False |
| 187 |
|
| 188 |
@api.depends('payment_method_line_id.code', 'payment_id.partner_id') |
| 189 |
def _compute_issuer_vat(self): |
| 190 |
new_third_party_checks = self.filtered(lambda x: x.payment_method_line_id.code == 'new_third_party_checks') |
| 191 |
for rec in new_third_party_checks: |
| 192 |
rec.issuer_vat = rec.payment_id.partner_id.vat |
| 193 |
(self - new_third_party_checks).issuer_vat = False |
| 194 |
|
| 195 |
@api.onchange('issuer_vat') |
| 196 |
def _clean_issuer_vat(self): |
| 197 |
for rec in self.filtered(lambda x: x.issuer_vat and x.company_id.country_id.code): |
| 198 |
stdnum_vat = stdnum.util.get_cc_module(rec.company_id.country_id.code, 'vat') |
| 199 |
if hasattr(stdnum_vat, 'compact'): |
| 200 |
rec.issuer_vat = stdnum_vat.compact(rec.issuer_vat) |
| 201 |
|
| 202 |
@api.constrains('issuer_vat') |
| 203 |
def _check_issuer_vat(self): |
| 204 |
for rec in self.filtered(lambda x: x.issuer_vat and x.company_id.country_id): |
| 205 |
self.env['res.partner']._run_vat_checks(rec.company_id.country_id, rec.issuer_vat, partner_name='Check Issuer VAT') |
| 206 |
|
| 207 |
@api.ondelete(at_uninstall=False) |
| 208 |
def _unlink_if_payment_is_draft(self): |
| 209 |
if any(check.payment_id.state != 'draft' for check in self): |
| 210 |
raise UserError(self.env._("Can't delete a check if payment is In Process!")) |