[IMP] ir_sequence,res_company,res_currency objects related to company_id.
[odoo/odoo.git] / bin / addons / base / res / res_company.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #    
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2009 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 osv import fields,osv
23 import os
24 import tools
25
26 class res_company(osv.osv):
27     _name = "res.company"
28
29     _columns = {
30         'name': fields.char('Company Name', size=64, required=True),
31         'parent_id': fields.many2one('res.company', 'Parent Company', select=True),
32         'child_ids': fields.one2many('res.company', 'parent_id', 'Child Companies'),
33         'partner_id': fields.many2one('res.partner', 'Partner', required=True),
34         'rml_header1': fields.char('Report Header', size=200),
35         'rml_footer1': fields.char('Report Footer 1', size=200),
36         'rml_footer2': fields.char('Report Footer 2', size=200),
37         'rml_header' : fields.text('RML Header'),
38         'rml_header2' : fields.text('RML Internal Header'),
39         'logo' : fields.binary('Logo'),
40         'currency_id': fields.many2one('res.currency', 'Currency', required=True),
41         'currency_ids': fields.one2many('res.currency', 'company_id', 'Currency')
42     }
43     
44     
45     def _company_default_get(self, cr, uid, object=False, context={}):
46         return self.pool.get('res.users').browse(cr, uid, uid).company_id.id
47     
48     def _get_child_ids(self, cr, uid, uid2, context={}):
49         company = self.pool.get('res.users').company_get(cr, uid, uid2)
50         ids = self._get_company_children(cr, uid, company)
51         return ids
52
53     def _get_company_children(self, cr, uid=None, company=None):
54         if not company:
55             return []
56         ids =  self.search(cr, uid, [('parent_id','child_of',[company])])
57         return ids
58     _get_company_children = tools.cache()(_get_company_children)
59
60     def _get_partner_hierarchy(self, cr, uid, company_id, context={}):
61         if company_id:
62             parent_id = self.browse(cr, uid, company_id)['parent_id']
63             if parent_id:
64                 return self._get_partner_hierarchy(cr, uid, parent_id.id, context)
65             else:
66                 return self._get_partner_descendance(cr, uid, company_id, [], context)
67         return []
68
69     def _get_partner_descendance(self, cr, uid, company_id, descendance, context={}):
70         descendance.append(self.browse(cr, uid, company_id).partner_id.id)
71         for child_id in self._get_company_children(cr, uid, company_id):
72             if child_id != company_id:
73                 descendance = self._get_partner_descendance(cr, uid, child_id, descendance)
74         return descendance
75
76     #
77     # This function restart the cache on the _get_company_children method
78     #
79     def cache_restart(self, cr):
80         self._get_company_children.clear_cache(cr.dbname)
81
82     def create(self, cr, *args, **argv):
83         self.cache_restart(cr)
84         return super(res_company, self).create(cr, *args, **argv)
85
86     def write(self, cr, *args, **argv):
87         self.cache_restart(cr)
88         # Restart the cache on the company_get method
89         self.pool.get('ir.rule').domain_get.clear_cache(cr.dbname)
90         return super(res_company, self).write(cr, *args, **argv)
91
92     def _get_euro(self, cr, uid, context={}):
93         try:
94             return self.pool.get('res.currency').search(cr, uid, [])[0]
95         except:
96             return False
97     
98     def _check_recursion(self, cr, uid, ids):
99         level = 100
100         while len(ids):
101             cr.execute('select distinct parent_id from res_company where id in ('+','.join(map(str, ids))+')')
102             ids = filter(None, map(lambda x:x[0], cr.fetchall()))
103             if not level:
104                 return False
105             level -= 1
106         return True
107     
108     def _get_header2(self,cr,uid,ids):
109         return """
110         <header>
111         <pageTemplate>
112         <frame id="first" x1="1.3cm" y1="1.5cm" width="18.4cm" height="26.5cm"/>
113         <pageGraphics>
114         <fill color="black"/>
115         <stroke color="black"/>
116         <setFont name="Helvetica" size="8"/>
117         <drawString x="1.3cm" y="28.3cm"> [[ formatLang(time.strftime("%Y-%m-%d"), date=True) ]]  [[ time.strftime("%H:%M") ]]</drawString>
118         <setFont name="Helvetica-Bold" size="10"/>
119         <drawString x="9.8cm" y="28.3cm">[[ company.partner_id.name ]]</drawString>
120         <setFont name="Helvetica" size="8"/>
121         <drawRightString x="19.7cm" y="28.3cm"><pageNumber/> /  </drawRightString>
122         <drawString x="19.8cm" y="28.3cm"><pageCount/></drawString>
123         <stroke color="#000000"/>
124         <lines>1.3cm 28.1cm 20cm 28.1cm</lines>
125         </pageGraphics>
126         </pageTemplate>
127 </header>"""
128     def _get_header(self,cr,uid,ids):
129         try :
130             return tools.file_open(os.path.join('base', 'report', 'corporate_rml_header.rml')).read()
131         except:
132             return """
133     <header>
134     <pageTemplate>
135         <frame id="first" x1="1.3cm" y1="2.5cm" height="23.0cm" width="19cm"/>
136         <pageGraphics>
137             <!-- You Logo - Change X,Y,Width and Height -->
138         <image x="1.3cm" y="27.6cm" height="40.0" >[[company.logo]]</image>
139             <setFont name="Helvetica" size="8"/>
140             <fill color="black"/>
141             <stroke color="black"/>
142             <lines>1.3cm 27.7cm 20cm 27.7cm</lines>
143
144             <drawRightString x="20cm" y="27.8cm">[[ company.rml_header1 ]]</drawRightString>
145
146
147             <drawString x="1.3cm" y="27.2cm">[[ company.partner_id.name ]]</drawString>
148             <drawString x="1.3cm" y="26.8cm">[[ company.partner_id.address and company.partner_id.address[0].street or  '' ]]</drawString>
149             <drawString x="1.3cm" y="26.4cm">[[ company.partner_id.address and company.partner_id.address[0].zip or '' ]] [[ company.partner_id.address and company.partner_id.address[0].city or '' ]] - [[ company.partner_id.address and company.partner_id.address[0].country_id and company.partner_id.address[0].country_id.name  or '']]</drawString>
150             <drawString x="1.3cm" y="26.0cm">Phone:</drawString>
151             <drawRightString x="7cm" y="26.0cm">[[ company.partner_id.address and company.partner_id.address[0].phone or '' ]]</drawRightString>
152             <drawString x="1.3cm" y="25.6cm">Mail:</drawString>
153             <drawRightString x="7cm" y="25.6cm">[[ company.partner_id.address and company.partner_id.address[0].email or '' ]]</drawRightString>
154             <lines>1.3cm 25.5cm 7cm 25.5cm</lines>
155
156             <!--page bottom-->
157
158             <lines>1.2cm 2.15cm 19.9cm 2.15cm</lines>
159
160             <drawCentredString x="10.5cm" y="1.7cm">[[ company.rml_footer1 ]]</drawCentredString>
161             <drawCentredString x="10.5cm" y="1.25cm">[[ company.rml_footer2 ]]</drawCentredString>
162             <drawCentredString x="10.5cm" y="0.8cm">Contact : [[ user.name ]] - Page: <pageNumber/></drawCentredString>
163         </pageGraphics>
164     </pageTemplate>
165 </header>"""
166     _defaults = {
167         'currency_id': _get_euro,
168         'rml_header':_get_header,
169         'rml_header2': _get_header2
170     }
171
172     _constraints = [
173         (_check_recursion, 'Error! You can not create recursive companies.', ['parent_id'])
174     ]
175
176 res_company()
177
178
179 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
180