2012-02-07 14:10:34 +00:00
# -*- coding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
from osv import fields , osv
2012-02-27 17:10:43 +00:00
from tools . translate import _
2012-02-07 14:10:34 +00:00
class product ( osv . osv ) :
2012-02-27 17:10:43 +00:00
_inherit = ' product.product '
_columns = {
2012-02-28 10:59:06 +00:00
' 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. ' ) ,
' 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 ' ) ,
2012-02-07 14:10:34 +00:00
}
2012-02-28 10:59:06 +00:00
def onchange_event_ok ( self , cr , uid , ids , event_ok , context = None ) :
return { ' value ' : { ' type ' : event_ok and ' service ' or False } }
2012-02-07 14:10:34 +00:00
product ( )
2012-02-08 13:20:40 +00:00
class sale_order_line ( osv . osv ) :
2012-02-27 17:10:43 +00:00
_inherit = ' sale.order.line '
_columns = {
2012-07-13 10:17:51 +00:00
' event_id ' : fields . many2one ( ' event.event ' , ' Event ' , help = " Choose an event and it will automatically create a registration for this event. " ) ,
2012-02-28 11:59:04 +00:00
#those 2 fields are used for dynamic domains and filled by onchange
2012-02-27 17:10:43 +00:00
' event_type_id ' : fields . related ( ' event_type_id ' , type = ' many2one ' , relation = " event.type " , string = " Event Type " ) ,
' event_ok ' : fields . related ( ' event_ok ' , string = ' event_ok ' , type = ' boolean ' ) ,
2012-02-08 13:20:40 +00:00
}
2012-02-09 08:39:58 +00:00
2012-02-10 13:11:21 +00:00
def product_id_change ( self , cr , uid , ids ,
pricelist ,
product , qty = 0 ,
uom = False ,
qty_uos = 0 ,
uos = False ,
name = ' ' ,
partner_id = False ,
lang = False ,
update_tax = True ,
date_order = False ,
packaging = False ,
fiscal_position = False ,
flag = False , context = None ) :
2012-02-09 14:22:54 +00:00
"""
2012-02-27 17:10:43 +00:00
check product if event type
2012-02-09 14:22:54 +00:00
"""
2012-02-27 17:10:43 +00:00
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 )
2012-02-09 08:39:58 +00:00
if product :
2012-02-27 17:10:43 +00:00
product_res = self . pool . get ( ' product.product ' ) . browse ( cr , uid , product , context = context )
2012-02-28 16:38:23 +00:00
if product_res . event_ok :
2012-02-27 17:10:43 +00:00
res [ ' value ' ] . update ( { ' event_type_id ' : product_res . event_type_id . id , ' event_ok ' : product_res . event_ok } )
2012-02-10 13:11:21 +00:00
return res
2012-02-08 13:20:40 +00:00
2012-02-27 17:10:43 +00:00
def button_confirm ( self , cr , uid , ids , context = None ) :
2012-02-09 14:22:54 +00:00
'''
create registration with sale order
'''
2012-02-27 17:10:43 +00:00
registration_obj = self . pool . get ( ' event.registration ' )
sale_obj = self . pool . get ( ' sale.order ' )
for order_line in self . browse ( cr , uid , ids , context = context ) :
2012-02-28 16:38:23 +00:00
if order_line . event_id . id :
2012-02-10 13:11:21 +00:00
dic = {
2012-02-27 17:10:43 +00:00
' name ' : order_line . order_id . partner_invoice_id . name ,
' partner_id ' : order_line . order_id . partner_id . id ,
' nb_register ' : int ( order_line . product_uom_qty ) ,
' email ' : order_line . order_id . partner_id . email ,
' phone ' : order_line . order_id . partner_id . phone ,
' street ' : order_line . order_id . partner_invoice_id . street ,
' city ' : order_line . order_id . partner_invoice_id . city ,
' origin ' : order_line . order_id . name ,
2012-02-28 16:38:23 +00:00
' event_id ' : order_line . event_id . id ,
2012-02-10 13:11:21 +00:00
}
2012-02-27 17:10:43 +00:00
registration_id = registration_obj . create ( cr , uid , dic , context = context )
message = _ ( " The registration %s has been created from the Sale Order %s . " ) % ( registration_id , order_line . order_id . name )
2012-06-13 12:08:38 +00:00
registration_obj . message_append_note ( cr , uid , [ registration_id ] , body = message , context = context )
2012-02-27 17:10:43 +00:00
return super ( sale_order_line , self ) . button_confirm ( cr , uid , ids , context = context )