コード例 #1
0
 public function actualizar($id)
 {
     DB::beginTransaction();
     try {
         $data = Input::all();
         $new = $data;
         unset($new['apikey']);
         unset($new['session_key']);
         $items = array();
         $new = array_map(function ($n) {
             return $n == 'NULL' ? NULL : $n;
         }, $new);
         if (isset($new["items"])) {
             foreach ($new["items"] as $i => $item) {
                 $items[$i] = array_map(function ($n) {
                     return $n == 'NULL' ? NULL : $n;
                 }, $item);
             }
             unset($new["items"]);
         }
         $modelo_presu = new Presupuesto();
         $presu = $modelo_presu->find($id);
         $presu->fill($new);
         if ($presu->save()) {
             $this->eventoAuditar($presu);
             if (count($items)) {
                 $lineas = $presu->lineas()->get();
                 foreach ($lineas as $l) {
                     $l->delete();
                 }
                 foreach ($items as $item) {
                     $presu_lin = new PresupuestoLinea();
                     $item['presupuesto_id'] = $presu->id;
                     $p_lin = $presu_lin->create($item);
                     if (!$p_lin->save()) {
                         DB::rollback();
                         return Response::json(array('error' => true, 'mensaje' => HerramientasController::getErrores($p_lin->validator), 'listado' => $data), 200);
                     }
                 }
             }
             DB::commit();
             return Response::json(array('error' => false, 'listado' => $presu->where('id', '=', $presu->id)->get()->toArray()), 200);
         } else {
             DB::rollback();
             return Response::json(array('error' => true, 'mensaje' => HerramientasController::getErrores($presu->validator), 'listado' => $data), 200);
         }
     } catch (\Exception $e) {
         DB::rollback();
         return Response::json(array('error' => true, 'mensaje' => $e->getMessage()), 200);
     }
 }