<?php
include '../conn_obj/utiles_pdo.php';
include '../conn_obj/conn_param.php';
date_default_timezone_set("America/Panama");
error_reporting(E_ALL & ~E_NOTICE & ~E_WARNING);
#error_reporting(E_ALL);
session_start();
if ($_POST) {

    $SaveLog = 0;
    $_SESSION['db_che'] = $_POST['db_che'];
    $_SESSION['suc_id'] = $_POST['suc_id'];
    $_SESSION['op__id'] = $_POST['op__id'];
    $_SESSION['op_tip'] = $_POST['op_tip'];
    $_SESSION['d_json'] = $_POST['d_json'];
    $_SESSION['sw_deb'] = isset($_POST['sw_deb']) ? $_POST['sw_deb'] : 0;

    $pDb_Nam = $_SESSION['db_che'];
    $pSuc_Id = $_SESSION['suc_id'];
    $pOp__id = $_SESSION['op__id'];
    $pOp_Tip = $_SESSION['op_tip'];
    $pd_json = $_SESSION['d_json'];
    $SaveLog = $_SESSION['sw_deb'];
    
    if ($SaveLog == 1) {
        echo "++++++++++++++++++++++++++++++++++++apt_pedido_upd.php+++++++++++++++++++++++++++++++++++++++++++++++++" . chr(13);
        echo "db_che   : $pDb_Nam" . chr(13);
        echo "suc_id   : $pSuc_Id" . chr(13);
        echo "Op__id   : $pOp__id" . chr(13);
        echo "op_tip   : $pOp__id" . chr(13);
        echo "d_json   : $pOp__id" . chr(13);
        echo "". chr(13);
    }

    $hoynow = getdate();
    $timeStamp = $hoynow["year"] . "-" . $hoynow["mon"] . "-" . $hoynow["mday"];
    $timeStamp = date("Y-m-d H:i:s");

    $oCnn = new Utiles($pDb_Nam, $Srv_Info['hostname'], $Srv_Info['username'], $Srv_Info['password'], $Srv_Info['charset']);
    $oTools = new Reposit_Functions();

    $Data = json_decode($pd_json, TRUE);
    $TablaPo = $reg1['operacion_upd_pend'];
    $reg_po = $reg1['regs_po'];
 
    $cSql = "DELETE FROM `$pDb_Nam`.`operacion_upd_pend` WHERE (apl_op_id='$pOp__id') AND (apl_op_tp='$pOp_Tip') ";
    $bQry_00 = $oCnn->query($cSql);

    $oExec = $oTools->jsonmysql($oCnn, $pDb_Nam, $pd_json, 'operacion_upd_pend', 0);
    
    $cSql = "
    UPDATE `$pDb_Nam`.`producto_op` po
    INNER JOIN `$pDb_Nam`.`operacion_upd_pend` ou ON (po.po_op_id= ou.apl_op_id) AND (po.po_relac_unic = ou.apl_relau) 
    SET po.po_cant_pend = (po.po_cant_pend - ou.po_cant_ap)
    WHERE (ou.apl_op_id='$pOp__id');";
    $bQry_01 = $oCnn->query($cSql);

    $cSql = "
    UPDATE `$pDb_Nam`.`producto_op` po
    INNER JOIN `$pDb_Nam`.`operacion_upd_pend` ou ON (po.po_op_id= ou.apl_op_id) AND (po.po_relac_unic = ou.apl_relau) 
    SET po.po_cant_pend = IF(po.po_cant_pend<0,0,po.po_cant_pend)
    WHERE (ou.apl_op_id='$pOp__id');";
    $bQry_01 = $oCnn->query($cSql);

    /*
    $cSql = "
    UPDATE `$pDb_Nam`.`producto_op` po
    INNER JOIN `$pDb_Nam`.`operacion_upd_pend` ou ON (po.po_op_id= ou.apl_op_id) AND (po.po_relac_unic= ou.apl_relau)
    SET po.po_cant = po.po_cant_pend
    WHERE (ou.apl_op_id='$pOp__id');";
    $bQry_02 = $oCnn->query($cSql);

    $cSql = "
    UPDATE `$pDb_Nam`.`producto_op` po
    INNER JOIN `$pDb_Nam`.`operacion_upd_pend` ou ON (po.po_op_id= ou.apl_op_id) AND (po.po_relac_unic= ou.apl_relau)
    SET po.po_cant = 0.00, po.po_cant_pend = 0.00
    WHERE (ou.apl_op_id='$pOp__id') 
    AND   (po.po_cant_pend<=0);";
    $bQry_03 = $oCnn->query($cSql);
    */
    $cSql = "
    UPDATE operacion op 
    INNER JOIN (SELECT po1.po_op_id, SUM(po1.po_cant_pend) AS s_po_cant_pend FROM `producto_op` po1 WHERE po1.po_op_id = '$pOp__id') po ON  (po.po_op_id = op.op_id)
        SET op.id_status=1
    WHERE (op.op_id='$pOp__id')
    AND   (po.s_po_cant_pend=0);";
    $oCnn->query($cSql);

    // $iResult = $oCnn->call_doc_pend_upd($pDb_Nam, $pOp__id);
     if ($bQry_01=true) {
        $msg = array(
            "success" => 'true',
            "mensaje" => 'ACTUALZIACION EXITOSA.'
        );
    } else {
        $msg = array(
            "success" => 'false',
            "mensaje" => 'ERROR EN LA EJECUCION DEL COMANDO. ' . $oCnn->error
        );
    }
   
} else {

    $msg = array(
        "success" => 'false',
        "mensaje" => 'SOLO SE ADMITEN LLAMADOS POR METODO POST.'
    );

    header('Temporary-Header: True', true, 403);
    header_remove('Temporary-Header');
}
$aResp[] = $msg;
header('content-type: application/json');
echo json_encode($aResp);

$oCnn->close_conn();
session_unset();
session_destroy();
/*
JOHNN GUTIERREZ, 
FECHA DE CREACION   2021-03-13
ULTIMA MODIFICASION 2021-01-16
ULTIMA MODIFICASION 2024-09-23
*/
?>
