 <?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.descuento) 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 descuento " .
    "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";

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

$sql = "select base, imp as porc_imp, ((a.base  *  a.imp) / 100) as imp, ((a.base  *  a.imp) / 100) + a.base as total, " .
    " CASE imp   " .
    " WHEN '0' THEN   " .
    " '0'   " .
    " WHEN '7.0' THEN   " .
    " '1'  " .
    " WHEN '10.0' THEN  " .
    " '2'  " .
    " WHEN '15.0' THEN  " .
    " '3'  " .
    " ELSE  " .
    " '4'  " .
    " END tipo_base  " .
    " from	(select sum(base) as base, imp, op_tipo from  " .
    "                     (select   p.pr_itbms as imp , co.op_tipo,  " .
    " CASE op_tipo  " .
    " WHEN '2' THEN  sum(o.po_precio - ((o.po_precio * pm.porcdesc) /100) -  (o.po_precio -  ((o.po_precio * pm.porcdesc) /100)) * pm.porcdesc_general /100)  " .
    " WHEN '16.0' THEN  (-sum(o.po_precio - ((o.po_precio * pm.porcdesc) /100)  -  (o.po_precio - ((o.po_precio * pm.porcdesc) /100)) * pm.porcdesc_general /100  ))  " .
    " END base  " .
    " from " . $_SESSION['database'] . ".producto_op o  " .
    " join " . $_SESSION['database'] . ".productos p on o.po_id_prod = p.pr_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 " .
    " join " . $_SESSION['database'] . ".operacion co on o.po_op_id = co.op_id " .
    " where co.ci_op_idx = " . $_SESSION['ci_op_idx'] .
    " group by p.pr_itbms, co.op_tipo) c " .
    " group by c.imp) a";

//print_r($sql);

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

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

    $json->datos = "false";

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

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

        $op_tipo = $r['op_tipo'];

        if ($op_tipo == 2) {
            $ci_vta_bruto = $r['ci_bruto'];
            $ci_vta_desc = $r['ci_desc'];
            $ci_vta_imp = $r['ci_imp'];

        } else {
            $ci_dev_bruto = $r['ci_bruto'];
            $ci_dev_desc = $r['ci_desc'];
            $ci_dev_imp = $r['ci_imp'];
        }

    }

    $jsonTempData = array();
    $jsonTempData['ci_vta_bruto'] = $ci_vta_bruto;
    $jsonTempData['ci_dev_bruto'] = $ci_dev_bruto;
    $jsonTempData['ci_vta_desc'] = $ci_vta_desc;
    $jsonTempData['ci_dev_desc'] = $ci_dev_desc;
    $jsonTempData['ci_vta_imp'] = $ci_vta_imp;
    $jsonTempData['ci_dev_imp'] = $ci_dev_imp;

    $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'];
        $jsonTempData['total'] = $r['total'];
        $jsonTempData['tipo_base'] = $r['tipo_base'];

        $jsonData[] = $jsonTempData;
    }

    $json->jsonCierre3 = $jsonData;

}

print_r(json_encode($json));

@mysqli_close($connection);
?>