Hi everyone.
I have a problem creating a custom functionality in Odoo 16. We are implementing a credit system with interests and, to that end, I'm attempting to modify the account.move model (invoices). By overriding the action_register_payment method, I have been able to create another invoice with the interests. However, I have been asked to have that second invoice and its payment conciliated when we create the payment record in the journal for the original invoice with the products.
After some research, I'm seeing that the actual method that creates the payment isn't really in the account.move model; instead, it is in an account.register.payment model, and I'm lost on how to work with that.
Could anyone give me an idea of what should I do?
Thanks in advance.
This is the function I have so far in the account.move model:"
def action_register_payment(self):
# Call the original method to handle normal payment registrations
res = super(AccountMove, self).action_register_payment()
# Fetch the "FoodSys Credit" product
foodsys_credit_product = self.env['product.product'].search([('name', '=', 'FoodSys Credit')], limit=1)
if not foodsys_credit_product:
raise UserError(_('The "FoodSys Credit" product is not found. Please create it first.'))
for move in self:
# Check if there's accrued interest for the sale order associated with the invoice
if move.sale_order_id and move.sale_order_id.interest_accrued > 0:
# Check if the "FoodSys Credit" product is not already in the product lines of the invoice
existing_credit_line = any(line.product_id.id == foodsys_credit_product.id for line in move.invoice_line_ids)
if existing_credit_line:
# If we find the product, we skip processing for this invoice
continue
# Prepare invoice line values using the accrued interest as the quantity
invoice_line_vals = {
'product_id': foodsys_credit_product.id,
'name': 'Interest Accrued',
'quantity': move.sale_order_id.interest_accrued,
'price_unit': 1.0,
'tax_ids': [(5,)]
}
# Create a new invoice with the interest
interest_invoice_vals = {
'partner_id': move.partner_id.id,
'invoice_date': fields.Date.today(),
'move_type': 'out_invoice',
'payment_reference': f'Factura de intereses de orden {move.sale_order_id.name}',
'invoice_line_ids': [(0, 0, invoice_line_vals)]
}
interest_invoice = self.env['account.move'].create(interest_invoice_vals)
interest_invoice.action_post()
# Link the interest invoice to the sale order
move.sale_order_id.interest_invoice_id = interest_invoice.id
return res