2019-07-22 05:54:34 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2019. Invoice Ninja LLC (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @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
|
|
|
|
|
|
|
use App\Filters\InvoiceFilters;
|
2019-07-23 05:31:53 +02:00
|
|
|
use App\Http\Controllers\Controller;
|
2019-08-29 06:07:04 +02:00
|
|
|
use App\Http\Requests\ClientPortal\ShowInvoiceRequest;
|
2019-07-22 05:54:34 +02:00
|
|
|
use App\Jobs\Entity\ActionEntity;
|
|
|
|
use App\Models\Invoice;
|
|
|
|
use App\Repositories\BaseRepository;
|
2019-08-28 04:36:53 +02:00
|
|
|
use App\Utils\Number;
|
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;
|
|
|
|
use Illuminate\Http\Request;
|
2019-11-05 23:52:57 +01:00
|
|
|
use Illuminate\Support\Facades\File;
|
2019-07-26 00:05:13 +02:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2019-11-05 23:52:57 +01:00
|
|
|
use Illuminate\Support\Facades\Storage;
|
2019-07-26 00:05:13 +02:00
|
|
|
use Yajra\DataTables\Facades\DataTables;
|
2019-07-25 06:51:00 +02:00
|
|
|
use Yajra\DataTables\Html\Builder;
|
2019-11-05 23:52:57 +01:00
|
|
|
use ZipStream\Option\Archive;
|
|
|
|
use ZipStream\ZipStream;
|
2019-07-22 05:54:34 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class InvoiceController
|
|
|
|
* @package App\Http\Controllers\ClientPortal\InvoiceController
|
|
|
|
*/
|
|
|
|
|
2019-07-23 05:31:53 +02:00
|
|
|
class InvoiceController extends Controller
|
2019-07-22 05:54:34 +02:00
|
|
|
{
|
|
|
|
use MakesHash;
|
2019-08-28 02:58:13 +02:00
|
|
|
use MakesDates;
|
2019-07-22 05:54:34 +02:00
|
|
|
/**
|
|
|
|
* Show the list of Invoices
|
|
|
|
*
|
|
|
|
* @param \App\Filters\InvoiceFilters $filters The filters
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
2019-07-25 06:51:00 +02:00
|
|
|
public function index(InvoiceFilters $filters, Builder $builder)
|
2019-10-08 07:09:59 +02:00
|
|
|
{
|
2019-08-29 00:13:26 +02:00
|
|
|
$invoices = Invoice::filter($filters)->with('client', 'client.country');
|
2019-07-26 00:19:01 +02:00
|
|
|
|
2019-07-25 06:51:00 +02:00
|
|
|
if (request()->ajax()) {
|
2019-08-15 07:19:00 +02:00
|
|
|
return DataTables::of($invoices)->addColumn('action', function ($invoice) {
|
2019-12-30 22:59:12 +01:00
|
|
|
return $this->buildClientButtons($invoice);
|
|
|
|
})
|
|
|
|
->addColumn('checkbox', function ($invoice) {
|
2019-07-30 00:28:38 +02:00
|
|
|
return '<input type="checkbox" name="hashed_ids[]" value="'. $invoice->hashed_id .'"/>';
|
2019-07-26 00:05:13 +02:00
|
|
|
})
|
2019-12-30 22:59:12 +01:00
|
|
|
->editColumn('status_id', function ($invoice) {
|
2019-08-14 04:16:09 +02:00
|
|
|
return Invoice::badgeForStatus($invoice->status);
|
2019-12-30 22:59:12 +01:00
|
|
|
})->editColumn('date', function ($invoice) {
|
2019-11-27 11:27:24 +01:00
|
|
|
return $this->formatDate($invoice->date, $invoice->client->date_format());
|
2019-12-30 22:59:12 +01:00
|
|
|
})->editColumn('due_date', function ($invoice) {
|
2019-08-29 00:13:26 +02:00
|
|
|
return $this->formatDate($invoice->due_date, $invoice->client->date_format());
|
2019-08-28 04:36:53 +02:00
|
|
|
})->editColumn('balance', function ($invoice) {
|
2019-09-11 05:46:23 +02:00
|
|
|
return Number::formatMoney($invoice->balance, $invoice->client);
|
2019-08-28 04:36:53 +02:00
|
|
|
})->editColumn('amount', function ($invoice) {
|
2019-09-11 05:46:23 +02:00
|
|
|
return Number::formatMoney($invoice->amount, $invoice->client);
|
2019-08-28 01:19:54 +02:00
|
|
|
})
|
2019-08-14 04:16:09 +02:00
|
|
|
->rawColumns(['checkbox', 'action', 'status_id'])
|
2019-07-25 06:51:00 +02:00
|
|
|
->make(true);
|
|
|
|
}
|
|
|
|
|
2019-08-14 07:40:22 +02:00
|
|
|
$data['html'] = $builder;
|
2019-07-22 05:54:34 +02:00
|
|
|
|
2019-07-25 06:51:00 +02:00
|
|
|
return view('portal.default.invoices.index', $data);
|
2019-07-22 05:54:34 +02:00
|
|
|
}
|
|
|
|
|
2019-10-08 06:04:35 +02:00
|
|
|
private function buildClientButtons($invoice)
|
|
|
|
{
|
|
|
|
$buttons = '<div>';
|
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
if ($invoice->isPayable()) {
|
2019-10-08 07:09:59 +02:00
|
|
|
$buttons .= "<button type=\"button\" class=\"btn btn-sm btn-info\" onclick=\"payInvoice('".$invoice->hashed_id."')\"><i class=\"glyphicon glyphicon-edit\"></i>".ctrans('texts.pay_now')."</button>";
|
2019-12-30 22:59:12 +01:00
|
|
|
// $buttons .= '<a href="/client/invoices/'. $invoice->hashed_id .'" class="btn btn-sm btn-info"><i class="glyphicon glyphicon-edit"></i>'.ctrans('texts.pay_now').'</a>';
|
2019-10-08 06:04:35 +02:00
|
|
|
}
|
2019-10-08 07:09:59 +02:00
|
|
|
|
2019-10-08 06:04:35 +02:00
|
|
|
$buttons .= '<a href="/client/invoices/'. $invoice->hashed_id .'" class="btn btn-sm btn-primary"><i class="glyphicon glyphicon-edit"></i>'.ctrans('texts.view').'</a>';
|
|
|
|
|
|
|
|
$buttons .="</div>";
|
|
|
|
|
|
|
|
return $buttons;
|
|
|
|
}
|
2019-07-22 05:54:34 +02:00
|
|
|
/**
|
|
|
|
* Display the specified resource.
|
|
|
|
*
|
|
|
|
* @param \App\Models\Invoice $invoice The invoice
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
2019-08-29 06:07:04 +02:00
|
|
|
public function show(ShowInvoiceRequest $request, Invoice $invoice)
|
2019-07-22 05:54:34 +02:00
|
|
|
{
|
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
|
|
|
];
|
|
|
|
|
2019-09-05 01:52:49 +02:00
|
|
|
return view('portal.default.invoices.show', $data);
|
2019-07-22 05:54:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-08-14 07:40:22 +02:00
|
|
|
* Pay one or more invoices
|
2019-12-30 22:59:12 +01:00
|
|
|
*
|
2019-08-14 07:40:22 +02:00
|
|
|
* @return View
|
2019-07-22 05:54:34 +02:00
|
|
|
*/
|
2019-08-14 12:23:44 +02:00
|
|
|
public function bulk()
|
2019-07-22 05:54:34 +02:00
|
|
|
{
|
2019-12-30 22:59:12 +01:00
|
|
|
$transformed_ids = $this->transformKeys(explode(",", request()->input('hashed_ids')));
|
2019-07-22 05:54:34 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
if (request()->input('action') == 'payment') {
|
2019-08-15 06:31:03 +02:00
|
|
|
return $this->makePayment($transformed_ids);
|
2019-12-30 22:59:12 +01:00
|
|
|
} elseif (request()->input('action') == 'download') {
|
2019-08-15 06:31:03 +02:00
|
|
|
return $this->downloadInvoicePDF($transformed_ids);
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
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) {
|
2019-09-11 08:35:31 +02:00
|
|
|
$invoice->balance = Number::formatMoney($invoice->balance, $invoice->client);
|
2019-09-05 07:04:52 +02:00
|
|
|
$invoice->due_date = $this->formatDate($invoice->due_date, $invoice->client->date_format());
|
|
|
|
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,
|
2019-10-08 05:06:27 +02:00
|
|
|
'hashed_ids' => $invoices->pluck('hashed_ids'),
|
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
|
|
|
|
2019-08-14 07:40:22 +02:00
|
|
|
return view('portal.default.invoices.payment', $data);
|
2019-07-22 05:54:34 +02:00
|
|
|
}
|
|
|
|
|
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
|
2019-12-30 22:59:12 +01:00
|
|
|
if (!$invoices || $invoices->count() == 0) {
|
2019-11-05 23:52:57 +01:00
|
|
|
return;
|
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) {
|
2019-11-05 23:52:57 +01:00
|
|
|
return response()->download(public_path($invoices->first()->pdf_file_path()));
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
2019-11-05 23:52:57 +01:00
|
|
|
|
|
|
|
|
|
|
|
# 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);
|
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
foreach ($invoices as $invoice) {
|
|
|
|
$zip->addFileFromPath(basename($invoice->pdf_file_path()), public_path($invoice->pdf_file_path()));
|
|
|
|
}
|
2019-11-05 23:52:57 +01:00
|
|
|
|
|
|
|
# finish the zip stream
|
|
|
|
$zip->finish();
|
2019-08-15 06:31:03 +02:00
|
|
|
}
|
2019-07-22 05:54:34 +02:00
|
|
|
}
|