from django.db.models import Sum, Q from LiveStock.models import LiveStockAllocations def jahad_warehousing(product): allocations = LiveStockAllocations.objects.filter(jahad__isnull=False,product=product, trash=False) # allocations = LiveStockAllocations.objects.filter(jahad=product.jahad, trash=False) input_allocations = allocations.filter(union__isnull=True, cooperative__isnull=True) output_allocations = allocations.filter(Q(union__isnull=False) | Q(cooperative__isnull=False)) input_weight = input_allocations.aggregate(total=Sum('weight'))[ 'total'] or 0 output_weight = output_allocations.aggregate(total=Sum('weight'))[ 'total'] or 0 product.total_weight = input_weight product.total_receipt_weight = input_weight product.total_allocated_weight = output_weight product.save() def union_warehousing(product): allocations = LiveStockAllocations.objects.filter(union=product.union,product__parent_product__name=product.parent_product.name, trash=False) input_allocations = allocations.filter(jahad__isnull=False) output_allocations = allocations.filter(cooperative__isnull=False) input_weight = input_allocations.aggregate(total=Sum('weight'))[ 'total'] or 0 output_weight = output_allocations.aggregate(total=Sum('weight'))[ 'total'] or 0 product.total_weight = input_weight product.total_receipt_weight = input_weight product.total_allocated_weight = output_weight product.save() def cooperative_warehousing(product): input_allocations = LiveStockAllocations.objects.filter(cooperative=product.cooperative,product__parent_product__name=product.parent_product.name, trash=False).values_list('id',flat=True) total_receipt_weight = input_allocations.filter(charge=False).aggregate(total=Sum('weight'))[ 'total'] or 0 real_input_weight = input_allocations.filter(charge=True).aggregate(total=Sum('weight'))[ 'total'] or 0 product.total_receipt_weight = total_receipt_weight product.total_weight = real_input_weight product.save()