# -*- coding: utf-8 -*-

from odoo import models, fields, api
import time
from odoo.exceptions import UserError,ValidationError
from datetime import datetime
class sos__mon(models.Model):
    _name = 'sos_mon'
    _description = 'Order Note'
    _rec_name = 'mon_no'
    _order = 'id desc' 

    mon_no = fields.Char(string="MON/SON/FON No", readonly= True, required= True, default="SOS/MON/001")
    mon_date = fields.Date(string="MON/SON/FON Date", required=True, default=fields.Date.today)
    min_no = fields.Many2one('sos_min',string="MIN/SIN/FIN No")
    filled_by = fields.Many2one('res.users', string='Filled By', readonly=True,required=True,default=lambda self: self.env.user)
    logged_inuser_group=fields.Boolean(string='Group Name',compute='compute_user_grp',store=True)
    dept = fields.Many2one('sos_departments',string="Department")
    purpose = fields.Char(string="Purpose")
    product_name = fields.Many2one('sos_fg', string='Material/Product Name & No')
    indent_ref_no = fields.Many2one('sos_fg_plan',string="Indent Reference No")
    prepared_by_name = fields.Many2one('res.users', string='Prepared by')
    prepared_by_image = fields.Image(related="prepared_by_name.signature_image",string='Prepared by Sign',readonly=True)
    prepared_on = fields.Datetime(string="Approved On")
    po_no=fields.Char(string="PO No")
    service_call_ref_no = fields.Many2one('sos_service_call_log_report',string="Service Call Ref No")
    reporting_to = fields.Many2one('res.users',related="prepared_by_name.reporting_to", string='Prepared by')
    stores_remarks = fields.Text(string="Remarks")
    dept_in_charge_name = fields.Many2one('res.users', string='Department In-Charge')
    dept_in_charge_image = fields.Image(related="dept_in_charge_name.signature_image",string='Department In-Charge Sign',readonly=True)
    dept_in_charge_approved_on = fields.Datetime(string="Approved On")
    dept_in_charge_reject_remarks = fields.Text(string="Reject Reason")
    top_management_name = fields.Many2one('res.users', string='Top Management Approver')
    top_management_approval_image = fields.Image(related="top_management_name.signature_image",string='Top Management Approval',readonly=True)
    top_management_approved_on = fields.Datetime(string="Approved On")
    
    stores_approved_by  = fields.Many2one('res.users', string='Stores Approved By')
    stores_approved_image = fields.Image(related="stores_approved_by.signature_image",string='Stores Approval Sign',readonly=True)
    stores_approved_on = fields.Datetime(string="Approved On")

    auto_load = fields.Selection([
        ('sfg', 'SFG'),
        ('fg', 'FG')
    ], string='Auto Load Items' ,default=False)
    material_option = fields.Boolean('Materials', default=True)
    sfg_option = fields.Boolean('Semi-Finished Goods')
    fg_option = fields.Boolean('Finished Goods') 
    order_type = fields.Char(string='order_type',copy=True)
    line_ids_material = fields.One2many('sos_mon_line_material', 'mon_id', string="Materials",copy=True)
    line_ids_sfg = fields.One2many('sos_mon_line_sfg', 'mon_id', string="Semi-Finished Goods",copy=True)
    line_ids_fg = fields.One2many('sos_mon_line_fg', 'mon_id', string="Finished Goods",copy=True)
    auto_load_sfg_items = fields.Many2one('sos_sfg', string='SFG Items')
    set_qty = fields.Integer(string="Set Quantity",default=1)
    fg_set_qty = fields.Integer(string="Set Quantity",default=1)
    auto_load_fg_items = fields.Many2one(
    'sos_fg',
    string='FG Items',
    domain="[('id', 'in', auto_load_fg_item_ids)]"
    )

    auto_load_fg_item_ids = fields.Many2many(
        'sos_fg',
        compute='_compute_fg_items',
        store=True
    )
    company_id = fields.Many2one('res.company', store=True, copy=False,
                                string="Company",
                                default=lambda self: self.env.user.company_id.id)
    currency_id = fields.Many2one('res.currency', string="Currency",
                                 related='company_id.currency_id',
                                 default=lambda
                                 self: self.env.user.company_id.currency_id.id)
    approx_value = fields.Monetary(compute='_compute_approx_value', string="Approximate Value", currency_field='currency_id', readonly=True,store=True)
    status = fields.Selection([ ('open', 'Open'),('close', 'Closed')], default='open' , string="Status")
    active = fields.Boolean(default=True)
    @api.constrains('indent_ref_no', 'auto_load_fg_items')
    def _check_duplicate_fg_items_per_indent(self):
        for record in self:
            if record.indent_ref_no and record.auto_load_fg_items:
                # Search for duplicates (excluding the current record)
                duplicate = self.search([
                    ('id', '!=', record.id),
                    ('indent_ref_no', '=', record.indent_ref_no.id),
                    ('auto_load_fg_items', '=', record.auto_load_fg_items.id)
                ], limit=1)

                if duplicate:
                    raise ValidationError(
                        f"Order Note for '{record.auto_load_fg_items.name}' for Indent '{record.indent_ref_no.plan_ref_no}' is already taken."
                    )
    @api.depends('indent_ref_no')
    def _compute_fg_items(self):
        for record in self:
            if record.indent_ref_no:
                record.auto_load_fg_item_ids = record.indent_ref_no.line_ids.mapped('fg_name')
            else:
                record.auto_load_fg_item_ids = self.env['sos_fg'].search([])



    @api.depends('filled_by')
    def compute_user_grp(self):
        target_groups = {'SCG User', 'Top Management User', 'SCG Manager', 'Production User'}
        for record in self:
            user_group_names = set(record.filled_by.groups_id.mapped('name'))
            record.logged_inuser_group = bool(target_groups & user_group_names)


    @api.onchange('mon_date')
    def _onchange_mon_date(self):
        if self.mon_date:
            min_date = datetime.strptime('2025-03-31', '%Y-%m-%d').date()
            if self.mon_date < min_date:
                return {
                    'warning': {
                        'title': "Invalid Date",
                        'message': "The Order Note Date cannot be before March 31, 2025. Please select a valid date.",
                    },
                    'value': {
                        'mon_date': False,  # Reset the field to empty
                    },
                }
    def unlink(self):
        for record in self:
            min_record = self.env['sos_min'].search([('mon_no', '=', record.id)], limit=1)
            if min_record:
                min_record.unlink()
        return super().unlink()
    @api.depends('line_ids_material.total_cost')
    def _compute_approx_value(self):
        for record in self:
            record.approx_value = sum(line.total_cost for line in record.line_ids_material)
    def action_bulk_upload(self):
        return {
            'type': 'ir.actions.act_window',
            'name': 'Order Note Bulk Upload',
            'res_model': 'mon_bulk_upload_wizard',
            'view_mode': 'form',
            'target': 'new',
            'context': {
            'default_mon_id': self.id,
            'from_button': True
        }
        }
    @api.onchange('fg_set_qty')
    def _onchange_fg_set_qty(self):
        if self.fg_set_qty:
            for line in self.line_ids_material:
                line.quantity = line.quantity * self.fg_set_qty
            for line in self.line_ids_sfg:
                line.quantity = line.quantity * self.fg_set_qty

    @api.onchange('set_qty')
    def _onchange_set_qty(self):
        if self.auto_load_sfg_items:
            self.material_option = True
            self.sfg_option = False
            self.fg_option = False
            self.line_ids_material = [(5, 0, 0)]
            sfg_record = self.env['sos_sfg_bom'].search([('name', '=', self.auto_load_sfg_items.name)], limit=1)
            if sfg_record:
                sfg_materials = self.env['sos_sfg_bom_line'].search([('bom_id', '=', sfg_record.id)])
                material_lines = []
                for material in sfg_materials:
                    line_vals = {
                        'mon_id': self.id,
                        'component_id': material.primary_component_id.id,
                        'qp_no': material.primary_component_id.qp_no,
                        'uom': material.primary_component_id.uom,
                        'quantity': material.quantity * self.set_qty,
                        'location': material.primary_component_id.location,
                    }
                    material_lines.append((0, 0, line_vals))

                self.line_ids_material = material_lines

    @api.onchange('auto_load')
    def _onchange_auto_load(self):
        if self.auto_load == 'sfg':
            self.auto_load_fg_items = False  # Clear FG field
            domain = [('id', 'in', self.env['sos_sfg'].search([]).ids)]
            return {'domain': {'auto_load_sfg_items': domain}}
        elif self.auto_load == 'fg':
            self.auto_load_sfg_items = False  # Clear SFG field
            domain = [('id', 'in', self.env['sos_fg'].search([]).ids)]
            return {'domain': {'auto_load_fg_items': domain}}
        else:
            return {'domain': {'auto_load_sfg_items': [], 'auto_load_fg_items': []}}

    @api.onchange('auto_load_fg_items')
    def _onchange_auto_load_fg_items(self):
        if not self.auto_load_fg_items:
            return
        if self.auto_load_fg_items:
            self.material_option = True
            self.sfg_option = True
            self.fg_option = False
            self.line_ids_material = [(5, 0, 0)]
            self.line_ids_sfg = [(5, 0, 0)]
            fg_record = self.env['sos_fg_bom'].search([('fg_name', '=', self.auto_load_fg_items.id),('is_primary', '=',True)], limit=1)
            if fg_record:
                sfg_lines = []
                for line_id in fg_record.fg_bom_line_ids:
                    line_vals = {
                        'mon_id': self.id,
                        'component_id': line_id.sfg_bom_id.name.id,
                        'qp_no': line_id.sfg_bom_id.name.qp_no,
                        'uom': line_id.sfg_bom_id.name.uom,
                        'quantity': line_id.quantity,
                        'location': line_id.sfg_bom_id.name.location,
                    }
                    sfg_lines.append((0, 0, line_vals))
                self.line_ids_sfg = sfg_lines
                material_lines = []
                for material in fg_record.sfg_bom_line_ids:
                    line_vals = {
                        'mon_id': self.id,
                        'component_id': material.primary_component_id.id,
                        'qp_no': material.primary_component_id.qp_no,
                        'uom': material.primary_component_id.uom,
                        'quantity': material.quantity,
                        'location': material.primary_component_id.location,
                    }
                    material_lines.append((0, 0, line_vals))

                self.line_ids_material = material_lines
            else:
                raise UserError("BOM Not Found")
    @api.onchange('auto_load_sfg_items')
    def _onchange_auto_load_sfg_items(self):
        if self.auto_load_sfg_items:
            self.material_option = True
            self.sfg_option = False
            self.fg_option = False
            self.line_ids_material = [(5, 0, 0)]
            sfg_record = self.env['sos_sfg_bom'].sudo().search([('name', '=', self.auto_load_sfg_items.name)], limit=1)
            if sfg_record:
                sfg_materials = self.env['sos_sfg_bom_line'].sudo().search([('bom_id', '=', sfg_record.id)])
                material_lines = []
                for material in sfg_materials:
                    line_vals = {
                        'mon_id': self.id,
                        'component_id': material.primary_component_id.id,
                        'qp_no': material.primary_component_id.qp_no,
                        'uom': material.primary_component_id.uom,
                        'quantity': material.quantity,
                        'location': material.primary_component_id.location,
                    }
                    material_lines.append((0, 0, line_vals))

                self.line_ids_material = material_lines
            else:
                raise UserError("No matching BOM found for the selected SFG item")

    @api.model
    def write(self, vals):
        record = super(sos__mon, self).write(vals)
        s_no = 0
        for line in self.line_ids_material:
            s_no += 1
            line.s_no = s_no
        return record
    @api.model
    def create_min(self):
        s_no = 0
        for line in self.line_ids_material:
            s_no += 1
            line.s_no = s_no
        order_types = self.order_type.split(',') if self.order_type else []
        # Create an associated MIN record
        sequence_util = self.env['sos_common_scripts']
        min_no = sequence_util.generate_sequence('sos_min', 'MIN', 'min_no')
        min_model = self.env['sos_min']
        min_record = min_model.create({
            'min_no': min_no,
            'mon_no': self.id, 
            'mon_date': self.mon_date,
            'min_date': self.mon_date,
            'order_type': self.order_type,
            'material_option': self.material_option,
            'sfg_option': self.sfg_option,
            'fg_option': self.fg_option,
            'mon_created_by': self.env.user.id,
            'reporting_to':self.filled_by.reporting_to.id,
            'ref_no':self.purpose
        })
        self.min_no = min_record.id
        for order_type in order_types:
            line_model_mapping = {
        "material": 'sos_min_line_material',
        "sfg": 'sos_min_line_sfg',
        "fg": 'sos_min_line_fg'
    }
          
            order_type_key = order_type.strip().lower()

            min_line_model_name = line_model_mapping.get(order_type_key)
            if not min_line_model_name:
                continue

            min_line_model = self.env[min_line_model_name]

            line_ids_field = f"line_ids_{order_type_key}"
            line_ids = getattr(self, line_ids_field, False)

            if not line_ids:
                continue  # Skip if no lines are available
            for item in line_ids:
                try:
                    if item.component_id.id:
                        if min_line_model_name == "sos_min_line_material":
                            create_vals = {
                                'min_id': min_record.id,
                                'component_id': item.component_id.id,
                                'material_code': item.component_id.material_code,
                                'quantity': item.quantity, 
                                'qp_no': item.qp_no,
                                'com_type':'exits'
                            }
                        else:
                            create_vals = {
                                'min_id': min_record.id,
                                'component_id': item.component_id.id,
                                'quantity': item.quantity, 
                                'qp_no': item.qp_no,
                                'com_type':'exits'
                            }
                    else:
                        create_vals = {
                            'min_id': min_record.id,
                            'new_component_id': item.new_component_id,
                            'quantity': item.quantity, 
                            'qp_no': item.qp_no,
                            'com_type':'new'
                        }
                    new_line = min_line_model.with_context(from_script=True).create(create_vals)
                except Exception as e:
                    print("Failed to create line record for %s: %s", min_line_model, str(e))




    @api.onchange('material_option', 'sfg_option', 'fg_option')
    def _onchange_material_option(self):
        options = []
        if self.material_option:
            options.append("Material")
        if self.sfg_option:
            options.append("SFG")
        if self.fg_option:
            options.append("FG")
        
        self.order_type = ",".join(options)                                         

    def _generate_id(self):
        sequence_util = self.env['sos_common_scripts']
        return sequence_util.generate_sequence('sos_mon','MON', 'mon_no')

    def action_report_mon_btn(self):
        try:
            action = self.env.ref("sos_inventory.action_report_mon").report_action(self)
            return action
        except ValueError as e:
            print(f"Failed to find report action: {e}")
    def action_report_mon_items_btn(self):
        try:
            action = self.env.ref("sos_inventory.action_report_mon_items_btn").report_action(self)
            return action
        except ValueError as e:
            print(f"Failed to find report action: {e}")

    def action_stores_esign_btn(self):
        sequence_util = self.env['sos_common_scripts']
        sequence_util.action_assign_signature(
        self, 
        'stores_approved_by',
        'stores_approved_on',
        'sos_inventory.sos_scg_group_user'
        )
    def action_report_esign_btn(self):
        self.mon_no = self._generate_id()
        self.env['sos_audit_log'].create_log('Request', 'Order Note Prepared')
        # Email part
        body_html = f"""
        <p>Below <b>Order Note</b> is waiting for your Approval</p>
        """
        sequence_util = self.env['sos_common_scripts']
        sequence_util.send_mon_min_email(self.env,'sos_mon',self.id,"deenalaura.m@sosaley.in","Order Note Approval Request",body_html,"user")
        # Email part ends
        self.create_min()
        return sequence_util.action_assign_signature(
        self, 
        'prepared_by_name',
        'prepared_on'
    )
    def action_departincharge_reject(self):
        # Email part
        body_html = f"""
            <p>Below <b>Order Note</b> was rejected with remarks: <b>{self.dept_in_charge_reject_remarks}</b></p>
        """
        send_email = self.env['sos_common_scripts']
        send_email.send_direct_email(self.env,"sos_mon",self.id,self.prepared_by_name.login,"Order Note Rejected",body_html)
        return {
                    'type': 'ir.actions.client',
                    'tag': 'display_notification',
                    'params': {
                        'message': "Rejected Successfully",
                        'type': 'success',
                        'sticky': False
                    }
                }
        # Email part ends
    def action_report_esign_btn1(self):
        sequence_util = self.env['sos_common_scripts']
        sequence_util.action_assign_signature(
        self, 
        'dept_in_charge_name',
        'dept_in_charge_approved_on'
        )
        # Email part
        body_html = f"""
        <p>Below <b>Order Note</b> is waiting for your Approval</p>
        """
        subject = f"Order Note Approval Request - {self.mon_no}"
        send_email = self.env['sos_common_scripts']
        send_email.send_mon_min_email(self.env,'sos_mon',self.id,"deenalaura.m@sosaley.in",subject,body_html,"topmanagement")
        # Email part ends
        

    def action_report_esign_btn2(self):
        sequence_util = self.env['sos_common_scripts']
        sequence_util.action_assign_signature(
        self, 
        'top_management_name',
        'top_management_approved_on',
        'sos_inventory.sos_management_user'
        )
        # Email part
        body_html = f"""
        <p>Below <b>Order Note</b> is waiting for your update</p>
        """ 
        send_email = self.env['sos_common_scripts']
        send_email.send_group_email(self.env,'sos_mon',self.id,"deenalaura.m@sosaley.in","Order Note Approval Request",body_html,'sos_inventory.sos_scg_group_user')
        # Email part ends
        


class Mon_Line_Material(models.Model):
    _name = 'sos_mon_line_material'
    _description = 'Material Order Lines'

    s_no = fields.Integer(string="S.No",readonly=True,default=1)
    mon_id = fields.Many2one('sos_mon', string="Materials", ondelete="cascade")
    com_type = fields.Selection([('exits', 'In-stock'),('new', 'New')], string="Availability", default='exits')
    value = fields.Char(string="value")
    component_id = fields.Many2one('sos_material', string="Part No")
    inhand_stock_qty=fields.Float(string="Inhand Qty",related="component_id.inhand_stock_qty")
    new_component_id = fields.Char(string="New Part No")
    qp_no = fields.Char(related='component_id.qp_no',string="QP No")
    material_code = fields.Char(related='component_id.material_code',string="Material Code")
    uom = fields.Selection([('meters', 'Meters'),('Nos', 'Nos'),('coils', 'Coils'), ('litre', 'litre'), ('kg', 'Kilogram'), ('Packs', 'Packs')], string="Uom")
    specifications = fields.Char(string="Specifications")
    quantity = fields.Float(string="Quantity",required=True,default=1)
    location = fields.Char(string="Location")
    company_id = fields.Many2one('res.company', store=True, copy=False,
                                string="Company",
                                default=lambda self: self.env.user.company_id.id)
    currency_id = fields.Many2one('res.currency', string="Currency",
                                 related='company_id.currency_id',
                                 default=lambda
                                 self: self.env.user.company_id.currency_id.id)
    approx_price = fields.Monetary(string="Approx Unit Price", compute='_compute_unit_price', store=True, readonly=False)
    total_cost = fields.Monetary(compute="_compute_total_cost", currency_field='currency_id')
    helper_field = fields.Many2many('sos_material', string="Helper Field")
    @api.onchange('value')
    def _onchange_value(self):
        if self.value:
            self.helper_field = self.env['sos_material'].search([('description', 'like', self.value)])
        else:
            self.helper_field = self.env['sos_material'].search([])

    @api.onchange('com_type')
    def _onchange_com_type(self):
        if self.com_type == 'new':
            self.component_id = False
            self.approx_price = 0.00
        else:
            self.new_component_id = ''
            self.approx_price = 0.00
    @api.depends('component_id.unit_price')
    def _compute_unit_price(self):
        for record in self:
            record.approx_price = record.component_id.unit_price

    @api.depends('approx_price', 'quantity')
    def _compute_total_cost(self):
        for record in self:
            record.total_cost = record.approx_price * record.quantity
    def create(self, vals):
        if isinstance(vals, list):
            for val in vals:
                mon_id = val.get('mon_id')
                if mon_id:
                    mon_record = self.env['sos_mon'].browse(mon_id)
                    min_record = self.env['sos_min'].search([('mon_no', '=', mon_id)], limit=1)
                    if val.get('com_type') == 'exits':
                        create_vals = {
                            'min_id': min_record.id,
                            'component_id': val.get('component_id'),
                            'quantity': val.get('quantity'), 
                            'qp_no': val.get('qp_no'),
                            'com_type':'exits'
                        }
                    else:
                        create_vals = {
                            'min_id': min_record.id,
                            'new_component_id': val.get('new_component_id'),
                            'quantity': val.get('quantity'), 
                            'qp_no': val.get('qp_no'),
                            'com_type':'new'
                        }
                    new_line = self.env['sos_min_line_material'].with_context(from_script=True).create(create_vals)

        return super(Mon_Line_Material, self).create(vals)
        
    def write(self, vals):
        for record in self:
            min_record = self.env['sos_min'].search([('mon_no', '=', record.mon_id.id)], limit=1)

            if min_record:
                new_quantity = vals.get('quantity', record.quantity)

                new_component_id = vals.get('component_id', record.component_id.id)
                new_new_component_id = vals.get('new_component_id', record.new_component_id)

                if record.com_type == 'exits':
                    updating_records = self.env['sos_min_line_material'].search([
                        ('min_id', '=', min_record.id),
                        ('component_id', '=', record.component_id.id)
                    ])
                    
                    if updating_records:
                        updating_records.write({
                            'component_id': new_component_id,
                            'quantity': new_quantity
                        })
                
                else:
                    updating_records = self.env['sos_min_line_material'].search([
                        ('min_id', '=', min_record.id),
                        ('new_component_id', '=', record.new_component_id)
                    ])
                    
                    if updating_records:
                        updating_records.write({
                            'new_component_id': new_new_component_id,
                            'quantity': new_quantity
                        })
        return super(Mon_Line_Material, self).write(vals)

    def unlink(self):
        for record in self:
            min_record = self.env['sos_min'].search([('mon_no', '=', record.mon_id.id)], limit=1)
            if record.com_type == 'exits':
                deleting_records = self.env['sos_min_line_material'].search([
                        ('min_id', '=', min_record.id),
                        ('component_id', '=', record.component_id.id)
                    ])
            else:
                deleting_records = self.env['sos_min_line_material'].search([
                        ('min_id', '=', min_record.id),
                        ('new_component_id', '=', record.new_component_id)
                    ])
            deleting_records.unlink()
        return super(Mon_Line_Material, self).unlink()
    @api.constrains('quantity')
    def _check_quantity(self):
        for record in self:
            if record.quantity <= 0:
                raise ValidationError("Quantity must be greater than zero.")
    @api.onchange('component_id')
    def _onchange_component_id(self):
        if self.component_id:
            self.qp_no = self.component_id.qp_no
            self.uom = self.component_id.uom
            self.location = self.component_id.location
        else:
            self.qp_no = False
            self.uom = False
            self.location = False

class Mon_Line_SFG(models.Model):
    _name = 'sos_mon_line_sfg'
    _description = 'SFG Order Lines'

    mon_id = fields.Many2one('sos_mon', string="SFG", ondelete="cascade")
    component_id = fields.Many2one('sos_sfg', string="SFG Name", required=True)
    qp_no = fields.Char(string="QP No")
    uom = fields.Selection([('Nos', 'Nos'), ('Packs', 'Packs'),('µF', 'µF'),('mF', 'mF'),  ('litre', 'litre'), ('Ω', 'Ω'), ('kΩ', 'kΩ'), ('Awg', 'Awg'), ('Amps', 'Amps')], string="Uom", default='Ω')
    specifications = fields.Char(string="Specifications")
    quantity = fields.Integer(string="Quantity",required=True)
    location = fields.Char(string="Location")
    @api.constrains('quantity')
    def _check_quantity(self):
        for record in self:
            if record.quantity <= 0:
                raise ValidationError("Quantity must be greater than zero.")
    @api.onchange('component_id')
    def _onchange_component_id(self):
        if self.component_id:
            self.qp_no = self.component_id.qp_no
            self.uom = self.component_id.uom
            self.location = self.component_id.location
        else:
            self.qp_no = False
            self.uom = False
            self.location = False
class Mon_Line_FG(models.Model):
    _name = 'sos_mon_line_fg'
    _description = 'FG Order Lines'

    mon_id = fields.Many2one('sos_mon', string="Materials4", ondelete="cascade")
    component_id = fields.Many2one('sos_fg',string="FG Name", required=True)
    qp_no = fields.Char(string="QP No")
    uom = fields.Selection([('Nos', 'Nos'), ('Packs', 'Packs'),('µF', 'µF'),('mF', 'mF'),  ('litre', 'litre'), ('Ω', 'Ω'), ('kΩ', 'kΩ'), ('Awg', 'Awg'), ('Amps', 'Amps')], string="Uom", default='Ω')
    specifications = fields.Char(string="Specifications")
    quantity = fields.Integer(string="Quantity",required=True)
    location = fields.Char(string="Location")        
    @api.constrains('quantity')
    def _check_quantity(self):
        for record in self:
            if record.quantity <= 0:
                raise ValidationError("Quantity must be greater than zero.")
    @api.onchange('component_id')
    def _onchange_component_id(self):
        if self.component_id:
            self.qp_no = self.component_id.qp_no
            self.uom = self.component_id.uom
            self.location = self.component_id.location
        else:
            self.qp_no = False
            self.uom = False
            self.location = False





