[REF] purchase: search view of purchase order and form view of merge order wizard
[odoo/odoo.git] / addons / base_setup / todo.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 from operator import itemgetter
22
23 from osv import osv, fields
24 import netsvc
25 import tools
26
27 class base_setup_company(osv.osv_memory):
28     """
29     """
30     _name = 'base.setup.company'
31     _inherit = 'res.config'
32     logger = netsvc.Logger()
33
34     def _get_all(self, cr, uid, model, context=None):
35         models = self.pool.get(model)
36         all_model_ids = models.search(cr, uid, [])
37
38         output = [(False, '')]
39         output.extend(
40             sorted([(o.id, o.name)
41                     for o in models.browse(cr, uid, all_model_ids,
42                                            context=context)],
43                    key=itemgetter(1)))
44         return output
45
46     def _get_all_states(self, cr, uid, context=None):
47         return self._get_all(
48             cr, uid, 'res.country.state', context=context)
49     def _get_all_countries(self, cr, uid, context=None):
50         return self._get_all(cr, uid, 'res.country', context=context)
51     def _get_all_currencies(self, cr, uid, context=None):
52         return self._get_all(cr, uid, 'res.currency', context=context)
53
54     def default_get(self, cr, uid, fields_list=None, context=None):
55         """ get default company if any, and the various other fields
56         from the company's fields
57         """
58         defaults = super(base_setup_company, self)\
59               .default_get(cr, uid, fields_list=fields_list, context=context)
60
61         companies = self.pool.get('res.company')
62         company_id = companies.search(cr, uid, [], limit=1, order="id")
63         if not company_id or 'company_id' not in fields_list:
64             return defaults
65         company = companies.browse(cr, uid, company_id[0])
66
67         defaults['company_id'] = company.id
68         defaults['currency'] = company.currency_id.id
69         for field in ['name','logo','rml_header1','rml_footer1','rml_footer2']:
70             defaults[field] = company[field]
71
72         if company.partner_id.address:
73             address = company.partner_id.address[0]
74             for field in ['street','street2','zip','city','email','phone']:
75                 defaults[field] = address[field]
76             for field in ['country_id','state_id']:
77                 if address[field]:
78                     defaults[field] = address[field].id
79
80         return defaults
81
82     _columns = {
83         'company_id':fields.many2one('res.company', 'Company'),
84         'name':fields.char('Company Name', size=64, required=True),
85         'street':fields.char('Street', size=128),
86         'street2':fields.char('Street 2', size=128),
87         'zip':fields.char('Zip Code', size=24),
88         'city':fields.char('City', size=128),
89         'state_id':fields.selection(_get_all_states, 'States'),
90         'country_id':fields.selection(_get_all_countries, 'Countries'),
91         'email':fields.char('E-mail', size=64),
92         'phone':fields.char('Phone', size=64),
93         'currency':fields.selection(_get_all_currencies, 'Currency', required=True),
94         'rml_header1':fields.char('Report Header', size=200,
95             help='''This sentence will appear at the top right corner of your reports.
96 We suggest you to put a slogan here:
97 "Open Source Business Solutions".'''),
98         'rml_footer1':fields.char('Report Footer 1', size=200,
99             help='''This sentence will appear at the bottom of your reports.
100 We suggest you to write legal sentences here:
101 Web: http://openerp.com - Fax: +32.81.73.35.01 - Fortis Bank: 126-2013269-07'''),
102         'rml_footer2':fields.char('Report Footer 2', size=200,
103             help='''This sentence will appear at the bottom of your reports.
104 We suggest you to put bank information here:
105 IBAN: BE74 1262 0121 6907 - SWIFT: CPDF BE71 - VAT: BE0477.472.701'''),
106         'logo':fields.binary('Logo'),
107     }
108
109     def execute(self, cr, uid, ids, context=None):
110         assert len(ids) == 1, "We should only get one object from the form"
111         payload = self.browse(cr, uid, ids[0], context=context)
112         if not getattr(payload, 'company_id', None):
113             raise ValueError('Case where no default main company is setup '
114                              'not handled yet')
115
116         company = payload.company_id
117         company.write({
118             'name':payload.name,
119             'rml_header1':payload.rml_header1,
120             'rml_footer1':payload.rml_footer1,
121             'rml_footer2':payload.rml_footer2,
122             'logo':payload.logo,
123         })
124
125         company.partner_id.write({
126             'name':payload.name,
127         })
128
129         address_data = {
130             'name':payload.name,
131             'street':payload.street,
132             'street2':payload.street2,
133             'zip':payload.zip,
134             'city':payload.city,
135             'email':payload.email,
136             'phone':payload.phone,
137             'country_id':int(payload.country_id),
138             'state_id':int(payload.state_id),
139         }
140
141         if company.partner_id.address:
142             company.partner_id.address[0].write(
143                 address_data)
144         else:
145             self.pool.get('res.partner.address').create(cr, uid,
146                     dict(address_data,
147                          partner_id=int(company.partner_id)),
148                     context=context)
149 base_setup_company()
150
151
152 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: