<?
//http://mixelaneo.blogspot.com/
header("Content-Type: application/xhtml+xml; charset=iso-8859-1");
use_helper('UrlFunctions');

$o_BalanceDiaNew = new BalanceDiaBean();

if($cboColaborador){
	$o_BalanceDiaNew->setIdColaborador($cboColaborador); 
}else{
	$o_BalanceDiaNew->setIdColaborador(myUser::getUser()->getIdColaborador()); 	
}
$o_BalanceDiaNew->setFechaBalance($txtFecha); 

$o_BalanceDiaDP = new BalanceDiaDP($o_BalanceDiaNew);
$a_resumen_detalle = $o_BalanceDiaDP->ObtenerResumenDetalleMercado();

$a_inicioOperacion = $a_resumen_detalle['InicioOperacion'];
$a_pagos = $a_resumen_detalle['Pagos'];
$a_sofa = $a_resumen_detalle['Sofa'];
$a_prestamos = $a_resumen_detalle['Prestamos'];

ViewResponse::getInstance()->setMethodAjax();
  
?>
<script>
functionSort = function(campo, type){
  var typeSort      = "";
  var elmSortCampos = $('sortCampos');
  var elmSortType   = $('sortType');
  var a_sortCampos  = elmSortCampos.value!=""?elmSortCampos.value.split(";"):[];
  var a_sortType    = elmSortType.value!=""?elmSortType.value.split(";"):[];
  var pos           = a_sortCampos.indexOf(campo);
  switch (type){
     case 'A' :
       a_sortType[pos]    = "2";
     break;
     case 'D' :
       a_sortCampos = sliceByPos(a_sortCampos, pos);
       a_sortType   = sliceByPos(a_sortType, pos);
     break;
     default :
       a_sortCampos[a_sortCampos.length] = campo;
       a_sortType[a_sortType.length]     = "1";
     break;
  }
  var strSortCampos = a_sortCampos.join(";");
  var strSortType   = a_sortType.join(";");
  concatValueStr('sortCampos='+strSortCampos+'&sortType='+strSortType+"&page=<?=$page?>");
}
</script>
<input type="hidden" id="sortCampos" size="100" value="<?=$valuesSort['sortCampos']?>">
<input type="hidden" id="sortType" size="100" value="<?=$valuesSort['sortType']?>">
      <table class="marco" border="0" cellpadding="0" cellspacing="0" width="90%">
        <tr>
          <td>
            <table class="grid" width="100%" border="0" cellpadding="0" cellspacing="0">
            	<tr>
            		<td colspan="9" style="font-family:Verdana, Arial, Helvetica, sans-serif; font-weight:bold; color:#000000; font-size:12px; background: #E3E9E2">Pedido</td>
				</tr>
				
				<tr>
				    <td colspan="9">
				<?php
				$o_Cr_pedido_prestamoFN = new Cr_pedido_prestamoFN(); 
				$o_Cr_pedido_prestamoFN->setFechaTransaccion( $txtFecha );
				if($cboColaborador){
					$o_Cr_pedido_prestamoFN->setIdColaborador($cboColaborador); 
				}else{
					$cboColaborador = myUser::getUser()->getIdColaborador();
					$o_Cr_pedido_prestamoFN->setIdColaborador(myUser::getUser()->getIdColaborador()); 	
				}
				$a_Cr_pedido_prestamo = $o_Cr_pedido_prestamoFN->select();
				?>

        			    <table class="grid" width="100%" border="0" cellpadding="0" cellspacing="0">
                          <tr>
                            <th width="2%" >&nbsp;</th>
                            <th width="5%" align="center">Nro Cliente</th> 
                            <th width="20%" align="center">Cliente</th> 
                    		<th width="5%"  align="center" >Nro Pedido</th>
                            <th width="5%" align="center">Fecha</th>
							<th width="5%" align="center">Estado</th>
                            <th width="5%" align="center">Monto</th>
							<th width="6%" align="center">Monto Aprob</th>
                            <!--<th width="5%" align="center">Linea Credito</th>-->
                            <th width="10%" align="center">Usr.Cre.</th>
                            <th width="10%" align="center">Fec.Cre.</th>
                          </tr>
                              <? if(count($a_Cr_pedido_prestamo)>0){ ?>
                                <?php $f_MontoTotal = 0.00 ?>
                              <? 
                              foreach ($a_Cr_pedido_prestamo as $iKey => $o_objBean)
                              { 
                                $i_Idpedido = $o_objBean->getIdpedido();
                              	$o_ClienteBeantemp = new ClienteBean();
                              	$o_ClienteBeantemp->setIdCliente($o_objBean->getIdCliente());
                              	$o_ClienteBLtemp = new ClienteBL();
                              	$o_ClienteBean = $o_ClienteBLtemp->busquedaCliente( $o_ClienteBeantemp );
                              	//$f_LineaCredito = $o_ClienteBean[0]->getLineaCredito();
                              	//echo "<pre>"; 	print_r( $o_ClienteBean ); 	echo "</pre>";
                              	$f_MontoTotal = $f_MontoTotal + floatval( $o_objBean->getMonto() );
								$f_MontoTotalFinal = $f_MontoTotalFinal + floatval( $o_objBean->getMontoFinal() );
								
								$a_prestamoId[] = $o_objBean->getIdPrestamo();
								$a_pedidoprestamoId[$o_objBean->getIdPrestamo()] = $o_objBean->getIdpedido();
                              ?>
                              <tr>
                                <td align="center"><?=$iKey+1?></td>
                                <td align="center"><?php echo $o_ClienteBean[0]->getIdCliente() ?></td>
                        		<td align="left"><?php echo $o_ClienteBean[0]->getApellidoPaterno()." ".$o_ClienteBean[0]->getApellidoMaterno()." ". $o_ClienteBean[0]->getNombres() ?></td>
                                <td align="center"><?php echo $o_objBean->getidpedido() ?></td>
                                <td align="center"><?php echo $o_objBean->getFechaTransaccion() ?></td>
								<td align="center"><?if($o_objBean->getEstado()=='A'){ echo "Aprobado"; }elseif($o_objBean->getEstado()=='G'){ echo "Generado"; }elseif($o_objBean->getEstado()=='P'){ echo "Pendiente";}  else {echo "Rechazado";} ?></td>
                                <td align="center"><?php echo "S/.&nbsp;".number_format( $o_objBean->getMonto(), 2 ) ?></td>
                                <td align="center" style="border-top: 1px solid black; background-color:#B8DB4D; font-family: times, serif; font-size:10pt;"><?php echo "S/.&nbsp;".number_format( $o_objBean->getMontoFinal(), 2 ) ?></td>
                                <td align="center"><?php echo "S/.&nbsp;".number_format( $f_LineaCredito, 2 ) ?></td>
                                <td align="center"><?php echo $o_objBean->getUsuarioCreacion() ?></td>
                                <td align="center"><?php echo $o_objBean->getFechaCreacion() ?></td>
                              </tr>
        				        <?php } ?>
                              <tr>
                                <td align="center">&nbsp;</td>
                                <td align="center">&nbsp;</td>
                        		<td align="center">&nbsp;</td>
                                <td align="center">&nbsp;</td>
                                <td align="center">&nbsp;</td>
								<!--<td align="center">&nbsp;</td>-->
                                <td align="center" style="border-top: 1px solid black; background-color: #B8DB4D; font-family: times, serif; font-size:10pt;color:#000;"><b><?php echo "S/.&nbsp;".number_format( $f_MontoTotal, 2 ) ?></b></td>
								<td align="center" style="border-top: 1px solid black; background-color: #B8DB4D; font-family: times, serif; font-size:10pt;color:#000;"><b><?php echo "S/.&nbsp;".number_format( $f_MontoTotalFinal, 2 ) ?></b></td>
                                <td align="center">&nbsp;</td>
                                <td align="center">&nbsp;</td>
                                <td align="center">&nbsp;</td>
                              </tr>
        				    <?php } ?>
				        </table>

				    </td>
				</tr>
            	<tr>
            		<td colspan="9" style="font-family:Verdana, Arial, Helvetica, sans-serif; font-weight:bold; color:#000000; font-size:12px; background: #E3E9E2">Caja</td>
				 </tr>
	              	<?php
	              	if(is_array($a_inicioOperacion)){?>
					<tr>
						<td colspan="9" align="right"><b><?php echo "S/.&nbsp;".number_format((float)$a_inicioOperacion['IO'],2);?></b></td>
	              	<?php	
	              	}
	              	?>
              	</tr>
              	<tr>
              		<td colspan="9" style="font-family:Verdana, Arial, Helvetica, sans-serif; font-weight:bold; color:#000000; font-size:12px; background: #E3E9E2">Pagos</td>
              	</tr>
              	<?php
              	$SumatoriaTotalPagos = 0;
              	if(is_array($a_pagos)){
              	foreach ($a_pagos as $key => $a_pagosTemp){?>
              		<tr>
					    <td width="2%">&nbsp;</td>
	              		<td width="10%" style="font-family:Verdana, Arial, Helvetica, sans-serif; color:#000000; font-size:12px; background: #E3E9E2"><?php echo "Mercado&nbsp;".ucwords(strtolower($a_pagosTemp['NombreMercado']))?></td>
	              		<td colspan="7"  align="right"><?php 
	              		$SumatoriaTotalPagos += $a_pagosTemp['PA'];
	              		echo "S/.&nbsp;".number_format($a_pagosTemp['PA'],2);
	              		$SumatoriaTotalPagos += $a_pagosTemp['PAM'];
	              		echo "<br>Por moras: S/.&nbsp;";
	              		
	              		?>
	              		<a style="cursor:pointer;text-decoration:underline;color:#00f;" href="#" onclick="objMainTab.modalDialog('<?php echo buildUrlComplet( 'op.php/ajax/requestPdo', 's_TypeRequest=infopopuppagos&s_fecha='.$txtFecha.'&i_idColaborador='. $cboColaborador)?>',{width:700,height:400})"><?php echo number_format($a_pagosTemp['PAM'],2)?></a>
	              		</td>
	              	</tr>	
              	<?php 
              	}
              	}
              	?>
              	<tr>
              		<td width="10%" align="right">&nbsp;</td>
              		<td colspan="8"  align="right"><B>T. Pagos&nbsp;= &nbsp;&nbsp; <?php echo "S/.&nbsp;".number_format((float)$SumatoriaTotalPagos,2)?></B></td>
              	</tr>
              	<tr>
              		<td colspan="9" style="font-family:Verdana, Arial, Helvetica, sans-serif; font-weight:bold; color:#000000; font-size:12px; background: #E3E9E2">Prestamos</td>
              		
              	</tr>
              	<?php
			    $sumatoriatotalprestamo = 0;
				$sumatoriatotalgastos = 0;
				
              	if(is_array($a_prestamos)){
              	foreach ($a_prestamos as $key => $a_prestamosTemp){
              		
              		?>
              	     
              		<tr>
	              	    <td width="2%" >&nbsp;</td>
						<td  colspan="8"  style="font-family:Verdana, Arial, Helvetica, sans-serif; color:#000000; font-size:12px; background: #E3E9E2"><?php echo "Mercado&nbsp;".$key?></td>
	              	</tr>
	              	<?php 
	              	if(is_array($a_prestamosTemp)){
					
	              	foreach ($a_prestamosTemp as $keydet => $a_PrestamoDetalle){
		              		if($a_PrestamoDetalle['OR']=='R'){
		              		   $origen='N';
		              		}elseif($a_PrestamoDetalle['OR']=='F'){
		              			$origen='F';
								if($a_PrestamoDetalle['ME']){
									$origen='R';
								}
		              		}
		              		//print_r($a_prestamoId);
		              		$bg = '';
		              		if(is_array($a_prestamoId) && in_array($keydet,$a_prestamoId)){
		              			$bg = "#ffcc00";
		              		}
	              		?>	
		              <tr style="background:<?php echo $bg;?>">
		              		<td width="2%" align="right" >&nbsp;<?php echo $a_pedidoprestamoId[$keydet]?>&nbsp;&nbsp;<?php // echo $keydet;?></td>
							<td width="2%">&nbsp;<a href="#" onclick="parent.objMainTab.modalDialog('<?php echo buildUrlComplet('mnt.php/cli/datDoc', "id=".$a_PrestamoDetalle['Idcliente']."&sinTabs=1") ?>',{width:1000,height:500})"><img title="Ver imagenes" src="/images/common/buttons/btnCamara.gif" /></a></td>
							<td style="font-family:Verdana, Arial, Helvetica, sans-serif; color:#000000; font-size:11px;"><a style="cursor:pointer;text-decoration:underline;color:#00f;" href="#" onclick="objMainTab.modalDialog('<?php echo buildUrlComplet( 'op.php/ajax/requestPdo', 's_TypeRequest=infopopupprestamo&i_idcliente='.$a_PrestamoDetalle['Idcliente'].'&s_tipo=' )?>',{width:700,height:400})"><?php echo ucwords(strtolower($a_PrestamoDetalle['NombreCliente']))?></a></td>
		              		<td>&nbsp;&nbsp;<?php echo $a_PrestamoDetalle['ES']?></td>
		              		<td>&nbsp;&nbsp;<?php echo $origen?></td>
		              		<!--<td>&nbsp;&nbsp;<?php echo $a_PrestamoDetalle['PO']."%"?></td>-->
		              		<td align="right"><?php $sumatoriatotalgastos += $a_PrestamoDetalle['GA']; echo "S/.&nbsp;".number_format((float)$a_PrestamoDetalle['GA'],2)?></td>
		              		<td align="center">&nbsp;&nbsp;<?php echo $a_PrestamoDetalle['PO']."%"?></td>
		              		<td  align="right">&nbsp;&nbsp;<?php echo "S/.".number_format($a_PrestamoDetalle['ME'],2);$sumatoriaME+=$a_PrestamoDetalle['ME']; ?></td>
		              		<td  align="right"><?php if ($a_PrestamoDetalle['OR']=='R') { $sumatoriatotalprestamo  +=  $a_PrestamoDetalle['PR'];$sumatoriatotalprestamonuevo  +=  $a_PrestamoDetalle['PR'];}elseif($a_PrestamoDetalle['ME']>0){ $sumatoriatotalprestamo  +=  $a_PrestamoDetalle['ME'];}  echo "S/.&nbsp;".number_format((float)$a_PrestamoDetalle['PR'],2) ;?></td>
		              	</tr>
						
	              	<?php }
              	}?>
              	<?php 
              	}?>
				<tr>
              		<td width="10%" align="right">&nbsp;</td>
              		<td align="right" colspan="3"></td>
              		<td align="right" colspan="2"><B>T. Gatos--></B><B><?php echo "S/.&nbsp;".number_format((float)$sumatoriatotalgastos,2)?></B></td>
					<td></td>
              		<td align="right"><B><?php echo "S/.&nbsp;".number_format((float)$sumatoriaME,2)?></B></td>
              		<td align="right"><B><?php echo "S/.&nbsp;".number_format((float)$sumatoriatotalprestamonuevo,2)?></B></td>
              	</tr>
				<tr>
              		<td width="10%" align="right">&nbsp;</td>
              		<td align="right" colspan="4"></td>
              		<td align="right" colspan="1"></td>
              		<td colspan="3"  align="right"><B>T. Entrega&nbsp;= &nbsp;&nbsp; <?php echo "S/.&nbsp;".number_format((float)$sumatoriatotalprestamo,2)?></B></td>
              	</tr>
              	
				<?
              	 }
              	 if(is_array($a_sofa)){
              	 	if($a_sofa['s']){?>
              	 <tr>
	              	 <td width="10%" align="right">&nbsp;</td>
              		<td colspan="8"  align="right"><B>T. Sobrantes&nbsp;= &nbsp;&nbsp; <?php echo "S/.&nbsp;".number_format((float)$a_sofa['s'],2)?></td>
              	 </tr>
              	 <?
              	 	}
              	 	
              	 	if($a_sofa['f']){?>
              	 <tr>
	              	 <td width="10%" align="right">&nbsp;</td>
              		<td colspan="8"  align="right"><B>T. Faltante&nbsp;= &nbsp;&nbsp; <?php echo "S/.&nbsp;".number_format((float)$a_sofa['f'],2)?></td>
              	 </tr>
              	 <?
              	 	}
              	 }
				   $totalresumen = $a_inicioOperacion['IO'] + $SumatoriaTotalPagos + $sumatoriatotalgastos - $sumatoriatotalprestamo + $a_sofa['s'] - $a_sofa['f']; 
				?>
				<tr>
				  <td colspan="9">&nbsp;</td>
				</tr>
              	<tr>
				   <td colspan="9"  align="right" style="font-family:Verdana, Arial, Helvetica, sans-serif; font-weight:bold; color:#000000; font-size:12px; background: #E3E9E2"><B>Totales&nbsp;= </B> &nbsp;&nbsp; <?php echo "S/.&nbsp;".number_format((float)$totalresumen,2);?> </td>
				</tr>
            </table>
          </td>
        </tr>
      </table>