<?php
$pdf = Yii::createComponent('application.extensions.mpdf60.mpdf');
//$html=''.$model->fecha_comprobante.'<br>'.$model->fecha_final.'<br>'.$model->id_cuenta.'<br>'.$model->cuenta_hasta.'';
$f1=$_GET['fecha1'];
$f2=$_GET['fecha2'];
$c1=$_GET['c1'];
$c2=$_GET['c2'];
$sm=$_GET['sm'];
$saldo=$_GET['saldo'];
$pag=$_GET['pag'];
//var_dump($_GET['trep']);
//echo $c1."<br>";
//echo $c2;
 $saldo=0;
//$html='<br><br><br><br><br>';
      //$cuenta= CuentasConsolidada::model()->findByPk($c1);
      $criteria = new CDbCriteria; 
      $criteria->select = "id_cuenta,codigo_cuenta,nombre_cuenta";
     if ($sm==1){
      $criteria->condition = "id_cuenta between $c1 and $c2 group by id_cuenta,codigo_cuenta,nombre_cuenta";
     }else{
      $criteria->condition = "id_cuenta between $c1 and $c2 and fecha_comprobante between '$f1' and  '$f2' group by id_cuenta,codigo_cuenta,nombre_cuenta";
     }
      $criteria->order= "id_cuenta asc";
      $mayor_cuenta= MayorAnaliticoCuenta::model()->findAll($criteria);
//       echo "<pre>";       print_r($mayor_cuenta);exit;
      foreach ($mayor_cuenta as $c){
       //  echo $c->codigo_cuenta." ".$c->nombre_cuenta."<br>";
      
 
      
      $criteria = new CDbCriteria;                                    
      $criteria->condition = "fecha_comprobante between '$f1' and  '$f2' and id_cuenta=$c->id_cuenta";
      $criteria->order = 'numero asc';
      $mayor= MayorAnaliticoCuenta::model()->findAll($criteria);
//     echo "<pre>";       print_r($criteria); exit;
//     echo "<pre>";       print_r($mayor); exit;
		   //$cuenta=$item->cedula. " ". $item->nombres ;
    
$html=$html.'<br>
    <table width="100%"  style="font-size:11px;; font-family: arial">
    <tr><td width="15%"><b>Código:</b></td><td>'.$c->codigo_cuenta.'</td></tr>
    <tr><td width="15%"><b>Descripción:</b></td><td>'.$c->nombre_cuenta.'</td></tr>
    </table>
    <table width="100%" style="font-size:10px;; font-family: arial">
    <tr>
    <td width="10%" style="border-width: 1px;  border-bottom:dotted;text-align:center"><b>Fecha</b></td>
    <td width="5%" style="border-width: 1px;  border-bottom:dotted;text-align:center"><b>N° Comp.</b></td>
    <td width="10%" style="border-width: 1px;  border-bottom:dotted;text-align:center"><b>Aux.</b></td>
    <td width="15%" style="border-width: 1px;  border-bottom:dotted;text-align:right"><b>Monto Debe</b></td>
    <td width="15%" style="border-width: 1px;  border-bottom:dotted;text-align:right"><b>Monto Haber</b></td>
    <td width="15%" style="border-width: 1px;  border-bottom:dotted;text-align:right"><b>Saldo</b></td>
    <td width="10%" style="border-width: 1px;  border-bottom:dotted;text-align:center"><b>Ref.</b></td>
    <td width="20%" style="border-width: 1px;  border-bottom:dotted;text-align:center"><b>Descripción</b></td>
    <td width="10%" style="border-width: 1px;  border-bottom:dotted;text-align:center"><b>Fecha Asiento</b></td>
    
    </tr>
    </table>
    <table width="100%" style="font-size:11px;font-family: arial">    
    <tr>
    <td width="5%" align="center"></td>
    <td width="5%" align="center"></td>
    <td width="15%" align="right">SALDO INICIAL.....</td>
    <td width="15%" align="right"></td>
    <td width="15%" align="right"></td>
    <td width="15%" align="right">0,00</td>
    <td width="10%" align="center"></td>
    <td width="20%"></td>
    <td width="10%"></td>
    </tr>
    </table> 
    
    ';
   $total_debe=0;
   $total_haber=0;
   $saldo=0;
foreach ($mayor as $m) {
   $total_debe=$total_debe+$m->monto_debe; 
   $total_haber=$total_haber+$m->monto_haber; 
    if($m->monto_debe>0){ 
    $saldo=$saldo+$m->monto_debe;
    }
    if($m->monto_haber>0){ 
    $saldo=$saldo-$m->monto_haber;
    }
   
     //$saldo=$saldo-$m->monto_haber;
     $html.='
     <table width="100%" style="font-size:11px; font-family: arial">    
    <tr>
    <td width="10%" align="center">'.date('d-m-Y',  strtotime($m->fecha_comprobante)).'</td>
    <td width="5%" align="center">'.$m->numero.'</td>
    <td width="10%" align="center">'.$m->cuenta->cedula.'</td>
    <td width="15%" align="right">'.number_format($m->monto_debe,2,',','.').'</td>
    <td width="15%" align="right">'.number_format($m->monto_haber,2,',','.').'</td>
    <td width="15%" align="right">'.$saldo.'</td>
    <td width="10%" align="center">'.$m->referencia.'</td>
    <td width="20%">'.$m->descripcion.'</td>
    <td width="10%">'.date('d-m-Y',  strtotime($m->fecha_creado)).'</td>
    </tr>
    
    </table>
    '; 
      }
    $html=$html.'
<table width="100%" style="font-size:11px;font-family: arial">    
    <tr>
    <td width="10%" align="center"></td>
    
    <td width="15%" align="right">TOTAL.....</td>
    <td width="15%" align="right" style="border-width: 5px;border: double ;border-top: black 1px dashed;border-left: black 1px dashed;border-right: black 1px dashed; border-color: black;">'.number_format($total_debe,2,',','.').'</td>
    <td width="15%" align="right" style="border-width: 5px;border: double ;border-top: black 1px dashed;border-left: black 1px dashed;border-right: black 1px dashed; border-color: black;">'.number_format($total_haber,2,',','.').'</td>
    <td width="15%" align="right" style="border-width: 5px;border: double ;border-top: black 1px dashed;border-left: black 1px dashed;border-right: black 1px dashed; border-color: black;">'.number_format($total_debe-$total_haber,2,',','.').'</td>
    <td width="10%" align="center"></td>
    <td width="20%"></td>
    <td width="10%"></td>
    </tr>
    </table> 
 
';   
    $pag="Pág. ".'{PAGENO}';
      }
      
$header=$header.'<p align="right" style="font-size:11px;">'.$pag.'</p><p align="center" style="font-family:Arial"><b>'.Empresa::RazonSocial().'<BR>
     RIF '.Empresa::Rif().'<br>
    Movimiento de una Cuenta Del '.$f1.' AL '.$f2.'<br><p align="center" style="font-size:10px">Emitido el '.date('d-m-Y').' a las '.date("h:i A").'</b></p><br></p>
    <br><br> 
    ';
$mpdf=new mPDF('win-1252','LETTER','','',9,9,45,5,5,5);
//$mpdf->SetHeader('    {DATE j/m/Y}.||Página {PAGENO}/{nbpg}               ');
$mpdf->SetHTMLHeader($header);
$mpdf->WriteHTML($html);
//echo $header;
//echo $html;
$mpdf->Output();
exit;
?>