| @@ -113,7 +113,7 @@ open class StockLedgerFixExportService( | |||||
| writer.write("-- stock-ledger-fix patch\n") | writer.write("-- stock-ledger-fix patch\n") | ||||
| writer.write("-- from $from inclusive to ${toExclusive.minusDays(1)} inclusive (date < $toExclusive)\n") | writer.write("-- from $from inclusive to ${toExclusive.minusDays(1)} inclusive (date < $toExclusive)\n") | ||||
| writer.write("-- parts=${parts.joinToString(",")}\n") | writer.write("-- parts=${parts.joinToString(",")}\n") | ||||
| writer.write("-- apply order: 1.0 lot outQty NULL→0 then inventory → 2.1 delete ledger + stock_out lot → ledger(+2.3) UPDATE → 2.7a lot inQty + SIL/SOL/adjustment record then ADJ INSERT → 2.6 stock_lot_day\n") | |||||
| writer.write("-- apply order: 1.0 inventory (stockUomId) then lot outQty NULL→0 → 2.1 delete ledger + stock_out lot → ledger(+2.3) UPDATE → 2.7a lot inQty + SIL/SOL/adjustment record then ADJ INSERT → 2.6 stock_lot_day\n") | |||||
| writer.write("-- INNER JOIN id: target rows without matching id are skipped on UPDATE\n") | writer.write("-- INNER JOIN id: target rows without matching id are skipped on UPDATE\n") | ||||
| if (want23 && !want10) { | if (want23 && !want10) { | ||||
| writer.write("-- WARN: 2.3 without 1.0 — new inventory ids may be missing on target; prefer include 1.0\n") | writer.write("-- WARN: 2.3 without 1.0 — new inventory ids may be missing on target; prefer include 1.0\n") | ||||
| @@ -153,10 +153,41 @@ open class StockLedgerFixExportService( | |||||
| } | } | ||||
| private fun writeExportInventory10(writer: BufferedWriter, jdbc: NamedParameterJdbcTemplate) { | private fun writeExportInventory10(writer: BufferedWriter, jdbc: NamedParameterJdbcTemplate) { | ||||
| writer.write("-- === 1.0 lot line outQty NULL → 0 (before inventory; trigger may run, next block overwrites buckets) ===\n") | |||||
| writer.write("-- === 1.0 inventory (stockUomId first, so the later outQty trigger updates this row) ===\n") | |||||
| writeInventoryUpsert(writer, jdbc) | |||||
| writeTempUpdate( | |||||
| writer, | |||||
| jdbc, | |||||
| emptyMap(), | |||||
| title = "-- === 1.0 soft-delete orphan inventory (stockUomId still null) ===\n", | |||||
| selectSql = """ | |||||
| SELECT id, modified, modifiedBy | |||||
| FROM inventory | |||||
| WHERE deleted = 1 | |||||
| AND modifiedBy IN ('DNP', 'stock-ledger-fix') | |||||
| ORDER BY id | |||||
| """.trimIndent(), | |||||
| tempName = "tmp_sl_fix_inv_del", | |||||
| columnDefs = "modified DATETIME NULL, modifiedBy VARCHAR(255) NULL", | |||||
| columns = listOf("id", "modified", "modifiedBy"), | |||||
| updateSql = """ | |||||
| UPDATE inventory i | |||||
| INNER JOIN tmp_sl_fix_inv_del t ON t.id = i.id | |||||
| SET | |||||
| i.deleted = 1, | |||||
| i.modified = t.modified, | |||||
| i.modifiedBy = t.modifiedBy; | |||||
| """.trimIndent(), | |||||
| emptyComment = "-- (no stock-ledger-fix deleted inventory rows)", | |||||
| ) | |||||
| writer.write("-- === 1.0 lot line outQty NULL → 0 (after stockUomId; trigger updates the existing bucket) ===\n") | |||||
| writer.write(StockLedgerFixInventorySql.ZERO_NULL_OUT_QTY_SQL.trim()) | writer.write(StockLedgerFixInventorySql.ZERO_NULL_OUT_QTY_SQL.trim()) | ||||
| writer.write(";\n\n") | writer.write(";\n\n") | ||||
| writer.write("-- === 1.0 inventory (INSERT with fixed id + UPDATE qty/status) ===\n") | |||||
| writer.write("-- === 1.0 inventory qty again (trigger above replaced onHandQty) ===\n") | |||||
| writeInventoryUpsert(writer, jdbc) | |||||
| } | |||||
| private fun writeInventoryUpsert(writer: BufferedWriter, jdbc: NamedParameterJdbcTemplate) { | |||||
| var batch = 0 | var batch = 0 | ||||
| jdbc.query( | jdbc.query( | ||||
| """ | """ | ||||
| @@ -218,31 +249,6 @@ open class StockLedgerFixExportService( | |||||
| } else { | } else { | ||||
| writer.write("-- (no inventory rows)\n\n") | writer.write("-- (no inventory rows)\n\n") | ||||
| } | } | ||||
| writeTempUpdate( | |||||
| writer, | |||||
| jdbc, | |||||
| emptyMap(), | |||||
| title = "-- === 1.0 soft-delete orphan inventory (stockUomId still null) ===\n", | |||||
| selectSql = """ | |||||
| SELECT id, modified, modifiedBy | |||||
| FROM inventory | |||||
| WHERE deleted = 1 | |||||
| AND modifiedBy IN ('DNP', 'stock-ledger-fix') | |||||
| ORDER BY id | |||||
| """.trimIndent(), | |||||
| tempName = "tmp_sl_fix_inv_del", | |||||
| columnDefs = "modified DATETIME NULL, modifiedBy VARCHAR(255) NULL", | |||||
| columns = listOf("id", "modified", "modifiedBy"), | |||||
| updateSql = """ | |||||
| UPDATE inventory i | |||||
| INNER JOIN tmp_sl_fix_inv_del t ON t.id = i.id | |||||
| SET | |||||
| i.deleted = 1, | |||||
| i.modified = t.modified, | |||||
| i.modifiedBy = t.modifiedBy; | |||||
| """.trimIndent(), | |||||
| emptyComment = "-- (no stock-ledger-fix deleted inventory rows)", | |||||
| ) | |||||
| } | } | ||||
| /** | /** | ||||