2020-01-30 02:27:22 +01:00
|
|
|
<?php
|
2021-02-22 10:46:01 +01:00
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2023-01-28 23:21:40 +01:00
|
|
|
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
|
2021-02-22 10:46:01 +01:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2021-02-22 10:46:01 +01:00
|
|
|
*/
|
2020-01-30 02:27:22 +01:00
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
use App\Events\Credit\CreditWasCreated;
|
|
|
|
use App\Events\Credit\CreditWasUpdated;
|
|
|
|
use App\Factory\CloneCreditFactory;
|
|
|
|
use App\Factory\CreditFactory;
|
|
|
|
use App\Filters\CreditFilters;
|
|
|
|
use App\Http\Requests\Credit\ActionCreditRequest;
|
2023-01-22 05:19:49 +01:00
|
|
|
use App\Http\Requests\Credit\BulkCreditRequest;
|
2020-01-30 02:27:22 +01:00
|
|
|
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;
|
2021-02-15 12:00:02 +01:00
|
|
|
use App\Http\Requests\Credit\UploadCreditRequest;
|
2022-03-02 03:51:38 +01:00
|
|
|
use App\Jobs\Credit\ZipCredits;
|
2020-10-28 00:02:32 +01:00
|
|
|
use App\Jobs\Entity\EmailEntity;
|
2021-03-07 22:32:38 +01:00
|
|
|
use App\Models\Account;
|
2020-03-03 10:44:26 +01:00
|
|
|
use App\Models\Client;
|
2020-01-30 02:27:22 +01:00
|
|
|
use App\Models\Credit;
|
2020-10-28 11:10:49 +01:00
|
|
|
use App\Models\Invoice;
|
2020-01-30 02:27:22 +01:00
|
|
|
use App\Repositories\CreditRepository;
|
2022-10-30 21:50:29 +01:00
|
|
|
use App\Services\PdfMaker\PdfMerge;
|
2020-01-30 02:27:22 +01:00
|
|
|
use App\Transformers\CreditTransformer;
|
2020-07-08 14:02:16 +02:00
|
|
|
use App\Utils\Ninja;
|
2020-01-30 02:27:22 +01:00
|
|
|
use App\Utils\Traits\MakesHash;
|
2021-02-15 12:00:02 +01:00
|
|
|
use App\Utils\Traits\SavesDocuments;
|
2020-10-28 11:10:49 +01:00
|
|
|
use Illuminate\Http\Response;
|
2021-07-07 13:39:49 +02:00
|
|
|
use Illuminate\Support\Facades\Storage;
|
2020-01-30 02:27:22 +01:00
|
|
|
|
2020-03-03 10:44:26 +01:00
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Class CreditController.
|
2020-03-03 10:44:26 +01:00
|
|
|
*/
|
2020-01-30 02:27:22 +01:00
|
|
|
class CreditController extends BaseController
|
|
|
|
{
|
|
|
|
use MakesHash;
|
2021-02-15 12:00:02 +01:00
|
|
|
use SavesDocuments;
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2020-01-30 02:27:22 +01:00
|
|
|
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
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Show the list of Credits.
|
2020-03-03 10:44:26 +01:00
|
|
|
*
|
2020-10-28 11:10:49 +01:00
|
|
|
* @param CreditFilters $filters The filters
|
2020-03-03 10:44:26 +01:00
|
|
|
*
|
2020-10-28 11:10:49 +01:00
|
|
|
* @return Response
|
2020-03-03 10:44:26 +01:00
|
|
|
*
|
|
|
|
* @OA\Get(
|
|
|
|
* path="/api/v1/credits",
|
|
|
|
* operationId="getCredits",
|
2021-02-15 12:07:47 +01:00
|
|
|
* tags={"credits"},
|
2020-03-03 10:44:26 +01:00
|
|
|
* 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",
|
2023-02-10 10:21:10 +01:00
|
|
|
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
|
2020-03-03 10:44:26 +01:00
|
|
|
* @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);
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-01-30 02:27:22 +01:00
|
|
|
return $this->listResponse($credits);
|
|
|
|
}
|
|
|
|
|
2020-03-03 10:44:26 +01:00
|
|
|
/**
|
|
|
|
* Show the form for creating a new resource.
|
|
|
|
*
|
2020-10-28 11:10:49 +01:00
|
|
|
* @param CreateCreditRequest $request The request
|
2020-03-03 10:44:26 +01:00
|
|
|
*
|
2020-10-28 11:10:49 +01:00
|
|
|
* @return Response
|
2020-03-03 10:44:26 +01:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* @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",
|
2023-02-10 10:21:10 +01:00
|
|
|
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
|
2020-03-03 10:44:26 +01:00
|
|
|
* @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.
|
|
|
|
*
|
2020-10-28 11:10:49 +01:00
|
|
|
* @param StoreCreditRequest $request The request
|
2020-03-03 10:44:26 +01:00
|
|
|
*
|
2020-10-28 11:10:49 +01:00
|
|
|
* @return Response
|
2020-03-03 10:44:26 +01:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* @OA\Post(
|
|
|
|
* path="/api/v1/credits",
|
|
|
|
* operationId="storeCredit",
|
|
|
|
* tags={"credits"},
|
|
|
|
* summary="Adds a credit",
|
|
|
|
* description="Adds an credit to the system",
|
2023-02-10 10:21:10 +01:00
|
|
|
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
|
2020-03-03 10:44:26 +01:00
|
|
|
* @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
|
|
|
|
2020-11-25 15:19:52 +01:00
|
|
|
$credit = $credit->service()
|
2020-11-04 09:43:20 +01:00
|
|
|
->fillDefaults()
|
2021-11-15 04:14:58 +01:00
|
|
|
->triggeredActions($request)
|
2020-11-04 09:43:20 +01:00
|
|
|
->save();
|
2020-01-30 02:27:22 +01:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if ($credit->invoice_id) {
|
2022-01-28 06:30:40 +01:00
|
|
|
$credit = $credit->service()->markSent()->save();
|
|
|
|
$credit->client->service()->updatePaidToDate(-1 * $credit->balance)->save();
|
|
|
|
}
|
|
|
|
|
2021-05-06 23:12:07 +02:00
|
|
|
event(new CreditWasCreated($credit, $credit->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null)));
|
2020-01-30 02:27:22 +01:00
|
|
|
|
|
|
|
return $this->itemResponse($credit);
|
|
|
|
}
|
|
|
|
|
2020-03-03 10:44:26 +01:00
|
|
|
/**
|
|
|
|
* Display the specified resource.
|
|
|
|
*
|
2020-10-28 11:10:49 +01:00
|
|
|
* @param ShowCreditRequest $request The request
|
|
|
|
* @param Credit $credit The credit
|
2020-03-03 10:44:26 +01:00
|
|
|
*
|
2020-10-28 11:10:49 +01:00
|
|
|
* @return Response
|
2020-03-03 10:44:26 +01:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* @OA\Get(
|
|
|
|
* path="/api/v1/credits/{id}",
|
|
|
|
* operationId="showCredit",
|
|
|
|
* tags={"credits"},
|
|
|
|
* summary="Shows an credit",
|
|
|
|
* description="Displays an credit by id",
|
2023-02-10 10:21:10 +01:00
|
|
|
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
|
2020-03-03 10:44:26 +01:00
|
|
|
* @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.
|
|
|
|
*
|
2020-10-28 11:10:49 +01:00
|
|
|
* @param EditCreditRequest $request The request
|
|
|
|
* @param Credit $credit The credit
|
2020-03-03 10:44:26 +01:00
|
|
|
*
|
2020-10-28 11:10:49 +01:00
|
|
|
* @return Response
|
2020-03-03 10:44:26 +01:00
|
|
|
*
|
|
|
|
* @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",
|
2023-02-10 10:21:10 +01:00
|
|
|
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
|
2020-03-03 10:44:26 +01:00
|
|
|
* @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.
|
|
|
|
*
|
2020-10-28 11:10:49 +01:00
|
|
|
* @param UpdateCreditRequest $request The request
|
|
|
|
* @param Credit $credit
|
|
|
|
* @return Response
|
2020-03-03 10:44:26 +01:00
|
|
|
*
|
|
|
|
*
|
2020-10-28 11:10:49 +01:00
|
|
|
* @throws \ReflectionException
|
2020-03-03 10:44:26 +01:00
|
|
|
* @OA\Put(
|
2020-12-07 21:25:56 +01:00
|
|
|
* path="/api/v1/credits/{id}",
|
2020-03-03 10:44:26 +01:00
|
|
|
* operationId="updateCredit",
|
|
|
|
* tags={"Credits"},
|
|
|
|
* summary="Updates an Credit",
|
|
|
|
* description="Handles the updating of an Credit by id",
|
2023-02-10 10:21:10 +01:00
|
|
|
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
|
2020-03-03 10:44:26 +01:00
|
|
|
* @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
|
|
|
|
2021-11-15 04:14:58 +01:00
|
|
|
$credit->service()
|
|
|
|
->triggeredActions($request)
|
|
|
|
->deletePdf();
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2021-05-06 23:12:07 +02:00
|
|
|
event(new CreditWasUpdated($credit, $credit->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null)));
|
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.
|
|
|
|
*
|
2020-10-28 11:10:49 +01:00
|
|
|
* @param DestroyCreditRequest $request
|
|
|
|
* @param Credit $credit
|
2020-03-03 10:44:26 +01:00
|
|
|
*
|
2020-10-28 11:10:49 +01:00
|
|
|
* @return Response
|
2020-03-03 10:44:26 +01:00
|
|
|
*
|
2020-10-28 11:10:49 +01:00
|
|
|
* @throws \Exception
|
2020-03-03 10:44:26 +01:00
|
|
|
* @OA\Delete(
|
|
|
|
* path="/api/v1/credits/{id}",
|
|
|
|
* operationId="deleteCredit",
|
|
|
|
* tags={"credits"},
|
|
|
|
* summary="Deletes a credit",
|
|
|
|
* description="Handles the deletion of an credit by id",
|
2023-02-10 10:21:10 +01:00
|
|
|
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
|
2020-03-03 10:44:26 +01:00
|
|
|
* @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)
|
|
|
|
{
|
2021-01-17 05:28:03 +01:00
|
|
|
$this->credit_repository->delete($credit);
|
2020-01-30 02:27:22 +01:00
|
|
|
|
2021-01-17 05:28:03 +01:00
|
|
|
return $this->itemResponse($credit->fresh());
|
2020-01-30 02:27:22 +01:00
|
|
|
}
|
|
|
|
|
2020-03-03 10:44:26 +01:00
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Perform bulk actions on the list view.
|
2020-03-03 10:44:26 +01:00
|
|
|
*
|
|
|
|
* @return Collection
|
|
|
|
*
|
|
|
|
* @OA\Post(
|
|
|
|
* path="/api/v1/credits/bulk",
|
|
|
|
* operationId="bulkCredits",
|
|
|
|
* tags={"credits"},
|
|
|
|
* summary="Performs bulk actions on an array of credits",
|
|
|
|
* description="",
|
2023-02-10 10:21:10 +01:00
|
|
|
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
|
2020-03-03 10:44:26 +01:00
|
|
|
* @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"),
|
|
|
|
* ),
|
|
|
|
* )
|
|
|
|
*/
|
2023-01-22 05:19:49 +01:00
|
|
|
public function bulk(BulkCreditRequest $request)
|
2020-01-30 02:27:22 +01:00
|
|
|
{
|
2023-01-22 05:19:49 +01:00
|
|
|
$action = $request->input('action');
|
2020-01-30 02:27:22 +01:00
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
if (Ninja::isHosted() && (stripos($action, 'email') !== false) && !auth()->user()->company()->account->account_sms_verified) {
|
2022-07-27 03:21:12 +02:00
|
|
|
return response(['message' => 'Please verify your account to send emails.'], 400);
|
2023-02-16 02:36:09 +01:00
|
|
|
}
|
2022-07-27 03:21:12 +02:00
|
|
|
|
2023-01-22 06:40:02 +01:00
|
|
|
$credits = Credit::withTrashed()
|
|
|
|
->whereIn('id', $request->ids)
|
|
|
|
->company()
|
|
|
|
->get();
|
2020-01-30 02:27:22 +01:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
if (! $credits) {
|
2021-01-24 23:24:13 +01:00
|
|
|
return response()->json(['message' => ctrans('texts.no_credits_found')]);
|
2020-01-30 02:27:22 +01:00
|
|
|
}
|
|
|
|
|
2022-03-02 03:26:30 +01:00
|
|
|
/*
|
|
|
|
* Download Invoice/s
|
|
|
|
*/
|
|
|
|
|
|
|
|
if ($action == 'bulk_download' && $credits->count() > 1) {
|
2022-03-02 03:51:38 +01:00
|
|
|
$credits->each(function ($credit) {
|
2022-03-02 03:26:30 +01:00
|
|
|
if (auth()->user()->cannot('view', $credit)) {
|
2022-06-21 11:57:17 +02:00
|
|
|
nlog('access denied');
|
|
|
|
|
2022-03-02 03:26:30 +01:00
|
|
|
return response()->json(['message' => ctrans('text.access_denied')]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-03-02 03:51:38 +01:00
|
|
|
ZipCredits::dispatch($credits, $credits->first()->company, auth()->user());
|
2022-03-02 03:26:30 +01:00
|
|
|
|
|
|
|
return response()->json(['message' => ctrans('texts.sent_message')], 200);
|
|
|
|
}
|
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
if ($action == 'bulk_print' && auth()->user()->can('view', $credits->first())) {
|
|
|
|
$paths = $credits->map(function ($credit) {
|
2022-10-30 21:50:29 +01:00
|
|
|
return $credit->service()->getCreditPdf($credit->invitations->first());
|
|
|
|
});
|
|
|
|
|
|
|
|
$merge = (new PdfMerge($paths->toArray()))->run();
|
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
return response()->streamDownload(function () use ($merge) {
|
|
|
|
echo($merge);
|
|
|
|
}, 'print.pdf', ['Content-Type' => 'application/pdf']);
|
2022-10-30 21:50:29 +01:00
|
|
|
}
|
|
|
|
|
2020-01-30 02:27:22 +01:00
|
|
|
$credits->each(function ($credit, $key) use ($action) {
|
|
|
|
if (auth()->user()->can('edit', $credit)) {
|
|
|
|
$this->performAction($credit, $action, true);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-04-24 12:35:26 +02:00
|
|
|
return $this->listResponse(Credit::with(['invitations','documents'])->withTrashed()->company()->whereIn('id', $request->ids));
|
2020-01-30 02:27:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2021-11-22 05:25:43 +01:00
|
|
|
case 'mark_paid':
|
|
|
|
$credit->service()->markPaid()->save();
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2021-11-22 05:25:43 +01:00
|
|
|
return $this->itemResponse($credit);
|
2023-02-16 02:36:09 +01:00
|
|
|
break;
|
2021-11-22 05:25:43 +01:00
|
|
|
|
2020-01-30 02:27:22 +01:00
|
|
|
case 'clone_to_credit':
|
|
|
|
$credit = CloneCreditFactory::create($credit, auth()->user()->id);
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-01-30 02:27:22 +01:00
|
|
|
return $this->itemResponse($credit);
|
|
|
|
break;
|
|
|
|
case 'history':
|
2020-09-06 11:38:10 +02:00
|
|
|
// code...
|
2020-01-30 02:27:22 +01:00
|
|
|
break;
|
|
|
|
case 'mark_sent':
|
2020-03-03 10:44:26 +01:00
|
|
|
$credit->service()->markSent()->save();
|
2020-01-30 02:27:22 +01:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
if (! $bulk) {
|
2020-01-30 02:27:22 +01:00
|
|
|
return $this->itemResponse($credit);
|
|
|
|
}
|
|
|
|
break;
|
2022-06-21 11:57:17 +02:00
|
|
|
case 'download':
|
2021-07-07 13:39:49 +02:00
|
|
|
// $file = $credit->pdf_file_path();
|
|
|
|
$file = $credit->service()->getCreditPdf($credit->invitations->first());
|
|
|
|
|
|
|
|
// return response()->download($file, basename($file), ['Cache-Control:' => 'no-cache'])->deleteFileAfterSend(true);
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
return response()->streamDownload(function () use ($file) {
|
|
|
|
echo Storage::get($file);
|
|
|
|
}, basename($file), ['Content-Type' => 'application/pdf']);
|
2023-02-16 02:36:09 +01: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
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
if (! $bulk) {
|
2022-08-18 06:08:50 +02:00
|
|
|
return $this->itemResponse($credit);
|
2020-01-30 02:27:22 +01:00
|
|
|
}
|
2020-07-19 23:54:10 +02:00
|
|
|
break;
|
|
|
|
case 'restore':
|
|
|
|
$this->credit_repository->restore($credit);
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
if (! $bulk) {
|
2022-08-18 06:08:50 +02:00
|
|
|
return $this->itemResponse($credit);
|
2020-07-19 23:54:10 +02:00
|
|
|
}
|
2020-01-30 02:27:22 +01:00
|
|
|
break;
|
|
|
|
case 'delete':
|
2020-01-30 04:02:25 +01:00
|
|
|
$this->credit_repository->delete($credit);
|
2020-01-30 02:27:22 +01:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
if (! $bulk) {
|
2022-08-18 06:08:50 +02:00
|
|
|
return $this->itemResponse($credit);
|
2020-01-30 02:27:22 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'email':
|
2022-07-08 01:00:36 +02:00
|
|
|
|
|
|
|
$credit->invitations->load('contact.client.country', 'credit.client.country', 'credit.company')->each(function ($invitation) use ($credit) {
|
|
|
|
EmailEntity::dispatch($invitation, $credit->company, 'credit');
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
if (! $bulk) {
|
|
|
|
return response()->json(['message'=>'email sent'], 200);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'send_email':
|
2020-10-28 00:02:32 +01:00
|
|
|
|
|
|
|
$credit->invitations->load('contact.client.country', 'credit.client.country', 'credit.company')->each(function ($invitation) use ($credit) {
|
2021-06-13 06:19:40 +02:00
|
|
|
EmailEntity::dispatch($invitation, $credit->company, 'credit');
|
2020-10-28 00:02:32 +01:00
|
|
|
});
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
if (! $bulk) {
|
2020-01-30 02:27:22 +01:00
|
|
|
return response()->json(['message'=>'email sent'], 200);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2021-01-24 23:24:13 +01:00
|
|
|
return response()->json(['message' => ctrans('texts.action_unavailable', ['action' => $action])], 400);
|
2020-01-30 02:27:22 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-03-06 14:41:15 +01:00
|
|
|
|
2023-02-01 09:56:02 +01:00
|
|
|
/**
|
|
|
|
* @OA\Get(
|
|
|
|
* path="/api/v1/credit/{invitation_key}/download",
|
|
|
|
* operationId="downloadCredit",
|
|
|
|
* tags={"quotes"},
|
|
|
|
* summary="Download a specific credit by invitation key",
|
|
|
|
* description="Downloads a specific quote",
|
2023-02-10 10:21:10 +01:00
|
|
|
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
|
2023-02-01 09:56:02 +01:00
|
|
|
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
|
|
|
|
* @OA\Parameter(ref="#/components/parameters/include"),
|
|
|
|
* @OA\Parameter(
|
|
|
|
* name="invitation_key",
|
|
|
|
* in="path",
|
|
|
|
* description="The Credit Invitation Key",
|
|
|
|
* example="D2J234DFA",
|
|
|
|
* required=true,
|
|
|
|
* @OA\Schema(
|
|
|
|
* type="string",
|
|
|
|
* format="string",
|
|
|
|
* ),
|
|
|
|
* ),
|
|
|
|
* @OA\Response(
|
|
|
|
* response=200,
|
|
|
|
* description="Returns the credit pdf",
|
|
|
|
* @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"),
|
|
|
|
* ),
|
|
|
|
* )
|
|
|
|
* @param $invitation_key
|
|
|
|
* @return \Symfony\Component\HttpFoundation\BinaryFileResponse
|
|
|
|
*/
|
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);
|
2021-09-20 13:16:28 +02:00
|
|
|
|
2023-02-01 09:54:30 +01:00
|
|
|
if (! $invitation) {
|
|
|
|
return response()->json(['message' => 'no record found'], 400);
|
|
|
|
}
|
2023-02-01 09:56:02 +01:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
$credit = $invitation->credit;
|
2020-03-06 14:41:15 +01:00
|
|
|
|
2021-07-07 13:39:49 +02:00
|
|
|
$file = $credit->service()->getCreditPdf($invitation);
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2021-09-20 13:16:28 +02:00
|
|
|
$headers = ['Content-Type' => 'application/pdf'];
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if (request()->input('inline') == 'true') {
|
2021-09-20 13:16:28 +02:00
|
|
|
$headers = array_merge($headers, ['Content-Disposition' => 'inline']);
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2021-09-20 13:16:28 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
return response()->streamDownload(function () use ($file) {
|
|
|
|
echo Storage::get($file);
|
|
|
|
}, basename($file), $headers);
|
2020-03-06 14:41:15 +01:00
|
|
|
}
|
2021-02-15 12:00:02 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the specified resource in storage.
|
|
|
|
*
|
|
|
|
* @param UploadCreditRequest $request
|
|
|
|
* @param Credit $client
|
|
|
|
* @return Response
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @OA\Put(
|
|
|
|
* path="/api/v1/credits/{id}/upload",
|
|
|
|
* operationId="uploadCredits",
|
|
|
|
* tags={"credits"},
|
|
|
|
* summary="Uploads a document to a credit",
|
|
|
|
* description="Handles the uploading of a document to a credit",
|
2023-02-10 10:21:10 +01:00
|
|
|
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
|
2021-02-15 12:00:02 +01:00
|
|
|
* @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",
|
|
|
|
* @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/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"),
|
|
|
|
* ),
|
|
|
|
* )
|
|
|
|
*/
|
|
|
|
public function upload(UploadCreditRequest $request, Credit $credit)
|
|
|
|
{
|
2022-06-21 11:57:17 +02:00
|
|
|
if (! $this->checkFeature(Account::FEATURE_DOCUMENTS)) {
|
2021-03-07 22:32:38 +01:00
|
|
|
return $this->featureFailure();
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($request->has('documents')) {
|
2021-02-15 12:00:02 +01:00
|
|
|
$this->saveDocuments($request->file('documents'), $credit);
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2021-02-15 12:00:02 +01:00
|
|
|
|
|
|
|
return $this->itemResponse($credit->fresh());
|
|
|
|
}
|
2020-01-30 02:27:22 +01:00
|
|
|
}
|