feat: 라이선스 및 창고 관리 API 연동 구현

- 라이선스 관리 API 연동 완료
  - LicenseRemoteDataSource, LicenseService 구현
  - LicenseListController, LicenseFormController API 연동
  - 페이지네이션, 검색, 필터링 기능 추가
  - 라이선스 할당/해제 기능 구현

- 창고 관리 API 연동 완료
  - WarehouseRemoteDataSource, WarehouseService 구현
  - WarehouseLocationListController, WarehouseLocationFormController API 연동
  - 창고별 장비 조회 및 용량 관리 기능 추가

- DI 컨테이너에 새로운 서비스 등록
- API 통합 문서 업데이트 (전체 진행률 100% 달성)
This commit is contained in:
JiWoong Sul
2025-07-25 00:18:49 +09:00
parent 37f35ca68b
commit 8384423cf2
23 changed files with 7591 additions and 926 deletions

View File

@@ -1,782 +0,0 @@
import 'package:flutter/material.dart';
import 'package:get_it/get_it.dart';
import 'package:superport/models/equipment_unified_model.dart';
import 'package:superport/models/company_model.dart';
import 'package:superport/models/address_model.dart';
import 'package:superport/services/equipment_service.dart';
import 'package:superport/services/mock_data_service.dart';
import 'package:superport/core/errors/failures.dart';
// 장비 출고 폼의 상태 및 비즈니스 로직을 담당하는 컨트롤러
class EquipmentOutFormController extends ChangeNotifier {
final MockDataService dataService;
final EquipmentService _equipmentService = GetIt.instance<EquipmentService>();
final GlobalKey<FormState> formKey = GlobalKey<FormState>();
final TextEditingController remarkController = TextEditingController();
bool _isLoading = false;
String? _error;
bool _isSaving = false;
bool _useApi = true; // Feature flag
String? _errorMessage;
// Getters
bool get isLoading => _isLoading;
String? get error => _error;
bool get isSaving => _isSaving;
String? get errorMessage => _errorMessage;
// 상태 변수
bool isEditMode = false;
String manufacturer = '';
String name = '';
String category = '';
String subCategory = '';
String subSubCategory = '';
String serialNumber = '';
String barcode = '';
int quantity = 1;
DateTime _outDate = DateTime.now();
DateTime get outDate => _outDate;
set outDate(DateTime value) {
_outDate = value;
notifyListeners();
}
bool hasSerialNumber = false;
DateTime? inDate;
String returnType = '재입고';
DateTime _returnDate = DateTime.now();
DateTime get returnDate => _returnDate;
set returnDate(DateTime value) {
_returnDate = value;
notifyListeners();
}
bool hasManagers = false;
// 출고 유형(출고/대여/폐기) 상태 변수 추가
String _outType = '출고'; // 기본값은 '출고'
String get outType => _outType;
set outType(String value) {
_outType = value;
notifyListeners();
}
// 기존 필드 - 호환성을 위해 유지
String? _selectedCompany;
String? get selectedCompany =>
selectedCompanies.isNotEmpty ? selectedCompanies[0] : null;
set selectedCompany(String? value) {
if (selectedCompanies.isEmpty) {
selectedCompanies.add(value);
} else {
selectedCompanies[0] = value;
}
_selectedCompany = value;
}
String? _selectedManager;
String? get selectedManager =>
selectedManagersPerCompany.isNotEmpty
? selectedManagersPerCompany[0]
: null;
set selectedManager(String? value) {
if (selectedManagersPerCompany.isEmpty) {
selectedManagersPerCompany.add(value);
} else {
selectedManagersPerCompany[0] = value;
}
_selectedManager = value;
}
String? selectedLicense;
List<String> companies = [];
// 회사 및 지점 관련 데이터
List<CompanyBranchInfo> companiesWithBranches = [];
List<String> managers = [];
List<String> filteredManagers = [];
List<String> licenses = [];
// 출고 유형별 상태 코드 매핑
static const Map<String, String> outTypeStatusMap = {
'출고': 'O', // Out
'대여': 'R', // Rent
'폐기': 'D', // Disposal
};
// 출고 회사 목록 관리
List<String?> selectedCompanies = [null]; // 첫 번째 드롭다운을 위한 초기값
List<List<String>> availableCompaniesPerDropdown =
[]; // 각 드롭다운마다 사용 가능한 회사 목록
List<String?> selectedManagersPerCompany = [null]; // 각 드롭다운 회사별 선택된 담당자
List<List<String>> filteredManagersPerCompany = []; // 각 드롭다운 회사별 필터링된 담당자 목록
List<bool> hasManagersPerCompany = [false]; // 각 회사별 담당자 유무
// 입력 데이터
Equipment? selectedEquipment;
int? selectedEquipmentInId;
int? equipmentOutId;
List<Map<String, dynamic>>? _selectedEquipments;
EquipmentOutFormController({required this.dataService});
// 선택된 장비 정보 설정 (디버그용)
set selectedEquipments(List<Map<String, dynamic>>? equipments) {
debugPrint('설정된 장비 목록: ${equipments?.length ?? 0}');
if (equipments != null) {
for (var i = 0; i < equipments.length; i++) {
final equipment = equipments[i]['equipment'] as Equipment;
debugPrint('장비 $i: ${equipment.manufacturer} ${equipment.name}');
}
}
_selectedEquipments = equipments;
}
List<Map<String, dynamic>>? get selectedEquipments => _selectedEquipments;
// 드롭다운 데이터 로드
void loadDropdownData() {
final allCompanies = dataService.getAllCompanies();
// 회사와 지점 통합 목록 생성
companiesWithBranches = [];
companies = [];
for (var company in allCompanies) {
// 회사 자체 정보 추가
final companyType =
company.companyTypes.isNotEmpty
? companyTypeToString(company.companyTypes.first)
: '-';
final companyInfo = CompanyBranchInfo(
id: company.id,
name: "${company.name} (${companyType})",
originalName: company.name,
isMainCompany: true,
companyId: company.id,
branchId: null,
);
companiesWithBranches.add(companyInfo);
companies.add(companyInfo.name);
// 지점 정보 추가
if (company.branches != null && company.branches!.isNotEmpty) {
for (var branch in company.branches!) {
final branchInfo = CompanyBranchInfo(
id: branch.id,
name: "${company.name} ${branch.name}",
displayName: branch.name,
originalName: branch.name,
isMainCompany: false,
companyId: company.id,
branchId: branch.id,
parentCompanyName: company.name,
);
companiesWithBranches.add(branchInfo);
companies.add(branchInfo.name);
}
}
}
// 나머지 데이터 로드
final allUsers = dataService.getAllUsers();
managers = allUsers.map((user) => user.name).toList();
filteredManagers = managers;
final allLicenses = dataService.getAllLicenses();
licenses = allLicenses.map((license) => license.name).toList();
if (companies.isEmpty) companies.add('기타');
if (managers.isEmpty) managers.add('기타');
if (licenses.isEmpty) licenses.add('기타');
updateManagersState();
// 출고 회사 드롭다운 초기화
availableCompaniesPerDropdown = [List.from(companies)];
filteredManagersPerCompany = [List.from(managers)];
hasManagersPerCompany = [hasManagers];
// 디버그 정보 출력
debugPrint('드롭다운 데이터 로드 완료');
debugPrint('장비 목록: ${_selectedEquipments?.length ?? 0}');
debugPrint('회사 및 지점 목록: ${companiesWithBranches.length}');
// 수정 모드인 경우 기존 선택값 동기화
if (isEditMode && equipmentOutId != null) {
final equipmentOut = dataService.getEquipmentOutById(equipmentOutId!);
if (equipmentOut != null && equipmentOut.company != null) {
String companyName = '';
// 회사 이름 찾기
for (String company in companies) {
if (company.startsWith(equipmentOut.company!)) {
companyName = company;
break;
}
}
if (companyName.isNotEmpty) {
selectedCompanies[0] = companyName;
filterManagersByCompanyAtIndex(companyName, 0);
// 기존 담당자 설정
if (equipmentOut.manager != null) {
selectedManagersPerCompany[0] = equipmentOut.manager;
}
}
// 라이센스 설정
if (equipmentOut.license != null) {
selectedLicense = equipmentOut.license;
}
}
}
}
// 회사에 따라 담당자 목록 필터링
void filterManagersByCompany(String? companyName) {
if (companyName == null || companyName.isEmpty) {
filteredManagers = managers;
} else {
// 회사 또는 지점 이름에서 CompanyBranchInfo 찾기
CompanyBranchInfo? companyInfo = _findCompanyInfoByDisplayName(
companyName,
);
if (companyInfo != null && companyInfo.companyId != null) {
int companyId = companyInfo.companyId!;
final companyUsers =
dataService
.getAllUsers()
.where((user) => user.companyId == companyId)
.toList();
if (companyUsers.isNotEmpty) {
filteredManagers = companyUsers.map((user) => user.name).toList();
} else {
filteredManagers = ['없음'];
}
} else {
filteredManagers = ['없음'];
}
}
if (selectedManager != null &&
!filteredManagers.contains(selectedManager)) {
selectedManager =
filteredManagers.isNotEmpty ? filteredManagers[0] : null;
}
updateManagersState();
// 첫 번째 회사에 대한 담당자 목록과 동기화
if (filteredManagersPerCompany.isNotEmpty) {
filteredManagersPerCompany[0] = List.from(filteredManagers);
hasManagersPerCompany[0] = hasManagers;
if (selectedManagersPerCompany.isNotEmpty) {
selectedManagersPerCompany[0] = selectedManager;
}
}
}
// 특정 인덱스의 회사에 따라 담당자 목록 필터링
void filterManagersByCompanyAtIndex(String? companyName, int index) {
if (companyName == null || companyName.isEmpty) {
filteredManagersPerCompany[index] = managers;
} else {
// 회사 또는 지점 이름에서 CompanyBranchInfo 찾기
CompanyBranchInfo? companyInfo = _findCompanyInfoByDisplayName(
companyName,
);
if (companyInfo != null && companyInfo.companyId != null) {
int companyId = companyInfo.companyId!;
final companyUsers =
dataService
.getAllUsers()
.where((user) => user.companyId == companyId)
.toList();
if (companyUsers.isNotEmpty) {
filteredManagersPerCompany[index] =
companyUsers.map((user) => user.name).toList();
} else {
filteredManagersPerCompany[index] = ['없음'];
}
} else {
filteredManagersPerCompany[index] = ['없음'];
}
}
if (selectedManagersPerCompany[index] != null &&
!filteredManagersPerCompany[index].contains(
selectedManagersPerCompany[index],
)) {
selectedManagersPerCompany[index] =
filteredManagersPerCompany[index].isNotEmpty
? filteredManagersPerCompany[index][0]
: null;
}
updateManagersStateAtIndex(index);
// 첫 번째 회사인 경우 기존 필드와 동기화
if (index == 0) {
filteredManagers = List.from(filteredManagersPerCompany[0]);
hasManagers = hasManagersPerCompany[0];
_selectedManager = selectedManagersPerCompany[0];
}
}
// 담당자 있는지 상태 업데이트
void updateManagersState() {
hasManagers =
filteredManagers.isNotEmpty &&
!(filteredManagers.length == 1 && filteredManagers[0] == '없음');
}
// 특정 인덱스의 담당자 상태 업데이트
void updateManagersStateAtIndex(int index) {
hasManagersPerCompany[index] =
filteredManagersPerCompany[index].isNotEmpty &&
!(filteredManagersPerCompany[index].length == 1 &&
filteredManagersPerCompany[index][0] == '없음');
}
// 출고 회사 추가
void addCompany() {
// 이미 선택된 회사 제외한 리스트 생성
List<String> availableCompanies = List.from(companies);
for (String? company in selectedCompanies) {
if (company != null) {
availableCompanies.remove(company);
}
}
// 새 드롭다운 추가
selectedCompanies.add(null);
availableCompaniesPerDropdown.add(availableCompanies);
selectedManagersPerCompany.add(null);
filteredManagersPerCompany.add(List.from(managers));
hasManagersPerCompany.add(false);
}
// 가능한 회사 목록 업데이트
void updateAvailableCompanies() {
// 각 드롭다운에 대해 사용 가능한 회사 목록 업데이트
for (int i = 0; i < selectedCompanies.length; i++) {
List<String> availableCompanies = List.from(companies);
// 이미 선택된 회사 제외
for (int j = 0; j < selectedCompanies.length; j++) {
if (i != j && selectedCompanies[j] != null) {
availableCompanies.remove(selectedCompanies[j]);
}
}
availableCompaniesPerDropdown[i] = availableCompanies;
}
}
// 선택 장비로 초기화
void initializeWithSelectedEquipment(Equipment equipment) {
manufacturer = equipment.manufacturer;
name = equipment.name;
category = equipment.category;
subCategory = equipment.subCategory;
subSubCategory = equipment.subSubCategory;
serialNumber = equipment.serialNumber ?? '';
barcode = equipment.barcode ?? '';
quantity = equipment.quantity;
hasSerialNumber = serialNumber.isNotEmpty;
inDate = equipment.inDate;
remarkController.text = equipment.remark ?? '';
}
// 회사/지점 표시 이름을 통해 CompanyBranchInfo 객체 찾기
CompanyBranchInfo? _findCompanyInfoByDisplayName(String displayName) {
for (var info in companiesWithBranches) {
if (info.name == displayName) {
return info;
}
}
return null;
}
// 출고 정보 저장 (UI에서 호출)
Future<void> saveEquipmentOut(Function(String) onSuccess, Function(String) onError) async {
if (formKey.currentState?.validate() != true) {
onError('폼 유효성 검사 실패');
return;
}
formKey.currentState?.save();
_isSaving = true;
_error = null;
notifyListeners();
try {
// 선택된 회사가 없는지 확인
bool hasAnySelectedCompany = selectedCompanies.any(
(company) => company != null,
);
if (!hasAnySelectedCompany) {
onError('최소 하나의 출고 회사를 선택해주세요');
return;
}
// 기존 방식으로 첫 번째 회사 정보 처리
String? companyName;
if (selectedCompanies.isNotEmpty && selectedCompanies[0] != null) {
CompanyBranchInfo? companyInfo = _findCompanyInfoByDisplayName(
selectedCompanies[0]!,
);
if (companyInfo != null) {
companyName =
companyInfo.isMainCompany
? companyInfo
.originalName // 본사인 경우 회사 원래 이름
: "${companyInfo.originalName} (${companyInfo.branchId})"; // 지점인 경우 지점 정보 포함
} else {
companyName = selectedCompanies[0]!.replaceAll(
RegExp(r' \(.*\)\$'),
'',
);
}
} else {
onError('최소 하나의 출고 회사를 선택해주세요');
return;
}
if (_useApi) {
// API 호출 방식
if (isEditMode && equipmentOutId != null) {
// TODO: 출고 정보 업데이트 API 호출
throw UnimplementedError('Equipment out update API not implemented yet');
} else {
// 장비 출고 처리
if (selectedEquipments != null && selectedEquipments!.isNotEmpty) {
List<String> successfulOuts = [];
List<String> failedOuts = [];
for (var equipmentData in selectedEquipments!) {
final equipment = equipmentData['equipment'] as Equipment;
if (equipment.id != null) {
// 회사 ID 가져오기 - 현재는 목 데이터에서 찾기
CompanyBranchInfo? companyInfo = _findCompanyInfoByDisplayName(
selectedCompanies[0]!,
);
int? companyId = companyInfo?.companyId;
int? branchId = companyInfo?.branchId;
if (companyId == null) {
// 목 데이터에서 회사 ID 찾기
final company = dataService.getAllCompanies().firstWhere(
(c) => c.name == companyName,
orElse: () => Company(
id: 1, // 기본값 설정
name: companyName ?? '기타',
businessNumber: '',
address: '',
phone: '',
companyTypes: [],
),
);
companyId = company.id;
}
if (companyId != null) {
try {
await _equipmentService.equipmentOut(
equipmentId: equipment.id!,
quantity: equipment.quantity,
companyId: companyId,
branchId: branchId,
notes: '${remarkController.text.trim()}${outType != '출고' ? ' (${outType})' : ''}',
);
successfulOuts.add('${equipment.manufacturer} ${equipment.name}');
} catch (e) {
failedOuts.add('${equipment.manufacturer} ${equipment.name}: $e');
}
}
}
}
// 결과 메시지 생성
if (failedOuts.isEmpty) {
onSuccess('${successfulOuts.length}개 장비 출고 완료');
} else if (successfulOuts.isEmpty) {
onError('모든 장비 출고 실패:\n${failedOuts.join('\n')}');
} else {
onSuccess('${successfulOuts.length}개 성공, ${failedOuts.length}개 실패\n실패: ${failedOuts.join(', ')}');
}
} else {
onError('출고할 장비가 선택되지 않았습니다');
}
}
} else {
// Mock 데이터 사용
if (isEditMode && equipmentOutId != null) {
final equipmentOut = dataService.getEquipmentOutById(equipmentOutId!);
if (equipmentOut != null) {
final updatedEquipmentOut = EquipmentOut(
id: equipmentOut.id,
equipment: equipmentOut.equipment,
outDate: equipmentOut.outDate,
status: returnType == '재입고' ? 'I' : 'R',
company: companyName,
manager: equipmentOut.manager,
license: equipmentOut.license,
returnDate: returnDate,
returnType: returnType,
remark: remarkController.text.trim(),
);
dataService.updateEquipmentOut(updatedEquipmentOut);
onSuccess('장비 출고 상태 변경 완료');
} else {
onError('출고 정보를 찾을 수 없습니다');
}
} else {
if (selectedEquipments != null && selectedEquipments!.isNotEmpty) {
// 여러 회사에 각각 출고 처리
List<String> successCompanies = [];
// 선택된 모든 회사에 대해 출고 처리
for (int i = 0; i < selectedCompanies.length; i++) {
if (selectedCompanies[i] == null) continue;
CompanyBranchInfo? companyInfo = _findCompanyInfoByDisplayName(
selectedCompanies[i]!,
);
String curCompanyName;
if (companyInfo != null) {
curCompanyName =
companyInfo.isMainCompany
? companyInfo
.originalName // 본사인 경우 회사 원래 이름
: "${companyInfo.originalName} (${companyInfo.branchId})"; // 지점인 경우 지점 정보 포함
} else {
curCompanyName = selectedCompanies[i]!.replaceAll(
RegExp(r' \(.*\)\$'),
'',
);
}
String? curManager = selectedManagersPerCompany[i];
if (curManager == null || curManager == '없음') {
// 담당자 없는 회사는 건너뛰기
continue;
}
// 해당 회사에 모든 장비 출고 처리
for (final equipmentData in selectedEquipments!) {
final equipment = equipmentData['equipment'] as Equipment;
final equipmentInId = equipmentData['equipmentInId'] as int;
final newEquipmentOut = EquipmentOut(
equipment: equipment,
outDate: outDate,
company: curCompanyName,
manager: curManager,
license: selectedLicense,
remark: remarkController.text.trim(),
);
dataService.changeEquipmentStatus(equipmentInId, newEquipmentOut);
}
successCompanies.add(companyInfo?.name ?? curCompanyName);
}
if (successCompanies.isEmpty) {
onError('모든 회사에 담당자가 없어 출고 처리할 수 없습니다');
} else {
onSuccess('${successCompanies.join(", ")} 회사로 다중 장비 출고 처리 완료');
}
} else if (selectedEquipmentInId != null) {
final equipment = Equipment(
manufacturer: manufacturer,
name: name,
category: category,
subCategory: subCategory,
subSubCategory: subSubCategory,
serialNumber: (hasSerialNumber) ? serialNumber : null,
barcode: barcode.isNotEmpty ? barcode : null,
quantity: quantity,
inDate: inDate,
remark: remarkController.text.trim(),
);
// 선택된 모든 회사에 대해 출고 처리
List<String> successCompanies = [];
for (int i = 0; i < selectedCompanies.length; i++) {
if (selectedCompanies[i] == null) continue;
CompanyBranchInfo? companyInfo = _findCompanyInfoByDisplayName(
selectedCompanies[i]!,
);
String curCompanyName;
if (companyInfo != null) {
curCompanyName =
companyInfo.isMainCompany
? companyInfo
.originalName // 본사인 경우 회사 원래 이름
: "${companyInfo.originalName} (${companyInfo.branchId})"; // 지점인 경우 지점 정보 포함
} else {
curCompanyName = selectedCompanies[i]!.replaceAll(
RegExp(r' \(.*\)\$'),
'',
);
}
String? curManager = selectedManagersPerCompany[i];
if (curManager == null || curManager == '없음') {
// 담당자 없는 회사는 건너뛰기
continue;
}
final newEquipmentOut = EquipmentOut(
equipment: equipment,
outDate: outDate,
company: curCompanyName,
manager: curManager,
license: selectedLicense,
remark: remarkController.text.trim(),
);
dataService.changeEquipmentStatus(
selectedEquipmentInId!,
newEquipmentOut,
);
successCompanies.add(companyInfo?.name ?? curCompanyName);
break; // 한 장비는 한 회사에만 출고
}
if (successCompanies.isEmpty) {
onError('모든 회사에 담당자가 없어 출고 처리할 수 없습니다');
} else {
onSuccess('${successCompanies.join(", ")} 회사로 장비 출고 처리 완료');
}
} else {
final equipment = Equipment(
manufacturer: manufacturer,
name: name,
category: category,
subCategory: subCategory,
subSubCategory: subSubCategory,
serialNumber: null,
barcode: null,
quantity: 1,
inDate: inDate,
remark: remarkController.text.trim(),
);
// 선택된 모든 회사에 대해 출고 처리
List<String> successCompanies = [];
for (int i = 0; i < selectedCompanies.length; i++) {
if (selectedCompanies[i] == null) continue;
CompanyBranchInfo? companyInfo = _findCompanyInfoByDisplayName(
selectedCompanies[i]!,
);
String curCompanyName;
if (companyInfo != null) {
curCompanyName =
companyInfo.isMainCompany
? companyInfo
.originalName // 본사인 경우 회사 원래 이름
: "${companyInfo.originalName} (${companyInfo.branchId})"; // 지점인 경우 지점 정보 포함
} else {
curCompanyName = selectedCompanies[i]!.replaceAll(
RegExp(r' \(.*\)\$'),
'',
);
}
String? curManager = selectedManagersPerCompany[i];
if (curManager == null || curManager == '없음') {
// 담당자 없는 회사는 건너뛰기
continue;
}
final newEquipmentOut = EquipmentOut(
equipment: equipment,
outDate: outDate,
company: curCompanyName,
manager: curManager,
license: selectedLicense,
remark: remarkController.text.trim(),
);
dataService.addEquipmentOut(newEquipmentOut);
successCompanies.add(companyInfo?.name ?? curCompanyName);
}
if (successCompanies.isEmpty) {
onError('모든 회사에 담당자가 없어 출고 처리할 수 없습니다');
} else {
onSuccess('${successCompanies.join(", ")} 회사로 새 출고 장비 추가 완료');
}
}
} on Failure catch (e) {
_error = e.message;
onError(e.message);
} catch (e) {
_error = 'An unexpected error occurred: $e';
onError(_error!);
} finally {
_isSaving = false;
notifyListeners();
}
}
// 날짜 포맷 유틸리티
String formatDate(DateTime date) {
return '${date.year}-${date.month.toString().padLeft(2, '0')}-${date.day.toString().padLeft(2, '0')}';
}
// 에러 처리
void clearError() {
_error = null;
_errorMessage = null;
notifyListeners();
}
// API 사용 여부 토글 (테스트용)
void toggleApiUsage() {
_useApi = !_useApi;
notifyListeners();
}
@override
void dispose() {
remarkController.dispose();
super.dispose();
}
}
/// 회사 및 지점 정보를 저장하는 클래스
class CompanyBranchInfo {
final int? id;
final String name; // 표시용 이름 (회사명 + 지점명 또는 회사명 (유형))
final String originalName; // 원래 이름 (회사 본사명 또는 지점명)
final String? displayName; // UI에 표시할 이름 (주로 지점명)
final bool isMainCompany; // 본사인지 지점인지 구분
final int? companyId; // 회사 ID
final int? branchId; // 지점 ID
final String? parentCompanyName; // 부모 회사명 (지점인 경우)
CompanyBranchInfo({
required this.id,
required this.name,
required this.originalName,
this.displayName,
required this.isMainCompany,
required this.companyId,
required this.branchId,
this.parentCompanyName,
});
}

View File

@@ -1,57 +1,186 @@
import 'package:flutter/material.dart';
import 'package:get_it/get_it.dart';
import 'package:superport/models/license_model.dart';
import 'package:superport/services/license_service.dart';
import 'package:superport/services/mock_data_service.dart';
// 라이센스 폼의 상태 및 비즈니스 로직을 담당하는 컨트롤러
class LicenseFormController {
final MockDataService dataService;
class LicenseFormController extends ChangeNotifier {
final bool useApi;
final MockDataService? mockDataService;
late final LicenseService _licenseService;
final GlobalKey<FormState> formKey = GlobalKey<FormState>();
bool isEditMode = false;
int? licenseId;
String name = '';
int durationMonths = 12; // 기본값: 12개월
String visitCycle = '미방문'; // 기본값: 미방문
bool _isEditMode = false;
int? _licenseId;
License? _originalLicense;
bool _isLoading = false;
String? _error;
bool _isSaving = false;
LicenseFormController({required this.dataService, this.licenseId});
// 폼 필드 값
String _name = '';
int _companyId = 1;
int _durationMonths = 12; // 기본값: 12개월
String _visitCycle = '미방문'; // 기본값: 미방문
LicenseFormController({
this.useApi = true,
this.mockDataService,
int? licenseId,
}) {
if (useApi && GetIt.instance.isRegistered<LicenseService>()) {
_licenseService = GetIt.instance<LicenseService>();
}
if (licenseId != null) {
_licenseId = licenseId;
_isEditMode = true;
loadLicense();
}
}
// Getters
bool get isEditMode => _isEditMode;
int? get licenseId => _licenseId;
License? get originalLicense => _originalLicense;
bool get isLoading => _isLoading;
String? get error => _error;
bool get isSaving => _isSaving;
String get name => _name;
int get companyId => _companyId;
int get durationMonths => _durationMonths;
String get visitCycle => _visitCycle;
// Setters
void setName(String value) {
_name = value;
notifyListeners();
}
void setCompanyId(int value) {
_companyId = value;
notifyListeners();
}
void setDurationMonths(int value) {
_durationMonths = value;
notifyListeners();
}
void setVisitCycle(String value) {
_visitCycle = value;
notifyListeners();
}
// 라이센스 정보 로드 (수정 모드)
void loadLicense() {
if (licenseId == null) return;
final license = dataService.getLicenseById(licenseId!);
if (license != null) {
name = license.name;
durationMonths = license.durationMonths;
visitCycle = license.visitCycle;
Future<void> loadLicense() async {
if (_licenseId == null) return;
_isLoading = true;
_error = null;
notifyListeners();
try {
if (useApi && GetIt.instance.isRegistered<LicenseService>()) {
_originalLicense = await _licenseService.getLicenseById(_licenseId!);
} else {
_originalLicense = mockDataService?.getLicenseById(_licenseId!);
}
if (_originalLicense != null) {
_name = _originalLicense!.name;
_companyId = _originalLicense!.companyId;
_durationMonths = _originalLicense!.durationMonths;
_visitCycle = _originalLicense!.visitCycle;
}
} catch (e) {
_error = e.toString();
} finally {
_isLoading = false;
notifyListeners();
}
}
// 라이센스 저장 (UI에서 호출)
void saveLicense(Function() onSuccess) {
if (formKey.currentState?.validate() != true) return;
// 라이센스 저장
Future<bool> saveLicense() async {
if (formKey.currentState?.validate() != true) return false;
formKey.currentState?.save();
if (isEditMode && licenseId != null) {
final license = dataService.getLicenseById(licenseId!);
if (license != null) {
final updatedLicense = License(
id: license.id,
companyId: license.companyId,
name: name,
durationMonths: durationMonths,
visitCycle: visitCycle,
);
dataService.updateLicense(updatedLicense);
}
} else {
// 라이센스 추가 시 임시 회사 ID 사용 또는 나중에 설정하도록 변경
final newLicense = License(
companyId: 1, // 기본값 또는 필요에 따라 수정
name: name,
durationMonths: durationMonths,
visitCycle: visitCycle,
_isSaving = true;
_error = null;
notifyListeners();
try {
final license = License(
id: _isEditMode ? _licenseId : null,
companyId: _companyId,
name: _name,
durationMonths: _durationMonths,
visitCycle: _visitCycle,
);
dataService.addLicense(newLicense);
if (useApi && GetIt.instance.isRegistered<LicenseService>()) {
if (_isEditMode) {
await _licenseService.updateLicense(license);
} else {
await _licenseService.createLicense(license);
}
} else {
if (_isEditMode) {
mockDataService?.updateLicense(license);
} else {
mockDataService?.addLicense(license);
}
}
return true;
} catch (e) {
_error = e.toString();
notifyListeners();
return false;
} finally {
_isSaving = false;
notifyListeners();
}
onSuccess();
}
// 폼 초기화
void resetForm() {
_name = '';
_companyId = 1;
_durationMonths = 12;
_visitCycle = '미방문';
_error = null;
formKey.currentState?.reset();
notifyListeners();
}
// 유효성 검사
String? validateName(String? value) {
if (value == null || value.isEmpty) {
return '라이선스명을 입력해주세요';
}
if (value.length < 2) {
return '라이선스명은 2자 이상이어야 합니다';
}
return null;
}
String? validateDuration(String? value) {
if (value == null || value.isEmpty) {
return '계약 기간을 입력해주세요';
}
final duration = int.tryParse(value);
if (duration == null || duration < 1) {
return '유효한 계약 기간을 입력해주세요';
}
return null;
}
@override
void dispose() {
super.dispose();
}
}

View File

@@ -1,21 +1,227 @@
import 'package:flutter/material.dart';
import 'package:get_it/get_it.dart';
import 'package:superport/models/license_model.dart';
import 'package:superport/services/license_service.dart';
import 'package:superport/services/mock_data_service.dart';
// 라이센스 목록 화면의 상태 및 비즈니스 로직을 담당하는 컨트롤러
class LicenseListController {
final MockDataService dataService;
List<License> licenses = [];
class LicenseListController extends ChangeNotifier {
final bool useApi;
final MockDataService? mockDataService;
late final LicenseService _licenseService;
List<License> _licenses = [];
List<License> _filteredLicenses = [];
bool _isLoading = false;
String? _error;
String _searchQuery = '';
int _currentPage = 1;
final int _pageSize = 20;
bool _hasMore = true;
int _total = 0;
LicenseListController({required this.dataService});
// 필터 옵션
int? _selectedCompanyId;
bool? _isActive;
String? _licenseType;
LicenseListController({this.useApi = true, this.mockDataService}) {
if (useApi && GetIt.instance.isRegistered<LicenseService>()) {
_licenseService = GetIt.instance<LicenseService>();
}
}
// Getters
List<License> get licenses => _filteredLicenses;
bool get isLoading => _isLoading;
String? get error => _error;
String get searchQuery => _searchQuery;
int get currentPage => _currentPage;
bool get hasMore => _hasMore;
int get total => _total;
int? get selectedCompanyId => _selectedCompanyId;
bool? get isActive => _isActive;
String? get licenseType => _licenseType;
// 데이터 로드
void loadData() {
licenses = dataService.getAllLicenses();
Future<void> loadData({bool isInitialLoad = true}) async {
if (_isLoading) return;
_isLoading = true;
_error = null;
if (isInitialLoad) {
_currentPage = 1;
_licenses.clear();
_hasMore = true;
}
notifyListeners();
try {
if (useApi && GetIt.instance.isRegistered<LicenseService>()) {
// API 사용
final fetchedLicenses = await _licenseService.getLicenses(
page: _currentPage,
perPage: _pageSize,
isActive: _isActive,
companyId: _selectedCompanyId,
licenseType: _licenseType,
);
if (isInitialLoad) {
_licenses = fetchedLicenses;
} else {
_licenses.addAll(fetchedLicenses);
}
_hasMore = fetchedLicenses.length >= _pageSize;
// 전체 개수 조회
_total = await _licenseService.getTotalLicenses(
isActive: _isActive,
companyId: _selectedCompanyId,
licenseType: _licenseType,
);
} else {
// Mock 데이터 사용
final allLicenses = mockDataService?.getAllLicenses() ?? [];
// 필터링 적용
var filtered = allLicenses;
if (_selectedCompanyId != null) {
filtered = filtered.where((l) => l.companyId == _selectedCompanyId).toList();
}
// 페이지네이션 적용
final startIndex = (_currentPage - 1) * _pageSize;
final endIndex = startIndex + _pageSize;
if (startIndex < filtered.length) {
final pageLicenses = filtered.sublist(
startIndex,
endIndex > filtered.length ? filtered.length : endIndex,
);
if (isInitialLoad) {
_licenses = pageLicenses;
} else {
_licenses.addAll(pageLicenses);
}
_hasMore = endIndex < filtered.length;
} else {
_hasMore = false;
}
_total = filtered.length;
}
_applySearchFilter();
if (!isInitialLoad) {
_currentPage++;
}
} catch (e) {
_error = e.toString();
} finally {
_isLoading = false;
notifyListeners();
}
}
// 다음 페이지 로드
Future<void> loadNextPage() async {
if (!_hasMore || _isLoading) return;
_currentPage++;
await loadData(isInitialLoad: false);
}
// 검색
void search(String query) {
_searchQuery = query;
_applySearchFilter();
notifyListeners();
}
// 검색 필터 적용
void _applySearchFilter() {
if (_searchQuery.isEmpty) {
_filteredLicenses = List.from(_licenses);
} else {
_filteredLicenses = _licenses.where((license) {
return license.name.toLowerCase().contains(_searchQuery.toLowerCase());
}).toList();
}
}
// 필터 설정
void setFilters({
int? companyId,
bool? isActive,
String? licenseType,
}) {
_selectedCompanyId = companyId;
_isActive = isActive;
_licenseType = licenseType;
loadData();
}
// 필터 초기화
void clearFilters() {
_selectedCompanyId = null;
_isActive = null;
_licenseType = null;
_searchQuery = '';
loadData();
}
// 라이센스 삭제
void deleteLicense(int id) {
dataService.deleteLicense(id);
loadData();
Future<void> deleteLicense(int id) async {
try {
if (useApi && GetIt.instance.isRegistered<LicenseService>()) {
await _licenseService.deleteLicense(id);
} else {
mockDataService?.deleteLicense(id);
}
// 목록에서 제거
_licenses.removeWhere((l) => l.id == id);
_applySearchFilter();
_total--;
notifyListeners();
} catch (e) {
_error = e.toString();
notifyListeners();
}
}
// 새로고침
Future<void> refresh() async {
await loadData();
}
// 만료 예정 라이선스 조회
Future<List<License>> getExpiringLicenses({int days = 30}) async {
try {
if (useApi && GetIt.instance.isRegistered<LicenseService>()) {
return await _licenseService.getExpiringLicenses(days: days);
} else {
// Mock 데이터에서 만료 예정 라이선스 필터링
final now = DateTime.now();
final allLicenses = mockDataService?.getAllLicenses() ?? [];
return allLicenses.where((license) {
// Mock 데이터는 만료일이 없으므로 임의로 계산
final expiryDate = now.add(Duration(days: license.durationMonths * 30));
final daysUntilExpiry = expiryDate.difference(now).inDays;
return daysUntilExpiry > 0 && daysUntilExpiry <= days;
}).toList();
}
} catch (e) {
_error = e.toString();
notifyListeners();
return [];
}
}
}

View File

@@ -1,10 +1,16 @@
import 'package:flutter/material.dart';
import 'package:get_it/get_it.dart';
import 'package:superport/models/warehouse_location_model.dart';
import 'package:superport/models/address_model.dart';
import 'package:superport/services/warehouse_service.dart';
import 'package:superport/services/mock_data_service.dart';
/// 입고지 폼 상태 및 저장/수정 로직을 담당하는 컨트롤러
class WarehouseLocationFormController {
class WarehouseLocationFormController extends ChangeNotifier {
final bool useApi;
final MockDataService? mockDataService;
late final WarehouseService _warehouseService;
/// 폼 키
final GlobalKey<FormState> formKey = GlobalKey<FormState>();
@@ -15,74 +21,157 @@ class WarehouseLocationFormController {
final TextEditingController remarkController = TextEditingController();
/// 주소 정보
Address address = const Address();
Address _address = const Address();
/// 저장 중 여부
bool isSaving = false;
bool _isSaving = false;
/// 수정 모드 여부
bool isEditMode = false;
bool _isEditMode = false;
/// 입고지 id (수정 모드)
int? id;
int? _id;
/// 로딩 상태
bool _isLoading = false;
/// 에러 메시지
String? _error;
/// 원본 창고 위치 (수정 모드)
WarehouseLocation? _originalLocation;
WarehouseLocationFormController({
this.useApi = true,
this.mockDataService,
int? locationId,
}) {
if (useApi && GetIt.instance.isRegistered<WarehouseService>()) {
_warehouseService = GetIt.instance<WarehouseService>();
}
if (locationId != null) {
initialize(locationId);
}
}
// Getters
Address get address => _address;
bool get isSaving => _isSaving;
bool get isEditMode => _isEditMode;
int? get id => _id;
bool get isLoading => _isLoading;
String? get error => _error;
WarehouseLocation? get originalLocation => _originalLocation;
/// 기존 데이터 세팅 (수정 모드)
void initialize(int? locationId) {
id = locationId;
if (id != null) {
final location = MockDataService().getWarehouseLocationById(id!);
if (location != null) {
isEditMode = true;
nameController.text = location.name;
address = location.address;
remarkController.text = location.remark ?? '';
Future<void> initialize(int locationId) async {
_id = locationId;
_isEditMode = true;
_isLoading = true;
_error = null;
notifyListeners();
try {
if (useApi && GetIt.instance.isRegistered<WarehouseService>()) {
_originalLocation = await _warehouseService.getWarehouseLocationById(locationId);
} else {
_originalLocation = mockDataService?.getWarehouseLocationById(locationId);
}
if (_originalLocation != null) {
nameController.text = _originalLocation!.name;
_address = _originalLocation!.address;
remarkController.text = _originalLocation!.remark ?? '';
}
} catch (e) {
_error = e.toString();
} finally {
_isLoading = false;
notifyListeners();
}
}
/// 주소 변경 처리
void updateAddress(Address newAddress) {
address = newAddress;
_address = newAddress;
notifyListeners();
}
/// 저장 처리 (추가/수정)
Future<bool> save(BuildContext context) async {
Future<bool> save() async {
if (!formKey.currentState!.validate()) return false;
isSaving = true;
if (isEditMode) {
// 수정
MockDataService().updateWarehouseLocation(
WarehouseLocation(
id: id!,
name: nameController.text.trim(),
address: address,
remark: remarkController.text.trim(),
),
);
} else {
// 추가
MockDataService().addWarehouseLocation(
WarehouseLocation(
id: 0,
name: nameController.text.trim(),
address: address,
remark: remarkController.text.trim(),
),
_isSaving = true;
_error = null;
notifyListeners();
try {
final location = WarehouseLocation(
id: _isEditMode ? _id! : 0,
name: nameController.text.trim(),
address: _address,
remark: remarkController.text.trim().isEmpty ? null : remarkController.text.trim(),
);
if (useApi && GetIt.instance.isRegistered<WarehouseService>()) {
if (_isEditMode) {
await _warehouseService.updateWarehouseLocation(location);
} else {
await _warehouseService.createWarehouseLocation(location);
}
} else {
if (_isEditMode) {
mockDataService?.updateWarehouseLocation(location);
} else {
mockDataService?.addWarehouseLocation(location);
}
}
return true;
} catch (e) {
_error = e.toString();
notifyListeners();
return false;
} finally {
_isSaving = false;
notifyListeners();
}
isSaving = false;
Navigator.pop(context, true);
return true;
}
/// 취소 처리
void cancel(BuildContext context) {
Navigator.pop(context, false);
/// 폼 초기화
void resetForm() {
nameController.clear();
remarkController.clear();
_address = const Address();
_error = null;
formKey.currentState?.reset();
notifyListeners();
}
/// 유효성 검사
String? validateName(String? value) {
if (value == null || value.isEmpty) {
return '입고지명을 입력해주세요';
}
if (value.length < 2) {
return '입고지명은 2자 이상이어야 합니다';
}
return null;
}
String? validateAddress() {
if (_address.isEmpty) {
return '주소를 입력해주세요';
}
return null;
}
/// 컨트롤러 해제
@override
void dispose() {
nameController.dispose();
remarkController.dispose();
super.dispose();
}
}

View File

@@ -1,36 +1,246 @@
import 'package:flutter/material.dart';
import 'package:get_it/get_it.dart';
import 'package:superport/models/warehouse_location_model.dart';
import 'package:superport/services/warehouse_service.dart';
import 'package:superport/services/mock_data_service.dart';
/// 입고지 리스트 상태 및 CRUD만 담당하는 컨트롤러 클래스 (SRP 적용)
/// UI, 네비게이션, 다이얼로그 등은 포함하지 않음
/// 향후 서비스/리포지토리 DI 구조로 확장 가능
class WarehouseLocationListController {
/// 입고지 데이터 서비스 (mock)
final MockDataService _dataService = MockDataService();
class WarehouseLocationListController extends ChangeNotifier {
final bool useApi;
final MockDataService? mockDataService;
late final WarehouseService _warehouseService;
List<WarehouseLocation> _warehouseLocations = [];
List<WarehouseLocation> _filteredLocations = [];
bool _isLoading = false;
String? _error;
String _searchQuery = '';
int _currentPage = 1;
final int _pageSize = 20;
bool _hasMore = true;
int _total = 0;
/// 전체 입고지 목록
List<WarehouseLocation> warehouseLocations = [];
// 필터 옵션
bool? _isActive;
WarehouseLocationListController({this.useApi = true, this.mockDataService}) {
if (useApi && GetIt.instance.isRegistered<WarehouseService>()) {
_warehouseService = GetIt.instance<WarehouseService>();
}
}
// Getters
List<WarehouseLocation> get warehouseLocations => _filteredLocations;
bool get isLoading => _isLoading;
String? get error => _error;
String get searchQuery => _searchQuery;
int get currentPage => _currentPage;
bool get hasMore => _hasMore;
int get total => _total;
bool? get isActive => _isActive;
/// 데이터 로드
void loadWarehouseLocations() {
warehouseLocations = _dataService.getAllWarehouseLocations();
Future<void> loadWarehouseLocations({bool isInitialLoad = true}) async {
if (_isLoading) return;
_isLoading = true;
_error = null;
if (isInitialLoad) {
_currentPage = 1;
_warehouseLocations.clear();
_hasMore = true;
}
notifyListeners();
try {
if (useApi && GetIt.instance.isRegistered<WarehouseService>()) {
// API 사용
final fetchedLocations = await _warehouseService.getWarehouseLocations(
page: _currentPage,
perPage: _pageSize,
isActive: _isActive,
);
if (isInitialLoad) {
_warehouseLocations = fetchedLocations;
} else {
_warehouseLocations.addAll(fetchedLocations);
}
_hasMore = fetchedLocations.length >= _pageSize;
// 전체 개수 조회
_total = await _warehouseService.getTotalWarehouseLocations(
isActive: _isActive,
);
} else {
// Mock 데이터 사용
final allLocations = mockDataService?.getAllWarehouseLocations() ?? [];
// 필터링 적용
var filtered = allLocations;
if (_isActive != null) {
// Mock 데이터에는 isActive 필드가 없으므로 모두 활성으로 처리
filtered = _isActive! ? allLocations : [];
}
// 페이지네이션 적용
final startIndex = (_currentPage - 1) * _pageSize;
final endIndex = startIndex + _pageSize;
if (startIndex < filtered.length) {
final pageLocations = filtered.sublist(
startIndex,
endIndex > filtered.length ? filtered.length : endIndex,
);
if (isInitialLoad) {
_warehouseLocations = pageLocations;
} else {
_warehouseLocations.addAll(pageLocations);
}
_hasMore = endIndex < filtered.length;
} else {
_hasMore = false;
}
_total = filtered.length;
}
_applySearchFilter();
if (!isInitialLoad) {
_currentPage++;
}
} catch (e) {
_error = e.toString();
} finally {
_isLoading = false;
notifyListeners();
}
}
// 다음 페이지 로드
Future<void> loadNextPage() async {
if (!_hasMore || _isLoading) return;
_currentPage++;
await loadWarehouseLocations(isInitialLoad: false);
}
// 검색
void search(String query) {
_searchQuery = query;
_applySearchFilter();
notifyListeners();
}
// 검색 필터 적용
void _applySearchFilter() {
if (_searchQuery.isEmpty) {
_filteredLocations = List.from(_warehouseLocations);
} else {
_filteredLocations = _warehouseLocations.where((location) {
return location.name.toLowerCase().contains(_searchQuery.toLowerCase()) ||
location.address.toString().toLowerCase().contains(_searchQuery.toLowerCase());
}).toList();
}
}
// 필터 설정
void setFilters({bool? isActive}) {
_isActive = isActive;
loadWarehouseLocations();
}
// 필터 초기화
void clearFilters() {
_isActive = null;
_searchQuery = '';
loadWarehouseLocations();
}
/// 입고지 추가
void addWarehouseLocation(WarehouseLocation location) {
_dataService.addWarehouseLocation(location);
loadWarehouseLocations();
Future<void> addWarehouseLocation(WarehouseLocation location) async {
try {
if (useApi && GetIt.instance.isRegistered<WarehouseService>()) {
await _warehouseService.createWarehouseLocation(location);
} else {
mockDataService?.addWarehouseLocation(location);
}
// 목록 새로고침
await loadWarehouseLocations();
} catch (e) {
_error = e.toString();
notifyListeners();
}
}
/// 입고지 수정
void updateWarehouseLocation(WarehouseLocation location) {
_dataService.updateWarehouseLocation(location);
loadWarehouseLocations();
Future<void> updateWarehouseLocation(WarehouseLocation location) async {
try {
if (useApi && GetIt.instance.isRegistered<WarehouseService>()) {
await _warehouseService.updateWarehouseLocation(location);
} else {
mockDataService?.updateWarehouseLocation(location);
}
// 목록에서 업데이트
final index = _warehouseLocations.indexWhere((l) => l.id == location.id);
if (index != -1) {
_warehouseLocations[index] = location;
_applySearchFilter();
notifyListeners();
}
} catch (e) {
_error = e.toString();
notifyListeners();
}
}
/// 입고지 삭제
void deleteWarehouseLocation(int id) {
_dataService.deleteWarehouseLocation(id);
loadWarehouseLocations();
Future<void> deleteWarehouseLocation(int id) async {
try {
if (useApi && GetIt.instance.isRegistered<WarehouseService>()) {
await _warehouseService.deleteWarehouseLocation(id);
} else {
mockDataService?.deleteWarehouseLocation(id);
}
// 목록에서 제거
_warehouseLocations.removeWhere((l) => l.id == id);
_applySearchFilter();
_total--;
notifyListeners();
} catch (e) {
_error = e.toString();
notifyListeners();
}
}
// 새로고침
Future<void> refresh() async {
await loadWarehouseLocations();
}
// 사용 중인 창고 위치 조회
Future<List<WarehouseLocation>> getInUseWarehouseLocations() async {
try {
if (useApi && GetIt.instance.isRegistered<WarehouseService>()) {
return await _warehouseService.getInUseWarehouseLocations();
} else {
// Mock 데이터에서는 모든 창고가 사용 중으로 간주
return mockDataService?.getAllWarehouseLocations() ?? [];
}
} catch (e) {
_error = e.toString();
notifyListeners();
return [];
}
}
}