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