[FIX] website_sale: auto set or remove carrier in function of product (service or not)

bzr revid: chm@openerp.com-20140117173836-s5mpospo9g5xifew
This commit is contained in:
Christophe Matthieu 2014-01-17 18:38:36 +01:00
parent cf79ae7635
commit 379f3fa3ff
7 changed files with 144 additions and 147 deletions

View File

@ -32,6 +32,7 @@
<record id="delivery_carrier" model="delivery.carrier">
<field name="name">The Poste</field>
<field name="normal_price">20</field>
<field name="partner_id" ref="res_partner_23"/>
<field name="product_id" ref="product_product_delivery"/>
</record>

View File

@ -308,74 +308,6 @@ class Ecommerce(http.Controller):
return request.redirect("/shop/product/%s/?enable_editor=1" % product.product_tmpl_id.id)
def add_product_to_cart(self, product_id=0, order_line_id=0, number=1, set_number=-1):
order_line_obj = request.registry.get('sale.order.line')
order_obj = request.registry.get('sale.order')
order = self.get_order()
if not order:
order = request.registry['website'].ecommerce_get_new_order(request.cr, request.uid, context=request.context)
request.context = dict(request.context, pricelist=self.get_pricelist())
# set order_line_id and product_id
if order_line_id:
order_line = None
for line in order.order_line:
if line.id == order_line_id:
order_line = line
break
if order_line:
product_id = order_line.product_id.id
else:
order_line_id = None
else:
order_line_ids = order_line_obj.search(
request.cr, SUPERUSER_ID,
[('order_id', '=', order.id), ('product_id', '=', product_id)], context=request.context)
if order_line_ids:
order_line_id = order_line_ids[0]
if not order_line_id and not product_id:
return 0
# values initialisation
quantity = 0
values = {}
if order_line_id:
order_line_val = order_line_obj.read(request.cr, SUPERUSER_ID, [order_line_id], [], context=request.context)[0]
if not product_id:
product_id = order_line_val['product_id'][0]
if set_number >= 0:
quantity = set_number
else:
quantity = order_line_val['product_uom_qty'] + number
if quantity < 0:
quantity = 0
order_line_ids = [order_line_id]
else:
fields = [k for k, v in order_line_obj._columns.items()]
values = order_line_obj.default_get(request.cr, SUPERUSER_ID, fields, context=request.context)
quantity = 1
order_line_ids = []
# change and record value
vals = order_line_obj._recalculate_product_values(request.cr, request.uid, order_line_ids, product_id, context=request.context)
values.update(vals)
values['product_uom_qty'] = quantity
values['product_id'] = product_id
values['order_id'] = order.id
if order_line_id:
order_line_obj.write(request.cr, SUPERUSER_ID, [order_line_id], values, context=request.context)
if not quantity:
order_line_obj.unlink(request.cr, SUPERUSER_ID, [order_line_id], context=request.context)
else:
order_line_id = order_line_obj.create(request.cr, SUPERUSER_ID, values, context=request.context)
order_obj.write(request.cr, SUPERUSER_ID, [order.id], {'order_line': [(4, order_line_id)]}, context=request.context)
return quantity
@website.route(['/shop/mycart/'], type='http', auth="public", multilang=True)
def mycart(self, **post):
cr, uid, context = request.cr, request.uid, request.context
@ -413,17 +345,23 @@ class Ecommerce(http.Controller):
@website.route(['/shop/add_cart/'], type='http', auth="public", multilang=True, methods=['POST'])
def add_cart(self, product_id, remove=None, **kw):
self.add_product_to_cart(product_id=int(product_id))
request.registry['website']._ecommerce_add_product_to_cart(request.cr, request.uid,
product_id=int(product_id),
context=request.context)
return request.redirect("/shop/mycart/")
@website.route(['/shop/change_cart/<int:order_line_id>/'], type='http', auth="public", multilang=True)
def add_cart_order_line(self, order_line_id=None, remove=None, **kw):
self.add_product_to_cart(order_line_id=order_line_id, number=(remove and -1 or 1))
request.registry['website']._ecommerce_add_product_to_cart(request.cr, request.uid,
order_line_id=order_line_id, number=(remove and -1 or 1),
context=request.context)
return request.redirect("/shop/mycart/")
@website.route(['/shop/add_cart_json/'], type='json', auth="public")
def add_cart_json(self, product_id=None, order_line_id=None, remove=None):
quantity = self.add_product_to_cart(product_id=product_id, order_line_id=order_line_id, number=(remove and -1 or 1))
quantity = request.registry['website']._ecommerce_add_product_to_cart(request.cr, request.uid,
product_id=product_id, order_line_id=order_line_id, number=(remove and -1 or 1),
context=request.context)
order = self.get_order()
return [quantity,
order.get_number_of_products(),
@ -432,7 +370,9 @@ class Ecommerce(http.Controller):
@website.route(['/shop/set_cart_json/'], type='json', auth="public")
def set_cart_json(self, path=None, product_id=None, order_line_id=None, set_number=0, json=None):
return self.add_product_to_cart(product_id=product_id, order_line_id=order_line_id, set_number=set_number)
return request.registry['website']._ecommerce_add_product_to_cart(request.cr, request.uid,
product_id=product_id, order_line_id=order_line_id, set_number=set_number,
context=request.context)
@website.route(['/shop/checkout/'], type='http', auth="public", multilang=True)
def checkout(self, **post):

View File

@ -57,6 +57,73 @@ class Website(orm.Model):
# Ecommerce quotation management
# ************************************************************
def _ecommerce_add_product_to_cart(self, cr, uid, product_id=0, order_line_id=0, number=1, set_number=-1, context=None):
order = self.ecommerce_get_current_order(cr, uid, context=context)
if not order:
order = self.ecommerce_get_new_order(cr, uid, context=context)
order_line_obj = self.pool.get('sale.order.line')
order_obj = self.pool.get('sale.order')
context = dict(context, pricelist=self.ecommerce_get_pricelist_id(cr, uid, None, context=context))
# set order_line_id and product_id
if order_line_id:
order_line = None
for line in order.order_line:
if line.id == order_line_id:
order_line = line
break
if order_line:
product_id = order_line.product_id.id
else:
order_line_id = None
else:
order_line_ids = order_line_obj.search(cr, SUPERUSER_ID,
[('order_id', '=', order.id), ('product_id', '=', product_id)], context=context)
if order_line_ids:
order_line_id = order_line_ids[0]
if not order_line_id and not product_id:
return 0
# values initialisation
quantity = 0
values = {}
if order_line_id:
order_line_val = order_line_obj.read(cr, SUPERUSER_ID, [order_line_id], [], context=context)[0]
if not product_id:
product_id = order_line_val['product_id'][0]
if set_number >= 0:
quantity = set_number
else:
quantity = order_line_val['product_uom_qty'] + number
if quantity < 0:
quantity = 0
order_line_ids = [order_line_id]
else:
fields = [k for k, v in order_line_obj._columns.items()]
values = order_line_obj.default_get(cr, SUPERUSER_ID, fields, context=context)
quantity = 1
order_line_ids = []
# change and record value
vals = order_line_obj._recalculate_product_values(cr, uid, order_line_ids, product_id, context=context)
values.update(vals)
values['product_uom_qty'] = quantity
values['product_id'] = product_id
values['order_id'] = order.id
if order_line_id:
order_line_obj.write(cr, SUPERUSER_ID, [order_line_id], values, context=context)
if not quantity:
order_line_obj.unlink(cr, SUPERUSER_ID, [order_line_id], context=context)
else:
order_line_id = order_line_obj.create(cr, SUPERUSER_ID, values, context=context)
order_obj.write(cr, SUPERUSER_ID, [order.id], {'order_line': [(4, order_line_id)]}, context=context)
return quantity
def _ecommerce_get_quotation_values(self, cr, uid, context=None):
""" Generate the values for a new ecommerce quotation. """
SaleOrder = self.pool.get('sale.order')

View File

@ -55,7 +55,7 @@ $(document).ready(function () {
}
set_my_cart_quantity(data[1]);
$link.parents(".input-group:first").find(".js_quantity").val(data[0]);
$('[data-oe-model="sale.order"][data-oe-field="amount_total"]').replaceWith(data[3]);
$('#mycart_total').replaceWith(data[3]);
});
return false;
});

View File

@ -531,32 +531,7 @@
</tr>
</tbody>
</table>
<table class='pull-right mb16' id="mycart_total" t-if="website_sale_order">
<colgroup>
<col width="100"/>
<col width="120"/>
</colgroup>
<thead>
<tr style="border-top: 1px solid #000" id="order_total">
<th><h3>Total:</h3></th>
<th class="text-right">
<h3><span t-field="website_sale_order.amount_total" t-field-options='{
"widget": "monetary",
"display_currency": "website.pricelist_id.currency_id"
}'/></h3>
</th>
</tr>
<tr class="text-muted" id="order_total_taxes">
<td><abbr title="Taxes may be updated after providing shipping address">Taxes:</abbr></td>
<td class="text-right">
<span t-field="website_sale_order.amount_tax" t-field-options='{
"widget": "monetary",
"display_currency": "website.pricelist_id.currency_id"
}'/>
</td>
</tr>
</thead>
</table>
<t t-call="website_sale.total"/>
<div class="clearfix"/>
<a href="/shop" class="btn btn-default mb32"><span class="fa fa-long-arrow-left"/> Continue Shopping</a>
<a t-if="website_sale_order and website_sale_order.website_order_line" href="/shop/checkout/" class="btn btn-primary pull-right mb32">Process Checkout <span class="fa fa-long-arrow-right"/></a>
@ -933,30 +908,7 @@
</tr>
</tbody>
</table>
<table class='pull-right mb16' id="mycart_total">
<colgroup>
<col width="100"/>
<col width="120"/>
</colgroup>
<thead>
<tr style="border-top: 1px solid #000" id="order_total">
<th><h3>Total:</h3></th>
<th class="text-right">
<h3><span t-field="website_sale_order.amount_total" t-field-options='{
"widget": "monetary",
"display_currency": "website.pricelist_id.currency_id"
}'/></h3>
</th>
</tr>
<tr class="text-muted" id="order_total_taxes">
<td>Taxes:</td>
<td class="text-right"><span t-field="website_sale_order.amount_tax" t-field-options='{
"widget": "monetary",
"display_currency": "website.pricelist_id.currency_id"
}'/></td>
</tr>
</thead>
</table>
<t t-call="website_sale.total"/>
<div class="clearfix"/>
<div class="oe_structure"/>
</div>
@ -1063,5 +1015,36 @@
</t>
</template>
<!-- Page Shop my cart and payment total -->
<template id="total">
<table class='pull-right mb16' id="mycart_total" t-if="website_sale_order">
<colgroup>
<col width="100"/>
<col width="120"/>
</colgroup>
<thead>
<tr style="border-top: 1px solid #000" id="order_total">
<th><h3>Total:</h3></th>
<th class="text-right">
<h3><span t-field="website_sale_order.amount_total" t-field-options='{
"widget": "monetary",
"display_currency": "website.pricelist_id.currency_id"
}'/></h3>
</th>
</tr>
<tr class="text-muted" id="order_total_taxes">
<td><abbr title="Taxes may be updated after providing shipping address">Taxes:</abbr></td>
<td class="text-right">
<span t-field="website_sale_order.amount_tax" t-field-options='{
"widget": "monetary",
"display_currency": "website.pricelist_id.currency_id"
}'/>
</td>
</tr>
</thead>
</table>
</template>
</data>
</openerp>

View File

@ -6,17 +6,35 @@ from openerp import SUPERUSER_ID
class Website(orm.Model):
_inherit = 'website'
def _ecommerce_get_quotation_values(self, cr, uid, context=None):
""" Override the quotation values generation to add carrier_id data """
values = super(Website, self)._ecommerce_get_quotation_values(cr, uid, context=context)
DeliveryCarrier = self.pool.get('delivery.carrier')
carrier_ids = DeliveryCarrier.search(cr, uid, [(1,"=",1)], context=context, limit=1)
# By default, select the first carrier
if carrier_ids:
values['carrier_id'] = carrier_ids[0]
return values
def _ecommerce_create_quotation(self, cr, uid, context=None):
order_id = super(Website, self)._ecommerce_create_quotation(cr, uid, context=context)
self.pool['sale.order'].delivery_set(cr, SUPERUSER_ID, [order_id], context=context)
order = self.pool['sale.order'].browse(cr, SUPERUSER_ID, order_id, context=context)
self._check_carrier_quotation(cr, uid, order, context=context)
return order_id
def _ecommerce_add_product_to_cart(self, cr, uid, product_id=0, order_line_id=0, number=1, set_number=-1, context=None):
quantity = super(Website, self)._ecommerce_add_product_to_cart(cr, uid,
product_id=product_id, order_line_id=order_line_id, number=number, set_number=set_number,
context=context)
order = self.ecommerce_get_current_order(cr, uid, context=context)
return self._check_carrier_quotation(cr, uid, order, context=context) and quantity or None
def _check_carrier_quotation(self, cr, uid, order, context=None):
# check to add or remove carrier_id
carrier_id = False
for line in order.website_order_line:
if line.product_id.type != "service":
carrier_id = True
break
if not carrier_id:
order.write({'carrier_id': None}, context=context)
self.pool['sale.order']._delivery_unset(cr, SUPERUSER_ID, order, context=context)
return None
elif not order.carrier_id:
carrier_ids = self.pool.get('delivery.carrier').search(cr, uid, [], context=context)
order.write({'carrier_id': carrier_ids[0]}, context=context)
order.delivery_set(context=context)
return None
return carrier_id

View File

@ -2,7 +2,7 @@
<openerp>
<data>
<template id="mycart_delivery" name="Delivery Costs" inherit_id="website_sale.mycart">
<template id="mycart_delivery" name="Delivery Costs" inherit_id="website_sale.total">
<xpath expr="//tr[@id='order_total_taxes']" position="after">
<tr class="text-muted" id="order_delivery">
<td><abbr title="Delivery will be updated after choosing a new delivery method">Delivery:</abbr></td>
@ -21,18 +21,6 @@
<script type="text/javascript" src="/website_sale_delivery/static/src/js/website_sale_delivery.js"></script>
</xpath>
<xpath expr="//tr[@id='order_total_taxes']" position="after">
<tr class="text-muted" id="order_delivery">
<td><abbr title="Delivery will be updated after choosing a new delivery method">Delivery:</abbr></td>
<td class="text-right">
<span t-field="order.amount_delivery" t-field-options='{
"widget": "monetary",
"display_currency": "website.pricelist_id.currency_id"
}'/>
</td>
</tr>
</xpath>
<xpath expr="//div[@id='payment_method']" position="before">
<div t-if="deliveries" class="row" id="delivery_carrier">
<h4>Choose your Delivery Method</h4>