[IMP] ecommerce with browse record for sale order
bzr revid: chm@openerp.com-20130701161247-qj5k7gaun2kg337g
This commit is contained in:
parent
029bd713cc
commit
3bbaf5db61
|
@ -13,4 +13,6 @@ OpenERP E-Commerce
|
|||
'data': [
|
||||
'views/ecommerce.xml'
|
||||
],
|
||||
'js': ['static/src/js/ecommerce.js'],
|
||||
'css': ['static/src/css/ecommerce.css'],
|
||||
}
|
||||
|
|
|
@ -9,61 +9,147 @@ import urllib
|
|||
|
||||
class Ecommerce(http.Controller):
|
||||
|
||||
@http.route(['/shop', '/shop/category/<cat_id>'], type='http', auth="db")
|
||||
def category(self, cat_id=0, offset=0):
|
||||
def get_cr_uid(self):
|
||||
try:
|
||||
request.session.check_security()
|
||||
editable = True
|
||||
uid = request.session._uid
|
||||
except http.SessionExpiredException:
|
||||
editable = False
|
||||
uid = openerp.SUPERUSER_ID
|
||||
return (request.cr, uid)
|
||||
|
||||
def get_values(self):
|
||||
cr, uid = self.get_cr_uid()
|
||||
|
||||
total_price = 0
|
||||
cart = {}
|
||||
if request.session.get('ecommerce'):
|
||||
order = request.registry.get('sale.order').browse(cr, uid, request.session.get('ecommerce'))
|
||||
total_price = order.amount_total
|
||||
for line in order.order_line:
|
||||
cart[line.product_id.id] = {'product_id': line.product_id.id, 'quantity': int(line.product_uom_qty)}
|
||||
|
||||
values = {
|
||||
'request': request,
|
||||
'registry': request.registry,
|
||||
'cr': cr,
|
||||
'uid': uid,
|
||||
'cart': cart,
|
||||
'total_price': total_price,
|
||||
}
|
||||
return values
|
||||
|
||||
@http.route(['/shop', '/shop/category/<cat_id>'], type='http', auth="db")
|
||||
def category(self, cat_id=0, offset=0):
|
||||
values = self.get_values()
|
||||
cr, uid = 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(request.cr, uid, [('parent_id', '=', False)])
|
||||
product_ids = product_obj.search(request.cr, uid, cat_id and [('pos_categ_id.id', 'child_of', cat_id)] or [(1, '=', 1)], limit=20, offset=offset)
|
||||
category_ids = category_obj.search(cr, uid, [('parent_id', '=', False)])
|
||||
product_ids = product_obj.search(cr, uid, cat_id and [('pos_categ_id.id', 'child_of', cat_id)] or [(1, '=', 1)], limit=20, offset=offset)
|
||||
|
||||
values = {
|
||||
'editable': editable,
|
||||
'request': request,
|
||||
'registry': request.registry,
|
||||
'cr': request.cr,
|
||||
'uid': uid,
|
||||
values.update({
|
||||
'current_category': cat_id,
|
||||
'categories': category_obj.browse(request.cr, uid, category_ids),
|
||||
'products': product_obj.browse(request.cr, uid, product_ids),
|
||||
}
|
||||
html = request.registry.get("ir.ui.view").render(request.cr, uid, "ecommerce.categories", values)
|
||||
'categories': category_obj.browse(cr, uid, category_ids),
|
||||
'products': product_obj.browse(cr, uid, product_ids),
|
||||
})
|
||||
html = request.registry.get("ir.ui.view").render(cr, uid, "ecommerce.categories", values)
|
||||
return html
|
||||
|
||||
@http.route(['/shop/category/<cat_id>/product/<product_id>', '/shop/product/<product_id>'], type='http', auth="db")
|
||||
@http.route(['/shop/product/<product_id>'], type='http', auth="db")
|
||||
def product(self, cat_id=0, product_id=0, offset=0):
|
||||
try:
|
||||
request.session.check_security()
|
||||
editable = True
|
||||
uid = request.session._uid
|
||||
except http.SessionExpiredException:
|
||||
editable = False
|
||||
uid = openerp.SUPERUSER_ID
|
||||
values = self.get_values()
|
||||
cr, uid = 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(request.cr, uid, [('parent_id', '=', False)])
|
||||
category_ids = category_obj.search(cr, uid, [('parent_id', '=', False)])
|
||||
|
||||
values = {
|
||||
'editable': editable,
|
||||
'request': request,
|
||||
'registry': request.registry,
|
||||
'cr': request.cr,
|
||||
'uid': uid,
|
||||
'categories': category_obj.browse(request.cr, uid, category_ids),
|
||||
'product': product_obj.browse(request.cr, uid, product_id),
|
||||
}
|
||||
html = request.registry.get("ir.ui.view").render(request.cr, uid, "ecommerce.product", values)
|
||||
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()
|
||||
cr, uid = 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({
|
||||
'my_cart': True,
|
||||
'categories': category_obj.browse(cr, uid, category_ids),
|
||||
'products': product_obj.browse(cr, uid, product_ids),
|
||||
})
|
||||
html = request.registry.get("ir.ui.view").render(cr, uid, "ecommerce.categories", values)
|
||||
return html
|
||||
|
||||
@http.route(['/shop/add_cart'], type='http', auth="db")
|
||||
def add_cart(self, product_id=0, remove=False):
|
||||
cr, uid = self.get_cr_uid()
|
||||
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
|
||||
partner = request.registry.get('res.users').browse(cr, uid, uid, context=context).partner_id
|
||||
|
||||
order_id = request.session.get('ecommerce')
|
||||
|
||||
# find or set order_id
|
||||
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.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
|
||||
order = order_obj.browse(cr, uid, order_id, context=context)
|
||||
|
||||
# values initialisation
|
||||
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:
|
||||
quantity = 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
|
||||
|
||||
# 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)
|
||||
|
||||
return "%s" % values['product_uom_qty']
|
||||
|
||||
@http.route(['/shop/remove_cart'], type='http', auth="db")
|
||||
def remove_cart(self, product_id=0):
|
||||
return self.add_cart(product_id=0, remove=True)
|
||||
|
||||
@http.route(['/shop/confirm_cart'], type='http', auth="db")
|
||||
def confirm_cart(self):
|
||||
return ""
|
||||
|
||||
# vim:expandtab:tabstop=4:softtabstop=4:shiftwidth=4:
|
||||
|
|
|
@ -0,0 +1,12 @@
|
|||
.oe_website_body .oe_ecommerce .btn.btn-success .oe_txt_empty,
|
||||
.oe_website_body .oe_ecommerce .btn.btn-primary .oe_txt,
|
||||
.oe_website_body .oe_ecommerce .btn.oe_hidden {
|
||||
display: none;
|
||||
}
|
||||
.oe_website_body .oe_ecommerce .oe_button_cart,
|
||||
.oe_website_body .oe_ecommerce .well.media a {
|
||||
float: right;
|
||||
},
|
||||
.oe_website_body .oe_ecommerce .well.media {
|
||||
margin-top: 30px;
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
$(document).ready(function (){
|
||||
$('.oe_ecommerce').on('click', '.btn-success,.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');
|
||||
|
||||
$.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;
|
||||
});
|
||||
});
|
||||
});
|
|
@ -9,7 +9,8 @@
|
|||
<field name="arch" type="xml">
|
||||
<data>
|
||||
<xpath expr="(//header//li)[last()]" position="after">
|
||||
<li class="pull-right"><a href="/shop/">Shop</a></li>
|
||||
<li class="pull-right"><a href="/shop/my_cart">My cart</a></li>
|
||||
<li class="pull-right"><a href="/shop">Shop</a></li>
|
||||
</xpath>
|
||||
<xpath expr="//footer/div/div/div[3]" position="inside">
|
||||
SHOP Footer
|
||||
|
@ -20,8 +21,8 @@
|
|||
|
||||
<!-- Product list -->
|
||||
|
||||
<record id="category_product" model="ir.ui.view">
|
||||
<field name="name">ecommerce_product</field>
|
||||
<record id="categories_product" model="ir.ui.view">
|
||||
<field name="name">categories_product</field>
|
||||
<field name="type">qweb</field>
|
||||
<field name="arch" type="xml">
|
||||
<div class="media">
|
||||
|
@ -29,6 +30,13 @@
|
|||
<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' %% (product.id not in cart and 'oe_hidden' or '')" t-att-data-id="product.id">Remove one</button>
|
||||
<button t-att-class="'btn %%s' %% (product.id in cart 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="product.id in cart and cart[product.id]['quantity']"/></span> in my cart)</span>
|
||||
</button>
|
||||
</div>
|
||||
<a t-att-href="'/shop/product/%%s' %% product.id"><h4 class="media-heading"><t t-esc="product.name"/></h4></a>
|
||||
<t t-esc="product.description_sale"/>
|
||||
<div><t t-esc="product.list_price"/> €</div>
|
||||
|
@ -59,8 +67,9 @@
|
|||
<field name="type">qweb</field>
|
||||
<field name="arch" type="xml">
|
||||
<t t-call="website.layout">
|
||||
<t t-set="title">Shop - Categories</t>
|
||||
<div class="container">
|
||||
<t t-set="title">Shop - <t t-if="not my_cart">Categories</t><t t-if="my_cart">My cart</t>
|
||||
</t>
|
||||
<div class="container oe_ecommerce">
|
||||
<div class="row">
|
||||
<div class="span4">
|
||||
<ul class="nav nav-list">
|
||||
|
@ -71,8 +80,14 @@
|
|||
</div>
|
||||
<div class="span8">
|
||||
<t t-foreach="products" t-as="product">
|
||||
<t t-call="ecommerce.category_product"/>
|
||||
<t t-call="ecommerce.categories_product"/>
|
||||
</t>
|
||||
<div t-if="my_cart" class="media well">
|
||||
<h1>
|
||||
<a href="/shop/confirm_cart"><button class="btn btn-success">Confirm my order</button></a>
|
||||
Total: <span class="oe_total"><t t-esc="total_price"/> €</span>
|
||||
</h1>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
@ -80,13 +95,15 @@
|
|||
</field>
|
||||
</record>
|
||||
|
||||
<!-- product -->
|
||||
|
||||
<record id="product" model="ir.ui.view">
|
||||
<field name="name">product</field>
|
||||
<field name="type">qweb</field>
|
||||
<field name="arch" type="xml">
|
||||
<t t-call="website.layout">
|
||||
<t t-set="title">Shop - Categories</t>
|
||||
<div class="container">
|
||||
<div class="container oe_ecommerce">
|
||||
<div class="row">
|
||||
<div class="span4">
|
||||
<ul class="nav nav-list">
|
||||
|
|
Loading…
Reference in New Issue