<?php

$hostname = 'localhost';
$username = 'root';
$password = '';

$ben = mysql_connect($hostname, $username, $password, true);
$ben_tyre = mysql_connect($hostname, $username, $password, true);

//// database selection removed;
//// database selection removed;

// database selection removed;
// database selection removed;

$sql = "Select * from s_ginmas where SDATE >='2014-08-25'";
$result = $ben->query($sql);
while ($row = $result->fetch()) {

    $sql1 = "insert into s_ginmas(SDATE, REF_NO, DEP_FROM, DEP_F_NAME, DEP_TO, DEP_T_NAME, AR_DATE, AR_NO, tmp_no) values ('".$row["SDATE"]."', '".$row["REF_NO"]."', '".$row["DEP_FROM"]."', '".$row["DEP_F_NAME"]."', '".$row["DEP_TO"]."', '".$row["DEP_T_NAME"]."', '".$row["AR_DATE"]."', '".$row["AR_NO"]."', '".$row["tmp_no"]."')";
    echo $sql1 . "<br>";
    $result1 = $ben_tyre->query($sql1);

}

$sql = "Select * from s_trn where SDATE >='2014-08-25' and LEDINDI='GINI'";
echo $sql . "<br>";
$result = $ben->query($sql);
while ($row = $result->fetch()) {

    $sql1 = "insert into s_trn(STK_NO, SDATE, QTY, LEDINDI, REFNO, DEPARTMENT, seri_no, Dev, SAL_EX, ACTIVE, DONO) values ('".$row["STK_NO"]."', '".$row["SDATE"]."', '".$row["QTY"]."', 'GINI', '".$row["REFNO"]."', '".$row["DEPARTMENT"]."', '".$row['seri_no']."', '".$row["Dev"]."', '".$row["SAL_EX"]."', '".$row["ACTIVE"]."', '".$row["DONO"]."')";
    echo $sql1 . "<br>";
    $result1 = $ben_tyre->query($sql1);
	
	$sql_mas = "Select * from s_ginmas where SDATE >='2014-08-25' and REF_NO='".$row["REFNO"]."'";
	$result_mas = $ben->query($sql_mas);
	$row_mas = $result_mas->fetch();

	$sql1="update s_submas set QTYINHAND=QTYINHAND-".$row["QTY"]." where STK_NO='".$row["STK_NO"]."' and STO_CODE='".$row_mas["DEP_FROM"]."'";
	echo $sql1 . "<br>";
	$result1 = $ben_tyre->query($sql1);

}

$sql = "Select * from s_trn where SDATE >='2014-08-25' and LEDINDI='GINR'";
echo $sql . "<br>";
$result = $ben->query($sql);
while ($row = $result->fetch()) {

    $sql1 = "insert into s_trn(STK_NO, SDATE, QTY, LEDINDI, REFNO, DEPARTMENT, seri_no, Dev, SAL_EX, ACTIVE, DONO) values ('".$row["STK_NO"]."', '".$row["SDATE"]."', '".$row["QTY"]."', 'GINR', '".$row["REFNO"]."', '".$row["DEPARTMENT"]."', '".$row['seri_no']."', '".$row["Dev"]."', '".$row["SAL_EX"]."', '".$row["ACTIVE"]."', '".$row["DONO"]."')";
    echo $sql1 . "<br>";
    $result1 = $ben_tyre->query($sql1);
	
	
	$sql_mas = "Select * from s_ginmas where SDATE >='2014-08-25' and REF_NO='".$row["REFNO"]."'";
	$result_mas = $ben->query($sql_mas);
	$row_mas = $result_mas->fetch();
	
	$sqlsub="Select * from s_submas where STK_NO='".$row["STK_NO"]."' and STO_CODE='".$row_mas["DEP_TO"]."'";
	$resultsub = $ben->query($sqlsub);
	if($rowsub = $resultsub->fetch()){
					
		$sql1="update s_submas set QTYINHAND=QTYINHAND+".$row["QTY"]." where STK_NO='".$row["STK_NO"]."' and STO_CODE='".$row_mas["DEP_TO"]."'";
		echo $sql1 . "<br>";
		$result1 = $ben->query($sql1);
	} else {
		$sql1="insert into s_submas(STO_CODE, STK_NO, DESCRIPt, OPEN_STK, QTYINHAND) values ('".$row_mas["DEP_TO"]."',  '".$row["STK_NO"]."', '".$row_mas["DEP_T_NAME"]."', 0, ".$row["QTY"].")";
		echo $sql1 . "<br>";
		$result1 = $ben->query($sql1);
	}

}




  
?>
