"Real" entity numbers in Invoice Settings preview

This refactors the GeneratesCounter trait slightly, such that arbitrary
entity numbers can be formatted according to the given padding/pattern.

With that small abstraction we can use the trait in the PdfMock instance
to show the exmple entity number using real patterns without actually
incrementing it or checking the number is available in the database.
This commit is contained in:
Dave Shoreman 2024-05-28 22:12:28 +01:00
parent 354cc3343b
commit 9195addb37
No known key found for this signature in database
GPG Key ID: C920D1D63709F443
2 changed files with 33 additions and 8 deletions

View File

@ -27,11 +27,13 @@ use App\Models\PurchaseOrderInvitation;
use App\Models\Quote;
use App\Models\QuoteInvitation;
use App\Models\Vendor;
use App\Utils\Traits\GeneratesCounter;
use App\Utils\Traits\MakesHash;
class PdfMock
{
use MakesHash;
use GeneratesCounter;
private mixed $mock;
@ -206,6 +208,14 @@ class PdfMock
*/
public function getStubVariables(): array
{
// Although $this->mock is the Invoice/etc entity, we need the invitation to get company details.
$entity_number = $this->getFormattedEntityNumber(
$this->mock->invitation,
29,
$this->settings->counter_padding,
$this->settings->invoice_number_pattern
);
return ['values' =>
[
'$client.shipping_postal_code' => '46420',
@ -370,7 +380,7 @@ class PdfMock
'$company.phone' => $this->settings->phone,
'$company.state' => $this->settings->state,
'$credit.number' => '0029',
'$entity_number' => '0029',
'$entity_number' => $entity_number,
'$credit_number' => '0029',
'$global_margin' => '6.35mm',
'$contact.phone' => '681-480-9828',

View File

@ -288,7 +288,6 @@ trait GeneratesCounter
*/
public function getNextProjectNumber(Project $project): string
{
$entity_number = $this->getNextEntityNumber(Project::class, $project->client, false);
return $this->replaceUserVars($project, $entity_number);
@ -412,7 +411,7 @@ trait GeneratesCounter
*
* @param string $pattern
* @param string $prefix
* @return string The padded and prefixed entity number
* @return string The padded, prefixed and unique entity number
*/
private function checkEntityNumber($class, $entity, $counter, $padding, $pattern, $prefix = ''): string
{
@ -420,11 +419,7 @@ trait GeneratesCounter
$check_counter = 1;
do {
$number = $this->padCounter($counter, $padding);
$number = $this->applyNumberPattern($entity, $number, $pattern);
$number = $this->prefixCounter($number, $prefix);
$number = $this->getFormattedEntityNumber($entity, $counter, $padding, $pattern);
$check = $class::where('company_id', $entity->company_id)->where('number', $number)->withTrashed()->exists();
@ -443,6 +438,26 @@ trait GeneratesCounter
return $number;
}
/**
* Formats the entity number according to pattern, prefix and padding.
*
* @param Collection $entity The entity ie App\Models\Client, Invoice, Quote etc
* @param int $counter The counter
* @param int $padding The padding
* @param string $pattern
* @param string $prefix
*
* @return string The padded and prefixed entity number
*/
public function getFormattedEntityNumber($entity, $counter, $padding, $pattern, $prefix = ''): string
{
$number = $this->padCounter($counter, $padding);
$number = $this->applyNumberPattern($entity, $number, $pattern);
return $this->prefixCounter($number, $prefix);
}
/*Check if a number is available for use. */
public function checkNumberAvailable($class, $entity, $number): bool
{