Add custom labels printed before box and canister slips

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-10-21 14:01:48 +03:30
parent cb21289876
commit 4d0dad33e6
10 changed files with 322 additions and 18 deletions

View File

@@ -36,6 +36,8 @@ class Order(models.Model):
# Label counts (set from weight on create; editable afterwards)
canister_count = models.PositiveIntegerField(default=0)
box_count = models.PositiveIntegerField(default=0)
# User-defined slips printed before box and canister labels: [{"weight", "count"}]
custom_labels = models.JSONField(default=list, blank=True)
canister_pages = models.PositiveIntegerField(editable=False, default=0)
box_pages = models.PositiveIntegerField(editable=False, default=0)
@@ -49,6 +51,10 @@ class Order(models.Model):
def __str__(self):
return f"Order #{self.id} — {self.product.code} — {self.batch_number}"
@property
def custom_count(self):
return sum(int(line['count']) for line in (self.custom_labels or []))
def apply_weight(self):
"""Derive canister/box counts from total_weight (create path)."""
self.canister_count = math.ceil(self.total_weight / self.canister_weight)
@@ -62,8 +68,36 @@ class Order(models.Model):
self.box_count = box_count
def save(self, *args, **kwargs):
# Mixed sheets: canister + box labels share pages; total in canister_pages
n = self.canister_count + self.box_count
# Mixed sheets: custom + box + canister labels share pages; total in canister_pages
n = self.canister_count + self.box_count + self.custom_count
self.canister_pages = math.ceil(n / 4) if n else 0
self.box_pages = 0
super().save(*args, **kwargs)
def clean_custom_labels(lines):
"""Normalize [{weight, count}] lines. Empty is valid. Raises ValueError."""
if not lines:
return []
cleaned = []
for line in lines:
if isinstance(line, dict):
weight, count = line.get('weight'), line.get('count')
else:
weight, count = line.weight, line.count
try:
weight = float(weight)
except (TypeError, ValueError):
raise ValueError('custom label weight and count must be numbers')
if isinstance(count, float) and not count.is_integer():
raise ValueError('custom label count must be a whole number')
try:
count = int(count)
except (TypeError, ValueError):
raise ValueError('custom label weight and count must be numbers')
if not math.isfinite(weight) or weight <= 0 or count < 1:
raise ValueError('custom label weight must be > 0 and count >= 1')
if weight.is_integer():
weight = int(weight)
cleaned.append({'weight': weight, 'count': count})
return cleaned