Files
opensourcepos/app/Database/Migrations/20261023000000_AddStockLocationDefaultAndSortOrder.php
T
objecttothisandClaude Sonnet 4.6 74e825ba6f feat(stock-locations): add default location and sort order support (#4643)
Refactor Stock_location model to camelCase naming and introduce
is_default and sort_order fields with full migration, UI, and
item-sync support. Multiple follow-up fixes consolidated.

Database
- Add is_default (TINYINT, backfilled to oldest non-deleted row) and
  sort_order (nullable unsigned TINYINT unique, dense 0..N-1) columns
  via new migration; MySQL 5.7-compatible backfill using user variables
  instead of ROW_NUMBER(); down() drops both columns

Model
- Rename show_locations → showLocations (camelCase convention)
- Add getDefaultLocationId(), setDefaultLocation(), saveSortOrder();
  validate location existence before setting as default
- delete() compacts sort_order via two-pass CASE update to avoid
  collisions after prior reorders
- Atomic sort_order assignment via explicit SQL COUNT query

Controller / Config
- Modularize save logic into sanitizeSubmittedLocations(),
  deleteRemovedLocations(), saveSubmittedLocations(),
  saveLocationOrderAndDefault(); wrap in single transaction
- Support stock_location_new inputs for new-row creation
- Resolve new-* ID placeholders to real IDs post-save
- Abort loop on save failure; default missing POST arrays to []
- Validate that at least one location array is non-empty

Views
- Add Default column with center-aligned radio buttons; disable for
  unsaved rows; use attr() instead of data() for location IDs
- Escape dynamic location IDs against XSS in all affected partials
- form_count_details.php: fall back to default location when current
  is undefined; guard quantity input similarly
- Add aria-label to default radio inputs

Libraries / Items
- Widen Item_lib::get_item_location() and get_stock_destination() to
  nullable return types
- Guard null location_id in Items::getRow() and getGenerateBarcodes()
- Item::get_multiple_info(): accept ?int $location_id; short-circuit
  with where('item_id', -1) instead of always-false where('1','0')

i18n
- Add stock_location_default, stock_location_default_header,
  stock_location_name_header across all locale files

Tests
- Stock_locationTest: getDefaultLocationId, setDefaultLocation,
  saveSortOrder, delete compaction, saveValue; rename to
  testMethod_Scenario convention
- Add ConfigControllerTest / StockLocationsControllerTest with
  AdminAuthTrait covering CRUD, reorder, default assignment, and
  malformed/empty/blank payload validation
- DatabaseBootstrapTrait-based setup for ItemTest and Stock_locationTest

Signed-off-by: objecttothis <17935339+objecttothis@users.noreply.github.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-10-27 14:36:34 +04:00

69 lines
2.3 KiB
PHP

<?php
namespace App\Database\Migrations;
use CodeIgniter\Database\Migration;
class AddStockLocationDefaultAndSortOrder extends Migration
{
/**
* Perform a migration step.
*/
public function up(): void
{
$table = $this->db->prefixTable('stock_locations');
$this->forge->addColumn('stock_locations', [
'is_default' => [
'type' => 'TINYINT',
'constraint' => 1,
'null' => false,
'default' => 0,
'after' => 'deleted',
],
'sort_order' => [
'type' => 'TINYINT',
'unsigned' => true,
'null' => true,
'default' => null,
'after' => 'is_default',
],
]);
// Backfill sort_order for non-deleted rows only, ranking alphabetically so upgraded
// installs see no visible reorder. Deleted rows get NULL: they don't occupy a sort
// position, so the active range always stays a dense 0..N-1 regardless of how many
// locations have been deleted over the life of the install.
$this->db->query('SET @rank := -1');
$this->db->query("
UPDATE $table AS destination
INNER JOIN (
SELECT
location_id,
(@rank := @rank + 1) AS new_sort_order
FROM $table
WHERE deleted = 0
ORDER BY location_name ASC, location_id ASC
) AS ranked ON ranked.location_id = destination.location_id
SET destination.sort_order = ranked.new_sort_order
");
$this->db->query("UPDATE $table SET sort_order = NULL WHERE deleted = 1");
// Default to the oldest (lowest location_id) non-deleted location so behavior is unchanged until an admin picks one explicitly.
$this->db->query("UPDATE $table SET is_default = 1 WHERE deleted = 0 ORDER BY location_id ASC LIMIT 1");
$this->forge->addUniqueKey('sort_order');
$this->forge->processIndexes('stock_locations');
}
/**
* Revert a migration step.
*/
public function down(): void
{
$this->forge->dropColumn('stock_locations', ['is_default', 'sort_order']);
}
}