Two things were wrong with this code:
1. The exchange rate conversion was backward. Since we work in USD, I
removed the conversion and used value_usd instead.
2. The calculation of pscale.factor was incorrect. If the old value is
1000, and we need to get to 1020, the record to be inserted must have
a value of 20, thus the value for factor needs to be:
(1020 - 1000) / 1000 = (1020 / 1000) - 1 = 0.02
0.02 * 1000 = 20, which is inserted in the new record.
The subtraction was missing before, which of course caused WAY wrong
numbers that should have been caught a long time ago. It was doing:
1020 / 1000 = 1.02
1.02 * 1000 = 1020 was inserted, doubling the intended adjustment.
Now that we think we know what we're doing, I'm using a more rigorous
approach to defining iterations and tags, and this condition is no
longer necessary, and in fact, may be harmful. This I'm removing it.
The initial load of data will have these values.
iter | tag | purpose
--------|-------------|-------------------------------------------------
plan | baseline | Adjustments will be made to these values only.
actuals | open-orders | Ordered before 2024 season, still open.
actuals | booked | Ordered in 2024 season so far
This happens when a customer places identical orders and specifies that
they be shipped in different seasons. We end up with one order shipped
in 2023 and one open for 2024. Under our current operating procedures,
open orders are subtracted from shipped orders and are presented as
baseline for the next forecast. The zero that appears in the baseline in
this case was causing all kinds of issues in the SQL scripts for
inserting the adjustments, ranging from finding zero rows to adjust to
division by zero.
Another change required to correct this was updating the iter value of
the open orders from 'actuals' to 'copy':
UPDATE rlarp.osm_pool
SET iter = 'copy'
WHERE tag = 'open-orders'
Lots of cleanup here too, removing large swaths of code that are no
longer needed. Many improvements the Excel workbook, which is kept in
Teams, not git. These changes may or may not have had accompanying VBA
changes.