658 lines
17 KiB
Plaintext
658 lines
17 KiB
Plaintext
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
|
|
<%@ page import="com.pms.common.utils.*"%>
|
|
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
|
|
<%@ page import="java.util.*" %>
|
|
<%@include file= "/init.jsp" %>
|
|
<%
|
|
PersonBean person = (PersonBean)session.getAttribute(Constants.PERSON_BEAN);
|
|
String connector = person.getUserId();
|
|
String contractObjId = request.getParameter("contractObjId");
|
|
%>
|
|
<!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
|
|
<title>견적서 메일 발송</title>
|
|
<style>
|
|
body {
|
|
margin: 10px;
|
|
background-color: #f5f5f5;
|
|
font-size: 13px;
|
|
}
|
|
.mail-form-container {
|
|
background: white;
|
|
padding: 15px 20px;
|
|
border-radius: 6px;
|
|
box-shadow: 0 1px 5px rgba(0,0,0,0.1);
|
|
max-width: 800px;
|
|
margin: 0 auto;
|
|
}
|
|
.form-title {
|
|
font-size: 18px;
|
|
font-weight: bold;
|
|
margin-bottom: 15px;
|
|
color: #333;
|
|
border-bottom: 2px solid #3085d6;
|
|
padding-bottom: 8px;
|
|
}
|
|
.form-group {
|
|
margin-bottom: 12px;
|
|
}
|
|
.form-group label {
|
|
display: block;
|
|
font-weight: bold;
|
|
margin-bottom: 5px;
|
|
color: #555;
|
|
font-size: 13px;
|
|
}
|
|
.form-group label.required:after {
|
|
content: " *";
|
|
color: red;
|
|
}
|
|
.form-group input[type="text"],
|
|
.form-group input[type="email"],
|
|
.form-group textarea {
|
|
width: 100%;
|
|
padding: 6px 8px;
|
|
border: 1px solid #ddd;
|
|
border-radius: 3px;
|
|
font-size: 13px;
|
|
box-sizing: border-box;
|
|
}
|
|
.form-group textarea {
|
|
min-height: 120px;
|
|
resize: vertical;
|
|
}
|
|
.manager-list {
|
|
border: 1px solid #ddd;
|
|
border-radius: 3px;
|
|
padding: 8px;
|
|
background-color: #fafafa;
|
|
max-height: 150px;
|
|
overflow-y: auto;
|
|
}
|
|
.manager-item {
|
|
padding: 5px;
|
|
margin-bottom: 3px;
|
|
background: white;
|
|
border-radius: 3px;
|
|
display: flex;
|
|
align-items: center;
|
|
font-size: 13px;
|
|
}
|
|
.manager-item input[type="checkbox"] {
|
|
margin-right: 8px;
|
|
width: 16px;
|
|
height: 16px;
|
|
cursor: pointer;
|
|
}
|
|
.manager-item label {
|
|
cursor: pointer;
|
|
margin: 0;
|
|
flex: 1;
|
|
font-size: 13px;
|
|
}
|
|
.no-managers {
|
|
color: #999;
|
|
text-align: center;
|
|
padding: 12px;
|
|
font-size: 13px;
|
|
}
|
|
.button-group {
|
|
text-align: center;
|
|
margin-top: 15px;
|
|
padding-top: 15px;
|
|
border-top: 1px solid #eee;
|
|
}
|
|
.btn {
|
|
padding: 8px 20px;
|
|
margin: 0 4px;
|
|
border: none;
|
|
border-radius: 3px;
|
|
font-size: 13px;
|
|
cursor: pointer;
|
|
transition: all 0.3s;
|
|
}
|
|
.btn-primary {
|
|
background-color: #3085d6;
|
|
color: white;
|
|
}
|
|
.btn-primary:hover {
|
|
background-color: #2874c5;
|
|
}
|
|
.btn-secondary {
|
|
background-color: #6c757d;
|
|
color: white;
|
|
}
|
|
.btn-secondary:hover {
|
|
background-color: #5a6268;
|
|
}
|
|
.info-text {
|
|
font-size: 11px;
|
|
color: #666;
|
|
margin-top: 3px;
|
|
}
|
|
.pdf-status {
|
|
padding: 8px 10px;
|
|
background-color: #e7f3ff;
|
|
border-left: 3px solid #3085d6;
|
|
border-radius: 3px;
|
|
margin-bottom: 12px;
|
|
font-size: 13px;
|
|
}
|
|
.pdf-status i {
|
|
color: #3085d6;
|
|
margin-right: 5px;
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div class="mail-form-container">
|
|
<div class="form-title">견적서 메일 발송</div>
|
|
|
|
<div class="pdf-status">
|
|
<i class="fa fa-file-pdf-o"></i>
|
|
<strong>PDF 첨부:</strong> 최종 차수 견적서가 자동으로 첨부됩니다.
|
|
</div>
|
|
|
|
<form id="mailForm">
|
|
<input type="hidden" id="contractObjId" name="contractObjId" value="<%=contractObjId%>"/>
|
|
<input type="hidden" id="pdfSessionId" name="pdfSessionId" value=""/>
|
|
|
|
<!-- 고객사 담당자 선택 -->
|
|
<div class="form-group">
|
|
<label>고객사 담당자 선택</label>
|
|
<div id="managerListContainer" class="manager-list">
|
|
<div class="no-managers">담당자 정보를 불러오는 중...</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- 수신인 이메일 -->
|
|
<div class="form-group">
|
|
<label for="toEmails" class="required">수신인 (To)</label>
|
|
<input type="text" id="toEmails" name="toEmails" placeholder="이메일 주소를 입력하세요 (여러 개는 쉼표로 구분)"/>
|
|
<div class="info-text">예: email1@example.com, email2@example.com</div>
|
|
</div>
|
|
|
|
<!-- 참조 이메일 -->
|
|
<div class="form-group">
|
|
<label for="ccEmails">참조 (CC)</label>
|
|
<input type="text" id="ccEmails" name="ccEmails" placeholder="참조 이메일 주소 (선택사항)"/>
|
|
<div class="info-text">작성자 이메일이 자동으로 참조에 추가됩니다.</div>
|
|
</div>
|
|
|
|
<!-- 메일 제목 -->
|
|
<div class="form-group">
|
|
<label for="subject" class="required">제목</label>
|
|
<input type="text" id="subject" name="subject" placeholder="메일 제목을 입력하세요"/>
|
|
</div>
|
|
|
|
<!-- 메일 내용 -->
|
|
<div class="form-group">
|
|
<label for="contents" class="required">내용</label>
|
|
<textarea id="contents" name="contents" placeholder="메일 내용을 입력하세요"></textarea>
|
|
</div>
|
|
|
|
<!-- 버튼 -->
|
|
<div class="button-group">
|
|
<button type="button" class="btn btn-primary" onclick="fn_sendMail()">발송</button>
|
|
<button type="button" class="btn btn-secondary" onclick="window.close()">취소</button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
|
|
<script>
|
|
var contractInfo = null;
|
|
var managerList = [];
|
|
|
|
$(document).ready(function(){
|
|
// 계약 정보 및 담당자 목록 로드
|
|
fn_loadContractInfo();
|
|
});
|
|
|
|
// 계약 정보 로드
|
|
function fn_loadContractInfo(){
|
|
var contractObjId = $("#contractObjId").val();
|
|
|
|
$.ajax({
|
|
url: "/contractMgmt/getContractInfoForMail.do",
|
|
type: "POST",
|
|
data: { objId: contractObjId },
|
|
dataType: "json",
|
|
success: function(data){
|
|
if(data.result === "success" && data.contractInfo){
|
|
contractInfo = data.contractInfo;
|
|
|
|
// 메일 제목 자동 생성
|
|
var contractNo = fnc_checkNull(contractInfo.CONTRACT_NO);
|
|
var customerName = fnc_checkNull(contractInfo.CUSTOMER_NAME);
|
|
$("#subject").val("[" + customerName + "] " + contractNo + " 견적서");
|
|
|
|
// 메일 내용 템플릿 생성
|
|
fn_generateMailTemplate();
|
|
|
|
// 고객사 담당자 목록 로드
|
|
var customerObjId = fnc_checkNull(contractInfo.CUSTOMER_OBJID);
|
|
if(customerObjId !== ""){
|
|
fn_loadCustomerManagers(customerObjId);
|
|
} else {
|
|
$("#managerListContainer").html('<div class="no-managers">고객사 정보가 없습니다.</div>');
|
|
}
|
|
} else {
|
|
Swal.fire({
|
|
title: '오류',
|
|
text: '계약 정보를 불러올 수 없습니다.',
|
|
icon: 'error'
|
|
}).then(() => {
|
|
window.close();
|
|
});
|
|
}
|
|
},
|
|
error: function(){
|
|
Swal.fire({
|
|
title: '오류',
|
|
text: '계약 정보 조회 중 오류가 발생했습니다.',
|
|
icon: 'error'
|
|
}).then(() => {
|
|
window.close();
|
|
});
|
|
}
|
|
});
|
|
}
|
|
|
|
// 고객사 담당자 목록 로드
|
|
function fn_loadCustomerManagers(customerObjId){
|
|
$.ajax({
|
|
url: "/contractMgmt/getCustomerManagerList.do",
|
|
type: "POST",
|
|
data: { customerObjId: customerObjId },
|
|
dataType: "json",
|
|
success: function(data){
|
|
if(data.result === "success" && data.managers && data.managers.length > 0){
|
|
managerList = data.managers;
|
|
fn_renderManagerList();
|
|
} else {
|
|
$("#managerListContainer").html('<div class="no-managers">등록된 담당자가 없습니다. 수신인을 직접 입력해주세요.</div>');
|
|
}
|
|
},
|
|
error: function(){
|
|
$("#managerListContainer").html('<div class="no-managers">담당자 정보를 불러올 수 없습니다. 수신인을 직접 입력해주세요.</div>');
|
|
}
|
|
});
|
|
}
|
|
|
|
// 담당자 목록 렌더링
|
|
function fn_renderManagerList(){
|
|
var html = '';
|
|
|
|
for(var i = 0; i < managerList.length; i++){
|
|
var manager = managerList[i];
|
|
var name = fnc_checkNull(manager.name);
|
|
var email = fnc_checkNull(manager.email);
|
|
|
|
if(name !== ""){
|
|
html += '<div class="manager-item">';
|
|
html += '<input type="checkbox" id="manager_' + i + '" data-email="' + email + '" onchange="fn_updateRecipients()">';
|
|
html += '<label for="manager_' + i + '">' + name;
|
|
if(email !== ""){
|
|
html += ' (' + email + ')';
|
|
}
|
|
html += '</label>';
|
|
html += '</div>';
|
|
}
|
|
}
|
|
|
|
if(html === ''){
|
|
html = '<div class="no-managers">등록된 담당자가 없습니다. 수신인을 직접 입력해주세요.</div>';
|
|
}
|
|
|
|
$("#managerListContainer").html(html);
|
|
}
|
|
|
|
// 담당자 선택 시 수신인 필드 업데이트
|
|
function fn_updateRecipients(){
|
|
var selectedEmails = [];
|
|
|
|
$("input[type='checkbox'][id^='manager_']:checked").each(function(){
|
|
var email = $(this).attr("data-email");
|
|
if(email && email !== ""){
|
|
selectedEmails.push(email);
|
|
}
|
|
});
|
|
|
|
$("#toEmails").val(selectedEmails.join(", "));
|
|
}
|
|
|
|
// 메일 내용 템플릿 생성
|
|
function fn_generateMailTemplate(){
|
|
var customerName = fnc_checkNull(contractInfo.CUSTOMER_NAME);
|
|
var contractNo = fnc_checkNull(contractInfo.CONTRACT_NO);
|
|
|
|
var template = "안녕하세요.\n\n";
|
|
template += customerName + " 귀하께서 요청하신 견적서를 첨부파일로 송부드립니다.\n\n";
|
|
template += "영업번호: " + contractNo + "\n\n";
|
|
template += "첨부된 견적서를 검토하신 후 문의사항이 있으시면 연락 주시기 바랍니다.\n\n";
|
|
template += "감사합니다.\n";
|
|
|
|
$("#contents").val(template);
|
|
}
|
|
|
|
// 메일 발송
|
|
function fn_sendMail(){
|
|
// 입력값 검증
|
|
var toEmails = $("#toEmails").val().trim();
|
|
var subject = $("#subject").val().trim();
|
|
var contents = $("#contents").val().trim();
|
|
|
|
if(toEmails === ""){
|
|
Swal.fire("수신인을 입력해주세요.");
|
|
$("#toEmails").focus();
|
|
return;
|
|
}
|
|
|
|
// 이메일 형식 검증
|
|
var emailPattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
var emails = toEmails.split(",").map(function(e){ return e.trim(); });
|
|
for(var i = 0; i < emails.length; i++){
|
|
if(!emailPattern.test(emails[i])){
|
|
Swal.fire("올바른 이메일 형식이 아닙니다: " + emails[i]);
|
|
$("#toEmails").focus();
|
|
return;
|
|
}
|
|
}
|
|
|
|
if(subject === ""){
|
|
Swal.fire("제목을 입력해주세요.");
|
|
$("#subject").focus();
|
|
return;
|
|
}
|
|
|
|
if(contents === ""){
|
|
Swal.fire("내용을 입력해주세요.");
|
|
$("#contents").focus();
|
|
return;
|
|
}
|
|
|
|
// 발송 확인
|
|
Swal.fire({
|
|
title: '메일 발송',
|
|
text: "견적서를 발송하시겠습니까?",
|
|
icon: 'question',
|
|
showCancelButton: true,
|
|
confirmButtonText: '발송',
|
|
cancelButtonText: '취소'
|
|
}).then((result) => {
|
|
if(result.isConfirmed){
|
|
// PDF 생성 및 발송 시작
|
|
fn_generatePdfAndSend();
|
|
}
|
|
});
|
|
}
|
|
|
|
// PDF 생성 및 발송
|
|
function fn_generatePdfAndSend(){
|
|
var contractObjId = $("#contractObjId").val();
|
|
|
|
Swal.fire({
|
|
title: 'PDF 생성 중...',
|
|
text: '견적서를 PDF로 변환하고 있습니다.',
|
|
allowOutsideClick: false,
|
|
onOpen: () => {
|
|
Swal.showLoading();
|
|
}
|
|
});
|
|
|
|
// 1. 최종 차수 견적서 정보 조회
|
|
$.ajax({
|
|
url: "/contractMgmt/getEstimateTemplateList.do",
|
|
type: "POST",
|
|
data: { objId: contractObjId },
|
|
dataType: "json",
|
|
success: function(data){
|
|
if(data.result === "success" && data.list && data.list.length > 0){
|
|
var latestEstimate = data.list[0];
|
|
var templateObjId = latestEstimate.OBJID || latestEstimate.objid;
|
|
var templateType = latestEstimate.TEMPLATE_TYPE || latestEstimate.template_type || latestEstimate.templateType;
|
|
|
|
// 2. PDF 생성
|
|
fn_generatePdf(contractObjId, templateObjId, templateType);
|
|
} else {
|
|
Swal.close();
|
|
Swal.fire({
|
|
title: '오류',
|
|
text: '견적서를 찾을 수 없습니다.',
|
|
icon: 'error'
|
|
});
|
|
}
|
|
},
|
|
error: function(){
|
|
Swal.close();
|
|
Swal.fire({
|
|
title: '오류',
|
|
text: '견적서 조회 중 오류가 발생했습니다.',
|
|
icon: 'error'
|
|
});
|
|
}
|
|
});
|
|
}
|
|
|
|
// PDF 생성
|
|
function fn_generatePdf(contractObjId, templateObjId, templateType){
|
|
var url = "";
|
|
if(templateType === "1"){
|
|
url = "/contractMgmt/estimateTemplate1.do?templateObjId=" + templateObjId;
|
|
} else if(templateType === "2"){
|
|
url = "/contractMgmt/estimateTemplate2.do?templateObjId=" + templateObjId;
|
|
}
|
|
|
|
// 숨겨진 iframe으로 페이지 로드
|
|
var iframe = $('<iframe>', {
|
|
id: 'pdfGeneratorFrame',
|
|
src: url,
|
|
style: 'position:absolute;width:0;height:0;border:none;'
|
|
}).appendTo('body');
|
|
|
|
// iframe 로드 완료 대기
|
|
iframe.on('load', function(){
|
|
try {
|
|
var iframeWindow = this.contentWindow;
|
|
|
|
// 데이터 로딩 완료 대기 (최대 120초 = 1200번 시도)
|
|
var checkDataLoaded = function(attempts) {
|
|
if(attempts > 1200) {
|
|
$('#pdfGeneratorFrame').remove();
|
|
Swal.close();
|
|
Swal.fire({
|
|
title: '타임아웃',
|
|
text: '견적서 데이터 로딩 시간이 초과되었습니다. (120초)',
|
|
icon: 'error'
|
|
});
|
|
return;
|
|
}
|
|
|
|
if(iframeWindow.dataLoaded === true) {
|
|
// PDF 생성 함수 호출
|
|
if(typeof iframeWindow.fn_generateAndUploadPdf === 'function'){
|
|
iframeWindow.fn_generateAndUploadPdf(function(pdfBase64){
|
|
$('#pdfGeneratorFrame').remove();
|
|
|
|
if(pdfBase64){
|
|
// PDF 업로드 후 메일 발송
|
|
fn_uploadPdfAndSendMail(contractObjId, pdfBase64);
|
|
} else {
|
|
Swal.close();
|
|
Swal.fire({
|
|
title: '오류',
|
|
text: 'PDF 생성에 실패했습니다.',
|
|
icon: 'error'
|
|
});
|
|
}
|
|
});
|
|
} else {
|
|
$('#pdfGeneratorFrame').remove();
|
|
Swal.close();
|
|
Swal.fire({
|
|
title: '오류',
|
|
text: '견적서 페이지 로드에 실패했습니다.',
|
|
icon: 'error'
|
|
});
|
|
}
|
|
} else {
|
|
setTimeout(function() {
|
|
checkDataLoaded(attempts + 1);
|
|
}, 100);
|
|
}
|
|
};
|
|
|
|
checkDataLoaded(0);
|
|
} catch(e) {
|
|
console.error('PDF 생성 오류:', e);
|
|
$('#pdfGeneratorFrame').remove();
|
|
Swal.close();
|
|
Swal.fire({
|
|
title: '오류',
|
|
text: 'PDF 생성 중 오류가 발생했습니다.',
|
|
icon: 'error'
|
|
});
|
|
}
|
|
});
|
|
|
|
// 타임아웃 설정 (180초 = 3분 - 장비 견적서는 데이터가 많아서 시간이 더 필요)
|
|
setTimeout(function(){
|
|
if($('#pdfGeneratorFrame').length > 0){
|
|
$('#pdfGeneratorFrame').remove();
|
|
Swal.close();
|
|
Swal.fire({
|
|
title: '타임아웃',
|
|
text: 'PDF 생성 시간이 초과되었습니다. (180초)\n견적서 데이터가 많은 경우 시간이 오래 걸릴 수 있습니다.',
|
|
icon: 'error'
|
|
});
|
|
}
|
|
}, 180000);
|
|
}
|
|
|
|
// PDF 업로드 및 메일 발송
|
|
function fn_uploadPdfAndSendMail(contractObjId, pdfBase64){
|
|
Swal.update({
|
|
text: 'PDF 업로드 중...'
|
|
});
|
|
|
|
// 청크 업로드 (기존 로직 활용)
|
|
var chunkSize = 100 * 1024;
|
|
var totalChunks = Math.ceil(pdfBase64.length / chunkSize);
|
|
var uploadedChunks = 0;
|
|
var sessionId = 'pdf_' + contractObjId + '_' + new Date().getTime();
|
|
|
|
function uploadChunk(chunkIndex) {
|
|
var start = chunkIndex * chunkSize;
|
|
var end = Math.min(start + chunkSize, pdfBase64.length);
|
|
var chunk = pdfBase64.substring(start, end);
|
|
|
|
$.ajax({
|
|
url: "/contractMgmt/uploadPdfChunk.do",
|
|
type: "POST",
|
|
data: {
|
|
sessionId: sessionId,
|
|
chunkIndex: chunkIndex,
|
|
totalChunks: totalChunks,
|
|
chunk: chunk
|
|
},
|
|
dataType: "json",
|
|
timeout: 30000,
|
|
success: function(data){
|
|
if(data.result === "success"){
|
|
uploadedChunks++;
|
|
|
|
var progress = Math.round((uploadedChunks / totalChunks) * 100);
|
|
Swal.update({
|
|
text: 'PDF 업로드 중... ' + progress + '%'
|
|
});
|
|
|
|
if(chunkIndex + 1 < totalChunks){
|
|
uploadChunk(chunkIndex + 1);
|
|
} else {
|
|
// 모든 청크 업로드 완료, 메일 발송
|
|
$("#pdfSessionId").val(sessionId);
|
|
fn_submitMailForm();
|
|
}
|
|
} else {
|
|
Swal.close();
|
|
Swal.fire({
|
|
title: '업로드 실패',
|
|
text: 'PDF 업로드 중 오류가 발생했습니다.',
|
|
icon: 'error'
|
|
});
|
|
}
|
|
},
|
|
error: function(){
|
|
Swal.close();
|
|
Swal.fire({
|
|
title: '오류',
|
|
text: 'PDF 업로드 중 시스템 오류가 발생했습니다.',
|
|
icon: 'error'
|
|
});
|
|
}
|
|
});
|
|
}
|
|
|
|
uploadChunk(0);
|
|
}
|
|
|
|
// 메일 발송 요청
|
|
function fn_submitMailForm(){
|
|
Swal.update({
|
|
text: '메일 발송 중...'
|
|
});
|
|
|
|
var formData = {
|
|
objId: $("#contractObjId").val(),
|
|
pdfSessionId: $("#pdfSessionId").val(),
|
|
toEmails: $("#toEmails").val(),
|
|
ccEmails: $("#ccEmails").val(),
|
|
subject: $("#subject").val(),
|
|
contents: $("#contents").val()
|
|
};
|
|
|
|
$.ajax({
|
|
url: "/contractMgmt/sendEstimateMailCustom.do",
|
|
type: "POST",
|
|
data: formData,
|
|
dataType: "json",
|
|
timeout: 60000,
|
|
success: function(data){
|
|
Swal.close();
|
|
if(data.result === "success"){
|
|
Swal.fire({
|
|
title: '발송 완료',
|
|
text: '견적서가 성공적으로 발송되었습니다.',
|
|
icon: 'success'
|
|
}).then(() => {
|
|
// 부모 창 새로고침
|
|
if(window.opener && typeof window.opener.fn_search === 'function'){
|
|
window.opener.fn_search();
|
|
}
|
|
window.close();
|
|
});
|
|
} else {
|
|
Swal.fire({
|
|
title: '발송 실패',
|
|
text: data.message || '메일 발송 중 오류가 발생했습니다.',
|
|
icon: 'error'
|
|
});
|
|
}
|
|
},
|
|
error: function(){
|
|
Swal.close();
|
|
Swal.fire({
|
|
title: '오류',
|
|
text: '메일 발송 중 시스템 오류가 발생했습니다.',
|
|
icon: 'error'
|
|
});
|
|
}
|
|
});
|
|
}
|
|
</script>
|
|
</body>
|
|
</html>
|
|
|