[IMP] website_sale: fix context value, get order line name insead of product name, wip payment

bzr revid: chm@openerp.com-20130823094017-90m72xi2oif4qkuj
This commit is contained in:
Christophe Matthieu 2013-08-23 11:40:17 +02:00
parent 4fc8193e46
commit 651e511ed9
3 changed files with 104 additions and 56 deletions

View File

@ -27,16 +27,14 @@ def get_order(order_id=None):
context = {
'pricelist': order.pricelist_id.id,
'partner': order.partner_id.id,
}
return order_obj.browse(request.cr, SUPERUSER_ID, order_id, context=context)
def get_current_order():
order = get_order(request.httprequest.session.get('ecommerce_order_id'))
if order.state != 'draft':
order = get_order()
request.httprequest.session['ecommerce_order_id'] = order.id
return order
if request.httprequest.session.get('ecommerce_order_id'):
return get_order(request.httprequest.session.get('ecommerce_order_id'))
else:
return False
class website(osv.osv):
_inherit = "website"
@ -60,6 +58,9 @@ class Ecommerce(http.Controller):
@http.route(['/shop/', '/shop/category/<cat_id>/', '/shop/category/<cat_id>/page/<int:page>/', '/shop/page/<int:page>/'], type='http', auth="public")
def category(self, cat_id=0, page=0, **post):
if 'promo' in post:
self.change_pricelist(post.get('code'))
website = request.registry['website']
product_obj = request.registry.get('product.template')
@ -81,12 +82,14 @@ class Ecommerce(http.Controller):
product_count = len(product_obj.search(request.cr, request.uid, domain))
pager = website.pager(url="/shop/category/%s/" % cat_id, total=product_count, page=page, step=step, scope=7, url_args=post)
product_ids = product_obj.search(request.cr, request.uid, domain, limit=step, offset=pager['offset'], order="website_published,name")
product_ids = product_obj.search(request.cr, request.uid, domain, limit=step, offset=pager['offset'])
context = request.httprequest.session.get('ecommerce_context', {})
values = website.get_rendering_context({
'categories': self.get_categories(),
'category_id': cat_id,
'products': product_obj.browse(request.cr, request.uid, product_ids),
'products': product_obj.browse(request.cr, request.uid, product_ids, context=context),
'search': post.get("search"),
'pager': pager,
})
@ -94,12 +97,16 @@ class Ecommerce(http.Controller):
@http.route(['/shop/product/<product_id>/'], type='http', auth="public")
def product(self, cat_id=0, product_id=0, **post):
if 'promo' in post:
self.change_pricelist(post.get('code'))
website = request.registry['website']
product_id = product_id and int(product_id) or 0
product_obj = request.registry.get('product.template')
context = get_current_order()._context
context = request.httprequest.session.get('ecommerce_context', {})
product = product_obj.browse(request.cr, request.uid, product_id, context=context)
values = website.get_rendering_context({
@ -110,17 +117,33 @@ class Ecommerce(http.Controller):
})
return website.render("website_sale.product", values)
def change_pricelist(self, code):
request.httprequest.session.setdefault('ecommerce_context', {})
order = get_current_order()
if order:
pricelist_obj = request.registry.get('product.pricelist')
pricelist_ids = pricelist_obj.search(request.cr, SUPERUSER_ID, [('code', '=', code)])
if pricelist_ids:
pricelist_id = pricelist_ids[0]
else:
pricelist_id = order.onchange_partner_id(request.uid, context={})['value']['pricelist_id']
request.httprequest.session['ecommerce_context']['pricelist'] = pricelist_id
values = {'pricelist_id': pricelist_id}
values.update(order.onchange_pricelist_id(pricelist_id, None)['value'])
order.write(values)
for line in order.order_line:
self.add_product_to_cart(line.product_id.id, 0)
@http.route(['/shop/mycart/'], type='http', auth="public")
def mycart(self, **post):
order = get_current_order()
website = request.registry['website']
prod_obj = request.registry.get('product.product')
if post.get('code'):
pricelist_obj = request.registry.get('product.pricelist')
pricelist_ids = pricelist_obj.search(request.cr, SUPERUSER_ID, [('code', '=', post.get('code'))])
if pricelist_ids:
order.write({'pricelist_id': pricelist_ids[0]})
if 'promo' in post:
self.change_pricelist(post.get('promo'))
suggested_ids = []
for line in order.order_line:
@ -139,13 +162,16 @@ class Ecommerce(http.Controller):
return website.render("website_sale.mycart", values)
def add_product_to_cart(self, product_id=0, number=1, set_number=-1):
context = {}
context = request.httprequest.session.get('ecommerce_context', {})
order_line_obj = request.registry.get('sale.order.line')
user_obj = request.registry.get('res.users')
product_id = product_id and int(product_id) or 0
order = get_current_order()
if not order:
order = get_order()
request.httprequest.session['ecommerce_order_id'] = order.id
quantity = 0
@ -158,27 +184,32 @@ class Ecommerce(http.Controller):
quantity = set_number
else:
quantity = order_line['product_uom_qty'] + number
if quantity <= 0:
order_line_obj.unlink(request.cr, SUPERUSER_ID, order_line_ids, context=context)
if quantity < 0:
quantity = 0
else:
fields = [k for k, v in order_line_obj._columns.items()]
values = order_line_obj.default_get(request.cr, SUPERUSER_ID, fields, context=context)
quantity = 1
values['product_uom_qty'] = quantity
values['product_id'] = product_id
values['order_id'] = order.id
# change and record value
if quantity > 0:
pricelist_id = order.pricelist_id and order.pricelist_id.id or False
values.update(order_line_obj.product_id_change(request.cr, SUPERUSER_ID, [], pricelist_id, product_id,
partner_id=user_obj.browse(request.cr, SUPERUSER_ID, request.uid).partner_id.id,
context=context)['value'])
if order_line_ids:
order_line_obj.write(request.cr, SUPERUSER_ID, order_line_ids, values, context=context)
else:
order_line_id = order_line_obj.create(request.cr, SUPERUSER_ID, values, context=context)
order.write({'order_line': [(4, order_line_id)]}, context=context)
pricelist_id = order.pricelist_id and order.pricelist_id.id or False
vals = order_line_obj.product_id_change(request.cr, SUPERUSER_ID, [], pricelist_id, product_id,
partner_id=user_obj.browse(request.cr, SUPERUSER_ID, request.uid).partner_id.id,
context=context)['value']
values.update(vals)
if order_line_ids:
order_line_obj.write(request.cr, SUPERUSER_ID, order_line_ids, values, context=context)
if not quantity:
order_line_obj.unlink(request.cr, SUPERUSER_ID, order_line_ids, context=context)
else:
order_line_id = order_line_obj.create(request.cr, SUPERUSER_ID, values, context=context)
order.write({'order_line': [(4, order_line_id)]}, context=context)
return quantity
@ -319,7 +350,6 @@ class Ecommerce(http.Controller):
shipping_id = partner_obj.create(request.cr, SUPERUSER_ID, shipping_value)
order_value = {
'state': 'progress',
'partner_id': partner_id,
'partner_invoice_id': partner_id,
'partner_shipping_id': shipping_id or partner_id
@ -327,17 +357,14 @@ class Ecommerce(http.Controller):
order_value.update(request.registry.get('sale.order').onchange_partner_id(request.cr, SUPERUSER_ID, [], request.uid, context={})['value'])
order.write(order_value)
request.httprequest.session['ecommerce_order_id_old'] = order.id
request.httprequest.session['ecommerce_order_id'] = None
return werkzeug.utils.redirect("/shop/payment/")
@http.route(['/shop/payment/'], type='http', auth="public")
def payment(self, **post):
website = request.registry['website']
order = get_order(request.httprequest.session.get('ecommerce_order_id_old'))
order = get_current_order()
if order.state != 'progress':
if not order or not order.order_line:
return self.mycart(**post)
values = website.get_rendering_context({
@ -354,4 +381,9 @@ class Ecommerce(http.Controller):
return website.render("website_sale.payment", values)
@http.route(['/shop/payment_validate/'], type='http', auth="public")
def payment_validate(self, **post):
request.httprequest.session['ecommerce_order_id'] = False
return werkzeug.utils.redirect("/shop/")
# vim:expandtab:tabstop=4:softtabstop=4:shiftwidth=4:

View File

@ -1,13 +1,19 @@
$(document).ready(function () {
$(".oe_website_sale input[name='shipping_different']").change(function() {
$(".oe_website_sale input[name='shipping_different']").change(function () {
$(".oe_website_sale .js_shipping").toggle();
});
$(".oe_website_sale .oe_mycart input.js_quantity").change(function() {
$(".oe_website_sale .oe_mycart input.js_quantity").change(function () {
var value = parseInt($(this).val());
if (!isNaN(value)) {
window.location.href = window.location.origin + window.location.pathname + 'set_cart/' + $(this).data('id') + '/' + value + '/';
}
});
$payment = $(".oe_website_sale .js_payment");
$("input[name='payment_type']", $payment).click(function (ev) {
var payment_id = $(ev.target).val();
$("div[data-id]", $payment).addClass("hidden");
$("a.btn:last, div[data-id='"+payment_id+"']", $payment).removeClass("hidden");
});
});

View File

@ -22,7 +22,7 @@
<a href="/shop/mycart/">
<i class="icon-shopping-cart"></i>
My cart
<t t-if="order.get_total_quantity()">
<t t-if="order and order.get_total_quantity()">
<span class="badge badge-success" t-esc="order.get_total_quantity()"/>
</t>
</a>
@ -116,9 +116,14 @@
</a>
<div>
<div t-field="product.description_sale"></div>
<div><span t-field="product.list_price"></span></div>
<div>
<t t-if="product.product_variant_ids[0].lst_price != product.product_variant_ids[0].price">
<small class="text-error" style="text-decoration: line-through;"><t t-esc="product.product_variant_ids[0].lst_price" /></small>
</t>
<b><t t-esc="product.product_variant_ids[0].price" /></b>
</div>
<div class="mb8 mt8">
<a t-attf-href="./add_cart/#{ product.id }/" class="btn btn-small btn-success">Add to cart</a>
<a t-if="product.website_published" t-attf-href="./add_cart/#{ product.id }/" class="btn btn-small btn-success">Add to cart</a>
</div>
</div>
</div>
@ -196,7 +201,7 @@
<t t-set="shop_content">
<div class="span8 oe_mycart">
<h2>My Shopping Cart</h2>
<table class='table table-hover' id="mycart_products">
<table class='table' id="mycart_products">
<colgroup>
<col width="80"/>
<col/>
@ -220,16 +225,14 @@
<a t-attf-href="/shop/product/#{ line.product_id.product_tmpl_id.id }/"><img class="img-rounded" t-att-src="line.product_id.img('image_small')"/></a>
</td>
<td>
<a t-attf-href="/shop/product/#{ line.product_id.product_tmpl_id.id }/">
<span t-field="line.product_id.name"/> <t t-if="line.product_id.variants">(<small t-field="line.product_id.variants"/>)</t>
</a><br/>
<a t-attf-href="/shop/product/#{ line.product_id.product_tmpl_id.id }/"><span t-field="line.name"/></a><br/>
<small t-field="line.product_id.description_sale"/>
</td>
<td>
<t t-if="line.product_id.lst_price != line.product_id.price">
<t t-if="line.product_id.lst_price != line.price_unit">
<small class="text-error" style="text-decoration: line-through;"><t t-esc="line.product_id.lst_price" /></small>
</t>
<span t-field="line.product_id.price"></span>
<span t-field="line.price_unit"></span>
</td>
<td>
<div class="mb8 mt8 pull-right">
@ -253,12 +256,9 @@
<tr> <th colspan="3">Subtotal </th> <th><t t-esc="order.amount_untaxed"/></th></tr>
<tr> <th colspan="3">Taxes </th> <th><t t-esc="order.amount_tax"/></th></tr>
<tr> <th colspan="3"><h4>Total</h4></th> <th><h4><t t-esc="order.amount_total"/></h4></th></tr>
<tr>
<th colspan="4"><a t-if="order.order_line" href="/shop/checkout/" class="pull-right btn btn-success">Proceed To Payment</a></th>
</tr>
</thead>
</table>
<a t-if="order.order_line" href="/shop/checkout/" class="btn btn-success">Proceed To Payment</a>
</div>
</t>
</t>
@ -319,7 +319,7 @@
<template id="reduction_code" inherit_option_id="website_sale.mycart" name="Reduction Code">
<xpath expr="//table[@id='mycart_total']" position="after">
<form t-if="order.order_line" class="well form-search" action="/shop/mycart/" method="post">
<input name="code" class='input' type="text" placeholder="Reduction Code..." t-att-value="order.pricelist_id.code"/>
<input name="promo" class='input' type="text" placeholder="Reduction Code..." t-att-value="order.pricelist_id.code or ''"/>
<button class="btn">Apply Code</button>
</form>
</xpath>
@ -349,9 +349,9 @@
<tbody t-if="order.order_line">
<t t-foreach="order.order_line" t-as="line">
<tr>
<td><t t-esc="line.name"/> <t t-if="line.product_id.variants">(<small t-field="line.product_id.variants"/>)</t></td>
<td><t t-esc="line.name"/></td>
<td><t t-esc="line.product_uom_qty"/></td>
<td><t t-esc="line.product_id.list_price"/></td>
<td><t t-esc="line.price_unit"/> </td>
</tr>
</t>
</tbody>
@ -516,6 +516,10 @@
<template id="payment">
<t t-call="website.layout">
<t t-set="head">
<script type="text/javascript" src="/website_sale/static/src/js/website_sale.js"></script>
<t t-raw="head or ''"/>
</t>
<t t-set="title">Shop - Payment</t>
<div class="container mt48 oe_website_sale">
<div class="row">
@ -533,7 +537,7 @@
<tr>
<td><t t-esc="line.name"/></td>
<td><t t-esc="line.product_uom_qty"/></td>
<td><t t-esc="line.product_id.list_price"/></td>
<td><t t-esc="line.price_unit"/> </td>
</tr>
</t>
</tbody>
@ -545,13 +549,19 @@
</table>
</div>
<div class="span8">
<div class="row">
<div class="js_payment row">
<h3 class="span8">Click on your payment method</h3>
<div class="span2">
<t t-foreach="payments or []" t-as="payment">
<label>
<input t-att-value="payment.id" type="radio" name="payment_type"/> <t t-esc="payment.name"/>
</label>
</t>
</div>
<t t-foreach="payments or []" t-as="payment">
<div t-att-title="payment.name">
<t t-raw="payment._content"/>
</div>
<div t-att-data-id="payment.id" t-raw="payment._content" class="hidden span6"/>
</t>
<a href="/shop/payment_validate/" class="hidden btn">I validate my payment</a>
</div>
</div>
</div>