invoiceninja/app/Http/Controllers/ClientPortal/InvoiceController.php

198 lines
6.1 KiB
PHP
Raw Permalink Normal View History

2019-07-22 13:54:34 +10:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
2019-07-22 13:54:34 +10:00
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
2019-07-22 13:54:34 +10:00
*
2021-06-16 16:58:16 +10:00
* @license https://www.elastic.co/licensing/elastic-license
2019-07-22 13:54:34 +10:00
*/
namespace App\Http\Controllers\ClientPortal;
2019-07-22 13:54:34 +10:00
2019-07-23 13:31:53 +10:00
use App\Http\Controllers\Controller;
use App\Http\Requests\ClientPortal\Invoices\ShowInvoicesRequest;
use App\Http\Requests\ClientPortal\Invoices\ProcessInvoicesInBulkRequest;
use App\Http\Requests\ClientPortal\Invoices\ShowInvoiceRequest;
2019-07-22 13:54:34 +10:00
use App\Models\Invoice;
2019-08-28 12:36:53 +10:00
use App\Utils\Number;
use App\Utils\TempFile;
2019-08-28 10:58:13 +10:00
use App\Utils\Traits\MakesDates;
2019-07-22 13:54:34 +10:00
use App\Utils\Traits\MakesHash;
2020-10-28 21:10:49 +11:00
use Illuminate\Contracts\View\Factory;
use Illuminate\View\View;
use ZipStream\Option\Archive;
use ZipStream\ZipStream;
use Illuminate\Support\Facades\Storage;
2019-07-22 13:54:34 +10:00
2019-07-23 13:31:53 +10:00
class InvoiceController extends Controller
2019-07-22 13:54:34 +10:00
{
use MakesHash, MakesDates;
2020-03-23 18:10:42 +01:00
2019-07-22 13:54:34 +10:00
/**
* Display list of invoices.
2019-07-22 13:54:34 +10:00
*
2020-10-28 21:10:49 +11:00
* @return Factory|View
2019-07-22 13:54:34 +10:00
*/
public function index(ShowInvoicesRequest $request)
{
return $this->render('invoices.index');
2019-10-08 14:04:35 +10:00
}
2019-07-22 13:54:34 +10:00
/**
* Show specific invoice.
2019-07-22 13:54:34 +10:00
*
2020-10-28 21:10:49 +11:00
* @param ShowInvoiceRequest $request
* @param Invoice $invoice
2019-07-22 13:54:34 +10:00
*
2020-10-28 21:10:49 +11:00
* @return Factory|View
2019-07-22 13:54:34 +10:00
*/
2019-08-29 14:07:04 +10:00
public function show(ShowInvoiceRequest $request, Invoice $invoice)
2019-07-22 13:54:34 +10:00
{
set_time_limit(0);
2021-05-03 21:51:00 +10:00
$invoice->service()->removeUnpaidGatewayFees()->save();
2019-08-29 14:07:04 +10:00
$data = [
2019-09-09 12:19:19 +10:00
'invoice' => $invoice,
2019-08-29 14:07:04 +10:00
];
2020-03-23 18:10:42 +01:00
if ($request->query('mode') === 'fullscreen') {
2021-05-10 13:26:13 +02:00
return render('invoices.show-fullscreen', $data);
}
return $this->render('invoices.show', $data);
2019-07-22 13:54:34 +10:00
}
/**
* Pay one or more invoices.
*
2020-03-23 18:10:42 +01:00
* @param ProcessInvoicesInBulkRequest $request
* @return mixed
2019-07-22 13:54:34 +10:00
*/
2020-03-23 18:10:42 +01:00
public function bulk(ProcessInvoicesInBulkRequest $request)
2019-07-22 13:54:34 +10:00
{
$transformed_ids = $this->transformKeys($request->invoices);
if ($request->input('action') == 'payment') {
return $this->makePayment((array) $transformed_ids);
} elseif ($request->input('action') == 'download') {
return $this->downloadInvoicePDF((array) $transformed_ids);
}
2020-03-23 18:10:42 +01:00
return redirect()
->back()
->with('message', ctrans('texts.no_action_provided'));
2019-08-15 14:31:03 +10:00
}
private function makePayment(array $ids)
{
$invoices = Invoice::whereIn('id', $ids)
->whereClientId(auth()->user()->client->id)
2021-06-20 08:14:56 +10:00
->withTrashed()
2019-09-05 15:04:52 +10:00
->get();
2021-01-08 14:25:54 +11:00
//filter invoices which are payable
$invoices = $invoices->filter(function ($invoice) {
return $invoice->isPayable();
2019-10-08 12:03:40 +10:00
});
2021-01-08 14:25:54 +11:00
//return early if no invoices.
if ($invoices->count() == 0) {
return back()
->with('message', ctrans('texts.no_payable_invoices_selected'));
}
2019-10-08 12:03:40 +10:00
2021-01-08 14:25:54 +11:00
//iterate and sum the payable amounts either partial or balance
$total = 0;
foreach($invoices as $invoice)
{
if($invoice->partial > 0)
$total += $invoice->partial;
else
$total += $invoice->balance;
}
//format data
$invoices->map(function ($invoice) {
$invoice->service()->removeUnpaidGatewayFees()->save();
2020-08-26 10:47:50 +10:00
$invoice->balance = Number::formatValue($invoice->balance, $invoice->client->currency());
$invoice->partial = Number::formatValue($invoice->partial, $invoice->client->currency());
2019-09-05 15:04:52 +10:00
return $invoice;
});
2021-01-08 14:25:54 +11:00
//format totals
2019-09-11 15:32:47 +10:00
$formatted_total = Number::formatMoney($total, auth()->user()->client);
2021-01-18 13:59:06 +11:00
$payment_methods = auth()->user()->client->service()->getPaymentMethods($total);
2019-09-09 12:19:19 +10:00
2021-05-12 10:13:42 +10:00
//if there is only one payment method -> lets return straight to the payment page
$data = [
'settings' => auth()->user()->client->getMergedSettings(),
'invoices' => $invoices,
2019-09-05 15:04:52 +10:00
'formatted_total' => $formatted_total,
2019-09-09 16:25:33 +10:00
'payment_methods' => $payment_methods,
'hashed_ids' => $invoices->pluck('hashed_id'),
2019-09-05 15:04:52 +10:00
'total' => $total,
];
2021-01-08 09:03:29 +11:00
2020-03-23 18:10:42 +01:00
return $this->render('invoices.payment', $data);
2019-07-22 13:54:34 +10:00
}
/**
* Helper function to download invoice PDFs.
*
* @param array $ids
*
* @return void
2020-10-28 21:10:49 +11:00
* @throws \ZipStream\Exception\FileNotFoundException
* @throws \ZipStream\Exception\FileNotReadableException
* @throws \ZipStream\Exception\OverflowException
*/
2019-08-15 14:31:03 +10:00
private function downloadInvoicePDF(array $ids)
{
$invoices = Invoice::whereIn('id', $ids)
->whereClientId(auth()->user()->client->id)
->get();
2019-07-22 13:54:34 +10:00
2019-08-15 14:31:03 +10:00
//generate pdf's of invoices locally
if (! $invoices || $invoices->count() == 0) {
return back()->with(['message' => ctrans('texts.no_items_selected')]);
}
2019-08-15 14:31:03 +10:00
//if only 1 pdf, output to buffer for download
if ($invoices->count() == 1) {
2021-06-12 21:50:01 +10:00
$invoice = $invoices->first();
$invitation = $invoice->invitations->first();
//$file = $invoice->pdf_file_path($invitation);
$file = $invoice->service()->getInvoicePdf(auth()->user());
// return response()->download($file, basename($file), ['Cache-Control:' => 'no-cache'])->deleteFileAfterSend(true);;
return response()->streamDownload(function () use($file) {
echo Storage::get($file);
}, basename($file), ['Content-Type' => 'application/pdf']);
}
// enable output of HTTP headers
$options = new Archive();
$options->setSendHttpHeaders(true);
// create a new zipstream object
$zip = new ZipStream(date('Y-m-d').'_'.str_replace(' ', '_', trans('texts.invoices')).'.zip', $options);
foreach ($invoices as $invoice) {
2021-07-30 10:37:32 +10:00
#add it to the zip
$zip->addFile(basename($invoice->pdf_file_path()), file_get_contents($invoice->pdf_file_path(null, 'url', true)));
}
// finish the zip stream
$zip->finish();
2019-08-15 14:31:03 +10:00
}
2019-07-22 13:54:34 +10:00
}