<?php
defined('BASEPATH') or exit('No direct script access allowed');

/**
 *
 * Model Xlsexport_model_model
 *
 * This Model for ...
 *
 * @package        CodeIgniter
 * @category    Model
 * @author    Setiawan Jodi <jodisetiawan@fisip-untirta.ac.id>
 * @link      https://github.com/setdjod/myci-extension/
 * @param     ...
 * @return    ...
 *
 */

class Xlsexport_model extends CI_Model
{

    protected $fechaLog = "";

    public function __construct()
    {
        parent::__construct();
        $this->fechaLog = "";
    }

    public function getLastDateLog($idImportacion)
    {
        $sql = "SELECT MAX(fecha) as fecha_maxima FROM config_fecha_importaciones WHERE id = $idImportacion";
        $result = $this->db->query($sql);
        $this->fechaLog = $result->row()->fecha_maxima;
        return;
    }

    public function getLicitaciones($idUltimaImportacion)
    {
        $sql = " SELECT
                    CF.id,
                    LI.id_licitacion,
                    GROUP_CONCAT( PROJI.itemClassification_value ) AS itemClassification_attr,
                    LI.link_uri,
                    LI.updated,
                    LI.estadoLicitacion,
                    CF.contractFolderID,
                    CF.contractFolderStatusCode,
                    LP.buyerProfileURIID,
                    LP.contractingPartyTypeCode,
                    MP.partyName,
                    MP.codpostal,
                    MP.websiteURI,
                    MP.city,
                    MP.direccion,
                    MP.contact_name,
                    MP.contact_email,
                    MPI.idPartyIdent AS partyItem,
                    MPI.attr as tipoIdParty,
                    PROJ.NAME as nameProject,
                    PROJ.estimatedOverallContractAmount,
                    PROJ.taxExclusiveAmount,
                    PROJ.totalAmount,
                    PROJ.countrySubentityCode,
                    PROJ.countrySubentity,
                    PROJ.durationMesure,
                    PROJ.medida,
                    PROJ.duracion_inicio,
                    PROJ.duracion_fin,
                    PROJ.contractExtension,
                    TT.procurementLegislationDocumentReference,
                    TT.allowedSubcontractTerms,
                    RCF.descripcion AS contrato_status,
                    RLP.nombre AS tipo_administracion,
                    TPC.descripcion AS tipo_contrato,
                    RTP.nombre AS tipoTender,
                    RTPC.nombre AS contrato_sistema,
                    RTPS.nombre AS submision,
                    RTPU.nombre AS urgency,
                    LI.fecha_creacion_log
                FROM
                    global_datos_iniciales_licitacion LI
                    LEFT JOIN global_contract_folder CF ON CF.id_ajena_licitacion = LI.id_licitacion
                    LEFT JOIN global_located_contracting_party LP ON LP.id_ajena_licitacion = LI.id_licitacion
                    LEFT JOIN global_party MP ON MP.id_ajena_licitacion = LI.id_licitacion
                    LEFT JOIN global_party_party_idenfitication MPI on MPI.id_ajena_licitacion = LI.id_licitacion AND MPI.attr = 'DIR3'
                    LEFT JOIN global_procurement_project PROJ ON PROJ.id_ajena_licitacion = LI.id_licitacion
                    LEFT JOIN global_procurement_project_itemclass PROJI ON PROJI.id_ajena_licitacion = LI.id_licitacion
                    LEFT JOIN global_tendering_process TP ON TP.id_ajena_licitacion = LI.id_licitacion
                    LEFT JOIN global_tendering_terms TT ON TT.id_ajena_licitacion = LI.id_licitacion
                    LEFT JOIN codice_contract_status RCF ON RCF.CODE = CF.contractFolderStatusCode
                    LEFT JOIN codice_located_contracting_party RLP ON RLP.CODE = LP.contractingPartyTypeCode
                    LEFT JOIN codice_tipo_contrato TPC ON TPC.CODE = PROJ.typeCode
                    LEFT JOIN codice_tender_process RTP ON RTP.CODE = TP.procedureCode
                    LEFT JOIN codice_tender_process_contracting_code RTPC ON RTPC.CODE = TP.contractingSystemCode
                    LEFT JOIN codice_tender_process_submission RTPS ON RTPS.CODE = TP.submissionMethodCode
                    LEFT JOIN codice_tender_process_urgency_code RTPU ON RTPU.CODE = TP.urgencyCode
                WHERE LI.id_importacion = $idUltimaImportacion
                GROUP BY
                    id_licitacion";
        $result = $this->db->query($sql);
        $datos['datos'] = $result->result();
        $datos['num_rows'] = $result->num_rows();
        return $datos;
    }

    public function getFullDatosUltimasInsertadas()
    {
        $sql = "SELECT *
                FROM
                vista_datos_licitacion";
        $result = $this->db->query($sql);
        $datos['datos'] = $result->result();
        $datos['num_rows'] = $result->num_rows();
        return $datos;
    }

    public function getTablaByFechaLog($tabla)
    {
        $sql = "SELECT * FROM $tabla
                WHERE fecha_creacion_log = '$this->fechaLog'";
        $result = $this->db->query($sql);
        $datos['datos'] = $result->result_array();
        $datos['num_rows'] = $result->num_rows();
        return $datos;
    }

    public function getProjectLotItem()
    {
        $sql = "SELECT
                    MP.idProcurementLot,
                    MP.countrySubentity,
                    MP.countrySubentityCode,
                    MP.totalAmount,
                    MP.taxExclusiveAmount,
                    MP.`name`,
                    MP.id_ajena_licitacion,
                    GROUP_CONCAT( MPI.idItem ) AS itemClassification_attr

                FROM
                    global_procurement_project_lot MP
                    JOIN global_procurement_project_lot_item MPI ON MP.id_compuesta = MPI.id_compuesta_padre
                WHERE
                    MPI.fecha_creacion_log = '$this->fechaLog'
                    GROUP BY
                    MP.id_compuesta";
        $result = $this->db->query($sql);
        $datos['datos'] = $result->result_array();
        $datos['num_rows'] = $result->num_rows();
        return $datos;
    }

    public function getFechasPublicadas($idLicitacion)
    {
        $sql = "SELECT MIN(fechas) as fecha
                FROM
                global_valid_notice_info_fecha WHERE id_ajena_licitacion = '$idLicitacion'";
        $result = $this->db->query($sql);
        $datos['datos'] = $result->row();
        $datos['num_rows'] = $result->num_rows();
        return $datos;
    }

    public function getTenderingTermsCodes()
    {
        $sql = "SELECT
                    TP.*,
                    RTP.nombre AS tipoTender, /*procedureCode*/
                    RTPC.nombre AS contrato_sistema, /*urgencyCode*/
                    RTPS.nombre AS submision, /*contractingSistemCode*/
                    RTPU.nombre AS urgency /*urgencyCode*/
                FROM
                global_tendering_process TP
                JOIN codice_tender_process RTP ON RTP.CODE = TP.procedureCode
                JOIN codice_tender_process_contracting_code RTPC ON RTPC.CODE = TP.contractingSystemCode
                JOIN codice_tender_process_submission RTPS ON RTPS.CODE = TP.submissionMethodCode
                JOIN codice_tender_process_urgency_code RTPU ON RTPU.CODE = TP.urgencyCode

                WHERE
                    TP.fecha_creacion_log = '$this->fechaLog'";
        $result = $this->db->query($sql);
        $datos['datos'] = $result->result_array();
        $datos['num_rows'] = $result->num_rows();
        return $datos;
    }

    public function getDatosRelacionadasTablaWithoutLicitacion($tabla, $tablaRelacion, $txtIdAjena, $id = "t1.id")
    {
        $sql = "SELECT
                    *,
                    t2.nombre as code_desc
                FROM
                    $tabla t1
                JOIN
                    $tablaRelacion t2 ON t2." . $txtIdAjena . " = t1." . $id . "
                WHERE t1.fecha_creacion_log = '$this->fechaLog'";
        $result = $this->db->query($sql);
        $datos['datos'] = $result->result_array();
        $datos['num_rows'] = $result->num_rows();
        return $datos;
    }
}

/* End of file Xlsexport_model_model.php */
/* Location: ./application/models/Xlsexport_model_model.php */
