1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-15 07:33:04 +01:00
invoiceninja/app/Http/Requests/Preview/PreviewPurchaseOrderRequest.php

134 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
*
2024-04-12 06:15:41 +02:00
* @copyright Copyright (c) 2024. 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;
use App\Http\Requests\Request;
2023-11-26 08:41:42 +01:00
use App\Models\PurchaseOrder;
2023-10-27 10:30:42 +02:00
use App\Models\PurchaseOrderInvitation;
2023-11-26 08:41:42 +01:00
use App\Models\Vendor;
use App\Utils\Traits\CleanLineItems;
use App\Utils\Traits\MakesHash;
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;
2022-06-29 03:37:40 +02:00
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
2024-01-14 05:05:00 +01:00
public function authorize(): bool
2022-06-29 03:37:40 +02:00
{
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
2024-01-14 05:05:00 +01:00
2024-01-12 02:14:09 +01:00
if($input['entity_id'] ?? false) {
$input['entity_id'] = $this->decodePrimaryKey($input['entity_id'], true);
}
2022-06-29 03:37:40 +02:00
$this->replace($input);
}
2023-10-27 10:30:42 +02:00
public function resolveInvitation()
{
$invitation = false;
2024-06-19 05:57:50 +02:00
if(! isset($this->entity_id)) {
2023-10-27 10:30:42 +02:00
return $this->stubInvitation();
}
$invitation = PurchaseOrderInvitation::withTrashed()->where('purchase_order_id', $this->entity_id)->first();
if($invitation) {
return $invitation;
}
return $this->stubInvitation();
2024-01-14 05:05:00 +01:00
2023-10-27 10:30:42 +02:00
}
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]);
2024-01-14 05:05:00 +01:00
2023-10-27 10:30:42 +02:00
$entity->setRelation('vendor', $vendor);
$entity->setRelation('company', $vendor->company);
$entity->setRelation('user', $vendor->user);
$entity->fill($this->all());
2024-01-14 05:05:00 +01:00
2023-10-27 10:30:42 +02:00
return $entity;
}
2022-06-29 03:37:40 +02:00
}