From 18e85709760b5241971bcdd4c71035fc1b9fc52f Mon Sep 17 00:00:00 2001 From: "Harry (Open ERP)" Date: Thu, 25 Mar 2010 17:38:31 +0530 Subject: [PATCH] [REF] stock bzr revid: hmo@tinyerp.com-20100325120831-jsnj6d63v1q0pmv6 --- addons/stock/product.py | 84 +++++++++++ addons/stock/stock.py | 135 ++++++++++++++++++ .../wizard/stock_change_standard_price.py | 77 ++-------- 3 files changed, 228 insertions(+), 68 deletions(-) diff --git a/addons/stock/product.py b/addons/stock/product.py index 3e789fa7ecd..cf7c5605b8f 100644 --- a/addons/stock/product.py +++ b/addons/stock/product.py @@ -25,6 +25,90 @@ from tools.translate import _ class product_product(osv.osv): _inherit = "product.product" + + def do_change_standard_price(self, cr, uid, ids, datas, context={}): + """ + Changes the Standard Price of Product. + And creates an account move accordingly. + + @param self: The object pointer. + @param cr: A database cursor + @param uid: ID of the user currently logged in + @param ids: List of IDs selected + @param datas : dict. contain default datas like new_price, stock_output_account, stock_input_account, stock_journal + @param context: A standard dictionary + + @return: + + """ + location_obj = self.pool.get('stock.location') + lot_obj = self.pool.get('stock.report.prodlots') + move_obj = self.pool.get('account.move') + move_line_obj = self.pool.get('account.move.line') + loc_ids = location_obj.search(cr, uid, [('account_id','<>',False),('usage','=','internal')]) + + 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) + + move_ids = [] + for rec_id in ids: + for location in location_obj.browse(cr, uid, loc_ids): + c = context.copy() + c.update({ + 'location': location.id, + 'compute_child': False + }) + + product = self.browse(cr, uid, rec_id, context=c) + qty = product.qty_available + diff = product.standard_price - new_price + assert diff, _("Could not find any difference between standard price and new price!") + if qty: + location_account = location.account_id and location.account_id.id or False + company_id = location.company_id and location.company_id.id or False + assert location_account, _('Inventory Account is not specified for Location: %s' % (location.name)) + assert company_id, _('Company is not specified in Location') + move_id = move_obj.create(cr, uid, { + 'journal_id': journal_id, + 'company_id': company_id + }) + + move_ids.append(move_id) + if diff > 0: + 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': location.name, + 'account_id': location_account, + 'credit': amount_diff, + 'move_id': move_id + }) + elif diff < 0: + 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': location.name, + 'account_id': location_account, + 'debit': amount_diff, + 'move_id': move_id + }) + + self.write(cr, uid, rec_id, {'standard_price': new_price}) + + return True + def view_header_get(self, cr, user, view_id, view_type, context): res = super(product_product, self).view_header_get(cr, user, view_id, view_type, context) if res: return res diff --git a/addons/stock/stock.py b/addons/stock/stock.py index 7a400b23138..5e684d90752 100644 --- a/addons/stock/stock.py +++ b/addons/stock/stock.py @@ -856,6 +856,141 @@ class stock_picking(osv.osv): continue return super(stock_picking, self).unlink(cr, uid, ids, context=context) + def do_partial(self, cr, uid, ids, partial_datas, context): + """ + @ partial_datas : dict. contain details of partial picking + like partner_id, address_id, delivery_date, delivery moves with product_id, product_qty, uom + """ + res = {} + move_obj = self.pool.get('stock.move') + delivery_obj = self.pool.get('stock.delivery') + product_obj = self.pool.get('product.product') + currency_obj = self.pool.get('res.currency') + users_obj = self.pool.get('res.users') + uom_obj = self.pool.get('product.uom') + price_type_obj = self.pool.get('product.price.type') + wf_service = netsvc.LocalService("workflow") + partner_id = partial_datas.get('partner_id', False) + address_id = partial_datas.get('address_id', False) + delivery_date = partial_datas.get('delivery_date', False) + for pick in self.browse(cr, uid, ids, context=context): + new_picking = None + new_moves = [] + + complete, too_many, too_few = [], [], [] + for move in pick.move_lines: + partial_data = partial_datas.get('move%s'%(move.id), False) + assert partial_data, _('Do not Found Partial data of Stock Move Line :%s' %(move.id)) + product_qty = partial_data.get('product_qty',0.0) + product_uom = partial_data.get('product_uom',False) + product_price = partial_data.get('product_price',0.0) + product_currency = partial_data.get('product_currency',False) + if move.product_qty == product_qty: + complete.append(move) + elif move.product_qty > product_qty: + too_few.append(move) + else: + too_many.append(move) + + # Average price computation + if (pick.type == 'in') and (move.product_id.cost_method == 'average'): + product = product_obj.browse(cr, uid, move.product_id.id) + user = users_obj.browse(cr, uid, uid) + + qty = uom_obj._compute_qty(cr, uid, product_uom, product_qty, product.uom_id.id) + pricetype = False + if user.company_id.property_valuation_price_type: + pricetype = price_type_obj.browse(cr, uid, user.company_id.property_valuation_price_type.id) + if pricetype and qty > 0: + new_price = currency_obj.compute(cr, uid, currency, + user.company_id.currency_id.id, product_price) + new_price = uom_obj._compute_price(cr, uid, product_uom, new_price, + product.uom_id.id) + if product.qty_available <= 0: + new_std_price = new_price + else: + # Get the standard price + amount_unit = product.price_get(pricetype.field, context)[product.id] + new_std_price = ((amount_unit * product.qty_available)\ + + (new_price * qty))/(product.qty_available + qty) + + # Write the field according to price type field + product_obj.write(cr, uid, [product.id], + {pricetype.field: new_std_price}) + move_obj.write(cr, uid, [move.id], {'price_unit': new_price}) + + delivery_id = delivery_obj.create(cr, uid, { + 'name': pick.name, + 'partner_id': partner_id, + 'address_id': address_id, + 'date': delivery_date, + 'picking_id':pick.id + }, context=context) + + for move in too_few: + if not new_picking: + + new_picking = self.copy(cr, uid, pick.id, + { + 'name': pool.get('ir.sequence').get(cr, uid, 'stock.picking.%s'%(pick.type)), + 'move_lines' : [], + 'state':'draft', + }) + if product_qty != 0: + + new_obj = move_obj.copy(cr, uid, move.id, + { + 'product_qty' : product_qty, + 'product_uos_qty': product_qty, #TODO: put correct uos_qty + 'picking_id' : new_picking, + 'delivery_id' : delivery_id, + 'state': 'assigned', + 'move_dest_id': False, + 'price_unit': move.price_unit, + }) + + move_obj.write(cr, uid, [move.id], + { + 'product_qty' : move.product_qty - product_qty, + 'product_uos_qty':move.product_qty - product_qty, #TODO: put correct uos_qty + + }) + + if new_picking: + move_obj.write(cr, uid, [c.id for c in complete], {'picking_id': new_picking}) + for move in too_many: + move_obj.write(cr, uid, [move.id], + { + 'product_qty' : product_qty, + 'product_uos_qty': product_qty, #TODO: put correct uos_qty + 'picking_id': new_picking, + }) + else: + for move in too_many: + move_obj.write(cr, uid, [move.id], + { + 'product_qty': product_qty, + 'product_uos_qty': product_qty + }) + + # At first we confirm the new picking (if necessary) + if new_picking: + wf_service.trg_validate(uid, 'stock.picking', new_picking, 'button_confirm', cr) + # Then we finish the good picking + if new_picking: + self.write(cr, uid, [pick.id], {'backorder_id': new_picking}) + self.action_move(cr, uid, [new_picking]) + wf_service.trg_validate(uid, 'stock.picking', new_picking, 'button_done', cr) + wf_service.trg_write(uid, 'stock.picking', pick.id, cr) + else: + self.action_move(cr, uid, [pick.id]) + wf_service.trg_validate(uid, 'stock.picking', pick.id, 'button_done', cr) + bo_name = '' + if new_picking: + bo_name = pick_obj.read(cr, uid, [new_picking], ['name'])[0]['name'] + res[pick.id] = {'new_picking': new_picking or False, 'back_order':bo_name} + return res + stock_picking() diff --git a/addons/stock/wizard/stock_change_standard_price.py b/addons/stock/wizard/stock_change_standard_price.py index 8454d1e32ff..337a59f1624 100644 --- a/addons/stock/wizard/stock_change_standard_price.py +++ b/addons/stock/wizard/stock_change_standard_price.py @@ -100,74 +100,15 @@ class change_standard_price(osv.osv_memory): rec_id = context and context.get('active_id', False) assert rec_id, _('Active ID is not set in Context') prod_obj = self.pool.get('product.product') - location_obj = self.pool.get('stock.location') - lot_obj = self.pool.get('stock.report.prodlots') - move_obj = self.pool.get('account.move') - move_line_obj = self.pool.get('account.move.line') - data_obj = self.pool.get('ir.model.data') - - res = self.browse(cr, uid, ids) - - loc_ids = location_obj.search(cr, uid, [('account_id','<>',False),('usage','=','internal')]) - - new_price = res[0].new_price - stock_output_acc = res[0].stock_account_output.id - stock_input_acc = res[0].stock_account_input.id - journal_id = res[0].stock_journal.id - - move_ids = [] - for location in location_obj.browse(cr, uid, loc_ids): - c = context.copy() - c.update({'location': location.id}) - c.update({'compute_child': False}) - - product = prod_obj.browse(cr, uid, rec_id, context=c) - qty = product.qty_available - diff = product.standard_price - new_price - assert diff, _("Could not find any difference between standard price and new price!") - if qty: - location_account = location.account_id and location.account_id.id or False - company_id = location.company_id and location.company_id.id or False - assert location_account, _('Inventory Account is not specified for Location: %s' % (location.name)) - assert company_id, _('Company is not specified in Location') - move_id = move_obj.create(cr, uid, { - 'journal_id': journal_id, - 'company_id': company_id - }) - - move_ids.append(move_id) - if diff > 0: - 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': location.name, - 'account_id': location_account, - 'credit': amount_diff, - 'move_id': move_id - }) - elif diff < 0: - 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': location.name, - 'account_id': location_account, - 'debit': amount_diff, - 'move_id': move_id - }) - - prod_obj.write(cr, uid, rec_id, {'standard_price': new_price}) - - return { } + res = self.browse(cr, uid, ids) + datas = { + 'new_price' : res[0].new_price, + 'stock_output_account' : res[0].stock_account_output.id, + 'stock_input_account' : res[0].stock_account_input.id, + 'stock_journal' : res[0].stock_journal.id + } + prod_obj.do_change_standard_price(cr, uid, [rec_id], datas, context) + return {} change_standard_price()