<?php
ini_set('display_errors', 1);
ini_set('memory_limit', '4096M');
/*
JOHNN GUTIERREZ,
FECHA DE CREACION   2021-01-27
ULTIMA MODIFICASION 2021-01-28
ULTIMA MODIFICASION 2023-01-07
ULTIMA MODIFICASION 2023-01-11
ULTIMA MODIFICASION 2025-04-25
 */
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();
$_SESSION['db_name'] = $_POST['db_name'];
$_SESSION['cod_dep'] = $_POST['cod_dep'];
$_SESSION['cod_pro'] = $_POST['cod_pro'];

$cDb_Name = $_SESSION['db_name'];
$cCod_Dep = $_SESSION['cod_dep'];
$cCod_Pro = $_SESSION['cod_pro'];
$cCod_Uni = trim($cCod_Dep) . '-' . trim($cCod_Pro);

$conn = new Utiles($cDb_Name, $Srv_Info['hostname'], $Srv_Info['username'], $Srv_Info['password']);

$cSql_Cmd = "SELECT ifnull(pr_man_exist,0) AS pr_man_exist FROM $cDb_Name.productos_det WHERE (pr_id='$cCod_Pro') AND  (suc_id=1)";
$conn->query($cSql_Cmd);
$oFndDat = $conn->fetchArray();

if (!(($cCod_Dep == null) || ($cCod_Dep == '') || ($cCod_Dep == '%') || ($cCod_Dep == '*'))) {

  $cSql_Cmd = "SELECT `pr_id` FROM $cDb_Name.productos WHERE (pr_id='$cCod_Pro')";
  $conn->query($cSql_Cmd);
  $oFndPro = $conn->fetchArray();
  if (!(($oFndPro['pr_id'] == null) || ($oFndPro['pr_id'] == ''))) {

    $cSql_Cmd = "INSERT IGNORE INTO `$cDb_Name`.`prod_existencia` (
    `pr_id`           , `p_existencia_dep_id` ,	`p_inv_inicial`   ,	`p_existencia`  ,
    `p_compras`       ,	`p_ncompras`          ,	`p_dcompras`      ,	`p_ventas`      ,
    `p_dventas`       ,	`p_pventas`           ,	`p_ventas_cort`   ,	`p_ventas_cost` ,
    `p_nventas`       ,	`p_ventas_afil`       ,	`p_dventas_afil`  ,	`p_cargos`      ,
    `p_descargos`     ,	`p_ajustes`           ,	`p_trasl_ent`     ,	`p_trasl_sal`   ,
    `p_apartado`      ,	`p_cargo_prod`        ,	`p_descargo_prod` ,	`p_cargo_auto`  ,
    `p_descargo_auto` ,	`p_rventas`           ,	`trig_unic`       ,	`syncprocesing` ,
    `usermodify`      ,	`useroption`) VALUES (
    '$cCod_Pro','$cCod_Dep',0,0,
    0,0,0,0,
    0,0,0,0,
    0,0,0,0,
    0,0,0,0,
    0,0,0,0,
    0,0,'$cCod_Uni',0,
    0,0)";

    $conn->query($cSql_Cmd);
  }
}

if ($oFndDat['pr_man_exist'] == 0) {
  $cSql_Cmd = "SELECT
      ex.pr_id,
      CONCAT(UPPER(TRIM(dp.dep_descripcion)),' -*[PRODUCTO NO MANEJA EXISTENCIA]*') AS dep_descripcion,
      000000 AS Exist_Total,
      000000 AS Exist_Ped,
      000000 AS Exist_Apart,
      000000 AS Exist_Disp,
      dp.dep_id
    FROM  `$cDb_Name`.`prod_existencia` ex
    LEFT OUTER JOIN `$cDb_Name`.`depositos` dp ON ex.p_existencia_dep_id=dp.dep_id
    WHERE  (ex.pr_id = '$cCod_Pro') AND (dp.para_la_venta=1) 
    AND   IF( (('$cCod_Dep'='%') OR ('$cCod_Dep'='*')),1=1,dp.dep_id='$cCod_Dep')
    ORDER BY ex.p_existencia_dep_id,ex.pr_id ";

  $conn->query($cSql_Cmd);
  $oData = $conn->fetchAll();
} else {
  $cSql_Cmd = "SELECT
              ex.pr_id,
              UPPER(TRIM(`fn_remove_accents_v2`(dp.dep_descripcion))) AS dep_descripcion,
              IFNULL(ex.p_existencia,0.00) AS Exist_Total,
              IFNULL(ex.p_pventas   ,0.00) AS Exist_Ped,
              IFNULL(ex.p_apartado  ,0.00) AS Exist_Apart,
              ( IFNULL(ex.p_existencia,0.00) - ( IFNULL(ex.p_pventas,0.00) + IFNULL(ex.p_apartado,0.00) ) ) AS Exist_Disp,
              dp.dep_id
            FROM  `$cDb_Name`.`prod_existencia` ex
            LEFT OUTER JOIN `$cDb_Name`.`depositos` dp ON (ex.p_existencia_dep_id = dp.dep_id)
            WHERE (ex.pr_id = '$cCod_Pro') AND (dp.para_la_venta=1)
            AND   IF( ( ('$cCod_Dep'='%') OR ('$cCod_Dep'='*') OR ('$cCod_Dep'='') ),1=1, (dp.dep_id='$cCod_Dep') )
            ORDER BY dp.dep_descripcion, ex.pr_id ";

  $conn->query($cSql_Cmd);
  $oData = $conn->fetchAll();
}
$oFndDat = null;
$oFndPro = null;
header('content-type: application/json');
echo json_encode($oData);

@mysqli_close($conn);
session_unset();
session_destroy();
?>