mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-21 03:26:07 +00:00
fix(costs): keep display-currency payments settled, count them as typed in the plan and the PDF, and file every booking type under its Costs key
A display-currency payment whose halves pointed opposite ways no longer stayed settled (#1445), the plan sidebar and the PDF total still went through the trip currency, and a zero-decimal display currency was not rounded. Every booking type now maps to one Costs key on every path; a cost the old map filed under 'other' still moves with a type change.
This commit is contained in:
@@ -1122,7 +1122,7 @@ function useDayPlanSidebar(props: DayPlanSidebarProps) {
|
||||
const budgetItems = useTripStore(s => s.budgetItems)
|
||||
const costsEnabled = useAddonStore(s => s.isEnabled('budget'))
|
||||
const totalCostLabel = useMemo(() => {
|
||||
const costs = planCosts({ days, assignments, reservations, budgetItems, costsEnabled, tripCurrency: currency })
|
||||
const costs = planCosts({ days, assignments, reservations, budgetItems, costsEnabled, tripCurrency: currency, displayCurrency: costBase })
|
||||
return formatMoneySum(costs.total, costBase, locale, fxRates)
|
||||
}, [days, assignments, reservations, budgetItems, costsEnabled, currency, costBase, locale, fxRates])
|
||||
|
||||
|
||||
@@ -65,6 +65,19 @@ describe('planCosts (#2551)', () => {
|
||||
expect(costs.byDay.get(2)).toEqual([{ amount: 100, currency: 'USD' }, { amount: 30, currency: 'EUR' }])
|
||||
})
|
||||
|
||||
it('counts an expense entered in the display currency as typed, not through the trip currency (#2965)', () => {
|
||||
// A yen trip read in euros: the flight was typed in euros, frozen at 0.008 EUR per yen.
|
||||
const flight = buildBudgetItem({ total_price: 1000, currency: 'EUR', exchange_rate: 0.008, place_id: 51 })
|
||||
const hotel = buildBudgetItem({ total_price: 20000, currency: null, place_id: 51 })
|
||||
const read = { ...base, tripCurrency: 'JPY', budgetItems: [flight, hotel], costsEnabled: true }
|
||||
expect(planCosts({ ...read, displayCurrency: 'eur' }).total).toEqual([
|
||||
{ amount: 1000, currency: 'EUR' },
|
||||
{ amount: 20000, currency: 'JPY' },
|
||||
])
|
||||
// Without a display currency (the PDF), the euros still count at their frozen rate.
|
||||
expect(planCosts(read).total).toEqual([{ amount: 1000 / 0.008 + 20000, currency: 'JPY' }])
|
||||
})
|
||||
|
||||
it('nets a refund against the expenses of its day', () => {
|
||||
const items = [
|
||||
buildBudgetItem({ total_price: 200, currency: null, place_id: 50 }),
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import type { AssignmentsMap, BudgetItem, Day, Reservation } from '../../types'
|
||||
import type { MoneyEntry } from '../../utils/formatters'
|
||||
import { bookedInTrip } from '../../hooks/useExchangeRates'
|
||||
import { bookedInTrip, inDisplayCurrency } from '../../hooks/useExchangeRates'
|
||||
import { daySpan, startKey } from './bookings/bookingsModel'
|
||||
|
||||
/** What the plan's cost figures add up: per day, and for the whole trip. */
|
||||
@@ -22,18 +22,22 @@ export interface PlanCosts {
|
||||
* An expense counts on the day its booking starts, else the first day its place is
|
||||
* planned on, else the day of its date. One that belongs to no day still counts in
|
||||
* the total. A foreign amount booked at a frozen rate counts at that rate, as Costs
|
||||
* counts it.
|
||||
* counts it. One entered in `displayCurrency`, when the caller reads the plan in one,
|
||||
* counts as typed, as Costs shows it (#2965): booked into the trip currency and back
|
||||
* at today's rate it drifted off the amount entered. The PDF passes none, since it
|
||||
* adds everything up in the trip currency.
|
||||
*
|
||||
* With Costs off there are no expenses, and a place's own price (set through the
|
||||
* API) still counts, once, on the first day the place is planned on.
|
||||
*/
|
||||
export function planCosts({ days, assignments, reservations, budgetItems, costsEnabled, tripCurrency }: {
|
||||
export function planCosts({ days, assignments, reservations, budgetItems, costsEnabled, tripCurrency, displayCurrency }: {
|
||||
days: Day[]
|
||||
assignments: AssignmentsMap
|
||||
reservations: Reservation[]
|
||||
budgetItems: BudgetItem[]
|
||||
costsEnabled: boolean
|
||||
tripCurrency: string
|
||||
displayCurrency?: string | null
|
||||
}): PlanCosts {
|
||||
const trip = (tripCurrency || 'EUR').toUpperCase()
|
||||
const firstDayOfPlace = new Map<number, number>()
|
||||
@@ -84,6 +88,10 @@ export function planCosts({ days, assignments, reservations, budgetItems, costsE
|
||||
for (const item of budgetItems) {
|
||||
const amount = item.total_price || 0
|
||||
if (amount === 0) continue
|
||||
if (displayCurrency && inDisplayCurrency(item.currency, trip, displayCurrency)) {
|
||||
add(dayOfExpense(item), { amount, currency: displayCurrency.toUpperCase() })
|
||||
continue
|
||||
}
|
||||
const booked = bookedInTrip(amount, item.currency, item.exchange_rate, trip)
|
||||
const entry = booked != null
|
||||
? { amount: booked, currency: trip }
|
||||
|
||||
Reference in New Issue
Block a user