ocr 등록 수입 추가

This commit is contained in:
주수현
2025-07-03 15:06:28 +09:00
parent 5d058f9683
commit 1e643c3c52
30 changed files with 715 additions and 65 deletions

View File

@@ -214,21 +214,9 @@
</div>
<div class="component-segment">
<div class="component-file">
<div class="file-list">
<div class="file-item">
<img class="profile-image-add" src="../../assets/images/acco/file_sample.png" alt="이미지샘플" />
<button type="button" class="btn-delete"><span class="hide-txt">삭제</span></button>
</div>
<div class="file-item">
<img class="profile-image-add" src="../../assets/images/acco/file_sample.png" alt="이미지샘플" />
<button type="button" class="btn-delete"><span class="hide-txt">삭제</span></button>
</div>
<div class="file-item">
<img class="profile-image-add" src="../../assets/images/acco/file_sample.png" alt="이미지샘플" />
<button type="button" class="btn-delete"><span class="hide-txt">삭제</span></button>
</div>
<div class="file-list" id="file-upload-container">
</div>
<button type="button" class="btn btn-m btn-link btn-full">
<button type="button" class="btn btn-m btn-link btn-full" onclick="modalControl.addFileInput();">
<i class="ico-add ico-small" aria-hidden="true"></i>
<span class="btn-txt">사진 첨부</span>
</button>
@@ -246,7 +234,7 @@
<div class="component-btns">
<div class="btns-row">
<!-- 버튼비활성 스타일 속성에 disabled 추가 -->
<button type="button" class="btn btn-full btn-l btn-primary">
<button type="button" onclick="modalControl.onSubmitReceiptFile();" class="btn btn-full btn-l btn-primary">
<span class="btn-txt">등록</span>
</button>
</div>

View File

@@ -162,6 +162,8 @@
create: /*[[|/api/record/income/${reqVO.searchMonth}/${reqVO.groupNo}/form/{id}|]]*/'createUrl',
remove: /*[[|/api/record/income/${reqVO.searchMonth}/${reqVO.groupNo}/remove|]]*/'removeUrl',
reset: /*[[|/api/record/income/${reqVO.searchMonth}/${reqVO.groupNo}/reset|]]*/'resetUrl',
receipt: /*[[|/api/record/receipt|]]*/'receiptUrl',
receiptCreate: /*[[|/api/record/income/receipt/${reqVO.searchMonth}/${reqVO.groupNo}/form/{id}|]]*/'receiptCreateUrl',
},
modal: {
// 등록 버튼
@@ -179,7 +181,7 @@
krip.daily.baseBindUI();
},
};
let uploadedReceipt =[];
let modalControl = {
_bindModelUI() {
const $select1 = document.querySelector('#cropCode');
@@ -265,6 +267,225 @@
},
);
},
addFileInput() {
// 새로운 input type="file" 요소 생성
const fileInput = document.createElement('input');
fileInput.type = 'file';
fileInput.accept = 'image/*';
fileInput.style.display = 'none';
fileInput.name = 'file';
// 파일 선택 시 이벤트 처리
fileInput.addEventListener('change', (event) => {
const files = event.target.files;
const previewContainer = $('#file-upload-container');
if (files.length === 0) {
const para = document.createElement('p');
para.textContent = '선택된 파일이 없습니다.';
para.classList.add('error');
previewContainer.append(para);
return;
}
Array.from(files).forEach(file => {
if (!file.type.startsWith('image/')) {
const para = document.createElement('p');
para.textContent = `파일 ${file.name}: 유효한 이미지 파일이 아닙니다.`;
para.classList.add('error');
previewContainer.append(para);
return;
}
let formData = new FormData();
formData.append('file', file);
ajaxUtil._showWaiting();
$.ajax({
method: 'POST',
url: '/api/record/receipt',
data: formData,
contentType: false,
processData: false,
enctype: 'multipart/form-data',
beforeSend: function(xhr) {
xhr.setRequestHeader(
$("meta[name='_csrf_header']").attr("content"),
$("meta[name='_csrf']").attr("content")
);
},
success(res) {
// 썸네일 생성
const imgSrc = URL.createObjectURL(file);
const fileItemId = `file-item-${Math.random().toString(36).substr(2, 9)}`;
const fileItem = $(`
<div class="file-item" id="${fileItemId}">
<img class="profile-image-add" src="${imgSrc}" alt="${file.name}"/>
<button type="button" class="btn-delete"><span class="hide-txt">삭제</span></button>
</div>
`);
previewContainer.append(fileItem);
// 파일 정보와 서버 응답 저장
uploadedReceipt.push({
fileId: fileItemId,
file: file,
receiptResVO: res
});
// 삭제 버튼 이벤트 추가
fileItem.find('.btn-delete').on('click', function() {
modalControl.deleteFile(fileItemId);
fileItem.remove()
});
ajaxUtil._hideWaiting();
},
error: function(xhr) {
const resJson = xhr.responseJSON || {};
const errorMessage = resJson.error || `HTTP ${xhr.status}: 요청 처리 중 오류가 발생했습니다.`;
console.error('영수증 업로드 실패:', xhr, errorMessage);
etUI.dialog.alert({
title: '영수증 업로드 실패',
message: '정상적으로 인식되지 않은 사진이 있습니다. 정확한 사진을 첨부해주세요.',
type: 'alert',
callback: function () {
fileInput.value = '';
},
});
ajaxUtil._hideWaiting();
}
});
});
});
// 입력 필드 초기화
fileInput.value = '';
const container = document.getElementById('file-upload-container');
container.appendChild(fileInput);
// 파일 선택 창 열기
fileInput.click();
},
deleteFile(fileItemId) {
const index = uploadedReceipt.findIndex(item => item.fileId === fileItemId);
if (index !== -1) {
URL.revokeObjectURL(uploadedReceipt[index].imgSrc); // 메모리 해제
$(`#${fileItemId}`).remove();
uploadedReceipt.splice(index, 1);
}
},
onSubmitReceiptFile(){
const receiptDataList = uploadedReceipt;
if (!receiptDataList.length) {
etUI.dialog.alert({
title: '등록 실패',
message: '등록할 영수증 데이터가 없습니다.',
type: 'alert',
callback: function () {
},
});
return;
}
const f = document.reqModalForm;
let formData = new FormData();
const reqVOList = receiptDataList.map(item => ({
file: { name: item.file.name },
fileUploadType: 'INCOME',
ocr: item.receiptResVO
}));
receiptDataList.forEach(item => {
formData.append('file', item.file);
});
formData.append('reqVO', new Blob([JSON.stringify(reqVOList)], { type: 'application/json' }));
ajaxUtil._showWaiting();
console.log("receiptDataList",receiptDataList);
$.ajax({
method: 'POST',
url: ENV.httpUrl.receiptCreate.replace('{id}', f.id.value ? f.id.value : '0'),
data: formData,
contentType: false, // * 중요 *
processData: false, // * 중요 *
enctype: 'multipart/form-data', // * 중요 *
beforeSend: function (xhr) {
xhr.setRequestHeader(
$("meta[name='_csrf_header']").attr("content"),
$("meta[name='_csrf']").attr("content"));
},
success(res) {
etUI.dialog.alert({
title: '등록',
message: '영수증이 성공적으로 등록되었습니다.',
type: 'alert',
callback: function () {
// 초기화
uploadedReceipt.forEach(item => URL.revokeObjectURL(item.imgSrc));
uploadedReceipt.length = 0;
$('#file-upload-container').empty();
location.reload();
},
});
},
statusCode: {
400: function (res) {
const resJson = res.responseJSON || {};
const errorMessage = resJson.error || '잘못된 요청입니다.';
etUI.dialog.alert({
title: '등록 실패',
message: '영수증이 등록 실패하였습니다.',
type: 'alert',
callback: function () {
// 초기화
uploadedReceipt.forEach(item => URL.revokeObjectURL(item.imgSrc));
uploadedReceipt.length = 0;
$('#file-upload-container').empty();
location.reload();
},
});
},
500: function (res) {
const resJson = res.responseJSON || {};
const errorMessage = resJson.error || '서버 오류가 발생했습니다.';
console.log(`서버 오류: ${errorMessage}`);
etUI.dialog.alert({
title: '등록 실패',
message: '영수증이 등록 실패하였습니다.',
type: 'alert',
callback: function () {
// 초기화
uploadedReceipt.forEach(item => URL.revokeObjectURL(item.imgSrc));
uploadedReceipt.length = 0;
$('#file-upload-container').empty();
location.reload();
},
});
},
},
error: function (xhr) {
const errorMessage = xhr.responseJSON?.error || `HTTP ${xhr.status}`;
console.log(`영수증 업로드 실패: ${errorMessage}`);
etUI.dialog.alert({
title: '등록 실패',
message: '영수증이 등록 실패하였습니다.',
type: 'alert',
callback: function () {
// 초기화
uploadedReceipt.forEach(item => URL.revokeObjectURL(item.imgSrc));
uploadedReceipt.length = 0;
$('#file-upload-container').empty();
location.reload();
},
});
}
});
ajaxUtil._hideWaiting();
},
};
</script>
</section>