1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 21:22:58 +01:00
invoiceninja/app/Http/Controllers/ClientPortal/InvoiceController.php
David Bomba ba75a44eb8
Laravel 7.x Shift (#40)
* Adopt Laravel coding style

The Laravel framework adopts the PSR-2 coding style with some additions.
Laravel apps *should* adopt this coding style as well.

However, Shift allows you to customize the adopted coding style by
adding your own [PHP CS Fixer][1] `.php_cs` config to your project.

You may use [Shift's .php_cs][2] file as a base.

[1]: https://github.com/FriendsOfPHP/PHP-CS-Fixer
[2]: https://gist.github.com/laravel-shift/cab527923ed2a109dda047b97d53c200

* Shift bindings

PHP 5.5.9+ adds the new static `class` property which provides the fully qualified class name. This is preferred over using class name strings as these references are checked by the parser.

* Shift core files

* Shift to Throwable

* Add laravel/ui dependency

* Unindent vendor mail templates

* Shift config files

* Default config files

In an effort to make upgrading the constantly changing config files
easier, Shift defaulted them so you can review the commit diff for
changes. Moving forward, you should use ENV variables or create a
separate config file to allow the core config files to remain
automatically upgradeable.

* Shift Laravel dependencies

* Shift cleanup

* Upgrade to Laravel 7

Co-authored-by: Laravel Shift <shift@laravelshift.com>
2020-09-06 19:38:10 +10:00

163 lines
4.9 KiB
PHP

<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Http\Controllers\ClientPortal;
use App\Http\Controllers\Controller;
use App\Http\Requests\ClientPortal\ProcessInvoicesInBulkRequest;
use App\Http\Requests\ClientPortal\ShowInvoiceRequest;
use App\Models\Invoice;
use App\Utils\Number;
use App\Utils\TempFile;
use App\Utils\Traits\MakesDates;
use App\Utils\Traits\MakesHash;
use ZipStream\Option\Archive;
use ZipStream\ZipStream;
class InvoiceController extends Controller
{
use MakesHash, MakesDates;
/**
* Display list of invoices.
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function index()
{
return $this->render('invoices.index');
}
/**
* Show specific invoice.
*
* @param \App\Http\Requests\ClientPortal\ShowInvoiceRequest $request
* @param \App\Models\Invoice $invoice
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function show(ShowInvoiceRequest $request, Invoice $invoice)
{
set_time_limit(0);
$data = [
'invoice' => $invoice,
];
if ($request->query('mode') === 'fullscreen') {
return $this->render('invoices.show.fullscreen', $data);
}
return $this->render('invoices.show', $data);
}
/**
* Pay one or more invoices.
*
* @param ProcessInvoicesInBulkRequest $request
* @return mixed
*/
public function bulk(ProcessInvoicesInBulkRequest $request)
{
$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);
}
return redirect()->back();
}
private function makePayment(array $ids)
{
$invoices = Invoice::whereIn('id', $ids)
->whereClientId(auth()->user()->client->id)
->get();
$total = $invoices->sum('balance');
$invoices = $invoices->filter(function ($invoice) {
return $invoice->isPayable();
});
if ($invoices->count() == 0) {
return back()->with(['warning' => 'No payable invoices selected']);
}
$invoices->map(function ($invoice) {
$invoice->balance = Number::formatValue($invoice->balance, $invoice->client->currency());
$invoice->partial = Number::formatValue($invoice->partial, $invoice->client->currency());
return $invoice;
});
$formatted_total = Number::formatMoney($total, auth()->user()->client);
$payment_methods = auth()->user()->client->getPaymentMethods($total);
$data = [
'settings' => auth()->user()->client->getMergedSettings(),
'invoices' => $invoices,
'formatted_total' => $formatted_total,
'payment_methods' => $payment_methods,
'hashed_ids' => $invoices->pluck('hashed_id'),
'total' => $total,
];
//REFACTOR entry point for online payments starts here
return $this->render('invoices.payment', $data);
}
/**
* Helper function to download invoice PDFs.
*
* @param array $ids
*
* @return void
*/
private function downloadInvoicePDF(array $ids)
{
$invoices = Invoice::whereIn('id', $ids)
->whereClientId(auth()->user()->client->id)
->get();
//generate pdf's of invoices locally
if (! $invoices || $invoices->count() == 0) {
return back()->with(['message' => ctrans('texts.no_items_selected')]);
}
//if only 1 pdf, output to buffer for download
if ($invoices->count() == 1) {
return response()->streamDownload(function () use ($invoices) {
echo file_get_contents($invoices->first()->pdf_file_path());
}, basename($invoices->first()->pdf_file_path()));
//return response()->download(TempFile::path($invoices->first()->pdf_file_path()), basename($invoices->first()->pdf_file_path()));
}
// 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) {
$zip->addFileFromPath(basename($invoice->pdf_file_path()), TempFile::path($invoice->pdf_file_path()));
}
// finish the zip stream
$zip->finish();
}
}