Skip to content

Commit

Permalink
Merge pull request #94 from heseya/feature/MPT-2008
Browse files Browse the repository at this point in the history
Added random number order code
  • Loading branch information
daVitekPL authored Oct 30, 2024
2 parents 3ea665b + caeb506 commit df28a63
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
6 changes: 6 additions & 0 deletions app/Services/NameService.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public function generate(): string

return $this->render($pattern, [
'r' => Str::of(Str::random())->upper(),
'n' => $this->generateRandomDigits(),

'year' => date('Y'),
'month' => date('n'),
Expand Down Expand Up @@ -92,4 +93,9 @@ private function render(string $pattern, array $params): string

return $number;
}

private function generateRandomDigits(int $count = 3, int $min = 10000, int $max = 99999): string
{
return implode('', array_map(fn () => mt_rand($min, $max), range(1, $count)));
}
}
11 changes: 10 additions & 1 deletion app/Services/OrderService.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ public function store(OrderDto $dto): Order
/** @var Order $order */
$order = Order::query()->create(
[
'code' => $this->nameService->generate(),
'code' => $this->generateUniqueOrderCode(),
'currency' => $currency->value,
'shipping_price_initial' => Money::zero($currency->value),
'shipping_price' => Money::zero($currency->value),
Expand Down Expand Up @@ -692,4 +692,13 @@ private function resolveShippingPlace(
default => null,
};
}

private function generateUniqueOrderCode(): string
{
do {
$code = $this->nameService->generate();
} while (Order::query()->where('code', '=', $code)->exists());

return $code;
}
}

0 comments on commit df28a63

Please sign in to comment.