feat(finance): add settlement currency and cycle fx rates

This commit is contained in:
2026-03-10 16:46:59 +04:00
parent 4c0508f618
commit fb85219409
38 changed files with 3546 additions and 114 deletions

View File

@@ -12,6 +12,7 @@
"0008_lowly_spiral.sql": "4f016332d60f7ef1fef0311210a0fa1a0bfc1d9b6da1da4380a60c14d54a9681",
"0009_quiet_wallflower.sql": "c5bcb6a01b6f22a9e64866ac0d11468105aad8b2afb248296370f15b462e3087",
"0010_wild_molecule_man.sql": "46027a6ac770cdc2efd4c3eb5bb53f09d1b852c70fdc46a2434e5a7064587245",
"0011_previous_ezekiel_stane.sql": "d996e64d3854de22e36dedeaa94e46774399163d90263bbb05e0b9199af79b70"
"0011_previous_ezekiel_stane.sql": "d996e64d3854de22e36dedeaa94e46774399163d90263bbb05e0b9199af79b70",
"0012_clumsy_maestro.sql": "173797fb435c6acd7c268c624942d6f19a887c329bcef409a3dde1249baaeb8a"
}
}

View File

@@ -0,0 +1,16 @@
CREATE TABLE "billing_cycle_exchange_rates" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"cycle_id" uuid NOT NULL,
"source_currency" text NOT NULL,
"target_currency" text NOT NULL,
"rate_micros" bigint NOT NULL,
"effective_date" date NOT NULL,
"source" text DEFAULT 'nbg' NOT NULL,
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
"updated_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
ALTER TABLE "household_billing_settings" ADD COLUMN "settlement_currency" text DEFAULT 'GEL' NOT NULL;--> statement-breakpoint
ALTER TABLE "billing_cycle_exchange_rates" ADD CONSTRAINT "billing_cycle_exchange_rates_cycle_id_billing_cycles_id_fk" FOREIGN KEY ("cycle_id") REFERENCES "public"."billing_cycles"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
CREATE UNIQUE INDEX "billing_cycle_exchange_rates_cycle_pair_unique" ON "billing_cycle_exchange_rates" USING btree ("cycle_id","source_currency","target_currency");--> statement-breakpoint
CREATE INDEX "billing_cycle_exchange_rates_cycle_idx" ON "billing_cycle_exchange_rates" USING btree ("cycle_id");

File diff suppressed because it is too large Load Diff

View File

@@ -85,6 +85,13 @@
"when": 1773096404367,
"tag": "0011_previous_ezekiel_stane",
"breakpoints": true
},
{
"idx": 12,
"version": "7",
"when": 1773146577992,
"tag": "0012_clumsy_maestro",
"breakpoints": true
}
]
}

View File

@@ -26,6 +26,7 @@ export const householdBillingSettings = pgTable(
householdId: uuid('household_id')
.notNull()
.references(() => households.id, { onDelete: 'cascade' }),
settlementCurrency: text('settlement_currency').default('GEL').notNull(),
rentAmountMinor: bigint('rent_amount_minor', { mode: 'bigint' }),
rentCurrency: text('rent_currency').default('USD').notNull(),
rentDueDay: integer('rent_due_day').default(20).notNull(),
@@ -257,6 +258,31 @@ export const rentRules = pgTable(
})
)
export const billingCycleExchangeRates = pgTable(
'billing_cycle_exchange_rates',
{
id: uuid('id').defaultRandom().primaryKey(),
cycleId: uuid('cycle_id')
.notNull()
.references(() => billingCycles.id, { onDelete: 'cascade' }),
sourceCurrency: text('source_currency').notNull(),
targetCurrency: text('target_currency').notNull(),
rateMicros: bigint('rate_micros', { mode: 'bigint' }).notNull(),
effectiveDate: date('effective_date').notNull(),
source: text('source').default('nbg').notNull(),
createdAt: timestamp('created_at', { withTimezone: true }).defaultNow().notNull(),
updatedAt: timestamp('updated_at', { withTimezone: true }).defaultNow().notNull()
},
(table) => ({
cyclePairUnique: uniqueIndex('billing_cycle_exchange_rates_cycle_pair_unique').on(
table.cycleId,
table.sourceCurrency,
table.targetCurrency
),
cycleIdx: index('billing_cycle_exchange_rates_cycle_idx').on(table.cycleId)
})
)
export const utilityBills = pgTable(
'utility_bills',
{
@@ -517,6 +543,7 @@ export type HouseholdTopicBinding = typeof householdTopicBindings.$inferSelect
export type HouseholdUtilityCategory = typeof householdUtilityCategories.$inferSelect
export type Member = typeof members.$inferSelect
export type BillingCycle = typeof billingCycles.$inferSelect
export type BillingCycleExchangeRate = typeof billingCycleExchangeRates.$inferSelect
export type UtilityBill = typeof utilityBills.$inferSelect
export type PurchaseEntry = typeof purchaseEntries.$inferSelect
export type PurchaseMessage = typeof purchaseMessages.$inferSelect