diff --git a/product_configurator_wizard/models/sale.py b/product_configurator_wizard/models/sale.py
index 34eeacde..c5b2e7fb 100644
--- a/product_configurator_wizard/models/sale.py
+++ b/product_configurator_wizard/models/sale.py
@@ -39,3 +39,17 @@ def reconfigure_product(self):
'target': 'new',
'res_id': wizard.id,
}
+
+
+class SaleOrder(models.Model):
+ _inherit = 'sale.order'
+
+ @api.multi
+ def configure_product(self):
+ res = self.env['ir.actions.act_window'].for_xml_id(
+ 'product_configurator_wizard',
+ 'action_wizard_product_configurator'
+ )
+ res['context'] = {'default_order_id': self.id
+ }
+ return res
diff --git a/product_configurator_wizard/tests/test_wizard.py b/product_configurator_wizard/tests/test_wizard.py
index 3acd8643..f9151349 100644
--- a/product_configurator_wizard/tests/test_wizard.py
+++ b/product_configurator_wizard/tests/test_wizard.py
@@ -54,10 +54,12 @@ def wizard_write_proceed(self, wizard, attr_vals, value_ids=None):
def test_wizard_configuration(self):
"""Test product configurator wizard"""
+ existing_lines = self.so.order_line
+
# Start a new configuration wizard
wizard_obj = self.env['product.configurator'].with_context({
'active_model': 'sale.order',
- 'active_id': self.so.id
+ 'default_order_id': self.so.id
})
wizard = wizard_obj.create({'product_tmpl_id': self.cfg_tmpl.id})
@@ -94,14 +96,11 @@ def test_wizard_configuration(self):
self.assertTrue(len(config_variants) == 1,
"Wizard did not create a configurable variant")
- def test_reconfiguration(self):
- """Test reconfiguration functionality of the wizard"""
- self.test_wizard_configuration()
-
- order_line = self.so.order_line.filtered(
- lambda l: l.product_id.config_ok
- )
+ created_line = self.so.order_line - existing_lines
+ self.assertTrue(len(created_line) == 1,
+ "Wizard did not create an order line")
+ def do_reconfigure(self, order_line):
reconfig_action = order_line.reconfigure_product()
wizard = self.env['product.configurator'].browse(
@@ -115,9 +114,37 @@ def test_reconfiguration(self):
while wizard.action_next_step():
pass
+ def test_reconfiguration(self):
+ """Test reconfiguration functionality of the wizard"""
+ self.test_wizard_configuration()
+
+ existing_lines = self.so.order_line
+
+ order_line = self.so.order_line.filtered(
+ lambda l: l.product_id.config_ok
+ )
+
+ self.do_reconfigure(order_line)
+
config_variants = self.env['product.product'].search([
('config_ok', '=', True)
])
self.assertTrue(len(config_variants) == 2,
"Wizard reconfiguration did not create a new variant")
+
+ created_line = self.so.order_line - existing_lines
+ self.assertTrue(len(created_line) == 0,
+ "Wizard created an order line on reconfiguration")
+
+ # test that running through again with the same values does not
+ # create another variant
+ self.do_reconfigure(order_line)
+
+ config_variants = self.env['product.product'].search([
+ ('config_ok', '=', True)
+ ])
+
+ self.assertTrue(len(config_variants) == 2,
+ "Wizard reconfiguration created a redundant variant")
+
diff --git a/product_configurator_wizard/views/sale_view.xml b/product_configurator_wizard/views/sale_view.xml
index 40485226..d8a4487f 100644
--- a/product_configurator_wizard/views/sale_view.xml
+++ b/product_configurator_wizard/views/sale_view.xml
@@ -17,10 +17,10 @@
-
diff --git a/product_configurator_wizard/wizard/product_configurator.py b/product_configurator_wizard/wizard/product_configurator.py
index 5e0055d0..e6b06a6f 100644
--- a/product_configurator_wizard/wizard/product_configurator.py
+++ b/product_configurator_wizard/wizard/product_configurator.py
@@ -234,6 +234,11 @@ def onchange(self, values, field_name, field_onchange):
comodel_name='sale.order.line',
readonly=True,
)
+ # Needed if creating a new line for an order, so we don't rely on active_id
+ order_id = fields.Many2one(
+ comodel_name='sale.order',
+ readonly=True,
+ )
@api.model
def fields_get(self, allfields=None, attributes=None):
@@ -785,10 +790,10 @@ def action_previous_step(self):
return wizard_action
- def _extra_line_values(self, so, product, new=True):
+ def _so_line_values(self, so, product, new=True):
""" Hook to allow custom line values to be put on the newly
created or edited lines."""
- vals = {}
+ vals = {'product_id': product.id}
if new:
vals.update({
'name': product.display_name,
@@ -822,17 +827,20 @@ def action_config_done(self):
'required steps and fields.')
)
- so = self.env['sale.order'].browse(self.env.context.get('active_id'))
-
- line_vals = {'product_id': variant.id}
- line_vals.update(self._extra_line_values(
- self.order_line_id.order_id or so, variant, new=True)
- )
+ # While, at the moment, the configurator works for
+ # Sales only, this is likely to change with PO requests
+ # already waiting.
+ if self.order_line_id or self.order_id:
+ line_vals = self._so_line_values(
+ self.order_line_id.order_id or self.order_id,
+ variant,
+ new=not self.order_line_id
+ )
- if self.order_line_id:
- self.order_line_id.write(line_vals)
- else:
- so.write({'order_line': [(0, 0, line_vals)]})
+ if self.order_line_id:
+ self.order_line_id.write(line_vals)
+ else:
+ self.order_id.write({'order_line': [(0, 0, line_vals)]})
self.unlink()
return
diff --git a/product_configurator_wizard/wizard/product_configurator_view.xml b/product_configurator_wizard/wizard/product_configurator_view.xml
index 88580774..d2fb6d9a 100644
--- a/product_configurator_wizard/wizard/product_configurator_view.xml
+++ b/product_configurator_wizard/wizard/product_configurator_view.xml
@@ -12,6 +12,7 @@
+