iv_and/create_invoice_screen.dart
joe a8242c2a7e
Some checks are pending
Flutter CI / build (push) Waiting to run
feat: Invoiceアプリの基本機能を追加
Co-authored-by: aider (ollama_chat/7b) <aider@aider.chat>
2026-01-16 09:35:27 +09:00

111 lines
3.9 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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<CreateInvoiceScreen> {
final TextEditingController _quantityController = TextEditingController();
int? _selectedCustomerId;
int? _selectedProductId;
@override
Widget build(BuildContext context) {
final customerProvider = Provider.of<CustomerProvider>(context);
final productProvider = Provider.of<ProductProvider>(context);
final invoiceProvider = Provider.of<InvoiceProvider>(context);
return Scaffold(
appBar: AppBar(
title: Text('請求書作成'),
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
children: [
DropdownButtonFormField<int>(
value: _selectedCustomerId,
items: customerProvider.customers.map((customer) {
return DropdownMenuItem<int>(
value: customer.id,
child: Text(customer.name),
);
}).toList(),
onChanged: (value) {
setState(() {
_selectedCustomerId = value;
});
},
decoration: InputDecoration(labelText: '顧客選択'),
),
SizedBox(height: 20),
DropdownButtonFormField<int>(
value: _selectedProductId,
items: productProvider.products.map((product) {
return DropdownMenuItem<int>(
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生成'),
),
],
),
),
);
}
}