diff --git a/slips/admin.py b/slips/admin.py index 04ca47a..17f7176 100644 --- a/slips/admin.py +++ b/slips/admin.py @@ -4,7 +4,8 @@ from .models import Product, Order @admin.register(Product) class ProductAdmin(admin.ModelAdmin): - list_display = ['code', 'description', 'default_net_weight', 'created_at'] + list_display = ['code', 'description', 'default_net_weight', 'requires_dates', 'created_at'] + list_editable = ['requires_dates'] search_fields = ['code', 'description'] diff --git a/slips/api.py b/slips/api.py index c092575..e18456a 100644 --- a/slips/api.py +++ b/slips/api.py @@ -41,6 +41,7 @@ def restore_products(request, payload: List[ProductIn]): defaults={ 'description': item.description, 'default_net_weight': item.default_net_weight, + 'requires_dates': item.requires_dates, }, ) if was_created: @@ -81,14 +82,22 @@ def list_orders(request, status: Optional[str] = Query(None)): return qs -@api.post('/orders', response={201: OrderOut}, tags=['Orders']) +@api.post('/orders', response={201: OrderOut, 400: dict}, tags=['Orders']) def create_order(request, payload: OrderIn): product = get_object_or_404(Product, id=payload.product_id) + if product.requires_dates: + if not payload.production_date or not payload.next_control_date: + return 400, {'detail': 'production_date and next_control_date are required'} + production_date = payload.production_date + next_control_date = payload.next_control_date + else: + production_date = None + next_control_date = None order = Order( product=product, batch_number=payload.batch_number, - production_date=payload.production_date, - next_control_date=payload.next_control_date, + production_date=production_date, + next_control_date=next_control_date, total_weight=payload.total_weight, canister_weight=payload.canister_weight, canisters_per_box=payload.canisters_per_box, diff --git a/slips/migrations/0003_product_requires_dates_order_dates_nullable.py b/slips/migrations/0003_product_requires_dates_order_dates_nullable.py new file mode 100644 index 0000000..8760383 --- /dev/null +++ b/slips/migrations/0003_product_requires_dates_order_dates_nullable.py @@ -0,0 +1,39 @@ +# Generated by Django 5.2.17 on 2026-10-14 08:25 + +import django.utils.timezone +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('slips', '0002_remove_order_net_weight_remove_order_pages_needed_and_more'), + ] + + operations = [ + migrations.AddField( + model_name='product', + name='requires_dates', + field=models.BooleanField(default=True), + ), + migrations.AlterField( + model_name='order', + name='box_count', + field=models.PositiveIntegerField(default=0), + ), + migrations.AlterField( + model_name='order', + name='canister_count', + field=models.PositiveIntegerField(default=0), + ), + migrations.AlterField( + model_name='order', + name='next_control_date', + field=models.DateField(blank=True, null=True), + ), + migrations.AlterField( + model_name='order', + name='production_date', + field=models.DateField(blank=True, default=django.utils.timezone.now, null=True), + ), + ] diff --git a/slips/models.py b/slips/models.py index 68e85c5..876cd13 100644 --- a/slips/models.py +++ b/slips/models.py @@ -7,6 +7,7 @@ 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: @@ -26,8 +27,8 @@ class Order(models.Model): 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) - next_control_date = models.DateField() + 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) diff --git a/slips/schemas.py b/slips/schemas.py index 238d0a1..f74ab57 100644 --- a/slips/schemas.py +++ b/slips/schemas.py @@ -8,19 +8,21 @@ class ProductOut(Schema): code: str description: str default_net_weight: float + requires_dates: bool class ProductIn(Schema): code: str description: str default_net_weight: float = 5.0 + requires_dates: bool = True class OrderIn(Schema): product_id: int batch_number: str - production_date: date - next_control_date: date + production_date: Optional[date] = None + next_control_date: Optional[date] = None total_weight: float canister_weight: float canisters_per_box: int @@ -32,8 +34,8 @@ class OrderOut(Schema): product_code: str product_description: str batch_number: str - production_date: date - next_control_date: date + production_date: Optional[date] = None + next_control_date: Optional[date] = None total_weight: float canister_weight: float canisters_per_box: int diff --git a/slips/static/slips/css/style.css b/slips/static/slips/css/style.css index aa40b6a..3a0aca5 100644 --- a/slips/static/slips/css/style.css +++ b/slips/static/slips/css/style.css @@ -351,6 +351,19 @@ form { min-width: 0; } +.checkbox-label { + display: flex; + align-items: center; + gap: 0.5rem; + cursor: pointer; +} + +.checkbox-label input[type="checkbox"] { + width: auto; + min-width: 0; + padding: 0; +} + label { font-size: 0.875rem; font-weight: 500; diff --git a/slips/static/slips/js/entry.js b/slips/static/slips/js/entry.js index 2a15b3c..c1131b0 100644 --- a/slips/static/slips/js/entry.js +++ b/slips/static/slips/js/entry.js @@ -1,5 +1,6 @@ document.addEventListener('DOMContentLoaded', () => { const productSelect = document.getElementById('product'); + const dateFields = document.getElementById('date-fields'); const dateInput = document.getElementById('prod-date'); const nextControlInput = document.getElementById('next-control'); const totalWeightInput = document.getElementById('total-weight'); @@ -26,6 +27,24 @@ document.addEventListener('DOMContentLoaded', () => { const day = String(d.getDate()).padStart(2, '0'); return `${d.getFullYear()}-${m}-${day}`; } + + function productRequiresDates() { + const opt = productSelect.options[productSelect.selectedIndex]; + if (!opt || !opt.value) return true; + return opt.dataset.requiresDates !== 'false'; + } + + function syncDateFields() { + const needed = productRequiresDates(); + dateFields.style.display = needed ? '' : 'none'; + dateInput.required = needed; + nextControlInput.required = needed; + if (needed && !dateInput.value) { + dateInput.value = localDmy(new Date()); + updateNextControl(); + } + } + dateInput.value = localDmy(new Date()); // Auto calculate +6 months @@ -61,31 +80,40 @@ document.addEventListener('DOMContentLoaded', () => { canistersPerBoxInput.addEventListener('input', updatePreview); updatePreview(); - // Auto weight from product selection + // Auto weight + date visibility from product selection productSelect.addEventListener('change', (e) => { const opt = e.target.options[e.target.selectedIndex]; if (opt && opt.dataset.weight) { canisterWeightInput.value = opt.dataset.weight; updatePreview(); } + syncDateFields(); }); + syncDateFields(); // Submit new order form.addEventListener('submit', async (e) => { e.preventDefault(); - - const prodDate = parseDmy(dateInput.value); - const nextDate = parseDmy(nextControlInput.value); - if (!prodDate || !nextDate) { - showToast(currentLang === 'fa' ? 'تاریخ را به صورت روز/ماه/سال وارد کنید' : 'Use date format dd/mm/yyyy', true); - return; + + const needsDates = productRequiresDates(); + let production_date = null; + let next_control_date = null; + if (needsDates) { + const prodDate = parseDmy(dateInput.value); + const nextDate = parseDmy(nextControlInput.value); + if (!prodDate || !nextDate) { + showToast(currentLang === 'fa' ? 'تاریخ را به صورت روز/ماه/سال وارد کنید' : 'Use date format dd/mm/yyyy', true); + return; + } + production_date = toYmd(prodDate); + next_control_date = toYmd(nextDate); } const payload = { product_id: parseInt(productSelect.value), batch_number: document.getElementById('batch').value, - production_date: toYmd(prodDate), - next_control_date: toYmd(nextDate), + production_date, + next_control_date, total_weight: parseFloat(totalWeightInput.value), canister_weight: parseFloat(canisterWeightInput.value), canisters_per_box: parseInt(canistersPerBoxInput.value) @@ -103,6 +131,7 @@ document.addEventListener('DOMContentLoaded', () => { form.reset(); dateInput.value = localDmy(new Date()); updateNextControl(); + syncDateFields(); updatePreview(); setTimeout(() => window.location.reload(), 1000); } else { diff --git a/slips/static/slips/js/i18n.js b/slips/static/slips/js/i18n.js index 9584cf3..1880f21 100644 --- a/slips/static/slips/js/i18n.js +++ b/slips/static/slips/js/i18n.js @@ -16,6 +16,8 @@ const translations = { 'product_code': 'Product Code', 'description': 'Description', 'default_weight': 'Default Weight', + 'requires_dates': 'Requires Date / Next Control', + 'requires_dates_short': 'Dates', 'save': 'Save', 'cancel': 'Cancel', 'batch_number': 'Batch #', @@ -65,6 +67,8 @@ const translations = { 'product_code': 'کد محصول', 'description': 'شرح', 'default_weight': 'وزن پیش‌فرض', + 'requires_dates': 'نیاز به تاریخ / کنترل بعدی', + 'requires_dates_short': 'تاریخ', 'save': 'ذخیره', 'cancel': 'لغو', 'batch_number': 'شماره بچ', diff --git a/slips/static/slips/js/products.js b/slips/static/slips/js/products.js index bc85f4b..e1e3d96 100644 --- a/slips/static/slips/js/products.js +++ b/slips/static/slips/js/products.js @@ -7,6 +7,7 @@ document.addEventListener('DOMContentLoaded', () => { code: document.getElementById('prod-code').value.trim(), description: document.getElementById('prod-desc').value.trim(), default_net_weight: parseFloat(document.getElementById('prod-weight').value), + requires_dates: document.getElementById('prod-requires-dates').checked, }; if (!payload.code || !payload.description) { showToast(currentLang === 'fa' ? 'کد و شرح لازم است' : 'Code and Description are required', true); @@ -51,6 +52,7 @@ function editProduct(btn) { document.getElementById('prod-code').value = btn.dataset.code; document.getElementById('prod-desc').value = btn.dataset.desc; document.getElementById('prod-weight').value = btn.dataset.weight; + document.getElementById('prod-requires-dates').checked = btn.dataset.requiresDates !== 'false'; setFormMode(true); document.getElementById('prod-code').focus(); } @@ -59,6 +61,7 @@ function cancelEdit() { document.getElementById('edit-id').value = ''; document.getElementById('product-form').reset(); document.getElementById('prod-weight').value = 5; + document.getElementById('prod-requires-dates').checked = true; setFormMode(false); } @@ -94,6 +97,7 @@ async function backupProducts() { code: p.code, description: p.description, default_net_weight: p.default_net_weight, + requires_dates: p.requires_dates !== false, })); const blob = new Blob([JSON.stringify(payload, null, 2) + '\n'], { type: 'application/json' }); const a = document.createElement('a'); @@ -122,7 +126,8 @@ function parseProductsBackup(raw) { const description = String(item.description || '').trim(); const weight = Number(item.default_net_weight); if (!code || !description || !Number.isFinite(weight)) throw new Error('invalid product: ' + code); - return { code, description, default_net_weight: weight }; + const requires_dates = item.requires_dates !== false; + return { code, description, default_net_weight: weight, requires_dates }; }); } diff --git a/slips/templates/slips/entry.html b/slips/templates/slips/entry.html index 0db9ce6..a473f9b 100644 --- a/slips/templates/slips/entry.html +++ b/slips/templates/slips/entry.html @@ -18,7 +18,7 @@ -
+
- +
- +
diff --git a/slips/templates/slips/print.html b/slips/templates/slips/print.html index 6e318eb..dc8b1e2 100644 --- a/slips/templates/slips/print.html +++ b/slips/templates/slips/print.html @@ -30,6 +30,7 @@ Batch: {{ o.batch_number }}
+ {% if o.product.requires_dates %}
Date: {{ o.production_date|date:"d/m/Y" }} @@ -38,6 +39,7 @@ Next: {{ o.next_control_date|date:"d/m/Y" }}
+ {% endif %} +
+ +
@@ -51,6 +57,7 @@ Code Description Weight + Dates @@ -60,12 +67,14 @@ {{ p.code }} {{ p.description }} {{ p.default_net_weight }} + {{ p.requires_dates|yesno:"✓,—" }}