2019-07-22 05:54:34 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2019-07-22 05:54:34 +02:00
|
|
|
*
|
|
|
|
* @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)
|
2019-07-22 05:54:34 +02:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2019-07-22 05:54:34 +02:00
|
|
|
*/
|
|
|
|
|
2019-07-23 01:25:53 +02:00
|
|
|
namespace App\Http\Controllers\ClientPortal;
|
2019-07-22 05:54:34 +02:00
|
|
|
|
2021-10-23 01:06:30 +02:00
|
|
|
use App\Utils\Ninja;
|
2019-08-28 04:36:53 +02:00
|
|
|
use App\Utils\Number;
|
2023-07-15 01:17:38 +02:00
|
|
|
use App\Models\Invoice;
|
|
|
|
use Illuminate\View\View;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use App\Models\QuoteInvitation;
|
2019-07-22 05:54:34 +02:00
|
|
|
use App\Utils\Traits\MakesHash;
|
2023-07-15 01:17:38 +02:00
|
|
|
use App\Models\CreditInvitation;
|
|
|
|
use App\Utils\Traits\MakesDates;
|
|
|
|
use App\Models\InvoiceInvitation;
|
|
|
|
use App\Http\Controllers\Controller;
|
2021-09-07 05:57:55 +02:00
|
|
|
use Illuminate\Http\RedirectResponse;
|
2023-07-15 01:17:38 +02:00
|
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
|
use Illuminate\Contracts\View\Factory;
|
|
|
|
use App\Models\PurchaseOrderInvitation;
|
2021-10-23 01:06:30 +02:00
|
|
|
use Illuminate\Support\Facades\Storage;
|
2023-07-15 01:17:38 +02:00
|
|
|
use App\Events\Invoice\InvoiceWasViewed;
|
|
|
|
use App\Events\Misc\InvitationWasViewed;
|
|
|
|
use App\Models\RecurringInvoiceInvitation;
|
|
|
|
use App\Jobs\Vendor\CreatePurchaseOrderPdf;
|
|
|
|
use App\Http\Requests\ClientPortal\Invoices\ShowInvoiceRequest;
|
|
|
|
use App\Http\Requests\ClientPortal\Invoices\ShowInvoicesRequest;
|
|
|
|
use App\Http\Requests\ClientPortal\Invoices\ProcessInvoicesInBulkRequest;
|
2019-07-22 05:54:34 +02:00
|
|
|
|
2019-07-23 05:31:53 +02:00
|
|
|
class InvoiceController extends Controller
|
2019-07-22 05:54:34 +02:00
|
|
|
{
|
2020-04-23 00:49:23 +02:00
|
|
|
use MakesHash, MakesDates;
|
2020-03-23 18:10:42 +01:00
|
|
|
|
2019-07-22 05:54:34 +02:00
|
|
|
/**
|
2020-04-23 00:49:23 +02:00
|
|
|
* Display list of invoices.
|
2019-07-22 05:54:34 +02:00
|
|
|
*
|
2020-10-28 11:10:49 +01:00
|
|
|
* @return Factory|View
|
2019-07-22 05:54:34 +02:00
|
|
|
*/
|
2021-06-17 14:43:14 +02:00
|
|
|
public function index(ShowInvoicesRequest $request)
|
2019-10-08 07:09:59 +02:00
|
|
|
{
|
2020-04-23 00:49:23 +02:00
|
|
|
return $this->render('invoices.index');
|
2019-10-08 06:04:35 +02:00
|
|
|
}
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2019-07-22 05:54:34 +02:00
|
|
|
/**
|
2020-04-23 00:49:23 +02:00
|
|
|
* Show specific invoice.
|
2019-07-22 05:54:34 +02:00
|
|
|
*
|
2020-10-28 11:10:49 +01:00
|
|
|
* @param ShowInvoiceRequest $request
|
|
|
|
* @param Invoice $invoice
|
2019-07-22 05:54:34 +02:00
|
|
|
*
|
2020-10-28 11:10:49 +01:00
|
|
|
* @return Factory|View
|
2019-07-22 05:54:34 +02:00
|
|
|
*/
|
2022-12-22 06:44:36 +01:00
|
|
|
public function show(ShowInvoiceRequest $request, Invoice $invoice, ?string $hash = null)
|
2019-07-22 05:54:34 +02:00
|
|
|
{
|
2020-03-24 10:15:30 +01:00
|
|
|
set_time_limit(0);
|
|
|
|
|
2022-02-16 00:47:54 +01:00
|
|
|
$invitation = $invoice->invitations()->where('client_contact_id', auth()->guard('contact')->user()->id)->first();
|
2021-10-23 01:06:30 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if ($invitation && auth()->guard('contact') && ! session()->get('is_silent') && ! $invitation->viewed_date) {
|
|
|
|
$invitation->markViewed();
|
2021-10-23 01:06:30 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
event(new InvitationWasViewed($invoice, $invitation, $invoice->company, Ninja::eventVars()));
|
2023-05-03 01:59:53 +02:00
|
|
|
event(new InvoiceWasViewed($invitation, $invoice->company, Ninja::eventVars()));
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2021-10-23 01:06:30 +02:00
|
|
|
|
2019-08-29 06:07:04 +02:00
|
|
|
$data = [
|
2019-09-09 04:19:19 +02:00
|
|
|
'invoice' => $invoice,
|
2022-08-28 08:27:12 +02:00
|
|
|
'invitation' => $invitation ?: $invoice->invitations->first(),
|
2022-06-21 11:57:17 +02:00
|
|
|
'key' => $invitation ? $invitation->key : false,
|
2022-12-22 05:58:18 +01:00
|
|
|
'hash' => $hash,
|
2019-08-29 06:07:04 +02:00
|
|
|
];
|
2020-03-23 18:10:42 +01:00
|
|
|
|
2021-02-18 13:18:41 +01:00
|
|
|
if ($request->query('mode') === 'fullscreen') {
|
2021-05-10 13:26:13 +02:00
|
|
|
return render('invoices.show-fullscreen', $data);
|
2020-06-18 12:48:31 +02:00
|
|
|
}
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2021-02-18 13:18:41 +01:00
|
|
|
return $this->render('invoices.show', $data);
|
2019-07-22 05:54:34 +02:00
|
|
|
}
|
|
|
|
|
2023-07-15 01:17:38 +02:00
|
|
|
public function showBlob($hash)
|
|
|
|
{
|
2023-07-17 01:47:14 +02:00
|
|
|
$data = Cache::get($hash);
|
2023-07-15 01:17:38 +02:00
|
|
|
|
|
|
|
match($data['entity_type']){
|
|
|
|
'invoice' => $invitation = InvoiceInvitation::withTrashed()->find($data['invitation_id']),
|
|
|
|
'quote' => $invitation = QuoteInvitation::withTrashed()->find($data['invitation_id']),
|
|
|
|
'credit' => $invitation = CreditInvitation::withTrashed()->find($data['invitation_id']),
|
|
|
|
'recurring_invoice' => $invitation = RecurringInvoiceInvitation::withTrashed()->find($data['invitation_id']),
|
|
|
|
};
|
|
|
|
|
|
|
|
$file = (new \App\Jobs\Entity\CreateRawPdf($invitation, $invitation->company->db))->handle();
|
|
|
|
|
|
|
|
// $headers = ['Content-Type' => 'application/pdf'];
|
|
|
|
// $entity_string = $data['entity_type'];
|
|
|
|
// $file_name = $invitation->{$entity_string}->numberFormatter().'.pdf';
|
|
|
|
// return response()->streamDownload(function () use ($file) {
|
|
|
|
// echo $file;
|
|
|
|
// }, $file_name, $headers);
|
|
|
|
|
|
|
|
$headers = ['Content-Type' => 'application/pdf'];
|
|
|
|
return response()->make($file, 200, $headers);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-07-22 05:54:34 +02:00
|
|
|
/**
|
2020-04-23 00:49:23 +02:00
|
|
|
* Pay one or more invoices.
|
2019-12-30 22:59:12 +01:00
|
|
|
*
|
2019-07-22 05:54:34 +02:00
|
|
|
*/
|
2021-12-15 01:04:10 +01:00
|
|
|
public function catch_bulk()
|
|
|
|
{
|
|
|
|
return $this->render('invoices.index');
|
|
|
|
}
|
|
|
|
|
2020-03-23 18:10:42 +01:00
|
|
|
public function bulk(ProcessInvoicesInBulkRequest $request)
|
2019-07-22 05:54:34 +02:00
|
|
|
{
|
2020-03-31 13:52:21 +02:00
|
|
|
$transformed_ids = $this->transformKeys($request->invoices);
|
2020-03-24 14:25:20 +01:00
|
|
|
|
|
|
|
if ($request->input('action') == 'payment') {
|
2020-09-06 11:38:10 +02:00
|
|
|
return $this->makePayment((array) $transformed_ids);
|
2020-03-24 14:25:20 +01:00
|
|
|
} elseif ($request->input('action') == 'download') {
|
2021-12-14 01:33:25 +01:00
|
|
|
return $this->downloadInvoices((array) $transformed_ids);
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
2020-03-23 18:10:42 +01:00
|
|
|
|
2021-01-06 14:22:48 +01:00
|
|
|
return redirect()
|
|
|
|
->back()
|
|
|
|
->with('message', ctrans('texts.no_action_provided'));
|
2019-08-15 06:31:03 +02:00
|
|
|
}
|
|
|
|
|
2021-12-14 01:33:25 +01:00
|
|
|
public function downloadInvoices($ids)
|
|
|
|
{
|
|
|
|
$data['invoices'] = Invoice::whereIn('id', $ids)
|
2022-03-16 06:29:39 +01:00
|
|
|
->whereClientId(auth()->guard('contact')->user()->client->id)
|
2021-12-14 01:33:25 +01:00
|
|
|
->withTrashed()
|
|
|
|
->get();
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if (count($data['invoices']) == 0) {
|
2021-12-14 01:33:25 +01:00
|
|
|
return back()->with(['message' => ctrans('texts.no_items_selected')]);
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2021-12-14 01:33:25 +01:00
|
|
|
|
|
|
|
return $this->render('invoices.download', $data);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function download(Request $request)
|
|
|
|
{
|
|
|
|
$transformed_ids = $this->transformKeys($request->invoices);
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2021-12-14 01:33:25 +01:00
|
|
|
return $this->downloadInvoicePDF((array) $transformed_ids);
|
|
|
|
}
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2021-09-07 05:57:55 +02:00
|
|
|
/**
|
2022-06-21 11:57:17 +02:00
|
|
|
* @param array $ids
|
|
|
|
* @return Factory|View|RedirectResponse
|
2021-09-07 05:57:55 +02:00
|
|
|
*/
|
2019-08-15 06:31:03 +02:00
|
|
|
private function makePayment(array $ids)
|
|
|
|
{
|
|
|
|
$invoices = Invoice::whereIn('id', $ids)
|
2022-03-16 06:29:39 +01:00
|
|
|
->whereClientId(auth()->guard('contact')->user()->client->id)
|
2021-06-20 00:14:56 +02:00
|
|
|
->withTrashed()
|
2019-09-05 07:04:52 +02:00
|
|
|
->get();
|
|
|
|
|
2021-01-08 04:25:54 +01:00
|
|
|
//filter invoices which are payable
|
2019-12-30 22:59:12 +01:00
|
|
|
$invoices = $invoices->filter(function ($invoice) {
|
2021-01-27 14:10:24 +01:00
|
|
|
return $invoice->isPayable();
|
2019-10-08 04:03:40 +02:00
|
|
|
});
|
|
|
|
|
2021-01-08 04:25:54 +01:00
|
|
|
//return early if no invoices.
|
2019-12-30 22:59:12 +01:00
|
|
|
if ($invoices->count() == 0) {
|
2021-01-06 14:22:48 +01:00
|
|
|
return back()
|
|
|
|
->with('message', ctrans('texts.no_payable_invoices_selected'));
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
2019-10-08 04:03:40 +02:00
|
|
|
|
2021-01-08 04:25:54 +01:00
|
|
|
//iterate and sum the payable amounts either partial or balance
|
|
|
|
$total = 0;
|
2022-06-21 11:57:17 +02:00
|
|
|
foreach ($invoices as $invoice) {
|
|
|
|
if ($invoice->partial > 0) {
|
2021-01-08 04:25:54 +01:00
|
|
|
$total += $invoice->partial;
|
2022-06-21 11:57:17 +02:00
|
|
|
} else {
|
2021-01-08 04:25:54 +01:00
|
|
|
$total += $invoice->balance;
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2021-01-08 04:25:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//format data
|
2019-12-30 22:59:12 +01:00
|
|
|
$invoices->map(function ($invoice) {
|
2022-04-02 07:13:31 +02:00
|
|
|
$invoice->service()->removeUnpaidGatewayFees();
|
2021-09-07 05:57:55 +02:00
|
|
|
$invoice->balance = $invoice->balance > 0 ? Number::formatValue($invoice->balance, $invoice->client->currency()) : 0;
|
2022-06-21 11:57:17 +02:00
|
|
|
$invoice->partial = $invoice->partial > 0 ? Number::formatValue($invoice->partial, $invoice->client->currency()) : 0;
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2019-09-05 07:04:52 +02:00
|
|
|
return $invoice;
|
|
|
|
});
|
|
|
|
|
2021-01-08 04:25:54 +01:00
|
|
|
//format totals
|
2022-03-16 06:29:39 +01:00
|
|
|
$formatted_total = Number::formatMoney($total, auth()->guard('contact')->user()->client);
|
2019-08-14 07:40:22 +02:00
|
|
|
|
2022-03-16 06:29:39 +01:00
|
|
|
$payment_methods = auth()->guard('contact')->user()->client->service()->getPaymentMethods($total);
|
2019-09-09 04:19:19 +02:00
|
|
|
|
2021-05-12 02:13:42 +02:00
|
|
|
//if there is only one payment method -> lets return straight to the payment page
|
|
|
|
|
2019-08-14 07:40:22 +02:00
|
|
|
$data = [
|
2022-03-16 06:29:39 +01:00
|
|
|
'settings' => auth()->guard('contact')->user()->client->getMergedSettings(),
|
2019-08-14 07:40:22 +02:00
|
|
|
'invoices' => $invoices,
|
2019-09-05 07:04:52 +02:00
|
|
|
'formatted_total' => $formatted_total,
|
2019-09-09 08:25:33 +02:00
|
|
|
'payment_methods' => $payment_methods,
|
2020-03-24 10:15:30 +01:00
|
|
|
'hashed_ids' => $invoices->pluck('hashed_id'),
|
2019-09-05 07:04:52 +02:00
|
|
|
'total' => $total,
|
2019-08-14 07:40:22 +02:00
|
|
|
];
|
2021-01-07 23:03:29 +01:00
|
|
|
|
2020-03-23 18:10:42 +01:00
|
|
|
return $this->render('invoices.payment', $data);
|
2019-07-22 05:54:34 +02:00
|
|
|
}
|
|
|
|
|
2020-04-23 00:49:23 +02:00
|
|
|
/**
|
|
|
|
* Helper function to download invoice PDFs.
|
|
|
|
*
|
|
|
|
* @param array $ids
|
|
|
|
*
|
|
|
|
*/
|
2019-08-15 06:31:03 +02:00
|
|
|
private function downloadInvoicePDF(array $ids)
|
|
|
|
{
|
|
|
|
$invoices = Invoice::whereIn('id', $ids)
|
2021-12-14 01:33:25 +01:00
|
|
|
->withTrashed()
|
2022-03-16 06:29:39 +01:00
|
|
|
->whereClientId(auth()->guard('contact')->user()->client->id)
|
2019-11-05 23:52:57 +01:00
|
|
|
->get();
|
2019-07-22 05:54:34 +02:00
|
|
|
|
2019-08-15 06:31:03 +02:00
|
|
|
//generate pdf's of invoices locally
|
2020-09-06 11:38:10 +02:00
|
|
|
if (! $invoices || $invoices->count() == 0) {
|
2020-08-25 08:55:55 +02:00
|
|
|
return back()->with(['message' => ctrans('texts.no_items_selected')]);
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
2019-08-15 06:31:03 +02:00
|
|
|
|
|
|
|
//if only 1 pdf, output to buffer for download
|
2019-12-30 22:59:12 +01:00
|
|
|
if ($invoices->count() == 1) {
|
2021-06-12 13:50:01 +02:00
|
|
|
$invoice = $invoices->first();
|
2021-12-09 06:34:23 +01:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
$file = $invoice->service()->getInvoicePdf(auth()->guard('contact')->user());
|
2021-12-09 06:34:23 +01:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
return response()->streamDownload(function () use ($file) {
|
|
|
|
echo Storage::get($file);
|
|
|
|
}, basename($file), ['Content-Type' => 'application/pdf']);
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
2019-11-05 23:52:57 +01:00
|
|
|
|
2022-02-18 10:55:44 +01:00
|
|
|
return $this->buildZip($invoices);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function buildZip($invoices)
|
|
|
|
{
|
|
|
|
// create new archive
|
|
|
|
$zipFile = new \PhpZip\ZipFile();
|
2022-06-21 11:57:17 +02:00
|
|
|
try {
|
2022-02-18 10:55:44 +01:00
|
|
|
foreach ($invoices as $invoice) {
|
2022-06-21 11:57:17 +02:00
|
|
|
//add it to the zip
|
2022-02-18 10:55:44 +01:00
|
|
|
$zipFile->addFromString(basename($invoice->pdf_file_path()), file_get_contents($invoice->pdf_file_path(null, 'url', true)));
|
|
|
|
}
|
|
|
|
|
|
|
|
$filename = date('Y-m-d').'_'.str_replace(' ', '_', trans('texts.invoices')).'.zip';
|
2022-06-21 11:57:17 +02:00
|
|
|
$filepath = sys_get_temp_dir().'/'.$filename;
|
2022-02-18 10:55:44 +01:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
$zipFile->saveAsFile($filepath) // save the archive to a file
|
2022-02-18 10:55:44 +01:00
|
|
|
->close(); // close archive
|
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
return response()->download($filepath, $filename)->deleteFileAfterSend(true);
|
2022-06-21 11:57:17 +02:00
|
|
|
} catch (\PhpZip\Exception\ZipException $e) {
|
2022-02-18 10:55:44 +01:00
|
|
|
// handle exception
|
2022-06-21 11:57:17 +02:00
|
|
|
} finally {
|
2022-02-18 10:55:44 +01:00
|
|
|
$zipFile->close();
|
|
|
|
}
|
2019-08-15 06:31:03 +02:00
|
|
|
}
|
2019-07-22 05:54:34 +02:00
|
|
|
}
|