2019-07-22 13:54:34 +10:00
|
|
|
<?php
|
|
|
|
|
/**
|
2020-09-06 19:38:10 +10:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2019-07-22 13:54:34 +10:00
|
|
|
*
|
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
|
*
|
2022-04-27 13:20:41 +10:00
|
|
|
* @copyright Copyright (c) 2022. 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
|
|
|
*/
|
|
|
|
|
|
2019-07-23 09:25:53 +10:00
|
|
|
namespace App\Http\Controllers\ClientPortal;
|
2019-07-22 13:54:34 +10:00
|
|
|
|
2021-10-23 10:06:30 +11:00
|
|
|
use App\Events\Invoice\InvoiceWasViewed;
|
|
|
|
|
use App\Events\Misc\InvitationWasViewed;
|
2019-07-23 13:31:53 +10:00
|
|
|
use App\Http\Controllers\Controller;
|
2021-06-17 14:43:14 +02:00
|
|
|
use App\Http\Requests\ClientPortal\Invoices\ProcessInvoicesInBulkRequest;
|
|
|
|
|
use App\Http\Requests\ClientPortal\Invoices\ShowInvoiceRequest;
|
2021-10-23 10:06:30 +11:00
|
|
|
use App\Http\Requests\ClientPortal\Invoices\ShowInvoicesRequest;
|
2019-07-22 13:54:34 +10:00
|
|
|
use App\Models\Invoice;
|
2021-10-23 10:06:30 +11:00
|
|
|
use App\Utils\Ninja;
|
2019-08-28 12:36:53 +10:00
|
|
|
use App\Utils\Number;
|
2020-04-16 18:41:25 +10:00
|
|
|
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;
|
2021-09-07 13:57:55 +10:00
|
|
|
use Illuminate\Contracts\Container\BindingResolutionException;
|
2020-10-28 21:10:49 +11:00
|
|
|
use Illuminate\Contracts\View\Factory;
|
2021-09-07 13:57:55 +10:00
|
|
|
use Illuminate\Http\RedirectResponse;
|
2021-10-23 10:06:30 +11:00
|
|
|
use Illuminate\Support\Facades\Storage;
|
2020-10-28 21:10:49 +11:00
|
|
|
use Illuminate\View\View;
|
2021-12-14 11:33:25 +11:00
|
|
|
use Illuminate\Http\Request;
|
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
|
|
|
{
|
2020-04-23 00:49:23 +02:00
|
|
|
use MakesHash, MakesDates;
|
2020-03-23 18:10:42 +01:00
|
|
|
|
2019-07-22 13:54:34 +10:00
|
|
|
/**
|
2020-04-23 00:49:23 +02: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
|
|
|
*/
|
2021-06-17 14:43:14 +02:00
|
|
|
public function index(ShowInvoicesRequest $request)
|
2019-10-08 16:09:59 +11:00
|
|
|
{
|
2021-12-09 16:34:23 +11:00
|
|
|
|
2020-04-23 00:49:23 +02:00
|
|
|
return $this->render('invoices.index');
|
2019-10-08 14:04:35 +10:00
|
|
|
}
|
2020-09-06 19:38:10 +10:00
|
|
|
|
2019-07-22 13:54:34 +10:00
|
|
|
/**
|
2020-04-23 00:49:23 +02: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
|
|
|
{
|
2020-03-24 20:15:30 +11:00
|
|
|
set_time_limit(0);
|
|
|
|
|
|
2021-05-03 21:51:00 +10:00
|
|
|
$invoice->service()->removeUnpaidGatewayFees()->save();
|
|
|
|
|
|
2022-02-16 10:47:54 +11:00
|
|
|
$invitation = $invoice->invitations()->where('client_contact_id', auth()->guard('contact')->user()->id)->first();
|
2021-10-23 10:06:30 +11:00
|
|
|
|
2022-06-10 13:03:05 +10:00
|
|
|
if ($invitation && auth()->guard('contact') && !session()->get('is_silent') && ! $invitation->viewed_date) {
|
2021-10-23 10:06:30 +11:00
|
|
|
|
|
|
|
|
$invitation->markViewed();
|
|
|
|
|
|
|
|
|
|
event(new InvitationWasViewed($invoice, $invitation, $invoice->company, Ninja::eventVars()));
|
|
|
|
|
event(new InvoiceWasViewed($invitation, $invitation->company, Ninja::eventVars()));
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-29 14:07:04 +10:00
|
|
|
$data = [
|
2019-09-09 12:19:19 +10:00
|
|
|
'invoice' => $invoice,
|
2022-02-16 10:47:54 +11:00
|
|
|
'key' => $invitation ? $invitation->key : false
|
2019-08-29 14:07:04 +10: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
|
|
|
}
|
2021-02-18 13:18:41 +01:00
|
|
|
return $this->render('invoices.show', $data);
|
2019-07-22 13:54:34 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2020-04-23 00:49:23 +02:00
|
|
|
* Pay one or more invoices.
|
2019-12-31 08:59:12 +11:00
|
|
|
*
|
2020-03-23 18:10:42 +01:00
|
|
|
* @param ProcessInvoicesInBulkRequest $request
|
|
|
|
|
* @return mixed
|
2019-07-22 13:54:34 +10:00
|
|
|
*/
|
2021-12-15 11:04:10 +11: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 13:54:34 +10:00
|
|
|
{
|
2020-03-31 22:52:21 +11:00
|
|
|
$transformed_ids = $this->transformKeys($request->invoices);
|
2020-03-25 00:25:20 +11:00
|
|
|
|
|
|
|
|
if ($request->input('action') == 'payment') {
|
2020-09-06 19:38:10 +10:00
|
|
|
return $this->makePayment((array) $transformed_ids);
|
2020-03-25 00:25:20 +11:00
|
|
|
} elseif ($request->input('action') == 'download') {
|
2021-12-14 11:33:25 +11:00
|
|
|
return $this->downloadInvoices((array) $transformed_ids);
|
2019-12-31 08:59:12 +11: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 14:31:03 +10:00
|
|
|
}
|
|
|
|
|
|
2021-12-14 11:33:25 +11:00
|
|
|
public function downloadInvoices($ids)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
$data['invoices'] = Invoice::whereIn('id', $ids)
|
2022-03-16 16:29:39 +11:00
|
|
|
->whereClientId(auth()->guard('contact')->user()->client->id)
|
2021-12-14 11:33:25 +11:00
|
|
|
->withTrashed()
|
|
|
|
|
->get();
|
|
|
|
|
|
|
|
|
|
if(count($data['invoices']) == 0)
|
|
|
|
|
return back()->with(['message' => ctrans('texts.no_items_selected')]);
|
|
|
|
|
|
|
|
|
|
return $this->render('invoices.download', $data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function download(Request $request)
|
|
|
|
|
{
|
|
|
|
|
$transformed_ids = $this->transformKeys($request->invoices);
|
|
|
|
|
return $this->downloadInvoicePDF((array) $transformed_ids);
|
|
|
|
|
}
|
2021-09-07 13:57:55 +10:00
|
|
|
/**
|
|
|
|
|
* @param array $ids
|
|
|
|
|
* @return Factory|View|RedirectResponse
|
|
|
|
|
*/
|
2019-08-15 14:31:03 +10:00
|
|
|
private function makePayment(array $ids)
|
|
|
|
|
{
|
|
|
|
|
$invoices = Invoice::whereIn('id', $ids)
|
2022-03-16 16:29:39 +11:00
|
|
|
->whereClientId(auth()->guard('contact')->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
|
2019-12-31 08:59:12 +11:00
|
|
|
$invoices = $invoices->filter(function ($invoice) {
|
2021-01-27 14:10:24 +01:00
|
|
|
return $invoice->isPayable();
|
2019-10-08 12:03:40 +10:00
|
|
|
});
|
|
|
|
|
|
2021-01-08 14:25:54 +11:00
|
|
|
//return early if no invoices.
|
2019-12-31 08:59:12 +11: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-31 08:59:12 +11:00
|
|
|
}
|
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
|
2019-12-31 08:59:12 +11:00
|
|
|
$invoices->map(function ($invoice) {
|
2022-04-02 16:13:31 +11:00
|
|
|
$invoice->service()->removeUnpaidGatewayFees();
|
2021-09-07 13:57:55 +10:00
|
|
|
$invoice->balance = $invoice->balance > 0 ? Number::formatValue($invoice->balance, $invoice->client->currency()) : 0;
|
|
|
|
|
$invoice->partial = $invoice->partial > 0 ? Number::formatValue($invoice->partial, $invoice->client->currency()) : 0;
|
2020-09-06 19:38:10 +10:00
|
|
|
|
2019-09-05 15:04:52 +10:00
|
|
|
return $invoice;
|
|
|
|
|
});
|
|
|
|
|
|
2021-01-08 14:25:54 +11:00
|
|
|
//format totals
|
2022-03-16 16:29:39 +11:00
|
|
|
$formatted_total = Number::formatMoney($total, auth()->guard('contact')->user()->client);
|
2019-08-14 15:40:22 +10:00
|
|
|
|
2022-03-16 16:29:39 +11:00
|
|
|
$payment_methods = auth()->guard('contact')->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
|
|
|
|
|
|
2019-08-14 15:40:22 +10:00
|
|
|
$data = [
|
2022-03-16 16:29:39 +11:00
|
|
|
'settings' => auth()->guard('contact')->user()->client->getMergedSettings(),
|
2019-08-14 15:40:22 +10:00
|
|
|
'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,
|
2020-03-24 20:15:30 +11:00
|
|
|
'hashed_ids' => $invoices->pluck('hashed_id'),
|
2019-09-05 15:04:52 +10:00
|
|
|
'total' => $total,
|
2019-08-14 15:40:22 +10:00
|
|
|
];
|
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
|
|
|
}
|
|
|
|
|
|
2020-04-23 00:49:23 +02:00
|
|
|
/**
|
|
|
|
|
* Helper function to download invoice PDFs.
|
|
|
|
|
*
|
|
|
|
|
* @param array $ids
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2019-08-15 14:31:03 +10:00
|
|
|
private function downloadInvoicePDF(array $ids)
|
|
|
|
|
{
|
|
|
|
|
$invoices = Invoice::whereIn('id', $ids)
|
2021-12-14 11:33:25 +11:00
|
|
|
->withTrashed()
|
2022-03-16 16:29:39 +11:00
|
|
|
->whereClientId(auth()->guard('contact')->user()->client->id)
|
2019-11-06 09:52:57 +11:00
|
|
|
->get();
|
2019-07-22 13:54:34 +10:00
|
|
|
|
2019-08-15 14:31:03 +10:00
|
|
|
//generate pdf's of invoices locally
|
2020-09-06 19:38:10 +10: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-31 08:59:12 +11:00
|
|
|
}
|
2019-08-15 14:31:03 +10:00
|
|
|
|
|
|
|
|
//if only 1 pdf, output to buffer for download
|
2019-12-31 08:59:12 +11:00
|
|
|
if ($invoices->count() == 1) {
|
2021-06-12 21:50:01 +10:00
|
|
|
$invoice = $invoices->first();
|
2021-12-09 16:34:23 +11:00
|
|
|
|
2022-03-16 16:29:39 +11:00
|
|
|
$file = $invoice->service()->getInvoicePdf(auth()->guard('contact')->user());
|
2021-12-09 16:34:23 +11:00
|
|
|
|
2021-12-14 11:33:25 +11:00
|
|
|
// return response()->download(file_get_contents(public_path($file)));
|
|
|
|
|
|
2021-07-09 08:01:37 +10:00
|
|
|
return response()->streamDownload(function () use($file) {
|
|
|
|
|
echo Storage::get($file);
|
2021-07-26 08:22:29 +10:00
|
|
|
}, basename($file), ['Content-Type' => 'application/pdf']);
|
2019-12-31 08:59:12 +11:00
|
|
|
}
|
2019-11-06 09:52:57 +11:00
|
|
|
|
2022-02-18 20:55:44 +11:00
|
|
|
return $this->buildZip($invoices);
|
2019-11-06 09:52:57 +11:00
|
|
|
|
2022-02-18 20:55:44 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private function buildZip($invoices)
|
|
|
|
|
{
|
|
|
|
|
// create new archive
|
|
|
|
|
$zipFile = new \PhpZip\ZipFile();
|
|
|
|
|
try{
|
|
|
|
|
|
|
|
|
|
foreach ($invoices as $invoice) {
|
2019-11-06 09:52:57 +11:00
|
|
|
|
2022-02-18 20:55:44 +11:00
|
|
|
#add it to the zip
|
|
|
|
|
$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';
|
|
|
|
|
$filepath = sys_get_temp_dir() . '/' . $filename;
|
|
|
|
|
|
|
|
|
|
$zipFile->saveAsFile($filepath) // save the archive to a file
|
|
|
|
|
->close(); // close archive
|
|
|
|
|
|
|
|
|
|
return response()->download($filepath, $filename)->deleteFileAfterSend(true);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
catch(\PhpZip\Exception\ZipException $e){
|
|
|
|
|
// handle exception
|
|
|
|
|
}
|
|
|
|
|
finally{
|
|
|
|
|
$zipFile->close();
|
|
|
|
|
}
|
2019-08-15 14:31:03 +10:00
|
|
|
}
|
2019-07-22 13:54:34 +10:00
|
|
|
}
|