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

[ENH] Store source order_id to avoid active_id #85

Open
wants to merge 2 commits into
base: 10.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions product_configurator_wizard/models/sale.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
43 changes: 35 additions & 8 deletions product_configurator_wizard/tests/test_wizard.py
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down Expand Up @@ -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(
Expand All @@ -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")

4 changes: 2 additions & 2 deletions product_configurator_wizard/views/sale_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
<field name="inherit_id" ref="sale.view_order_form"/>
<field name="arch" type="xml">
<xpath expr="//field[@name='order_line']" position="before">
<button name="%(action_wizard_product_configurator)s"
<button name="configure_product"
states="draft,sent"
class="oe_highlight"
type="action"
type="object"
style="margin-top: 15px;margin-bottom: 10px;"
string="Configure Product"
groups="product_configurator.group_product_configurator"/>
Expand Down
32 changes: 20 additions & 12 deletions product_configurator_wizard/wizard/product_configurator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<sheet>
<group col="4">
<group name='static_form' colspan="3" states='select'>
<field name="order_id" invisible="1"/>
<field name="product_id" invisible="1"/>
<field name="product_tmpl_id" attrs="{'readonly': [('product_id', '!=', False)]}" required="True"/>
<field attrs="{'invisible': [('attribute_line_ids', '=', [])]}" name="attribute_line_ids">
Expand Down