Example #1
0
def get_cart_costs(request, cart, total=False):
    """Returns price and tax of the given cart.
    """
    if cart is None:
        return {"price" : 0, "tax" : 0}

    cache_key = "cart-costs-%s-%s" % (total, cart.id)
    cart_costs = cache.get(cache_key)

    if cart_costs is None:
        cart_price = 0
        cart_tax = 0
        for item in cart.items():
            cart_price += item.get_price()
            cart_tax += item.get_tax()

        if total:
            # Shipping
            shipping_method = shipping_utils.get_selected_shipping_method(request)
            shipping_costs = shipping_utils.get_shipping_costs(request, shipping_method)
            cart_price += shipping_costs["price"]
            cart_tax += shipping_costs["tax"]

            # Payment
            payment_method = payment_utils.get_selected_payment_method(request)
            payment_costs = payment_utils.get_payment_costs(request, payment_method)
            cart_price += payment_costs["price"]
            cart_tax += payment_costs["tax"]

        cart_costs = {"price" : cart_price, "tax" : cart_tax}
        cache.set(cache_key, cart_costs)

    return cart_costs
Example #2
0
def cart_inline(request, template_name="lfs/cart/cart_inline.html"):
    """The actual content of the cart. This is factored out to be reused within
    'normal' and ajax requests.
    """
    cart = cart_utils.get_cart(request)
    shopping_url = lfs.cart.utils.get_go_on_shopping_url(request)
    if cart is None:
        return render_to_string(template_name, RequestContext(request, {"shopping_url": shopping_url}))

    shop = core_utils.get_default_shop()
    countries = shop.countries.all()
    selected_country = shipping_utils.get_selected_shipping_country(request)

    # Get default shipping method, so that we have a one in any case.
    selected_shipping_method = shipping_utils.get_selected_shipping_method(request)
    selected_payment_method = payment_utils.get_selected_payment_method(request)

    shipping_costs = shipping_utils.get_shipping_costs(request, selected_shipping_method)

    payment_costs = payment_utils.get_payment_costs(request, selected_payment_method)

    cart_costs = cart_utils.get_cart_costs(request, cart)
    cart_price = cart_costs["price"] + shipping_costs["price"] + payment_costs["price"]

    cart_tax = cart_costs["tax"] + shipping_costs["tax"] + payment_costs["tax"]

    max_delivery_time = cart_utils.get_cart_max_delivery_time(request, cart)

    # Calc delivery date for cart (which is the maximum of all cart items)
    max_delivery_date = cart_utils.get_cart_max_delivery_time(request, cart)

    return render_to_string(
        template_name,
        RequestContext(
            request,
            {
                "cart": cart,
                "max_delivery_date": max_delivery_date,
                "cart_price": cart_price,
                "cart_tax": cart_tax,
                "shipping_methods": shipping_utils.get_valid_shipping_methods(request),
                "selected_shipping_method": selected_shipping_method,
                "shipping_price": shipping_costs["price"],
                "payment_methods": payment_utils.get_valid_payment_methods(request),
                "selected_payment_method": selected_payment_method,
                "payment_price": payment_costs["price"],
                "countries": countries,
                "selected_country": selected_country,
                "max_delivery_time": max_delivery_time,
                "shopping_url": shopping_url,
            },
        ),
    )
Example #3
0
File: views.py Project: potar/lfs
def cart_inline(request, template_name="lfs/cart/cart_inline.html"):
    """The actual content of the cart. This is factored out to be reused within
    'normal' and ajax requests.
    """
    cart = cart_utils.get_cart(request)
    shopping_url = lfs.cart.utils.get_go_on_shopping_url(request)
    if cart is None:
        return render_to_string(template_name, RequestContext(request, {
            "shopping_url" : shopping_url,
        }))

    shop = core_utils.get_default_shop()
    countries = shop.countries.all()
    selected_country = shipping_utils.get_selected_shipping_country(request)

    # Get default shipping method, so that we have a one in any case.
    selected_shipping_method = shipping_utils.get_selected_shipping_method(request)
    selected_payment_method = payment_utils.get_selected_payment_method(request)

    shipping_costs = shipping_utils.get_shipping_costs(request,
        selected_shipping_method)

    payment_costs = payment_utils.get_payment_costs(request,
        selected_payment_method)

    cart_costs = cart_utils.get_cart_costs(request, cart)
    cart_price = \
        cart_costs["price"] + shipping_costs["price"] + payment_costs["price"]

    cart_tax = \
        cart_costs["tax"] + shipping_costs["tax"] + payment_costs["tax"]

    max_delivery_time = cart_utils.get_cart_max_delivery_time(request, cart)

    # Calc delivery date for cart (which is the maximum of all cart items)
    max_delivery_date = cart_utils.get_cart_max_delivery_time(request, cart)

    return render_to_string(template_name, RequestContext(request, {
        "cart" : cart,
        "max_delivery_date" : max_delivery_date,
        "cart_price" : cart_price,
        "cart_tax" : cart_tax,
        "shipping_methods" : shipping_utils.get_valid_shipping_methods(request),
        "selected_shipping_method" : selected_shipping_method,
        "shipping_price" : shipping_costs["price"],
        "payment_methods" : payment_utils.get_valid_payment_methods(request),
        "selected_payment_method" : selected_payment_method,
        "payment_price" : payment_costs["price"],
        "countries" : countries,
        "selected_country" : selected_country,
        "max_delivery_time" : max_delivery_time,
        "shopping_url" : shopping_url,
    }))
Example #4
0
def get_cart_costs(request, cart, total=False):
    """Returns price and tax of the given cart.
    """
    if cart is None:
        return {"price": 0, "tax": 0}

    cache_key = "cart-costs-%s-%s" % (total, cart.id)
    cart_costs = cache.get(cache_key)

    if cart_costs is None:
        cart_price = 0
        cart_tax = 0
        for item in cart.items():
            cart_price += item.get_price()
            cart_tax += item.get_tax()

        if total:
            # Shipping
            shipping_method = shipping_utils.get_selected_shipping_method(
                request)
            shipping_costs = shipping_utils.get_shipping_costs(
                request, shipping_method)
            cart_price += shipping_costs["price"]
            cart_tax += shipping_costs["tax"]

            # Payment
            payment_method = payment_utils.get_selected_payment_method(request)
            payment_costs = payment_utils.get_payment_costs(
                request, payment_method)
            cart_price += payment_costs["price"]
            cart_tax += payment_costs["tax"]

        cart_costs = {"price": cart_price, "tax": cart_tax}
        cache.set(cache_key, cart_costs)

    return cart_costs
Example #5
0
def add_order(request):
    """Adds an order based on current cart for the current customer.

    It assumes that the customer is prepared with all needed information. This
    is within the responsibility of the checkout form.
    """
    customer = customer_utils.get_customer(request)
    order = None

    invoice_address = customer.selected_invoice_address
    if request.POST.get("no_shipping"):
        shipping_address = customer.selected_invoice_address
    else:
        shipping_address = customer.selected_shipping_address

    cart = cart_utils.get_cart(request)
    if cart is None:
        return order

    shipping_method = shipping_utils.get_selected_shipping_method(request)
    shipping_costs = shipping_utils.get_shipping_costs(request, shipping_method)

    payment_method = payment_utils.get_selected_payment_method(request)
    payment_costs = payment_utils.get_payment_costs(request, payment_method)

    # Set email dependend on login state. An anonymous customer doesn't  have a
    # django user account, so we set the name of the invoice address to the
    # customer name.

    # Note: After this has been processed the order's customer email has an
    # email in any case. That means you can use it to send emails to the
    # customer.
    if request.user.is_authenticated():
        user = request.user
        customer_email = user.email
    else:
        user = None
        customer_email = customer.selected_invoice_address.email

    # Calculate the totals
    price = cart.get_price_gross(request) + shipping_costs["price_gross"] + payment_costs["price"]
    tax = cart.get_tax(request) + shipping_costs["tax"] + payment_costs["tax"]

    # Discounts
    discounts = lfs.discounts.utils.get_valid_discounts(request)
    for discount in discounts:
        price = price - discount["price_gross"]
        tax = tax - discount["tax"]

    # Add voucher if one exists
    is_voucher_effective = False
    try:
        voucher_number = lfs.voucher.utils.get_current_voucher_number(request)
        voucher = Voucher.objects.get(number=voucher_number)
    except Voucher.DoesNotExist:
        voucher = None
    else:
        is_voucher_effective, voucher_message = voucher.is_effective(request, cart)
        if is_voucher_effective:
            voucher_number = voucher.number
            voucher_price = voucher.get_price_gross(request, cart)
            voucher_tax = voucher.get_tax(request, cart)

            price -= voucher_price
            tax -= voucher_tax
        else:
            voucher = None

    if price < 0:
        price = 0
    if tax < 0:
        tax = 0

    # Copy addresses
    invoice_address = deepcopy(invoice_address)
    invoice_address.id = None
    invoice_address.save()

    shipping_address = deepcopy(shipping_address)
    shipping_address.id = None
    shipping_address.save()

    order = Order.objects.create(
        user=user,
        session=request.session.session_key,
        price=price,
        tax=tax,

        customer_firstname=customer.selected_invoice_address.firstname,
        customer_lastname=customer.selected_invoice_address.lastname,
        customer_email=customer_email,

        shipping_method=shipping_method,
        shipping_price=shipping_costs["price_gross"],
        shipping_tax=shipping_costs["tax"],
        payment_method=payment_method,
        payment_price=payment_costs["price"],
        payment_tax=payment_costs["tax"],

        invoice_address=invoice_address,
        shipping_address=shipping_address,

        message=request.POST.get("message", ""),
    )

    invoice_address.order = order
    invoice_address.save()

    shipping_address.order = order
    shipping_address.save()

    requested_delivery_date = request.POST.get("requested_delivery_date", None)
    if requested_delivery_date is not None:
        order.requested_delivery_date = requested_delivery_date
        order.save()

    if is_voucher_effective:
        voucher.mark_as_used()
        order.voucher_number = voucher_number
        order.voucher_price = voucher_price
        order.voucher_tax = voucher_tax
        order.save()

    # Copy bank account if one exists
    if customer.selected_bank_account:
        bank_account = customer.selected_bank_account
        order.account_number = bank_account.account_number
        order.bank_identification_code = bank_account.bank_identification_code
        order.bank_name = bank_account.bank_name
        order.depositor = bank_account.depositor

    order.save()

    # Copy cart items
    for cart_item in cart.get_items():
        if cart_item.amount == 0:
            continue
        order_item = OrderItem.objects.create(
            order=order,

            price_net=cart_item.get_price_net(request),
            price_gross=cart_item.get_price_gross(request),
            tax=cart_item.get_tax(request),

            product=cart_item.product,
            product_sku=cart_item.product.sku,
            product_name=cart_item.product.get_name(),
            product_amount=cart_item.amount,
            product_price_net=cart_item.product.get_price_net(request),
            product_price_gross=cart_item.get_product_price_gross(request),
            product_tax=cart_item.product.get_tax(request),
        )

        cart_item.product.decrease_stock_amount(cart_item.amount)

        # Copy properties to order
        if cart_item.product.is_configurable_product():
            for cpv in cart_item.properties.all():
                OrderItemPropertyValue.objects.create(
                    order_item=order_item, property=cpv.property, value=cpv.value)

    for discount in discounts:
        OrderItem.objects.create(
            order=order,
            price_net=-discount["price_net"],
            price_gross=-discount["price_gross"],
            tax=-discount["tax"],
            product_sku=discount["sku"],
            product_name=discount["name"],
            product_amount=1,
            product_price_net=-discount["price_net"],
            product_price_gross=-discount["price_gross"],
            product_tax=-discount["tax"],
        )

    # Send signal before cart is deleted.
    order_created.send({"order": order, "cart": cart, "request": request})

    cart.delete()

    # Note: Save order for later use in thank you page. The order will be
    # removed from the session if the thank you page has been called.
    request.session["order"] = order

    ong = import_symbol(settings.LFS_ORDER_NUMBER_GENERATOR)
    try:
        order_numbers = ong.objects.get(id="order_number")
    except ong.DoesNotExist:
        order_numbers = ong.objects.create(id="order_number")

    try:
        order_numbers.init(request, order)
    except AttributeError:
        pass

    order.number = order_numbers.get_next()
    order.save()

    return order
Example #6
0
def cart_inline(request, template_name="lfs/cart/cart_inline.html"):
    """
    The actual content of the cart.

    This is factored out to be reused within 'normal' and ajax requests.
    """
    cart = cart_utils.get_cart(request)
    shopping_url = lfs.cart.utils.get_go_on_shopping_url(request)
    if cart is None:
        return render_to_string(
            template_name,
            RequestContext(request, {
                "shopping_url": shopping_url,
            }))

    shop = core_utils.get_default_shop(request)
    countries = shop.shipping_countries.all()
    selected_country = shipping_utils.get_selected_shipping_country(request)

    # Get default shipping method, so that we have a one in any case.
    selected_shipping_method = shipping_utils.get_selected_shipping_method(
        request)
    selected_payment_method = payment_utils.get_selected_payment_method(
        request)

    shipping_costs = shipping_utils.get_shipping_costs(
        request, selected_shipping_method)

    # Payment
    payment_costs = payment_utils.get_payment_costs(request,
                                                    selected_payment_method)

    # Cart costs
    cart_price = cart.get_price_gross(
        request) + shipping_costs["price_gross"] + payment_costs["price"]
    cart_tax = cart.get_tax(
        request) + shipping_costs["tax"] + payment_costs["tax"]

    # Discounts
    discounts = lfs.discounts.utils.get_valid_discounts(request)
    for discount in discounts:
        cart_price = cart_price - discount["price_gross"]
        cart_tax = cart_tax - discount["tax"]

    # Voucher
    voucher_number = lfs.voucher.utils.get_current_voucher_number(request)
    try:
        voucher = Voucher.objects.get(number=voucher_number)
    except Voucher.DoesNotExist:
        display_voucher = False
        voucher_value = 0
        voucher_tax = 0
        voucher_message = MESSAGES[6]
    else:
        lfs.voucher.utils.set_current_voucher_number(request, voucher_number)
        is_voucher_effective, voucher_message = voucher.is_effective(
            request, cart)
        if is_voucher_effective:
            display_voucher = True
            voucher_value = voucher.get_price_gross(request, cart)
            cart_price = cart_price - voucher_value
            voucher_tax = voucher.get_tax(request, cart)
            cart_tax = cart_tax - voucher_tax
        else:
            display_voucher = False
            voucher_value = 0
            voucher_tax = 0

    # Calc delivery time for cart (which is the maximum of all cart items)
    max_delivery_time = cart.get_delivery_time(request)

    cart_items = []
    for cart_item in cart.get_items():
        product = cart_item.product
        quantity = product.get_clean_quantity(cart_item.amount)
        cart_items.append({
            "obj":
            cart_item,
            "quantity":
            quantity,
            "product":
            product,
            "product_price_net":
            cart_item.get_price_net(request),
            "product_price_gross":
            cart_item.get_price_gross(request),
            "product_tax":
            cart_item.get_tax(request),
        })

    return render_to_string(
        template_name,
        RequestContext(
            request, {
                "cart":
                cart,
                "cart_items":
                cart_items,
                "cart_price":
                cart_price,
                "cart_tax":
                cart_tax,
                "shipping_methods":
                shipping_utils.get_valid_shipping_methods(request),
                "selected_shipping_method":
                selected_shipping_method,
                "shipping_costs":
                shipping_costs,
                "payment_methods":
                payment_utils.get_valid_payment_methods(request),
                "selected_payment_method":
                selected_payment_method,
                "payment_price":
                payment_costs["price"],
                "countries":
                countries,
                "selected_country":
                selected_country,
                "max_delivery_time":
                max_delivery_time,
                "shopping_url":
                shopping_url,
                "discounts":
                discounts,
                "display_voucher":
                display_voucher,
                "voucher_number":
                voucher_number,
                "voucher_value":
                voucher_value,
                "voucher_tax":
                voucher_tax,
                "voucher_message":
                voucher_message,
            }))
Example #7
0
def cart_inline(request, template_name="lfs/cart/cart_inline.html"):
    """
    The actual content of the cart.

    This is factored out to be reused within 'normal' and ajax requests.
    """
    cart = cart_utils.get_cart(request)
    shopping_url = lfs.cart.utils.get_go_on_shopping_url(request)
    if cart is None:
        return render_to_string(template_name, RequestContext(request, {"shopping_url": shopping_url}))

    shop = core_utils.get_default_shop(request)
    countries = shop.shipping_countries.all()
    selected_country = shipping_utils.get_selected_shipping_country(request)

    # Get default shipping method, so that we have a one in any case.
    selected_shipping_method = shipping_utils.get_selected_shipping_method(request)
    selected_payment_method = payment_utils.get_selected_payment_method(request)

    shipping_costs = shipping_utils.get_shipping_costs(request, selected_shipping_method)

    # Payment
    payment_costs = payment_utils.get_payment_costs(request, selected_payment_method)

    # Cart costs
    cart_price = cart.get_price_gross(request) + shipping_costs["price_gross"] + payment_costs["price"]
    cart_tax = cart.get_tax(request) + shipping_costs["tax"] + payment_costs["tax"]

    # Discounts
    discounts = lfs.discounts.utils.get_valid_discounts(request)
    for discount in discounts:
        cart_price = cart_price - discount["price_gross"]
        cart_tax = cart_tax - discount["tax"]

    # Voucher
    voucher_number = lfs.voucher.utils.get_current_voucher_number(request)
    try:
        voucher = Voucher.objects.get(number=voucher_number)
    except Voucher.DoesNotExist:
        display_voucher = False
        voucher_value = 0
        voucher_tax = 0
        voucher_message = MESSAGES[6]
    else:
        lfs.voucher.utils.set_current_voucher_number(request, voucher_number)
        is_voucher_effective, voucher_message = voucher.is_effective(request, cart)
        if is_voucher_effective:
            display_voucher = True
            voucher_value = voucher.get_price_gross(request, cart)
            cart_price = cart_price - voucher_value
            voucher_tax = voucher.get_tax(request, cart)
            cart_tax = cart_tax - voucher_tax
        else:
            display_voucher = False
            voucher_value = 0
            voucher_tax = 0

    # Calc delivery time for cart (which is the maximum of all cart items)
    max_delivery_time = cart.get_delivery_time(request)

    cart_items = []
    for cart_item in cart.get_items():
        product = cart_item.product
        quantity = product.get_clean_quantity(cart_item.amount)
        cart_items.append(
            {
                "obj": cart_item,
                "quantity": quantity,
                "product": product,
                "product_price_net": cart_item.get_price_net(request),
                "product_price_gross": cart_item.get_price_gross(request),
                "product_tax": cart_item.get_tax(request),
            }
        )

    return render_to_string(
        template_name,
        RequestContext(
            request,
            {
                "cart": cart,
                "cart_items": cart_items,
                "cart_price": cart_price,
                "cart_tax": cart_tax,
                "shipping_methods": shipping_utils.get_valid_shipping_methods(request),
                "selected_shipping_method": selected_shipping_method,
                "shipping_costs": shipping_costs,
                "payment_methods": payment_utils.get_valid_payment_methods(request),
                "selected_payment_method": selected_payment_method,
                "payment_price": payment_costs["price"],
                "countries": countries,
                "selected_country": selected_country,
                "max_delivery_time": max_delivery_time,
                "shopping_url": shopping_url,
                "discounts": discounts,
                "display_voucher": display_voucher,
                "voucher_number": voucher_number,
                "voucher_value": voucher_value,
                "voucher_tax": voucher_tax,
                "voucher_message": voucher_message,
            },
        ),
    )
Example #8
0
def get_cart_costs(request, cart, total=False, cached=True):
    """Returns a dictionary with price and tax of the given cart:

        returns {
            "price" : the cart's price,
            "tax" : the cart's tax,
        }

    This is function DEPRECATED.
    """
    logger.info("Decprecated: lfs.cart.utils: the function 'get_cart_costs' is deprecated. Please use the methods 'get_price/get_tax' of the Cart/Shipping/Payment objects.")

    if cart is None:
        return {"price": 0, "tax": 0}

    cache_key = "%s-cart-costs-%s-%s" % (settings.CACHE_MIDDLEWARE_KEY_PREFIX, total, cart.id)

    if cached:
        cart_costs = cache.get(cache_key)
    else:
        cart_costs = None

    if cart_costs is None:

        items = cart.get_items()

        cart_price = 0
        cart_tax = 0
        for item in items:
            cart_price += item.get_price_gross(request)
            cart_tax += item.get_tax(request)

        if len(items) > 0 and total:
            # Shipping
            shipping_method = shipping_utils.get_selected_shipping_method(request)
            shipping_costs = shipping_utils.get_shipping_costs(request, shipping_method)
            cart_price += shipping_costs["price"]
            cart_tax += shipping_costs["tax"]

            # Payment
            payment_method = payment_utils.get_selected_payment_method(request)
            payment_costs = payment_utils.get_payment_costs(request, payment_method)
            cart_price += payment_costs["price"]
            cart_tax += payment_costs["tax"]

            # Discounts
            import lfs.discounts.utils
            discounts = lfs.discounts.utils.get_valid_discounts(request)
            for discount in discounts:
                cart_price = cart_price - discount["price"]

            # Vouchers
            try:
                voucher_number = lfs.voucher.utils.get_current_voucher_number(request)
                voucher = Voucher.objects.get(number=voucher_number)
            except Voucher.DoesNotExist:
                pass
            else:
                voucher_value = voucher.get_price_gross(cart)
                cart_price = cart_price - voucher_value

        cart_costs = {"price": cart_price, "tax": cart_tax}
        cache.set(cache_key, cart_costs)

    return cart_costs
Example #9
0
def cart_inline(request, template_name="lfs/cart/cart_inline.html"):
    """
    The actual content of the cart.

    This is factored out to be reused within 'normal' and ajax requests.
    """
    cart = cart_utils.get_cart(request)
    shopping_url = lfs.cart.utils.get_go_on_shopping_url(request)
    if cart is None:
        return render_to_string(template_name, request=request, context={
            "shopping_url": shopping_url,
        })

    shop = core_utils.get_default_shop(request)
    countries = shop.shipping_countries.all()
    selected_country = shipping_utils.get_selected_shipping_country(request)

    # Get default shipping method, so that we have a one in any case.
    selected_shipping_method = shipping_utils.get_selected_shipping_method(request)
    selected_payment_method = payment_utils.get_selected_payment_method(request)

    shipping_costs = shipping_utils.get_shipping_costs(request, selected_shipping_method)

    # Payment
    payment_costs = payment_utils.get_payment_costs(request, selected_payment_method)

    # Cart costs
    cart_price = cart.get_price_gross(request) + shipping_costs["price_gross"] + payment_costs["price"]
    cart_tax = cart.get_tax(request) + shipping_costs["tax"] + payment_costs["tax"]

    # get voucher data (if voucher exists)
    voucher_data = lfs.voucher.utils.get_voucher_data(request, cart)

    # get discounts data
    discounts_data = lfs.discounts.utils.get_discounts_data(request)

    # calculate total value of discounts and voucher that sum up
    summed_up_value = discounts_data['summed_up_value']
    if voucher_data['sums_up']:
        summed_up_value += voucher_data['voucher_value']

    # initialize discounts with summed up discounts
    use_voucher = voucher_data['voucher'] is not None
    discounts = discounts_data['summed_up_discounts']
    if voucher_data['voucher_value'] > summed_up_value or discounts_data['max_value'] > summed_up_value:
        # use not summed up value
        if voucher_data['voucher_value'] > discounts_data['max_value']:
            # use voucher only
            discounts = []
        else:
            # use discount only
            discounts = discounts_data['max_discounts']
            use_voucher = False

    for discount in discounts:
        cart_price -= discount["price_gross"]
        cart_tax -= discount["tax"]

    if use_voucher:
        cart_price -= voucher_data['voucher_value']
        cart_tax -= voucher_data['voucher_tax']

    cart_price = max(0, cart_price)
    cart_tax = max(0, cart_tax)

    # Calc delivery time for cart (which is the maximum of all cart items)
    max_delivery_time = cart.get_delivery_time(request)

    cart_items = []
    for cart_item in cart.get_items():
        product = cart_item.product
        quantity = product.get_clean_quantity(cart_item.amount)
        cart_items.append({
            "obj": cart_item,
            "quantity": quantity,
            "product": product,
            "product_price_net": cart_item.get_price_net(request),
            "product_price_gross": cart_item.get_price_gross(request),
            "product_tax": cart_item.get_tax(request),
        })

    return render_to_string(template_name, request=request, context={
        "cart": cart,
        "cart_items": cart_items,
        "cart_price": cart_price,
        "cart_tax": cart_tax,
        "shipping_methods": shipping_utils.get_valid_shipping_methods(request),
        "selected_shipping_method": selected_shipping_method,
        "shipping_costs": shipping_costs,
        "payment_methods": payment_utils.get_valid_payment_methods(request),
        "selected_payment_method": selected_payment_method,
        "payment_price": payment_costs["price"],
        "countries": countries,
        "selected_country": selected_country,
        "max_delivery_time": max_delivery_time,
        "shopping_url": shopping_url,
        "discounts": discounts,
        "display_voucher": use_voucher,
        "voucher_number": voucher_data['voucher_number'],
        "voucher_value": voucher_data['voucher_value'],
        "voucher_tax": voucher_data['voucher_tax'],
        "voucher_message": voucher_data['voucher_message'],
    })
Example #10
0
def add_order(request):
    """Adds an order based on current cart for the current customer.

    It assumes that the customer is prepared with all needed information. This
    is within the responsibility of the checkout form.
    """
    customer = customer_utils.get_customer(request)
    order = None

    invoice_address = customer.selected_invoice_address
    if request.POST.get("no_shipping"):
        shipping_address = customer.selected_invoice_address
    else:
        shipping_address = customer.selected_shipping_address

    cart = cart_utils.get_cart(request)
    if cart is None:
        return order

    shipping_method = shipping_utils.get_selected_shipping_method(request)
    shipping_costs = shipping_utils.get_shipping_costs(request,
                                                       shipping_method)

    payment_method = payment_utils.get_selected_payment_method(request)
    payment_costs = payment_utils.get_payment_costs(request, payment_method)

    # Set email dependend on login state. An anonymous customer doesn't  have a
    # django user account, so we set the name of the invoice address to the
    # customer name.

    # Note: After this has been processed the order's customer email has an
    # email in any case. That means you can use it to send emails to the
    # customer.
    if request.user.is_authenticated():
        user = request.user
        customer_email = user.email
    else:
        user = None
        customer_email = customer.selected_invoice_address.email

    # Calculate the totals
    price = cart.get_price_gross(
        request) + shipping_costs["price"] + payment_costs["price"]
    tax = cart.get_tax(request) + shipping_costs["tax"] + payment_costs["tax"]

    # Discounts
    discounts = lfs.discounts.utils.get_valid_discounts(request)
    for discount in discounts:
        price = price - discount["price"]
        tax = tax - discount["tax"]

    # Add voucher if one exists
    try:
        voucher_number = lfs.voucher.utils.get_current_voucher_number(request)
        voucher = Voucher.objects.get(number=voucher_number)
    except Voucher.DoesNotExist:
        voucher = None
    else:
        is_voucher_effective, voucher_message = voucher.is_effective(
            request, cart)
        if is_voucher_effective:
            voucher_number = voucher.number
            voucher_price = voucher.get_price_gross(request, cart)
            voucher_tax = voucher.get_tax(request, cart)

            price -= voucher_price
            tax -= voucher_tax
        else:
            voucher = None

    order = Order.objects.create(
        user=user,
        session=request.session.session_key,
        price=price,
        tax=tax,
        customer_firstname=customer.selected_invoice_address.firstname,
        customer_lastname=customer.selected_invoice_address.lastname,
        customer_email=customer_email,
        shipping_method=shipping_method,
        shipping_price=shipping_costs["price"],
        shipping_tax=shipping_costs["tax"],
        payment_method=payment_method,
        payment_price=payment_costs["price"],
        payment_tax=payment_costs["tax"],
        invoice_firstname=customer.selected_invoice_address.firstname,
        invoice_lastname=customer.selected_invoice_address.lastname,
        invoice_company_name=customer.selected_invoice_address.company_name,
        invoice_line1=invoice_address.line1,
        invoice_line2=invoice_address.line2,
        invoice_city=invoice_address.city,
        invoice_state=invoice_address.state,
        invoice_code=invoice_address.zip_code,
        invoice_country=Country.objects.get(code=invoice_address.country.code),
        invoice_phone=customer.selected_invoice_address.phone,
        shipping_firstname=shipping_address.firstname,
        shipping_lastname=shipping_address.lastname,
        shipping_company_name=shipping_address.company_name,
        shipping_line1=shipping_address.line1,
        shipping_line2=shipping_address.line2,
        shipping_city=shipping_address.city,
        shipping_state=shipping_address.state,
        shipping_code=shipping_address.zip_code,
        shipping_country=Country.objects.get(
            code=shipping_address.country.code),
        shipping_phone=shipping_address.phone,
        message=request.POST.get("message", ""),
    )

    requested_delivery_date = request.POST.get("requested_delivery_date", None)
    if requested_delivery_date is not None:
        order.requested_delivery_date = requested_delivery_date
        order.save()

    if voucher:
        voucher.mark_as_used()
        order.voucher_number = voucher_number
        order.voucher_price = voucher_price
        order.voucher_tax = voucher_tax
        order.save()

    # Copy bank account if one exists
    if customer.selected_bank_account:
        bank_account = customer.selected_bank_account
        order.account_number = bank_account.account_number
        order.bank_identification_code = bank_account.bank_identification_code
        order.bank_name = bank_account.bank_name
        order.depositor = bank_account.depositor

    order.save()

    # Copy cart items
    for cart_item in cart.get_items():
        order_item = OrderItem.objects.create(
            order=order,
            price_net=cart_item.get_price_net(request),
            price_gross=cart_item.get_price_gross(request),
            tax=cart_item.get_tax(request),
            product=cart_item.product,
            product_sku=cart_item.product.sku,
            product_name=cart_item.product.get_name(),
            product_amount=cart_item.amount,
            product_price_net=cart_item.product.get_price_net(request),
            product_price_gross=cart_item.get_product_price_gross(request),
            product_tax=cart_item.product.get_tax(request),
        )

        cart_item.product.decrease_stock_amount(cart_item.amount)

        # Copy properties to order
        if cart_item.product.is_configurable_product():
            for cpv in cart_item.properties.all():
                OrderItemPropertyValue.objects.create(order_item=order_item,
                                                      property=cpv.property,
                                                      value=cpv.value)

    for discount in discounts:
        OrderItem.objects.create(
            order=order,
            price_net=-(discount["price"] - discount["tax"]),
            price_gross=-discount["price"],
            tax=-discount["tax"],
            product_sku=discount["sku"],
            product_name=discount["name"],
            product_amount=1,
            product_price_net=-(discount["price"] - discount["tax"]),
            product_price_gross=-discount["price"],
            product_tax=-discount["tax"],
        )

    # Send signal before cart is deleted.
    order_created.send({"order": order, "cart": cart, "request": request})

    cart.delete()

    # Note: Save order for later use in thank you page. The order will be
    # removed from the session if the thank you page has been called.
    request.session["order"] = order

    ong = import_symbol(settings.LFS_ORDER_NUMBER_GENERATOR)
    try:
        order_numbers = ong.objects.get(id="order_number")
    except ong.DoesNotExist:
        order_numbers = ong.objects.create(id="order_number")

    try:
        order_numbers.init(request, order)
    except AttributeError:
        pass

    order.number = order_numbers.get_next()
    order.save()

    return order
Example #11
0
def add_order(request):
    """Adds an order based on current cart for the current customer.

    It assumes that the customer is prepared with all needed information. This
    is within the responsibility of the checkout form.
    """
    customer = customer_utils.get_customer(request)
    order = None

    not_required_address = getattr(settings, 'LFS_CHECKOUT_NOT_REQUIRED_ADDRESS', 'shipping')

    invoice_address = customer.selected_invoice_address
    shipping_address = customer.selected_shipping_address
    if not_required_address == 'shipping':
        if request.POST.get("no_shipping"):
            shipping_address = customer.selected_invoice_address
        else:
            shipping_address = customer.selected_shipping_address
    else:
        if request.POST.get("no_invoice"):
            invoice_address = customer.selected_shipping_address
        else:
            invoice_address = customer.selected_invoice_address

    cart = cart_utils.get_cart(request)
    if cart is None:
        return order

    shipping_method = shipping_utils.get_selected_shipping_method(request)
    shipping_costs = shipping_utils.get_shipping_costs(request, shipping_method)

    payment_method = payment_utils.get_selected_payment_method(request)
    payment_costs = payment_utils.get_payment_costs(request, payment_method)

    # Set email dependend on login state. An anonymous customer doesn't  have a
    # django user account, so we set the name of the invoice address to the
    # customer name.

    # Note: After this has been processed the order's customer email has an
    # email in any case. That means you can use it to send emails to the
    # customer.
    if request.user.is_authenticated():
        user = request.user
        customer_email = user.email
    else:
        user = None
        customer_email = customer.selected_invoice_address.email

    # Calculate the totals
    price = cart.get_price_gross(request) + shipping_costs["price_gross"] + payment_costs["price"]
    tax = cart.get_tax(request) + shipping_costs["tax"] + payment_costs["tax"]

    # get voucher data (if voucher exists)
    voucher_data = get_voucher_data(request, cart)

    # get discounts data
    discounts_data = lfs.discounts.utils.get_discounts_data(request)

    # calculate total value of discounts and voucher that sum up
    summed_up_value = discounts_data['summed_up_value']
    if voucher_data['sums_up']:
        summed_up_value += voucher_data['voucher_value']

    # initialize discounts with summed up discounts
    use_voucher = voucher_data['voucher'] is not None
    discounts = discounts_data['summed_up_discounts']
    if voucher_data['voucher_value'] > summed_up_value or discounts_data['max_value'] > summed_up_value:
        # use not summed up value
        if voucher_data['voucher_value'] > discounts_data['max_value']:
            # use voucher only
            discounts = []
        else:
            # use discount only
            discounts = discounts_data['max_discounts']
            use_voucher = False

    for discount in discounts:
        price -= discount["price_gross"]
        tax -= discount["tax"]

    if use_voucher:
        price -= voucher_data['voucher_value']
        tax -= voucher_data['voucher_tax']

    if price < 0:
        price = 0
    if tax < 0:
        tax = 0

    # Copy addresses
    invoice_address = deepcopy(invoice_address)
    invoice_address.id = None
    invoice_address.pk = None
    invoice_address.save()

    shipping_address = deepcopy(shipping_address)
    shipping_address.id = None
    shipping_address.pk = None
    shipping_address.save()

    order = Order.objects.create(
        user=user,
        session=request.session.session_key,
        price=price,
        tax=tax,

        customer_firstname=customer.selected_invoice_address.firstname,
        customer_lastname=customer.selected_invoice_address.lastname,
        customer_email=customer_email,

        shipping_method=shipping_method,
        shipping_price=shipping_costs["price_gross"],
        shipping_tax=shipping_costs["tax"],
        payment_method=payment_method,
        payment_price=payment_costs["price"],
        payment_tax=payment_costs["tax"],

        invoice_address=invoice_address,
        shipping_address=shipping_address,

        message=request.POST.get("message", ""),
    )

    delivery_time = cart.get_delivery_time(request)
    if delivery_time:
        OrderDeliveryTime.objects.create(order=order, min=delivery_time.min, max=delivery_time.max, unit=delivery_time.unit)

    invoice_address.order = order
    invoice_address.save()

    shipping_address.order = order
    shipping_address.save()

    requested_delivery_date = request.POST.get("requested_delivery_date", None)
    if requested_delivery_date is not None:
        order.requested_delivery_date = requested_delivery_date
        order.save()

    if use_voucher:
        voucher_data['voucher'].mark_as_used()
        order.voucher_number = voucher_data['voucher_number']
        order.voucher_price = voucher_data['voucher_value']
        order.voucher_tax = voucher_data['voucher_tax']
        order.save()

    # Copy bank account if one exists
    if customer.selected_bank_account:
        bank_account = customer.selected_bank_account
        order.account_number = bank_account.account_number
        order.bank_identification_code = bank_account.bank_identification_code
        order.bank_name = bank_account.bank_name
        order.depositor = bank_account.depositor

    order.save()

    # Copy cart items
    for cart_item in cart.get_items():
        if cart_item.amount == 0:
            continue
        order_item = OrderItem.objects.create(
            order=order,

            price_net=cart_item.get_price_net(request),
            price_gross=cart_item.get_price_gross(request),
            tax=cart_item.get_tax(request),

            product=cart_item.product,
            product_sku=cart_item.product.sku,
            product_name=cart_item.product.get_name(),
            product_amount=cart_item.amount,
            product_price_net=cart_item.product.get_price_net(request),
            product_price_gross=cart_item.get_product_price_gross(request),
            product_tax=cart_item.product.get_tax(request),
        )

        cart_item.product.decrease_stock_amount(cart_item.amount)

        # Copy properties to order
        if cart_item.product.is_configurable_product():
            for cpv in cart_item.properties.all():
                OrderItemPropertyValue.objects.create(
                    order_item=order_item, property=cpv.property, value=cpv.value)

    for discount in discounts:
        OrderItem.objects.create(
            order=order,
            price_net=-discount["price_net"],
            price_gross=-discount["price_gross"],
            tax=-discount["tax"],
            product_sku=discount["sku"],
            product_name=discount["name"],
            product_amount=1,
            product_price_net=-discount["price_net"],
            product_price_gross=-discount["price_gross"],
            product_tax=-discount["tax"],
        )

    # Re-initialize selected addresses to be equal to default addresses for next order
    customer.sync_default_to_selected_addresses()
    customer.save()

    # Send signal before cart is deleted.
    order_created.send(order, cart=cart, request=request)

    cart.delete()

    # Note: Save order for later use in thank you page. The order will be
    # removed from the session if the thank you page has been called.
    request.session["order"] = order

    ong = import_symbol(settings.LFS_ORDER_NUMBER_GENERATOR)
    try:
        order_numbers = ong.objects.get(id="order_number")
    except ong.DoesNotExist:
        order_numbers = ong.objects.create(id="order_number")

    try:
        order_numbers.init(request, order)
    except AttributeError:
        pass

    order.number = order_numbers.get_next()
    order.save()

    return order
Example #12
0
def get_cart_costs(request, cart, total=False, cached=True):
    """Returns a dictionary with price and tax of the given cart:

        returns {
            "price" : the cart's price,
            "tax" : the cart's tax,
        }

    This is function DEPRECATED.
    """
    logger.info(
        "Decprecated: lfs.cart.utils: the function 'get_cart_costs' is deprecated. Please use the methods 'get_price/get_tax' of the Cart/Shipping/Payment objects."
    )

    if cart is None:
        return {"price": 0, "tax": 0}

    cache_key = "%s-cart-costs-%s-%s" % (settings.CACHE_MIDDLEWARE_KEY_PREFIX,
                                         total, cart.id)

    if cached:
        cart_costs = cache.get(cache_key)
    else:
        cart_costs = None

    if cart_costs is None:

        items = cart.get_items()

        cart_price = 0
        cart_tax = 0
        for item in items:
            cart_price += item.get_price_gross(request)
            cart_tax += item.get_tax(request)

        if len(items) > 0 and total:
            # Shipping
            shipping_method = shipping_utils.get_selected_shipping_method(
                request)
            shipping_costs = shipping_utils.get_shipping_costs(
                request, shipping_method)
            cart_price += shipping_costs["price"]
            cart_tax += shipping_costs["tax"]

            # Payment
            payment_method = payment_utils.get_selected_payment_method(request)
            payment_costs = payment_utils.get_payment_costs(
                request, payment_method)
            cart_price += payment_costs["price"]
            cart_tax += payment_costs["tax"]

            # Discounts
            import lfs.discounts.utils
            discounts = lfs.discounts.utils.get_valid_discounts(request)
            for discount in discounts:
                cart_price = cart_price - discount["price"]

            # Vouchers
            try:
                voucher_number = lfs.voucher.utils.get_current_voucher_number(
                    request)
                voucher = Voucher.objects.get(number=voucher_number)
            except Voucher.DoesNotExist:
                pass
            else:
                voucher_value = voucher.get_price_gross(cart)
                cart_price = cart_price - voucher_value

        cart_costs = {"price": cart_price, "tax": cart_tax}
        cache.set(cache_key, cart_costs)

    return cart_costs
Example #13
0
def add_order(request):
    """Adds an order based on current cart for the current customer. 
    
    It assumes that the customer is prepared with all needed information. This 
    is within the responsibility of the checkout form.
    """
    customer = customer_utils.get_customer(request)
    order = None

    invoice_address = customer.selected_invoice_address
    if customer.selected_shipping_address:
        shipping_address = customer.selected_shipping_address
    else:
        shipping_address = customer.selected_invoice_address
    
    cart = cart_utils.get_cart(request)
    if cart is None:
        return order
    cart_costs = cart_utils.get_cart_costs(request, cart, total=False)
    
    shipping_method = shipping_utils.get_selected_shipping_method(request)
    shipping_costs = shipping_utils.get_shipping_costs(request, shipping_method)

    payment_method = payment_utils.get_selected_payment_method(request)
    payment_costs = payment_utils.get_payment_costs(request, payment_method)

    # Set email dependend on login state. An anonymous customer doesn't  have a 
    # django user account, so we set the name of the invoice address to the 
    # customer name.
    
    # Note: After this has been processed the order's customer email has an
    # email in any case. That means you can use it to send emails to the
    # customer.
    if request.user.is_authenticated():
        user = request.user
        customer_email = user.email
    else:
        user = None
        customer_email = invoice_address.email
        
    # Calculate the totals    
    price = cart_costs["price"] + shipping_costs["price"] + payment_costs["price"]
    tax = cart_costs["tax"] + shipping_costs["tax"] + payment_costs["tax"]
    
    order = Order.objects.create(
        user = user,
        session = request.session.session_key,
        price = price,
        tax = tax,

        customer_firstname = invoice_address.firstname,
        customer_lastname = invoice_address.lastname,
        customer_email = customer_email,
                
        shipping_method = shipping_method,
        shipping_price = shipping_costs["price"],
        shipping_tax = shipping_costs["tax"],
        payment_method = payment_method,
        payment_price = payment_costs["price"],
        payment_tax = payment_costs["tax"],

        invoice_firstname = invoice_address.firstname,
        invoice_lastname = invoice_address.lastname,
        invoice_company_name = invoice_address.company_name,
        invoice_street = invoice_address.street,
        invoice_zip_code = invoice_address.zip_code,
        invoice_city = invoice_address.city,
        invoice_country = invoice_address.country,
        invoice_phone = invoice_address.phone,

        shipping_firstname = shipping_address.firstname,
        shipping_lastname = shipping_address.lastname,
        shipping_company_name = shipping_address.company_name,
        shipping_street = shipping_address.street,
        shipping_zip_code = shipping_address.zip_code,
        shipping_city = shipping_address.city,
        shipping_country = shipping_address.country,
        shipping_phone = shipping_address.phone,

        message = request.POST.get("message", ""),
    )
        
    # Copy bank account if one exists
    if customer.selected_bank_account:
        bank_account = customer.selected_bank_account
        order.account_number = bank_account.account_number
        order.bank_identification_code = bank_account.bank_identification_code
        order.bank_name = bank_account.bank_name
        order.depositor = bank_account.depositor
    
    order.save()
    
    # Copy cart items
    for cart_item in cart.cartitem_set.all():        
        OrderItem.objects.create(
            order=order,
            
            price_net = cart_item.get_price_net(),
            price_gross = cart_item.get_price_gross(),
            tax = cart_item.get_tax(),

            product = cart_item.product,
            product_sku = cart_item.product.sku,
            product_name = cart_item.product.get_name(),
            product_amount=cart_item.amount,                        
            product_price_net = cart_item.product.get_price_net(),
            product_price_gross = cart_item.product.get_price_gross(),
            product_tax = cart_item.product.get_tax(),
        )
        
        cart_item.product.decrease_stock_amount(cart_item.amount)
        
    cart.delete()
    order_submitted.send(order)
    
    # Note: Save order for later use in thank you page. The order will be
    # removed from the session if the thank you page has been called.
    request.session["order"] = order
    
    return order

                               
Example #14
0
def add_order(request):
    """Adds an order based on current cart for the current customer.

    It assumes that the customer is prepared with all needed information. This
    is within the responsibility of the checkout form.
    """
    customer = customer_utils.get_customer(request)
    order = None

    not_required_address = getattr(settings,
                                   'LFS_CHECKOUT_NOT_REQUIRED_ADDRESS',
                                   'shipping')

    invoice_address = customer.selected_invoice_address
    shipping_address = customer.selected_shipping_address
    if not_required_address == 'shipping':
        if request.POST.get("no_shipping"):
            shipping_address = customer.selected_invoice_address
        else:
            shipping_address = customer.selected_shipping_address
    else:
        if request.POST.get("no_invoice"):
            invoice_address = customer.selected_shipping_address
        else:
            invoice_address = customer.selected_invoice_address

    cart = cart_utils.get_cart(request)
    if cart is None:
        return order

    shipping_method = shipping_utils.get_selected_shipping_method(request)
    shipping_costs = shipping_utils.get_shipping_costs(request,
                                                       shipping_method)

    payment_method = payment_utils.get_selected_payment_method(request)
    payment_costs = payment_utils.get_payment_costs(request, payment_method)

    # Set email dependend on login state. An anonymous customer doesn't  have a
    # django user account, so we set the name of the invoice address to the
    # customer name.

    # Note: After this has been processed the order's customer email has an
    # email in any case. That means you can use it to send emails to the
    # customer.
    if request.user.is_authenticated():
        user = request.user
        customer_email = user.email
    else:
        user = None
        customer_email = customer.selected_invoice_address.email

    # Calculate the totals
    price = cart.get_price_gross(
        request) + shipping_costs["price_gross"] + payment_costs["price"]
    tax = cart.get_tax(request) + shipping_costs["tax"] + payment_costs["tax"]

    # get voucher data (if voucher exists)
    voucher_data = get_voucher_data(request, cart)

    # get discounts data
    discounts_data = lfs.discounts.utils.get_discounts_data(request)

    # calculate total value of discounts and voucher that sum up
    summed_up_value = discounts_data['summed_up_value']
    if voucher_data['sums_up']:
        summed_up_value += voucher_data['voucher_value']

    # initialize discounts with summed up discounts
    use_voucher = voucher_data['voucher'] is not None
    discounts = discounts_data['summed_up_discounts']
    if voucher_data['voucher_value'] > summed_up_value or discounts_data[
            'max_value'] > summed_up_value:
        # use not summed up value
        if voucher_data['voucher_value'] > discounts_data['max_value']:
            # use voucher only
            discounts = []
        else:
            # use discount only
            discounts = discounts_data['max_discounts']
            use_voucher = False

    for discount in discounts:
        price -= discount["price_gross"]
        tax -= discount["tax"]

    if use_voucher:
        price -= voucher_data['voucher_value']
        tax -= voucher_data['voucher_tax']

    if price < 0:
        price = 0
    if tax < 0:
        tax = 0

    # Copy addresses
    invoice_address = deepcopy(invoice_address)
    invoice_address.id = None
    invoice_address.pk = None
    invoice_address.save()

    shipping_address = deepcopy(shipping_address)
    shipping_address.id = None
    shipping_address.pk = None
    shipping_address.save()

    order = Order.objects.create(
        user=user,
        session=request.session.session_key,
        price=price,
        tax=tax,
        customer_firstname=customer.selected_invoice_address.firstname,
        customer_lastname=customer.selected_invoice_address.lastname,
        customer_email=customer_email,
        shipping_method=shipping_method,
        shipping_price=shipping_costs["price_gross"],
        shipping_tax=shipping_costs["tax"],
        payment_method=payment_method,
        payment_price=payment_costs["price"],
        payment_tax=payment_costs["tax"],
        invoice_address=invoice_address,
        shipping_address=shipping_address,
        message=request.POST.get("message", ""),
    )

    delivery_time = cart.get_delivery_time(request)
    if delivery_time:
        OrderDeliveryTime.objects.create(order=order,
                                         min=delivery_time.min,
                                         max=delivery_time.max,
                                         unit=delivery_time.unit)

    invoice_address.order = order
    invoice_address.save()

    shipping_address.order = order
    shipping_address.save()

    requested_delivery_date = request.POST.get("requested_delivery_date", None)
    if requested_delivery_date is not None:
        order.requested_delivery_date = requested_delivery_date
        order.save()

    if use_voucher:
        voucher_data['voucher'].mark_as_used()
        order.voucher_number = voucher_data['voucher_number']
        order.voucher_price = voucher_data['voucher_value']
        order.voucher_tax = voucher_data['voucher_tax']
        order.save()

    # Copy bank account if one exists
    if customer.selected_bank_account:
        bank_account = customer.selected_bank_account
        order.account_number = bank_account.account_number
        order.bank_identification_code = bank_account.bank_identification_code
        order.bank_name = bank_account.bank_name
        order.depositor = bank_account.depositor

    order.save()

    # Copy cart items
    for cart_item in cart.get_items():
        if cart_item.amount == 0:
            continue
        order_item = OrderItem.objects.create(
            order=order,
            price_net=cart_item.get_price_net(request),
            price_gross=cart_item.get_price_gross(request),
            tax=cart_item.get_tax(request),
            product=cart_item.product,
            product_sku=cart_item.product.sku,
            product_name=cart_item.product.get_name(),
            product_amount=cart_item.amount,
            product_price_net=cart_item.product.get_price_net(request),
            product_price_gross=cart_item.get_product_price_gross(request),
            product_tax=cart_item.product.get_tax(request),
        )

        cart_item.product.decrease_stock_amount(cart_item.amount)

        # Copy properties to order
        if cart_item.product.is_configurable_product():
            for cpv in cart_item.properties.all():
                OrderItemPropertyValue.objects.create(order_item=order_item,
                                                      property=cpv.property,
                                                      value=cpv.value)

    for discount in discounts:
        OrderItem.objects.create(
            order=order,
            price_net=-discount["price_net"],
            price_gross=-discount["price_gross"],
            tax=-discount["tax"],
            product_sku=discount["sku"],
            product_name=discount["name"],
            product_amount=1,
            product_price_net=-discount["price_net"],
            product_price_gross=-discount["price_gross"],
            product_tax=-discount["tax"],
        )

    # Re-initialize selected addresses to be equal to default addresses for next order
    customer.sync_default_to_selected_addresses()
    customer.save()

    # Send signal before cart is deleted.
    order_created.send(order, cart=cart, request=request)

    cart.delete()

    # Note: Save order for later use in thank you page. The order will be
    # removed from the session if the thank you page has been called.
    request.session["order"] = order

    ong = import_symbol(settings.LFS_ORDER_NUMBER_GENERATOR)
    try:
        order_numbers = ong.objects.get(id="order_number")
    except ong.DoesNotExist:
        order_numbers = ong.objects.create(id="order_number")

    try:
        order_numbers.init(request, order)
    except AttributeError:
        pass

    order.number = order_numbers.get_next()
    order.save()

    return order
Example #15
0
def add_order(request):
    """Adds an order based on current cart for the current customer.

    It assumes that the customer is prepared with all needed information. This
    is within the responsibility of the checkout form.
    """
    customer = customer_utils.get_customer(request)
    order = None

    invoice_address = customer.selected_invoice_address
    if customer.selected_shipping_address:
        shipping_address = customer.selected_shipping_address
    else:
        shipping_address = customer.selected_invoice_address

    cart = cart_utils.get_cart(request)
    if cart is None:
        return order

    shipping_method = shipping_utils.get_selected_shipping_method(request)
    shipping_costs = shipping_utils.get_shipping_costs(request, shipping_method)

    payment_method = payment_utils.get_selected_payment_method(request)
    payment_costs = payment_utils.get_payment_costs(request, payment_method)

    # Set email dependend on login state. An anonymous customer doesn't  have a
    # django user account, so we set the name of the invoice address to the
    # customer name.

    # Note: After this has been processed the order's customer email has an
    # email in any case. That means you can use it to send emails to the
    # customer.
    if request.user.is_authenticated():
        user = request.user
        customer_email = user.email
    else:
        user = None
        customer_email = customer.selected_invoice_address.email

    # Calculate the totals
    price = cart.get_price_gross(request) + shipping_costs["price"] + payment_costs["price"]
    tax = cart.get_tax(request) + shipping_costs["tax"] + payment_costs["tax"]

    # Discounts
    discounts = lfs.discounts.utils.get_valid_discounts(request)
    for discount in discounts:
        price = price - discount["price"]
        tax = tax - discount["tax"]

    # Add voucher if one exists
    try:
        voucher_number = lfs.voucher.utils.get_current_voucher_number(request)
        voucher = Voucher.objects.get(number=voucher_number)
    except Voucher.DoesNotExist:
        voucher = None
    else:
        is_voucher_effective, voucher_message = voucher.is_effective(cart)
        if is_voucher_effective:
            voucher_number = voucher.number
            voucher_price = voucher.get_price_gross(cart)
            voucher_tax = voucher.get_tax(cart)

            price -= voucher_price
            tax -= voucher_tax
        else:
            voucher = None

    order = Order.objects.create(
        user=user,
        session=request.session.session_key,
        price=price,
        tax=tax,

        customer_firstname=customer.selected_invoice_address.firstname,
        customer_lastname=customer.selected_invoice_address.lastname,
        customer_email=customer_email,

        shipping_method=shipping_method,
        shipping_price=shipping_costs["price"],
        shipping_tax=shipping_costs["tax"],
        payment_method=payment_method,
        payment_price=payment_costs["price"],
        payment_tax=payment_costs["tax"],

        invoice_firstname=customer.selected_invoice_address.firstname,
        invoice_lastname=customer.selected_invoice_address.lastname,
        invoice_line1=invoice_address.company_name,
        invoice_line2=invoice_address.street,
        invoice_city=invoice_address.city,
        invoice_state=invoice_address.state,
        invoice_code=invoice_address.zip_code,
        invoice_country=Country.objects.get(code=invoice_address.country.code),
        invoice_phone=customer.selected_invoice_address.phone,

        shipping_firstname=shipping_address.firstname,
        shipping_lastname=shipping_address.lastname,
        shipping_line1=shipping_address.company_name,
        shipping_line2=shipping_address.street,
        shipping_city=shipping_address.city,
        shipping_state=shipping_address.state,
        shipping_code=shipping_address.zip_code,
        shipping_country=Country.objects.get(code=shipping_address.country.code),
        shipping_phone=shipping_address.phone,

        message=request.POST.get("message", ""),
    )

    requested_delivery_date = request.POST.get("requested_delivery_date", None)
    if requested_delivery_date is not None:
        order.requested_delivery_date = requested_delivery_date
        order.save()

    if voucher:
        voucher.mark_as_used()
        order.voucher_number = voucher_number
        order.voucher_price = voucher_price
        order.voucher_tax = voucher_tax
        order.save()

    # Copy bank account if one exists
    if customer.selected_bank_account:
        bank_account = customer.selected_bank_account
        order.account_number = bank_account.account_number
        order.bank_identification_code = bank_account.bank_identification_code
        order.bank_name = bank_account.bank_name
        order.depositor = bank_account.depositor

    order.save()

    # Copy cart items
    for cart_item in cart.cartitem_set.all():
        order_item = OrderItem.objects.create(
            order=order,

            price_net=cart_item.get_price_net(request),
            price_gross=cart_item.get_price_gross(request),
            tax=cart_item.get_tax(request),

            product=cart_item.product,
            product_sku=cart_item.product.sku,
            product_name=cart_item.product.get_name(),
            product_amount=cart_item.amount,
            product_price_net=cart_item.product.get_price_net(request),
            product_price_gross=cart_item.get_product_price_gross(request),
            product_tax=cart_item.product.get_tax(request),
        )

        cart_item.product.decrease_stock_amount(cart_item.amount)

        # Copy properties to order
        if cart_item.product.is_configurable_product():
            for cpv in cart_item.properties.all():
                OrderItemPropertyValue.objects.create(
                    order_item=order_item, property=cpv.property, value=cpv.value)

    for discount in discounts:
        OrderItem.objects.create(
            order=order,
            price_net=-(discount["price"] - discount["tax"]),
            price_gross=-discount["price"],
            tax=-discount["tax"],

            product_sku=discount["sku"],
            product_name=discount["name"],
            product_amount=1,
            product_price_net=-(discount["price"] - discount["tax"]),
            product_price_gross=-discount["price"],
            product_tax=-discount["tax"],
        )

    cart.delete()

    # Note: Save order for later use in thank you page. The order will be
    # removed from the session if the thank you page has been called.
    request.session["order"] = order

    try:
        order_numbers = MODELS.OrderNumberGenerator.objects.get(id="order_number")
    except MODELS.OrderNumberGenerator.DoesNotExist:
        order_numbers = MODELS.OrderNumberGenerator.objects.create(id="order_number")

    try:
        order_numbers.init(request, order)
    except AttributeError:
        pass

    order.number = order_numbers.get_next()
    order.save()

    return order
Example #16
0
def add_order(request):
    """Adds an order based on current cart for the current customer. 
    
    It assumes that the customer is prepared with all needed information. This 
    is within the responsibility of the checkout form.
    """
    customer = customer_utils.get_customer(request)
    order = None

    invoice_address = customer.selected_invoice_address
    if customer.selected_shipping_address:
        shipping_address = customer.selected_shipping_address
    else:
        shipping_address = customer.selected_invoice_address

    cart = cart_utils.get_cart(request)
    if cart is None:
        return order
    cart_costs = cart_utils.get_cart_costs(request, cart, total=False)

    shipping_method = shipping_utils.get_selected_shipping_method(request)
    shipping_costs = shipping_utils.get_shipping_costs(request,
                                                       shipping_method)

    payment_method = payment_utils.get_selected_payment_method(request)
    payment_costs = payment_utils.get_payment_costs(request, payment_method)

    # Set email dependend on login state. An anonymous customer doesn't  have a
    # django user account, so we set the name of the invoice address to the
    # customer name.

    # Note: After this has been processed the order's customer email has an
    # email in any case. That means you can use it to send emails to the
    # customer.
    if request.user.is_authenticated():
        user = request.user
        customer_email = user.email
    else:
        user = None
        customer_email = invoice_address.email

    # Calculate the totals
    price = cart_costs["price"] + shipping_costs["price"] + payment_costs[
        "price"]
    tax = cart_costs["tax"] + shipping_costs["tax"] + payment_costs["tax"]

    order = Order.objects.create(
        user=user,
        session=request.session.session_key,
        price=price,
        tax=tax,
        customer_firstname=invoice_address.firstname,
        customer_lastname=invoice_address.lastname,
        customer_email=customer_email,
        shipping_method=shipping_method,
        shipping_price=shipping_costs["price"],
        shipping_tax=shipping_costs["tax"],
        payment_method=payment_method,
        payment_price=payment_costs["price"],
        payment_tax=payment_costs["tax"],
        invoice_firstname=invoice_address.firstname,
        invoice_lastname=invoice_address.lastname,
        invoice_company_name=invoice_address.company_name,
        invoice_street=invoice_address.street,
        invoice_zip_code=invoice_address.zip_code,
        invoice_city=invoice_address.city,
        invoice_country=invoice_address.country,
        invoice_phone=invoice_address.phone,
        shipping_firstname=shipping_address.firstname,
        shipping_lastname=shipping_address.lastname,
        shipping_company_name=shipping_address.company_name,
        shipping_street=shipping_address.street,
        shipping_zip_code=shipping_address.zip_code,
        shipping_city=shipping_address.city,
        shipping_country=shipping_address.country,
        shipping_phone=shipping_address.phone,
        message=request.POST.get("message", ""),
    )

    # Copy bank account if one exists
    if customer.selected_bank_account:
        bank_account = customer.selected_bank_account
        order.account_number = bank_account.account_number
        order.bank_identification_code = bank_account.bank_identification_code
        order.bank_name = bank_account.bank_name
        order.depositor = bank_account.depositor

    order.save()

    # Copy cart items
    for cart_item in cart.cartitem_set.all():
        OrderItem.objects.create(
            order=order,
            price_net=cart_item.get_price_net(),
            price_gross=cart_item.get_price_gross(),
            tax=cart_item.get_tax(),
            product=cart_item.product,
            product_sku=cart_item.product.sku,
            product_name=cart_item.product.get_name(),
            product_amount=cart_item.amount,
            product_price_net=cart_item.product.get_price_net(),
            product_price_gross=cart_item.product.get_price_gross(),
            product_tax=cart_item.product.get_tax(),
        )

        cart_item.product.decrease_stock_amount(cart_item.amount)

    cart.delete()
    order_submitted.send(order)

    # Note: Save order for later use in thank you page. The order will be
    # removed from the session if the thank you page has been called.
    request.session["order"] = order

    return order