Skip to content
Permalink
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/purchase.py
  2. +10 −10 addons/purchase_stock/models/stock_rule.py
@@ -84,7 +84,12 @@ def write(self, vals):
@api.multi
def button_approve(self, force=False):
result = super(PurchaseOrder, self).button_approve(force=force)
self._create_picking()
if not self.group_id:
self.group_id = self.group_id.create({
'name': self.name,
'partner_id': self.partner_id.id
})
self.order_line._create_moves()
return result

@api.multi
@@ -175,45 +180,6 @@ def _get_destination_location(self):
return self.dest_address_id.property_stock_customer.id
return self.picking_type_id.default_location_dest_id.id

@api.model
def _prepare_picking(self):
if not self.group_id:
self.group_id = self.group_id.create({
'name': self.name,
'partner_id': self.partner_id.id
})
if not self.partner_id.property_stock_supplier.id:
raise UserError(_("You must set a Vendor Location for this partner %s") % self.partner_id.name)
return {
'picking_type_id': self.picking_type_id.id,
'partner_id': self.partner_id.id,
'date': self.date_order,
'origin': self.name,
'location_dest_id': self._get_destination_location(),
'location_id': self.partner_id.property_stock_supplier.id,
'company_id': self.company_id.id,
}

@api.multi
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({
'name': self.name,
'partner_id': self.partner_id.id
})
if not self.partner_id.property_stock_supplier.id:
raise UserError(_("You must set a Vendor Location for this partner %s") % self.partner_id.name)
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
moves._action_assign()
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':
line._create_or_update_picking()
line._create_moves()
return line

@api.multi
@@ -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
# --------------------------------------------------

@api.multi
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):
})
activity._onchange_activity_type_id()

# 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:
self.env['stock.move']\
.create(move_val)\
._action_confirm()\
._action_assign()
moves = self.env['stock.move'].create(move_vals)
moves._action_confirm()
moves._action_assign()

@api.multi
def _get_stock_move_price_unit(self):
@@ -369,17 +327,10 @@ def _prepare_stock_moves(self):
res.append(template)
return res

@api.multi
def _create_stock_moves(self):
values = []
for line in self:
for val in line._prepare_stock_moves():
values.append(val)
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: l.product_id.id)
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: l.product_id.id)
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)
po_line.write(vals)
else:
@@ -108,8 +108,8 @@ def _run_buy(self, procurements):
# order to create it in batch.
partner = procurement.values['supplier'].name
po_line_values.append(self._prepare_purchase_order_line(
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))
self.env['purchase.order.line'].sudo().create(po_line_values)

@@ -165,13 +165,14 @@ def _merge_procurements(self, procurements_to_merge):
})
merged_procurement = self.env['procurement.group'].Procurement(
procurement.product_id, quantity, procurement.product_uom,
procurement.location_id, procurement.name, procurement.origin,
procurement.location_id,
procurement.name, procurement.origin,
procurement.company_id, values
)
merged_procurements.append(merged_procurement)
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,
'location_dest_id': self.location_id.id,
'location_dest_id': location_id.id,
'move_dest_ids': [(4, x.id) for x in values.get('move_dest_ids', [])]
}

@api.model
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,
'product_id': product_id.id,
'product_uom': product_id.uom_po_id.id,
'price_unit': price_unit,
'date_planned': date_planned,
'location_dest_id': self.location_id.id,
'location_dest_id': location_id.id,
'orderpoint_id': values.get('orderpoint_id', False) and values.get('orderpoint_id').id,
'taxes_id': [(6, 0, taxes_id.ids)],
'order_id': po.id,

0 comments on commit 809f07a

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