diff --git a/app/Models/Stock_location.php b/app/Models/Stock_location.php index 0b4f8ea6c..2f2cba333 100644 --- a/app/Models/Stock_location.php +++ b/app/Models/Stock_location.php @@ -260,10 +260,12 @@ class Stock_location extends Model $locationDataToSave = ['location_name' => $locationName, 'deleted' => 0]; if (!$this->exists($locationId)) { + $table = $this->db->prefixTable('stock_locations'); + $this->db->transStart(); - $builder = $this->db->table('stock_locations'); - $locationDataToSave['sort_order'] = $builder->where('deleted', 0)->countAllResults(); + $row = $this->db->query("SELECT COUNT(*) AS count FROM $table WHERE deleted = 0 FOR UPDATE")->getRow(); + $locationDataToSave['sort_order'] = (int) $row->count; $builder = $this->db->table('stock_locations'); $builder->insert($locationDataToSave);