1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 21:22:58 +01:00
invoiceninja/app/Repositories/PurchaseOrderRepository.php

103 lines
3.7 KiB
PHP
Raw Normal View History

2022-05-29 04:13:19 +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:19 +02:00
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Repositories;
2022-06-06 14:27:17 +02:00
use App\Factory\PurchaseOrderInvitationFactory;
2022-05-29 04:13:19 +02:00
use App\Models\PurchaseOrder;
use App\Models\PurchaseOrderInvitation;
2022-06-06 14:27:17 +02:00
use App\Models\VendorContact;
2022-05-29 04:13:19 +02:00
use App\Utils\Traits\MakesHash;
class PurchaseOrderRepository extends BaseRepository
{
use MakesHash;
public function __construct()
{
}
public function save(array $data, PurchaseOrder $purchase_order) : ?PurchaseOrder
{
$purchase_order->fill($data);
2022-06-06 14:27:17 +02:00
2022-05-29 04:13:19 +02:00
$purchase_order->save();
2022-06-06 14:27:17 +02:00
if (isset($data['invitations'])) {
$invitations = collect($data['invitations']);
/* Get array of Keys which have been removed from the invitations array and soft delete each invitation */
$purchase_order->invitations->pluck('key')->diff($invitations->pluck('key'))->each(function ($invitation) {
$invitation = PurchaseOrderInvitation::where('key', $invitation)->first();
if ($invitation) {
2022-06-06 14:27:17 +02:00
$invitation->delete();
}
2022-06-06 14:27:17 +02:00
});
2022-06-06 14:27:17 +02:00
foreach ($data['invitations'] as $invitation) {
//if no invitations are present - create one.
if (! $this->getInvitation($invitation)) {
if (isset($invitation['id'])) {
2022-06-06 14:27:17 +02:00
unset($invitation['id']);
}
2022-06-06 14:27:17 +02:00
//make sure we are creating an invite for a contact who belongs to the client only!
$contact = VendorContact::find($invitation['vendor_contact_id']);
if ($contact && $purchase_order->vendor_id == $contact->vendor_id) {
$new_invitation = PurchaseOrderInvitation::withTrashed()
->where('vendor_contact_id', $contact->id)
->where('purchase_order_id', $purchase_order->id)
->first();
if ($new_invitation && $new_invitation->trashed()) {
$new_invitation->restore();
} else {
$new_invitation = PurchaseOrderInvitationFactory::create($purchase_order->company_id, $purchase_order->user_id);
$new_invitation->purchase_order_id = $purchase_order->id;
$new_invitation->vendor_contact_id = $contact->id;
$new_invitation->key = $this->createDbHash($purchase_order->company->db);
2023-02-01 05:00:45 +01:00
$new_invitation->saveQuietly();
2022-06-06 14:27:17 +02:00
}
}
}
}
}
2022-06-06 14:27:17 +02:00
/* If no invitations have been created, this is our fail safe to maintain state*/
if ($purchase_order->invitations()->count() == 0) {
2022-06-06 14:27:17 +02:00
$purchase_order->service()->createInvitations();
}
2022-06-06 14:27:17 +02:00
/* Recalculate invoice amounts */
$purchase_order = $purchase_order->calc()->getPurchaseOrder();
2022-05-29 04:13:19 +02:00
return $purchase_order;
}
2022-06-06 14:27:17 +02:00
public function getInvitationByKey($key) :?PurchaseOrderInvitation
{
return PurchaseOrderInvitation::where('key', $key)->first();
}
2022-05-29 04:13:19 +02:00
public function getInvitation($invitation, $resource = null)
2022-06-06 14:27:17 +02:00
{
if (is_array($invitation) && ! array_key_exists('key', $invitation)) {
2022-06-06 14:27:17 +02:00
return false;
}
2022-06-06 14:27:17 +02:00
$invitation = PurchaseOrderInvitation::where('key', $invitation['key'])->first();
return $invitation;
}
2022-05-29 04:13:19 +02:00
}