<?php

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');

$fecha1='2024-01-01';


// verificar ICH vs ECH
if(1==2)
{
     $sql_data ='select * from caja1 ';
     $sql_data.='left join caja2 on idnum1=idnum2 ';
     $sql_data.='where docu like "ICH%" ';
     $sql_data.='and fecha>="'.$fecha1.'" ';
     $sql_data.='order by enti, fecha, idnum1 ';
     $res_data=msql_query($sql_data,$link_data);

     echo '<br>===>'.msql_num_rows($res_data);

     $xconta=0;
     $lenti='';
     while($aso_data=msql_fetch_assoc($res_data))
     {
     //      echo '<br>==> '.$aso_data['status'].' = '.$aso_data['idnum1'].' = '.$aso_data['docu'].' = '.$aso_data['enti'].' = '.$aso_data['numero'].' = '.$aso_data['fecha'].' = '.number_format($aso_data['cant'].$aso_data['puni'],2);


          $sql_val1 ='select * from caja1 ';
          $sql_val1.='left join caja2 on idnum1=idnum2 ';
          $sql_val1.='where docu like "ECH%" ';
          $sql_val1.='and fecha>="'.$aso_data['fecha'].'" ';
          $sql_val1.='and numero="'.$aso_data['numero'].'" ';
          $sql_val1.='and cant*puni="'.$aso_data['cant']*$aso_data['puni'].'" ';
          $res_val1=msql_query($sql_val1,$link_data);
          $nrows1=msql_num_rows($res_val1);

          $sql_val2 ='select * from bcos1 ';
          $sql_val2.='left join bcos2 on idnum1=idnum2 ';
          $sql_val2.='where docu like "DB1%" ';
          $sql_val2.='and fecha>="'.$aso_data['fecha'].'" ';
     //      $sql_val2.='and numero="'.$aso_data['numero'].'" ';
          $sql_val2.='and enti like "'.substr($aso_data['enti'],0,10).'%" ';
          $sql_val2.='and cant*puni="'.$aso_data['cant']*$aso_data['puni'].'" ';
          $res_val2=msql_query($sql_val2,$link_data);
          $nrows2=msql_num_rows($res_val2);


          if($nrows1!=$nrows2)
          {
               if($aso_data['enti']!=$lenti){echo '<br>'; }
               $lenti=$aso_data['enti'];

               $xconta++;
               echo '<br>';
               echo '=> '.str_pad($xconta,3,'0',STR_PAD_LEFT);
               echo '==> '.$aso_data['status'].' = '.$aso_data['idnum1'].' = '.$aso_data['docu'].' = '.$aso_data['enti'].' = '.$aso_data['numero'].' = '.$aso_data['fecha'].' = '.number_format($aso_data['cant'].$aso_data['puni'],2);
               echo ' => '.$nrows1;
               echo ' = '.$nrows2;
          }

          if($nrows2>1)
          {
               echo '<br>==> '.$sql_val2;
          }



     }

     
}





// verificar DB10 vs FC10
if(1==1)
{
     
     $sql_cxco='select * from (select enti_zsub as zona,min(fecha) as fecha,docum as docu,ptoem as ptoe,numer*1 as numero,vence,enti,left(enti_descri,200) as nombre,sum(total) as total,sum(case when substring(docu,1,1)not in("R","N") then (total-saldo) end) as abono,sum(case when docu like "N%" then abs(saldo) end) as n_cre,sum(case when docu like "R%" then abs(saldo) end) as reten,max(case when docu like "R%" then idnum1 end) as idreten_hide,iva as iva_hide,sum(saldo) as saldo,case when (sum(saldo))>=0.019999 and diasv>0 then diasv else 0 end as dias_v,case when docu=docum and concat(concepto_t,detalle_t)!="" then concat(concepto_t," /",detalle_t) when docu!=docum then concat(concepto_t," /",detalle_t," :: ",docu,"-",numero) else "" end as concepto,(rcdd_hide) as rcdd_hide,(enti_rcdd_hide) as enti_rcdd_hide,left(rcdd_descri, locate(" ",rcdd_descri)+5) as rcdd,(vndd_hide) as vndd_hide,(enti_vndd_hide) as enti_vndd_hide,left(vndd_descri, locate(" ",vndd_descri)+5) as vndd,max(fecha) as u_movi,ifnull(status,"") as st,tabla1 as tabla1_hide,docu as docu_hide,numero as numero_hide,idnum1 as idnum1_hide,iddet as iddet_hide,idfrom as idfrom_hide,mdfrom as mdfrom_hide,status as st_hide from (select * from (select idnum1,iddet, case when left(docu,2) in ("FC","AC","LC","PR") then 1 else 2 end as headet, docu,ptoe,numero,fecha, fecha as fcobro, adddate(fecha,ifnull(plazo,0)) as vence, datediff(curdate(),adddate(fecha,ifnull(plazo,0))) as diasv, sum(case when status in("O","X") then 0 when left(docu,2) in ("FC","AC","LC","PR") 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) 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))*-1 end) as saldo, sum(case when status in("O","X") then 0 when left(docu,2) in ("FC","AC","LC","PR") 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) else 0 end) as total, case when left(docu,2) in ("FC","AC","LC","PR") then docu else todocu end as docum, case when left(docu,2) in ("FC","AC","LC","PR") then ptoe else toptoe end as ptoem, case when left(docu,2) in ("FC","AC","LC","PR") then numero else tonume end as numer, max(iva) as iva, 0 as anticipo, 0 as retencion, 0 as cair, 0 as base, 0 as porc_rta, 0 as porc_iva, 0 as efectivo, 0 as cheque, 0 as protesto, left(enti,10) as enti, enti_codigo,enti_descri,enti_razsoc,enti_nomcom,enti_zsub,enti_tpro,enti_status,concat((enti_wsapp),"; ",(enti_telefono),"; ",(enti_celular)) as enti_telefonos,concat(enti_email,"; ",enti_emailce) as enti_emails,docu_descri,zsub_descri,tpro_descri, concepto,detalle, replace(replace(replace(replace(trim(ifnull(concepto,"")),",",", "),";","; ")," "," "),"+"," +") as concepto_t,replace(replace(replace(replace(trim(ifnull(detalle,"")),",",", "),";","; ")," "," "),"+"," +") as detalle_t,vndd as vndd_hide, enti_vndd as enti_vndd_hide, vndd_descri, rcdd as rcdd_hide, enti_rcdd as enti_rcdd_hide, rcdd_descri, "" as caja, "" as ctab, status, idate, idnum1 as idfrom, "vtas" as mdfrom, "vtas" as tabla1 from vtas1 left join vtas2 on idnum1=idnum2 left join enti0 on enti=enti_codigo left join docu0 on docu=docu_codigo left join zsub0 on zsub=zsub_codigo left join tpro0 on enti_tpro=tpro_codigo left join vndd0 on vndd=vndd_codigo left join rcdd0 on enti_rcdd=rcdd_codigo where status not in ("X") group by idnum1 union select idnum1,iddet, 2 as headet, docu,ptoe,numero,fecha, fcobro, 0 as vence, 0 as diasv, sum(case when status in("O","X") then 0 when left(docu,2)in("IE","AC","IV","IC") and ifnull(fcobro,fecha)>fecha and ifnull(fcobro,fecha)>now() then 0 when left(docu,2)in("IE","AC","IV","IC") then abs(cant)*-1 else abs(cant) end) as saldo, 0 as total, docum,ptoem,numer, 0 as iva, sum(case when status in("O","X") then 0 when docu in("AC11","AP11") then abs(cant)*-1 else 0 end) as anticipo, 0 as retencion, 0 as cair, 0 as base, 0 as porc_rta, 0 as porc_iva, sum(case when status in("O","X") then 0 when left(docu,2) in("IE") and fcobro>"2025-03-15" then abs(cant) else 0 end) as efectivo, sum(case when status in("O","X") then 0 when left(docu,3) in("ICH") and fcobro>"2025-03-15" then abs(cant) else 0 end) as cheque, sum(case when status in("O","X") then 0 when left(docum,2) in("PR") then abs(cant)*-1 else 0 end) as protesto, left(enti,10) as enti, enti_codigo,enti_descri,enti_razsoc,enti_nomcom,enti_zsub,enti_tpro,enti_status,concat((enti_wsapp),"; ",(enti_telefono),"; ",(enti_celular)) as enti_telefonos,concat(enti_email,"; ",enti_emailce) as enti_emails,docu_descri,zsub_descri,tpro_descri, concepto,detalle, replace(replace(replace(replace(trim(ifnull(concepto,"")),",",", "),";","; ")," "," "),"+"," +") as concepto_t,replace(replace(replace(replace(trim(ifnull(detalle,"")),",",", "),";","; ")," "," "),"+"," +") as detalle_t,"" as vndd_hide, "" as enti_vndd_hide, "" as vndd_descri,"" as rcdd_hide, enti_rcdd as enti_rcdd_hide, rcdd_descri, caja as caja, "" as ctab, status, idate, idnum1 as idfrom, "caja" as mdfrom, "caja" as tabla1 from caja1 left join caja2 on idnum1=idnum2 left join enti0 on enti=enti_codigo left join docu0 on docu=docu_codigo left join zsub0 on enti_zsub=zsub_codigo left join tpro0 on enti_tpro=tpro_codigo left join rcdd0 on enti_rcdd=rcdd_codigo where status not in ("X") and (left(docu,2)in("IE","AC","IV","IC") or left(docu,2)in("EE","XC"))and left(docum,2)in("FC","AC","LC","PR") group by idnum1,iddet union select idnum1,iddet, case when left(docu,2) in ("PR") then 1 else 2 end as headet, docu,ptoe,numero, fecha, fcobro, 0 as vence, 0 as diasv, sum(case when status in("O","X") then 0 when left(docu,2)in("DxB","DC") then abs(cant)*-1 else abs(cant) end) as saldo, sum(case when status in("O","X") then 0 when left(docu,2)in("PR") then abs(cant) else 0 end) as total, case when left(docu,2) in ("PR") then docu else docum end as docum, case when left(docu,2) in ("PR") then ptoe else ptoem end as ptoem, case when left(docu,2) in ("PR") then numero else numer end as numer, 0 as iva, 0 as anticipo, 0 as retencion, 0 as cair, 0 as base, 0 as porc_rta, 0 as porc_iva, 0 as efectivo, 0 as cheque, sum(case when status in("O","X") then 0 when left(docu,2)in("PR") then abs(cant) when left(docum,2)in("PR") then abs(cant)*-1 else 0 end) as protesto, left(enti,10) as enti, enti_codigo,enti_descri,enti_razsoc,enti_nomcom,enti_zsub,enti_tpro,enti_status,concat((enti_wsapp),"; ",(enti_telefono),"; ",(enti_celular)) as enti_telefonos,concat(enti_email,"; ",enti_emailce) as enti_emails,docu_descri,zsub_descri,tpro_descri, concepto,detalle, replace(replace(replace(replace(trim(ifnull(concepto,"")),",",", "),";","; ")," "," "),"+"," +") as concepto_t,replace(replace(replace(replace(trim(ifnull(detalle,"")),",",", "),";","; ")," "," "),"+"," +") as detalle_t,"" as vndd_hide, "" as enti_vndd_hide, "" as vndd_descri,"" as rcdd_hide, enti_rcdd as enti_rcdd_hide, rcdd_descri, "" as caja, ctab as ctab, status, idate, idnum1 as idfrom, "bcos" as mdfrom, "bcos" as tabla1 from bcos1 left join bcos2 on idnum1=idnum2 left join enti0 on enti=enti_codigo left join docu0 on docu=docu_codigo left join zsub0 on enti_zsub=zsub_codigo left join tpro0 on enti_tpro=tpro_codigo left join rcdd0 on enti_rcdd=rcdd_codigo where status not in ("X") and (left(docu,2)in("DxB","DC") or left(docu,2)in("CH","DP","PR"))and (left(docum,2)in("FC","AC","LC","PR") or docu like "PR%")group by idnum1,iddet union select idnum1,iddet, 2 as headet, docu,ptoe,numero,fecha, fecha as fcobro, 0 as vence, 0 as diasv, (case when status in("O","X") then 0 when left(docu,2)in("RC") then abs(cant*puni)/100*(-1) else abs(cant*puni)/100 end) as saldo, 0 as total, docum,ptoem,numer, 0 as iva, 0 as anticipo, (case when status in("O","X") then 0 when left(docu,2) in("RC","RP") then abs(cant*puni)/100 else 0 end) as retencion, (cair) as cair, 0 as base, (case when status in("O","X") then 0 when cair*1>=300 then puni else 0 end)as porc_rta, (case when status in("O","X") then 0 when cair*1<300 then puni else 0 end)as porc_iva, 0 as efectivo, 0 as cheque, 0 as protesto, left(enti,10) as enti, enti_codigo,enti_descri,enti_razsoc,enti_nomcom,enti_zsub,enti_tpro,enti_status,concat((enti_wsapp),"; ",(enti_telefono),"; ",(enti_celular)) as enti_telefonos,concat(enti_email,"; ",enti_emailce) as enti_emails,docu_descri,zsub_descri,tpro_descri, concepto,detalle, replace(replace(replace(replace(trim(ifnull(concepto,"")),",",", "),";","; ")," "," "),"+"," +") as concepto_t,replace(replace(replace(replace(trim(ifnull(detalle,"")),",",", "),";","; ")," "," "),"+"," +") as detalle_t,"" as vndd_hide, "" as enti_vndd_hide, "" as vndd_descri,"" as rcdd_hide, enti_rcdd as enti_rcdd_hide, rcdd_descri, "" as caja, "" as ctab, status, idate, idnum1 as idfrom, "retc" as mdfrom, "retc" as tabla1 from retc1 left join retc2 on idnum1=idnum2 left join enti0 on enti=enti_codigo left join docu0 on docu=docu_codigo left join zsub0 on enti_zsub=zsub_codigo left join tpro0 on enti_tpro=tpro_codigo left join rcdd0 on enti_rcdd=rcdd_codigo where status not in ("X") and numer*1>0 and left(docu,2)in("RC") group by idnum1,iddet union select idnum1,iddet, case when left(docu,2)in("FC","AC","LC","PR") or docu like "%00" then 1 else 2 end as headet, docu,ptoe,numero, fecha, fecha as fcobro, 0 as vence, 0 as diasv, sum(case when status in("O","X") then 0 when left(docu,2)in("CC") then abs(cant*puni)*-1 else abs(cant*puni) end) as saldo, sum(case when status in("O","X") then 0 when left(docu,2)in("FC","AC","LC","PR") or docu like "%00" then abs(cant*puni) else 0 end) as total, case when left(docu,2)in("FC","AC","LC","PR") then docu else docum end as docum, case when left(docu,2)in("FC","AC","LC","PR") then ptoe else ptoem end as ptoem, case when left(docu,2)in("FC","AC","LC","PR") then numero else numer end as numer, 0 as iva, sum(case when status in("O","X") then 0 when docu in("AC10","AP10") then abs(cant*puni) else 0 end) as anticipo, 0 as retencion, 0 as cair, 0 as base, 0 as porc_rta, 0 as porc_iva, 0 as efectivo, 0 as cheque, 0 as protesto, left(enti,10) as enti, enti_codigo,enti_descri,enti_razsoc,enti_nomcom,enti_zsub,enti_tpro,enti_status,concat((enti_wsapp),"; ",(enti_telefono),"; ",(enti_celular)) as enti_telefonos,concat(enti_email,"; ",enti_emailce) as enti_emails,docu_descri,zsub_descri,tpro_descri, concepto,detalle, replace(replace(replace(replace(trim(ifnull(concepto,"")),",",", "),";","; ")," "," "),"+"," +") as concepto_t,replace(replace(replace(replace(trim(ifnull(detalle,"")),",",", "),";","; ")," "," "),"+"," +") as detalle_t,"" as vndd_hide, "" as enti_vndd_hide, "" as vndd_descri,"" as rcdd_hide, enti_rcdd as enti_rcdd_hide, rcdd_descri, "" as caja, "" as ctab, status, idate, idnum1 as idfrom, "dxco" as mdfrom, "dxco" as tabla1 from dxco1 left join dxco2 on idnum1=idnum2 left join enti0 on enti=enti_codigo left join docu0 on docu=docu_codigo left join zsub0 on enti_zsub=zsub_codigo left join tpro0 on enti_tpro=tpro_codigo left join rcdd0 on enti_rcdd=rcdd_codigo where status not in ("X") group by idnum1,iddet ) as cxcopa where status not in("X") and fecha>="2000-01-01" and docu not in("CC18","XC18","CP18","XP18") and fecha<="2025-03-14" ) as maes0 where docum like "F%" or docum like "PR%" or docum like "AC%" or docum like "%00"group by docum,numer order by fecha desc, idnum1 desc, iddet desc ) as maes1 where docu not like "A%" and saldo>0.019999 order by enti,fecha ';
     $res_cxco=msql_query($sql_cxco,$link_data);     

     
     while($aso_cxco=msql_fetch_assoc($res_cxco))
     {
     
          $sql_data ='select * from bcos1 ';
          $sql_data.='left join bcos2 on idnum1=idnum2 ';
          $sql_data.='where docu like "DB%" ';
          $sql_data.='and fecha>="'.$aso_cxco['fecha'].'" ';
          $sql_data.='and docum not like "IC%" ';
          $sql_data.='and enti like "'.substr($aso_cxco['enti'],0,10).'%" ';
          $sql_data.='order by enti, fecha, idnum1 ';
          $res_data=msql_query($sql_data,$link_data);
          
          $nrows1=msql_num_rows($res_data);
          
          
          if($nrows1>0)
          {
          echo '<br>';
          echo '==> '.$aso_cxco['fecha'].' - '.$aso_cxco['docu'].' - '.$aso_cxco['numero'].' - '.$aso_cxco['enti'].' - '.$aso_cxco['nombre'].' - ';
          echo '==>'.$nrows1;
          }

//           $xconta=0;
//           $lenti='';
//           while($aso_data=msql_fetch_assoc($res_data))
//           {
//           //      echo '<br>==> '.$aso_data['status'].' = '.$aso_data['idnum1'].' = '.$aso_data['docu'].' = '.$aso_data['enti'].' = '.$aso_data['numero'].' = '.$aso_data['fecha'].' = '.number_format($aso_data['cant'].$aso_data['puni'],2);


//                $sql_val1 ='select * from caja1 ';
//                $sql_val1.='left join caja2 on idnum1=idnum2 ';
//                $sql_val1.='where docu like "ECH%" ';
//                $sql_val1.='and fecha>="'.$aso_data['fecha'].'" ';
//                $sql_val1.='and numero="'.$aso_data['numero'].'" ';
//                $sql_val1.='and cant*puni="'.$aso_data['cant']*$aso_data['puni'].'" ';
//                $res_val1=msql_query($sql_val1,$link_data);
//                $nrows1=msql_num_rows($res_val1);

//                $sql_val2 ='select * from bcos1 ';
//                $sql_val2.='left join bcos2 on idnum1=idnum2 ';
//                $sql_val2.='where docu like "DB1%" ';
//                $sql_val2.='and fecha>="'.$aso_data['fecha'].'" ';
//           //      $sql_val2.='and numero="'.$aso_data['numero'].'" ';
//                $sql_val2.='and enti like "'.substr($aso_data['enti'],0,10).'%" ';
//                $sql_val2.='and cant*puni="'.$aso_data['cant']*$aso_data['puni'].'" ';
//                $res_val2=msql_query($sql_val2,$link_data);
//                $nrows2=msql_num_rows($res_val2);


//                if($nrows1!=$nrows2)
//                {
//                     if($aso_data['enti']!=$lenti){echo '<br>'; }
//                     $lenti=$aso_data['enti'];

//                     $xconta++;
//                     echo '<br>';
//                     echo '=> '.str_pad($xconta,3,'0',STR_PAD_LEFT);
//                     echo '==> '.$aso_data['status'].' = '.$aso_data['idnum1'].' = '.$aso_data['docu'].' = '.$aso_data['enti'].' = '.$aso_data['numero'].' = '.$aso_data['fecha'].' = '.number_format($aso_data['cant'].$aso_data['puni'],2);
//                     echo ' => '.$nrows1;
//                     echo ' = '.$nrows2;
//                }

//                if($nrows2>1)
//                {
//                     echo '<br>==> '.$sql_val2;
//                }



//           }

          
     }     
     
}





?>