mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-05-24 02:14:21 -04:00
Refactor for PurchaseOrder Previews
This commit is contained in:
parent
ed3560a637
commit
aa0072eb20
@ -11,32 +11,33 @@
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\DataMapper\Analytics\LivePreview;
|
||||
use App\Factory\PurchaseOrderFactory;
|
||||
use App\Http\Requests\Preview\PreviewPurchaseOrderRequest;
|
||||
use App\Jobs\Util\PreviewPdf;
|
||||
use App\Libraries\MultiDB;
|
||||
use App\Utils\Ninja;
|
||||
use App\Models\Client;
|
||||
use App\Models\PurchaseOrder;
|
||||
use App\Models\PurchaseOrderInvitation;
|
||||
use App\Models\Vendor;
|
||||
use App\Libraries\MultiDB;
|
||||
use App\Jobs\Util\PreviewPdf;
|
||||
use App\Models\PurchaseOrder;
|
||||
use App\Models\VendorContact;
|
||||
use App\Repositories\PurchaseOrderRepository;
|
||||
use App\Utils\Traits\MakesHash;
|
||||
use App\Utils\VendorHtmlEngine;
|
||||
use App\Services\Pdf\PdfService;
|
||||
use App\Utils\PhantomJS\Phantom;
|
||||
use App\Services\PdfMaker\Design;
|
||||
use App\Utils\HostedPDF\NinjaPdf;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use App\Services\PdfMaker\PdfMaker;
|
||||
use Illuminate\Support\Facades\App;
|
||||
use App\Factory\PurchaseOrderFactory;
|
||||
use App\Utils\Traits\MakesInvoiceHtml;
|
||||
use Turbo124\Beacon\Facades\LightLogs;
|
||||
use App\Models\PurchaseOrderInvitation;
|
||||
use App\Utils\Traits\Pdf\PageNumbering;
|
||||
use Illuminate\Support\Facades\Response;
|
||||
use App\DataMapper\Analytics\LivePreview;
|
||||
use App\Repositories\PurchaseOrderRepository;
|
||||
use App\Services\PdfMaker\Design as PdfDesignModel;
|
||||
use App\Services\PdfMaker\Design as PdfMakerDesign;
|
||||
use App\Services\PdfMaker\PdfMaker;
|
||||
use App\Utils\HostedPDF\NinjaPdf;
|
||||
use App\Utils\Ninja;
|
||||
use App\Utils\PhantomJS\Phantom;
|
||||
use App\Utils\Traits\MakesHash;
|
||||
use App\Utils\Traits\MakesInvoiceHtml;
|
||||
use App\Utils\Traits\Pdf\PageNumbering;
|
||||
use App\Utils\VendorHtmlEngine;
|
||||
use Illuminate\Support\Facades\App;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Response;
|
||||
use Turbo124\Beacon\Facades\LightLogs;
|
||||
use App\Http\Requests\Preview\PreviewPurchaseOrderRequest;
|
||||
|
||||
class PreviewPurchaseOrderController extends BaseController
|
||||
{
|
||||
@ -173,6 +174,35 @@ class PreviewPurchaseOrderController extends BaseController
|
||||
/** @var \App\Models\User $user */
|
||||
$user = auth()->user();
|
||||
|
||||
$invitation = $request->resolveInvitation();
|
||||
$vendor = $request->getVendor();
|
||||
$settings = $user->company()->settings;
|
||||
|
||||
/** Set translations */
|
||||
App::forgetInstance('translator');
|
||||
$t = app('translator');
|
||||
App::setLocale($invitation->contact->preferredLocale());
|
||||
$t->replace(Ninja::transformTranslations($settings));
|
||||
|
||||
$entity_obj = $invitation->purchase_order;
|
||||
$entity_obj->fill($request->all());
|
||||
|
||||
$ps = new PdfService($invitation, 'purchase_order', [
|
||||
'client' => $entity_obj->client ?? false,
|
||||
'vendor' => $vendor ?? false,
|
||||
"purchase_orders" => [$entity_obj],
|
||||
]);
|
||||
|
||||
$pdf = $ps->boot()->getPdf();
|
||||
return $pdf;
|
||||
|
||||
}
|
||||
|
||||
public function livex(PreviewPurchaseOrderRequest $request)
|
||||
{
|
||||
/** @var \App\Models\User $user */
|
||||
$user = auth()->user();
|
||||
|
||||
$company = $user->company();
|
||||
|
||||
MultiDB::setDb($company->db);
|
||||
|
@ -104,7 +104,7 @@ class PreviewInvoiceRequest extends Request
|
||||
return $invitation;
|
||||
}
|
||||
|
||||
$invitation = $this->stubInvitation();
|
||||
return $this->stubInvitation();
|
||||
}
|
||||
|
||||
public function getClient(): ?Client
|
||||
|
@ -11,15 +11,21 @@
|
||||
|
||||
namespace App\Http\Requests\Preview;
|
||||
|
||||
use App\Models\Vendor;
|
||||
use App\Models\PurchaseOrder;
|
||||
use App\Http\Requests\Request;
|
||||
use App\Utils\Traits\CleanLineItems;
|
||||
use App\Utils\Traits\MakesHash;
|
||||
use App\Utils\Traits\CleanLineItems;
|
||||
use App\Models\PurchaseOrderInvitation;
|
||||
|
||||
class PreviewPurchaseOrderRequest extends Request
|
||||
{
|
||||
use MakesHash;
|
||||
use CleanLineItems;
|
||||
|
||||
private ?Vendor $vendor = null;
|
||||
private string $entity_plural = '';
|
||||
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*
|
||||
@ -27,7 +33,10 @@ class PreviewPurchaseOrderRequest extends Request
|
||||
*/
|
||||
public function authorize() : bool
|
||||
{
|
||||
return auth()->user()->hasIntersectPermissionsOrAdmin(['create_purchase_order', 'edit_purchase_order', 'view_purchase_order']);
|
||||
/** @var \App\Models\User $user */
|
||||
$user = auth()->user();
|
||||
|
||||
return $user->hasIntersectPermissionsOrAdmin(['create_purchase_order', 'edit_purchase_order', 'view_purchase_order']);
|
||||
}
|
||||
|
||||
public function rules()
|
||||
@ -52,4 +61,77 @@ class PreviewPurchaseOrderRequest extends Request
|
||||
|
||||
$this->replace($input);
|
||||
}
|
||||
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -79,8 +79,9 @@ class UpdateCalculatedFields
|
||||
|
||||
$project->tasks->each(function ($task) use (&$duration) {
|
||||
|
||||
if(is_iterable($task->time_log)) {
|
||||
foreach(json_decode($task->time_log) as $log) {
|
||||
if(is_iterable(json_decode($task->time_log) )) {
|
||||
|
||||
foreach(json_decode($task->time_log) as $log) {
|
||||
|
||||
$start_time = $log[0];
|
||||
$end_time = $log[1] == 0 ? time() : $log[1];
|
||||
|
Loading…
x
Reference in New Issue
Block a user