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 openerp.osv import fields, osv
23 from openerp.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 sales order line.'),
29 'event_type_id': fields.many2one('event.type', 'Type of Event', help='Select event types so when we use this product in sales order lines, it will filter events of this type only.'),
32 def onchange_event_ok(self, cr, uid, ids, type, event_ok, context=None):
33 return {'value': {'type': event_ok and 'service' or type != 'service' and type or False}}
36 class sale_order_line(osv.osv):
37 _inherit = 'sale.order.line'
39 'event_id': fields.many2one('event.event', 'Event',
40 help="Choose an event and it will automatically create a registration for this event."),
41 'event_ticket_id': fields.many2one('event.event.ticket', 'Event Ticket',
42 help="Choose an event ticket and it will automatically create a registration for this event ticket."),
43 #those 2 fields are used for dynamic domains and filled by onchange
44 'event_type_id': fields.related('product_id','event_type_id', type='many2one', relation="event.type", string="Event Type"),
45 'event_ok': fields.related('product_id', 'event_ok', string='event_ok', type='boolean'),
48 def product_id_change(self, cr, uid, ids,
61 fiscal_position=False,
62 flag=False, context=None):
64 check product if event type
66 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)
68 product_res = self.pool.get('product.product').browse(cr, uid, product, context=context)
69 if product_res.event_ok:
70 res['value'].update({'event_type_id': product_res.event_type_id.id, 'event_ok':product_res.event_ok})
73 def button_confirm(self, cr, uid, ids, context=None):
75 create registration with sales order
79 registration_obj = self.pool.get('event.registration')
80 for order_line in self.browse(cr, uid, ids, context=context):
81 if order_line.event_id:
83 'name': order_line.order_id.partner_invoice_id.name,
84 'partner_id': order_line.order_id.partner_id.id,
85 'nb_register': int(order_line.product_uom_qty),
86 'email': order_line.order_id.partner_id.email,
87 'phone': order_line.order_id.partner_id.phone,
88 'origin': order_line.order_id.name,
89 'event_id': order_line.event_id.id,
90 'event_ticket_id': order_line.event_ticket_id and order_line.event_ticket_id.id or None,
93 if order_line.event_ticket_id:
94 if order_line.event_ticket_id.register_avail != 9999 and dic['nb_register'] > order_line.event_ticket_id.register_avail:
95 raise osv.except_osv(_('Error!'), _('There are not enough tickets available (%s) for %s' % (order_line.event_ticket_id.register_avail, order_line.event_ticket_id.name)))
96 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)
98 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)
100 context.update({'mail_create_nolog': True})
101 registration_id = registration_obj.create(cr, uid, dic, context=context)
102 registration_obj.message_post(cr, uid, [registration_id], body=message, context=context)
103 return super(sale_order_line, self).button_confirm(cr, uid, ids, context=context)
105 def onchange_event_ticket_id(self, cr, uid, ids, event_ticket_id=False, context=None):
106 price = event_ticket_id and self.pool.get("event.event.ticket").browse(cr, uid, event_ticket_id, context=context).price or False
107 return {'value': {'price_unit': price}}
110 class event_event(osv.osv):
111 _inherit = 'event.event'
113 def _get_register_max(self, cr, uid, ids, field_name, arg, context=None):
114 result = dict.fromkeys(ids, 0)
115 for rec in self.browse(cr, uid, ids, context=context):
116 result[rec.id] = sum([ep.register_max for ep in rec.event_ticket_ids])
120 'event_ticket_ids': fields.one2many('event.event.ticket', "event_id", "Event Ticket"),
121 'organizer_id': fields.many2one('res.partner', "Organizer"),
122 'phone': fields.related('organizer_id', 'phone', type='char', string='Phone'),
123 'email': fields.related('organizer_id', 'email', type='char', string='Email'),
124 'register_max': fields.function(_get_register_max,
125 string='Maximum Registrations',
126 help="The maximum registration level is equal to the sum of the maximum registration of event ticket." +
127 "If you have too much registrations you are not able to confirm your event. (0 to ignore this rule )",
131 def check_registration_limits(self, cr, uid, ids, context=None):
132 for event in self.browse(cr, uid, ids, context=context):
133 if event.event_ticket_ids:
134 event.event_ticket_ids.check_registration_limits_before(0)
135 return super(event_event, self).check_registration_limits(cr, uid, ids, context=context)
138 class event_ticket(osv.osv):
139 _name = 'event.event.ticket'
141 def _get_register(self, cr, uid, ids, fields, args, context=None):
142 """Get Confirm or uncofirm register value.
143 @param ids: List of Event Ticket registration type's id
144 @param fields: List of function fields(register_current and register_prospect).
145 @param context: A standard dictionary for contextual values
146 @return: Dictionary of function fields value.
149 for event in self.browse(cr, uid, ids, context=context):
151 reg_open = reg_done = reg_draft =0
152 for registration in event.registration_ids:
153 if registration.state == 'open':
154 reg_open += registration.nb_register
155 elif registration.state == 'done':
156 reg_done += registration.nb_register
157 elif registration.state == 'draft':
158 reg_draft += registration.nb_register
161 if field == 'register_current':
163 elif field == 'register_attended':
165 elif field == 'register_prospect':
167 elif field == 'register_avail':
168 #the number of ticket is unlimited if the event.register_max field is not set.
169 #In that cas we arbitrary set it to 9999, it is used in the kanban view to special case the display of the 'subscribe' button
170 number = event.register_max - reg_open if event.register_max != 0 else 9999
171 res[event.id][field] = number
175 'name': fields.char('Name', size=64, required=True),
176 'event_id': fields.many2one('event.event', "Event", required=True, ondelete='cascade'),
177 'product_id': fields.many2one('product.product', 'Product', required=True, domain=[("event_type_id", "!=", False)]),
178 'registration_ids': fields.one2many('event.registration', 'event_ticket_id', 'Registrations'),
179 'deadline': fields.date("Sales End"),
180 'price': fields.float('Price'),
181 'register_max': fields.integer('Maximum Registrations'),
182 'register_current': fields.function(_get_register, string='Current Registrations', type='integer', multi='register_numbers'),
183 'register_avail': fields.function(_get_register, string='Available Registrations', type='integer', multi='register_numbers'),
184 'register_prospect': fields.function(_get_register, string='Unconfirmed Registrations', type='integer', multi='register_numbers'),
185 'register_attended': fields.function(_get_register, string='# of Participations', type='integer', multi='register_numbers'),
188 def check_registration_limits_before(self, cr, uid, ids, number, context=None):
189 for ticket in self.browse(cr, uid, ids, context=context):
190 if ticket.register_max:
191 if not ticket.register_avail:
192 raise osv.except_osv(_('Warning!'),_('No Tickets Available for "%s"' % ticket.name))
193 elif number + ticket.register_current > ticket.register_max:
194 raise osv.except_osv(_('Warning!'), _('There only %d tickets available for "%s"' % (ticket.register_avail, ticket.name)))
197 def onchange_product_id(self, cr, uid, ids, product_id=False, context=None):
198 return {'value': {'price': self.pool.get("product.product").browse(cr, uid, product_id).list_price or 0}}
201 class event_registration(osv.osv):
202 """Event Registration"""
203 _inherit= 'event.registration'
205 'event_ticket_id': fields.many2one('event.event.ticket', 'Event Ticket'),
208 def registration_open(self, cr, uid, ids, context=None):
209 """ Open Registration
211 for registration in self.browse(cr, uid, ids, context=context):
212 if registration.event_ticket_id:
213 registration.event_ticket_id.check_registration_limits_before(1)
214 return super(event_registration, self).registration_open(cr, uid, ids, context=context)