1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-20 16:31:33 +02:00
invoiceninja/app/Http/Controllers/ProductController.php

483 lines
17 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)
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
2019-05-11 05:32:07 +02:00
*
* @license https://opensource.org/licenses/AAL
*/
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;
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;
2019-04-03 11:54:27 +02:00
use App\Jobs\Entity\ActionEntity;
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;
use Illuminate\Http\Request;
class ProductController extends BaseController
{
use MakesHash;
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.
*/
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
*
*
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.
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",
* @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",
2019-10-07 08:31:26 +02:00
* @OA\Header(header="X-API-Version", ref="#/components/headers/X-API-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
*/
public function index(ProductFilters $filters)
{
$products = Product::filter($filters);
return $this->listResponse($products);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\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",
2019-10-07 08:31:26 +02:00
* @OA\Header(header="X-API-Version", ref="#/components/headers/X-API-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.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\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",
2019-10-07 08:31:26 +02:00
* @OA\Header(header="X-API-Version", ref="#/components/headers/X-API-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.
*
2019-04-03 11:54:27 +02:00
* @param Product $product
2019-04-03 02:09:22 +02:00
* @return \Illuminate\Http\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",
2019-10-07 08:31:26 +02:00
* @OA\Header(header="X-API-Version", ref="#/components/headers/X-API-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.
*
2019-04-03 11:54:27 +02:00
* @param Product $product
2019-04-03 02:09:22 +02:00
* @return \Illuminate\Http\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",
2019-10-07 08:31:26 +02:00
* @OA\Header(header="X-API-Version", ref="#/components/headers/X-API-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.
*
* @param \Illuminate\Http\Request $request
2019-04-03 11:54:27 +02:00
* @param Product $product
2019-04-03 02:09:22 +02:00
* @return \Illuminate\Http\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",
2019-10-07 08:31:26 +02:00
* @OA\Header(header="X-API-Version", ref="#/components/headers/X-API-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();
2019-04-03 05:22:13 +02:00
$product = $this->product_repo->save($request, $product);
return $this->itemResponse($product);
2019-04-03 02:09:22 +02:00
}
/**
* Remove the specified resource from storage.
*
2019-04-03 11:54:27 +02:00
* @param Product $product
2019-04-03 02:09:22 +02:00
* @return \Illuminate\Http\Response
2019-10-07 06:29:16 +02:00
*
*
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",
2019-10-07 08:31:26 +02:00
* @OA\Header(header="X-API-Version", ref="#/components/headers/X-API-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
*/
2019-04-03 11:54:27 +02:00
public function destroy(Product $product)
2019-04-03 02:09:22 +02:00
{
2019-04-03 11:54:27 +02:00
$product->delete();
return response()->json([], 200);
}
/**
* 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",
2019-10-07 08:31:26 +02:00
* @OA\Header(header="X-API-Version", ref="#/components/headers/X-API-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');
$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
}
}