 <?php

if (isset($_SERVER['HTTP_ORIGIN'])) {
    header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
    header('Access-Control-Allow-Credentials: true');
    header('Access-Control-Max-Age: 86400'); // cache for 1 day
}

error_reporting(E_ERROR | E_PARSE);

$_SESSION['database'] = $_GET['database'];
$_SESSION['ci_op_idx'] = $_GET['ci_op_idx'];

include 'conn.php';
include 'funciones.php';

$jsonData = array();
$jsonTempData = array();

$ci_vta_bruto = 0.00;
$ci_dev_bruto = 0.00;
$ci_vta_desc = 0.00;
$ci_dev_desc = 0.00;
$ci_vta_imp = 0.00;
$ci_dev_imp = 0.00;

$sql = "select
        a.op_tipo,
		sum(a.sub_total) as ci_bruto,
		sum(a.impuesto) as ci_imp,
		sum(a.desgeneral) as desgeneral,
		sum(a.po_desc1) as ci_desc
		from (
		select distinct(o.po_op_id) as orden_id,
		co.op_subtotal as sub_total,
		co.op_tipo,
		co.op_itbms as impuesto,
		co.op_desc as desgeneral,
        o.po_desc1
		from " . $_SESSION['database'] . ".producto_op o
		join " . $_SESSION['database'] . ".operacion co on o.po_op_id = co.op_id
		where ci_op_idx = " . $_SESSION['ci_op_idx'] . ") a
		group by a.op_tipo order by a.op_tipo";
//print_r($sql);

$qur = $connection->query($sql);

$sql = " select  sum(base) as base, porc_imp, imp from (select id_contribuyente, cliente_id,
	   sum(round(base,2)) as base,
	   case when id_contribuyente = 4 then 0 else imp end  as porc_imp,
	   case when id_contribuyente = 4 then 0 else round(((a.base  *  a.imp) / 100)+0.000001,2) end as imp,
	   case when id_contribuyente = 4 then a.base else round(((a.base  *  a.imp) / 100)+0.000001 + a.base,2) end as total
	   from	(select id_contribuyente, op_id_cliente as cliente_id, sum(base) as base, imp, op_tipo from
		(select cli.id_contribuyente,  o.po_itbms as imp , co.op_tipo, co.op_id_cliente,
		CASE co.op_tipo
		WHEN '2'       THEN  sum(  round((ROUND(o.po_precio * o.po_cant,2) - round(((( ROUND(o.po_precio * o.po_cant,2)) * pm.porcdesc) /100)+0.000001,2)  -   (( ROUND(o.po_precio * o.po_cant,2)) - round(((( ROUND(o.po_precio * o.po_cant,2)) * pm.porcdesc) /100)+0.000001,2)     ) * pm.porcdesc_general /100)+0.000001,2) )
		WHEN '16'      THEN -sum(  round((ROUND(o.po_precio * o.po_cant,2) - round(((( ROUND(o.po_precio * o.po_cant,2)) * pm.porcdesc) /100)+0.000001,2)  -   (( ROUND(o.po_precio * o.po_cant,2)) - round(((( ROUND(o.po_precio * o.po_cant,2)) * pm.porcdesc) /100)+0.000001,2)     ) * pm.porcdesc_general /100)+0.000001,2) )
		END base
		from " . $_SESSION['database'] . ".producto_op o
		join " . $_SESSION['database'] . ".operacion co on o.po_op_id = co.op_id
		join " . $_SESSION['database'] . ".clientes cli on co.op_id_cliente = cli.c_id
        join " . $_SESSION['database'] . ".producto_op_mas pm on o.po_id_prod = pm.po_id_prod and o.po_op_id = pm.po_op_id and o.po_relac_unic = pm.po_relac_unic
		where co.ci_op_idx = " . $_SESSION['ci_op_idx'] . "
		group by  o.po_itbms, cli.id_contribuyente, co.op_tipo) c
		group by  c.imp, c.id_contribuyente) a group by porc_imp, imp, id_contribuyente) z group by porc_imp, imp
		order by porc_imp asc";

//print_r($sql);

$qur2 = $connection->query($sql);

if ($_SESSION['codigo'] == "99") {

    $json->datos = "false";

} else {
    $json->datos = "true";

    while ($r = mysqli_fetch_assoc($qur)) {

        $jsonTempData = array();
        $jsonTempData['op_tipo'] = $r['op_tipo'];
        $jsonTempData['ci_bruto'] = number_format($r['ci_bruto'], 2, '.', '');
        $jsonTempData['ci_desc'] = number_format($r['ci_desc'], 2, '.', '');
        $jsonTempData['ci_imp'] = number_format($r['ci_imp'], 2, '.', '');
        $jsonTempData['desgeneral'] = number_format($r['desgeneral'], 2, '.', '');

        $jsonData[] = $jsonTempData;
        $json->jsonCierre1 = $jsonData;

    }

    $jsonData = array();
    $jsonTempData = array();

    while ($r = mysqli_fetch_assoc($qur2)) {

        $jsonTempData = array();
        $jsonTempData['base'] = $r['base'];
        $jsonTempData['porc_imp'] = $r['porc_imp'];
        $jsonTempData['imp'] = $r['imp'];

        $jsonData[] = $jsonTempData;
    }

    $json->jsonCierre3 = $jsonData;

}

print_r(json_encode($json));

@mysqli_close($connection);
?>