vendor_payment_diff_amount/models/payment_deduction_line.py

80 lines
2.8 KiB
Python

# -*- coding: utf-8 -*-
from odoo import models, fields, api, _
from odoo.exceptions import ValidationError
class PaymentDeductionLine(models.Model):
_name = 'payment.deduction.line'
_description = 'Payment Deduction Line'
_order = 'sequence, id'
sequence = fields.Integer(string='Sequence', default=10)
payment_id = fields.Many2one(
'account.payment',
string='Payment',
ondelete='cascade',
index=True,
)
batch_payment_line_id = fields.Many2one(
'account.batch.payment.line',
string='Batch Payment Line',
ondelete='cascade',
index=True,
)
company_id = fields.Many2one(
'res.company',
compute='_compute_company_currency',
store=True,
readonly=True,
)
currency_id = fields.Many2one(
'res.currency',
compute='_compute_company_currency',
store=True,
readonly=True,
)
@api.depends('payment_id.company_id', 'payment_id.currency_id',
'batch_payment_line_id.currency_id')
def _compute_company_currency(self):
for line in self:
if line.payment_id:
line.company_id = line.payment_id.company_id
line.currency_id = line.payment_id.currency_id
elif line.batch_payment_line_id:
line.company_id = line.batch_payment_line_id.batch_payment_id.journal_id.company_id
line.currency_id = line.batch_payment_line_id.currency_id
else:
line.company_id = self.env.company
line.currency_id = self.env.company.currency_id
amount_substract = fields.Monetary(
string='Deduction Amount',
currency_field='currency_id',
required=True,
help='Amount to be deducted from the payment (e.g., withholding tax, fees)',
)
substract_account_id = fields.Many2one(
'account.account',
string='Deduction Account',
required=True,
domain="[('account_type', 'not in', ['asset_cash', 'asset_cash_bank', 'asset_receivable', 'liability_payable']), ('deprecated', '=', False)]",
help='Account where the deduction will be recorded (use tax payable or expense accounts, NOT payable/receivable accounts)',
)
name = fields.Char(
string='Description',
help='Optional description for this deduction',
)
@api.constrains('amount_substract')
def _check_amount_substract(self):
for line in self:
if line.amount_substract <= 0:
raise ValidationError(_("Deduction amount must be greater than zero."))
@api.onchange('substract_account_id')
def _onchange_substract_account_id(self):
"""Auto-fill description based on account name if not set"""
if self.substract_account_id and not self.name:
self.name = self.substract_account_id.name