[IMP] ecommerce
bzr revid: chm@openerp.com-20130702120625-dbgj6d3rutestrlo
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import openerp
|
||||
import simplejson
|
||||
from openerp.addons.web import http
|
||||
from openerp.addons.web.http import request
|
||||
from werkzeug.exceptions import NotFound
|
||||
@@ -16,34 +17,45 @@ class Ecommerce(http.Controller):
|
||||
uid = request.session._uid
|
||||
except http.SessionExpiredException:
|
||||
uid = openerp.SUPERUSER_ID
|
||||
partner_id = request.session.get('partner_id') or request.registry.get('res.users').browse(cr, uid, uid).partner_id.id
|
||||
|
||||
partner_id = request.session.variables.get('ecommerce_partner_id', False)
|
||||
if not partner_id and uid != 1:
|
||||
partner_id = request.registry.get('res.users').browse(cr, uid, uid).partner_id.id
|
||||
request.session.variables['ecommerce_partner_id'] = partner_id
|
||||
|
||||
return (cr, uid, partner_id)
|
||||
|
||||
def get_values(self):
|
||||
cr, uid, partner_id = self.get_cr_uid()
|
||||
order_obj = request.registry.get('sale.order')
|
||||
category_obj = request.registry.get('pos.category')
|
||||
context = {}
|
||||
|
||||
if not request.session.get('ecommerce'):
|
||||
order_id = request.session.variables.get('ecommerce')
|
||||
# check if order allready exists
|
||||
try:
|
||||
order_obj.browse(cr, uid, order_id).pricelist_id
|
||||
except:
|
||||
order_id = None
|
||||
|
||||
if not order_id:
|
||||
fields = [k for k, v in order_obj._columns.items()]
|
||||
order_value = order_obj.default_get(cr, uid, fields, context=context)
|
||||
order_value['partner_id'] = partner_id
|
||||
order_value['partner_id'] = partner_id or request.registry.get('res.users').browse(cr, uid, uid).partner_id.id
|
||||
order_value.update(order_obj.onchange_partner_id(cr, uid, [], uid, context=context)['value'])
|
||||
order_id = order_obj.create(cr, uid, order_value, context=context)
|
||||
request.session['ecommerce'] = order_id
|
||||
request.session.variables['ecommerce'] = order_id
|
||||
|
||||
cart = {}
|
||||
order = order_obj.browse(cr, uid, request.session.get('ecommerce'))
|
||||
for line in order.order_line:
|
||||
cart[line.product_id.id] = {'product_id': line.product_id.id, 'quantity': int(line.product_uom_qty)}
|
||||
category_ids = category_obj.search(cr, uid, [('parent_id', '=', False)])
|
||||
|
||||
values = {
|
||||
'temp': 0,
|
||||
'request': request,
|
||||
'registry': request.registry,
|
||||
'cr': cr,
|
||||
'uid': uid,
|
||||
'cart': cart,
|
||||
'order': order,
|
||||
'order': order_obj.browse(cr, uid, order_id),
|
||||
'categories': category_obj.browse(cr, uid, category_ids),
|
||||
}
|
||||
return values
|
||||
|
||||
@@ -72,86 +84,79 @@ class Ecommerce(http.Controller):
|
||||
cr, uid, partner_id = self.get_cr_uid()
|
||||
|
||||
product_id = product_id and int(product_id) or 0
|
||||
category_obj = request.registry.get('pos.category')
|
||||
product_obj = request.registry.get('product.product')
|
||||
category_ids = category_obj.search(cr, uid, [('parent_id', '=', False)])
|
||||
|
||||
values.update({
|
||||
'categories': category_obj.browse(cr, uid, category_ids),
|
||||
'product': product_obj.browse(cr, uid, product_id),
|
||||
})
|
||||
html = request.registry.get("ir.ui.view").render(cr, uid, "ecommerce.product", values)
|
||||
return html
|
||||
|
||||
@http.route(['/shop/my_cart'], type='http', auth="db")
|
||||
def my_cart(self, cat_id=0, offset=0):
|
||||
values = self.get_values()
|
||||
def my_cart(self, offset=0):
|
||||
cr, uid, partner_id = self.get_cr_uid()
|
||||
|
||||
cat_id = cat_id and int(cat_id) or 0
|
||||
category_obj = request.registry.get('pos.category')
|
||||
product_obj = request.registry.get('product.product')
|
||||
|
||||
category_ids = category_obj.search(cr, uid, [('parent_id', '=', False)])
|
||||
product_ids = [product for product in values['cart']]
|
||||
|
||||
values.update({
|
||||
'categories': category_obj.browse(cr, uid, category_ids),
|
||||
})
|
||||
html = request.registry.get("ir.ui.view").render(cr, uid, "ecommerce.my_cart", values)
|
||||
html = request.registry.get("ir.ui.view").render(cr, uid, "ecommerce.my_cart", self.get_values())
|
||||
return html
|
||||
|
||||
@http.route(['/shop/add_cart'], type='http', auth="db")
|
||||
def add_cart(self, product_id=0, remove=False):
|
||||
cr, uid, partner_id = self.get_cr_uid()
|
||||
values = self.get_values()
|
||||
context = {}
|
||||
|
||||
order_obj = request.registry.get('sale.order')
|
||||
order_line_obj = request.registry.get('sale.order.line')
|
||||
|
||||
product_id = product_id and int(product_id) or 0
|
||||
order_id = request.session.get('ecommerce')
|
||||
order = order_obj.browse(cr, uid, order_id, context=context)
|
||||
order = values['order']
|
||||
|
||||
quantity = 0
|
||||
|
||||
# values initialisation
|
||||
order_line_ids = order_line_obj.search(cr, uid, [('order_id', '=', order_id), ('product_id', '=', product_id)], context=context)
|
||||
order_line_ids = order_line_obj.search(cr, uid, [('order_id', '=', order.id), ('product_id', '=', product_id)], context=context)
|
||||
values = {}
|
||||
if order_line_ids:
|
||||
order_line = order_line_obj.read(cr, uid, order_line_ids, [], context=context)[0]
|
||||
quantity = order_line['product_uom_qty'] + (remove and -1 or 1)
|
||||
if quantity <= 0:
|
||||
order_line_obj.unlink(cr, uid, order_line_ids, context=context)
|
||||
return "0"
|
||||
else:
|
||||
fields = [k for k, v in order_line_obj._columns.items()]
|
||||
values = order_line_obj.default_get(cr, uid, fields, context=context)
|
||||
quantity = 1
|
||||
values['product_uom_qty'] = quantity
|
||||
values['product_id'] = product_id
|
||||
values['order_id'] = order_id
|
||||
values['order_id'] = order.id
|
||||
|
||||
# change and record value
|
||||
pricelist_id = order.pricelist_id and order.pricelist_id.id or False
|
||||
values.update(order_line_obj.product_id_change(cr, uid, [], pricelist_id, product_id, partner_id=partner_id, context=context)['value'])
|
||||
if order_line_ids:
|
||||
order_line_obj.write(cr, uid, order_line_ids, values, context=context)
|
||||
else:
|
||||
order_line_id = order_line_obj.create(cr, uid, values, context=context)
|
||||
order.write({'order_line': [(4, order_line_id)]}, context=context)
|
||||
if quantity:
|
||||
pricelist_id = order.pricelist_id and order.pricelist_id.id or False
|
||||
values.update(order_line_obj.product_id_change(cr, uid, [], pricelist_id, product_id, partner_id=partner_id, context=context)['value'])
|
||||
if order_line_ids:
|
||||
order_line_obj.write(cr, uid, order_line_ids, values, context=context)
|
||||
else:
|
||||
order_line_id = order_line_obj.create(cr, uid, values, context=context)
|
||||
order.write({'order_line': [(4, order_line_id)]}, context=context)
|
||||
|
||||
return "%s" % values['product_uom_qty']
|
||||
html = request.registry.get("ir.ui.view").render(cr, uid, "ecommerce.total", self.get_values())
|
||||
|
||||
return simplejson.dumps({"quantity": quantity, "totalHTML": html})
|
||||
|
||||
@http.route(['/shop/remove_cart'], type='http', auth="db")
|
||||
def remove_cart(self, product_id=0):
|
||||
return self.add_cart(product_id=product_id, remove=True)
|
||||
|
||||
@http.route(['/shop/customer'], type='http', auth="db")
|
||||
def confirm_cart(self):
|
||||
@http.route(['/shop/confirm_cart'], type='http', auth="db")
|
||||
def confirm_cart(self, *arg, **post):
|
||||
cr, uid, partner_id = self.get_cr_uid()
|
||||
values = self.get_values()
|
||||
values.update({
|
||||
'partner_id': request.registry.get('res.users').browse(cr, uid, partner_id),
|
||||
})
|
||||
if post:
|
||||
print post
|
||||
|
||||
{'city': u'regfs', 'name': u'Demo User', 'zip': u'5432534', 'street2': u'', 'country_id': u'gfgfdg', 'phone': u'4523545', 'state': u'fgdgsfgdf', 'street': u'fdsfdsffdsfqsdf 54', 'email': u'demo@example.com'}
|
||||
|
||||
values['partner'] = partner_id and request.registry.get('res.users').browse(cr, uid, partner_id) or False
|
||||
|
||||
html = request.registry.get("ir.ui.view").render(cr, uid, "ecommerce.customer", values)
|
||||
return html
|
||||
|
||||
|
||||
@@ -1,19 +1,20 @@
|
||||
$(document).ready(function (){
|
||||
$('.oe_ecommerce').on('click', '.btn-success,.btn-primary,.btn-inverse', function (e) {
|
||||
$('.oe_ecommerce').on('click', '.oe_product .btn-success,.oe_product .btn-primary,.btn-inverse', function (e) {
|
||||
var $button = $(e.currentTarget);
|
||||
var link = $button.hasClass('btn-inverse') ? '/shop/remove_cart' : '/shop/add_cart';
|
||||
// var $add = $button.parent().find('.btn-success,.btn-primary');
|
||||
// var $remove = $button.parent().find('.btn-inverse');
|
||||
var $add = $button.parent().find('.btn-success,.btn-primary');
|
||||
var $remove = $button.parent().find('.btn-inverse');
|
||||
|
||||
$.get(link, {'product_id': $button.data('id')}, function (quantity) {
|
||||
// var quantity = parseInt(quantity);
|
||||
// $add.find('.oe_quantity').html(quantity);
|
||||
// $add.toggleClass('btn-primary', !quantity).toggleClass('btn-success', !!quantity);
|
||||
// $remove.toggleClass('oe_hidden', !quantity);
|
||||
// if (!quantity) {
|
||||
// $button.parents('.media:first').remove()
|
||||
// }
|
||||
window.location.href = window.location.pathname;
|
||||
$.get(link, {'product_id': $button.data('id')}, function (result) {
|
||||
var result = JSON.parse(result);
|
||||
var quantity = parseInt(result.quantity);
|
||||
$add.find('.oe_quantity').html(quantity);
|
||||
$add.toggleClass('btn-primary', !quantity).toggleClass('btn-success', !!quantity);
|
||||
$remove.toggleClass('oe_hidden', !quantity);
|
||||
if (!quantity) {
|
||||
$button.parents('.oe_product:first').remove()
|
||||
}
|
||||
$('.oe_ecommerce .oe_total').replaceWith(''+result.totalHTML);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -39,7 +39,7 @@
|
||||
<div class="span8">
|
||||
<t t-raw="shop_content">
|
||||
<t t-foreach="products" t-as="product">
|
||||
<t t-set="quantity" t-value="product.id in cart and cart[product.id]['quantity'] or 0"/>
|
||||
<t t-set="quantity" t-value="([int(line.product_uom_qty) for line in (order.order_line or []) if line.product_id.id == product.id] + [0])[0]"/>
|
||||
<t t-call="ecommerce.products"/>
|
||||
</t>
|
||||
</t>
|
||||
@@ -73,13 +73,13 @@
|
||||
<field name="name">products</field>
|
||||
<field name="type">qweb</field>
|
||||
<field name="arch" type="xml">
|
||||
<div class="media">
|
||||
<div class="media oe_product">
|
||||
<div class="pull-left" style="width:64px; height:64px;">
|
||||
<img style="max-width:64px; max-height:64px; margin:auto;" class="media-object" t-att-src="'data:image/png;base64,' + product.image"/>
|
||||
</div>
|
||||
<div class="media-body">
|
||||
<div class="oe_button_cart">
|
||||
<button t-if="my_cart" t-att-class="'btn btn-inverse %%s' %% (not quantity and 'oe_hidden' or '')" t-att-data-id="product.id">Remove one</button>
|
||||
<button t-att-class="'btn btn-inverse %%s' %% (not quantity and 'oe_hidden' or '')" t-att-data-id="product.id">Remove one</button>
|
||||
<button t-att-class="'btn %%s' %% (quantity and 'btn-success' or 'btn-primary')" t-att-data-id="product.id">
|
||||
<span class="oe_txt_empty">Add to cart</span>
|
||||
<span class="oe_txt">Add one (<span class="oe_quantity"><t t-esc="quantity"/></span> in my cart)</span>
|
||||
@@ -121,22 +121,38 @@
|
||||
<t t-call="ecommerce.page">
|
||||
<t t-set="title">My cart</t>
|
||||
<t t-set="shop_content">
|
||||
<t t-foreach="order.order_line" t-as="line">
|
||||
<t t-foreach="order.order_line or []" t-as="line">
|
||||
<t t-set="product" t-value="line.product_id"/>
|
||||
<t t-set="quantity" t-value="int(line.product_uom_qty)"/>
|
||||
<t t-call="ecommerce.products"/>
|
||||
</t>
|
||||
<div class="media well">
|
||||
<h1>
|
||||
<a href="/shop/customer"><button class="btn btn-success">Confirm my order</button></a>
|
||||
Total: <span class="oe_total"><t t-esc="order.amount_total"/> €</span>
|
||||
</h1>
|
||||
</div>
|
||||
<t t-call="ecommerce.total"/>
|
||||
</t>
|
||||
</t>
|
||||
</field>
|
||||
</record>
|
||||
|
||||
<!-- Total Shop my cart -->
|
||||
|
||||
<record id="total" model="ir.ui.view">
|
||||
<field name="name">total</field>
|
||||
<field name="type">qweb</field>
|
||||
<field name="arch" type="xml">
|
||||
<div class="oe_total">
|
||||
<div class="media well" t-if="order.order_line">
|
||||
<a href="/shop/confirm_cart"><button class="btn btn-success">Next stage</button></a>
|
||||
<table>
|
||||
<tr><td>Untaxed Amount</td><td><t t-esc="order.amount_untaxed"/> €</td></tr>
|
||||
<tr><td>Taxes</td><td><t t-esc="order.amount_tax"/> €</td></tr>
|
||||
<tr><td>Total</td><td><h3><t t-esc="order.amount_total"/> €</h3></td></tr>
|
||||
</table>
|
||||
</div>
|
||||
<div class="media well" t-if="not order.order_line">
|
||||
Your cart is empty
|
||||
</div>
|
||||
</div>
|
||||
</field>
|
||||
</record>
|
||||
<!-- Page confirm my cart -->
|
||||
|
||||
<record id="customer" model="ir.ui.view">
|
||||
@@ -146,10 +162,21 @@
|
||||
<t t-call="ecommerce.page">
|
||||
<t t-set="title">Your information</t>
|
||||
<t t-set="shop_content">
|
||||
<form action="/shop/confirm_cart">
|
||||
<input name="name"/>
|
||||
<input name="firstname"/>
|
||||
<button class="btn btn-success">Next</button>
|
||||
<form action="/shop/confirm_cart" method="post" enctype="x-www-form-urlencoded">
|
||||
<div>Please enter your informations</div>
|
||||
<input name="name" required="True" placeholder="Name..." t-att-value="partner and partner.name or ''"/><br/>
|
||||
<input name="phone" type="tel" required="True" placeholder="Phone number..." t-att-value="partner and partner.phone or ''"/>
|
||||
<input name="email" type="email" required="True" placeholder="Email..." t-att-value="partner and partner.email or ''"/>
|
||||
<div>Please enter your address</div>
|
||||
<address>
|
||||
<input name="street" required="True" placeholder="Street..." t-att-value="partner and partner.street or ''"/><br/>
|
||||
<input name="street2" placeholder="Street..." t-att-value="partner and partner.street2 or ''"/><br/>
|
||||
<input name="zip" required="True" placeholder="Zip-code..." t-att-value="partner and partner.zip or ''"/>
|
||||
<input name="city" required="True" placeholder="City..." t-att-value="partner and partner.city or ''"/><br/>
|
||||
<input name="country_id" required="True" placeholder="Country..." t-att-value="partner and partner.country_id and partner.country_id.name_get()[1] or ''"/>
|
||||
<input name="state" required="True" placeholder="State..." t-att-value="partner and partner.state_id and partner.state_id.name_get()[1] or ''"/>
|
||||
</address>
|
||||
<t t-call="ecommerce.total"/>
|
||||
</form>
|
||||
</t>
|
||||
</t>
|
||||
|
||||
@@ -160,21 +160,6 @@
|
||||
</field>
|
||||
</record>
|
||||
|
||||
<record id="layout-event" model="ir.ui.view">
|
||||
<field name="name">event</field>
|
||||
<field name="inherit_id" ref="layout"/>
|
||||
<field name="arch" type="xml">
|
||||
<data>
|
||||
<xpath expr="//ul[@class='oe_website_nav span8 nav nav-pills']" position="inside">
|
||||
<li>Event</li>
|
||||
</xpath>
|
||||
<xpath expr="//footer[@class='oe_website_footer']" position="inside">
|
||||
<div>Our last events</div>
|
||||
</xpath>
|
||||
</data>
|
||||
</field>
|
||||
</record>
|
||||
|
||||
<!--
|
||||
# WIIIP !!
|
||||
module_template = Template("""
|
||||
|
||||
Reference in New Issue
Block a user