h-1.flutter.4/lib/models/inventory.dart

95 lines
No EOL
2.7 KiB
Dart

// Version: 1.6 - Inventory モデル定義(在庫管理)
import '../services/database_helper.dart';
/// 在庫情報モデル
class Inventory {
int? id;
String productCode; // データベースでは 'product_code' カラム
String name;
double unitPrice;
int stock;
int minStock; // 再仕入れ水準
int maxStock; // 最大在庫量
String supplierName;
DateTime createdAt;
DateTime updatedAt;
Inventory({
this.id,
required this.productCode,
required this.name,
required this.unitPrice,
this.stock = 0,
this.minStock = 10,
this.maxStock = 1000,
this.supplierName = '',
DateTime? createdAt,
DateTime? updatedAt,
}) : createdAt = createdAt ?? DateTime.now(),
updatedAt = updatedAt ?? DateTime.now();
/// マップから Inventory オブジェクトへ変換
factory Inventory.fromMap(Map<String, dynamic> map) {
return Inventory(
id: map['id'] as int?,
productCode: map['product_code'] as String, // 'product_code' を使用する
name: map['name'] as String,
unitPrice: (map['unit_price'] as num).toDouble(),
stock: map['stock'] as int? ?? 0,
minStock: map['min_stock'] as int? ?? 10,
maxStock: map['max_stock'] as int? ?? 1000,
supplierName: map['supplier_name'] as String? ?? '',
createdAt: DateTime.parse(map['created_at']),
updatedAt: DateTime.parse(map['updated_at']),
);
}
/// Map に変換
Map<String, dynamic> toMap() {
return {
'id': id,
'product_code': productCode,
'name': name,
'unit_price': unitPrice,
'stock': stock,
'min_stock': minStock,
'max_stock': maxStock,
'supplier_name': supplierName,
'created_at': createdAt.toIso8601String(),
'updated_at': updatedAt.toIso8601String(),
};
}
/// カピービルダ
Inventory copyWith({
int? id,
String? productCode,
String? name,
double? unitPrice,
int? stock,
int? minStock,
int? maxStock,
String? supplierName,
DateTime? createdAt,
DateTime? updatedAt,
}) {
return Inventory(
id: id ?? this.id,
productCode: productCode ?? this.productCode,
name: name ?? this.name,
unitPrice: unitPrice ?? this.unitPrice,
stock: stock ?? this.stock,
minStock: minStock ?? this.minStock,
maxStock: maxStock ?? this.maxStock,
supplierName: supplierName ?? this.supplierName,
createdAt: createdAt ?? this.createdAt,
updatedAt: updatedAt ?? this.updatedAt,
);
}
/// 在庫不足フラグを取得
bool get isLowStock => stock <= minStock;
/// 在庫補充が必要か判定
bool needsRestock() => stock <= minStock;
}