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

138 lines
3.5 KiB
PHP
Raw Normal View History

2022-06-29 03:37:40 +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-29 03:37:40 +02:00
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Http\Requests\Preview;
2023-10-27 10:30:42 +02:00
use App\Models\Vendor;
use App\Models\PurchaseOrder;
2022-06-29 03:37:40 +02:00
use App\Http\Requests\Request;
use App\Utils\Traits\MakesHash;
2023-10-27 10:30:42 +02:00
use App\Utils\Traits\CleanLineItems;
use App\Models\PurchaseOrderInvitation;
2022-06-29 03:37:40 +02:00
class PreviewPurchaseOrderRequest extends Request
{
use MakesHash;
use CleanLineItems;
2023-10-27 10:30:42 +02:00
private ?Vendor $vendor = null;
private string $entity_plural = '';
2022-06-29 03:37:40 +02:00
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize() : bool
{
2023-10-27 10:30:42 +02:00
/** @var \App\Models\User $user */
$user = auth()->user();
return $user->hasIntersectPermissionsOrAdmin(['create_purchase_order', 'edit_purchase_order', 'view_purchase_order']);
2022-06-29 03:37:40 +02:00
}
public function rules()
{
$rules = [];
$rules['number'] = ['nullable'];
return $rules;
}
2022-07-15 14:59:25 +02:00
public function prepareForValidation()
2022-06-29 03:37:40 +02:00
{
$input = $this->all();
$input = $this->decodePrimaryKeys($input);
$input['line_items'] = isset($input['line_items']) ? $this->cleanItems($input['line_items']) : [];
$input['amount'] = 0;
$input['balance'] = 0;
$input['number'] = isset($input['number']) ? $input['number'] : ctrans('texts.live_preview').' #'.rand(0, 1000); //30-06-2023
2022-06-29 03:37:40 +02:00
$this->replace($input);
}
2023-10-27 10:30:42 +02:00
public function resolveInvitation()
{
$invitation = false;
if(! $this->entity_id ?? false) {
return $this->stubInvitation();
}
$invitation = PurchaseOrderInvitation::withTrashed()->where('purchase_order_id', $this->entity_id)->first();
if($invitation) {
return $invitation;
}
return $this->stubInvitation();
}
public function getVendor(): ?Vendor
{
if(!$this->vendor) {
$this->vendor = Vendor::query()->with('contacts', 'company', 'user')->withTrashed()->find($this->vendor_id);
}
return $this->vendor;
}
public function setVendor(Vendor $vendor): self
{
$this->vendor = $vendor;
return $this;
}
public function stubInvitation()
{
$vendor = Vendor::query()->with('contacts', 'company', 'user')->withTrashed()->find($this->vendor_id);
$this->setVendor($vendor);
$invitation = false;
$entity = $this->stubEntity($vendor);
$invitation = PurchaseOrderInvitation::factory()->make();
$invitation->setRelation('purchase_order', $entity);
$invitation->setRelation('contact', $vendor->contacts->first()->load('vendor.company'));
$invitation->setRelation('company', $vendor->company);
return $invitation;
}
private function stubEntity(Vendor $vendor)
{
$entity = PurchaseOrder::factory()->make(['vendor_id' => $vendor->id,'user_id' => $vendor->user_id, 'company_id' => $vendor->company_id]);
$entity->setRelation('vendor', $vendor);
$entity->setRelation('company', $vendor->company);
$entity->setRelation('user', $vendor->user);
$entity->fill($this->all());
return $entity;
}
private function convertEntityPlural(string $entity) :self
{
$this->entity_plural = 'purchase_orders';
return $this;
}
2022-06-29 03:37:40 +02:00
}