Skip to Content
Menú
This question has been flagged
2 Respostes
896 Vistes

Is there a way in Odoo 18 to stop processing a sales order when the customer exceeds their credit limit?  I'd like a workflow to notify Credit Control that the order is being held until they approve the excess credit.  Thanks.

Avatar
Descartar
Autor

Thanks for the quick reply, Rani.  I'll give it a try.

Autor Best Answer

Wonderful!  Thanks for the help!

Avatar
Descartar
Best Answer

Hi,

Try the below steps,


1-On each sale order, you already have a field partner_credit_warning (boolean or selection) that indicates whether the partner has exceeded their credit limit.

2- When the user clicks Confirm on a sale order:

          - If partner_credit_warning is False → the order confirms as usual.

          - If partner_credit_warning is True → instead of confirming, Odoo will automatically create an Approval Request in the Approvals app (targeted to Credit Control team).

3- Credit Control reviews the approval.

4- If approved → the SO is confirmed. If rejected → the SO stays unconfirmed.


Sample code:

from odoo import models, api



class SaleOrder(models.Model):

    _inherit = "sale.order"


    @api.multi

    def action_confirm(self):

        for order in self:

            if order.partner_credit_warning:

                # Create an approval request

                approval_category = self.env.ref('approvals.approval_category_credit_limit', raise_if_not_found=False)

                approval_vals = {

                    'name': f"Credit approval for SO{order.name}",

                    'request_owner_id': self.env.user.id,

                    'category_id': approval_category.id if approval_category else False,

                    'reason': f"Customer {order.partner_id.name} exceeded credit limit. "

                              f"Order total: {order.amount_total}.",

                    'sale_order_id': order.id,   # link back for traceability

                }

                self.env['approval.request'].create(approval_vals)


                # Put order in waiting state instead of confirming

                order.state = 'waiting_credit_approval'

            else:

                super(SaleOrder, order).action_confirm()


* Use the existing partner_credit_warning field to flag over-limit customers.

* Override action_confirm() so if the flag is set, the SO does not confirm but instead creates an Approval Request.

*Put the SO in a custom state (waiting_credit_approval) until Credit Control approves.

* With Approvals module + custom category, Credit Control can either approve (SO continues) or reject (SO stays blocked).


Hope it helps

Avatar
Descartar