1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-15 07:33:04 +01:00
invoiceninja/app/Services/PurchaseOrder/PurchaseOrderService.php

176 lines
4.9 KiB
PHP
Raw Normal View History

2022-05-29 04:13:09 +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-05-29 04:13:09 +02:00
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Services\PurchaseOrder;
use App\Jobs\EDocument\CreateEDocument;
2022-05-29 04:13:09 +02:00
use App\Models\PurchaseOrder;
use App\Utils\Ninja;
2022-05-29 04:13:09 +02:00
use App\Utils\Traits\MakesHash;
2024-03-19 15:39:29 +01:00
use Illuminate\Support\Facades\Storage;
2022-05-29 04:13:09 +02:00
class PurchaseOrderService
{
use MakesHash;
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()
{
$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) {
$this->purchase_order->design_id = $this->decodePrimaryKey($settings->purchase_order_design_id);
2023-02-16 02:36:09 +01:00
}
2024-01-14 05:05:00 +01:00
2023-02-16 02:36:09 +01:00
if (!isset($this->purchase_order->footer) || empty($this->purchase_order->footer)) {
$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)) {
$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)) {
$this->purchase_order->public_notes = $this->purchase_order->vendor->public_notes;
2023-02-16 02:36:09 +01:00
}
2024-01-14 05:05:00 +01:00
2023-02-16 02:36:09 +01:00
if ($settings->counter_number_applied == 'when_saved') {
$this->applyNumber()->save();
}
2022-05-29 04:13:09 +02:00
return $this;
}
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();
}
public function getEPurchaseOrder($contact = null)
{
return (new CreateEDocument($this->purchase_order))->handle();
}
2024-04-04 23:42:31 +02:00
public function getEDocument($contact = null)
{
return $this->getEPurchaseOrder($contact);
}
2024-06-14 09:09:44 +02:00
public function deleteEPurchaseOrder()
{
$this->purchase_order->load('invitations');
$this->purchase_order->invitations->each(function ($invitation) {
try {
// if (Storage::disk(config('filesystems.default'))->exists($this->invoice->client->e_invoice_filepath($invitation).$this->invoice->getFileName("xml"))) {
2024-03-19 15:33:58 +01:00
Storage::disk(config('filesystems.default'))->delete($this->purchase_order->vendor->e_document_filepath($invitation).$this->purchase_order->getFileName("xml"));
// }
// if (Ninja::isHosted() && Storage::disk('public')->exists($this->invoice->client->e_invoice_filepath($invitation).$this->invoice->getFileName("xml"))) {
if (Ninja::isHosted()) {
2024-03-20 12:54:18 +01:00
Storage::disk('public')->delete($this->purchase_order->vendor->e_document_filepath($invitation).$this->purchase_order->getFileName("xml"));
}
} catch (\Exception $e) {
nlog($e->getMessage());
}
});
return $this;
}
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();
return $this;
}
public function adjustBalance($adjustment)
{
$this->purchase_order->balance += $adjustment;
return $this;
}
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();
return $this;
}
2022-07-06 11:25:22 +02:00
public function expense()
{
$this->markSent();
2024-01-14 05:05:00 +01:00
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-10-26 04:57:44 +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
}