34 lines
820 B
PHP
34 lines
820 B
PHP
<?php
|
|
|
|
namespace App\Helpers;
|
|
|
|
use Illuminate\Support\Str;
|
|
use App\Models\Order;
|
|
|
|
class OrderHelper
|
|
{
|
|
/**
|
|
* Generate a unique order number with prefix.
|
|
*
|
|
* @param string $prefix
|
|
* @return string
|
|
*/
|
|
public static function generateOrderNumber(string $prefix = 'ORD'): string
|
|
{
|
|
do {
|
|
// Generate a random string of 8 uppercase characters/numbers
|
|
$random = strtoupper(Str::random(8));
|
|
$orderNum = $prefix . '-' . $random;
|
|
|
|
if ($prefix === 'INV') {
|
|
$exists = \App\Models\Invoice::where('order_num', $orderNum)->exists();
|
|
} else {
|
|
$exists = Order::where('order_num', $orderNum)->exists();
|
|
}
|
|
|
|
} while ($exists);
|
|
|
|
return $orderNum;
|
|
}
|
|
}
|