<?php
date_default_timezone_set('Asia/Colombo'); 
 
require_once("config.inc.php");
require_once("DBConnector.php");
$db = new DBConnector(); $dbinv = $db->GetConnection();





	
	
	
// 
$sqlt = "select * from  viewsubmas where STO_CODE='03' and   (     BRAND_NAME ='TIK'   )" ; 
$result1 = $db->RunQuery($sqlt);
echo $sqlt."</br>";
while ($rssalma = $result1->fetch()) { 
    $ref="STKUP".date("Y-m-d");
    if($rssalma['QTYINHAND']>0){
        $sql="Insert into s_trn (REFNO, SDATE, LEDINDI,QTY,STK_NO, DEPARTMENT)values 
    	('".$ref."', '".date("Y-m-d")."', 'INV', '".$rssalma['QTYINHAND']."', '".$rssalma['STK_NO']."','03') ";  
    	$result =$db->RunQuery($sql);	
    	
    	$sql4 = "select sum(QTYINHAND) as QTYINHAND from s_submas where STK_NO='" . $rssalma['STK_NO'] . "' and   STO_CODE !='03'";
    	$result4 =$db->RunQuery($sql4);
    	$row4 = $result4->fetch();
    	
        $tot=$row4['QTYINHAND'];
        $sql21="update s_mas set QTYINHAND=" . $tot . " where STK_NO='" . $rssalma['STK_NO'] . "'"; 
        $result21 =$db->RunQuery($sql21);
     
    	
    // 	 $sql = "update s_mas set QTYINHAND = QTYINHAND-'" . $rssalma['QTYINHAND'] . "' where STK_NO= '" . $rssalma['STK_NO'] . "'"; 
    //      $resultp =$db->RunQuery($sql);
         
         $sql = "update s_submas set QTYINHAND = QTYINHAND-'" . $rssalma['QTYINHAND'] . "' where STK_NO= '" . $rssalma['STK_NO'] . "' and STO_CODE='03'";
         $resultp =$db->RunQuery($sql);
         
         echo "up";
         
    }else{
      $sql="Insert into s_trn (REFNO, SDATE, LEDINDI,QTY,STK_NO, DEPARTMENT)values 
    	('".$ref."', '".date("Y-m-d")."', 'GRN', '".$rssalma['QTYINHAND']."'*-1, '".$rssalma['STK_NO']."','03') ";  
    	$result =$db->RunQuery($sql);
    	
    	$sql4 = "select sum(QTYINHAND) as QTYINHAND from s_submas where STK_NO='" . $rssalma['STK_NO'] . "' and   STO_CODE !='03'";
    	$result4 =$db->RunQuery($sql4);
    	$row4 = $result4->fetch();
    	
        $tot=$row4['QTYINHAND'];
        $sql21="update s_mas set QTYINHAND=" . $tot . " where STK_NO='" . $rssalma['STK_NO'] . "'"; 
        $result21 =$db->RunQuery($sql21);
        
        
    	
    // 	 $sql = "update s_mas set QTYINHAND = QTYINHAND+'" . $rssalma['QTYINHAND'] . "'*-1 where STK_NO= '" . $rssalma['STK_NO'] . "'"; 
    //      $resultp =$db->RunQuery($sql);
         
         $sql = "update s_submas set QTYINHAND = QTYINHAND+'" . $rssalma['QTYINHAND'] . "'*-1 where STK_NO= '" . $rssalma['STK_NO'] . "' and STO_CODE='03'";
         $resultp =$db->RunQuery($sql);
         
         
         echo "up";
    }
    
			
   
}
// 	$sql="Select * from s_mas ";
// 	$result =$db->RunQuery($sql);
// 	while($row = $result->fetch()){
//  	  $M_STOCODE='04';
//  	  $stkNo=$row["STK_NO"];
// 		$sql1="select * from s_submas where sto_code= '".trim($M_STOCODE)."' and stk_no= '".trim($stkNo)."'"; 
// 		$result1 =$db->RunQuery($sql1);
// 		if($row1 = $result1->fetch()){
		 
// 		} else {
// 			$sql2="Insert into s_submas (STO_CODE, STK_NO, DESCRIPt,QTYINHAND) values ('".trim($M_STOCODE)."', '".trim($stkNo)."', '".$row["DESCRIPT"]."','0')";
// 			$result2 =$db->RunQuery($sql2);echo $stkNo.'<br>';
// 		}
	
// 	}
 
?>



