- CLAUDE.md 대폭 개선: 개발 가이드라인 및 프로젝트 상태 문서화 - 백엔드 API 통합: 모든 엔티티 간 Foreign Key 관계 완벽 구현 - UI 일관성 강화: shadcn_ui 컴포넌트 표준화 적용 - 데이터 모델 개선: DTO 및 모델 클래스 백엔드 스키마와 100% 일치 - 사용자 관리: 회사 연결, 중복 검사, 입력 검증 기능 추가 - 창고 관리: 우편번호 연결, 중복 검사 기능 강화 - 회사 관리: 우편번호 연결, 중복 검사 로직 구현 - 장비 관리: 불필요한 카테고리 필드 제거, 벤더-모델 관계 정리 - 우편번호 시스템: 검색 다이얼로그 Provider 버그 수정 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
474 lines
18 KiB
Dart
474 lines
18 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:flutter/services.dart';
|
|
import 'package:shadcn_ui/shadcn_ui.dart';
|
|
import 'package:superport/screens/common/templates/form_layout_template.dart';
|
|
import 'package:superport/utils/currency_formatter.dart';
|
|
import 'controllers/equipment_in_form_controller.dart';
|
|
import 'widgets/equipment_vendor_model_selector.dart';
|
|
import 'package:superport/utils/formatters/number_formatter.dart';
|
|
|
|
/// 새로운 Equipment 입고 폼 (Lookup API 기반)
|
|
class EquipmentInFormScreen extends StatefulWidget {
|
|
final int? equipmentInId;
|
|
final Map<String, dynamic>? preloadedData; // 사전 로드된 데이터
|
|
|
|
const EquipmentInFormScreen({super.key, this.equipmentInId, this.preloadedData});
|
|
|
|
@override
|
|
State<EquipmentInFormScreen> createState() => _EquipmentInFormScreenState();
|
|
}
|
|
|
|
class _EquipmentInFormScreenState extends State<EquipmentInFormScreen> {
|
|
late EquipmentInFormController _controller;
|
|
late TextEditingController _serialNumberController;
|
|
late TextEditingController _initialStockController;
|
|
late TextEditingController _purchasePriceController;
|
|
Future<void>? _initFuture;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
|
|
// preloadedData가 있으면 전달, 없으면 일반 초기화
|
|
if (widget.preloadedData != null) {
|
|
_controller = EquipmentInFormController.withPreloadedData(
|
|
preloadedData: widget.preloadedData!,
|
|
);
|
|
_initFuture = Future.value(); // 데이터가 이미 있으므로 즉시 완료
|
|
} else {
|
|
_controller = EquipmentInFormController(equipmentInId: widget.equipmentInId);
|
|
// 수정 모드일 때 데이터 로드를 Future로 처리
|
|
if (_controller.isEditMode) {
|
|
_initFuture = _initializeEditMode();
|
|
} else {
|
|
_initFuture = Future.value(); // 신규 모드는 즉시 완료
|
|
}
|
|
}
|
|
|
|
_controller.addListener(_onControllerUpdated);
|
|
|
|
// TextEditingController 초기화
|
|
_serialNumberController = TextEditingController(text: _controller.serialNumber);
|
|
_initialStockController = TextEditingController(text: _controller.initialStock.toString());
|
|
_purchasePriceController = TextEditingController(
|
|
text: _controller.purchasePrice != null
|
|
? CurrencyFormatter.formatKRW(_controller.purchasePrice)
|
|
: ''
|
|
);
|
|
}
|
|
|
|
Future<void> _initializeEditMode() async {
|
|
await _controller.initializeForEdit();
|
|
// 데이터 로드 후 컨트롤러 업데이트
|
|
setState(() {
|
|
_serialNumberController.text = _controller.serialNumber;
|
|
_purchasePriceController.text = _controller.purchasePrice != null
|
|
? CurrencyFormatter.formatKRW(_controller.purchasePrice)
|
|
: '';
|
|
});
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_controller.removeListener(_onControllerUpdated);
|
|
_controller.dispose();
|
|
_serialNumberController.dispose();
|
|
_serialNumberController.dispose();
|
|
_initialStockController.dispose();
|
|
_purchasePriceController.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
void _onControllerUpdated() {
|
|
if (mounted) setState(() {});
|
|
}
|
|
|
|
// Legacy 필드 제거 - Vendor/Model cascade selector 사용
|
|
|
|
// 유효한 구매처 ID 반환 (드롭다운 assertion 오류 방지)
|
|
int? _getValidCompanyId() {
|
|
if (_controller.selectedCompanyId == null) return null;
|
|
|
|
final isValid = _controller.companies.containsKey(_controller.selectedCompanyId);
|
|
print('DEBUG [_getValidCompanyId] selectedCompanyId: ${_controller.selectedCompanyId}, isValid: $isValid, available companies: ${_controller.companies.length}');
|
|
|
|
return isValid ? _controller.selectedCompanyId : null;
|
|
}
|
|
|
|
// 유효한 창고 ID 반환 (드롭다운 assertion 오류 방지)
|
|
int? _getValidWarehouseId() {
|
|
if (_controller.selectedWarehouseId == null) return null;
|
|
|
|
final isValid = _controller.warehouses.containsKey(_controller.selectedWarehouseId);
|
|
print('DEBUG [_getValidWarehouseId] selectedWarehouseId: ${_controller.selectedWarehouseId}, isValid: $isValid, available warehouses: ${_controller.warehouses.length}');
|
|
|
|
return isValid ? _controller.selectedWarehouseId : null;
|
|
}
|
|
|
|
Future<void> _onSave() async {
|
|
if (_controller.isSaving) return;
|
|
|
|
final success = await _controller.save();
|
|
if (success && mounted) {
|
|
Navigator.pop(context, true);
|
|
} else if (_controller.error != null && mounted) {
|
|
ShadToaster.of(context).show(
|
|
ShadToast.destructive(
|
|
title: const Text('오류'),
|
|
description: Text(_controller.error!),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
// 간소화된 디버깅
|
|
print('🎯 [UI] canSave: ${_controller.canSave} | 장비번호: "${_controller.serialNumber}" | 제조사: "${_controller.manufacturer}"');
|
|
|
|
return FutureBuilder<void>(
|
|
future: _initFuture,
|
|
builder: (context, snapshot) {
|
|
// 수정 모드에서 데이터 로딩 중일 때 로딩 화면 표시
|
|
if (_controller.isEditMode && snapshot.connectionState != ConnectionState.done) {
|
|
return FormLayoutTemplate(
|
|
title: '장비 정보 로딩 중...',
|
|
onSave: null,
|
|
onCancel: () => Navigator.of(context).pop(),
|
|
isLoading: false,
|
|
child: const Center(
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
ShadProgress(),
|
|
SizedBox(height: 16),
|
|
Text('장비 정보를 불러오는 중입니다...'),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
// 데이터 로드 완료 또는 신규 모드
|
|
return FormLayoutTemplate(
|
|
title: _controller.isEditMode ? '장비 수정' : '장비 입고',
|
|
onSave: _controller.canSave && !_controller.isSaving ? _onSave : null,
|
|
onCancel: () => Navigator.of(context).pop(),
|
|
isLoading: _controller.isSaving,
|
|
child: Form(
|
|
key: _controller.formKey,
|
|
child: SingleChildScrollView(
|
|
padding: const EdgeInsets.only(bottom: 24),
|
|
child: Column(
|
|
children: [
|
|
_buildBasicFields(),
|
|
const SizedBox(height: 24),
|
|
_buildLocationSection(),
|
|
const SizedBox(height: 24),
|
|
_buildPurchaseSection(),
|
|
const SizedBox(height: 24),
|
|
_buildRemarkSection(),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
);
|
|
},
|
|
);
|
|
}
|
|
|
|
Widget _buildBasicFields() {
|
|
return ShadCard(
|
|
child: Padding(
|
|
padding: const EdgeInsets.all(16.0),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Text(
|
|
'기본 정보',
|
|
style: Theme.of(context).textTheme.titleMedium?.copyWith(
|
|
fontWeight: FontWeight.bold,
|
|
),
|
|
),
|
|
const SizedBox(height: 16),
|
|
|
|
// 장비 번호 (필수)
|
|
ShadInputFormField(
|
|
controller: _serialNumberController,
|
|
readOnly: _controller.isFieldReadOnly('serialNumber'),
|
|
placeholder: Text(_controller.isFieldReadOnly('serialNumber')
|
|
? (_controller.serialNumber.isNotEmpty ? _controller.serialNumber : '장비 번호 없음')
|
|
: '장비 번호를 입력하세요'),
|
|
label: Text(_controller.isFieldReadOnly('serialNumber')
|
|
? '장비 번호 * 🔒' : '장비 번호 *'),
|
|
validator: (value) {
|
|
if (value.trim().isEmpty ?? true) {
|
|
return '장비 번호는 필수입니다';
|
|
}
|
|
return null;
|
|
},
|
|
onChanged: _controller.isFieldReadOnly('serialNumber') ? null : (value) {
|
|
_controller.serialNumber = value.trim() ?? '';
|
|
setState(() {});
|
|
print('DEBUG [장비번호 입력] value: "$value", controller.serialNumber: "${_controller.serialNumber}"');
|
|
},
|
|
),
|
|
const SizedBox(height: 16),
|
|
|
|
// Vendor→Model cascade 선택기
|
|
EquipmentVendorModelSelector(
|
|
initialVendorId: _controller.vendorId,
|
|
initialModelId: _controller.modelsId,
|
|
onChanged: _controller.onVendorModelChanged,
|
|
isReadOnly: _controller.isFieldReadOnly('modelsId'),
|
|
),
|
|
const SizedBox(height: 16),
|
|
|
|
// 시리얼 번호 (선택)
|
|
ShadInputFormField(
|
|
controller: _serialNumberController,
|
|
readOnly: _controller.isFieldReadOnly('serialNumber'),
|
|
placeholder: Text(_controller.isFieldReadOnly('serialNumber')
|
|
? '수정불가' : '시리얼 번호를 입력하세요'),
|
|
label: Text(_controller.isFieldReadOnly('serialNumber')
|
|
? '시리얼 번호 🔒' : '시리얼 번호'),
|
|
onChanged: _controller.isFieldReadOnly('serialNumber') ? null : (value) {
|
|
_controller.serialNumber = value.trim() ?? '';
|
|
setState(() {});
|
|
print('DEBUG [시리얼번호 입력] value: "$value", controller.serialNumber: "${_controller.serialNumber}"');
|
|
},
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
|
|
Widget _buildLocationSection() {
|
|
return ShadCard(
|
|
child: Padding(
|
|
padding: const EdgeInsets.all(16.0),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Text(
|
|
'위치 정보',
|
|
style: Theme.of(context).textTheme.titleMedium?.copyWith(
|
|
fontWeight: FontWeight.bold,
|
|
),
|
|
),
|
|
const SizedBox(height: 16),
|
|
|
|
// 구매처 (드롭다운 전용)
|
|
ShadSelect<int>(
|
|
initialValue: _getValidCompanyId(),
|
|
placeholder: const Text('구매처를 선택하세요'),
|
|
options: _controller.companies.entries.map((entry) =>
|
|
ShadOption(
|
|
value: entry.key,
|
|
child: Text(entry.value),
|
|
)
|
|
).toList(),
|
|
selectedOptionBuilder: (context, value) {
|
|
// companies가 비어있거나 해당 value가 없는 경우 처리
|
|
if (_controller.companies.isEmpty) {
|
|
return const Text('로딩중...');
|
|
}
|
|
return Text(_controller.companies[value] ?? '선택하세요');
|
|
},
|
|
onChanged: (value) {
|
|
setState(() {
|
|
_controller.selectedCompanyId = value;
|
|
});
|
|
print('DEBUG [구매처 선택] value: $value, companies: ${_controller.companies.length}');
|
|
},
|
|
),
|
|
const SizedBox(height: 16),
|
|
|
|
// 입고지 (드롭다운 전용)
|
|
ShadSelect<int>(
|
|
initialValue: _getValidWarehouseId(),
|
|
placeholder: const Text('입고지를 선택하세요'),
|
|
options: _controller.warehouses.entries.map((entry) =>
|
|
ShadOption(
|
|
value: entry.key,
|
|
child: Text(entry.value),
|
|
)
|
|
).toList(),
|
|
selectedOptionBuilder: (context, value) {
|
|
// warehouses가 비어있거나 해당 value가 없는 경우 처리
|
|
if (_controller.warehouses.isEmpty) {
|
|
return const Text('로딩중...');
|
|
}
|
|
return Text(_controller.warehouses[value] ?? '선택하세요');
|
|
},
|
|
onChanged: (value) {
|
|
setState(() {
|
|
_controller.selectedWarehouseId = value;
|
|
});
|
|
print('DEBUG [입고지 선택] value: $value, warehouses: ${_controller.warehouses.length}');
|
|
},
|
|
),
|
|
const SizedBox(height: 16),
|
|
|
|
// 초기 재고 수량 (신규 등록 시에만 표시)
|
|
if (!_controller.isEditMode)
|
|
ShadInputFormField(
|
|
controller: _initialStockController,
|
|
label: const Text('초기 재고 수량 *'),
|
|
placeholder: const Text('입고할 수량을 입력하세요'),
|
|
description: const Text('장비 등록 시 자동으로 입고 처리됩니다'),
|
|
keyboardType: TextInputType.number,
|
|
inputFormatters: [
|
|
FilteringTextInputFormatter.digitsOnly,
|
|
],
|
|
validator: (value) {
|
|
if (value.isEmpty) {
|
|
return '재고 수량은 필수입니다';
|
|
}
|
|
final quantity = int.tryParse(value);
|
|
if (quantity == null || quantity <= 0) {
|
|
return '1개 이상의 수량을 입력하세요';
|
|
}
|
|
return null;
|
|
},
|
|
onChanged: (value) {
|
|
final quantity = int.tryParse(value) ?? 1;
|
|
_controller.initialStock = quantity;
|
|
},
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _buildPurchaseSection() {
|
|
return ShadCard(
|
|
child: Padding(
|
|
padding: const EdgeInsets.all(16.0),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Text(
|
|
'구매 정보',
|
|
style: Theme.of(context).textTheme.titleMedium?.copyWith(
|
|
fontWeight: FontWeight.bold,
|
|
),
|
|
),
|
|
const SizedBox(height: 16),
|
|
|
|
Row(
|
|
children: [
|
|
// 구매일
|
|
Expanded(
|
|
child: InkWell(
|
|
onTap: _controller.isFieldReadOnly('purchaseDate') ? null : () async {
|
|
final date = await showDatePicker(
|
|
context: context,
|
|
initialDate: _controller.purchaseDate ?? DateTime.now(),
|
|
firstDate: DateTime(2000),
|
|
lastDate: DateTime(2100),
|
|
);
|
|
if (date != null) {
|
|
setState(() {
|
|
_controller.purchaseDate = date;
|
|
});
|
|
}
|
|
},
|
|
child: Container(
|
|
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 10),
|
|
decoration: BoxDecoration(
|
|
border: Border.all(
|
|
color: _controller.isFieldReadOnly('purchaseDate')
|
|
? Colors.grey[300]!
|
|
: Theme.of(context).dividerColor,
|
|
),
|
|
borderRadius: BorderRadius.circular(6),
|
|
color: _controller.isFieldReadOnly('purchaseDate')
|
|
? Colors.grey[50]
|
|
: null,
|
|
),
|
|
child: Row(
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
children: [
|
|
Text(
|
|
_controller.purchaseDate != null
|
|
? '${_controller.purchaseDate!.year}-${_controller.purchaseDate!.month.toString().padLeft(2, '0')}-${_controller.purchaseDate!.day.toString().padLeft(2, '0')}'
|
|
: _controller.isFieldReadOnly('purchaseDate') ? '구매일 미설정' : '날짜 선택',
|
|
style: TextStyle(
|
|
color: _controller.isFieldReadOnly('purchaseDate')
|
|
? Colors.grey[600]
|
|
: null,
|
|
),
|
|
),
|
|
Icon(
|
|
Icons.calendar_today,
|
|
size: 16,
|
|
color: _controller.isFieldReadOnly('purchaseDate')
|
|
? Colors.grey[600] : null,
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
),
|
|
const SizedBox(width: 16),
|
|
|
|
// 구매 가격
|
|
Expanded(
|
|
child: ShadInputFormField(
|
|
controller: _purchasePriceController,
|
|
readOnly: _controller.isFieldReadOnly('purchasePrice'),
|
|
label: Text(_controller.isFieldReadOnly('purchasePrice')
|
|
? '구매 가격 🔒' : '구매 가격'),
|
|
placeholder: Text(_controller.isFieldReadOnly('purchasePrice')
|
|
? '수정불가' : '₩2,000,000'),
|
|
keyboardType: _controller.isFieldReadOnly('purchasePrice')
|
|
? null : TextInputType.number,
|
|
inputFormatters: _controller.isFieldReadOnly('purchasePrice')
|
|
? null : [CurrencyInputFormatter()], // 새로운 통화 포맷터
|
|
onChanged: (value) {
|
|
// 숫자만 추출하여 저장
|
|
final digitsOnly = value.replaceAll(RegExp(r'[^\d]'), '');
|
|
_controller.purchasePrice = int.tryParse(digitsOnly)?.toDouble();
|
|
},
|
|
),
|
|
),
|
|
],
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _buildRemarkSection() {
|
|
return ShadCard(
|
|
child: Padding(
|
|
padding: const EdgeInsets.all(16.0),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Text(
|
|
'비고',
|
|
style: Theme.of(context).textTheme.titleMedium?.copyWith(
|
|
fontWeight: FontWeight.bold,
|
|
),
|
|
),
|
|
const SizedBox(height: 16),
|
|
|
|
ShadInputFormField(
|
|
controller: _controller.remarkController,
|
|
label: const Text('비고'),
|
|
placeholder: const Text('비고사항을 입력하세요'),
|
|
maxLines: 3,
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
} |