1 ##############################################################################
3 # OpenERP, Open Source Management Solution
5 # 2004-2010 Tiny SPRL (<http://tiny.be>).
6 # 2009-2010 Veritos (http://veritos.nl).
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.
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.
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/>.
22 ##############################################################################
24 from openerp.osv import osv
26 class account_invoice_line(osv.osv):
27 _inherit = "account.invoice.line"
29 def move_line_get(self, cr, uid, invoice_id, context=None):
30 res = super(account_invoice_line,self).move_line_get(cr, uid, invoice_id, context=context)
31 inv = self.pool.get('account.invoice').browse(cr, uid, invoice_id, context=context)
32 company_currency = inv.company_id.currency_id.id
33 def get_price(cr, uid, inv, company_currency,i_line):
34 cur_obj = self.pool.get('res.currency')
35 if inv.currency_id.id != company_currency:
36 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})
38 price = i_line.product_id.standard_price * i_line.quantity
41 if inv.type in ('out_invoice','out_refund'):
42 for i_line in inv.invoice_line:
43 if i_line.product_id and i_line.product_id.valuation == 'real_time':
44 if inv.type == 'out_invoice':
45 # debit account dacc will be the output account
46 # first check the product, if empty check the category
47 dacc = i_line.product_id.property_stock_account_output and i_line.product_id.property_stock_account_output.id
49 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
52 # debit account dacc will be the input account
53 # first check the product, if empty check the category
54 dacc = i_line.product_id.property_stock_account_input and i_line.product_id.property_stock_account_input.id
56 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
57 # in both cases the credit account cacc will be the expense account
58 # first check the product, if empty check the category
59 cacc = i_line.product_id.property_account_expense and i_line.product_id.property_account_expense.id
61 cacc = i_line.product_id.categ_id.property_account_expense_categ and i_line.product_id.categ_id.property_account_expense_categ.id
65 'name': i_line.name[:64],
66 'price_unit':i_line.product_id.standard_price,
67 'quantity':i_line.quantity,
68 'price':get_price(cr, uid, inv, company_currency, i_line),
70 'product_id':i_line.product_id.id,
71 'uos_id':i_line.uos_id.id,
72 'account_analytic_id': False,
73 'taxes':i_line.invoice_line_tax_id,
78 'name': i_line.name[:64],
79 'price_unit':i_line.product_id.standard_price,
80 'quantity':i_line.quantity,
81 'price': -1 * get_price(cr, uid, inv, company_currency, i_line),
83 'product_id':i_line.product_id.id,
84 'uos_id':i_line.uos_id.id,
85 'account_analytic_id': False,
86 'taxes':i_line.invoice_line_tax_id,
88 elif inv.type in ('in_invoice','in_refund'):
89 for i_line in inv.invoice_line:
90 if i_line.product_id and i_line.product_id.valuation == 'real_time':
91 if i_line.product_id.type != 'service':
92 # get the price difference account at the product
93 acc = i_line.product_id.property_account_creditor_price_difference and i_line.product_id.property_account_creditor_price_difference.id
95 # if not found on the product get the price difference account at the category
96 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
98 if inv.type == 'in_invoice':
99 # oa will be the stock input account
100 # first check the product, if empty check the category
101 oa = i_line.product_id.property_stock_account_input and i_line.product_id.property_stock_account_input.id
103 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
106 # oa will be the stock output account
107 # first check the product, if empty check the category
108 oa = i_line.product_id.property_stock_account_output and i_line.product_id.property_stock_account_output.id
110 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
112 # get the fiscal position
113 fpos = i_line.invoice_id.fiscal_position or False
114 a = self.pool.get('account.fiscal.position').map_account(cr, uid, fpos, oa)
116 # calculate and write down the possible price difference between invoice price and product price
118 if a == line['account_id'] and i_line.product_id.id == line['product_id']:
119 uom = i_line.product_id.uos_id or i_line.product_id.uom_id
120 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)
121 if i_line.product_id.cost_method != 'standard' and i_line.purchase_line_id:
122 #for average/fifo/lifo costing method, fetch real cost price from incomming moves
123 stock_move_obj = self.pool.get('stock.move')
124 valuation_stock_move = stock_move_obj.search(cr, uid, [('purchase_line_id', '=', i_line.purchase_line_id.id)], limit=1, context=context)
125 if valuation_stock_move:
126 valuation_price_unit = stock_move_obj.browse(cr, uid, valuation_stock_move[0], context=context).price_unit
127 if valuation_price_unit != i_line.price_unit and line['price_unit'] == i_line.price_unit and acc:
128 price_diff = i_line.price_unit - valuation_price_unit
129 line.update({'price': valuation_price_unit * line['quantity']})
132 'name': i_line.name[:64],
133 'price_unit': price_diff,
134 'quantity': line['quantity'],
135 'price': price_diff * line['quantity'],
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', []),
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, context=None, company_id=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, context, company_id)
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
155 oa = product_obj.categ_id.property_stock_account_input_categ and product_obj.categ_id.property_stock_account_input_categ.id
157 oa = product_obj.property_stock_account_output and product_obj.property_stock_account_output.id
159 oa = product_obj.categ_id.property_stock_account_output_categ and product_obj.categ_id.property_stock_account_output_categ.id
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})
166 class account_invoice(osv.osv):
167 _inherit = "account.invoice"
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,
189 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: