Help

0

Detect debtor customer when creating a new invoice

Avatar
Tomas Cordoba

I would like to get an "alert" or something when I'm creating a new invoice. After selecting a customer I would like to alert me if the selected customer has some unpaid invoices. 

There is any module already developed that includes this functionality? 


1 Comment
Avatar
Discard

I didn't see module with this functionality but it's easy to do, you can add boolean field and on partner field change you can check if the customer has unpaid invoice then set this flag to true and then from form view you use an alert like the one shown when the customer has outstanding payment and use the Boolean flag to show/hide this alert.

The below alert for outstanding payment:
<div groups="account.group_account_invoice,account.group_account_readonly" class="alert alert-info" role="alert" style="margin-bottom:0px;" attrs="{'invisible': ['|', '|', ('move_type', 'not in', ('out_invoice', 'out_refund')), ('invoice_has_outstanding', '=', False), ('payment_state', 'not in', ('not_paid', 'partial'))]}">
You have <bold><a class="alert-link" href="#outstanding" role="button">outstanding payments</a></bold> for this customer. You can allocate them to mark this invoice as paid.
</div>

1 Answer
0
Avatar
Tomas Cordoba
Best Answer

I was looking for a more complete answer. This is what i did: I've created a custom module, inside i have created two files, one for extending the "account.move" class and the other to extend the view "account.view_move_form"

models/accunt_move.py

This files add a new attribute to the class account.move, its computed value, and watches for changes on the partner selector (partner_id), so every time the value is changed the function is executed

from odoo import api, fields, models, _


class AccountMove(models.Model):
_inherit = "account.move"

debtor_warning = fields.Boolean(default=False, compute='_onchange_debtor_warning', store=False)

@api.onchange('partner_id')
def _onchange_debtor_warning(self):
if self.partner_id:
unpaid_invoices = self.env['account.move'].search_count([('partner_id','=', self.partner_id.id),('payment_state','=','not_paid')])
self.debtor_warning = (unpaid_invoices > 0)

views/account_move.xml

This file adds the computed field (in invisible mode) and add some HTML tags for the alert message




Account Move Inherit
account.move




class="alert alert-danger"
style="height:40px; margin: 0 auto; margin-top: 16px; width: 90%!important;"
attrs="{'invisible':[('debtor_warning', '=', False)]}">
This customer has unpaid invoices!





Hope it helps someone!
Bye

Avatar
Discard