Skip to content

Commit

Permalink
[MIG] website_sale_secondary_unit: Migration to 15.0
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlosRoca13 committed Aug 16, 2022
1 parent 9a91e68 commit 645429c
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 64 deletions.
15 changes: 11 additions & 4 deletions website_sale_secondary_unit/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{
"name": "Website Sale Secondary Unit",
"summary": "Allow manage secondary units in website shop",
"version": "13.0.1.1.2",
"version": "15.0.1.0.0",
"development_status": "Beta",
"category": "Website",
"website": "https:/OCA/e-commerce",
Expand All @@ -15,12 +15,19 @@
"data": [
"security/ir.model.access.csv",
"security/website_sale_secondary_unit.xml",
"views/assets.xml",
"templates/assets.xml",
"views/product_template_views.xml",
"views/product_secondary_unit_views.xml",
"views/product_template_views.xml",
"views/templates.xml",
],
"demo": ["data/demo.xml"],
"post_init_hook": "post_init_hook",
"assets": {
"web.assets_frontend": [
"/website_sale_secondary_unit/static/src/js/website_sale_secondary_unit.js",
"/website_sale_secondary_unit/static/src/scss/website_sale_secondary_unit.scss",
],
"we.assets_tests": [
"/website_sale_secondary_unit/static/src/js/website_sale_secondary_unit_tour.js"
],
},
}
17 changes: 15 additions & 2 deletions website_sale_secondary_unit/controllers/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class WebsiteSaleSecondaryUnit(WebsiteSale):
def cart_update(self, product_id, add_qty=1, set_qty=0, **kw):
# Add secondary uom info to session
request.session.pop("secondary_uom_id", None)
if "secondary_uom_id" in kw:
if kw.get("secondary_uom_id"):
secondary_uom = request.env["product.secondary.unit"].browse(
int(kw["secondary_uom_id"])
)
Expand All @@ -20,10 +20,15 @@ def cart_update(self, product_id, add_qty=1, set_qty=0, **kw):

@http.route()
def cart_update_json(
self, product_id, line_id=None, add_qty=None, set_qty=None, display=True
self, product_id, line_id=None, add_qty=None, set_qty=None, display=True, **kw
):
so_line = request.env["sale.order.line"].browse(line_id)
request.session.pop("secondary_uom_id", None)
if kw.get("secondary_uom_id"):
secondary_uom = request.env["product.secondary.unit"].browse(
int(kw["secondary_uom_id"])
)
request.session["secondary_uom_id"] = secondary_uom.id
if so_line.sudo().secondary_uom_id:
request.session["secondary_uom_id"] = so_line.sudo().secondary_uom_id.id
return super().cart_update_json(
Expand All @@ -32,4 +37,12 @@ def cart_update_json(
add_qty=add_qty,
set_qty=set_qty,
display=display,
**kw,
)

def _prepare_product_values(self, product, category, search, **kwargs):
res = super()._prepare_product_values(product, category, search, **kwargs)
res["secondary_uom_ids"] = product.secondary_uom_ids.filtered(
lambda su: su.active and su.is_published
)
return res

This file was deleted.

23 changes: 15 additions & 8 deletions website_sale_secondary_unit/models/sale_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ def _cart_find_product_line(self, product_id=None, line_id=None, **kwargs):
)
return so_lines

def _website_product_id_change(self, order_id, product_id, qty=0):
res = super()._website_product_id_change(order_id, product_id, qty=qty)
def _website_product_id_change(self, order_id, product_id, qty=0, **kwargs):
res = super()._website_product_id_change(
order_id, product_id, qty=qty, **kwargs
)
secondary_uom_id = self.env.context.get("secondary_uom_id", False)
res["secondary_uom_id"] = secondary_uom_id
return res
Expand All @@ -46,7 +48,7 @@ def _cart_update(
secondary_uom_id = sol.secondary_uom_id.id
else:
secondary_uom_id = request.session.get("secondary_uom_id", False)
ctx = self.env.context.copy()
self.env.context.copy()
if not secondary_uom_id:
# Check the default value for secondary uom or is a product can
# not allow to sell in base unit, so the default secondary uom
Expand All @@ -62,9 +64,9 @@ def _cart_update(
precision_rounding=secondary_uom.uom_id.rounding,
)
secondary_uom_id = secondary_uom.id
if secondary_uom_id:
ctx["secondary_uom_id"] = secondary_uom_id
return super(SaleOrder, self.with_context(ctx))._cart_update(
return super(
SaleOrder, self.with_context(secondary_uom_id=secondary_uom_id)
)._cart_update(
product_id=product_id,
line_id=line_id,
add_qty=add_qty,
Expand All @@ -74,7 +76,7 @@ def _cart_update(
)

def _compute_cart_info(self):
super()._compute_cart_info()
res = super()._compute_cart_info()
for order in self:
secondary_unit_lines = order.website_order_line.filtered("secondary_uom_id")
if secondary_unit_lines:
Expand All @@ -84,6 +86,7 @@ def _compute_cart_info(self):
so_lines = order.website_order_line - secondary_unit_lines
cart_quantity = int(sum(so_lines.mapped("product_uom_qty")))
order.cart_quantity = cart_quantity + cart_secondary_quantity
return res


class SaleOrderLine(models.Model):
Expand Down Expand Up @@ -118,7 +121,11 @@ def write(self, vals):
and Uom.browse(vals["product_uom"])
or line.product_uom
)
if "product_uom_qty" in vals and secondary_uom:
if (
"product_uom_qty" in vals
and secondary_uom
and secondary_uom.dependency_type == "dependent"
):
factor = secondary_uom.factor * uom.factor
vals["secondary_uom_qty"] = float_round(
vals["product_uom_qty"] / (factor or 1.0),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,28 @@ odoo.define("website_sale_secondary_unit.animation", function (require) {
},
});
});

odoo.define("website_sale_secondary_unit.website_sale", function (require) {
"use strict";

var publicWidget = require("web.public.widget");
require("website_sale.website_sale");

publicWidget.registry.WebsiteSale.include({
_submitForm: function () {
if (
!("secondary_uom_id" in this.rootProduct) &&
$(this.$target).find("#secondary_uom").length
) {
this.rootProduct.secondary_uom_id = $(this.$target)
.find("#secondary_uom")
.val();
this.rootProduct.secondary_uom_qty = $(this.$target)
.find(".secondary-quantity")
.val();
}

this._super.apply(this, arguments);
},
});
});
11 changes: 0 additions & 11 deletions website_sale_secondary_unit/templates/assets.xml

This file was deleted.

16 changes: 0 additions & 16 deletions website_sale_secondary_unit/views/assets.xml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
Copyright 2021 Tecnativa - Pedro M. Baeza
License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). -->
<odoo>
<record id="product_secondary_unit_view_tree" model="ir.ui.view">
<field name="model">product.secondary.unit</field>
<record id="product_template_form_view_secondary_unit" model="ir.ui.view">
<field name="model">product.template</field>
<field
name="inherit_id"
ref="product_secondary_unit.product_secondary_unit_view_tree"
ref="product_secondary_unit.product_template_form_view"
/>
<field name="arch" type="xml">
<field name="factor" position="after">
<xpath expr="//tree/field[@name='factor']" position="after">
<field name="website_published" />
</field>
</xpath>
</field>
</record>
</odoo>
4 changes: 2 additions & 2 deletions website_sale_secondary_unit/views/product_template_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
<!-- Copyright 2019 Tecnativa - Sergio Teruel
License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). -->
<odoo>
<record id="product_template_form_view" model="ir.ui.view">
<record id="product_template_form_view_website_sale" model="ir.ui.view">
<field name="name">product.template.website.secondary.unit.form</field>
<field name="model">product.template</field>
<field name="inherit_id" ref="website_sale.product_template_form_view" />
<field name="arch" type="xml">
<field name="website_style_ids" position="after">
<field name="website_ribbon_id" position="after">
<field name="allow_uom_sell" />
</field>
</field>
Expand Down
12 changes: 6 additions & 6 deletions website_sale_secondary_unit/views/templates.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
expr="//div[@id='product_details']//t[@t-call='website_sale.product_price']"
position="after"
>
<t t-if="product.secondary_uom_ids">
<t t-if="secondary_uom_ids">
<div class="mb8 secondary-unit">
<t t-call="website_sale_secondary_unit.secondary_qty" />
<select
Expand All @@ -63,15 +63,15 @@
name="secondary_uom_id"
>
<option
t-if="product.secondary_uom_ids and product.allow_uom_sell"
t-if="secondary_uom_ids and product.allow_uom_sell"
value="0"
t-att-selected="'selected' if not product.sale_secondary_uom_id else None"
t-att-data-secondary-uom-factor="1.0"
t-att-data-product-uom-factor="1.0"
>
<span t-esc="product.uom_id.sudo().name" />
</option>
<t t-foreach="product.secondary_uom_ids" t-as="secondary_uom">
<t t-foreach="secondary_uom_ids" t-as="secondary_uom">
<option
t-att-value="secondary_uom.id"
t-att-selected="'selected' if product.sudo().sale_secondary_uom_id.id == secondary_uom.id else None"
Expand All @@ -89,8 +89,8 @@
</xpath>
</template>
<template id="product_price" inherit_id="website_sale.product_price">
<xpath expr="//b[hasclass('oe_price')]" position="after">
<t t-if="product.secondary_uom_ids">
<xpath expr="//span[hasclass('oe_price')]" position="after">
<t t-if="secondary_uom_ids">
/ <span
class="css_editable_mode_hidden price_uom"
t-field="product.uom_id"
Expand All @@ -102,7 +102,7 @@
<xpath expr="//input[@name='add_qty']/.." position="attributes">
<attribute
name="t-attf-class"
>css_quantity input-group oe_website_spinner #{'d-none' if product.secondary_uom_ids else None}</attribute>
>css_quantity input-group oe_website_spinner #{'d-none' if secondary_uom_ids else None}</attribute>
</xpath>
</template>
<template id="cart_lines" inherit_id="website_sale.cart_lines">
Expand Down

0 comments on commit 645429c

Please sign in to comment.