 <?php
    include_once("utiles.php");
    //error_reporting(E_ALL);
    error_reporting(E_ALL & ~E_NOTICE & ~E_WARNING);
    date_default_timezone_set('America/Panama');
    $iAffected_rows = 0;

    function Gen_Saldo_cxc($poConn2, $cpDatab2, $cpSuc_id2, $cpOp_Id2, $pcSaveLog2)
    {
        $Saldo_Cxc = '0.00';
        $records =0;
        if ($pcSaveLog2 == 1) {
            echo '*-------------------------->Gen_Saldo_cxc>-------------------------*';
            echo '--->Database :' . $cpDatab2;
            echo '--->Sucursal :' . $cpSuc_id2;
            echo '--->Cxc_Opid :' . $cpOp_Id2;
            echo '*------------------------------------------------------------------*';
        }

        $cFieldDB = 'ROUND(IFNULL(op_debito ,0),2) ';
        $cFieldCR = 'ROUND(IFNULL(op_credito,0),2) ';

        $sql = 'SELECT
        SUM(' . $cFieldCR . ')-SUM(' . $cFieldDB . ') AS abonos
        FROM ' . $cpDatab2 . '.operacion_cxc
        WHERE id_tipo in(25,21,22)
        AND op_rel ="' . $cpOp_Id2 . '"
        AND IFNULL(op_aptdo,0)=0 ';
        //AND suc_id="' . $cpSuc_id2 . '"';

        $data = getRow($poConn2, $sql, $records, false);

        if ($records == 0) {
            $Saldo_Cxc = '0.00';
        } else {
            $Saldo_Cxc = ((($data['abonos'] == '') or ($data['abonos'] == null)) ? '0.00' : $data['abonos']);
        }

        if ($pcSaveLog2 == 1) {
            echo 'SQL : ' . $sql;
            echo 'RESP: ' . $Saldo_Cxc;
            echo '*FIN-----------------------<Gen_Saldo_cxc<-------------------------*';
        }
        return $Saldo_Cxc;
    }

    function jsonmysql($poConn3, $cpDatab3, $JsonStr3, $TblName, $pcSaveLog3)
    {
        if ($pcSaveLog3 == 1) {
            echo '*-------------------------->jsonmysql--------------------------*';
            echo '--->Database :' . $cpDatab3;
            echo '--->Jason :' . $JsonStr3;
            echo '--->Table :' . $TblName;
            echo '*--------------------------------------------------------------*';
        }

        $data_arr = json_decode(utf8_encode($JsonStr3), TRUE);
        $xTable = $data_arr[$TblName];
        $regs_cnt = $data_arr['regs_cnt'];

        if ($pcSaveLog3 == 1) {
            echo 'REGISTROS: ' . $regs_cnt;
        }

        // echo 'son '.$regs."de la tabla".$TblName."<br>";
        $ib = 0;

        while ($ib < $regs_cnt) {
            /*
         * el valor presentado sería
         * $i antes del incremento
         * (post-incremento)
         */

            // echo 'vuelta nro: '.$ib;

            $opfinal = $xTable[$ib];

            $str_fields = '';
            $str_values = '';
            $str_update = '';
            $i = 0;
            $valuekey = '';
            $fieldkey = '';
            foreach ($opfinal as $field => $value) {

                $fieldkey = $field;
                $valuekey = $value;
                // if($i<9999) {
                if ($i > 0) {
                    $field = ',' . $field;
                    $fieldkey = ',' . $fieldkey;

                    if ($value == '') {
                        $value = $value . ',null';
                        $valuekey = 'null';
                    } else {
                        $value = ',"' . $value . '"';
                        $valuekey = '"' . $valuekey . '"';
                    }

                    // $field=','.$field.$i;
                    // $value=',"'.$value.$i.'"';
                } else {
                    $value = '"' . $value . '"';
                }

                // $str_update = $str_update . $fieldkey . '=' . $valuekey;
                // if ($ib == 0) {
                $str_update = $str_update . $fieldkey . '=VALUES(' . str_replace(',', '', $fieldkey) . ') ';
                // }
                $str_fields = $str_fields . $field;
                $str_values = $str_values . $value;
                $i = $i + 1;

                // }
            }

            // echo $str_update;

            $sql = "INSERT INTO " . $cpDatab3 . "." . $TblName . " (" . $str_fields . ") VALUES (" . $str_values . ") ON DUPLICATE KEY UPDATE " . $str_update . "";

            // echo $sql;
            $sql = str_replace("0000-00-00 00:00:00", "2009-01-01 01:01:01", $sql);
            $iAffected_rows = 0;
            $result = update_Sql($poConn3, $sql, $iAffected_rows);

            if ($pcSaveLog3 == 1) {
                logs2('SQL : ' . $sql);
                logs2('RES: ' . ($result ? 'true' : 'false'));
            }

            $ib++;

            // echo "inserta: ".$sql;
        }

        if ($pcSaveLog3 == 1) {
            echo '*FIN------------------------jsonmysql<-------------------------*';
        }
        return $result;
    }

    function empty2null($value)
    {
        return ($value === '' ? null : $value);
    }

    function cleanString($text)
    {
        $utf8 = array(
            '/[�����]/u'   =>   'a',
            '/[�����]/u'    =>   'A',
            '/[����]/u'     =>   'I',
            '/[����]/u'     =>   'i',
            '/[����]/u'     =>   'e',
            '/[����]/u'     =>   'E',
            '/[������]/u'   =>   'o',
            '/[�����]/u'    =>   'O',
            '/[����]/u'     =>   'u',
            '/[����]/u'     =>   'U',
            '/�/'           =>   'c',
            '/�/'           =>   'C',
            '/�/'           =>   'n',
            '/�/'           =>   'N',
            '/�/'           =>   '-', // UTF-8 hyphen to "normal" hyphen
            '/[�����]/u'    =>   ' ', // Literally a single quote
            '/[�����]/u'    =>   ' ', // Double quote
            '/ /'           =>   ' ', // nonbreaking space (equiv. to 0x160)
        );
        return preg_replace(array_keys($utf8), array_values($utf8), $text);
    }

    function jsonmysql2($tabla, $reg_p, $nombre, $connection1, $database, $pSaveLog)
    {

        // echo 'son '.$reg_p."de la tabla".$nombre."<br>";
        $ib = 0;

        while ($ib < $reg_p) {
            /*
         * el valor presentado serÃ­a
         * $i antes del incremento
         * (post-incremento)
         */

            // echo 'vuelta nro: '.$ib;

            $opfinal = $tabla[$ib];

            $cadena = '';
            $cadena2 = '';
            $cadena3 = '';
            $i = 0;
            $valuekey = '';
            $fieldkey = '';
            foreach ($opfinal as $field => $value) {

                $fieldkey = $field;
                $valuekey = $value;
                // if($i<9999) {
                if ($i > 0) {
                    $field = ',' . $field;
                    $fieldkey = ',' . $fieldkey;

                    if ($value == '') {
                        $value = $value . ',null';
                        $valuekey = 'null';
                    } else {
                        $value = ',"' . $value . '"';
                        $valuekey = '"' . $valuekey . '"';
                    }

                    // $field=','.$field.$i;
                    // $value=',"'.$value.$i.'"';
                } else {
                    $value = '"' . $value . '"';
                }

                // if ($ib == 0) {
                $cadena3 = $cadena3 . $fieldkey . '=VALUES(' . str_replace(',', '', $fieldkey) . ') ';
                // }
                $cadena = $cadena . $field;
                $cadena2 = $cadena2 . $value;
                $i = $i + 1;

                // }
            }

            // echo $cadena3;

            $sql = "insert into " . $database . "." . $nombre . " (" . $cadena . ") values (" . $cadena2 . ") ON DUPLICATE KEY UPDATE " . $cadena3 . "";
            if ($nombre == 'clientes') {
                // echo $sql;
            }
            $sql = str_replace("0000-00-00 00:00:00", "2009-01-01 01:01:01", $sql);
            $iAffected_rows = 0;
            $result = update_Sql($connection1, $sql, $iAffected_rows);

            if ($pSaveLog == '1') {
                logs2('Nombre : ' . $nombre);
                logs2('Sql    : ' . $sql);
                logs2('RES    : ' . ($result ? 'true' : 'false'));
            }

            $ib++;
        }

        return $result;
    }

    // CREADO POR: JOHNN GUTIERREZ 2021-02-17
    // ULTIMA MODIFICACION: 2021-02-17
    ?>
