Add nested inventory package tracking
This commit is contained in:
+50
-4
@@ -58,11 +58,20 @@ def overview(request):
|
||||
choices = {'mg': ('mg', 'g', 'kg'), 'g': ('mg', 'g', 'kg'), 'kg': ('mg', 'g', 'kg'),
|
||||
'ml': ('ml', 'L'), 'L': ('ml', 'L')}
|
||||
cards[key] = {'product': item.product, 'unit': unit, 'packages': [], 'total': Decimal(0),
|
||||
'units': choices.get(unit, (unit,)),
|
||||
'units': list(choices.get(unit, (unit,))), 'piece_total': 0, 'has_pieces': False,
|
||||
'package_count': 0,
|
||||
'count_unit': dimension in ('pieces', 'packs', 'portions')}
|
||||
cards[key]['packages'].append(item)
|
||||
cards[key]['package_count'] += item.quantity if item.original_amount is None else 1
|
||||
cards[key]['total'] += (item.remaining_amount if item.remaining_amount is not None
|
||||
else Decimal(item.quantity)) * factor
|
||||
if item.remaining_pieces is not None:
|
||||
cards[key]['piece_total'] += item.remaining_pieces
|
||||
cards[key]['has_pieces'] = True
|
||||
if 'pieces' not in cards[key]['units'] and (item.piece_amount is not None or item.inner_packs_per_package):
|
||||
cards[key]['units'].append('pieces')
|
||||
if item.inner_packs_per_package and 'inner_packs' not in cards[key]['units']:
|
||||
cards[key]['units'].append('inner_packs')
|
||||
recent = InventoryConsumption.objects.filter(user=request.user, reverses__isnull=True).select_related('product').prefetch_related('lines__package').order_by('-created_at')[:8]
|
||||
return render(request, 'food/inventory_overview.html', {
|
||||
'items': items, 'cards': list(cards.values()), 'recent_consumption': recent,
|
||||
@@ -98,7 +107,8 @@ def add(request):
|
||||
item = form.save(commit=False)
|
||||
item.user, item.product, item.location, item.category = request.user, product, location, category
|
||||
if item.original_amount is not None:
|
||||
item.remaining_amount = item.original_amount
|
||||
if item.remaining_amount is None:
|
||||
item.remaining_amount = item.original_amount
|
||||
if item.quantity > 1:
|
||||
count = item.quantity
|
||||
item.quantity = 1
|
||||
@@ -222,7 +232,12 @@ def change_quantity(request, pk):
|
||||
if item.original_amount is not None:
|
||||
KitchenInventoryItem.objects.create(user=item.user, product=item.product, location=item.location,
|
||||
category=item.category, original_amount=item.original_amount, remaining_amount=item.original_amount,
|
||||
inventory_unit=item.inventory_unit, best_before=item.best_before)
|
||||
inventory_unit=item.inventory_unit, pieces_per_package=item.pieces_per_package,
|
||||
remaining_pieces=item.pieces_per_package, piece_amount=item.piece_amount,
|
||||
inner_packs_per_package=item.inner_packs_per_package,
|
||||
pieces_per_inner_pack=item.pieces_per_inner_pack, inner_pack_amount=item.inner_pack_amount,
|
||||
inner_pack_label=item.inner_pack_label, piece_label=item.piece_label,
|
||||
best_before=item.best_before)
|
||||
else:
|
||||
item.quantity += 1
|
||||
item.save(update_fields=('quantity', 'updated_at'))
|
||||
@@ -237,7 +252,12 @@ def add_existing(request, pk):
|
||||
if item.original_amount is not None:
|
||||
KitchenInventoryItem.objects.create(user=item.user, product=item.product, location=item.location,
|
||||
category=item.category, original_amount=item.original_amount, remaining_amount=item.original_amount,
|
||||
inventory_unit=item.inventory_unit, best_before=item.best_before)
|
||||
inventory_unit=item.inventory_unit, pieces_per_package=item.pieces_per_package,
|
||||
remaining_pieces=item.pieces_per_package, piece_amount=item.piece_amount,
|
||||
inner_packs_per_package=item.inner_packs_per_package,
|
||||
pieces_per_inner_pack=item.pieces_per_inner_pack, inner_pack_amount=item.inner_pack_amount,
|
||||
inner_pack_label=item.inner_pack_label, piece_label=item.piece_label,
|
||||
best_before=item.best_before)
|
||||
else:
|
||||
item.quantity += 1
|
||||
item.save(update_fields=('quantity', 'updated_at'))
|
||||
@@ -254,6 +274,32 @@ def preview_consumption(request, pk):
|
||||
request.POST.get('unit'), request.POST.get('package_id') or None)
|
||||
except StockError as exc:
|
||||
return render(request, 'food/partials/inventory_preview.html', {'error': str(exc)})
|
||||
base_unit = product.base_unit
|
||||
base_dimension = FACTORS[base_unit][0]
|
||||
equivalent = Decimal(0)
|
||||
compatible_equivalent = True
|
||||
estimated_piece_weight = False
|
||||
for deduction in plan['deductions']:
|
||||
unit = deduction['unit']
|
||||
if FACTORS[unit][0] != base_dimension:
|
||||
compatible_equivalent = False
|
||||
break
|
||||
equivalent += deduction['amount'] * FACTORS[unit][1]
|
||||
if deduction['package'].inner_packs_per_package and plan['unit'] in ('pieces', 'inner_packs'):
|
||||
estimated_piece_weight = True
|
||||
nutrition = []
|
||||
if compatible_equivalent and equivalent:
|
||||
for label, field in (('Energy', 'energy_kcal_100'), ('Carbohydrates', 'carbohydrates_100'),
|
||||
('Protein', 'protein_100'), ('Fat', 'fat_100'),
|
||||
('Fibre', 'fibre_100'), ('Sugars', 'sugars_100'), ('Salt', 'salt_100')):
|
||||
value = getattr(product, field)
|
||||
if value is not None:
|
||||
nutrition.append({'label': label, 'value': (value * equivalent / 100).quantize(Decimal('.1')),
|
||||
'unit': 'kcal' if label == 'Energy' else 'g'})
|
||||
plan['equivalent_amount'] = equivalent.quantize(Decimal('.001')) if compatible_equivalent else None
|
||||
plan['equivalent_unit'] = base_unit
|
||||
plan['estimated_piece_weight'] = estimated_piece_weight
|
||||
plan['nutrition'] = nutrition
|
||||
return render(request, 'food/partials/inventory_preview.html',
|
||||
{'plan': plan, 'product': product, 'package_id': request.POST.get('package_id', '')})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user