- Create/Update metered billing plan

This commit is contained in:
Čarodej
2021-12-22 09:05:15 +01:00
parent 6bb7789232
commit b1cb7be678
9 changed files with 353 additions and 115 deletions
@@ -14,13 +14,13 @@ class FormatUsageEstimatesAction
$usage = match ($estimate['feature']) {
'bandwidth' => Metric::megabytes($estimate['usage'])->format(),
'storage' => Metric::megabytes($estimate['usage'])->format(),
'flat-fee' => intval($estimate['usage']) . ' ' . __('Pcs.'),
'flatFee' => intval($estimate['usage']) . ' ' . __('Pcs.'),
};
// Normalize units
$amount = match ($estimate['feature']) {
'bandwidth', 'storage' => $estimate['amount'] / 1000,
'flat-fee' => $estimate['amount'],
'flatFee' => $estimate['amount'],
};
return [
@@ -12,12 +12,21 @@ class ReportUsageAction
->where('status', 'active')
->cursor()
->each(function ($subscription) {
$this->recordBandwidth($subscription);
$this->recordStorageCapacity($subscription);
if ($subscription->plan->meteredFeatures()->where('key', 'bandwidth')->exists()) {
$this->recordBandwidth($subscription);
}
if ($subscription->plan->meteredFeatures()->where('key', 'storage')->exists()) {
$this->recordStorageUsage($subscription);
}
if ($subscription->plan->meteredFeatures()->where('key', 'flatFee')->exists()) {
$this->recordFlatFee($subscription);
}
});
}
private function recordStorageCapacity(Subscription $subscription): void
private function recordStorageUsage(Subscription $subscription): void
{
// Sum all file size
$filesize = DB::table('files')
@@ -45,4 +54,10 @@ class ReportUsageAction
// Record storage capacity usage
$subscription->recordUsage('bandwidth', $amount);
}
private function recordFlatFee(Subscription $subscription): void
{
// Record flat fee
$subscription->recordUsage('flatFee', 1);
}
}