-1

I have this function:

public function referenciaDuplicada($referencia , $compra_id = null){
    if( $compra_id ){
        return Compra::where('referencia',$referencia)->where('id','!=',$compra_id)->count();
    }
    return Compra::where('referencia',$referencia)->count();
}

This is to prevent the user from create a Compra with duplicities on 'referencia' value.

I'm using it here:

public function update(UpdateRequest $request){

    $subotal_articulos = $this->subotalArticulos($request->articulos);
    $referencia_duplicada = $this->referenciaDuplicada($request->orden_compra['referencia'],$request->orden_compra['po_id']);
    $remision_duplicada = $this->remisionDuplicada($request->orden_compra['remision'],$request->orden_compra['po_id']);

    if( $referencia_duplicada > 0 ){
        return response()->json(['errors' =>
            ['El número de Referencia ya existe. Por favor, ingrese una nueva Referencia.'],
            'data' => null
        ], 422);
    }

Currently the user is creating 'Compra', but if Compra is cancelled you should be able to use the 'referencia' value because Compra was deleted. However, even thouh Compra was cancelled, 'referencia' used for that Compra cannot be used anymore.

I want to add a condition to validate that, when Compra 'estatus' value is 'aprobado', the user could not continue any further and throw the error above.

Rodri6uez
  • 399
  • 1
  • 5
  • 14
  • just add the condition in `referenciaDuplicada` query builder – N69S Aug 14 '20 at 17:53
  • Yeah but how that cpndition should be typed? I´m trying to use whereNotIn but I do not know where to write it. it should be: whereNotIn('estatus' , ['rechazada' , 'cancelada']) – Rodri6uez Aug 14 '20 at 18:28
  • Does this answer your question? [Laravel Eloquent "WHERE NOT IN"](https://stackoverflow.com/questions/25849015/laravel-eloquent-where-not-in) – N69S Aug 14 '20 at 22:10

1 Answers1

0

The solution was to add a condition for referenciaDuplicada .

Rodri6uez
  • 399
  • 1
  • 5
  • 14