<?php
	
	require_once("config.inc.php");
	require_once("DBConnector.php");
	$db = new DBConnector();
							
	$entREFNO = "TRN/" . $_GET["dtfrom"];

 

// 	$sql="Select * from s_mas";
// 	$result =$db->RunQuery($sql);
// 	while($row = mysql_fetch_array($result)){
//  		$M_STOCODE='04';
		 
// 		$sql2="Insert into s_submas (STO_CODE, STK_NO, DESCRIPt) values ('".trim($M_STOCODE)."', '".trim($row['STK_NO'])."', '".$row['DESCRIPT']."')";
// 		$result2 =$db->RunQuery($sql2);
	 
// 	}
	
	
// 	$sql="delete from s_trn where REFNO='" . $entREFNO . "'";
// 	$result =$db->RunQuery($sql);
							
 
	
	
// 	$sql_smas="select STO_CODE,STK_NO from s_submas where STO_CODE='04'"; 
// 	$result_smas =$db->RunQuery($sql_smas);
// 	while($row_smas = mysql_fetch_array($result_smas)){
// 	    	$sql="insert into s_trn (SDATE, STK_NO, REFNO, QTY, LEDINDI, DEPARTMENT) values 
// 		('" . $_GET["dtfrom"] . "', '" . $row_smas["STK_NO"] . "' , '" . $entREFNO . "', " . number_format(0, 0, ".", "") . ", 'TRN', '".$row_smas['STO_CODE']."')";
// 		$result =$db->RunQuery($sql);
		

// 	}
	
// 	$sql_smas="select DEPARTMENT,STK_NO,sum(QTY) as qty from stk_take where  DEPARTMENT='04'    group by STK_NO";  
// 	$result_smas =$db->RunQuery($sql_smas);
// 	while($row_smas = mysql_fetch_array($result_smas)){ 
		
// 		$stkqty = $row_smas['qty'];
// 		$sql_stkTake="select sum(QTY) as qty,sum(damage) damQTy from stk_take where STK_NO='" . trim($row_smas["STK_NO"]) . "' and DEPARTMENT='".$row_smas['DEPARTMENT']."' ";
// 		//echo $sql_stkTake;
// 		$result_stkTake =$db->RunQuery($sql_stkTake);
// 		if($row_stkTake = mysql_fetch_array($result_stkTake)){	
// 			if ($row_stkTake["qty"]>0){ 
// 				$stkqty=$row_stkTake["qty"]; 
// 			} 
			
// 			if ($row_stkTake["damQTy"]>0){ 
// 				$stkqty=$stkqty-$row_stkTake["damQTy"]; 
// 			} 
// 		}
		
// 		$sql_stkDeli="select sum(QTY) as qty from stk_take_undelever where STK_NO='" . trim($row_smas["STK_NO"]) . "' and DEPARTMENT='".$row_smas['STO_CODE']."'";
// 		$result_stkDeli =$db->RunQuery($sql_stkDeli);
// 		if($row_stkDeli = mysql_fetch_array($result_stkDeli)){	
// 			if ($row_stkDeli["qty"]>0){ 
// 				$stkqty=$stkqty-$row_stkDeli["qty"]; 
// 			} 
// 		}
		
   
//   		if ($stkqty < 0) { $stkqty = 0; }
 
		
// 		$sql_submas="select * from s_submas where  STK_NO='" . $row_smas["STK_NO"] . "' and STO_CODE='".$row_smas['DEPARTMENT']."'";
// 		$result_submas =$db->RunQuery($sql_submas);
// 		if($row_submas = mysql_fetch_array($result_submas)){	
// 		    $sql="update s_submas set QTYINHAND=".number_format($stkqty, 2, ".", "")." where  STK_NO='" . $row_smas["STK_NO"] . "' and STO_CODE='".$row_smas['DEPARTMENT']."'";
// 		    $result =$db->RunQuery($sql);
// 		}else{
// 		    $sql="insert into s_submas (OPENT_DATE, STK_NO, tmp_no, QTY, STO_CODE) values 
//     		('" . $_GET["dtfrom"] . "', '" . $row_smas["STK_NO"] . "'  ,'hi', " . number_format($stkqty, 2, ".", "") . " , '".$row_smas['DEPARTMENT']."')";
//     		$result =$db->RunQuery($sql);
// 		}
		
		
		
// 		$sql="update s_mas set QTYINHAND=QTYINHAND+".number_format($stkqty, 2, ".", "")." where  STK_NO='" . $row_smas["STK_NO"] . "' ";
// 		$result =$db->RunQuery($sql);
   
//   	}
  	
  	
//   	$sql_smas="select * from s_submas where STO_CODE='04'  and QTYINHAND !='0.00'  "; 
// 	$result_smas =$db->RunQuery($sql_smas);
// 	while($row_smas = mysql_fetch_array($result_smas)){ 
// 		$sql="update s_trn set QTY='".$row_smas['QTYINHAND']."' where  STK_NO='" . $row_smas["STK_NO"] . "' and DEPARTMENT='".$row_smas['STO_CODE']."' and REFNO='TRN/2022-11-06'"; 
// 	    $result =$db->RunQuery($sql);
	    
// 	}

    $sql_smas="select sum(QTYINHAND) AS QTYINHAND,STK_NO from s_submas   group by STK_NO "; 
	$result_smas =$db->RunQuery($sql_smas);
	while($row_smas = mysql_fetch_array($result_smas)){ 
		$sql="update s_mas set QTYINHAND='".$row_smas['QTYINHAND']."' where  STK_NO='" . $row_smas["STK_NO"] . "'   ";  
	    $result =$db->RunQuery($sql);
	    
	}
	
	echo "Completed";		

?>
