Salim R
Salim R

Reputation: 343

odoo 8 - Sales - add constraint on "Confirm Sales" button

I need to forbid the user from confirming a sales order (action occurred when clicking on confirm sale button: Sales-> Sales Order-> choose an order). So I override the action that confirm the Sales, and add a condition which raise a validation Error, so the super wont be called in case an invalid condition(e.g. order line with product without names).

But the issue occured in S.O. - Edit mode and when clicking on confirm SO an insert to the database(insert in sale_order_line) is executed before executing the following overwritten function.

# the inherited class
class sales_warning(models.Model):
    _inherit = "sale.order"

    @api.multi   
    def action_button_confirm(self):
        print "begin overwritten action_button_confirm()"
        if(ForbidCondition==True):
            raise ValidationError("You cannot confirm a S.O...")
        else:
            res = super(sales_warning, self).action_button_confirm()        
            return res

Logs showing the write operation before invoking the overwritten function:

*crm werkzeug: .. "POST /web/dataset/call_kw/sale.order/write HTTP/1.1" 200

begin overwritten action_button_confirm()*

Question: is there a way to add a constraint the will be launched before any other action such as the write above, or a way to add pre-called function that checked every order line if it has a valid item name?

Upvotes: 0

Views: 854

Answers (2)

Salim R
Salim R

Reputation: 343

Since the error (i.e. calling the write function when confirming the SO in edit mode) occurs only in Edit mode, note that in read only mode and when saving the SO the write wont be called (seems Odoo consider nothing to be updated in Sales order line-read only mode), so I choose to hide the confirm button in edit mode by adding oe_read_only css class to the button:

<xpath expr="//button[@name='action_button_confirm']" position="attributes">           

      <attribute name="class">oe_read_only</attribute>             

 </xpath>

In Brief:

-SO (Sale order) in read only mode: confirm button is visible and Won't do an update (update sale_order_line) to DB once it's clicked, only the action action_button_confirm is called

-SO in Edit mode: Hide the button of confirmation in order to prevent updating the sale_order_line

Upvotes: 1

thangtn
thangtn

Reputation: 876

In Odoo, all button actions will be called after write method natively. If you want to add constrains, use '@api.constrains' to raise error popup when the user click on any button

Upvotes: 0

Related Questions