2022-05-29 04:13:09 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Services\PurchaseOrder;
|
|
|
|
|
2022-06-10 09:04:16 +02:00
|
|
|
use App\Jobs\Vendor\CreatePurchaseOrderPdf;
|
2022-05-29 04:13:09 +02:00
|
|
|
use App\Models\PurchaseOrder;
|
2022-05-31 00:28:32 +02:00
|
|
|
use App\Services\PurchaseOrder\ApplyNumber;
|
|
|
|
use App\Services\PurchaseOrder\CreateInvitations;
|
2022-06-06 14:27:17 +02:00
|
|
|
use App\Services\PurchaseOrder\GetPurchaseOrderPdf;
|
2022-07-06 11:25:22 +02:00
|
|
|
use App\Services\PurchaseOrder\PurchaseOrderExpense;
|
2022-06-10 09:04:16 +02:00
|
|
|
use App\Services\PurchaseOrder\TriggeredActions;
|
2022-05-29 04:13:09 +02:00
|
|
|
use App\Utils\Traits\MakesHash;
|
|
|
|
|
|
|
|
class PurchaseOrderService
|
|
|
|
{
|
|
|
|
use MakesHash;
|
|
|
|
|
|
|
|
public PurchaseOrder $purchase_order;
|
|
|
|
|
2022-06-06 14:27:17 +02:00
|
|
|
public function __construct(PurchaseOrder $purchase_order)
|
2022-05-29 04:13:09 +02:00
|
|
|
{
|
|
|
|
$this->purchase_order = $purchase_order;
|
|
|
|
}
|
|
|
|
|
2022-05-31 00:28:32 +02:00
|
|
|
public function createInvitations()
|
|
|
|
{
|
|
|
|
|
|
|
|
$this->purchase_order = (new CreateInvitations($this->purchase_order))->run();
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function applyNumber()
|
|
|
|
{
|
2022-07-04 07:27:09 +02:00
|
|
|
$this->purchase_order = (new ApplyNumber($this->purchase_order->vendor, $this->purchase_order))->run();
|
2022-05-31 00:28:32 +02:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2022-05-29 04:13:09 +02:00
|
|
|
public function fillDefaults()
|
|
|
|
{
|
|
|
|
|
2022-07-12 00:22:03 +02:00
|
|
|
$settings = $this->purchase_order->company->settings;
|
2022-05-29 04:13:09 +02:00
|
|
|
|
2022-07-12 00:22:03 +02:00
|
|
|
if (! $this->purchase_order->design_id)
|
2022-07-27 12:03:46 +02:00
|
|
|
$this->purchase_order->design_id = $this->decodePrimaryKey($settings->purchase_order_design_id);
|
2022-07-12 00:22:03 +02:00
|
|
|
|
|
|
|
if (!isset($this->invoice->footer) || empty($this->invoice->footer))
|
|
|
|
$this->purchase_order->footer = $settings->purchase_order_footer;
|
2022-05-29 04:13:09 +02:00
|
|
|
|
2022-07-12 00:22:03 +02:00
|
|
|
if (!isset($this->purchase_order->terms) || empty($this->purchase_order->terms))
|
|
|
|
$this->purchase_order->terms = $settings->purchase_order_terms;
|
2022-05-29 04:13:09 +02:00
|
|
|
|
2022-07-12 00:22:03 +02:00
|
|
|
if (!isset($this->purchase_order->public_notes) || empty($this->purchase_order->public_notes))
|
|
|
|
$this->purchase_order->public_notes = $this->purchase_order->vendor->public_notes;
|
|
|
|
|
|
|
|
if($settings->counter_number_applied == 'when_saved'){
|
|
|
|
$this->applyNumber()->save();
|
|
|
|
}
|
2022-05-29 04:13:09 +02:00
|
|
|
|
|
|
|
return $this;
|
2022-07-12 00:22:03 +02:00
|
|
|
|
2022-05-29 04:13:09 +02:00
|
|
|
}
|
2022-06-05 05:58:29 +02:00
|
|
|
|
2022-06-10 09:04:16 +02:00
|
|
|
public function triggeredActions($request)
|
|
|
|
{
|
|
|
|
$this->purchase_order = (new TriggeredActions($this->purchase_order->load('invitations'), $request))->run();
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2022-06-06 14:27:17 +02:00
|
|
|
public function getPurchaseOrderPdf($contact = null)
|
|
|
|
{
|
|
|
|
return (new GetPurchaseOrderPdf($this->purchase_order, $contact))->run();
|
|
|
|
}
|
|
|
|
|
2022-06-05 05:58:29 +02:00
|
|
|
public function setStatus($status)
|
|
|
|
{
|
|
|
|
$this->purchase_order->status_id = $status;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function markSent()
|
|
|
|
{
|
2022-06-05 11:22:58 +02:00
|
|
|
$this->purchase_order = (new MarkSent($this->purchase_order->vendor, $this->purchase_order))->run();
|
2022-06-05 05:58:29 +02:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2022-12-19 04:57:44 +01:00
|
|
|
public function adjustBalance($adjustment)
|
|
|
|
{
|
|
|
|
$this->purchase_order->balance += $adjustment;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2022-06-10 09:04:16 +02:00
|
|
|
public function touchPdf($force = false)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
|
|
|
|
if($force){
|
|
|
|
|
|
|
|
$this->purchase_order->invitations->each(function ($invitation) {
|
2022-09-07 06:15:27 +02:00
|
|
|
(new CreatePurchaseOrderPdf($invitation))->handle();
|
2022-06-10 09:04:16 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->purchase_order->invitations->each(function ($invitation) {
|
|
|
|
CreatePurchaseOrderPdf::dispatch($invitation);
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
catch(\Exception $e){
|
|
|
|
|
|
|
|
nlog("failed creating purchase orders in Touch PDF");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2022-07-06 12:04:59 +02:00
|
|
|
public function add_to_inventory()
|
|
|
|
{
|
2022-07-07 03:20:43 +02:00
|
|
|
if($this->purchase_order->status_id >= PurchaseOrder::STATUS_RECEIVED)
|
|
|
|
return $this->purchase_order;
|
|
|
|
|
|
|
|
$this->purchase_order = (new PurchaseOrderInventory($this->purchase_order))->run();
|
2022-07-06 12:04:59 +02:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2022-07-06 11:25:22 +02:00
|
|
|
public function expense()
|
|
|
|
{
|
|
|
|
$this->markSent();
|
|
|
|
|
2022-07-07 14:09:39 +02:00
|
|
|
if($this->purchase_order->expense()->exists())
|
|
|
|
return $this;
|
|
|
|
|
2022-07-06 11:25:22 +02:00
|
|
|
$expense = (new PurchaseOrderExpense($this->purchase_order))->run();
|
|
|
|
|
|
|
|
return $expense;
|
|
|
|
}
|
|
|
|
|
2022-06-05 11:41:19 +02:00
|
|
|
/**
|
|
|
|
* Saves the purchase order.
|
|
|
|
* @return \App\Models\PurchaseOrder object
|
|
|
|
*/
|
|
|
|
public function save(): ?PurchaseOrder
|
|
|
|
{
|
|
|
|
$this->purchase_order->saveQuietly();
|
|
|
|
|
|
|
|
return $this->purchase_order;
|
|
|
|
}
|
|
|
|
|
2022-05-29 04:13:09 +02:00
|
|
|
}
|