626 lines
17 KiB
PHP
626 lines
17 KiB
PHP
<?php
|
|
////////////////// 퍼스트가든 VIP 관리툴 함수들 //////////////////
|
|
// 행 갯수 구하기
|
|
function get_num_rows($table, $where="") {
|
|
$sql_common = sql_query("SELECT * FROM {$table} {$where}");
|
|
$result = sql_num_rows($sql_common);
|
|
|
|
return $result;
|
|
}
|
|
|
|
// 결과 가져오기
|
|
function get_result($table, $ord_by="", $query_limit="", $where="") {
|
|
$query = "SELECT * FROM {$table} {$where} {$ord_by} {$query_limit}";
|
|
$result = sql_query($query);
|
|
$R = array();
|
|
while($row=sql_fetch_array($result)) { // 전체 배열에 저장
|
|
array_push($R, $row);
|
|
}
|
|
|
|
return $R;
|
|
}
|
|
|
|
// 페이지 아이템 갯수 구하기
|
|
function get_item_count($table) {
|
|
$t_query = sql_query("SELECT * FROM {$table}"); // 전체 숫자 구함
|
|
$result = sql_num_rows($t_query);
|
|
|
|
return $result;
|
|
}
|
|
|
|
// 페이지 이름 가져오기
|
|
function get_page_name($page, $setpage) {
|
|
$page_name = array_search($page, array_column($setpage, 'pid'));
|
|
return $setpage[$page_name]['pname'];
|
|
}
|
|
|
|
// 테이블 내 필드 수 구하기
|
|
function get_total_count($db_name) {
|
|
$t_query = sql_query("SELECT * FROM {$db_name}"); // 전체 숫자 구함
|
|
$result = sql_num_rows($t_query);
|
|
|
|
return $result;
|
|
}
|
|
|
|
|
|
// 연간회원권
|
|
// 시작일 기준 종료일 만들기
|
|
function getAnnualEdate($date) {
|
|
$date = new DateTime($date);
|
|
$date->modify('+1 Year');
|
|
$date->modify('-1 Day');
|
|
return $date->format('Y-m-d');
|
|
}
|
|
|
|
// 연간회원 권종구분 가져오기
|
|
function getAnnualGroup($used = 0){
|
|
global $fg;
|
|
|
|
$query = "SELECT * FROM {$fg['annual_category_table']}";
|
|
|
|
if ($used == 1){ // 사용하는 카테고리만
|
|
$query .= " WHERE used = '{$used}'";
|
|
}
|
|
|
|
$result = sql_query($query);
|
|
|
|
$gr = array();
|
|
while($category=sql_fetch_array($result)){
|
|
array_push($gr,$category);
|
|
}
|
|
|
|
return $gr;
|
|
}
|
|
|
|
// 회원 상태 가져오기
|
|
function getAnnualStatus($mem_no){
|
|
global $fg;
|
|
|
|
$query = "SELECT status FROM {$fg['annual_member_table']}";
|
|
|
|
$query .= " WHERE mem_no = '{$mem_no}'";
|
|
|
|
$result = sql_fetch($query);
|
|
$result = $result['status'];
|
|
/* $gr = array();
|
|
while($category=sql_fetch_array($result)){
|
|
array_push($gr,$category);
|
|
}
|
|
*/
|
|
return $result;
|
|
}
|
|
|
|
// 계정관련(직원)
|
|
// 부서 정보 가져오기
|
|
function getTeamName($used = 0){
|
|
global $fg;
|
|
|
|
$where = ""; // $where 초기화
|
|
if ($used == 1) $where = " WHERE tused = '{$used}'";
|
|
$query = "SELECT * FROM {$fg['member_group_table']} {$where} ORDER BY tname";
|
|
|
|
$r = sql_query($query);
|
|
|
|
$result = array();
|
|
while($row=sql_fetch_array($r)){
|
|
array_push($result,$row);
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
// 구성원 수 구하기
|
|
function getMemberCount($team){
|
|
global $fg;
|
|
|
|
$where = " WHERE tid = '{$team}'";
|
|
$query = "SELECT * FROM {$fg['member_table']} {$where}";
|
|
|
|
$row = sql_query($query);
|
|
$result = sql_num_rows($row);
|
|
|
|
return $result;
|
|
}
|
|
|
|
// 멤버정보 가져오기
|
|
function getMemberInfo($id){
|
|
global $fg;
|
|
|
|
$where = " WHERE user_id = '{$id}'";
|
|
$query = "SELECT * FROM {$fg['member_table']} {$where}";
|
|
|
|
$result = sql_fetch($query);
|
|
|
|
return $result;
|
|
}
|
|
|
|
|
|
// VIP카드 관련
|
|
// VIP관련
|
|
// 카테고리명 가져오기
|
|
function getVipCatName($used = 0){
|
|
global $fg;
|
|
|
|
$query = "SELECT * FROM {$fg['vip_category_table']}";
|
|
if ($used == 1){
|
|
$query .= " WHERE ca_use = '{$used}'";
|
|
}
|
|
|
|
$result = sql_query($query);
|
|
|
|
$gr = array();
|
|
while($category=sql_fetch_array($result)){
|
|
array_push($gr,$category);
|
|
}
|
|
|
|
return $gr;
|
|
}
|
|
|
|
// VIP그룹별 유효기간 가져오기
|
|
function getVipMemberLimit($gr_id){
|
|
global $fg;
|
|
|
|
$query = "SELECT * FROM {$fg['vip_category_table']} WHERE gr_id = '{$gr_id}'";
|
|
$getdate = sql_fetch($query);
|
|
$result = $getdate['gr_date'];
|
|
|
|
return $result;
|
|
}
|
|
|
|
// 재고
|
|
/*
|
|
VIP카드 재고 등록
|
|
개별 등록 또는 순차 등록만 허용함
|
|
등록 전 해당 번호 구간에 대해 이미 있는지 확인하고 있다면 오류를 반환, 없다면 등록함
|
|
해당 카드에 대해 추적하고 수량 관리를 하기 위함
|
|
*/
|
|
|
|
|
|
function get_vip_stock(){
|
|
global $fg;
|
|
|
|
$query = "SELECT * FROM {$fg['vip_card_stock_table']} ";
|
|
|
|
$r=sql_query($query);
|
|
|
|
$result = array();
|
|
while($row=sql_fetch_array($r)){
|
|
array_push($result,$row);
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
// 상태별 값 가져오기
|
|
function get_qty($status){
|
|
global $fg;
|
|
|
|
$input = array('입고');
|
|
$dispo = array('폐기', '복구');
|
|
|
|
switch ($status) {
|
|
case '입고' :
|
|
$sel = 'input';
|
|
break;
|
|
case '출고' :
|
|
$sel = 'output';
|
|
break;
|
|
case in_array($status,$dispo) :
|
|
$sel = 'dispo';
|
|
break;
|
|
}
|
|
|
|
if (in_array($status, $dispo)) {
|
|
$where = " WHERE status = '폐기' OR status = '복구'";
|
|
} else {
|
|
$where = " WHERE status = '{$status}'";
|
|
}
|
|
|
|
$query = "SELECT sum({$sel}) FROM {$fg['vip_card_stock_table']} ".$where;
|
|
|
|
$result = sql_fetch($query);
|
|
|
|
$fname = 'sum('.$sel.')'; // sql_fetch를 사용하면 sum(필드명)으로 나오기 때문에
|
|
$result = $result[$fname]; // 배열을 값으로 변경
|
|
|
|
return $result;
|
|
}
|
|
|
|
// 그룹 출고수량 가져오기
|
|
function get_gr_qty($gid = 0, $status = ""){
|
|
global $fg;
|
|
|
|
if($gid > 0 && $status) {
|
|
$where = " WHERE gr_id='{$gid}' AND status='{$status}'";
|
|
} else if($gid > 0) {
|
|
$where = " WHERE gr_id='{$gid}'";
|
|
} else if($status) {
|
|
$where = " WHERE status='{$status}'";
|
|
} else {
|
|
$where = "";
|
|
}
|
|
|
|
$query = "SELECT * FROM {$fg['vip_list_table']} ".$where;
|
|
$result = sql_num_rows(sql_query($query));
|
|
|
|
return $result;
|
|
}
|
|
|
|
// 팀 재고 가져오기
|
|
function get_team_qty($tid = 0, $status = ""){
|
|
global $fg;
|
|
|
|
if($tid != 0 || $status) $where = " WHERE ";
|
|
if($tid != 0) $where .= "rec_team = '{$tid}' ";
|
|
if($tid != 0 && $status) $where .= "AND ";
|
|
if($status) $where .= "status = '{$status}' ";
|
|
|
|
$query = "SELECT * FROM {$fg['vip_list_table']} ".$where;
|
|
$item_qty = sql_num_rows(sql_query($query));
|
|
|
|
return $item_qty;
|
|
}
|
|
|
|
// log 기록
|
|
function log_update($work, $work_detail, $userid, $time) {
|
|
global $fg;
|
|
|
|
$result = sql_query("INSERT INTO {$fg['log_table']} (work, work_detail, id, date) VALUES ('{$work}', '{$work_detail}', '{$userid}', '{$time}')");
|
|
|
|
return $result;
|
|
}
|
|
|
|
// 베이커리 관련
|
|
|
|
// 작성자 불러오기
|
|
function getAuthorInfo($date) {
|
|
global $fg;
|
|
$query = "SELECT * FROM {$fg['bakery_author_table']} WHERE date = '{$date}'";
|
|
$result = sql_fetch($query);
|
|
if ($result) {
|
|
return $result;
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// 값이 있는지 확인
|
|
function getBakeryInvenRow($date) {
|
|
global $fg;
|
|
$query = "SELECT * FROM {$fg['bakery_inventory_table']} WHERE date = '{$date}'";
|
|
$result = sql_num_rows(sql_query($query));
|
|
|
|
return $result;
|
|
}
|
|
|
|
// 상품 데이터 불러오기
|
|
function getBakeryInvenData($date, $ord_by = "product_name ASC") {
|
|
global $fg;
|
|
|
|
// LEFT JOIN을 사용하여 bakery_product_table과 연결하고 used가 1인 데이터만 불러옴
|
|
$query = "SELECT a.*, b.used, b.product_name
|
|
FROM {$fg['bakery_inventory_table']} AS a
|
|
LEFT JOIN {$fg['bakery_product_table']} AS b
|
|
ON a.barcode = b.barcode
|
|
WHERE b.used = 1 AND a.date = '{$date}'
|
|
ORDER BY {$ord_by}
|
|
";
|
|
error_log($query);
|
|
$result = sql_query($query);
|
|
$R = array();
|
|
|
|
while ($row = sql_fetch_array($result)) {
|
|
array_push($R, $row);
|
|
}
|
|
|
|
return $R;
|
|
}
|
|
|
|
function getBakeryInvenDataPeriod($start_date, $end_date, $searchWord = "", $ord_by = "a.date ASC, b.product_name ASC") {
|
|
global $fg;
|
|
|
|
// 검색 조건
|
|
$search_sql = "";
|
|
if ($searchWord !== "") {
|
|
$searchWord = sql_real_escape_string($searchWord);
|
|
$search_sql = "AND b.product_name LIKE '%{$searchWord}%'";
|
|
}
|
|
|
|
$query = "SELECT
|
|
b.product_name,
|
|
SUM(a.production) AS production,
|
|
SUM(a.inhouse_use) AS inhouse_use,
|
|
SUM(a.recycling) AS recycling,
|
|
SUM(a.disposal) AS disposal,
|
|
SUM(a.sales) AS sales,
|
|
SUM(a.payment_amount) AS payment_amount
|
|
FROM {$fg['bakery_inventory_table']} AS a
|
|
LEFT JOIN {$fg['bakery_product_table']} AS b
|
|
ON a.barcode = b.barcode
|
|
WHERE b.used = 1
|
|
AND a.date BETWEEN '{$start_date}' AND '{$end_date}'
|
|
{$search_sql}
|
|
GROUP BY a.barcode
|
|
ORDER BY {$ord_by}";
|
|
|
|
error_log($query);
|
|
|
|
$result = sql_query($query);
|
|
$R = array();
|
|
|
|
while ($row = sql_fetch_array($result)) {
|
|
$R[] = $row;
|
|
}
|
|
|
|
return $R;
|
|
}
|
|
|
|
// 기간별 합계 수량 구하기
|
|
function getBakeryInvenSumPeriod($start_date, $end_date, $searchWord = "") {
|
|
global $fg;
|
|
|
|
$search_sql = "";
|
|
if ($searchWord !== "") {
|
|
$searchWord = sql_real_escape_string($searchWord);
|
|
$search_sql = "AND b.product_name LIKE '%{$searchWord}%'";
|
|
}
|
|
|
|
$query = "SELECT
|
|
SUM(a.production) AS t_production,
|
|
SUM(a.inhouse_use) AS t_inhouse_use,
|
|
SUM(a.recycling) AS t_recycling,
|
|
SUM(a.disposal) AS t_disposal,
|
|
SUM(a.sales) AS t_sales,
|
|
SUM(a.payment_amount) AS t_payment_amount
|
|
FROM {$fg['bakery_inventory_table']} AS a
|
|
LEFT JOIN {$fg['bakery_product_table']} AS b
|
|
ON a.barcode = b.barcode
|
|
WHERE b.used = 1
|
|
AND a.date BETWEEN '{$start_date}' AND '{$end_date}'
|
|
{$search_sql}";
|
|
|
|
error_log($query);
|
|
|
|
return sql_fetch($query); // 결과 없으면 false 반환
|
|
}
|
|
|
|
// 누계 구하기
|
|
function getBakeryInvenSummaryData($date, $ord_by = "b.product_name ASC") {
|
|
global $fg;
|
|
|
|
// 해당 월의 시작일과 종료일 계산
|
|
$month_start = date('Y-m-01', strtotime($date));
|
|
$month_end = $date;
|
|
|
|
// 전월 마지막 날짜 계산
|
|
$prev_month_last_day = date('Y-m-t', strtotime($month_start . ' -1 month'));
|
|
|
|
// 전월 current_stock 조회
|
|
$prev_query = "
|
|
SELECT SUM(current_stock) AS total_previous_stock
|
|
FROM {$fg['bakery_inventory_table']} AS a
|
|
LEFT JOIN {$fg['bakery_product_table']} AS b
|
|
ON a.barcode = b.barcode
|
|
WHERE b.used = 1
|
|
AND a.date = '{$prev_month_last_day}'
|
|
";
|
|
$prev_result = sql_fetch($prev_query);
|
|
$total_previous_stock = $prev_result['total_previous_stock'] ?? '';
|
|
|
|
// 현재 월 누적 합산 조회
|
|
$query = "
|
|
SELECT
|
|
SUM(a.production) AS total_production,
|
|
SUM(a.inhouse_use) AS total_inhouse_use,
|
|
SUM(a.recycling) AS total_recycling,
|
|
SUM(a.disposal) AS total_disposal,
|
|
SUM(a.sales) AS total_sales,
|
|
SUM(a.sales_amount) AS total_sales_amount,
|
|
SUM(a.menu_discount) AS total_menu_discount,
|
|
SUM(a.payment_amount) AS total_payment_amount
|
|
FROM {$fg['bakery_inventory_table']} AS a
|
|
LEFT JOIN {$fg['bakery_product_table']} AS b
|
|
ON a.barcode = b.barcode
|
|
WHERE b.used = 1
|
|
AND a.date BETWEEN '{$month_start}' AND '{$month_end}'
|
|
";
|
|
$result = sql_fetch($query);
|
|
|
|
// previous_stock 추가
|
|
$result['total_previous_stock'] = $total_previous_stock;
|
|
|
|
return $result;
|
|
}
|
|
|
|
// 전체 품명 불러오기
|
|
function getBakeryProductList($ord_by = "ASC") {
|
|
global $fg;
|
|
|
|
$query = "SELECT * FROM {$fg['bakery_product_table']} ORDER BY product_name {$ord_by}";
|
|
$result = sql_query($query);
|
|
$R = array();
|
|
while($row=sql_fetch_array($result)) { // 전체 배열에 저장
|
|
array_push($R, $row);
|
|
}
|
|
return $R;
|
|
}
|
|
|
|
// 사용 중인(used=1) 품목 수 구하기
|
|
function cntUsedBakeryItems() {
|
|
global $fg;
|
|
|
|
$query = "SELECT COUNT(*) AS cnt FROM {$fg['bakery_product_table']} WHERE used = 1";
|
|
|
|
error_log($query);
|
|
|
|
$result = sql_fetch($query);
|
|
$result = $result['cnt'];
|
|
|
|
return $result;
|
|
}
|
|
|
|
|
|
// 어제자 최종재고 불러오기
|
|
function getPrevStock($date, $barcode) {
|
|
global $fg;
|
|
|
|
$prev_date = date("Y-m-d", strtotime("-1 day", strtotime($date)));
|
|
|
|
// 바코드를 기준으로 어제자 값을 가져옴
|
|
$query = "SELECT current_stock FROM {$fg['bakery_inventory_table']} WHERE date = '{$prev_date}' AND barcode = '{$barcode}'";
|
|
$result = sql_fetch($query);
|
|
|
|
if ($result) {
|
|
return $result['current_stock'];
|
|
} else {
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
// 데이터가 있는지 확인
|
|
function getBakeryRowData($barcode, $searchDate) {
|
|
global $fg;
|
|
|
|
$query = "SELECT * FROM {$fg['bakery_inventory_table']} WHERE barcode = '$barcode' AND date = '$searchDate'";
|
|
$result = sql_fetch($query);
|
|
|
|
if ($result) {
|
|
return $result;
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// 계정에 따른 수정 가능 처리
|
|
function bakeryChkDate($date, $userid) {
|
|
$today = new DateTime(); // 오늘 날짜
|
|
if ( $userid == "bakery" ) { // 베이커리 계정은 7일 전까지의 데이터만 수정 가능
|
|
$dateChk = $today->modify('-7 days');
|
|
} else { // 나머지 계정은 14일 전
|
|
$dateChk = $today->modify('-14 days');
|
|
}
|
|
// 비교를 위해 입력 날짜를 DateTime 객체로 변환
|
|
$inputDate = new DateTime($date);
|
|
|
|
// 날짜 비교
|
|
return $inputDate <= $dateChk;
|
|
}
|
|
|
|
// DB 현재고 업데이트 함수
|
|
function bakeryCurrentStockUpdate($searchDate, $barcode, $current_stock) {
|
|
global $fg;
|
|
|
|
$query = "UPDATE {$fg['bakery_inventory_table']} SET current_stock = {$current_stock} WHERE date = '{$searchDate}' AND barcode = '{$barcode}'";
|
|
$result = sql_query($query);
|
|
|
|
return $result;
|
|
}
|
|
|
|
// 사전 목록 생성
|
|
function bakeryPreUpdate($searchDate, $edit_datetime) {
|
|
global $fg;
|
|
|
|
$result = false;
|
|
$isDebug = true;
|
|
$prevDate = date("Y-m-d", strtotime("$searchDate -1 day")); // 하루 전 날짜 구하기
|
|
|
|
$today = new DateTime();
|
|
$searchDateObj = new DateTime($searchDate);
|
|
|
|
if ($isDebug) error_log("bakeryPreUpdate started for searchDate: $searchDate, edit_datetime: $edit_datetime, prevDate: $prevDate");
|
|
|
|
// 오늘자 데이터 갯수와 어제자 데이터 갯수가 다르면 업데이트(페이지가 로딩될 때마다 업데이트 하는것을 방지)
|
|
$cntRecordQuery = "SELECT COUNT(*) as cnt FROM `{$fg['bakery_product_table']}`;";
|
|
$cntRecordResult = sql_query($cntRecordQuery);
|
|
$cntRecord = sql_fetch_array($cntRecordResult)['cnt'];
|
|
|
|
$searchDateRow = getBakeryInvenRow($searchDate);
|
|
|
|
if ($searchDateRow != getBakeryInvenRow($prevDate) || ($today <= $searchDateObj && $searchDateRow != $cntRecord) ) {
|
|
if ($isDebug) error_log("Inventory row difference detected.");
|
|
|
|
/*
|
|
// 어제 날짜의 데이터를 가져옴
|
|
$chkQuery = "SELECT a.date, a.barcode, b.product_name
|
|
FROM {$fg['bakery_inventory_table']} AS a
|
|
LEFT JOIN {$fg['bakery_product_table']} AS b
|
|
ON a.barcode = b.barcode
|
|
WHERE a.date = '{$prevDate}' AND b.used = 1";
|
|
*/
|
|
|
|
// product_table에서 used = 1인 product_name과 barcode를 가지고 옴
|
|
$chkQuery = "SELECT * FROM {$fg['bakery_product_table']} WHERE used = 1";
|
|
|
|
$chkResult = sql_query($chkQuery);
|
|
if ($isDebug) error_log("chkQuery: $chkQuery");
|
|
|
|
// chkResult 출력 확인
|
|
if (!$chkResult) {
|
|
if ($isDebug) error_log("chkResult is empty.");
|
|
return false;
|
|
}
|
|
|
|
if ($isDebug) error_log("chkResult obtained.");
|
|
|
|
// 업데이트
|
|
while ($row = sql_fetch_array($chkResult)) {
|
|
if ($isDebug) error_log("Processing row: " . print_r($row, true));
|
|
|
|
// `bakery_product_table`에 바코드가 없는 경우 추가
|
|
$checkQuery = "SELECT COUNT(*) as cnt FROM {$fg['bakery_product_table']} WHERE barcode = '{$row['barcode']}'";
|
|
$checkResult = sql_query($checkQuery);
|
|
$count = sql_fetch_array($checkResult)['cnt'];
|
|
|
|
if ($isDebug) error_log("Barcode count for {$row['barcode']} is $count");
|
|
|
|
if ($count == 0) {
|
|
if ($isDebug) error_log("Inserting new product with barcode: {$row['barcode']}");
|
|
$insertQuery = "INSERT INTO {$fg['bakery_product_table']} (product_name, barcode, used) VALUES ('{$row['product_name']}', '{$row['barcode']}', 1)";
|
|
sql_query($insertQuery);
|
|
}
|
|
|
|
// LEFT JOIN을 통해 `used`가 1인 항목만 업데이트
|
|
$updateQuery = "INSERT IGNORE INTO {$fg['bakery_inventory_table']} (date, barcode, edit_datetime)
|
|
VALUES ('{$searchDate}', '{$row['barcode']}', '{$edit_datetime}')";
|
|
/*
|
|
SELECT '{$searchDate}', b.barcode, '{$edit_datetime}'
|
|
FROM {$fg['bakery_inventory_table']} AS a
|
|
LEFT JOIN {$fg['bakery_product_table']} AS b
|
|
ON a.barcode = b.barcode
|
|
WHERE b.used = 1 AND a.date = '{$prevDate}'";
|
|
*/
|
|
|
|
if ($isDebug) error_log("Executing updateQuery: $updateQuery");
|
|
$result = sql_query($updateQuery);
|
|
|
|
|
|
}
|
|
}
|
|
|
|
if ($isDebug) error_log("bakeryPreUpdate completed with result: " . ($result ? 'true' : 'false'));
|
|
return $result;
|
|
}
|
|
|
|
// 기타
|
|
// 하이픈 넣기
|
|
function addTelHyphen($tel)
|
|
{
|
|
$tel = preg_replace("/[^0-9]/", "", $tel);
|
|
if (substr($tel,0,2)=='02')
|
|
return preg_replace("/([0-9]{2})([0-9]{3,4})([0-9]{4})$/", "\\1-\\2-\\3", $tel);
|
|
else if (strlen($tel)=='8' && (substr($tel,0,2)=='15' || substr($tel,0,2)=='16' || substr($tel,0,2)=='18'))
|
|
return preg_replace("/([0-9]{4})([0-9]{4})$/", "\\1-\\2", $tel);
|
|
else
|
|
return preg_replace("/([0-9]{3})([0-9]{3,4})([0-9]{4})$/", "\\1-\\2-\\3", $tel);
|
|
}
|
|
/*
|
|
// 브라우저 캐시 초기화
|
|
function clearBrowserCache() {
|
|
header("Pragma: no-cache");
|
|
header("Cache: no-cache");
|
|
header("Cache-Control: no-cache, must-revalidate");
|
|
header("Expires:Mon, 26 Jul 1997 05:00:00 GMT");
|
|
}
|
|
*/
|
|
|