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

class Summaries_model extends CI_Model
{

    public function __construct()
    {
        parent::__construct();
    }

    public function addSummary($fecha)
    {

        // $fecha = date('2018-10-29');
        $issueDate = date("Y-m-d");
        $RB = false;
        $AB = false;
        $AF = false;
        //*************************TRJ032 - ALEXANDER ROCA - 06/04/2019*******************		
        //if(!$this->getNullSales($fecha)){
        //*************************TRJ032 - ALEXANDER ROCA - 06/04/2019*******************			
        $summary = $this->getSummaryByDate($issueDate);
        if ($summary->number) {
            $number1 = (int)$summary->number + 1;
            //$number2 = $number1 + 1;
        } else {
            $number1 = 1;
            //$number2 = 2;
        }

        // if($this->getSummaryAllSales($fecha,$number1)){
        // $RB = true;
        // }
        $sendBoleta = $this->getSummaryAllSales($fecha, $number1);
        if ($sendBoleta > 0) {
            $RB = true;
            $number2 = $sendBoleta + 1;
        } else {
            $number2 = $number1 + 1;
        }

        //$number2 = $number1 + 1;

        if ($this->getSummaryUndoSales($fecha, $number2, 1)) {
            $AB = true;
        }

        $summary_voided = $this->getSummaryByDate($issueDate, $baja = true);
        if ($summary_voided->number) {
            $number3 = (int)$summary_voided->number + 1;
        } else {
            $number3 = 1;
        }

        if ($this->getSummaryUndoSales($fecha, $number3, 2)) {
            $AF = true;
        }

        if ($RB || $AB || $AF) {
            return true;
        }
        //*************************TRJ032 - ALEXANDER ROCA - 06/04/2019*******************
        /*}else{
            // die("Resumen no enviado, existen ventas con precio 0");
            return false;
        }*/
        //*************************TRJ032 - ALEXANDER ROCA - 06/04/2019*******************
        return false;
    }

    public function getSummaryAllSales($date, $num = 1)
    {
        $endDate = strtotime('+1 day', strtotime($date));
        $endDate = date('Y-m-d 00:00:00', $endDate);

        $this->db->select('sales.*, customers.document_type_id as tipo_documento, customers.cf1 as dni, customers.cf2 as ruc', FALSE)
            ->join('customers', 'customers.id=sales.customer_id', 'left')
            ->order_by('sales.date')
            // ->where('sales.status !=', "DEBE")
            ->where('sales.invoice_id !=', NULL)
            ->where('sales.date >', $date)
            ->where('sales.date <', $endDate)
            ->where('sales.estado !=', 2); //************TRJ074 - ALEXANDER ROCA - 23/08/2019************	

        $q = $this->db->get_where('sales', array('document_type' => 1));

        $cont_filas = 1;
        $num_XML = $num - 1;

        $limite_summaries = (array)$this->consultaLimitSummaries();
        $cant_limit_summmaries = (int)$limite_summaries['summaries_limit'];

        if ($q->num_rows() > 0) {
            foreach (($q->result()) as $row) {
                $data[] = $row;

                if ($cont_filas == $q->num_rows() || $cont_filas % $cant_limit_summmaries == 0) {

                    $num_XML++;

                    if ($this->send_summary($date, $date, $data, $num_XML)) {
                        //return true;
                    }

                    $data = null;
                }

                $cont_filas++;
            }


            if ($num_XML < $num) {
                $num_XML = $num;
            }

            // if($this->send_summary($date, $date, $data, $num)){
            // return true;
            // }

            return $num_XML;
        }

        return 0;
    }

    public function getSummaryUndoSales($date, $num = 2, $document_type = 1)
    {

        $endDate = strtotime('+1 day', strtotime($date));
        $endDate = date('Y-m-d 00:00:00', $endDate);
        if ($document_type == 1) {
            $this->db->select('sales.*, customers.document_type_id as tipo_documento, customers.cf1 as dni, customers.cf2 as ruc', FALSE)
                ->join('customers', 'customers.id=sales.customer_id', 'left')
                ->order_by('sales.date')
                // ->where('sales.status !=', "DEBE")
                ->where('sales.invoice_id !=', NULL)
                ->where('sales.updated_at >', $date)
                ->where('sales.updated_at <', $endDate)
                ->where('sales.estado =', 0);
        }
        if ($document_type == 2) {
            $this->db->select('sales.*, customers.document_type_id as tipo_documento, customers.cf1 as dni, customers.cf2 as ruc', FALSE)
                ->join('customers', 'customers.id=sales.customer_id', 'left')
                ->join('send_invoice_items', 'send_invoice_items.sale_id=sales.id')
                ->join('send_invoice', 'send_invoice.id=send_invoice_items.send_invoice_id')
                ->order_by('sales.date')
                // ->where('sales.status !=', "DEBE")
                ->where('sales.invoice_id !=', NULL)
                ->where('sales.updated_at >', $date)
                ->where('sales.updated_at <', $endDate)
                ->where('sales.estado =', 0)
                ->where('send_invoice.status =', 4)
                ->where('send_invoice.estado =', 1);
        }


        $q = $this->db->get_where('sales', array('document_type' => $document_type));

        $dateb = true;
        if ($q->num_rows() > 0) {
            foreach (($q->result()) as $row) {
                $data[] = $row;

                if ($dateb) {
                    $datet = explode(' ', $row->date, 2);
                    $firstDate = $datet[0];
                    $dateb = false;
                }
            }

            $items = (array)$data;
            $summary;
            $processed_date = $date;
            $date = $firstDate;

            $count = 0;
            foreach ($items as $item) {

                $count = $count + 1;
                $datef = explode(' ', $item->date, 2);
                $referenceDate = $datef[0];

                if ($date != $referenceDate) {
                    if ($document_type == 2) {
                        $this->send_summary_voided($processed_date, $date, $summary, $num, 0);
                    } else if ($document_type == 1) {
                        $this->send_summary($processed_date, $date, $summary, $num, 0);
                    }
                    $num = $num + 1;
                    unset($summary);
                    $summary[] = $item;
                } else {
                    $summary[] = $item;
                }

                $date = $referenceDate;

                if ($q->num_rows() == $count) {
                    if ($document_type == 2) {
                        $this->send_summary_voided($processed_date, $date, $summary, $num, 0);
                    } else if ($document_type == 1) {
                        $this->send_summary($processed_date, $date, $summary, $num, 0);
                    }
                }
            }

            return true;
        }
        return false;
    }

    public function send_summary($processed_date, $referenceDate, $items, $numero, $estado = 1)
    {

        // $items = $this->getSummarySales($referenceDate, $estado, $endDate);
        // die(json_encode($items));
        if ($items) {
            //Header
            $type = "RC";
            $supplierID = $this->session->userdata('ruc');
            $date = date("Ymd");

            $passFirma = $this->session->userdata('pass_firma');
            $usuSol = $this->session->userdata('user_sol');
            $passSol = $this->session->userdata('pass_sol');

            $issueDate = date("Y-m-d");
            $supplierName = $this->session->userdata('razon_social');

            /*if($this->session->userdata('beta') == 0){
                $urlDoc = "post/xmlenvio";
            }else{
                $urlDoc = "beta/post/xmlenvio";
            }*/
            $urlDoc = "resumen_boletas.php";

            $header = array(
                "codigo" => $type, //type
                "secuencia" => (string)$numero, //serieNumero
                "ruc" => $supplierID, //supplierID
                "serie" => $date, //date
                "clave_firma" => $passFirma, //passFirma
                "usuariosol" => $usuSol, //usuSol
                "clavesol" => $passSol, //passSol
                "fecha_documento" => $issueDate, //issueDate
                "fecha_referencia" => $referenceDate, //referenceDate
                "razon_social" => $this->replace_invalid_caracters($supplierName), //supplierName
                'tipo_doc' => '6',
                "codigo_ubigeo" => "",
                "direccion" => "",
                "direccion_departamento" => "",
                "direccion_provincia" => "",
                "direccion_distrito" => "",
                "direccion_codigopais" => "PE",
                'tipo_proceso' => $this->session->userdata('beta'),
            );

            $sales_ids;
            $cant = 0;
            foreach ($items as $item) {

                if ($estado == 0) {
                    $conditionCode = "3";
                } else {
                    $conditionCode = "1";
                }

                $documentTypeCode = "03";
                $documentID = $item->dni;
                if ($documentID == "" | $documentID == null) {
                    $documentID = $item->ruc;
                }
                $document = $item->tipo_documento; //*******TRJ073 - KENY PONTE - 17/09/2019********* */
                //$document="1";

                $serieNumero = $item->invoice_id;

                $cant = $cant + 1;
                $itemArray[$cant] = array(
                    "EXPORTACION" => "0",
                    "GRATUITAS" => "0",
                    "NRO_COMPROBANTE_REF" => "0",
                    "TIPO_COMPROBANTE_REF" => "0",
                    "MONTO_CARGO_X_ASIG" => "0",
                    "CARGO_X_ASIGNACION" => "0",
                    'ISC' => '0',
                    "OTROS" => "0",
                    "TIPO_COMPROBANTE" => $documentTypeCode, //documentTypeCode
                    "NRO_COMPROBANTE" => $serieNumero, //serieNumero
                    "NRO_DOCUMENTO" => $documentID, //customerID
                    "TIPO_DOCUMENTO" => $document, //additionalAccountID
                    "STATUS" => $conditionCode, //conditionCode
                    "TOTAL" => $item->grand_total, //totalAmount
                    //"paidAmount" => $item->total,//paidAmount
                    //"instructionID" => "01",//instructionID
                    "IGV" => $item->total_tax, //taxTotalTaxAmount
                    //"taxSubtotalTaxAmount" => $item->product_tax,//taxSubtotalTaxAmount
                    "COD_MONEDA" => $item->currency, //currencyCode
                    "GRAVADA" => $item->affected, //affected
                    "INAFECTO" => $item->non_affected, //non_affected
                    "EXONERADO" => $item->exonerated, //exonerated
                    "TOTAL_ICBPER" => $item->tax_ICBPER //tax_ICBPER
                );

                $sales_ids[] = $item->id;
            }

            //datos a enviar
            $postData["header"] = $header;
            $postData["item"] = $itemArray;

            // Create the context for the request
            $context = stream_context_create(array(
                'http' => array(
                    'method' => 'POST',
                    'header' => "Authorization: application/json\r\n" .
                        "Content-Type: application/json\r\n",
                    'content' => json_encode($postData)
                )
            ));

            // Send the request
            $url = $this->config->item('aws_url');
            $response = file_get_contents($url . $urlDoc, FALSE, $context);

            // Check for errors
            // if($response === FALSE){
            // die(json_encode($postData));
            // }
            /************************************************/
            if (file_exists("datos.txt")) {
                $archivo = fopen("datos.txt", "a");
                fwrite($archivo, PHP_EOL . json_encode($postData) . PHP_EOL . json_encode($response) . PHP_EOL);
                fclose($archivo);
            } else {
                $archivo = fopen("datos.txt", "w");
                fwrite($archivo, PHP_EOL . json_encode($postData) . PHP_EOL . json_encode($response) . PHP_EOL);
                fclose($archivo);
            }
            /****************************************************/

            if ($response) {

                $res = json_decode($response, TRUE);


                if ($estado == 0) {
                    $tipo = "AB";
                } else {
                    $tipo = "RB";
                }

                if ($res['hash_cdr'] == "") {
                    $status = 1;
                    $flg_response = "0";

                    if (isset($res["cod_sunat"])) {
                        $error_code = substr($res["cod_sunat"], 16, strlen($res["cod_sunat"]));
                        $respuesta = $res["mensaje"];
                    } else {
                        $error_code = "0100";
                        $respuesta = "SYS: error al conectarse con sunat";
                    }
                } else {
                    $status = 2;
                    $flg_response = "1";
                    $error_code = "0";
                    $codigo = 2;
                }

                $summary = array(
                    'issue_date' => $issueDate,
                    'number' => $numero,
                    'file_name' => $supplierID . "-" . $type . "-" . $date . "-" . $numero,
                    'ticket' => $res['cod_ticket'], //*****Renato TRJ023 25/04/2019   ********
                    'reference_date' => $referenceDate,
                    'processed_date' => $processed_date,
                    'type' => $tipo,
                    'flg_response' => $flg_response,
                    'error_code' => $error_code,
                    'status' => $status,
                    'uCrea' => $this->session->userdata('user_id'),
                    'fCrea' => date('Y-m-d H:i:s'),
                    'estado' => 1,
                );

                if ($this->db->insert('send_invoice', $summary)) {

                    $summary_id = $this->db->insert_id();

                    $data = array(
                        'send_invoice_id' => $summary_id,
                        'sale_id' => NULL,
                        'uCrea' => $this->session->userdata('user_id'),
                        'fCrea' => date('Y-m-d H:i:s'),
                        'estado' => 1,
                    );

                    foreach ($sales_ids as $sale) {
                        $data["sale_id"] = $sale;
                        $this->db->insert('send_invoice_items', $data);

                        $this->db->where('id', $sale);
                        $query = $this->db->get('sales');

                        $dataSale = $query->row();
                        if($flg_response==0 || $error_code !=0){
                            if($error_code == '0100'){
                                $response_descrip = 'SYS: Resumen de envio que incluye la Boleta '.$dataSale->invoice_id.', pendiente de respuesta de SUNAT.';
                                if($tipo=='AB'){
                                    $response_descrip = 'SYS: Boleta '.$dataSale->invoice_id.', anulada, pendiente de respuesta de SUNAT..';
                                }
                            }else{
                                $response_descrip = 'SIS: resumen de envio que incluye a la Boleta '.$dataSale->invoice_id.', con error de envio.';
                                if($tipo=='AB'){
                                    $response_descrip = 'SUNAT: Anulación de boleta '.$dataSale->invoice_id.', con error de envio.';
                                }
                            }
                        }else{
                            
                            if($tipo=='AB'){
                                $response_descrip = 'SUNAT: La boleta  '.$dataSale->invoice_id.', ha sido anulada.';

                            }else{
                                if($dataSale->estado == 0){
                                $response_descrip = ' SYS: La anulacion de la boleta '.$dataSale->invoice_id.', se encuentra pendiente de envio.';
                                }else{
                                    $response_descrip = 'SUNAT: La boleta  '.$dataSale->invoice_id.', ha sido aceptada.';
    
                                }
                            }
                        }
                        $datos_actualizados = array(
                            'response_descrip' => $response_descrip,
                            'flg_response' => $flg_response,
                            'error_code'=>$error_code
                        );
                        // Realiza la actualización en la base de datos
                        $this->db->where('id', $sale);
                        $this->db->update('sales', $datos_actualizados);
                    }

                    if ($res['cod_ticket'] != "0") {
                        $file_name = $supplierID . "-" . $type . "-" . $date . "-" . $numero;
                        $ticket = $res['cod_ticket'];
                        $respuesta = $res['msj_sunat'];

                        log_message('error',json_encode($res));
                        $this->summaries_model->insertStatusTicket($respuesta, $file_name, $ticket, $codigo);
                    }

                    return true;
                }
            }
        }

        return false;
    }

    public function send_summary_voided($processed_date, $referenceDate, $items, $numero, $estado = 1)
    {

        if ($items) {

            $type = "RA";
            $supplierID = $this->session->userdata('ruc');
            $date = date("Ymd");

            $passFirma = $this->session->userdata('pass_firma');
            $usuSol = $this->session->userdata('user_sol');
            $passSol = $this->session->userdata('pass_sol');

            $issueDate = date("Y-m-d");
            $supplierName = $this->session->userdata('razon_social');

            /*if($this->session->userdata('beta') == 0){
                $urlDoc = "post/xmlenvio";
            }else{
                $urlDoc = "beta/post/xmlenvio";
            }*/
            $urlDoc = "baja_sunat.php";

            $header = array(
                "codigo" => $type, //type
                "secuencia" => (string)$numero, //serieNumero
                "ruc" => $supplierID, //supplierID
                "serie" => $date, //date
                "clave_firma" => $passFirma, //passFirma
                "usuariosol" => $usuSol, //usuSol
                "clavesol" => $passSol, //passSol
                "fecha_baja" => $issueDate, //issueDate
                "fecha_referencia" => $referenceDate, //referenceDate
                "razon_social" => $this->replace_invalid_caracters($supplierName), //supplierName
                'tipo_doc' => '6',
                "codigo_ubigeo" => "",
                "direccion" => "",
                "direccion_departamento" => "",
                "direccion_provincia" => "",
                "direccion_distrito" => "",
                "direccion_codigopais" => "PE",
                'tipo_proceso' => $this->session->userdata('beta'),
            );

            $sales_ids;
            $cant = 0;
            foreach ($items as $item) {

                $documentTypeCode = "01";
                $documentID = $item->ruc;
                $serieNumero = $item->invoice_id;
                $datos = explode('-', $serieNumero, 2);
                $serie = $datos[0];
                $sNumero = $datos[1];
                $reasonDescription = "CANCELACION";

                $cant = $cant + 1;
                $itemArray[$cant] = array(
                    "TIPO_COMPROBANTE" => $documentTypeCode,
                    "SERIE" => $serie,
                    "NUMERO" => $sNumero,
                    //"reasonDescription" => $item->grand_total
                    "MOTIVO" => $reasonDescription
                );

                $sales_ids[] = $item->id;
            }

            //datos a enviar
            $postData["header"] = $header;
            $postData["item"] = $itemArray;

            // Create the context for the request
            $context = stream_context_create(array(
                'http' => array(
                    'method' => 'POST',
                    'header' => "Authorization: application/json\r\n" .
                        "Content-Type: application/json\r\n",
                    'content' => json_encode($postData)
                )
            ));

            // Send the request
            $url = $this->config->item('aws_url');
            $response = file_get_contents($url . $urlDoc, FALSE, $context);
            /************************************************/
            if (file_exists("datos.txt")) {
                $archivo = fopen("datos.txt", "a");
                fwrite($archivo, PHP_EOL . json_encode($postData) . PHP_EOL . json_encode($response) . PHP_EOL);
                fclose($archivo);
            } else {
                $archivo = fopen("datos.txt", "w");
                fwrite($archivo, PHP_EOL . json_encode($postData) . PHP_EOL . json_encode($response) . PHP_EOL);
                fclose($archivo);
            }
            /****************************************************/
            // Check for errors
            // if($response === FALSE){
            //     die(json_encode($postData));
            // }

            if ($response) {

                $res = json_decode($response, TRUE);

                if ($estado == 0) {
                    $tipo = "AF";
                } else {
                    $tipo = "RF";
                }

                /*if($res[1] != 0 || $res[0] == 0){
                    $status = 1;
                }else{
                    $status = 2;
                }*/
                if ($res['hash_cdr'] == "") {
                    $status = 1;
                    $flg_response = "0";

                    if (isset($res["cod_sunat"])) {
                        $error_code = substr($res["cod_sunat"], 16, strlen($res["cod_sunat"]));
                        $respuesta = $res["mensaje"];
                    } else {
                        $error_code = "no definido";
                        $respuesta = "error al conectarse con sunat";
                    }
                } else {
                    $status = 2;
                    $flg_response = "1";
                    $error_code = "0";
                    $codigo = 2;
                }

                $summary_voided = array(
                    'issue_date' => $issueDate,
                    'number' => $numero,
                    'file_name' => $supplierID . "-" . $type . "-" . $date . "-" . $numero,
                    'ticket' => $res['cod_ticket'], //*****Renato TRJ023 25/04/2019   ********,
                    'reference_date' => $referenceDate,
                    'processed_date' => $processed_date,
                    'type' => $tipo,
                    'flg_response' => $flg_response,
                    'error_code' => $error_code,
                    'status' => $status,
                    'uCrea' => $this->session->userdata('user_id'),
                    'fCrea' => date('Y-m-d H:i:s'),
                    'uActualiza' => NULL,
                    'fActualiza' => NULL,
                    'estado' => 1,
                );

                if ($this->db->insert('send_invoice', $summary_voided)) {

                    $summary_id = $this->db->insert_id();

                    $data = array(
                        'send_invoice_id' => $summary_id,
                        'sale_id' => NULL,
                        'uCrea' => $this->session->userdata('user_id'),
                        'fCrea' => date('Y-m-d H:i:s'),
                        'estado' => 1,
                    );

                    foreach ($sales_ids as $sale) {
                        $data["sale_id"] = $sale;
                        $this->db->insert('send_invoice_items', $data);

                        $this->db->where('id', $sale);
                        $query = $this->db->get('sales');

                        $dataSale = $query->row();
                        if($flg_response==0 || $error_code !=0){
                            if($error_code == '0100'){
                                $response_descrip = 'SYS: Resumen de envio que incluye la factura '.$dataSale->invoice_id.', pendiente de respuesta de SUNAT.';
                            }else{
                                $response_descrip = 'SUNAT:'.$respuesta;
                            }
                        }else{
                            $response_descrip = 'SUNAT: La Factura  '.$dataSale->invoice_id.', ha sido anulada.';
                        }
                        $datos_actualizados = array(
                            'response_descrip' => $response_descrip,
                            'flg_response' => $flg_response,
                            'error_code'=>$error_code
                        );
                        // Realiza la actualización en la base de datos
                        $this->db->where('id', $sale);
                        $this->db->update('sales', $datos_actualizados);
                    }

                    if ($res['cod_ticket'] != "0") {
                        $file_name = $supplierID . "-" . $type . "-" . $date . "-" . $numero;
                        $ticket = $res['cod_ticket'];
                        $respuesta = $res['msj_sunat'];

                        $this->summaries_model->insertStatusTicket($respuesta, $file_name, $ticket, $codigo);
                    }

                    return true;
                }
            }
        }

        return false;
    }

    public function getSummaryByDate($fecha, $baja = null)
    {

        $this->db->select("send_invoice.*", FALSE)
            ->order_by('number', 'desc')
            ->where("issue_date LIKE '%" . $fecha . "%'");

        $this->db->where("type NOT LIKE '%RF%'");

        if ($baja) {
            $this->db->where("type LIKE '%AF%'");
        } else {
            $this->db->where("type NOT LIKE '%AF%'");
        }

        $q = $this->db->get_where('send_invoice', array('estado' => 1), 1);
        if ($q->num_rows() > 0) {
            return $q->row();
        }
        return FALSE;
    }

    public function getNullSales($date, $endDate = NULL)
    {
        if (!$endDate) {
            $endDate = strtotime('+1 day', strtotime($date));
            $endDate = date('Y-m-d 00:00:00', $endDate);
        }

        $this->db->select('sales.*', FALSE)
            ->where('sales.date >', $date)
            ->where('sales.date <', $endDate);

        $q = $this->db->get_where('sales', array('grand_total' => 0));

        if ($q->num_rows() > 0) {
            return $q->row();
        }
        return FALSE;
    }

    public function updateSaleCab($id, $data)
    {
        if ($this->db->update('sales', $data, array('id' => $id))) {
            return TRUE;
        }
        return false;
    }

    public function date_unique($str)
    {
        $q = $this->db->limit(1)
            ->where("type NOT LIKE '%RF%'")
            ->get_where('send_invoice', array('processed_date' => $str));
        // die(json_encode($q->num_rows()));

        $unique = false;
        if ($q->num_rows() == 0) {
            $unique = true;
        }

        return $unique;
    }

    public function fechaResumen($id)
    {

        $q = $this->db->get_where('send_invoice', array('id' => $id), 1);

        if ($q->num_rows() > 0) {
            return $q->row();
        }
        return FALSE;
    }

    //************TRJ042 - ALEXANDER ROCA - 30/04/2019************	
    public function getSummaryUndoSalesReenvio($date, $num = 2, $document_type = 1, $dateReferencia)
    {
        $endDate = strtotime('+1 day', strtotime($date));
        $endDate = date('Y-m-d 00:00:00', $endDate);

        $endDate2 = strtotime('+1 day', strtotime($dateReferencia));
        $endDate2 = date('Y-m-d 00:00:00', $endDate2);

        $this->db->select('sales.*,customers.document_type_id as tipo_documento, customers.cf1 as dni, customers.cf2 as ruc', FALSE)
            ->join('customers', 'customers.id=sales.customer_id', 'left')
            ->order_by('sales.date')
            // ->where('sales.status !=', "DEBE")
            ->where('sales.invoice_id !=', NULL)
            ->where('sales.updated_at >', $date)
            ->where('sales.updated_at <', $endDate)
            ->where('sales.date >', $dateReferencia)
            ->where('sales.date <', $endDate2)
            ->where('sales.estado =', 0);

        $q = $this->db->get_where('sales', array('document_type' => $document_type));

        $dateb = true;
        if ($q->num_rows() > 0) {
            foreach (($q->result()) as $row) {
                $data[] = $row;

                if ($dateb) {
                    $datet = explode(' ', $row->date, 2);
                    $firstDate = $datet[0];
                    $dateb = false;
                }
            }

            $items = (array)$data;
            $summary;
            $processed_date = $date;
            $date = $firstDate;

            $count = 0;
            foreach ($items as $item) {

                $count = $count + 1;
                $datef = explode(' ', $item->date, 2);
                $referenceDate = $datef[0];

                if ($date != $referenceDate) {
                    if ($document_type == 2) {
                        $this->send_summary_voided($processed_date, $date, $summary, $num, 0);
                    } else if ($document_type == 1) {
                        $this->send_summary($processed_date, $date, $summary, $num, 0);
                    }
                    $num = $num + 1;
                    unset($summary);
                    $summary[] = $item;
                } else {
                    $summary[] = $item;
                }

                $date = $referenceDate;

                if ($q->num_rows() == $count) {
                    if ($document_type == 2) {
                        $this->send_summary_voided($processed_date, $date, $summary, $num, 0);
                    } else if ($document_type == 1) {
                        $this->send_summary($processed_date, $date, $summary, $num, 0);
                    }
                }
            }

            return true;
        }
        return false;
    }

    public function addSummaryReenvio($fecha, $tipo, $fechaReferencia)
    {

        // $fecha = date('2018-10-29');
        $issueDate = date("Y-m-d");
        $RB = false;
        $AB = false;
        $AF = false;

        if ($tipo == 'RB') {
            $summary1 = $this->getSummaryByDate($issueDate);

            if ($summary1->number) {
                $number1 = (int)$summary1->number + 1;
            } else {
                $number1 = 1;
            }

            if ($this->getSummaryAllSales($fecha, $number1)) {
                $RB = true;
            }

            if ($RB) {
                return true;
            }

            return false;
        }



        if ($tipo == 'AB') {
            $summary2 = $this->getSummaryByDate($issueDate);

            if ($summary2->number) {
                $number2 = (int)$summary2->number + 1;
            } else {
                $number2 = 1;
            }

            if ($this->getSummaryUndoSalesReenvio($fecha, $number2, 1, $fechaReferencia)) {
                $AB = true;
            }

            if ($AB) {
                return true;
            }
            return false;
        }


        if ($tipo == 'AF') {
            $summary_voided = $this->getSummaryByDate($issueDate, $baja = true);
            if ($summary_voided->number) {
                $number3 = (int)$summary_voided->number + 1;
            } else {
                $number3 = 1;
            }

            if ($this->getSummaryUndoSalesReenvio($fecha, $number3, 2, $fechaReferencia)) {
                $AF = true;
            }

            if ($AF) {
                return true;
            }
            return false;
        }


        return false;
    }

    public function updateSendInvoiceReenvio($id)
    {
        if ($this->db->update('send_invoice', array('estado' => 0), array('id' => $id))) {

            if ($this->updateSendInvoiceItemsReenvio($id)) {
                return TRUE;
            }
        }
        return false;
    }

    public function updateSendInvoiceItemsReenvio($id)
    {
        if ($this->db->update('send_invoice_items', array('estado' => 0), array('send_invoice_id' => $id))) {
            return TRUE;
        }
        return false;
    }

    //************TRJ042 - ALEXANDER ROCA - 30/04/2019************	
    //***********TRJO71 - KENY PONTE -3/12/2019*********************** */
    public function consultTicket()
    {

        $query = $this->db->query("SELECT ticket, file_name FROM `tec_send_invoice`
        WHERE status_ticket='' AND estado = 1 AND  ticket > 99999999999999");
        if ($query) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
    }

    public function insertStatusTicket($respuesta, $file_name, $ticket, $codigo)
    {
        try {
            $sp = $this->db->query("CALL insert_consult_ticket('" . $respuesta . "','" . $file_name . "','" . $ticket . "'," . $codigo . ")");
        } catch (mysqli_sql_exception $e) {
            throw $e;
        }
    }

    public function ConsultTicktesForID($id)
    {
        $query =  $this->db->query("SELECT file_name,ticket FROM tec_send_invoice WHERE  id=" . $id);
        if ($query->row() > 0) {
            foreach ($query->result() as $row) {
                $data = $row;
            }
            return $data;
        }
    }
    //*********************TRJ071 - KENY PONTE - 3/12/2019********** */  

    public function consultaLimitSummaries()
    {

        $this->db->select('summaries_limit');
        $this->db->from('settings');
        $this->db->where('setting_id', 1);
        $q = $this->db->get();
        if ($q->num_rows() > 0) {
            return $q->row();
        }
        return false;
    }
    public function replace_invalid_caracters($cadena)
    {
        $cadena = str_replace("'", "", $cadena);
        $cadena = str_replace("#", "", $cadena);
        $cadena = str_replace("$", "", $cadena);
        $cadena = str_replace("%", "", $cadena);
        $cadena = str_replace("&", "", $cadena);
        $cadena = str_replace("'", "", $cadena);
        $cadena = str_replace("(", "", $cadena);
        $cadena = str_replace(")", "", $cadena);
        $cadena = str_replace("*", "", $cadena);
        $cadena = str_replace("+", "", $cadena);
        $cadena = str_replace("-", "", $cadena);
        $cadena = str_replace(".", "", $cadena);
        $cadena = str_replace("/", "", $cadena);
        $cadena = str_replace("<", "", $cadena);
        $cadena = str_replace("=", "", $cadena);
        $cadena = str_replace(">", "", $cadena);
        $cadena = str_replace("?", "", $cadena);
        $cadena = str_replace("@", "", $cadena);
        $cadena = str_replace("[", "", $cadena);
        $cadena = str_replace("\\", "", $cadena);
        $cadena = str_replace("]", "", $cadena);
        $cadena = str_replace("^", "", $cadena);
        $cadena = str_replace("_", "", $cadena);
        $cadena = str_replace("`", "", $cadena);
        $cadena = str_replace("{", "", $cadena);
        $cadena = str_replace("|", "", $cadena);
        $cadena = str_replace("}", "", $cadena);
        $cadena = str_replace("~", "", $cadena);
        $cadena = str_replace("¡", "", $cadena);
        $cadena = str_replace("¢", "", $cadena);
        $cadena = str_replace("£", "", $cadena);
        $cadena = str_replace("¤", "", $cadena);
        $cadena = str_replace("¥", "", $cadena);
        $cadena = str_replace("¦", "", $cadena);
        $cadena = str_replace("§", "", $cadena);
        $cadena = str_replace("¨", "", $cadena);
        $cadena = str_replace("©", "", $cadena);
        $cadena = str_replace("ª", "", $cadena);
        $cadena = str_replace("«", "", $cadena);
        $cadena = str_replace("¬", "", $cadena);
        $cadena = str_replace("®", "", $cadena);
        $cadena = str_replace("°", "", $cadena);
        $cadena = str_replace("±", "", $cadena);
        $cadena = str_replace("²", "", $cadena);
        $cadena = str_replace("³", "", $cadena);
        $cadena = str_replace("´", "", $cadena);
        $cadena = str_replace("µ", "", $cadena);
        $cadena = str_replace("¶", "", $cadena);
        $cadena = str_replace("·", "", $cadena);
        $cadena = str_replace("¸", "", $cadena);
        $cadena = str_replace("¹", "", $cadena);
        $cadena = str_replace("º", "", $cadena);
        $cadena = str_replace("»", "", $cadena);
        $cadena = str_replace("¼", "", $cadena);
        $cadena = str_replace("½", "", $cadena);
        $cadena = str_replace("¾", "", $cadena);
        $cadena = str_replace("¿", "", $cadena);
        $cadena = str_replace("À", "A", $cadena);
        $cadena = str_replace("Á", "A", $cadena);
        $cadena = str_replace("Â", "A", $cadena);
        $cadena = str_replace("Ã", "A", $cadena);
        $cadena = str_replace("Ä", "A", $cadena);
        $cadena = str_replace("Å", "A", $cadena);
        $cadena = str_replace("Æ", "", $cadena);
        $cadena = str_replace("Ç", "", $cadena);
        $cadena = str_replace("È", "E", $cadena);
        $cadena = str_replace("É", "E", $cadena);
        $cadena = str_replace("Ê", "E", $cadena);
        $cadena = str_replace("Ë", "E", $cadena);
        $cadena = str_replace("Ì", "I", $cadena);
        $cadena = str_replace("Í", "I", $cadena);
        $cadena = str_replace("Î", "I", $cadena);
        $cadena = str_replace("Ï", "I", $cadena);
        $cadena = str_replace("Ð", "", $cadena);
        $cadena = str_replace("Ñ", "N", $cadena);
        $cadena = str_replace("Ò", "O", $cadena);
        $cadena = str_replace("Ó", "O", $cadena);
        $cadena = str_replace("Ô", "O", $cadena);
        $cadena = str_replace("Õ", "O", $cadena);
        $cadena = str_replace("Ö", "O", $cadena);
        $cadena = str_replace("×", "", $cadena);
        $cadena = str_replace("Ø", "", $cadena);
        $cadena = str_replace("Ù", "U", $cadena);
        $cadena = str_replace("Ú", "U", $cadena);
        $cadena = str_replace("Û", "U", $cadena);
        $cadena = str_replace("Ü", "U", $cadena);
        $cadena = str_replace("Ý", "Y", $cadena);
        $cadena = str_replace("Þ", "", $cadena);
        $cadena = str_replace("ß", "", $cadena);
        $cadena = str_replace("à", "a", $cadena);
        $cadena = str_replace("á", "a", $cadena);
        $cadena = str_replace("â", "a", $cadena);
        $cadena = str_replace("ã", "a", $cadena);
        $cadena = str_replace("ä", "a", $cadena);
        $cadena = str_replace("å", "a", $cadena);
        $cadena = str_replace("æ", "", $cadena);
        $cadena = str_replace("ç", "", $cadena);
        $cadena = str_replace("è", "e", $cadena);
        $cadena = str_replace("é", "e", $cadena);
        $cadena = str_replace("ê", "e", $cadena);
        $cadena = str_replace("ë", "e", $cadena);
        $cadena = str_replace("ì", "i", $cadena);
        $cadena = str_replace("í", "i", $cadena);
        $cadena = str_replace("î", "i", $cadena);
        $cadena = str_replace("ï", "i", $cadena);
        $cadena = str_replace("ð", "o", $cadena);
        $cadena = str_replace("ñ", "n", $cadena);
        $cadena = str_replace("ò", "o", $cadena);
        $cadena = str_replace("ó", "o", $cadena);
        $cadena = str_replace("ô", "o", $cadena);
        $cadena = str_replace("õ", "o", $cadena);
        $cadena = str_replace("ö", "o", $cadena);
        $cadena = str_replace("÷", "", $cadena);
        $cadena = str_replace("ø", "", $cadena);
        $cadena = str_replace("ù", "u", $cadena);
        $cadena = str_replace("ú", "u", $cadena);
        $cadena = str_replace("û", "u", $cadena);
        $cadena = str_replace("ü", "u", $cadena);
        $cadena = str_replace("ý", "y", $cadena);
        $cadena = str_replace("þ", "", $cadena);
        $cadena = str_replace("ÿ", "", $cadena);
        $cadena = str_replace("Œ", "", $cadena);
        $cadena = str_replace("œ", "", $cadena);
        $cadena = str_replace("Š", "", $cadena);
        $cadena = str_replace("š", "", $cadena);
        $cadena = str_replace("Ÿ", "", $cadena);
        $cadena = str_replace("ƒ", "", $cadena);
        $cadena = str_replace("–", "", $cadena);
        $cadena = str_replace("—", "", $cadena);
        $cadena = str_replace("‘", "", $cadena);
        $cadena = str_replace("’", "", $cadena);
        $cadena = str_replace("‚", "", $cadena);
        $cadena = str_replace("“", "", $cadena);
        $cadena = str_replace("”", "", $cadena);
        $cadena = str_replace("„", "", $cadena);
        $cadena = str_replace("†", "", $cadena);
        $cadena = str_replace("‡", "", $cadena);
        $cadena = str_replace("•", "", $cadena);
        $cadena = str_replace("…", "", $cadena);
        $cadena = str_replace("‰", "", $cadena);
        $cadena = str_replace("€", "", $cadena);
        $cadena = str_replace("™", "", $cadena);

        return $cadena;
    }
}
