<?php
declare(strict_types=0);
// session_start();
// echo '<meta http-equiv="refresh" content="5">';
foreach($_GET  as $key => $value){${$key}=trim($value);$_PARAMS[$key]=${$key};}  
foreach($_POST as $key => $value){${$key}=trim($value);$_PARAMS[$key]=${$key};}
include_once('lib_main.php');


$empr0=data_maes('empr',$cfg_empr);

echo '<br>============'.$empr0['empr_codigo'];
echo '<br>';

$show=1;

if($empr0['empr_codigo']=='pcs1' or $empr0['empr_codigo']=='imcp')
{
     
     $nameview='dim_fecha';
     echo '<br>==>'.$nameview;
     $sql_view='drop view '.$nameview;
     $res_view=msql_query($sql_view,$link_data);
     
     $sql_view='';
     $sql_view.='create view '.$nameview.' as ';
     $sql_view.='select ';
     $sql_view.='distinct fecha, ';
     
     $sql_view.='date_format(fecha,"%Y") as anio,';
     $sql_view.='quarter(fecha) as cuarto, ';
     $sql_view.='ceil(date_format(fecha,"%v")/4) as periodo, ';
     $sql_view.='date_format(fecha,"%m") as mes,';
     $sql_view.='date_format(fecha,"%v") as sema, ';
     $sql_view.='date_format(fecha,"%d") as dia,';
     
     $sql_view.='upper(date_format(fecha,"%w")) as dia_num,';
     $sql_view.='upper(date_format(fecha,"%a")) as dia_nom,';
     $sql_view.='upper(date_format(fecha,"%W")) as dia_nombre,';
     $sql_view.='date_format(fecha,"%j") as dia_anio, ';
     
     $sql_view.='date_format(fecha,"%Y-%m") as anio_mes,';
     $sql_view.='date_format(fecha,"%x-%v") as anio_sem, ';
     
     $sql_view.='upper(date_format(fecha,"%b")) as mes_nom,';
     $sql_view.='upper(date_format(fecha,"%M")) as mes_nombre,';
//      $sql_view.='date_format(idate,"%T") as ihora, ';
//      $sql_view.='date_format(fecha,"%d-%b-%Y") as dmy1,';
//      $sql_view.='date_format(fecha,"%d/%b/%Y") as dmy1,';
     
     
     $sql_view.='status as st ';
     $sql_view.='from cont1 ';
     $sql_view.='where ifnull(status,"")not in("O","X") ';
     $sql_view.='and fecha is not null ';
//      $sql_view.='order by fecha ';
     $res_view=msql_query($sql_view,$link_data);

     if($show==1){show_view($nameview);}
     
////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
     
     $nameview='dim_enti';
     echo '<br>==>'.$nameview;
     
     $sql_view='drop view '.$nameview;
     $res_view=msql_query($sql_view,$link_data);
     
     $sql_view='';
     $sql_view.='create view '.$nameview.' as ';
     $sql_view.='select ';
     $sql_view.='left(enti_codigo,10) as enti, ';
     $sql_view.='max(enti_descri) as nombre, ';
     $sql_view.='max(enti_razsoc) as raz_social, ';
     $sql_view.='pais_descri as pais, ';
     $sql_view.='prvc_descri as provincia, ';
     $sql_view.='enti_status as st ';
     $sql_view.='from enti0 ';
     $sql_view.='left join pais0 on enti_pais=pais_codigo ';
     $sql_view.='left join prvc0 on left(enti_codigo,2)=prvc_codigo ';
     $sql_view.='where length(trim(ifnull(enti_codigo,"")))>=10 ';
//      $sql_view.='and left() ';
     $sql_view.='group by left(enti_codigo,10) ';
     $res_view=msql_query($sql_view,$link_data);
     
     if($show==1){show_view($nameview);}
     
////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
     
     
     $nameview='dim_item';
     echo '<br>==>'.$nameview;
     
     $sql_view='drop view '.$nameview;
     $res_view=msql_query($sql_view,$link_data);
     
     $sql_view='';
     $sql_view.='create view '.$nameview.' as ';
     $sql_view.='select ';
     $sql_view.='item_codigo as item, ';
     $sql_view.='item_descri as producto, ';
     $sql_view.='lsub0.lsub_descri as linea, ';
     $sql_view.='lsub00.lsub_descri as sub_linea, ';
     $sql_view.='item_status as st ';
     $sql_view.='from item0 ';
     $sql_view.='left join lsub0 on left(item_lsub,5)=trim(lsub0.lsub_codigo) ';
     $sql_view.='left join lsub0 lsub00 on item_lsub=lsub00.lsub_codigo ';
     $sql_view.='where length(trim(ifnull(item_codigo,"")))>=6 ';
     $res_view=msql_query($sql_view,$link_data);
     
     if($show==1){show_view($nameview);}
     
////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
     
     
     $nameview='dim_ctas';
     echo '<br>==>'.$nameview;
     
     $sql_view='drop view '.$nameview;
     $res_view=msql_query($sql_view,$link_data);
     
     $sql_view='';
     $sql_view.='create view '.$nameview.' as ';
     $sql_view.='select ';
     $sql_view.='ctas_codigo as cuenta, ';
     $sql_view.='ctas_descri as contable, ';
     $sql_view.='ctas_status as st ';
     $sql_view.='from ctas0 ';
//      $sql_view.='left join pais0 on enti_pais=pais_codigo ';
//      $sql_view.='left join prvc0 on enti_provincia=prvc_codigo ';
     $sql_view.='where ctas_codigo in(select distinct ctas from cont2) ';
     $res_view=msql_query($sql_view,$link_data);
     
     if($show==1){show_view($nameview);}

////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
     
     
     $nameview='dim_cont';
     echo '<br>==>'.$nameview;
     
     $sql_view='drop view '.$nameview;
     $res_view=msql_query($sql_view,$link_data);
     
     $sql_view='';
     $sql_view.='create view '.$nameview.' as ';
     $sql_view.='select ';
//      $sql_view.='convert(idnum1,char) as idn, ';
//      $sql_view.='if(ifnull(mdfrom,"")="","CONT",mdfrom) as modulo, ';
//      $sql_view.='docu,';
// //      $sql_view.='docu_descri as documento, ';
//      $sql_view.='numero,';
//      $sql_view.='fecha, ';
//      $sql_view.='enti, ';
//      $sql_view.='item, ';
     $sql_view.='ctas, ';
//      $sql_view.='if(cant>0,cant,0) as debito, ';
//      $sql_view.='if(cant<0,abs(cant),0) as credito, ';
//      $sql_view.='sum(case when cant>0 and (left(ctas,1)in(1,2,3) or (left(ctas,1)not in (1,2,3) and docu not like "DTR%")) then cant else 0 end) as debito, ';
//      $sql_view.='sum(case when cant<0 and (left(ctas,1)in(1,2,3) or (left(ctas,1)not in (1,2,3) and docu not like "DTR%")) then abs(cant) else 0 end) as credito, ';
     
     
     for($idx=2016; $idx<=date('Y')*1 ; $idx++)
     {
          $sql_view.='sum(case when cant>0 and ((left(ctas,1)in(1,2,3) and left(fecha,4)<="'.$idx.'") or (left(ctas,1)not in (1,2,3) and left(fecha,4)="'.$idx.'" and docu not like "DTR%")) then cant else 0 end) as deb_'.$idx.', ';
          $sql_view.='sum(case when cant>0 and ((left(ctas,1)in(1,2,3) and left(fecha,4)<="'.$idx.'") or (left(ctas,1)not in (1,2,3) and left(fecha,4)="'.$idx.'" and docu not like "DTR%")) then abs(cant) else 0 end) as cre_'.$idx.', ';
     }
//      $sql_view.='concepto, ';
//      $sql_view.='detalle, ';
     $sql_view.='ifnull(status,"") as status ';
     $sql_view.='from cont1 ';
     $sql_view.='left join cont2 on idnum1=idnum2 ';
     $sql_view.='left join csub0 on csub=csub_codigo ';
     $sql_view.='left join docu0 on docu=docu_codigo ';
     $sql_view.='where ifnull(status,"")not in("O","X") ';
     $sql_view.='and cant!=0 ';
     $sql_view.='group by ctas ';
//      $sql_view.='and docu like "DTR%" ';
//      $sql_view.='and left(ctas,1)=4';
     $res_view=msql_query($sql_view,$link_data);
  
     if($show==1){show_view($nameview);}
     
     
////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
     
     
     $nameview='fact_cont';
     echo '<br>==>'.$nameview;
     
     $sql_view='drop view '.$nameview;
     $res_view=msql_query($sql_view,$link_data);
     
     $sql_view='';
     $sql_view.='create view '.$nameview.' as ';
     $sql_view.='select ';
     $sql_view.='convert(idnum1,char) as idn, ';
     $sql_view.='if(ifnull(mdfrom,"")="","CONT",mdfrom) as modulo, ';
     $sql_view.='docu,';
//      $sql_view.='docu_descri as documento, ';
     $sql_view.='numero,';
     $sql_view.='fecha, ';
     $sql_view.='enti, ';
     $sql_view.='item, ';
     $sql_view.='ctas, ';
//      $sql_view.='if(cant>0,cant,0) as debito, ';
//      $sql_view.='if(cant<0,abs(cant),0) as credito, ';
     $sql_view.='case when cant>0 and (left(ctas,1)in(1,2,3) or (left(ctas,1)not in (1,2,3) and docu not like "DTR%")) then cant else 0 end as debito, ';
     $sql_view.='case when cant<0 and (left(ctas,1)in(1,2,3) or (left(ctas,1)not in (1,2,3) and docu not like "DTR%")) then abs(cant) else 0 end as credito, ';
     $sql_view.='concepto, ';
     $sql_view.='detalle, ';
     $sql_view.='ifnull(status,"") as status ';
     $sql_view.='from cont1 ';
     $sql_view.='left join cont2 on idnum1=idnum2 ';
     $sql_view.='left join csub0 on csub=csub_codigo ';
     $sql_view.='left join docu0 on docu=docu_codigo ';
     $sql_view.='where ifnull(status,"")not in("O","X") ';
     $sql_view.='and cant!=0 ';
//      $sql_view.='and docu like "DTR%" ';
     $res_view=msql_query($sql_view,$link_data);
  
     if($show==1){show_view($nameview);}
     
////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
     
     $nameview='fact_vtas';
     echo '<br>==>'.$nameview;
     
     $sql_view='drop view '.$nameview;
     $res_view=msql_query($sql_view,$link_data);
     
     $sql_view='';
     $sql_view.='create view '.$nameview.' as ';
     $sql_view.='select ';
     $sql_view.='convert(idnum1,char) as idn, ';
     $sql_view.='convert(iddet,char) as lin, ';
//      $sql_view.='if(ifnull(mdfrom,"")="","CONT",mdfrom) as modulo, ';
     $sql_view.='docu,';
//      $sql_view.='docu_descri as documento, ';
     $sql_view.='numero,';
     $sql_view.='fecha, ';
     $sql_view.='left(enti,10) as enti, ';
     $sql_view.='item, ';
     
//      $ldocu1="XXXX";
     $sql_view.='(case ';
     $sql_view.='when ifnull(status,"")in("O","X") then 0 ';
//      $sql_view.='when left(docu,2)in('.$ldocu1.') ';
//      $sql_view.='then abs(cant) ';
     $sql_view.='else abs(cant)*1 end) ';
     $sql_view.='as unidades, ';
     
     $sql_view.='puni as prec_unit, ';
     $sql_view.='cuni as cost_unit, ';
     
     $sql_view.='(case ';
     $sql_view.='when status in("O","X") then 0 ';
//      $sql_view.='when left(docu,2)in('.$ldocu1.') ';
//      $sql_view.='then abs(cant*puni) ';
     $sql_view.='else abs(cant*puni)*1 end)';
     $sql_view.='as subtotal, ';
     
     $sql_view.='(case ';
     $sql_view.='when ifnull(status,"")in("O","X") then 0 ';
//      $sql_view.='when left(docu,2)in('.$ldocu1.') ';
//      $sql_view.='then abs(cant*puni*ifnull(dcto,0)/100) ';
     $sql_view.='else abs(cant*puni*ifnull(dcto,0)/100)*1 end)';
     $sql_view.='as descuento, ';

     $sql_view.='(case ';
     $sql_view.='when ifnull(status,"")in("O","X") then 0 ';
//      $sql_view.='when left(docu,2)in('.$ldocu1.') ';
//      $sql_view.='then (abs(cant*puni)*(1-ifnull(dcto,0)/100) +(abs(cant*puni)*(ifnull(ice,0)/100))) *(ifnull(iva,0)/100) ';
     $sql_view.='else ((abs(cant*puni)*(1-ifnull(dcto,0)/100) +(abs(cant*puni)*(ifnull(ice,0)/100))) *(ifnull(iva,0)/100))*1 end)';
     $sql_view.='as iva, ';

     $sql_view.='(case ';
     $sql_view.='when ifnull(status,"")in("O","X") then 0 ';
//      $sql_view.='when left(docu,2)in('.$ldocu1.') ';
//      $sql_view.='then  (abs(cant*puni)*(1-ifnull(dcto,0)/100) +(abs(cant*puni)*(ifnull(ice,0)/100))) *(1+ifnull(iva,0)/100) +(cant*irbp) +(abs(cant*puni*(1-ifnull(dcto,0)/100))*prop/100) ';
     $sql_view.='else ((abs(cant*puni)*(1-ifnull(dcto,0)/100) +(abs(cant*puni)*(ifnull(ice,0)/100))) *(1+ifnull(iva,0)/100) +(cant*irbp) +(abs(cant*puni*(1-ifnull(dcto,0)/100))*prop/100)) end)';
     $sql_view.='as total, ';
     
     $sql_view.='ifnull(cntn,"") as cntn, ';
     $sql_view.='concepto, ';
     $sql_view.='detalle, ';
     $sql_view.='ifnull(status,"") as status ';
     $sql_view.='from vtas1 ';
     $sql_view.='left join vtas2 on idnum1=idnum2 ';
//      $sql_view.='left join csub0 on csub=csub_codigo ';
//      $sql_view.='left join docu0 on docu=docu_codigo ';
     $sql_view.='where ifnull(status,"")not in("O","X") ';
     $sql_view.='and cant!=0 ';
//      $sql_view.='group by idnum1,iddet ';
     $res_view=msql_query($sql_view,$link_data);
  
     if($show==1){show_view($nameview);}
     
////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
     
     $nameview='fact_comp';
     echo '<br>==>'.$nameview;
     
     $sql_view='drop view '.$nameview;
     $res_view=msql_query($sql_view,$link_data);
     
     $sql_view='';
     $sql_view.='create view '.$nameview.' as ';
     $sql_view.='select ';
     $sql_view.='convert(idnum1,char) as idn, ';
     $sql_view.='convert(iddet,char) as lin, ';
//      $sql_view.='if(ifnull(mdfrom,"")="","CONT",mdfrom) as modulo, ';
     $sql_view.='docu,';
//      $sql_view.='docu_descri as documento, ';
     $sql_view.='numero,';
     $sql_view.='fecha, ';
     $sql_view.='left(enti,10) as enti, ';
     $sql_view.='item, ';
     
//      $ldocu1="XXXX";
     $sql_view.='(case ';
     $sql_view.='when ifnull(status,"")in("O","X") then 0 ';
//      $sql_view.='when left(docu,2)in('.$ldocu1.') ';
//      $sql_view.='then abs(cant) ';
     $sql_view.='else abs(cant)*1 end) ';
     $sql_view.='as unidades, ';
     
     $sql_view.='puni as prec_unit, ';
//      $sql_view.='cuni as cost_unit, ';
     
     $sql_view.='(case ';
     $sql_view.='when status in("O","X") then 0 ';
//      $sql_view.='when left(docu,2)in('.$ldocu1.') ';
//      $sql_view.='then abs(cant*puni) ';
     $sql_view.='else abs(cant*puni)*1 end)';
     $sql_view.='as subtotal, ';
     
     $sql_view.='(case ';
     $sql_view.='when ifnull(status,"")in("O","X") then 0 ';
//      $sql_view.='when left(docu,2)in('.$ldocu1.') ';
//      $sql_view.='then abs(cant*puni*ifnull(dcto,0)/100) ';
     $sql_view.='else abs(cant*puni*ifnull(dcto,0)/100)*1 end)';
     $sql_view.='as descuento, ';

     $sql_view.='(case ';
     $sql_view.='when ifnull(status,"")in("O","X") then 0 ';
//      $sql_view.='when left(docu,2)in('.$ldocu1.') ';
//      $sql_view.='then (abs(cant*puni)*(1-ifnull(dcto,0)/100) +(abs(cant*puni)*(ifnull(ice,0)/100))) *(ifnull(iva,0)/100) ';
     $sql_view.='else ((abs(cant*puni)*(1-ifnull(dcto,0)/100) +(abs(cant*puni)*(ifnull(ice,0)/100))) *(ifnull(iva,0)/100))*1 end)';
     $sql_view.='as iva, ';

     $sql_view.='(case ';
     $sql_view.='when ifnull(status,"")in("O","X") then 0 ';
//      $sql_view.='when left(docu,2)in('.$ldocu1.') ';
//      $sql_view.='then  (abs(cant*puni)*(1-ifnull(dcto,0)/100) +(abs(cant*puni)*(ifnull(ice,0)/100))) *(1+ifnull(iva,0)/100) +(cant*irbp) +(abs(cant*puni*(1-ifnull(dcto,0)/100))*prop/100) ';
     $sql_view.='else ((abs(cant*puni)*(1-ifnull(dcto,0)/100) +(abs(cant*puni)*(ifnull(ice,0)/100))) *(1+ifnull(iva,0)/100) +(cant*irbp) +(abs(cant*puni*(1-ifnull(dcto,0)/100))*prop/100)) end)';
     $sql_view.='as total, ';
     
//      $sql_view.='ifnull(cntn,"") as cntn, ';
     $sql_view.='concepto, ';
     $sql_view.='detalle, ';
     $sql_view.='ifnull(status,"") as status ';
     $sql_view.='from comp1 ';
     $sql_view.='left join comp2 on idnum1=idnum2 ';
//      $sql_view.='left join csub0 on csub=csub_codigo ';
//      $sql_view.='left join docu0 on docu=docu_codigo ';
     $sql_view.='where ifnull(status,"")not in("O","X") ';
     $sql_view.='and cant!=0 ';
//      $sql_view.='group by idnum1,iddet ';
     $res_view=msql_query($sql_view,$link_data);
  
     if($show==1){show_view($nameview);}
     
////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
     
     $nameview='fact_inve';
     echo '<br>==>'.$nameview;
     
     $sql_view='drop view '.$nameview;
     $res_view=msql_query($sql_view,$link_data);
     
     $sql_view='';
     $sql_view.='create view '.$nameview.' as ';
     $sql_view.='select ';
     $sql_view.='convert(idnum1,char) as idn, ';
     $sql_view.='convert(iddet,char) as lin, ';
//      $sql_view.='if(ifnull(mdfrom,"")="","CONT",mdfrom) as modulo, ';
     $sql_view.='docu,';
//      $sql_view.='docu_descri as documento, ';
     $sql_view.='numero,';
     $sql_view.='fecha, ';
     $sql_view.='left(enti,10) as enti, ';
     $sql_view.='item, ';
     
//      $ldocu1="XXXX";
     $sql_view.='(case ';
     $sql_view.='when ifnull(status,"")in("O","X") then 0 ';
//      $sql_view.='when left(docu,2)in('.$ldocu1.') ';
//      $sql_view.='then abs(cant) ';
     $sql_view.='else abs(cant)*1 end) ';
     $sql_view.='as unidades, ';
     
     $sql_view.='puni as cost_unit, ';
     
     $sql_view.='(case ';
     $sql_view.='when status in("O","X") then 0 ';
//      $sql_view.='when left(docu,2)in('.$ldocu1.') ';
//      $sql_view.='then abs(cant*puni) ';
     $sql_view.='else abs(cant*puni)*1 end)';
     $sql_view.='as total, ';
     
//      $sql_view.='(case ';
//      $sql_view.='when ifnull(status,"")in("O","X") then 0 ';
// //      $sql_view.='when left(docu,2)in('.$ldocu1.') ';
// //      $sql_view.='then abs(cant*puni*ifnull(dcto,0)/100) ';
//      $sql_view.='else abs(cant*puni*ifnull(dcto,0)/100)*1 end)';
//      $sql_view.='as descuento, ';

//      $sql_view.='(case ';
//      $sql_view.='when ifnull(status,"")in("O","X") then 0 ';
// //      $sql_view.='when left(docu,2)in('.$ldocu1.') ';
// //      $sql_view.='then (abs(cant*puni)*(1-ifnull(dcto,0)/100) +(abs(cant*puni)*(ifnull(ice,0)/100))) *(ifnull(iva,0)/100) ';
//      $sql_view.='else ((abs(cant*puni)*(1-ifnull(dcto,0)/100) +(abs(cant*puni)*(ifnull(ice,0)/100))) *(ifnull(iva,0)/100))*1 end)';
//      $sql_view.='as iva, ';

//      $sql_view.='(case ';
//      $sql_view.='when ifnull(status,"")in("O","X") then 0 ';
// //      $sql_view.='when left(docu,2)in('.$ldocu1.') ';
// //      $sql_view.='then  (abs(cant*puni)*(1-ifnull(dcto,0)/100) +(abs(cant*puni)*(ifnull(ice,0)/100))) *(1+ifnull(iva,0)/100) +(cant*irbp) +(abs(cant*puni*(1-ifnull(dcto,0)/100))*prop/100) ';
//      $sql_view.='else ((abs(cant*puni)*(1-ifnull(dcto,0)/100) +(abs(cant*puni)*(ifnull(ice,0)/100))) *(1+ifnull(iva,0)/100) +(cant*irbp) +(abs(cant*puni*(1-ifnull(dcto,0)/100))*prop/100)) end)';
//      $sql_view.='as total, ';
     
//      $sql_view.='ifnull(cntn,"") as cntn, ';
     $sql_view.='concepto, ';
     $sql_view.='detalle, ';
     $sql_view.='ifnull(status,"") as status ';
     $sql_view.='from vtas1 ';
     $sql_view.='left join vtas2 on idnum1=idnum2 ';
//      $sql_view.='left join csub0 on csub=csub_codigo ';
//      $sql_view.='left join docu0 on docu=docu_codigo ';
     $sql_view.='where ifnull(status,"")not in("O","X") ';
     $sql_view.='and cant!=0 ';
//      $sql_view.='group by idnum1,iddet ';
     $res_view=msql_query($sql_view,$link_data);
  
     if($show==1){show_view($nameview);}
     
////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
     
     
     
     
     
}



function show_view($nameview)
{
     if(empty($nameview)){return;}
     if(empty($link_data)){$link_data=$_SESSION['link_data'];}
     
     echo '<br>';
     $sql_query='select * from '.$nameview.' ';
//      if(substr($nameview,0,3)=='dim')
//      {$sql_query.='order by idate desc ';}
     
     if(substr($nameview,0,4)=='fact')
     {$sql_query.='order by fecha desc ';}
     
     $sql_query.='limit 10 ';
     
     $res_query=msql_query($sql_query,$link_data);
     $zreso=$res_query;
     $info=$nameview;
     $name=$nameview;
     $zhead=0;
     $zhidebtns=1;
     include('ele_grid.php');
}
     

?>