Skip to content

Commit

Permalink
[16.0][MIG] sale_order_secondary_unit
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrea committed Aug 10, 2023
1 parent 6440b24 commit cfd2f9d
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 41 deletions.
2 changes: 1 addition & 1 deletion sale_order_secondary_unit/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{
"name": "Sale Order Secondary Unit",
"summary": "Sale product in a secondary unit",
"version": "15.0.2.0.0",
"version": "16.0.1.0.0",
"development_status": "Production/Stable",
"category": "Sale",
"website": "https://github.com/OCA/sale-workflow",
Expand Down
9 changes: 5 additions & 4 deletions sale_order_secondary_unit/models/sale_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class SaleOrderLine(models.Model):
store=True, readonly=False, compute="_compute_product_uom_qty", copy=True
)

@api.depends("secondary_uom_qty", "secondary_uom_id", "product_uom_qty")
@api.depends("product_uom_qty")
def _compute_product_uom_qty(self):
self._compute_helper_target_field_qty()

Expand All @@ -30,14 +30,14 @@ def onchange_product_uom_for_secondary(self):
self._onchange_helper_product_uom_for_secondary()

@api.onchange("product_id")
def product_id_change(self):
def _onchange_product_id_warning(self):
"""
If default sales secondary unit set on product, put on secondary
quantity 1 for being the default quantity. We override this method,
that is the one that sets by default 1 on the other quantity with that
purpose.
"""
res = super().product_id_change()
res = super()._onchange_product_id_warning()
line_uom_qty = self.product_uom_qty
self.secondary_uom_id = self.product_id.sale_secondary_uom_id
if self.product_id.sale_secondary_uom_id:
Expand All @@ -48,8 +48,9 @@ def product_id_change(self):
self.product_uom_qty = line_uom_qty
return res

@api.depends("secondary_uom_qty", "product_uom_qty", "price_unit")
@api.onchange("price_unit", "secondary_uom_qty", "secondary_uom_id")
def _compute_secondary_uom_unit_price(self):
self._compute_helper_target_field_qty()
for line in self:
if line.secondary_uom_id:
try:
Expand Down
18 changes: 11 additions & 7 deletions sale_order_secondary_unit/tests/test_sale_order_secondary_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def setUpClass(cls):
},
)
],
"list_price": 1000.00,
}
)
cls.secondary_unit = cls.env["product.secondary.unit"].search(
Expand Down Expand Up @@ -65,7 +66,7 @@ def test_onchange_secondary_unit_product_uom_qty(self):
self.assertEqual(self.order.order_line.secondary_uom_qty, 7.0)

def test_default_secondary_unit(self):
self.order.order_line.product_id_change()
self.order.order_line._onchange_product_id_warning()
self.assertEqual(self.order.order_line.secondary_uom_id, self.secondary_unit)

def test_onchange_order_product_uom(self):
Expand Down Expand Up @@ -95,12 +96,15 @@ def test_independent_type(self):

def test_secondary_uom_unit_price(self):
# Remove secondary uom in sale line to do a complete test of secondary price
self.order.order_line.secondary_uom_id = False
self.assertEqual(self.order.order_line.secondary_uom_unit_price, 0)
self.order.order_line.update(
{"secondary_uom_id": self.secondary_unit.id, "product_uom_qty": 2}
)

with Form(self.env["sale.order"]) as order_form:
order_form.partner_id = self.partner
order_form.pricelist_id = self.price_list
with order_form.order_line.new() as line_form:
line_form.product_id = self.product
line_form.product_uom_qty = 2
line_form.secondary_uom_id = self.secondary_unit
line_form.price_unit = 1000.00
self.order = order_form.save()
self.assertEqual(self.order.order_line.secondary_uom_qty, 4)
self.assertEqual(self.order.order_line.secondary_uom_unit_price, 500)

Expand Down
7 changes: 5 additions & 2 deletions sale_order_secondary_unit/views/product_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@
<field name="name">Product template Secondary Unit</field>
<field name="model">product.template</field>
<field name="inherit_id" ref="product.product_template_form_view" />
<field name="groups_id" eval="[(4, ref('uom.group_uom'))]" />
<field name="arch" type="xml">
<field name="uom_id" position="after">
<field name="sale_secondary_uom_id" options="{'no_create': True}" />
<field
name="sale_secondary_uom_id"
groups="uom.group_uom"
options="{'no_create': True}"
/>
</field>
</field>
</record>
Expand Down
57 changes: 30 additions & 27 deletions sale_order_secondary_unit/views/sale_order_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,48 +6,51 @@
<field name="name">Sale Order Secondary Unit</field>
<field name="model">sale.order</field>
<field name="inherit_id" ref="sale.view_order_form" />
<field name="groups_id" eval="[(4, ref('uom.group_uom'))]" />
<field name="arch" type="xml">
<xpath
expr="//field[@name='order_line']/form//field[@name='price_subtotal']"
position="after"
>
<label for="secondary_uom_qty" />
<div class="o_row" name="secondary_uom_qty">
<field
name="secondary_uom_qty"
class="oe_inline oe_no_button"
widget="numeric_step"
options="{'auto_select': True}"
attrs="{'readonly': [('state', 'in', ('done', 'cancel'))]}"
/>
<field
name="secondary_uom_id"
class="oe_inline oe_no_button"
domain="['|', ('product_id', '=', product_id),
'&amp;', ('product_tmpl_id.product_variant_ids', 'in', [product_id]),
('product_id', '=', False)]"
options="{'no_create': True, 'no_open': True}"
attrs="{'readonly': [('product_uom_readonly', '=', True)]}"
/>
</div>
<t groups="uom.group_uom">
<label for="secondary_uom_qty" />
<div class="o_row" name="secondary_uom_qty">
<field
name="secondary_uom_qty"
class="oe_inline oe_no_button"
widget="numeric_step"
options="{'auto_select': True}"
attrs="{'readonly': [('state', 'in', ('done', 'cancel'))]}"
/>
<field
name="secondary_uom_id"
class="oe_inline oe_no_button"
domain="['|', ('product_id', '=', product_id),
'&amp;', ('product_tmpl_id.product_variant_ids', 'in', [product_id]),
('product_id', '=', False)]"
options="{'no_create': True, 'no_open': True}"
attrs="{'readonly': [('product_uom_readonly', '=', True)]}"
/>
</div>
</t>
</xpath>
<xpath
expr="//field[@name='order_line']/tree//field[@name='product_uom_qty']"
position="before"
>
<field

<field
name="secondary_uom_qty"
attrs="{'readonly': [('parent.state', 'in', ('done', 'cancel'))]}"
/>
<field
<field
name="secondary_uom_id"
domain="['|', ('product_id', '=', product_id),
'&amp;', ('product_tmpl_id.product_variant_ids', 'in', [product_id]),
('product_id', '=', False)]"
'&amp;', ('product_tmpl_id.product_variant_ids', 'in', [product_id]),
('product_id', '=', False)]"
options="{'no_create': True}"
attrs="{'readonly': [('product_uom_readonly', '=', True)]}"
/>

</xpath>
<xpath
expr="//field[@name='order_line']/kanban//field[@name='company_id']"
Expand All @@ -56,13 +59,13 @@
<field name="secondary_uom_id" invisible="1" />
<field name="secondary_uom_qty" invisible="1" />
</xpath>
<xpath
expr="//field[@name='order_line']/kanban//t[@t-esc='record.product_uom_qty.value']"
<xpath
expr="//field[@name='order_line']/kanban//t[@t-out='record.product_uom_qty.value']"
position="before"
>
<t t-if="record.secondary_uom_id.value">
<t t-esc="record.secondary_uom_qty.value" />
<t t-esc="record.secondary_uom_id.value" /> ->
<t t-esc="record.secondary_uom_id.value" />
</t>
</xpath>
</field>
Expand Down
6 changes: 6 additions & 0 deletions setup/sale_order_secondary_unit/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import setuptools

setuptools.setup(
setup_requires=['setuptools-odoo'],
odoo_addon=True,
)

0 comments on commit cfd2f9d

Please sign in to comment.