1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 15:13:29 +01:00
invoiceninja/app/Services/PurchaseOrder/CreateInvitations.php

104 lines
3.5 KiB
PHP
Raw Normal View History

<?php
2022-06-05 11:41:19 +02:00
/**
* 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-06-05 11:41:19 +02:00
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Services\PurchaseOrder;
use App\Factory\PurchaseOrderInvitationFactory;
2022-06-05 11:41:19 +02:00
use App\Factory\VendorContactFactory;
use App\Models\PurchaseOrder;
use App\Models\PurchaseOrderInvitation;
use App\Services\AbstractService;
use App\Utils\Traits\MakesHash;
use Illuminate\Support\Str;
class CreateInvitations extends AbstractService
{
use MakesHash;
public PurchaseOrder $purchase_order;
public function __construct(PurchaseOrder $purchase_order)
{
$this->purchase_order = $purchase_order;
}
2022-06-05 11:41:19 +02:00
private function createBlankContact()
{
2022-06-05 11:41:19 +02:00
$new_contact = VendorContactFactory::create($this->purchase_order->company_id, $this->purchase_order->user_id);
$new_contact->vendor_id = $this->purchase_order->vendor_id;
$new_contact->contact_key = Str::random(40);
$new_contact->is_primary = true;
$new_contact->save();
}
2022-06-06 14:27:17 +02:00
public function run()
{
2022-06-10 09:04:16 +02:00
$contacts = $this->purchase_order->vendor->contacts()->get();
if ($contacts->count() == 0) {
$this->createBlankContact();
$this->purchase_order->refresh();
$contacts = $this->purchase_order->vendor->contacts;
}
$contacts->each(function ($contact) {
2022-06-05 11:41:19 +02:00
$invitation = PurchaseOrderInvitation::where('company_id', $this->purchase_order->company_id)
->where('vendor_contact_id', $contact->id)
->where('purchase_order_id', $this->purchase_order->id)
->withTrashed()
->first();
if (! $invitation) {
try {
$ii = PurchaseOrderInvitationFactory::create($this->purchase_order->company_id, $this->purchase_order->user_id);
$ii->key = $this->createDbHash($this->purchase_order->company->db);
$ii->purchase_order_id = $this->purchase_order->id;
$ii->vendor_contact_id = $contact->id;
$ii->save();
} catch (\Exception $e) {
2022-06-06 14:27:17 +02:00
nlog($e->getMessage());
}
} elseif (! $contact->send_email) {
$invitation->delete();
}
});
if ($this->purchase_order->invitations()->count() == 0) {
if ($contacts->count() == 0) {
$contact = $this->createBlankContact();
} else {
$contact = $contacts->first();
2022-06-06 14:27:17 +02:00
$invitation = PurchaseOrderInvitation::where('company_id', $this->purchase_order->company_id)
->where('vendor_contact_id', $contact->id)
->where('purchase_order_id', $this->purchase_order->id)
->withTrashed()
->first();
if ($invitation) {
$invitation->restore();
return $this->purchase_order;
}
}
2022-06-05 11:41:19 +02:00
$ii = PurchaseOrderInvitationFactory::create($this->purchase_order->company_id, $this->purchase_order->user_id);
$ii->key = $this->createDbHash($this->purchase_order->company->db);
$ii->purchase_order_id = $this->purchase_order->id;
$ii->vendor_contact_id = $contact->id;
$ii->save();
}
return $this->purchase_order;
}
}