|
| 1 | +-- Migrates schema-level-1 invoices for an arbitrary set of customers to schema level 2. |
| 2 | +-- |
| 3 | +-- Gathering invoice details are intentionally excluded: they are invalid legacy data and are |
| 4 | +-- removed by the preceding gathering-detail cleanup migration. |
| 5 | +CREATE OR REPLACE FUNCTION om_func_migrate_customer_invoices_to_schema_level_2_bulk(p_customer_ids TEXT[]) |
| 6 | +RETURNS BIGINT |
| 7 | +AS $$ |
| 8 | +DECLARE |
| 9 | + v_updated_invoices BIGINT; |
| 10 | +BEGIN |
| 11 | + INSERT INTO billing_standard_invoice_detailed_lines ( |
| 12 | + id, |
| 13 | + namespace, |
| 14 | + created_at, |
| 15 | + updated_at, |
| 16 | + deleted_at, |
| 17 | + name, |
| 18 | + description, |
| 19 | + currency, |
| 20 | + amount, |
| 21 | + taxes_total, |
| 22 | + taxes_inclusive_total, |
| 23 | + taxes_exclusive_total, |
| 24 | + charges_total, |
| 25 | + discounts_total, |
| 26 | + total, |
| 27 | + service_period_start, |
| 28 | + service_period_end, |
| 29 | + quantity, |
| 30 | + invoicing_app_external_id, |
| 31 | + child_unique_reference_id, |
| 32 | + per_unit_amount, |
| 33 | + category, |
| 34 | + payment_term, |
| 35 | + index, |
| 36 | + invoice_id, |
| 37 | + parent_line_id, |
| 38 | + credits_total, |
| 39 | + credits_applied |
| 40 | + ) |
| 41 | + SELECT |
| 42 | + l.id, |
| 43 | + l.namespace, |
| 44 | + l.created_at, |
| 45 | + l.updated_at, |
| 46 | + l.deleted_at, |
| 47 | + l.name, |
| 48 | + l.description, |
| 49 | + l.currency, |
| 50 | + l.amount, |
| 51 | + l.taxes_total, |
| 52 | + l.taxes_inclusive_total, |
| 53 | + l.taxes_exclusive_total, |
| 54 | + l.charges_total, |
| 55 | + l.discounts_total, |
| 56 | + l.total, |
| 57 | + l.period_start AS service_period_start, |
| 58 | + l.period_end AS service_period_end, |
| 59 | + l.quantity, |
| 60 | + l.invoicing_app_external_id, |
| 61 | + l.child_unique_reference_id, |
| 62 | + c.per_unit_amount, |
| 63 | + c.category, |
| 64 | + c.payment_term, |
| 65 | + c.index, |
| 66 | + l.invoice_id, |
| 67 | + l.parent_line_id, |
| 68 | + l.credits_total, |
| 69 | + l.credits_applied |
| 70 | + FROM billing_invoices i |
| 71 | + JOIN billing_invoice_lines l |
| 72 | + ON l.invoice_id = i.id |
| 73 | + AND l.namespace = i.namespace |
| 74 | + JOIN billing_invoice_flat_fee_line_configs c |
| 75 | + ON c.id = l.fee_line_config_id |
| 76 | + AND c.namespace = l.namespace |
| 77 | + WHERE |
| 78 | + i.customer_id = ANY(p_customer_ids) |
| 79 | + AND i.schema_level = 1 |
| 80 | + AND i.status <> 'gathering' |
| 81 | + AND l.status = 'detailed' |
| 82 | + AND l.type = 'flat_fee' |
| 83 | + ON CONFLICT (id) DO NOTHING; |
| 84 | + |
| 85 | + INSERT INTO billing_standard_invoice_detailed_line_amount_discounts ( |
| 86 | + id, |
| 87 | + namespace, |
| 88 | + created_at, |
| 89 | + updated_at, |
| 90 | + deleted_at, |
| 91 | + child_unique_reference_id, |
| 92 | + description, |
| 93 | + reason, |
| 94 | + invoicing_app_external_id, |
| 95 | + amount, |
| 96 | + rounding_amount, |
| 97 | + source_discount, |
| 98 | + line_id |
| 99 | + ) |
| 100 | + SELECT |
| 101 | + d.id, |
| 102 | + d.namespace, |
| 103 | + d.created_at, |
| 104 | + d.updated_at, |
| 105 | + d.deleted_at, |
| 106 | + d.child_unique_reference_id, |
| 107 | + d.description, |
| 108 | + d.reason, |
| 109 | + d.invoicing_app_external_id, |
| 110 | + d.amount, |
| 111 | + d.rounding_amount, |
| 112 | + d.source_discount, |
| 113 | + d.line_id |
| 114 | + FROM billing_invoices i |
| 115 | + JOIN billing_invoice_lines l |
| 116 | + ON l.invoice_id = i.id |
| 117 | + AND l.namespace = i.namespace |
| 118 | + JOIN billing_standard_invoice_detailed_lines migrated_l |
| 119 | + ON migrated_l.id = l.id |
| 120 | + AND migrated_l.namespace = l.namespace |
| 121 | + JOIN billing_invoice_line_discounts d |
| 122 | + ON d.line_id = l.id |
| 123 | + AND d.namespace = l.namespace |
| 124 | + WHERE |
| 125 | + i.customer_id = ANY(p_customer_ids) |
| 126 | + AND i.schema_level = 1 |
| 127 | + AND i.status <> 'gathering' |
| 128 | + AND l.status = 'detailed' |
| 129 | + AND l.type = 'flat_fee' |
| 130 | + ON CONFLICT (id) DO NOTHING; |
| 131 | + |
| 132 | + UPDATE billing_invoices |
| 133 | + SET schema_level = 2 |
| 134 | + WHERE |
| 135 | + customer_id = ANY(p_customer_ids) |
| 136 | + AND schema_level = 1; |
| 137 | + |
| 138 | + GET DIAGNOSTICS v_updated_invoices = ROW_COUNT; |
| 139 | + |
| 140 | + RETURN v_updated_invoices; |
| 141 | +END; |
| 142 | +$$ LANGUAGE plpgsql VOLATILE; |
0 commit comments