예제 #1
0
파일: mini_cart.py 프로젝트: hforge/shop
 def get_namespace(self, resource, context):
     cart = ProductCart(context)
     nb_products = cart.get_nb_products()
     shop = get_shop(context.resource)
     # Get total price
     total = cart.get_total_price(shop, with_delivery=False)
     if shop.show_ht_price(context):
         total = total['without_tax']
     else:
         total = total['with_tax']
     # Return namespace
     return {'nb_products': nb_products,
             'total': total,
             'txts_class': 'hidden' if nb_products < 2 else '',
             'txt_class': 'hidden' if nb_products > 1 else ''}
예제 #2
0
 def get_namespace(self, resource, context):
     cart = ProductCart(context)
     nb_products = cart.get_nb_products()
     shop = get_shop(context.resource)
     # Get total price
     total = cart.get_total_price(shop, with_delivery=False)
     if shop.show_ht_price(context):
         total = total['without_tax']
     else:
         total = total['with_tax']
     # Return namespace
     return {
         'nb_products': nb_products,
         'total': total,
         'txts_class': 'hidden' if nb_products < 2 else '',
         'txt_class': 'hidden' if nb_products > 1 else ''
     }
예제 #3
0
    def get_price(self, country, list_weight):
        list_weight = deepcopy(list_weight)
        shop = get_shop(self)
        # Is Free ?
        if self.get_property('is_free'):
            return decimal(0)
        # Transform country to zone
        countries = shop.get_resource('countries').handler
        country_record = countries.get_record(int(country))
        if countries.get_record_value(country_record, 'enabled') is False:
            return None
        zone = countries.get_record_value(country_record, 'zone')
        # XXX to refactor
        # Max value
        mode = self.get_property('mode')
        if mode == 'weight':
            list_weight.sort(reverse=True)
            list_values = list_weight
        elif mode == 'quantity':
            list_values = [1] * len(list_weight)
        # XXX limit by models
        only_this_models = self.get_property('only_this_models')
        if only_this_models:
            cart = ProductCart(get_context())
            for product_cart in cart.products:
                product = shop.get_resource(product_cart['name'], soft=True)
                if product.get_property('product_model') not in only_this_models:
                    return None

        # Get corresponding weight/quantity in table of price
        list_price_ok = {}
        prices = self.get_resource('prices').handler
        min_value = 0
        for record in prices.search(PhraseQuery('zone', zone),
                                    sort_by='max-%s' % mode):
            max_value = prices.get_record_value(record, 'max-%s' % mode)
            price = prices.get_record_value(record, 'price')
            list_price_ok[max_value] = {'min': min_value,
                                        'max': max_value,
                                        'price': price}
            min_value = max_value
        # No price ?
        if len(list_price_ok.keys()) == 0:
            return None
        # Check all weigh if < max_weight
        if mode == 'weight':
            max_value = max(list_price_ok.keys())
            for key in list_weight:
                if key > max_value:
                    return None
        # On crée une partition de poids
        current_value = decimal(0)
        partition = []
        list_values.sort(reverse=True)
        while list_values:
            if current_value + list_values[-1] <= max_value:
                current_value += list_values.pop()
            else:
                partition.append(current_value)
                current_value = decimal(0)
            if len(list_values) == 0:
                partition.append(current_value)
        # Calcul total price
        total_price = decimal(0)
        for p in partition:
            for value in list_price_ok.values():
                if p >= value['min'] and p <= value['max']:
                    total_price += value['price']
                    break
        # Add insurance
        if self.get_property('insurance') > decimal(0):
            context = get_context()
            cart = ProductCart(context)
            products_price = cart.get_total_price(shop,
                                with_delivery=False, pretty=False)
            percent = self.get_property('insurance') / 100
            total_price += products_price['with_tax'] * percent
        return total_price