import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; import 'customer_provider.dart'; import 'product_provider.dart'; import 'invoice_provider.dart'; class CreateInvoiceScreen extends StatefulWidget { @override _CreateInvoiceScreenState createState() => _CreateInvoiceScreenState(); } class _CreateInvoiceScreenState extends State { final TextEditingController _quantityController = TextEditingController(); int? _selectedCustomerId; int? _selectedProductId; @override Widget build(BuildContext context) { final customerProvider = Provider.of(context); final productProvider = Provider.of(context); final invoiceProvider = Provider.of(context); return Scaffold( appBar: AppBar( title: Text('請求書作成'), ), body: Padding( padding: const EdgeInsets.all(16.0), child: Column( children: [ DropdownButtonFormField( value: _selectedCustomerId, items: customerProvider.customers.map((customer) { return DropdownMenuItem( value: customer.id, child: Text(customer.name), ); }).toList(), onChanged: (value) { setState(() { _selectedCustomerId = value; }); }, decoration: InputDecoration(labelText: '顧客選択'), ), SizedBox(height: 20), DropdownButtonFormField( value: _selectedProductId, items: productProvider.products.map((product) { return DropdownMenuItem( value: product.id, child: Text(product.name), ); }).toList(), onChanged: (value) { setState(() { _selectedProductId = value; }); }, decoration: InputDecoration(labelText: '商品選択'), ), SizedBox(height: 20), TextField( controller: _quantityController, keyboardType: TextInputType.number, decoration: InputDecoration(labelText: '数量'), ), SizedBox(height: 20), ElevatedButton( onPressed: () async { if (_selectedCustomerId != null && _selectedProductId != null && _quantityController.text.isNotEmpty) { final quantity = int.parse(_quantityController.text); final product = productProvider.products.firstWhere((p) => p.id == _selectedProductId); final invoiceItem = InvoiceItem( invoiceId: 0, // 後で生成される ID productId: _selectedProductId!, quantity: quantity, unitPrice: product.unitPrice, discount: product.discount, ); final total = (product.unitPrice * quantity) - (product.unitPrice * quantity * product.discount); final tax = total * 0.1; // 簡易的な税率 final discountTotal = product.unitPrice * quantity * product.discount; final invoice = Invoice( customerId: _selectedCustomerId!, date: DateTime.now().toIso8601String(), total: total, tax: tax, discountTotal: discountTotal, ); await invoiceProvider.addInvoice(invoice); // 追加の処理(PDF生成など) Navigator.pop(context); } }, child: Text('PDF生成'), ), ], ), ), ); } }