48 lines
1.2 KiB
PHP
48 lines
1.2 KiB
PHP
<?php
|
|
|
|
namespace App\Helpers;
|
|
|
|
use App\Models\Invoice;
|
|
use App\Models\Order;
|
|
use Illuminate\Support\Str;
|
|
|
|
class OrderHelper
|
|
{
|
|
/**
|
|
* Generate a unique prefixed number.
|
|
*
|
|
* Examples:
|
|
* generateOrderNumber() → ORD-A1B2C3D4
|
|
* generateOrderNumber('INV') → INV-A1B2C3D4
|
|
*
|
|
* Uniqueness is guaranteed by checking the correct column on each model.
|
|
*/
|
|
public static function generateOrderNumber(string $prefix = 'ORD'): string
|
|
{
|
|
do {
|
|
$number = $prefix . '-' . strtoupper(Str::random(8));
|
|
|
|
$exists = match ($prefix) {
|
|
'INV' => Invoice::where('invoice_number', $number)->exists(),
|
|
default => Order::where('order_num', $number)->exists(),
|
|
};
|
|
} while ($exists);
|
|
|
|
return $number;
|
|
}
|
|
|
|
/** Number of months a billing_cycle value represents (used for pricing and expires_at math). */
|
|
public static function cycleToMonths(?string $cycle): int
|
|
{
|
|
return match ($cycle) {
|
|
'monthly' => 1,
|
|
'quarterly' => 3,
|
|
'semi_annually' => 6,
|
|
'annually' => 12,
|
|
'biennially' => 24,
|
|
'triennially' => 36,
|
|
default => 1,
|
|
};
|
|
}
|
|
}
|