<?php use_helper( 'UrlFunctions' ); ?>
<?php use_helper( 'IncludePartial' ); ?>
<?php use_helper( 'UtilFunctions' ); ?>
<style>
.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;
    text-align: center;
}
.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;*/
}
.grid td div
{
    overflow: hidden; white-space: nowrap; font-size: 10px;
}
</style>
<?php if( $IdPersona ){ ?>
<!--<table cellspacing="0" cellpadding="0" border="0" align="center" width="99%" style="margin-bottom: 2px; margin-top: 2px;">
    <tr>
        <td align="right">
            <div id="s_ImagenLoad" style="display: none;"><img src="<?php echo Constantes::ROOT_DIR_APP ?>web/images/progress.gif" border="0"/></div>
        </td>
        <td align="right">
            <input type="button" class="button" value="Asociar" onclick="asociarDocumentoFactura()">
        </td>
    </tr>
</table>-->
<?php }else{ ?><br><?php } ?>
<div class="table-responsive">
    <table class="grid" cellspacing="0" cellpadding="0" border="0" align="center" width="99%" style="border-bottom: medium none;">
        <tbody>
            <tr>
                <th width="2%" align="center" style="border-top-left-radius: 5px;" nowrap> <input type="checkbox" id="all_checked"> </th>
                <th width="2%" align="center" nowrap>No</th>
                <th width="8%" align="center" nowrap>Almacen</th>
                <th width="15%" align="" nowrap>Proveedor</th>
                <th width="5%" align="center" nowrap>No OC</th>
                <th width="5%" align="center" nowrap>Clasificaci&oacute;n</th>
                
                <th width="8%" align="center" nowrap>Nro Documento</th>
                <th width="8%" align="center" nowrap>Fecha Doc</th>
                <th width="8%" align="center" nowrap>Monto Doc</th>
                <th width="8%" align="center" nowrap>Igv Doc</th>
                <th width="8%" align="center" nowrap>Total Doc</th>
                
                <th width="8%" align="center" nowrap>Doc Referencia</th>
                <th width="5%" align="center" nowrap>Estado</th>
                
                <th width="2%" align="center" nowrap>&nbsp;</th>
            </tr>
        </tbody>
        <tbody id="content_all_row">
            <?php foreach ( $a_ALM_OrdenGuiaFactura as $i_Key => $a_ALM_OrdenGuiaFacturaBN ){ ?>
                <?php
                $i_IdOrd = $a_ALM_OrdenGuiaFacturaBN['IdCompania']."_".$a_ALM_OrdenGuiaFacturaBN['IdOrdenCompra'];
                $i_Id = $a_ALM_OrdenGuiaFacturaBN['IdConcat'];
                ?>
            <tr class="content_row">
                <?php if( $a_ALM_OrdenGuiaFacturaBN['Estado'] == "P" ){ ?>
                <td align="center"> <?php if( $IdPersona ){ ?><input type="checkbox" value="<?=$a_ALM_OrdenGuiaFacturaBN['IdConcat']?>" ><?php } ?> </td>
                <?php }else{ ?><td align="center" >&nbsp;</td><?php } ?>
                
                <td align="center" class=""><label class="labelData"><?=$i_Key+1?></label></td>
                <td align="center" class=""><label class="labelData"><?=$a_ALM_OrdenGuiaFacturaBN['SucursalDesc']?></label></td>
                <td align="" class="">
                    <div class="labelData" title="<?=$a_ALM_OrdenGuiaFacturaBN['ProveedorDes']?>">
                        <?=$a_ALM_OrdenGuiaFacturaBN['ProveedorDes']?>
                    </div>
                </td>
                <td align="center" class="">
                    <?php if( $a_ALM_OrdenGuiaFacturaBN['IdOrdenCompra'] ){ ?>
                    <label class="labelData" style="color:blue;text-decoration:underline;cursor:pointer;" onclick="showViewOrd('<?=$i_IdOrd?>');"><?=$a_ALM_OrdenGuiaFacturaBN['IdOrdenCompra']?></label>
                    <?php }else{ ?>
                    -
                    <?php } ?>
                </td>
    
                <td align="center" class="" nowrap="nowrap">
                    <?php if( $a_ALM_OrdenGuiaFacturaBN['OrdenClasificacion'] ){ ?>
                    <label class="labelData"><?=$a_ALM_OrdenGuiaFacturaBN['OrdenClasificacion']?></label>
                    <?php }else{ ?>
                    <label class="labelData"><?=VTA_TipoDocumentoFN::getObjectVTATipoDocumento( $a_ALM_OrdenGuiaFacturaBN['TipoDoc'] )->getDescripcionLocal()?></label>
                    <?php } ?>
                </td>
                
                <td align="center" class="" rowspan="<?=$a_ALM_OrdenGuiaFacturaBN['SerieFacturaConcat']?>"  nowrap><label class="labelData"><?=$a_ALM_OrdenGuiaFacturaBN['SerieFacturaConcat']?></label></td>
                <td align="center" class="" nowrap="nowrap"><label class="labelData"><?=$a_ALM_OrdenGuiaFacturaBN['FechaFactura']?></label></td>
                <td align="center" class="" rowspan=""><label class="labelData"><?=number_format( $a_ALM_OrdenGuiaFacturaBN['MontoFactura'], 2 )?></label></td>
                <td align="center" class="" rowspan=""><label class="labelData"><?=number_format( $a_ALM_OrdenGuiaFacturaBN['IGVFactura'], 2 )?></label></td>
                <td align="center" class="" rowspan=""><label class="labelData"><?=number_format( $a_ALM_OrdenGuiaFacturaBN['TotalFactura'], 2 )?></label></td>
                
                <td align="center" class="" nowrap="nowrap">
                    <?php if( $a_ALM_OrdenGuiaFacturaBN['TipoDoc'] == "NC" ){ ?>
                    <label class="labelData" style="color:blue;text-decoration:underline;cursor:pointer;" onclick="editFactura('<?=$a_ALM_OrdenGuiaFacturaBN['IdOrdenGuiaReferencia']?>');">
                        <?=$a_ALM_OrdenGuiaFacturaBN['DocRefxNCR']?>
                    </label>
                    <?php }else{ ?>
                    <label class="labelData" style="color:blue;text-decoration:underline;cursor:pointer;" onclick="showView('<?=$i_Id?>');">
                        GUI <?=$a_ALM_OrdenGuiaFacturaBN['SerieGuiaConcat']?>
                    </label>
                    <?php } ?>
                </td>
                <td align="center" class="" rowspan="<?=$a_ALM_OrdenGuiaFacturaBN['SerieFacturaConcat']?>" nowrap><label class="labelData"><?=$a_ALM_OrdenGuiaFacturaBN['EstadoDesc']?></label></td>
                
                <td align="center" rowspan="<?=$a_ALM_OrdenGuiaFacturaBN['SerieFacturaConcat']?>" nowrap>
                    <table cellpadding="0" cellspacing="0" border="0">
                        <tr>
                            <td>
                                <?php if( $a_ALM_OrdenGuiaFacturaBN['OrdenClasificacion'] != 'O/S - SERVICIO' ){ ?>
                                <span style='cursor:pointer; margin: 0 auto;' class='ui-icon ui-icon-document' title='Desasociar Guias' 
                                    onclick="showViewTrx('<?=$a_ALM_OrdenGuiaFacturaBN['IdCompania']?>_<?=$a_ALM_OrdenGuiaFacturaBN['IdTipoDocumentoRef']?>_<?=$a_ALM_OrdenGuiaFacturaBN['IdNumeroDocumentoRef']?>' )">
                                </span>
                                <?php } ?>
                            </td>
                            <!-- <td><span style='cursor:pointer; margin: 0 auto;' class='ui-icon ui-icon-circle-minus' title='Desasociar Guias' onclick="desasociarDocumentoFactura( '<?=$a_ALM_OrdenGuiaFacturaBN['SerieFacturaConcat']?>' )"></span></td> -->
                            <td><span style='cursor:pointer; margin: 0 auto;' class='ui-icon ui-icon-pencil' title='Editar Factura' onclick="editFactura( '<?=$i_Id?>','<?=$a_ALM_OrdenGuiaFacturaBN['SerieFacturaConcat']?>' );"></span></td>
                        </tr>
                    </table>
                </td>
                
            </tr>
            <?php } ?>
        </tbody>
        <!-- <tr style="background: #0078ae url(/pc/web/css/jquery/themes/start/images/ui-bg_glass_45_0078ae_1x400.png) repeat-x scroll 50% 50%;"> -->
        <?php if( $a_ALM_OrdenGuiaFactura ){ ?>
        <tbody>
            <tr>
                <th align="center" style="border-bottom-left-radius: 5px; height: 24px; border-bottom-right-radius: 5px;" colspan="14"> &nbsp; </th>
            </tr>
        </tbody>
        <?php } ?>
    </table>
</div>

<script type="text/javascript">
asociarDocumentoFactura = function()
{
    var checkboxes = $( "#content_all_row" ).find( 'input:checked' );
    var url_param  = "";
    if( checkboxes.length > 0 )
    {
        var a_values = [];
        $.each
        ( 
            checkboxes, 
            function( index, o_elm ) 
            {
                a_values.push( o_elm.value );
            }
        );
        url_param = a_values.join( "," );
    }
    else
    {
        alert( "Debe de seleccionar por lo menos un registro." );
        return false;
    }
    
    $( '#myIframe' ).attr( 'src', '<?=buildUrl( 'asoguias/asociarFact', 'IdDet=' )?>' + url_param );
    $( '#dialog' ).dialog( 'open' );
}
desasociarDocumentoFactura = function( serie_num_fact )
{
    if( serie_num_fact == "" )
    {
        alert( "La Serie y Numero esta vacia." );
    }
    $( '#myIframe2' ).attr( 'src', '<?=buildUrl( 'asoguias/desasociarFact', 'SerieNumFact=' )?>' + serie_num_fact );
    $( '#dialog2' ).dialog( 'open' );
}
</script>
<script>
$( "#all_checked" )
.click
(
    function()
    {
        var checkboxes = $( "#content_all_row" ).find( ':checkbox' );
        if( $( "#all_checked" ).is( ':checked' ) ) 
        {
            checkboxes.prop( 'checked', true );
        } 
        else 
        {
            checkboxes.prop( 'checked', false );
        }
    }
);
$( ".row" )
.click
(
    function( elm_sel )
    {
        if( $( $( $( elm_sel.target ).closest( 'tr' ) ).find( 'input:checkbox' ) ).is( ":checked" ) )
        {
            $( $( $( elm_sel.target ).closest( 'tr' ) ).find( 'input:checkbox' ) ).prop( 'checked', false );
        }
        else
        {
            $( $( $( elm_sel.target ).closest( 'tr' ) ).find( 'input:checkbox' ) ).prop( 'checked', true );
        }
    }
);
</script>

<script>
showViewTrx = function( i_Id )
{
    $( '#myIframe' ).attr( 'src', '<?=buildUrl( 'transacciones/viewdel', 'block=001&s_TipoProc=shw&i_Id=' )?>' + i_Id );
    $( '#dialog' ).dialog( 'open' );
}
showView = function( i_Id )
{
    //$( '#myIframe3' ).attr( 'src', '<?=buildUrlComplet( 'alm.php/oc/detail', 'noedit=1&action=oc_viewdel&s_TipoProc=upd&i_Id=' )?>' + i_Id );
    $( '#myIframe3' ).attr( 'src', '<?=buildUrl( 'asoguias/viewTrans', 'IdConcat=' )?>' + i_Id );
    $( '#dialog3' ).dialog( 'open' );
}
showViewOrd = function( i_Id )
{
    $( '#myIframe3' ).attr( 'src', '<?=buildUrl( 'oc/viewdel', 's_TipoProc=shw&i_Id=' )?>' + i_Id );
    $( '#dialog3' ).dialog( 'open' );
}
editFactura = function( IdConcat, s_SerieFact )
{
    $( '#myIframe' ).attr( 'src', '<?=buildUrl( 'asoguias/editFact', 'IdConcat=' )?>' + IdConcat + '/SerieFact/' + s_SerieFact );
    $( '#dialog' ).dialog( 'open' );
}
</script>