From 471f5e08bf717219c3677d705f7af1a3548f3b6d Mon Sep 17 00:00:00 2001 From: Lorenzo Battistini Date: Fri, 10 May 2013 11:53:19 +0200 Subject: [PATCH] [add] do_change_standard_price --- stock_lot_valuation/stock.py | 115 ++++++++++++++++++++++++++++++++++- 1 file changed, 114 insertions(+), 1 deletion(-) diff --git a/stock_lot_valuation/stock.py b/stock_lot_valuation/stock.py index bc80286c0..5918da384 100644 --- a/stock_lot_valuation/stock.py +++ b/stock_lot_valuation/stock.py @@ -47,6 +47,119 @@ class stock_production_lot(osv.osv): lot.company_id.currency_id.id, context['currency_id'], res[lot.id],context=context) return res + + def do_change_standard_price(self, cr, uid, ids, datas, context=None): + """ Changes the Standard Price of Lot and creates an account move accordingly. + @param datas : dict. contain default datas like new_price, stock_output_account, stock_input_account, stock_journal + @param context: A standard dictionary + """ + location_obj = self.pool.get('stock.location') + move_obj = self.pool.get('account.move') + move_line_obj = self.pool.get('account.move.line') + if context is None: + context = {} + + new_price = datas.get('new_price', 0.0) + stock_output_acc = datas.get('stock_output_account', False) + stock_input_acc = datas.get('stock_input_account', False) + journal_id = datas.get('stock_journal', False) + lot_obj=self.browse(cr, uid, ids, context=context)[0] + account_valuation = lot_obj.product_it.categ_id.property_stock_valuation_account_id + account_valuation_id = account_valuation and account_valuation.id or False + if not account_valuation_id: raise osv.except_osv(_('Error!'), _('Specify valuation Account for Product Category: %s.') % (lot_obj.product_it.categ_id.name)) + move_ids = [] + loc_ids = location_obj.search(cr, uid,[('usage','=','internal')]) + for rec_id in ids: + for location in location_obj.browse(cr, uid, loc_ids, context=context): + c = context.copy() + c.update({ + 'location': location.id, + 'compute_child': False + }) + + lot = self.browse(cr, uid, rec_id, context=c) + qty = lot.stock_available + diff = lot.standard_price - new_price + if not diff: raise osv.except_osv(_('Error!'), _("No difference between standard price and new price!")) + if qty: + company_id = location.company_id and location.company_id.id or False + if not company_id: raise osv.except_osv(_('Error!'), _('Please specify company in Location.')) + # + # Accounting Entries + # + product = lot.product_id + if not journal_id: + journal_id = product.categ_id.property_stock_journal and product.categ_id.property_stock_journal.id or False + if not journal_id: + raise osv.except_osv(_('Error!'), + _('Please define journal '\ + 'on the product category: "%s" (id: %d).') % \ + (product.categ_id.name, + product.categ_id.id,)) + move_id = move_obj.create(cr, uid, { + 'journal_id': journal_id, + 'company_id': company_id + }) + + move_ids.append(move_id) + + + if diff > 0: + if not stock_input_acc: + stock_input_acc = product.\ + property_stock_account_input.id + if not stock_input_acc: + stock_input_acc = product.categ_id.\ + property_stock_account_input_categ.id + if not stock_input_acc: + raise osv.except_osv(_('Error!'), + _('Please define stock input account ' \ + 'for this product: "%s" (id: %d).') % \ + (product.name, + product.id,)) + amount_diff = qty * diff + move_line_obj.create(cr, uid, { + 'name': product.name, + 'account_id': stock_input_acc, + 'debit': amount_diff, + 'move_id': move_id, + }) + move_line_obj.create(cr, uid, { + 'name': product.categ_id.name, + 'account_id': account_valuation_id, + 'credit': amount_diff, + 'move_id': move_id + }) + elif diff < 0: + if not stock_output_acc: + stock_output_acc = product.\ + property_stock_account_output.id + if not stock_output_acc: + stock_output_acc = product.categ_id.\ + property_stock_account_output_categ.id + if not stock_output_acc: + raise osv.except_osv(_('Error!'), + _('Please define stock output account ' \ + 'for this product: "%s" (id: %d).') % \ + (product.name, + product.id,)) + amount_diff = qty * -diff + move_line_obj.create(cr, uid, { + 'name': product.name, + 'account_id': stock_output_acc, + 'credit': amount_diff, + 'move_id': move_id + }) + move_line_obj.create(cr, uid, { + 'name': product.categ_id.name, + 'account_id': account_valuation_id, + 'debit': amount_diff, + 'move_id': move_id + }) + + self.write(cr, uid, rec_id, {'standard_price': new_price}) + + return move_ids class stock_move(orm.Model): _inherit = "stock.move" @@ -55,7 +168,7 @@ class stock_move(orm.Model): res = super(stock_move,self)._get_reference_accounting_values_for_valuation( cr, uid, move, context=context) if not move.product_id.cost_method == 'average' or not move.price_unit: - if move.prodlot_id: + if move.product_id.lot_valuation and move.prodlot_id: if context is None: context = {} currency_ctx = dict(context, currency_id = move.company_id.currency_id.id)