104 lines
4.0 KiB
Python
104 lines
4.0 KiB
Python
import math
|
|
from django.db import models
|
|
from django.utils import timezone
|
|
|
|
|
|
class Product(models.Model):
|
|
code = models.CharField(max_length=50, unique=True)
|
|
description = models.CharField(max_length=200)
|
|
default_net_weight = models.FloatField(default=5.0)
|
|
requires_dates = models.BooleanField(default=True)
|
|
created_at = models.DateTimeField(auto_now_add=True)
|
|
|
|
class Meta:
|
|
ordering = ['code']
|
|
|
|
def __str__(self):
|
|
return f"{self.code} — {self.description}"
|
|
|
|
|
|
class Order(models.Model):
|
|
STATUS_CHOICES = [
|
|
('pending', 'Pending'),
|
|
('printing', 'Printing'),
|
|
('done', 'Done'),
|
|
('cancelled', 'Cancelled'),
|
|
]
|
|
|
|
product = models.ForeignKey(Product, on_delete=models.CASCADE, related_name='orders')
|
|
batch_number = models.CharField(max_length=50)
|
|
production_date = models.DateField(default=timezone.now, null=True, blank=True)
|
|
next_control_date = models.DateField(null=True, blank=True)
|
|
total_weight = models.FloatField(default=0)
|
|
canister_weight = models.FloatField(default=5)
|
|
canisters_per_box = models.PositiveIntegerField(default=4)
|
|
|
|
# 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)
|
|
|
|
status = models.CharField(max_length=20, choices=STATUS_CHOICES, default='pending')
|
|
created_at = models.DateTimeField(auto_now_add=True)
|
|
printed_at = models.DateTimeField(null=True, blank=True)
|
|
|
|
class Meta:
|
|
ordering = ['-created_at']
|
|
|
|
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)
|
|
self.box_count = math.ceil(self.canister_count / self.canisters_per_box)
|
|
|
|
def set_counts(self, canister_count=None, box_count=None):
|
|
"""Edit print quantities only; total_weight stays the packaging truth."""
|
|
if canister_count is not None:
|
|
self.canister_count = canister_count
|
|
if box_count is not None:
|
|
self.box_count = box_count
|
|
|
|
def save(self, *args, **kwargs):
|
|
# 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
|