2022-05-29 04:13:09 +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-05-29 04:13:09 +02:00
|
|
|
*
|
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Services\PurchaseOrder;
|
|
|
|
|
|
|
|
use App\Models\PurchaseOrder;
|
|
|
|
use App\Utils\Traits\MakesHash;
|
2023-03-18 09:06:32 +01:00
|
|
|
use App\Services\PurchaseOrder\SendEmail;
|
|
|
|
use App\Jobs\Vendor\CreatePurchaseOrderPdf;
|
2022-05-29 04:13:09 +02:00
|
|
|
|
|
|
|
class PurchaseOrderService
|
|
|
|
{
|
|
|
|
use MakesHash;
|
|
|
|
|
2023-08-14 12:01:28 +02:00
|
|
|
public function __construct(public PurchaseOrder $purchase_order)
|
2022-05-29 04:13:09 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2023-02-16 02:36:09 +01: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);
|
2023-02-16 02:36:09 +01:00
|
|
|
}
|
2022-07-12 00:22:03 +02:00
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
if (!isset($this->purchase_order->footer) || empty($this->purchase_order->footer)) {
|
2022-07-12 00:22:03 +02:00
|
|
|
$this->purchase_order->footer = $settings->purchase_order_footer;
|
2023-02-16 02:36:09 +01:00
|
|
|
}
|
2022-05-29 04:13:09 +02:00
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
if (!isset($this->purchase_order->terms) || empty($this->purchase_order->terms)) {
|
2022-07-12 00:22:03 +02:00
|
|
|
$this->purchase_order->terms = $settings->purchase_order_terms;
|
2023-02-16 02:36:09 +01:00
|
|
|
}
|
2022-05-29 04:13:09 +02:00
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
if (!isset($this->purchase_order->public_notes) || empty($this->purchase_order->public_notes)) {
|
2022-07-12 00:22:03 +02:00
|
|
|
$this->purchase_order->public_notes = $this->purchase_order->vendor->public_notes;
|
2023-02-16 02:36:09 +01:00
|
|
|
}
|
2022-07-12 00:22:03 +02:00
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
if ($settings->counter_number_applied == 'when_saved') {
|
2022-07-12 00:22:03 +02:00
|
|
|
$this->applyNumber()->save();
|
|
|
|
}
|
2022-05-29 04:13:09 +02:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
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 {
|
2023-02-16 02:36:09 +01:00
|
|
|
if ($force) {
|
2022-06-10 09:04:16 +02:00
|
|
|
$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);
|
|
|
|
});
|
2023-02-16 02:36:09 +01:00
|
|
|
} catch(\Exception $e) {
|
2022-06-10 09:04:16 +02:00
|
|
|
nlog("failed creating purchase orders in Touch PDF");
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2022-07-06 12:04:59 +02:00
|
|
|
public function add_to_inventory()
|
|
|
|
{
|
2023-02-16 02:36:09 +01:00
|
|
|
if ($this->purchase_order->status_id >= PurchaseOrder::STATUS_RECEIVED) {
|
2022-07-07 03:20:43 +02:00
|
|
|
return $this->purchase_order;
|
2023-02-16 02:36:09 +01:00
|
|
|
}
|
2022-07-07 03:20:43 +02:00
|
|
|
|
|
|
|
$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();
|
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
if ($this->purchase_order->expense()->exists()) {
|
2022-07-07 14:09:39 +02:00
|
|
|
return $this;
|
2023-02-16 02:36:09 +01:00
|
|
|
}
|
2022-07-07 14:09:39 +02:00
|
|
|
|
2022-07-06 11:25:22 +02:00
|
|
|
$expense = (new PurchaseOrderExpense($this->purchase_order))->run();
|
|
|
|
|
|
|
|
return $expense;
|
|
|
|
}
|
|
|
|
|
2023-03-18 09:06:32 +01:00
|
|
|
public function sendEmail($contact = null)
|
|
|
|
{
|
|
|
|
$send_email = new SendEmail($this->purchase_order, null, $contact);
|
|
|
|
|
|
|
|
return $send_email->run();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-06-05 11:41:19 +02:00
|
|
|
/**
|
|
|
|
* Saves the purchase order.
|
2023-08-14 12:01:28 +02:00
|
|
|
* @return \App\Models\PurchaseOrder
|
2022-06-05 11:41:19 +02:00
|
|
|
*/
|
|
|
|
public function save(): ?PurchaseOrder
|
|
|
|
{
|
|
|
|
$this->purchase_order->saveQuietly();
|
|
|
|
|
|
|
|
return $this->purchase_order;
|
|
|
|
}
|
2022-05-29 04:13:09 +02:00
|
|
|
}
|