예제 #1
0
 public function store()
 {
     try {
         if (Input::get('detalleorden') && Input::get('idcliente')) {
             DB::beginTransaction();
             $serieobj = Series::where('idsucursal', '=', Input::get('idsucursal'))->where('activa', '=', 1)->first();
             $empresa = Empresas::find(Session::get('idempresa'));
             $cliente = Clientes::find(Input::get('idcliente'));
             $registro = new Ventas();
             $registro->tipo = $serieobj->tipo;
             $registro->serie = $serieobj->serie;
             $registro->numero = $serieobj->actual;
             $registro->nitempresa = $empresa->nit;
             $registro->nitempresa = $empresa->nit;
             $registro->nitcliente = $cliente->nit;
             $registro->idcliente = $cliente->id;
             $registro->valor = Input::get('valor');
             $registro->iva = Input::get('totaliva');
             $registro->total = Input::get('total');
             $registro->idempresa = $empresa->id;
             $registro->idsucursal = Input::get('idsucursal');
             if ($registro->save()) {
                 $detalleordenver = json_decode(Input::get('detalleorden'));
                 foreach ($detalleordenver as &$productover) {
                     $detalleventa = new DetalleVentas();
                     $detalleventa->idventa = $registro->id;
                     $detalleventa->idproducto = $productover->idproducto;
                     $detalleventa->cantidad = $productover->cantidad;
                     $detalleventa->precio = $productover->precio;
                     $detalleventa->subtotal = $productover->subtotal;
                     $detalleventa->save();
                 }
                 DB::commit();
                 $respuesta['registros'] = $registro->toArray();
                 $respuesta['mensaje'] = 'Registro creado exitosamente';
                 $respuesta['resultado'] = true;
                 return $respuesta;
             } else {
                 DB::rollback();
                 $respuesta['registros'] = array();
                 $respuesta['mensaje'] = 'Error al crear el registro';
                 $respuesta['resultado'] = false;
                 return $respuesta;
             }
         } else {
             $respuesta['registros'] = array();
             $respuesta['mensaje'] = 'Todos los campos son requeridos';
             $respuesta['resultado'] = false;
             return $respuesta;
         }
     } catch (\Exception $e) {
         DB::rollback();
         $respuesta['registros'] = array();
         $respuesta['mensaje'] = 'Error general: ' . $e;
         $respuesta['resultado'] = false;
         return $respuesta;
     }
 }
예제 #2
0
 public function app()
 {
     // Get RB sites that are not USGS
     // (USGS now removed in DataController sitesUpdate)
     $sites = Site::where('sitecode', 'LIKE', '%RB_%')->get();
     foreach ($sites as $site) {
         $site->series = Series::where('sitecode', '=', $site->sitecode)->get();
     }
     return view('pages.rbc', compact('sites'));
 }
예제 #3
0
 public function dataUpdate($sitecode, $variablecode, $silent = false)
 {
     error_reporting(E_ALL);
     ini_set('display_errors', 1);
     $filename = "{$sitecode}.{$variablecode}.json";
     $filepath = storage_path("data/{$filename}");
     // Get basic URL
     $where = ['sitecode' => $sitecode, 'variablecode' => $variablecode];
     $series = Series::where($where)->first();
     if (is_null($series)) {
         return;
     }
     $getdataURL = $series->getdataurl;
     // Parse the URL
     $parsedURL = parse_url($getdataURL);
     // Parse the query portion of the url
     parse_str($parsedURL['query'], $query);
     // startDate: the most recent timestamp+1 or empty
     $lastline = $this->lastJSONArray($filepath);
     $lastTimestamp = 0;
     if ($lastline != '') {
         $json = json_decode(trim($lastline, ','));
         $lastTimestamp = Carbon::createFromTimeStamp($json[0], "MST");
         // Account for 7 hour offset + 1 second so we don't get this record again
         $lastTimestamp->addSecond();
         $query['startDate'] = $lastTimestamp->format('Y-m-d\\TH:i:s');
         $trimcomma = false;
     } else {
         // This is an empty file. Plan to remove leading comma.
         $this->tepln(function () {
             return "File is empty";
         }, $silent);
         $trimcomma = true;
     }
     /*
     	    // endDate: now
     		$query['endDate'] = Carbon::now()->setTimezone('UTC')->format('Y-m-dTh:i:s');
     */
     // Rebuild the URL
     $parsedURL['query'] = http_build_query($query);
     $url = $parsedURL['scheme'] . "://" . $parsedURL['host'] . $parsedURL['path'] . "?" . $parsedURL['query'];
     // Get the XML
     $this->tepln(function () use(&$xml, $url, $query) {
         $xml = simplexml_load_file($url);
         return "downloaded XML with " . count($xml->timeSeries->values->value) . " values from '" . $query['startDate'] . "' until now";
     }, $silent);
     // Process XML
     $newdatastring = "";
     $startDate = $query['startDate'];
     $this->tepln(function () use(&$newdatastring, $xml, $lastTimestamp) {
         // Bad data looks like
         $noValue = $xml->timeSeries->variable->noDataValue;
         // Iterate through all data, ignoring bad values
         foreach ($xml->timeSeries->values->value as $value) {
             // Only add 'valid' values
             if ((string) $value != $noValue) {
                 $time = $value->attributes()->dateTimeUTC;
                 $time = Carbon::parse($time)->timestamp;
                 $value = (string) $value;
                 $newdatastring .= ",[{$time},{$value}]";
             }
         }
         return "processed new values";
     }, $silent);
     // Save processed data
     $this->tepln(function () use($filepath, $newdatastring, $trimcomma) {
         if ($trimcomma) {
             $newdatastring = trim($newdatastring, ',');
         }
         file_put_contents($filepath, $newdatastring, FILE_APPEND | LOCK_EX);
         return 'appended to existing data file';
     }, $silent);
     // Redirect
     //return redirect()->back();
     return;
 }