def get(self, request, *args, **kwargs):
     product_id = self.request.GET.get('product_id', None)
     get_product = get_manufactured_product(product_id)
     recipes = get_product_recipe(get_product)
     serializer = ManufacturedProductRecipeDetailSerializer(recipes,
                                                            many=True)
     return Response(serializer.data)
 def validate(self, attrs):
     from apps.product.utils.product import get_product_recipe
     if not attrs.get('product'):
         raise ValidationError({'error': 'Product not found'})
     if not attrs.get('quantity'):
         raise ValidationError({'error': 'quantity not found'})
     recipes = get_product_recipe(attrs.get('product'))
     if check_warehouse_product_quantity(
             recipes, attrs.get('quantity')) != len(recipes):
         raise ValidationError({'xatolik': 'omborda yetarli mahsulot yoq'})
     if len(recipes) == 0:
         raise ValidationError({'xatolik': 'Mahsulot retsepti topilmadi'})
     return attrs
 def update(self, instance, validated_data):
     from apps.product.utils.product import get_product_recipe
     product = validated_data.get('product')
     quantity = validated_data.get('quantity')
     recipes = get_product_recipe(product)
     if check_warehouse_product_quantity(recipes, quantity) == len(recipes):
         instance.product = validated_data.get('product', instance.product)
         instance.quantity = validated_data.get('quantity',
                                                instance.quantity)
         instance.save()
         return instance
     else:
         raise ValidationError('omborda yetarli mahsulot yoq')
def manufactured_product_add_update(instance, validated_data):
    product = instance.product
    ware_product = get_warehouse_manufactured_product(product)
    recipe = get_product_recipe(product)
    ware_product.subtract_quantity(instance.quantity)
    ware_product.set_total_price()
    ware_product.set_average_price()
    ware_product.save()
    for i in recipe:
        product = i.product
        value = i.value
        warehouse = get_warehouse_product(product)
        price = get_product_price(product)
        quantity_value = instance.quantity * value
        warehouse.quantity += quantity_value
        new_total_price = price * quantity_value
        warehouse.total_price += new_total_price
        warehouse.save()
    manufactured_product_add(validated_data)
def manufactured_product_add(validated_data):
    product = validated_data.get('product')
    ware_product = get_warehouse_manufactured_product(product)
    recipe = get_product_recipe(product)
    for i in recipe:
        product = i.product
        value = i.value
        warehouse = get_warehouse_product(product)
        price = get_product_price(product)
        quantity_value = Decimal(
            validated_data.get('quantity')) * Decimal(value)
        new_total_price = Decimal(price) * quantity_value
        warehouse.quantity -= Decimal(quantity_value)
        warehouse.total_price -= Decimal(new_total_price)
        warehouse.save()
    ware_product.add_quantity(Decimal(validated_data.get('quantity')))
    ware_product.set_total_price()
    ware_product.set_average_price()
    ware_product.save()