[IMP] purchase_requisition{_stock}: add new alternative POs option

Previously the "call to tender" flow involved creating a
`purchase.requisition` record, creating each RFQ via that record, and
then manually going through each RFQ to compare the prices/dates/etc.
By linking the "call to tender" POs within the POs, we remove an
unnecessary `purchase.requestion` record and makes it easier to track
which RFQs are related to each other as an Alternative RFQ.

On top of this, we add some comparision features to make it easier to
determine the best RFQ, specificially the ability to compare PO lines in
the same view with some visual aids (best price/date colored green +
buttons to check these lines to make them easier to view). We also add
an extra feature to set the qty of selected/non-chosen PO lines to 0 to
aid in the RFQ selection process (only applies to non-confirmed/done/
cancelled POs).

Some other features included with this:
- option to cancel alternative POs when confirming one, which purposely
  does not cancel ones that have already been confirmed/completed.
- new wizard for creating alternative POs so user can select whether or
  not they want to copy the products/qtys from the original PO.

Important Notes:
- JS Customizations:
  - custom many2many widget added so user:
    - can click between alternative POs in same window + keep breadcrumb.
      This is because all alt POs are interconnected and long breadcrumb
      chain is possible (+ we want to avoid windows within windows.) It
      is expected that user will be aware that unsaved changes will
      auto-save when alt PO is clicked on.
    - cannot unlink a PO from itself (this is automagically done during
      the write) since this will remove all of its linked POs and might
      confuse users.
  - custom view js for Comparing Order Lines to help highlight best
    options, including ensuring that the best options are still
    highlighted after clicking on Choose/Clear buttons (since the best
    option can change afterwards, we recalc + update via RPC)
- General implementation warnings:
  - Anytime any button/alternative PO is clicked on within a PO, the
    form will auto-save. This is due to how the current action service
    handles changing views.
  - POs created via "Create Alternative" button purposely:
     - require a vendor to ensure correct lead times/prices
     - show all vendor/product warnings in wizard because we cannot
       reproduce the pop-up warning that would occur in the PO when
       they are selected. We also purposely block the PO creation when a
       blocking warning is set since we cannot remove the values
       (especially in the case of a blocking vendor message) from a
       newly created PO.
  - Technical purchase.order.group model created to help with difficult
    management of complicated behaviors:
     - unlink from self if a PO is no longer linked to any other POs
     - linkages must be symmetric (i.e. linkage PO1 => PO2 must
       reflect PO2 => PO1 in their form views)
     - don't lose existing linkages (i.e. PO1 => PO2 and PO2 => PO3
       should auto-link PO1 => PO3)
    These last two behaviors are difficult to do without grouping due to
    possibility of remove and adding linkages at the same time. To avoid
    complex code to ensure these complexities hold when when creating a
    new PO, linkaging to alternatives is not allowed when PO is not yet
    saved as a record.

Task: 2695116
Upgrade PR: odoo/upgrade#3586

closes odoo/odoo#87656

Signed-off-by: Arnold Moyaux (arm) <arm@odoo.com>
This commit is contained in:
Tiffany Chang (tic)
2022-06-20 12:15:59 +02:00
parent 863c5090f0
commit f533e40f0e
21 changed files with 831 additions and 3 deletions
+1
View File
@@ -1,3 +1,4 @@
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from . import models
from . import wizard
@@ -22,6 +22,20 @@ are agreements you have with vendors to benefit from a predetermined pricing.
'views/purchase_requisition_views.xml',
'report/purchase_requisition_report.xml',
'report/report_purchaserequisition.xml',
'wizard/purchase_requisition_alternative_warning.xml',
'wizard/purchase_requisition_create_alternative.xml',
],
'assets': {
'web.assets_backend': [
'purchase_requisition/static/src/js/purchase_order_alternatives_widget.js',
'purchase_requisition/static/src/js/purchase_order_compare_list_renderer.js',
'purchase_requisition/static/src/js/purchase_order_line_compare_list_controller.js',
'purchase_requisition/static/src/js/purchase_order_line_compare_list_renderer.js',
'purchase_requisition/static/src/js/purchase_order_line_compare_list_view.js',
],
'web.assets_qweb': [
'purchase_requisition/static/src/xml/purchase_order_line_compare.xml',
],
},
'license': 'LGPL-3',
}
@@ -55,5 +55,40 @@
<field name="price_unit">60</field>
<field name="product_qty">10</field>
</record>
<!-- Call to Tender, i.e. linked RFQs -->
<!-- Note that forcecreate="0" is added to this demo data due to product type being updated to 'product'
by stock, but upgrade issue will occur since 'product' type won't exist yet in purchase when trying
to add the new PO lines (i.e. stock product type update won't exist yet, but it will be in the db) -->
<record id="rfq3" model="purchase.order" forcecreate="0">
<field name="partner_id" ref="base.res_partner_1"/>
<field name="user_id" ref="base.user_admin"/>
</record>
<record id="rfq3_line" model="purchase.order.line" forcecreate="0">
<field name="order_id" ref="rfq3"/>
<field name="name" model="purchase.order.line" eval="obj().env.ref('product.product_product_13').partner_ref"/>
<field name="date_planned" eval="time.strftime('%Y-%m-10')"/>
<field name="product_id" ref="product.product_product_13"/>
<field name="product_uom" ref="uom.product_uom_unit"/>
<field name="price_unit">100</field>
<field name="product_qty">5</field>
</record>
<record id="rfq4" model="purchase.order" forcecreate="0">
<field name="partner_id" ref="base.res_partner_2"/>
<field name="user_id" ref="base.user_admin"/>
</record>
<record id="rfq4_line" model="purchase.order.line" forcecreate="0">
<field name="order_id" ref="rfq4"/>
<field name="name" model="purchase.order.line" eval="obj().env.ref('product.product_product_13').partner_ref"/>
<field name="date_planned" eval="time.strftime('%Y-%m-15')"/>
<field name="product_id" ref="product.product_product_13"/>
<field name="product_uom" ref="uom.product_uom_unit"/>
<field name="price_unit">120</field>
<field name="product_qty">4</field>
</record>
<record id="rfq_group" model="purchase.order.group" forcecreate="0">
<field name="order_ids" eval="[Command.set([ref('purchase_requisition.rfq3'), ref('purchase_requisition.rfq4')])]"/>
</record>
</data>
</odoo>
+166 -1
View File
@@ -1,16 +1,46 @@
# -*- encoding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import api, fields, models
from collections import defaultdict
from odoo import api, fields, models, _, Command
from odoo.tools import get_lang
class PurchaseOrderGroup(models.Model):
_name = 'purchase.order.group'
_description = "Technical model to group PO for call to tenders"
order_ids = fields.One2many('purchase.order', 'purchase_group_id')
def write(self, vals):
res = super().write(vals)
# when len(POs) == 1, only linking PO to itself at this point => self implode (delete) group
self.filtered(lambda g: len(g.order_ids) <= 1).unlink()
return res
class PurchaseOrder(models.Model):
_inherit = 'purchase.order'
requisition_id = fields.Many2one('purchase.requisition', string='Purchase Agreement', copy=False)
is_quantity_copy = fields.Selection(related='requisition_id.is_quantity_copy', readonly=False)
purchase_group_id = fields.Many2one('purchase.order.group')
alternative_po_ids = fields.One2many(
'purchase.order', related='purchase_group_id.order_ids', readonly=False,
domain="[('id', '!=', id), ('state', 'in', ['draft', 'sent', 'to approve'])]",
string="Alternative POs", check_company=True,
help="Other potential purchase orders for purchasing products")
has_alternatives = fields.Boolean(
"Has Alternatives", compute='_compute_has_alternatives',
help="Whether or not this purchase order is linked to another purchase order as an alternative.")
@api.depends('purchase_group_id')
def _compute_has_alternatives(self):
self.has_alternatives = False
self.filtered(lambda po: po.purchase_group_id).has_alternatives = True
@api.onchange('requisition_id')
def _onchange_requisition_id(self):
if not self.requisition_id:
@@ -78,6 +108,19 @@ class PurchaseOrder(models.Model):
self.order_line = order_lines
def button_confirm(self):
if self.alternative_po_ids and not self.env.context.get('skip_alternative_check', False):
alternative_po_ids = self.alternative_po_ids.filtered(lambda po: po.state in ['draft', 'sent', 'to approve'] and po.id not in self.ids)
if alternative_po_ids:
view = self.env.ref('purchase_requisition.purchase_requisition_alternative_warning_form')
return {
'name': _('Alternative Warning'),
'type': 'ir.actions.act_window',
'view_mode': 'form',
'res_model': 'purchase.requisition.alternative.warning',
'views': [(view.id, 'form')],
'target': 'new',
'context': dict(self.env.context, default_alternative_po_ids=alternative_po_ids.ids, default_po_ids=self.ids),
}
res = super(PurchaseOrder, self).button_confirm()
for po in self:
if not po.requisition_id:
@@ -92,6 +135,13 @@ class PurchaseOrder(models.Model):
@api.model_create_multi
def create(self, vals_list):
orders = super().create(vals_list)
if self.env.context.get('origin_po_id'):
# po created as an alt to another PO:
origin_po_id = self.env['purchase.order'].browse(self.env.context.get('origin_po_id'))
if origin_po_id.purchase_group_id:
origin_po_id.purchase_group_id.order_ids |= orders
else:
self.env['purchase.order.group'].create({'order_ids': [Command.set(origin_po_id.ids + orders.ids)]})
mt_note = self.env['ir.model.data']._xmlid_to_res_id('mail.mt_note')
for order in orders:
if order.requisition_id:
@@ -102,13 +152,98 @@ class PurchaseOrder(models.Model):
return orders
def write(self, vals):
if vals.get('purchase_group_id', False):
# store in case linking to a PO with existing linkages
orig_purchase_group = self.purchase_group_id
result = super(PurchaseOrder, self).write(vals)
if vals.get('requisition_id'):
self.message_post_with_view('mail.message_origin_link',
values={'self': self, 'origin': self.requisition_id, 'edit': True},
subtype_id=self.env['ir.model.data']._xmlid_to_res_id('mail.mt_note'))
if vals.get('alternative_po_ids', False):
if not self.purchase_group_id and len(self.alternative_po_ids + self) > len(self):
# this can create a new group + delete an existing one (or more) when linking to already linked PO(s), but this is
# simplier than additional logic checking if exactly 1 exists or merging multiple groups if > 1
self.env['purchase.order.group'].create({'order_ids': [Command.set(self.ids + self.alternative_po_ids.ids)]})
elif self.purchase_group_id and len(self.alternative_po_ids + self) <= 1:
# write in purchase group isn't called so we have to manually unlink obsolete groups here
self.purchase_group_id.unlink()
if vals.get('purchase_group_id', False):
# the write is for multiple POs => don't double count the POs of the final group
additional_groups = orig_purchase_group - self.purchase_group_id
if additional_groups:
additional_pos = (additional_groups.order_ids - self.purchase_group_id.order_ids)
additional_groups.unlink()
if additional_pos:
self.purchase_group_id.order_ids |= additional_pos
return result
def action_create_alternative(self):
ctx = dict(**self.env.context, default_origin_po_id=self.id)
return {
'name': _('Create alternative'),
'type': 'ir.actions.act_window',
'view_mode': 'form',
'res_model': 'purchase.requisition.create.alternative',
'view_id': self.env.ref('purchase_requisition.purchase_requisition_create_alternative_form').id,
'target': 'new',
'context': ctx,
}
def action_compare_alternative_lines(self):
best_price_ids, best_date_ids, best_price_unit_ids = self.get_tender_best_lines()
ctx = dict(
self.env.context,
search_default_groupby_product=True,
params={
'best_price_ids': best_price_ids,
'best_date_ids': best_date_ids,
'best_price_unit_ids': best_price_unit_ids,
},
)
view_id = self.env.ref('purchase_requisition.purchase_order_line_compare_tree').id
return {
'name': _('Compare Order Lines'),
'type': 'ir.actions.act_window',
'view_mode': 'list',
'res_model': 'purchase.order.line',
'views': [(view_id, "list")],
'domain': [('order_id', 'in', (self | self.alternative_po_ids).ids)],
'context': ctx,
}
def get_tender_best_lines(self):
product_to_best_price_line = defaultdict(lambda: self.env['purchase.order.line'])
product_to_best_date_line = defaultdict(lambda: self.env['purchase.order.line'])
product_to_best_price_unit = defaultdict(lambda: self.env['purchase.order.line'])
order_lines = self.order_line | self.alternative_po_ids.order_line
for line in order_lines:
if not line.product_qty or not line.price_subtotal or line.state in ['cancel', 'purchase', 'done']:
continue
if not product_to_best_price_line[line.product_id] or product_to_best_price_line[line.product_id][0].price_subtotal > line.price_subtotal:
product_to_best_price_line[line.product_id] = line
elif product_to_best_price_line[line.product_id][0].price_subtotal == line.price_subtotal:
product_to_best_price_line[line.product_id] |= line
if not product_to_best_date_line[line.product_id] or product_to_best_date_line[line.product_id][0].date_planned > line.date_planned:
product_to_best_date_line[line.product_id] = line
elif product_to_best_date_line[line.product_id][0].date_planned == line.date_planned:
product_to_best_date_line[line.product_id] |= line
if not product_to_best_price_unit[line.product_id] or product_to_best_price_unit[line.product_id][0].price_unit > line.price_unit:
product_to_best_price_unit[line.product_id] = line
elif product_to_best_price_unit[line.product_id][0].price_unit == line.price_unit:
product_to_best_price_unit[line.product_id] |= line
best_price_ids = set()
best_date_ids = set()
best_price_unit_ids = set()
for lines in product_to_best_price_line.values():
best_price_ids.update(lines.ids)
for lines in product_to_best_date_line.values():
best_date_ids.update(lines.ids)
for lines in product_to_best_price_unit.values():
best_price_unit_ids.update(lines.ids)
return list(best_price_ids), list(best_date_ids), list(best_price_unit_ids)
class PurchaseOrderLine(models.Model):
_inherit = 'purchase.order.line'
@@ -130,3 +265,33 @@ class PurchaseOrderLine(models.Model):
pol.name = name
break
super(PurchaseOrderLine, po_lines_without_requisition)._compute_price_unit_and_date_planned_and_name()
def action_clear_quantities(self):
zeroed_lines = self.filtered(lambda l: l.state not in ['cancel', 'purchase', 'done'])
zeroed_lines.write({'product_qty': 0})
if len(self) > len(zeroed_lines):
return {
'type': 'ir.actions.client',
'tag': 'display_notification',
'params': {
'title': _("Some not cleared"),
'message': _("Some quantities were not cleared because their status is not a RFQ status."),
'sticky': False,
}
}
return False
def action_choose(self):
order_lines = (self.order_id | self.order_id.alternative_po_ids).mapped('order_line')
order_lines = order_lines.filtered(lambda l: l.product_qty and l.product_id.id in self.product_id.ids and l.id not in self.ids)
if order_lines:
return order_lines.action_clear_quantities()
return {
'type': 'ir.actions.client',
'tag': 'display_notification',
'params': {
'title': _("Nothing to clear"),
'message': _("There are no quantites to clear."),
'sticky': False,
}
}
@@ -5,3 +5,6 @@ access_purchase_requisition,purchase.requisition,model_purchase_requisition,purc
access_purchase_requisition_line_purchase_user,purchase.requisition.line,model_purchase_requisition_line,purchase.group_purchase_user,1,1,1,1
access_purchase_requisition_manager,purchase.requisition manager,model_purchase_requisition,purchase.group_purchase_manager,1,0,0,0
access_purchase_requisition_line_manager,purchase.requisition.line manager,model_purchase_requisition_line,purchase.group_purchase_manager,1,0,0,0
access_purchase_requisition_alternative_warning, purchase.requisition.alternative.warning,model_purchase_requisition_alternative_warning,purchase.group_purchase_user,1,1,1,1
access_purchase_requisition_create_alternative, purchase.requisition.create.alternative,model_purchase_requisition_create_alternative,purchase.group_purchase_user,1,1,1,1
access_purchase_requisition_purchase_order_group, purchase.order.group,model_purchase_order_group,purchase.group_purchase_user,1,1,1,1
1 id name model_id:id group_id:id perm_read perm_write perm_create perm_unlink
5 access_purchase_requisition_line_purchase_user purchase.requisition.line model_purchase_requisition_line purchase.group_purchase_user 1 1 1 1
6 access_purchase_requisition_manager purchase.requisition manager model_purchase_requisition purchase.group_purchase_manager 1 0 0 0
7 access_purchase_requisition_line_manager purchase.requisition.line manager model_purchase_requisition_line purchase.group_purchase_manager 1 0 0 0
8 access_purchase_requisition_alternative_warning purchase.requisition.alternative.warning model_purchase_requisition_alternative_warning purchase.group_purchase_user 1 1 1 1
9 access_purchase_requisition_create_alternative purchase.requisition.create.alternative model_purchase_requisition_create_alternative purchase.group_purchase_user 1 1 1 1
10 access_purchase_requisition_purchase_order_group purchase.order.group model_purchase_order_group purchase.group_purchase_user 1 1 1 1
@@ -0,0 +1,52 @@
/** @odoo-module **/
import field_registry from 'web.field_registry';
import { FieldMany2Many } from 'web.relational_fields';
import PurchaseOrderCompareListRenderer from '@purchase_requisition/js/purchase_order_compare_list_renderer';
const FieldMany2ManySameWindow = FieldMany2Many.extend({
/**
* @override
*/
init: function (parent, name, record, options) {
this._super(...arguments);
this.isMany2Many = true;
},
/**
* Override to ensure we can't unlink a PO from itself (i.e. confusing behavior)
* @override
*/
_getRenderer: function () {
if (this.view.arch.tag === 'tree') {
return PurchaseOrderCompareListRenderer;
}
return this._super(...arguments);
},
/**
* Override to open record in same window w/breadcrumb extended
* @override
*/
_onOpenRecord: function (ev) {
ev.stopPropagation();
const targetId = ev.target.state.data.find(obj => {
return obj.id === ev.data.id;
}).res_id;
// don't re-open current record
if (targetId !== this.record.res_id) {
this._rpc({
model: this.model,
method: 'get_formview_action',
args: [[targetId]],
context: this.record.getContext(this.recordParams),
domain: this.record.getDomain(this.recordParams),
}).then(action => {
this.trigger_up('do_action', {action: action});
});
}
}
});
field_registry.add('many2many_same_window', FieldMany2ManySameWindow);
@@ -0,0 +1,26 @@
/** @odoo-module **/
import ListRenderer from 'web.ListRenderer';
const PurchaseOrderCompareListRenderer = ListRenderer.extend({
init: function (parent, state, params) {
this._super(...arguments);
this.active_id = parent.res_id;
},
/**
* @override
* @private
* @returns {Promise}
*/
_renderRow: function (record) {
const $tr = this._super(...arguments);
if (record.res_id === this.active_id && $tr.find('.o_list_record_remove').length) {
$tr.find(".o_list_record_remove").remove();
}
return $tr;
}
});
export default PurchaseOrderCompareListRenderer;
@@ -0,0 +1,66 @@
/** @odoo-module **/
import ListController from 'web.ListController';
const PurchaseOrderLineCompareListController = ListController.extend({
// -------------------------------------------------------------------------
// Public
// -------------------------------------------------------------------------
init: function (parent, model, renderer, params) {
this.context = renderer.state.getContext();
this.best_date_ids = this.context.params.best_date_ids;
this.best_price_ids = this.context.params.best_price_ids;
this.best_price_unit_ids = this.context.params.best_price_unit_ids;
return this._super(...arguments);
},
// -------------------------------------------------------------------------
// Handlers
// -------------------------------------------------------------------------
/**
* @override
*/
_onButtonClicked: function (ev) {
if (ev.data.attrs.class && ev.data.attrs.class.split(' ').includes('o_clear_qty_buttons')) {
ev.stopPropagation();
var self = this;
return this._callButtonAction(ev.data.attrs, ev.data.record).then(() => {
const context = this.model.localData[0] && this.model.localData[0].getContext() || {};
return self._rpc({
model: "purchase.order",
method: 'get_tender_best_lines',
args: [self.context.active_id],
context: context,
}).then((best_lines) => {
self.renderer.best_date_ids = best_lines[0] || [];
self.renderer.best_price_ids = best_lines[1] || [];
self.renderer.best_price_unit_ids = best_lines[2] || [];
self.reload();
});
});
} else {
this._super.apply(this, arguments);
}
},
_onHeaderButtonClicked: async function (node) {
await this._super(...arguments);
if (node.attrs.name && node.attrs.name === 'action_clear_quantities') {
const context = this.model.localData[0] && this.model.localData[0].getContext() || {};
const best_lines = await this._rpc({
model: 'purchase.order',
method: 'get_tender_best_lines',
args: this.context.active_ids,
context,
});
this.renderer.best_date_ids = best_lines[0];
this.renderer.best_price_ids = best_lines[1];
this.renderer.best_price_unit_ids = best_lines[2];
this.reload();
}
},
});
export default PurchaseOrderLineCompareListController;
@@ -0,0 +1,35 @@
/** @odoo-module **/
import ListRenderer from 'web.ListRenderer';
const PurchaseOrderLineCompareListRenderer = ListRenderer.extend({
init: function (parent, state, params) {
this._super(...arguments);
this.best_date_ids = this.state.context.params.best_date_ids;
this.best_price_ids = this.state.context.params.best_price_ids;
this.best_price_unit_ids = this.state.context.params.best_price_unit_ids;
},
/**
* @override
* @private
* @returns {Promise}
*/
_renderRow: function (record) {
const $tr = this._super(...arguments);
if (this.best_date_ids.includes(record.res_id)) {
$tr.find('[name="date_planned"]').toggleClass('text-success');
}
if (this.best_price_ids.includes(record.res_id)) {
$tr.find('[name="price_subtotal"]').toggleClass('text-success');
}
if (this.best_price_unit_ids.includes(record.res_id)) {
$tr.find('[name="price_unit"]').toggleClass('text-success');
}
return $tr;
}
});
export default PurchaseOrderLineCompareListRenderer;
@@ -0,0 +1,17 @@
/** @odoo-module **/
import ListView from 'web.ListView';
import PurchaseOrderLineCompareListController from '@purchase_requisition/js/purchase_order_line_compare_list_controller';
import PurchaseOrderLineCompareListRenderer from '@purchase_requisition/js/purchase_order_line_compare_list_renderer';
import viewRegistry from 'web.view_registry';
const PurchaseOrderLineCompareListView = ListView.extend({
config: Object.assign({}, ListView.prototype.config, {
Controller: PurchaseOrderLineCompareListController,
Renderer: PurchaseOrderLineCompareListRenderer,
}),
});
viewRegistry.add('purchase_order_line_compare', PurchaseOrderLineCompareListView);
export default PurchaseOrderLineCompareListView;
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<templates id="template" xml:space="preserve">
<t t-extend="ListView.buttons" t-name="PurchaseOrderLineCompare.Buttons">
<t t-jquery="button.o_list_button_discard" t-operation="after">
<button class="btn btn-secondary o_clear_qty_buttons mr-2" type="button">
Clear Selected
</button>
</t>
</t>
</templates>
@@ -2,6 +2,10 @@
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo.addons.purchase_requisition.tests.common import TestPurchaseRequisitionCommon
from odoo import Command
from odoo.tests import Form
from datetime import timedelta
class TestPurchaseRequisition(TestPurchaseRequisitionCommon):
@@ -112,3 +116,102 @@ class TestPurchaseRequisition(TestPurchaseRequisitionCommon):
('partner_id', '=', vendor.id)
]) - supplier_info
self.assertEqual(new_si.purchase_requisition_id, requisition_blanket, 'the blanket order is not linked to the supplier info')
def test_07_alternative_purchases_wizards(self):
"""Directly link POs to each other as 'Alternatives': check that wizards and
their flows correctly work."""
orig_po = self.env['purchase.order'].create({
'partner_id': self.res_partner_1.id,
})
unit_price = 50
po_form = Form(orig_po)
with po_form.order_line.new() as line:
line.product_id = self.product_09
line.product_qty = 5.0
line.price_unit = unit_price
po_form.save()
# first flow: check that creating an alt PO correctly auto-links both POs to each other
action = orig_po.action_create_alternative()
alt_po_wiz = Form(self.env['purchase.requisition.create.alternative'].with_context(**action['context']))
alt_po_wiz.partner_id = self.res_partner_1
alt_po_wiz.copy_products = True
alt_po_wiz = alt_po_wiz.save()
alt_po_wiz.action_create_alternative()
self.assertEqual(len(orig_po.alternative_po_ids), 2, "Original PO should be auto-linked to itself and newly created PO")
# check alt po was created with correct values
alt_po_1 = orig_po.alternative_po_ids.filtered(lambda po: po.id != orig_po.id)
self.assertEqual(orig_po.order_line.product_id, alt_po_1.order_line.product_id, "Alternative PO should have copied the product to purchase from original PO")
self.assertEqual(orig_po.order_line.product_qty, alt_po_1.order_line.product_qty, "Alternative PO should have copied the qty to purchase from original PO")
self.assertEqual(len(alt_po_1.alternative_po_ids), 2, "Newly created PO should be auto-linked to itself and original PO")
# check compare POLs correctly calcs best date/price PO lines: orig_po.date_planned = best & alt_po.price = best
alt_po_1.order_line.date_planned += timedelta(days=1)
alt_po_1.order_line.price_unit = unit_price - 10
action = orig_po.action_compare_alternative_lines()
params = action['context']['params']
best_price_pol = self.env['purchase.order.line'].browse(params['best_price_ids'])
best_date_pol = self.env['purchase.order.line'].browse(params['best_date_ids'])
self.assertEqual(best_price_pol.order_id.id, alt_po_1.id, "Best price PO line was not correctly calculated")
self.assertEqual(best_date_pol.order_id.id, orig_po.id, "Best date PO line was not correctly calculated")
# second flow: create extra alt PO, check that all 3 POs are correctly auto-linked
action = orig_po.action_create_alternative()
alt_po_wiz = Form(self.env['purchase.requisition.create.alternative'].with_context(**action['context']))
alt_po_wiz.partner_id = self.res_partner_1
alt_po_wiz.copy_products = True
alt_po_wiz = alt_po_wiz.save()
alt_po_wiz.action_create_alternative()
self.assertEqual(len(orig_po.alternative_po_ids), 3, "Original PO should be auto-linked to newly created alternative PO")
self.assertEqual(len(alt_po_1.alternative_po_ids), 3, "Alternative PO should be auto-linked to newly created alternative PO")
alt_po_2 = orig_po.alternative_po_ids.filtered(lambda po: po.id not in [alt_po_1.id, orig_po.id])
self.assertEqual(len(alt_po_2.alternative_po_ids), 3, "All alternative POs should be auto-linked to each other")
# third flow: confirm one of the POs when alt POs are a mix of confirmed + RFQs
alt_po_2.write({'state': 'purchase'})
action = orig_po.button_confirm()
warning_wiz = Form(self.env['purchase.requisition.alternative.warning'].with_context(**action['context']))
warning_wiz = warning_wiz.save()
self.assertEqual(warning_wiz.alternative_po_count, 1, "POs not in a RFQ status should not be listed as possible to cancel")
warning_wiz.action_cancel_alternatives()
self.assertEqual(alt_po_1.state, 'cancel', "Alternative PO should have been cancelled")
self.assertEqual(orig_po.state, 'purchase', "Original PO should have been confirmed")
def test_08_purchases_multi_linkages(self):
"""Directly link POs to each other as 'Alternatives': check linking/unlinking
POs that are already linked correctly work."""
pos = []
for _ in range(5):
pos += self.env['purchase.order'].create({
'partner_id': self.res_partner_1.id,
}).ids
pos = self.env['purchase.order'].browse(pos)
po_1, po_2, po_3, po_4, po_5 = pos
po_1.alternative_po_ids |= po_2
po_3.alternative_po_ids |= po_4
groups = self.env['purchase.order.group'].search([('order_ids', 'in', pos.ids)])
self.assertEqual(len(po_1.alternative_po_ids), 2, "PO1 and PO2 should only be linked to each other")
self.assertEqual(len(po_3.alternative_po_ids), 2, "PO3 and PO4 should only be linked to each other")
self.assertEqual(len(groups), 2, "There should only be 2 groups: (PO1,PO2) and (PO3,PO4)")
# link non-linked PO to already linked PO
po_5.alternative_po_ids |= po_4
groups = self.env['purchase.order.group'].search([('order_ids', 'in', pos.ids)])
self.assertEqual(len(po_3.alternative_po_ids), 3, "PO3 should now be linked to PO4 and PO5")
self.assertEqual(len(po_4.alternative_po_ids), 3, "PO4 should now be linked to PO3 and PO5")
self.assertEqual(len(po_5.alternative_po_ids), 3, "PO5 should now be linked to PO3 and PO4")
self.assertEqual(len(groups), 2, "There should only be 2 groups: (PO1,PO2) and (PO3,PO4,PO5)")
# link already linked PO to already linked PO
po_5.alternative_po_ids |= po_1
groups = self.env['purchase.order.group'].search([('order_ids', 'in', pos.ids)])
self.assertEqual(len(po_1.alternative_po_ids), 5, "All 5 POs should be linked to each other now")
self.assertEqual(len(groups), 1, "There should only be 1 group containing all 5 POs (other group should have auto-deleted")
# remove all links, make sure group auto-deletes
(pos - po_5).alternative_po_ids = [Command.clear()]
groups = self.env['purchase.order.group'].search([('order_ids', 'in', pos.ids)])
self.assertEqual(len(po_5.alternative_po_ids), 0, "Last PO should auto unlink from itself since group should have auto-deleted")
self.assertEqual(len(groups), 0, "The group should have auto-deleted")
@@ -9,10 +9,39 @@
<field name="partner_id" position="replace">
<field name="is_quantity_copy" invisible="1"/>
<field name="partner_id" widget="res_partner_many2one" context="{'res_partner_search_mode': 'supplier', 'show_vat': True}" attrs="{'readonly': ['|', ('is_quantity_copy', '=', 'none'), ('state', 'in', ['purchase', 'done', 'cancel'])]}" placeholder="Name, TIN, Email, or Reference" force_save="1"/>
</field>
<field name="partner_ref" position="after">
</field>
<field name="partner_ref" position="after">
<field name="requisition_id" domain="[('state', 'in', ('in_progress', 'open', 'ongoing')), ('vendor_id', 'in', (partner_id, False)), ('company_id', '=', company_id)]"/>
</field>
<xpath expr="//page[@name='purchase_delivery_invoice']" position="after">
<page string="Alternatives" name="alternative_pos">
<group>
<group>
<p>Create a call for tender by adding alternative request for quotations to different vendors.
Make your choice by selecting the best combination of lead time, OTD and/or total amount.
By comparing product lines you can also decide to order some products from one vendor and others from another vendor.</p>
</group>
<group>
<p>
<button name="action_create_alternative" type="object" class="btn-link d-block" string="Create Alternative" icon="fa-copy" attrs="{'invisible': [('id', '=', False)]}" />
<button name="action_compare_alternative_lines" type="object" class="btn-link d-block" string="Compare Product Lines" icon="fa-bar-chart" attrs="{'invisible': [('alternative_po_ids', '=', [])]}"/>
</p>
</group>
</group>
<field name="alternative_po_ids" attrs="{'readonly': [('id', '=', False)]}" widget="many2many_same_window" context="{'quotation_only': True}">
<tree string="Alternative Purchase Order" create="0" decoration-muted="state in ['cancel', 'purchase', 'done']" decoration-bf="id == parent.id">
<control>
<create string="Link to Existing RfQ"/>
</control>
<field name="partner_id"/>
<field name="name" string="Reference"/>
<field name="date_planned"/>
<field name="amount_total"/>
<field name="state"/>
</tree>
</field>
</page>
</xpath>
</field>
</record>
@@ -27,4 +56,65 @@
</field>
</record>
<record id="purchase_order_kpis_tree_inherit_purchase_requisition" model="ir.ui.view">
<field name="name">purchase.order.kpis.tree.inherit.purchase.requisition</field>
<field name="model">purchase.order</field>
<field name="inherit_id" ref="purchase.purchase_order_kpis_tree"/>
<field name="arch" type="xml">
<field name="name" position="before">
<field name="has_alternatives" invisible="1"/>
<button class="fa fa-copy"
title="Has Alternatives"
disabled="1"
attrs="{'invisible': [('has_alternatives', '=', False)]}"/>
</field>
</field>
</record>
<record id="purchase_order_tree_inherit_purchase_requisition" model="ir.ui.view">
<field name="name">purchase.order.tree.inherit.purchase.requisition</field>
<field name="model">purchase.order</field>
<field name="inherit_id" ref="purchase.purchase_order_tree"/>
<field name="arch" type="xml">
<field name="name" position="before">
<field name="has_alternatives" invisible="1"/>
<button class="fa fa-copy"
title="Has Alternatives"
disabled="1"
attrs="{'invisible': [('has_alternatives', '=', False)]}"/>
</field>
</field>
</record>
<record id="purchase_order_line_compare_tree" model="ir.ui.view">
<field name="name">purchase.order.line.compare.tree</field>
<field name="model">purchase.order.line</field>
<field name="priority">1000</field>
<field name="arch" type="xml">
<tree string="Purchase Order Lines"
decoration-muted="state in ['cancel', 'purchase', 'done']"
create="0" delete="0" edit="0" expand="1"
js_class="purchase_order_line_compare">
<header>
<button name="action_clear_quantities" string="Clear Selected" type="object" class="o_clear_qty_buttons"/>
</header>
<field name="product_id" readonly="1"/>
<field name="partner_id" string="Vendor"/>
<field name="order_id" string="Reference" readonly="1"/>
<field name="state"/>
<field name="name" readonly="1"/>
<field name="date_planned" readonly="1"/>
<field name="product_qty"/>
<field name="product_uom" groups="uom.group_uom"/>
<field name="price_unit"/>
<field name="price_subtotal" string="Total"/>
<field name="currency_id"/>
<button name="action_choose" string="Choose" type="object" class="o_clear_qty_buttons" icon="fa-bullseye"
attrs="{'invisible': [('product_qty', '&lt;=', 0.0)]}"/>
<button name="action_clear_quantities" string="Clear" type="object" class="o_clear_qty_buttons" icon="fa-times"
attrs="{'invisible': ['|', ('product_qty', '&lt;=', 0.0), ('state', 'in', ['cancel', 'purchase', 'done'])]}"/>
</tree>
</field>
</record>
</odoo>
@@ -0,0 +1,2 @@
from . import purchase_requisition_alternative_warning
from . import purchase_requisition_create_alternative
@@ -0,0 +1,29 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import api, fields, models
class PurchaseRequisitionAlternativeWarning(models.TransientModel):
_name = 'purchase.requisition.alternative.warning'
_description = 'Wizard in case PO still has open alternative requests for quotation'
po_ids = fields.Many2many('purchase.order', 'warning_purchase_order_rel', string="POs to Confirm")
alternative_po_ids = fields.Many2many('purchase.order', 'warning_purchase_order_alternative_rel', string="Alternative POs")
alternative_po_count = fields.Integer(compute="_compute_alternative_po_count")
@api.depends("alternative_po_ids")
def _compute_alternative_po_count(self):
for wizard in self:
wizard.alternative_po_count = len(wizard.alternative_po_ids)
def action_keep_alternatives(self):
return self._action_done()
def action_cancel_alternatives(self):
# in theory alternative_po_ids shouldn't have any po_ids in it, but it's possible by accident/forcing it, so avoid cancelling them to be safe
self.alternative_po_ids.filtered(lambda po: po.state in ['draft', 'sent', 'to approve'] and po.id not in self.po_ids.ids).button_cancel()
return self._action_done()
def _action_done(self):
return self.po_ids.with_context({'skip_alternative_check': True}).button_confirm()
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<data>
<record id="purchase_requisition_alternative_warning_form" model="ir.ui.view">
<field name="name">Alternative Warning</field>
<field name="model">purchase.requisition.alternative.warning</field>
<field name="arch" type="xml">
<form string="Alternative Warning">
<div>
<field name="alternative_po_count"/>
alternative request for quotations are still open, do you want to cancel them?
</div>
<field name="alternative_po_ids" nolabel="1" readonly="1">
<tree create="0" delete="0" edit="0">
<field name="partner_id"/>
<field name="name" string="Reference"/>
<field name="date_planned"/>
<field name="amount_total"/>
<field name="state"/>
</tree>
</field>
<footer>
<button name="action_cancel_alternatives" string="Cancel Alternatives" data-hotkey="q" type="object" colspan="1" class="btn-primary"/>
<button name="action_keep_alternatives" string="Keep Alternatives" data-hotkey="w" type="object" colspan="1" class="btn-primary"/>
<button string="Cancel" data-hotkey="x" special="cancel" colspan="1" class="btn-secondary"/>
</footer>
</form>
</field>
</record>
</data>
</odoo>
@@ -0,0 +1,77 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import _, api, fields, models, Command
from odoo.exceptions import UserError
class PurchaseRequisitionCreateAlternative(models.TransientModel):
_name = 'purchase.requisition.create.alternative'
_description = 'Wizard to preset values for alternative PO'
origin_po_id = fields.Many2one(
'purchase.order', help="The original PO that this alternative PO is being created for."
)
partner_id = fields.Many2one(
'res.partner', string='Vendor', required=True,
help="Choose a vendor for alternative PO")
creation_blocked = fields.Boolean(
help="If the chosen vendor or if any of the products in the original PO have a blocking warning then we prevent creation of alternative PO. "
"This is because normally these fields are cleared w/warning message within form view, but we cannot recreate that in this case.",
compute="_compute_purchase_warn",
groups="purchase.group_warning_purchase")
purchase_warn_msg = fields.Text(
'Warning Messages',
compute="_compute_purchase_warn",
groups="purchase.group_warning_purchase")
copy_products = fields.Boolean(
"Copy Products", default=True,
help="If this is checked, the product quantites of the original PO will be copied")
@api.depends('partner_id', 'copy_products')
def _compute_purchase_warn(self):
self.creation_blocked = False
self.purchase_warn_msg = ''
# follows partner warning logic from PurchaseOrder
if not self.env.user.has_group('purchase.group_warning_purchase'):
return
partner = self.partner_id
# If partner has no warning, check its company
if partner and partner.purchase_warn == 'no-message':
partner = partner.parent_id
if partner and partner.purchase_warn != 'no-message':
self.purchase_warn_msg = _("Warning for %(partner)s:\n%(warning_message)s\n", partner=partner.name, warning_message=partner.purchase_warn_msg)
if partner.purchase_warn == 'block':
self.creation_blocked = True
self.purchase_warn_msg += _("This is a blocking warning!\n")
if self.copy_products and self.origin_po_id.order_line:
for line in self.origin_po_id.order_line:
if line.product_id.purchase_line_warn != 'no-message':
self.purchase_warn_msg += _("Warning for %(product)s:\n%(warning_message)s\n", product=line.product_id.name, warning_message=line.product_id.purchase_line_warn_msg)
if line.product_id.purchase_line_warn == 'block':
self.creation_blocked = True
self.purchase_warn_msg += _("This is a blocking warning!\n")
def action_create_alternative(self):
if self.env.user.has_group('purchase.group_warning_purchase') and self.creation_blocked:
raise UserError(
_('The vendor you have selected or at least one of the products you are copying from the original '
'order has a blocking warning on it and cannot be selected to create an alternative.')
)
vals = {
'date_order': self.origin_po_id.date_order,
'partner_id': self.partner_id.id,
'user_id': self.origin_po_id.user_id.id,
}
if self.copy_products and self.origin_po_id:
vals['order_line'] = [Command.create({'product_id': line.product_id.id, 'product_qty': line.product_qty}) for line in self.origin_po_id.order_line]
alt_po = self.env['purchase.order'].with_context(origin_po_id=self.origin_po_id.id).create(vals)
return {
'type': 'ir.actions.act_window',
'view_mode': 'form',
'res_model': 'purchase.order',
'res_id': alt_po.id,
'context': {
'active_id': alt_po.id,
},
}
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<data>
<record id="purchase_requisition_create_alternative_form" model="ir.ui.view">
<field name="name">Create Alternative</field>
<field name="model">purchase.requisition.create.alternative</field>
<field name="arch" type="xml">
<form string="Create Alternative">
<group>
<field name="origin_po_id" invisible="1"/>
<group>
<field name="partner_id"/>
</group>
<group>
<field name="copy_products"/>
</group>
<group groups="purchase.group_warning_purchase" attrs="{'invisible': [('purchase_warn_msg', '=', False)]}">
<field name="purchase_warn_msg"/>
</group>
</group>
<footer>
<button name="action_create_alternative" string="Create Alternative" data-hotkey="q" type="object" colspan="1" class="btn-primary"/>
<button string="Cancel" data-hotkey="x" special="cancel" colspan="1" class="btn-secondary"/>
</footer>
</form>
</field>
</record>
</data>
</odoo>
@@ -13,6 +13,7 @@
'data': [
'security/ir.model.access.csv',
'data/purchase_requisition_stock_data.xml',
'views/purchase_views.xml',
'views/purchase_requisition_views.xml',
],
'installable': True,
@@ -7,8 +7,24 @@ from odoo import api, fields, models
class PurchaseOrder(models.Model):
_inherit = 'purchase.order'
on_time_rate_perc = fields.Float(string="OTD", compute="_compute_on_time_rate_perc")
@api.depends('on_time_rate')
def _compute_on_time_rate_perc(self):
for po in self:
if po.on_time_rate > 0:
po.on_time_rate_perc = po.on_time_rate / 100
else:
po.on_time_rate_perc = -1
@api.onchange('requisition_id')
def _onchange_requisition_id(self):
super(PurchaseOrder, self)._onchange_requisition_id()
if self.requisition_id:
self.picking_type_id = self.requisition_id.picking_type_id.id
class PurchaseOrderLine(models.Model):
_inherit = 'purchase.order.line'
on_time_rate_perc = fields.Float(string="OTD", related="order_id.on_time_rate_perc")
@@ -0,0 +1,24 @@
<?xml version="1.0"?>
<odoo>
<record id="purchase_order_form_inherit_purchase_requisition_stock" model="ir.ui.view">
<field name="name">purchase.order.form.inherit.purchase.requisition.stock</field>
<field name="model">purchase.order</field>
<field name="inherit_id" ref="purchase_requisition.purchase_order_form_inherit"/>
<field name="arch" type="xml">
<xpath expr="//page[@name='alternative_pos']//field[@name='alternative_po_ids']//field[@name='partner_id']" position="after">
<field name='on_time_rate_perc' widget="percentage" attrs="{'invisible': [('on_time_rate_perc', '&lt;', 0)]}"/>
</xpath>
</field>
</record>
<record id="purchase_order_line_compare_tree_inherit_purchase_requisition_stock" model="ir.ui.view">
<field name="name">purchase.order.line.compare.tree.purchase.requisition.stock</field>
<field name="model">purchase.order.line</field>
<field name="inherit_id" ref="purchase_requisition.purchase_order_line_compare_tree"/>
<field name="arch" type="xml">
<field name="partner_id" position="after">
<field name='on_time_rate_perc' widget="percentage" attrs="{'invisible': [('on_time_rate_perc', '&lt;', 0)]}"/>
</field>
</field>
</record>
</odoo>