<?php use_helper( 'UrlFunctions' ); ?>
<?php use_helper( 'UtilFunctions' ); ?>
<?php use_helper( 'IncludePartial' ); ?>

<?php if( $b_NoExpExcel ){ ?>
<style>
label
{
    margin-right: 0;
}
.grid
{
    border: 1px solid #77d5f7;
    border-top-left-radius: 5px;
    border-top-right-radius: 5px;
    border-bottom-right-radius: 5px;
    border-bottom-left-radius: 5px;
}
.grid th 
{
	border-color: #77d5f7;
    background-color: #0a7db1;
    font-weight: normal;
    font-size: 11px;
    font-family: Verdana,Arial,sans-serif;
    height: 24px;
    padding: 0 2px;
}
.grid td 
{
    border-left: 0 none;
    border-right-color: #77d5f7;
    border-right-style: solid;
    border-right-width: 1px;
    border-bottom-color: #77d5f7;
    border-bottom-style: solid;
    border-bottom-width: 1px;
    padding: 0 2px;
    /*white-space: pre;*/
    height: 22px;
    /*border: 1px solid #77d5f7;*/
}
.tooltip_transf
{
    color: blue;
    text-decoration: underline;
}
</style>
<?php } ?>
<table class="grid" cellspacing="0" cellpadding="0" border="0" align="center" width="99%" style="border-bottom: medium none; margin-top: 10px; table-layout: fixed;">
    <tbody>
        <tr>
            <th width="1%" align="center" style="border-top-left-radius: 5px;" >No</th>
            <th width="5%" align="center" >Fecha</th>
            <th width="8%" align="center" >Item</th>
            <th width="15%" align="center" >Descripci&oacute;n</th>
            <th width="5%" align="center" >Transacci&oacute;n</th>
            <th width="10%" align="center" >Comprobante</th>
            <th width="15%" align="center" >Persona</th>
            <th width="5%" align="center" >Cantidad</th>
            <th width="5%" align="center" >Cantidad Acumulado</th>
            <th width="5%" align="center" >Lote</th>
            <th width="5%" align="center" >Almacen</th>
            <th width="4%" align="center" >Usuario</th>
            <th width="1%">&nbsp;</th>
        </tr>
    </tbody>
    <tbody id="content_all_row">
        <?php
        $i_CantidadShow = 0;
        $i_Acumulado = 0;
        $i_SaldoInicia = 0;
        $a_SaldoInicialItem = array();
        ?>
        <?php foreach ( $a_ALM_Kardex as $i_Key => $o_ALM_KardexBN ){ ?>
            <?php
            $i_IdCompania = $o_ALM_KardexBN->getIdCompania();
            
            $s_ToolTip1 = $o_ALM_KardexBN->getReferenciaTipoDocumentoDesc();
            
            $s_IdItem    = $o_ALM_KardexBN->getIdItem();
            $s_IdAlmacen = $o_ALM_KardexBN->getIdAlmacen();
            $s_IdLote    = $o_ALM_KardexBN->getIdLote();
            $s_IdFecha   = $o_ALM_KardexBN->getIdFecha();
            $i_Cantidad  = floatval( $o_ALM_KardexBN->getCantidad() );
            
            $s_Periodo = substr( str_replace( "-", "", $s_IdFecha ), 0, 6 );
            
            $s_PeriodoAnt = substr( $s_Periodo, 0, 4 ).str_pad( ( intval( substr( $s_Periodo, -2 ) ) - 1 ), 2, "0", STR_PAD_LEFT );
            $s_PeriodoSig = substr( $s_Periodo, 0, 4 ).str_pad( ( intval( substr( $s_Periodo, -2 ) ) + 1 ), 2, "0", STR_PAD_LEFT );
            
            $s_PeriodoAntShow = substr( $s_PeriodoAnt, 0, 4 )."-".substr( $s_PeriodoAnt, -2 );
            $s_PeriodoShow = substr( $s_Periodo, 0, 4 )."-".substr( $s_Periodo, -2 );
            
            $s_Key = $s_IdItem."_".$s_IdLote."_".$s_PeriodoAnt;
            $b_CtrlSaldoInicial = false;
            
            if( !isset( $a_SaldoInicialItem[$s_Key] ) )
            {
                $a_SaldoInicialItem[$s_Key] = ALM_KardexFN::buscarStockSaldo( $s_IdAlmacen, $s_IdLote, $s_IdItem, $s_PeriodoAnt );
                $i_SaldoInicia = $a_SaldoInicialItem[$s_Key];
                //AHORA SE RECUPERA LOS TRANSACCIONES ANTERIORES PARA RESTARLO
                $s_IdFechaIni = $s_PeriodoShow."-01";
                $s_IdFechaFin = $s_FechaInicio;
                $a_LstTransKardex = ALM_KardexFN::recuperarTransxRango( $i_IdCompania, $s_IdAlmacen, $s_IdLote, $s_IdItem, $s_IdFechaIni, $s_IdFechaFin );
                foreach ( $a_LstTransKardex as $o_ALM_KardexBN_Trx )
                {
                    $i_Cantidad_Trx  = floatval( $o_ALM_KardexBN_Trx->getCantidad() );
                    if( $o_ALM_KardexBN_Trx->getTipoOperacion() == ALM_TransaccionCabeceraBN::TIPO_INGRESO )
                    {
                        $i_SaldoInicia = $i_SaldoInicia + $i_Cantidad_Trx;
                    }
                    else
                    {
                        $i_SaldoInicia = $i_SaldoInicia - $i_Cantidad_Trx;
                    }
                }
                $b_CtrlSaldoInicial = true;
                $i_Acumulado = $i_SaldoInicia;
            }
            
            if( $o_ALM_KardexBN->getTipoOperacion() == ALM_TransaccionCabeceraBN::TIPO_INGRESO )
            {
                $i_CantidadShow = $i_Cantidad;
                $i_Acumulado = $i_Acumulado + $i_Cantidad;
            }
            else
            {
                $i_CantidadShow = $i_Cantidad * -1;
                $i_Acumulado = $i_Acumulado - $i_Cantidad;
            }
            //POR AHORA SOLO PARA TRANSFERENCIA - MOSTRAR INFO
            $s_ClassTooltipTransf = "";
            if( $o_ALM_KardexBN->getReferenciaTipoDocumento() == "NT" )
            {
                $s_ClassTooltipTransf = "tooltip_transf";
            }
            //ID POPUP
            $s_IdPopup = $o_ALM_KardexBN->getIdCompania()."_".$o_ALM_KardexBN->getReferenciaTipoDocumento()."_".$o_ALM_KardexBN->getReferenciaNumeroDocumento();
            ?>
            <?php if( $b_CtrlSaldoInicial ){ ?>
        <tr class="content_row">
            <td align="center" class="row">&nbsp;</td>
            <td align="center" class="row"><label class="labelData"><b><?=$s_PeriodoAntShow?></b></label></td>
            <td align="center" class="row">&nbsp;</td>
            <td align="center" class="row"><label class="labelData"><b>Saldo Inicial</b></label></td>
            <td align="center" class="row">&nbsp;</td>
            <td align="center" class="row">&nbsp;</td>
            <td align="center" class="row">&nbsp;</td>
            <td align="center" class="row"><label class="labelData"><b><?=$i_SaldoInicia?></b></label></td>
            <td align="center" class="row"><label class="labelData"><b><?=$i_SaldoInicia?></b></label></td>
            <td align="center" class="row"><label class="labelData"><b><?=$s_IdLote?></b></label></td>
            <td align="center" class="row">&nbsp;</td>
            <td align="center" class="row">&nbsp;</td>
            <?php if( $b_NoExpExcel ){ ?>
            <td align="center" class="row">&nbsp;</td>
            <?php } ?>
        </tr>
            <?php } ?>
        <tr class="content_row">
            <td align="center" class="row"><label class="labelData"><?=$i_Key+1?></label></td>
            <td align="center" class="row"><label class="labelData"><?=$o_ALM_KardexBN->getFechaTransaccion()?></label></td>
            <td align="center" class="row"><label class="labelData"><?=$o_ALM_KardexBN->getIdItem()?></label></td>
            <td align="left" class="row">
                <div class="labelData" style="overflow: hidden; white-space: nowrap;" title="<?=$o_ALM_KardexBN->getDescripcionItem()?>">
                    <?=$o_ALM_KardexBN->getDescripcionItem()?>
                </div>
            </td>
            <td align="center" class="row tooltip_tipo" title="<?=$s_ToolTip1?>"><label class="labelData"><?=$o_ALM_KardexBN->getReferenciaTipoDocumento()?></label></td>
            <td align="center" class="row" onmouseout='$( "#dialog_tooltip ").dialog( "close" );'>
                <label class="labelData <?=$s_ClassTooltipTransf?>"
                    tipodoc="<?=$o_ALM_KardexBN->getReferenciaTipoDocumento()?>" 
                    numerodoc="<?=$o_ALM_KardexBN->getReferenciaNumeroDocumento()?>"
                    iditem="<?=$o_ALM_KardexBN->getIdItem()?>">
                    <?=$o_ALM_KardexBN->getDocumentoRefFiscal()?>
                </label>
            </td>
            <td align="center" class="row">
                <div class="labelData" style="overflow: hidden; white-space: nowrap;" title="<?=$o_ALM_KardexBN->getPerProvClieDescripcion()?>">
                    <?=$o_ALM_KardexBN->getPerProvClieDescripcion()?>
                </div>
            </td>
            <td align="center" class="row"><label class="labelData"><?=$i_CantidadShow?></label></td>
            <td align="center" class="row"><label class="labelData"><?=$i_Acumulado?></label></td>
            <td align="center" class="row"><label class="labelData"><?=$o_ALM_KardexBN->getIdLote()?></label></td>
            <td align="center" class="row">
                <div class="labelData" style="overflow: hidden; white-space: nowrap;" title="<?=$o_ALM_KardexBN->getDescripcionAlmacen()?>"><?=$o_ALM_KardexBN->getDescripcionAlmacen()?></div>
            </td>
            <td align="center" class="row">
                <div class="labelData" style="overflow: hidden;" title="<?=$o_ALM_KardexBN->getUsuarioCreacion()?>"><?=$o_ALM_KardexBN->getUsuarioCreacion()?></div>
            </td>
            <?php if( $b_NoExpExcel ){ ?>
            <td align="center" class="row">
                <?php if( $s_ClassTooltipTransf != "" ){ ?>
                <span onclick="showView( '<?=$s_IdPopup?>' );" title="Visualizar Detalle Completo" class="ui-icon ui-icon-document" style="cursor:pointer; margin: 0 auto;"></span>
                <?php }else{ ?>&nbsp;<?php } ?> 
            </td>
            <?php } ?>
        </tr>
        <?php } ?>
    </tbody>
</table>
<?php if( $b_NoExpExcel ){ ?>
<div id="dialog_tooltip"></div>

<script>
$( "#dialog_tooltip" )
.dialog
(
    {
    	resizable: false,
        autoOpen : false,
        //show     : "blind",
        //hide     : "explode",
        title    : "", 
        width    : 500, 
        height   : 250,
        closeOnEscape : true
    }
);
/*
$( ".tooltip_transf" )
.hover
(
    function () 
    {
        console.log( "function1" );
        $( "#dialog_tooltip" ).dialog('open');
    }, 
    function () 
    {
    	console.log( "function2" );
        $( "#dialog_tooltip" ).dialog( 'close' );
    }
)
.mousemove
(
    function( e ) 
    {
    	console.log( "mousemove" );
    	$( "#dialog_tooltip" ).dialog( "option", { position: [e.pageX+5, e.pageY+5] }); 
    }
);
*/

$( ".tooltip_transf" )
.bind
(
    "mouseover", 
    function ( o_Object ) 
    {
        var tipodoc   = $( o_Object.target ).attr( "tipodoc" );
        var numerodoc = $( o_Object.target ).attr( "numerodoc" );
        var iditem    = $( o_Object.target ).attr( "iditem" );
    	//console.log( "mouseover" );
    	//console.log( tipodoc );
    	//console.log( numerodoc );
    	//console.log( iditem );

    	$.get
        (
            "<?=buildUrl( 'rep/showTrans', '' )?>" + "Tipo/" + tipodoc + "/Numero/" + numerodoc + "/IdItem/" + iditem, 
            function( data ) 
            {
                $( "#dialog_tooltip" ).html( data );
                $( "#dialog_tooltip" ).dialog( 'open' ); // open
                
            }
        );
    }
);

$( ".tooltip_transf" )
.bind
(
    "mouseleave", 
    function () 
    {
    	console.log( "mouseleave" );
        $( "#dialog_tooltip ").dialog( "close" ); // open
    }
);

$( document ).mousemove
(
    function ( e ) 
    {
        $( "#dialog_tooltip" ).dialog( "option", { position: [e.pageX+5, e.pageY+5] } );
    }
);

</script>

<script>
$( 
    function() 
    {
        $( ".tooltip_tipo" ).tooltip
        (
            {
                position: 
                {
                    my: "center bottom-20",
                    at: "center top",
                    using: function( position, feedback ) 
                    {
                        $( this ).css( position );
                        $( "<div>" )
                            .addClass( "arrow" )
                            .addClass( feedback.vertical )
                            .addClass( feedback.horizontal )
                            .appendTo( this );
                    }
                }
            }
        );
    }
);
</script>
<script>
//DETECTA EL ESCAPE
$( document ).bind
(
    'keyup', 
    function( e ) 
    {
        if( e.which === 27 ) //ESCAPE
        {
            $( "#dialog_tooltip ").dialog( "close" ); // open
            $( "#dialog ").dialog( "close" ); // open
        }
    }
);
</script>
<style>
  .ui-tooltip, .arrow:after {
    background: black;
    border: 2px solid white;
  }
  .ui-tooltip {
    padding: 10px 20px;
    color: white;
    border-radius: 20px;
    font: bold 14px "Helvetica Neue", Sans-Serif;
    text-transform: uppercase;
    box-shadow: 0 0 7px black;
  }
  .arrow {
    width: 70px;
    height: 16px;
    overflow: hidden;
    position: absolute;
    left: 50%;
    margin-left: -35px;
    bottom: -16px;
  }
  .arrow.top {
    top: -16px;
    bottom: auto;
  }
  .arrow.left {
    left: 20%;
  }
  .arrow:after {
    content: "";
    position: absolute;
    left: 20px;
    top: -20px;
    width: 25px;
    height: 25px;
    box-shadow: 6px 5px 9px -9px black;
    -webkit-transform: rotate(45deg);
    -ms-transform: rotate(45deg);
    transform: rotate(45deg);
  }
  .arrow.top:after {
    bottom: -20px;
    top: auto;
  }
</style>

<?=crearFuentePopupIframe( array( "div_id"=>"dialog", "iframe_id"=>"myIframe", "popup_title"=>"Resumen", "popup_width"=>"870", "popup_height"=>"420" ) )?>
<script>
showView = function( i_Id )
{
    $( '#myIframe' ).attr( 'src', '<?=buildUrlComplet( 'alm.php/transacciones/viewdel', 'block=001&s_TipoProc=shw&i_Id=' )?>' + i_Id );
    $( '#dialog' ).dialog( 'open' );
}
</script>
<?php } ?>