Exemplo n.º 1
0
    def calculate( self, cart, contact ):
        """
        Calculates rates according to colissimo 2012 rates (see
        django-colissimo app)
        """

        self.cart = cart
        self.contact = contact

        log.debug("Starting LaPoste Colissimo calculations")

        self.is_valid = False

        settings = config_get_group( SHIP_MODULE_NAME )

        # Compute cart total weight
        total_weight = float(settings.BOX_DEFAULT_WEIGHT.value)
        try:
            cart_products = cart.get_shipment_list()
        except:
            # We should have raised an exception ...
            cart_products = []

        products_weight = 0
        for product in cart_products:
            try:
                products_weight += float( product.smart_attr('weight') )
            except TypeError:
                log.debug( "Pass: "******"Updated total weight to " + str(total_weight+products_weight) )
        total_weight += products_weight

        if not products_weight:
            self.charges = Decimal("0.0")
            self.is_valid = False
            self._calculated = True
            log.error("Input cart is empty")
        else:
            rate= Rate()
            rs = rate.get_rates( contact.shipping_address.country.printable_name, total_weight )
            # Quick and dirty (enought for my customer...)
            level = settings.RECOMMANDED_DEFAULT_LEVEL.value
            if level > 5:
                level = 0

            log.debug( "Rates: %s - Level %s - Rate: %.2f (weight: %.2f price: %.2f)" % (str(rs), level, rs[ level ].price, total_weight, cart.total ) )

            self.charges = Decimal( rs[ level ].price )

            self.is_valid = True
            self._calculated = True
Exemplo n.º 2
0
 def _do_calculate_rate(self):
     #Calcul du poids et encombrement du colis
     total_weight = Decimal('0')
     has_bulky_product = False
     for cart_item in self.cart.cartitem_set.all(): #Pour chaque article du panier
         
         #Verifie que le colis ne contient pas de produit encombrant
         has_bulky_product = has_bulky_product or self._is_product_bulky(cart_item.product)
         
         #Poids du produit
         product_weight = self._get_product_weight(cart_item.product)
             
         #MAJ le poids du colis
         total_weight += product_weight * cart_item.quantity
     
     #Recupere le pays d'expedition
     country = self.contact.shipping_address.country.name
     
     #si le panier contient un produit encombrant : on ne teste pas l'envoi en lettre suivie
     if not has_bulky_product:
         try:
             #Recherche d'un tarif lettre suivie pour le poids du colis et pays d'expedition
             rate = LetterRate.get_rates(country, float(total_weight))
             #Colis expediable en lettre suivie: retourne le prix
             return rate  #Type LetterRate
         except (LetterRate.DoesNotExist, ValueError):
             pass #Colis non expediable en lettre suivie: continue et test le colissimo
     
     #Recherche d'un tarif colissimo pour le poids du colis, pays d'expedition et niveau de recommandé configuré
     #Le niveau de recommandé est lue depuis la configuration livesettings
     try:
         recommanded_level = self.settings.RECOMMANDED_LEVEL
         rate = Rate.get_rates(country, float(total_weight)).get(recommanded__level=recommanded_level) #Type Rate
     except Rate.DoesNotExist, msg:
         #Pas de tarif correspondant : essai avec un niveau de recommandé minimal
         try:
             rate = Rate.get_rates(country, float(total_weight)).get(recommanded__level='R0') #Type Rate
         except Rate.DoesNotExist, msg:
             #Pas de tarif : Envoi impossible
             rate = None