<?php use_helper('UrlFunctions'); ?>
<?php use_helper( 'IncludePartial' ); ?>
<script>
mostrarCargaImg = function(){ $('#s_ImagenLoad')[0].style.display = 'inline'; }
ocultarCargaImg = function(){ $('#s_ImagenLoad')[0].style.display = 'none'; }

mostrarMensajeError = function( s_MensajeError )
{
    $( "#resultContent" ).html( '<div id="mensaje_error_content" class="error">' + s_MensajeError + '</div>' );
    setTimeout( function(){ $( "#mensaje_error_content" ).fadeOut( 800 ).fadeIn( 800 ).fadeOut( 500 ).fadeIn( 500 ).fadeOut( 300 ); }, 4000 );
}
</script>
<script>
ejecutarGuardar = function()
{
    mostrarCargaImg();
    var strFormSerialize = $( 'form' ).serialize();
    executeAjaxJson( 
                        "<?php echo buildUrl( 'oc/saveAddEdit', "block=001" ) ?>", 
                        strFormSerialize,
                        function( o_json )
                        {
                            console.log( o_json );
                            ocultarCargaImg();
                            if( o_json.error )
                            {
                                mostrarMensajeError( o_json.mensaje_error );
                                return false;
                            }
                            //return false;
                            if( o_json.type_sub == "1" )
                            {
                                location.href = "<?=buildUrl( 'oc/detail', "block=001&s_TipoProc=upd&i_Id=")?>" + o_json.id;
                            }
                            else
                            {
                                location.href = "<?=buildUrl( 'oc/list', "block=001")?>";
                            }
                        } 
                   );
}
</script>
<style>
/*div.growlUI { background: url( check48.png ) no-repeat 10px 10px }*/
div.growlUI h1, div.growlUI h2 { color: white; padding: 0; text-align: center; }
</style>
<table class="cuerpo" width="98%" border="0" cellpadding="0" cellspacing="0">
    <tr>
        <td valign="top">
            <form id="form1" name="form1" method="POST" onsubmit="return false;"  enctype="multipart/form-data">
                <input type="hidden" name="s_TipoProc" id="s_TipoProc" value="<?=$s_TipoProc?>">
                <input type="hidden" name="TypeSub" id="TypeSub" value="1">
                <table class="" width="100%">
                    <tr>
                        <td valign="top">
                            <table width="100%" border="0" cellspacing="0" cellpadding="0">
                                <tr>
                                    <td class="marco marcoTitle">
                                        <table class="" width="100%" height="100%" border="0" cellspacing="0" cellpadding="0">
                                            <tr>
                                                <td width="100%" valign="top">

                                                    <input type="hidden" name="i_Id" id="i_Id" value="<?=$o_COM_OrdenCompraCabeceraBN->getIdOrdenCompraConcat()?>">
                                                    <div id="resultContent" style=""></div>
                                                    
                                                    <table width="100%">
                                                        <tr>
                                                            <td width="100%" valign="top" >
                                                                <div style="">
                                                                    <fieldset class="formGen">
                                                                        <legend class="fielsettex">Informacion General</legend>
                                                                        <table border="0" cellspacing="6" cellpadding="0" style="margin-left: 20px;" width="100%">
                                                                            <tr>
                                                                                <td nowrap="nowrap" width="20%" class="cabecera"><label class="label req" for="IdCompania" >Empresa</label></td>
                                                                                <td nowrap="nowrap" width="20%">
                                                                                    <input type="hidden" name="IdCompania" value="<?=$o_COM_OrdenCompraCabeceraBN->getIdCompania()?>">
                                                                                    <select disabled class="combo">
                                                                                        <option value=""> - Seleccione - </option>
                                                                                        <?php foreach ( $a_GSS_Compania as $i_Key => $o_a_GSS_CompaniaBN ){ ?>
                                                                                        <option value="<?=$o_a_GSS_CompaniaBN->getIdCompania()?>" <?php if( $o_a_GSS_CompaniaBN->getIdCompania()==$o_COM_OrdenCompraCabeceraBN->getIdCompania() ){ echo "SELECTED"; } ?> > <?=$o_a_GSS_CompaniaBN->getDescripcionCorta()?> </option>
                                                                                        <?php } ?>
                                                                                    </select>
                                                                                </td>
                                                                                <td nowrap="nowrap" width="10%">&nbsp;</td>
                                                                                
                                                                                <td nowrap="nowrap" width="20%" class="cabecera"><label class="label req" for="FechaDocumento" >Fecha</label></td>
                                                                                <td nowrap="nowrap" width="20%">
                                                                                    <input type="text" class="text" name="FechaDocumento" id="FechaDocumento" size="10px" value="<?=date( "Y-m-d" )?>" style="text-align:center;" /> 
                                                                                    <script type="text/javascript">
                                                                                    $( "#FechaDocumento" ).datepicker( { dateFormat: "yy-mm-dd", changeYear:true, yearRange: "-100:+0", changeMonth: true, onClose: function (){ this.focus(); } } );
                                                                                    </script>
                                                                                </td>
                                                                            </tr>
    														                <tr>
    														                    <td nowrap="nowrap" class="cabecera"><label class="label req" for="IdTransaccion" >Tipo Transacci&oacute;n</label></td>
    														                    <td nowrap="nowrap">
    														                        <input type="hidden" name="IdTransaccion" value="<?=COM_OrdenCompraCabeceraBN::TRANSACCION_X_DEFECTO?>">
    														                        <select class="combo" id="IdTransaccion" name="IdTransaccion" disabled>
    														                            <option value=""> - Seleccione - </option>
    													                                <?php foreach ( $a_ALM_TransaccionMast as $i_Key => $o_ALM_TransaccionMastBN ){ ?>
    														                            <option value="<?=$o_ALM_TransaccionMastBN->getIdTransaccion()?>" <?php if( COM_OrdenCompraCabeceraBN::TRANSACCION_X_DEFECTO == $o_ALM_TransaccionMastBN->getIdTransaccion() ){ echo "SELECTED"; } ?>> <?=utf8_encode( $o_ALM_TransaccionMastBN->getDescripcionLocal() )?> </option>
    													                                <?php } ?>
    														                        </select>
    														                    </td>
    														                    <td nowrap="nowrap" >&nbsp;</td>
    														                    
    														                    <td nowrap="nowrap" class="cabecera"><label class="label req">Almacen</label></td>
    														                    <td nowrap="nowrap">
                                                                                    <select disabled="disabled" id="IdAlmacen" name="IdAlmacen" style="width: 90%">
                                                                                        <option value=""> - Seleccione - </option>
                                                                                        <?php foreach ( $a_ALM_AlmacenMastOrg as $i_Key => $o_ALM_AlmacenMastOrgBN ){ ?>
                                                                                        <option value="<?=$o_ALM_AlmacenMastOrgBN->getIdAlmacen()?>" <?php if( $o_COM_OrdenCompraCabeceraBN->getIdAlmacenEntrega()==$o_ALM_AlmacenMastOrgBN->getIdAlmacen() ){ echo "SELECTED"; } ?>> <?=$o_ALM_AlmacenMastOrgBN->getDescripcionLocal()?> </option>
                                                                                        <?php } ?>
                                                                                    </select>
                                                                                </td>
    														                </tr>
    														                <tr>
    														                    <td nowrap="nowrap" class="cabecera"><label class="label req" for="ReferenciaTipoDocumento" >Doc Referencia</label></td>
    														                    <td nowrap="nowrap">
                                                                                <select class="combo" id="ReferenciaTipoDocumento" name="ReferenciaTipoDocumento">
                                                                                    <?php foreach ( $a_ALM_TipoDocumento as $i_Key => $o_ALM_TipoDocumentoBN ){ ?>
                                                                                    <option value="<?=$o_ALM_TipoDocumentoBN->getIdTipoDocumento()?>" <?php if( $o_ALM_TipoDocumentoBN->getIdTipoDocumento()==COM_OrdenCompraCabeceraBN::TIPO_DOCUMENTO ){ echo "SELECTED"; } ?>> <?=utf8_encode( $o_ALM_TipoDocumentoBN->getDescripcion() )?> </option>
                                                                                    <?php } ?>
                                                                                </select>&nbsp;
                                                                                &nbsp;
                                                                                <input type="text" class="text required" id="ReferenciaNumeroDocumento" name="ReferenciaNumeroDocumento" value="" style="text-align:center;" size="8"/>
    														                    </td>
    														                    <td nowrap="nowrap" >&nbsp;</td>
    														                    
    														                    <td nowrap="nowrap" class="cabecera"><label class="label req" for="GuiaProveedor" ># Guia Proveedor</label></td>
    														                    <td nowrap="nowrap">
                                                                                    <input type="text" class="text required" name="GuiaProveedor" id="GuiaProveedor" value="" style="text-align:center; width: 80%" />
                                                                                </td>
    														                </tr>
                                                                        </table>
                                                                    </fieldset>
                                                                </div>
                                                            </td>
                                                        </tr>
                                                    </table>
                                                </td>
                                            </tr>
                                        </table>
                                    </td>
                                </tr>
                                <tr> <td>&nbsp;</td> </tr>
                                <tr>
                                    <td class="marco">
                                        <table class="" width="100%" height="100%" border="0" cellspacing="0" cellpadding="0">
                                            <tr>
                                                <td width="100%" valign="top">
                                                    <fieldset class="formGen" style="margin-top:0px">
                                                        <legend class="fielsettex">Detalles</legend> 

                                                        <table class="grid" cellspacing="0" cellpadding="3" border="0" align="center" width="100%" style="border-bottom: 1px solid #999999;">
                                                            <tbody id="">
                                                                <tr>
                                                                    <th width="2%"  align="center"><b>No</b></th>
                                                                    <th width="10%" align="center"><b>C&oacute;digo</b></th>
                                                                    <th width="30%" align=""      ><b>Descripci&oacute;n</b></th>
                                                                    <th width="10%" align="center"><b>Stock Actual</b></th>
                                                                    <th width="10%" align="center"><b>U.Medida</b></th>
                                                                    <th width="10%" align="center"><b>Cantidad</b></th>
                                                                    <th width="10%" align="center"><b>Cant.Pedida</b></th>
                                                                    <th width="10%" align="center"><b>Precio</b></th>
                                                                    <th width="10%" align="center"><b>Ex.Impuesto</b></th>
                                                                </tr>
                                                            </tbody>
                                                            <tbody id="">
                                                                <?php foreach ( $a_COM_OrdenCompraDetalle as $i_Key => $o_COM_OrdenCompraDetalleBN ){ ?>
                                                                <tr>
                                                                    <td align="center"><?=( $i_Key + 1 )?></td>
                                                                    <td align="center"><?=$o_COM_OrdenCompraDetalleBN->getIdItem()?></td>
                                                                    <td align=""><?=$o_COM_OrdenCompraDetalleBN->getDescripcion()?></td>
                                                                    <td align="center"><?=$o_COM_OrdenCompraDetalleBN->getBuscarStockActual()?></td>
                                                                    <td align="center"><?=$o_COM_OrdenCompraDetalleBN->getObjectUnidad()->getDescripcionCorta()?></td>
                                                                    <td align="center"><input type="text" class="text" style="width: 60%; text-align: center;" name="" cantref="<?=$o_COM_OrdenCompraDetalleBN->getCantidadPedida()?>" /> </td>
                                                                    <td align="center"><?=$o_COM_OrdenCompraDetalleBN->getCantidadPedida()?></td>
                                                                    <td align="center"><?=number_format( $o_COM_OrdenCompraDetalleBN->getPrecioUnitario(), 2 )?></td>
                                                                    <td align="center"><?=$o_COM_OrdenCompraDetalleBN->getObjectItem()->getNoAfectoImpuestoVentasFlag()?></td>
                                                                </tr>
                                                                <?php } ?>
                                                            </tbody>
                                                        </table>
                                                    </fieldset>
                                                </td>
                                            </tr>
                                        </table>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <table width="100%" border="0" style="margin-top: 10px;">
                                            <tr>
                                                <td align="center">
                                                    <input type="button" class="button" value="Cancelar" onclick="location.href='<?php echo buildUrl( 'oc/list', 'block=001' ) ?>'">
                                                    <input type="submit" class="button" value="Grabar y Continuar" onclick="">
                                                </td>
                                            </tr>
                                            <tr>
                                                <td align="center">
                                                    <div id="s_ImagenLoad" style="display: none;"><img src="<?php echo Constantes::ROOT_DIR_APP ?>web/images/progress.gif" border="0"/>&nbsp;<label class="laber req">Grabando...</label></div>
                                                    &nbsp;
                                                </td>
                                            </tr>
                                        </table>
                                    </td>
                                </tr>
                            </table>
                        </td>
                    </tr>
                </table>
            </form>
        </td>
    </tr>
</table>
<script type="text/javascript">
$( ".grid td input:text" )
.keydown
(
    function ( e ) 
    {
        if ( $.inArray( e.keyCode, [46, 8, 9, 27, 13, 110] ) !== -1 || ( e.keyCode == 65 && ( e.ctrlKey === true || e.metaKey === true ) ) ||  ( e.keyCode >= 35 && e.keyCode <= 40 ) ) 
        {
            return;
        }
        if ( ( e.shiftKey || ( e.keyCode < 48 || e.keyCode > 57 ) ) && ( e.keyCode < 96 || e.keyCode > 105 ) )
        {
            e.preventDefault();
        }
    }
)
.blur
(
    function( obj )
    {
        if( $( obj.target ).val() != "" )
        {
            var i_cant    = parseInt( $( obj.target ).val() );
            var i_cantref = parseInt( $( obj.target ).attr( "cantref" ) );
            if( i_cant > i_cantref )
            {
            	$( obj.target ).val( "" );
            	$( obj.target ).focus();
            	$.growlUI( '', 'La cantidad es mayor a lo pedido' ); 
            }
        }
    }
);
</script>
<script>
validacionExtra = function()
{
    var b_Control = true;
    if( $( "#IdAlmacen" ).val() == $( "#AlmacenTraslado" ).val() )
    {
        alert( "Los Almacenes de Origen y Destino tienen que ser diferentes" );
        b_Control = false;
    }
    b_Control = validacionDetalles();
    console.log( b_Control );
    return b_Control;
}

$.validator.setDefaults
(
    {
        submitHandler: function( form ) 
        {
            if( validacionExtra() )
            {
                ejecutarGuardar();
            }
        }
    }
);
$( "#form1" ).validate();
</script>