Skip to content

Commit

Permalink
Merge PR #791 into 12.0
Browse files Browse the repository at this point in the history
Signed-off-by hveficent
  • Loading branch information
OCA-git-bot committed Nov 18, 2019
2 parents 826da2e + 00434b2 commit 19cc547
Show file tree
Hide file tree
Showing 12 changed files with 284 additions and 85 deletions.
36 changes: 22 additions & 14 deletions purchase_request/migrations/12.0.1.0.0/post-migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,13 @@ def create_service_allocation(env, po_line, pr_line, qty):
return alloc


def allocate_from_stock_move(ml, ml_done=None):
def allocate_from_stock_move(env, ml, alloc_uom, ml_done=None):
# done here because open_product_qty is zero so cannot call method in
# stock_move_line
if ml_done is None:
ml_done = []
ml.product_uom_id._compute_quantity(
ml.qty_done, ml.product_id.uom_id)
to_allocate_qty = ml.qty_done
to_allocate_qty = ml.product_uom_id._compute_quantity(
ml.qty_done, alloc_uom)
for allocation in \
ml.filtered(
lambda m: m.id not in ml_done).move_id.\
Expand All @@ -56,7 +55,7 @@ def allocate_stockable(env):
'and consumables')
openupgrade.logged_query(cr, """
SELECT rel.purchase_request_line_id, rel.purchase_order_line_id, sm.id,
sm.product_qty, sm.product_uom_qty, prl.product_qty
sm.product_qty, prl.product_qty, sm.product_uom
FROM purchase_request_purchase_order_line_rel rel
INNER JOIN purchase_request_line prl ON prl.id =
rel.purchase_request_line_id
Expand All @@ -71,9 +70,11 @@ def allocate_stockable(env):
res = cr.fetchall()
ml_done = []
for (purchase_request_line_id, purchase_order_line_id, sm_id, product_qty,
product_uom_qty, req_qty) in res:
req_qty, sm_uom_id) in res:
purchase_request_line = env['purchase.request.line'].browse(
purchase_request_line_id)
sm_uom = env['uom.uom'].browse(sm_uom_id)
alloc_uom = purchase_request_line.product_uom_id or sm_uom
pending_qty = purchase_request_line.product_qty - \
purchase_request_line.qty_done
if not pending_qty:
Expand All @@ -82,16 +83,18 @@ def allocate_stockable(env):
# we allocated what is in the stock move
create_allocation(
env, purchase_order_line_id, purchase_request_line_id,
sm_id, pending_qty)
sm_id, pending_qty, alloc_uom)
# cannot call super, open_qty is zero
sm = env['stock.move'].browse(sm_id)
if sm.state == 'done':
ml_done = allocate_from_stock_move(sm.move_line_ids, ml_done)
ml_done = allocate_from_stock_move(env, sm.move_line_ids,
alloc_uom, ml_done)
else:
# we allocated what is in the PR line
req_qty = sm_uom._compute_quantity(req_qty, alloc_uom)
create_allocation(
env, purchase_order_line_id, purchase_request_line_id,
False, req_qty)
False, req_qty, alloc_uom)
purchase_request_line._compute_qty()


Expand All @@ -101,7 +104,7 @@ def allocate_service(env):
logger.info('Allocating purchase request for services')
openupgrade.logged_query(cr, """
SELECT rel.purchase_request_line_id, rel.purchase_order_line_id,
pol.product_qty, pol.product_uom_qty
pol.product_qty, pol.product_uom
FROM purchase_request_purchase_order_line_rel rel
INNER JOIN purchase_request_line prl ON prl.id =
rel.purchase_request_line_id
Expand All @@ -113,15 +116,20 @@ def allocate_service(env):
""")
res = cr.fetchall()
for (purchase_request_line_id, purchase_order_line_id, product_qty,
product_uom_qty) in res:
pol_product_uom) in res:
purchase_request_line = env['purchase.request.line'].browse(
purchase_request_line_id)
product_alloc_uom = \
purchase_request_line.product_uom_id or pol_product_uom
product_alloc_qty = pol_product_uom._compute_quantity(
product_qty, product_alloc_uom)
alloc = create_service_allocation(
env, purchase_order_line_id, purchase_request_line_id,
product_qty)
product_alloc_qty)
pol = env['purchase.order.line'].browse(purchase_order_line_id)
pol.with_context(no_notify=True).update_service_allocations(0.0)
alloc._compute_open_product_qty()
env['purchase.request.line'].browse(
purchase_request_line_id)._compute_qty()
purchase_request_line._compute_qty()


@openupgrade.migrate(use_env=True)
Expand Down
14 changes: 14 additions & 0 deletions purchase_request/models/purchase_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,20 @@ def button_confirm(self):
self._purchase_request_confirm_message()
return res

@api.multi
def unlink(self):
alloc_to_unlink = self.env['purchase.request.allocation']
for rec in self:
for alloc in rec.order_line.mapped(
'purchase_request_lines').mapped(
'purchase_request_allocation_ids').filtered(
lambda alloc: alloc.purchase_line_id.order_id.id == rec.id
):
alloc_to_unlink += alloc
res = super().unlink()
alloc_to_unlink.unlink()
return res


class PurchaseOrderLine(models.Model):
_inherit = "purchase.order.line"
Expand Down
42 changes: 9 additions & 33 deletions purchase_request/models/purchase_request_allocation.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,13 @@ class PurchaseRequestAllocation(models.Model):
product_uom_id = fields.Many2one(
string='UoM', comodel_name='uom.uom',
related='purchase_request_line_id.product_uom_id',
readonly=True,
readonly=True, required=True
)
requested_product_uom_qty = fields.Float(
'Requested Quantity (UoM)',
'Requested Quantity',
help='Quantity of the purchase request line allocated to the'
'stock move, in the UoM of the Purchase Request Line',
)
requested_product_qty = fields.Float(
'Requested Quantity',
help='Quantity of the purchase request line allocated to the stock'
'move, in the default UoM of the product',
compute='_compute_requested_product_qty'
)

allocated_product_qty = fields.Float(
'Allocated Quantity',
Expand All @@ -62,45 +56,27 @@ class PurchaseRequestAllocation(models.Model):
open_product_qty = fields.Float('Open Quantity',
compute='_compute_open_product_qty')

@api.depends('purchase_request_line_id.product_id',
'purchase_request_line_id.product_uom_id',
'purchase_request_line_id')
def _compute_requested_product_qty(self):
for rec in self:
if not rec.product_uom_id:
rec.requested_product_qty = rec.requested_product_uom_qty
else:
rec.requested_product_qty = \
rec.product_uom_id._compute_quantity(
rec.requested_product_uom_qty, rec.product_id.uom_id)
purchase_state = fields.Selection(
related='purchase_line_id.state'
)

@api.depends('requested_product_qty', 'allocated_product_qty',
@api.depends('requested_product_uom_qty', 'allocated_product_qty',
'stock_move_id', 'stock_move_id.state',
'stock_move_id.product_uom_qty',
'stock_move_id.move_line_ids.qty_done',
'purchase_line_id',
'purchase_line_id.qty_received',
'purchase_line_id.state')
'purchase_state')
def _compute_open_product_qty(self):
for rec in self:
if rec.purchase_line_id.state in ['cancel', 'done']:
if rec.purchase_state in ['cancel', 'done']:
rec.open_product_qty = 0.0
else:
rec.open_product_qty = \
rec.requested_product_qty - rec.allocated_product_qty
rec.requested_product_uom_qty - rec.allocated_product_qty
if rec.open_product_qty < 0.0:
rec.open_product_qty = 0.0

def _split(self, new_stock_move_id):
new_stock_move = self.env['stock.move'].browse(new_stock_move_id)
for rec in self:
if not rec.requested_product_qty > rec.allocated_product_qty:
continue
new_alloc = rec.copy()
new_alloc.stock_move_id = new_stock_move_id
new_alloc.requested_product_qty = new_stock_move.product_uom_qty
rec.requested_product_qty = rec.allocated_product_qty

@api.model
def _purchase_request_confirm_done_message_content(self, message_data):
message = ''
Expand Down
31 changes: 14 additions & 17 deletions purchase_request/models/purchase_request_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,9 @@ class PurchaseRequestLine(models.Model):
@api.depends('purchase_request_allocation_ids',
'purchase_request_allocation_ids.stock_move_id.state',
'purchase_request_allocation_ids.stock_move_id',
'purchase_request_allocation_ids.purchase_line_id',
'purchase_request_allocation_ids.purchase_line_id.state',
'purchase_request_allocation_ids.purchase_line_id')
'request_id.state')
def _compute_qty_to_buy(self):
for pr in self:
qty_to_buy = sum(pr.mapped('product_qty')) - \
Expand All @@ -150,15 +151,8 @@ def _compute_qty(self):
open_qty = sum(
request.purchase_request_allocation_ids.mapped(
'open_product_qty'))
if request.product_uom_id:
request.qty_done = request.product_id.uom_id._compute_quantity(
done_qty, request.product_uom_id)
request.qty_in_progress = \
request.product_id.uom_id._compute_quantity(
open_qty, request.product_uom_id)
else:
request.qty_done = done_qty
request.qty_in_progress = open_qty
request.qty_done = done_qty
request.qty_in_progress = open_qty

@api.depends('purchase_request_allocation_ids',
'purchase_request_allocation_ids.stock_move_id.state',
Expand Down Expand Up @@ -336,20 +330,23 @@ def _get_supplier_min_qty(self, product, partner_id=False):
def _calc_new_qty(self, request_line, po_line=None,
new_pr_line=False):
purchase_uom = po_line.product_uom or request_line.product_id.uom_po_id
uom = request_line.product_uom_id
qty = uom._compute_quantity(request_line.product_qty, purchase_uom)
# Make sure we use the minimum quantity of the partner corresponding
# to the PO. This does not apply in case of dropshipping
# TODO: Not implemented yet.
# Make sure we use the minimum quantity of the partner corresponding
# to the PO. This does not apply in case of dropshipping
supplierinfo_min_qty = 0.0
if not po_line.order_id.dest_address_id:
supplierinfo_min_qty = self._get_supplier_min_qty(
po_line.product_id, po_line.order_id.partner_id)

rl_qty = 0.0
# Recompute quantity by adding existing running procurements.
for rl in po_line.purchase_request_lines:
rl_qty += rl.product_uom_id._compute_quantity(
rl.product_qty, purchase_uom)
if new_pr_line:
rl_qty = po_line.product_uom_qty
else:
for prl in po_line.purchase_request_lines:
for alloc in prl.purchase_request_allocation_ids:
rl_qty += alloc.product_uom_id._compute_quantity(
alloc.requested_product_uom_qty, purchase_uom)
qty = max(rl_qty, supplierinfo_min_qty)
return qty

Expand Down
2 changes: 1 addition & 1 deletion purchase_request/models/stock_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def copy_data(self, default=None):
default['purchase_request_allocation_ids'] = []
first_it = True
for alloc in self.purchase_request_allocation_ids.filtered(
lambda al: al.requested_product_qty >
lambda al: al.requested_product_uom_qty >
al.allocated_product_qty):
qty_done = sum(
alloc.stock_move_id.mapped('move_line_ids.qty_done'))
Expand Down
12 changes: 8 additions & 4 deletions purchase_request/models/stock_move_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,20 +61,24 @@ def allocate(self):
for ml in self.filtered(
lambda m: m.exists() and
m.move_id.purchase_request_allocation_ids):
ml.product_uom_id._compute_quantity(
ml.qty_done, ml.product_id.uom_id)

# We do sudo because potentially the user that completes the move
# may not have permissions for purchase.request.
to_allocate_qty = ml.qty_done
to_allocate_uom = ml.product_uom_id
for allocation in \
ml.move_id.purchase_request_allocation_ids.sudo():
allocated_qty = 0.0
if allocation.open_product_qty and to_allocate_qty:
to_allocate_uom_qty = to_allocate_uom._compute_quantity(
to_allocate_qty, allocation.product_uom_id)
allocated_qty = min(
allocation.open_product_qty, to_allocate_qty)
allocation.open_product_qty, to_allocate_uom_qty)
allocation.allocated_product_qty += allocated_qty
to_allocate_qty -= allocated_qty
to_allocate_uom_qty -= allocated_qty
to_allocate_qty = allocation.product_uom_id. \
_compute_quantity(to_allocate_uom_qty, to_allocate_uom)

request = allocation.purchase_request_line_id.request_id
if allocated_qty:
message_data = self._prepare_message_data(ml, request,
Expand Down
2 changes: 1 addition & 1 deletion purchase_request/readme/CONFIGURE.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ To configure the product follow this steps:

With this configuration, whenever a procurement order is created and the supply
rule selected is 'Buy' the application will create a Purchase Request instead
of a Purchase Order.
of a Purchase Order.

0 comments on commit 19cc547

Please sign in to comment.