1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as
9 # published by the Free Software Foundation, either version 3 of the
10 # License, or (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 ##############################################################################
22 from osv import fields, osv
23 from tools.translate import _
25 class product(osv.osv):
26 _inherit = 'product.product'
28 'event_ok': fields.boolean('Event Subscription', help='Determine if a product needs to create automatically an event registration at the confirmation of a sale order line.'),
29 'event_type_id': fields.many2one('event.type', 'Type of Event', help='Filter the list of event on this category only, in the sale order lines'),
32 def onchange_event_ok(self, cr, uid, ids, event_ok, context=None):
33 return {'value': {'type': event_ok and 'service' or False}}
37 class sale_order_line(osv.osv):
38 _inherit = 'sale.order.line'
40 'event_id': fields.many2one('event.event', 'Event', help="Choose an event and it will automatically create a registration for this event."),
41 #those 2 fields are used for dynamic domains and filled by onchange
42 'event_type_id': fields.related('event_type_id', type='many2one', relation="event.type", string="Event Type"),
43 'event_ok': fields.related('event_ok', string='event_ok', type='boolean'),
46 def product_id_change(self, cr, uid, ids,
58 fiscal_position=False,
59 flag=False, context=None):
61 check product if event type
63 res = super(sale_order_line,self).product_id_change(cr, uid, ids, pricelist, product, qty=qty, uom=uom, qty_uos=qty_uos, uos=uos, name=name, partner_id=partner_id, lang=lang, update_tax=update_tax, date_order=date_order, packaging=packaging, fiscal_position=fiscal_position, flag=flag, context=context)
65 product_res = self.pool.get('product.product').browse(cr, uid, product, context=context)
66 if product_res.event_ok:
67 res['value'].update({'event_type_id': product_res.event_type_id.id, 'event_ok':product_res.event_ok})
70 def button_confirm(self, cr, uid, ids, context=None):
72 create registration with sale order
74 registration_obj = self.pool.get('event.registration')
75 sale_obj = self.pool.get('sale.order')
76 for order_line in self.browse(cr, uid, ids, context=context):
77 if order_line.event_id.id:
79 'name': order_line.order_id.partner_invoice_id.name,
80 'partner_id': order_line.order_id.partner_id.id,
81 'nb_register': int(order_line.product_uom_qty),
82 'email': order_line.order_id.partner_id.email,
83 'phone': order_line.order_id.partner_id.phone,
84 'street': order_line.order_id.partner_invoice_id.street,
85 'city': order_line.order_id.partner_invoice_id.city,
86 'origin': order_line.order_id.name,
87 'event_id': order_line.event_id.id,
89 registration_id = registration_obj.create(cr, uid, dic, context=context)
90 message = _("The registration %s has been created from the Sale Order %s.") % (registration_id, order_line.order_id.name)
91 registration_obj.message_post(cr, uid, [registration_id], body=message, context=context)
92 return super(sale_order_line, self).button_confirm(cr, uid, ids, context=context)