<?php use_helper('UrlFunctions'); ?>
<?php use_helper( 'UtilFunctions' ); ?>
<?php use_helper( 'IncludePartial' ); ?>
<style>
.header { background-color: #E1E1E1; color: #444444; font-family: Arial,Helvetica,sans-serif; font-size: 11px; font-weight: bold; }
.pager { background-color: #F4F4F4; background-image: none; border-bottom: thin solid #999999; }
</style>

<div class="shadow-sm p-3 mb-2 bg-white rounded card card-body">
    <div class="row">
        <div class="col mb-1">
            <h4 class="text-info text-nowrap mb-0"><?=$s_Titulo?></h4>
        </div>
    </div>
</div>

<ul class="nav nav-tabs m-1" id="myTab" role="tablist" style="margin-bottom: 0px !important;">
    <li class="nav-item">
        <a class="nav-link " id="home-tab" data-toggle="tab" href="#home" role="tab" aria-controls="home" aria-selected="false" onclick="location.href='<?=buildUrl( 'listprecios/addedit', "s_TipoProc=upd&i_Id=".$i_Id )?>';">General</a>
    </li>
    <li class="nav-item">
        <a class="nav-link active" id="profile-tab" data-toggle="tab" href="#profile" role="tab" aria-controls="profile" aria-selected="true">Detalle</a>
    </li>
</ul>

<div class="tab-content card" id="myTabContent">
    <div class="tab-pane fade show active" id="profile" role="tabpanel" aria-labelledby="profile-tab">
        <div class="form-row mx-0">
            <?php include_partial( "infohead", array( "o_DefaultBN" => $o_DefaultBN ) ); ?>
        </div>
    </div>
</div>

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

<form name="form1" id="form1" method="POST" action="" onsubmit="">
    <input type="hidden" id="i_Id" name="i_Id" value="<?=$i_Id?>">
    <input type="hidden" id="Control" name="Control" value="1">
    
    <div class="card">
        <!--  <h5 class="card-header">Datos Generales</h5>-->
        
        <div class="card-body" style="padding: 2px;">
            
            <div class="form-row mx-0">
                <div class="col-md-4 mb-1">
                    <label class="col-form-label" for="">Linea</label>
                    <?=crearSelect( array( "IdLinea", "getIdLinea", "getDescripcion" ), $a_MST_ClaseLinea, $IdLinea, "listprecios", "llenarComboLinea", array( "IdFamilia" ), "GetFamilia", array( "IdLinea", "IdFamilia", "CodigoBarras" ) )?>
                </div>
                <div class="col-md-4 mb-1">
                    <label class="col-form-label" for="IdRango">Familia</label>
                    <?=crearSelect( array( "IdFamilia", "getIdFamilia", "getDescripcion" ), $a_MST_ClaseFamilia, $IdFamilia )?>
                </div>
                <div class="col-md-4 mb-1">
                    <label class="col-form-label" for="IdItem">C&oacute;digo</label>
                    <input type="text" id="IdItem" name="IdItem" value="<?=$IdItem?>" class="form-control" >
                </div>
            </div>
    
        </div>
        
        <div class="card-body" style="padding: 2px;">
            
            <div class="form-row mx-0">
                <div class="col-md-4 mb-1">
                    <label class="col-form-label" for="">Tipo Lista</label>
                    <input type="text" id="" name="" value="<?=$o_DefaultBN->getTipoListaDesc()?>" class="form-control" readonly="readonly">
                    <input type="hidden" id="IdTipoLista" name="IdTipoLista" value="<?=$o_DefaultBN->getIdTipoLista()?>">
                </div>
                <div class="col-md-4 mb-1">
                    <label class="col-form-label" for="IdRango">Rango</label>
                    <select class="form-control form-control-sm custom-select my-0 mr-sm-2" name="IdRango" id="IdRango" onchange="" >
                        <option value=""> - SELECIONE - </option>
                        <?php foreach ( $a_MST_Rango as $o_MST_RangoBN ){ ?>
                        <option value="<?=$o_MST_RangoBN->getIdRango()?>" <?php if( $o_MST_RangoBN->getIdRango() == $s_IdRango ){ echo "selected"; } ?> > <?=strtoupper( $o_MST_RangoBN->getDescripcion() )?> </option>
                        <?php } ?>
                    </select>
                </div>
                <div class="col-md-4 mb-1">
                    <label class="col-form-label" for="">&nbsp;</label>
                    <button type="submit" class="btn btn-primary btn-block" onclick="">BUSCAR</button>
                </div>
            </div>
    
        </div>
    </div>
    
    <div class="dropdown-divider"></div>
    
    <div id="content_alert" style=""></div>
    
    <fieldset class="">
        <!-- <legend class="w-auto">Productos</legend> -->
        <div class="table-responsive" id="content_precios">
            <table class="table  table-bordered table-hover">
                <thead class="table-primary">
                    <tr>
                        <th scope="col" style="text-align: center;">Nro</th>
                        <th scope="col" style="text-align: center;">Codigo</th>
                        <th scope="col" style="text-align: center;">Descripcion</th>
                        <th scope="col" style="text-align: center;">Costo</th>
                        <th scope="col" style="text-align: center;">&nbsp;</th>
                        <th scope="col" style="text-align: center;">&nbsp;</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ( $a_ALM_ItemMast as $i_Key => $o_ALM_ItemMastBN ) { ?>
                        <?php $s_IdTR = "TR_".$i_Key; ?>
                    <tr id="<?=$s_IdTR?>" >
                        <td align="center" class="text-nowrap"><?=( $i_Key + 1)?></td>
                        <td align="center"><?=$o_ALM_ItemMastBN->getIdItem()?></td>
                        <td align="center"><?=$o_ALM_ItemMastBN->getDescripcionCompleta()?></td>
                        <td align="center">
                            <input type="text" class="text" name="PrecioCostoIGV[<?=$o_ALM_ItemMastBN->getIdItem()?>]"
                                            id="<?=$s_IdTR?>_PrecioCostoIGV" 
                                            value="<?=$o_ALM_ItemMastBN->getPrecioCostoLstPrecioIGV( myUser::getUser()->getIdBusiness(), myUser::getUser()->getIdSucursal() )?>"
                                            style="text-align:center; width: 90px;" readonly="readonly"/>
                        </td>
                        <td align="center">
                        
                            <?php if( $o_DefaultBN->getIdTipoLista() == 1 || $o_DefaultBN->getIdTipoLista() == 2 ){ ?>
                            <div class="card">
                                <div class="form-row mx-0">
                                    <div class="col-md-3 mt-1 mb-1"><label class="col-form-label" for=""><b>Rango</b></label></div>
                                    <div class="col-md-3 mt-1 mb-1"><label class="col-form-label" for=""><b>Valor Sin IGV</b></label></div>
                                    <div class="col-md-3 mt-1 mb-1"><label class="col-form-label" for=""><b>IGV</b></label></div>
                                    <div class="col-md-3 mt-1 mb-1"><label class="col-form-label" for=""><b>Total</b></label></div>
                                </div>
                            </div>
                                <?php foreach ( $a_MST_RangoDetalle as $i_Key2 => $o_MST_RangoDetalleBN ) { ?>
                                    <?php 
                                    $s_IdItem = $o_ALM_ItemMastBN->getIdItem();
                                    $i_IdTP = $o_MST_RangoDetalleBN->getIdRangoDetalle();
                                    
                                    $o_MST_ListaPrecioDetalleFN = new MST_ListaPrecioDetalleFN();
                                    $o_MST_ListaPrecioDetalleFN->setIdCompania( myUser::getUser()->getIdBusiness() );
                                    $o_MST_ListaPrecioDetalleFN->setIdSucursal( myUser::getUser()->getIdSucursal() );
                                    $o_MST_ListaPrecioDetalleFN->setIdItem( $s_IdItem );
                                    $o_MST_ListaPrecioDetalleFN->setIdTipoLista( $o_DefaultBN->getIdTipoLista() );
                                    $o_MST_ListaPrecioDetalleFN->setIdRango( $s_IdRango );
                                    $o_MST_ListaPrecioDetalleFN->setIdRangoDetalle( $i_IdTP );
                                    $a_MST_ListaPrecioDetalle = $o_MST_ListaPrecioDetalleFN->select();
                                    $f_PrecioVenta    = 0;
                                    $f_MontoIGV       = 0;
                                    $f_PrecioVentaIGV = 0;
                                    if( $a_MST_ListaPrecioDetalle )
                                    {
                                        $f_PrecioVenta    = $a_MST_ListaPrecioDetalle[0]->getPrecioVenta();
                                        $f_MontoIGV    = $a_MST_ListaPrecioDetalle[0]->getMontoIGV();
                                        $f_PrecioVentaIGV = $a_MST_ListaPrecioDetalle[0]->getPrecioVentaIGV();
                                    }
                                    ?>
                            <div class="card">
                                <div class="form-row mx-0">
                                    <div class="col-md-3 mt-1 mb-1">
                                        <label class="col-form-label" for=""><?=$o_MST_RangoDetalleBN->getMinimo()?> - <?=$o_MST_RangoDetalleBN->getMaximo()?></label>
                                    </div>
                                    <div class="col-md-3 mt-1 mb-1">
                                        <input type="text" id="<?=$s_IdTR?>_<?=$i_Key2?>_PrecioVenta" name="PrecioVenta[<?=$s_IdItem?>][<?=$i_IdTP?>]" value="<?=$f_PrecioVenta?>" 
                                            class="form-control text-center form-control-sm" onfocus="this.select();"
                                            	onblur="calcularMontos( '<?=$s_IdTR?>_<?=$i_Key2?>', false )" 
                                            >
                                    </div>
                                    <div class="col-md-3 mt-1 mb-1">
                                        <input type="text" id="<?=$s_IdTR?>_<?=$i_Key2?>_MontoIGV" name="MontoIGV[<?=$s_IdItem?>][<?=$i_IdTP?>]" value="<?=$f_MontoIGV?>" readonly="readonly" 
                                            class="form-control text-center form-control-sm" onfocus="this.select();" 
                                            >
                                    </div>
                                    <div class="col-md-3 mt-1 mb-1">
                                        <input type="text" id="<?=$s_IdTR?>_<?=$i_Key2?>_PrecioVentaIGV" name="PrecioVentaIGV[<?=$s_IdItem?>][<?=$i_IdTP?>]" value="<?=$f_PrecioVentaIGV?>"
                                            onblur="calcularMontos( '<?=$s_IdTR?>_<?=$i_Key2?>', true )" 
                                            class="form-control text-center form-control-sm" onfocus="this.select();" 
                                            >
                                    </div>
                                </div>
                            </div>
                                <?php } ?>
                            <?php } ?>
                            
                            <!-- TIPO DE PRECIOS -->
                            <?php if( $o_DefaultBN->getIdTipoLista() == 3 ){ ?>
                            <table class="table table-bordered">
                                <thead>
                                    <tr>
                                        <th scope="col"><label class="col-form-label" for=""><b>Tipo</b></label></th>
                                        <th scope="col"><label class="col-form-label" for=""><b>Utilidad</b></label></th>
                                        <th scope="col"><label class="col-form-label" for=""><b>Pre Uni</b></label></th>
                                        <th scope="col"><label class="col-form-label" for=""><b>Pre UniIGV</b></label></th>
                                    </tr>
                                </thead>
                                <tbody>
                                <?php foreach ( MST_TipoPrecioFN::select_all() as $i_Key2 => $o_MST_TipoPrecioBN ){ ?>
                                    <?php $i_IdTP = $o_MST_TipoPrecioBN->getIdTipoPrecio() ?>
                                    
                                    <tr>
                                        <th scope="row"><?=$o_MST_TipoPrecioBN->getDescripcion()?></th>
                                        <td>
                                            <input type="text" class="text" name="PorcentajeUtilidad[<?=$o_ALM_ItemMastBN->getIdItem()?>][<?=$i_IdTP?>]" id="<?=$s_IdTR?>_<?=$i_Key2?>_PorcentajeUtilidad" 
                                                value="<?=$o_ALM_ItemMastBN->getPorcentajeUtilidadLstPrecio( myUser::getUser()->getIdBusiness(), myUser::getUser()->getIdSucursal(), $i_IdTP )?>" onblur="" onkeypress="return NumCheckDecimal( event, this );" 
                                                onfocus="this.select();" style="text-align:center;"/>
                                        </td>
                                        <td>
                                            <input type="text" class="text" name="PrecioVenta[<?=$o_ALM_ItemMastBN->getIdItem()?>][<?=$i_IdTP?>]" id="<?=$s_IdTR?>_<?=$i_Key2?>_PrecioVenta" 
                                                value="<?=$o_ALM_ItemMastBN->getPrecioVentaLstPrecio( myUser::getUser()->getIdBusiness(), myUser::getUser()->getIdSucursal(), $i_IdTP )?>" onblur="calcularMontoSinIGV('<?=$s_IdTR?>','<?=$i_Key2?>', false);" 
                                                onkeypress="return NumCheckDecimal( event, this );" onfocus="this.select();" style="text-align:center;"/>
                                            <input type="hidden" name="MontoIGV[<?=$o_ALM_ItemMastBN->getIdItem()?>][<?=$i_IdTP?>]" id="<?=$s_IdTR?>_<?=$i_Key2?>_MontoIGV" 
                                                value="<?=$o_ALM_ItemMastBN->getMontoIGVLstPrecio( myUser::getUser()->getIdBusiness(), myUser::getUser()->getIdSucursal(), $i_IdTP )?>">
                                        </td>
                                        <td>
                                            <input type="text" class="text" name="PrecioVentaIGV[<?=$o_ALM_ItemMastBN->getIdItem()?>][<?=$i_IdTP?>]" id="<?=$s_IdTR?>_<?=$i_Key2?>_PrecioVentaIGV" 
                                                value="<?=$o_ALM_ItemMastBN->getPrecioVentaLstPrecioIGV( myUser::getUser()->getIdBusiness(), myUser::getUser()->getIdSucursal(), $i_IdTP )?>" onblur="calcularMontoSinIGV('<?=$s_IdTR?>','<?=$i_Key2?>', true);" 
                                                onkeypress="return NumCheckDecimal( event, this );" onfocus="this.select();" style="text-align:center;"/>
                                        </td>
                                    </tr>
                                <?php } ?>
                                </tbody>
                            </table>
                            <?php } ?>
                            
                        </td>
                        
                        
                        <?php if( $o_DefaultBN->getIdTipoLista() == 3 ){ ?>
                        <td align="center"><input type="button" class="btn btn-primary btn-block" value="Guardar" onclick="executeSaveDetail2('<?=$s_IdTR?>', true);"/></td>
                        <?php }else{ ?>
                        <td align="center"><button type="button" class="btn btn-primary btn-block" onclick="executeSaveDetail('<?=$s_IdTR?>', true);">GUARDAR</button></td>
                        <?php } ?>
                    </tr>
                    <?php } ?>
                </tbody>
            </table>
        </div>
    </fieldset>
    
</form>
                
<script>
executeSaveDetail = function( s_Id, b_Control )
{
    var b_Result = validacionesFormularioGrilla();
    if( b_Result )
    {
        return false;
    }
    
    if( b_Control )
    {
        var serializearray = $( "#"+s_Id+" :input" ).serializeArray();

        var strFormSerialize = $.param( serializearray );
        
        var strFormSerialize = decodeURIComponent( $.param( serializearray ) );
        
        strFormSerialize = strFormSerialize + "&i_Id=<?=$i_Id?>&TypeSub=0";
    }
    else
    {
        var strFormSerialize = $( 'form' ).serialize();
    }
    
    x = confirm( "Esta seguro de guardar?" );
    if( x )
    {
        mostrarCargaImg();
        a_Params = [];
        a_Params.push( "i_Id=" + i_Id );
        executeAjaxJson
        ( 
            "<?=buildUrl( "listprecios/saveDetailV2" )?>", 
            strFormSerialize, 
            function( o_json )
            {
                if( o_json.error )
                {
                    mostrarMensajeError( o_json.mensaje_error );
                    return true;
                }
                ocultarCargaImg();
            } 
        );
    }
}

calcularUtilidad = function( IdTR, Key2 )
{
    var id_precio_costo    = IdTR + "_PrecioCostoIGV";
    var id_porcentaje_util = IdTR + "_PorcentajeUtilidad";
    var id_precio_venta    = IdTR + "_PrecioVentaIGV";
    if( $( "#" + id_porcentaje_util ).length <= 0 )
    {
        id_porcentaje_util = IdTR + "_" + Key2 + "_PorcentajeUtilidad";
        id_precio_venta    = IdTR + "_" + Key2 + "_PrecioVentaIGV";
    }
    if( $( "#" + id_precio_costo ).val() != ""  && $( "#" + id_precio_venta ).val() != "" )
    {
        var precio_costo = parseFloat( $( "#" + id_precio_costo ).val() );
        var precio_venta = parseFloat( $( "#" + id_precio_venta ).val() );

        /*var utilidad_cal_1 = precio_venta - precio_costo;
        var utilidad_cal_2 = redondear( ( utilidad_cal_1 * 100 ) / precio_costo, 2 );*/
        var utilidad_cal_2 = redondear( ( 100 * ( precio_venta - precio_costo ) / precio_venta ), 2 );
        
        $( "#" + id_porcentaje_util ).val( utilidad_cal_2 );
    }
}

calcularMontoSinIGV = function( IdTR, Key2, conIGV )
{
    id_precio_venta_igv = IdTR + "_" + Key2 + "_PrecioVentaIGV";
    id_precio_venta     = IdTR + "_" + Key2 + "_PrecioVenta";
    id_monto_igv        = IdTR + "_" + Key2 + "_MontoIGV";

    if( conIGV )
    {
        if( $( "#" + id_precio_venta_igv ).val() != "" )
        {
            var precio_venta_igv = parseFloat( $( "#" + id_precio_venta_igv ).val() );
            precio_venta = redondear( ( precio_venta_igv / 1.18 ), 2 );
            monto_igv = redondear( ( precio_venta_igv - precio_venta ), 2 );
    
            $( "#" + id_precio_venta ).val( precio_venta );
            $( "#" + id_monto_igv ).val( monto_igv );
        }
    }
    else
    {
    	if( $( "#" + id_precio_venta ).val() != "" )
    	{
    		var precio_venta = parseFloat( $( "#" + id_precio_venta ).val() );
            var monto_igv = redondear( ( precio_venta * 0.18 ), 2 );
            var precio_venta_igv = redondear( precio_venta + monto_igv, 2 );

            $( "#" + id_precio_venta_igv ).val( precio_venta_igv );
            $( "#" + id_monto_igv ).val( monto_igv );
    	}
    }
    calcularUtilidad( IdTR, Key2 );
}

calcularMontos = function( str, conIGV )
{
	id_precio_venta_igv = str + "_PrecioVentaIGV";
    id_precio_venta     = str + "_PrecioVenta";
    id_monto_igv        = str + "_MontoIGV";

    if( conIGV )
    {
    	if( $( "#" + id_precio_venta_igv ).val() != "" )
    	{
            var precio_venta_igv = parseFloat( $( "#" + id_precio_venta_igv ).val() );
            precio_venta = redondear( ( precio_venta_igv / 1.18 ), 2 );
            monto_igv = redondear( precio_venta_igv - precio_venta, 2 );    
    
            $( "#" + id_precio_venta ).val( precio_venta );
            $( "#" + id_monto_igv ).val( monto_igv );
    	}
    }
    else
    {
    	if( $( "#" + id_precio_venta ).val() != "" )
    	{
            var precio_venta = parseFloat( $( "#" + id_precio_venta ).val() );
            monto_igv = redondear( ( precio_venta * 0.18 ), 2 );
            precio_venta_igv = redondear( precio_venta + monto_igv, 2 );
    
            $( "#" + id_precio_venta_igv ).val( precio_venta_igv );
            $( "#" + id_monto_igv ).val( monto_igv );
    	}
    }
}
</script>
                                            
<script>
executeSaveDetail = function( s_Id, b_Control )
{
    var IdTipoLista = $( "#IdTipoLista" ).val();
    var IdRango     = $( "#IdRango" ).val();
    
    if( b_Control )
    {
        var serializearray = $( "#"+s_Id+" :input" ).serializeArray();
        var strFormSerialize = $.param( serializearray );
        var strFormSerialize = decodeURIComponent( $.param( serializearray ) );
        
        strFormSerialize = strFormSerialize + "&i_Id=<?=$i_Id?>&IdTipoLista=" + IdTipoLista + "&IdRango=" + IdRango;
    }
    else
    {
        var strFormSerialize = $( 'form' ).serialize();
    }
    
    x = confirm( "Esta seguro de guardar?" );
    if( x )
    {
    	blockContent( 'content_precios' );
        a_Params = [];
        a_Params.push( "i_Id=" + i_Id );
        executeAjaxJson
        ( 
            "<?=buildUrl( "listprecios/saveDetailV3" )?>", 
            strFormSerialize, 
            function( o_json )
            {
            	unBlockContent( 'content_precios' );
                if( o_json.error )
                {
                	showAlertBootStrap( 'content_alert', 'alert-danger', o_json.mensaje_error );
                    return true;
                }
                
            } 
        );
    }
}
</script>


