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

<link rel="stylesheet" type="text/css" media="screen" href="https://cdn.datatables.net/responsive/2.2.5/css/responsive.bootstrap4.min.css" />
<link rel="stylesheet" type="text/css" media="screen" href="https://cdn.datatables.net/1.10.21/css/dataTables.bootstrap4.min.css" />
<link rel="stylesheet" type="text/css" media="screen" href="https://cdn.datatables.net/fixedheader/3.1.7/css/fixedHeader.dataTables.min.css" />

<!--<link rel="stylesheet" type="text/css" media="screen" href="https://cdn.datatables.net/1.10.21/css/jquery.dataTables.min.css" />-->

<script type="text/javascript" src="https://cdn.datatables.net/1.10.21/js/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/1.10.21/js/dataTables.bootstrap4.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/responsive/2.2.5/js/dataTables.responsive.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/responsive/2.2.5/js/responsive.bootstrap4.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/fixedheader/3.1.7/js/dataTables.fixedHeader.min.js"></script>

<style type="text/css">
.ui-dialog .ui-dialog-titlebar-close 
{
    position: absolute;
    right: .3em;
    top: 50%;
    width: 20px;
    margin: -10px 0 0 0;
    padding: 1px;
    height: 20px;
    background-position: -96px -128px;
    /*background-image: url("images/ui-icons_056b93_256x240.png");*/
    background-image: url("<?=Constantes::ROOT_DIR_APP?>web/css/jquery/themes/start_1.12.1/images/ui-icons_056b93_256x240.png");
}
</style>

<style type="text/css">
/*
#example_wrapper .row
{
    margin-right: 0;
    margin-left: 0;
}
table.dataTable tbody tr:hover 
{
   background-color: #DAE6FE !important;;
}
*/
.table td, .table th 
{
    /*padding: 8px;*/
    font-size: 13px;
}
th { white-space: nowrap; }

</style>

<div class="titleBody descripProy">
    <ul>
        <li>
            Recepci&oacute;n Ordenes de Compra
        </li>
    </ul>
</div>

<div class="marco">
    <?php 
    include_partial
    ( 
        "infohead", 
        array( 
                "o_COM_OrdenCompraCabeceraBN"    => $o_COM_OrdenCompraCabeceraBN,
                "o_GSS_CompaniaBN"               => $o_GSS_CompaniaBN,
                "o_COM_OrdenItemClasificacionBN" => $o_COM_OrdenItemClasificacionBN,
                "o_GSS_MonedaMastBN"             => $o_GSS_MonedaMastBN,
                "o_ALM_AlmacenMastBN"            => $o_ALM_AlmacenMastBN,
             ),
        array( "Module"=>"oc" )
     ); 
    ?>
</div>

<?php if( $o_COM_OrdenCompraCabeceraBN->getEstado() == COM_OrdenCompraCabeceraBN::ORDEN_COMPRA_APROBADO ){ ?>
<div class="mt-1 marco" style="" id="content_buttons">
    
    <button type="button" class="btn btn-primary btn-sm mt-1" onclick="completarParmetros();">Recepcionar lo Seleccionado</button>
    <button type="button" class="btn btn-primary btn-sm mt-1" onclick="actualizarEstadoCompletado();">Cerrar Orden de Compra</button>
    <button type="button" class="btn btn-primary btn-sm mt-1" onclick="window.open( '<?=buildUrl( 'oc/reporte', "i_Id=".$i_Id )?>', '_blank' );">Imprimir Orden de Compra</button>
    
    <?php if( myUser::getUser()->hasGlobalAccess() ){ ?>
    <button type="button" class="btn btn-primary btn-sm mt-1" onclick="abrirPopupAgregarDetalles();">Agregar Det</button>
    <button type="button" class="btn btn-primary btn-sm mt-1" onclick="location.reload();">Recargar Detalle</button>
    <!-- <input type="button" class="button" value="Asociar NC" onclick="abrirPopupAsociarNC();"> -->
    <?php } ?>
</div>
<?php } ?>

<div class="table-responsive">
    <!-- <table class="table table-striped table-bordered" style="width:100%" id="example" > -->
    <table class="table table-sm table-bordered table-hover" id="example">
        <thead id="">
            <tr class="table-primary">
                <th width="1%"  align="center" ><input type="checkbox" id="all_checked"></th>
                <th width="1%"  align="center" ><b>No</b></th>
                <th width="8%" align="center" ><b>C&oacute;digo</b></th>
                <th width="8%" align="center" ><b>Cant.<br>Pedida</b></th>
                <th width="8%" align="center" ><b>U.Medida</b></th>
                <th width="8%" align="center" ><b>Marca/Labo</b></th>
                <th width="35%" align=""       ><b>Descripci&oacute;n</b></th>
                <th width="8%" align="center" ><b>Precio</b></th>
                <th width="8%" align="center" ><b>Total</b></th>
                <th width="8%" align="center" ><b>Cant.<br>Recibida</b></th>
                <th width="8%" align="center" ><b>Pend.<br>Recibir</b></th>
                <th width="5%"  align="center" >&nbsp;</th>
            </tr>
        </thead>
        
        <tbody id="content_all_row">
            <?php $f_MontoTotal = 0.00 ?>
            <?php foreach ( $a_COM_OrdenCompraDetalle as $i_Key => $o_COM_OrdenCompraDetalleBN ){ ?>
                <?php
                $f_CanidadConv = $o_COM_OrdenCompraDetalleBN->getCantidadPedida();
                /*$o_MST_UnidadConversionMastFN = new MST_UnidadConversionMastFN();
                $o_MST_UnidadConversionMastBN = $o_MST_UnidadConversionMastFN->selectByPK( $o_COM_OrdenCompraDetalleBN->getObjectUnidad()->getIdUnidad(), 'UNI' );
                if( $o_MST_UnidadConversionMastBN )
                {
                    $f_CanidadConv = ( $o_COM_OrdenCompraDetalleBN->getCantidadPedida() * $o_MST_UnidadConversionMastBN->getCantidadEquivalente() );
                }*/
                ?>
            <tr class="content_row">
                <td align="center">
                    <?php if( $o_COM_OrdenCompraCabeceraBN->getEstado() == COM_OrdenCompraCabeceraBN::ORDEN_COMPRA_APROBADO ){ ?>
                        <?php if( $o_COM_OrdenCompraDetalleBN->getCantidadPedida() != $o_COM_OrdenCompraDetalleBN->getCantidadRecibida() ){ ?>
                    <input type="checkbox" name="" id="" value="<?=$o_COM_OrdenCompraDetalleBN->getIdSecuencia()?>">
                        <?php } ?>
                    <?php } ?>
                </td>
                <td  align="center"><?=( $i_Key + 1 )?></td>
                <td  align="center" style="cursor:pointer; color:#2743B2; text-decoration:underline;" onclick="buscarResumenStock( $( this ).html() );"><?=$o_COM_OrdenCompraDetalleBN->getIdItem()?></td>
                <td  align="center"><?=$o_COM_OrdenCompraDetalleBN->getCantidadPedida()?></td>
                <td  align="center"><?=$o_COM_OrdenCompraDetalleBN->getObjectUnidad()->getDescripcionCorta()?></td>
                <td  align="center" title=""><?=$o_COM_OrdenCompraDetalleBN->getMarcaDesc()?></td>
                <td  align="" id=""><?=$o_COM_OrdenCompraDetalleBN->getDescripcion()?></td>
                <td  align="center"><?=number_format( $o_COM_OrdenCompraDetalleBN->getPrecioUnitarioIGV(), 2 )?></td>
                <td  align="center"><?=number_format( $o_COM_OrdenCompraDetalleBN->getPrecioTotalConIGV(), 2 )?></td>
                <td  align="center"><?=number_format( $o_COM_OrdenCompraDetalleBN->getCantidadRecibida(), 2 )?></td>
                <td  align="center"><?=number_format( ( $f_CanidadConv - $o_COM_OrdenCompraDetalleBN->getCantidadRecibida() ), 2 )?></td>
                <td align="center">
                    <?php if( $o_COM_OrdenCompraCabeceraBN->getEstado() == COM_OrdenCompraCabeceraBN::ORDEN_COMPRA_APROBADO ){ ?>
                        <?php if( $o_COM_OrdenCompraDetalleBN->getCantidadPedida() != $o_COM_OrdenCompraDetalleBN->getCantidadRecibida() ){ ?>
                    <input type="button" class="button" value="Recepcionar" onclick="completarParmetro( this );">
                        <?php } ?>
                    <?php } ?>
                </td>
            </tr>
                <?php $f_MontoTotal = ( $f_MontoTotal + floatval( $o_COM_OrdenCompraDetalleBN->getSubTotal() ) ) ?>
            <?php } ?>
        </tbody>
        <tbody>
            <tr>
                <td colspan="7">&nbsp;</td>
                <td align="center">Sub Total <?=GSS_MonedaMastFN::getSiglaxId( $o_COM_OrdenCompraCabeceraBN->getIdMoneda() )?></td>
                <td align="center"><?=number_format( ( $o_COM_OrdenCompraCabeceraBN->getMontoAfecto() + $o_COM_OrdenCompraCabeceraBN->getMontoNoAfecto() ), 2 )?></td>
                <td align="center">&nbsp;</td>
                <td align="center">&nbsp;</td>
                <td align="center">&nbsp;</td>
            </tr>
            <tr>
                <td colspan="7">&nbsp;</td>
                <td align="center">IGV <?=GSS_ParametroMastFN::getTextoIgvActual()?> <?=GSS_MonedaMastFN::getSiglaxId( $o_COM_OrdenCompraCabeceraBN->getIdMoneda() )?></td>
                <td align="center"><?=number_format( $o_COM_OrdenCompraCabeceraBN->getMontoIGV(), 2 )?></td>
                <td align="center">&nbsp;</td>
                <td align="center">&nbsp;</td>
                <td align="center">&nbsp;</td>
            </tr>
            <tr>
                <td colspan="7">&nbsp;</td>
                <td align="center">Total <?=GSS_MonedaMastFN::getSiglaxId( $o_COM_OrdenCompraCabeceraBN->getIdMoneda() )?></td>
                <td align="center"><?=number_format( ( $o_COM_OrdenCompraCabeceraBN->getMontoBruto() ), 2 )?></td>
                <td align="center">&nbsp;</td>
                <td align="center">&nbsp;</td>
                <td align="center">&nbsp;</td>
            </tr>
        </tbody>
    </table>
</div>

<div class="dropdown-divider"></div>

<table cellpadding="0" cellspacing="0" width="100%" border="0" style="">
    <tr>
        <td align="center" width="25%">&nbsp;</td>
        <td align="center" width="50%">
            <button type="button" class="btn btn-primary btn-lg btn-block" onclick="location.href='<?php echo buildUrl( 'recepcion/list' ) ?>'">Salir</button>
        </td>
        <td align="center" width="25%">&nbsp;</td>
    </tr>
</table>
                                        
<?=crearPopupResumenStock( array( "name_function"=>"buscarResumenStock" ) )?>

<?=crearFuentePopupIframe( array( "div_id"=>"dialog", "iframe_id"=>"myIframe", "popup_title"=>"", "modal"=>true ) )?>
<?=crearFuentePopupIframe( array( "div_id"=>"dialog2", "iframe_id"=>"myIframe2", "popup_title"=>"", "modal"=>true ) )?>

<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 type="text/javascript">
configurarDataTable = function()
{
    $( '#example' ).DataTable
    (
        {
            language:       { "url": "//cdn.datatables.net/plug-ins/9dcbecd42ad/i18n/Spanish.json" },
            responsive:     false,
            searching:      false,
            info:           false,
            lengthChange:   false,
            paging:         false,
            dom:            'lrtip',
            fixedHeader: 
            {
                header: true,
                footer: true
            }
        }
    );
}

setTimeout
(
    function () 
    {
        configurarDataTable();
    }, 
    1000
);
</script>

<script>
actualizarEstadoCompletado = function()
{
    x = confirm( "Esta seguro de Cerrar la Orden de Compra?" );
    if( x )
    {
        blockContent( "content_grid" );
        var str_serialize = "i_Id=<?=$i_Id?>&status=CO";
        executeAjaxJson( 
                            "<?php echo buildUrl( 'recepcion/updateStatus' ) ?>", 
                            str_serialize,
                            function( o_json )
                            {
                                console.log( o_json );
                                if( o_json.error )
                                {
                                    mostrarMensajeError( o_json.mensaje_error );
                                    return false;
                                }
                                location.reload();
                            } 
                       );
    }
}
</script>

<script>
abrirPopupAsociarNC = function()
{
    $( "span.ui-dialog-title" ).text( 'Asociar Nota de Credito Proveedor' ); 
    $( '#myIframe2' ).attr( 'src', '<?=buildUrl( 'recepcion/asociarNCProv', 'i_Id='.$o_COM_OrdenCompraCabeceraBN->getIdOrdenCompraConcat() )?>' );
    $( '#dialog2' ).dialog( 'open' );
}
abrirPopupAgregarDetalles = function()
{
    $( "span.ui-dialog-title" ).text( 'Agregar Detalles' ); 
    $( '#myIframe2' ).attr( 'src', '<?=buildUrl( 'oc/detail', 'i_Id='.$o_COM_OrdenCompraCabeceraBN->getIdOrdenCompraConcat() )?>' + '/externo/1' );
    $( '#dialog2' ).dialog( 'open' );
}
completarParmetro = function( elm_sel )
{
    if( $( $( $( elm_sel ).closest( 'tr' ) ).find( 'input:checkbox' ) ).is( ":checked" ) )
    {
        var i_value = $( $( $( elm_sel ).closest( 'tr' ) ).find( 'input:checkbox' ) ).val();
        openPopup( i_value );
    }
    else
    {
        alert( "Por favor hacer clic primero sobre el Check del lado izquierdo del registro para poder Recepcionar." );
    }
}
completarParmetros = function()
{
    var checkboxes = $( "#content_all_row" ).find( 'input:checked' );
    
    if( checkboxes.length > 0 )
    {
        var a_values = [];
        $.each
        ( 
            checkboxes, 
            function( index, o_elm ) 
            {
                a_values.push( o_elm.value );
            }
        );
        openPopup( a_values.join( "_" ) );
    }
    else
    {
        alert( "Debe de seleccionar por lo menos un registro." );
    }
}
openPopup = function( secuencia )
{
    $( "span.ui-dialog-title" ).text( 'Recepcionado de Ordenes de Compra' ); 
    $( '#myIframe2' ).attr( 'src', '<?=buildUrl( 'recepcion/receipt', 'i_Id='.$o_COM_OrdenCompraCabeceraBN->getIdOrdenCompraConcat() )?>' + "/secuencia/" + secuencia );
    $( '#dialog2' ).dialog( 'open' );    
}
closeWindow = function()
{
    $( '#dialog' ).dialog( 'close' );
    $( '#dialog2' ).dialog( 'close' );
}
recargarPagina = function( IdTrans )
{
    console.log( "123" );
    location.href = "<?=buildUrl( 'recepcion/process', 's_TipoProc='.$s_TipoProc.'&i_Id='.$o_COM_OrdenCompraCabeceraBN->getIdOrdenCompraConcat().'&IdTrans=' )?>" + IdTrans;
}
abrirPopupBlanco = function( IdTrans )
{
    x = confirm( "Desea imprimir la recepcion?" )
    if( x )
    {
       window.open( '<?=buildUrl( 'transacciones/reporte', "i_Id=" )?>' + IdTrans );
    }
}
</script>
<?php if( $IdTrans ){ ?>
<script type="text/javascript">
abrirPopupBlanco( '<?=$IdTrans?>' );
</script>
<?php } ?>