diff --git a/nfu_sale_product_min_max_qty/models/sale_order.py b/nfu_sale_product_min_max_qty/models/sale_order.py index 23b1a50..2b388c5 100644 --- a/nfu_sale_product_min_max_qty/models/sale_order.py +++ b/nfu_sale_product_min_max_qty/models/sale_order.py @@ -11,17 +11,15 @@ class SaleOrder(models.Model): sale_max_qty_chooser = self.env["sale.max.qty.chooser"].create({}) draft_sale_orders = self.filtered(lambda x: x.state == "draft") for sale_order in draft_sale_orders: - sale_order_lines = self.env["sale.order.line"].search([("id", "in", sale_order.order_line.ids)]) - - for line in sale_order_lines: - if not line.display_type: - self.env["sale.max.qty.line"].create( - { - "sale_line_id": line.id, - "sale_max_qty_chooser": sale_max_qty_chooser.id, - "qty": line.product_uom_qty, - } - ) + sale_max_qty_chooser.sale_order_ids += sale_order + for line in sale_order.order_line.filtered(lambda x: not x.display_type): + self.env["sale.max.qty.line"].create( + { + "sale_line_id": line.id, + "sale_max_qty_chooser": sale_max_qty_chooser.id, + "qty": line.product_uom_qty, + } + ) action = { "name": _("Min max qty wizard"), @@ -32,6 +30,18 @@ class SaleOrder(models.Model): "res_id": sale_max_qty_chooser.id, } return action + + def action_sale_order_lines(self): + """ + Here you need to create the wizzard objects first and then call the wizzard with the newly created id + """ + return { + "name": _("Min max qty wizard"), + "type": "ir.actions.act_window", + "res_model": "sale.order.line", + "view_mode": "tree,form", + "domain": [("id", "in", self.order_line.ids)], + } def _prepare_order_line_values( self, diff --git a/nfu_sale_product_min_max_qty/views/sale_order_views.xml b/nfu_sale_product_min_max_qty/views/sale_order_views.xml index b54308e..9380572 100644 --- a/nfu_sale_product_min_max_qty/views/sale_order_views.xml +++ b/nfu_sale_product_min_max_qty/views/sale_order_views.xml @@ -19,6 +19,7 @@
diff --git a/nfu_sale_product_min_max_qty/wizard/sale_max_qty_chooser.py b/nfu_sale_product_min_max_qty/wizard/sale_max_qty_chooser.py index 4246ccc..6063b27 100644 --- a/nfu_sale_product_min_max_qty/wizard/sale_max_qty_chooser.py +++ b/nfu_sale_product_min_max_qty/wizard/sale_max_qty_chooser.py @@ -8,7 +8,7 @@ class SaleMaxQtyChooser(models.TransientModel): _description = "Sale Max Quantity Chhooser" # fill from init or compute from given sale order lines - # sale_order_ids = fields.Many2many("sale.order") + sale_order_ids = fields.Many2many("sale.order") sale_max_qty_ids = fields.One2many("sale.max.qty.line", "sale_max_qty_chooser") def update_and_confirm_sale_order(self): diff --git a/nfu_sale_product_min_max_qty/wizard/sale_max_qty_chooser_views.xml b/nfu_sale_product_min_max_qty/wizard/sale_max_qty_chooser_views.xml index 89da727..ad902a5 100644 --- a/nfu_sale_product_min_max_qty/wizard/sale_max_qty_chooser_views.xml +++ b/nfu_sale_product_min_max_qty/wizard/sale_max_qty_chooser_views.xml @@ -8,11 +8,15 @@

Headline

+
- + + + + diff --git a/nfu_sale_product_min_max_qty/wizard/sale_max_qty_line.py b/nfu_sale_product_min_max_qty/wizard/sale_max_qty_line.py index 8e64311..b660e37 100644 --- a/nfu_sale_product_min_max_qty/wizard/sale_max_qty_line.py +++ b/nfu_sale_product_min_max_qty/wizard/sale_max_qty_line.py @@ -1,4 +1,4 @@ -from odoo import fields, models +from odoo import fields, models, api class SaleMaxQtyLine(models.TransientModel): @@ -11,6 +11,24 @@ class SaleMaxQtyLine(models.TransientModel): sale_max_qty_chooser = fields.Many2one("sale.max.qty.chooser") qty = fields.Float() + max_qty = fields.Float(related="sale_line_id.product_uom_max_qty") + packaging_size = fields.Float(compute="_compute_packaging_size") + quantity_fits_packaging = fields.Boolean(compute='_compute_quantity_fits_packaging') + sale_line_id = fields.Many2one("sale.order.line") product_id = fields.Many2one(related="sale_line_id.product_id") reset_to_draft = fields.Boolean() + + def _compute_packaging_size(self): + for sale_order in self: + sale_order.packaging_size = min(sale_order.sale_line_id.product_id.packaging_ids.mapped('qty'), default=1.0) + + @api.depends('packaging_size', 'qty') + def _compute_quantity_fits_packaging(self): + for sale_order in self: + order_lines = self.env['sale.max.qty.line'].search([ + ('product_id', '=', sale_order.product_id.id), + ('sale_max_qty_chooser', '=', sale_order.sale_max_qty_chooser.id), + ]) + total_quantity = sum(order_lines.mapped('qty')) + sale_order.quantity_fits_packaging = total_quantity % sale_order.packaging_size == 0 \ No newline at end of file