Ejemplo n.º 1
0
 def augment_order(self, customer, product, campaign, user_created, quantity=0, incl_tax=True):
     enterprise_id = product.company.enterprise_id
     item = OrderItem()
     item.order = self
     item.product = product
     item.creator = user_created
     discount = product.get_discount_price(campaign)
     retail = product.get_price(campaign)
     item.unit_price = discount if discount else retail
     item.unit_cost = product.unit_cost
     item.unit_discount_price = discount if discount else 0.0
     item.unit_retail_price = retail
     item.quantity = quantity
     if campaign.tax_rate and incl_tax:
         item.tax = (item.unit_price * item.quantity) * campaign.tax_rate
     if quantity > 0:
         if product.track_inventory:
             InventoryJournal.create_new(product, "Sale", item.quantity, item)
     item.save()
     if product.can_have_children():
         item.flush()  # we need this to get the parent ID.
         children = product.get_children()
         if children and len(children) > 0:
             for kid in children:
                 child_item = OrderItem()
                 child_item.order = self
                 child_item.parent_id = item.order_item_id
                 child_item.product = kid.child
                 child_item.creator = user_created
                 child_item.unit_price = 0.0
                 child_item.unit_discount_price = 0.0
                 child_item.unit_cost = product.unit_cost
                 child_item.quantity = kid.child_quantity
                 if kid.child.track_inventory:
                     InventoryJournal.create_new(kid.child, "Sale", child_item.quantity, child_item)
     Status.add(customer, self, Status.find_event(enterprise_id, self, "MODIFIED"), "Order Modified ")
     self.save()
     self.flush()
     return item
Ejemplo n.º 2
0
    def create_new(cart, customer, enterprise_id, campaign, user_created, order_note=None, incl_tax=True):
        """ KB: [2010-09-09]: Given a cart full of products, create a new order and return it.
        if a given product is a parent, then create an kid order_item of zero cost and attach it to the parent.
        """
        cord = CustomerOrder()
        cord.creator = user_created
        cord.customer = customer
        cord.campaign = campaign
        cord.shipping_total = util.nvl(cart.shipping_total, 0.0)
        cord.shipping_note = cart.shipping_selection_name
        cord.handling_total = util.nvl(cart.handling_total, 0.0)
        cord.shipping_addr1 = cart.shipping_addr1
        cord.shipping_addr2 = cart.shipping_addr2
        cord.shipping_city = cart.shipping_city
        cord.shipping_state = cart.shipping_state
        cord.shipping_zip = cart.shipping_zip
        cord.shipping_country = cart.shipping_country
        cord.shipping_phone = cart.shipping_phone
        cart.calculate_cart_discount_for_order(cord)
        cord.note = order_note
        cord.save()
        cord.flush()
        for cart_item in cart.items:
            prd = Product.load(cart_item["product"].product_id)
            item = OrderItem()
            item.order = cord
            item.product = prd
            item.creator = user_created
            item.start_dt = cart_item["start_dt"]
            item.note = cart_item["note"]
            item.save()
            item.flush()

            attribute_order_items = []
            for attribute_product_id in cart_item["attributes"].keys():
                attribute_order_item = OrderItem()
                attribute_order_item.parent_id = item.order_item_id
                attribute_order_item.order = cord
                ao_prd = Product.load(attribute_product_id)
                attribute_order_item.product = ao_prd
                attribute_order_item.creator = user_created
                attribute_order_item.unit_cost = ao_prd.unit_cost
                attribute_order_item.unit_discount_price = ao_prd.get_discount_price(campaign)
                attribute_order_item.unit_retail_price = ao_prd.get_retail_price(campaign)
                attribute_order_item.quantity = cart_item["attributes"][attribute_product_id]["quantity"]
                attribute_order_item.save()
                attribute_order_items.append(attribute_order_item)

            # KB: [2013-02-24]: Discount is calculated by using the highest price of the discounts for the product and all of its selected attributes
            discount = max(
                [util.nvl(aois.unit_discount_price) for aois in attribute_order_items]
                + [prd.get_discount_price(campaign)]
            )
            # KB: [2013-02-24]: Retail is calculated by using the highest price of the retail prices for the product and all its selected attributes.
            retail = max(
                [util.nvl(aois.unit_retail_price, 0.0) for aois in attribute_order_items]
                + [cart_item["base_price"] if "base_price" in cart_item else prd.get_retail_price(campaign)]
            )

            item.quantity = float(cart_item["quantity"])
            item.unit_price = discount if discount else retail
            if campaign.tax_rate and incl_tax:
                item.tax = (item.unit_price * item.quantity) * campaign.tax_rate
            item.unit_cost = prd.unit_cost
            item.unit_discount_price = discount if discount else None
            item.unit_retail_price = retail
            item.save()
            if prd.track_inventory:
                InventoryJournal.create_new(prd, "Sale", int(item.quantity), item)
            if item.unit_discount_price is not None:
                discount = DiscountProduct.find_by_product(prd)
                Journal.create_new(
                    (item.unit_retail_price - item.unit_discount_price) * int(item.quantity),
                    customer,
                    cord,
                    None,
                    typ="AutomaticDiscount",
                    attachment=discount,
                )
            Status.add(
                customer,
                item,
                Status.find_event(enterprise_id, item, "CREATED"),
                "Item added to order %s @ $%s" % (prd.name, util.money(item.unit_price)),
            )
            if prd.can_have_children():
                item.flush()  # we need this to get the parent ID.
                children = prd.get_children()
                if children and len(children) > 0:
                    for kid in children:
                        child_item = OrderItem()
                        child_item.order = cord
                        child_item.parent_id = item.order_item_id
                        child_item.product = kid.child
                        child_item.creator = user_created
                        child_item.start_dt = cart_item["start_dt"]
                        child_item.unit_price = 0.0
                        child_item.unit_discount_price = None
                        child_item.unit_retail_price = 0.0
                        child_item.unit_cost = prd.unit_cost
                        child_item.quantity = kid.child_quantity
                        if kid.child.track_inventory:
                            InventoryJournal.create_new(kid.child, "Sale", child_item.quantity, child_item)
        Status.add(customer, cord, Status.find_event(enterprise_id, cord, "CREATED"), "Order created ")
        if cord.discount:
            discount_amount = None
            if cord.discount.percent_off:
                item_price = cord.total_item_price()
                discount_amount = item_price - (item_price * cord.discount.percent_off)
            elif cord.discount.shipping_percent_off:
                # (9.0 / (1.0-0.1)) = 10.00
                discount_amount = cart.shipping_discount_total
            if discount_amount and int(discount_amount) > 0:
                Journal.create_new(
                    discount_amount, customer, cord, None, typ="AutomaticDiscount", attachment=cord.discount
                )
        cord.save()
        cord.flush()
        return cord