1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-11 05:32:39 +01:00
invoiceninja/app/Http/Controllers/VendorPortal/InvitationController.php

107 lines
3.8 KiB
PHP
Raw Normal View History

2022-06-13 11:59:24 +02:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @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)
2022-06-13 11:59:24 +02:00
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Http\Controllers\VendorPortal;
use App\Events\Misc\InvitationWasViewed;
use App\Events\PurchaseOrder\PurchaseOrderWasViewed;
2022-06-13 11:59:24 +02:00
use App\Http\Controllers\Controller;
2022-06-29 03:37:40 +02:00
use App\Jobs\Vendor\CreatePurchaseOrderPdf;
2022-06-13 11:59:24 +02:00
use App\Models\PurchaseOrderInvitation;
use App\Utils\Ninja;
use App\Utils\Traits\MakesDates;
use App\Utils\Traits\MakesHash;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Str;
/**
* Class InvitationController.
*/
class InvitationController extends Controller
{
use MakesHash;
use MakesDates;
public function purchaseOrder(string $invitation_key)
{
Auth::logout();
2022-06-17 01:08:07 +02:00
$invitation = PurchaseOrderInvitation::withTrashed()
->where('key', $invitation_key)
2022-06-13 11:59:24 +02:00
->whereHas('purchase_order', function ($query) {
2023-02-16 02:36:09 +01:00
$query->where('is_deleted', 0);
2022-06-13 11:59:24 +02:00
})
->with('contact.vendor')
->first();
2023-02-16 02:36:09 +01:00
if (!$invitation) {
return abort(404, 'The resource is no longer available.');
}
2022-06-13 11:59:24 +02:00
2023-02-16 02:36:09 +01:00
if ($invitation->contact->trashed()) {
2022-06-13 11:59:24 +02:00
$invitation->contact->restore();
2023-02-16 02:36:09 +01:00
}
2022-06-13 11:59:24 +02:00
$vendor_contact = $invitation->contact;
$entity = 'purchase_order';
2023-02-16 02:36:09 +01:00
if (empty($vendor_contact->email)) {
$vendor_contact->email = Str::random(15) . "@example.com";
} $vendor_contact->save();
2022-06-13 11:59:24 +02:00
if (request()->has('vendor_hash') && request()->input('vendor_hash') == $invitation->contact->vendor->vendor_hash) {
request()->session()->invalidate();
auth()->guard('vendor')->loginUsingId($vendor_contact->id, true);
} else {
request()->session()->invalidate();
auth()->guard('vendor')->loginUsingId($vendor_contact->id, true);
}
session()->put('is_silent', request()->has('silent'));
2022-06-13 11:59:24 +02:00
if (auth()->guard('vendor')->user() && ! session()->get('is_silent') && ! $invitation->viewed_date) {
$invitation->markViewed();
event(new InvitationWasViewed($invitation->purchase_order, $invitation, $invitation->company, Ninja::eventVars()));
event(new PurchaseOrderWasViewed($invitation, $invitation->company, Ninja::eventVars()));
2023-02-16 02:36:09 +01:00
} else {
2022-06-13 11:59:24 +02:00
return redirect()->route('vendor.'.$entity.'.show', [$entity => $this->encodePrimaryKey($invitation->purchase_order_id), 'silent' => session()->get('is_silent')]);
}
return redirect()->route('vendor.'.$entity.'.show', [$entity => $this->encodePrimaryKey($invitation->purchase_order_id)]);
}
2022-06-29 03:37:40 +02:00
public function download(string $invitation_key)
{
2022-06-29 03:44:05 +02:00
$invitation = PurchaseOrderInvitation::withTrashed()
2022-06-29 03:37:40 +02:00
->where('key', $invitation_key)
->with('contact.vendor')
->firstOrFail();
2022-06-13 11:59:24 +02:00
2023-02-16 02:36:09 +01:00
if (!$invitation) {
2022-06-29 03:37:40 +02:00
return response()->json(["message" => "no record found"], 400);
2023-02-16 02:36:09 +01:00
}
2022-06-13 11:59:24 +02:00
2022-06-29 03:37:40 +02:00
$file_name = $invitation->purchase_order->numberFormatter().'.pdf';
2022-06-13 11:59:24 +02:00
2022-06-29 03:47:16 +02:00
$file = (new CreatePurchaseOrderPdf($invitation))->rawPdf();
2022-06-13 11:59:24 +02:00
2022-06-29 03:37:40 +02:00
$headers = ['Content-Type' => 'application/pdf'];
2022-06-13 11:59:24 +02:00
2023-02-16 02:36:09 +01:00
if (request()->input('inline') == 'true') {
2022-06-29 03:37:40 +02:00
$headers = array_merge($headers, ['Content-Disposition' => 'inline']);
2023-02-16 02:36:09 +01:00
}
2022-06-13 11:59:24 +02:00
2023-02-16 02:36:09 +01:00
return response()->streamDownload(function () use ($file) {
echo $file;
}, $file_name, $headers);
2022-06-29 03:37:40 +02:00
}
2022-06-13 11:59:24 +02:00
}