1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-11 21:52:35 +01:00
invoiceninja/app/Http/Controllers/ClientPortal/InvitationController.php

345 lines
13 KiB
PHP
Raw Normal View History

2019-09-23 07:59:01 +02:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
2019-09-23 07:59:01 +02:00
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2023-01-28 23:21:40 +01:00
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
2019-09-23 07:59:01 +02:00
*
2021-06-16 08:58:16 +02:00
* @license https://www.elastic.co/licensing/elastic-license
2019-09-23 07:59:01 +02:00
*/
namespace App\Http\Controllers\ClientPortal;
2023-10-26 04:57:44 +02:00
use App\Events\Contact\ContactLoggedIn;
use App\Events\Credit\CreditWasViewed;
use App\Events\Invoice\InvoiceWasViewed;
use App\Events\Misc\InvitationWasViewed;
use App\Events\Quote\QuoteWasViewed;
use App\Http\Controllers\Controller;
use App\Jobs\Entity\CreateRawPdf;
2021-08-30 14:04:51 +02:00
use App\Models\ClientContact;
use App\Models\CreditInvitation;
2021-10-05 12:29:32 +02:00
use App\Models\InvoiceInvitation;
2023-10-26 04:57:44 +02:00
use App\Models\Payment;
2022-06-02 04:41:44 +02:00
use App\Models\PurchaseOrderInvitation;
2023-10-26 04:57:44 +02:00
use App\Models\QuoteInvitation;
2021-10-06 05:47:17 +02:00
use App\Services\ClientPortal\InstantPayment;
2023-10-26 04:57:44 +02:00
use App\Utils\Ninja;
use App\Utils\Traits\MakesDates;
use App\Utils\Traits\MakesHash;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
2019-09-23 07:59:01 +02:00
/**
* Class InvitationController.
2019-09-23 07:59:01 +02:00
*/
class InvitationController extends Controller
{
use MakesHash;
use MakesDates;
public function router(string $entity, string $invitation_key)
2022-06-02 04:41:44 +02:00
{
Auth::logout();
return $this->genericRouter($entity, $invitation_key);
}
public function recurringRouter(string $invitation_key)
{
return $this->genericRouter('recurring_invoice', $invitation_key);
}
public function invoiceRouter(string $invitation_key)
{
return $this->genericRouter('invoice', $invitation_key);
}
public function quoteRouter(string $invitation_key)
{
return $this->genericRouter('quote', $invitation_key);
}
public function creditRouter(string $invitation_key)
{
return $this->genericRouter('credit', $invitation_key);
}
private function genericRouter(string $entity, string $invitation_key)
2019-09-23 07:59:01 +02:00
{
2023-02-16 02:36:09 +01:00
if (!in_array($entity, ['invoice', 'credit', 'quote', 'recurring_invoice'])) {
2021-10-05 06:20:44 +02:00
return response()->json(['message' => 'Invalid resource request']);
2023-02-16 02:36:09 +01:00
}
2021-10-05 06:20:44 +02:00
2021-12-02 03:58:55 +01:00
$is_silent = 'false';
$key = $entity.'_id';
2020-11-11 01:13:39 +01:00
$entity_obj = 'App\Models\\'.ucfirst(Str::camel($entity)).'Invitation';
2019-09-23 07:59:01 +02:00
2022-06-17 01:08:07 +02:00
$invitation = $entity_obj::withTrashed()
->with($entity)
2022-06-17 01:08:07 +02:00
->where('key', $invitation_key)
->with('contact.client')
2022-07-31 20:53:39 +02:00
->firstOrFail();
2021-11-08 00:17:49 +01:00
if ($invitation->trashed() || $invitation->{$entity}->is_deleted) {
return $this->render('generic.not_available', ['account' => $invitation->company->account, 'company' => $invitation->company]);
2023-02-16 02:36:09 +01:00
}
2019-09-23 07:59:01 +02:00
2023-02-16 02:36:09 +01:00
if ($invitation->contact->trashed()) {
$invitation->contact->restore();
2023-02-16 02:36:09 +01:00
}
2021-06-08 13:45:23 +02:00
$client_contact = $invitation->contact;
2023-08-10 09:24:50 +02:00
event(new ContactLoggedIn($client_contact, $client_contact->company, Ninja::eventVars()));
2021-06-08 13:45:23 +02:00
2023-02-16 02:36:09 +01:00
if (empty($client_contact->email)) {
$client_contact->email = Str::random(15) . "@example.com";
$client_contact->save();
2023-10-26 04:57:44 +02:00
}
2024-01-14 05:05:00 +01:00
2020-11-25 15:19:52 +01:00
if (request()->has('client_hash') && request()->input('client_hash') == $invitation->contact->client->client_hash) {
2022-01-25 03:43:44 +01:00
request()->session()->invalidate();
auth()->guard('contact')->loginUsingId($client_contact->id, true);
2020-11-25 15:19:52 +01:00
} elseif ((bool) $invitation->contact->client->getSetting('enable_client_portal_password') !== false) {
2022-02-08 13:29:59 +01:00
//if no contact password has been set - allow user to set password - then continue to view entity
2023-02-16 02:36:09 +01:00
if (empty($invitation->contact->password)) {
return $this->render('view_entity.set_password', [
'root' => 'themes',
'entity_type' => $entity,
'invitation_key' => $invitation_key
]);
2022-02-08 13:29:59 +01:00
}
$this->middleware('auth:contact');
return redirect()->route('client.login');
2020-11-25 15:19:52 +01:00
} else {
2022-01-25 03:43:44 +01:00
request()->session()->invalidate();
auth()->guard('contact')->loginUsingId($client_contact->id, true);
}
2022-02-28 06:50:25 +01:00
if (auth()->guard('contact')->user() && ! request()->has('silent') && ! $invitation->viewed_date) {
$invitation->markViewed();
2020-07-17 03:18:40 +02:00
2023-02-16 02:36:09 +01:00
if (!session()->get('is_silent')) {
event(new InvitationWasViewed($invitation->{$entity}, $invitation, $invitation->{$entity}->company, Ninja::eventVars()));
2023-02-16 02:36:09 +01:00
}
2023-02-16 02:36:09 +01:00
if (!session()->get('is_silent')) {
$this->fireEntityViewedEvent($invitation, $entity);
2023-02-16 02:36:09 +01:00
}
} else {
2021-12-02 03:58:55 +01:00
$is_silent = 'true';
2023-07-11 13:16:15 +02:00
return redirect()->route('client.'.$entity.'.show', [$entity => $this->encodePrimaryKey($invitation->{$key}), 'silent' => $is_silent]);
2023-04-26 07:44:54 +02:00
return redirect()->route('client.'.$entity.'.show', [$entity => $this->encodePrimaryKey($invitation->{$key}), 'silent' => $is_silent])->header('Cache-Control', 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
2021-12-02 03:58:55 +01:00
}
2023-07-11 13:16:15 +02:00
return redirect()->route('client.'.$entity.'.show', [$entity => $this->encodePrimaryKey($invitation->{$key})]);
2023-04-26 07:44:54 +02:00
return redirect()->route('client.'.$entity.'.show', [$entity => $this->encodePrimaryKey($invitation->{$key})])->header('Cache-Control', 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
}
2020-07-17 03:18:40 +02:00
private function fireEntityViewedEvent($invitation, $entity_string)
{
switch ($entity_string) {
case 'invoice':
event(new InvoiceWasViewed($invitation, $invitation->company, Ninja::eventVars()));
break;
2020-08-12 04:02:21 +02:00
case 'quote':
event(new QuoteWasViewed($invitation, $invitation->company, Ninja::eventVars()));
break;
case 'credit':
event(new CreditWasViewed($invitation, $invitation->company, Ninja::eventVars()));
break;
2020-07-17 03:18:40 +02:00
default:
// code...
2020-07-17 03:18:40 +02:00
break;
}
}
public function routerForDownload(string $entity, string $invitation_key)
{
2022-05-07 01:50:25 +02:00
set_time_limit(45);
2022-06-02 04:41:44 +02:00
2023-11-14 01:13:33 +01:00
// if (Ninja::isHosted()) {
2023-11-26 08:41:42 +01:00
return $this->returnRawPdf($entity, $invitation_key);
2023-11-14 01:13:33 +01:00
// }
2021-09-27 03:40:17 +02:00
2023-11-14 01:13:33 +01:00
// return redirect('client/'.$entity.'/'.$invitation_key.'/download_pdf');
2019-09-23 07:59:01 +02:00
}
2021-09-27 03:40:17 +02:00
private function returnRawPdf(string $entity, string $invitation_key)
{
2023-02-16 02:36:09 +01:00
if (!in_array($entity, ['invoice', 'credit', 'quote', 'recurring_invoice'])) {
2021-10-05 06:20:44 +02:00
return response()->json(['message' => 'Invalid resource request']);
2023-02-16 02:36:09 +01:00
}
2021-10-05 06:20:44 +02:00
2021-09-27 03:40:17 +02:00
$key = $entity.'_id';
$entity_obj = 'App\Models\\'.ucfirst(Str::camel($entity)).'Invitation';
2022-06-17 01:08:07 +02:00
$invitation = $entity_obj::withTrashed()
->where('key', $invitation_key)
2021-09-27 03:40:17 +02:00
->with('contact.client')
->firstOrFail();
2023-02-16 02:36:09 +01:00
if (!$invitation) {
2021-09-27 03:40:17 +02:00
return response()->json(["message" => "no record found"], 400);
2023-02-16 02:36:09 +01:00
}
2021-09-27 03:40:17 +02:00
$file_name = $invitation->{$entity}->numberFormatter().'.pdf';
2023-10-26 03:25:56 +02:00
$file = (new CreateRawPdf($invitation))->handle();
2021-09-27 03:40:17 +02:00
$headers = ['Content-Type' => 'application/pdf'];
2023-02-16 02:36:09 +01:00
if (request()->input('inline') == 'true') {
2021-09-27 03:40:17 +02:00
$headers = array_merge($headers, ['Content-Disposition' => 'inline']);
2023-02-16 02:36:09 +01:00
}
2021-09-27 03:40:17 +02:00
2023-02-16 02:36:09 +01:00
return response()->streamDownload(function () use ($file) {
echo $file;
}, $file_name, $headers);
2021-09-27 03:40:17 +02:00
}
public function routerForIframe(string $entity, string $client_hash, string $invitation_key)
{
}
2021-08-30 14:04:51 +02:00
2023-10-26 03:25:56 +02:00
2023-10-26 04:57:44 +02:00
public function handlePasswordSet(Request $request)
2023-10-26 03:25:56 +02:00
{
$entity_obj = 'App\Models\\'.ucfirst(Str::camel($request->entity_type)).'Invitation';
$key = $request->entity_type.'_id';
$invitation = $entity_obj::where('key', $request->invitation_key)
->whereHas($request->entity_type, function ($query) {
$query->where('is_deleted', 0);
})
->with('contact.client')
->firstOrFail();
$contact = $invitation->contact;
$contact->password = Hash::make($request->password);
$contact->save();
$request->session()->invalidate();
auth()->guard('contact')->loginUsingId($contact->id, true);
if (! $invitation->viewed_date) {
$invitation->markViewed();
if (! session()->get('is_silent')) {
event(new InvitationWasViewed($invitation->{$request->entity_type}, $invitation, $invitation->{$request->entity_type}->company, Ninja::eventVars()));
}
if (! session()->get('is_silent')) {
$this->fireEntityViewedEvent($invitation, $request->entity_type);
}
}
return redirect()->route('client.'.$request->entity_type.'.show', [$request->entity_type => $this->encodePrimaryKey($invitation->{$key})]);
}
2021-08-30 14:04:51 +02:00
public function paymentRouter(string $contact_key, string $payment_id)
{
2023-08-04 08:40:44 +02:00
/** @var \App\Models\ClientContact $contact **/
$contact = ClientContact::withTrashed()->where('contact_key', $contact_key)->firstOrFail();
2024-01-14 05:05:00 +01:00
2023-08-04 08:40:44 +02:00
/** @var \App\Models\Payment $payment **/
2021-08-30 14:04:51 +02:00
$payment = Payment::find($this->decodePrimaryKey($payment_id));
2023-02-16 02:36:09 +01:00
if ($payment->client_id != $contact->client_id) {
2021-08-30 14:04:51 +02:00
abort(403, 'You are not authorized to view this resource');
2023-02-16 02:36:09 +01:00
}
2021-08-30 14:04:51 +02:00
auth()->guard('contact')->loginUsingId($contact->id, true);
2021-08-30 14:04:51 +02:00
return redirect()->route('client.payments.show', $payment->hashed_id);
}
2021-10-05 12:29:32 +02:00
2021-10-06 05:47:17 +02:00
public function payInvoice(Request $request, string $invitation_key)
2021-10-05 12:29:32 +02:00
{
2022-06-17 01:08:07 +02:00
$invitation = InvoiceInvitation::withTrashed()
->where('key', $invitation_key)
2021-10-05 12:29:32 +02:00
->with('contact.client')
->firstOrFail();
2022-06-02 04:41:44 +02:00
2024-03-13 00:36:41 +01:00
2023-02-16 02:36:09 +01:00
if ($invitation->contact->trashed()) {
$invitation->contact->restore();
2023-02-16 02:36:09 +01:00
}
2024-01-14 05:05:00 +01:00
auth()->guard('contact')->loginUsingId($invitation->contact->id, true);
2024-01-14 05:05:00 +01:00
$invoice = $invitation->invoice->service()->removeUnpaidGatewayFees()->save();
2021-10-06 05:47:17 +02:00
2023-02-16 02:36:09 +01:00
if ($invoice->partial > 0) {
2021-10-06 05:47:17 +02:00
$amount = round($invoice->partial, (int)$invoice->client->currency()->precision);
2023-02-16 02:36:09 +01:00
} else {
2021-10-06 05:47:17 +02:00
$amount = round($invoice->balance, (int)$invoice->client->currency()->precision);
2023-02-16 02:36:09 +01:00
}
2021-10-06 05:47:17 +02:00
$gateways = $invitation->contact->client->service()->getPaymentMethods($amount);
2024-01-14 05:05:00 +01:00
if (is_array($gateways) && count($gateways) >= 1) {
2021-10-06 05:47:17 +02:00
$data = [
'company_gateway_id' => $gateways[0]['company_gateway_id'],
'payment_method_id' => $gateways[0]['gateway_type_id'],
'payable_invoices' => [
['invoice_id' => $invitation->invoice->hashed_id, 'amount' => $amount],
],
2024-03-13 00:36:41 +01:00
'signature' => false,
'contact_first_name' => $invitation->contact->first_name ?? '',
'contact_last_name' => $invitation->contact->last_name ?? '',
'contact_email' => $invitation->contact->email ?? ''
2021-10-06 05:47:17 +02:00
];
$request->replace($data);
return (new InstantPayment($request))->run();
}
$entity = 'invoice';
2023-02-16 02:36:09 +01:00
if ($invoice && is_array($gateways) && count($gateways) == 0) {
return redirect()->route('client.invoice.show', ['invoice' => $this->encodePrimaryKey($invitation->invoice_id)]);
2023-02-16 02:36:09 +01:00
}
2021-10-06 06:05:16 +02:00
abort(404, "Invoice not found");
2021-10-05 12:29:32 +02:00
}
2022-03-06 08:06:07 +01:00
public function unsubscribe(Request $request, string $entity, string $invitation_key)
{
2023-02-16 02:36:09 +01:00
if ($entity == 'invoice') {
2022-02-27 08:33:29 +01:00
$invite = InvoiceInvitation::withTrashed()->where('key', $invitation_key)->first();
$invite->contact->send_email = false;
$invite->contact->save();
2023-02-16 02:36:09 +01:00
} elseif ($entity == 'quote') {
2022-02-27 08:33:29 +01:00
$invite = QuoteInvitation::withTrashed()->where('key', $invitation_key)->first();
$invite->contact->send_email = false;
$invite->contact->save();
2023-02-16 02:36:09 +01:00
} elseif ($entity == 'credit') {
2022-02-27 08:33:29 +01:00
$invite = CreditInvitation::withTrashed()->where('key', $invitation_key)->first();
$invite->contact->send_email = false;
$invite->contact->save();
2023-02-16 02:36:09 +01:00
} elseif ($entity == 'purchase_order') {
2022-06-02 04:41:44 +02:00
$invite = PurchaseOrderInvitation::withTrashed()->where('key', $invitation_key)->first();
$invite->contact->send_email = false;
$invite->contact->save();
2023-02-16 02:36:09 +01:00
} else {
2022-02-25 03:55:32 +01:00
return abort(404);
2023-02-16 02:36:09 +01:00
}
2022-02-25 03:55:32 +01:00
$data['logo'] = $invite->company->present()->logo();
return $this->render('generic.unsubscribe', $data);
}
2019-09-23 07:59:01 +02:00
}