1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 21:22:58 +01:00
invoiceninja/app/Http/Controllers/RecurringQuoteController.php
David Bomba ba75a44eb8
Laravel 7.x Shift (#40)
* Adopt Laravel coding style

The Laravel framework adopts the PSR-2 coding style with some additions.
Laravel apps *should* adopt this coding style as well.

However, Shift allows you to customize the adopted coding style by
adding your own [PHP CS Fixer][1] `.php_cs` config to your project.

You may use [Shift's .php_cs][2] file as a base.

[1]: https://github.com/FriendsOfPHP/PHP-CS-Fixer
[2]: https://gist.github.com/laravel-shift/cab527923ed2a109dda047b97d53c200

* Shift bindings

PHP 5.5.9+ adds the new static `class` property which provides the fully qualified class name. This is preferred over using class name strings as these references are checked by the parser.

* Shift core files

* Shift to Throwable

* Add laravel/ui dependency

* Unindent vendor mail templates

* Shift config files

* Default config files

In an effort to make upgrading the constantly changing config files
easier, Shift defaulted them so you can review the commit diff for
changes. Moving forward, you should use ENV variables or create a
separate config file to allow the core config files to remain
automatically upgradeable.

* Shift Laravel dependencies

* Shift cleanup

* Upgrade to Laravel 7

Co-authored-by: Laravel Shift <shift@laravelshift.com>
2020-09-06 19:38:10 +10:00

609 lines
24 KiB
PHP

<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Http\Controllers;
use App\Factory\CloneRecurringQuoteFactory;
use App\Factory\CloneRecurringQuoteToQuoteFactory;
use App\Factory\RecurringQuoteFactory;
use App\Filters\RecurringQuoteFilters;
use App\Http\Requests\RecurringQuote\ActionRecurringQuoteRequest;
use App\Http\Requests\RecurringQuote\CreateRecurringQuoteRequest;
use App\Http\Requests\RecurringQuote\DestroyRecurringQuoteRequest;
use App\Http\Requests\RecurringQuote\EditRecurringQuoteRequest;
use App\Http\Requests\RecurringQuote\ShowRecurringQuoteRequest;
use App\Http\Requests\RecurringQuote\StoreRecurringQuoteRequest;
use App\Http\Requests\RecurringQuote\UpdateRecurringQuoteRequest;
use App\Jobs\Entity\ActionEntity;
use App\Models\RecurringQuote;
use App\Repositories\BaseRepository;
use App\Repositories\RecurringQuoteRepository;
use App\Transformers\RecurringQuoteTransformer;
use App\Utils\Traits\MakesHash;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
/**
* Class RecurringQuoteController.
*/
class RecurringQuoteController extends BaseController
{
use MakesHash;
protected $entity_type = RecurringQuote::class;
protected $entity_transformer = RecurringQuoteTransformer::class;
/**
* @var RecurringQuoteRepository
*/
protected $recurring_quote_repo;
protected $base_repo;
/**
* RecurringQuoteController constructor.
*
* @param \App\Repositories\RecurringQuoteRepository $recurring_quote_repo The RecurringQuote repo
*/
public function __construct(RecurringQuoteRepository $recurring_quote_repo)
{
parent::__construct();
$this->recurring_quote_repo = $recurring_quote_repo;
}
/**
* Show the list of recurring_invoices.
*
* @param \App\Filters\RecurringQuoteFilters $filters The filters
*
* @return \Illuminate\Http\Response
*
*
* @OA\Get(
* path="/api/v1/recurring_quotes",
* operationId="getRecurringQuotes",
* tags={"recurring_quotes"},
* summary="Gets a list of recurring_quotes",
* description="Lists recurring_quotes, search and filters allow fine grained lists to be generated.
Query parameters can be added to performed more fine grained filtering of the recurring_quotes, these are handled by the RecurringQuoteFilters 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 recurring_quotes",
* @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/RecurringQuote"),
* ),
* @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 index(RecurringQuoteFilters $filters)
{
$recurring_quotes = RecurringQuote::filter($filters);
return $this->listResponse($recurring_quotes);
}
/**
* Show the form for creating a new resource.
*
* @param \App\Http\Requests\RecurringQuote\CreateRecurringQuoteRequest $request The request
*
* @return \Illuminate\Http\Response
*
*
* @OA\Get(
* path="/api/v1/recurring_quotes/create",
* operationId="getRecurringQuotesCreate",
* tags={"recurring_quotes"},
* summary="Gets a new blank RecurringQuote 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 RecurringQuote 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/RecurringQuote"),
* ),
* @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 create(CreateRecurringQuoteRequest $request)
{
$recurring_quote = RecurringQuoteFactory::create(auth()->user()->company()->id, auth()->user()->id);
return $this->itemResponse($recurring_quote);
}
/**
* Store a newly created resource in storage.
*
* @param \App\Http\Requests\RecurringQuote\StoreRecurringQuoteRequest $request The request
*
* @return \Illuminate\Http\Response
*
*
* @OA\Post(
* path="/api/v1/recurring_quotes",
* operationId="storeRecurringQuote",
* tags={"recurring_quotes"},
* summary="Adds a RecurringQuote",
* description="Adds an RecurringQuote 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 RecurringQuote 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/RecurringQuote"),
* ),
* @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 store(StoreRecurringQuoteRequest $request)
{
$recurring_quote = $this->recurring_quote_repo->save($request, RecurringQuoteFactory::create(auth()->user()->company()->id, auth()->user()->id));
return $this->itemResponse($recurring_quote);
}
/**
* Display the specified resource.
*
* @param \App\Http\Requests\RecurringQuote\ShowRecurringQuoteRequest $request The request
* @param \App\Models\RecurringQuote $recurring_quote The RecurringQuote
*
* @return \Illuminate\Http\Response
*
*
* @OA\Get(
* path="/api/v1/recurring_quotes/{id}",
* operationId="showRecurringQuote",
* tags={"recurring_quotes"},
* summary="Shows an RecurringQuote",
* description="Displays an RecurringQuote 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 RecurringQuote Hashed ID",
* example="D2J234DFA",
* required=true,
* @OA\Schema(
* type="string",
* format="string",
* ),
* ),
* @OA\Response(
* response=200,
* description="Returns the RecurringQuote 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/RecurringQuote"),
* ),
* @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 show(ShowRecurringQuoteRequest $request, RecurringQuote $recurring_quote)
{
return $this->itemResponse($recurring_quote);
}
/**
* Show the form for editing the specified resource.
*
* @param \App\Http\Requests\RecurringQuote\EditRecurringQuoteRequest $request The request
* @param \App\Models\RecurringQuote $recurring_quote The RecurringQuote
*
* @return \Illuminate\Http\Response
*
*
* @OA\Get(
* path="/api/v1/recurring_quotes/{id}/edit",
* operationId="editRecurringQuote",
* tags={"recurring_quotes"},
* summary="Shows an RecurringQuote for editting",
* description="Displays an RecurringQuote 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 RecurringQuote Hashed ID",
* example="D2J234DFA",
* required=true,
* @OA\Schema(
* type="string",
* format="string",
* ),
* ),
* @OA\Response(
* response=200,
* description="Returns the RecurringQuote 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/RecurringQuote"),
* ),
* @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 edit(EditRecurringQuoteRequest $request, RecurringQuote $recurring_quote)
{
return $this->itemResponse($recurring_quote);
}
/**
* Update the specified resource in storage.
*
* @param \App\Http\Requests\RecurringQuote\UpdateRecurringQuoteRequest $request The request
* @param \App\Models\RecurringQuote $recurring_quote The RecurringQuote
*
* @return \Illuminate\Http\Response
*
*
* @OA\Put(
* path="/api/v1/recurring_quotes/{id}",
* operationId="updateRecurringQuote",
* tags={"recurring_quotes"},
* summary="Updates an RecurringQuote",
* description="Handles the updating of an RecurringQuote 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 RecurringQuote Hashed ID",
* example="D2J234DFA",
* required=true,
* @OA\Schema(
* type="string",
* format="string",
* ),
* ),
* @OA\Response(
* response=200,
* description="Returns the RecurringQuote 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/RecurringQuote"),
* ),
* @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 update(UpdateRecurringQuoteRequest $request, RecurringQuote $recurring_quote)
{
if ($request->entityIsDeleted($recurring_quote)) {
return $request->disallowUpdate();
}
$recurring_quote = $this->recurring_quote_repo->save(request(), $recurring_quote);
return $this->itemResponse($recurring_quote);
}
/**
* Remove the specified resource from storage.
*
* @param \App\Http\Requests\RecurringQuote\DestroyRecurringQuoteRequest $request
* @param \App\Models\RecurringQuote $recurring_quote
*
* @return \Illuminate\Http\Response
*
*
* @OA\Delete(
* path="/api/v1/recurring_quotes/{id}",
* operationId="deleteRecurringQuote",
* tags={"recurring_quotes"},
* summary="Deletes a RecurringQuote",
* description="Handles the deletion of an RecurringQuote 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 RecurringQuote 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"),
* @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",
* description="Unexpected Error",
* @OA\JsonContent(ref="#/components/schemas/Error"),
* ),
* )
*/
public function destroy(DestroyRecurringQuoteRequest $request, RecurringQuote $recurring_quote)
{
$recurring_quote->delete();
return response()->json([], 200);
}
/**
* Perform bulk actions on the list view.
*
* @return Collection
*
*
* @OA\Post(
* path="/api/v1/recurring_quotes/bulk",
* operationId="bulkRecurringQuotes",
* tags={"recurring_quotes"},
* summary="Performs bulk actions on an array of recurring_quotes",
* 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(
* description="Hashed ids",
* 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,
* description="The RecurringQuote response",
* @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/RecurringQuote"),
* ),
* @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 bulk()
{
$action = request()->input('action');
$ids = request()->input('ids');
$recurring_quotes = RecurringQuote::withTrashed()->find($this->transformKeys($ids));
$recurring_quotes->each(function ($recurring_quote, $key) use ($action) {
if (auth()->user()->can('edit', $recurring_quote)) {
$this->recurring_quote_repo->{$action}($recurring_quote);
}
});
return $this->listResponse(RecurringQuote::withTrashed()->whereIn('id', $this->transformKeys($ids)));
}
/**
* Recurring Quote Actions.
*
*
*
* @OA\Get(
* path="/api/v1/recurring_quotes/{id}/{action}",
* operationId="actionRecurringQuote",
* tags={"recurring_quotes"},
* summary="Performs a custom action on an RecurringQuote",
* description="Performs a custom action on an RecurringQuote.
The current range of actions are as follows
- clone_to_RecurringQuote
- clone_to_quote
- history
- delivery_note
- mark_paid
- download
- archive
- delete
- email",
* @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 RecurringQuote Hashed ID",
* example="D2J234DFA",
* required=true,
* @OA\Schema(
* type="string",
* format="string",
* ),
* ),
* @OA\Parameter(
* name="action",
* in="path",
* description="The action string to be performed",
* example="clone_to_quote",
* required=true,
* @OA\Schema(
* type="string",
* format="string",
* ),
* ),
* @OA\Response(
* response=200,
* description="Returns the RecurringQuote 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/RecurringQuote"),
* ),
* @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 action(ActionRecurringQuoteRequest $request, RecurringQuote $recurring_quote, $action)
{
switch ($action) {
case 'clone_to_RecurringQuote':
// $recurring_invoice = CloneRecurringQuoteFactory::create($recurring_invoice, auth()->user()->id);
// return $this->itemResponse($recurring_invoice);
break;
case 'clone_to_quote':
// $quote = CloneRecurringQuoteToQuoteFactory::create($recurring_invoice, auth()->user()->id);
// todo build the quote transformer and return response here
break;
case 'history':
// code...
break;
case 'delivery_note':
// code...
break;
case 'mark_paid':
// code...
break;
case 'archive':
// code...
break;
case 'delete':
// code...
break;
case 'email':
//dispatch email to queue
break;
default:
// code...
break;
}
}
}