
<?php
date_default_timezone_set("America/Panama");
session_start();
$_SESSION['name'] = $_POST['datos'];
$database = $_POST['dbname'];
//$_SESSION['suc_id'] = $_POST['suc_id'];
$_SESSION['dbname'] = $_POST['dbname'];

$_SESSION['suc_id'] = $_POST['suc_id'];
$_SESSION['est_id'] = $_POST['est_id'];
$pack = $_POST['pack'];

include 'conn.php';
include 'creacier_tmp2.php';
include 'sincro_cier2.php';
include 'funciones.php';

//$comillas= "\"";

$comillas = "'";

//$data = $_SESSION['name'];

$data = json_decode($_SESSION['name'], true);

//print_r($data);

//$json = $data;

//echo json_encode($json);

$books = $data;
$data33 = json_encode($data);

logs("llegooooooooooo total" . $_SESSION['name']);

//print_r($books);

$recibidos = $books['registros'];

$i = 0;

if ($connection->query($sql99) === true) {
    //   echo "New TMP created successfully";

//logs("Creando tabla Tmp: ".$sql99);

} else {
    logs("Error: " . $sql99 . "<br>" . $conn->error);
}

if ($connection->query($sql99d) === true) {
    //   echo "New TMP created successfully";

//logs("Creando tabla Tmp: ".$sql99);

} else {
    logs("Error: " . $sql99d . "<br>" . $conn->error);
}

if ($connection->query($sql99es) === true) {
    //   echo "New TMP created successfully";

    logs("Creando tabla Tmp: " . $sql99es);

} else {
    logs("Error: " . $sql99es . "<br>" . $conn->error);
}

if ($connection->query($sql99e) === true) {
    //   echo "New TMP created successfully";

//logs("Creando tabla Tmp: ".$sql99);

} else {
    logs("Error: " . $sql99d . "<br>" . $conn->error);
}

$books2 = $books["cierres"];

logs("llegooooooooooo2" . $books2);

$data33 = utf8_decode($books2);

logs("sql inicial dentro de : " . $data33);

foreach ($books2 as $book) {

// '". $book["op_id"]."',
    //'". $book["op_fecha"]."');";

//$strnombre = str_replace("'", "", $book["po_descripcion"]);
    $_SESSION['trigger'] = str_replace("'", "", $book["campo149"]);

    $sql = $sqlinsert . "

'" . $book["campo1"] . "',
'" . $book["campo2"] . "',
'" . $book["campo3"] . "',
'" . $book["campo4"] . "',
'" . $book["campo5"] . "',
'" . $book["campo6"] . "',
'" . $book["campo7"] . "',
'" . $book["campo8"] . "',
'" . $book["campo9"] . "',
'" . $book["campo10"] . "',
'" . $book["campo11"] . "',
'" . $book["campo12"] . "',
'" . $book["campo13"] . "',
'" . $book["campo14"] . "',
'" . $book["campo15"] . "',
'" . $book["campo16"] . "',
'" . $book["campo17"] . "',
'" . $book["campo18"] . "',
'" . $book["campo19"] . "',
'" . $book["campo20"] . "',
'" . $book["campo21"] . "',
'" . $book["campo22"] . "',
'" . $book["campo23"] . "',
'" . $book["campo24"] . "',
'" . $book["campo25"] . "',
'" . $book["campo26"] . "',
'" . $book["campo27"] . "',
'" . $book["campo28"] . "',
'" . $book["campo29"] . "',
'" . $book["campo30"] . "',
'" . $book["campo31"] . "',
'" . $book["campo32"] . "',
'" . $book["campo33"] . "',
'" . $book["campo34"] . "',
'" . $book["campo35"] . "',
'" . $book["campo36"] . "',
'" . $book["campo37"] . "',
'" . $book["campo38"] . "',
'" . $book["campo39"] . "',
'" . $book["campo40"] . "',
'" . $book["campo41"] . "',
'" . $book["campo42"] . "',
'" . $book["campo43"] . "',
'" . $book["campo44"] . "',
'" . $book["campo45"] . "',
'" . $book["campo46"] . "',
'" . $book["campo47"] . "',
'" . $book["campo48"] . "',
'" . $book["campo49"] . "',
'" . $book["campo50"] . "',
'" . $book["campo51"] . "',
'" . $book["campo52"] . "',
'" . $book["campo53"] . "',
'" . $book["campo54"] . "',
'" . $book["campo55"] . "',
'" . $book["campo56"] . "',
'" . $book["campo57"] . "',
'" . $book["campo58"] . "',
'" . $book["campo59"] . "',
'" . $book["campo60"] . "',
'" . $book["campo61"] . "',
'" . $book["campo62"] . "',
'" . $book["campo63"] . "',
'" . $book["campo64"] . "',
'" . $book["campo65"] . "',
'" . $book["campo66"] . "',
'" . $book["campo67"] . "',
'" . $book["campo68"] . "',
'" . $book["campo69"] . "',
'" . $book["campo70"] . "',
'" . $book["campo71"] . "',
'" . $book["campo72"] . "',
'" . $book["campo73"] . "',
'" . $book["campo74"] . "',
'" . $book["campo75"] . "',
'" . $book["campo76"] . "',
'" . $book["campo77"] . "',
'" . $book["campo78"] . "',
'" . $book["campo79"] . "',
'" . $book["campo80"] . "',
'" . $book["campo81"] . "',
'" . $book["campo82"] . "',
'" . $book["campo83"] . "',
'" . $book["campo84"] . "',
'" . $book["campo85"] . "',
'" . $book["campo86"] . "',
'" . $book["campo87"] . "',
'" . $book["campo88"] . "',
'" . $book["campo89"] . "',
'" . $book["campo90"] . "',
'" . $book["campo91"] . "',
'" . $book["campo92"] . "',
'" . $book["campo93"] . "',
'" . $book["campo94"] . "',
'" . $book["campo95"] . "',
'" . $book["campo96"] . "',
'" . $book["campo97"] . "',
'" . $book["campo98"] . "',
'" . $book["campo99"] . "',
'" . $book["campo100"] . "',
'" . $book["campo101"] . "',
'" . $book["campo102"] . "',
'" . $book["campo103"] . "',
'" . $book["campo104"] . "',
'" . $book["campo105"] . "',
'" . $book["campo106"] . "',
'" . $book["campo107"] . "',
'" . $book["campo108"] . "',
'" . $book["campo109"] . "',
'" . $book["campo110"] . "',
'" . $book["campo111"] . "',
'" . $book["campo112"] . "',
'" . $book["campo113"] . "',

'" . $book["campo114"] . "',

'" . $book["campo115"] . "',


'" . $book["campo116"] . "',
'" . $book["campo117"] . "',
'" . $book["campo118"] . "',
'" . $book["campo119"] . "',
'" . $book["campo120"] . "',
'" . $book["campo121"] . "',
'" . $book["campo122"] . "',
'" . $book["campo123"] . "',
'" . $book["campo124"] . "',
'" . $book["campo125"] . "',
'" . $book["campo126"] . "',
'" . $book["campo127"] . "',
'" . $book["campo128"] . "',
'" . $book["campo129"] . "',
'" . $book["campo130"] . "',
'" . $book["campo131"] . "',
'" . $book["campo132"] . "',
'" . $book["campo133"] . "',
'" . $book["campo134"] . "',
'" . $book["campo135"] . "',
'" . $book["campo136"] . "',
'" . $book["campo137"] . "',
'" . $book["campo138"] . "',
'" . $book["campo139"] . "',
'" . $book["campo140"] . "',
'" . $book["campo141"] . "',
'" . $book["campo142"] . "',
'" . $book["campo143"] . "',
'" . $book["campo144"] . "',
'" . $book["campo145"] . "',
'" . $book["campo146"] . "',
'" . $book["campo147"] . "',
'" . $book["campo148"] . "',
'" . $book["campo149"] . "',
'" . $book["campo150"] . "',
'" . $book["campo151"] . "',
'" . $book["campo152"] . "',
'" . $book["campo153"] . "',
'" . $book["campo154"] . "',
'" . $book["campo155"] . "',
'" . $book["campo156"] . "',
'0',
'0',
'" . $book["campo159"] . "',
'" . $book["campo160"] . "',
'" . $book["campo161"] . "',
'" . $book["campo162"] . "',
'" . $book["campo163"] . "',
'" . $book["campo164"] . "',
'" . $book["campo165"] . "',
'" . $book["campo166"] . "',
'" . $book["campo167"] . "',
'" . $book["campo168"] . "',
'" . $book["campo169"] . "',
'" . $book["campo170"] . "',
'" . $book["campo171"] . "',
'" . $book["campo172"] . "',
'" . $book["campo173"] . "',
'" . $book["campo174"] . "',
'" . $book["campo175"] . "',
'" . $book["campo176"] . "',
'" . $book["campo177"] . "',
'" . $book["campo178"] . "',
'" . $book["campo179"] . "',
'" . $book["campo180"] . "',
'" . $book["campo181"] . "',
'" . $book["campo182"] . "',
'" . $book["campo183"] . "',
'" . $book["campo184"] . "',
'" . $book["campo185"] . "',
'" . $book["campo186"] . "',
'" . $book["campo187"] . "',
'" . $book["campo188"] . "',
'" . $book["campo189"] . "',
'" . $book["campo190"] . "',
'" . $book["campo191"] . "',
'" . $book["campo192"] . "',
'" . $book["campo193"] . "',
'" . $book["campo194"] . "',
'" . $book["campo195"] . "',
'" . $book["campo196"] . "',
'" . $book["campo197"] . "',
'" . $book["campo198"] . "',
'" . $book["campo199"] . "',
'" . $book["campo200"] . "',
'" . $book["campo201"] . "',
'" . $book["campo202"] . "',
'" . $book["campo203"] . "',
'" . $book["campo204"] . "',
'" . $book["campo205"] . "',
'" . $book["campo206"] . "',
'" . $book["campo207"] . "',
'" . $book["campo208"] . "',
'" . $book["campo209"] . "',
'" . $book["campo210"] . "',
'" . $book["campo211"] . "',
'" . $book["campo212"] . "',
'" . $book["campo213"] . "',

'" . $book["campo217"] . "',
'" . $book["campo218"] . "',
'" . $book["campo219"] . "',
'" . $book["campo220"] . "',
'" . $book["campo221"] . "',
'" . $book["campo222"] . "',
'" . $book["campo223"] . "',
'" . $book["campo224"] . "',
'" . $book["campo225"] . "',
'" . $book["campo226"] . "',
'" . $book["campo227"] . "',
'" . $book["campo228"] . "',
'" . $book["campo229"] . "',
'" . $book["campo230"] . "',
'" . $book["campo231"] . "',
'" . $book["campo232"] . "',
'" . $book["campo233"] . "',
'" . $book["campo234"] . "',
'" . $book["campo235"] . "',
'" . $book["campo236"] . "',
'" . $book["campo237"] . "',
'" . $book["campo238"] . "',
'" . $book["campo239"] . "',
'" . $book["campo240"] . "',
'" . $book["campo241"] . "',
'" . $book["campo242"] . "',
'" . $book["campo243"] . "',
'" . $book["campo244"] . "',

'" . $book["campo245"] . "',
'" . $book["campo246"] . "',
'" . $book["campo247"] . "',

'" . $book["campo248"] . "',
'" . $book["campo249"] . "',
'" . $book["campo250"] . "',
'" . $book["campo251"] . "',
'" . $book["campo252"] . "',
'" . $book["campo253"] . "',
'" . $book["campo254"] . "',
'" . $book["campo255"] . "',
'" . $book["campo256"] . "',


'" . $book["campo257"] . "')";

//'".$book["campo245"]."',
    //'".$book["campo246"]."',
    //'".$book["campo247"]."',

//echo 'sql ' . $sql ."<br>";

//. mysqli_error($sql)

    logs("insertando cierreeeeeeeeeeeeeeeeee : " . $sql);

    if ($connection->query($sql)) {
        $msg = array("status" => 1, "msg" => "Your record inserted successfully");

        $i = $i + 1;

        logs("insertando cierreeeeeeeeeeeeeeeeee : " . $sql);
    } else {
        echo "Error: " . $sql . "<br>" . mysqli_error($connection);

        logs("Error: " . $sql . "<br>" . mysqli_error($connection));
    }

}

$books2e = $books["cierrese"];

foreach ($books2e as $booke) {

// '". $book["op_id"]."',
    //'". $book["op_fecha"]."');";

//$strnombre = str_replace("'", "", $book["po_descripcion"]);
    //$strop_num_ncf = str_replace("'", "", $book["op_num_ncf"]);

    $sqle = $sqlinserte . "

'" . $booke["campo1"] . "',
'" . $booke["campo2"] . "',
'" . $booke["campo3"] . "',
'" . $booke["campo4"] . "',
'" . $booke["campo5"] . "',
'" . $booke["campo6"] . "',
'" . $booke["campo7"] . "',
'" . $booke["campo8"] . "',
'" . $booke["campo9"] . "',
'" . $booke["campo10"] . "',
'" . $booke["campo11"] . "',
'" . $booke["campo12"] . "',
'" . $booke["campo13"] . "',
'" . $booke["campo14"] . "',
'" . $booke["campo15"] . "',
'" . $booke["campo16"] . "',
'" . $booke["campo17"] . "')";

//logs("insertando cierre e: " . $sqle );

//. mysqli_error($sql)

    if ($connection->query($sqle)) {
        $msg = array("status" => 1, "msg" => "Your record inserted successfully");

//$i=$i+1;

    } else {
        echo "Error: " . $sqle . "<br>" . mysqli_error($connection);

        logs("Error: " . $sqle . "<br>" . mysqli_error($connection));
    }

}

$books2d = $books["cierresd"];

foreach ($books2d as $bookd) {

// '". $book["op_id"]."',
    //'". $book["op_fecha"]."');";

//$strnombre = str_replace("'", "", $book["po_descripcion"]);
    //$strop_num_ncf = str_replace("'", "", $book["op_num_ncf"]);

    $sqld = $sqlinsertd . "

'" . $bookd["campo1"] . "',
'" . $bookd["campo2"] . "',
'" . $bookd["campo3"] . "',
'" . $bookd["campo4"] . "',
'" . $bookd["campo5"] . "',
'" . $bookd["campo6"] . "',
'" . $bookd["campo7"] . "',
'" . $bookd["campo8"] . "',
'" . $bookd["campo9"] . "')";

//logs( 'sql ' . $sqld );

//. mysqli_error($sql)

//logs("insertando cierres d: " . $sqld  );

    if ($connection->query($sqld)) {
        $msg = array("status" => 1, "msg" => "Your record inserted successfully");

//$i=$i+1;

    } else {
        echo "Error: " . $sqld . "<br>" . mysqli_error($connection);

        logs("Error: " . $sqld . "<br>" . mysqli_error($connection));
    }

}
;

logs("valorr:  " . $recibidos . " valor i: " . $i);

$books2es = $books["cierres_es"];

foreach ($books2es as $bookes) {

// '". $book["op_id"]."',
    //'". $book["op_fecha"]."');";

//$strnombre = str_replace("'", "", $book["po_descripcion"]);
    //$strop_num_ncf = str_replace("'", "", $book["op_num_ncf"]);

    $sqles = $sqlinsertes . "

'" . $bookes["campo1"] . "',
'" . $bookes["campo2"] . "',
'" . $bookes["campo3"] . "',
'" . $bookes["campo4"] . "',
'" . $bookes["campo5"] . "',
'" . $bookes["campo6"] . "',
'" . $bookes["campo7"] . "',
'" . $bookes["campo8"] . "',
'" . $bookes["campo9"] . "',
'" . $bookes["campo10"] . "',
'" . $bookes["campo11"] . "',
'" . $bookes["campo12"] . "',
'" . $bookes["campo13"] . "',
'" . $bookes["campo14"] . "',
'" . $bookes["campo15"] . "',
'" . $bookes["campo16"] . "',
'" . $bookes["campo17"] . "',
'" . $bookes["campo18"] . "',
'" . $bookes["campo19"] . "',
'" . $bookes["campo20"] . "',
'" . $bookes["campo21"] . "',
'" . $bookes["campo22"] . "',
'" . $bookes["campo23"] . "',
'" . $bookes["campo24"] . "',
'" . $bookes["campo25"] . "',
'" . $bookes["campo26"] . "')";

//logs("insertando cierre e: " . $sqles );

//. mysqli_error($sql)

    if ($connection->query($sqles)) {
        $msg = array("status" => 1, "msg" => "Your record inserted successfully");

//$i=$i+1;

    } else {
        echo "Error: " . $sqle . "<br>" . mysqli_error($connection);

        logs("Error: " . $sqle . "<br>" . mysqli_error($connection));
    }

}

$sqlb1 = 'delete from ' . $_SESSION['dbname'] . '.cierres_gen_v2 where trig_unic="' . $_SESSION['trigger'] . '" limit 1';
logs("borra  =" . $sqlb1);
if ($connection->query($sqlb1) === true) {
} else {logs("Error: " . $sqlb1 . " " . $conn->error);}
;

$sqlb2 = 'delete from ' . $_SESSION['dbname'] . '.operacion_det_pago_fx where trig_unic2="' . $_SESSION['trigger'] . '" ';
logs("borra  =" . $sqlb2);
if ($connection->query($sqlb2) === true) {
} else {logs("Error: " . $sqlb2 . " " . $conn->error);}
;

$sqlb3 = 'delete from ' . $_SESSION['dbname'] . '.operacion_det_desc_fx where trig_unic2="' . $_SESSION['trigger'] . '" ';
logs("borra  =" . $sqlb3);
if ($connection->query($sqlb3) === true) {
} else {logs("Error: " . $sqlb3 . " " . $conn->error);}
;

$sqlb4 = 'delete from ' . $_SESSION['dbname'] . '.operacion_det_pago_ax where trig_unic2="' . $_SESSION['trigger'] . '" ';
logs("borra  =" . $sqlb4);
if ($connection->query($sqlb4) === true) {
} else {logs("Error: " . $sqlb4 . " " . $conn->error);}
;

$sqlb5 = 'delete from ' . $_SESSION['dbname'] . '.entradas_salidas_det_pago_fx where trig_unic2="' . $_SESSION['trigger'] . '" ';
logs("borra  =" . $sqlb5);
if ($connection->query($sqlb5) === true) {
} else {logs("Error: " . $sqlb5 . " " . $conn->error);}
;

$sqlb6 = 'delete from ' . $_SESSION['dbname'] . '.entradas_salidas_desg where trig_unic="' . $_SESSION['trigger'] . '" ';
logs("borra  =" . $sqlb6);
if ($connection->query($sqlb6) === true) {
} else {logs("Error: " . $sqlb6 . " " . $conn->error);}
;

$sqlb7 = 'delete from ' . $_SESSION['dbname'] . '.entradas_salidas_det_pago where trig_unic="' . $_SESSION['trigger'] . '" ';
logs("borra  =" . $sqlb7);
if ($connection->query($sqlb7) === true) {
} else {logs("Error: " . $sqlb7 . " " . $conn->error);}
;

if ($recibidos == $i) {

    logs(" Entro a actualizar los cierresssssssssss: ");

    if ($connection->query($sqlop2) === true) {
        logs("insert op   =" . $sqlop2);

//logs("insert op: ". $sqlop2);

        logs("cierre principal");
    } else {
        echo "Error: " . $sqlop2 . "<br>" . $conn->error;

        logs("Error: " . $sqlop2 . "<br>" . $conn->error);
    }
    ;

    if ($connection->query($sql2a) === true) {
        //    echo "insert op   =".$sqlop2;

        logs("insert pago 2aaaaaaaaaaaa: " . $sql2a);
    } else {
        echo "Error: " . $sql2a . "<br>" . $conn->error;

        logs("Error: " . $sql2a . "<br>" . $conn->error);
    }

    if ($connection->query($sql3a) === true) {
        //    echo "insert op   =".$sqlop2;

        logs("insert desc: " . $sqlop3a);
    } else {
        echo "Error: " . $sql3a . "<br>" . $conn->error;

        logs("Error: " . $sql3a . "<br>" . $conn->error);
    }

    if ($connection->query($sql3d) === true) {
        //    echo "insert op   =".$sqlop2;

//logs("insert pago: ". $sqlop3d);
    } else {
        echo "Error: " . $sql3d . "<br>" . $conn->error;

        logs("Error: " . $sql3d . "<br>" . $conn->error);
    }

    if ($connection->query($sql4d) === true) {
        //    echo "insert op   =".$sqlop2;

//logs("insert op: ". $sqlop2);
    } else {
        echo "Error: " . $sql4d . "<br>" . $conn->error;

        logs("Error: " . $sql4d . "<br>" . $conn->error);
    }

    if ($connection->query($sql5d) === true) {
        //    echo "insert op   =".$sqlop2;

//logs("insert op: ". $sqlop2);
    } else {
        echo "Error: " . $sql5d . "<br>" . $conn->error;

        logs("Error: " . $sql5d . "<br>" . $conn->error);
    }
/*
if ($connection->query($sql6d) === TRUE) {
//    echo "insert op   =".$sqlop2;

logs("insert cierre entrada: ". $sql6d);
} else {
echo "Error: " . $sql6d . "<br>" . $conn->error;

logs("Error: " . $sql6d . "<br>" . $conn->error);
}
 */

    if ($connection->query($sql6d_es) === true) {
        //    echo "insert op   =".$sqlop2;

        logs("insert cierre entrada: " . $sql6d_es);
    } else {
        echo "Error: " . $sql6d_es . "<br>" . $conn->error;

        logs("Error: " . $sql6d_es . "<br>" . $conn->error);
    }

//$sqlcorre="update ".$database.".estaciones set e_ftp_rop='".$pack."' where e_id='".$_SESSION['est_id']."'" ;

    if ($_SESSION['est_id'] == "0") {
        logs("Entro por sucursales " . $_SESSION['est_id']);

        $sqlcorre = "update " . $database . ".sucursales set suc_ftp_rcier='" . $pack . "' where suc_id='" . $_SESSION['suc_id'] . "'";
    } else {
        logs("Entro por estaciones " . $_SESSION['est_id']);

        $sqlcorre = "update " . $database . ".estaciones set e_ftp_rcier='" . $pack . "' where e_id='" . $_SESSION['est_id'] . "' and suc_id='" . $_SESSION['suc_id'] . "'";

    }

    if ($connection->query($sqlcorre) === true) {
        //    echo "update prod_op";

        logs("actualiza corre: " . $sqlcorre);

    } else {
        echo "Error: " . $sqlcorre . "<br>" . $conn->error;
    }

}

/*
$deletetmp='DROP TABLE '.$_SESSION['dbname'] ."_tmp.".$_SESSION['unico'];

if ($connection->query($deletetmp) === TRUE) {

logs("borra_tmp: ". $deletetmp);

} else {
echo "Error: " . $deletetmp . "<br>" . $connection->error;
}

$deletetmp='DROP TABLE '.$_SESSION['dbname'] ."_tmp.".$_SESSION['unico'].'2';

if ($connection->query($deletetmp) === TRUE) {

logs("borra_tmp: ". $deletetmp);

} else {
echo "Error: " . $deletetmp . "<br>" . $connection->error;
}

$deletetmp='DROP TABLE '.$_SESSION['dbname'] ."_tmp.".$_SESSION['unico'].'3';

if ($connection->query($deletetmp) === TRUE) {

logs("borra_tmp: ". $deletetmp);

} else {
echo "Error: " . $deletetmp . "<br>" . $connection->error;
}

$deletetmp='DROP TABLE '.$_SESSION['dbname'] ."_tmp.".$_SESSION['unico'].'5';

if ($connection->query($deletetmp) === TRUE) {

logs("borra_tmp: ". $deletetmp);

} else {
echo "Error: " . $deletetmp . "<br>" . $connection->error;
}
 */

//retorno = "{\"estado\":\"OK\",\"mensaje\":\""
//                            + "Oper. enviadas en total: " + registros
//                            + " Procesados: " + acum + "\"}";

$msg = array("estado" => "OK", "mensaje" => "Oper. enviadas en total: " . $recibidos . " Procesados: " . $i);

$json = $msg;

header('content-type: application/json');
echo json_encode($json);

//print_r ($books2);
//access property of object in array
//   foreach ($books2 as $book) {
//   echo 'This book is a ' . $book->op_id . ' Autor:  ' . $book->op_fecha ."<br>";
//}

@mysqli_close($connection);
?>