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

from odoo import models, fields, api
from datetime import date


class sos_inhouse_validation_reports(models.Model):
    _name = 'sos_inhouse_validation_reports'
    _description = 'Inhouse Validation Report'
    _order = 'id asc' 
    ihvr_no = fields.Char(string="In-House Validation Report No", readonly= True, required= True, default=lambda self: self._generate_id())
    validation_date = fields.Date(string="In-House Validation Report Date")
    master_equipment_name = fields.Char(string="Master Equipment Name")
    master_equipment_id = fields.Char(string="Master Equipment ID")
    master_equipment_make = fields.Char(string="Master Equipment Make")
    calibration_report_no = fields.Char(string="Master Equipment Calibration Report No")
    master_equipment_calibrated_date=fields.Date(string="Master Equipment Calibrated Date")
    master_equipment_validation_upto=fields.Date(string="Master Equipment Calibration Valid up to")
    opreating_range = fields.Char(string="Master Equipment Calibrated Operating Range")
    measuring_tolerance = fields.Char(string="Master Equipment Measuring Tolerance")
    equipment_name=fields.Char(string="Equipment Name")
    equipment_id=fields.Char(string="Equipment ID")
    equipment_make=fields.Char(string="Equipment Make")
    validation_procedure_no=fields.Char(string="Validation Procedure No")
    eqp_validation_opreating_range = fields.Char(string="Equipment Validation Operating Range")
    eqp_measuring_tolerance = fields.Char(string="Equipment Measuring Tolerance")
    line_ids = fields.One2many('sos_inhouse_validation_reports_line', 'ref_id', string="History", ondelete='cascade')
    qc_name = fields.Many2one('res.users', string='QC Manager')
    qc_approval_image = fields.Image(related="qc_name.signature_image",readonly=True)
    qc_approved_on = fields.Datetime(string="QC Approved On")
    qa_name = fields.Many2one('res.users', string='QA Manager')
    qa_approval_image = fields.Image(related="qa_name.signature_image",readonly=True)
    qa_approved_on = fields.Datetime(string="QA Approved On")
    conclusion = fields.Selection([('PASS','PASS'),('FAIL','FAIL')],string="In-House Validation Conclusion",default="PASS")
    validity = fields.Date(string="In-House Validation Valid till")
    sticker_affix = fields.Selection([('Yes','Yes'),('No','No')],string="Validation Sticker Affixed on MME",default="Yes")

    def _generate_id(self):
        sequence_util = self.env['sos_common_scripts']
        return sequence_util.generate_sequence('sos_inhouse_validation_reports','IHVR', 'ihvr_no')
    def action_qc_esign_btn(self):
        sequence_util = self.env['sos_common_scripts']
        # Email part
        body_html = f"""
        <p>Below <b>Inhouse Validation Report</b> is waiting for your approval</p>
        """
        sequence_util.send_group_email(self.env,'sos_inhouse_validation_reports',self.id,"deenalaura.m@sosaley.in","Inhouse Validation Report Approval Request",body_html,'sos_inventory.sos_qc_user')

        return sequence_util.action_assign_signature(
        self, 
        'qc_name',
        'qc_approved_on'
    )
    def action_qa_esign_btn(self):
        sequence_util = self.env['sos_common_scripts']
        return sequence_util.action_assign_signature(
        self, 
        'qa_name',
        'qa_approved_on'
    )
    
class sos_inhouse_validation_reports_Lines(models.Model):
    _name = 'sos_inhouse_validation_reports_line'
    _description = 'Inhouse Validation Report Lines'

    ref_id = fields.Many2one('sos_inhouse_validation_reports', ondelete="cascade")
    validation_points =  fields.Char(string="Validation Points")
    standard_values =  fields.Char(string="Standard Values as per Master Equipment")
    observed_values =  fields.Char(string="Observed Values in the MME for Validation")
    error_observed =  fields.Char(string="Error Observed")
    error_percentage =  fields.Char(string="Error %")
    result = fields.Selection([('PASS','PASS'),('FAIL','FAIL')],string="Result",default="PASS")