[title]="첩약보험 약재가격 입력루틴";
작성자 정보
- 삼둡 작성
- 작성일
컨텐츠 정보
- 2,119 조회
- 목록
본문
$allupdatesql="";
$currentYear = date('Y'); // 현재 년도
$currentQuarter = ceil(date('n') / 3); // 현재 분기
echo "현재의 사분기는 다음과 같습니다.", $currentYear,"-",$currentQuarter, "<br>\n";
***
$orgcurrentYear=$currentYear;
$orgcurrentQuarter=$currentQuarter;
***
// Function to calculate average cost per gram
function calculateAverageCost($totalCost, $totalQuantity) {
if ($totalQuantity > 0) {
return $totalCost / $totalQuantity;
} else {
return 0;
}
}
// Get the current year and quarter
$currentYear = date('Y');
$currentQuarter = ceil(date('n') / 3);
// Iterate over all quarters until data is found or until we reach the first quarter of the year 1970
$tablename= "herb"; //
$search_where="WHERE stock > 0 ";
$orderby="order by herb asc";
//전체 글 수를 구합니다. (쿼리문을 사용하여 결과를 배열로 저장하는 일반적인 방법)
$query="select count(*) from $tablename $search_where $orderby"; // SQL 쿼리문을 문자열 변수에 일단 저장하고
echo $query;
//$result=mysql_query($query) or die (mysql_error()); // 위의 쿼리문을 실제로 실행하여 결과를 result에 저장한 >다음
$result=sql_q2($query);
$row=mysql_fetch_row($result); //위 결과 값을 하나하나 배열로 저장합니다.
$total_no=$row[0];
echo "<h1> $total_no </h1>";
$query="SELECT herb, cost from $tablename $search_where $orderby "; // SQL 쿼리문
//echo $query;
//echo $query,":::",$total_page,",",$cur_num;
//$result=mysql_query($query) or die (mysql_error()); // 쿼리문을 실행 결과
//쿼리 결과를 하나씩 불러와 실제 HTML에 나타내는 것은 HTML 문 중간에 삽입합니다.
$result=sql_q2($query);
$herbcount=0;
while($array=mysql_fetch_array($result)) { //herb를 돌리는 루프
$herbcount++;
//if ($herbcount==10) { echo "10개 넘었어요";break; }
$herb=$array['herb'];
$excost=$array['cost'];
$today = new DateTime(); // 현재 날짜 및 시간을 가져옵니다.
$year = (int)$today->format('Y'); // 연도를 가져옵니다.
$quarter = ceil((int)$today->format('n') / 3); // 분기를 계산합니다.
echo "<h2> $herb 의 입고상태를 체크해보겠습니다. </h2> \n";
$timer=0;
while ($year> 2018) {
$timer++;
if ($timer==500000)
{ break; }
$quarter--; // If it's the first quarter of the year, consider the last quarter of the previous year
if ($quarter == 0) {
$quarter = 4;
$year--;
} //쿼터를 하나 줄이고 0되면 전년도 4분기로 변경
$currentYear = $year; // 현재 연도
$currentQuarter = $quarter; // 현재 분기
echo "$year-$quarter <--- this check <br> \n";
// 현재 사분기의 첫 날 계산
// 현재 사분기의 첫 날 계산
$startThisQuarterDate = date("Y-m-d", strtotime($currentYear . "-". (($currentQuarter - 1) * 3 + 1) . "-01"));
// 현재 사분기의 끝 날 계산
$endThisQuarterDate = date("Y-m-t", strtotime($currentYear . "-". ($currentQuarter * 3) . "-01"));
$avgPrevCostPerGram = 0;
// Get the data for the previous quarter
$ssql = "SELECT count(*) FROM used_herb
WHERE herb='$herb' AND mode='입고' AND time>= '" . $startThisQuarterDate . "'
AND time <= '" . $endThisQuarterDate. "'";
$rresult=sql_q2($ssql);
$row=mysql_fetch_row($rresult);
$rrows=$row[0];
if ($rrows) {
echo "&& $rrows && 이번 사분기에는 이 갯수만큼의 입고가 확인되었습니다. <br> \n"; }
$ssql = "SELECT herb,
cost , using_gram
FROM used_herb
WHERE herb='$herb' AND mode='입고' AND time>= '" . $startThisQuarterDate . "'
AND time <= '" . $endThisQuarterDate. "'";
// echo "<pre> $ssql </pre> \n <br> ";
$rresult = sql_q2($ssql);
if ($rrows > 0) {
$totalPrevCost = 0;
$totalPrevQuantity = 0;
$herbcount=0;
while ($row = mysql_fetch_array($rresult)) {
$totalPrevCost += $row["cost"];
$herbcount++;
}//while 해당 구문에서 나오는 코스트와 양을 모두 잡아놓는다. 그래서 다 더한다.
//$avgPrevCostPerGram = calculateAverageCost($totalPrevCost, $totalPrevQuantity);
$avgPrevCostPerGram = $totalPrevCost / $herbcount;
// Update the herb table with the calculated average cost per gram for the matching herbname
$cost = round($avgPrevCostPerGram);
$updateSql = "UPDATE herb
SET cost = $cost
WHERE herb='$herb';";
// $conn->query($updateSql);
echo "<pre> $updateSql </pre> \n <br> ";
$time = time();
$time=date("Y-m-d H:i:s", $time);
$historysql="insert into herbhistory set herb='$herb', event='단가변경', cost='$cost', comment='$herb:$excost -> $cost:전사분기 고려 단가변경', write_time='$time';";
$allupdatesql .=$updateSql . "<br>\n". $historysql."<br>\n<br>\n";
echo "Herb table updated successfully for quarter $currentQuarter in $currentYear. <br> updated into $herb --> cost: $avgPrevCostPerGram = $totalPrevCost / $herbcount <br><br><br> \n";
break; // Break the loop once data is found and updated
}// if num_rows>0
// Move to the previous quarter
//전사분기의 데이터가 없다면
//이번 사분기의 데이터가 있는지 체크해보아야한다.
// 현재 사분기의 첫 날 계산
$startThisQuarterDate = date("Y-m-d", strtotime($orgcurrentYear . "-". (($orgcurrentQuarter - 1) * 3 + 1) . "-01"));
// 현재 사분기의 끝 날 계산
$endThisQuarterDate = date("Y-m-t", strtotime($orgcurrentYear . "-". ($orgcurrentQuarter * 3) . "-01"));
$avgPrevCostPerGram = 0;
// Get the data for the previous quarter
$ssql = "SELECT count(*) FROM used_herb
WHERE herb='$herb' AND mode='입고' AND time>= '" . $startThisQuarterDate . "'
AND time <= '" . $endThisQuarterDate. "'";
$rresult=sql_q2($ssql);
$row=mysql_fetch_row($rresult);
$rrows=$row[0];
if ($rrows) {
echo "&& $rrows &&%%%%% 전 사분기 입고가 없어, 현 사분기를 검사했더니, 현 사분기에는 이 갯수만큼의 입고가 확인되었습니다. <br> \n";
$ssql = "SELECT herb,
cost , using_gram
FROM used_herb
WHERE herb='$herb' AND mode='입고' AND time>= '" . $startThisQuarterDate . "'
AND time <= '" . $endThisQuarterDate. "'";
// echo "<pre> $ssql </pre> \n <br> ";
$rresult = sql_q2($ssql);
$totalPrevCost = 0;
$totalPrevQuantity = 0;
$herbcount=0;
while ($row = mysql_fetch_array($rresult)) {
$totalPrevCost += $row["cost"];
$herbcount++;
}//while 해당 구문에서 나오는 코스트와 양을 모두 잡아놓는다. 그래서 다 더한다.
//$avgPrevCostPerGram = calculateAverageCost($totalPrevCost, $totalPrevQuantity);
$avgPrevCostPerGram = $totalPrevCost / $herbcount;
// Update the herb table with the calculated average cost per gram for the matching herbname
$cost = round($avgPrevCostPerGram);
$updateSql = "UPDATE herb
SET cost = $cost
WHERE herb='$herb';";
// $conn->query($updateSql);
echo "<pre> $updateSql </pre> \n <br> ";
$time = time();
$time=date("Y-m-d H:i:s", $time);
$historysql="insert into herbhistory set herb='$herb', event='단가변경', cost='$cost', comment='$herb:$excost -> $cost:전사분기 없어서 현사분기로 단가책정', write_time='$time';";
$allupdatesql .=$updateSql . "<br>\n". $historysql."<br>\n<br>\n";
echo "Herb table updated successfully for quarter $currentQuarter in $currentYear. <br> updated into $herb --> cost: $avgPrevCostPerGram = $totalPrevCost / $herbcount <br><br><br> \n";
break; // Break the loop once data is found and updated
} //전사분기 데이터 없었고, 현 사분기의 데이터는 있어서, 약재가 반영 끝, 아래로는 그것도 없어서 전전전으로 루프돌리러감
//현재 사분기 약재 검사 끝. 2024-04-30의 디버그 끝
$currentQuarter--;
// If it's the first quarter of the year, consider the last quarter of the previous year
if ($currentQuarter == 0) {
$currentQuarter = 4;
$currentYear--;
} //if 1쿼터 전쿼터는 전년도 4쿼터
} //while
} //while herb
관련자료
-
이전
-
다음