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