Files
product-slip/slips/print_layout.py
2026-10-11 12:53:57 +03:30

69 lines
2.1 KiB
Python

"""Build print pages: per-slip weights, always 4 slips per page."""
def _display_weight(w):
"""Whole numbers as int (5 not 5.0) for print labels."""
f = float(w)
return int(f) if f.is_integer() else f
def _pad_to_four(weights):
"""Pad short page by repeating the first slip on that page."""
if not weights:
return weights
first = weights[0]
while len(weights) < 4:
weights.append(first)
return weights
def build_print_pages(order):
"""Mix box then canister labels on shared sheets (pad only last page)."""
cw = _display_weight(order.canister_weight)
canister_weights = [cw] * order.canister_count
# Box labels — last box may be a partial fill
cans_left = order.canister_count
box_weights = []
for _ in range(order.box_count):
cans_in_box = min(order.canisters_per_box, cans_left)
box_weights.append(_display_weight(order.canister_weight * cans_in_box))
cans_left -= cans_in_box
weights = box_weights + canister_weights
pages = []
for i in range(0, len(weights), 4):
pages.append({
'slips': _pad_to_four(weights[i:i + 4]),
})
return pages
# ponytail: assert-based check; upgrade to pytest if suite grows
if __name__ == '__main__':
class O:
canister_count = 10
canister_weight = 5.0
canisters_per_box = 4
box_count = 3
pages = build_print_pages(O())
assert len(pages) == 4 # 13 labels → 4 mixed pages
assert all(len(p['slips']) == 4 for p in pages)
assert pages[0]['slips'] == [20, 20, 10, 5] # boxes first, then cans
assert all(isinstance(w, int) for w in pages[0]['slips'])
assert pages[1]['slips'] == [5, 5, 5, 5]
assert pages[2]['slips'] == [5, 5, 5, 5]
assert pages[3]['slips'] == [5, 5, 5, 5] # last page full
# 3 cans + 1 box → one mixed page
class Small:
canister_count = 3
canister_weight = 5.0
canisters_per_box = 4
box_count = 1
one = build_print_pages(Small())
assert len(one) == 1
assert one[0]['slips'] == [15, 5, 5, 5]
print('ok')