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

Module: DDMRP History

Entity: stock.buffer

Compute Method: _compute_history_chart

Inputs Method Outputs
  • id
  • product_uom
  • product_uom.name
def stacked(df, categories):
            areas = {}
            last = np.zeros(len(df[categories[0]]))
            for cat in categories:
                _next = last + df[cat]
                areas[cat] = np.hstack((last[::-1], _next))
                last = _next
            return areas
hex_colors = self._get_colors_hex_map(pallete="planning")
planning_colors = [
            hex_colors["1_red"],
            hex_colors["2_yellow"],
            hex_colors["3_green"],
        ]
for rec in self:
            history = self.env["ddmrp.history"].search(
                [("buffer_id", "=", rec.id)], order="date"
            )
            if len(history) < 2:
                rec.planning_history_chart = json.dumps(
                    {
                        "div": _("Not enough data available."),
                        "script": "",
                    }
                )
                continue

            N = len(history)
            categories = ["top_of_red", "top_of_yellow", "top_of_green"]
            data = {}

            dates = [r.date for r in history]
            data["date"] = dates
            data[categories[0]] = [r.top_of_red for r in history]
            data[categories[1]] = [r.top_of_yellow - r.top_of_red for r in history]
            data[categories[2]] = [r.top_of_green - r.top_of_yellow for r in history]
            data["net_flow_position"] = [r.net_flow_position for r in history]
            data["on_hand_position"] = [r.on_hand_position for r in history]

            df = pd.DataFrame(data)
            df = df.set_index(["date"])

            areas = stacked(df, categories)

            x2 = np.hstack((data["date"][::-1], data["date"]))

            tops = [
                data[categories[0]][i] + data[categories[1]][i] + data[categories[2]][i]
                for i in range(N)
            ] + [max(data["on_hand_position"]), max(data["net_flow_position"])]
            top_y = max(tops)
            min_y = min(
                [0, min(data["on_hand_position"]), min(data["net_flow_position"])]
            )
            if top_y <= min_y:
                top_y = min_y + 100
            p = figure(
                frame_height=400,
                x_range=(dates[0], dates[-1]),
                y_range=(min_y, top_y),
                x_axis_type="datetime",
            )
            p.sizing_mode = "stretch_width"
            p.toolbar.logo = None

            p.grid.minor_grid_line_color = "#eeeeee"
            p.patches(
                [x2] * len(areas),
                [areas[cat] for cat in categories],
                color=planning_colors,
                alpha=0.8,
                line_color=None,
            )
            date_format = (
                self.env["res.lang"]._lang_get(self.env.lang or "en_US").date_format
            )
            p.xaxis.formatter = DatetimeTickFormatter(
                hours=date_format,
                days=date_format,
                months=date_format,
                years=date_format,
            )
            p.xaxis.major_label_orientation = pi / 4
            p.xaxis.axis_label_text_font = "helvetica"

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

            p.line(dates, data["net_flow_position"], line_width=3)
            p.line(dates, data["on_hand_position"], line_width=3, line_dash="dotted")

            script, div = components(p, wrap_script=False)
            json_data = json.dumps(
                {
                    "div": div,
                    "script": script,
                }
            )
            rec.planning_history_chart = json_data

Back to entity