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

from odoo import models, fields, api
import time
from odoo.exceptions import UserError
from odoo.exceptions import ValidationError

class sos_Disposal_Register(models.Model):
    _name = 'sos_disposal_register'
    _description = 'Rejected Item Disposal Register'
    _rec_name ='dr_no'


    dr_no = fields.Char(string="Disposal Register No", readonly= True, required= True, default=lambda self: self._generate_id())
    ncmr_no = fields.Many2one('sos_ncmr', string="NCMR No")
    ncmr_date = fields.Date(related="ncmr_no.ncmr_date", string="NCMR Date",readonly=False)
    aodr_no = fields.Many2one('sos_aodr', string="AODR No")
    aodr_date = fields.Date(related="aodr_no.aodr_date",string="AODR Date",readonly=False)
    goods_type = fields.Selection([
        ('material', 'Material'),
        ('sfg', 'SFG'),
        ('fg', 'FG')
    ], string='Type',default="material")
    customer_name = fields.Char(string="Customer Name")
    supplier_name = fields.Char(string="Vendor/Service Provider Name")
    material_name = fields.Many2one('sos_material', string="Material Name")
    material_qp_no = fields.Char(related="material_name.qp_no",string="QP No",readonly=False)
    sfg_name = fields.Many2one('sos_sfg', string="SFG Name")
    sfg_qp_no = fields.Char(related="sfg_name.qp_no",string="QP No",readonly=False)
    fg_name = fields.Many2one('sos_fg', string="FG Name")
    fg_qp_no = fields.Char(related="fg_name.qp_no",string="QP No",readonly=False)
    rejected_qty = fields.Integer(string="Rejected Quantity")
    customer_decision = fields.Selection([('payment_return','Payment Refunded'),('free_replacement','Issued Freen Replacemenet')],string='Decision to the Customer')
    action_on_rejects = fields.Selection([('supplier_return','Returned to Supplier'),('supplier_scrap','Returned to Scrap Yard')],string='Actions on Rejections')
    qa_by_name = fields.Many2one('res.users', string='QA Sign')
    qa_by_image = fields.Image(related="qa_by_name.signature_image",string='Prepared by Sign',readonly=True)
    qa_tested_on = fields.Datetime(string="Approved On")
    qa_comments = fields.Text(string="QA Comments")
    lot_no = fields.Char(string="Lot No")
    mfg_date = fields.Date(string="Manufacturing Date")
    return_reason = fields.Text(string="Return For 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")
    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")
    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")
    qc_by_name = fields.Many2one('res.users', string='QC Sign')
    qc_by_image = fields.Image(related="qc_by_name.signature_image",string='QC by Sign',readonly=True)
    qc_tested_on = fields.Datetime(string="Approved On")
    def _generate_id(self):
        sequence_util = self.env['sos_common_scripts']
        return sequence_util.generate_sequence('sos_disposal_register','DR', 'dr_no')
    def action_top_esign_btn(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 Item is waiting for your approval to Dispose</p>
        """ 
        sequence_util.send_group_email(self.env,'sos_disposal_register',self.id,"deenalaura.m@sosaley.in","Disposal Request",body_html,'sos_inventory.sos_qa_user')
        # Email part ends
    def action_report_esign_btn(self):
        sequence_util = self.env['sos_common_scripts']
        # Email part
        body_html = f"""
         <p>Below Item is waiting for your approval to Dispose</p>
        """
        sequence_util.send_mon_min_email(self.env,'sos_disposal_register',self.id,"deenalaura.m@sosaley.in","Disposal Request",body_html,"user")
        # Email part ends
        sequence_util = self.env['sos_common_scripts']
        return sequence_util.action_assign_signature(
        self, 
        'prepared_by_name',
        'prepared_on'
    )
    def action_qa_esign_btn(self):
        sequence_util = self.env['sos_common_scripts']
        return sequence_util.action_assign_signature(
        self, 
        'qa_by_name',
        'qa_tested_on'
    )
    def action_qc_esign_btn(self):
        sequence_util = self.env['sos_common_scripts']
        # Email part
        body_html = f"""
         <p>Below Item is waiting for your approval to Dispose</p>
        """
        
        sequence_util.send_direct_email(self.env,"sos_disposal_register",self.id,"ramachandran.r@sosaley.in","Disposal Approved",body_html)
        # Email part ends
        return sequence_util.action_assign_signature(
        self, 
        'qc_by_name',
        'qc_tested_on'
    )
    def action_report_esign_btn_dept_incharge(self):
        sequence_util = self.env['sos_common_scripts']
        # Email part
        body_html = f"""
        <p>Below Item is waiting for your approval to Dispose</p>
        """ 
        sequence_util.send_group_email(self.env,'sos_disposal_register',self.id,"deenalaura.m@sosaley.in","Disposal Request",body_html,'sos_inventory.sos_qc_user')
        # Email part ends
        return sequence_util.action_assign_signature(
        self, 
        'dept_in_charge_name',
        'dept_in_charge_approved_on'
    )
    