From 5b172758c824a447256b1e0f0e34a4903a159e91 Mon Sep 17 00:00:00 2001 From: Guewen Baconnier Date: Tue, 8 Oct 2019 17:45:13 +0200 Subject: [PATCH] Add methods to get the center position of a cell --- stock_location_tray/models/stock_location.py | 17 +++++++++ stock_location_tray/tests/test_location.py | 36 ++++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/stock_location_tray/models/stock_location.py b/stock_location_tray/models/stock_location.py index 9677b0779..601034aa8 100644 --- a/stock_location_tray/models/stock_location.py +++ b/stock_location_tray/models/stock_location.py @@ -153,6 +153,23 @@ class StockLocation(models.Model): ) ) + def tray_cell_center_position(self): + """Return the center position in mm of a cell + + The returned position is a tuple with the number of millimeters + from the bottom-left corner. Tuple: (left, bottom) + """ + if not self.cell_in_tray_type_id: + return 0, 0 + posx = self.posx + posy = self.posy + cell_width = self.cell_in_tray_type_id.width_per_cell + cell_depth = self.cell_in_tray_type_id.depth_per_cell + # posx and posy start at one, we want to count from 0 + from_left = (posx - 1) * cell_width + (cell_width / 2) + from_bottom = (posy - 1) * cell_depth + (cell_depth / 2) + return from_left, from_bottom + def _tray_cell_coords(self): if not self.cell_in_tray_type_id: return [] diff --git a/stock_location_tray/tests/test_location.py b/stock_location_tray/tests/test_location.py index 29272cfd3..1f0981255 100644 --- a/stock_location_tray/tests/test_location.py +++ b/stock_location_tray/tests/test_location.py @@ -168,3 +168,39 @@ class TestLocation(LocationTrayTypeCase): message = "cannot be modified when they contain products" with self.assertRaisesRegex(exceptions.UserError, message): self.tray_location.tray_type_id = tray_type + + def test_location_center_pos(self): + cell = self.env.ref( + "stock_location_tray.stock_location_tray_demo_x3y2" + ) + tray_type = cell.cell_in_tray_type_id + number_of_x = 4 + number_of_y = 2 + self.assertEqual( + (number_of_x, number_of_y), (tray_type.cols, tray_type.rows) + ) + + total_width = 80 + total_depth = 30 + tray_type.width = total_width + tray_type.depth = total_depth + + self.assertEqual( + (total_width / number_of_x, total_depth / number_of_y), + (tray_type.width_per_cell, tray_type.depth_per_cell), + ) + from_left, from_bottom = cell.tray_cell_center_position() + # fmt: off + expected_left = ( + (total_width / number_of_x) # width of a cell + * 2 # we want the center of the cell x3, so we want 2 full cells + + ((total_width / number_of_x) / 2) # + the half of our cell + ) + expected_bottom = ( + (total_depth / number_of_y) # depth of a cell + * 1 # we want the center of the cell y2, so we want 1 full cells + + ((total_depth / number_of_y) / 2) # + the half of our cell + ) + # fmt: on + self.assertEqual(from_left, expected_left) + self.assertEqual(from_bottom, expected_bottom)