[add] packaging to webshop

This commit is contained in:
Niels Göttsch 2024-07-29 22:18:08 +02:00
parent afbc9aaddc
commit 5d582e7636
7 changed files with 80 additions and 3 deletions

View file

@ -1 +1,2 @@
from . import sale_order from . import sale_order
from . import sale_order_line

View file

@ -47,3 +47,11 @@ class SaleOrder(models.Model):
values["product_uom_max_qty"] = max_qty values["product_uom_max_qty"] = max_qty
return values return values
@api.model_create_multi
def create(self, vals_list):
orders = super().create(vals_list)
for order in orders:
if order.website_id:
order.action_add_to_batch()
return orders

View file

@ -0,0 +1,10 @@
from odoo import models
class SaleOrderLine(models.Model):
_inherit = "sale.order.line"
def compute_open_packages(self):
self.ensure_one()
open_qty = self.batch_uom_qty % self.product_packaging_id.qty
return open_qty

View file

@ -1,6 +1,14 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<odoo> <odoo>
<template id="cart_lines_max_qty" inherit_id="website_sale.cart_lines" name="Shopping Cart Lines"> <template id="cart_lines_packaging" inherit_id="website_sale.cart_lines" name="Shopping Cart Lines">
<xpath expr="//td[hasclass('td-product_name')]" position="inside">
<div>
<span t-if="line.product_packaging_id">Open Package: <t t-esc="line.compute_open_packages()"/>/<t
t-esc="line.product_packaging_id.qty"
/><br/></span>
<span t-if="line.batch_uom_qty &gt; 0">Ordered Quantity: <t t-esc="line.batch_uom_qty"/></span>
</div>
</xpath>
<xpath expr="//th[hasclass('td-qty')]" position="after"> <xpath expr="//th[hasclass('td-qty')]" position="after">
<th class="text-center td-qty"> <th class="text-center td-qty">
<t t-if="show_qty"> <t t-if="show_qty">
@ -74,7 +82,15 @@
</td> </td>
</xpath> </xpath>
</template> </template>
<template id="cart_summary_max_qty" inherit_id="website_sale.cart_summary" name="Cart right column">
<template id="cart_summary_packaging" inherit_id="website_sale.cart_summary" name="Cart right column">
<xpath expr="//td[hasclass('td-product_name')]" position="inside">
<div>
<span t-if="line.product_packaging_id">Open Package: <t t-esc="line.product_packaging_id.qty"/><br
/></span>
<span t-if="line.batch_uom_qty &gt; 0">Ordered Quantity: <t t-esc="line.batch_uom_qty"/></span>
</div>
</xpath>
<xpath expr="//th[hasclass('td-qty')]" position="after"> <xpath expr="//th[hasclass('td-qty')]" position="after">
<th class="border-top-0 td-qty">Max Qty</th> <th class="border-top-0 td-qty">Max Qty</th>
</xpath> </xpath>

View file

@ -1,5 +1,6 @@
from odoo import _, api, fields, models from odoo import _, api, fields, models
from odoo.exceptions import UserError from odoo.exceptions import UserError
from odoo.tools import float_round
class SaleOrderLine(models.Model): class SaleOrderLine(models.Model):
@ -7,6 +8,7 @@ class SaleOrderLine(models.Model):
product_uom_ordered_qty = fields.Float(string="Ordered Qty", digits="Product Unit of Measure", default=1.0) product_uom_ordered_qty = fields.Float(string="Ordered Qty", digits="Product Unit of Measure", default=1.0)
product_uom_max_qty = fields.Float(string="Max Qty", digits="Product Unit of Measure") product_uom_max_qty = fields.Float(string="Max Qty", digits="Product Unit of Measure")
batch_uom_qty = fields.Float(compute="_compute_batch_uom_qty")
@api.constrains("product_uom_qty", "product_uom_max_qty") @api.constrains("product_uom_qty", "product_uom_max_qty")
def _check_product_uom_qty(self): def _check_product_uom_qty(self):
@ -18,3 +20,40 @@ class SaleOrderLine(models.Model):
"The quantity must be less than or equal to the maximum quantity." "The quantity must be less than or equal to the maximum quantity."
) )
) )
@api.depends("product_uom_qty")
def _compute_batch_uom_qty(self):
for line in self:
if line.order_id.batch_id:
batch_id = line.batch_id
product_id = line.product_id
batch_uom_qtys = (
self.env["sale.order.line"]
.sudo()
.search([("batch_id", "=", batch_id.id), ("product_id", "=", product_id.id)])
).mapped("product_uom_qty")
line.batch_uom_qty = sum(batch_uom_qtys)
else:
line.batch_uom_qty = 0
@api.depends("product_packaging_id", "product_uom", "product_uom_qty", "batch_id")
def _compute_product_packaging_qty(self):
for line in self:
if line.batch_id and line.product_packaging_id:
packaging_uom = line.product_packaging_id.product_uom_id
batch_uom_qty = line.product_uom._compute_quantity(line.batch_uom_qty, packaging_uom)
line.product_packaging_qty = float_round(
batch_uom_qty / line.product_packaging_id.qty, precision_rounding=packaging_uom.rounding
)
else:
super()._compute_product_packaging_qty()
return True
@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)
vals["product_packaging_id"] = packaging.id
return super().create(vals_list)

View file

@ -10,6 +10,7 @@
</xpath> </xpath>
<xpath expr="//field[@name='order_line']/tree/field[@name='product_uom_qty']" position="after"> <xpath expr="//field[@name='order_line']/tree/field[@name='product_uom_qty']" position="after">
<field name="product_uom_max_qty"/> <field name="product_uom_max_qty"/>
<field name="batch_uom_qty"/>
</xpath> </xpath>
</field> </field>
</record> </record>

View file

@ -1,9 +1,11 @@
from odoo import api, models from odoo import api, fields, models
class SaleOrderLine(models.Model): class SaleOrderLine(models.Model):
_inherit = "sale.order.line" _inherit = "sale.order.line"
batch_id = fields.Many2one(related="order_id.batch_id")
def _update_batch_products(self): def _update_batch_products(self):
for order_line in self: for order_line in self:
batch_id = order_line.order_id.batch_id batch_id = order_line.order_id.batch_id