2020-01-20 02:31:58 +01:00
|
|
|
<?php
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2020-01-20 02:31:58 +01:00
|
|
|
*
|
|
|
|
* @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)
|
2020-01-20 02:31:58 +01:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2020-01-20 02:31:58 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Repositories;
|
|
|
|
|
|
|
|
use App\Factory\VendorFactory;
|
|
|
|
use App\Models\Vendor;
|
|
|
|
use App\Utils\Traits\GeneratesCounter;
|
|
|
|
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* VendorRepository.
|
2020-01-20 02:31:58 +01:00
|
|
|
*/
|
|
|
|
class VendorRepository extends BaseRepository
|
|
|
|
{
|
|
|
|
use GeneratesCounter;
|
2020-11-03 14:27:41 +01:00
|
|
|
|
2020-01-20 02:31:58 +01:00
|
|
|
protected $contact_repo;
|
|
|
|
|
|
|
|
/**
|
2020-11-03 14:27:41 +01:00
|
|
|
* VendorContactRepository constructor.
|
2020-01-20 02:31:58 +01:00
|
|
|
*/
|
|
|
|
public function __construct(VendorContactRepository $contact_repo)
|
|
|
|
{
|
|
|
|
$this->contact_repo = $contact_repo;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Saves the vendor and its contacts.
|
2020-01-20 02:31:58 +01:00
|
|
|
*
|
2020-10-28 11:10:49 +01:00
|
|
|
* @param array $data The data
|
2020-11-03 14:27:41 +01:00
|
|
|
* @param \App\Models\Vendor $vendor The vendor
|
2020-01-20 02:31:58 +01:00
|
|
|
*
|
2020-11-03 14:27:41 +01:00
|
|
|
* @return vendor|\App\Models\Vendor|null Vendor Object
|
2020-10-28 11:10:49 +01:00
|
|
|
* @throws \Laracasts\Presenter\Exceptions\PresenterException
|
2020-01-20 02:31:58 +01:00
|
|
|
*/
|
2024-01-14 05:05:00 +01:00
|
|
|
public function save(array $data, Vendor $vendor): ?Vendor
|
2020-01-20 02:31:58 +01:00
|
|
|
{
|
2023-07-20 01:08:17 +02:00
|
|
|
$saveable_vendor = $data;
|
|
|
|
|
|
|
|
if(array_key_exists('contacts', $data)) {
|
|
|
|
unset($saveable_vendor['contacts']);
|
|
|
|
}
|
|
|
|
|
|
|
|
$vendor->fill($saveable_vendor);
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2023-02-01 03:46:39 +01:00
|
|
|
$vendor->saveQuietly();
|
2020-01-20 02:31:58 +01:00
|
|
|
|
2021-01-25 11:34:12 +01:00
|
|
|
if ($vendor->number == '' || ! $vendor->number) {
|
|
|
|
$vendor->number = $this->getNextVendorNumber($vendor);
|
2023-11-26 08:41:42 +01:00
|
|
|
}
|
2020-01-20 02:31:58 +01:00
|
|
|
|
2023-02-01 03:46:39 +01:00
|
|
|
$vendor->saveQuietly();
|
2020-01-20 02:31:58 +01:00
|
|
|
|
2023-11-23 01:33:52 +01:00
|
|
|
if (isset($data['contacts']) || $vendor->contacts()->count() == 0) {
|
2020-11-03 14:27:41 +01:00
|
|
|
$this->contact_repo->save($data, $vendor);
|
2020-01-20 02:31:58 +01:00
|
|
|
}
|
|
|
|
|
2023-11-23 01:33:52 +01:00
|
|
|
if (array_key_exists('documents', $data) && count($data['documents']) >= 1) {
|
2020-10-12 22:42:02 +02:00
|
|
|
$this->saveDocuments($data['documents'], $vendor);
|
|
|
|
}
|
|
|
|
|
2020-01-20 02:31:58 +01:00
|
|
|
return $vendor;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store vendors in bulk.
|
|
|
|
*
|
|
|
|
* @param array $vendor
|
|
|
|
* @return vendor|null
|
|
|
|
*/
|
|
|
|
public function create($vendor): ?Vendor
|
|
|
|
{
|
|
|
|
return $this->save(
|
|
|
|
$vendor,
|
|
|
|
VendorFactory::create(auth()->user()->company()->id, auth()->user()->id)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|