2020-01-30 02:27:22 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
use App\Events\Credit\CreditWasCreated;
|
|
|
|
use App\Events\Credit\CreditWasUpdated;
|
|
|
|
use App\Factory\CloneCreditFactory;
|
|
|
|
use App\Factory\CloneCreditToQuoteFactory;
|
|
|
|
use App\Factory\CreditFactory;
|
|
|
|
use App\Filters\CreditFilters;
|
|
|
|
use App\Http\Requests\Credit\ActionCreditRequest;
|
|
|
|
use App\Http\Requests\Credit\CreateCreditRequest;
|
|
|
|
use App\Http\Requests\Credit\DestroyCreditRequest;
|
|
|
|
use App\Http\Requests\Credit\EditCreditRequest;
|
|
|
|
use App\Http\Requests\Credit\ShowCreditRequest;
|
|
|
|
use App\Http\Requests\Credit\StoreCreditRequest;
|
|
|
|
use App\Http\Requests\Credit\UpdateCreditRequest;
|
|
|
|
use App\Jobs\Credit\StoreCredit;
|
|
|
|
use App\Jobs\Invoice\EmailCredit;
|
|
|
|
use App\Jobs\Invoice\MarkInvoicePaid;
|
2020-03-03 10:44:26 +01:00
|
|
|
use App\Models\Client;
|
2020-01-30 02:27:22 +01:00
|
|
|
use App\Models\Credit;
|
|
|
|
use App\Repositories\CreditRepository;
|
|
|
|
use App\Transformers\CreditTransformer;
|
2020-07-08 14:02:16 +02:00
|
|
|
use App\Utils\Ninja;
|
2020-04-16 10:41:25 +02:00
|
|
|
use App\Utils\TempFile;
|
2020-01-30 02:27:22 +01:00
|
|
|
use App\Utils\Traits\MakesHash;
|
|
|
|
|
2020-03-03 10:44:26 +01:00
|
|
|
/**
|
|
|
|
* Class CreditController
|
|
|
|
* @package App\Http\Controllers\CreditController
|
|
|
|
*/
|
|
|
|
|
2020-01-30 02:27:22 +01:00
|
|
|
class CreditController extends BaseController
|
|
|
|
{
|
|
|
|
use MakesHash;
|
|
|
|
|
|
|
|
protected $entity_type = Credit::class;
|
|
|
|
|
|
|
|
protected $entity_transformer = CreditTransformer::class;
|
|
|
|
|
|
|
|
protected $credit_repository;
|
|
|
|
|
|
|
|
public function __construct(CreditRepository $credit_repository)
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
|
|
|
|
$this->credit_repository = $credit_repository;
|
|
|
|
}
|
|
|
|
|
2020-03-03 10:44:26 +01:00
|
|
|
/**
|
|
|
|
* Show the list of Credits
|
|
|
|
*
|
|
|
|
* @param \App\Filters\CreditFilters $filters The filters
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*
|
|
|
|
* @OA\Get(
|
|
|
|
* path="/api/v1/credits",
|
|
|
|
* operationId="getCredits",
|
|
|
|
* tags={"invoices"},
|
|
|
|
* summary="Gets a list of credits",
|
|
|
|
* description="Lists credits, search and filters allow fine grained lists to be generated.
|
|
|
|
*
|
|
|
|
* Query parameters can be added to performed more fine grained filtering of the credits, these are handled by the CreditFilters 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 credits",
|
2020-06-21 23:30:25 +02:00
|
|
|
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
|
2020-03-03 10:44:26 +01: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/Credit"),
|
|
|
|
* ),
|
|
|
|
* @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"),
|
|
|
|
* ),
|
|
|
|
* )
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2020-01-30 02:27:22 +01:00
|
|
|
public function index(CreditFilters $filters)
|
|
|
|
{
|
|
|
|
$credits = Credit::filter($filters);
|
|
|
|
|
|
|
|
return $this->listResponse($credits);
|
|
|
|
}
|
|
|
|
|
2020-03-03 10:44:26 +01:00
|
|
|
/**
|
|
|
|
* Show the form for creating a new resource.
|
|
|
|
*
|
|
|
|
* @param \App\Http\Requests\Credit\CreateCreditRequest $request The request
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @OA\Get(
|
|
|
|
* path="/api/v1/credits/create",
|
|
|
|
* operationId="getCreditsCreate",
|
|
|
|
* tags={"credits"},
|
|
|
|
* summary="Gets a new blank credit 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 credit object",
|
2020-06-21 23:30:25 +02:00
|
|
|
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
|
2020-03-03 10:44:26 +01: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/Credit"),
|
|
|
|
* ),
|
|
|
|
* @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"),
|
|
|
|
* ),
|
|
|
|
* )
|
|
|
|
*
|
|
|
|
*/
|
2020-01-30 02:27:22 +01:00
|
|
|
public function create(CreateCreditRequest $request)
|
|
|
|
{
|
|
|
|
$credit = CreditFactory::create(auth()->user()->company()->id, auth()->user()->id);
|
|
|
|
|
|
|
|
return $this->itemResponse($credit);
|
|
|
|
}
|
|
|
|
|
2020-03-03 10:44:26 +01:00
|
|
|
/**
|
|
|
|
* Store a newly created resource in storage.
|
|
|
|
*
|
|
|
|
* @param \App\Http\Requests\Credit\StoreCreditRequest $request The request
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @OA\Post(
|
|
|
|
* path="/api/v1/credits",
|
|
|
|
* operationId="storeCredit",
|
|
|
|
* tags={"credits"},
|
|
|
|
* summary="Adds a credit",
|
|
|
|
* description="Adds an credit 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 credit object",
|
2020-06-21 23:30:25 +02:00
|
|
|
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
|
2020-03-03 10:44:26 +01: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/Credit"),
|
|
|
|
* ),
|
|
|
|
* @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"),
|
|
|
|
* ),
|
|
|
|
* )
|
|
|
|
*
|
|
|
|
*/
|
2020-01-30 02:27:22 +01:00
|
|
|
public function store(StoreCreditRequest $request)
|
|
|
|
{
|
2020-03-03 10:44:26 +01:00
|
|
|
$client = Client::find($request->input('client_id'));
|
|
|
|
|
2020-03-07 07:31:26 +01:00
|
|
|
$credit = $this->credit_repository->save($request->all(), CreditFactory::create(auth()->user()->company()->id, auth()->user()->id));
|
2020-01-30 02:27:22 +01:00
|
|
|
|
|
|
|
$credit = StoreCredit::dispatchNow($credit, $request->all(), $credit->company);
|
|
|
|
|
2020-07-08 14:02:16 +02:00
|
|
|
event(new CreditWasCreated($credit, $credit->company, Ninja::eventVars()));
|
2020-01-30 02:27:22 +01:00
|
|
|
|
|
|
|
return $this->itemResponse($credit);
|
|
|
|
}
|
|
|
|
|
2020-03-03 10:44:26 +01:00
|
|
|
/**
|
|
|
|
* Display the specified resource.
|
|
|
|
*
|
|
|
|
* @param \App\Http\Requests\Credit\ShowCreditRequest $request The request
|
|
|
|
* @param \App\Models\Credit $credit The credit
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @OA\Get(
|
|
|
|
* path="/api/v1/credits/{id}",
|
|
|
|
* operationId="showCredit",
|
|
|
|
* tags={"credits"},
|
|
|
|
* summary="Shows an credit",
|
|
|
|
* description="Displays an credit 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 Credit Hashed ID",
|
|
|
|
* example="D2J234DFA",
|
|
|
|
* required=true,
|
|
|
|
* @OA\Schema(
|
|
|
|
* type="string",
|
|
|
|
* format="string",
|
|
|
|
* ),
|
|
|
|
* ),
|
|
|
|
* @OA\Response(
|
|
|
|
* response=200,
|
|
|
|
* description="Returns the credit object",
|
2020-06-21 23:30:25 +02:00
|
|
|
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
|
2020-03-03 10:44:26 +01: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/Credit"),
|
|
|
|
* ),
|
|
|
|
* @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"),
|
|
|
|
* ),
|
|
|
|
* )
|
|
|
|
*
|
|
|
|
*/
|
2020-01-30 02:27:22 +01:00
|
|
|
public function show(ShowCreditRequest $request, Credit $credit)
|
|
|
|
{
|
|
|
|
return $this->itemResponse($credit);
|
|
|
|
}
|
|
|
|
|
2020-03-03 10:44:26 +01:00
|
|
|
/**
|
|
|
|
* Show the form for editing the specified resource.
|
|
|
|
*
|
|
|
|
* @param \App\Http\Requests\Invoice\EditInvoiceRequest $request The request
|
|
|
|
* @param \App\Models\Invoice $credit The credit
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*
|
|
|
|
* @OA\Get(
|
|
|
|
* path="/api/v1/credits/{id}/edit",
|
2020-04-09 12:48:04 +02:00
|
|
|
* operationId="editCredit",
|
2020-03-03 10:44:26 +01:00
|
|
|
* tags={"credits"},
|
|
|
|
* summary="Shows an credit for editting",
|
|
|
|
* description="Displays an credit 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 Invoice Hashed ID",
|
|
|
|
* example="D2J234DFA",
|
|
|
|
* required=true,
|
|
|
|
* @OA\Schema(
|
|
|
|
* type="string",
|
|
|
|
* format="string",
|
|
|
|
* ),
|
|
|
|
* ),
|
|
|
|
* @OA\Response(
|
|
|
|
* response=200,
|
|
|
|
* description="Returns the credit object",
|
2020-06-21 23:30:25 +02:00
|
|
|
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
|
2020-03-03 10:44:26 +01: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/Invoice"),
|
|
|
|
* ),
|
|
|
|
* @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"),
|
|
|
|
* ),
|
|
|
|
* )
|
|
|
|
*
|
|
|
|
*/
|
2020-01-30 02:27:22 +01:00
|
|
|
public function edit(EditCreditRequest $request, Credit $credit)
|
|
|
|
{
|
|
|
|
return $this->itemResponse($credit);
|
|
|
|
}
|
|
|
|
|
2020-03-03 10:44:26 +01:00
|
|
|
/**
|
|
|
|
* Update the specified resource in storage.
|
|
|
|
*
|
|
|
|
* @param \App\Http\Requests\Credit\UpdateCreditRequest $request The request
|
|
|
|
* @param \App\Models\Credit $Credit The Credit
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @OA\Put(
|
|
|
|
* path="/api/v1/Credits/{id}",
|
|
|
|
* operationId="updateCredit",
|
|
|
|
* tags={"Credits"},
|
|
|
|
* summary="Updates an Credit",
|
|
|
|
* description="Handles the updating of an Credit 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 Credit Hashed ID",
|
|
|
|
* example="D2J234DFA",
|
|
|
|
* required=true,
|
|
|
|
* @OA\Schema(
|
|
|
|
* type="string",
|
|
|
|
* format="string",
|
|
|
|
* ),
|
|
|
|
* ),
|
|
|
|
* @OA\Response(
|
|
|
|
* response=200,
|
|
|
|
* description="Returns the Credit object",
|
2020-06-21 23:30:25 +02:00
|
|
|
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
|
2020-03-03 10:44:26 +01: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/Credit"),
|
|
|
|
* ),
|
|
|
|
* @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"),
|
|
|
|
* ),
|
|
|
|
* )
|
|
|
|
*
|
|
|
|
*/
|
2020-01-30 02:27:22 +01:00
|
|
|
public function update(UpdateCreditRequest $request, Credit $credit)
|
|
|
|
{
|
2020-03-21 06:37:30 +01:00
|
|
|
if ($request->entityIsDeleted($credit)) {
|
2020-01-30 02:27:22 +01:00
|
|
|
return $request->disallowUpdate();
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|
2020-01-30 02:27:22 +01:00
|
|
|
|
2020-01-30 04:02:25 +01:00
|
|
|
$credit = $this->credit_repository->save($request->all(), $credit);
|
2020-01-30 02:27:22 +01:00
|
|
|
|
2020-07-08 14:02:16 +02:00
|
|
|
event(new CreditWasUpdated($credit, $credit->company, Ninja::eventVars()));
|
2020-01-30 02:27:22 +01:00
|
|
|
|
|
|
|
return $this->itemResponse($credit);
|
|
|
|
}
|
|
|
|
|
2020-03-03 10:44:26 +01:00
|
|
|
/**
|
|
|
|
* Remove the specified resource from storage.
|
|
|
|
*
|
|
|
|
* @param \App\Http\Requests\Credit\DestroyCreditRequest $request
|
|
|
|
* @param \App\Models\Credit $credit
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*
|
|
|
|
* @OA\Delete(
|
|
|
|
* path="/api/v1/credits/{id}",
|
|
|
|
* operationId="deleteCredit",
|
|
|
|
* tags={"credits"},
|
|
|
|
* summary="Deletes a credit",
|
|
|
|
* description="Handles the deletion of an credit 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 Credit Hashed ID",
|
|
|
|
* example="D2J234DFA",
|
|
|
|
* required=true,
|
|
|
|
* @OA\Schema(
|
|
|
|
* type="string",
|
|
|
|
* format="string",
|
|
|
|
* ),
|
|
|
|
* ),
|
|
|
|
* @OA\Response(
|
|
|
|
* response=200,
|
|
|
|
* description="Returns a HTTP status",
|
2020-06-21 23:30:25 +02:00
|
|
|
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
|
2020-03-03 10:44:26 +01: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",
|
|
|
|
* description="Unexpected Error",
|
|
|
|
* @OA\JsonContent(ref="#/components/schemas/Error"),
|
|
|
|
* ),
|
|
|
|
* )
|
|
|
|
*
|
|
|
|
*/
|
2020-01-30 02:27:22 +01:00
|
|
|
public function destroy(DestroyCreditRequest $request, Credit $credit)
|
|
|
|
{
|
|
|
|
$credit->delete();
|
|
|
|
|
|
|
|
return response()->json([], 200);
|
|
|
|
}
|
|
|
|
|
2020-03-03 10:44:26 +01:00
|
|
|
/**
|
|
|
|
* Perform bulk actions on the list view
|
|
|
|
*
|
|
|
|
* @return Collection
|
|
|
|
*
|
|
|
|
* @OA\Post(
|
|
|
|
* path="/api/v1/credits/bulk",
|
|
|
|
* operationId="bulkCredits",
|
|
|
|
* tags={"credits"},
|
|
|
|
* summary="Performs bulk actions on an array of credits",
|
|
|
|
* 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="User credentials",
|
|
|
|
* 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 Bulk Action response",
|
2020-06-21 23:30:25 +02:00
|
|
|
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
|
2020-03-03 10:44:26 +01: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",
|
|
|
|
* description="Unexpected Error",
|
|
|
|
* @OA\JsonContent(ref="#/components/schemas/Error"),
|
|
|
|
* ),
|
|
|
|
* )
|
|
|
|
*
|
|
|
|
*/
|
2020-01-30 02:27:22 +01:00
|
|
|
public function bulk()
|
|
|
|
{
|
|
|
|
$action = request()->input('action');
|
|
|
|
|
|
|
|
$ids = request()->input('ids');
|
|
|
|
|
|
|
|
$credits = Credit::withTrashed()->whereIn('id', $this->transformKeys($ids));
|
|
|
|
|
|
|
|
if (!$credits) {
|
|
|
|
return response()->json(['message' => 'No Credits Found']);
|
|
|
|
}
|
|
|
|
|
|
|
|
$credits->each(function ($credit, $key) use ($action) {
|
|
|
|
if (auth()->user()->can('edit', $credit)) {
|
|
|
|
$this->performAction($credit, $action, true);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return $this->listResponse(Credit::withTrashed()->whereIn('id', $this->transformKeys($ids)));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function action(ActionCreditRequest $request, Credit $credit, $action)
|
|
|
|
{
|
|
|
|
return $this->performAction($credit, $action);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function performAction(Credit $credit, $action, $bulk = false)
|
|
|
|
{
|
|
|
|
/*If we are using bulk actions, we don't want to return anything */
|
|
|
|
switch ($action) {
|
|
|
|
case 'clone_to_credit':
|
|
|
|
$credit = CloneCreditFactory::create($credit, auth()->user()->id);
|
|
|
|
return $this->itemResponse($credit);
|
|
|
|
break;
|
|
|
|
case 'history':
|
|
|
|
# code...
|
|
|
|
break;
|
|
|
|
case 'mark_sent':
|
2020-03-03 10:44:26 +01:00
|
|
|
$credit->service()->markSent()->save();
|
2020-01-30 02:27:22 +01:00
|
|
|
|
|
|
|
if (!$bulk) {
|
|
|
|
return $this->itemResponse($credit);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'download':
|
2020-06-24 10:59:56 +02:00
|
|
|
return response()->streamDownload(function () use($credit) {
|
|
|
|
echo file_get_contents($credit->pdf_file_path());
|
|
|
|
}, basename($credit->pdf_file_path()));
|
|
|
|
//return response()->download(TempFile::path($credit->pdf_file_path()), basename($credit->pdf_file_path()));
|
2020-04-16 10:41:25 +02:00
|
|
|
break;
|
2020-01-30 02:27:22 +01:00
|
|
|
case 'archive':
|
2020-01-30 04:02:25 +01:00
|
|
|
$this->credit_repository->archive($credit);
|
2020-01-30 02:27:22 +01:00
|
|
|
|
|
|
|
if (!$bulk) {
|
|
|
|
return $this->listResponse($credit);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'delete':
|
2020-01-30 04:02:25 +01:00
|
|
|
$this->credit_repository->delete($credit);
|
2020-01-30 02:27:22 +01:00
|
|
|
|
|
|
|
if (!$bulk) {
|
|
|
|
return $this->listResponse($credit);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'email':
|
|
|
|
EmailCredit::dispatch($credit, $credit->company);
|
|
|
|
if (!$bulk) {
|
|
|
|
return response()->json(['message'=>'email sent'], 200);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return response()->json(['message' => "The requested action `{$action}` is not available."], 400);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-03-06 14:41:15 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
public function downloadPdf($invitation_key)
|
2020-03-06 14:41:15 +01:00
|
|
|
{
|
|
|
|
$invitation = $this->credit_repository->getInvitationByKey($invitation_key);
|
|
|
|
$contact = $invitation->contact;
|
|
|
|
$credit = $invitation->credit;
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$file_path = $credit->service()->getCreditPdf($contact);
|
2020-03-06 14:41:15 +01:00
|
|
|
|
|
|
|
return response()->download($file_path);
|
|
|
|
}
|
2020-01-30 02:27:22 +01:00
|
|
|
}
|