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

85 lines
2.0 KiB
PHP
Raw Normal View History

<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
*
2021-06-16 08:58:16 +02:00
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Repositories;
use App\Factory\VendorFactory;
use App\Models\Vendor;
use App\Utils\Traits\GeneratesCounter;
/**
* VendorRepository.
*/
class VendorRepository extends BaseRepository
{
use GeneratesCounter;
2020-11-03 14:27:41 +01:00
protected $contact_repo;
/**
2020-11-03 14:27:41 +01:00
* VendorContactRepository constructor.
*/
public function __construct(VendorContactRepository $contact_repo)
{
$this->contact_repo = $contact_repo;
}
/**
* Saves the vendor and its contacts.
*
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-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
*/
public function save(array $data, Vendor $vendor) : ?Vendor
{
$vendor->fill($data);
$vendor->save();
2021-01-25 11:34:12 +01:00
if ($vendor->number == '' || ! $vendor->number) {
$vendor->number = $this->getNextVendorNumber($vendor);
} //todo write tests for this and make sure that custom vendor numbers also works as expected from here
$vendor->save();
if (isset($data['contacts'])) {
2020-11-03 14:27:41 +01:00
$this->contact_repo->save($data, $vendor);
}
if (empty($data['name'])) {
$data['name'] = $vendor->present()->name();
}
2020-10-12 22:42:02 +02:00
if (array_key_exists('documents', $data)) {
$this->saveDocuments($data['documents'], $vendor);
}
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)
);
}
}