Skip to content

Commit

Permalink
Update to combine all final invoices into invoice_type: final
Browse files Browse the repository at this point in the history
  • Loading branch information
jshearer committed Sep 26, 2023
1 parent 1c686c1 commit 0513e52
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 32 deletions.
24 changes: 11 additions & 13 deletions crates/billing-integrations/src/stripe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,12 @@ fn parse_date(arg: &str) -> Result<NaiveDate, ParseError> {
#[derive(Debug, Clone, PartialEq, Eq, Hash, sqlx::Type, Serialize, Deserialize)]
#[sqlx(rename_all = "snake_case")]
enum InvoiceType {
#[serde(rename = "usage")]
Usage,
#[serde(rename = "manual")]
Manual,
#[serde(rename = "current_month")]
CurrentMonth,
#[serde(rename = "final")]
Final,
#[serde(rename = "preview")]
Preview,
#[serde(rename = "manual")]
Manual,
}

#[derive(Serialize, Default, Debug)]
Expand Down Expand Up @@ -113,10 +111,10 @@ impl Invoice {
recreate_finalized: bool,
) -> anyhow::Result<()> {
match (&self.invoice_type, &self.extra) {
(InvoiceType::CurrentMonth, _) => {
bail!("Should not create Stripe invoices for dynamic current_month invoices")
(InvoiceType::Preview, _) => {
bail!("Should not create Stripe invoices for preview invoices")
}
(InvoiceType::Usage, Some(extra)) => {
(InvoiceType::Final, Some(extra)) => {
let unwrapped_extra = extra.clone().0.expect(
"This is just a sqlx quirk, if the outer Option is Some then this will be Some",
);
Expand All @@ -129,15 +127,15 @@ impl Invoice {
return Ok(());
}
}
(InvoiceType::Usage, None) => {
(InvoiceType::Final, None) => {
bail!("Invoice should have extra")
}
_ => {}
};

// An invoice should be generated in Stripe if the tenant is on a paid plan, which means:
// * The tenant has a free trial start date
if let InvoiceType::Usage = self.invoice_type {
if let InvoiceType::Final = self.invoice_type {
if let None = get_tenant_trial_date(&db_client, self.billed_prefix.to_owned()).await? {
tracing::info!("Skipping usage invoice for tenant in free tier");
return Ok(());
Expand Down Expand Up @@ -437,9 +435,9 @@ pub async fn do_publish_invoices(cmd: &PublishInvoice) -> anyhow::Result<()> {
});

tracing::info!(
"Processing {usage} usage-based bills, and {manual} manually-entered bills.",
"Processing {usage} usage-based invoices, and {manual} manually-entered invoices.",
usage = invoice_type_counter
.remove(&InvoiceType::Usage)
.remove(&InvoiceType::Final)
.unwrap_or_default(),
manual = invoice_type_counter
.remove(&InvoiceType::Manual)
Expand Down
20 changes: 3 additions & 17 deletions supabase/migrations/30_manual_billing.sql
Original file line number Diff line number Diff line change
Expand Up @@ -326,16 +326,6 @@ manual_bills as (
from internal.manual_bills
inner join authorized_tenants on manual_bills.tenant ^@ authorized_tenants.tenant
),
current_month as (
select
date_trunc('month', (report->>'billed_month')::timestamptz)::date as date_start,
(date_trunc('month', (report->>'billed_month')::timestamptz) + interval '1 month' - interval '1 day')::date as date_end,
report->>'billed_prefix' as billed_prefix,
coalesce(nullif(report->'line_items', 'null'::jsonb), '[]'::jsonb) as line_items,
coalesce(nullif(report->'subtotal', 'null'::jsonb), to_jsonb(0))::integer as subtotal,
report as extra
from authorized_tenants, internal.billing_report_202308(authorized_tenants.tenant, now()) as report
),
generated_prior_months as (
select
date_trunc('month', (report->>'billed_month')::timestamptz)::date as date_start,
Expand All @@ -347,13 +337,13 @@ generated_prior_months as (
from authorized_tenants
join generate_series(
greatest(date '2023-08-01', date_trunc('month', authorized_tenants.created_at)::date),
date_trunc('month',now()::date - interval '1 month'),
date_trunc('month',now()::date),
'1 month'
) as invoice_month on not exists(select 1 from historical_bills where historical_bills.date_start = invoice_month)
join internal.billing_report_202308(authorized_tenants.tenant, invoice_month) as report on true
)
select
h.date_start, h.date_end, h.billed_prefix, h.line_items, h.subtotal, h.extra, 'usage' as invoice_type
h.date_start, h.date_end, h.billed_prefix, h.line_items, h.subtotal, h.extra, 'final' as invoice_type
from historical_bills h
union all
select
Expand All @@ -362,11 +352,7 @@ from generated_prior_months p
union all
select
m.date_start, m.date_end, m.billed_prefix, m.line_items, m.subtotal, m.extra, 'manual' as invoice_type
from manual_bills m
union all
select
c.date_start, c.date_end, c.billed_prefix, c.line_items, c.subtotal, c.extra, 'current_month' as invoice_type
from current_month c;
from manual_bills m;

grant select on table invoices_ext to authenticated;

Expand Down
4 changes: 2 additions & 2 deletions supabase/tests/billing.test.sql
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ begin
'date_end', (date_trunc('month', now()) + interval '1 month' - interval '1 day')::date,
'subtotal', 0,
'line_items', '[]'::jsonb,
'invoice_type', 'current_month',
'invoice_type', 'preview',
'billed_prefix', 'aliceCo/'
),
'{
Expand All @@ -638,7 +638,7 @@ begin
"billed_prefix": "aliceCo/",
"date_end": "2022-08-31",
"date_start": "2022-08-01",
"invoice_type": "usage",
"invoice_type": "final",
"line_items": [
{
"count": 1,
Expand Down

0 comments on commit 0513e52

Please sign in to comment.