import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; import 'package:fcs/model/storage_model.dart'; import 'package:fcs/pages/storage/inventory_item.dart'; import 'package:fcs/pages/util.dart'; import 'package:fcs/theme/theme.dart'; import 'package:fcs/vo/inventory_line.dart'; import 'package:fcs/vo/inventory_taking.dart'; import 'package:fcs/widget/local_text.dart'; import 'package:fcs/widget/my_data_table.dart'; import 'package:fcs/widget/progress.dart'; class TakeInventory extends StatefulWidget { @override _TakeInventoryState createState() => _TakeInventoryState(); } class _TakeInventoryState extends State { InventoryTaking inventoryTaking = InventoryTaking(); bool _isLoading = false; @override void initState() { super.initState(); var _storages = Provider.of(context, listen: false).storages; _storages.forEach((s) { s.products.forEach((p) { inventoryTaking.inventoryLines.add(InventoryLine( productID: p.id, productName: p.name, storageID: p.storageID, storageName: p.storageName, oldQty: p.quantity, quantity: p.quantity)); }); }); } @override Widget build(BuildContext context) { return LocalProgress( inAsyncCall: _isLoading, child: Scaffold( appBar: AppBar( backgroundColor: primaryColor, title: LocalText( context, 'inventory.take', fontSize: 20, color: Colors.white, ), actions: [ IconButton( icon: Icon(Icons.send), onPressed: () { showConfirmDialog(context, "inventory.confirm", () { _submit(); }); }, ) ], ), floatingActionButton: FloatingActionButton( backgroundColor: primaryColor, child: Icon(Icons.add), onPressed: () async { final InventoryLine inventoryLine = await Navigator.push( context, MaterialPageRoute(builder: (context) => InventoryItem()), ); _save(inventoryLine); }, ), body: SingleChildScrollView( scrollDirection: Axis.vertical, child: SingleChildScrollView( scrollDirection: Axis.horizontal, child: MyDataTable( columns: [ MyDataColumn( label: LocalText(context, "inventory.product")), MyDataColumn( label: LocalText(context, "inventory.storage")), MyDataColumn( label: LocalText(context, "inventory.old.qty"), ), MyDataColumn( label: LocalText(context, "inventory.new.qty"), ), ], rows: getInventoryTable(inventoryTaking.inventoryLines), ), ), )), ); } List getInventoryTable(List invLines) { return invLines.map((p) { return MyDataRow( onSelectChanged: (bool selected) async { var inventoryLine = await Navigator.push( context, MaterialPageRoute( builder: (context) => InventoryItem( inventoryLine: p, )), ); _save(inventoryLine); }, cells: [ MyDataCell( new Text( p.productName, style: textStyle, ), ), MyDataCell( new Text(p.storageName.toString(), style: textStyle), ), MyDataCell( new Text(p.oldQty.toString(), style: textStyle), ), MyDataCell( new Text(p.quantity.toString(), style: textStyle), ), ], ); }).toList(); } _save(InventoryLine inventoryLine) { if (inventoryLine == null) return; if (inventoryLine.action == "create") { if (inventoryTaking.inventoryLines.contains(inventoryLine)) { showMsgDialog(context, "Error", "Duplicate line"); return; } inventoryTaking.inventoryLines.add(inventoryLine); } else if (inventoryLine.action == "delete") { inventoryTaking.inventoryLines.remove(inventoryLine); } } _submit() async { if (inventoryTaking.inventoryLines.length == 0) { showMsgDialog(context, "Error", "No inventory line"); return; } setState(() { _isLoading = true; }); try { StorageModel storageModel = Provider.of(context); await storageModel.createInventoryTaking(inventoryTaking); Navigator.pop(context); } catch (e) { showMsgDialog(context, "Error", e.toString()); } finally { setState(() { _isLoading = false; }); } } }