Forward port of branch saas-3 up to fc9fc3e
[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 openerp.osv import osv, fields
25 from openerp.tools.float_utils import float_round as round
26
27 class account_invoice_line(osv.osv):
28     _inherit = "account.invoice.line"
29
30     _columns = {
31         'move_id': fields.many2one('stock.move', string="Move line", help="If the invoice was generated from a stock.picking, reference to the related move line."),
32     }
33
34     def move_line_get(self, cr, uid, invoice_id, context=None):
35         res = super(account_invoice_line,self).move_line_get(cr, uid, invoice_id, context=context)
36         inv = self.pool.get('account.invoice').browse(cr, uid, invoice_id, context=context)
37         company_currency = inv.company_id.currency_id.id
38         def get_price(cr, uid, inv, company_currency, i_line, price_unit):
39             cur_obj = self.pool.get('res.currency')
40             decimal_precision = self.pool.get('decimal.precision')
41             if inv.currency_id.id != company_currency:
42                 price = cur_obj.compute(cr, uid, company_currency, inv.currency_id.id, price_unit * i_line.quantity, context={'date': inv.date_invoice})
43             else:
44                 price = price_unit * i_line.quantity
45             return round(price, decimal_precision.precision_get(cr, uid, 'Account'))
46
47         if inv.type in ('out_invoice','out_refund'):
48             for i_line in inv.invoice_line:
49                 if i_line.product_id and i_line.product_id.valuation == 'real_time':
50                     # debit account dacc will be the output account
51                     # first check the product, if empty check the category
52                     dacc = i_line.product_id.property_stock_account_output and i_line.product_id.property_stock_account_output.id
53                     if not dacc:
54                         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
55                     # in both cases the credit account cacc will be the expense account
56                     # first check the product, if empty check the category
57                     cacc = i_line.product_id.property_account_expense and i_line.product_id.property_account_expense.id
58                     if not cacc:
59                         cacc = i_line.product_id.categ_id.property_account_expense_categ and i_line.product_id.categ_id.property_account_expense_categ.id
60                     if dacc and cacc:
61                         price_unit = i_line.move_id and i_line.move_id.price_unit or i_line.product_id.standard_price
62                         res.append({
63                             'type':'src',
64                             'name': i_line.name[:64],
65                             'price_unit':price_unit,
66                             'quantity':i_line.quantity,
67                             'price':get_price(cr, uid, inv, company_currency, i_line, price_unit),
68                             'account_id':dacc,
69                             'product_id':i_line.product_id.id,
70                             'uos_id':i_line.uos_id.id,
71                             'account_analytic_id': False,
72                             'taxes':i_line.invoice_line_tax_id,
73                             })
74
75                         res.append({
76                             'type':'src',
77                             'name': i_line.name[:64],
78                             'price_unit':price_unit,
79                             'quantity':i_line.quantity,
80                             'price': -1 * get_price(cr, uid, inv, company_currency, i_line, price_unit),
81                             'account_id':cacc,
82                             'product_id':i_line.product_id.id,
83                             'uos_id':i_line.uos_id.id,
84                             'account_analytic_id': False,
85                             'taxes':i_line.invoice_line_tax_id,
86                             })
87         elif inv.type in ('in_invoice','in_refund'):
88             for i_line in inv.invoice_line:
89                 if i_line.product_id and i_line.product_id.valuation == 'real_time':
90                     if i_line.product_id.type != 'service':
91                         # get the price difference account at the product
92                         acc = i_line.product_id.property_account_creditor_price_difference and i_line.product_id.property_account_creditor_price_difference.id
93                         if not acc:
94                             # if not found on the product get the price difference account at the category
95                             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
96                         a = None
97
98                         # oa will be the stock input account
99                         # first check the product, if empty check the category
100                         oa = i_line.product_id.property_stock_account_input and i_line.product_id.property_stock_account_input.id
101                         if not oa:
102                             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
103                         if oa:
104                             # get the fiscal position
105                             fpos = i_line.invoice_id.fiscal_position or False
106                             a = self.pool.get('account.fiscal.position').map_account(cr, uid, fpos, oa)
107                         diff_res = []
108                         decimal_precision = self.pool.get('decimal.precision')
109                         account_prec = decimal_precision.precision_get(cr, uid, 'Account')
110                         # calculate and write down the possible price difference between invoice price and product price
111                         for line in res:
112                             if line.get('invl_id', 0) == i_line.id and a == line['account_id']:
113                                 uom = i_line.product_id.uos_id or i_line.product_id.uom_id
114                                 valuation_price_unit = self.pool.get('product.uom')._compute_price(cr, uid, uom.id, i_line.product_id.standard_price, i_line.uos_id.id)
115                                 if i_line.product_id.cost_method != 'standard' and i_line.purchase_line_id:
116                                     #for average/fifo/lifo costing method, fetch real cost price from incomming moves
117                                     stock_move_obj = self.pool.get('stock.move')
118                                     valuation_stock_move = stock_move_obj.search(cr, uid, [('purchase_line_id', '=', i_line.purchase_line_id.id)], limit=1, context=context)
119                                     if valuation_stock_move:
120                                         valuation_price_unit = stock_move_obj.browse(cr, uid, valuation_stock_move[0], context=context).price_unit
121                                 if inv.currency_id.id != company_currency:
122                                     valuation_price_unit = self.pool.get('res.currency').compute(cr, uid, company_currency, inv.currency_id.id, valuation_price_unit, context={'date': inv.date_invoice})
123                                 if valuation_price_unit != i_line.price_unit and line['price_unit'] == i_line.price_unit and acc:
124                                     # price with discount and without tax included
125                                     price_unit = self.pool['account.tax'].compute_all(cr, uid, line['taxes'],
126                                         i_line.price_unit * (1-(i_line.discount or 0.0)/100.0), line['quantity'])['total']
127                                     price_line = round(valuation_price_unit * line['quantity'], account_prec)
128                                     price_diff = round(price_unit - price_line, account_prec)
129                                     line.update({'price': price_line})
130                                     diff_res.append({
131                                         'type': 'src',
132                                         'name': i_line.name[:64],
133                                         'price_unit': round(price_diff / line['quantity'], account_prec),
134                                         'quantity': line['quantity'],
135                                         'price': price_diff,
136                                         'account_id': acc,
137                                         'product_id': line['product_id'],
138                                         'uos_id': line['uos_id'],
139                                         'account_analytic_id': line['account_analytic_id'],
140                                         'taxes': line.get('taxes', []),
141                                         })
142                         res += diff_res
143         return res
144
145     def product_id_change(self, cr, uid, ids, product, uom_id, qty=0, name='', type='out_invoice', partner_id=False, fposition_id=False, price_unit=False, currency_id=False, company_id=None, context=None):
146         fiscal_pool = self.pool.get('account.fiscal.position')
147         res = super(account_invoice_line, self).product_id_change(cr, uid, ids, product, uom_id, qty, name, type, partner_id, fposition_id, price_unit, currency_id, company_id, context)
148         if not product:
149             return res
150         if type in ('in_invoice','in_refund'):
151             product_obj = self.pool.get('product.product').browse(cr, uid, product, context=context)
152             if type == 'in_invoice':
153                 oa = product_obj.property_stock_account_input and product_obj.property_stock_account_input.id
154                 if not oa:
155                     oa = product_obj.categ_id.property_stock_account_input_categ and product_obj.categ_id.property_stock_account_input_categ.id
156             else:
157                 oa = product_obj.property_stock_account_output and product_obj.property_stock_account_output.id
158                 if not oa:
159                     oa = product_obj.categ_id.property_stock_account_output_categ and product_obj.categ_id.property_stock_account_output_categ.id
160             if oa:
161                 fpos = fposition_id and fiscal_pool.browse(cr, uid, fposition_id, context=context) or False
162                 a = fiscal_pool.map_account(cr, uid, fpos, oa)
163                 res['value'].update({'account_id':a})
164         return res
165
166 class account_invoice(osv.osv):
167     _inherit = "account.invoice"
168
169     def _prepare_refund(self, cr, uid, invoice, date=None, period_id=None, description=None, journal_id=None, context=None):
170         invoice_data = super(account_invoice, self)._prepare_refund(cr, uid, invoice, date, period_id,
171                                                                     description, journal_id, context=context)
172         if invoice.type == 'in_invoice':
173             fiscal_position = self.pool.get('account.fiscal.position')
174             for _, _, line_dict in invoice_data['invoice_line']:
175                 if line_dict.get('product_id'):
176                     product = self.pool.get('product.product').browse(cr, uid, line_dict['product_id'], context=context)
177                     counterpart_acct_id = product.property_stock_account_output and \
178                             product.property_stock_account_output.id
179                     if not counterpart_acct_id:
180                         counterpart_acct_id = product.categ_id.property_stock_account_output_categ and \
181                                 product.categ_id.property_stock_account_output_categ.id
182                     if counterpart_acct_id:
183                         fpos = invoice.fiscal_position or False
184                         line_dict['account_id'] = fiscal_position.map_account(cr, uid,
185                                                                               fpos,
186                                                                               counterpart_acct_id)
187         return invoice_data
188
189 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: