# -*- coding: utf-8 -*- from odoo import models, fields, api, _ class AccountBatchPayment(models.Model): _inherit = "account.batch.payment" def generate_payments_from_lines(self): """Override to transfer deduction fields to generated payments""" self.ensure_one() payment_ids = [] # First, try to use the journal's available payment methods available_payment_methods = self.journal_id._get_available_payment_method_lines(self.batch_type) payment_method_line = available_payment_methods.filtered(lambda x: x.code == 'direct_batch') if not payment_method_line: # If no direct batch payment method found, use the first available payment method if available_payment_methods: payment_method_line = available_payment_methods[0] else: # Fallback: try to find or create a direct batch payment method line payment_method_line = self.env['account.payment.method.line'].search([ ('payment_method_id.code', '=', 'direct_batch'), ('journal_id', '=', self.journal_id.id) ], limit=1) if not payment_method_line: # Try to create the payment method line if it doesn't exist payment_method = self.env['account.payment.method'].search([ ('code', '=', 'direct_batch'), ('payment_type', '=', self.batch_type) ], limit=1) if payment_method: # Check if a payment method line already exists for this journal and method existing_pml = self.env['account.payment.method.line'].search([ ('payment_method_id', '=', payment_method.id), ('journal_id', '=', self.journal_id.id) ], limit=1) if not existing_pml: payment_method_line = self.env['account.payment.method.line'].create({ 'name': payment_method.name, 'payment_method_id': payment_method.id, 'journal_id': self.journal_id, }) else: payment_method_line = existing_pml else: from odoo.exceptions import ValidationError raise ValidationError(_("No payment method found for this journal.")) # Check that all lines have a partner for line in self.direct_payment_line_ids: if not line.partner_id: from odoo.exceptions import ValidationError raise ValidationError(_("All payment lines must have a partner selected.")) for line in self.direct_payment_line_ids: # Determine payment type and partner type based on batch type payment_type = self.batch_type partner_type = 'customer' if self.batch_type == 'inbound' else 'supplier' # Create the payment with deduction fields payment_vals = { 'payment_type': payment_type, 'partner_type': partner_type, 'partner_id': line.partner_id.id, 'amount': line.amount, 'currency_id': line.currency_id.id, 'date': line.date, 'journal_id': self.journal_id.id, 'payment_method_line_id': payment_method_line.id, 'ref': line.memo, 'expense_account_id': line.expense_account_id.id if line.expense_account_id else False, 'amount_substract': line.amount_substract, 'substract_account_id': line.substract_account_id.id if line.substract_account_id else False, } payment = self.env['account.payment'].create(payment_vals) payment.action_post() # Link the payment to the line line.payment_id = payment.id payment_ids.append(payment.id) # Add the generated payments to the batch if payment_ids: self.write({ 'payment_ids': [(4, payment_id) for payment_id in payment_ids] }) # Automatically validate the batch payment after generating payments if self.state == 'draft': try: self.validate_batch() except Exception as e: # If validation fails, log the error but don't prevent payment creation import logging _logger = logging.getLogger(__name__) _logger.warning(f"Failed to automatically validate batch payment {self.id}: {str(e)}") class AccountBatchPaymentLine(models.Model): _inherit = "account.batch.payment.line" amount_substract = fields.Monetary( string='Substract Amount', currency_field='currency_id', help='Amount to be deducted from the payment (e.g., withholding tax, fees)', ) substract_account_id = fields.Many2one( 'account.account', string='Substract Account', domain="[('account_type', 'not in', ['asset_cash', 'asset_cash_bank']), ('deprecated', '=', False)]", help='Account where the deduction will be recorded', )