diff --git a/lib/models/product_model.dart b/lib/models/product_model.dart index e661208..1adf0c3 100644 --- a/lib/models/product_model.dart +++ b/lib/models/product_model.dart @@ -2,13 +2,13 @@ class ProductResponse { final bool success; final int totalData; final int totalPages; - final List product; + final List products; ProductResponse({ required this.success, required this.totalData, required this.totalPages, - required this.product, + required this.products, }); factory ProductResponse.fromJson(Map json) { @@ -16,7 +16,7 @@ class ProductResponse { success: json['success'], totalData: json['total_data'], totalPages: json['total_pages'], - product: (json['product'] as List) + products: (json['products'] as List) .map((item) => Product.fromJson(item)) .toList(), ); @@ -27,7 +27,7 @@ class ProductResponse { 'success': success, 'total_data': totalData, 'total_pages': totalPages, - 'product': product.map((item) => item.toJson()).toList(), + 'products': products.map((item) => item.toJson()).toList(), }; } } @@ -85,6 +85,8 @@ class Product { createdAt: DateTime.parse(json['createdAt']), updatedAt: DateTime.parse(json['updatedAt']), v: json['__v'], + sale: json['sale'], // Nullable field + inventory: json['inventory'], // Nullable field ); } @@ -104,6 +106,8 @@ class Product { 'createdAt': createdAt.toIso8601String(), 'updatedAt': updatedAt.toIso8601String(), '__v': v, + 'sale': sale, // Nullable field + 'inventory': inventory, // Nullable field }; } } diff --git a/lib/provider/product_provider.dart b/lib/provider/product_provider.dart index 421e9b6..2e806f7 100644 --- a/lib/provider/product_provider.dart +++ b/lib/provider/product_provider.dart @@ -32,7 +32,7 @@ class ProductProvider extends ChangeNotifier { setLoading(false); if (response.statusCode == 200) { productResponse = ProductResponse.fromJson(response.data); - productList = productResponse!.product; + productList = productResponse!.products; notifyListeners(); } } catch (e) { diff --git a/lib/screens/add_products_screen.dart b/lib/screens/add_products_screen.dart index e53ca14..6877c4f 100644 --- a/lib/screens/add_products_screen.dart +++ b/lib/screens/add_products_screen.dart @@ -1,3 +1,4 @@ +import 'package:cheminova/models/pd_rd_response_model.dart'; import 'package:cheminova/models/product_model.dart'; import 'package:cheminova/provider/product_provider.dart'; import 'package:cheminova/screens/data_submit_successfull.dart'; @@ -9,7 +10,13 @@ import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; class AddProductsScreen extends StatefulWidget { - const AddProductsScreen({super.key}); + final PdRdResponseModel distributor; + final String distributorType; + const AddProductsScreen({ + super.key, + required this.distributor, + required this.distributorType, + }); @override State createState() => _AddProductsScreenState(); @@ -64,9 +71,9 @@ class _AddProductsScreenState extends State { padding: const EdgeInsets.only(right: 20), ), ], - title: const Text( - 'Add Products', - style: TextStyle( + title: Text( + widget.distributor.name, + style: const TextStyle( fontSize: 20, color: Colors.black, fontWeight: FontWeight.w400, @@ -215,7 +222,7 @@ class _AddProductsScreenState extends State { provider .submitSelectedProducts( "PrincipalDistributor", - "66a0e19c981736b70ed4e34e") + widget.distributor.id) .then((value) { if (value) { Navigator.push( @@ -268,17 +275,27 @@ class _ProductBlockState extends State { setState(() { if (saleController.text.isNotEmpty && inventoryController.text.isNotEmpty) { - int sale = int.parse(saleController.text); - int inventory = int.parse(inventoryController.text); - if (inventory > sale) { - errorMessage = 'Inventory should be less than or equal to sales'; + // Check if the input can be parsed as an integer + if (int.tryParse(saleController.text) == null || + int.tryParse(inventoryController.text) == null) { + errorMessage = 'Please enter valid integer value'; } else { - errorMessage = null; + // Parse the input as integers + int sale = int.parse(saleController.text); + int inventory = int.parse(inventoryController.text); + + // Validate if inventory is less than or equal to sales + if (inventory > sale) { + errorMessage = 'Inventory should be less than or equal to sales'; + } else { + errorMessage = null; + } } } else { - errorMessage = null; + errorMessage = 'Please fill in both fields'; } }); + return errorMessage == null; } diff --git a/lib/screens/update_inventory_screen.dart b/lib/screens/update_inventory_screen.dart index 782569d..386c53a 100644 --- a/lib/screens/update_inventory_screen.dart +++ b/lib/screens/update_inventory_screen.dart @@ -65,12 +65,21 @@ class _UpdateInventoryScreenState extends State { text: 'SUBMIT', backgroundColor: const Color(0xff004791), onPressed: () { - Navigator.push( - context, - MaterialPageRoute( - builder: (context) => const AddProductsScreen(), - ), - ); + if(selectedDistributor == null || selectedDistributorType == null){ + ScaffoldMessenger.of(context).showSnackBar( + const SnackBar(content: Text('Please select distributor type and distributor')), + ); + }else{ + Navigator.push( + context, + MaterialPageRoute( + builder: (context) => AddProductsScreen( + distributor: selectedDistributor!, + distributorType: selectedDistributorType!, + ), + ), + ); + } }, ), ),