diff --git a/lib/modules/master_data/data/datasources/master_crud_remote_data_source.dart b/lib/modules/master_data/data/datasources/master_crud_remote_data_source.dart index e11d3e7..c12bd7c 100644 --- a/lib/modules/master_data/data/datasources/master_crud_remote_data_source.dart +++ b/lib/modules/master_data/data/datasources/master_crud_remote_data_source.dart @@ -62,7 +62,10 @@ class MasterCrudRemoteDataSource { final items = list .whereType>() - .map((item) => Map.from(item)) + .map((item) => _sanitizeLocationRow( + definition, + Map.from(item), + )) .toList(); final pagination = parsePagination( @@ -104,7 +107,12 @@ class MasterCrudRemoteDataSource { return list .whereType() - .map((item) => Map.from(item)) + .map( + (item) => _sanitizeLocationRow( + definition, + Map.from(item), + ), + ) .where(isActiveOptionRow) .toList(); } @@ -114,7 +122,7 @@ class MasterCrudRemoteDataSource { String id, ) async { final response = await dio.get('${definition.apiPath}/$id'); - return _extractData(response.data); + return _sanitizeLocationRow(definition, _extractData(response.data)); } Future> create( @@ -125,7 +133,7 @@ class MasterCrudRemoteDataSource { definition.apiPath, data: payload..removeWhere((_, value) => value == null), ); - return _extractData(response.data); + return _sanitizeLocationRow(definition, _extractData(response.data)); } Future> update( @@ -137,7 +145,7 @@ class MasterCrudRemoteDataSource { '${definition.apiPath}/$id', data: payload..removeWhere((_, value) => value == null), ); - return _extractData(response.data); + return _sanitizeLocationRow(definition, _extractData(response.data)); } Future delete(MasterDefinition definition, String id) async { @@ -197,4 +205,16 @@ class MasterCrudRemoteDataSource { if (data is Map) return Map.from(data); return Map.from(body); } + + /// Locations API no longer nests warehouses under plants. + Map _sanitizeLocationRow( + MasterDefinition definition, + Map row, + ) { + if (definition.id != 'locations') return row; + row.remove('parent_id'); + row.remove('plant_id'); + row.remove('plant'); + return row; + } } diff --git a/lib/modules/master_data/domain/entities/master_definition.dart b/lib/modules/master_data/domain/entities/master_definition.dart index 873f730..75b4d94 100644 --- a/lib/modules/master_data/domain/entities/master_definition.dart +++ b/lib/modules/master_data/domain/entities/master_definition.dart @@ -34,7 +34,7 @@ class MasterFieldDef { final bool showInForm; /// When true, shown in the form but not editable (value set by other fields). final bool readOnly; - /// Master key used to populate dropdown options (e.g. `plants` for plant_id). + /// Master key used to populate dropdown options (e.g. `locations` for FKs). final String? optionsMasterKey; /// Extra query parameters when loading [optionsMasterKey] options. final Map? optionsQueryParams; @@ -48,7 +48,7 @@ class MasterFieldDef { /// Show this field only when [visibleWhenFieldKey] equals [visibleWhenValue]. final String? visibleWhenFieldKey; final String? visibleWhenValue; - /// Nested object key on list rows for display (e.g. `plant` for `parent_id`). + /// Nested object key on list rows for display (e.g. `gst_rate` for `gst_rate_id`). final String? listNestedKey; /// Cache key for dropdown option rows (includes query params when set). @@ -376,32 +376,51 @@ const masterDefinitions = [ MasterFieldDef(key: 'code', label: 'Code', required: true, showInList: true), MasterFieldDef(key: 'name', label: 'Name', required: true, showInList: true), MasterFieldDef( - key: 'parent_id', - label: 'Parent Plant', - type: MasterFieldType.dropdown, + key: 'gstin', + label: 'GSTIN', showInList: true, - optionsMasterKey: 'locations', - optionsQueryParams: const {'type': 'plant'}, - listNestedKey: 'plant', visibleWhenFieldKey: 'type', - visibleWhenValue: 'warehouse', - required: true, + visibleWhenValue: 'plant', + ), + MasterFieldDef( + key: 'city', + label: 'City', + showInList: true, + visibleWhenFieldKey: 'type', + visibleWhenValue: 'plant', ), - MasterFieldDef(key: 'gstin', label: 'GSTIN', showInList: true), - MasterFieldDef(key: 'city', label: 'City', showInList: true), MasterFieldDef( key: 'state', label: 'State', type: MasterFieldType.dropdown, optionsMasterKey: 'location_states', showInList: true, + visibleWhenFieldKey: 'type', + visibleWhenValue: 'plant', + ), + MasterFieldDef( + key: 'address', + label: 'Address', + multiline: true, + visibleWhenFieldKey: 'type', + visibleWhenValue: 'plant', + ), + MasterFieldDef( + key: 'pincode', + label: 'Pincode', + visibleWhenFieldKey: 'type', + visibleWhenValue: 'plant', + ), + MasterFieldDef( + key: 'phone', + label: 'Phone', + visibleWhenFieldKey: 'type', + visibleWhenValue: 'plant', ), - MasterFieldDef(key: 'address', label: 'Address', multiline: true), - MasterFieldDef(key: 'pincode', label: 'Pincode'), - MasterFieldDef(key: 'phone', label: 'Phone'), MasterFieldDef( key: 'location', label: 'Location Detail', + showInList: true, visibleWhenFieldKey: 'type', visibleWhenValue: 'warehouse', ), diff --git a/lib/modules/master_data/presentation/providers/master_provider.dart b/lib/modules/master_data/presentation/providers/master_provider.dart index fe8c30e..8271ca4 100644 --- a/lib/modules/master_data/presentation/providers/master_provider.dart +++ b/lib/modules/master_data/presentation/providers/master_provider.dart @@ -301,6 +301,8 @@ class MasterFormNotifier extends FamilyAsyncNotifier values) { + if (_definition.id != 'locations') return; + values.remove('parent_id'); + values.remove('plant_id'); + values.remove('plant'); + } + void updateValue(String key, dynamic value) { final current = state.valueOrNull; if (current == null) return; @@ -469,6 +479,11 @@ class MasterFormNotifier extends FamilyAsyncNotifier value.toString().trim(), }; } + + // Never send plant nesting fields for locations (API breaking change). + if (_definition.id == 'locations') { + payload.remove('parent_id'); + payload.remove('plant_id'); + payload.remove('plant'); + } + return payload; } diff --git a/lib/modules/masters/data/datasources/master_remote_data_source.dart b/lib/modules/masters/data/datasources/master_remote_data_source.dart index 376ded7..61cb7a4 100644 --- a/lib/modules/masters/data/datasources/master_remote_data_source.dart +++ b/lib/modules/masters/data/datasources/master_remote_data_source.dart @@ -72,6 +72,11 @@ class MasterRemoteDataSource { for (final item in rows) { if (!isActiveOptionRow(item)) continue; + // Ignore obsolete plant nesting fields if the API still returns them. + item.remove('parent_id'); + item.remove('plant_id'); + item.remove('plant'); + final id = item['id']?.toString() ?? ''; final name = _optionLabel(item); if (id.isEmpty || name.isEmpty) continue;