<?
session_start();
if ($_SESSION['estado'] != "ON")
		{
		?>
		<script>
			parent.location.href = 'index.php';
		</script>
		<? 
		exit();
		}
$id_empresa = $_SESSION['ss_empresa'];
$id_user = $_SESSION['ss_user'];

include("../conex.php");


// cabeza 
       $sql_in = "SELECT * FROM salida";	
		$res_in = mysql_query($sql_in,$enlace);	
		$row=0;

		while (mysql_fetch_object ($res_in))
          {
		  $id_salida=mysql_result($res_in, $row, "id");
		  $id_sucursal=mysql_result($res_in, $row, "id_sucursal");
		  $txtndcto=mysql_result($res_in, $row, "num_dcto");
		  $id_bodega=mysql_result($res_in, $row, "bodega");
		  $rut=mysql_result($res_in, $row, "rut_cliente");
		  	$fecha = mysql_result($res_in, $row, "fecha_emision");
				  
			  
			 $txtobserva='';
			 
	//**************  pago de facturas *********
      $pago=0;	

if ($id_bodega==1)
     {
	 $sql_in_pago = "SELECT sum(Opr_monto2) as pago FROM pagos_arica where Opr_numero=$txtndcto";
      }
	  else
	  if ($id_bodega==2)
     {
	 $sql_in_pago = "SELECT sum(Opr_monto2) as pago FROM pagos_iquique where Opr_numero=$txtndcto";
      }
	  else
	  if ($id_bodega==3)
     {
	 $sql_in_pago = "SELECT sum(Opr_monto2) as pago FROM pagos_antofagasta where Opr_numero=$txtndcto";
      }

		  $res_in_pago = mysql_query($sql_in_pago,$enlace);		 
			
					if (mysql_fetch_object($res_in_pago))
 						{
						$pago = mysql_result($res_in_pago,0 , "pago");
						
						}			 
			 		

	
			$des = 'Pago Factura '.$txtndcto;			
	
	//********************* actualiza numero factura ************************	

$up_dcto = "UPDATE salida SET pagada = $pago WHERE id = $id_salida";
	$res_up_dcto = mysql_query($up_dcto,$enlace);	
	

	if ($pago!=0)
	{
	
	//********************* cta_cte
		 
		$csql="INSERT INTO pago_cliente (empresa, rut, num_salida, fecha, descripcion, valor, tipo, usuario, sucursal) 
			VALUES ($id_empresa, '$rut', $id_salida, '$fecha', '$des', $pago, 'A', $id_user, $id_sucursal)";
			
			$con = mysql_query($csql,$enlace); 
	}
		 	  
		  $row+=1;
	  }





	
		?>
<script>
	alert("Traspaso realizado con exito");
	window.top.open('','_parent','');window.top.close();
</script>
		
	
	
