🔓데이터베이스/SQL

재고관리 매출관리 프로그램 만들어보기[문제2번]

하얀성 2023. 10. 29. 16:28

1. sales 테이블 데이터구조 생성

별도 테이블인 sale를 운영하라 되어있다. 

그렇다면

데이터를 input에서 입력했을 때 product 테이블의 pcode, punit 뿐만 아니라 

sales의 것도 함께 입력해줘야 한다.

 

그런데 문제에서 sales(매출 테이블)을 만들라 했는데 상품수량(punit)이라 해서 헷갈렸음.

상품수량이 아니라 판매수량으로 생각해주고 테이블 설계해야 될듯.

 


2. 매출관리. product ,sales 두 테이블 합쳐서 출력하기

 

1. db안에 한줄 정보채움(출력 잘되는지 확인하기 위한 데이터)

 

출력결과

show.php 

<?
	echo("<center><h2>매출관리<h2></center>");
	
	
	
	$con = mysql_connect("localhost","root","apmsetup");
	mysql_select_db("class",$con);
	
	

	
	echo("
		<table border=1 width=800 align=center>
			<tr border>
				<td align=center>판매날짜</td>
				<td align=center>상품코드</td>
				<td align=center>상품이름</td>
				<td align=center>판매수량</td>
				<td align=center>상품단가</td>
				<td align=center>부분합계</td>
			</tr>
		
	");
	
	$query = "SELECT sales.wdate, sales.pcode, product.pname, sales.punit, product.pprice 
          FROM sales 
          JOIN product ON sales.pcode = product.pcode";

			
	$result = mysql_query($query, $con);
	$total = mysql_num_rows($result);
	
	
	
	$i=0;
	while($i<$total):
		$owdate = mysql_result($result,$i,"wdate");
		$opcode = mysql_result($result,$i,"pcode");
		$opname = mysql_result($result,$i,"pname");
		$opunit = mysql_result($result,$i,"punit");
		$opprice = mysql_result($result,$i,"pprice");
		$osum = $opunit * $opprice;
		echo("
			<tr>
				<td align=center>$owdate</td>
				<td align=center>$opcode</td>
				<td align=center>$opname</td>
				<td align=center>$opunit</td>
				<td align=center>$opprice</td>
				<td align=center>$osum</td>
			</tr>
		");
		$i++;
	endwhile;
	
	echo("<tr>
			<td colspan=6 align=center>매출합계:</td>
		</tr>
		");
	echo("</table><br>");
?>

3. 매출합계 계산 후 출력

월,일,시,분 나오도록 출력코드 수정(판매날짜는 데이터 입력시간으로 함)

$wdate = date('m월 d일 H시 i분');
	
		echo("
			<center>
			<table border=0 width=500>
			<form method=post action=process3.php>
				<tr>
					<td>상품코드: <input type=text size=10 name=ipcode><td>
					<td>상품수량: <input type=text size=7 name=ipunit><td>
					<input type=hidden name=iwdate value='$wdate'>
					<td><input type=submit value='판매완료'><td>
				</tr>
			</form>
			</table>
			</center><br>
		");

 

show.php의 출력부분에서 매출합계 변수를 추가하여 부분합계값을 누적시키고 출력.

$i=0;
	$totalSum=0;
	while($i<$total):
		$owdate = mysql_result($result,$i,"wdate");
		$opcode = mysql_result($result,$i,"pcode");
		$opname = mysql_result($result,$i,"pname");
		$opunit = mysql_result($result,$i,"punit");
		$opprice = mysql_result($result,$i,"pprice");
		$osum = $opunit * $opprice;
		$totalSum = $totalSum + $osum;
		echo("
			<tr>
				<td align=center>$owdate</td>
				<td align=center>$opcode</td>
				<td align=center>$opname</td>
				<td align=center>$opunit</td>
				<td align=center>$opprice</td>
				<td align=center>$osum</td>
			</tr>
		");
		$i++;
	endwhile;

	echo("<tr>
			<td colspan=6 align=center>매출합계: $totalSum 원</td>
		</tr>
		");
	echo("</table><br>");

(4,5번 코드는 process3.php 한곳에 함께 적음.)

4. 판매완료를 누르면 매출관리의 판매 수량 감소.  판매수량이 상품수량 초과시 감소x 

 

일부러 초과시키면?


5. 판매완료를 누르면 재고관리의 상품 수량 감소

 

<?
	$con = mysql_connect("localhost","root","apmsetup");
	mysql_select_db("class",$con);
	
	// 판매수량만큼 수량감소 시키기 + 판매수량이 수량 초과시 실행x 코드
	$result = mysql_query("select punit from product where pcode='$ipcode'",$con);
	$ppunit = mysql_result($result,0,"punit");
	
	if($ppunit < $ipunit){
		 echo ("판매량이 현재 상품 수량을 초과하였습니다.");
		 echo("<a href=show.php>[매출관리다시하기]</a>");
	    exit; // 코드실행 중지
		
	}
	
	//새 수량 계산
	$newppunit = $ppunit - $ipunit;
	mysql_query("update product set punit = $newppunit where pcode='$ipcode'",$con);
	
	mysql_query("insert into sales values('$ipcode',$ipunit,'$iwdate')",$con);
	
	mysql_close($con);
	
	echo("<meta http-equiv='Refresh' content='0; url=show.php'>");
?>