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.