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;
|
|
|
|
|
2023-10-26 04:57:44 +02:00
|
|
|
use App\Events\Misc\InvitationWasViewed;
|
|
|
|
use App\Events\PurchaseOrder\PurchaseOrderWasViewed;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
use App\Jobs\Vendor\CreatePurchaseOrderPdf;
|
|
|
|
use App\Models\PurchaseOrderInvitation;
|
2022-06-13 11:59:24 +02:00
|
|
|
use App\Utils\Ninja;
|
2023-07-20 08:46:10 +02:00
|
|
|
use App\Utils\Traits\MakesDates;
|
2023-10-26 04:57:44 +02:00
|
|
|
use App\Utils\Traits\MakesHash;
|
2023-07-20 08:46:10 +02:00
|
|
|
use Illuminate\Support\Facades\App;
|
2022-06-13 11:59:24 +02:00
|
|
|
use Illuminate\Support\Facades\Auth;
|
2023-10-26 04:57:44 +02:00
|
|
|
use Illuminate\Support\Str;
|
2022-06-13 11:59:24 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
|
2022-06-16 06:42:24 +02:00
|
|
|
session()->put('is_silent', request()->has('silent'));
|
2022-06-13 11:59:24 +02:00
|
|
|
|
2022-06-16 06:42: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()));
|
2022-10-27 02:28:09 +02:00
|
|
|
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
|
|
|
|
2023-07-20 08:46:10 +02:00
|
|
|
App::setLocale($invitation->contact->preferredLocale());
|
|
|
|
|
2022-06-29 03:37:40 +02:00
|
|
|
$file_name = $invitation->purchase_order->numberFormatter().'.pdf';
|
2022-06-13 11:59:24 +02:00
|
|
|
|
2023-10-26 08:50:07 +02:00
|
|
|
$file = $invitation->purchase_order->service()->getPurchaseOrderPdf();
|
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
|
|
|
}
|