<?php
error_reporting(E_ALL & ~E_NOTICE & ~E_WARNING);
date_default_timezone_set("America/Panama");

include "../conn_obj/conn_param.php";

if ($conn = @mysqli_connect($Srv_Info['hostname'], $Srv_Info['username'], $Srv_Info['password'])) {
    mysqli_select_db($conn, $Srv_Info['dbname']);
    mysqli_query($conn, "SET NAMES 'utf8'");
} else
    die("err db");
header('content-type: application/json');

session_start();
$_SESSION['pDb_Name'] = $_POST['pDb_Name'];
$_SESSION['pCod_Cte'] = $_POST['pCod_Cte'];
$_SESSION['pDebuger'] = $_POST['pDebuger'];

$cDb_Name = $_SESSION['pDb_Name'];
$cCod_Cte = $_SESSION['pCod_Cte'];
$iDebuger = $_SESSION['pDebuger'];

$cDb_NameC = "`$cDb_Name`";

$now = new DateTime();
$cDateTime = $now->format('Y-m-d H:i:s');
$cDateOnly = $now->format('Y-m-d');


$cCmd_Debug = "";
if ($iDebuger == 1) {
    $cCmd_Debug .= "/*" . chr(13);
    $cCmd_Debug .= "Parametros de entrada: " . chr(13);
    $cCmd_Debug .= "pDb_Name = $cDb_Name" . chr(13);
    $cCmd_Debug .= "pCod_Cte = $cCod_Cte" . chr(13);
    $cCmd_Debug .= "pDebuger = $iDebuger" . chr(13);
    $cCmd_Debug .= "*/" . chr(13);
}

$rows = array();

$cSql_Str = "
    SELECT 
    	c_id 				    AS Cod_Cte, 
    	SUM(IF(id_tipo= 2,1,0)) AS Num_Fac, 
    	SUM(IF(id_tipo=22,1,0)) AS Num_Ndb, 
    	COUNT(c_id) 			AS Cnt_Regs, 
    	(SUM(op_debito+op_credito) - SUM(op_abono) ) AS Mn_Pend 
    FROM $cDb_NameC.operacion_cxc 
    WHERE (c_id ='$cCod_Cte')
    AND   (op_aptdo = 0) 
    AND   ( (id_tipo = 2) OR (id_tipo = 22) )
    AND   ( (op_debito + op_credito) > op_abono);";
if ($iDebuger == 1)
    $cCmd_Debug .= $cSql_Str;
$oData = mysqli_query($conn, $cSql_Str);

while ($r = mysqli_fetch_assoc($oData)) {
    $cCmd_Debug .= $r[0];
    $rows[] = $r;
}

if ($oData != null)
    mysqli_free_result($oData);

if ($iDebuger == 1)
    echo ($cCmd_Debug);

print_r(json_encode($rows));

@mysqli_close($conn);

/*
JOHNN GUTIERREZ
jg_gutierrez@yahoo.es
2024-01-27
*/
