Ver Mensaje Individual
  #8 (permalink)  
Antiguo 01/10/2013, 21:14
mutant_x
 
Fecha de Ingreso: enero-2007
Mensajes: 58
Antigüedad: 17 años, 3 meses
Puntos: 3
Respuesta: Problema con un codigo PHP

listo resuelto el problema ya funciona pero tiene dos defectos
1 en el codigo yo concateno una frase de texto con el resultado de tres variables esto lo guardo en la variable $TiempoDeUso deberia guardar eu texto como este:
"En el inventario, El tiempo de uso es de: 16 años 2 meses 28 dias"
pero en lugar de eso guarda en la base de datos el valor 5155 """UN NUMERO"" no se por que...

2- el codigo lo puse dentro de un do while para que corra el codigo para todos los registros de la tabla activo..
empiesa bien pero se queda 30 segundos haciendolo y manda este mensaje:
Fatal error: Maximum execution time of 30 seconds exceeded in C:\wamp\www\SARA_WEB\pruebafechas.php on line113
supongo que si resuelvo el problema 1 entonces puedo poner este codigo en una funcion en la base de datos y hacer que se ejecute una vez al dia...(ESPERO QUE ESO SE PUEDA HACER EN MYSQL)

pego aqui el codigo ya corregidopra aquellos que son nuevos como yo principalmente y para todo aque que necesite un ejemplo de manejo de fechas, variables consultas y Update a la base de datos asi como el uso de operaciones aritmeticas.

basicamente lo que hace es cojer los datos de una tabla llamada activos y le calcula la depreciacion y el tiempo de uso del activo desde su fecha de compra hasta el momento, y despues actualiza la misma tabla con los nuevos valores

perdon si el codigo se desformatea al pegarlo pero no se como hacer para que se quede intacto

Código PHP:
Ver original
  1. <?php
  2. if (!function_exists("GetSQLValueString")) {
  3. function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
  4. {
  5.   if (PHP_VERSION < 6) {
  6.     $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  7.   }
  8.  
  9.   $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
  10.  
  11.   switch ($theType) {
  12.     case "text":
  13.       $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  14.       break;    
  15.     case "long":
  16.     case "int":
  17.       $theValue = ($theValue != "") ? intval($theValue) : "NULL";
  18.       break;
  19.     case "double":
  20.       $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
  21.       break;
  22.     case "date":
  23.       $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  24.       break;
  25.     case "defined":
  26.       $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
  27.       break;
  28.   }
  29.   return $theValue;
  30. }
  31. }
  32.  
  33. mysql_select_db($database_conexionsara, $conexionsara);
  34. $query_ConsultaActivos = "SELECT * FROM activos";
  35. $ConsultaActivos = mysql_query($query_ConsultaActivos, $conexionsara) or die(mysql_error());
  36. $row_ConsultaActivos = mysql_fetch_assoc($ConsultaActivos);
  37. $totalRows_ConsultaActivos = mysql_num_rows($ConsultaActivos);
  38.  
  39.     date_default_timezone_set("America/Panama");
  40.     /*$Porcentaje = $_POST["Porc"];*/
  41.     /*$ValorActual = $_POST["ValorActual"];*/
  42.     do{
  43.     $Porcentaje =$row_ConsultaActivos['PORCDEP'];
  44.     $ValorActual =$row_ConsultaActivos['VALORACTUAL'];
  45.     $Secuencia = $row_ConsultaActivos['SECUENCIA'];
  46.     $ValorAgregado = $row_ConsultaActivos['VALORAGREGADO'];
  47.     $Valor = $row_ConsultaActivos['VALOR'];
  48.     if (($Porcentaje)> 0){
  49.         $date = strtotime(date("y-m-d"));
  50.         $date1 = date("Y-m-d");
  51.         $AñoAct= date("Y", $date);
  52.         $MesAct= date("m", $date);
  53.         $DiaAct= date("d", $date);
  54.        
  55.         $date7 = strtotime($row_ConsultaActivos['FECHAADQUISICION']);
  56.         $AñoAdq= date("Y", $date7);
  57.         $MesAdq= date("m", $date7);
  58.         $DiaAdq= date("d", $date7);
  59.        
  60.         if(($DiaAct)< $DiaAdq){
  61.             $MesAct= $MesAct - 1;
  62.             $DiaAct = $DiaAct +30;
  63.         }
  64.         if(($MesAct)< $MesAdq){
  65.             $AñoAct= $AñoAct - 1;
  66.             $MesAct = $MesAct +12;
  67.         }
  68.        
  69.         $Dia = $DiaAct - $DiaAdq;
  70.         $Mes = $MesAct - $MesAdq;
  71.         $Año = $AñoAct - $AñoAdq;
  72.        
  73.         $TiempoDeUso = ( "En el inventario, El tiempo de uso es de: ". $Año ." años ".$Mes . " meses ". $Dia.  " dias ");
  74.         echo $TiempoDeUso;
  75.         echo $row_ConsultaActivos['SECUENCIA'];
  76.         $AñosEnDias = $Año * 365;
  77.         $MesesEnDias = $Mes * 30;
  78.         $TiempoDeUso = $AñosEnDias + $MesesEnDias + $Dia;
  79.     //==============================================
  80.     // SI EL VALOR ACTUAL ES MAYOR O IGUAL A 100
  81.     //==============================================       
  82.         if (($ValorActual)>= 100) {
  83.             $depacum = $row_ConsultaActivos['TOTALDEPRECIADO'];
  84.             $DepAnual=(($ValorActual * $Porcentaje)/100)* $Año;
  85.             $DepMen = ((($ValorActual * $Porcentaje)/100)/12)* $Mes;
  86.             $DepDia = (((($ValorActual * $Porcentaje)/100)/12)/30)* $Dia;
  87.             $DepTotal = $DepAnual + $DepMen + $DepDia;
  88.             $ValorReal = $ValorActual - $DepTotal;
  89.             $NewVal= $ValorActual - $DepTotal;
  90.             if (($NewVal)<= 1){
  91.                 $NewVal = 1;
  92.                 $TotalDepreciado = $ValorActual - 1;
  93.             }else{
  94.                 $TotalDepreciado = $DepTotal;
  95.             }
  96.             $RestaAcumulada = $depacum - $DepTotal;
  97.     //==============================================
  98.     // ACTUALIZACION LOS DATOS DE LA TABLA ACTIVOS
  99.     //==============================================
  100.       $updateSQL = sprintf("UPDATE activos SET DEPRECIACIONACUMULADA=%s, VALORREAL=%s, VALORDEPRECANUAL=%s, VALORDEPRECMENS=%s, VALORDEPRECDIARIO=%s,  FECHAACTUAL=%s, TOTALDEPRECIADO=%s, TIEMPOUSO=%s WHERE SECUENCIA=%s",
  101.                            GetSQLValueString($DepTotal, "double"),
  102.                            GetSQLValueString($NewVal, "double"),
  103.                            GetSQLValueString($DepAnual, "double"),
  104.                            GetSQLValueString($DepMen, "double"),
  105.                            GetSQLValueString($DepDia, "double"),
  106.                            GetSQLValueString(date("Y-m-d"), "date"),
  107.                            GetSQLValueString($TotalDepreciado, "double"),
  108.                            GetSQLValueString($TiempoDeUso, "text"),
  109.                            GetSQLValueString($Secuencia, "text"));
  110.       mysql_select_db($database_conexionsara, $conexionsara);
  111.       /*mysql_query("SET NAMES 'utf-8'");*/
  112.       $Result1 = mysql_query($updateSQL, $conexionsara) or die(mysql_error());
  113.      
  114.       if(($DepTotal)>$ValorAgregado){
  115.           $TotalDepreciado = ($Valor + $DepTotal)-1;
  116.             $updateSQL1 = sprintf("UPDATE activos SET TOTALDEPRECIADO=%s WHERE SECUENCIA=%s",
  117.                                        GetSQLValueString($TotalDepreciado, "double"),
  118.                                        GetSQLValueString($Secuencia, "text"));
  119.             mysql_select_db($database_conexionsara, $conexionsara);
  120.             mysql_query("SET NAMES 'utf-8'");
  121.             $Result1 = mysql_query($updateSQL1, $conexionsara) or die(mysql_error());
  122.       }
  123.      
  124.     if (($RestaAcumulada)<> 0){
  125.         $DepreciacionAcumulada = $depacum + $DepDia;
  126.             $updateSQL2 = sprintf("UPDATE activos SET DEPRECIACIONACUMULADA=%s WHERE SECUENCIA=%s",
  127.                                        GetSQLValueString($DepreciacionAcumulada, "double"),
  128.                                        GetSQLValueString($Secuencia, "text"));
  129.             mysql_select_db($database_conexionsara, $conexionsara);
  130.             mysql_query("SET NAMES 'utf-8'");
  131.             $Result1 = mysql_query($updateSQL2, $conexionsara) or die(mysql_error());      
  132.     }
  133.     //==============================================
  134.     // SI EL VALOR ACTUAL ES MENOR A 100
  135.     //==============================================       
  136.         }
  137.         if (($ValorActual)< 100) {         
  138.             $DepDia = $ValorActual / 365 ;
  139.             $DepTotal=$TiempoDeUso * $DepDia;
  140.             $NewVal = $ValorActual - $DepTotal;
  141.             if (($NewVal)<= 0){
  142.                 $NewVal = 1;
  143.                 $TotalDepreciado = $ValorActual - 1;
  144.             }else{
  145.                 $TotalDepreciado = $DepTotal;
  146.             }
  147.           $Fecha1MySQL = implode( '-', array_reverse( explode( '/', $date ) ) ) ;
  148.           $updateSQL = sprintf("UPDATE activos SET TOTALDEPRECIADO=%s, VALORREAL=%s,  VALORDEPRECDIARIO=%s, FECHAACTUAL=%s,  TIEMPOUSO=%s WHERE SECUENCIA=%s",
  149.                                GetSQLValueString($TotalDepreciado, "double"),
  150.                                GetSQLValueString($NewVal, "double"),
  151.                                GetSQLValueString($DepDia, "double"),
  152.                                GetSQLValueString($Fecha1MySQL, "date"),
  153.                                GetSQLValueString($TiempoDeUso, "text"),
  154.                                GetSQLValueString($Secuencia, "text"));
  155.           mysql_select_db($database_conexionsara, $conexionsara);
  156.           /*mysql_query("SET NAMES 'utf-8'");*/
  157.           $Result1 = mysql_query($updateSQL, $conexionsara) or die(mysql_error());         
  158.         }
  159.     }
  160. } while ($row_ConsultaActivos = mysql_fetch_assoc($ConsultaActivos));
  161.   $rows = mysql_num_rows($ConsultaOrigen);
  162.   if($rows > 0) {
  163.   mysql_data_seek($ConsultaActivos, 0);
  164. $row_ConsultaActivos = mysql_fetch_assoc($ConsultaActivos);
  165. }
  166.    
  167. ?>