from odoo import models, fields, api
from datetime import date,datetime
from odoo.exceptions import ValidationError,UserError
import re

class SOS_TC_Return_Customer(models.Model):
    _name = 'sos_transfer_challan_return_from_customer'
    _description = 'Transfer Challan'
    _rec_name="tc_no"
    _order = 'tc_no desc' 
    
    
    tc_no = fields.Char(string="TC No",default=lambda self: self._generate_id(),readonly= True, required= True)
    line_ids = fields.One2many('sos_transfer_challan_return_from_customer_lines', 'tc_id', string="Finished Goods",copy=True, ondelete='cascade')
    mrn_ref_no = fields.Many2one('sos_mrn',string="Return Note Reference",readonly= True)
    def _generate_id(self):
        sequence_util = self.env['sos_common_scripts']
        return sequence_util.generate_sequence('sos_transfer_challan_return_from_customer','TC', 'tc_no')
    
class SOS_TC_Return_CustomerLines(models.Model):
    _name = 'sos_transfer_challan_return_from_customer_lines'
    _description = 'tc Lines'

    tc_id = fields.Many2one('sos_transfer_challan_return_from_customer')
    item_name = fields.Char(string="Item Name")
    item_type = fields.Selection([ ('FG', 'FG'),('SFG', 'SFG'),('Material', 'Material')], 
                                 default='FG' , string="Type")
    
    return_incoming_doc_ref = fields.Many2one('sos_return_iqi',string="IQI Ref No",readonly=True)
    return_fg_incoming_doc_ref = fields.Many2one('sos_return_fir',string="Return FIR ref",readonly=True)
    combined_incoming_doc_ref = fields.Reference(
        selection=[
            ('sos_return_iqi', 'Return IQI Ref No'),
            ('sos_return_fir', 'FIR Ref')
        ],
        string="Incoming Document Reference",
        compute="_compute_combined_incoming_doc_ref",
        store=False
    )
    stores_handovered_by = fields.Many2one('res.users', string='Stores Handovered By',related="tc_id.mrn_ref_no.stores_approved_by")
    stores_handovered_on = fields.Datetime(string="Stores Handovered On",related="tc_id.mrn_ref_no.stores_approved_on")
    qc_tested_by = fields.Many2one('res.users', string='QC Tested By',compute="_compute_qc_tested_by")
    qc_tested_on = fields.Datetime(string="QC Tested On")
    received_qty = fields.Integer(string="Received Quantity")
    approved_qty = fields.Integer(string="Approved Quantity")
    rejected_qty = fields.Integer(string="Rejected Quantity")
    ncmr_ref = fields.Many2one('sos_ncmr',string="NCMR Ref(If any)")
    stores_received_by = fields.Many2one('res.users', string='Stores Received By')
    stores_received_on = fields.Datetime(string="Stores Received On")
    
    @api.depends('item_type','return_incoming_doc_ref','return_fg_incoming_doc_ref')
    def _compute_qc_tested_by(self):
        for record in self:
            if record.item_type == "FG":
                record.qc_tested_by = record.return_fg_incoming_doc_ref.qc_by_name
                record.qc_tested_on = record.return_fg_incoming_doc_ref.qc_tested_on
                record.qc_tested_on = record.return_fg_incoming_doc_ref.qc_tested_on
                record.received_qty = record.return_fg_incoming_doc_ref.batch_size
                record.approved_qty = record.return_fg_incoming_doc_ref.approved_qty
                record.rejected_qty = record.return_fg_incoming_doc_ref.rejected_qty
                record.ncmr_ref     = record.return_fg_incoming_doc_ref.ncmr_ref
                record.stores_received_by = record.return_fg_incoming_doc_ref.stores_received_by
                record.stores_received_on = record.return_fg_incoming_doc_ref.stores_received_on
            else:
                record.qc_tested_by = record.return_incoming_doc_ref.qc_by_name
                record.qc_tested_on = record.return_incoming_doc_ref.qc_tested_on
                record.received_qty = record.return_incoming_doc_ref.received_qty
                record.approved_qty = record.return_incoming_doc_ref.approved_qty
                record.rejected_qty = record.return_incoming_doc_ref.rejected_qty
                record.ncmr_ref     = record.return_incoming_doc_ref.ncmr_ref
                record.stores_received_by = record.return_incoming_doc_ref.stores_received_by
                record.stores_received_on = record.return_incoming_doc_ref.stores_received_on
    @api.depends('return_incoming_doc_ref','return_fg_incoming_doc_ref')
    def _compute_combined_incoming_doc_ref(self):
        for record in self:
            if record.return_incoming_doc_ref:
                record.combined_incoming_doc_ref = f'sos_return_iqi,{record.return_incoming_doc_ref.id}'
            elif record.return_fg_incoming_doc_ref:
                record.combined_incoming_doc_ref = f'sos_return_fir,{record.return_fg_incoming_doc_ref.id}'
            else:
                record.combined_incoming_doc_ref = False