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
|
|
|
|
*
|
2020-01-07 01:13:47 +01:00
|
|
|
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
|
2019-07-22 05:54:34 +02:00
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
|
|
|
|
2019-07-23 01:25:53 +02:00
|
|
|
namespace App\Http\Controllers\ClientPortal;
|
2019-07-22 05:54:34 +02:00
|
|
|
|
2019-07-23 05:31:53 +02:00
|
|
|
use App\Http\Controllers\Controller;
|
2020-03-23 18:10:42 +01:00
|
|
|
use App\Http\Requests\ClientPortal\ProcessInvoicesInBulkRequest;
|
2019-08-29 06:07:04 +02:00
|
|
|
use App\Http\Requests\ClientPortal\ShowInvoiceRequest;
|
2019-07-22 05:54:34 +02:00
|
|
|
use App\Models\Invoice;
|
2019-08-28 04:36:53 +02:00
|
|
|
use App\Utils\Number;
|
2020-04-16 10:41:25 +02:00
|
|
|
use App\Utils\TempFile;
|
2019-08-28 02:58:13 +02:00
|
|
|
use App\Utils\Traits\MakesDates;
|
2019-07-22 05:54:34 +02:00
|
|
|
use App\Utils\Traits\MakesHash;
|
2019-11-05 23:52:57 +01:00
|
|
|
use ZipStream\Option\Archive;
|
|
|
|
use ZipStream\ZipStream;
|
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-03-23 18:10:42 +01:00
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
2019-07-22 05:54:34 +02:00
|
|
|
*/
|
2020-04-23 00:49:23 +02:00
|
|
|
public function index()
|
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-04-23 00:49:23 +02:00
|
|
|
* @param \App\Http\Requests\ClientPortal\ShowInvoiceRequest $request
|
|
|
|
* @param \App\Models\Invoice $invoice
|
2019-07-22 05:54:34 +02:00
|
|
|
*
|
2020-03-23 18:10:42 +01:00
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
2019-07-22 05:54:34 +02:00
|
|
|
*/
|
2019-08-29 06:07:04 +02:00
|
|
|
public function show(ShowInvoiceRequest $request, Invoice $invoice)
|
2019-07-22 05:54:34 +02:00
|
|
|
{
|
2020-03-24 10:15:30 +01:00
|
|
|
set_time_limit(0);
|
|
|
|
|
2019-08-29 06:07:04 +02:00
|
|
|
$data = [
|
2019-09-09 04:19:19 +02:00
|
|
|
'invoice' => $invoice,
|
2019-08-29 06:07:04 +02:00
|
|
|
];
|
2020-03-23 18:10:42 +01:00
|
|
|
|
2020-06-18 12:48:31 +02:00
|
|
|
if ($request->query('mode') === 'fullscreen') {
|
|
|
|
return $this->render('invoices.show.fullscreen', $data);
|
|
|
|
}
|
|
|
|
|
2020-03-23 18:10:42 +01:00
|
|
|
return $this->render('invoices.show', $data);
|
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
|
|
|
*
|
2020-03-23 18:10:42 +01:00
|
|
|
* @param ProcessInvoicesInBulkRequest $request
|
|
|
|
* @return mixed
|
2019-07-22 05:54:34 +02:00
|
|
|
*/
|
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') {
|
2020-09-06 11:38:10 +02:00
|
|
|
return $this->downloadInvoicePDF((array) $transformed_ids);
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
2020-03-23 18:10:42 +01:00
|
|
|
|
|
|
|
return redirect()->back();
|
2019-08-15 06:31:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private function makePayment(array $ids)
|
|
|
|
{
|
|
|
|
$invoices = Invoice::whereIn('id', $ids)
|
2019-08-14 07:40:22 +02:00
|
|
|
->whereClientId(auth()->user()->client->id)
|
2019-09-05 07:04:52 +02:00
|
|
|
->get();
|
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
$total = $invoices->sum('balance');
|
2019-09-21 06:09:25 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
$invoices = $invoices->filter(function ($invoice) {
|
2019-09-05 07:04:52 +02:00
|
|
|
return $invoice->isPayable();
|
2019-10-08 04:03:40 +02:00
|
|
|
});
|
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
if ($invoices->count() == 0) {
|
2019-10-08 04:03:40 +02:00
|
|
|
return back()->with(['warning' => 'No payable invoices selected']);
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
2019-10-08 04:03:40 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
$invoices->map(function ($invoice) {
|
2020-08-26 02:47:50 +02:00
|
|
|
$invoice->balance = Number::formatValue($invoice->balance, $invoice->client->currency());
|
|
|
|
$invoice->partial = Number::formatValue($invoice->partial, $invoice->client->currency());
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2019-09-05 07:04:52 +02:00
|
|
|
return $invoice;
|
|
|
|
});
|
|
|
|
|
2019-09-11 07:32:47 +02:00
|
|
|
$formatted_total = Number::formatMoney($total, auth()->user()->client);
|
2019-08-14 07:40:22 +02:00
|
|
|
|
2019-09-09 04:19:19 +02:00
|
|
|
$payment_methods = auth()->user()->client->getPaymentMethods($total);
|
|
|
|
|
2019-08-14 07:40:22 +02:00
|
|
|
$data = [
|
2019-09-21 06:09:25 +02:00
|
|
|
'settings' => auth()->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
|
|
|
];
|
2019-08-14 12:23:44 +02:00
|
|
|
|
2020-08-25 15:06:38 +02:00
|
|
|
//REFACTOR entry point for online payments starts here
|
|
|
|
|
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
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2019-08-15 06:31:03 +02:00
|
|
|
private function downloadInvoicePDF(array $ids)
|
|
|
|
{
|
|
|
|
$invoices = Invoice::whereIn('id', $ids)
|
|
|
|
->whereClientId(auth()->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) {
|
2020-09-06 11:38:10 +02:00
|
|
|
return response()->streamDownload(function () use ($invoices) {
|
2020-06-24 10:59:56 +02:00
|
|
|
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()));
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
2019-11-05 23:52:57 +01:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
// enable output of HTTP headers
|
2019-11-05 23:52:57 +01:00
|
|
|
$options = new Archive();
|
|
|
|
$options->setSendHttpHeaders(true);
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
// create a new zipstream object
|
|
|
|
$zip = new ZipStream(date('Y-m-d').'_'.str_replace(' ', '_', trans('texts.invoices')).'.zip', $options);
|
2019-11-05 23:52:57 +01:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
foreach ($invoices as $invoice) {
|
2020-04-16 10:41:25 +02:00
|
|
|
$zip->addFileFromPath(basename($invoice->pdf_file_path()), TempFile::path($invoice->pdf_file_path()));
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
2019-11-05 23:52:57 +01:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
// finish the zip stream
|
2019-11-05 23:52:57 +01:00
|
|
|
$zip->finish();
|
2019-08-15 06:31:03 +02:00
|
|
|
}
|
2019-07-22 05:54:34 +02:00
|
|
|
}
|