[MERGE] merge with latest stable
[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, context=context).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         lines = self.browse(cr, uid, ids, context=context)
136         account_ids = [line.account_id.id for line in lines]
137         account_names = dict(self.pool.get('account.account').name_get(cr, uid, account_ids, context=context))
138         for line in lines:
139             if line.state != 'article':
140                 if line.state == 'line':
141                     res[line.id] = '-----------------------------------------'
142                 elif line.state == 'break':
143                     res[line.id] = 'PAGE BREAK'
144                 else:
145                     res[line.id] = ' '
146             else:
147                 res[line.id] = account_names.get(line.account_id.id, '')
148         return res
149
150     _name = "account.invoice.line"
151     _order = "invoice_id, sequence asc"
152     _description = "Invoice Line"
153     _inherit = "account.invoice.line"
154     _columns = {
155         'state': fields.selection([
156                 ('article','Product'),
157                 ('title','Title'),
158                 ('text','Note'),
159                 ('subtotal','Sub Total'),
160                 ('line','Separator Line'),
161                 ('break','Page Break'),]
162             ,'Type', select=True, required=True),
163         'sequence': fields.integer('Sequence Number', help="Gives the sequence order when displaying a list of invoice lines."),
164         '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"),
165     }
166
167     def _default_account(self, cr, uid, context=None):
168         if context is None:
169             context = {}
170         current_company = self.pool.get('res.users').browse(cr,uid,uid).company_id.id
171         account_id = self.pool.get('account.account').search(cr, uid, [('company_id','=',current_company),('parent_id','=',False)], limit=1,context=context)
172         return account_id and account_id[0] or False
173
174     _defaults = {
175         'state': 'article',
176         'sequence': 0,
177     }
178
179 account_invoice_line()
180
181 class one2many_mod2(fields.one2many):
182
183     def get(self, cr, obj, ids, name, user=None, offset=0, context=None, values=None):
184         if context is None:
185             context = {}
186         if not values:
187             values = {}
188         res = {}
189         for id in ids:
190             res[id] = []
191         ids2 = obj.pool.get(self._obj).search(cr, user, [(self._fields_id,'in',ids),('state','=','article')], limit=self._limit)
192         for r in obj.pool.get(self._obj)._read_flat(cr, user, ids2, [self._fields_id], context=context, load='_classic_write'):
193             res[r[self._fields_id]].append( r['id'] )
194         return res
195
196 class account_invoice(osv.osv):
197
198     def copy(self, cr, uid, id, default=None, context=None):
199         if default is None:
200             default = {}
201         default['invoice_line'] = False
202         return super(account_invoice, self).copy(cr, uid, id, default, context)
203
204     _inherit = "account.invoice"
205     _columns = {
206         'abstract_line_ids': fields.one2many('account.invoice.line', 'invoice_id', 'Invoice Lines',readonly=True, states={'draft':[('readonly',False)]}),
207         'invoice_line': one2many_mod2('account.invoice.line', 'invoice_id', 'Invoice Lines',readonly=True, states={'draft':[('readonly',False)]}),
208     }
209
210 account_invoice()
211
212 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: