1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-16 08:02:40 +01:00
invoiceninja/app/Http/Controllers/ProductController.php

548 lines
20 KiB
PHP
Raw Normal View History

2019-04-03 02:09:22 +02:00
<?php
2019-05-11 05:32:07 +02:00
/**
* Invoice Ninja (https://invoiceninja.com).
2019-05-11 05:32:07 +02:00
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
2019-05-11 05:32:07 +02:00
*
2021-06-16 08:58:16 +02:00
* @license https://www.elastic.co/licensing/elastic-license
2019-05-11 05:32:07 +02:00
*/
2019-04-03 02:09:22 +02:00
namespace App\Http\Controllers;
2019-04-03 04:34:28 +02:00
use App\Factory\ProductFactory;
2019-04-03 02:09:22 +02:00
use App\Filters\ProductFilters;
2019-04-03 04:34:28 +02:00
use App\Http\Requests\Product\CreateProductRequest;
use App\Http\Requests\Product\DestroyProductRequest;
2019-04-03 03:17:21 +02:00
use App\Http\Requests\Product\EditProductRequest;
2019-04-03 04:34:28 +02:00
use App\Http\Requests\Product\ShowProductRequest;
use App\Http\Requests\Product\StoreProductRequest;
2019-04-03 05:22:13 +02:00
use App\Http\Requests\Product\UpdateProductRequest;
2021-02-15 21:58:19 +01:00
use App\Http\Requests\Product\UploadProductRequest;
2021-03-07 22:32:38 +01:00
use App\Models\Account;
2019-04-03 02:09:22 +02:00
use App\Models\Product;
2019-04-03 04:34:28 +02:00
use App\Repositories\ProductRepository;
2019-04-03 02:09:22 +02:00
use App\Transformers\ProductTransformer;
use App\Utils\Traits\MakesHash;
2021-02-15 21:58:19 +01:00
use App\Utils\Traits\SavesDocuments;
2019-04-03 02:09:22 +02:00
use Illuminate\Http\Request;
2020-10-28 11:10:49 +01:00
use Illuminate\Http\Response;
2019-04-03 02:09:22 +02:00
class ProductController extends BaseController
{
use MakesHash;
2021-02-15 21:58:19 +01:00
use SavesDocuments;
2019-04-03 02:09:22 +02:00
2019-04-03 04:34:28 +02:00
protected $entity_type = Product::class;
protected $entity_transformer = ProductTransformer::class;
2019-04-03 02:09:22 +02:00
2019-04-03 04:34:28 +02:00
protected $product_repo;
2019-04-03 02:09:22 +02:00
/**
* ProductController constructor.
2020-10-28 11:10:49 +01:00
* @param ProductRepository $product_repo
*/
2019-04-03 04:34:28 +02:00
public function __construct(ProductRepository $product_repo)
2019-04-03 03:17:21 +02:00
{
parent::__construct();
2019-04-03 04:34:28 +02:00
$this->product_repo = $product_repo;
2019-04-03 03:17:21 +02:00
}
2019-04-03 02:09:22 +02:00
/**
2019-10-07 06:29:16 +02:00
* @OA\Get(
* path="/api/v1/products",
* operationId="getProducts",
* tags={"products"},
* summary="Gets a list of products",
* description="Lists products, search and filters allow fine grained lists to be generated.
2020-10-28 11:10:49 +01:00
Query parameters can be added to performed more fine grained filtering of the products, these are handled by the ProductFilters class which defines the methods available",
2019-10-07 06:29:16 +02:00
* @OA\Parameter(ref="#/components/parameters/X-Api-Secret"),
* @OA\Parameter(ref="#/components/parameters/X-Api-Token"),
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\Parameter(ref="#/components/parameters/include"),
* @OA\Response(
* response=200,
* description="A list of products",
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
2019-10-07 06:29:16 +02:00
* @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"),
* @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"),
* @OA\JsonContent(ref="#/components/schemas/Product"),
* ),
* @OA\Response(
* response=422,
* description="Validation error",
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
* ),
* @OA\Response(
* response="default",
2019-10-07 06:29:16 +02:00
* description="Unexpected Error",
* @OA\JsonContent(ref="#/components/schemas/Error"),
* ),
* )
2020-10-28 11:10:49 +01:00
* @param ProductFilters $filters
* @return Response|mixed
2019-04-03 02:09:22 +02:00
*/
public function index(ProductFilters $filters)
{
$products = Product::filter($filters);
2019-04-03 02:09:22 +02:00
return $this->listResponse($products);
}
/**
* Show the form for creating a new resource.
*
2020-10-28 11:10:49 +01:00
* @param CreateProductRequest $request
* @return Response
2019-10-07 06:29:16 +02:00
*
*
*
2019-10-07 06:29:16 +02:00
* @OA\Get(
* path="/api/v1/products/create",
* operationId="getProductsCreate",
* tags={"products"},
* summary="Gets a new blank Product object",
* description="Returns a blank object with default values",
* @OA\Parameter(ref="#/components/parameters/X-Api-Secret"),
* @OA\Parameter(ref="#/components/parameters/X-Api-Token"),
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\Parameter(ref="#/components/parameters/include"),
* @OA\Response(
* response=200,
* description="A blank Product object",
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
2019-10-07 06:29:16 +02:00
* @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"),
* @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"),
* @OA\JsonContent(ref="#/components/schemas/Product"),
* ),
* @OA\Response(
* response=422,
* description="Validation error",
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
*
* ),
* @OA\Response(
* response="default",
2019-10-07 06:29:16 +02:00
* description="Unexpected Error",
* @OA\JsonContent(ref="#/components/schemas/Error"),
* ),
* )
2019-04-03 02:09:22 +02:00
*/
2019-04-03 04:34:28 +02:00
public function create(CreateProductRequest $request)
2019-04-03 02:09:22 +02:00
{
2019-04-03 04:34:28 +02:00
$product = ProductFactory::create(auth()->user()->company()->id, auth()->user()->id);
return $this->itemResponse($product);
2019-04-03 02:09:22 +02:00
}
/**
* Store a newly created resource in storage.
*
2020-10-28 11:10:49 +01:00
* @param StoreProductRequest $request
* @return Response
2019-10-07 06:29:16 +02:00
*
*
*
* @OA\Post(
* path="/api/v1/products",
* operationId="storeProduct",
* tags={"products"},
* summary="Adds a Product",
* description="Adds an Product to the system",
* @OA\Parameter(ref="#/components/parameters/X-Api-Secret"),
* @OA\Parameter(ref="#/components/parameters/X-Api-Token"),
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\Parameter(ref="#/components/parameters/include"),
* @OA\Response(
* response=200,
* description="Returns the saved Product object",
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
2019-10-07 06:29:16 +02:00
* @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"),
* @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"),
* @OA\JsonContent(ref="#/components/schemas/Product"),
* ),
* @OA\Response(
* response=422,
* description="Validation error",
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
*
* ),
* @OA\Response(
* response="default",
2019-10-07 06:29:16 +02:00
* description="Unexpected Error",
* @OA\JsonContent(ref="#/components/schemas/Error"),
* ),
* )
2019-04-03 02:09:22 +02:00
*/
2019-04-03 04:34:28 +02:00
public function store(StoreProductRequest $request)
2019-04-03 02:09:22 +02:00
{
$product = $this->product_repo->save($request->all(), ProductFactory::create(auth()->user()->company()->id, auth()->user()->id));
2019-04-03 04:34:28 +02:00
return $this->itemResponse($product);
2019-04-03 02:09:22 +02:00
}
/**
* Display the specified resource.
*
2020-10-28 11:10:49 +01:00
* @param ShowProductRequest $request
* @param Product $product
* @return Response
2019-10-07 06:29:16 +02:00
*
*
* @OA\Get(
* path="/api/v1/products/{id}",
* operationId="showProduct",
* tags={"products"},
* summary="Shows an Product",
* description="Displays an Product by id",
* @OA\Parameter(ref="#/components/parameters/X-Api-Secret"),
* @OA\Parameter(ref="#/components/parameters/X-Api-Token"),
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\Parameter(ref="#/components/parameters/include"),
* @OA\Parameter(
* name="id",
* in="path",
* description="The Product Hashed ID",
* example="D2J234DFA",
* required=true,
* @OA\Schema(
* type="string",
* format="string",
* ),
* ),
* @OA\Response(
* response=200,
* description="Returns the Product object",
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
2019-10-07 06:29:16 +02:00
* @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"),
* @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"),
* @OA\JsonContent(ref="#/components/schemas/Product"),
* ),
* @OA\Response(
* response=422,
* description="Validation error",
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
*
* ),
* @OA\Response(
* response="default",
2019-10-07 06:29:16 +02:00
* description="Unexpected Error",
* @OA\JsonContent(ref="#/components/schemas/Error"),
* ),
* )
2019-04-03 02:09:22 +02:00
*/
2019-04-03 03:17:21 +02:00
public function show(ShowProductRequest $request, Product $product)
2019-04-03 02:09:22 +02:00
{
2019-04-03 03:17:21 +02:00
return $this->itemResponse($product);
2019-04-03 02:09:22 +02:00
}
/**
* Show the form for editing the specified resource.
*
2020-10-28 11:10:49 +01:00
* @param EditProductRequest $request
* @param Product $product
* @return Response
*
2019-10-07 06:29:16 +02:00
* @OA\Get(
* path="/api/v1/products/{id}/edit",
* operationId="editProduct",
* tags={"products"},
* summary="Shows an Product for editting",
* description="Displays an Product by id",
* @OA\Parameter(ref="#/components/parameters/X-Api-Secret"),
* @OA\Parameter(ref="#/components/parameters/X-Api-Token"),
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\Parameter(ref="#/components/parameters/include"),
* @OA\Parameter(
* name="id",
* in="path",
* description="The Product Hashed ID",
* example="D2J234DFA",
* required=true,
* @OA\Schema(
* type="string",
* format="string",
* ),
* ),
* @OA\Response(
* response=200,
* description="Returns the Product object",
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
2019-10-07 06:29:16 +02:00
* @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"),
* @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"),
* @OA\JsonContent(ref="#/components/schemas/Product"),
* ),
* @OA\Response(
* response=422,
* description="Validation error",
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
*
* ),
* @OA\Response(
* response="default",
2019-10-07 06:29:16 +02:00
* description="Unexpected Error",
* @OA\JsonContent(ref="#/components/schemas/Error"),
* ),
* )
2019-04-03 02:09:22 +02:00
*/
2019-04-03 03:17:21 +02:00
public function edit(EditProductRequest $request, Product $product)
2019-04-03 02:09:22 +02:00
{
2019-04-03 03:17:21 +02:00
return $this->itemResponse($product);
2019-04-03 02:09:22 +02:00
}
/**
* Update the specified resource in storage.
*
2020-10-28 11:10:49 +01:00
* @param UpdateProductRequest $request
* @param Product $product
* @return Response
2019-10-07 06:29:16 +02:00
*
*
2019-10-07 06:29:16 +02:00
* @OA\Put(
* path="/api/v1/products/{id}",
* operationId="updateProduct",
* tags={"products"},
* summary="Updates an Product",
* description="Handles the updating of an Product by id",
* @OA\Parameter(ref="#/components/parameters/X-Api-Secret"),
* @OA\Parameter(ref="#/components/parameters/X-Api-Token"),
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\Parameter(ref="#/components/parameters/include"),
* @OA\Parameter(
* name="id",
* in="path",
* description="The Product Hashed ID",
* example="D2J234DFA",
* required=true,
* @OA\Schema(
* type="string",
* format="string",
* ),
* ),
* @OA\Response(
* response=200,
* description="Returns the Product object",
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
2019-10-07 06:29:16 +02:00
* @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"),
* @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"),
* @OA\JsonContent(ref="#/components/schemas/Product"),
* ),
* @OA\Response(
* response=422,
* description="Validation error",
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
*
* ),
* @OA\Response(
* response="default",
2019-10-07 06:29:16 +02:00
* description="Unexpected Error",
* @OA\JsonContent(ref="#/components/schemas/Error"),
* ),
* )
*/
2019-04-03 05:22:13 +02:00
public function update(UpdateProductRequest $request, Product $product)
2019-04-03 02:09:22 +02:00
{
if ($request->entityIsDeleted($product)) {
return $request->disallowUpdate();
}
2020-01-15 10:43:40 +01:00
$product = $this->product_repo->save($request->all(), $product);
2019-04-03 05:22:13 +02:00
return $this->itemResponse($product);
2019-04-03 02:09:22 +02:00
}
/**
* Remove the specified resource from storage.
*
2020-10-28 11:10:49 +01:00
* @param DestroyProductRequest $request
* @param Product $product
* @return Response
2019-10-07 06:29:16 +02:00
*
*
2020-10-28 11:10:49 +01:00
* @throws \Exception
2019-10-07 06:29:16 +02:00
* @OA\Delete(
* path="/api/v1/products/{id}",
* operationId="deleteProduct",
* tags={"products"},
* summary="Deletes a Product",
* description="Handles the deletion of an Product by id",
* @OA\Parameter(ref="#/components/parameters/X-Api-Secret"),
* @OA\Parameter(ref="#/components/parameters/X-Api-Token"),
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\Parameter(ref="#/components/parameters/include"),
* @OA\Parameter(
* name="id",
* in="path",
* description="The Product Hashed ID",
* example="D2J234DFA",
* required=true,
* @OA\Schema(
* type="string",
* format="string",
* ),
* ),
* @OA\Response(
* response=200,
* description="Returns a HTTP status",
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
2019-10-07 06:29:16 +02:00
* @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"),
* @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"),
* ),
* @OA\Response(
* response=422,
* description="Validation error",
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
*
* ),
* @OA\Response(
* response="default",
2019-10-07 06:29:16 +02:00
* description="Unexpected Error",
* @OA\JsonContent(ref="#/components/schemas/Error"),
* ),
* )
2019-04-03 02:09:22 +02:00
*/
public function destroy(DestroyProductRequest $request, Product $product)
2019-04-03 02:09:22 +02:00
{
2021-02-22 23:47:54 +01:00
$this->product_repo->delete($product);
2019-04-03 11:54:27 +02:00
2021-02-22 23:47:54 +01:00
return $this->itemResponse($product->fresh());
2019-04-03 11:54:27 +02:00
}
/**
* Perform bulk actions on the list view.
*
2019-04-03 11:54:27 +02:00
* @return Collection
2019-10-07 06:29:16 +02:00
*
*
2019-10-07 06:29:16 +02:00
* @OA\Post(
* path="/api/v1/products/bulk",
* operationId="bulkProducts",
* tags={"products"},
* summary="Performs bulk actions on an array of products",
* description="",
* @OA\Parameter(ref="#/components/parameters/X-Api-Secret"),
* @OA\Parameter(ref="#/components/parameters/X-Api-Token"),
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\Parameter(ref="#/components/parameters/index"),
* @OA\RequestBody(
2019-10-07 06:57:14 +02:00
* description="Hashed IDs",
2019-10-07 06:29:16 +02:00
* required=true,
* @OA\MediaType(
* mediaType="application/json",
* @OA\Schema(
* type="array",
* @OA\Items(
* type="integer",
* description="Array of hashed IDs to be bulk 'actioned",
* example="[0,1,2,3]",
* ),
* )
* )
* ),
* @OA\Response(
* response=200,
2019-10-07 06:57:14 +02:00
* description="The Product response",
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
2019-10-07 06:29:16 +02:00
* @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"),
* @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"),
2019-10-07 06:57:14 +02:00
* @OA\JsonContent(ref="#/components/schemas/Product"),
2019-10-07 06:29:16 +02:00
* ),
* @OA\Response(
* response=422,
* description="Validation error",
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
* ),
* @OA\Response(
* response="default",
2019-10-07 06:29:16 +02:00
* description="Unexpected Error",
* @OA\JsonContent(ref="#/components/schemas/Error"),
* ),
* )
2019-04-03 11:54:27 +02:00
*/
public function bulk()
{
$action = request()->input('action');
2019-04-03 11:54:27 +02:00
$ids = request()->input('ids');
$products = Product::withTrashed()->find($this->transformKeys($ids));
2019-04-03 11:54:27 +02:00
$products->each(function ($product, $key) use ($action) {
if (auth()->user()->can('edit', $product)) {
$this->product_repo->{$action}($product);
}
2019-04-03 11:54:27 +02:00
});
return $this->listResponse(Product::withTrashed()->whereIn('id', $this->transformKeys($ids)));
2019-04-03 02:09:22 +02:00
}
2021-02-15 21:58:19 +01:00
/**
* Update the specified resource in storage.
*
* @param UploadProductRequest $request
* @param Product $product
* @return Response
*
*
*
* @OA\Put(
* path="/api/v1/products/{id}/upload",
* operationId="uploadProduct",
* tags={"products"},
* summary="Uploads a document to a product",
* description="Handles the uploading of a document to a product",
* @OA\Parameter(ref="#/components/parameters/X-Api-Secret"),
* @OA\Parameter(ref="#/components/parameters/X-Api-Token"),
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\Parameter(ref="#/components/parameters/include"),
* @OA\Parameter(
* name="id",
* in="path",
* description="The Product Hashed ID",
* example="D2J234DFA",
* required=true,
* @OA\Schema(
* type="string",
* format="string",
* ),
* ),
* @OA\Response(
* response=200,
* description="Returns the Product object",
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
* @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"),
* @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"),
* @OA\JsonContent(ref="#/components/schemas/Product"),
* ),
* @OA\Response(
* response=422,
* description="Validation error",
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
*
* ),
* @OA\Response(
* response="default",
* description="Unexpected Error",
* @OA\JsonContent(ref="#/components/schemas/Error"),
* ),
* )
*/
public function upload(UploadProductRequest $request, Product $product)
{
2021-03-07 22:32:38 +01:00
if(!$this->checkFeature(Account::FEATURE_DOCUMENTS))
return $this->featureFailure();
2021-02-15 21:58:19 +01:00
if ($request->has('documents'))
$this->saveDocuments($request->file('documents'), $product);
return $this->itemResponse($product->fresh());
}
2019-04-03 02:09:22 +02:00
}