cdb_ui/lib/pages/supply.dart

327 lines
10 KiB
Dart
Raw Normal View History

2024-09-07 21:56:52 +00:00
import 'package:cdb_ui/api.dart';
import 'package:flutter/material.dart';
import 'package:qr_bar_code_scanner_dialog/qr_bar_code_scanner_dialog.dart';
class SupplyPage extends StatefulWidget {
final Item item;
2024-09-15 15:49:50 +00:00
final Function refresh;
2024-09-25 16:14:42 +00:00
final List<String>? onlyVariants;
final String? forcePrice;
final String? forceOrigin;
2024-09-07 21:56:52 +00:00
2024-09-25 16:14:42 +00:00
// callback function for receiving a transaction without creating on the API
final Function(SupplyForm)? onCreate;
const SupplyPage(this.item, this.refresh,
{this.onlyVariants,
this.onCreate,
this.forceOrigin,
this.forcePrice,
super.key});
2024-09-07 21:56:52 +00:00
@override
State<SupplyPage> createState() => _SupplyPageState();
}
class _SupplyPageState extends State<SupplyPage> {
2024-09-25 16:14:42 +00:00
late List<String> availableVariants;
2024-09-07 21:56:52 +00:00
late String variant;
final _formKey = GlobalKey<FormState>();
String _selectedOrigin = "";
String _selectedLocation = "";
2024-09-20 11:10:26 +00:00
late TextEditingController _priceController;
2024-09-20 23:37:11 +00:00
late TextEditingController _noteController;
2024-09-07 21:56:52 +00:00
@override
void initState() {
super.initState();
2024-09-25 16:14:42 +00:00
availableVariants =
widget.onlyVariants ?? widget.item.variants.keys.toList();
2024-09-07 21:56:52 +00:00
variant = widget.item.variants.keys.first;
2024-09-25 16:14:42 +00:00
_selectedOrigin = widget.forceOrigin ?? "";
_priceController = TextEditingController(text: widget.forcePrice ?? "");
2024-09-20 23:37:11 +00:00
_noteController = TextEditingController(text: "");
2024-09-07 21:56:52 +00:00
}
2024-09-16 07:42:55 +00:00
void _supply() {
2024-09-07 21:56:52 +00:00
if (_formKey.currentState!.validate()) {
_formKey.currentState!.save();
2024-09-25 16:14:42 +00:00
if (widget.onCreate != null) {
// todo : create shadow transaction
var t = SupplyForm(
itemID: widget.item.id,
variant: variant,
price: "${_priceController.text}",
origin: _selectedOrigin,
location: _selectedLocation,
note: _noteController.text);
widget.onCreate!(t);
Navigator.of(context).pop();
widget.refresh();
return;
}
2024-09-16 07:42:55 +00:00
API()
2024-09-21 02:15:00 +00:00
.supplyItem(widget.item.id, variant, "${_priceController.text}",
2024-09-20 23:37:11 +00:00
_selectedOrigin, _selectedLocation, _noteController.text)
2024-09-16 07:42:55 +00:00
.then((_) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Item added successfully!')),
);
Navigator.of(context).pop();
widget.refresh();
});
2024-09-07 21:56:52 +00:00
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Add New Item'),
),
body: FutureBuilder(
future: _fetchData(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return const Center(child: CircularProgressIndicator());
}
2024-09-15 13:15:46 +00:00
var data = snapshot.data as Map<String, dynamic>;
var locations = data['locations']! as Map<String, Location>;
2024-09-07 21:56:52 +00:00
var origins = data['origins']! as List<String>;
return Padding(
padding: const EdgeInsets.all(16.0),
child: Form(
key: _formKey,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
// Variant Selection
DropdownButtonFormField<String>(
hint: const Text('Select Variant'),
value: variant,
onChanged: (value) {
setState(() {
variant = value!;
});
},
2024-09-25 16:14:42 +00:00
items: availableVariants.map((entryKey) {
var entry = widget.item.variants[entryKey]!;
2024-09-07 21:56:52 +00:00
return DropdownMenuItem<String>(
2024-09-25 16:14:42 +00:00
value: entryKey,
child: Text(entry.name),
2024-09-07 21:56:52 +00:00
);
}).toList(),
onSaved: (value) {
variant = value!;
},
),
const SizedBox(height: 16),
// Origin Field with Dropdown and Text Input
2024-09-25 16:14:42 +00:00
if (widget.forceOrigin == null)
AutocompletedTextField(
options: origins,
getValue: () => _selectedOrigin,
onChanged: (value) {
_selectedOrigin = value;
},
onSelection: (String selection) async {
var price = _priceController.text.isEmpty
? await API()
.getLatestPrice(widget.item.id, variant,
origin: selection)
.then((x) => x.value.toStringAsFixed(2))
: _priceController.text;
setState(() {
_priceController.text = price;
_selectedOrigin = selection;
});
},
label: "Origin"),
2024-09-07 21:56:52 +00:00
const SizedBox(height: 16),
// Price Field
2024-09-25 16:14:42 +00:00
if (widget.forcePrice == null)
TextFormField(
decoration: const InputDecoration(labelText: 'Price'),
keyboardType: TextInputType.number,
controller: _priceController,
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter a price';
}
if (double.tryParse(value) == null) {
return 'Please enter a valid number';
}
return null;
},
),
2024-09-07 21:56:52 +00:00
const SizedBox(height: 16),
// Location Dropdown
2024-09-15 13:15:46 +00:00
Row(
children: [
Expanded(
child: DropdownButtonFormField<String>(
hint: const Text('Select Location'),
value: _selectedLocation,
onChanged: (value) {
setState(() {
_selectedLocation = value!;
});
},
items: locations.keys
.map<DropdownMenuItem<String>>((id) {
return DropdownMenuItem<String>(
value: id,
2024-09-16 07:42:55 +00:00
child:
Text(locations[id]!.fullNamePath(locations)),
2024-09-15 13:15:46 +00:00
);
}).toList(),
onSaved: (value) {
_selectedLocation = value!;
},
),
),
2024-09-15 15:49:50 +00:00
const SizedBox(
2024-09-15 13:15:46 +00:00
width: 12,
),
IconButton(
onPressed: () {
QrBarCodeScannerDialog().getScannedQrBarCode(
context: context,
onCode: (code) {
setState(() {
_selectedLocation = code!;
});
},
);
2024-09-07 21:56:52 +00:00
},
2024-09-15 13:15:46 +00:00
icon: const Icon(Icons.qr_code),
),
],
2024-09-07 21:56:52 +00:00
),
2024-09-20 23:37:11 +00:00
// Note
TextFormField(
2024-09-25 08:49:21 +00:00
decoration: const InputDecoration(labelText: 'Note'),
controller: _noteController,
maxLines: 5),
2024-09-20 23:37:11 +00:00
2024-09-07 21:56:52 +00:00
const SizedBox(height: 20),
// Submit Button
ElevatedButton(
onPressed: _supply,
child: const Text('Add Item'),
),
],
),
),
);
},
),
);
}
2024-09-15 13:15:46 +00:00
Future<Map<String, dynamic>> _fetchData() async {
var locations = await API().getLocations();
2024-09-07 21:56:52 +00:00
var origins = await API().getUniqueField(widget.item.id, variant, "origin");
origins.insert(0, "");
2024-09-15 13:15:46 +00:00
locations[""] = Location.zero();
2024-09-07 21:56:52 +00:00
return {'locations': locations, 'origins': origins};
}
}
2024-09-17 14:33:55 +00:00
// ignore: must_be_immutable
class AutocompletedTextField extends StatelessWidget {
late List<String> options;
late Function(String) onChanged;
late String Function() getValue;
late Function(String) onSelection;
late String label;
AutocompletedTextField(
{super.key,
required this.options,
required this.getValue,
required this.onChanged,
required this.onSelection,
required this.label});
@override
Widget build(BuildContext context) {
return Autocomplete<String>(
optionsBuilder: (TextEditingValue textEditingValue) {
if (textEditingValue.text.isEmpty) {
return options;
}
return options.where((String option) {
return option
.toLowerCase()
.contains(textEditingValue.text.toLowerCase());
});
},
onSelected: onSelection,
fieldViewBuilder:
(context, textEditingController, focusNode, onFieldSubmitted) {
textEditingController.text = getValue();
return TextFormField(
onChanged: onChanged,
controller: textEditingController,
focusNode: focusNode,
decoration: InputDecoration(
labelText: label,
border: const OutlineInputBorder(),
),
);
},
);
}
}
2024-09-25 16:14:42 +00:00
class SupplyForm {
final String itemID;
final String variant;
final String price;
final String? origin;
final String? location;
final String note;
factory SupplyForm.fromJson(Map<String, dynamic> json) {
return SupplyForm(
itemID: json['item'],
variant: json['variant'],
price: json['price'],
origin: json['origin'],
location: json['location'],
note: json['note'],
);
}
SupplyForm({
required this.itemID,
required this.variant,
required this.price,
required this.origin,
required this.location,
required this.note,
});
Transaction transaction(Map<String, Location> locations) {
return Transaction.inMemory(itemID, variant, price, origin,
location != null ? locations[location!] : null, note);
}
}