77 lines
2 KiB
Dart
77 lines
2 KiB
Dart
import 'package:supabase_flutter/supabase_flutter.dart';
|
|
import 'package:uuid/uuid.dart';
|
|
|
|
class Stocks {
|
|
final SupabaseClient _supabase = Supabase.instance.client;
|
|
|
|
Future<List> getList() async {
|
|
List<Map<String, dynamic>> stockData = [];
|
|
|
|
try {
|
|
final data =
|
|
await _supabase.from('stocks').select('ref_medicines(medicine_name), expiration_date, quantity, price');
|
|
|
|
for (var item in data) {
|
|
stockData.add({
|
|
'medicine_name': item['ref_medicines']['medicine_name'],
|
|
'quantity': item['quantity'].toString(),
|
|
'expiration_date': item['expiration_date'],
|
|
'price': item['price'],
|
|
});
|
|
}
|
|
|
|
return stockData;
|
|
} catch (e) {
|
|
return [];
|
|
}
|
|
}
|
|
|
|
Future<String> getUUID(String name) async {
|
|
try {
|
|
final medUUID = await _supabase.from('ref_medicines').select('ref_medicines_uuid').eq('medicine_name', name);
|
|
final data = await _supabase
|
|
.from('stocks')
|
|
.select('stocks_uuid')
|
|
.eq('ref_medicines_uuid', medUUID.first['ref_medicines_uuid']);
|
|
return data.first['stocks_uuid'].toString();
|
|
} catch (e) {
|
|
return '';
|
|
}
|
|
}
|
|
|
|
Future<bool> postStock(String muuid, String name, String quantity) async {
|
|
final uuid = Uuid().v4();
|
|
|
|
final stock = {
|
|
'stock_uuid': uuid,
|
|
'ref_medicines_uuid': muuid,
|
|
'expiration_date': name,
|
|
'quantity': quantity,
|
|
};
|
|
|
|
try {
|
|
await _supabase.from('stocks').insert(stock);
|
|
return true;
|
|
} catch (e) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
Future<String> getQuantity(String uuid) async {
|
|
try {
|
|
final data = await _supabase.from('stocks').select('quantity').eq('stocks_uuid', uuid);
|
|
return data.first['quantity'].toString();
|
|
} catch (e) {
|
|
return '';
|
|
}
|
|
}
|
|
|
|
Future<bool> updateStock(String uuid, String column, String value) async {
|
|
try {
|
|
await _supabase.from('stocks').update({column: value}).eq('stocks_uuid', uuid).select();
|
|
return true;
|
|
} catch (e) {
|
|
return false;
|
|
}
|
|
}
|
|
}
|