from odoo import models, fields, api



class sos_travel_bills(models.Model):
    _name = 'sos_travel_reimbursement_bills'
    _description = 'Travel Bills'
    _rec_name = 'plan_id'
    _order = 'id desc'

    plan_id = fields.Many2one('sos_travel_plan',string="Plan Id",required= True)
    travel_type =  fields.Selection([('installation','Installation & Commissioning'),('service','Service'),('demo','Demo'),('validation','Validation'),('warranty','Warranty Call'),('amc','AMC/Cloud Service')],string='Type',related="plan_id.travel_type")
    claimed_by  = fields.Many2one(
    'res.users',
    string='Claimed By',
    default=lambda self: self.env.user,
    domain=lambda self: [
        '|', '|',
        ('groups_id', 'in', self.env.ref('sos_inventory.sos_ce_user').ids),
        ('groups_id', 'in', self.env.ref('sos_inventory.sos_sales_user').ids),
        ('groups_id', 'in', self.env.ref('sos_inventory.sos_ce_head').ids),
            ]
        )
    persons = fields.Many2many(
    'res.users',
    string='Number of Persons Travelled',
    domain=lambda self: [
        '|', '|', '|',
        ('groups_id', 'in', self.env.ref('sos_inventory.sos_ce_user').ids),
        ('groups_id', 'in', self.env.ref('sos_inventory.sos_sales_user').ids),
        ('groups_id', 'in', self.env.ref('sos_inventory.sos_ce_head').ids),
        ('groups_id', 'in', self.env.ref('sos_inventory.sos_qc_user').ids),
            ]
        )
    customer_name = fields.Char(string="Customer Name")
    customer_location = fields.Char(string="Customer Location")
    line_ids = fields.One2many('sos_travel_reimbursement_bills_lines', 'ref_id', string="Bills Line Ids",copy=True)
    total_expense = fields.Float(string="Total Expense", compute="_compute_final_value", store=True)
    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")
    accounts_approved_on = fields.Datetime(string="Accounts Approved On")
    accounts_approved_name = fields.Many2one('res.users', string='Accounts Sign')
    accounts_approved_by_image = fields.Image(related="accounts_approved_name.signature_image",string='Accounts Sign',readonly=True)
    top_management_name  = fields.Many2one('res.users', string='Stores Approved By')
    top_management_approval_image = fields.Image(related="top_management_name.signature_image",string='Stores Approval Sign',readonly=True)
    top_management_approved_on = fields.Datetime(string="Approved On")
    def action_report_esign_btn(self):
        # Email part
        body_html = f"""
        <p>Below <b>Reimbursement bills</b> are waiting for your Approval</p>
        """
        sequence_util = self.env['sos_common_scripts']
        sequence_util.send_group_email(self.env,'sos_travel_reimbursement_bills',self.id,"deenalaura.m@sosaley.in","Travel Reimbursement Approval Request",body_html,'sos_inventory.sos_ce_head')
        # Email part ends
        return sequence_util.action_assign_signature(
        self, 
        'prepared_by_name',
        'prepared_on'
    )
    def action_report_esign_btn1(self):
        sequence_util = self.env['sos_common_scripts']
        # Email part
        body_html = f"""
        <p>Below <b>Reimbursement bills</b> are waiting for your Approval</p>
        """
        sequence_util.send_group_email(self.env,'sos_travel_reimbursement_bills',self.id,"deenalaura.m@sosaley.in","Travel Reimbursement Approval Request",body_html,'sos_inventory.sos_finance_user')
        # Email part ends
        sequence_util.action_assign_signature(
        self, 
        'dept_in_charge_name',
        'dept_in_charge_approved_on'
        )
    
    def action_acc_esign_btn(self):
        sequence_util = self.env['sos_common_scripts']
        # Email part
        body_html = f"""
        <p>Below <b>Reimbursement bills</b> are waiting for your Approval</p>
        """
        sequence_util.send_direct_email(self.env,"sos_travel_reimbursement_bills",self.id,"ramachandran.r@sosaley.in","Travel Reimbursement Approval Request",body_html)
        # Email part ends
        
        sequence_util.action_assign_signature(
        self, 
        'accounts_approved_name',
        'accounts_approved_on'
        )
    
    def action_top_esign(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'
        )
    @api.depends('line_ids.cost')
    def _compute_final_value(self):
        for record in self:
            record.total_expense = sum(record.line_ids.mapped('cost'))
    @api.onchange('plan_id')
    def _onchange_plan_id(self):
        if self.plan_id:
            self.customer_name = self.plan_id.customer_name.customer_name
            self.customer_location = self.plan_id.customer_location
            self.persons=self.plan_id.persons
        else:
            self.customer_name = False
            self.customer_location = False
            self.persons = False
    def action_report_travel_reimbursement_btn(self):
        try:
            action = self.env.ref("sos_installation_commision.action_report_travel_bills").report_action(self)
            return action
        except ValueError as e:
            print(f"Failed to find report action: {e}")
class sos_travel_bills_lines(models.Model):
    _name = 'sos_travel_reimbursement_bills_lines'
    _description = 'Bills Line Items'

    ref_id = fields.Many2one('sos_travel_reimbursement_bills')
    date_of_expense = fields.Date(string="Date")
    purpose = fields.Char(string="Purpose")
    expense_type =  fields.Selection([('food','Food'),('accomadation','Accomadation'),('travel','Travel'),('purchase','Purchase'),('recharge','Recharge'),('contract','Contract Labour')],string='Expense Type')
    cost = fields.Float(string="Cost")
    remarks = fields.Text(string="Remarks")
