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 ##############################################################################
21 from operator import itemgetter
23 from osv import osv, fields
27 class base_setup_company(osv.osv_memory):
30 _name = 'base.setup.company'
31 _inherit = 'res.config'
32 logger = netsvc.Logger()
34 def _get_all(self, cr, uid, model, context=None):
35 models = self.pool.get(model)
36 all_model_ids = models.search(cr, uid, [])
38 output = [(False, '')]
40 sorted([(o.id, o.name)
41 for o in models.browse(cr, uid, all_model_ids,
46 def _get_all_states(self, cr, uid, context=None):
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)
52 def _show_company_data(self, cr, uid, context=None):
53 # We only want to show the default company data in demo mode, otherwise users tend to forget
54 # to fill in the real company data in their production databases
55 return self.pool.get('ir.model.data').get_object(cr, uid, 'base', 'module_meta_information').demo
58 def default_get(self, cr, uid, fields_list=None, context=None):
59 """ get default company if any, and the various other fields
60 from the company's fields
62 defaults = super(base_setup_company, self)\
63 .default_get(cr, uid, fields_list=fields_list, context=context)
64 companies = self.pool.get('res.company')
65 company_id = companies.search(cr, uid, [], limit=1, order="id")
66 if not company_id or 'company_id' not in fields_list:
68 company = companies.browse(cr, uid, company_id[0], context=context)
69 defaults['company_id'] = company.id
71 if not self._show_company_data(cr, uid, context=context):
74 defaults['currency'] = company.currency_id.id
75 for field in ['name','rml_header1','rml_footer1','rml_footer2']:
76 defaults[field] = company[field]
78 if company.partner_id.address:
79 address = company.partner_id.address[0]
80 for field in ['street','street2','zip','city','email','phone']:
81 defaults[field] = address[field]
82 for field in ['country_id','state_id']:
84 defaults[field] = address[field].id
89 'company_id':fields.many2one('res.company', 'Company'),
90 'name':fields.char('Company Name', size=64, required=True),
91 'street':fields.char('Street', size=128),
92 'street2':fields.char('Street 2', size=128),
93 'zip':fields.char('Zip Code', size=24),
94 'city':fields.char('City', size=128),
95 'state_id':fields.selection(_get_all_states, 'Fed. State'),
96 'country_id':fields.selection(_get_all_countries, 'Country'),
97 'email':fields.char('E-mail', size=64),
98 'phone':fields.char('Phone', size=64),
99 'currency':fields.many2one('res.currency', 'Currency', required=True),
100 'rml_header1':fields.char('Report Header', size=200,
101 help='''This sentence will appear at the top right corner of your reports.
102 We suggest you to put a slogan here:
103 "Open Source Business Solutions".'''),
104 'rml_footer1':fields.char('Report Footer 1', size=200,
105 help='''This sentence will appear at the bottom of your reports.
106 We suggest you to write legal sentences here:
107 Web: http://openerp.com - Fax: +32.81.73.35.01 - Fortis Bank: 126-2013269-07'''),
108 'rml_footer2':fields.char('Report Footer 2', size=200,
109 help='''This sentence will appear at the bottom of your reports.
110 We suggest you to put bank information here:
111 IBAN: BE74 1262 0121 6907 - SWIFT: CPDF BE71 - VAT: BE0477.472.701'''),
112 'account_no':fields.char('Bank Account No', size=64),
113 'website': fields.char('Company Website', size=64, help="Example: http://openerp.com"),
116 def execute(self, cr, uid, ids, context=None):
117 assert len(ids) == 1, "We should only get one object from the form"
118 payload = self.browse(cr, uid, ids[0], context=context)
119 if not getattr(payload, 'company_id', None):
120 raise ValueError('Case where no default main company is setup '
122 company = payload.company_id
125 'rml_header1':payload.rml_header1,
126 'rml_footer1':payload.rml_footer1,
127 'rml_footer2':payload.rml_footer2,
128 'currency_id':payload.currency.id,
129 'account_no':payload.account_no,
132 company.partner_id.write({
134 'website':payload.website,
139 'street':payload.street,
140 'street2':payload.street2,
143 'email':payload.email,
144 'phone':payload.phone,
145 'country_id':int(payload.country_id),
146 'state_id':int(payload.state_id),
149 if company.partner_id.address:
150 company.partner_id.address[0].write(
153 self.pool.get('res.partner.address').create(cr, uid,
155 partner_id=int(company.partner_id)),
159 class res_currency(osv.osv):
160 _inherit = 'res.currency'
162 def name_get(self, cr, uid, ids, context=None):
165 # We can use the following line,if we want to restrict this name_get for company setup only
166 # But, its better to show currencies as name(Code).
169 if isinstance(ids, (int, long)):
171 reads = self.read(cr, uid, ids, ['name','symbol'], context, load='_classic_write')
172 return [(x['id'], tools.ustr(x['name']) + (x['symbol'] and (' (' + tools.ustr(x['symbol']) + ')') or '')) for x in reads]
176 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: