Merge pull request 'Mix box and canister labels on shared sheets' (#31) from cursor/mixed-sheet-box-first into main

This commit was merged in pull request #31.
This commit is contained in:
2026-10-11 09:25:04 +00:00
5 changed files with 30 additions and 26 deletions

View File

@@ -64,6 +64,8 @@ class Order(models.Model):
def save(self, *args, **kwargs):
if not self.canister_count and self.total_weight and self.canister_weight:
self.apply_weight()
self.canister_pages = math.ceil(self.canister_count / 4) if self.canister_count else 0
self.box_pages = math.ceil(self.box_count / 4) if self.box_count else 0
# Mixed sheets: canister + box labels share pages; total in canister_pages
n = self.canister_count + self.box_count
self.canister_pages = math.ceil(n / 4) if n else 0
self.box_pages = 0
super().save(*args, **kwargs)

View File

@@ -18,16 +18,9 @@ def _pad_to_four(weights):
def build_print_pages(order):
pages = []
"""Mix box then canister labels on shared sheets (pad only last page)."""
cw = _display_weight(order.canister_weight)
# Canister labels (uniform weight)
canister_weights = [cw] * order.canister_count
for i in range(0, len(canister_weights), 4):
pages.append({
'type': 'Canister',
'slips': _pad_to_four(canister_weights[i:i + 4]),
})
# Box labels — last box may be a partial fill
cans_left = order.canister_count
@@ -37,12 +30,12 @@ def build_print_pages(order):
box_weights.append(_display_weight(order.canister_weight * cans_in_box))
cans_left -= cans_in_box
for i in range(0, len(box_weights), 4):
weights = box_weights + canister_weights
pages = []
for i in range(0, len(weights), 4):
pages.append({
'type': 'Box',
'slips': _pad_to_four(box_weights[i:i + 4]),
'slips': _pad_to_four(weights[i:i + 4]),
})
return pages
@@ -55,11 +48,21 @@ if __name__ == '__main__':
box_count = 3
pages = build_print_pages(O())
assert len(pages) == 4 # 3 canister pages + 1 box page
assert len(pages) == 4 # 13 labels → 4 mixed pages
assert all(len(p['slips']) == 4 for p in pages)
assert pages[0]['slips'] == [5, 5, 5, 5]
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[2]['slips'] == [5, 5, 5, 5] # last canister page padded
assert pages[3]['type'] == 'Box'
assert pages[3]['slips'] == [20, 20, 10, 20] # partial last box; pad from first
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')

View File

@@ -46,14 +46,12 @@ document.addEventListener('DOMContentLoaded', () => {
const cpb = parseInt(canistersPerBoxInput.value) || 1;
const cc = Math.ceil(tw / cw);
const cp = Math.ceil(cc / 4);
const bc = Math.ceil(cc / cpb);
const bp = Math.ceil(bc / 4);
const pages = Math.ceil((cc + bc) / 4) || 0;
const t = translations[currentLang] || translations.en;
previewDisplay.textContent =
`${t.canisters_unit}: ${cc} (${cp} ${t.preview_pages}) | ${t.boxes_unit}: ${bc} (${bp} ${t.preview_pages})`;
`${t.canisters_unit}: ${cc} | ${t.boxes_unit}: ${bc} | ${pages} ${t.preview_pages}`;
}
window.updateLabelsPreview = updatePreview;

View File

@@ -59,7 +59,7 @@
<div class="form-group">
<label data-i18n="labels_preview">Labels Generated</label>
<div class="computed-value" style="font-size: 0.9em" id="preview-display">
Canisters: - ( - pages ) | Boxes: - ( - pages )
Canisters: - | Boxes: - | - pages
</div>
</div>

View File

@@ -176,8 +176,9 @@
<div class="preview-controls">
<p style="margin-bottom: 10px; font-family: sans-serif;">
Order #{{ order.id }} — {{ order.product.code }} <br>
{{ order.canister_count }} Canisters ({{ order.canister_pages }} Pages) |
{{ order.box_count }} Boxes ({{ order.box_pages }} Pages)
{{ order.canister_count }} Canisters |
{{ order.box_count }} Boxes |
{{ order.canister_pages|add:order.box_pages }} Pages
</p>
<label class="page-range">
Pages