fix(analytics): cost weighed lines by weight, not row count #31

Merged
aefril merged 1 commits from feature/weight-based-products into main 2026-09-06 18:07:50 +02:00
Owner

A weight-based line is one weighing, so order_items.quantity is pinned to 1
while unit_price and unit_cost are per unit of weight. Analytics SQL was
multiplying and dividing per-unit rates by the raw quantity, costing a 4.2 ons
fish as a single ons: standard_hpp_total and moving_average_hpp_total came out
far too low across all four product reports, overstating gross profit, and
average_price and fifo_hpp_per_unit read per weighing while
standard_hpp_per_unit read per unit, so the three HPP figures in one row could
not be compared.

Adds billableQty and billableQtyNet as the single place that decides the
multiplier, mirroring entities.OrderItem.BillableQuantity.

quantity_sold and total_items stay as weighing counts; weight_sold already
carries the amount. revenue and fifo_hpp_total were already correct via
total_price/total_cost.

Co-Authored-By: Claude Opus 5 (1M context) noreply@anthropic.com

A weight-based line is one weighing, so order_items.quantity is pinned to 1 while unit_price and unit_cost are per unit of weight. Analytics SQL was multiplying and dividing per-unit rates by the raw quantity, costing a 4.2 ons fish as a single ons: standard_hpp_total and moving_average_hpp_total came out far too low across all four product reports, overstating gross profit, and average_price and fifo_hpp_per_unit read per weighing while standard_hpp_per_unit read per unit, so the three HPP figures in one row could not be compared. Adds billableQty and billableQtyNet as the single place that decides the multiplier, mirroring entities.OrderItem.BillableQuantity. quantity_sold and total_items stay as weighing counts; weight_sold already carries the amount. revenue and fifo_hpp_total were already correct via total_price/total_cost. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
aefril added 1 commit 2026-09-06 18:07:04 +02:00
A weight-based line is one weighing, so order_items.quantity is pinned to 1
while unit_price and unit_cost are per unit of weight. Analytics SQL was
multiplying and dividing per-unit rates by the raw quantity, costing a 4.2 ons
fish as a single ons: standard_hpp_total and moving_average_hpp_total came out
far too low across all four product reports, overstating gross profit, and
average_price and fifo_hpp_per_unit read per weighing while
standard_hpp_per_unit read per unit, so the three HPP figures in one row could
not be compared.

Adds billableQty and billableQtyNet as the single place that decides the
multiplier, mirroring entities.OrderItem.BillableQuantity.

quantity_sold and total_items stay as weighing counts; weight_sold already
carries the amount. revenue and fifo_hpp_total were already correct via
total_price/total_cost.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
aefril merged commit f0ff59fea7 into main 2026-09-06 18:07:50 +02:00
Sign in to join this conversation.
No Reviewers
No labels
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: apksel-dev/apskel-pos-backend#31