def get(self, request, *args, **kwargs):
     from apps.biscuit.utils.biscuit import get_biscuit
     biscuit_id = self.request.GET.get('biscuit_id', None)
     get_biscuit = get_biscuit(biscuit_id)
     recipes = get_biscuit_recipe(get_biscuit)
     serializer = BiscuitRecipeDetailSerializer(recipes, many=True)
     return Response(serializer.data)
Example #2
0
def calculate_biscuit_price():
    data = {'data': []}
    produced_biscuits = ProduceBiscuit.objects.filter(for_price='un_calculate')
    each_price_for_biscuit = Decimal(calculate_expense() /
                                     len(produced_biscuits))
    staff_salary_for_biscuit = Decimal(staff_salary() / len(produced_biscuits))
    technological_man_salary = Decimal(technological_salary() /
                                       len(produced_biscuits))
    for produced_biscuit in produced_biscuits:
        total_price = 0
        biscuit = produced_biscuit.biscuit
        quantity = produced_biscuit.quantity
        recipes = get_biscuit_recipe(biscuit)
        for recipe in recipes:
            product = recipe.product
            value = recipe.value
            product_price = get_product_price(product)
            total_price = Decimal(total_price + product_price * value *
                                  quantity + each_price_for_biscuit +
                                  staff_salary_for_biscuit +
                                  technological_man_salary) / Decimal(quantity)
        data['data'].append({
            'biscuit': biscuit.id,
            'biscuit_cost': total_price
        })
    return data
def subtract_quantity_unfit_biscuit(instance):
    obj = AddUnFitBiscuitLog.objects.get(unfit_biscuit_id=instance.id)
    unfit_biscuit, _ = get_or_create_unfit_biscuit(instance.biscuit,
                                                   instance.status)
    unfit_biscuit.subtract_quantity(obj.quantity)
    unfit_biscuit.set_total_price()
    unfit_biscuit.save()
    recipe = get_biscuit_recipe(instance.biscuit)
    for i in recipe:
        product = i.product
        value = i.value
        warehouse = get_warehouse_product(product)
        price = get_product_price(product)
        quantity_value = obj.quantity * value
        warehouse.quantity += quantity_value
        new_total_price = price * quantity_value
        warehouse.total_price += new_total_price
        new_quantity_value = instance.quantity * value
        warehouse.quantity -= new_quantity_value
        new_total_price = price * new_quantity_value
        warehouse.total_price -= new_total_price
        warehouse.save()
    unfit_biscuit.add_quantity(instance.quantity)
    unfit_biscuit.set_total_price()
    unfit_biscuit.save()
    obj.quantity = instance.quantity
    obj.save()
def unfit_biscuit_add_quantity(validated_data, unfit_biscuit):
    recipe = get_biscuit_recipe(validated_data.get('biscuit'))
    for i in recipe:
        product = i.product
        value = i.value
        warehouse = get_warehouse_product(product)
        price = get_product_price(product)
        quantity_value = validated_data.get('quantity') * value
        warehouse.quantity -= quantity_value
        new_total_price = price * quantity_value
        warehouse.total_price -= new_total_price
        warehouse.save()
    unfit_biscuit.add_quantity(validated_data.get('quantity'))
    unfit_biscuit.set_total_price()
    unfit_biscuit.save()
def add_quantity_unfit_biscuit(instance):
    unfit_biscuit, _ = get_or_create_unfit_biscuit(instance.biscuit,
                                                   instance.status)
    recipe = get_biscuit_recipe(instance.biscuit)
    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()
    unfit_biscuit.add_quantity(instance.quantity)
    unfit_biscuit.set_total_price()
    unfit_biscuit.save()
def subtract_product(validated_data):
    biscuit = validated_data.get('biscuit')
    ware_biscuit = get_warehouse_biscuit(biscuit)
    recipe = get_biscuit_recipe(biscuit)
    for i in recipe:
        product = i.product
        value = i.value
        warehouse = get_warehouse_product(product)
        price = get_product_price(product)
        quantity_value = validated_data.get('quantity') * value
        new_total_price = price * quantity_value
        warehouse.quantity -= quantity_value
        warehouse.total_price -= new_total_price
        warehouse.save()
    ware_biscuit.add_quantity(validated_data.get('quantity'))
    ware_biscuit.set_total_price()
    ware_biscuit.save()
def unfit_biscuit_subtract_quantity(instance, validated_data):
    unfit_biscuit, _ = get_or_create_unfit_biscuit(instance.biscuit, instance.status)
    unfit_biscuit.subtract_quantity(instance.quantity)
    unfit_biscuit.set_total_price()
    unfit_biscuit.save()
    old_recipe = get_biscuit_recipe(instance.biscuit)
    for i in old_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()
    unfit_biscuit, _ = get_or_create_unfit_biscuit(validated_data.get('biscuit'), validated_data.get('status'))
    unfit_biscuit_add_quantity(validated_data, unfit_biscuit)
def subtract_product_update(instance, validated_data):
    biscuit = instance.biscuit
    old_recipe = get_biscuit_recipe(biscuit)
    ware_biscuit = get_warehouse_biscuit(biscuit)
    ware_biscuit.subtract_quantity(instance.quantity)
    ware_biscuit.set_total_price()
    ware_biscuit.save()
    for i in old_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()
    subtract_product(validated_data)
    def validate(self, attrs):
        if not attrs.get('biscuit'):
            raise ValidationError({'xatolik': 'pechene topilmadi'})

        if not attrs.get('quantity'):
            raise ValidationError({'xatolik': 'miqdori topilmadi'})
        recipes = get_biscuit_recipe(attrs.get('biscuit'))
        quantity = attrs.get('quantity')
        check_price = check_biscuit_price_for_staff('technological_man')
        if not Account.objects.filter(
                user__is_chief_technological_man=True).exists():
            raise ValidationError({'xatolik': 'bosh_texnologik_bol topilmadi'})
        if len(recipes) == 0:
            raise ValidationError(
                {'error': 'Ushbu pecheneni retsepti kiritilmagan!'})
        if check_warehouse_product_quantity(recipes, quantity) != len(recipes):
            raise ValidationError('Mahsulot yetarli emas!')
        if check_price == False:
            raise ValidationError(
                'Bosh texnologikni pecheneni uchun ish haqqi kiritilmagan!')
        return attrs