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