[FIX]: Fixed buildbot warning.
[odoo/odoo.git] / addons / account_anglo_saxon / invoice.py
1 ##############################################################################
2 #    
3 #    OpenERP, Open Source Management Solution
4 #    Copyright (C) 
5 #    2004-2010 Tiny SPRL (<http://tiny.be>). 
6 #    2009-2010 Veritos (http://veritos.nl).
7 #    All Rights Reserved
8 #
9 #    This program is free software: you can redistribute it and/or modify
10 #    it under the terms of the GNU Affero General Public License as
11 #    published by the Free Software Foundation, either version 3 of the
12 #    License, or (at your option) any later version.
13 #
14 #    This program is distributed in the hope that it will be useful,
15 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
16 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 #    GNU Affero General Public License for more details.
18 #
19 #    You should have received a copy of the GNU Affero General Public License
20 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.     
21 #
22 ##############################################################################
23
24 from osv import osv
25
26 class account_invoice_line(osv.osv):
27     _inherit = "account.invoice.line"
28     
29     def move_line_get(self, cr, uid, invoice_id, context=None):
30         res = super(account_invoice_line,self).move_line_get(cr, uid, invoice_id, context=context)
31         inv = self.pool.get('account.invoice').browse(cr, uid, invoice_id, context=context)
32         if inv.type in ('out_invoice','out_refund'):
33             for i_line in inv.invoice_line:
34                 if i_line.product_id:
35                     if inv.type == 'out_invoice':
36                         # debit account dacc will be the output account
37                         # first check the product, if empty check the category                        
38                         dacc = i_line.product_id.product_tmpl_id.property_stock_account_output and i_line.product_id.product_tmpl_id.property_stock_account_output.id
39                         if not dacc:
40                             dacc = i_line.product_id.categ_id.property_stock_account_output_categ and i_line.product_id.categ_id.property_stock_account_output_categ.id
41                     else:
42                         # = out_refund
43                         # debit account dacc will be the input account
44                         # first check the product, if empty check the category                        
45                         dacc = i_line.product_id.product_tmpl_id.property_stock_account_input and i_line.product_id.product_tmpl_id.property_stock_account_input.id
46                         if not dacc:
47                             dacc = i_line.product_id.categ_id.property_stock_account_input_categ and i_line.product_id.categ_id.property_stock_account_input_categ.id
48                     # in both cases the credit account cacc will be the expense account
49                     # first check the product, if empty check the category                                    
50                     cacc = i_line.product_id.product_tmpl_id.property_account_expense and i_line.product_id.product_tmpl_id.property_account_expense.id
51                     if not cacc:
52                         cacc = i_line.product_id.categ_id.property_account_expense_categ and i_line.product_id.categ_id.property_account_expense_categ.id
53                     if dacc and cacc:
54                         res.append({
55                             'type':'src',
56                             'name': i_line.name[:64],
57                             'price_unit':i_line.product_id.product_tmpl_id.standard_price,
58                             'quantity':i_line.quantity,
59                             'price':i_line.product_id.product_tmpl_id.standard_price * i_line.quantity,
60                             'account_id':dacc,
61                             'product_id':i_line.product_id.id,
62                             'uos_id':i_line.uos_id.id,
63                             'account_analytic_id':i_line.account_analytic_id.id,
64                             'taxes':i_line.invoice_line_tax_id,
65                             })
66                         
67                         res.append({
68                             'type':'src',
69                             'name': i_line.name[:64],
70                             'price_unit':i_line.product_id.product_tmpl_id.standard_price,
71                             'quantity':i_line.quantity,
72                             'price': -1 * i_line.product_id.product_tmpl_id.standard_price * i_line.quantity,
73                             'account_id':cacc,
74                             'product_id':i_line.product_id.id,
75                             'uos_id':i_line.uos_id.id,
76                             'account_analytic_id':i_line.account_analytic_id.id,
77                             'taxes':i_line.invoice_line_tax_id,
78                             })
79         elif inv.type in ('in_invoice','in_refund'):
80             for i_line in inv.invoice_line:
81                 if i_line.product_id:
82                     if i_line.product_id.product_tmpl_id.type != 'service':
83                         # get the price difference account at the product                        
84                         acc = i_line.product_id.product_tmpl_id.property_account_creditor_price_difference and i_line.product_id.product_tmpl_id.property_account_creditor_price_difference.id
85                         if not acc:
86                             # if not found on the product get the price difference account at the category                            
87                             acc = i_line.product_id.categ_id.property_account_creditor_price_difference_categ and i_line.product_id.categ_id.property_account_creditor_price_difference_categ.id
88                         a = None
89                         if inv.type == 'in_invoice':
90                             # oa will be the stock input account
91                             # first check the product, if empty check the category                             
92                             oa = i_line.product_id.product_tmpl_id.property_stock_account_input and i_line.product_id.product_tmpl_id.property_stock_account_input.id
93                             if not oa:
94                                 oa = i_line.product_id.categ_id.property_stock_account_input_categ and i_line.product_id.categ_id.property_stock_account_input_categ.id
95                         else:
96                             # = in_refund
97                             # oa will be the stock output account
98                             # first check the product, if empty check the category                            
99                             oa = i_line.product_id.product_tmpl_id.property_stock_account_output and i_line.product_id.product_tmpl_id.property_stock_account_output.id
100                             if not oa:
101                                 oa = i_line.product_id.categ_id.property_stock_account_output_categ and i_line.product_id.categ_id.property_stock_account_output_categ.id
102                         if oa:
103                             # get the fiscal position                            
104                             fpos = i_line.invoice_id.fiscal_position or False
105                             a = self.pool.get('account.fiscal.position').map_account(cr, uid, fpos, oa)
106                         diff_res = []
107                         # calculate and write down the possible price difference between invoice price and product price                        
108                         for line in res:
109                             if a == line['account_id'] and i_line.product_id.id == line['product_id']:
110                                 uom = i_line.product_id.uos_id or i_line.product_id.uom_id
111                                 standard_price = self.pool.get('product.uom')._compute_price(cr, uid, uom.id, i_line.product_id.product_tmpl_id.standard_price, i_line.uos_id.id)
112                                 if standard_price != i_line.price_unit and line['price_unit'] == i_line.price_unit and acc:
113                                     price_diff = i_line.price_unit - standard_price
114                                     line.update({'price':standard_price * line['quantity']})
115                                     diff_res.append({
116                                         'type':'src',
117                                         'name': i_line.name[:64],
118                                         'price_unit':price_diff,
119                                         'quantity':line['quantity'],
120                                         'price': price_diff * line['quantity'],
121                                         'account_id':acc,
122                                         'product_id':line['product_id'],
123                                         'uos_id':line['uos_id'],
124                                         'account_analytic_id':line['account_analytic_id'],
125                                         'taxes':line.get('taxes',[]),
126                                         })
127                         res += diff_res
128         return res   
129     
130     def product_id_change(self, cr, uid, ids, product, uom, qty=0, name='', type='out_invoice', partner_id=False, fposition_id=False, price_unit=False, address_invoice_id=False, currency_id=False, context=None):
131         if not product:
132             return super(account_invoice_line, self).product_id_change(cr, uid, ids, product, uom, qty, name, type, partner_id, fposition_id, price_unit, address_invoice_id, currency_id, context)
133         else:
134             res = super(account_invoice_line, self).product_id_change(cr, uid, ids, product, uom, qty, name, type, partner_id, fposition_id, price_unit, address_invoice_id, currency_id, context)
135
136         if type in ('in_invoice','in_refund'):
137             product_obj = self.pool.get('product.product').browse(cr, uid, product, context=context)
138             if type == 'in_invoice':
139                 oa = product_obj.product_tmpl_id.property_stock_account_input and product_obj.product_tmpl_id.property_stock_account_input.id
140                 if not oa:
141                     oa = product_obj.categ_id.property_stock_account_input_categ and product_obj.categ_id.property_stock_account_input_categ.id
142             else:
143                 oa = product_obj.product_tmpl_id.property_stock_account_output and product_obj.product_tmpl_id.property_stock_account_output.id
144                 if not oa:
145                     oa = product_obj.categ_id.property_stock_account_output_categ and product_obj.categ_id.property_stock_account_output_categ.id
146             if oa:
147                 fpos = fposition_id and self.pool.get('account.fiscal.position').browse(cr, uid, fposition_id, context=context) or False
148                 a = self.pool.get('account.fiscal.position').map_account(cr, uid, fpos, oa)
149                 res['value'].update({'account_id':a})            
150         return res
151          
152 account_invoice_line()
153
154 class account_invoice(osv.osv):
155     _inherit = "account.invoice"
156
157     def _refund_cleanup_lines(self, cr, uid, lines):
158         for line in lines:
159             inv_id = line['invoice_id']
160             inv_obj = self.browse(cr, uid, inv_id[0])
161             if inv_obj.type == 'in_invoice':
162                 if line.get('product_id',False):
163                     product_obj = self.pool.get('product.product').browse(cr, uid, line['product_id'][0])
164                     oa = product_obj.product_tmpl_id.property_stock_account_output and product_obj.product_tmpl_id.property_stock_account_output.id
165                     if not oa:
166                         oa = product_obj.categ_id.property_stock_account_output_categ and product_obj.categ_id.property_stock_account_output_categ.id
167                     if oa:
168                         fpos = inv_obj.fiscal_position or False
169                         a = self.pool.get('account.fiscal.position').map_account(cr, uid, fpos, oa)
170                         account_data = self.pool.get('account.account').read(cr, uid, [a], ['name'])[0]
171                         line.update({'account_id': (account_data['id'],account_data['name'])})
172         res = super(account_invoice,self)._refund_cleanup_lines(cr, uid, lines)
173         return res
174     
175 account_invoice()
176 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: