113 lines
3.8 KiB
Dart
113 lines
3.8 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
import 'package:kifi_app/features/inventory/domain/stock_movement.dart';
|
|
import 'package:kifi_app/features/inventory/providers/products_provider.dart';
|
|
import 'package:intl/intl.dart';
|
|
|
|
class StockLedgerTab extends ConsumerStatefulWidget {
|
|
final int productId;
|
|
|
|
const StockLedgerTab({super.key, required this.productId});
|
|
|
|
@override
|
|
ConsumerState<StockLedgerTab> createState() => _StockLedgerTabState();
|
|
}
|
|
|
|
class _StockLedgerTabState extends ConsumerState<StockLedgerTab> {
|
|
List<StockMovement>? _ledger;
|
|
bool _isLoading = true;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_fetchLedger();
|
|
}
|
|
|
|
Future<void> _fetchLedger() async {
|
|
try {
|
|
final ledger = await ref.read(productsProvider.notifier).fetchStockLedger(widget.productId);
|
|
if (mounted) {
|
|
setState(() {
|
|
_ledger = ledger;
|
|
_isLoading = false;
|
|
});
|
|
}
|
|
} catch (e) {
|
|
if (mounted) {
|
|
setState(() {
|
|
_isLoading = false;
|
|
});
|
|
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(e.toString())));
|
|
}
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
if (_isLoading) {
|
|
return const Center(child: CircularProgressIndicator());
|
|
}
|
|
|
|
if (_ledger == null || _ledger!.isEmpty) {
|
|
return const Center(child: Text("No stock movements recorded."));
|
|
}
|
|
|
|
return RefreshIndicator(
|
|
onRefresh: _fetchLedger,
|
|
child: ListView.builder(
|
|
padding: const EdgeInsets.all(16.0),
|
|
itemCount: _ledger!.length,
|
|
itemBuilder: (context, index) {
|
|
final movement = _ledger![index];
|
|
final isAddition = movement.type == 'ADDITION' || movement.type == 'OPENING';
|
|
final qty = movement.items?.isNotEmpty == true ? movement.items!.first.quantity : 0.0;
|
|
|
|
return Card(
|
|
elevation: 0,
|
|
margin: const EdgeInsets.only(bottom: 12),
|
|
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
|
|
child: ListTile(
|
|
contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
|
|
leading: Container(
|
|
padding: const EdgeInsets.all(10),
|
|
decoration: BoxDecoration(
|
|
color: isAddition ? Colors.green.withOpacity(0.1) : Colors.red.withOpacity(0.1),
|
|
shape: BoxShape.circle,
|
|
),
|
|
child: Icon(
|
|
isAddition ? Icons.arrow_downward : Icons.arrow_upward,
|
|
color: isAddition ? Colors.green : Colors.red,
|
|
),
|
|
),
|
|
title: Text(movement.type, style: const TextStyle(fontWeight: FontWeight.bold)),
|
|
subtitle: Text(
|
|
movement.createdAt != null ? DateFormat('dd MMM yyyy, hh:mm a').format(movement.createdAt!) : '',
|
|
style: TextStyle(color: Colors.grey[600]),
|
|
),
|
|
trailing: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
crossAxisAlignment: CrossAxisAlignment.end,
|
|
children: [
|
|
Text(
|
|
'${isAddition ? '+' : '-'}${qty.toStringAsFixed(1)}',
|
|
style: TextStyle(
|
|
fontWeight: FontWeight.bold,
|
|
fontSize: 16,
|
|
color: isAddition ? Colors.green : Colors.red,
|
|
),
|
|
),
|
|
if (movement.notes != null && movement.notes!.isNotEmpty)
|
|
Text(
|
|
movement.notes!,
|
|
style: TextStyle(color: Colors.grey[500], fontSize: 12),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
},
|
|
),
|
|
);
|
|
}
|
|
}
|