Allow zero label counts; keep reprint weights stable #36
10
slips/api.py
10
slips/api.py
@@ -114,10 +114,12 @@ def update_order(request, order_id: int, payload: OrderUpdateIn):
|
|||||||
if payload.canister_count is not None or payload.box_count is not None:
|
if payload.canister_count is not None or payload.box_count is not None:
|
||||||
if order.status != 'pending':
|
if order.status != 'pending':
|
||||||
return 400, {'detail': 'Only pending orders can change label counts'}
|
return 400, {'detail': 'Only pending orders can change label counts'}
|
||||||
if payload.canister_count is not None and payload.canister_count < 1:
|
cc = payload.canister_count if payload.canister_count is not None else order.canister_count
|
||||||
return 400, {'detail': 'canister_count must be >= 1'}
|
bc = payload.box_count if payload.box_count is not None else order.box_count
|
||||||
if payload.box_count is not None and payload.box_count < 1:
|
if cc < 0 or bc < 0:
|
||||||
return 400, {'detail': 'box_count must be >= 1'}
|
return 400, {'detail': 'counts must be >= 0'}
|
||||||
|
if cc == 0 and bc == 0:
|
||||||
|
return 400, {'detail': 'canister_count and box_count cannot both be 0'}
|
||||||
order.set_counts(payload.canister_count, payload.box_count)
|
order.set_counts(payload.canister_count, payload.box_count)
|
||||||
|
|
||||||
if payload.status is not None:
|
if payload.status is not None:
|
||||||
|
|||||||
@@ -55,16 +55,13 @@ class Order(models.Model):
|
|||||||
self.box_count = math.ceil(self.canister_count / self.canisters_per_box)
|
self.box_count = math.ceil(self.canister_count / self.canisters_per_box)
|
||||||
|
|
||||||
def set_counts(self, canister_count=None, box_count=None):
|
def set_counts(self, canister_count=None, box_count=None):
|
||||||
"""Edit label counts; keep total_weight aligned with canisters."""
|
"""Edit print quantities only; total_weight stays the packaging truth."""
|
||||||
if canister_count is not None:
|
if canister_count is not None:
|
||||||
self.canister_count = canister_count
|
self.canister_count = canister_count
|
||||||
self.total_weight = canister_count * self.canister_weight
|
|
||||||
if box_count is not None:
|
if box_count is not None:
|
||||||
self.box_count = box_count
|
self.box_count = box_count
|
||||||
|
|
||||||
def save(self, *args, **kwargs):
|
def save(self, *args, **kwargs):
|
||||||
if not self.canister_count and self.total_weight and self.canister_weight:
|
|
||||||
self.apply_weight()
|
|
||||||
# Mixed sheets: canister + box labels share pages; total in canister_pages
|
# Mixed sheets: canister + box labels share pages; total in canister_pages
|
||||||
n = self.canister_count + self.box_count
|
n = self.canister_count + self.box_count
|
||||||
self.canister_pages = math.ceil(n / 4) if n else 0
|
self.canister_pages = math.ceil(n / 4) if n else 0
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
"""Build print pages: per-slip weights, always 4 slips per page."""
|
"""Build print pages: per-slip weights, always 4 slips per page."""
|
||||||
|
|
||||||
|
import math
|
||||||
|
|
||||||
|
|
||||||
def _display_weight(w):
|
def _display_weight(w):
|
||||||
"""Whole numbers as int (5 not 5.0) for print labels."""
|
"""Whole numbers as int (5 not 5.0) for print labels."""
|
||||||
@@ -17,18 +19,37 @@ def _pad_to_four(weights):
|
|||||||
return weights
|
return weights
|
||||||
|
|
||||||
|
|
||||||
|
def _box_weight_template(order):
|
||||||
|
"""Box weights from order packaging (total_weight), not print counts."""
|
||||||
|
if not order.canister_weight or not order.canisters_per_box:
|
||||||
|
return []
|
||||||
|
logical_cans = math.ceil(order.total_weight / order.canister_weight)
|
||||||
|
if not logical_cans:
|
||||||
|
return []
|
||||||
|
cans_left = logical_cans
|
||||||
|
logical_boxes = math.ceil(logical_cans / order.canisters_per_box)
|
||||||
|
template = []
|
||||||
|
for _ in range(logical_boxes):
|
||||||
|
cans_in_box = min(order.canisters_per_box, cans_left)
|
||||||
|
template.append(_display_weight(order.canister_weight * cans_in_box))
|
||||||
|
cans_left -= cans_in_box
|
||||||
|
return template
|
||||||
|
|
||||||
|
|
||||||
def build_print_pages(order):
|
def build_print_pages(order):
|
||||||
"""Mix box then canister labels on shared sheets (pad only last page)."""
|
"""Mix box then canister labels on shared sheets (pad only last page).
|
||||||
|
|
||||||
|
canister_count / box_count are print quantities only; slip weights come from
|
||||||
|
total_weight packaging so reprint steppers do not change label content.
|
||||||
|
"""
|
||||||
cw = _display_weight(order.canister_weight)
|
cw = _display_weight(order.canister_weight)
|
||||||
canister_weights = [cw] * order.canister_count
|
canister_weights = [cw] * order.canister_count
|
||||||
|
|
||||||
# Box labels — last box may be a partial fill
|
template = _box_weight_template(order)
|
||||||
cans_left = order.canister_count
|
if not template:
|
||||||
box_weights = []
|
box_weights = []
|
||||||
for _ in range(order.box_count):
|
else:
|
||||||
cans_in_box = min(order.canisters_per_box, cans_left)
|
box_weights = [template[i % len(template)] for i in range(order.box_count)]
|
||||||
box_weights.append(_display_weight(order.canister_weight * cans_in_box))
|
|
||||||
cans_left -= cans_in_box
|
|
||||||
|
|
||||||
weights = box_weights + canister_weights
|
weights = box_weights + canister_weights
|
||||||
pages = []
|
pages = []
|
||||||
@@ -42,6 +63,7 @@ def build_print_pages(order):
|
|||||||
# ponytail: assert-based check; upgrade to pytest if suite grows
|
# ponytail: assert-based check; upgrade to pytest if suite grows
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
class O:
|
class O:
|
||||||
|
total_weight = 50.0
|
||||||
canister_count = 10
|
canister_count = 10
|
||||||
canister_weight = 5.0
|
canister_weight = 5.0
|
||||||
canisters_per_box = 4
|
canisters_per_box = 4
|
||||||
@@ -55,8 +77,18 @@ if __name__ == '__main__':
|
|||||||
assert pages[1]['slips'] == [5, 5, 5, 5]
|
assert pages[1]['slips'] == [5, 5, 5, 5]
|
||||||
assert pages[2]['slips'] == [5, 5, 5, 5]
|
assert pages[2]['slips'] == [5, 5, 5, 5]
|
||||||
assert pages[3]['slips'] == [5, 5, 5, 5] # last page full
|
assert pages[3]['slips'] == [5, 5, 5, 5] # last page full
|
||||||
|
|
||||||
|
# Fewer canister labels to print — box weights unchanged
|
||||||
|
class FewerCans(O):
|
||||||
|
canister_count = 7
|
||||||
|
|
||||||
|
fewer = build_print_pages(FewerCans())
|
||||||
|
assert fewer[0]['slips'][:3] == [20, 20, 10]
|
||||||
|
assert len(fewer) == 3 # 3 boxes + 7 cans = 10 → 3 pages
|
||||||
|
|
||||||
# 3 cans + 1 box → one mixed page
|
# 3 cans + 1 box → one mixed page
|
||||||
class Small:
|
class Small:
|
||||||
|
total_weight = 15.0
|
||||||
canister_count = 3
|
canister_count = 3
|
||||||
canister_weight = 5.0
|
canister_weight = 5.0
|
||||||
canisters_per_box = 4
|
canisters_per_box = 4
|
||||||
|
|||||||
@@ -174,10 +174,14 @@ function showToast(message, isError = false) {
|
|||||||
|
|
||||||
async function adjustCount(id, field, delta) {
|
async function adjustCount(id, field, delta) {
|
||||||
const spanId = field === 'canister_count' ? `cc-${id}` : `bc-${id}`;
|
const spanId = field === 'canister_count' ? `cc-${id}` : `bc-${id}`;
|
||||||
|
const otherId = field === 'canister_count' ? `bc-${id}` : `cc-${id}`;
|
||||||
const span = document.getElementById(spanId);
|
const span = document.getElementById(spanId);
|
||||||
if (!span) return;
|
if (!span) return;
|
||||||
const next = parseInt(span.textContent, 10) + delta;
|
const next = parseInt(span.textContent, 10) + delta;
|
||||||
if (next < 1) return;
|
if (next < 0) return;
|
||||||
|
const other = document.getElementById(otherId);
|
||||||
|
const otherVal = other ? parseInt(other.textContent, 10) : 0;
|
||||||
|
if (next === 0 && otherVal === 0) return;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const res = await fetch(`/api/orders/${id}`, {
|
const res = await fetch(`/api/orders/${id}`, {
|
||||||
|
|||||||
Reference in New Issue
Block a user