Exemplo n.º 1
0
    def addOrder(self, customer=None, cart=None):
        """
        """
        cartmanager = ICartManagement(self.context)
        if customer is None:
            cm = ICustomerManagement(self.context)
            customer = cm.getAuthenticatedCustomer()

        if cart is None:
            cart = cartmanager.getCart()

        portal = getToolByName(self.context, 'portal_url').getPortalObject()

        ## The current user may not be allowed to create an order, so we
        ## temporarily change the security context to use a temporary
        ## user with manager role.
        old_sm = getSecurityManager()
        tmp_user = UnrestrictedUser(old_sm.getUser().getId(), '', ['Manager'],
                                    '')

        tmp_user = tmp_user.__of__(portal.acl_users)
        newSecurityManager(None, tmp_user)

        # Add a new order
        new_id = self._createOrderId()
        self.orders.invokeFactory("Order", id=new_id)
        new_order = getattr(self.orders, new_id)

        # Copy Customer to Order
        customer = ICustomerManagement(self.context).getAuthenticatedCustomer()
        cm = ICopyManagement(customer)
        cm.copyTo(new_order)

        # Add cart items to order
        IItemManagement(new_order).addItemsFromCart(cart)

        # Add total tax
        new_order.setTax(ITaxes(cart).getTaxForCustomer())

        # Add shipping values to order
        sm = IShippingPriceManagement(self.context)
        new_order.setShippingPriceNet(sm.getPriceNet())
        new_order.setShippingPriceGross(sm.getPriceForCustomer())
        new_order.setShippingTax(sm.getTaxForCustomer())
        new_order.setShippingTaxRate(sm.getTaxRateForCustomer())

        # Add payment price values to order
        pp = IPaymentPriceManagement(self.context)
        new_order.setPaymentPriceGross(pp.getPriceForCustomer())
        new_order.setPaymentPriceNet(pp.getPriceNet())
        new_order.setPaymentTax(pp.getTaxForCustomer())
        new_order.setPaymentTaxRate(pp.getTaxRateForCustomer())

        ## Reset security manager
        setSecurityManager(old_sm)

        # Index with customer again
        new_order.reindexObject()

        return new_order
class TestShopShippingManagement(EasyShopTestCase):
    """
    """
    def afterSetUp(self):
        """
        """
        super(TestShopShippingManagement, self).afterSetUp()
        self.shop.taxes.invokeFactory("CustomerTax", id="customer", rate=10.0)
        self.sm = IShippingPriceManagement(self.shop)
        
    def testGetShippingPrice(self):
        """
        """
        price = self.sm.getShippingPrice("default")
        self.assertEqual(price.getPrice(), 10.0)
                
    def testGetShippingPrices(self):
        """
        """
        self.shop.shippingprices.invokeFactory("ShippingPrice", "s1")
        self.shop.shippingprices.invokeFactory("ShippingPrice", "s2")
        self.shop.shippingprices.invokeFactory("ShippingPrice", "s3")
        self.shop.shippingprices.invokeFactory("ShippingPrice", "s4")
        
        ids = [p.getId() for p in self.sm.getShippingPrices()]
        self.assertEqual(ids, ["default", "s1", "s2", "s3", "s4"])
                
    def testGetPriceGross(self):
        """
        """
        self.assertEqual(self.sm.getPriceGross(), 10.0)
                
    def testGetTaxRate(self):
        """
        """
        self.assertEqual(self.sm.getTaxRate(), 19.0)
        
    def testGetTaxRateForCustomer(self):
        """
        """
        self.assertEqual(self.sm.getTaxRateForCustomer(), 10.0)
        
    def testGetTax(self):
        """
        """
        self.assertEqual("%.2f" % self.sm.getTax(), "1.60")
                
    def testGetTaxForCustomer_1(self):
        """
        """
        self.assertEqual(self.sm.getTaxForCustomer(), 0)

    def testGetTaxForCustomer_2(self):
        """
        """
        self.login("newmember")
        view = getMultiAdapter((self.shop.products.product_1, self.shop.products.product_1.REQUEST), name="addToCart")
        view.addToCart()
        
        self.assertEqual("%.2f" % self.sm.getTaxForCustomer(), "0.84")

    def testGetPriceNet(self):
        """
        """
        self.assertEqual("%.2f" % self.sm.getPriceNet(), "8.40")
        
    def testGetPriceForCustomer_1(self):
        """
        """
        self.assertEqual(self.sm.getPriceForCustomer(), 0.0)

    def testGetPriceForCustomer_2(self):
        """
        """
        self.login("newmember")
        view = getMultiAdapter((self.shop.products.product_1, self.shop.products.product_1.REQUEST), name="addToCart")
        view.addToCart()
        
        self.assertEqual("%.2f" % self.sm.getPriceForCustomer(), "9.24")
        
    def testCreateTemporaryShippingProduct(self):
        """
        """
        product = self.sm._createTemporaryShippingProduct()
        self.assertEqual(product.getPrice(), 10.0)
        self.assertEqual(product.getId(), "shipping")
Exemplo n.º 3
0
class TestShopShippingManagement(EasyShopTestCase):
    """
    """
    def afterSetUp(self):
        """
        """
        super(TestShopShippingManagement, self).afterSetUp()
        self.shop.taxes.invokeFactory("CustomerTax", id="customer", rate=10.0)
        self.sm = IShippingPriceManagement(self.shop)

    def testGetShippingPrice(self):
        """
        """
        price = self.sm.getShippingPrice("default")
        self.assertEqual(price.getPrice(), 10.0)

    def testGetShippingPrices(self):
        """
        """
        self.shop.shippingprices.invokeFactory("ShippingPrice", "s1")
        self.shop.shippingprices.invokeFactory("ShippingPrice", "s2")
        self.shop.shippingprices.invokeFactory("ShippingPrice", "s3")
        self.shop.shippingprices.invokeFactory("ShippingPrice", "s4")

        ids = [p.getId() for p in self.sm.getShippingPrices()]
        self.assertEqual(ids, ["default", "s1", "s2", "s3", "s4"])

    def testGetPriceGross(self):
        """
        """
        self.assertEqual(self.sm.getPriceGross(), 10.0)

    def testGetTaxRate(self):
        """
        """
        self.assertEqual(self.sm.getTaxRate(), 19.0)

    def testGetTaxRateForCustomer(self):
        """
        """
        self.assertEqual(self.sm.getTaxRateForCustomer(), 10.0)

    def testGetTax(self):
        """
        """
        self.assertEqual("%.2f" % self.sm.getTax(), "1.60")

    def testGetTaxForCustomer_1(self):
        """
        """
        self.assertEqual(self.sm.getTaxForCustomer(), 0)

    def testGetTaxForCustomer_2(self):
        """
        """
        self.login("newmember")
        view = getMultiAdapter((self.shop.products.product_1,
                                self.shop.products.product_1.REQUEST),
                               name="addToCart")
        view.addToCart()

        self.assertEqual("%.2f" % self.sm.getTaxForCustomer(), "0.84")

    def testGetPriceNet(self):
        """
        """
        self.assertEqual("%.2f" % self.sm.getPriceNet(), "8.40")

    def testGetPriceForCustomer_1(self):
        """
        """
        self.assertEqual(self.sm.getPriceForCustomer(), 0.0)

    def testGetPriceForCustomer_2(self):
        """
        """
        self.login("newmember")
        view = getMultiAdapter((self.shop.products.product_1,
                                self.shop.products.product_1.REQUEST),
                               name="addToCart")
        view.addToCart()

        self.assertEqual("%.2f" % self.sm.getPriceForCustomer(), "9.24")

    def testCreateTemporaryShippingProduct(self):
        """
        """
        product = self.sm._createTemporaryShippingProduct()
        self.assertEqual(product.getPrice(), 10.0)
        self.assertEqual(product.getId(), "shipping")
Exemplo n.º 4
0
    def addOrder(self, customer=None, cart=None):
        """
        """
        cartmanager = ICartManagement(self.context)
        if customer is None:
            cm = ICustomerManagement(self.context)
            customer = cm.getAuthenticatedCustomer()

        if cart is None:
            cart = cartmanager.getCart()

        portal = getToolByName(self.context, 'portal_url').getPortalObject()

        ## The current user may not be allowed to create an order, so we
        ## temporarily change the security context to use a temporary
        ## user with manager role.
        old_sm = getSecurityManager()
        tmp_user = UnrestrictedUser(
            old_sm.getUser().getId(),
            '', ['Manager'], 
            ''
        )
        
        tmp_user = tmp_user.__of__(portal.acl_users)
        newSecurityManager(None, tmp_user)
  
        # Add a new order
        new_id = self._createOrderId()
        self.orders.invokeFactory("Order", id=new_id)
        new_order = getattr(self.orders, new_id)

        # Copy Customer to Order
        customer = ICustomerManagement(self.context).getAuthenticatedCustomer()
        cm = ICopyManagement(customer)
        cm.copyTo(new_order)
                    
        # Add cart items to order
        IItemManagement(new_order).addItemsFromCart(cart)

        # Add total tax 
        new_order.setTax(ITaxes(cart).getTaxForCustomer())
        
        # Add shipping values to order
        sm = IShippingPriceManagement(self.context)
        new_order.setShippingPriceNet(sm.getPriceNet())
        new_order.setShippingPriceGross(sm.getPriceForCustomer())
        new_order.setShippingTax(sm.getTaxForCustomer())
        new_order.setShippingTaxRate(sm.getTaxRateForCustomer())

        # Add payment price values to order 
        pp = IPaymentPriceManagement(self.context)
        new_order.setPaymentPriceGross(pp.getPriceForCustomer())
        new_order.setPaymentPriceNet(pp.getPriceNet())
        new_order.setPaymentTax(pp.getTaxForCustomer())
        new_order.setPaymentTaxRate(pp.getTaxRateForCustomer())
        
        ## Reset security manager
        setSecurityManager(old_sm)
        
        # Index with customer again
        new_order.reindexObject()
        
        return new_order