from odoo import api, exceptions, fields, models, _
import datetime


class InbEndToEnd(models.Model):
    _name = "inb.end.to.end"
    _description = "Inbound End-to-End"

    po_number = fields.Char('PO Number')
    po_id = fields.Integer('Purchase ID')
    irn_id = fields.Char('IRN IDs')
    irn_number = fields.Char('IRN Numbers')
    irn_date_create = fields.Text('IRN Created Date')
    irn_date_confirm = fields.Text('IRN Confirm Date')
    qc_id = fields.Char('QC IDs')
    qc_number = fields.Char('QC Numbers')
    qc_date_create = fields.Text('QC Created Date')
    qc_date_confirm = fields.Text('QC Confirm Date')
    aqc_id = fields.Char('AQC IDs')
    aqc_number = fields.Char('AQC Number')
    aqc_date_create = fields.Text('AQC Created Date')
    aqc_date_confirm = fields.Text('AQC Confirm Date')
    pqc_id = fields.Char('PQC IDs')
    pqc_number = fields.Char('PQC Number')
    pqc_date_create = fields.Text('PQC Created Date')
    pqc_date_confirm = fields.Text('PQC Confirm Date')
    putaway_id = fields.Char('Putaway IDs')
    putaway_number = fields.Char('Putaway Number')
    putaway_date_create = fields.Text('Putaway Created Date')
    putaway_date_confirm = fields.Text('Putaway Confirm Date')

    def irn_data_create(self, po_number, po_id, irn_id, irn_number, irn_date_create):

        # check po_number and po_id already exists
        po_count = self.search_count([('po_number','=',po_number), ('po_id', '=', po_id)])

        if int(po_count) == 0:
            created_id = self.create({
                'po_number': po_number,
                'po_id': po_id,
                'irn_id': irn_id,
                'irn_number': irn_number,
                'irn_date_create': irn_date_create,
            })
        else:

            inb_etoe_id = self.search([('po_number', '=', po_number), ('po_id', '=', po_id)])

            # browse and then update
            for etoe in self.browse(inb_etoe_id):
                self.write({
                    'irn_id': str(etoe.irn_id) + "," + str(irn_id),
                    'irn_number': str(etoe.irn_number) + "," + str(irn_number),
                    'irn_date_create': str(etoe.irn_date_create) + "," + str(irn_date_create),
                })

        return True

    def irn_confirm_update(self, po_number, po_id, irn_date_confirm):

        inb_etoe_id = self.search([('po_number', '=', po_number), ('po_id', '=', po_id)])

        # browse and then update
        for etoe in self.browse(inb_etoe_id):

            self.write({
                'irn_date_confirm': str(irn_date_confirm) if str(etoe.irn_date_confirm) == 'False'
                else str(etoe.irn_date_confirm) + "," + str(irn_date_confirm)
            })

        return True

    def qc_data_create(self,  po_number, po_id, qc_id, qc_number, qc_date_create):

        inb_etoe_id = self.search( [('po_number', '=', po_number), ('po_id', '=', po_id)])

        # browse and then update
        for etoe in self.browse(inb_etoe_id):
            self.write( inb_etoe_id, {
                'qc_id': str(qc_id) if str(etoe.qc_id) == 'False' else str(etoe.qc_id) + "," + str(qc_id),
                'qc_number': str(qc_number) if str(etoe.qc_number) == 'False' else str(etoe.qc_number) + "," + str(qc_number),
                'qc_date_create': str(qc_date_create) if str(etoe.qc_date_create) == 'False' else str(etoe.qc_date_create) + "," + str(qc_date_create),
            })

        return True

    def qc_confirm_update(self,  po_number, po_id, qc_date_confirm):

        inb_etoe_id = self.search( [('po_number', '=', po_number), ('po_id', '=', po_id)])

        # browse and then update
        for etoe in self.browse( inb_etoe_id):
            self.write({
                'qc_date_confirm': str(qc_date_confirm) if str(etoe.qc_date_confirm) == 'False' else str(etoe.qc_date_confirm) + "," + str(qc_date_confirm),
            })

        return True

    def aqc_data_create(self,  po_number, po_id, aqc_id, aqc_number, aqc_date_create):

        inb_etoe_id = self.search( [('po_number', '=', po_number), ('po_id', '=', po_id)])

        # browse and then update
        for etoe in self.browse( inb_etoe_id):
            self.write( {
                'aqc_id': str(aqc_id) if str(etoe.aqc_id) == 'False' else str(etoe.aqc_id) + "," + str(aqc_id),
                'aqc_number': str(aqc_number) if str(etoe.aqc_number) == 'False' else str(etoe.aqc_number) + "," + str(aqc_number),
                'aqc_date_create': str(aqc_date_create) if str(etoe.aqc_date_create) == 'False' else str(etoe.aqc_date_create) + "," + str(aqc_date_create),
            })

        return True

    def aqc_confirm_update(self,  po_number, po_id, aqc_date_confirm):

        inb_etoe_id = self.search([('po_number', '=', po_number), ('po_id', '=', po_id)])

        # browse and then update
        for etoe in self.browse( inb_etoe_id):
            self.write({
                'aqc_date_confirm': str(aqc_date_confirm) if str(etoe.aqc_date_confirm) == 'False' else str(etoe.aqc_date_confirm) + "," + str(aqc_date_confirm),
            })

        return True

    def pqc_data_create(self,  po_number, po_id, pqc_id, pqc_number, pqc_date_create):

        inb_etoe_id = self.search( [('po_number', '=', po_number), ('po_id', '=', po_id)])

        # browse and then update
        for etoe in self.browse(inb_etoe_id):
            self.write( {
                'pqc_id': str(pqc_id) if str(etoe.pqc_id) == 'False' else str(etoe.pqc_id) + "," + str(pqc_id),
                'pqc_number': str(pqc_number) if str(etoe.pqc_number) == 'False' else str(etoe.pqc_number) + "," + str(pqc_number),
                'pqc_date_create': str(pqc_date_create) if str(etoe.pqc_date_create) == 'False' else str(etoe.pqc_date_create) + "," + str(pqc_date_create),
            })

        return True

    def pqc_confirm_update(self,  po_number, po_id, pqc_date_confirm):

        inb_etoe_id = self.search( [('po_number', '=', po_number), ('po_id', '=', po_id)])

        # browse and then update
        for etoe in self.browse( inb_etoe_id):
            self.write( {
                'pqc_date_confirm': str(pqc_date_confirm) if str(etoe.pqc_date_confirm) == 'False' else str(etoe.pqc_date_confirm) + "," + str(pqc_date_confirm),
            })

        return True

    def putaway_data_create(self,  po_number, po_id, putaway_id, putaway_number, putaway_date_create):

        inb_etoe_id = self.search( [('po_number', '=', po_number), ('po_id', '=', po_id)])

        # browse and then update
        for etoe in self.browse( inb_etoe_id):
            self.write( {
                'putaway_id': str(putaway_id) if str(etoe.putaway_id) == 'False' else str(etoe.putaway_id) + "," + str(putaway_id),
                'putaway_number': str(putaway_number) if str(etoe.putaway_number) == 'False' else str(etoe.putaway_number) + "," + str(putaway_number),
                'putaway_date_create': str(putaway_date_create) if str(etoe.putaway_date_create) == 'False' else str(etoe.putaway_date_create) + "," + str(putaway_date_create),
            })

        return True

    def putaway_confirm_update(self,  po_number, po_id, putaway_date_confirm):

        inb_etoe_id = self.search( [('po_number', '=', po_number), ('po_id', '=', po_id)])

        # browse and then update
        for etoe in self.browse(inb_etoe_id):
            self.write( {
                'putaway_date_confirm': str(putaway_date_confirm) if str(etoe.putaway_date_confirm) == 'False' else str(etoe.putaway_date_confirm) + "," + str(putaway_date_confirm),
            })

        return True
