Skip to content
Browse files

[IMP] purchase_stock: resolved conflicts and made some amendments

  • Loading branch information...
pch-odoo committed Mar 14, 2019
1 parent d652b64 commit 809f07aec0b8862c7d83e4389a88d7c788c32c6d
Showing with 24 additions and 73 deletions.
  1. +14 −63 addons/purchase_stock/models/
  2. +10 −10 addons/purchase_stock/models/
@@ -84,7 +84,12 @@ def write(self, vals):
def button_approve(self, force=False):
result = super(PurchaseOrder, self).button_approve(force=force)
if not self.group_id:
self.group_id = self.group_id.create({
return result

@@ -175,45 +180,6 @@ def _get_destination_location(self):

def _prepare_picking(self):
if not self.group_id:
self.group_id = self.group_id.create({
if not
raise UserError(_("You must set a Vendor Location for this partner %s") %
return {
'date': self.date_order,
'location_dest_id': self._get_destination_location(),

def _create_picking(self):
for order in self:
if any([ptype in ['product', 'consu'] for ptype in order.order_line.mapped('product_id.type')]):
if not self.group_id:
self.group_id = self.group_id.create({
if not
raise UserError(_("You must set a Vendor Location for this partner %s") %
moves = order.order_line._create_stock_moves()
moves = moves.filtered(lambda x: x.state not in ('done', 'cancel'))._action_confirm()
seq = 0
for move in sorted(moves, key=lambda move: move.date_expected):
seq += 5
move.sequence = seq
return True

class PurchaseOrderLine(models.Model):
_inherit = 'purchase.order.line'
@@ -256,7 +222,7 @@ def _compute_qty_received(self):
def create(self, values):
line = super(PurchaseOrderLine, self).create(values)
if line.order_id.state == 'purchase':
return line

@@ -268,15 +234,15 @@ def write(self, values):
('purchase_line_id', 'in', self.ids), ('state', '!=', 'done')
]).write({'date_expected': values['date_planned']})
if 'product_qty' in values:
self.filtered(lambda l: l.order_id.state == 'purchase')._create_or_update_picking()
self.filtered(lambda l: l.order_id.state == 'purchase')._create_moves()
return result

# --------------------------------------------------
# Business methods
# --------------------------------------------------

def _create_or_update_picking(self):
def _create_moves(self):
for line in self:
if line.product_id.type in ('product', 'consu'):
# Prevent decreasing below received quantity
@@ -295,18 +261,10 @@ def _create_or_update_picking(self):

# If the user increased quantity of existing line or created a new line
pickings = line.order_id.picking_ids.filtered(lambda x: x.state not in ('done', 'cancel') and x.location_dest_id.usage in ('internal', 'transit'))
picking = pickings and pickings[0] or False
if not picking:
res = line.order_id._prepare_picking()
picking = self.env['stock.picking'].create(res)
move_vals = line._prepare_stock_moves()
for move_val in move_vals:
moves = self.env['stock.move'].create(move_vals)

def _get_stock_move_price_unit(self):
@@ -369,17 +327,10 @@ def _prepare_stock_moves(self):
return res

def _create_stock_moves(self):
values = []
for line in self:
for val in line._prepare_stock_moves():
return self.env['stock.move'].create(values)

def _find_candidate(self, product_id, product_qty, product_uom, location_id, name, origin, company_id, values):
""" Return the record in self where the procument with values passed as
args can be merged. If it returns an empty record then a new line will
be created.
return self and self[0] or self.env['purchase.order.line']
lines = self.filtered(lambda x: x.location_dest_id == location_id)
return lines and lines[0] or self.env['purchase.order.line']
@@ -87,7 +87,7 @@ def _run_buy(self, procurements):
procurements = self._merge_procurements(procurements_to_merge)

po_lines_by_product = {}
grouped_po_lines = groupby(po.order_line.filtered(lambda l: l.product_uom == l.product_id.uom_po_id and l.location_dest_id == self.location_id).sorted('product_id'), key=lambda l:
grouped_po_lines = groupby(po.order_line.filtered(lambda l: l.product_uom == l.product_id.uom_po_id).sorted('product_id'), key=lambda l:
for product, po_lines in grouped_po_lines:
po_lines_by_product[product] = self.env['purchase.order.line'].concat(*list(po_lines))
po_line_values = []
@@ -99,7 +99,7 @@ def _run_buy(self, procurements):
# If the procurement can be merge in an existing line. Directly
# write the new values on it.
vals = self._update_purchase_order_line(procurement.product_id,
procurement.product_qty, procurement.product_uom, company_id,
procurement.product_qty, procurement.product_uom, procurement.location_id, company_id,
procurement.values, po_line)
@@ -108,8 +108,8 @@ def _run_buy(self, procurements):
# order to create it in batch.
partner = procurement.values['supplier'].name
procurement.product_id, procurement.product_qty,
procurement.product_uom, procurement.company_id,
procurement.product_id, procurement.product_qty,procurement.location_id,
procurement.product_uom, procurement.company_id,
procurement.values, po))

@@ -165,13 +165,14 @@ def _merge_procurements(self, procurements_to_merge):
merged_procurement = self.env[''].Procurement(
procurement.product_id, quantity, procurement.product_uom,
procurement.location_id,, procurement.origin,
procurement.location_id,, procurement.origin,
procurement.company_id, values
return merged_procurements

def _update_purchase_order_line(self, product_id, product_qty, product_uom, company_id, values, line):
def _update_purchase_order_line(self, product_id, product_qty, product_uom, location_id, company_id, values, line):
partner = values['supplier'].name
procurement_uom_po_qty = product_uom._compute_quantity(product_qty, product_id.uom_po_id)
seller = product_id._select_seller(
@@ -188,12 +189,12 @@ def _update_purchase_order_line(self, product_id, product_qty, product_uom, comp
return {
'product_qty': line.product_qty + procurement_uom_po_qty,
'price_unit': price_unit,
'move_dest_ids': [(4, for x in values.get('move_dest_ids', [])]

def _prepare_purchase_order_line(self, product_id, product_qty, product_uom, company_id, values, po):
def _prepare_purchase_order_line(self, product_id, product_qty, location_id, product_uom, company_id, values, po):
partner = values['supplier'].name
procurement_uom_po_qty = product_uom._compute_quantity(product_qty, product_id.uom_po_id)
# _select_seller is used if the supplier have different price depending
@@ -224,15 +225,14 @@ def _prepare_purchase_order_line(self, product_id, product_qty, product_uom, com
name += '\n' + product_lang.description_purchase

date_planned = self.env['purchase.order.line']._get_date_planned(seller, po=po)

return {
'name': name,
'product_qty': procurement_uom_po_qty,
'price_unit': price_unit,
'date_planned': date_planned,
'orderpoint_id': values.get('orderpoint_id', False) and values.get('orderpoint_id').id,
'taxes_id': [(6, 0, taxes_id.ids)],

0 comments on commit 809f07a

Please sign in to comment.
You can’t perform that action at this time.