diff --git a/wms_connector/models/stock_warehouse.py b/wms_connector/models/stock_warehouse.py index a085088448a..eee11290ee8 100644 --- a/wms_connector/models/stock_warehouse.py +++ b/wms_connector/models/stock_warehouse.py @@ -143,12 +143,20 @@ def action_open_flows(self): def refresh_wms_products(self): for rec in self: - rec.wms_product_sync_ids.unlink() - for prd in self.env["product.product"].search( + prd_with_sync = self.wms_product_sync_ids.product_id + prd_matching = self.env["product.product"].search( rec.wms_export_product_filter_id and rec.wms_export_product_filter_id._get_eval_domain() or [] - ): + ) + to_create = prd_matching - prd_with_sync + for prd in to_create: self.env["wms.product.sync"].create( {"product_id": prd.id, "warehouse_id": rec.id} ) + self.env["wms.product.sync"].search( + [ + ("warehouse_id", "=", rec.id), + ("product_id", "in", (prd_with_sync - prd_matching).ids), + ] + ).unlink() diff --git a/wms_connector/tests/test_activate_sync.py b/wms_connector/tests/test_activate_sync.py index 6d918aca097..677c0bf4e52 100644 --- a/wms_connector/tests/test_activate_sync.py +++ b/wms_connector/tests/test_activate_sync.py @@ -43,3 +43,23 @@ def test_wms_product_sync_created_filter(self): 1, len(self.env["wms.product.sync"].search([])), ) + + def test_wms_product_sync_updates(self): + self.warehouse.active_wms_sync = True + match_unlink, match_keep, nomatch_match = self.env["product.product"].search( + [], limit=3 + ) + self.warehouse.wms_export_product_filter_id.domain = ( + '[("id", "in", {})]'.format((match_unlink + match_keep).ids) + ) + self.warehouse.refresh_wms_products() + self.assertEqual( + self.warehouse.wms_product_sync_ids.product_id, (match_unlink + match_keep) + ) + self.warehouse.wms_export_product_filter_id.domain = ( + '[("id", "in", {})]'.format((match_keep + nomatch_match).ids) + ) + self.warehouse.refresh_wms_products() + self.assertEqual( + self.warehouse.wms_product_sync_ids.product_id, match_keep + nomatch_match + )