akaunting/app/Utilities/DocumentNumber.php

40 lines
1.1 KiB
PHP
Raw Normal View History

2023-04-28 17:38:49 +02:00
<?php
2023-04-29 01:43:48 +03:00
namespace App\Utilities;
2023-04-28 17:38:49 +02:00
2023-04-29 01:43:48 +03:00
use App\Interfaces\Utility\DocumentNumber as DocumentNumberInterface;
2023-04-28 17:38:49 +02:00
use App\Models\Common\Contact;
2023-04-29 01:43:48 +03:00
class DocumentNumber implements DocumentNumberInterface
2023-04-28 17:38:49 +02:00
{
2023-04-29 01:43:48 +03:00
public function getNextNumber(string $type, ?Contact $contact): string
2023-04-28 17:38:49 +02:00
{
$type = $this->resolveTypeAlias($type);
$prefix = setting($type . '.number_prefix');
2023-04-29 01:43:48 +03:00
$next = (string) setting($type . '.number_next');
$digit = (int) setting($type . '.number_digit');
2023-04-28 17:38:49 +02:00
return $prefix . str_pad($next, $digit, '0', STR_PAD_LEFT);
}
2023-04-29 01:43:48 +03:00
public function increaseNextNumber(string $type, ?Contact $contact): void
2023-04-28 17:38:49 +02:00
{
$type = $this->resolveTypeAlias($type);
$next = setting($type . '.number_next', 1) + 1;
setting([$type . '.number_next' => $next]);
setting()->save();
}
protected function resolveTypeAlias(string $type): string
{
if ($alias = config('type.document.' . $type . '.alias')) {
return $alias . '.' . str_replace('-', '_', $type);
}
return $type;
}
}