-
diff --git a/front/src/main/resources/templates/record/income/index.html b/front/src/main/resources/templates/record/income/index.html
index a14500bf..bc87605e 100644
--- a/front/src/main/resources/templates/record/income/index.html
+++ b/front/src/main/resources/templates/record/income/index.html
@@ -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 = $(`
+
+

+
삭제
+
+ `);
+ 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();
+ },
+
};
diff --git a/pom.xml b/pom.xml
index 4d08b2c1..00f47f8f 100644
--- a/pom.xml
+++ b/pom.xml
@@ -93,6 +93,10 @@
jsch
0.1.55
+
+ com.fasterxml.jackson.core
+ jackson-databind
+
com.h2database
h2