<?
//http://mixelaneo.blogspot.com/
if(!$quitarCab){
	header("Content-Type: application/xhtml+xml; charset=iso-8859-1");
	ViewResponse::getInstance()->setMethodAjax();
}
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();

//echo "<pre>";
//print_r($a_resumen_detalle);
//echo "</pre>";

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

/*
print_r($a_DepositosCobrador);
 */ 
?>
<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">
				<?php
				$SumatoriaTotalDepositoCobrador = 0;
              	if(is_array($a_DepositosCobrador)){?>
              	<tr>
              		<td colspan="8" style="background-image: url(/images/common/css/grid/fondoTit.gif);font-family:Verdana, Arial, Helvetica, sans-serif; font-weight:bold; color:#fff; font-size:12px;">Depositos del Colaborador</td>
              		
              	</tr>
              	<?php
              	
              		foreach ($a_DepositosCobrador as $keydp => $a_DepositosCob){
              			$SumatoriaTotalDepositoCobrador += $a_DepositosCob['total'];
						
						$o_sucursalmercadoBean = new SucursalMercadoBean();
						$o_sucursalmercadoBean->setIdMercado($keydp);
						$o_sucursalmercadoDP = new SucursalMercadoDP($o_sucursalmercadoBean);
						$a_o_sucursalmercadoDP = $o_sucursalmercadoDP->select();
						foreach ($a_o_sucursalmercadoDP as $keyM => $o_sucursalmercado){
							$IdSucursal = $o_sucursalmercado->getIdSucursal();
						}
						
						?>
              			<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_DepositosCob['NombreMercado']))?></td>
		              		<td colspan="6"  align="right"><a href="#" onclick="parent.objMainTab.modalDialog('<?php echo buildUrlComplet( 'op.php/arqueo/showDepositocobrador', '&idClase=DEPOSITOCOBRADOR&Idsucursal='.$IdSucursal.'&Idmercado='.$keydp.'&Idcolaborador='.$cboColaborador.'&fecha='.$txtFecha)?>',{width:950,height:400})" style='text-decoration:underline;color:#0E6633'><?php echo $a_DepositosCob['total'];?></label></a></td>
		              	</tr>
		              	<?php
              		}
              	
              	}
				?>
            	 <tr>
            		<td colspan="8" style="font-family:Verdana, Arial, Helvetica, sans-serif; font-weight:bold; color:#000000; font-size:12px; background: #E3E9E2">Pedido</td>
				</tr>
				
				<tr>
				    <td colspan="8">
				<?php
				/*
				$o_Cr_pedido_prestamoFN = new Cr_pedido_prestamoFN(); 
				$o_Cr_pedido_prestamoFN->setFechaTransaccion( $txtFecha );
				if($cboColaborador){
					$o_Cr_pedido_prestamoFN->setIdColaborador($cboColaborador); 
				}else{
					$o_Cr_pedido_prestamoFN->setIdColaborador(myUser::getUser()->getIdColaborador()); 	
				}
				$a_Cr_pedido_prestamo = $o_Cr_pedido_prestamoFN->select();
				*/
			    $o_Cr_pedido_prestamoFN = new Cr_pedido_prestamoFN(); 
				$o_Cr_pedido_prestamoFN->setEliminado('-1');
				$o_Cr_pedido_prestamoFN->setFechaTransaccion( $txtFecha );
				$o_Cr_pedido_prestamoFN->addCondition(Cr_pedido_prestamoFN::Estado ,"<>","'G'",false);
				if($cboColaborador){
					$o_Cr_pedido_prestamoFN->setIdColaborador($cboColaborador); 
				}else{
					$o_Cr_pedido_prestamoFN->setIdColaborador(myUser::getUser()->getIdColaborador()); 	
				}
				$a_Cr_pedido_prestamo = $o_Cr_pedido_prestamoFN->select();
									
				
				
				$o_Cr_pedido_prestamoFN = new Cr_pedido_prestamoFN(); 
				$o_Cr_pedido_prestamoFN->setEliminado('-1');
				$o_Cr_pedido_prestamoFN->setFechaEntrega( $txtFecha );
				$o_Cr_pedido_prestamoFN->addCondition(Cr_pedido_prestamoFN::Estado ,"=","'G'",false);
				if($cboColaborador){
					$o_Cr_pedido_prestamoFN->setIdColaborador($cboColaborador); 
				}else{
					$o_Cr_pedido_prestamoFN->setIdColaborador(myUser::getUser()->getIdColaborador()); 	
				}
				$a_Cr_pedido_prestamoPE = $o_Cr_pedido_prestamoFN->select();
				
				//print_r($a_Cr_pedido_prestamoPE);
				
				
				if(is_array($a_Cr_pedido_prestamoPE)){
					if(!is_array($a_Cr_pedido_prestamo)) $a_Cr_pedido_prestamo = array();
					$a_Cr_pedido_prestamo = array_merge($a_Cr_pedido_prestamo, $a_Cr_pedido_prestamoPE);
				}
				?>

        			    <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 Trx.</th>
                            <th width="5%" align="center">Fecha Entrega</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">Telef. Contacto</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 );
                              	
                              	//echo "<pre>"; 	print_r( $o_ClienteBean ); 	echo "</pre>";
                              	$f_MontoTotal = $f_MontoTotal + floatval( $o_objBean->getMonto() );
								$f_MontoTotalFinal = $f_MontoTotalFinal + floatval( $o_objBean->getMontoFinal() );
                              ?>
                              <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"><?php echo $o_objBean->getFechaEntrega() ?></td>
								<td align="center"><?php if($o_objBean->getEstado()=='P'){ echo "Pendiente"; }elseif($o_objBean->getEstado()=='A'){ echo "Aprobado"; }elseif($o_objBean->getEstado()=='G'){ echo "Generado"; } 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  $o_objBean->getTelefonoContacto() ?></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">&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="8" 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="8" align="right"><b><?php echo "S/.&nbsp;".number_format((float)$a_inicioOperacion['IO'],2);?></b></td>
	              	<?php	
	              	}
	              	?>
              	</tr>
              	<tr>
              		<td colspan="8" 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){
              	    //print_r($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="6"  align="right"><?php 
	              		$SumatoriaTotalPagos += $a_pagosTemp['PA'];
	              		//echo "Deposito S/.&nbsp;";
	              		?>
	           
	              		<?php
	              		echo "Normal   S/.&nbsp;";
	              		?>
	              		<a href="#" onclick="objMainTab.modalDialog('<?php echo buildUrlComplet( 'op.php/ajax/requestPdo', 's_TypeRequest=infopopuppagos&s_fecha='.$txtFecha.'&i_idColaborador='. $a_pagosTemp['IdColaborador']."&TipoPago=NORMAL")?>',{width:1140,height:400})" style='text-decoration:underline;color:#0000FF'><?php echo number_format($a_pagosTemp['PA'],2)?></a>

	              		<?php
	              		$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='. $a_pagosTemp['IdColaborador']."&TipoMora=2")?>',{width:1050,height:400})"><?php echo number_format($a_pagosTemp['PAM'],2)?></a>
	              		</td>
	              	</tr>	
              	<?php 
              	}
              	}
              	?>
              	<tr>
              		<td width="10%" align="right">&nbsp;</td>
              		<td colspan="7" align="right"><B>T. Pagos&nbsp;= &nbsp;&nbsp; <?php echo "S/.&nbsp;".number_format((float)$SumatoriaTotalPagos,2)?></B></td>
              	</tr>
              	<tr>
              		<td colspan="8" 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="7" style="font-family:Verdana, Arial, Helvetica, sans-serif; color:#000000; font-size:12px; background: #E3E9E2"><?php echo "Mercado&nbsp;".$key?></td>
	              		<td></td>
	              	</tr>
	              	<?php foreach ($a_prestamosTemp as $keydet => $a_PrestamoDetalle){
		              		if($a_PrestamoDetalle['OR']=='R'){
		              		   $origen='N';
		              		}elseif($a_PrestamoDetalle['OR']=='F'){
		              			$origen='R';
		              		}
	              		?>	
		              <tr>
		              		<td width="2%"><a title="Mostrar" style="cursor:pointer;text-decoration:underline;color:#00f;" onclick="parent.objMainTab.modalDialog('<?=buildUrlComplet('mnt.php/cli/show/id/'.$a_PrestamoDetalle['Idcliente']);?>','block=001')"><?php echo $a_PrestamoDetalle['Idcliente']?></a></td>
							<td width="2%"><a title="Mostrar Estado Cuenta" style="cursor:pointer;text-decoration:underline;color:#00f;" onclick="parent.objMainTab.modalDialog('<?=buildUrlComplet('mnt.php/pre/estado_cuenta/id/'.$keydet);?>',{title:'',width:990,height:630})"><?php echo $keydet;?></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:900,height:420})"><?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>&nbsp;&nbsp;&nbsp;&nbsp;<?php $sumatoriatotalgastos += $a_PrestamoDetalle['GA']; echo "S/.&nbsp;".number_format((float)$a_PrestamoDetalle['GA'],2)?></td>
		              		<td>&nbsp;&nbsp;<?php echo $a_PrestamoDetalle['PO']."%"?></td>
		              		<td  align="right"><?php if ($a_PrestamoDetalle['OR']=='R') { $sumatoriatotalprestamo  +=  $a_PrestamoDetalle['PR'];}  echo "S/.&nbsp;".number_format((float)$a_PrestamoDetalle['PR'],2) ;?></td>
		              	</tr>
						
	              	<?php }?>
					  
              	<?php 
              	}?><tr>
              		<td width="10%" align="right">&nbsp;</td>
              		<td colspan="7" align="right"><B>T. Gatos&nbsp;= &nbsp;&nbsp; <?php echo "S/.&nbsp;".number_format((float)$sumatoriatotalgastos,2)?>&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; <B>T. Prestamos&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="7" 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="7" 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']; //+ $SumatoriaTotalDepositoCobrador; 
				?>
				<tr>
				  <td colspan="8">&nbsp;</td>
				</tr>
              	<tr>
				   <td colspan="8"  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>