cdb_ui/lib/pages/supply.dart
2024-10-08 10:11:18 +02:00

354 lines
11 KiB
Dart

import 'package:cdb_ui/api.dart';
import 'package:flutter/material.dart';
import 'package:simple_barcode_scanner/enum.dart';
import 'package:simple_barcode_scanner/simple_barcode_scanner.dart';
Future<String?> scanQRCode(BuildContext context,
{String title = "Scan QR Code"}) async {
var res = await Navigator.push(
context,
MaterialPageRoute(
builder: (context) => SimpleBarcodeScannerPage(
scanType: ScanType.qr,
appBarTitle: title,
),
));
return res;
}
class SupplyPage extends StatefulWidget {
final Item item;
final Function refresh;
final List<String>? onlyVariants;
final String? forcePrice;
final String? forceOrigin;
// 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});
@override
State<SupplyPage> createState() => _SupplyPageState();
}
class _SupplyPageState extends State<SupplyPage> {
late List<String> availableVariants;
late String variant;
final _formKey = GlobalKey<FormState>();
String _selectedOrigin = "";
String _selectedLocation = "";
late TextEditingController _priceController;
late TextEditingController _noteController;
@override
void initState() {
super.initState();
availableVariants =
widget.onlyVariants ?? widget.item.variants.keys.toList();
variant = availableVariants.first;
_selectedOrigin = widget.forceOrigin ?? "";
_priceController = TextEditingController(text: widget.forcePrice ?? "");
_noteController = TextEditingController(text: "");
}
void _supply() {
if (_formKey.currentState!.validate()) {
_formKey.currentState!.save();
if (widget.onCreate != null) {
var t = SupplyForm(
itemID: widget.item.id,
variant: variant,
price: double.parse(_priceController.text),
origin: _selectedOrigin,
location: _selectedLocation,
note: _noteController.text);
widget.onCreate!(t);
Navigator.of(context).pop();
widget.refresh();
return;
}
API()
.supplyItem(
widget.item.id,
variant,
double.parse(_priceController.text),
_selectedOrigin,
_selectedLocation,
_noteController.text)
.then((_) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Item added successfully!')),
);
Navigator.of(context).pop();
widget.refresh();
});
}
}
@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());
}
var data = snapshot.data as Map<String, dynamic>;
var locations = data['locations']! as Map<String, Location>;
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!;
});
},
items: availableVariants.map((entryKey) {
var entry = widget.item.variants[entryKey]!;
return DropdownMenuItem<String>(
value: entryKey,
child: Text(entry.name),
);
}).toList(),
onSaved: (value) {
variant = value!;
},
),
const SizedBox(height: 16),
// Origin Field with Dropdown and Text Input
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.toStringAsFixed(2))
: _priceController.text;
setState(() {
_priceController.text = price;
_selectedOrigin = selection;
});
},
label: "Origin"),
const SizedBox(height: 16),
],
// Price Field
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;
},
),
const SizedBox(height: 16),
],
// Location Dropdown
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,
child:
Text(locations[id]!.fullNamePath(locations)),
);
}).toList(),
onSaved: (value) {
_selectedLocation = value!;
},
),
),
const SizedBox(
width: 12,
),
IconButton(
onPressed: () async {
var code = await scanQRCode(context);
setState(() {
if (API().getLocations().keys.contains(code)) {
_selectedLocation = code!;
}
});
},
icon: const Icon(Icons.qr_code),
),
],
),
// Note
TextFormField(
decoration: const InputDecoration(labelText: 'Note'),
controller: _noteController,
maxLines: 5),
const SizedBox(height: 20),
// Submit Button
ElevatedButton(
onPressed: _supply,
child: const Text('Add Item'),
),
],
),
),
);
},
),
);
}
Future<Map<String, dynamic>> _fetchData() async {
var locations = API().getLocations();
var origins = await API().getUniqueField(widget.item.id, variant, "origin");
origins.insert(0, "");
locations[""] = Location.zero();
return {'locations': locations, 'origins': origins};
}
}
// 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(),
),
);
},
);
}
}
class SupplyForm {
final String itemID;
final String variant;
final double 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'],
);
}
Map<String, dynamic> json() {
return {
"item": itemID,
"variant": variant,
"price": price,
"origin": origin,
"location": location,
"note": 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);
}
}