8a816b0c962c19e79c271b2eb564d7c9d4ba432c
[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
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     def move_line_get(self, cr, uid, invoice_id, context=None):
31         res = super(account_invoice_line,self).move_line_get(cr, uid, invoice_id, context=context)
32         inv = self.pool.get('account.invoice').browse(cr, uid, invoice_id, context=context)
33         company_currency = inv.company_id.currency_id.id
34         def get_price(cr, uid, inv, company_currency,i_line):
35             cur_obj = self.pool.get('res.currency')
36             decimal_precision = self.pool.get('decimal.precision')
37             if inv.currency_id.id != company_currency:
38                 price = cur_obj.compute(cr, uid, company_currency, inv.currency_id.id, i_line.product_id.standard_price * i_line.quantity, context={'date': inv.date_invoice})
39             else:
40                 price = i_line.product_id.standard_price * i_line.quantity
41             return round(price, decimal_precision.precision_get(cr, uid, 'Account'))
42
43         if inv.type in ('out_invoice','out_refund'):
44             for i_line in inv.invoice_line:
45                 if i_line.product_id and i_line.product_id.valuation == 'real_time':
46                     # debit account dacc will be the output account
47                     # first check the product, if empty check the category
48                     dacc = i_line.product_id.property_stock_account_output and i_line.product_id.property_stock_account_output.id
49                     if not dacc:
50                         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
51                     # in both cases the credit account cacc will be the expense account
52                     # first check the product, if empty check the category
53                     cacc = i_line.product_id.property_account_expense and i_line.product_id.property_account_expense.id
54                     if not cacc:
55                         cacc = i_line.product_id.categ_id.property_account_expense_categ and i_line.product_id.categ_id.property_account_expense_categ.id
56                     if dacc and cacc:
57                         res.append({
58                             'type':'src',
59                             'name': i_line.name[:64],
60                             'price_unit':i_line.product_id.standard_price,
61                             'quantity':i_line.quantity,
62                             'price':get_price(cr, uid, inv, company_currency, i_line),
63                             'account_id':dacc,
64                             'product_id':i_line.product_id.id,
65                             'uos_id':i_line.uos_id.id,
66                             'account_analytic_id': False,
67                             'taxes':i_line.invoice_line_tax_id,
68                             })
69
70                         res.append({
71                             'type':'src',
72                             'name': i_line.name[:64],
73                             'price_unit':i_line.product_id.standard_price,
74                             'quantity':i_line.quantity,
75                             'price': -1 * get_price(cr, uid, inv, company_currency, i_line),
76                             'account_id':cacc,
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         elif inv.type in ('in_invoice','in_refund'):
83             for i_line in inv.invoice_line:
84                 if i_line.product_id and i_line.product_id.valuation == 'real_time':
85                     if i_line.product_id.type != 'service':
86                         # get the price difference account at the product
87                         acc = i_line.product_id.property_account_creditor_price_difference and i_line.product_id.property_account_creditor_price_difference.id
88                         if not acc:
89                             # if not found on the product get the price difference account at the category
90                             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
91                         a = None
92
93                         # oa will be the stock input account
94                         # first check the product, if empty check the category
95                         oa = i_line.product_id.property_stock_account_input and i_line.product_id.property_stock_account_input.id
96                         if not oa:
97                             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
98                         if oa:
99                             # get the fiscal position
100                             fpos = i_line.invoice_id.fiscal_position or False
101                             a = self.pool.get('account.fiscal.position').map_account(cr, uid, fpos, oa)
102                         diff_res = []
103                         decimal_precision = self.pool.get('decimal.precision')
104                         account_prec = decimal_precision.precision_get(cr, uid, 'Account')
105                         # calculate and write down the possible price difference between invoice price and product price
106                         for line in res:
107                             if line.get('invl_id', 0) == i_line.id and a == line['account_id']:
108                                 uom = i_line.product_id.uos_id or i_line.product_id.uom_id
109                                 standard_price = self.pool.get('product.uom')._compute_price(cr, uid, uom.id, i_line.product_id.standard_price, i_line.uos_id.id)
110                                 if inv.currency_id.id != company_currency:
111                                     standard_price = self.pool.get('res.currency').compute(cr, uid, company_currency, inv.currency_id.id, standard_price, context={'date': inv.date_invoice})
112                                 if standard_price != i_line.price_unit and line['price_unit'] == i_line.price_unit and acc:
113                                     price_diff = round(i_line.price_unit - standard_price, account_prec)
114                                     line.update({'price': round(standard_price * line['quantity'], account_prec)})
115                                     diff_res.append({
116                                         'type':'src',
117                                         'name': i_line.name[:64],
118                                         'price_unit':price_diff,
119                                         'quantity':line['quantity'],
120                                         'price': round(price_diff * line['quantity'], account_prec),
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_id, qty=0, name='', type='out_invoice', partner_id=False, fposition_id=False, price_unit=False, currency_id=False, context=None, company_id=None):
131         fiscal_pool = self.pool.get('account.fiscal.position')
132         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)
133         if not product:
134             return res
135         if type in ('in_invoice','in_refund'):
136             product_obj = self.pool.get('product.product').browse(cr, uid, product, context=context)
137             if type == 'in_invoice':
138                 oa = product_obj.property_stock_account_input and product_obj.property_stock_account_input.id
139                 if not oa:
140                     oa = product_obj.categ_id.property_stock_account_input_categ and product_obj.categ_id.property_stock_account_input_categ.id
141             else:
142                 oa = product_obj.property_stock_account_output and product_obj.property_stock_account_output.id
143                 if not oa:
144                     oa = product_obj.categ_id.property_stock_account_output_categ and product_obj.categ_id.property_stock_account_output_categ.id
145             if oa:
146                 fpos = fposition_id and fiscal_pool.browse(cr, uid, fposition_id, context=context) or False
147                 a = fiscal_pool.map_account(cr, uid, fpos, oa)
148                 res['value'].update({'account_id':a})
149         return res
150
151 class account_invoice(osv.osv):
152     _inherit = "account.invoice"
153
154     def _prepare_refund(self, cr, uid, invoice, date=None, period_id=None, description=None, journal_id=None, context=None):
155         invoice_data = super(account_invoice, self)._prepare_refund(cr, uid, invoice, date, period_id,
156                                                                     description, journal_id, context=context)
157         if invoice.type == 'in_invoice':
158             fiscal_position = self.pool.get('account.fiscal.position')
159             for _, _, line_dict in invoice_data['invoice_line']:
160                 if line_dict.get('product_id'):
161                     product = self.pool.get('product.product').browse(cr, uid, line_dict['product_id'], context=context)
162                     counterpart_acct_id = product.property_stock_account_output and \
163                             product.property_stock_account_output.id
164                     if not counterpart_acct_id:
165                         counterpart_acct_id = product.categ_id.property_stock_account_output_categ and \
166                                 product.categ_id.property_stock_account_output_categ.id
167                     if counterpart_acct_id:
168                         fpos = invoice.fiscal_position or False
169                         line_dict['account_id'] = fiscal_position.map_account(cr, uid,
170                                                                               fpos,
171                                                                               counterpart_acct_id)
172         return invoice_data
173
174 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: