445 lines
12 KiB
PHP
445 lines
12 KiB
PHP
<?php
|
|
header('Content-Type: application/json; charset=UTF-8');
|
|
|
|
// ------------------------------
|
|
// DB 연결
|
|
// ------------------------------
|
|
$DB_HOST = "localhost";
|
|
$DB_NAME = "egbim";
|
|
$DB_USER = "egbim";
|
|
$DB_PASS = "baron3840!!";
|
|
|
|
try {
|
|
$pdo = new PDO(
|
|
"mysql:host={$DB_HOST};dbname={$DB_NAME};charset=utf8mb4",
|
|
$DB_USER,
|
|
$DB_PASS,
|
|
[
|
|
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
|
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
|
|
]
|
|
);
|
|
} catch (Exception $e) {
|
|
echo json_encode([
|
|
"status" => "fail",
|
|
"message" => "DB 연결 실패: " . $e->getMessage()
|
|
]);
|
|
exit;
|
|
}
|
|
|
|
// ------------------------------
|
|
// 기준 연도
|
|
// ------------------------------
|
|
// $year = date("Y");
|
|
|
|
$START_YEAR = 2025;
|
|
|
|
$year = isset($_GET['year']) ? (int)$_GET['year'] : $START_YEAR;
|
|
if ($year < $START_YEAR) $year = $START_YEAR;
|
|
|
|
$yearStart = "{$year}-01-01";
|
|
$yearEnd = "{$year}-12-31";
|
|
|
|
// ------------------------------
|
|
// 연간 누적 판매수
|
|
// ------------------------------
|
|
$total_copy = $pdo->query("
|
|
SELECT COALESCE(SUM(quantity), 0)
|
|
FROM sales_results
|
|
WHERE sales_date BETWEEN '{$yearStart}' AND '{$yearEnd}'
|
|
")->fetchColumn();
|
|
|
|
// ------------------------------
|
|
// 연간 매출액
|
|
// ------------------------------
|
|
$total_amount = $pdo->query("
|
|
SELECT COALESCE(SUM(total_amount), 0)
|
|
FROM sales_results
|
|
WHERE sales_date BETWEEN '{$yearStart}' AND '{$yearEnd}'
|
|
")->fetchColumn();
|
|
|
|
// ------------------------------
|
|
// 월별 판매량 (그래프용)
|
|
// ------------------------------
|
|
$monthly_rows = $pdo->query("
|
|
SELECT MONTH(sales_date) AS m, SUM(quantity) AS qty
|
|
FROM sales_results
|
|
WHERE sales_date BETWEEN '{$yearStart}' AND '{$yearEnd}'
|
|
GROUP BY MONTH(sales_date)
|
|
ORDER BY m
|
|
")->fetchAll();
|
|
|
|
$monthly = array_fill(1, 12, 0);
|
|
foreach ($monthly_rows as $row) {
|
|
$monthly[(int)$row['m']] = (int)$row['qty'];
|
|
}
|
|
|
|
// ------------------------------
|
|
// 개인별 실적
|
|
// ------------------------------
|
|
$persons = $pdo->query("
|
|
SELECT m.emp_name, SUM(r.quantity) AS qty
|
|
FROM sales_results r
|
|
JOIN sales_members m
|
|
ON TRIM(TRAILING ')' FROM SUBSTRING_INDEX(r.emp_no, '(', -1)) = m.emp_no
|
|
WHERE r.sales_date BETWEEN '{$yearStart}' AND '{$yearEnd}'
|
|
GROUP BY r.emp_no
|
|
ORDER BY qty DESC
|
|
")->fetchAll();
|
|
|
|
// ------------------------------
|
|
// 업종별 판매 현황 (설계/교육/제작/감리/시공/기타)
|
|
// ------------------------------
|
|
$biz_rows = $pdo->query("
|
|
SELECT business_type, COUNT(*) AS cnt
|
|
FROM sales_clients
|
|
GROUP BY business_type
|
|
")->fetchAll();
|
|
|
|
$industry = [
|
|
"설계" => 0,
|
|
"교육" => 0,
|
|
"제작" => 0,
|
|
"감리" => 0,
|
|
"시공" => 0,
|
|
"기타" => 0
|
|
];
|
|
|
|
foreach ($biz_rows as $b) {
|
|
if (isset($industry[$b['business_type']])) {
|
|
$industry[$b['business_type']] = (int)$b['cnt'];
|
|
}
|
|
}
|
|
|
|
// ------------------------------
|
|
// 기관별 판매 현황 (공공기관/일반기업/교육기관/기타)
|
|
// ------------------------------
|
|
$org_rows = $pdo->query("
|
|
SELECT org_type, COUNT(*) AS cnt
|
|
FROM sales_clients
|
|
GROUP BY org_type
|
|
")->fetchAll();
|
|
|
|
$institution = [
|
|
"일반기업" => 0,
|
|
"교육기관" => 0,
|
|
"공공기관" => 0,
|
|
"기타" => 0
|
|
];
|
|
|
|
foreach ($org_rows as $o) {
|
|
if (isset($institution[$o['org_type']])) {
|
|
$institution[$o['org_type']] = (int)$o['cnt'];
|
|
}
|
|
}
|
|
|
|
// ------------------------------
|
|
// 전체 업체수 (거래처 수)
|
|
// ------------------------------
|
|
$client_count = $pdo->query("
|
|
SELECT COUNT(*)
|
|
FROM sales_clients
|
|
")->fetchColumn();
|
|
|
|
// ------------------------------
|
|
// 월 목표 & 달성률
|
|
// ------------------------------
|
|
$current_month = "{$year}-" . date("m"); // 연도 고정
|
|
|
|
// $target_qty = $pdo->query("
|
|
// SELECT COALESCE(target_qty, 0)
|
|
// FROM sales_targets
|
|
// WHERE target_month = '{$current_month}'
|
|
// LIMIT 1
|
|
// ")->fetchColumn();
|
|
$target_qty = $pdo->query("
|
|
SELECT COALESCE(SUM(target_qty), 0)
|
|
FROM sales_targets
|
|
WHERE target_month = '{$current_month}'
|
|
")->fetchColumn();
|
|
|
|
$month_sales = $pdo->query("
|
|
SELECT COALESCE(SUM(quantity), 0)
|
|
FROM sales_results
|
|
WHERE DATE_FORMAT(sales_date, '%Y-%m') = '{$current_month}'
|
|
")->fetchColumn();
|
|
|
|
$target_rate = ($target_qty > 0)
|
|
? round(($month_sales / $target_qty) * 100, 1)
|
|
: 0;
|
|
|
|
// ------------------------------
|
|
// 당월 매출액
|
|
// ------------------------------
|
|
$month_sales_amount = $pdo->query("
|
|
SELECT COALESCE(SUM(total_amount), 0)
|
|
FROM sales_results
|
|
WHERE DATE_FORMAT(sales_date, '%Y-%m') = '{$current_month}'
|
|
")->fetchColumn();
|
|
|
|
// ------------------------------
|
|
// 연간 목표 수량 (1~12월 target 합)
|
|
// ------------------------------
|
|
$yearly_target_qty = $pdo->query("
|
|
SELECT COALESCE(SUM(target_qty), 0)
|
|
FROM sales_targets
|
|
WHERE LEFT(target_month, 4) = '{$year}'
|
|
")->fetchColumn();
|
|
|
|
// ------------------------------
|
|
// 연간 목표 달성률
|
|
// ------------------------------
|
|
$yearly_rate = ($yearly_target_qty > 0)
|
|
? round(($total_copy / $yearly_target_qty) * 100, 1)
|
|
: 0;
|
|
|
|
// // ------------------------------
|
|
// // 🔥 [신규] 전월 대비 증감 계산
|
|
// // ------------------------------
|
|
// $prevMonthDate = strtotime("{$year}-" . date("m") . "-01 -1 month");
|
|
// $prev_month = date("Y-m", $prevMonthDate);
|
|
|
|
// // ⬇ 전월 판매수
|
|
// $prev_copy = $pdo->query("
|
|
// SELECT COALESCE(SUM(quantity), 0)
|
|
// FROM sales_results
|
|
// WHERE DATE_FORMAT(sales_date, '%Y-%m') = '{$prev_month}'
|
|
// ")->fetchColumn();
|
|
|
|
// // ⬇ 전월 판매액
|
|
// $prev_amount = $pdo->query("
|
|
// SELECT COALESCE(SUM(total_amount), 0)
|
|
// FROM sales_results
|
|
// WHERE DATE_FORMAT(sales_date, '%Y-%m') = '{$prev_month}'
|
|
// ")->fetchColumn();
|
|
|
|
// // ⬇ 전월 연간 목표 달성률 (전월까지 총합 / 연간 목표)
|
|
// $prev_year_copy = $pdo->query("
|
|
// SELECT COALESCE(SUM(quantity), 0)
|
|
// FROM sales_results
|
|
// WHERE sales_date >= '{$year}-01-01'
|
|
// AND sales_date < '{$prev_month}-01'
|
|
// ")->fetchColumn();
|
|
|
|
// $prev_rate = ($yearly_target_qty > 0)
|
|
// ? round(($prev_year_copy / $yearly_target_qty) * 100, 1)
|
|
// : 0;
|
|
|
|
// // ------------------------------
|
|
// // 🔥 [신규] 차이 계산
|
|
// // ------------------------------
|
|
// $diff_copy = $total_copy - $prev_year_copy;
|
|
// $diff_amount = $total_amount - $prev_amount;
|
|
// $diff_rate = $yearly_rate - $prev_rate;
|
|
|
|
|
|
|
|
|
|
// ------------------------------
|
|
// 🔥 [전월 대비] 기준 월 계산
|
|
// ------------------------------
|
|
$currentYm = "{$year}-" . date('m'); // 예: 2025-12
|
|
$prevYm = date('Y-m', strtotime("{$currentYm}-01 -1 month")); // 예: 2025-11
|
|
|
|
// ------------------------------
|
|
// 🔥 이번 달 (월 기준) 판매수 / 매출
|
|
// ------------------------------
|
|
$curr_copy = $pdo->query("
|
|
SELECT COALESCE(SUM(quantity), 0)
|
|
FROM sales_results
|
|
WHERE DATE_FORMAT(sales_date, '%Y-%m') = '{$currentYm}'
|
|
")->fetchColumn();
|
|
|
|
$curr_amount = $pdo->query("
|
|
SELECT COALESCE(SUM(total_amount), 0)
|
|
FROM sales_results
|
|
WHERE DATE_FORMAT(sales_date, '%Y-%m') = '{$currentYm}'
|
|
")->fetchColumn();
|
|
|
|
// ------------------------------
|
|
// 🔥 전월 (월 기준) 판매수 / 매출
|
|
// ------------------------------
|
|
$prev_copy = $pdo->query("
|
|
SELECT COALESCE(SUM(quantity), 0)
|
|
FROM sales_results
|
|
WHERE DATE_FORMAT(sales_date, '%Y-%m') = '{$prevYm}'
|
|
")->fetchColumn();
|
|
|
|
$prev_amount = $pdo->query("
|
|
SELECT COALESCE(SUM(total_amount), 0)
|
|
FROM sales_results
|
|
WHERE DATE_FORMAT(sales_date, '%Y-%m') = '{$prevYm}'
|
|
")->fetchColumn();
|
|
|
|
// ------------------------------
|
|
// 🔥 전월 대비 증감 (⭐ 핵심)
|
|
// ------------------------------
|
|
$diff_copy = $curr_copy - $prev_copy; // 예: 166 - 228 = -62
|
|
$diff_amount = $curr_amount - $prev_amount;
|
|
|
|
// ------------------------------
|
|
// 🔥 (선택) 달성률도 "월 기준"으로 비교
|
|
// ------------------------------
|
|
$curr_rate = ($yearly_target_qty > 0)
|
|
? round(($curr_copy / $yearly_target_qty) * 100, 1)
|
|
: 0;
|
|
|
|
$prev_rate = ($yearly_target_qty > 0)
|
|
? round(($prev_copy / $yearly_target_qty) * 100, 1)
|
|
: 0;
|
|
|
|
$diff_rate = round($curr_rate - $prev_rate, 1);
|
|
|
|
|
|
|
|
|
|
// ------------------------------
|
|
// 이번 달 주차별 판매량
|
|
// ------------------------------
|
|
$weekly = [
|
|
1 => 0,
|
|
2 => 0,
|
|
3 => 0,
|
|
4 => 0,
|
|
5 => 0
|
|
];
|
|
|
|
$rows = $pdo->query("
|
|
SELECT
|
|
CEIL(DAY(sales_date) / 7) AS week_no,
|
|
SUM(quantity) AS qty
|
|
FROM sales_results
|
|
WHERE sales_date BETWEEN '{$year}-" . date("m") . "-01'
|
|
AND LAST_DAY('{$year}-" . date("m") . "-01')
|
|
GROUP BY week_no
|
|
")->fetchAll();
|
|
|
|
foreach ($rows as $r) {
|
|
$week = (int)$r['week_no'];
|
|
if ($week >= 1 && $week <= 5) {
|
|
$weekly[$week] = (int)$r['qty'];
|
|
}
|
|
}
|
|
|
|
// ------------------------------
|
|
// 🔥 개인별 연간 실적
|
|
// ------------------------------
|
|
$members = $pdo->query("
|
|
SELECT DISTINCT emp_no, emp_name, position
|
|
FROM sales_members
|
|
")->fetchAll();
|
|
|
|
// 🔥 표시 순서를 강제 지정 (네가 원하는 순서)
|
|
$order = ["J01201", "223070"]; // 권혁진, 염승호
|
|
|
|
// 🔥 필요한 사람만 필터링
|
|
$members = array_filter($members, function($m) use ($order){
|
|
return in_array($m['emp_no'], $order);
|
|
});
|
|
|
|
// 🔥 강제 순서 정렬
|
|
usort($members, function($a, $b) use ($order) {
|
|
return array_search($a['emp_no'], $order) <=> array_search($b['emp_no'], $order);
|
|
});
|
|
|
|
$individual = [];
|
|
|
|
foreach ($members as $m) {
|
|
$emp_no = $m['emp_no'];
|
|
|
|
// 월별 판매량
|
|
$rows = $pdo->query("
|
|
SELECT MONTH(sales_date) AS m, SUM(quantity) AS qty
|
|
FROM sales_results
|
|
WHERE TRIM(TRAILING ')' FROM SUBSTRING_INDEX(emp_no, '(', -1)) = '{$emp_no}'
|
|
AND sales_date BETWEEN '{$yearStart}' AND '{$yearEnd}'
|
|
GROUP BY MONTH(sales_date)
|
|
ORDER BY m
|
|
")->fetchAll();
|
|
|
|
$monthly2 = array_fill(1, 12, 0);
|
|
foreach ($rows as $r) {
|
|
$monthly2[(int)$r['m']] = (int)$r['qty'];
|
|
}
|
|
|
|
$total2 = array_sum($monthly2);
|
|
|
|
$target2 = $pdo->query("
|
|
SELECT COALESCE(SUM(target_qty), 0)
|
|
FROM sales_targets
|
|
WHERE TRIM(TRAILING ')' FROM SUBSTRING_INDEX(emp_no, '(', -1)) = '{$emp_no}'
|
|
AND LEFT(target_month, 4) = '{$year}'
|
|
")->fetchColumn();
|
|
|
|
$rate2 = ($target2 > 0)
|
|
? round(($total2 / $target2) * 100, 1)
|
|
: 0;
|
|
|
|
$individual[] = [
|
|
"emp_no" => $emp_no,
|
|
"name" => $m['emp_name'],
|
|
"position" => $m['position'],
|
|
"monthly" => array_values($monthly2),
|
|
"total" => $total2,
|
|
"target_qty" => (int)$target2,
|
|
"rate" => $rate2
|
|
];
|
|
}
|
|
|
|
// 📌 이번달 YYYY-MM 구하기
|
|
$issueMonth = date("Y-m");
|
|
|
|
// 📌 이슈 불러오기
|
|
$stmt = $pdo->prepare("
|
|
SELECT issue_text
|
|
FROM sales_month_issues
|
|
WHERE issue_month = ?
|
|
LIMIT 1
|
|
");
|
|
$stmt->execute([$issueMonth]);
|
|
$issueText = $stmt->fetchColumn() ?: "";
|
|
|
|
|
|
|
|
// ------------------------------
|
|
// 최종 JSON 출력
|
|
// ------------------------------
|
|
echo json_encode([
|
|
"status" => "ok",
|
|
"year" => $year,
|
|
|
|
"issue_text" => $issueText,
|
|
|
|
// 연간 실적
|
|
"total_copy" => (int)$total_copy,
|
|
"total_amount" => (int)$total_amount,
|
|
"yearly_target_qty" => (int)$yearly_target_qty,
|
|
"yearly_rate" => $yearly_rate,
|
|
|
|
// 월별 그래프 데이터
|
|
"monthly" => $monthly,
|
|
|
|
// 개인별 / 업종별 / 기관별
|
|
"persons" => $persons,
|
|
"industry" => $industry,
|
|
"institution" => $institution,
|
|
"client_count" => (int)$client_count,
|
|
|
|
// 이번 달 목표
|
|
"target_qty" => (int)$target_qty,
|
|
"month_sales" => (int)$month_sales,
|
|
"target_rate" => $target_rate,
|
|
|
|
"month_sales_amount" => (int)$month_sales_amount,
|
|
|
|
//증감
|
|
"diff_copy" => $diff_copy,
|
|
"diff_amount" => $diff_amount,
|
|
"diff_rate" => $diff_rate,
|
|
|
|
"weekly" => $weekly,
|
|
|
|
"individual" => $individual,
|
|
], JSON_UNESCAPED_UNICODE);
|
|
?>
|