how to override the delete button in one2many from sale order

1.6k Views Asked by At

in one2many line delete button is there when a boolean field is true in that line the line will not delete and raise an exception. i do like the below code

but i will not effected in code. there is no any response the line deleted. please.... tell me any one

thanks in advance....

from odoo import api, models, fields, _, SUPERUSER_ID
import string
from odoo import exceptions
from odoo.exceptions import ValidationError


class ouc_sale_order(models.Model):
    _inherit = 'sale.order'
    c_state = fields.Selection(
        [("draft", "Draft Proforma Invoice"), ("sent", "Proforma Invoice Sent"), ("cancel", "Cancelled"),
         ("waiting_date", "Waiting Schedule"), ("progress", "Sales Order"), ("manual", "Sale to Invoice"),
         ("shipping_except", "Shipping Exception"), ("invoice_except", "Invoice Exception"), ("done", "Done")],
        string='State')


class ouc_sales_order_line(models.Model):
    _inherit = 'sale.order.line'

    c_status = fields.Selection([('New', 'New'), ('renewal', 'Renewal'), ('upgrade', 'Upgrade'), ('upsell', 'Upsell'), ('PDC', 'PDC'),
         ('etc', 'etc')], string='Status', default='New')
    c_fptags_id = fields.Many2one('ouc.fptag', string='FPTAGs')
    c_product_template_id = fields.Many2one('product.template', string='Product Template', related='product_id.product_tmpl_id')
    c_package_id = fields.Many2one('ouc.package', string='Packages')
    c_pkg_expiry_in_month = fields.Integer(string='Package Expires After(Months)', related='c_package_id.validity')
    c_subtotaltax = fields.Float('Subtotal with Tax')
    c_taxamount = fields.Float('Tax Amount')
    c_client_id = fields.Char('Client Id')
    c_default_discount = fields.Float('Default Discount (%)')
    c_max_discount = fields.Float('Maximum Discount (%)')
    c_subscription_status = fields.Boolean(string='Subscription')


#@api.multi
#def unlink(self):
#    for record in self:
#        if record.c_subscription_status:
#            raise exceptions.ValidationError(_('You didn\'t delete this record'))
#    return super(ouc_sales_order_line,self).unlink()
1

There are 1 best solutions below

0
On

This code of your is working!!!

Just done little correction > Added default=False in field.(not necessary but good practice!)

class ouc_sales_order_line(models.Model):
    _inherit = 'sale.order.line'

    c_subscription_status = fields.Boolean(string='Subscription', default=False)

    @api.multi
    def unlink(self):
       for record in self:
           if record.c_subscription_status:
               raise ValidationError(_('You didn\'t delete this record'))
       return super(ouc_sales_order_line, self).unlink()

Get order in which your boolean field is set TRUE in order_line delete record and > save > it will raise validation error you placed!