inventory update
This commit is contained in:
parent
de188372d9
commit
5391d953a7
@ -47,6 +47,8 @@ class Product {
|
|||||||
final DateTime createdAt;
|
final DateTime createdAt;
|
||||||
final DateTime updatedAt;
|
final DateTime updatedAt;
|
||||||
final int v;
|
final int v;
|
||||||
|
int? sale;
|
||||||
|
int? inventory;
|
||||||
|
|
||||||
Product({
|
Product({
|
||||||
required this.id,
|
required this.id,
|
||||||
@ -63,6 +65,8 @@ class Product {
|
|||||||
required this.createdAt,
|
required this.createdAt,
|
||||||
required this.updatedAt,
|
required this.updatedAt,
|
||||||
required this.v,
|
required this.v,
|
||||||
|
this.sale,
|
||||||
|
this.inventory,
|
||||||
});
|
});
|
||||||
|
|
||||||
factory Product.fromJson(Map<String, dynamic> json) {
|
factory Product.fromJson(Map<String, dynamic> json) {
|
||||||
|
@ -12,11 +12,14 @@ class ProductProvider extends ChangeNotifier {
|
|||||||
final _apiClient = ApiClient();
|
final _apiClient = ApiClient();
|
||||||
ProductResponse? productResponse;
|
ProductResponse? productResponse;
|
||||||
List<Product> productList = [];
|
List<Product> productList = [];
|
||||||
|
final List<Product> _selectedProducts = [];
|
||||||
|
|
||||||
bool _isLoading = false;
|
bool _isLoading = false;
|
||||||
|
|
||||||
bool get isLoading => _isLoading;
|
bool get isLoading => _isLoading;
|
||||||
|
|
||||||
|
List<Product> get selectedProducts => _selectedProducts;
|
||||||
|
|
||||||
void setLoading(bool loading) {
|
void setLoading(bool loading) {
|
||||||
_isLoading = loading;
|
_isLoading = loading;
|
||||||
notifyListeners();
|
notifyListeners();
|
||||||
@ -37,4 +40,36 @@ class ProductProvider extends ChangeNotifier {
|
|||||||
print("Error: $e");
|
print("Error: $e");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<bool> submitSelectedProducts(
|
||||||
|
String addedFor, String addedForId) async {
|
||||||
|
setLoading(true);
|
||||||
|
try {
|
||||||
|
final data = {
|
||||||
|
"products": selectedProducts.map((product) {
|
||||||
|
return {
|
||||||
|
"SKU": product.SKU,
|
||||||
|
"ProductName": product.name,
|
||||||
|
"Sale": product.sale,
|
||||||
|
"Inventory": product.inventory,
|
||||||
|
};
|
||||||
|
}).toList(),
|
||||||
|
"addedFor": addedFor,
|
||||||
|
"addedForId": addedForId,
|
||||||
|
};
|
||||||
|
|
||||||
|
Response response =
|
||||||
|
await _apiClient.post(ApiUrls.submitProducts, data: data);
|
||||||
|
setLoading(false);
|
||||||
|
if (response.statusCode == 201) {
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
setLoading(false);
|
||||||
|
print("Error: $e");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,7 +16,6 @@ class AddProductsScreen extends StatefulWidget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class _AddProductsScreenState extends State<AddProductsScreen> {
|
class _AddProductsScreenState extends State<AddProductsScreen> {
|
||||||
List<Product> selectedProducts = [];
|
|
||||||
List<Product> filteredProducts = [];
|
List<Product> filteredProducts = [];
|
||||||
final searchController = TextEditingController();
|
final searchController = TextEditingController();
|
||||||
late ProductProvider provider;
|
late ProductProvider provider;
|
||||||
@ -28,7 +27,7 @@ class _AddProductsScreenState extends State<AddProductsScreen> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Future<void> loadProducts() async {
|
Future<void> loadProducts() async {
|
||||||
final provider = Provider.of<ProductProvider>(context, listen: false);
|
provider = Provider.of<ProductProvider>(context, listen: false);
|
||||||
await provider.getProducts();
|
await provider.getProducts();
|
||||||
setState(() {
|
setState(() {
|
||||||
filteredProducts = provider.productList;
|
filteredProducts = provider.productList;
|
||||||
@ -87,20 +86,19 @@ class _AddProductsScreenState extends State<AddProductsScreen> {
|
|||||||
children: [
|
children: [
|
||||||
Column(
|
Column(
|
||||||
children: [
|
children: [
|
||||||
if (selectedProducts.isNotEmpty)
|
if (provider.selectedProducts.isNotEmpty)
|
||||||
Expanded(
|
Expanded(
|
||||||
child: ListView.builder(
|
child: ListView.builder(
|
||||||
itemCount: selectedProducts.length,
|
itemCount: provider.selectedProducts.length,
|
||||||
itemBuilder: (context, index) {
|
itemBuilder: (context, index) {
|
||||||
return ProductBlock(
|
return ProductBlock(index: index);
|
||||||
product: selectedProducts[index]);
|
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
Align(
|
Align(
|
||||||
alignment: selectedProducts.isEmpty
|
alignment: provider.selectedProducts.isEmpty
|
||||||
? Alignment.center
|
? Alignment.center
|
||||||
: Alignment.bottomCenter,
|
: Alignment.bottomCenter,
|
||||||
child: Padding(
|
child: Padding(
|
||||||
@ -146,9 +144,10 @@ class _AddProductsScreenState extends State<AddProductsScreen> {
|
|||||||
filteredProducts.length,
|
filteredProducts.length,
|
||||||
itemBuilder: (context, index) {
|
itemBuilder: (context, index) {
|
||||||
bool isAlreadySelected =
|
bool isAlreadySelected =
|
||||||
selectedProducts.contains(
|
provider.selectedProducts
|
||||||
filteredProducts[
|
.contains(
|
||||||
index]);
|
filteredProducts[
|
||||||
|
index]);
|
||||||
return Card(
|
return Card(
|
||||||
child: ListTile(
|
child: ListTile(
|
||||||
title: Text(
|
title: Text(
|
||||||
@ -173,8 +172,9 @@ class _AddProductsScreenState extends State<AddProductsScreen> {
|
|||||||
? null
|
? null
|
||||||
: () {
|
: () {
|
||||||
setState(() {
|
setState(() {
|
||||||
selectedProducts.add(
|
provider
|
||||||
filteredProducts[
|
.selectedProducts
|
||||||
|
.add(filteredProducts[
|
||||||
index]);
|
index]);
|
||||||
});
|
});
|
||||||
Navigator.pop(
|
Navigator.pop(
|
||||||
@ -202,7 +202,7 @@ class _AddProductsScreenState extends State<AddProductsScreen> {
|
|||||||
style: TextStyle(color: Colors.black),
|
style: TextStyle(color: Colors.black),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
if (selectedProducts.isNotEmpty) ...[
|
if (provider.selectedProducts.isNotEmpty) ...[
|
||||||
const SizedBox(height: 16.0),
|
const SizedBox(height: 16.0),
|
||||||
CommonElevatedButton(
|
CommonElevatedButton(
|
||||||
borderRadius: 30,
|
borderRadius: 30,
|
||||||
@ -211,13 +211,19 @@ class _AddProductsScreenState extends State<AddProductsScreen> {
|
|||||||
text: 'SUBMIT',
|
text: 'SUBMIT',
|
||||||
backgroundColor: const Color(0xff004791),
|
backgroundColor: const Color(0xff004791),
|
||||||
onPressed: () {
|
onPressed: () {
|
||||||
Navigator.push(
|
provider
|
||||||
context,
|
.submitSelectedProducts("PrincipalDistributor", "66a0e19c981736b70ed4e34e")
|
||||||
MaterialPageRoute(
|
.then((value) {
|
||||||
builder: (context) =>
|
if (value) {
|
||||||
const DataSubmitSuccessfull(),
|
Navigator.push(
|
||||||
),
|
context,
|
||||||
);
|
MaterialPageRoute(
|
||||||
|
builder: (context) =>
|
||||||
|
const DataSubmitSuccessfull(),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
});
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
@ -235,9 +241,9 @@ class _AddProductsScreenState extends State<AddProductsScreen> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class ProductBlock extends StatefulWidget {
|
class ProductBlock extends StatefulWidget {
|
||||||
final Product product;
|
final int index;
|
||||||
|
|
||||||
const ProductBlock({super.key, required this.product});
|
const ProductBlock({super.key, required this.index});
|
||||||
|
|
||||||
@override
|
@override
|
||||||
_ProductBlockState createState() => _ProductBlockState();
|
_ProductBlockState createState() => _ProductBlockState();
|
||||||
@ -247,13 +253,15 @@ class _ProductBlockState extends State<ProductBlock> {
|
|||||||
final saleController = TextEditingController();
|
final saleController = TextEditingController();
|
||||||
final inventoryController = TextEditingController();
|
final inventoryController = TextEditingController();
|
||||||
String? errorMessage;
|
String? errorMessage;
|
||||||
|
late ProductProvider provider;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
|
provider = Provider.of<ProductProvider>(context, listen: false);
|
||||||
}
|
}
|
||||||
|
|
||||||
void validateInput() {
|
bool validateInput() {
|
||||||
setState(() {
|
setState(() {
|
||||||
if (saleController.text.isNotEmpty &&
|
if (saleController.text.isNotEmpty &&
|
||||||
inventoryController.text.isNotEmpty) {
|
inventoryController.text.isNotEmpty) {
|
||||||
@ -268,6 +276,7 @@ class _ProductBlockState extends State<ProductBlock> {
|
|||||||
errorMessage = null;
|
errorMessage = null;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
return errorMessage == null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
@ -281,9 +290,9 @@ class _ProductBlockState extends State<ProductBlock> {
|
|||||||
child: Column(
|
child: Column(
|
||||||
crossAxisAlignment: CrossAxisAlignment.start,
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
children: [
|
children: [
|
||||||
Text('Product: ${widget.product.name}',
|
Text('Product: ${provider.selectedProducts[widget.index].name}',
|
||||||
style: const TextStyle(fontSize: 16)),
|
style: const TextStyle(fontSize: 16)),
|
||||||
Text('SKU: ${widget.product.SKU}',
|
Text('SKU: ${provider.selectedProducts[widget.index].SKU}',
|
||||||
style: const TextStyle(fontSize: 15)),
|
style: const TextStyle(fontSize: 15)),
|
||||||
const SizedBox(height: 8),
|
const SizedBox(height: 8),
|
||||||
TextField(
|
TextField(
|
||||||
@ -292,7 +301,10 @@ class _ProductBlockState extends State<ProductBlock> {
|
|||||||
keyboardType: TextInputType.number,
|
keyboardType: TextInputType.number,
|
||||||
// enabled: widget.product.isPurchased,
|
// enabled: widget.product.isPurchased,
|
||||||
enabled: true,
|
enabled: true,
|
||||||
onChanged: (_) => validateInput(),
|
onChanged: (_) => validateInput()
|
||||||
|
? provider.selectedProducts[widget.index].sale =
|
||||||
|
int.parse(saleController.text)
|
||||||
|
: null,
|
||||||
),
|
),
|
||||||
TextField(
|
TextField(
|
||||||
controller: inventoryController,
|
controller: inventoryController,
|
||||||
@ -300,7 +312,10 @@ class _ProductBlockState extends State<ProductBlock> {
|
|||||||
keyboardType: TextInputType.number,
|
keyboardType: TextInputType.number,
|
||||||
// enabled: widget.product.isPurchased,
|
// enabled: widget.product.isPurchased,
|
||||||
enabled: true,
|
enabled: true,
|
||||||
onChanged: (_) => validateInput(),
|
onChanged: (_) => validateInput()
|
||||||
|
? provider.selectedProducts[widget.index].inventory =
|
||||||
|
int.parse(inventoryController.text)
|
||||||
|
: null,
|
||||||
),
|
),
|
||||||
if (errorMessage != null)
|
if (errorMessage != null)
|
||||||
Padding(
|
Padding(
|
||||||
|
@ -14,4 +14,5 @@ class ApiUrls {
|
|||||||
static const String getProducts = '${baseUrl}product/getAll/user/';
|
static const String getProducts = '${baseUrl}product/getAll/user/';
|
||||||
static const String getPdRdUrl =
|
static const String getPdRdUrl =
|
||||||
'inventory/distributors-TM/RetailDistributor';
|
'inventory/distributors-TM/RetailDistributor';
|
||||||
|
static const String submitProducts = 'inventory/add-TM';
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user