<commit_before># ©  2008-2018 <NAME> <<EMAIL>>
#              <NAME> <<EMAIL>(@)<EMAIL>(.)com
# See README.rst file on addons root folder for license details

from dateutil.relativedelta import relativedelta

from odoo import _, api, fields, models


class StorageSheetReport(models.TransientModel):
    _name = "stock.storage.sheet.report"
    _description = "Storage sheet"

    # Filters fields, used for data computation

    location_id = fields.Many2one(
        "stock.location",
        domain="[('usage','=','internal'),('company_id','=',company_id)]",
        required=True,
    )

    date_range_id = fields.Many2one("date.range", string="Date range")
    date_from = fields.Date("Start Date", required=True, default=fields.Date.today)
    date_to = fields.Date("End Date", required=True, default=fields.Date.today)

    product_id = fields.Many2one(
        "product.product",
        string="Product",
        required=True,
        domain=[("type", "=", "product")],
    )

    company_id = fields.Many2one("res.company", string="Company", default=lambda self: self.env.user.company_id)

    line_product_ids = fields.One2many("stock.storage.sheet.report.line", "report_id")

    @api.model
    def default_get(self, fields_list):
        defaults = super(StorageSheetReport, self).default_get(fields_list)

        active_model = self.env.context.get("active_model", False)
        active_ids = self.env.context.get("active_ids", False)
        active_id = self.env.context.get("active_id", False)
        if active_model == "product.template":
            product = self.env["product.product"].search([("product_tmpl_id", "in", active_ids)], limit=1)
            defaults["product_id"] = product.id
        elif active_model == "product.product":
            defaults["product_id"] = active_id

        today = fields.Date.context_today(self)

        from_date = today + relativedelta(day=1, months=0, days=0)
        to_date = today + relativedelta(day=1, months=1, days=-1)

        defaults["date_from"] = from_date
        defaults["date_to"] = to_date
        return defaults

    @api.onchange("date_range_id")
    def onchange_date_range_id(self):
        """Handle date range change."""
        if self.date_range_id:
            self.date_from = self.date_range_id.date_start
            self.date_to = self.date_range_id.date_end

    def do_compute(self):
        self.env["stock.move"].check_access_rights("read")

        lines = self.env["stock.storage.sheet.report.line"].search([("report_id", "=", self.id)])
        lines.unlink()

        to_date = self.date_from

        query = """
    SELECT svl.product_id,  sum(value), sum(svl.quantity), array_agg(svl.id)

        from stock_move as sm
        left join  stock_valuation_layer as svl on svl.stock_move_id = sm.id
        where sm.state = 'done' AND
                sm.product_id = %(product)s AND
              sm.company_id = %(company)s AND
              sm.date <  %(date)s AND
            ( sm.location_id = %(location)s OR
             sm.location_dest_id = %(location)s)
            GROUP BY svl.product_id
        """

        params = {
            "location": self.location_id.id,
            "product": self.product_id.id,
            "company": self.company_id.id,
            "date": to_date,
        }

        self.env.cr.execute(query, params=params)

        res = self.env.cr.fetchall()
        for row in res:
            values = {
                "report_id": self.id,
                "product_id": row[0],
                "amount": row[1],
                "quantity": row[2],
                "type": "balance",<commit_msg>fix raport<commit_after># ©  2008-2018 <NAME> <<EMAIL>>
#              <NAME> <<EMAIL>u(@)gmail(.)com
# See README.rst file on addons root folder for license details

from dateutil.relativedelta import relativedelta

from odoo import _, api, fields, models


class StorageSheetReport(models.TransientModel):
    _name = "stock.storage.sheet.report"
    _description = "Storage sheet"

    # Filters fields, used for data computation

    location_id = fields.Many2one(
        "stock.location",
        domain="[('usage','=','internal'),('company_id','=',company_id)]",
        required=True,
    )

    date_range_id = fields.Many2one("date.range", string="Date range")
    date_from = fields.Date("Start Date", required=True, default=fields.Date.today)
    date_to = fields.Date("End Date", required=True, default=fields.Date.today)

    product_id = fields.Many2one(
        "product.product",
        string="Product",
        required=True,
        domain=[("type", "=", "product")],
    )

    company_id = fields.Many2one("res.company", string="Company", default=lambda self: self.env.user.company_id)

    line_product_ids = fields.One2many("stock.storage.sheet.report.line", "report_id")

    @api.model
    def default_get(self, fields_list):
        defaults = super(StorageSheetReport, self).default_get(fields_list)

        active_model = self.env.context.get("active_model", False)
        active_ids = self.env.context.get("active_ids", False)
        active_id = self.env.context.get("active_id", False)
        if active_model == "product.template":
            product = self.env["product.product"].search([("product_tmpl_id", "in", active_ids)], limit=1)
            defaults["product_id"] = product.id
        elif active_model == "product.product":
            defaults["product_id"] = active_id

        today = fields.Date.context_today(self)

        from_date = today + relativedelta(day=1, months=0, days=0)
        to_date = today + relativedelta(day=1, months=1, days=-1)

        defaults["date_from"] = from_date
        defaults["date_to"] = to_date
        return defaults

    @api.onchange("date_range_id")
    def onchange_date_range_id(self):
        """Handle date range change."""
        if self.date_range_id:
            self.date_from = self.date_range_id.date_start
            self.date_to = self.date_range_id.date_end

    def do_compute(self):
        self.env["stock.move"].check_access_rights("read")

        lines = self.env["stock.storage.sheet.report.line"].search([("report_id", "=", self.id)])
        lines.unlink()

        to_date = self.date_from

        query = """
    SELECT svl.product_id,  sum(svl.value), sum(svl.quantity), array_agg(svl.id)

        from stock_move as sm
        left join  stock_valuation_layer as svl on svl.stock_move_id = sm.id
        where sm.state = 'done' AND
                sm.product_id = %(product)s AND
              sm.company_id = %(company)s AND
              sm.date <  %(date)s AND
            ( sm.location_id = %(location)s OR
             sm.location_dest_id = %(location)s)
            GROUP BY svl.product_id
        """

        params = {
            "location": self.location_id.id,
            "product": self.product_id.id,
            "company": self.company_id.id,
            "date": to_date,
        }

        self.env.cr.execute(query, params=params)

        res = self.env.cr.fetchall()
        for row in res:
            values = {
                "report_id": self.id,
                "product_id": row[0],
                "amount": row[1],
                "quantity": row[2],
                "type": "balance",
{'fraction_non_alphanumeric': 0.13493810178817056, 'fraction_numerical': 0.006327372764786795, 'mean_word_length': 2.5040963855421685, 'pattern_counts': {'":': 22, '<': 18, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1786050', 'n_tokens_mistral': 2321, 'n_tokens_neox': 2229, 'n_words': 426}