[IMP]:scrum,mrp:added report header title.
[odoo/odoo.git] / addons / account_invoice_layout / account_invoice_layout.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 osv import fields, osv
23
24 class notify_message(osv.osv):
25     _name = 'notify.message'
26     _description = 'Notify By Messages'
27     _columns = {
28         'name':  fields.char('Title', size=64, required=True),
29         'msg': fields.text('Special Message', size=128, required=True, help='This notification will appear at the bottom of the Invoices when printed.', translate=True)
30     }
31
32 notify_message()
33
34 class account_invoice_line(osv.osv):
35
36     def move_line_get_item(self, cr, uid, line, context=None):
37         if line.state != 'article':
38             return None
39         return super(account_invoice_line, self).move_line_get_item(cr, uid, line, context)
40
41     def fields_get(self, cr, uid, fields=None, context=None):
42         article = {
43             'article': [('readonly', False), ('invisible', False)],
44             'text': [('readonly', True), ('invisible', True), ('required', False)],
45             'subtotal': [('readonly', True), ('invisible', True), ('required', False)],
46             'title': [('readonly', True), ('invisible', True), ('required', False)],
47             'break': [('readonly', True), ('invisible', True), ('required', False)],
48             'line': [('readonly', True), ('invisible', True), ('required', False)],
49         }
50         states = {
51             'name': {
52                 'break': [('readonly', True),('required', False),('invisible', True)],
53                 'line': [('readonly', True),('required', False),('invisible', True)],
54                 },
55             'product_id': article,
56             'account_id': article,
57             'quantity': article,
58             'uos_id': article,
59             'price_unit': article,
60             'discount': article,
61             'invoice_line_tax_id': article,
62             'account_analytic_id': article,
63         }
64         res = super(account_invoice_line, self).fields_get(cr, uid, fields, context)
65         for field in res:
66             if states.has_key(field):
67                 for key,value in states[field].items():
68                     res[field].setdefault('states',{})
69                     res[field]['states'][key] = value
70         return res
71
72     def onchange_invoice_line_view(self, cr, uid, id, type, context=None, *args):
73
74         if (not type):
75             return {}
76         if type != 'article':
77             temp = {'value': {
78                     'product_id': False,
79                     'uos_id': False,
80                     'account_id': False,
81                     'price_unit': False,
82                     'price_subtotal': False,
83                     'quantity': 0,
84                     'discount': False,
85                     'invoice_line_tax_id': False,
86                     'account_analytic_id': False,
87                     },
88                 }
89             if type == 'line':
90                 temp['value']['name'] = ' '
91             if type == 'break':
92                 temp['value']['name'] = ' '
93             if type == 'subtotal':
94                 temp['value']['name'] = 'Sub Total'
95             return temp
96         return {}
97
98     def create(self, cr, user, vals, context=None):
99         if vals.has_key('state'):
100             if vals['state'] == 'line':
101                 vals['name'] = ' '
102             if vals['state'] == 'break':
103                 vals['name'] = ' '
104             if vals['state'] != 'article':
105                 vals['quantity']= 0
106                 vals['account_id']= self._default_account(cr, user, None)
107         return super(account_invoice_line, self).create(cr, user, vals, context)
108
109     def write(self, cr, user, ids, vals, context=None):
110         if vals.has_key('state'):
111             if vals['state'] != 'article':
112                 vals['product_id']= False
113                 vals['uos_id']= False
114                 vals['account_id']= self._default_account(cr, user, None)
115                 vals['price_unit']= False
116                 vals['price_subtotal']= False
117                 vals['quantity']= 0
118                 vals['discount']= False
119                 vals['invoice_line_tax_id']= False
120                 vals['account_analytic_id']= False
121             if vals['state'] == 'line':
122                 vals['name'] = ' '
123             if vals['state'] == 'break':
124                 vals['name'] = ' '
125         return super(account_invoice_line, self).write(cr, user, ids, vals, context)
126
127     def copy_data(self, cr, uid, id, default=None, context=None):
128         if default is None:
129             default = {}
130         default['state'] = self.browse(cr, uid, id).state
131         return super(account_invoice_line, self).copy_data(cr, uid, id, default, context)
132
133     def _fnct(self, cr, uid, ids, name, args, context=None):
134         res = {}
135
136         lines = self.browse(cr, uid, ids)
137         account_ids = [line.account_id.id for line in lines]
138         account_names = dict(self.pool.get('account.account').name_get(cr, uid, account_ids, context=context))
139         for line in lines:
140             if line.state != 'article':
141                 if line.state == 'line':
142                     res[line.id] = '-----------------------------------------'
143                 elif line.state == 'break':
144                     res[line.id] = 'PAGE BREAK'
145                 else:
146                     res[line.id] = ' '
147             else:
148                 res[line.id] = account_names.get(line.account_id.id, '')
149         return res
150
151     _name = "account.invoice.line"
152     _order = "invoice_id, sequence asc"
153     _description = "Invoice Line"
154     _inherit = "account.invoice.line"
155     _columns = {
156         'state': fields.selection([
157                 ('article','Product'),
158                 ('title','Title'),
159                 ('text','Note'),
160                 ('subtotal','Sub Total'),
161                 ('line','Separator Line'),
162                 ('break','Page Break'),]
163             ,'Type', select=True, required=True),
164         'sequence': fields.integer('Sequence Number', help="Gives the sequence order when displaying a list of invoice lines."),
165         'functional_field': fields.function(_fnct, arg=None, fnct_inv=None, fnct_inv_arg=None, type='char', fnct_search=None, obj=None, method=True, store=False, string="Source Account"),
166     }
167
168     def _default_account(self, cr, uid, context=None):
169         cr.execute("select id from account_account where parent_id IS NULL LIMIT 1")
170         res = cr.fetchone()
171         return res[0]
172
173     _defaults = {
174         'state': 'article',
175         'sequence': 0,
176     }
177
178 account_invoice_line()
179
180 class one2many_mod2(fields.one2many):
181
182     def get(self, cr, obj, ids, name, user=None, offset=0, context=None, values=None):
183         if not context:
184             context = {}
185         if not values:
186             values = {}
187         res = {}
188         for id in ids:
189             res[id] = []
190         ids2 = obj.pool.get(self._obj).search(cr, user, [(self._fields_id,'in',ids),('state','=','article')], limit=self._limit)
191         for r in obj.pool.get(self._obj)._read_flat(cr, user, ids2, [self._fields_id], context=context, load='_classic_write'):
192             res[r[self._fields_id]].append( r['id'] )
193         return res
194
195 class account_invoice(osv.osv):
196
197     def copy(self, cr, uid, id, default=None, context=None):
198         if default is None:
199             default = {}
200         default['invoice_line'] = False
201         return super(account_invoice, self).copy(cr, uid, id, default, context)
202
203     _inherit = "account.invoice"
204     _columns = {
205         'abstract_line_ids': fields.one2many('account.invoice.line', 'invoice_id', 'Invoice Lines',readonly=True, states={'draft':[('readonly',False)]}),
206         'invoice_line': one2many_mod2('account.invoice.line', 'invoice_id', 'Invoice Lines',readonly=True, states={'draft':[('readonly',False)]}),
207     }
208
209 account_invoice()
210
211 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: