Exemplo n.º 1
0
 /**
  * @param int             $optionId
  * @param PutStockRequest $request
  *
  * @throws \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
  *
  * @return \Illuminate\Http\RedirectResponse
  */
 public function putStock(int $optionId, PutStockRequest $request)
 {
     $option = $this->optionResource->with(['product', 'availableStock'])->where('id', '=', $optionId)->firstOrFail();
     if ($request->quantity() === count($option->availableStock)) {
         $this->webUi->infoMessage("Stock for `{$option->label}` was not changed.");
         return $this->redirectToShowProduct($option->product);
     }
     if ($request->quantity() > count($option->availableStock)) {
         $new = $this->increaseStock($request->quantity(), $option);
         $this->webUi->successMessage("Added {$new} stock item/s for `{$option->label}`.");
         return $this->redirectToShowProduct($option->product);
     }
     if ($request->quantity() < count($option->availableStock)) {
         $removed = $this->reduceStock($request->quantity(), $option);
         $this->webUi->successMessage("Removed {$removed} stock item/s from `{$option->label}`.");
         return $this->redirectToShowProduct($option->product);
     }
     throw new BadRequestHttpException('Failed to diff stock quantity.');
 }