Merge branch 'master' of https://github.com/odoo/odoo
[odoo/odoo.git] / addons / membership / wizard / membership_invoice.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
6 #
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.
11 #
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.
16 #
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/>.
19 #
20 ##############################################################################
21
22 from openerp.osv import fields, osv
23 import openerp.addons.decimal_precision as dp
24
25 class membership_invoice(osv.osv_memory):
26     """Membership Invoice"""
27
28     _name = "membership.invoice"
29     _description = "Membership Invoice"
30     _columns = {
31         'product_id': fields.many2one('product.product','Membership', required=True),
32         'member_price': fields.float('Member Price', digits_compute= dp.get_precision('Product Price'), required=True),
33     }
34     def onchange_product(self, cr, uid, ids, product_id=False):
35         """This function returns value of  product's member price based on product id.
36         """
37         if not product_id:
38             return {'value': {'member_price': False}}
39         return {'value': {'member_price': self.pool.get('product.product').price_get(cr, uid, [product_id])[product_id]}}
40
41     def membership_invoice(self, cr, uid, ids, context=None):
42         mod_obj = self.pool.get('ir.model.data')
43         partner_obj = self.pool.get('res.partner')
44         datas = {}
45         if context is None:
46             context = {}
47         data = self.browse(cr, uid, ids, context=context)
48         if data:
49             data = data[0]
50             datas = {
51                 'membership_product_id': data.product_id.id,
52                 'amount': data.member_price
53             }
54         invoice_list = partner_obj.create_membership_invoice(cr, uid, context.get('active_ids', []), datas=datas, context=context)
55
56         try:
57             search_view_id = mod_obj.get_object_reference(cr, uid, 'account', 'view_account_invoice_filter')[1]
58         except ValueError:
59             search_view_id = False
60         try:
61             form_view_id = mod_obj.get_object_reference(cr, uid, 'account', 'invoice_form')[1]
62         except ValueError:
63             form_view_id = False
64
65         return  {
66             'domain': [('id', 'in', invoice_list)],
67             'name': 'Membership Invoices',
68             'view_type': 'form',
69             'view_mode': 'tree,form',
70             'res_model': 'account.invoice',
71             'type': 'ir.actions.act_window',
72             'views': [(False, 'tree'), (form_view_id, 'form')],
73             'search_view_id': search_view_id,
74         }
75
76
77 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: