diff --git a/stock_forecast_report/README.rst b/stock_forecast_report/README.rst new file mode 100644 index 0000000..7b670bc --- /dev/null +++ b/stock_forecast_report/README.rst @@ -0,0 +1,89 @@ +===================== +Stock Forecast Report +===================== + +.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png + :target: https://odoo-community.org/page/development-status + :alt: Beta +.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png + :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html + :alt: License: AGPL-3 +.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fstock--logistics--reporting-lightgray.png?logo=github + :target: https://github.com/OCA/stock-logistics-reporting/tree/12.0/stock_forecast_report + :alt: OCA/stock-logistics-reporting +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/stock-logistics-reporting-12-0/stock-logistics-reporting-12-0-stock_forecast_report + :alt: Translate me on Weblate +.. |badge5| image:: https://img.shields.io/badge/runbot-Try%20me-875A7B.png + :target: https://runbot.odoo-community.org/runbot/151/12.0 + :alt: Try me on Runbot + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This module backports the stock forecast report from v14. + +.. image:: https://raw.githubusercontent.com/OCA/stock-logistics-reporting/12.0/stock_forecast_report/static/description/screenshot_stock_forecast.png + +**Table of contents** + +.. contents:: + :local: + +Usage +===== + +To use this module, you need to: + +#. Go to *Inventory > Reports > Forecasted Inventory* + +Known issues / Roadmap +====================== + +* This is a backport from the 14.0 Odoo core forecast report. + +Bug Tracker +=========== + +Bugs are tracked on `GitHub Issues `_. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us smashing it by providing a detailed and welcomed +`feedback `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +~~~~~~~ + +* Tecnativa + +Contributors +~~~~~~~~~~~~ + +* `Tecnativa `_: + + * David Vidal + +Maintainers +~~~~~~~~~~~ + +This module is maintained by the OCA. + +.. image:: https://odoo-community.org/logo.png + :alt: Odoo Community Association + :target: https://odoo-community.org + +OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use. + +This module is part of the `OCA/stock-logistics-reporting `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/stock_forecast_report/__init__.py b/stock_forecast_report/__init__.py new file mode 100644 index 0000000..6532353 --- /dev/null +++ b/stock_forecast_report/__init__.py @@ -0,0 +1 @@ +from . import reports diff --git a/stock_forecast_report/__manifest__.py b/stock_forecast_report/__manifest__.py new file mode 100644 index 0000000..38c5691 --- /dev/null +++ b/stock_forecast_report/__manifest__.py @@ -0,0 +1,19 @@ +# Copyright 2021 Odoo S.A. +# Copyright 2021 Tecnativa - David Vidal +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +{ + "name": "Stock Forecast Report", + "summary": "Backport of core 14.0 Forecast Report", + "version": "12.0.1.0.0", + "category": "Inventory", + "website": "https://github.com/OCA/stock-logistics-reporting", + "author": "Tecnativa, Odoo Community Association (OCA)", + "license": "AGPL-3", + "application": False, + "installable": True, + "depends": ["stock"], + "data": [ + "reports/report_stock_quantity.xml", + "security/ir.model.access.csv", + ], +} diff --git a/stock_forecast_report/readme/CONTRIBUTORS.rst b/stock_forecast_report/readme/CONTRIBUTORS.rst new file mode 100644 index 0000000..94b6ba9 --- /dev/null +++ b/stock_forecast_report/readme/CONTRIBUTORS.rst @@ -0,0 +1,3 @@ +* `Tecnativa `_: + + * David Vidal diff --git a/stock_forecast_report/readme/DESCRIPTION.rst b/stock_forecast_report/readme/DESCRIPTION.rst new file mode 100644 index 0000000..62afaea --- /dev/null +++ b/stock_forecast_report/readme/DESCRIPTION.rst @@ -0,0 +1,3 @@ +This module backports the stock forecast report from v14. + +.. image:: static/description/screenshot_stock_forecast.png diff --git a/stock_forecast_report/readme/ROADMAP.rst b/stock_forecast_report/readme/ROADMAP.rst new file mode 100644 index 0000000..4ba7a44 --- /dev/null +++ b/stock_forecast_report/readme/ROADMAP.rst @@ -0,0 +1 @@ +This won't be needed on 14.0+. diff --git a/stock_forecast_report/readme/USAGE.rst b/stock_forecast_report/readme/USAGE.rst new file mode 100644 index 0000000..908ae85 --- /dev/null +++ b/stock_forecast_report/readme/USAGE.rst @@ -0,0 +1,3 @@ +To use this module, you need to: + +#. Go to *Inventory > Reports > Forecasted Inventory* diff --git a/stock_forecast_report/reports/__init__.py b/stock_forecast_report/reports/__init__.py new file mode 100644 index 0000000..bd8c611 --- /dev/null +++ b/stock_forecast_report/reports/__init__.py @@ -0,0 +1 @@ +from . import report_stock_quantity diff --git a/stock_forecast_report/reports/report_stock_quantity.py b/stock_forecast_report/reports/report_stock_quantity.py new file mode 100644 index 0000000..3d4ae28 --- /dev/null +++ b/stock_forecast_report/reports/report_stock_quantity.py @@ -0,0 +1,175 @@ +# Copyright 2021 Odoo S.A. +# Copyright 2021 Tecnativa - David Vidal +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +from odoo import fields, models, tools + + +class ReportStockQuantity(models.Model): + _name = "report.stock.quantity" + _auto = False + _description = "Stock Quantity Report" + + date = fields.Date(string="Date", readonly=True) + product_tmpl_id = fields.Many2one( + comodel_name="product.template", + related="product_id.product_tmpl_id", + readonly=True, + ) + product_id = fields.Many2one( + comodel_name="product.product", + string="Product", + readonly=True, + ) + state = fields.Selection( + selection=[ + ("forecast", "Forecasted Stock"), + ("in", "Forecasted Receipts"), + ("out", "Forecasted Deliveries"), + ], + string="State", + readonly=True, + ) + product_qty = fields.Float(string="Quantity", readonly=True) + move_ids = fields.One2many(comodel_name="stock.move", readonly=True) + company_id = fields.Many2one("res.company", readonly=True) + warehouse_id = fields.Many2one("stock.warehouse", readonly=True) + + def init(self): + tools.drop_view_if_exists(self._cr, "report_stock_quantity") + query = """ +CREATE or REPLACE VIEW report_stock_quantity AS ( +WITH forecast_qty AS ( + SELECT + m.id, + m.product_id, + CASE + WHEN (whs.id IS NOT NULL AND whd.id IS NULL) + OR ls.usage = 'transit' THEN 'out' + WHEN (whs.id IS NULL AND whd.id IS NOT NULL) + OR ld.usage = 'transit' THEN 'in' + END AS state, + m.date_expected::date AS date, + CASE + WHEN (whs.id IS NOT NULL AND whd.id IS NULL) + OR ls.usage = 'transit' THEN -product_qty + WHEN (whs.id IS NULL AND whd.id IS NOT NULL) + OR ld.usage = 'transit' THEN product_qty + END AS product_qty, + m.company_id, + CASE + WHEN (whs.id IS NOT NULL AND whd.id IS NULL) + OR ls.usage = 'transit' THEN whs.id + WHEN (whs.id IS NULL AND whd.id IS NOT NULL) + OR ld.usage = 'transit' THEN whd.id + END AS warehouse_id + FROM + stock_move m + LEFT JOIN stock_location ls ON (ls.id=m.location_id) + LEFT JOIN stock_location ld ON (ld.id=m.location_dest_id) + LEFT JOIN stock_warehouse whs + ON ls.parent_path like concat('%/', whs.view_location_id, '/%') + LEFT JOIN stock_warehouse whd + ON ld.parent_path like concat('%/', whd.view_location_id, '/%') + LEFT JOIN product_product pp ON pp.id=m.product_id + LEFT JOIN product_template pt ON pt.id=pp.product_tmpl_id + WHERE + pt.type = 'product' AND + product_qty != 0 AND + (whs.id IS NOT NULL OR whd.id IS NOT NULL) AND + (whs.id IS NULL OR whd.id IS NULL OR whs.id != whd.id) AND + m.state NOT IN ('cancel', 'draft', 'done') + UNION ALL + SELECT + -q.id as id, + q.product_id, + 'forecast' as state, + date.*::date, + q.quantity as product_qty, + q.company_id, + wh.id as warehouse_id + FROM + GENERATE_SERIES( + (now() at time zone 'utc')::date - interval '3month', + (now() at time zone 'utc')::date + interval '3 month', + '1 day'::interval + ) date, + stock_quant q + LEFT JOIN stock_location l ON (l.id=q.location_id) + LEFT JOIN stock_warehouse wh ON l.parent_path like concat( + '%/', wh.view_location_id, '/%') + WHERE + (l.usage = 'internal' AND wh.id IS NOT NULL) OR + l.usage = 'transit' + UNION ALL + SELECT + m.id, + m.product_id, + 'forecast' as state, + GENERATE_SERIES( + CASE + WHEN m.state = 'done' THEN ( + now() at time zone 'utc' + )::date - interval '3month' + ELSE m.date_expected::date + END, + CASE + WHEN m.state != 'done' THEN ( + now() at time zone 'utc' + )::date + interval '3 month' + ELSE m.date_expected::date - interval '1 day' + END, '1 day'::interval)::date date, + CASE + WHEN ( + (whs.id IS NOT NULL AND whd.id IS NULL) OR ls.usage = 'transit' + ) AND m.state = 'done' THEN product_qty + WHEN ( + (whs.id IS NULL AND whd.id IS NOT NULL) OR ld.usage = 'transit' + ) AND m.state = 'done' THEN -product_qty + WHEN ( + whs.id IS NOT NULL AND whd.id IS NULL + ) OR ls.usage = 'transit' THEN -product_qty + WHEN ( + whs.id IS NULL AND whd.id IS NOT NULL + ) OR ld.usage = 'transit' THEN product_qty + END AS product_qty, + m.company_id, + CASE + WHEN ( + whs.id IS NOT NULL AND whd.id IS NULL + ) OR ls.usage = 'transit' THEN whs.id + WHEN ( + whs.id IS NULL AND whd.id IS NOT NULL + ) OR ld.usage = 'transit' THEN whd.id + END AS warehouse_id + FROM + stock_move m + LEFT JOIN stock_location ls on (ls.id=m.location_id) + LEFT JOIN stock_location ld on (ld.id=m.location_dest_id) + LEFT JOIN stock_warehouse whs ON ls.parent_path like concat( + '%/', whs.view_location_id, '/%' + ) + LEFT JOIN stock_warehouse whd ON ld.parent_path like concat( + '%/', whd.view_location_id, '/%' + ) + LEFT JOIN product_product pp on pp.id=m.product_id + LEFT JOIN product_template pt on pt.id=pp.product_tmpl_id + WHERE + pt.type = 'product' AND + product_qty != 0 AND + (whs.id IS NOT NULL OR whd.id IS NOT NULL) AND + (whs.id IS NULL or whd.id IS NULL OR whs.id != whd.id) AND + m.state NOT IN ('cancel', 'draft') +) -- /forecast_qty +SELECT + MIN(id) as id, + product_id, + state, + date, + sum(product_qty) as product_qty, + company_id, + warehouse_id +FROM forecast_qty +GROUP BY product_id, state, date, company_id, warehouse_id +); +""" + self.env.cr.execute(query) diff --git a/stock_forecast_report/reports/report_stock_quantity.xml b/stock_forecast_report/reports/report_stock_quantity.xml new file mode 100644 index 0000000..18060d3 --- /dev/null +++ b/stock_forecast_report/reports/report_stock_quantity.xml @@ -0,0 +1,104 @@ + + + + stock_report_view_graph + report.stock.quantity + + + + + + + + + + + stock_report_view_pivot + report.stock.quantity + + + + + + + + + + + report.stock.quantity.search + report.stock.quantity + + + + + + + + + + + + + + + + + + + + + + + report.stock.quantity + + { + 'search_default_product_tmpl_id': active_id, + 'search_default_filter_forecast': 1, + 'graph_groupbys': ['date:day', 'product_id'], + 'graph_intervalMapping': {}, + 'graph_mode': 'line', + 'group_by': ['date:day', 'product_id'], + 'graph_measure': 'product_qty', + 'pivot_row_groupby': ['product_id'], + 'pivot_column_groupby': ['date:day'], + 'pivot_measures': ['product_qty'] + } + + + + + report.stock.quantity + + { + 'search_default_product_id': active_id, + 'search_default_filter_forecast': 1, + 'graph_groupbys': ['date:day', 'product_id'], + 'graph_intervalMapping': {}, + 'graph_mode': 'line', + 'group_by': ['date:day', 'product_id'], + 'graph_measure': 'product_qty', + 'pivot_row_groupby': ['product_id'], + 'pivot_column_groupby': ['date:day'], + 'pivot_measures': ['product_qty'] + } + + + + + Forecasted Inventory + report.stock.quantity + graph,pivot + { + 'search_default_filter_forecasted_next_days': 1, + 'graph_groupbys': ['date:day', 'product_id'], + 'graph_mode': 'line', + 'group_by': ['date:day', 'product_id'], + 'graph_measure': 'product_qty', + 'pivot_row_groupby': ['product_id'], + 'pivot_column_groupby': ['date:day'], + 'pivot_measures': ['product_qty'] + } + + + + diff --git a/stock_forecast_report/security/ir.model.access.csv b/stock_forecast_report/security/ir.model.access.csv new file mode 100644 index 0000000..71bec4e --- /dev/null +++ b/stock_forecast_report/security/ir.model.access.csv @@ -0,0 +1,2 @@ +id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink +access_report_stock_quantity,access_report_stock_quantity,model_report_stock_quantity,base.group_user,1,0,0,0 diff --git a/stock_forecast_report/static/description/icon.png b/stock_forecast_report/static/description/icon.png new file mode 100644 index 0000000..2b57236 Binary files /dev/null and b/stock_forecast_report/static/description/icon.png differ diff --git a/stock_forecast_report/static/description/index.html b/stock_forecast_report/static/description/index.html new file mode 100644 index 0000000..a053f00 --- /dev/null +++ b/stock_forecast_report/static/description/index.html @@ -0,0 +1,438 @@ + + + + + + +Stock Forecast Report + + + +
+

Stock Forecast Report

+ + +

Beta License: AGPL-3 OCA/stock-logistics-reporting Translate me on Weblate Try me on Runbot

+

This module backports the stock forecast report from v14.

+https://raw.githubusercontent.com/OCA/stock-logistics-reporting/12.0/stock_forecast_report/static/description/screenshot_stock_forecast.png +

Table of contents

+ +
+

Usage

+

To use this module, you need to:

+
    +
  1. Go to Inventory > Reports > Forecasted Inventory
  2. +
+
+
+

Known issues / Roadmap

+
    +
  • This is a backport from the 14.0 Odoo core forecast report.
  • +
+
+
+

Bug Tracker

+

Bugs are tracked on GitHub Issues. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us smashing it by providing a detailed and welcomed +feedback.

+

Do not contact contributors directly about support or help with technical issues.

+
+
+

Credits

+
+

Authors

+
    +
  • Tecnativa
  • +
+
+
+

Contributors

+ +
+
+

Maintainers

+

This module is maintained by the OCA.

+Odoo Community Association +

OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use.

+

This module is part of the OCA/stock-logistics-reporting project on GitHub.

+

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

+
+
+
+ + diff --git a/stock_forecast_report/static/description/screenshot_stock_forecast.png b/stock_forecast_report/static/description/screenshot_stock_forecast.png new file mode 100644 index 0000000..30a6c52 Binary files /dev/null and b/stock_forecast_report/static/description/screenshot_stock_forecast.png differ diff --git a/stock_forecast_report/tests/__init__.py b/stock_forecast_report/tests/__init__.py new file mode 100644 index 0000000..dbe0c4a --- /dev/null +++ b/stock_forecast_report/tests/__init__.py @@ -0,0 +1 @@ +from . import test_report_stock_quantity diff --git a/stock_forecast_report/tests/test_report_stock_quantity.py b/stock_forecast_report/tests/test_report_stock_quantity.py new file mode 100644 index 0000000..e2be7c1 --- /dev/null +++ b/stock_forecast_report/tests/test_report_stock_quantity.py @@ -0,0 +1,102 @@ +# Copyright 2021 Odoo S.A. +# Copyright 2021 Tecnativa - David Vidal +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +from odoo import fields +from odoo.tests.common import TransactionCase + + +class TestReportStockQuantity(TransactionCase): + def setUp(self): + super().setUp() + self.product1 = self.env["product.product"].create({ + "name": "Mellohi", + "default_code": "C418", + "type": "product", + "categ_id": self.env.ref("product.product_category_all").id, + "tracking": "lot", + "barcode": "scan_me" + }) + wh = self.env["stock.warehouse"].create({ + "name": "Base Warehouse", + "code": "TESTWH" + }) + self.categ_unit = self.env.ref("uom.product_uom_categ_unit") + self.uom_unit = self.env["uom.uom"].search( + [ + ("category_id", "=", self.categ_unit.id), + ("uom_type", "=", "reference") + ], + limit=1 + ) + self.customer_location = self.env.ref("stock.stock_location_customers") + self.supplier_location = self.env.ref("stock.stock_location_suppliers") + # Replenish + self.move1 = self.env["stock.move"].create({ + "name": "test_in_1", + "location_id": self.supplier_location.id, + "location_dest_id": wh.lot_stock_id.id, + "product_id": self.product1.id, + "product_uom": self.uom_unit.id, + "product_uom_qty": 100.0, + "state": "done", + "date": fields.Datetime.now(), + "date_expected": fields.Datetime.now(), + }) + self.quant1 = self.env["stock.quant"].create({ + "product_id": self.product1.id, + "location_id": wh.lot_stock_id.id, + "quantity": 100.0, + }) + self.move2 = self.env["stock.move"].create({ + "name": "test_out_1", + "location_id": wh.lot_stock_id.id, + "location_dest_id": self.customer_location.id, + "product_id": self.product1.id, + "product_uom": self.uom_unit.id, + "product_uom_qty": 120.0, + "state": "partially_available", + "date": fields.Datetime.add(fields.Datetime.now(), days=3), + "date_expected": fields.Datetime.add(fields.Datetime.now(), days=3), + }) + self.move2.date = fields.Datetime.add(fields.Datetime.now(), days=3) + + def test_report_stock_quantity(self): + from_date = fields.Date.to_string( + fields.Date.add(fields.Date.today(), days=-1)) + to_date = fields.Date.to_string( + fields.Date.add(fields.Date.today(), days=4)) + report = self.env["report.stock.quantity"].read_group( + [ + ("date", ">=", from_date), + ("date", "<=", to_date), + ("product_id", "=", self.product1.id) + ], + ["product_qty", "date", "product_id", "state"], + ["date:day", "product_id", "state"], + lazy=False + ) + forecast_report = ( + [x["product_qty"] for x in report if x["state"] == "forecast"] + ) + self.assertEqual(forecast_report, [0, 100, 100, 100, -20, -20]) + + def test_report_stock_quantity_with_product_qty_filter(self): + from_date = fields.Date.to_string( + fields.Date.add(fields.Date.today(), days=-1) + ) + to_date = fields.Date.to_string( + fields.Date.add(fields.Date.today(), days=4) + ) + report = self.env["report.stock.quantity"].read_group( + [ + ("product_qty", "<", 0), ("date", ">=", from_date), + ("date", "<=", to_date), ("product_id", "=", self.product1.id) + ], + ["product_qty", "date", "product_id", "state"], + ["date:day", "product_id", "state"], + lazy=False + ) + forecast_report = ( + [x["product_qty"] for x in report if x["state"] == "forecast"] + ) + self.assertEqual(forecast_report, [-20, -20])