[CLEAN] event_sale: lint / cleaned module, migrated event models to the new API.
[odoo/odoo.git] / addons / event_sale / models / sale_order.py
1 # -*- coding: utf-8 -*-
2
3 from openerp.osv import fields, osv
4 from openerp.tools.translate import _
5
6
7 class sale_order_line(osv.osv):
8     _inherit = 'sale.order.line'
9     _columns = {
10         'event_id': fields.many2one(
11             'event.event', 'Event',
12             help="Choose an event and it will automatically create a registration for this event."),
13         'event_ticket_id': fields.many2one(
14             'event.event.ticket', 'Event Ticket',
15             help="Choose an event ticket and it will automatically create a registration for this event ticket."),
16         # those 2 fields are used for dynamic domains and filled by onchange
17         # TDE: really necessary ? ...
18         'event_type_id': fields.related('product_id', 'event_type_id', type='many2one', relation="event.type", string="Event Type"),
19         'event_ok': fields.related('product_id', 'event_ok', string='event_ok', type='boolean'),
20     }
21
22     def product_id_change(self, cr, uid, ids, pricelist, product, qty=0, uom=False,
23                           qty_uos=0, uos=False, name='', partner_id=False, lang=False,
24                           update_tax=True, date_order=False, packaging=False,
25                           fiscal_position=False, flag=False, context=None):
26         """ check product if event type """
27         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)
28         if product:
29             product_res = self.pool.get('product.product').browse(cr, uid, product, context=context)
30             if product_res.event_ok:
31                 res['value'].update(event_type_id=product_res.event_type_id.id,
32                                     event_ok=product_res.event_ok)
33             else:
34                 res['value'].update(event_type_id=False,
35                                     event_ok=False)
36         return res
37
38     def button_confirm(self, cr, uid, ids, context=None):
39         '''
40         create registration with sales order
41         '''
42         context = dict(context or {})
43         registration_obj = self.pool.get('event.registration')
44         for order_line in self.browse(cr, uid, ids, context=context):
45             if order_line.event_id:
46                 dic = {
47                     'name': order_line.order_id.partner_invoice_id.name,
48                     'partner_id': order_line.order_id.partner_id.id,
49                     'nb_register': int(order_line.product_uom_qty),
50                     'email': order_line.order_id.partner_id.email,
51                     'phone': order_line.order_id.partner_id.phone,
52                     'origin': order_line.order_id.name,
53                     'event_id': order_line.event_id.id,
54                     'event_ticket_id': order_line.event_ticket_id and order_line.event_ticket_id.id or None,
55                 }
56
57                 if order_line.event_ticket_id:
58                     message = _("The registration has been created for event <i>%s</i> with the ticket <i>%s</i> from the Sale Order %s. ") % (order_line.event_id.name, order_line.event_ticket_id.name, order_line.order_id.name)
59                 else:
60                     message = _("The registration has been created for event <i>%s</i> from the Sale Order %s. ") % (order_line.event_id.name, order_line.order_id.name)
61
62                 context.update({'mail_create_nolog': True})
63                 registration_id = registration_obj.create(cr, uid, dic, context=context)
64                 registration_obj.message_post(cr, uid, [registration_id], body=message, context=context)
65         return super(sale_order_line, self).button_confirm(cr, uid, ids, context=context)
66
67     def onchange_event_ticket_id(self, cr, uid, ids, event_ticket_id=False, context=None):
68         price = event_ticket_id and self.pool["event.event.ticket"].browse(cr, uid, event_ticket_id, context=context).price or False
69         return {'value': {'price_unit': price}}