Ejemplo n.º 1
0
def business_sale(request, business_url=None):
    if request.method == "POST":
        if not request.user.is_anonymous:
            business = Business.objects.filter(url=business_url).first()
            if not business:
                return Response(status=status.HTTP_400_BAD_REQUEST)
            business_owner = business.user
            user = User.objects.get(id=request.user.id)
            client_user_query = ClientUser.objects.filter(user=user)
            for client_user in client_user_query:
                if client_user.client.id_user == business_owner:
                    client = client_user.client
            sale = Sale()
            sale.id_user = business_owner
            sale.id_client = client
            sale.date = request.data.get('date')
            sale.discount = request.data.get('discount')
            sale.subtotal = request.data.get('subtotal')
            sale.pay_type = request.data.get('pay_type')
            sale.total = request.data.get('total')
            sale.finished = request.data.get('finished')
            sale.save()
            products = request.data.get('products')
            for product_request in products:
                product = Product.objects.get(
                    id=product_request.get('id_product'))
                SaleProduct.objects.create(
                    id_sale=sale,
                    product=product,
                    quantity=product_request.get('quantity'))
            return Response(status=status.HTTP_200_OK)
        else:
            return Response(status=status.HTTP_401_UNAUTHORIZED)
    else:
        return Response(status=status.HTTP_404_NOT_FOUND)
Ejemplo n.º 2
0
 def create(self, validated_data):
     sale = Sale()
     sale.id_user = validated_data.get('id_user')
     sale.id_client = validated_data.get('id_client')
     sale.date = validated_data.get('date')
     sale.discount = validated_data.get('discount')
     sale.subtotal = validated_data.get('subtotal')
     sale.pay_type = validated_data.get('pay_type')
     sale.total = validated_data.get('total')
     sale.finished = validated_data.get('finished')
     sale.products = validated_data.get('products')
     sale.save()
     # Save every product in table SaleProduct
     for product in sale.products:
         sale_product = SaleProduct()
         sale_product.id_sale = sale
         sale_product.product = Product.objects.get(
             id=product['id_product'])
         sale_product.quantity = product['quantity']
         sale_product.save()
     return sale