From e8d0f213e903f0e39614b986f2e84c418634f8e8 Mon Sep 17 00:00:00 2001 From: Patrick Alvin Alcala Date: Mon, 7 Apr 2025 17:23:57 +0800 Subject: [PATCH] fix saving and display of stocks --- lib/pages/add_medicine_page.dart | 14 +- .../customer_pages/customer_search_page.dart | 323 ++++++++++-------- lib/tables/prescriptions.dart | 27 ++ lib/tables/ref_medicines.dart | 2 +- lib/tables/stocks.dart | 27 ++ 5 files changed, 255 insertions(+), 138 deletions(-) create mode 100644 lib/tables/prescriptions.dart diff --git a/lib/pages/add_medicine_page.dart b/lib/pages/add_medicine_page.dart index 7f41481..6955158 100644 --- a/lib/pages/add_medicine_page.dart +++ b/lib/pages/add_medicine_page.dart @@ -190,8 +190,20 @@ class _AddMedicinePageState extends State { final medTypeUUID = await _refTypes.getUUID(_selectedType); final medManufacturerUUID = await _refManufacturer.getUUID(_selectedManufacturer); - await _refMedicines.postMedicine( + final posted = await _refMedicines.postMedicine( uuid, medName, medManufacturerUUID, medGenericUUID, medTypeUUID, encrpytedBarcode); + + if (posted) { + if (mounted) { + showNotification(context, 'Medicine Added Successfully', true); + setState(() => _isLoading = false); + context.pop(); + } + } else { + if (mounted) { + showNotification(context, 'Error: Medicine Not Added', false); + } + } } else { if (mounted) { showNotification(context, 'Error: No Internet Connection', false); diff --git a/lib/pages/customer_pages/customer_search_page.dart b/lib/pages/customer_pages/customer_search_page.dart index 93bb18f..3d8ac4a 100644 --- a/lib/pages/customer_pages/customer_search_page.dart +++ b/lib/pages/customer_pages/customer_search_page.dart @@ -28,17 +28,20 @@ class _CustomerSearchPageState extends State { late String imageUrl = ''; late List _stockList = []; late List _cartList = []; - // late final List _medicinesList = []; - Future _getURL() async { - final image = await _storage.getImageURL('ref_medicines_images', '91ba5229-244b-4377-ba4e-9de57b9ba4df.webp'); - setState(() { - imageUrl = image; - }); + Future _getURL(String imageName) async { + final image = await _storage.getImageURL('ref_medicines_images', '$imageName.webp'); + return image; } Future _getAllStocks() async { - _stockList = await _stocks.getList(); + _stockList = await _stocks.getListWithUUID(); + if (_stockList.isNotEmpty) { + for (int i = 0; i < _stockList.length; i++) { + _stockList[i]['uuid'] = await _getURL(_stockList[i]['uuid']); + } + setState(() {}); + } } Future _getCurrentUserId() async { @@ -79,7 +82,7 @@ class _CustomerSearchPageState extends State { await _getAllStocks(); await _getCarts(); // // // await _getAllMedicines(); - await _getURL(); + // await _getURL(); } @override @@ -100,7 +103,7 @@ class _CustomerSearchPageState extends State { Widget build(BuildContext context) { return Scaffold( body: CustomerPagebackgroundWidget( - height: MediaQuery.of(context).size.height + 800, + height: MediaQuery.of(context).size.height + 200, child: Column( children: [ Column( @@ -121,13 +124,13 @@ class _CustomerSearchPageState extends State { ), ), const Gap(32), - const IndicatorWidget(text: 'All Stocks'), + const IndicatorWidget(text: 'Prescribed Medicines'), const Gap(8), SingleChildScrollView( scrollDirection: Axis.horizontal, child: Row( children: [ - if (imageUrl.isEmpty) + if (_stockList.isEmpty) const Row( children: [ ItemCardWidget( @@ -159,7 +162,7 @@ class _CustomerSearchPageState extends State { else for (var item in _stockList) ItemCardWidget( - imageUrl: imageUrl, + imageUrl: item['uuid'], text: item['medicine_name'].toString(), subtext: item['generic_name'].toString(), price: item['price'].toDouble(), @@ -169,137 +172,185 @@ class _CustomerSearchPageState extends State { ], )), const Gap(16), - const IndicatorWidget(text: 'For Fever'), + const IndicatorWidget(text: 'All Stocks'), const Gap(8), SingleChildScrollView( scrollDirection: Axis.horizontal, child: Row( children: [ - ItemCardWidget( - imageUrl: imageUrl, - text: 'sample', - subtext: 'generic_name', - price: 500, - quantity: 0, - isLoading: imageUrl.isEmpty, - cart: 2), - ItemCardWidget( - imageUrl: imageUrl, - text: 'sample', - subtext: 'generic_name', - price: 500, - quantity: 15, - isLoading: imageUrl.isEmpty, - cart: 0), - ItemCardWidget( - imageUrl: imageUrl, - text: 'sample', - subtext: 'generic_name', - price: 500, - quantity: 15, - isLoading: imageUrl.isEmpty, - cart: 0), - ], - )), - const Gap(16), - const IndicatorWidget(text: 'For Headaches'), - const Gap(8), - SingleChildScrollView( - scrollDirection: Axis.horizontal, - child: Row( - children: [ - ItemCardWidget( - imageUrl: imageUrl, - text: 'sample', - subtext: 'generic_name', - price: 500, - quantity: 15, - isLoading: imageUrl.isEmpty, - cart: 0), - ItemCardWidget( - imageUrl: imageUrl, - text: 'sample', - subtext: 'generic_name', - price: 500, - quantity: 15, - isLoading: imageUrl.isEmpty, - cart: 0), - ItemCardWidget( - imageUrl: imageUrl, - text: 'sample', - subtext: 'generic_name', - price: 500, - quantity: 15, - isLoading: imageUrl.isEmpty, - cart: 0), - ], - )), - const Gap(16), - const IndicatorWidget(text: 'For Colds and Coughes'), - const Gap(8), - SingleChildScrollView( - scrollDirection: Axis.horizontal, - child: Row( - children: [ - ItemCardWidget( - imageUrl: imageUrl, - text: 'sample', - subtext: 'generic_name', - price: 500, - quantity: 15, - isLoading: imageUrl.isEmpty, - cart: 0), - ItemCardWidget( - imageUrl: imageUrl, - text: 'sample', - subtext: 'generic_name', - price: 500, - quantity: 15, - isLoading: imageUrl.isEmpty, - cart: 0), - ItemCardWidget( - imageUrl: imageUrl, - text: 'sample', - subtext: 'generic_name', - price: 500, - quantity: 15, - isLoading: imageUrl.isEmpty, - cart: 0), - ], - )), - const Gap(16), - const IndicatorWidget(text: 'For Infection and Irritations'), - const Gap(8), - SingleChildScrollView( - scrollDirection: Axis.horizontal, - child: Row( - children: [ - ItemCardWidget( - imageUrl: imageUrl, - text: 'sample', - subtext: 'generic_name', - price: 500, - quantity: 15, - isLoading: imageUrl.isEmpty, - cart: 0), - ItemCardWidget( - imageUrl: imageUrl, - text: 'sample', - subtext: 'generic_name', - price: 500, - quantity: 15, - isLoading: imageUrl.isEmpty, - cart: 0), - ItemCardWidget( - imageUrl: imageUrl, - text: 'sample', - subtext: 'generic_name', - price: 500, - quantity: 15, - isLoading: imageUrl.isEmpty, - cart: 0), + if (_stockList.isEmpty) + const Row( + children: [ + ItemCardWidget( + imageUrl: 'imageUrl', + text: 'item[medicine_name]', + subtext: 'item[generic_name]', + price: 123, + quantity: 123, + isLoading: true, + cart: 0), + ItemCardWidget( + imageUrl: 'imageUrl', + text: 'item[medicine_name]', + subtext: 'item[generic_name]', + price: 123, + quantity: 123, + isLoading: true, + cart: 0), + ItemCardWidget( + imageUrl: 'imageUrl', + text: 'item[medicine_name]', + subtext: 'item[generic_name]', + price: 123, + quantity: 123, + isLoading: true, + cart: 0) + ], + ) + else + for (var item in _stockList) + ItemCardWidget( + imageUrl: item['uuid'].toString(), + text: item['medicine_name'].toString(), + subtext: item['generic_name'].toString(), + price: item['price'].toDouble(), + quantity: item['quantity'].toDouble(), + isLoading: false, + cart: 0), ], )), + // const Gap(16), + // const IndicatorWidget(text: 'For Fever'), + // const Gap(8), + // SingleChildScrollView( + // scrollDirection: Axis.horizontal, + // child: Row( + // children: [ + // ItemCardWidget( + // imageUrl: imageUrl, + // text: 'sample', + // subtext: 'generic_name', + // price: 500, + // quantity: 0, + // isLoading: imageUrl.isEmpty, + // cart: 2), + // ItemCardWidget( + // imageUrl: imageUrl, + // text: 'sample', + // subtext: 'generic_name', + // price: 500, + // quantity: 15, + // isLoading: imageUrl.isEmpty, + // cart: 0), + // ItemCardWidget( + // imageUrl: imageUrl, + // text: 'sample', + // subtext: 'generic_name', + // price: 500, + // quantity: 15, + // isLoading: imageUrl.isEmpty, + // cart: 0), + // ], + // )), + // const Gap(16), + // const IndicatorWidget(text: 'For Headaches'), + // const Gap(8), + // SingleChildScrollView( + // scrollDirection: Axis.horizontal, + // child: Row( + // children: [ + // ItemCardWidget( + // imageUrl: imageUrl, + // text: 'sample', + // subtext: 'generic_name', + // price: 500, + // quantity: 15, + // isLoading: imageUrl.isEmpty, + // cart: 0), + // ItemCardWidget( + // imageUrl: imageUrl, + // text: 'sample', + // subtext: 'generic_name', + // price: 500, + // quantity: 15, + // isLoading: imageUrl.isEmpty, + // cart: 0), + // ItemCardWidget( + // imageUrl: imageUrl, + // text: 'sample', + // subtext: 'generic_name', + // price: 500, + // quantity: 15, + // isLoading: imageUrl.isEmpty, + // cart: 0), + // ], + // )), + // const Gap(16), + // const IndicatorWidget(text: 'For Colds and Coughes'), + // const Gap(8), + // SingleChildScrollView( + // scrollDirection: Axis.horizontal, + // child: Row( + // children: [ + // ItemCardWidget( + // imageUrl: imageUrl, + // text: 'sample', + // subtext: 'generic_name', + // price: 500, + // quantity: 15, + // isLoading: imageUrl.isEmpty, + // cart: 0), + // ItemCardWidget( + // imageUrl: imageUrl, + // text: 'sample', + // subtext: 'generic_name', + // price: 500, + // quantity: 15, + // isLoading: imageUrl.isEmpty, + // cart: 0), + // ItemCardWidget( + // imageUrl: imageUrl, + // text: 'sample', + // subtext: 'generic_name', + // price: 500, + // quantity: 15, + // isLoading: imageUrl.isEmpty, + // cart: 0), + // ], + // )), + // const Gap(16), + // const IndicatorWidget(text: 'For Infection and Irritations'), + // const Gap(8), + // SingleChildScrollView( + // scrollDirection: Axis.horizontal, + // child: Row( + // children: [ + // ItemCardWidget( + // imageUrl: imageUrl, + // text: 'sample', + // subtext: 'generic_name', + // price: 500, + // quantity: 15, + // isLoading: imageUrl.isEmpty, + // cart: 0), + // ItemCardWidget( + // imageUrl: imageUrl, + // text: 'sample', + // subtext: 'generic_name', + // price: 500, + // quantity: 15, + // isLoading: imageUrl.isEmpty, + // cart: 0), + // ItemCardWidget( + // imageUrl: imageUrl, + // text: 'sample', + // subtext: 'generic_name', + // price: 500, + // quantity: 15, + // isLoading: imageUrl.isEmpty, + // cart: 0), + // ], + // )), ], ) ], diff --git a/lib/tables/prescriptions.dart b/lib/tables/prescriptions.dart new file mode 100644 index 0000000..97d585e --- /dev/null +++ b/lib/tables/prescriptions.dart @@ -0,0 +1,27 @@ +import 'package:supabase_flutter/supabase_flutter.dart'; +import 'package:uuid/uuid.dart'; + +class Prescriptions { + final SupabaseClient _supabase = Supabase.instance.client; + + Future getPrescriptions(String uuid) async { + try { + final data = await _supabase.from('prescriptions').select('prescriptions_uuid').eq('user_id', uuid); + return data.toList(); + } catch (e) { + return []; + } + } + + Future postPrescription(String userId, String medicineUuid) async { + try { + final prescriptionUUID = Uuid().v4(); + await _supabase + .from('prescriptions') + .insert({'prescriptions_uuid': prescriptionUUID, 'user_id': userId, 'ref_medicine_uuid': medicineUuid}); + return true; + } catch (e) { + return false; + } + } +} diff --git a/lib/tables/ref_medicines.dart b/lib/tables/ref_medicines.dart index ce0a67f..4695507 100644 --- a/lib/tables/ref_medicines.dart +++ b/lib/tables/ref_medicines.dart @@ -90,7 +90,7 @@ class RefMedicines { final medicine = { 'ref_medicines_uuid': uuid, 'medicine_name': name, - 'ref_manufactorers_uuid': muuid, + 'ref_manufacturers_uuid': muuid, 'ref_generic_names_uuid': guuid, 'ref_types_uuid': tuuid, 'barcode': barcode diff --git a/lib/tables/stocks.dart b/lib/tables/stocks.dart index 0b344fd..d16caa9 100644 --- a/lib/tables/stocks.dart +++ b/lib/tables/stocks.dart @@ -29,6 +29,33 @@ class Stocks { } } + Future getListWithUUID() async { + List> stockData = []; + + try { + final data = await _supabase + .from('stocks') + .select( + 'ref_medicines_uuid, ref_medicines(medicine_name, ref_generic_names(generic_name)), expiration_date, quantity, price') + .order('ref_medicines(medicine_name)', ascending: true); + + for (var item in data) { + stockData.add({ + 'uuid': item['ref_medicines_uuid'], + 'medicine_name': item['ref_medicines']['medicine_name'], + 'generic_name': item['ref_medicines']['ref_generic_names']['generic_name'], + 'expiration_date': item['expiration_date'], + 'quantity': item['quantity'], + 'price': item['price'], + }); + } + + return stockData; + } catch (e) { + return []; + } + } + Future getUUID(String name) async { try { final medUUID = await _supabase.from('ref_medicines').select('ref_medicines_uuid').eq('medicine_name', name);