Skip to content
This repository was archived by the owner on Apr 28, 2022. It is now read-only.

Commit 474b0ac

Browse files
author
Richard deMeester
committed
[ENH] Store source order_id to avoid active_id
1 parent 89750da commit 474b0ac

File tree

5 files changed

+67
-15
lines changed

5 files changed

+67
-15
lines changed

product_configurator_wizard/models/sale.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,17 @@ def reconfigure_product(self):
3939
'target': 'new',
4040
'res_id': wizard.id,
4141
}
42+
43+
44+
class SaleOrder(models.Model):
45+
_inherit = 'sale.order'
46+
47+
@api.multi
48+
def configure_product(self):
49+
res = self.env['ir.actions.act_window'].for_xml_id(
50+
'product_configurator_wizard',
51+
'action_wizard_product_configurator'
52+
)
53+
res['context'] = {'default_order_id': self.id
54+
}
55+
return res

product_configurator_wizard/tests/test_wizard.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,12 @@ def wizard_write_proceed(self, wizard, attr_vals, value_ids=None):
5454
def test_wizard_configuration(self):
5555
"""Test product configurator wizard"""
5656

57+
existing_lines = self.so.order_line
58+
5759
# Start a new configuration wizard
5860
wizard_obj = self.env['product.configurator'].with_context({
5961
'active_model': 'sale.order',
60-
'active_id': self.so.id
62+
'default_order_id': self.so.id
6163
})
6264

6365
wizard = wizard_obj.create({'product_tmpl_id': self.cfg_tmpl.id})
@@ -94,10 +96,16 @@ def test_wizard_configuration(self):
9496
self.assertTrue(len(config_variants) == 1,
9597
"Wizard did not create a configurable variant")
9698

99+
created_line = self.so.order_line - existing_lines
100+
self.assertTrue(len(created_line) == 1,
101+
"Wizard did not create an order line")
102+
97103
def test_reconfiguration(self):
98104
"""Test reconfiguration functionality of the wizard"""
99105
self.test_wizard_configuration()
100106

107+
existing_lines = self.so.order_line
108+
101109
order_line = self.so.order_line.filtered(
102110
lambda l: l.product_id.config_ok
103111
)
@@ -121,3 +129,24 @@ def test_reconfiguration(self):
121129

122130
self.assertTrue(len(config_variants) == 2,
123131
"Wizard reconfiguration did not create a new variant")
132+
133+
created_line = self.so.order_line - existing_lines
134+
self.assertTrue(len(created_line) == 0,
135+
"Wizard created an order line on reconfiguration")
136+
137+
# test that running through again with the same values does not
138+
# create another variant
139+
attr_vals = self.get_attr_values(['diesel', '220d'])
140+
self.wizard_write_proceed(wizard, attr_vals)
141+
142+
# Cycle through steps until wizard ends
143+
while wizard.action_next_step():
144+
pass
145+
146+
config_variants = self.env['product.product'].search([
147+
('config_ok', '=', True)
148+
])
149+
150+
self.assertTrue(len(config_variants) == 2,
151+
"Wizard reconfiguration created a redundant variant")
152+

product_configurator_wizard/views/sale_view.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717
<field name="inherit_id" ref="sale.view_order_form"/>
1818
<field name="arch" type="xml">
1919
<xpath expr="//field[@name='order_line']" position="before">
20-
<button name="%(action_wizard_product_configurator)s"
20+
<button name="configure_product"
2121
states="draft,sent"
2222
class="oe_highlight"
23-
type="action"
23+
type="object"
2424
style="margin-top: 15px;margin-bottom: 10px;"
2525
string="Configure Product"
2626
groups="product_configurator.group_product_configurator"/>

product_configurator_wizard/wizard/product_configurator.py

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,11 @@ def onchange(self, values, field_name, field_onchange):
234234
comodel_name='sale.order.line',
235235
readonly=True,
236236
)
237+
# Needed if creating a new line for an order, so we don't rely on active_id
238+
order_id = fields.Many2one(
239+
comodel_name='sale.order',
240+
readonly=True,
241+
)
237242

238243
@api.model
239244
def fields_get(self, allfields=None, attributes=None):
@@ -785,10 +790,10 @@ def action_previous_step(self):
785790

786791
return wizard_action
787792

788-
def _extra_line_values(self, so, product, new=True):
793+
def _so_line_values(self, so, product, new=True):
789794
""" Hook to allow custom line values to be put on the newly
790795
created or edited lines."""
791-
vals = {}
796+
vals = {'product_id': product.id}
792797
if new:
793798
vals.update({
794799
'name': product.display_name,
@@ -822,17 +827,20 @@ def action_config_done(self):
822827
'required steps and fields.')
823828
)
824829

825-
so = self.env['sale.order'].browse(self.env.context.get('active_id'))
826-
827-
line_vals = {'product_id': variant.id}
828-
line_vals.update(self._extra_line_values(
829-
self.order_line_id.order_id or so, variant, new=True)
830-
)
830+
# While, at the moment, the configurator works for
831+
# Sales only, this is likely to change with PO requests
832+
# already waiting.
833+
if self.order_line_id or self.order_id:
834+
line_vals = self._so_line_values(
835+
self.order_line_id.order_id or self.order_id,
836+
variant,
837+
new=not self.order_line_id
838+
)
831839

832-
if self.order_line_id:
833-
self.order_line_id.write(line_vals)
834-
else:
835-
so.write({'order_line': [(0, 0, line_vals)]})
840+
if self.order_line_id:
841+
self.order_line_id.write(line_vals)
842+
else:
843+
self.order_id.write({'order_line': [(0, 0, line_vals)]})
836844

837845
self.unlink()
838846
return

product_configurator_wizard/wizard/product_configurator_view.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<sheet>
1313
<group col="4">
1414
<group name='static_form' colspan="3" states='select'>
15+
<field name="order_id" invisible="1"/>
1516
<field name="product_id" invisible="1"/>
1617
<field name="product_tmpl_id" attrs="{'readonly': [('product_id', '!=', False)]}" required="True"/>
1718
<field attrs="{'invisible': [('attribute_line_ids', '=', [])]}" name="attribute_line_ids">

0 commit comments

Comments
 (0)