taxable-lines.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /**
  2. * Which lines of a document are taxed.
  3. *
  4. * A workshop says which kinds of charge its tax applies to: parts, labor and
  5. * the shop fee. That answer is the default for a new line of that kind. The
  6. * line then carries its own flag, because the rule has exceptions the kind
  7. * cannot express (a disposal fee among the parts, fabrication labor that is
  8. * taxed where repair labor is not), and because a later change in settings
  9. * must not re-tax a job that already went out.
  10. *
  11. * Nothing here reads settings or the database: it is shared by the editors,
  12. * which total a document as it is typed, and the server, which has the last
  13. * word on what is stored.
  14. */
  15. import { roundMoney } from './money'
  16. /** The kinds of charge the workshop's tax can be switched on or off for. */
  17. export interface TaxApplies {
  18. parts: boolean
  19. labor: boolean
  20. shopFee: boolean
  21. }
  22. /** Every kind taxed: how every workshop starts, and what "no setting" means. */
  23. export const TAX_APPLIES_TO_ALL: TaxApplies = { parts: true, labor: true, shopFee: true }
  24. export type TaxLineKind = keyof TaxApplies
  25. /** A shop fee is stored among the labor lines and told apart by how it is priced. */
  26. export function laborLineKind(line: { pricingType?: string | null }): 'labor' | 'shopFee' {
  27. return line.pricingType === 'shopFee' ? 'shopFee' : 'labor'
  28. }
  29. /**
  30. * Whether a line is taxed: what the line itself says, or the workshop's
  31. * answer for its kind when it says nothing. A line says nothing until
  32. * somebody chooses for it, or when it comes from a client built before
  33. * lines could be untaxed.
  34. */
  35. export function resolveTaxable(
  36. explicit: boolean | null | undefined,
  37. kind: TaxLineKind,
  38. applies: TaxApplies = TAX_APPLIES_TO_ALL
  39. ): boolean {
  40. return typeof explicit === 'boolean' ? explicit : applies[kind]
  41. }
  42. export function partTaxable(
  43. line: { taxable?: boolean | null },
  44. applies: TaxApplies = TAX_APPLIES_TO_ALL
  45. ): boolean {
  46. return resolveTaxable(line.taxable, 'parts', applies)
  47. }
  48. export function laborTaxable(
  49. line: { taxable?: boolean | null; pricingType?: string | null },
  50. applies: TaxApplies = TAX_APPLIES_TO_ALL
  51. ): boolean {
  52. return resolveTaxable(line.taxable, laborLineKind(line), applies)
  53. }
  54. type MoneyLine = { total?: number | null; taxable?: boolean | null }
  55. const sum = (lines: readonly MoneyLine[], pick: (line: MoneyLine) => boolean): number =>
  56. lines.reduce((acc, line) => (pick(line) ? acc + (Number(line.total) || 0) : acc), 0)
  57. /** The taxable share of a list of part lines. */
  58. export function taxablePartsTotal(
  59. parts: readonly MoneyLine[],
  60. applies: TaxApplies = TAX_APPLIES_TO_ALL
  61. ): number {
  62. return sum(parts, (line) => partTaxable(line, applies))
  63. }
  64. /** The taxable share of a list of labor lines, shop fee included. */
  65. export function taxableLaborTotal(
  66. labor: readonly (MoneyLine & { pricingType?: string | null })[],
  67. applies: TaxApplies = TAX_APPLIES_TO_ALL
  68. ): number {
  69. return sum(labor, (line) => laborTaxable(line, applies))
  70. }
  71. /**
  72. * The sum of a document's taxable lines: the `taxableSubtotal` its totals
  73. * are built from. Pass the lines the subtotal was summed from and no others
  74. * (a quote leaves out the lines the customer declined).
  75. */
  76. export function taxableSubtotalOf(
  77. parts: readonly MoneyLine[],
  78. labor: readonly (MoneyLine & { pricingType?: string | null })[],
  79. applies: TaxApplies = TAX_APPLIES_TO_ALL
  80. ): number {
  81. return roundMoney(taxablePartsTotal(parts, applies) + taxableLaborTotal(labor, applies))
  82. }
  83. /** True when at least one line of the document is left out of the tax. */
  84. export function hasUntaxedLines(
  85. parts: readonly MoneyLine[],
  86. labor: readonly (MoneyLine & { pricingType?: string | null })[],
  87. applies: TaxApplies = TAX_APPLIES_TO_ALL
  88. ): boolean {
  89. return (
  90. parts.some((line) => !partTaxable(line, applies)) ||
  91. labor.some((line) => !laborTaxable(line, applies))
  92. )
  93. }
  94. /**
  95. * Part lines with the flag settled: each keeps what it said, and one that
  96. * said nothing takes the workshop's answer for parts. What the server stores,
  97. * so a stored line always carries a plain yes or no.
  98. */
  99. export function withTaxableParts<T extends { taxable?: boolean | null }>(
  100. lines: readonly T[],
  101. applies: TaxApplies
  102. ): (Omit<T, 'taxable'> & { taxable: boolean })[] {
  103. return lines.map((line) => ({ ...line, taxable: partTaxable(line, applies) }))
  104. }
  105. /** Labor lines with the flag settled; a shop fee line takes the answer for the fee. */
  106. export function withTaxableLabor<
  107. T extends { taxable?: boolean | null; pricingType?: string | null },
  108. >(lines: readonly T[], applies: TaxApplies): (Omit<T, 'taxable'> & { taxable: boolean })[] {
  109. return lines.map((line) => ({ ...line, taxable: laborTaxable(line, applies) }))
  110. }
  111. /**
  112. * How much of a document is taxed, for the places that show its totals: the
  113. * taxable part of the parts, of the labor and of the subtotal, in the terms
  114. * the lines were entered in, and what the tax was charged on once the
  115. * discount is counted. Only ever built for a document with at least one
  116. * untaxed line; where every line is taxed there is nothing to say, and the
  117. * totals read as they always have.
  118. */
  119. export interface TaxableShare {
  120. parts: number
  121. labor: number
  122. subtotal: number
  123. /** The "Taxable amount" a totals box shows above the tax. */
  124. taxableAmount: number
  125. }
  126. /**
  127. * The sum of a template's lines that the workshop taxes today, for the
  128. * invoice about to be generated from it. A template's lines carry no tax
  129. * choice of their own, so each follows the rule for its kind; a shop fee is
  130. * a labor row told apart by how it is priced, and has its own rule.
  131. */
  132. export function recurringTaxableSubtotal(
  133. template: {
  134. cost: number
  135. templateParts: { quantity: number; unitPrice: number }[]
  136. templateLabor: { hours: number; rate: number; pricingType?: string | null }[]
  137. },
  138. applies: TaxApplies
  139. ): number {
  140. const parts = template.templateParts.reduce((sum, p) => sum + p.quantity * p.unitPrice, 0)
  141. const labor = template.templateLabor.reduce(
  142. (sum, l) => (laborTaxable(l, applies) ? sum + l.hours * l.rate : sum),
  143. 0
  144. )
  145. return template.cost + (applies.parts ? parts : 0) + labor
  146. }