Odoo Mindscape. Understand Odoo faster. Explore modules, fields, dependencies and version differences in one place instead of digging through the source code.

Module: DDMRP

Entity: stock.buffer

Compute Method: _compute_ddmrp_demand_supply_chart

Inputs Method Outputs
for rec in self:
            if not rec.buffer_profile_id:
                # Not a buffer, skip.
                rec.ddmrp_demand_chart = ""
                rec.ddmrp_supply_chart = ""
                continue

            # Prepare data:
            demand_data = rec._get_demand_by_days(rec.demand_stock_move_ids)
            mrp_data = rec._get_qualified_mrp_moves(rec.qualified_demand_mrp_move_ids)
            supply_data = rec._get_incoming_by_days()
            width = timedelta(days=0.4)
            date_format = (
                self.env["res.lang"]._lang_get(self.env.lang or "en_US").date_format
            )

            # Plot demand data:
            if demand_data or mrp_data:
                x_demand = list(convert_datetime_type(x) for x in demand_data.keys())
                y_demand = list(demand_data.values())
                x_mrp = list(convert_datetime_type(x) for x in mrp_data.keys())
                y_mrp = list(mrp_data.values())

                p = figure(
                    frame_width=500,
                    frame_height=400,
                    y_axis_label="Quantity",
                    x_axis_type="datetime",
                )
                p.toolbar.logo = None
                p.sizing_mode = "stretch_both"
                # TODO: # p.xaxis.label_text_font = "helvetica"
                p.xaxis.formatter = DatetimeTickFormatter(
                    hours=date_format,
                    days=date_format,
                    months=date_format,
                    years=date_format,
                )
                p.xaxis.major_label_orientation = pi / 4

                if demand_data:
                    p.vbar(
                        x=x_demand,
                        width=width,
                        bottom=0,
                        top=y_demand,
                        color="firebrick",
                    )
                if mrp_data:
                    p.vbar(
                        x=x_mrp, width=width, bottom=0, top=y_mrp, color="lightsalmon"
                    )
                p.line(
                    [
                        datetime.today() - timedelta(days=1),
                        datetime.today() + timedelta(days=rec.order_spike_horizon),
                    ],
                    [rec.order_spike_threshold, rec.order_spike_threshold],
                    line_width=2,
                    line_dash="dashed",
                )

                unit = rec.product_uom.name
                hover = HoverTool(
                    tooltips=[("qty", f"$y {unit}")], point_policy="follow_mouse"
                )
                p.add_tools(hover)

                script, div = components(p, wrap_script=False)
                json_data = json.dumps(
                    {
                        "div": div,
                        "script": script,
                    }
                )
                rec.ddmrp_demand_chart = json_data
            else:
                rec.ddmrp_demand_chart = json.dumps(
                    {
                        "div": _("No demand detected."),
                        "script": "",
                    }
                )

            # Plot supply data:
            if supply_data:
                x_supply = list(convert_datetime_type(x) for x in supply_data.keys())
                y_supply = list(supply_data.values())

                p = figure(
                    frame_width=500,
                    frame_height=400,
                    y_axis_label="Quantity",
                    x_axis_type="datetime",
                )
                p.toolbar.logo = None
                p.sizing_mode = "stretch_both"
                p.xaxis.formatter = DatetimeTickFormatter(
                    hours=date_format,
                    days=date_format,
                    months=date_format,
                    years=date_format,
                )
                p.xaxis.major_label_orientation = pi / 4

                # White line to have similar proportion to demand chart.
                p.line(
                    [
                        datetime.today() - timedelta(days=1),
                        datetime.today() + timedelta(days=rec.order_spike_horizon),
                    ],
                    [rec.order_spike_threshold, rec.order_spike_threshold],
                    line_width=2,
                    line_dash="dashed",
                    color="white",
                )

                p.vbar(x=x_supply, width=width, bottom=0, top=y_supply, color="grey")

                unit = rec.product_uom.name
                hover = HoverTool(
                    tooltips=[("qty", f"$y {unit}")], point_policy="follow_mouse"
                )
                p.add_tools(hover)

                script, div = components(p, wrap_script=False)
                json_data = json.dumps(
                    {
                        "div": div,
                        "script": script,
                    }
                )
                rec.ddmrp_supply_chart = json_data
            else:
                rec.ddmrp_supply_chart = json.dumps(
                    {
                        "div": _("No supply detected."),
                        "script": "",
                    }
                )

Back to entity