264 lines
9.8 KiB
PHP
264 lines
9.8 KiB
PHP
|
|
<script>
|
|
$(document).ready(function() {
|
|
var ordBy = <?php echo json_encode(isset($ord_by) ? $ord_by : ''); ?>;
|
|
var searchDate = <?php echo json_encode(isset($searchDate) ? $searchDate : ''); ?>;
|
|
|
|
// 삭제 확인 및 처리 함수
|
|
window.confirmDelete = function(idx) {
|
|
if (confirm('정말 삭제하시겠습니까? 삭제한 후 복원할 수 없습니다.')) {
|
|
var delete_url = 'bakery_product_list.ajax.delete.php';
|
|
var formData = {
|
|
idx: idx
|
|
};
|
|
|
|
$.post(delete_url, formData, function(json) {
|
|
if (json !== undefined && json.isSuccess) {
|
|
alert('처리가 완료되었습니다.');
|
|
location.reload();
|
|
} else {
|
|
alert('처리중 에러가 발생하였습니다: ' + (json || '알 수 없는 오류'));
|
|
}
|
|
});
|
|
}
|
|
};
|
|
|
|
// 수정 폼 전송 및 처리 함수 추가
|
|
$('.modify').on('submit', function(event) {
|
|
event.preventDefault(); // 기본 제출 동작 방지
|
|
|
|
var form = $(this);
|
|
var formData = form.serialize(); // 폼 데이터 직렬화
|
|
|
|
$.post('bakery_product_list.ajax.update.php', formData, function(json) {
|
|
console.log(json); // 반환된 데이터를 확인하기 위한 로그
|
|
|
|
if (json !== undefined && json.isSuccess) {
|
|
alert('수정이 완료되었습니다.');
|
|
location.reload();
|
|
} else if(json !== undefined && !json.isSuccess) {
|
|
alert('수정 중 에러가 발생하였습니다: ' + (json.message || '알 수 없는 오류'));
|
|
} else {
|
|
alert('수정 중 에러가 발생하였습니다. JSON 데이터가 정의되지 않았습니다.');
|
|
}
|
|
}).fail(function() {
|
|
alert('AJAX 요청 실패.');
|
|
});
|
|
});
|
|
|
|
// 추가 폼 전송 및 처리 함수 추가
|
|
$('.add').on('submit', function(event) {
|
|
event.preventDefault(); // 기본 제출 동작 방지
|
|
|
|
var form = $(this);
|
|
var formData = form.serialize(); // 폼 데이터 직렬화
|
|
|
|
$.post('bakery_product_list.ajax.add.php', formData, function(json) {
|
|
console.log(json); // 반환된 데이터를 확인하기 위한 로그
|
|
|
|
if (json !== undefined && json.isSuccess) {
|
|
alert('품목 추가가 완료되었습니다.');
|
|
location.reload();
|
|
} else if(json !== undefined && !json.isSuccess) {
|
|
alert('추가 중 에러가 발생하였습니다: ' + (json.message || '알 수 없는 오류'));
|
|
} else {
|
|
alert('추가 중 에러가 발생하였습니다. JSON 데이터가 정의되지 않았습니다.');
|
|
}
|
|
}).fail(function() {
|
|
alert('AJAX 요청 실패.');
|
|
});
|
|
});
|
|
|
|
function attachThClickEvent() {
|
|
$('table').off('click', 'th'); // 기존 이벤트 제거
|
|
$('table').on('click', 'th', function() {
|
|
if ($(this).hasClass('no-click')) {
|
|
return; // no-click 클래스를 가진 th 요소는 클릭 이벤트를 무시하고 이후 코드 실행 안함
|
|
}
|
|
console.log('th clicked:', $(this).data('column')); // 로그 추가
|
|
const column = $(this).data('column');
|
|
let order = $(this).data('order') || 'asc'; // 기본값 'asc'
|
|
order = order === 'asc' ? 'desc' : 'asc';
|
|
$(this).data('order', order);
|
|
|
|
sortTable(column, order); // 테이블 정렬 함수 호출
|
|
});
|
|
}
|
|
|
|
function sortTable(column, order) {
|
|
const tbody = $('#bakeryTable tbody');
|
|
const rows = tbody.find('tr').toArray();
|
|
|
|
rows.sort((a, b) => {
|
|
const aValue = $(a).find(`td:eq(${getColumnIndex(column)})`).text();
|
|
const bValue = $(b).find(`td:eq(${getColumnIndex(column)})`).text();
|
|
|
|
if (order === 'asc') {
|
|
return aValue.localeCompare(bValue, undefined, {numeric: true});
|
|
} else {
|
|
return bValue.localeCompare(aValue, undefined, {numeric: true});
|
|
}
|
|
});
|
|
|
|
$.each(rows, (index, row) => {
|
|
tbody.append(row);
|
|
// 정렬 후 No. 열의 값을 재설정
|
|
$(row).find('td').eq(0).text(index + 1);
|
|
});
|
|
}
|
|
|
|
function getColumnIndex(column) {
|
|
return $(`#bakeryTable th[data-column="${column}"]`).index();
|
|
}
|
|
|
|
attachThClickEvent();
|
|
|
|
// 날짜 입력값 변경 시 페이지 전체 갱신
|
|
$('#searchDateInput').on('change', function() {
|
|
const searchDate = $(this).val();
|
|
const url = new URL(window.location.href);
|
|
url.searchParams.set('searchDate', searchDate);
|
|
window.location.href = url.toString();
|
|
});
|
|
|
|
|
|
// 수정 버튼 클릭 시
|
|
window.toggleEditMode = function() {
|
|
document.querySelectorAll('tbody .value').forEach(span => {
|
|
span.classList.toggle('d-none');
|
|
});
|
|
document.querySelectorAll('tbody input.form-control').forEach(input => {
|
|
input.classList.toggle('d-none');
|
|
if (input.hasAttribute('readonly')) {
|
|
input.removeAttribute('readonly');
|
|
} else {
|
|
input.setAttribute('readonly', 'readonly');
|
|
}
|
|
});
|
|
document.getElementById('editButton').classList.toggle('d-none');
|
|
document.getElementById('confirmButton').classList.toggle('d-none');
|
|
}
|
|
|
|
// 확인 버튼 클릭 시 데이터 업데이트 및 합계 갱신
|
|
window.confirmEditMode = function() {
|
|
var formData = new FormData();
|
|
document.querySelectorAll('tbody input.form-control').forEach(input => {
|
|
formData.append(input.id, input.value);
|
|
});
|
|
formData.append('searchDate', $('#searchDate').val());
|
|
|
|
$.ajax({
|
|
url: 'bakery_stock.ajax.confirm.update.php',
|
|
type: 'POST',
|
|
data: formData,
|
|
processData: false,
|
|
contentType: false,
|
|
success: function(response) {
|
|
console.log('Data updated successfully'); // 로그 추가
|
|
toggleEditMode();
|
|
// updateTableBody({ ord_by: ordBy, searchDate: $('#searchDate').val() }); // 불필요한 호출 제거
|
|
updateTotal(); // 합계 갱신
|
|
// location.reload();
|
|
},
|
|
error: function(xhr, status, error) {
|
|
console.error('Error: ' + error);
|
|
}
|
|
});
|
|
}
|
|
|
|
// 파일 업로드 버튼 클릭 시 파일 선택 창 열기
|
|
$('#uploadInventoryButton').on('click', function() {
|
|
$('#inventoryFileInput').click();
|
|
});
|
|
|
|
// 파일 선택 시 AJAX로 파일 업로드
|
|
$('#inventoryFileInput').on('change', function() {
|
|
var file = $('#inventoryFileInput')[0].files[0];
|
|
if (isValidExcelFile(file)) {
|
|
var formData = new FormData();
|
|
formData.append('file', file);
|
|
formData.append('searchDate', $('#searchDate').val());
|
|
|
|
$.ajax({
|
|
url: 'bakery_stock.ajax.upload_inventory.php',
|
|
type: 'POST',
|
|
data: formData,
|
|
contentType: false,
|
|
processData: false,
|
|
success: function(response) {
|
|
alert('파일 업로드 완료');
|
|
location.reload(); // 파일 업로드 후 페이지 새로고침
|
|
},
|
|
error: function() {
|
|
alert('파일 업로드 실패');
|
|
}
|
|
});
|
|
} else {
|
|
alert('엑셀 파일만 업로드할 수 있습니다.');
|
|
}
|
|
});
|
|
|
|
function isValidExcelFile(file) {
|
|
const validExtensions = ['.xls', '.xlsx'];
|
|
const fileName = file.name.toLowerCase();
|
|
return validExtensions.some(ext => fileName.endsWith(ext));
|
|
}
|
|
|
|
// 엑셀변환 ajax
|
|
document.getElementById('exportExcelButton').addEventListener('click', function() {
|
|
$.ajax({
|
|
url: 'bakery_stock_excel.php',
|
|
type: 'POST',
|
|
data: { searchDate: $('#searchDate').val(), ordBy: ordBy },
|
|
xhrFields: {
|
|
responseType: 'blob'
|
|
},
|
|
success: function(data) {
|
|
var blob = new Blob([data], { type: 'application/vnd.ms-excel' });
|
|
var link = document.createElement('a');
|
|
link.href = window.URL.createObjectURL(blob);
|
|
link.download = 'bakery_data_' + $('#searchDate').val() + '.xls';
|
|
link.click();
|
|
},
|
|
error: function(jqXHR, textStatus, errorThrown) {
|
|
console.error('엑셀 파일 다운로드 실패:', textStatus, errorThrown);
|
|
}
|
|
});
|
|
});
|
|
|
|
// author 업데이트
|
|
$('#author input').on('blur', function(event) {
|
|
var formData = new FormData(document.getElementById('author')); // 폼 데이터 직렬화
|
|
|
|
console.log("Form Data:", formData); // 직렬화된 데이터 로그 확인
|
|
|
|
$.ajax({
|
|
type: 'POST',
|
|
url: 'bakery_stock.ajax.author.update.php',
|
|
data: formData,
|
|
processData: false,
|
|
contentType: false,
|
|
success: function(response) {
|
|
if (response.success) {
|
|
console.log('데이터가 성공적으로 저장되었습니다.');
|
|
} else {
|
|
console.error('데이터 저장에 실패하였습니다: ' + response.message);
|
|
}
|
|
},
|
|
error: function(xhr, status, error) {
|
|
console.error('AJAX 호출 중 오류가 발생하였습니다: ' + error);
|
|
}
|
|
});
|
|
});
|
|
|
|
// 출력 버튼 클릭 이벤트 추가
|
|
$('#print').on('click', function() {
|
|
var searchDate = $('#searchDate').val(); // searchDate 값을 가져옴
|
|
var printWindow = window.open('bakery_stock_print.php?searchDate=' + searchDate, '_blank', 'width=800,height=600');
|
|
printWindow.onload = function() {
|
|
printWindow.print();
|
|
};
|
|
});
|
|
|
|
});
|
|
</script>
|