app/Http/Controllers/Compras_OtController.php
/**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
Store a newly created resource in storage.
Store a newly created resource in storage .
public function store(Request $request)
    {
      $data= $request->all();
          $compras=$data['compras'];
          foreach ($compras as $compra) {
        $vl=$this->validatorCrearCompra($compra);
        if ($vl->fails())
        {
           return response([
              'status' => Response::HTTP_BAD_REQUEST,
              'response_time' => microtime(true) - LARAVEL_START,
              'msg' => 'Error al crear la Compra',
              'error' => 'ERR_01',
              'request' => $request->all(),
              'obj' =>$vl->errors()
              ],Response::HTTP_BAD_REQUEST);
        }else
        {
           try
           {
              DB::beginTransaction();
                 $model_compras= new Compras_Ot;
                 $model_compras->fill($compra);
                 $model_compras->creador_id=  Auth::guard('api')->user()->id;
                 $model_compras->compra_externa= 1;
                 $model_compras->save();
              DB::commit();
              return response([
                 'status' => Response::HTTP_OK,
                 'response_time' => microtime(true) - LARAVEL_START,
                 'msg' => 'Se han creado las compras correctamente !!',
                 'obj' =>  $model_compras
                 ],Response::HTTP_OK);
           }catch(Exception $e){
              DB::rollback();
              return response([
                 'status' => Response::HTTP_BAD_REQUEST,
                 'response_time' => microtime(true) - LARAVEL_START,
                 'error_creacion' => 'fallo_en_la_creacion',
                 'msg' => 'Error al actualizar la tarea. No se envio la notificación',
                 'error' => config('constants.ERR_04'),
                 'consola' =>$e->getMessage(),
                 'request' => $request->all()
                 ],Response::HTTP_BAD_REQUEST);
           }
        }
      }
    }
public function store ( Request $ request ) { $ data = $ request -> all ( ) ; $ compras = $ data [ 'compras' ] ; foreach ( $ compras as $ compra ) { $ vl = $ this -> validatorCrearCompra ( $ compra ) ; if ( $ vl -> fails ( ) ) { return response ( [ 'status' => Response :: HTTP_BAD_REQUEST , 'response_time' => microtime ( true ) - LARAVEL_START , 'msg' => 'Error al crear la Compra' , 'error' => 'ERR_01' , 'request' => $ request -> all ( ) , 'obj' => $ vl -> errors ( ) ] , Response :: HTTP_BAD_REQUEST ) ; } else { try { DB :: beginTransaction ( ) ; $ model_compras = new Compras_Ot ; $ model_compras -> fill ( $ compra ) ; $ model_compras -> creador_id = Auth :: guard ( 'api' ) -> user ( ) -> id ; $ model_compras -> compra_externa = 1 ; $ model_compras -> save ( ) ; DB :: commit ( ) ; return response ( [ 'status' => Response :: HTTP_OK , 'response_time' => microtime ( true ) - LARAVEL_START , 'msg' => 'Se han creado las compras correctamente !!' , 'obj' => $ model_compras ] , Response :: HTTP_OK ) ; } catch ( Exception $ e ) { DB :: rollback ( ) ; return response ( [ 'status' => Response :: HTTP_BAD_REQUEST , 'response_time' => microtime ( true ) - LARAVEL_START , 'error_creacion' => 'fallo_en_la_creacion' , 'msg' => 'Error al actualizar la tarea. No se envio la notificación', 'error' => config ( 'constants.ERR_04' ) , 'consola' => $ e -> getMessage ( ) , 'request' => $ request -> all ( ) ] , Response :: HTTP_BAD_REQUEST ) ; } } } }
Store a newly created resource in storage.
Store a newly created resource in storage .
//Validaciòn de las entradas por el metodo POST //Inicio una transacción por si falla algún ingreso no quede registro en ninguna tabla /*El siguiente for recorre el listado de compras y los agrega*/ //Mensaje a mostrar