From 233b3f5525eaf4cf791514aca182933ed6042619 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20BEAU?= Date: Wed, 1 Nov 2023 22:54:37 +0100 Subject: [PATCH] wms_connector: fix export, review product sync --- wms_connector/models/stock_warehouse.py | 4 ++-- .../models/synchronize_exportable_mixin.py | 20 +++++++++++------- wms_connector/models/wms_product_sync.py | 21 +++++++++++++++---- wms_connector/security/wms_product_sync.xml | 2 +- wms_connector/views/wms_product_sync.xml | 1 + 5 files changed, 33 insertions(+), 15 deletions(-) diff --git a/wms_connector/models/stock_warehouse.py b/wms_connector/models/stock_warehouse.py index 2c60243edf..7d993af1bd 100644 --- a/wms_connector/models/stock_warehouse.py +++ b/wms_connector/models/stock_warehouse.py @@ -106,7 +106,7 @@ def _wms_domain_for(self, model_domain): domains = { "product": [ ("warehouse_id", "=", self.id), - ("wms_export_date", "=", False), + ("to_export", "=", True), ], "pickings_in": self.wms_export_picking_in_filter_id._get_eval_domain(), "pickings_out": self.wms_export_picking_out_filter_id._get_eval_domain(), @@ -233,7 +233,7 @@ def button_open_wms_sync_ids(self): "res_model": "wms.product.sync", "type": "ir.actions.act_window", "target": "current", - "domain": self._wms_domain_for("product"), + "domain": [("warehouse_id", "=", self.id)], } def button_open_wms_pickings_in(self): diff --git a/wms_connector/models/synchronize_exportable_mixin.py b/wms_connector/models/synchronize_exportable_mixin.py index 0e5fcc526f..2585114d4e 100644 --- a/wms_connector/models/synchronize_exportable_mixin.py +++ b/wms_connector/models/synchronize_exportable_mixin.py @@ -13,9 +13,12 @@ class SynchronizeExportableMixin(models.AbstractModel): _name = "synchronize.exportable.mixin" _description = "Synchronizable export mixin" - wms_export_date = fields.Date() - wms_export_attachment = fields.Many2one("attachment.queue", index=True) - wms_export_error = fields.Char() + + wms_export_date = fields.Datetime(readonly=True) + wms_export_attachment = fields.Many2one( + "attachment.queue", index=True, readonly=True + ) + wms_export_error = fields.Char(readonly=True) @property def record_per_file(self): @@ -29,9 +32,9 @@ def _get_export_data(self): records = self.browse() sequence = 0 for rec in self: - records |= rec try: data += rec._prepare_export_data(sequence) + records |= rec rec.wms_export_error = "" sequence += 1 except Exception as e: @@ -49,8 +52,9 @@ def _get_export_data(self): def synchronize_export(self): for records, data in self._get_export_data(): - attachments = records._format_to_exportfile(data) - records.track_export(attachments) + vals = self._format_to_exportfile(data) + attachment = self.env["attachment.queue"].create(vals) + records.track_export(attachment) def track_export(self, attachment): self.wms_export_date = datetime.datetime.now() @@ -59,6 +63,7 @@ def track_export(self, attachment): def _prepare_export_data(self, idx) -> list: raise NotImplementedError + # TODO cleanup this code def _format_to_exportfile(self, name, data): return self._format_to_exportfile_csv(name, data) @@ -72,13 +77,12 @@ def _format_to_exportfile_csv(self, data): writer.writerow(row) csv_file.seek(0) ast = self.env.context.get("attachment_task") - vals = { + return { "name": self._get_export_name(), "datas": base64.b64encode(csv_file.getvalue().encode("utf-8")), "task_id": ast.id, "file_type": ast.file_type, } - return self.env["attachment.queue"].create(vals) def _get_export_name(self): raise NotImplementedError diff --git a/wms_connector/models/wms_product_sync.py b/wms_connector/models/wms_product_sync.py index c4b132a5ed..fd7c2a15ee 100644 --- a/wms_connector/models/wms_product_sync.py +++ b/wms_connector/models/wms_product_sync.py @@ -1,7 +1,7 @@ # Copyright 2023 Akretion # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). -from odoo import fields, models +from odoo import api, fields, models class WmsProductSync(models.Model): @@ -9,11 +9,24 @@ class WmsProductSync(models.Model): _name = "wms.product.sync" _description = "Wms Product Sync" - name = fields.Char(related="product_id.name") - product_id = fields.Many2one("product.product", required=True) - warehouse_id = fields.Many2one("stock.warehouse", required=True) + name = fields.Char( + related="product_id.name", + ) + product_id = fields.Many2one("product.product", required=True, readonly=True) + warehouse_id = fields.Many2one("stock.warehouse", required=True, readonly=True) active = fields.Boolean(default=True) + to_export = fields.Boolean(compute="_compute_to_export", store=True, readonly=False) + + @api.depends("product_id.name", "active") + def _compute_to_export(self): + for record in self: + record.to_export = True + def _schedule_export(self, warehouse, domain=False): warehouse.refresh_wms_products() return super()._schedule_export(warehouse, domain) + + def track_export(self, attachment): + super().track_export(attachment) + self.to_export = False diff --git a/wms_connector/security/wms_product_sync.xml b/wms_connector/security/wms_product_sync.xml index 3e4949e6ba..7abd8a0129 100644 --- a/wms_connector/security/wms_product_sync.xml +++ b/wms_connector/security/wms_product_sync.xml @@ -10,7 +10,7 @@ - + diff --git a/wms_connector/views/wms_product_sync.xml b/wms_connector/views/wms_product_sync.xml index 7f36236c4a..894db5761d 100644 --- a/wms_connector/views/wms_product_sync.xml +++ b/wms_connector/views/wms_product_sync.xml @@ -38,6 +38,7 @@ +