|
| 1 | +<script lang="ts"> |
| 2 | + import { FormList, InputChoice, InputNumber } from '$lib/elements/forms'; |
| 3 | + import { formatCurrency } from '$lib/helpers/numbers'; |
| 4 | + import type { Coupon, Estimation } from '$lib/sdk/billing'; |
| 5 | + import { sdk } from '$lib/stores/sdk'; |
| 6 | + import { AppwriteException } from '@appwrite.io/console'; |
| 7 | + import Card from '../card.svelte'; |
| 8 | + import DiscountsApplied from './discountsApplied.svelte'; |
| 9 | + import { addNotification } from '$lib/stores/notifications'; |
| 10 | +
|
| 11 | + export let organizationId: string | undefined = undefined; |
| 12 | + export let billingPlan: string; |
| 13 | + export let collaborators: string[]; |
| 14 | + export let fixedCoupon = false; |
| 15 | + export let couponData: Partial<Coupon>; |
| 16 | +
|
| 17 | + export let billingBudget: number; |
| 18 | +
|
| 19 | + let budgetEnabled = false; |
| 20 | + let estimation: Estimation; |
| 21 | +
|
| 22 | + async function getEstimate( |
| 23 | + billingPlan: string, |
| 24 | + collaborators: string[], |
| 25 | + couponId: string | undefined |
| 26 | + ) { |
| 27 | + try { |
| 28 | + estimation = await sdk.forConsole.billing.estimationCreateOrganization( |
| 29 | + billingPlan, |
| 30 | + couponId === '' ? null : couponId, |
| 31 | + collaborators ?? [] |
| 32 | + ); |
| 33 | + } catch (e) { |
| 34 | + if (e instanceof AppwriteException) { |
| 35 | + if ( |
| 36 | + e.type === 'billing_coupon_not_found' || |
| 37 | + e.type === 'billing_coupon_already_used' || |
| 38 | + e.type === 'billing_credit_unsupported' |
| 39 | + ) { |
| 40 | + couponData = { |
| 41 | + code: null, |
| 42 | + status: null, |
| 43 | + credits: null |
| 44 | + }; |
| 45 | + } |
| 46 | + } |
| 47 | + addNotification({ |
| 48 | + type: 'error', |
| 49 | + isHtml: false, |
| 50 | + message: e.message |
| 51 | + }); |
| 52 | + } |
| 53 | + } |
| 54 | +
|
| 55 | + async function getUpdatePlanEstimate( |
| 56 | + organizationId: string, |
| 57 | + billingPlan: string, |
| 58 | + collaborators: string[], |
| 59 | + couponId: string | undefined |
| 60 | + ) { |
| 61 | + try { |
| 62 | + estimation = await sdk.forConsole.billing.estimationUpdatePlan( |
| 63 | + organizationId, |
| 64 | + billingPlan, |
| 65 | + couponId && couponId.length > 0 ? couponId : null, |
| 66 | + collaborators ?? [] |
| 67 | + ); |
| 68 | + } catch (e) { |
| 69 | + if (e instanceof AppwriteException) { |
| 70 | + if ( |
| 71 | + e.type === 'billing_coupon_not_found' || |
| 72 | + e.type === 'billing_coupon_already_used' || |
| 73 | + e.type === 'billing_credit_unsupported' |
| 74 | + ) { |
| 75 | + couponData = { |
| 76 | + code: null, |
| 77 | + status: null, |
| 78 | + credits: null |
| 79 | + }; |
| 80 | + } |
| 81 | + } |
| 82 | + addNotification({ |
| 83 | + type: 'error', |
| 84 | + isHtml: false, |
| 85 | + message: e.message |
| 86 | + }); |
| 87 | + } |
| 88 | + } |
| 89 | +
|
| 90 | + $: organizationId |
| 91 | + ? getUpdatePlanEstimate(organizationId, billingPlan, collaborators, couponData?.code) |
| 92 | + : getEstimate(billingPlan, collaborators, couponData?.code); |
| 93 | +</script> |
| 94 | + |
| 95 | +{#if estimation} |
| 96 | + <Card class="u-flex u-flex-vertical u-gap-8"> |
| 97 | + <slot /> |
| 98 | + {#if estimation} |
| 99 | + {#each estimation.items ?? [] as item} |
| 100 | + {#if item.value > 0} |
| 101 | + <span class="u-flex u-main-space-between"> |
| 102 | + <p class="text">{item.label}</p> |
| 103 | + <p class="text">{formatCurrency(item.value)}</p> |
| 104 | + </span> |
| 105 | + {/if} |
| 106 | + {/each} |
| 107 | + {#each estimation.discounts ?? [] as item} |
| 108 | + <DiscountsApplied {fixedCoupon} bind:couponData {...item} /> |
| 109 | + {/each} |
| 110 | + <div class="u-sep-block-start" /> |
| 111 | + <span class="u-flex u-main-space-between"> |
| 112 | + <p class="text">Total due</p> |
| 113 | + <p class="text"> |
| 114 | + {formatCurrency(estimation.grossAmount)} |
| 115 | + </p> |
| 116 | + </span> |
| 117 | + |
| 118 | + <p class="text u-margin-block-start-16"> |
| 119 | + You'll pay <span class="u-bold">{formatCurrency(estimation.grossAmount)}</span> now. |
| 120 | + {#if couponData?.code}Once your credits run out,{:else}Then{/if} you'll be charged |
| 121 | + <span class="u-bold">{formatCurrency(estimation.amount)}</span> every 30 days. |
| 122 | + </p> |
| 123 | + {/if} |
| 124 | + |
| 125 | + <FormList class="u-margin-block-start-24"> |
| 126 | + <InputChoice |
| 127 | + type="switchbox" |
| 128 | + id="budget" |
| 129 | + label="Enable budget cap" |
| 130 | + tooltip="If enabled, you will be notified when your spending reaches 75% of the set cap. Update cap alerts in your organization settings." |
| 131 | + fullWidth |
| 132 | + bind:value={budgetEnabled}> |
| 133 | + {#if budgetEnabled} |
| 134 | + <div class="u-margin-block-start-16"> |
| 135 | + <InputNumber |
| 136 | + id="budget" |
| 137 | + label="Budget cap (USD)" |
| 138 | + placeholder="0" |
| 139 | + min={0} |
| 140 | + bind:value={billingBudget} /> |
| 141 | + </div> |
| 142 | + {/if} |
| 143 | + </InputChoice> |
| 144 | + </FormList> |
| 145 | + </Card> |
| 146 | +{/if} |
0 commit comments