[IMP] : Added context=None on methods used for _constraints and replaced context...
[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 fields, 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         if context is None:
31             context = {}
32         res = super(account_invoice_line,self).move_line_get(cr, uid, invoice_id, context=context)
33         inv = self.pool.get('account.invoice').browse(cr, uid, invoice_id, context=context)
34         if inv.type in ('out_invoice','out_refund'):
35             for i_line in inv.invoice_line:
36                 if i_line.product_id:
37                     if inv.type == 'out_invoice':
38                         # debit account dacc will be the output account
39                         # first check the product, if empty check the category                        
40                         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
41                         if not dacc:
42                             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
43                     else:
44                         # = out_refund
45                         # debit account dacc will be the input account
46                         # first check the product, if empty check the category                        
47                         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
48                         if not dacc:
49                             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
50                     # in both cases the credit account cacc will be the expense account
51                     # first check the product, if empty check the category                                    
52                     cacc = i_line.product_id.product_tmpl_id.property_account_expense and i_line.product_id.product_tmpl_id.property_account_expense.id
53                     if not cacc:
54                         cacc = i_line.product_id.categ_id.property_account_expense_categ and i_line.product_id.categ_id.property_account_expense_categ.id
55                     if dacc and cacc:
56                         res.append({
57                             'type':'src',
58                             'name': i_line.name[:64],
59                             'price_unit':i_line.product_id.product_tmpl_id.standard_price,
60                             'quantity':i_line.quantity,
61                             'price':i_line.product_id.product_tmpl_id.standard_price * i_line.quantity,
62                             'account_id':dacc,
63                             'product_id':i_line.product_id.id,
64                             'uos_id':i_line.uos_id.id,
65                             'account_analytic_id':i_line.account_analytic_id.id,
66                             'taxes':i_line.invoice_line_tax_id,
67                             })
68                         
69                         res.append({
70                             'type':'src',
71                             'name': i_line.name[:64],
72                             'price_unit':i_line.product_id.product_tmpl_id.standard_price,
73                             'quantity':i_line.quantity,
74                             'price': -1 * i_line.product_id.product_tmpl_id.standard_price * i_line.quantity,
75                             'account_id':cacc,
76                             'product_id':i_line.product_id.id,
77                             'uos_id':i_line.uos_id.id,
78                             'account_analytic_id':i_line.account_analytic_id.id,
79                             'taxes':i_line.invoice_line_tax_id,
80                             })
81         elif inv.type in ('in_invoice','in_refund'):
82             for i_line in inv.invoice_line:
83                 if i_line.product_id:
84                     if i_line.product_id.product_tmpl_id.type != 'service':
85                         # get the price difference account at the product                        
86                         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
87                         if not acc:
88                             # if not found on the product get the price difference account at the category                            
89                             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
90                         a = None
91                         if inv.type == 'in_invoice':
92                             # oa will be the stock input account
93                             # first check the product, if empty check the category                             
94                             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
95                             if not oa:
96                                 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
97                         else:
98                             # = in_refund
99                             # oa will be the stock output account
100                             # first check the product, if empty check the category                            
101                             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
102                             if not oa:
103                                 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
104                         if oa:
105                             # get the fiscal position                            
106                             fpos = i_line.invoice_id.fiscal_position or False
107                             a = self.pool.get('account.fiscal.position').map_account(cr, uid, fpos, oa)
108                         diff_res = []
109                         # calculate and write down the possible price difference between invoice price and product price                        
110                         for line in res:
111                             if a == line['account_id'] and i_line.product_id.id == line['product_id']:
112                                 uom = i_line.product_id.uos_id or i_line.product_id.uom_id
113                                 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)
114                                 if standard_price != i_line.price_unit and line['price_unit'] == i_line.price_unit and acc:
115                                     price_diff = i_line.price_unit - standard_price
116                                     line.update({'price':standard_price * line['quantity']})
117                                     diff_res.append({
118                                         'type':'src',
119                                         'name': i_line.name[:64],
120                                         'price_unit':price_diff,
121                                         'quantity':line['quantity'],
122                                         'price': price_diff * line['quantity'],
123                                         'account_id':acc,
124                                         'product_id':line['product_id'],
125                                         'uos_id':line['uos_id'],
126                                         'account_analytic_id':line['account_analytic_id'],
127                                         'taxes':line.get('taxes',[]),
128                                         })
129                         res += diff_res
130         return res   
131     
132     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):
133         if context is None:
134             context = {}
135         if not product:
136             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)
137         else:
138             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)
139
140         if type in ('in_invoice','in_refund'):
141             product_obj = self.pool.get('product.product').browse(cr, uid, product, context=context)
142             if type == 'in_invoice':
143                 oa = product_obj.product_tmpl_id.property_stock_account_input and product_obj.product_tmpl_id.property_stock_account_input.id
144                 if not oa:
145                     oa = product_obj.categ_id.property_stock_account_input_categ and product_obj.categ_id.property_stock_account_input_categ.id
146             else:
147                 oa = product_obj.product_tmpl_id.property_stock_account_output and product_obj.product_tmpl_id.property_stock_account_output.id
148                 if not oa:
149                     oa = product_obj.categ_id.property_stock_account_output_categ and product_obj.categ_id.property_stock_account_output_categ.id
150             if oa:
151                 fpos = fposition_id and self.pool.get('account.fiscal.position').browse(cr, uid, fposition_id, context=context) or False
152                 a = self.pool.get('account.fiscal.position').map_account(cr, uid, fpos, oa)
153                 res['value'].update({'account_id':a})            
154         return res
155          
156 account_invoice_line()
157
158 class account_invoice(osv.osv):
159     _inherit = "account.invoice"
160
161     def _refund_cleanup_lines(self, cr, uid, lines):
162         for line in lines:
163             inv_id = line['invoice_id']
164             inv_obj = self.browse(cr, uid, inv_id[0])
165             if inv_obj.type == 'in_invoice':
166                 if line.get('product_id',False):
167                     product_obj = self.pool.get('product.product').browse(cr, uid, line['product_id'][0])
168                     oa = product_obj.product_tmpl_id.property_stock_account_output and product_obj.product_tmpl_id.property_stock_account_output.id
169                     if not oa:
170                         oa = product_obj.categ_id.property_stock_account_output_categ and product_obj.categ_id.property_stock_account_output_categ.id
171                     if oa:
172                         fpos = inv_obj.fiscal_position or False
173                         a = self.pool.get('account.fiscal.position').map_account(cr, uid, fpos, oa)
174                         account_data = self.pool.get('account.account').read(cr, uid, [a], ['name'])[0]
175                         line.update({'account_id': (account_data['id'],account_data['name'])})
176         res = super(account_invoice,self)._refund_cleanup_lines(cr, uid, lines)
177         return res
178     
179 account_invoice()
180 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: