check qt
This commit is contained in:
@@ -7,10 +7,12 @@ These scripts are read-only diagnostics for a PostgreSQL test database.
|
||||
Checks the two core lot quantity invariants documented in
|
||||
`lots-and-quantities.md` and `lots-and-quantities.en.md`.
|
||||
|
||||
Zero `lot_qt` rows are ignored by the quantity sums when they are still linked
|
||||
to at least one virtual lot through `lot_p` or `lot_s`; they are treated as
|
||||
legitimate memory of an open quantity consumed by a physical lot. A `lot_qt`
|
||||
row without both `lot_p` and `lot_s` is reported as anomalous.
|
||||
Zero `lot_qt` rows are ignored completely. They are treated as legitimate
|
||||
memory of an open quantity consumed by a physical lot. This is required because
|
||||
`lot_qt` represents usable open forecast and stops at zero, while the virtual
|
||||
lot may become negative to compensate the difference between theoretical and
|
||||
executed quantity. A non-zero `lot_qt` row without both `lot_p` and `lot_s` is
|
||||
reported as anomalous.
|
||||
|
||||
Run it on a restored test database:
|
||||
|
||||
|
||||
@@ -21,9 +21,11 @@ Rules checked:
|
||||
Rows linked to a purchase lot and rows without purchase lot are both
|
||||
included.
|
||||
|
||||
Zero lot_qt rows are allowed when they keep the memory of an open quantity
|
||||
consumed by a physical lot, as long as at least lot_p or lot_s is set. Unlinked
|
||||
lot_qt rows are reported as anomalies.
|
||||
Zero lot_qt rows are completely ignored by the checks. They may be the memory
|
||||
of an open quantity consumed by a physical lot. This also matters when the
|
||||
virtual lot becomes negative to compensate the difference between theoretical
|
||||
and executed quantity: lot_qt represents usable open forecast and stops at
|
||||
zero.
|
||||
|
||||
Notes:
|
||||
- The script uses the lot current state, i.e. lot_lot.lot_state joined to
|
||||
@@ -354,13 +356,17 @@ FROM (
|
||||
b.line_id,
|
||||
b.virtual_lot_id,
|
||||
ROUND(b.lot_qt_quantity, 5) AS observed_value,
|
||||
ROUND(COALESCE(b.virtual_quantity, 0), 5) AS expected_value,
|
||||
ROUND(b.lot_qt_quantity - COALESCE(b.virtual_quantity, 0), 5) AS diff,
|
||||
'sum lot_qt where lot_p = virtual lot must equal virtual lot quantity'
|
||||
ROUND(GREATEST(COALESCE(b.virtual_quantity, 0), 0), 5) AS expected_value,
|
||||
ROUND(
|
||||
b.lot_qt_quantity
|
||||
- GREATEST(COALESCE(b.virtual_quantity, 0), 0), 5) AS diff,
|
||||
'sum non-zero lot_qt where lot_p = virtual lot must equal positive virtual lot quantity'
|
||||
AS detail
|
||||
FROM purchase_lot_qt_balance b, params p
|
||||
WHERE b.lot_qt_cross_category_count = 0
|
||||
AND ABS(b.lot_qt_quantity - COALESCE(b.virtual_quantity, 0)) > p.epsilon
|
||||
AND ABS(
|
||||
b.lot_qt_quantity
|
||||
- GREATEST(COALESCE(b.virtual_quantity, 0), 0)) > p.epsilon
|
||||
|
||||
UNION ALL
|
||||
|
||||
@@ -372,13 +378,17 @@ FROM (
|
||||
b.line_id,
|
||||
b.virtual_lot_id,
|
||||
ROUND(b.lot_qt_quantity, 5) AS observed_value,
|
||||
ROUND(COALESCE(b.virtual_quantity, 0), 5) AS expected_value,
|
||||
ROUND(b.lot_qt_quantity - COALESCE(b.virtual_quantity, 0), 5) AS diff,
|
||||
'sum lot_qt where lot_s = virtual lot must equal virtual lot quantity'
|
||||
ROUND(GREATEST(COALESCE(b.virtual_quantity, 0), 0), 5) AS expected_value,
|
||||
ROUND(
|
||||
b.lot_qt_quantity
|
||||
- GREATEST(COALESCE(b.virtual_quantity, 0), 0), 5) AS diff,
|
||||
'sum non-zero lot_qt where lot_s = virtual lot must equal positive virtual lot quantity'
|
||||
AS detail
|
||||
FROM sale_lot_qt_balance b, params p
|
||||
WHERE b.lot_qt_cross_category_count = 0
|
||||
AND ABS(b.lot_qt_quantity - COALESCE(b.virtual_quantity, 0)) > p.epsilon
|
||||
AND ABS(
|
||||
b.lot_qt_quantity
|
||||
- GREATEST(COALESCE(b.virtual_quantity, 0), 0)) > p.epsilon
|
||||
|
||||
UNION ALL
|
||||
|
||||
@@ -460,10 +470,11 @@ FROM (
|
||||
COALESCE(lqt.lot_quantity, 0)::numeric AS observed_value,
|
||||
0::numeric AS expected_value,
|
||||
COALESCE(lqt.lot_quantity, 0)::numeric AS diff,
|
||||
'lot_qt row has neither lot_p nor lot_s; zero rows are allowed only when linked to at least one virtual lot'
|
||||
'non-zero lot_qt row has neither lot_p nor lot_s'
|
||||
AS detail
|
||||
FROM lot_qt lqt
|
||||
WHERE lqt.lot_p IS NULL
|
||||
AND lqt.lot_s IS NULL
|
||||
AND COALESCE(lqt.lot_quantity, 0) <> 0
|
||||
) issues
|
||||
ORDER BY check_name, contract_number, line_id, virtual_lot_id;
|
||||
|
||||
Reference in New Issue
Block a user