|
| 1 | +package fi.espoo.evaka.invoicing.service |
| 2 | + |
| 3 | +import fi.espoo.evaka.shared.ChildId |
| 4 | +import fi.espoo.evaka.shared.DaycareId |
| 5 | +import fi.espoo.evaka.shared.InvoiceCorrectionId |
| 6 | +import fi.espoo.evaka.shared.PersonId |
| 7 | +import fi.espoo.evaka.shared.db.Database |
| 8 | +import fi.espoo.evaka.shared.db.Predicate |
| 9 | +import fi.espoo.evaka.shared.db.PredicateSql |
| 10 | +import fi.espoo.evaka.shared.domain.FiniteDateRange |
| 11 | +import java.time.LocalDate |
| 12 | +import java.time.YearMonth |
| 13 | + |
| 14 | +private fun Database.Read.getInvoiceCorrections(where: Predicate) = |
| 15 | + createQuery { |
| 16 | + sql( |
| 17 | + """ |
| 18 | +SELECT |
| 19 | + id, |
| 20 | + target_month, |
| 21 | + head_of_family_id, |
| 22 | + child_id, |
| 23 | + unit_id, |
| 24 | + product, |
| 25 | + period, |
| 26 | + amount, |
| 27 | + unit_price, |
| 28 | + description, |
| 29 | + note |
| 30 | +FROM invoice_correction |
| 31 | +WHERE |
| 32 | + target_month IS NOT NULL AND |
| 33 | + ${predicate(where.forTable("invoice_correction"))} |
| 34 | +""" |
| 35 | + ) |
| 36 | + } |
| 37 | + .toList { |
| 38 | + InvoiceCorrection( |
| 39 | + id = column("id"), |
| 40 | + targetMonth = YearMonth.from(column<LocalDate>("target_month")), |
| 41 | + headOfFamilyId = column("head_of_family_id"), |
| 42 | + childId = column("child_id"), |
| 43 | + unitId = column("unit_id"), |
| 44 | + product = column("product"), |
| 45 | + period = column("period"), |
| 46 | + amount = column("amount"), |
| 47 | + unitPrice = column("unit_price"), |
| 48 | + description = column("description"), |
| 49 | + note = column("note"), |
| 50 | + ) |
| 51 | + } |
| 52 | + |
| 53 | +// TODO: indeksi |
| 54 | +// CREATE INDEX idx$invoice_correction_target ON invoice_correction(target) |
| 55 | +fun Database.Read.getInvoiceCorrectionsForMonth( |
| 56 | + month: YearMonth |
| 57 | +): List<InvoiceGenerator.InvoiceCorrection> = |
| 58 | + getInvoiceCorrections(Predicate { where("$it.target_month = ${bind(month.atDay(1))}") }).map { |
| 59 | + InvoiceGenerator.InvoiceCorrection( |
| 60 | + id = it.id, |
| 61 | + headOfFamilyId = it.headOfFamilyId, |
| 62 | + childId = it.childId, |
| 63 | + unitId = it.unitId, |
| 64 | + product = it.product, |
| 65 | + period = it.period, |
| 66 | + amount = it.amount, |
| 67 | + unitPrice = it.unitPrice, |
| 68 | + description = it.description, |
| 69 | + ) |
| 70 | + } |
| 71 | + |
| 72 | +fun Database.Read.getInvoiceCorrectionsByIds( |
| 73 | + ids: Set<InvoiceCorrectionId> |
| 74 | +): List<InvoiceCorrection> = |
| 75 | + getInvoiceCorrections(Predicate { where("$it.id = ANY(${bind(ids)})") }) |
| 76 | + |
| 77 | +fun Database.Transaction.movePastUnappliedInvoiceCorrections( |
| 78 | + headOfFamilyIds: Set<PersonId>?, |
| 79 | + targetMonth: YearMonth, |
| 80 | +) { |
| 81 | + val firstOfMonth = targetMonth.atDay(1) |
| 82 | + val headOfFamilyPredicate = |
| 83 | + if (headOfFamilyIds != null) { |
| 84 | + PredicateSql { where("head_of_family_id = ANY (${bind(headOfFamilyIds)})") } |
| 85 | + } else { |
| 86 | + PredicateSql.alwaysTrue() |
| 87 | + } |
| 88 | + |
| 89 | + // TODO: This query has to scan all past invoice corrections |
| 90 | + execute { |
| 91 | + sql( |
| 92 | + """ |
| 93 | +UPDATE invoice_correction |
| 94 | +SET target_month = ${bind(firstOfMonth)} |
| 95 | +WHERE |
| 96 | + target_month < ${bind(firstOfMonth)} AND |
| 97 | + ${predicate(headOfFamilyPredicate)} AND |
| 98 | + NOT EXISTS (SELECT FROM invoice_row WHERE correction_id = invoice_correction.id) |
| 99 | +""" |
| 100 | + ) |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +data class InvoiceCorrectionInsert( |
| 105 | + val targetMonth: YearMonth, |
| 106 | + val headOfFamilyId: PersonId, |
| 107 | + val childId: ChildId, |
| 108 | + val unitId: DaycareId, |
| 109 | + val product: ProductKey, |
| 110 | + val period: FiniteDateRange, |
| 111 | + val amount: Int, |
| 112 | + val unitPrice: Int, |
| 113 | + val description: String, |
| 114 | + val note: String, |
| 115 | +) |
| 116 | + |
| 117 | +fun Database.Transaction.insertInvoiceCorrections(corrections: Iterable<InvoiceCorrectionInsert>) = |
| 118 | + executeBatch(corrections) { |
| 119 | + sql( |
| 120 | + """ |
| 121 | +INSERT INTO invoice_correction (target_month, head_of_family_id, child_id, unit_id, product, period, amount, unit_price, description, note) |
| 122 | +VALUES ( |
| 123 | + ${bind { it.targetMonth.atDay(1) }}, |
| 124 | + ${bind { it.headOfFamilyId }}, |
| 125 | + ${bind { it.childId }}, |
| 126 | + ${bind { it.unitId }}, |
| 127 | + ${bind { it.product }}, |
| 128 | + ${bind { it.period }}, |
| 129 | + ${bind { it.amount }}, |
| 130 | + ${bind { it.unitPrice }}, |
| 131 | + ${bind { it.description }}, |
| 132 | + ${bind { it.note }} |
| 133 | +) |
| 134 | +""" |
| 135 | + ) |
| 136 | + } |
| 137 | + |
| 138 | +data class InvoiceCorrectionUpdate( |
| 139 | + val id: InvoiceCorrectionId, |
| 140 | + val amount: Int, |
| 141 | + val unitPrice: Int, |
| 142 | +) |
| 143 | + |
| 144 | +fun Database.Transaction.updateInvoiceCorrections(items: Iterable<InvoiceCorrectionUpdate>) = |
| 145 | + executeBatch(items) { |
| 146 | + sql( |
| 147 | + """ |
| 148 | +UPDATE invoice_correction |
| 149 | +SET amount = ${bind { it.amount }}, unit_price = ${bind { it.unitPrice }} |
| 150 | +WHERE id = ${bind { it.id }} |
| 151 | +""" |
| 152 | + ) |
| 153 | + } |
0 commit comments