From 1544065c452230b4205e985b511b12cce75c8170 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?NIels=20G=C3=B6ttsch?=
Date: Mon, 2 Dec 2024 21:44:04 +0100
Subject: [PATCH] [imp] nfu_ecommerce: Add Open Packages to Product list
---
nfu_ecommerce/README.rst | 3 +-
nfu_ecommerce/__manifest__.py | 9 +++-
nfu_ecommerce/controllers/main.py | 41 ++++++++++++++
nfu_ecommerce/readme/HISTORY.rst | 3 +-
nfu_ecommerce/static/description/index.html | 2 +
nfu_ecommerce/views/templates.xml | 54 +++++++++++++++++++
nfu_sale_order_batch_packaging/README.rst | 1 +
.../__manifest__.py | 2 +-
.../models/__init__.py | 2 +
.../models/product_product.py | 11 ++++
.../models/product_template.py | 11 ++++
.../models/sale_order_batch_product.py | 29 ++++++++--
.../readme/HISTORY.rst | 3 +-
.../static/description/index.html | 2 +
.../views/sale_order_batch_views.xml | 3 ++
15 files changed, 167 insertions(+), 9 deletions(-)
create mode 100644 nfu_sale_order_batch_packaging/models/product_product.py
create mode 100644 nfu_sale_order_batch_packaging/models/product_template.py
diff --git a/nfu_ecommerce/README.rst b/nfu_ecommerce/README.rst
index 3cc0636..ecc4d2c 100644
--- a/nfu_ecommerce/README.rst
+++ b/nfu_ecommerce/README.rst
@@ -38,8 +38,9 @@ Changelog
:0.0.1: Initial module.
:1.0.0: Live
:1.1.0: Add Open Packagings Filter and fix rounding issue
-:1.1.1: Pass arguments to super again
:1.1.1: Make publish on website multi company
+:1.1.2: Pass arguemnts to super again
+:1.2.0: Add Open Packages to Product list
Bug Tracker
===========
diff --git a/nfu_ecommerce/__manifest__.py b/nfu_ecommerce/__manifest__.py
index 699e5fc..28cbf67 100644
--- a/nfu_ecommerce/__manifest__.py
+++ b/nfu_ecommerce/__manifest__.py
@@ -4,8 +4,13 @@
"author": "BAKEUP",
"website": "https://www.bakeup.org",
"category": "website",
- "version": "16.0.1.1.1",
- "depends": ["website_sale", "nfu_sale_order_batch_packaging", "website_decimal_quantity"],
+ "version": "16.0.1.2.0",
+ "depends": [
+ "website_sale",
+ "nfu_sale_order_batch_packaging",
+ "website_decimal_quantity",
+ "nfu_product_bnn_attributes",
+ ],
"data": ["views/product_template_views.xml", "views/templates.xml"],
"assets": {"web.assets_frontend": ["nfu_ecommerce/static/src/js/website_sale.js"]},
"license": "LGPL-3",
diff --git a/nfu_ecommerce/controllers/main.py b/nfu_ecommerce/controllers/main.py
index 217ba46..1d443da 100644
--- a/nfu_ecommerce/controllers/main.py
+++ b/nfu_ecommerce/controllers/main.py
@@ -1,5 +1,6 @@
from odoo import fields, http
from odoo.http import request
+from odoo.tools import lazy
from odoo.addons.website_sale.controllers.main import WebsiteSale
@@ -17,6 +18,46 @@ class WebsiteSale(WebsiteSale):
def _get_additional_shop_values(self, values):
new_values = super()._get_additional_shop_values(values)
new_values["open_packagings"] = True if request.httprequest.args.get("open_packagings") else False
+ # ToDo: Add batch qtys if open batch
+ batch = request.website.sale_get_order(force_create=True).batch_id
+ batch_products = batch.product_ids.filtered(lambda p: p.product_packaging_id)
+ packaging_info = {}
+ for product in values.get("products").sudo().filtered(lambda p: p.packaging_ids):
+ batch_product = batch_products.filtered(lambda p: p.product_template_id == product)
+ if batch_product:
+ batch_product = batch_product[0]
+ packaging_info[product.id] = {
+ "state": batch_product.open_packaging_state,
+ "open_qty": batch_product.open_packaging_qty,
+ "package_qty": batch_product.product_packaging_qty,
+ }
+ else:
+ packaging = product.sudo()._get_nfu_packaging()
+ if packaging and packaging.qty > 1:
+ packaging_info[product.id] = {"state": False, "open_qty": 0, "package_qty": packaging.qty}
+ new_values["packaging_info"] = packaging_info
+ new_values["get_product_packagings"] = lambda product: lazy(lambda: packaging_info.get(product.id, False))
+ return new_values
+
+ def _prepare_product_values(self, product, category, search, **kwargs):
+ new_values = super()._prepare_product_values(product, category, search, **kwargs)
+ batch = request.website.sale_get_order(force_create=True).batch_id
+ batch_product = batch.product_ids.filtered(
+ lambda p: p.product_template_id == product and p.product_packaging_id
+ )
+ packaging_info = {}
+ if batch_product:
+ batch_product = batch_product[0]
+ packaging_info = {
+ "state": batch_product.open_packaging_state,
+ "open_qty": batch_product.open_packaging_qty,
+ "package_qty": batch_product.product_packaging_qty,
+ }
+ else:
+ packaging = product.sudo()._get_nfu_packaging()
+ if packaging and packaging.qty > 1:
+ packaging_info = {"state": False, "open_qty": 0, "package_qty": packaging.qty}
+ new_values["packaging_info"] = packaging_info
return new_values
def _get_search_options(
diff --git a/nfu_ecommerce/readme/HISTORY.rst b/nfu_ecommerce/readme/HISTORY.rst
index 11c32a8..f680a1a 100644
--- a/nfu_ecommerce/readme/HISTORY.rst
+++ b/nfu_ecommerce/readme/HISTORY.rst
@@ -2,4 +2,5 @@
:1.0.0: Live
:1.1.0: Add Open Packagings Filter and fix rounding issue
:1.1.1: Pass arguments to super again
-:1.1.1: Make publish on website multi company
\ No newline at end of file
+:1.1.2: Make publish on website multi company
+:1.2.0: Add Open Packages to Product list
\ No newline at end of file
diff --git a/nfu_ecommerce/static/description/index.html b/nfu_ecommerce/static/description/index.html
index 29cd34d..da69fbb 100644
--- a/nfu_ecommerce/static/description/index.html
+++ b/nfu_ecommerce/static/description/index.html
@@ -403,6 +403,8 @@ and after the current order.
| 1.1.1: | Make publish on website multi company |
+| 1.2.0: | Add Open Packages to Product list |
+
diff --git a/nfu_ecommerce/views/templates.xml b/nfu_ecommerce/views/templates.xml
index 49342c2..4cb7083 100644
--- a/nfu_ecommerce/views/templates.xml
+++ b/nfu_ecommerce/views/templates.xml
@@ -25,6 +25,60 @@
+
+
+
+
+
+ Open Qty: /
+
+
+ State:
+
+
+
+
+
+
diff --git a/nfu_sale_order_batch_packaging/README.rst b/nfu_sale_order_batch_packaging/README.rst
index 198ce03..ad0dc1f 100644
--- a/nfu_sale_order_batch_packaging/README.rst
+++ b/nfu_sale_order_batch_packaging/README.rst
@@ -37,6 +37,7 @@ Changelog
:1.0.0: rename to nfu_sale_order_batch_packaging
:1.1.0: Live
:1.2.0: Add regeneration action for batch products
+:1.2.1: refactoring
Bug Tracker
===========
diff --git a/nfu_sale_order_batch_packaging/__manifest__.py b/nfu_sale_order_batch_packaging/__manifest__.py
index d2f494d..c127d49 100644
--- a/nfu_sale_order_batch_packaging/__manifest__.py
+++ b/nfu_sale_order_batch_packaging/__manifest__.py
@@ -4,7 +4,7 @@
"author": "BAKEUP",
"website": "https://www.bakeup.org",
"category": "Sale",
- "version": "16.0.1.2.0",
+ "version": "16.0.1.2.1",
"depends": ["sale", "sale_order_batch"],
"data": [
"views/sale_order_views.xml",
diff --git a/nfu_sale_order_batch_packaging/models/__init__.py b/nfu_sale_order_batch_packaging/models/__init__.py
index 9f32727..f6eae4a 100644
--- a/nfu_sale_order_batch_packaging/models/__init__.py
+++ b/nfu_sale_order_batch_packaging/models/__init__.py
@@ -1,3 +1,5 @@
+from . import product_product
+from . import product_template
from . import sale_order_batch
from . import sale_order_line
from . import sale_order_batch_product
diff --git a/nfu_sale_order_batch_packaging/models/product_product.py b/nfu_sale_order_batch_packaging/models/product_product.py
new file mode 100644
index 0000000..78f70cc
--- /dev/null
+++ b/nfu_sale_order_batch_packaging/models/product_product.py
@@ -0,0 +1,11 @@
+from odoo import models
+
+
+class ProductProduct(models.Model):
+ _inherit = "product.product"
+
+ def _get_nfu_packaging(self):
+ self.ensure_one()
+ if self.packaging_ids:
+ return self.packaging_ids[0]
+ return False
diff --git a/nfu_sale_order_batch_packaging/models/product_template.py b/nfu_sale_order_batch_packaging/models/product_template.py
new file mode 100644
index 0000000..8d77185
--- /dev/null
+++ b/nfu_sale_order_batch_packaging/models/product_template.py
@@ -0,0 +1,11 @@
+from odoo import models
+
+
+class ProductTemplate(models.Model):
+ _inherit = "product.template"
+
+ def _get_nfu_packaging(self):
+ self.ensure_one()
+ if self.packaging_ids:
+ return self.packaging_ids[0]
+ return False
diff --git a/nfu_sale_order_batch_packaging/models/sale_order_batch_product.py b/nfu_sale_order_batch_packaging/models/sale_order_batch_product.py
index db0a8db..86544de 100644
--- a/nfu_sale_order_batch_packaging/models/sale_order_batch_product.py
+++ b/nfu_sale_order_batch_packaging/models/sale_order_batch_product.py
@@ -1,10 +1,15 @@
from odoo import api, fields, models
+PACKAGING_STATES = [("open", "Open"), ("full", "Full"), ("last_open", "Last package open")]
+
+
class SaleOrderBatchProduct(models.Model):
_inherit = "sale.order.batch.product"
+ product_uom_max_qty = fields.Float("Max Qty", compute="_compute_product_uom_max_qty")
open_packaging_qty = fields.Float(compute="_compute_open_packagin_qty", store=True)
+ open_packaging_state = fields.Selection(selection=PACKAGING_STATES, compute="_compute_open_packagin_state")
@api.depends("sale_order_line_ids.product_uom_qty")
def _compute_open_packagin_qty(self):
@@ -16,12 +21,30 @@ class SaleOrderBatchProduct(models.Model):
0 if open_packaging_qty == product.product_packaging_qty else open_packaging_qty
)
+ @api.depends("sale_order_line_ids.product_uom_max_qty")
+ def _compute_product_uom_max_qty(self):
+ for product in self:
+ if product.sale_order_line_ids:
+ product.product_uom_max_qty = sum(product.sale_order_line_ids.mapped("product_uom_max_qty"))
+ else:
+ product.product_uom_max_qty = 0
+
+ @api.depends("open_packaging_qty", "product_packaging_qty")
+ def _compute_open_packagin_state(self):
+ for product in self:
+ if product.open_packaging_qty == 0:
+ product.open_packaging_state = "full"
+ elif product.open_packaging_qty < product.product_packaging_qty:
+ product.open_packaging_state = "open"
+ else:
+ product.open_packaging_state = "last_open"
+
@api.model_create_multi
def create(self, vals_list):
for vals in vals_list:
-
if vals.get("product_id") and not vals.get("product_packaging_id"):
- product_id = vals.get("product_id")
- packaging = self.env["product.packaging"].search([("product_id", "=", product_id)], limit=1)
+ packaging = (
+ self.env["product.product"].search([("id", "=", vals.get("product_id"))])._get_nfu_packaging()
+ )
vals["product_packaging_id"] = packaging.id
return super().create(vals_list)
diff --git a/nfu_sale_order_batch_packaging/readme/HISTORY.rst b/nfu_sale_order_batch_packaging/readme/HISTORY.rst
index eaeee39..0c83ee9 100644
--- a/nfu_sale_order_batch_packaging/readme/HISTORY.rst
+++ b/nfu_sale_order_batch_packaging/readme/HISTORY.rst
@@ -1,4 +1,5 @@
:0.0.1: Initial module.
:1.0.0: rename to nfu_sale_order_batch_packaging
:1.1.0: Live
-:1.2.0: Add regeneration action for batch products
\ No newline at end of file
+:1.2.0: Add regeneration action for batch products
+:1.2.1: refactoring
\ No newline at end of file
diff --git a/nfu_sale_order_batch_packaging/static/description/index.html b/nfu_sale_order_batch_packaging/static/description/index.html
index bc06d57..cd5df09 100644
--- a/nfu_sale_order_batch_packaging/static/description/index.html
+++ b/nfu_sale_order_batch_packaging/static/description/index.html
@@ -399,6 +399,8 @@ and adds a packaging default to sale order batch.
| 1.2.0: | Add regeneration action for batch products |
+| 1.2.1: | refactoring |
+
diff --git a/nfu_sale_order_batch_packaging/views/sale_order_batch_views.xml b/nfu_sale_order_batch_packaging/views/sale_order_batch_views.xml
index 09d11de..d70a098 100644
--- a/nfu_sale_order_batch_packaging/views/sale_order_batch_views.xml
+++ b/nfu_sale_order_batch_packaging/views/sale_order_batch_views.xml
@@ -30,6 +30,9 @@
+
+
+