def action_pay(self, resource, context, form): from orders import Order cart = ProductCart(context) root = context.root # Check if cart is valid if not cart.is_valid(): return context.come_back(CART_ERROR, goto='/') # Calcul total price total_price_with_tax = decimal(0) total_price_without_tax = decimal(0) total_weight = decimal(0) for cart_elt in cart.products: product = context.root.get_resource(cart_elt['name']) quantity = cart_elt['quantity'] declination = cart_elt['declination'] unit_price_with_tax = product.get_price_with_tax(declination) unit_price_without_tax = product.get_price_without_tax(declination) total_price_with_tax += unit_price_with_tax * quantity total_price_without_tax += unit_price_without_tax * quantity total_weight += product.get_weight(declination) * quantity # Get Shipping price shipping_price = cart.get_shipping_ns(resource, context)['price'] total_price_with_tax += shipping_price total_price_without_tax += shipping_price # Arrondi total_price_with_tax = get_arrondi(total_price_with_tax) total_price_without_tax = get_arrondi(total_price_without_tax) # Guess ref number # We take last order name + 1 search = root.search(format='order') orders = search.get_documents(sort_by='creation_datetime', reverse=True) if orders: ref = str(int(orders[0].name) + 1) else: ref = '1' # We create a new order kw = {'user': context.user, 'payment_mode': form['payment'], 'shipping_price': shipping_price, 'total_price': total_price_with_tax, 'total_weight': total_weight, 'cart': cart, 'shop': resource, 'shop_uri': context.uri.resolve('/')} orders = resource.get_resource('orders') order = Order.make_resource(Order, orders, ref, title={'en': u'#%s' % ref}, **kw) # We clear the cart cart.clear() # We show the payment form kw = {'ref': ref, 'amount': total_price_with_tax, 'amount_without_tax': total_price_without_tax, 'resource_validator': str(order.get_abspath()), 'mode': form['payment']} payments = resource.get_resource('payments') return payments.show_payment_form(context, kw)
def action_pay(self, resource, context, form): from orders import Order cart = ProductCart(context) root = context.root # Check if cart is valid if not cart.is_valid(): return context.come_back(CART_ERROR, goto='/') # Calcul total price total_price_with_tax = decimal(0) total_price_without_tax = decimal(0) total_weight = decimal(0) for cart_elt in cart.products: product = context.root.get_resource(cart_elt['name']) quantity = cart_elt['quantity'] declination = cart_elt['declination'] unit_price_with_tax = product.get_price_with_tax(declination) unit_price_without_tax = product.get_price_without_tax(declination) total_price_with_tax += unit_price_with_tax * quantity total_price_without_tax += unit_price_without_tax * quantity total_weight += product.get_weight(declination) * quantity # Get Shipping price shipping_price = cart.get_shipping_ns(resource, context)['price'] total_price_with_tax += shipping_price total_price_without_tax += shipping_price # Arrondi total_price_with_tax = get_arrondi(total_price_with_tax) total_price_without_tax = get_arrondi(total_price_without_tax) # Guess ref number # We take last order name + 1 search = root.search(format='order') orders = search.get_documents(sort_by='creation_datetime', reverse=True) if orders: ref = str(int(orders[0].name) + 1) else: ref = '1' # We create a new order kw = { 'user': context.user, 'payment_mode': form['payment'], 'shipping_price': shipping_price, 'total_price': total_price_with_tax, 'total_weight': total_weight, 'cart': cart, 'shop': resource, 'shop_uri': context.uri.resolve('/') } orders = resource.get_resource('orders') order = Order.make_resource(Order, orders, ref, title={'en': u'#%s' % ref}, **kw) # We clear the cart cart.clear() # We show the payment form kw = { 'ref': ref, 'amount': total_price_with_tax, 'amount_without_tax': total_price_without_tax, 'resource_validator': str(order.get_abspath()), 'mode': form['payment'] } payments = resource.get_resource('payments') return payments.show_payment_form(context, kw)
def get_namespace(self, resource, context): namespace = { 'products': [], 'show_ht_price': resource.show_ht_price(context), 'see_actions': self.see_actions } abspath = resource.get_abspath() # Get cart cart = ProductCart(context) # Get products informations total_weight = decimal(0) total = {'with_tax': decimal(0), 'without_tax': decimal(0)} for product_cart in cart.products: # Get product product = context.root.get_resource(product_cart['name'], soft=True) # Check product is buyable if not product or not product.is_buyable(context): continue quantity = product_cart['quantity'] declination = product_cart['declination'] # Weight total_weight += product.get_weight(declination) * quantity # Prices unit_price_with_tax = product.get_price_with_tax(declination) unit_price_without_tax = product.get_price_without_tax(declination) total_price_with_tax = unit_price_with_tax * quantity total_price_without_tax = unit_price_without_tax * quantity price = { 'unit': { 'with_tax': format_price(unit_price_with_tax), 'without_tax': format_price(unit_price_without_tax) }, 'total': { 'with_tax': format_price(total_price_with_tax), 'without_tax': format_price(total_price_without_tax) } } total['without_tax'] += total_price_without_tax total['with_tax'] += total_price_with_tax # All declination_name = product_cart['declination'] if declination_name: declination_ns = product.get_declination_namespace( declination_name) else: declination_ns = None can_add_quantity = product.is_in_stock_or_ignore_stock( quantity + 1, declination_name) namespace['products'].append({ 'id': product_cart['id'], 'name': product.name, 'img': product.get_cover_namespace(context), 'title': product.get_title(), 'href': context.get_link(product), 'can_add_quantity': can_add_quantity, 'quantity': quantity, 'declination': declination_ns, 'price': price }) namespace['total'] = total # Get shippings namespace['ship'] = None if cart.shipping: namespace['ship'] = cart.get_shipping_ns(resource, context) namespace['total']['with_tax'] += namespace['ship']['price'] namespace['total']['without_tax'] += namespace['ship']['price'] # Format total prices for key in ['with_tax', 'without_tax']: namespace['total'][key] = format_price(namespace['total'][key]) return namespace
def get_namespace(self, resource, context): namespace = {'products': [], 'show_ht_price': resource.show_ht_price(context), 'see_actions': self.see_actions} abspath = resource.get_abspath() # Get cart cart = ProductCart(context) # Get products informations total_weight = decimal(0) total = {'with_tax': decimal(0), 'without_tax': decimal(0)} for product_cart in cart.products: # Get product product = context.root.get_resource(product_cart['name'], soft=True) # Check product is buyable if not product or not product.is_buyable(context): continue quantity = product_cart['quantity'] declination = product_cart['declination'] # Weight total_weight += product.get_weight(declination) * quantity # Prices unit_price_with_tax = product.get_price_with_tax(declination) unit_price_without_tax = product.get_price_without_tax(declination) total_price_with_tax = unit_price_with_tax * quantity total_price_without_tax = unit_price_without_tax * quantity price = { 'unit': {'with_tax': format_price(unit_price_with_tax), 'without_tax': format_price(unit_price_without_tax)}, 'total': {'with_tax': format_price(total_price_with_tax), 'without_tax': format_price(total_price_without_tax)}} total['without_tax'] += total_price_without_tax total['with_tax'] += total_price_with_tax # All declination_name = product_cart['declination'] if declination_name: declination_ns = product.get_declination_namespace(declination_name) else: declination_ns = None can_add_quantity = product.is_in_stock_or_ignore_stock(quantity+1, declination_name) namespace['products'].append( {'id': product_cart['id'], 'name': product.name, 'img': product.get_cover_namespace(context), 'title': product.get_title(), 'href': context.get_link(product), 'can_add_quantity': can_add_quantity, 'quantity': quantity, 'declination': declination_ns, 'price': price}) namespace['total'] = total # Get shippings namespace['ship'] = None if cart.shipping: namespace['ship'] = cart.get_shipping_ns(resource, context) namespace['total']['with_tax'] += namespace['ship']['price'] namespace['total']['without_tax'] += namespace['ship']['price'] # Format total prices for key in ['with_tax', 'without_tax']: namespace['total'][key] = format_price(namespace['total'][key]) return namespace