<?php 
session_start();
include('../includes/dbcon.php');

$str=" ";


$edate=date("Y-m-d");
$userid=$_SESSION['hrm_id'];

$inv_no= trim($_POST['inv_no']);
$loc_id= trim($_POST['loc_id']);
$error=0;

$qty_sale=0;
$vat=10;
$cost='MAT';
$tot_inv_gross=0;
/////!!!!!!!!!!!!!!!!!!  sequence   !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
$querydoc=mysqli_query($con,"select * from sequence where seq_id=9 ")or die(mysqli_error());
while($row=mysqli_fetch_array($querydoc)){  
$inv_new_no=$row['cno'];
}
///~~~~~~~~~~~~~~~~~~~~~~~~~~~   End of sequence ~~~~~~~~~~~~~~~~~~

//echo $inv_new_no;

/////!!!!!!!!!!!!!!!!!!  Open inv_temp_header   !!!!!!!!!!!!!!!!!!!!!!!!!!!!
$query=mysqli_query($con," select * from inv_temp_header where invd_id='$inv_no'  ")or die(mysqli_error($con));
$count=mysqli_num_rows($query);

if ($count>0)
{

	while($rowsp=mysqli_fetch_array($query)){			
		//$grnd_no=$rowd['grnd_no'];	
		$invd_date=$rowsp['invd_date'];
		$loc_id=$rowsp['loc_id'];	
		$cus_id=$rowsp['cus_id'];
		$invd_ref=$rowsp['invd_ref'];
		$note=$rowsp['note'];
		$discount=$rowsp['discount'];
	} ////  while($rowsp=mysqli_fetch_array($query)){	

	/// !!!!!!!!!!!!!!!!!!!      item detail
	$queryd=mysqli_query($con," SELECT inv_temp_detail.*,item_master.item_name,item_master.sales_price AS sp,item_master.cost_price AS cp,
		item_stocks.loc_qoh
 		FROM inv_temp_detail
		LEFT JOIN item_master ON item_master.item_id=inv_temp_detail.item_id
		LEFT JOIN item_stocks ON item_stocks.item_id=inv_temp_detail.item_id AND item_stocks.loc_id=$loc_id 
		where inv_temp_detail.invd_id='$inv_no'
   		")or die(mysqli_error($con));

			$countd=mysqli_num_rows($queryd);
			mysqli_begin_transaction($con);
			if ($countd>0)
			{

				while($rowd=mysqli_fetch_array($queryd)){ 	
					$item_id=$rowd['item_id'];	
					$qty=$rowd['qty'];
					$sp=$rowd['sp'];
					$cp=$rowd['cp'];
					$loc_qoh=$rowd['loc_qoh'];		
					$item_dis=$rowd['item_dis'];

					//qty_sale
					if 	($loc_qoh >= $qty) {
						$qty_sale=$qty;

					}
					else
					{
						$qty_sale=$loc_qoh;

					}

					$tot_temp=0  ;
		            $tot_temp_vat=0;




					
			if ( mysqli_query($con,"INSERT INTO inv_detail(inv_id,item_id,qty,item_dis,sales_price,cost_price,qoh,loc_id) 
	        VALUES($inv_new_no,$item_id,0-$qty,$item_dis,$sp,$cp,$loc_qoh,$loc_id )  ")or die(mysqli_error($con)))
	        {  

	     
	        		//////////////// Stock Update	 

$queryitem=mysqli_query($con," select * from item_stocks_remove where item_id=$item_id and rem_type= 1  ")or die(mysqli_error($con));
			$countitem=mysqli_num_rows($queryitem);
			
			if ($countitem>0)
			{

if ( mysqli_query($con,"update item_stocks_remove set qty =qty + $qty  where item_id=$item_id and rem_type= 1 ")or die(mysqli_error($con)))			
						{   }


			}  ////	if ($countitem>0)

else {

if ( mysqli_query($con,"INSERT INTO item_stocks_remove(item_id,rem_type,qty) 
	        VALUES($item_id,1,$qty )  ")or die(mysqli_error($con)))
	        {  

	        		    $tot_temp=($qty * $sp)-($qty * $sp * $item_dis/100 )  ;
		               	$tot_temp_vat=$tot_temp + ( $tot_temp * $vat/100 );
		               	$tot_inv_gross=$tot_inv_gross + $tot_temp_vat ;


	         }

}





//////////////////////////////////////////////////////////
//////////////////////////////////////////
	        	//if ( mysqli_query($con,"update item_stocks set loc_qoh = loc_qoh + $qty where item_id=$item_id and  loc_id='$loc_id' ")or die(mysqli_error($con)))
		             //  {         

		              // 	$tot_temp=($qty * $sp)-($qty * $sp * $item_dis/100 )  ;
		               //	$tot_temp_vat=$tot_temp + ( $tot_temp * $vat/100 );
		              // 	$tot_inv_gross=$tot_inv_gross + $tot_temp_vat ;
		               // }	
				///////////////////////end of stock update
//if ( mysqli_query($con,"update item_master set  qoh=qoh + $qty where item_id=$item_id  ")or die(mysqli_error($con)))
		//{
			//echo "<script>document.location='Admin_location_view.php'</script>"; 
		//}











			} ////  if ( mysqli_query($con,"INSERT INTO inv_detail(

				} ////while($rowd=mysqli_fetch_array($queryd)){ 
			}  //// if ($countd>0)

///Final inv tot calculation /////////////////////




///~~~~~~~~~~~~~~end of Final inv tot calculation /////////////////////





      if ( mysqli_query($con,"INSERT INTO inv_header(inv_id,inv_date,loc_id,inv_ref,cus_id,note,discount,inv_type) 
		VALUES($inv_new_no,'$edate',$loc_id,'$invd_ref','$cus_id','$note',$discount ,3)  ")or die(mysqli_error($con)))
	        {


	 if ( mysqli_query($con,"INSERT INTO seheader(docid,docdate,doctype,cost,amount,refno,empcode,party_id,party_type,cancel,dbcr,userid,edate) 
 VALUES('$inv_new_no','$edate','sr','$cost',0-$tot_inv_gross,'$invd_ref',0,$cus_id,1,0,'cr',$userid,'$edate' )  ")or die(mysqli_error($con)))
	        {



	        	if ( mysqli_query($con,"update sequence set cno =cno + 1  where seq_id=9  ")or die(mysqli_error($con)))			
						{   
							if ( mysqli_query($con,"delete from inv_temp_detail where invd_id='$inv_no' ")or die(mysqli_error($con))) {	


								if ( mysqli_query($con,"delete from inv_temp_header where invd_id='$inv_no' ")or die(mysqli_error($con))) {	


							if (!mysqli_commit($con)) {
							    echo("Transaction commit failed\n");
							    exit();
							}
							else {
					       // echo "<script>document.location='inv_new.php '</script>";
								//echo "aa";

							}



							}

								 }  ////  if ( mysqli_query($con,"delete from 


			}  ///// if ( mysqli_query($con,"delete from inv_temp_detail 



						} /// if ( mysqli_query($con,"update sequence set cno =cno + 1  where 
             } ///// f ( mysqli_query($con,"INSERT INTO inv_header(


} /////if ($count>0)

?>
<script>
        function printPage() {
           // window.print(); // This initiates the print dialog
url='http://localhost:81/saleshub/pages/srg_final_print.php?inv_no=<?php echo $inv_new_no;?> ';
 var printWindow = window.open( url, 'Print', 'left=200, top=200, width=950, height=500, toolbar=0, resizable=0' , '_blank');

    printWindow.addEventListener('load', function() {
        if (Boolean(printWindow.chrome)) {
            printWindow.print();
            setTimeout(function(){
                printWindow.close();
               window.location.href = "srg_new.php";
            }, 500);
        } else {
            printWindow.print();
            printWindow.close();
            // window.location.href = "inv_new.php";
        }
    }, true);



        }

window.onload = function() {
        printPage();
      // window.location.href = "inv_new.php";
    };
        
    </script>    
