* added support for hourly rate column in detail table * allow to register icon in extension * allow to show QR code secret * new translations * bump theme and packages * fix validation for invoice-document-filenames with uppercase character * max upload size 1MB * fix last month in daterange-picker in certain situations, more years in quick-select * remove unused package-versions-deprecated * link preferences from contract warning message * added page_setup page layout * prevent DROP TABLE in addSQL() and replace drop table with schema call * added azuyalabs/yasumi
141 lines
4.3 KiB
PHP
141 lines
4.3 KiB
PHP
<?php
|
|
|
|
/*
|
|
* This file is part of the Kimai time-tracking app.
|
|
*
|
|
* For the full copyright and license information, please view the LICENSE
|
|
* file that was distributed with this source code.
|
|
*/
|
|
|
|
namespace App\Customer;
|
|
|
|
use App\Configuration\SystemConfiguration;
|
|
use App\Entity\Customer;
|
|
use App\Event\CustomerCreateEvent;
|
|
use App\Event\CustomerCreatePostEvent;
|
|
use App\Event\CustomerCreatePreEvent;
|
|
use App\Event\CustomerMetaDefinitionEvent;
|
|
use App\Event\CustomerUpdatePostEvent;
|
|
use App\Event\CustomerUpdatePreEvent;
|
|
use App\Repository\CustomerRepository;
|
|
use App\Repository\Query\CustomerQuery;
|
|
use App\Utils\NumberGenerator;
|
|
use App\Validator\ValidationFailedException;
|
|
use InvalidArgumentException;
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface;
|
|
|
|
final class CustomerService
|
|
{
|
|
public function __construct(
|
|
private CustomerRepository $repository,
|
|
private SystemConfiguration $configuration,
|
|
private ValidatorInterface $validator,
|
|
private EventDispatcherInterface $dispatcher
|
|
) {
|
|
}
|
|
|
|
private function getDefaultTimezone(): string
|
|
{
|
|
if (null === ($timezone = $this->configuration->getCustomerDefaultTimezone())) {
|
|
$timezone = date_default_timezone_get();
|
|
}
|
|
|
|
return $timezone;
|
|
}
|
|
|
|
public function createNewCustomer(string $name): Customer
|
|
{
|
|
$customer = new Customer($name);
|
|
$customer->setTimezone($this->getDefaultTimezone());
|
|
$customer->setCountry($this->configuration->getCustomerDefaultCountry());
|
|
$customer->setCurrency($this->configuration->getCustomerDefaultCurrency());
|
|
$customer->setNumber($this->calculateNextCustomerNumber());
|
|
|
|
$this->dispatcher->dispatch(new CustomerMetaDefinitionEvent($customer));
|
|
$this->dispatcher->dispatch(new CustomerCreateEvent($customer));
|
|
|
|
return $customer;
|
|
}
|
|
|
|
public function saveNewCustomer(Customer $customer): Customer
|
|
{
|
|
if (null !== $customer->getId()) {
|
|
throw new InvalidArgumentException('Cannot create customer, already persisted');
|
|
}
|
|
|
|
$this->validateCustomer($customer);
|
|
|
|
$this->dispatcher->dispatch(new CustomerCreatePreEvent($customer));
|
|
$this->repository->saveCustomer($customer);
|
|
$this->dispatcher->dispatch(new CustomerCreatePostEvent($customer));
|
|
|
|
return $customer;
|
|
}
|
|
|
|
/**
|
|
* @param Customer $customer
|
|
* @param string[] $groups
|
|
* @throws ValidationFailedException
|
|
*/
|
|
private function validateCustomer(Customer $customer, array $groups = []): void
|
|
{
|
|
$errors = $this->validator->validate($customer, null, $groups);
|
|
|
|
if ($errors->count() > 0) {
|
|
throw new ValidationFailedException($errors, 'Validation Failed');
|
|
}
|
|
}
|
|
|
|
public function updateCustomer(Customer $customer): Customer
|
|
{
|
|
$this->validateCustomer($customer);
|
|
|
|
$this->dispatcher->dispatch(new CustomerUpdatePreEvent($customer));
|
|
$this->repository->saveCustomer($customer);
|
|
$this->dispatcher->dispatch(new CustomerUpdatePostEvent($customer));
|
|
|
|
return $customer;
|
|
}
|
|
|
|
public function findCustomerByName(string $name): ?Customer
|
|
{
|
|
return $this->repository->findOneBy(['name' => $name]);
|
|
}
|
|
|
|
public function findCustomerByNumber(string $number): ?Customer
|
|
{
|
|
return $this->repository->findOneBy(['number' => $number]);
|
|
}
|
|
|
|
/**
|
|
* @return iterable<Customer>
|
|
*/
|
|
public function findCustomer(CustomerQuery $query): iterable
|
|
{
|
|
return $this->repository->getCustomersForQuery($query);
|
|
}
|
|
|
|
public function countCustomer(bool $visible = true): int
|
|
{
|
|
return $this->repository->countCustomer($visible);
|
|
}
|
|
|
|
public function calculateNextCustomerNumber(): string
|
|
{
|
|
$format = $this->configuration->find('customer.number_format');
|
|
if (empty($format) || !\is_string($format)) {
|
|
$format = '{cc,4}';
|
|
}
|
|
|
|
$numberGenerator = new NumberGenerator($format, function (string $originalFormat, string $format, int $increaseBy): string|int {
|
|
return match ($format) {
|
|
'cc' => $this->repository->count([]) + $increaseBy,
|
|
default => $originalFormat,
|
|
};
|
|
});
|
|
|
|
return $numberGenerator->getNumber();
|
|
}
|
|
}
|