1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 13:12:50 +01:00
invoiceninja/app/Http/Controllers/InvoiceController.php

1030 lines
38 KiB
PHP
Raw Normal View History

<?php
2019-05-11 05:32:07 +02:00
/**
* Invoice Ninja (https://invoiceninja.com).
2019-05-11 05:32:07 +02:00
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2024-04-12 06:15:41 +02:00
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
2019-05-11 05:32:07 +02:00
*
2021-06-16 08:58:16 +02:00
* @license https://www.elastic.co/licensing/elastic-license
2019-05-11 05:32:07 +02:00
*/
namespace App\Http\Controllers;
2019-05-15 11:20:52 +02:00
use App\Events\Invoice\InvoiceWasCreated;
use App\Events\Invoice\InvoiceWasUpdated;
2023-10-26 04:57:44 +02:00
use App\Factory\CloneInvoiceFactory;
2019-04-23 15:17:49 +02:00
use App\Factory\CloneInvoiceToQuoteFactory;
2023-10-26 04:57:44 +02:00
use App\Factory\InvoiceFactory;
use App\Filters\InvoiceFilters;
use App\Http\Requests\Invoice\ActionInvoiceRequest;
2022-11-05 05:13:08 +01:00
use App\Http\Requests\Invoice\BulkInvoiceRequest;
2023-10-26 04:57:44 +02:00
use App\Http\Requests\Invoice\CreateInvoiceRequest;
use App\Http\Requests\Invoice\DestroyInvoiceRequest;
use App\Http\Requests\Invoice\EditInvoiceRequest;
use App\Http\Requests\Invoice\ShowInvoiceRequest;
use App\Http\Requests\Invoice\StoreInvoiceRequest;
use App\Http\Requests\Invoice\UpdateInvoiceRequest;
use App\Http\Requests\Invoice\UpdateReminderRequest;
2023-10-26 04:57:44 +02:00
use App\Http\Requests\Invoice\UploadInvoiceRequest;
use App\Jobs\Cron\AutoBill;
use App\Jobs\Invoice\BulkInvoiceJob;
use App\Jobs\Invoice\UpdateReminders;
use App\Jobs\Invoice\ZipInvoices;
use App\Models\Account;
use App\Models\Invoice;
use App\Models\Quote;
use App\Repositories\InvoiceRepository;
use App\Services\PdfMaker\PdfMerge;
use App\Services\Template\TemplateAction;
use App\Transformers\InvoiceTransformer;
use App\Transformers\QuoteTransformer;
use App\Utils\Ninja;
use App\Utils\Traits\MakesHash;
use App\Utils\Traits\SavesDocuments;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Storage;
2019-04-04 11:28:53 +02:00
/**
* Class InvoiceController.
2019-04-04 11:28:53 +02:00
*/
class InvoiceController extends BaseController
{
use MakesHash;
2021-02-15 12:34:05 +01:00
use SavesDocuments;
protected $entity_type = Invoice::class;
protected $entity_transformer = InvoiceTransformer::class;
/**
* @var InvoiceRepository
*/
protected $invoice_repo;
/**
* InvoiceController constructor.
*
2020-10-28 11:10:49 +01:00
* @param InvoiceRepository $invoice_repo The invoice repo
*/
public function __construct(InvoiceRepository $invoice_repo)
{
parent::__construct();
$this->invoice_repo = $invoice_repo;
}
/**
* Show the list of Invoices.
*
2020-10-28 11:10:49 +01:00
* @param InvoiceFilters $filters The filters
*
2024-06-16 00:30:25 +02:00
* @return Response| \Illuminate\Http\JsonResponse
*
* @OA\Get(
* path="/api/v1/invoices",
* operationId="getInvoices",
* tags={"invoices"},
* summary="Gets a list of invoices",
* description="Lists invoices, search and filters allow fine grained lists to be generated.
*
* Query parameters can be added to performed more fine grained filtering of the invoices, these are handled by the InvoiceFilters class which defines the methods available",
2023-02-10 10:21:10 +01:00
* @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 invoices",
* @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/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"),
* ),
* )
*/
public function index(InvoiceFilters $filters)
{
set_time_limit(45);
$invoices = Invoice::filter($filters);
return $this->listResponse($invoices);
}
/**
* Show the form for creating a new resource.
*
2020-10-28 11:10:49 +01:00
* @param CreateInvoiceRequest $request The request
*
2024-06-16 00:30:25 +02:00
* @return Response| \Illuminate\Http\JsonResponse
*
*
* @OA\Get(
* path="/api/v1/invoices/create",
* operationId="getInvoicesCreate",
* tags={"invoices"},
* summary="Gets a new blank invoice 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"),
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\Parameter(ref="#/components/parameters/include"),
* @OA\Response(
* response=200,
* description="A blank invoice 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/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"),
* ),
* )
*/
public function create(CreateInvoiceRequest $request)
{
2023-08-15 01:49:09 +02:00
/** @var \App\Models\User $user */
$user = auth()->user();
$invoice = InvoiceFactory::create($user->company()->id, $user->id);
2023-12-14 23:08:44 +01:00
$invoice->date = now()->addSeconds($user->company()->utc_offset())->format('Y-m-d');
return $this->itemResponse($invoice);
}
/**
* Store a newly created resource in storage.
*
2020-10-28 11:10:49 +01:00
* @param StoreInvoiceRequest $request The request
*
2024-06-16 00:30:25 +02:00
* @return Response| \Illuminate\Http\JsonResponse
*
*
* @OA\Post(
* path="/api/v1/invoices",
* operationId="storeInvoice",
* tags={"invoices"},
* summary="Adds a invoice",
* description="Adds an invoice to the system",
2023-02-10 10:21:10 +01:00
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\Parameter(ref="#/components/parameters/include"),
* @OA\RequestBody(
* required=true,
2021-10-18 06:17:07 +02:00
* @OA\JsonContent(ref="#/components/schemas/FillableInvoice")
* ),
* @OA\Response(
* response=200,
* description="Returns the saved invoice 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/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"),
* ),
* )
*/
public function store(StoreInvoiceRequest $request)
{
2024-01-14 05:05:00 +01:00
2023-08-15 01:49:09 +02:00
/** @var \App\Models\User $user */
$user = auth()->user();
$invoice = $this->invoice_repo->save($request->all(), InvoiceFactory::create($user->company()->id, $user->id));
2020-11-04 09:43:20 +01:00
$invoice = $invoice->service()
->fillDefaults()
->triggeredActions($request)
2022-06-08 12:40:26 +02:00
->adjustInventory()
2020-11-04 09:43:20 +01:00
->save();
2023-08-15 01:49:09 +02:00
event(new InvoiceWasCreated($invoice, $invoice->company, Ninja::eventVars($user ? $user->id : null)));
2022-03-10 01:32:04 +01:00
$transaction = [
'invoice' => $invoice->transaction_event(),
'payment' => [],
'client' => $invoice->client->transaction_event(),
'credit' => [],
'metadata' => [],
];
2022-10-28 04:09:17 +02:00
// TransactionLog::dispatch(TransactionEvent::INVOICE_UPDATED, $transaction, $invoice->company->db);
return $this->itemResponse($invoice);
}
/**
* Display the specified resource.
*
2020-10-28 11:10:49 +01:00
* @param ShowInvoiceRequest $request The request
* @param Invoice $invoice The invoice
*
2024-06-16 00:30:25 +02:00
* @return Response| \Illuminate\Http\JsonResponse
*
*
* @OA\Get(
* path="/api/v1/invoices/{id}",
* operationId="showInvoice",
* tags={"invoices"},
* summary="Shows an invoice",
* description="Displays an invoice by id",
2023-02-10 10:21:10 +01:00
* @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 invoice 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/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"),
* ),
* )
*/
public function show(ShowInvoiceRequest $request, Invoice $invoice)
{
return $this->itemResponse($invoice);
}
/**
* Show the form for editing the specified resource.
*
2020-10-28 11:10:49 +01:00
* @param EditInvoiceRequest $request The request
* @param Invoice $invoice The invoice
*
2024-06-16 00:30:25 +02:00
* @return Response| \Illuminate\Http\JsonResponse
*
* @OA\Get(
* path="/api/v1/invoices/{id}/edit",
* operationId="editInvoice",
* tags={"invoices"},
* summary="Shows an invoice for editting",
* description="Displays an invoice by id",
2023-02-10 10:21:10 +01:00
* @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 invoice 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/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"),
* ),
* )
*/
public function edit(EditInvoiceRequest $request, Invoice $invoice)
{
return $this->itemResponse($invoice);
}
/**
* Update the specified resource in storage.
*
2020-10-28 11:10:49 +01:00
* @param UpdateInvoiceRequest $request The request
* @param Invoice $invoice The invoice
*
2024-06-16 00:30:25 +02:00
* @return Response| \Illuminate\Http\JsonResponse
*
*
* @OA\Put(
* path="/api/v1/invoices/{id}",
* operationId="updateInvoice",
* tags={"invoices"},
* summary="Updates an invoice",
* description="Handles the updating of an invoice by id",
2023-02-10 10:21:10 +01:00
* @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 invoice 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/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"),
* ),
* )
*/
public function update(UpdateInvoiceRequest $request, Invoice $invoice)
{
if ($request->entityIsDeleted($invoice)) {
return $request->disallowUpdate();
}
if ($invoice->isLocked()) {
return response()->json(['message' => '', 'errors' => ['number' => ctrans('texts.locked_invoice')]], 422);
}
2020-07-28 06:29:56 +02:00
2022-06-08 12:40:26 +02:00
$old_invoice = $invoice->line_items;
$invoice = $this->invoice_repo->save($request->all(), $invoice);
2022-06-08 12:40:26 +02:00
$invoice->service()
->triggeredActions($request)
->adjustInventory($old_invoice);
2021-05-06 23:12:07 +02:00
event(new InvoiceWasUpdated($invoice, $invoice->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null)));
return $this->itemResponse($invoice);
}
/**
* Remove the specified resource from storage.
*
2020-10-28 11:10:49 +01:00
* @param DestroyInvoiceRequest $request
* @param Invoice $invoice
*
2020-10-28 11:10:49 +01:00
* @return Response
*
2020-10-28 11:10:49 +01:00
* @throws \Exception
* @OA\Delete(
* path="/api/v1/invoices/{id}",
* operationId="deleteInvoice",
* tags={"invoices"},
* summary="Deletes a invoice",
* description="Handles the deletion of an invoice by id",
2023-02-10 10:21:10 +01:00
* @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 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(DestroyInvoiceRequest $request, Invoice $invoice)
{
$this->invoice_repo->delete($invoice);
return $this->itemResponse($invoice->fresh());
}
2022-11-05 05:13:08 +01:00
public function bulk(BulkInvoiceRequest $request)
{
2024-01-14 05:05:00 +01:00
2023-08-15 01:49:09 +02:00
/** @var \App\Models\User $user */
$user = auth()->user();
2022-11-05 05:13:08 +01:00
$action = $request->input('action');
$ids = $request->input('ids');
2023-08-15 01:49:09 +02:00
if (Ninja::isHosted() && (stripos($action, 'email') !== false) && !$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
}
2023-04-03 14:34:47 +02:00
2023-11-01 01:00:23 +01:00
if (Ninja::isHosted() && (stripos($action, 'email') !== false) && $user->account->emailQuotaExceeded()) {
return response(['message' => ctrans('texts.email_quota_exceeded_subject')], 400);
}
2024-01-14 05:51:31 +01:00
if(in_array($request->action, ['auto_bill', 'mark_paid']) && $user->cannot('create', \App\Models\Payment::class)) {
return response(['message' => ctrans('texts.not_authorized'), 'errors' => ['ids' => [ctrans('texts.not_authorized')]]], 422);
}
$invoices = Invoice::withTrashed()->whereIn('id', $this->transformKeys($ids))->company()->get();
2024-06-24 09:37:08 +02:00
if ($invoices->count() == 0 ) {
return response()->json(['message' => 'No Invoices Found']);
}
/*
* Download Invoice/s
*/
2021-11-09 11:15:08 +01:00
if ($action == 'bulk_download' && $invoices->count() > 1) {
2023-10-26 04:57:44 +02:00
$invoices->each(function ($invoice) use ($user) {
2023-08-15 01:49:09 +02:00
if ($user->cannot('view', $invoice)) {
2021-01-24 23:24:13 +01:00
return response()->json(['message' => ctrans('text.access_denied')]);
}
});
2021-11-09 11:59:52 +01:00
2021-02-18 00:30:31 +01:00
ZipInvoices::dispatch($invoices, $invoices->first()->company, auth()->user());
2021-01-24 23:24:13 +01:00
return response()->json(['message' => ctrans('texts.sent_message')], 200);
}
2024-01-14 05:05:00 +01:00
if ($action == 'download' && $invoices->count() >= 1 && $user->can('view', $invoices->first())) {
2023-11-15 22:55:16 +01:00
$filename = $invoices->first()->getFileName();
2024-01-14 05:05:00 +01:00
2023-11-26 08:41:42 +01:00
return response()->streamDownload(function () use ($invoices) {
2023-11-16 01:36:14 +01:00
echo $invoices->first()->service()->getInvoicePdf();
2023-11-15 22:55:16 +01:00
}, $filename, ['Content-Type' => 'application/pdf']);
}
2023-08-15 01:49:09 +02:00
if ($action == 'bulk_print' && $user->can('view', $invoices->first())) {
2023-02-16 02:36:09 +01:00
$paths = $invoices->map(function ($invoice) {
2023-10-26 03:25:56 +02:00
return (new \App\Jobs\Entity\CreateRawPdf($invoice->invitations->first()))->handle();
2022-10-30 21:37:52 +01:00
});
2023-11-16 01:36:14 +01:00
return response()->streamDownload(function () use ($paths) {
echo $merge = (new PdfMerge($paths->toArray()))->run();
2023-02-16 02:36:09 +01:00
}, 'print.pdf', ['Content-Type' => 'application/pdf']);
2022-10-30 21:37:52 +01:00
}
2023-10-26 04:57:44 +02:00
if($action == 'template' && $user->can('view', $invoices->first())) {
2023-10-06 12:07:31 +02:00
2024-01-14 05:05:00 +01:00
$hash_or_response = $request->boolean('send_email') ? 'email sent' : \Illuminate\Support\Str::uuid();
2023-10-26 04:57:44 +02:00
TemplateAction::dispatch(
$ids,
$request->template_id,
Invoice::class,
$user->id,
$user->company(),
$user->company()->db,
$hash_or_response,
$request->boolean('send_email')
);
2024-01-14 05:05:00 +01:00
2023-10-06 12:07:31 +02:00
return response()->json(['message' => $hash_or_response], 200);
}
2024-01-13 08:04:03 +01:00
if($action == 'set_payment_link' && $request->has('subscription_id')) {
2024-01-14 05:05:00 +01:00
$invoices->each(function ($invoice) use ($user, $request) {
if($user->can('edit', $invoice)) {
2024-01-13 08:04:03 +01:00
$invoice->service()->setPaymentLink($request->subscription_id)->save();
2024-01-14 05:05:00 +01:00
}
2024-01-13 08:04:03 +01:00
});
2024-01-14 05:51:31 +01:00
return $this->listResponse(Invoice::withTrashed()->whereIn('id', $this->transformKeys($ids))->company());
2024-01-13 08:04:03 +01:00
}
/*
* Send the other actions to the switch
*/
2023-08-15 01:49:09 +02:00
$invoices->each(function ($invoice, $key) use ($action, $user) {
if ($user->can('edit', $invoice)) {
$this->performAction($invoice, $action, true);
}
});
/* Need to understand which permission are required for the given bulk action ie. view / edit */
2024-01-14 05:51:31 +01:00
return $this->listResponse(Invoice::withTrashed()->whereIn('id', $this->transformKeys($ids))->company());
}
/**
* @OA\Get(
* path="/api/v1/invoices/{id}/{action}",
* operationId="actionInvoice",
* tags={"invoices"},
* summary="Performs a custom action on an invoice",
* description="Performs a custom action on an invoice.
*
2020-10-28 11:10:49 +01:00
* The current range of actions are as follows
* - clone_to_invoice
* - clone_to_quote
* - history
* - delivery_note
* - mark_paid
* - download
* - archive
* - delete
* - email",
2023-02-10 10:21:10 +01:00
* @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\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 invoice 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/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-10-28 11:10:49 +01:00
* @param ActionInvoiceRequest $request
* @param Invoice $invoice
* @param $action
* @return \App\Http\Controllers\Response|\Illuminate\Http\JsonResponse|Response|mixed|\Symfony\Component\HttpFoundation\StreamedResponse
*/
public function action(ActionInvoiceRequest $request, Invoice $invoice, $action)
{
return $this->performAction($invoice, $action);
}
private function performAction(Invoice $invoice, $action, $bulk = false)
{
2023-10-30 05:51:11 +01:00
/** @var \App\Models\User $user */
$user = auth()->user();
/*If we are using bulk actions, we don't want to return anything */
switch ($action) {
2022-11-23 03:02:41 +01:00
case 'auto_bill':
2023-10-30 05:51:11 +01:00
AutoBill::dispatch($invoice->id, $invoice->company->db);
2022-11-23 03:02:41 +01:00
return $this->itemResponse($invoice);
case 'clone_to_invoice':
$invoice = CloneInvoiceFactory::create($invoice, auth()->user()->id);
return $this->itemResponse($invoice);
2023-04-03 14:34:47 +02:00
case 'clone_to_quote':
$quote = CloneInvoiceToQuoteFactory::create($invoice, auth()->user()->id);
2020-11-11 01:13:39 +01:00
$this->entity_transformer = QuoteTransformer::class;
$this->entity_type = Quote::class;
return $this->itemResponse($quote);
case 'history':
// code...
break;
case 'delivery_note':
// code...
break;
case 'mark_paid':
2022-01-28 00:56:14 +01:00
if ($invoice->status_id == Invoice::STATUS_PAID || $invoice->is_deleted === true) {
2021-01-24 23:24:13 +01:00
return $this->errorResponse(['message' => ctrans('texts.invoice_cannot_be_marked_paid')], 400);
}
2022-03-10 22:13:27 +01:00
$invoice = $invoice->service()->markPaid()->save();
if (! $bulk) {
return $this->itemResponse($invoice);
}
break;
case 'mark_sent':
2022-12-08 01:17:18 +01:00
$invoice->service()->markSent(true)->save();
if (! $bulk) {
return $this->itemResponse($invoice);
}
break;
case 'download':
2021-05-15 04:19:36 +02:00
2023-11-16 01:47:57 +01:00
return response()->streamDownload(function () use ($invoice) {
echo $invoice->service()->getInvoicePdf();
}, $invoice->getFileName(), ['Content-Type' => 'application/pdf']);
2021-07-07 13:39:49 +02:00
2020-06-08 23:43:46 +02:00
case 'restore':
$this->invoice_repo->restore($invoice);
if (! $bulk) {
2022-08-18 06:08:50 +02:00
return $this->itemResponse($invoice);
2020-06-08 23:43:46 +02:00
}
break;
case 'archive':
$this->invoice_repo->archive($invoice);
if (! $bulk) {
2022-08-18 06:08:50 +02:00
return $this->itemResponse($invoice);
}
break;
case 'delete':
2021-01-24 23:24:13 +01:00
$this->invoice_repo->delete($invoice);
if (! $bulk) {
2022-08-18 06:08:50 +02:00
return $this->itemResponse($invoice);
}
break;
case 'cancel':
2023-09-05 03:35:52 +02:00
$invoice = $invoice->service()->handleCancellation()->save();
if (! $bulk) {
$this->itemResponse($invoice);
}
break;
case 'email':
2022-07-08 01:00:36 +02:00
case 'send_email':
//check query parameter for email_type and set the template else use calculateTemplate
$template = request()->has('email_type') ? request()->input('email_type') : $invoice->calculateTemplate('invoice');
2022-07-08 01:00:36 +02:00
BulkInvoiceJob::dispatch($invoice, $template);
2022-07-08 01:00:36 +02:00
if (! $bulk) {
return response()->json(['message' => 'email sent'], 200);
}
break;
2024-01-14 05:05:00 +01:00
default:
2021-01-24 23:24:13 +01:00
return response()->json(['message' => ctrans('texts.action_unavailable', ['action' => $action])], 400);
}
}
/**
* @OA\Get(
* path="/api/v1/invoice/{invitation_key}/download",
* operationId="downloadInvoice",
* tags={"invoices"},
* summary="Download a specific invoice by invitation key",
* description="Downloads a specific invoice",
2023-02-10 10:21:10 +01:00
* @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="invitation_key",
* in="path",
* description="The Invoice Invitation Key",
* example="D2J234DFA",
* required=true,
* @OA\Schema(
* type="string",
* format="string",
* ),
* ),
* @OA\Response(
* response=200,
* description="Returns the invoice 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"),
* ),
* )
2020-10-28 11:10:49 +01:00
* @param $invitation_key
2024-06-16 06:35:56 +02:00
* @return \Symfony\Component\HttpFoundation\StreamedResponse | \Illuminate\Http\JsonResponse | \Illuminate\Http\Response
*/
public function downloadPdf($invitation_key)
{
$invitation = $this->invoice_repo->getInvitationByKey($invitation_key);
2021-08-15 07:30:46 +02:00
if (! $invitation) {
return response()->json(['message' => 'no record found'], 400);
}
2021-08-15 07:30:46 +02:00
$invoice = $invitation->invoice;
App::setLocale($invitation->contact->preferredLocale());
2023-06-23 00:10:05 +02:00
$file_name = $invoice->numberFormatter().'.pdf';
2023-06-21 16:16:20 +02:00
2023-10-26 03:25:56 +02:00
$file = (new \App\Jobs\Entity\CreateRawPdf($invitation))->handle();
2021-06-17 10:59:15 +02:00
2021-09-20 13:16:28 +02:00
$headers = ['Content-Type' => 'application/pdf'];
2023-04-03 14:34:47 +02:00
if (request()->input('inline') == 'true') {
$headers = array_merge($headers, ['Content-Disposition' => 'inline']);
}
return response()->streamDownload(function () use ($file) {
2023-06-21 16:16:20 +02:00
echo $file;
}, $file_name, $headers);
2023-04-03 14:34:47 +02:00
}
/**
* @OA\Get(
2023-04-17 10:23:07 +02:00
* path="/api/v1/invoice/{invitation_key}/download_e_invoice",
2023-04-03 14:34:47 +02:00
* operationId="downloadXInvoice",
* tags={"invoices"},
* summary="Download a specific x-invoice by invitation key",
* description="Downloads a specific x-invoice",
* @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="invitation_key",
* in="path",
* description="The Invoice Invitation Key",
* example="D2J234DFA",
* required=true,
* @OA\Schema(
* type="string",
* format="string",
* ),
* ),
* @OA\Response(
* response=200,
* description="Returns the x-invoice 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
2024-06-16 06:35:56 +02:00
* @return \Symfony\Component\HttpFoundation\StreamedResponse | \Illuminate\Http\JsonResponse | \Illuminate\Http\Response
2023-04-03 14:34:47 +02:00
*/
2023-04-17 09:24:16 +02:00
public function downloadEInvoice($invitation_key)
2023-04-03 14:34:47 +02:00
{
$invitation = $this->invoice_repo->getInvitationByKey($invitation_key);
if (! $invitation) {
return response()->json(['message' => 'no record found'], 400);
}
$contact = $invitation->contact;
$invoice = $invitation->invoice;
2023-04-17 09:24:16 +02:00
$file = $invoice->service()->getEInvoice($contact);
2023-08-16 11:55:35 +02:00
$file_name = $invoice->getFileName("xml");
2023-04-03 14:34:47 +02:00
$headers = ['Content-Type' => 'application/xml'];
if (request()->input('inline') == 'true') {
2021-09-20 13:16:28 +02:00
$headers = array_merge($headers, ['Content-Disposition' => 'inline']);
}
2021-09-20 13:16:28 +02:00
return response()->streamDownload(function () use ($file) {
2023-08-16 11:55:35 +02:00
echo $file;
}, $file_name, $headers);
}
2020-11-04 02:27:07 +01:00
/**
* @OA\Get(
* path="/api/v1/invoices/{id}/delivery_note",
* operationId="deliveryNote",
* tags={"invoices"},
* summary="Download a specific invoice delivery notes",
* description="Downloads a specific invoice delivery notes",
2023-02-10 10:21:10 +01:00
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
2020-11-04 02:27:07 +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 Hahsed Id",
* example="D2J234DFA",
* required=true,
* @OA\Schema(
* type="string",
* format="string",
* ),
* ),
* @OA\Response(
* response=200,
* description="Returns the invoice delivery note 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 $invoice
2024-06-16 06:35:56 +02:00
* @return \Symfony\Component\HttpFoundation\StreamedResponse | \Illuminate\Http\JsonResponse | \Illuminate\Http\Response
2020-11-04 02:27:07 +01:00
*/
public function deliveryNote(ShowInvoiceRequest $request, Invoice $invoice)
{
2024-01-14 05:05:00 +01:00
2023-11-16 01:36:14 +01:00
return response()->streamDownload(function () use ($invoice) {
echo $invoice->service()->getInvoiceDeliveryNote($invoice, $invoice->invitations->first()->contact);
}, $invoice->getDeliveryNoteName(), ['Content-Type' => 'application/pdf']);
2023-10-04 06:29:31 +02:00
2020-11-04 02:27:07 +01:00
}
2021-02-15 12:34:05 +01:00
/**
* Update the specified resource in storage.
*
* @param UploadInvoiceRequest $request
* @param Invoice $invoice
2024-06-16 00:30:25 +02:00
* @return Response| \Illuminate\Http\JsonResponse
2021-02-15 12:34:05 +01:00
*
*
*
* @OA\Put(
* path="/api/v1/invoices/{id}/upload",
* operationId="uploadInvoice",
* tags={"invoices"},
* summary="Uploads a document to a invoice",
* description="Handles the uploading of a document to a invoice",
2023-02-10 10:21:10 +01:00
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
2021-02-15 12:34:05 +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 Invoice 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/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"),
* ),
* )
*/
public function upload(UploadInvoiceRequest $request, Invoice $invoice)
{
2023-08-15 16:23:18 +02:00
if (! $this->checkFeature(Account::FEATURE_DOCUMENTS)) {
2021-03-07 11:14:53 +01:00
return $this->featureFailure();
}
if ($request->has('documents')) {
2023-08-15 16:23:18 +02:00
$this->saveDocuments($request->file('documents'), $invoice, $request->input('is_public', true));
}
2021-02-15 12:34:05 +01:00
if ($request->has('file')) {
$this->saveDocuments($request->file('file'), $invoice, $request->input('is_public', true));
}
2022-03-09 10:39:32 +01:00
2021-02-15 12:34:05 +01:00
return $this->itemResponse($invoice->fresh());
}
2022-05-17 13:03:07 +02:00
public function update_reminders(UpdateReminderRequest $request)
{
2023-08-15 01:49:09 +02:00
/** @var \App\Models\User $user */
$user = auth()->user();
UpdateReminders::dispatch($user->company());
2022-05-17 13:03:07 +02:00
return response()->json(['message' => 'Updating reminders'], 200);
2022-05-17 13:03:07 +02:00
}
}