[FIX] website_event_sale: display the discounted price in website.
authorChristophe Matthieu <chm@odoo.com>
Fri, 24 Oct 2014 12:16:16 +0000 (14:16 +0200)
committerDenis Ledoux <dle@odoo.com>
Fri, 24 Oct 2014 12:16:16 +0000 (14:16 +0200)
Add price_reduce compute method on ticket and sale order line

addons/event_sale/event_sale.py
addons/sale/sale.py
addons/website_event_sale/controllers/main.py
addons/website_event_sale/models/sale_order.py
addons/website_event_sale/views/website_event_sale.xml
addons/website_sale/controllers/main.py
addons/website_sale/views/templates.xml

index 2d947be..f4549e9 100644 (file)
@@ -23,6 +23,7 @@ from openerp import api
 from openerp.fields import Integer, One2many, Html
 from openerp.osv import fields, osv
 from openerp.tools.translate import _
+import openerp.addons.decimal_precision as dp
 
 class product_template(osv.osv):
     _inherit = 'product.template'
@@ -180,7 +181,14 @@ class event_ticket(osv.osv):
         current_date = fields.date.context_today(self, cr, uid, context=context)
         return {ticket.id: ticket.deadline and ticket.deadline < current_date
                       for ticket in self.browse(cr, uid, ids, context=context)}
-        
+
+    def _get_price_reduce(self, cr, uid, ids, field_name, arg, context=None):
+        res = dict.fromkeys(ids, 0.0)
+        for ticket in self.browse(cr, uid, ids, context=context):
+            product = ticket.product_id
+            discount = product.lst_price and (product.lst_price - product.price) / product.lst_price or 0.0
+            res[ticket.id] = (1.0-discount) * ticket.price
+        return res
 
     _columns = {
         'name': fields.char('Name', required=True, translate=True),
@@ -189,7 +197,8 @@ class event_ticket(osv.osv):
         'registration_ids': fields.one2many('event.registration', 'event_ticket_id', 'Registrations'),
         'deadline': fields.date("Sales End"),
         'is_expired': fields.function(_is_expired, type='boolean', string='Is Expired'),
-        'price': fields.float('Price'),
+        'price': fields.float('Price', digits_compute=dp.get_precision('Product Price')),
+        'price_reduce': fields.function(_get_price_reduce, type='float', string='Price Reduce', digits_compute=dp.get_precision('Product Price')),
         'seats_max': fields.integer('Maximum Available Seats', oldname='register_max', help="You can for each event define a maximum registration level. If you have too much registrations you are not able to confirm your event. (put 0 to ignore this rule )"),
         'seats_reserved': fields.function(_get_seats, string='Reserved Seats', type='integer', multi='seats_reserved'),
         'seats_available': fields.function(_get_seats, string='Available Seats', type='integer', multi='seats_reserved'),
index e50346e..2273a88 100644 (file)
@@ -843,6 +843,11 @@ class sale_order_line(osv.osv):
                                     WHERE rel.invoice_id = ANY(%s)""", (list(ids),))
         return [i[0] for i in cr.fetchall()]
 
+    def _get_price_reduce(self, cr, uid, ids, field_name, arg, context=None):
+        res = dict.fromkeys(ids, 0.0)
+        for line in self.browse(cr, uid, ids, context=context):
+            res[line.id] = line.price_subtotal / line.product_uom_qty
+        return res
 
     _name = 'sale.order.line'
     _description = 'Sales Order Line'
@@ -859,6 +864,7 @@ class sale_order_line(osv.osv):
             }),
         'price_unit': fields.float('Unit Price', required=True, digits_compute= dp.get_precision('Product Price'), readonly=True, states={'draft': [('readonly', False)]}),
         'price_subtotal': fields.function(_amount_line, string='Subtotal', digits_compute= dp.get_precision('Account')),
+        'price_reduce': fields.function(_get_price_reduce, type='float', string='Price Reduce', digits_compute=dp.get_precision('Product Price')),
         'tax_id': fields.many2many('account.tax', 'sale_order_tax', 'order_line_id', 'tax_id', 'Taxes', readonly=True, states={'draft': [('readonly', False)]}),
         'address_allotment_id': fields.many2one('res.partner', 'Allotment Partner',help="A partner to whom the particular product needs to be allotted."),
         'product_uom_qty': fields.float('Quantity', digits_compute= dp.get_precision('Product UoS'), required=True, readonly=True, states={'draft': [('readonly', False)]}),
index 063e60c..f57f0c8 100644 (file)
@@ -23,11 +23,22 @@ from openerp import SUPERUSER_ID
 from openerp.addons.web import http
 from openerp.addons.web.http import request
 from openerp.addons.website_event.controllers.main import website_event
+from openerp.addons.website_sale.controllers.main import get_pricelist
 from openerp.tools.translate import _
 
 
 class website_event(website_event):
 
+    @http.route(['/event/<model("event.event"):event>/register'], type='http', auth="public", website=True)
+    def event_register(self, event, **post):
+        pricelist_id = int(get_pricelist())
+        values = {
+            'event': event.with_context(pricelist=pricelist_id),
+            'main_object': event.with_context(pricelist=pricelist_id),
+            'range': range,
+        }
+        return request.website.render("website_event.event_description_full", values)
+
     @http.route(['/event/cart/update'], type='http', auth="public", methods=['POST'], website=True)
     def cart_update(self, event_id, **post):
         cr, uid, context = request.cr, request.uid, request.context
index 450fcdd..127efc9 100644 (file)
@@ -2,6 +2,7 @@
 from openerp import SUPERUSER_ID
 from openerp.osv import osv, fields
 from openerp.tools.translate import _
+import openerp.addons.decimal_precision as dp
 
 # defined for access rules
 class sale_order(osv.Model):
index cf68e99..667b778 100644 (file)
     </xpath>
 </template>
 
-<template id="cart" inherit_id="website_sale.cart" name="My Cart Event's Price">
-    <xpath expr="//td[@name='price']/t" position="attributes">
-        <attribute name="t-if">abs(line.product_id.lst_price - line.price_unit) &gt; 0.2 and not line.product_id.event_ok</attribute>
-    </xpath>
-</template>
-
 <template id="event_description_full" inherit_id="website_event.event_description_full" customize_show="True" name="Event's Ticket form">
     <xpath expr="//div[@t-field='event.description']" position="before">
         <form t-attf-action="/event/cart/update?event_id=#{ event.id }" method="post" t-if="event.event_ticket_ids">
                         </td>
                         <td><span itemprop="priceValidUntil" t-field="ticket.deadline"/></td>
                         <td>
-                            <t t-if="ticket.price or editable"><span t-field="ticket.price" t-field-options='{
+                            <t t-if="ticket.price or editable">
+                              <t t-if="(ticket.price-ticket.price_reduce) &gt; 1">
+                                <del class="text-danger" style="white-space: nowrap;" t-field="ticket.price" t-field-options='{
+                                  "widget": "monetary",
+                                  "from_currency": "website.currency_id",
+                                  "display_currency": "user_id.partner_id.property_product_pricelist.currency_id"
+                                }'/>&amp;nbsp;
+                              </t>
+                              <span t-field="ticket.price_reduce" t-field-options='{
                                    "widget": "monetary",
                                    "display_currency": "website.pricelist_id.currency_id"
                               }'/>
index df3ab9b..6399d55 100644 (file)
@@ -102,17 +102,20 @@ class QueryURL(object):
         return path
 
 
+def get_pricelist():
+    cr, uid, context, pool = request.cr, request.uid, request.context, request.registry
+    sale_order = context.get('sale_order')
+    if sale_order:
+        pricelist = sale_order.pricelist_id
+    else:
+        partner = pool['res.users'].browse(cr, SUPERUSER_ID, uid, context=context).partner_id
+        pricelist = partner.property_product_pricelist
+    return pricelist
+
 class website_sale(http.Controller):
 
     def get_pricelist(self):
-        cr, uid, context, pool = request.cr, request.uid, request.context, request.registry
-        sale_order = context.get('sale_order')
-        if sale_order:
-            pricelist = sale_order.pricelist_id
-        else:
-            partner = pool['res.users'].browse(cr, SUPERUSER_ID, uid, context=context).partner_id
-            pricelist = partner.property_product_pricelist
-        return pricelist
+        return get_pricelist()
 
     def get_attribute_value_ids(self, product):
         cr, uid, context, pool = request.cr, request.uid, request.context, request.registry
index 12c56ac..ef216be 100644 (file)
                                   <div class="text-muted" t-field="line.name"/>
                               </td>
                               <td class="text-center" name="price">
-                                <t t-if="(compute_currency(line.product_id.lst_price) - line.price_unit) &gt; 0.01">
+                                <t t-if="(line.price_unit - line.price_reduce) &gt; 1">
                                   <del class="text-danger" style="white-space: nowrap;"
-                                    t-field="line.product_id.lst_price" t-field-options='{
+                                    t-field="line.price_unit" t-field-options='{
                                         "widget": "monetary",
                                         "from_currency": "website.currency_id",
                                         "display_currency": "user_id.partner_id.property_product_pricelist.currency_id"
                                     }'/>&amp;nbsp;
                                 </t>
-                                <span t-field="line.price_unit" style="white-space: nowrap;" t-field-options='{
+                                <span t-field="line.price_reduce" style="white-space: nowrap;" t-field-options='{
                                      "widget": "monetary",
                                      "display_currency": "user_id.partner_id.property_product_pricelist.currency_id"
                                  }'/>