<?php

//ini_set("display_errors",1);

session_start();

require_once ("../../config/Cado.php");

require_once ("../catalogo/cCatalogo.php");

$oCatalogo = new cCatalogo();

require_once ("../categoria/cCategoria.php");

$oCategoria = new cCategoria();

require_once ("../almacen/cAlmacen.php");

$oAlmacen = new cAlmacen();

require_once("../producto/cStock.php");

$oStock = new cStock();

require_once("../producto/cPrecio.php");

$oPrecio = new cPrecio();



require_once ("../formatos/formato.php");







//almacenes array

$dts=$oAlmacen->filtrar_x_empresa($_SESSION['empresa_id']);

while($dt = mysql_fetch_array($dts)){

	$alm_id[]=$dt['tb_almacen_id'];

	$alm_nom[]=$dt['tb_almacen_nom'];

}

mysql_free_result($dts);





$nom = $_POST['txt_fil_pro_nom'];

$codbar = $_POST['txt_fil_pro_codbar'];

$cat_id = $_POST['cmb_fil_cat_id'];

$mar_id = $_POST['cmb_fil_mar_id'];





	$dts1=$oCatalogo->catalogo_stock_general($nom,$codbar,$cat_id,$mar_id);

	$num_rows= mysql_num_rows($dts1);



?>

<script type="text/javascript">



$('.cantidad').autoNumeric({

	aSep: '',

	aDec: '.',

	vMin: '0',

	vMax: '99999'

});



function actualizar_stock(act,idf,preid,almid,stoid)

{

	$('#lbl_sto_'+idf).hide();

	if($('#txt_sto_'+idf).val()!="")

	{

		$.ajax({

			type: "POST",

			url: "../producto/stock_reg.php",

			async:true,

			dataType: "html",                      

			data: ({

				action:	'actualizar_stock',

				tipo:	act,

				alm_id: almid,

				pre_id: preid,

				sto_id: stoid,

				sto_num: $('#txt_sto_'+idf).val()

			}),

			beforeSend: function() {

				$('#lbl_sto_'+idf).addClass("ui-state-highlight ui-corner-all");

				$('#lbl_sto_'+idf).html('G...');

				$('#lbl_sto_'+idf).show(100);

				//$('#div_catalogo_filtro').html('Cargando <img src="images/loadingf11.gif" align="absmiddle"/>');

			},

			success: function(html){

				$('#lbl_sto_'+idf).html(html);

				//$('#div_catalogo_filtro').html(html);

			},

			complete: function(){

				//$('#lbl_sto_'+idf).hide();

				//catalogo_tabla();

			}

		});

	}

	else

	{

		$('#lbl_sto_'+idf).addClass("ui-state-highlight ui-corner-all");

		$('#lbl_sto_'+idf).html('Número?');

		$('#lbl_sto_'+idf).show(100);

	}

}



$(document).ready(function() {

	$('.btn_imprimir_codbar').button({

		icons: {primary: "ui-icon-print"},

		text: false

	});

	$('.btn_guardar').button({

		icons: {primary: "ui-icon-disk"},

		text: true

	});

	$('.btn_editar_stock_rep').button({

		icons: {primary: "ui-icon-pencil"},

		text: false

	});

	$("#tabla_producto").tablesorter({

		widgets: ['zebra', 'zebraHover'] ,

		widthFixed: true,

		headers: {

			//4: {sorter: 'shortDate' }

		},

		//sortForce: [[0,0]],

		<?php if($num_rows>0){?>

		sortList: [[2,0],[1,0],[0,0]]

		<?php }?>

    });





}); 

</script>







<section class="content" id="table" style="min-height:0px;padding-bottom:0px;margin-bottom:-30px;">

	<div class="row">

		<div class="col-xs-12">

			<div class="box">

				<div class="box-body">

					<table id="tabla_producto" class="table table-bordered table-striped table-responsive">

					

			            <thead>

			                <tr>

			                  	<th>NOMBRE</th>

			                    <th>MARCA</th>

			                    <th>CATEGORIA</th>

			                    <th>UN</th>

			                    <th align="right" nowrap="nowrap">COSTO</th>

			                    <th align="right" nowrap="nowrap">PRECIO VENTA</th>

			                    <th align="right" nowrap title="PRECIO MINIMO"> P.  MIN. S/.</th>

			                    <th align="right" nowrap title="PRECIO MINIMO"> P.  MAY. S/.</th>

			                    <?php foreach ($alm_nom as $value) {

			                    	echo "<th align='right'>".$value."</th>";

			                    } ?>

			                    <th>&nbsp;</th>

			                    <!--th align="right">< ?php echo $alm_nom[0]?></th>

			                    <th align="right">< ?php echo $alm_nom[1]?></th>

			                    <th align="right">< ?php echo $alm_nom[2]?></th-->

			                </tr>

			            </thead>

						<?php

			            if($num_rows>=1){

			            ?>

			            <tbody>

			                <?php

								while($dt1 = mysql_fetch_array($dts1)){

									//stock

									$rws = $oStock->stock_por_presentacion($dt1['tb_presentacion_id'],$_POST['alm_id']);

									$num_filas=mysql_num_rows($rws);

									$rw = mysql_fetch_array($rws);

									$stock_num=$rw['tb_stock_num'];

									mysql_free_result($rws);							

									$stock_unidad=0;						

									if($num_filas>0){

										$st_uni=floor($stock_num/$dt1['tb_catalogo_fac']);

										$st_res=$stock_num%$dt1['tb_catalogo_fac'];



										if($st_res!=0){

											//$stock_unidad="$st_uni + r$st_res";

											$stock_unidad=$st_uni;

										} else{

											$stock_unidad=$st_uni;

										}							

										$action_stock='editar';

										$stock_id=$rw['tb_stock_id'];

									}

									else

									{

										$action_stock='insertar';

										$stock_unidad='-';

									}

									//PRECIOS

									$precio=1;

									//echo '→'.$precio.' / '.$dt1['tb_catalogo_id']; // 1 / 240

									$rws = $oPrecio->consultar_precio($precio,$dt1['tb_catalogo_id']);

									$rw = mysql_fetch_array($rws);

									$predet_id1=$rw['tb_preciodetalle_id'];

									$predet_val1=$rw['tb_preciodetalle_val'];

									$precio=2;

									$rws = $oPrecio->consultar_precio($precio,$dt1['tb_catalogo_id']);

									$rw = mysql_fetch_array($rws);

									$predet_val2=$rw['tb_preciodetalle_val'];

									//echo '→'.$predet_id1;//''

									//echo '→'.$predet_val1;//''

									mysql_free_result($rws);

									$total_costo += $dt1['tb_catalogo_precos'];

									$total_precio_venta += $dt1['tb_catalogo_preven'];

									$total_precio_min += $predet_val1;

									$total_precio_may += $predet_val2;

								?>

			                <tr>

			                 	<td>

			                    	<span style=""><?php echo $dt1['tb_producto_nom']?></span>

			                	</td>

			                    <td><?php echo $dt1['tb_marca_nom']?></td>

			                    <td><?php echo $dt1['tb_categoria_nom']?></td>

			                    <td title="<?php echo $dt1['tb_unidad_nom']?>">

									<span style=""><?php echo $dt1['tb_unidad_abr']?></span>

			                    </td>

			                    <td align="right"><?php echo formato_money($dt1['tb_catalogo_precos'])?></td>

			                    <td align="right"><?php echo formato_money($dt1['tb_catalogo_preven'])?></td>

								<td align="right" nowrap><?php if($predet_val1!=0)echo formato_money($predet_val1)?></td>

								<td align="right" nowrap><?php if($predet_val2!=0)echo formato_money($predet_val2)?></td>

			                    <?php

			                    	//stock

			                    	//echo '→'.$dt1['tb_presentacion_id'];//''240

									//echo '→'.$alm_id[0];//1

			                    $total_almacen;

			                    foreach ($alm_id as $value) {

			                    	$rws = $oStock->stock_por_presentacion($dt1['tb_presentacion_id'],$value);

			                    	$num_filas=mysql_num_rows($rws);

			                    	$rw = mysql_fetch_array($rws);

			                    	$stock_num=$rw['tb_stock_num'];

			                    	mysql_free_result($rws);

			                    	$stock_unidad=0;

			                    	

			                    	if($num_filas>0){

			                    		

			                    		if($stock_num>0){

			                    			$st_uni=floor($stock_num/$dt1['tb_catalogo_fac']);

			                    			$st_res=$stock_num%$dt1['tb_catalogo_fac'];	

			                    			if($st_res!=0){

													//$stock_unidad="$st_uni + r$st_res";

			                    				$stock_unidad=$st_uni;

			                    			}else{

			                    				$stock_unidad=$st_uni;

			                    			}

			                    		}else{

			                    			$stock_unidad=$stock_num;

			                    		}

			                    		

			                    		$action_stock='editar';

			                    		$stock_id=$rw['tb_stock_id'];

			                    	}else{

			                    		$action_stock='insertar';

			                    		$stock_unidad='-';

			                    	}

			                    	echo "<td align='right' nowrap>".$stock_unidad."</td>";

			                    	$total_almacen[$value] += $stock_unidad;

			                    }						

									//fin

									$producto_nom = $dt1['tb_producto_nom'];

									$producto_nom = str_replace("'","\'", $producto_nom);

									$producto_nom = str_replace('"',"\&quot", $producto_nom);

								?>

								<td align="center">

									<a class="btn_imprimir_codbar" href="#imprimir" onClick="producto_codbar_form('<?php echo $dt1['tb_catalogo_id']?>','<?php echo $producto_nom?>','<?php echo $dt1['tb_presentacion_codbar']?>','<?php echo $stock_unidad?>')">Imprimir</a>

									<a class="btn_editar_stock_rep" href="#editar" onClick="producto_form('editar','<?php echo $dt1['tb_producto_id']?>')">Editar Producto</a>

								</td>                                       

			                </tr>

			                <?php

			            	}

			                mysql_free_result($dts1);

			                ?>

			            </tbody>

			            <?php

						}

						?>

							<tr class="even">

			                  <td colspan="4">TOTAL</td>

			                  <td align="right"><strong><?php echo formato_money($total_costo)?></strong></td>

			                  <td align="right"><strong><?php echo formato_money($total_precio_venta)?></strong></td>

			                  <td align="right"><strong><?php echo formato_money($total_precio_min)?></strong></td>

			                  <td align="right"><strong><?php echo formato_money($total_precio_may)?></strong></td>

			                  <?php



			                  	foreach ($alm_id as $value){

			                  		echo "<td align='right' nowrap>".$total_almacen[$value]."</td>";

			                  	}

			                  	

			                  ?>

			                  <td>&nbsp;</td>

			                </tr>

			                <tr class="even">

			                 	<td colspan="<?php echo (9+(count($alm_id))); ?>"><?php echo $num_rows.' registros'?></td>

			                </tr>

			        



        				</table>

					</div>

				</div>

			</div>

		</div>

	</section>







