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 ##############################################################################
23 from osv import fields, osv
24 from tools.translate import _
26 class product(osv.osv):
27 _inherit = 'product.product'
29 '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.'),
30 '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'),
33 def onchange_event_ok(self, cr, uid, ids, event_ok, context=None):
34 return {'value': {'type': event_ok and 'service' or False}}
39 class sale_order_line(osv.osv):
40 _inherit = 'sale.order.line'
42 'event_id': fields.many2one('event.event', 'Event', help="Choose an event and it will authomaticaly create a registration for this event"),
43 #those 2 fields are used for dynamic domains and filled by onchange
44 'event_type_id': fields.related('event_type_id', type='many2one', relation="event.type", string="Event Type"),
45 'event_ok': fields.related('event_ok', string='event_ok', type='boolean'),
48 def product_id_change(self, cr, uid, ids,
60 fiscal_position=False,
61 flag=False, context=None):
63 check product if event type
65 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)
67 product_res = self.pool.get('product.product').browse(cr, uid, product, context=context)
68 if product_res.event_ok:
69 res['value'].update({'event_type_id': product_res.event_type_id.id, 'event_ok':product_res.event_ok})
72 def button_confirm(self, cr, uid, ids, context=None):
74 create registration with sale order
76 registration_obj = self.pool.get('event.registration')
77 sale_obj = self.pool.get('sale.order')
78 for order_line in self.browse(cr, uid, ids, context=context):
79 if order_line.event_id.id:
81 'name': order_line.order_id.partner_invoice_id.name,
82 'partner_id': order_line.order_id.partner_id.id,
83 'contact_id': order_line.order_id.partner_invoice_id.id,
84 'nb_register': int(order_line.product_uom_qty),
85 'email': order_line.order_id.partner_id.email,
86 'phone': order_line.order_id.partner_id.phone,
87 'street': order_line.order_id.partner_invoice_id.street,
88 'city': order_line.order_id.partner_invoice_id.city,
89 'origin': order_line.order_id.name,
90 'event_id': order_line.event_id.id,
92 registration_id = registration_obj.create(cr, uid, dic, context=context)
93 message = _("The registration %s has been created from the Sale Order %s.") % (registration_id, order_line.order_id.name)
94 registration_obj.log(cr, uid, registration_id, message)
95 return super(sale_order_line, self).button_confirm(cr, uid, ids, context=context)
97 class event_event(osv.osv):
98 _inherit = 'event.event'
100 'event_item_ids': fields.one2many('event.items','event_id', 'Event Items'),
103 def order_now(self, cr, uid, ids, context=None):
105 register_pool = self.pool.get('event.registration')
106 sale_order_line_pool = self.pool.get('sale.order.line')
107 sale_order = self.pool.get('sale.order')
108 partner_pool = self.pool.get("res.partner")
109 data_obj = self.pool.get("ir.model.data")
110 prod_pricelist_obj = self.pool.get('product.pricelist')
111 user = self.pool.get("res.users").browse(cr, uid, uid, context=context)
112 partner_ids = partner_pool.search(cr, uid, [('name', '=', user.name), ('email', '=', user.user_email)])
114 # partner not found create new partner.....
115 new_partner_id = partner_pool.create(cr, uid, {'name': user.name, 'email': user.user_email})
116 partner_invoice_id = partner_pool.address_get(cr, uid, [new_partner_id], ['invoice'])['invoice']
117 partner_shipping_id = partner_pool.address_get(cr, uid, [new_partner_id], ['delivery'])['delivery']
119 new_partner_id = partner_ids[0]
120 partner_invoice_id = partner_pool.address_get(cr, uid, [new_partner_id], ['invoice'])['invoice']
121 partner_shipping_id = partner_pool.address_get(cr, uid, [new_partner_id], ['delivery'])['delivery']
122 for event_id in self.browse(cr, uid, ids, context=context):
123 price_list = prod_pricelist_obj.search(cr,uid,[],context=context)[0]
124 new_sale_id = sale_order.create(cr, uid, {
125 'date_order': event_id.date_begin,
126 'pricelist_id': price_list,
127 'partner_id': new_partner_id,
128 'partner_invoice_id': partner_invoice_id,
129 'partner_shipping_id': partner_shipping_id
131 if event_id.event_item_ids:
132 for items in event_id.event_item_ids:
133 product = items.product_id.id
134 sale_order_line = sale_order_line_pool.create(cr, uid, {
135 'order_id': new_sale_id,
136 'name': items.product_id.name,
137 'product_uom_qty': items.qty,
138 'product_id': items.product_id.id,
139 'price_unit': items.price,
140 'date_planned': items.sales_end_date,
142 view_id = data_obj._get_id(cr, uid, 'sale', 'view_order_form')
144 res_id = data_obj.browse(cr, uid, view_id, context=context).res_id
146 'name': _('Quotation'),
149 'res_model': 'sale.order',
150 'res_id': new_sale_id,
151 'views':[(res_id,'form'), (False, 'tree')],
152 'type': 'ir.actions.act_window',
158 def subscribe_to_event(self, cr, uid, ids, context=None):
160 data_obj = self.pool.get("ir.model.data")
161 view_id = data_obj._get_id(cr, uid, 'event', 'view_event_page')
163 res_id = data_obj.browse(cr, uid, view_id, context=context).res_id
165 'name': _('Event Page'),
168 'views':[(res_id,'form'), (False, 'tree')],
169 'res_model': 'event.event',
171 'type': 'ir.actions.act_window',
177 class event_items(osv.osv):
178 _name = "event.items"
180 'product_id': fields.many2one('product.product', 'Product', required=True),
181 'price': fields.integer('Price'),
182 'max_qty': fields.integer('Max.Quantity available'),
183 'qty': fields.integer('QTY', type ='integer'),
184 'uom_id': fields.many2one('product.uom', 'UoM'),
185 'discount': fields.integer('Discount'),
186 'event_id': fields.many2one('event.event', 'Event'),
187 'sales_end_date': fields.date('Sales End')
190 def onchange_product_id(self, cr, uid, ids, product, context=None):
191 product_obj = self.pool.get('product.product')
194 return {'value': data}
195 price = product_obj.browse(cr, uid, product).list_price
196 uom = product_obj.browse(cr, uid, product).uom_id.id
197 data['price'] = price
199 return {'value': data}