112 lines
3.2 KiB
Dart
112 lines
3.2 KiB
Dart
import 'dart:async';
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
import '../../../core/network/dio_client.dart';
|
|
|
|
class UnitOfMeasure {
|
|
final int? id;
|
|
final int? userId;
|
|
final String name;
|
|
final String? abbreviation;
|
|
|
|
UnitOfMeasure({
|
|
this.id,
|
|
this.userId,
|
|
required this.name,
|
|
this.abbreviation,
|
|
});
|
|
|
|
factory UnitOfMeasure.fromJson(Map<String, dynamic> json) {
|
|
return UnitOfMeasure(
|
|
id: json['id'],
|
|
userId: json['userId'] ?? json['user_id'],
|
|
name: json['name'] ?? '',
|
|
abbreviation: json['abbreviation'],
|
|
);
|
|
}
|
|
|
|
Map<String, dynamic> toJson() {
|
|
return {
|
|
'id': id,
|
|
'userId': userId,
|
|
'name': name,
|
|
'abbreviation': abbreviation,
|
|
};
|
|
}
|
|
|
|
String get displayName => abbreviation != null && abbreviation!.isNotEmpty ? '$name ($abbreviation)' : name;
|
|
}
|
|
|
|
class UomsNotifier extends AsyncNotifier<List<UnitOfMeasure>> {
|
|
@override
|
|
FutureOr<List<UnitOfMeasure>> build() async {
|
|
return _fetchUoms();
|
|
}
|
|
|
|
Future<List<UnitOfMeasure>> _fetchUoms() async {
|
|
try {
|
|
final response = await DioClient().dio.get('/inventory/uom');
|
|
if (response.statusCode == 200) {
|
|
final List<dynamic> data = response.data;
|
|
final list = data.map((e) => UnitOfMeasure.fromJson(e)).toList();
|
|
if (list.isNotEmpty) {
|
|
return list;
|
|
}
|
|
}
|
|
} catch (_) {}
|
|
|
|
// If empty in database, seed standard units of measure
|
|
try {
|
|
final defaults = [
|
|
{'name': 'Pieces', 'abbreviation': 'pcs'},
|
|
{'name': 'Units', 'abbreviation': 'unit'},
|
|
{'name': 'Boxes', 'abbreviation': 'box'},
|
|
{'name': 'Sets', 'abbreviation': 'set'},
|
|
{'name': 'Pairs', 'abbreviation': 'pair'},
|
|
{'name': 'Packs', 'abbreviation': 'pk'},
|
|
{'name': 'Grams', 'abbreviation': 'g'},
|
|
{'name': 'Kilograms', 'abbreviation': 'kg'},
|
|
{'name': 'Milligrams', 'abbreviation': 'mg'},
|
|
{'name': 'Carats', 'abbreviation': 'ct'},
|
|
{'name': 'Meters', 'abbreviation': 'm'},
|
|
{'name': 'Liters', 'abbreviation': 'l'},
|
|
{'name': 'Milliliters', 'abbreviation': 'ml'},
|
|
{'name': 'Dozens', 'abbreviation': 'doz'},
|
|
];
|
|
for (final def in defaults) {
|
|
await DioClient().dio.post('/inventory/uom', data: def);
|
|
}
|
|
final response = await DioClient().dio.get('/inventory/uom');
|
|
if (response.statusCode == 200) {
|
|
final List<dynamic> data = response.data;
|
|
return data.map((e) => UnitOfMeasure.fromJson(e)).toList();
|
|
}
|
|
} catch (_) {}
|
|
|
|
return [];
|
|
}
|
|
|
|
Future<void> refresh() async {
|
|
state = const AsyncValue.loading();
|
|
state = await AsyncValue.guard(() => _fetchUoms());
|
|
}
|
|
|
|
Future<UnitOfMeasure?> createUom(String name, String abbreviation) async {
|
|
try {
|
|
final response = await DioClient().dio.post(
|
|
'/inventory/uom',
|
|
data: {'name': name, 'abbreviation': abbreviation},
|
|
);
|
|
if (response.statusCode == 200 || response.statusCode == 201) {
|
|
final newUom = UnitOfMeasure.fromJson(response.data);
|
|
await refresh();
|
|
return newUom;
|
|
}
|
|
} catch (_) {}
|
|
return null;
|
|
}
|
|
}
|
|
|
|
final uomsProvider = AsyncNotifierProvider<UomsNotifier, List<UnitOfMeasure>>(() {
|
|
return UomsNotifier();
|
|
});
|