This customization is designed for Odoo V15.X with Studio App
⚠️ Constraints: the following Studio customization works as expected in single currency databases, within Odoo Sales module and assumes ALL clients have a credit line. It has unexpected behaviours in other scenarios. There, additional logic might be required.
Display useful fields in Contacts
In the model res.partner two required fields are available but hidden to the user interface by default. Open the form view with Studio and bring them to the interface:
- Credit limit (credit_limit)
- Total Receivable (credit)
⚠️ Warning: a credit limit above 0 is required for this solution to work
Display credit info in Sale Order
- Create related fields in sale.order model using the customer field for the relation with res.partner:
- Credit limit (x_studio_credit_limit) from Customer > Credit limit
- Total Receivable (x_studio_credit) from Customer > Total Receivable.
Bonus: set monetary widget on these two related fields
Credit consumption with current sale order
In order to configure a blocking feature, a basic logic is constructed through a credit usage ratio. If ratio is above 100% (1.000) the credit is surpassed by current transaction. Without the two related fields created in sales order, this calculation is not possible due to technicalities of Odoo.
- Create “Consumed (%)” field (x_studio_ratio, type=float, widget=progressbar) in sale.order model
- Create Automated action (Settings / Technical / Automation / Automated Actions)
-
Model: sale.order
-
Trigger: On creation & Update
-
Data to write
- Field: Credit usage
- Evaluation Type: Python expression (in this specific configuration does not create billable lines of code)
- Value: the following python expression will sum both Total Receivable and the current transaction subtotal (tax excluded)
-
(record.x_studio_credit+record.amount_untaxed)/record.x_studio_credit_limit
Bonus: use amount_total instead of amount_untaxed if taxes are included in the calculation. If progress bar widget is used, multiple the whole expression by 100 in order to get a percentage (1 to 100) visualization.
⚠️ Warning: if credit amount is not set on the customer contact, Odoo might encounter errors (division by zero) when creating a sales order.
This automation works when the sale order is created or updated (in any field).
Blocking feature and authorized approvers
This blocking feature has the intention to avoid the order confirmation when the credit limit is surpassed.
⚠️ Constraints: this feature only works for total receivable amount. If there are uninvoiced sales order (other than the current transaction), the amount is not considered as part of the used credit.
- Create approval user group (Settings / Users & Companies / Groups)
- Export this user group in order to be used in Studio Approvals
- In sale order form view, enable Studio and select Confirm button
- Set approval for the approval group with the following condition rule: Credit usage > 1
Bonus: if progress bar widget is chosen, use 100 instead of 1
Proof of Concept
The following scenario is below credit limit. Thus, the user can confirm the sales order.
The following scenario is above credit limit. Thus, the user cannot confirm the sales order and manager approval (defined by user group) is required.
I find your implementation very convincing (even if I wouldn't do it with Studio, that's forbidden in our project - for good reason - with maximum penalty).
I just wonder why Odoo itself does not come up with such ideas. What I can see at the moment from the credit limit check in V.16 is completely inadequate in my opinion.