[14.0] [MIG] stock_location_empty: Migrate to version 14.0

This commit is contained in:
sonhd
2021-12-21 10:25:36 +07:00
parent 7a5c189798
commit a8ceac5ce7
10 changed files with 588 additions and 26 deletions

View File

@@ -1,45 +1,45 @@
# Copyright 2018 Camptocamp SA
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from psycopg2 import sql
from odoo import api, fields, models
from odoo import fields, models
class StockLocation(models.Model):
_inherit = "stock.location"
stock_amount = fields.Integer(
stock_amount = fields.Float(
compute="_compute_location_amount", search="_search_location_amount"
)
@api.multi
def _search_location_amount(self, operator, value):
if operator not in ("=", "!=", "<", "<=", ">", ">="):
return []
# pylint: disable=sql-injection
self.env.cr.execute(
query = sql.SQL(
"""
SELECT loc.id FROM stock_location loc
LEFT OUTER JOIN stock_quant quant ON loc.id = quant.location_id
GROUP BY loc.id
HAVING coalesce(sum(quantity), 0) %s %%s;"""
% operator,
(value,),
HAVING coalesce(sum(quantity), 0) {operator} %(value)s;""".format(
operator=operator
)
)
ids = [row[0] for row in self.env.cr.fetchall()]
self.env.cr.execute(query, {"value": value})
res = self.env.cr.fetchall()
ids = [row[0] for row in res]
return [("id", "in", ids)]
@api.multi
def _compute_location_amount(self):
self.env.cr.execute(
query = sql.SQL(
"""
SELECT location_id, sum(quantity)
FROM stock_quant
WHERE location_id IN %s
WHERE location_id IN %(values)s
GROUP BY location_id;
""",
(tuple(self.ids),),
"""
)
self.env.cr.execute(query, {"values": tuple(self.ids)})
totals = dict(self.env.cr.fetchall())
for location in self:
location.stock_amount = totals.get(location.id, 0)