diff --git a/app/Livewire/BillingPortal/Summary.php b/app/Livewire/BillingPortal/Summary.php index d812336bf0f6..886ef58577af 100644 --- a/app/Livewire/BillingPortal/Summary.php +++ b/app/Livewire/BillingPortal/Summary.php @@ -12,12 +12,154 @@ namespace App\Livewire\BillingPortal; +use App\Models\RecurringInvoice; +use App\Models\Subscription; +use App\Utils\Number; +use Livewire\Attributes\On; use Livewire\Component; class Summary extends Component { + public Subscription $subscription; + public array $context; + #[On('purchase.context')] + public function handleContext(string $property, $value): self + { + $this->context[$property] = $value; + + // The following may not be needed, as we can pass arround $context. + // cache()->set($this->hash, $this->context); + + return $this; + } + + public function mount() + { + $bundle = $this->context['bundle'] ?? [ + 'recurring_products' => [], + 'optional_recurring_products' => [], + 'one_time_products' => [], + 'optional_one_time_products' => [], + ]; + + foreach ($this->subscription->service()->recurring_products() as $key => $product) { + $bundle['recurring_products'][$product->hashed_id] = [ + 'product' => $product, + 'quantity' => 1, + ]; + } + + foreach ($this->subscription->service()->products() as $key => $product) { + $bundle['one_time_products'][$product->hashed_id] = [ + 'product' => $product, + 'quantity' => 1, + ]; + } + + $this->dispatch('purchase.context', property: 'bundle', value: $bundle); + } + + public function oneTimePurchasesTotal(bool $raw = false) + { + if (isset($this->context['bundle']['recurring_products']) === false) { + return 0; + } + + $one_time = collect($this->context['bundle']['one_time_products'])->sum(function ($item) { + return $item['product']['cost']; + }); + + $one_time_optional = collect($this->context['bundle']['optional_one_time_products'])->sum(function ($item) { + return $item['product']['cost']; + }); + + if ($raw) { + return $one_time + $one_time_optional; + } + + return Number::formatMoney($one_time + $one_time_optional, $this->subscription->company); + + } + + public function recurringPurchasesTotal(bool $raw = false) + { + if (isset($this->context['bundle']['recurring_products']) === false) { + return 0; + } + + $recurring = collect($this->context['bundle']['recurring_products'])->sum(function ($item) { + return $item['product']['cost']; + }); + + $recurring_optional = collect($this->context['bundle']['optional_recurring_products'])->sum(function ($item) { + return $item['product']['cost']; + }); + + if ($raw) { + return $recurring + $recurring_optional; + } + + return \sprintf( + '%s/%s', + Number::formatMoney($recurring + $recurring_optional, $this->subscription->company), + RecurringInvoice::frequencyForKey($this->subscription->frequency_id) + ); + } + + public function total() + { + return Number::formatMoney( + collect([ + $this->oneTimePurchasesTotal(raw: true), + $this->recurringPurchasesTotal(raw: true), + ])->sum(), + $this->subscription->company + ); + } + + public function items() + { + if (isset($this->context['bundle']) === false) { + return []; + } + + foreach ($this->context['bundle']['recurring_products'] as $key => $item) { + $products[] = [ + 'product_key' => $item['product']['product_key'], + 'quantity' => $item['quantity'], + 'total' => Number::formatMoney($item['product']['cost'] * $item['quantity'], $this->subscription->company) . ' / ' . RecurringInvoice::frequencyForKey($this->subscription->frequency_id), + ]; + } + + foreach ($this->context['bundle']['optional_recurring_products'] as $key => $item) { + $products[] = [ + 'product_key' => $item['product']['product_key'], + 'quantity' => $item['quantity'], + 'total' => Number::formatMoney($item['product']['cost'] * $item['quantity'], $this->subscription->company) . ' / ' . RecurringInvoice::frequencyForKey($this->subscription->frequency_id), + ]; + } + + foreach ($this->context['bundle']['one_time_products'] as $key => $item) { + $products[] = [ + 'product_key' => $item['product']['product_key'], + 'quantity' => $item['quantity'], + 'total' => Number::formatMoney($item['product']['cost'] * $item['quantity'], $this->subscription->company), + ]; + } + + foreach ($this->context['bundle']['optional_one_time_products'] as $key => $item) { + $products[] = [ + 'product_key' => $item['product']['product_key'], + 'quantity' => $item['quantity'], + 'total' => Number::formatMoney($item['product']['cost'] * $item['quantity'], $this->subscription->company), + ]; + } + + return $products; + } + public function render() { return view('billing-portal.v3.summary');