feat(billing): renewals, coupons, VAT numbering, billing cycles and invoice payment tracking
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 5.5
parent
ea9a53f742
commit
25f23d541e
@@ -4,33 +4,53 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\Support;
|
||||
|
||||
use App\Helpers\OrderHelper;
|
||||
use App\Models\Addon;
|
||||
use App\Models\Product;
|
||||
use App\Models\Service;
|
||||
|
||||
class RenewalPricing
|
||||
{
|
||||
/** @return list<int> */
|
||||
public static function validMonths(string $billingCycle): array
|
||||
/** @return list<array{billing_cycle: string, months: int, price: int}> */
|
||||
public static function options(?Product $product): array
|
||||
{
|
||||
$cycleMonths = OrderHelper::cycleToMonths($billingCycle);
|
||||
|
||||
if ($cycleMonths > 24) {
|
||||
return [$cycleMonths];
|
||||
if (! $product) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$maxMultiple = intdiv(24, $cycleMonths);
|
||||
$options = [];
|
||||
foreach ($product->pricings()->where('is_active', 1)->get() as $pricing) {
|
||||
$cycle = (string) $pricing->billing_cycle;
|
||||
$months = BillingCycleCatalog::monthsFor($cycle);
|
||||
|
||||
return array_map(static fn (int $n): int => $n * $cycleMonths, range(1, $maxMultiple));
|
||||
if (! $months || (int) $pricing->price <= 0 || ! ProductBillingCycles::allows($product->product_type, $cycle)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$options[$months] = ['billing_cycle' => $cycle, 'months' => $months, 'price' => (int) $pricing->price];
|
||||
}
|
||||
ksort($options);
|
||||
|
||||
return array_values($options);
|
||||
}
|
||||
|
||||
public static function addonTotal(Service $service, int $cycles): int
|
||||
public static function option(?Product $product, int $months): ?array
|
||||
{
|
||||
foreach (self::options($product) as $option) {
|
||||
if ($option['months'] === $months) {
|
||||
return $option;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static function addonTotal(Service $service, float $cycles): int
|
||||
{
|
||||
return (int) array_sum(array_column(self::addonInvoiceItems($service, $cycles, 0), 'total_price'));
|
||||
}
|
||||
|
||||
/** @return list<array<string, mixed>> */
|
||||
public static function addonInvoiceItems(Service $service, int $cycles, int $months): array
|
||||
public static function addonInvoiceItems(Service $service, float $cycles, int $months): array
|
||||
{
|
||||
$meta = array_filter(
|
||||
(array) ($service->orderItem?->addons_meta ?? []),
|
||||
@@ -47,7 +67,7 @@ class RenewalPricing
|
||||
foreach ($meta as $entry) {
|
||||
$qty = (int) $entry['qty'];
|
||||
$unitPrice = (int) ($currentPrices[$entry['addon_id'] ?? null] ?? $entry['price'] ?? 0);
|
||||
$totalPrice = $unitPrice * $qty * $cycles;
|
||||
$totalPrice = (int) round($unitPrice * $qty * $cycles);
|
||||
|
||||
if ($totalPrice <= 0) {
|
||||
continue;
|
||||
@@ -59,7 +79,7 @@ class RenewalPricing
|
||||
'description' => 'Addon: ' . ($entry['name'] ?? strtoupper((string) ($entry['code'] ?? ''))) . ' x' . $qty,
|
||||
'months' => $months,
|
||||
'quantity' => $qty,
|
||||
'unit_price' => $unitPrice * $cycles,
|
||||
'unit_price' => (int) round($unitPrice * $cycles),
|
||||
'total_price' => $totalPrice,
|
||||
];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user