2024-02-12 18:55:09 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Livewire\BillingPortal;
|
|
|
|
|
2024-02-13 18:39:53 +01:00
|
|
|
use App\Models\RecurringInvoice;
|
|
|
|
use App\Models\Subscription;
|
|
|
|
use App\Utils\Number;
|
2024-02-14 18:29:57 +01:00
|
|
|
use Livewire\Attributes\Computed;
|
2024-02-13 18:39:53 +01:00
|
|
|
use Livewire\Attributes\On;
|
2024-02-12 18:55:09 +01:00
|
|
|
use Livewire\Component;
|
|
|
|
|
|
|
|
class Summary extends Component
|
|
|
|
{
|
2024-02-13 18:39:53 +01:00
|
|
|
public Subscription $subscription;
|
|
|
|
|
2024-02-12 18:55:09 +01:00
|
|
|
public array $context;
|
|
|
|
|
2024-02-13 18:39:53 +01:00
|
|
|
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,
|
2024-02-14 18:29:57 +01:00
|
|
|
'notes' => $product->markdownNotes(),
|
2024-02-13 18:39:53 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($this->subscription->service()->products() as $key => $product) {
|
|
|
|
$bundle['one_time_products'][$product->hashed_id] = [
|
|
|
|
'product' => $product,
|
|
|
|
'quantity' => 1,
|
2024-02-16 17:42:38 +01:00
|
|
|
'notes' => $product->markdownNotes(),
|
2024-02-13 18:39:53 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2024-02-14 18:29:57 +01:00
|
|
|
foreach ($this->subscription->service()->optional_recurring_products() as $key => $product) {
|
|
|
|
$bundle['optional_recurring_products'][$product->hashed_id] = [
|
|
|
|
'product' => $product,
|
|
|
|
'quantity' => 0,
|
2024-02-16 17:42:38 +01:00
|
|
|
'notes' => $product->markdownNotes(),
|
2024-02-14 18:29:57 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($this->subscription->service()->optional_products() as $key => $product) {
|
|
|
|
$bundle['optional_one_time_products'][$product->hashed_id] = [
|
|
|
|
'product' => $product,
|
|
|
|
'quantity' => 0,
|
2024-02-16 17:42:38 +01:00
|
|
|
'notes' => $product->markdownNotes(),
|
2024-02-14 18:29:57 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2024-02-13 18:39:53 +01:00
|
|
|
$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) {
|
2024-02-14 18:29:57 +01:00
|
|
|
return $item['product']['cost'] * $item['quantity'];
|
2024-02-13 18:39:53 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
$one_time_optional = collect($this->context['bundle']['optional_one_time_products'])->sum(function ($item) {
|
2024-02-14 18:29:57 +01:00
|
|
|
return $item['product']['cost'] * $item['quantity'];
|
2024-02-13 18:39:53 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
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) {
|
2024-02-14 18:29:57 +01:00
|
|
|
return $item['product']['cost'] * $item['quantity'];
|
2024-02-13 18:39:53 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
$recurring_optional = collect($this->context['bundle']['optional_recurring_products'])->sum(function ($item) {
|
2024-02-14 18:29:57 +01:00
|
|
|
return $item['product']['cost'] * $item['quantity'];
|
2024-02-13 18:39:53 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
if ($raw) {
|
|
|
|
return $recurring + $recurring_optional;
|
|
|
|
}
|
|
|
|
|
|
|
|
return \sprintf(
|
|
|
|
'%s/%s',
|
|
|
|
Number::formatMoney($recurring + $recurring_optional, $this->subscription->company),
|
|
|
|
RecurringInvoice::frequencyForKey($this->subscription->frequency_id)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-02-14 18:29:57 +01:00
|
|
|
#[Computed()]
|
2024-02-13 18:39:53 +01:00
|
|
|
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 [];
|
|
|
|
}
|
2024-02-17 01:15:55 +01:00
|
|
|
$products = [];
|
|
|
|
|
2024-02-13 18:39:53 +01:00
|
|
|
foreach ($this->context['bundle']['recurring_products'] as $key => $item) {
|
|
|
|
$products[] = [
|
|
|
|
'product_key' => $item['product']['product_key'],
|
|
|
|
'quantity' => $item['quantity'],
|
2024-02-15 19:33:20 +01:00
|
|
|
'total_raw' => $item['product']['cost'] * $item['quantity'],
|
2024-02-13 18:39:53 +01:00
|
|
|
'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'],
|
2024-02-15 19:33:20 +01:00
|
|
|
'total_raw' => $item['product']['cost'] * $item['quantity'],
|
2024-02-13 18:39:53 +01:00
|
|
|
'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'],
|
2024-02-15 19:33:20 +01:00
|
|
|
'total_raw' => $item['product']['cost'] * $item['quantity'],
|
2024-02-13 18:39:53 +01:00
|
|
|
'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'],
|
2024-02-15 19:33:20 +01:00
|
|
|
'total_raw' => $item['product']['cost'] * $item['quantity'],
|
2024-02-13 18:39:53 +01:00
|
|
|
'total' => Number::formatMoney($item['product']['cost'] * $item['quantity'], $this->subscription->company),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2024-02-16 17:42:38 +01:00
|
|
|
$this->dispatch('purchase.context', property: 'products', value: $products);
|
|
|
|
|
2024-02-13 18:39:53 +01:00
|
|
|
return $products;
|
|
|
|
}
|
|
|
|
|
2024-02-12 18:55:09 +01:00
|
|
|
public function render()
|
|
|
|
{
|
|
|
|
return view('billing-portal.v3.summary');
|
|
|
|
}
|
|
|
|
}
|