Skip to content
Permalink
Browse files

[MOV] *: _prepare_purchase_order_line

Move the method `_prepare_purchase_order_line` from 'stock.rule' to
'purchase.order.line'. The purpose is to reuse it in approvals_purchase.

task-2040710

closes #41046

Related: odoo/enterprise#6702
Signed-off-by: Simon Lejeune (sle) <sle@openerp.com>
  • Loading branch information
svs-odoo committed Nov 28, 2019
1 parent 7037cf6 commit 621a93b7b723999d943f1c4da78547763498c008
@@ -755,3 +755,47 @@ def _prepare_account_move_line(self, move):
'tax_ids': [(6, 0, self.taxes_id.ids)],
'display_type': self.display_type,
}

@api.model
def _prepare_purchase_order_line(self, product_id, product_qty, product_uom, company_id, supplier, po):
partner = supplier.name
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
# the quantities ordered.
seller = product_id.with_company(company_id)._select_seller(
partner_id=partner,
quantity=uom_po_qty,
date=po.date_order and po.date_order.date(),
uom_id=product_id.uom_po_id)

taxes = product_id.supplier_taxes_id
fpos = po.fiscal_position_id
taxes_id = fpos.map_tax(taxes, product_id, seller.name)
if taxes_id:
taxes_id = taxes_id.filtered(lambda x: x.company_id.id == company_id.id)

price_unit = self.env['account.tax']._fix_tax_included_price_company(seller.price, product_id.supplier_taxes_id, taxes_id, company_id) if seller else 0.0
if price_unit and seller and po.currency_id and seller.currency_id != po.currency_id:
price_unit = seller.currency_id._convert(
price_unit, po.currency_id, po.company_id, po.date_order or fields.Date.today())

product_lang = product_id.with_context(
lang=partner.lang,
partner_id=partner.id,
)
name = product_lang.display_name
if product_lang.description_purchase:
name += '\n' + product_lang.description_purchase

date_planned = self._get_date_planned(seller, po=po)

return {
'name': name,
'product_qty': uom_po_qty,
'product_id': product_id.id,
'product_uom': product_id.uom_po_id.id,
'price_unit': price_unit,
'date_planned': date_planned,
'taxes_id': [(6, 0, taxes_id.ids)],
'order_id': po.id,
}
@@ -388,6 +388,17 @@ def _prepare_stock_moves(self, picking):
res.append(template)
return res

@api.model
def _prepare_purchase_order_line_from_procurement(self, product_id, product_qty, product_uom, company_id, values, po):
supplier = values.get('supplier')
res = self._prepare_purchase_order_line(product_id, product_qty, product_uom, company_id, supplier, po)
res['move_dest_ids'] = [(4, x.id) for x in values.get('move_dest_ids', [])]
res['orderpoint_id'] = values.get('orderpoint_id', False) and values.get('orderpoint_id').id
res['propagate_cancel'] = values.get('propagate_cancel')
res['propagate_date'] = values.get('propagate_date')
res['propagate_date_minimum_delta'] = values.get('propagate_date_minimum_delta')
return res

def _create_stock_moves(self, picking):
values = []
for line in self.filtered(lambda l: not l.display_type):
@@ -110,7 +110,7 @@ def _run_buy(self, procurements):
# Generate the create values for it and add it to a list in
# order to create it in batch.
partner = procurement.values['supplier'].name
po_line_values.append(self._prepare_purchase_order_line(
po_line_values.append(self.env['purchase.order.line']._prepare_purchase_order_line_from_procurement(
procurement.product_id, procurement.product_qty,
procurement.product_uom, procurement.company_id,
procurement.values, po))
@@ -198,55 +198,6 @@ def _update_purchase_order_line(self, product_id, product_qty, product_uom, comp
res['orderpoint_id'] = orderpoint_id.id
return res

@api.model
def _prepare_purchase_order_line(self, product_id, product_qty, 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
# the quantities ordered.
seller = product_id.with_company(company_id)._select_seller(
partner_id=partner,
quantity=procurement_uom_po_qty,
date=po.date_order and po.date_order.date(),
uom_id=product_id.uom_po_id)

taxes = product_id.supplier_taxes_id
fpos = po.fiscal_position_id
taxes_id = fpos.map_tax(taxes, product_id, seller.name)
if taxes_id:
taxes_id = taxes_id.filtered(lambda x: x.company_id.id == company_id.id)

price_unit = self.env['account.tax']._fix_tax_included_price_company(seller.price, product_id.supplier_taxes_id, taxes_id, company_id) if seller else 0.0
if price_unit and seller and po.currency_id and seller.currency_id != po.currency_id:
price_unit = seller.currency_id._convert(
price_unit, po.currency_id, po.company_id, po.date_order or fields.Date.today())

product_lang = product_id.with_context(
lang=partner.lang,
partner_id=partner.id,
)
name = product_lang.display_name
if product_lang.description_purchase:
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,
'propagate_cancel': values.get('propagate_cancel'),
'date_planned': date_planned,
'propagate_date': values['propagate_date'],
'propagate_date_minimum_delta': values['propagate_date_minimum_delta'],
'orderpoint_id': values.get('orderpoint_id', False) and values.get('orderpoint_id').id,
'taxes_id': [(6, 0, taxes_id.ids)],
'order_id': po.id,
'move_dest_ids': [(4, x.id) for x in values.get('move_dest_ids', [])],
}

def _prepare_purchase_order(self, company_id, origins, values):
""" Create a purchase order for procuremets that share the same domain
returned by _make_po_get_domain.
@@ -19,6 +19,12 @@ def _find_candidate(self, product_id, product_qty, product_uom, location_id, nam
lines = self.filtered(lambda po_line: po_line.sale_line_id.id == values['sale_line_id']) if values.get('sale_line_id') else self
return super(PurchaseOrderLine, lines)._find_candidate(product_id, product_qty, product_uom, location_id, name, origin, company_id, values)

@api.model
def _prepare_purchase_order_line_from_procurement(self, product_id, product_qty, product_uom, company_id, values, po):
res = super()._prepare_purchase_order_line_from_procurement(product_id, product_qty, product_uom, company_id, values, po)
res['sale_line_id'] = values.get('sale_line_id', False)
return res


class StockRule(models.Model):
_inherit = 'stock.rule'
@@ -33,9 +39,3 @@ def _get_procurements_to_merge_groupby(self, procurement):
@api.model
def _get_procurements_to_merge_sorted(self, procurement):
return procurement.values.get('sale_line_id'), super(StockRule, self)._get_procurements_to_merge_sorted(procurement)

@api.model
def _prepare_purchase_order_line(self, product_id, product_qty, product_uom, company_id, values, po):
res = super(StockRule, self)._prepare_purchase_order_line(product_id, product_qty, product_uom, company_id, values, po)
res['sale_line_id'] = values.get('sale_line_id', False)
return res

0 comments on commit 621a93b

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