<?php
 session_start();
 function nombreprog($codprog){
 require ("conecta.php");
  $sql="select * from m06004 where codigo='$codprog' ;";
  $res=mysql_query($sql,$con);
  if (mysql_num_rows($res)==0){
   return ("sin información");
   }else{
    while($fila=mysql_fetch_row($res)){
	 $nom=$fila[1];
	 }//while
	 return $nom;
	}//si 
 }//fin funcion
 function editafecha($fec){
  $fece=substr($fec,6,2) . "/" . substr($fec,4,2) . "/" . substr($fec,0,4);
  return $fece;
 }//fin editafecha
 function calculaped($num,$con){
  $neto=0;
  $sql="select * from adai01 where llave like '$num%';";
  $res=mysql_query($sql,$con) or die(mysql_error());
  while ($fila=mysql_fetch_row($res)){
   $st=$st + $fila[6];   
   }
   $sql2="select * from adae01 where numero='$num';";   
   //echo $sql2. "<br>";
   $res2=mysql_query($sql2,$con) or die(mysql_error());
   $cont=0; 
  while ($fila2=mysql_fetch_row($res2)){
   $cont++;
   $d1=$fila2[8];
   $d2=$fila2[9];
   $d3=$fila2[10];   
  }  
  if ($d1>0){
   $f1="0." . (100-$d1);
   }else{
   $f1=1;
  }//si 
  if ($d2>0){
   $f2="0." . (100-$d2);
   }else{
   $f2=1;
  }//si
  if ($d3>0){
   $f3="0." . (100-$d3);
   }else{
   $f3=1;
  }//si
 // echo $st . "<br>";  
  $neto=round($st*$f1*$f2*$f3);
  $iva=round($neto*0.19);
  $total=$neto + $iva;
  $sql="update adae01 set st1='$st',neto='$neto',iva='$iva',total='$total' where numero like '%$num%';";
  $res=mysql_query($sql,$con) or die(mysql_error());  
 }//fin calculaped 
 function codprov($interno,$rut,$con){
  $codprov="";
  $sql="select * from m01007 where interno='$interno' and rut_prov='$rut';";
  $result=mysql_query($sql,$con) or die(mysql_error());
  if (mysql_num_rows($result)>0){
   while ($fila=mysql_fetch_row($result)){
    $codprov=$fila[2];
   }//wend
  }
  return $codprov;
 }//fin codprov
 function ingcodprov($interno,$rut,$cp,$con){//****ingresa codigos de proveedor
  $sql="insert into m01007 (interno,rut_prov,cod_prov)
  values ('$interno','$rut','$cp');";
  $res=mysql_query($sql,$con) or die(mysql_error());
 }//fin ingcodprov
 
function editafechag($fec){
 $anno=substr($fec,6,4);
 $mes=substr($fec,3,2);
 $dia=substr($fec,0,2);
 $final=$anno . $mes .$dia;
 return $final;
} 
function codsql($cod)
  {
  $cad="";
  $car="";
  for ($i=0; $i<strlen($cod);$i++)
   {
    $car=substr($cod,$i,1);
	
	if (($car=="/") || ($car=="-") || ($car==" "))
	 {
	  $car="%";
	 }	 
	 if ($car=="+"){$car=" ";}	 	 
	 $cad=$cad  . $car;
	}// fin for
	//if (strlen($cad)>0)
	 //{
	  $cad=$cad . "%";
	 //} 
	return $cad;
  }// end function
function sacacomilla($des)
  {
  $cad="";
  $car="";
  for ($i=0; $i<strlen($des);$i++)
   {
    $car=substr($des,$i,1);
	
	if (($car=="\"") )
	 {
	  $car=" ";
	 } 	
	 
	 $cad=$cad  . $car;
	}// fin for
	//if (strlen($cad)>0)
	 //{	  
	 //} 
	return $cad;
  }// end function  
  function purificatxt($des)
  {
  $cad="";
  $car="";
  for ($i=0; $i<strlen($des);$i++)
   {
    $car=substr($des,$i,1);
	
	if (($car=="\"")  || ($car==".") || ($car=="-") )
	 {
	  $car="";
	 } 	
	 
	 $cad=$cad  . $car;
	}// fin for
	//if (strlen($cad)>0)
	 //{	  
	 //} 
	return $cad;
  }// end function  
function editarut($rut){
 $rut=purificatxt($rut);
 $lon=strlen($rut);
 $dig=substr($rut,$lon-1,1);
 $num=substr($rut,0,$lon-1); 
 $num=number_format($num,0,".",",") . "-" . $dig;
 return $num;  
} 
 ?>	
 


