<?php
include('db_connection.php');

$Mainclass=new DB_Controller();


$Order_Id=$_REQUEST['Order_No'];
$Condition=" Order_No='".$Order_Id."'";
//$Get_Indent_Details=$Mainclass->Challan_Details('indent_master',$Condition);

$Get_Indent_Details=$Mainclass->Fetch_individual_data('gold_indent_master','*',$Condition);


/*$FetchDealerCon=" Order_Code= '".$Get_Indent_Details['Dealer_Code']."'";
$Field_Needed="State,Dealer_Credit_Limit,Dealer_Type";
$DeaerDtl=$Mainclass->Fetch_needed_data_condition('dealer_master',$Field_Needed,$FetchDealerCon);*/

$DeaerDtl=$Mainclass->Fetch_individual_data('dealer_master',$Field_Needed,$FetchDealerCon);


if($Get_Indent_Details['Status']!='Dispatched')
{

 $currentYr=date("y");
	    $currentMonth=date("m");
		if($currentMonth<04)
		{
			$currentYrFinacila=($currentYr - 1);
		}
		else
		{
			$currentYrFinacila=$currentYr;
		}
		
		$FinalcialRange=$currentYrFinacila."-".($currentYrFinacila+1);
		
		
		
		//=============Gate Pass No=========================//

    
	//$sql="select max(SLN) as SLN from stn_gatepass_master where F_Year='".$FinalcialRange."'";
	$sql="select Gate_Pass_No from gold_stn_gatepass_master where F_Year='".$FinalcialRange."' order by SLN desc limit 1";
	$result= mysqli_query($Mainclass->dbh,$sql);
	$row=mysqli_fetch_array($result);
	$ExplodeGP=explode('/',$row['Gate_Pass_No']);
	$Gate_Pass_Count= $ExplodeGP[2]+ 1;
	$New_Gate_Pass=sprintf("%05d", $Gate_Pass_Count);
	$Gate_Pass_No="CP/G/".$New_Gate_Pass."/".$FinalcialRange;
	
	/*if($row['SLN']==0)
	{	
	
		
		
		$Gate_Pass_No="CP/00001/".$FinalcialRange;
	}
	else
	{
		$NewSLN=$row['SLN']+1;
		
		$NewCode=sprintf("%05d", $NewSLN);
		
		 
		 
		
		$Gate_Pass_No="CP/".$NewCode."/".$FinalcialRange;
	}*/
	
	
	//====================BILL=============//
	 if(strtoupper($DeaerDtl['Dealer_Type'])=="OWNED" && strtoupper($DeaerDtl['State'])=="WEST BENGAL")
     {
	   $Bill_No="";
	 }
	 else
	 {
		//$sql="select max(SLN) as SLN from bill_master where F_Year='".$FinalcialRange."'";
		$sql="select Bill_No from gold_bill_master where F_Year='".$FinalcialRange."' order by SLN desc limit 1";
		$result= mysqli_query($Mainclass->dbh,$sql);
		$row=mysqli_fetch_array($result);
		$ExplodeBill=explode('/',$row['Bill_No']);
		$Bill_Count= $ExplodeBill[3]+ 1;
		$NewBill=sprintf("%03d", $Bill_Count);
		$Bill_No="HO/I/G/".$NewBill."/".$FinalcialRange;
		/*if($row['SLN']==0)
		{	
		
			
				$Bill_No="HO/I/00001/".$FinalcialRange;
			  
			
		
		}
		else
		{
			$NewSLN=$row['SLN']+1;
			
			$NewCode=sprintf("%05d", $NewSLN);
			
			 
			
			  $Bill_No="HO/I/".$NewCode."/".$FinalcialRange;
			
			
			
		}*/
	
	}
	
	
	//====================Stn=========================//
	
	
	$sql="select Stn_No from gold_stn_master  where F_Year='".$FinalcialRange."' and Dealer_Code='".$Get_Indent_Details['Dealer_Code']."' order by SLN desc limit 1";
	$result= mysqli_query($Mainclass->dbh,$sql);
	$row=mysqli_fetch_array($result);
	if($row['Stn_No']=="")
	{	
		$Stn_No=$Get_Indent_Details['Dealer_Code']."/STN/I/G/00001/".$FinalcialRange;
		
		
	
	}
	else
	{
	
	    $FetchStn=$row['Stn_No'];
		
		$ExpStn=explode("/",$FetchStn);
		
		$GetStn=$ExpStn[4];
		
		$NewStn=$GetStn+1;
		
		$NewCode=sprintf("%05d", $NewStn);
		
		 

		
		$Stn_No=$Get_Indent_Details['Dealer_Code']."/STN/I/G/".$NewCode."/".$FinalcialRange;
		
		
	
	}
	

$Secret_Code=rand(1000000000,9999999999);


if(strtoupper($DeaerDtl['Dealer_Type'])=="OWNED" && strtoupper($DeaerDtl['State'])=="WEST BENGAL")
{
	$Bill_No="";
}
else{

    $SqlNew="insert into gold_bill_master(Order_No,Bill_No,F_Year,Date)values('".$Order_Id."','".$Bill_No."','".$FinalcialRange."','".date('Y-m-d H:i:s')."')";
	mysqli_query($Mainclass->dbh,$SqlNew);
}


$SqlNew="insert into gold_stn_gatepass_master(Order_No,Secret_Code,Gate_Pass_No,F_Year,Date)values('".$Order_Id."','".$Secret_Code."','".$Gate_Pass_No."','".$FinalcialRange."','".date('Y-m-d H:i:s')."')";
mysqli_query($Mainclass->dbh,$SqlNew);


$SqlNewStn="insert into gold_stn_master(Order_No,Stn_No,F_Year,Dealer_Code,Date)values('".$Order_Id."','".$Stn_No."','".$FinalcialRange."','".$Get_Indent_Details['Dealer_Code']."','".date('Y-m-d H:i:s')."')";
mysqli_query($Mainclass->dbh,$SqlNewStn);


$Condition=" Order_No='".$Order_Id."'";
$FieldValue="Status='Dispatched',Dispatched_Date='".date('Y-m-d H:i:s')."',STN_NO='".$Stn_No."',Bill_No='".$Bill_No."', Gate_Pass_No='".$Gate_Pass_No."',Secret_Code='".$Secret_Code."'";

$Update_Order_Master=$Mainclass->Update_Data('gold_indent_master',$FieldValue,$Condition);

//=================challan import===============//

                           
							 
							 $condition="  Order_No='".$_REQUEST['Order_No']."'";

                             //$Get_Dealers_Stock=$Mainclass->Challan_det_return('indent_details',$condition);
							 
							 $Get_Dealers_Stock=$Mainclass->Fetch_multiple_data('gold_indent_details','*',$condition);
							 
							 $C_Qty=0;
							 
							 foreach($Get_Dealers_Stock as $stock)
	                         {
							 
							 
                                //$Get_Category=mysqli_fetch_array(mysqli_query($Mainclass->dbh,"select SLN from product_category_master where Code='".$arr[$i][2]."'"));
							
								$Get_Last_Data="select Quantity from dealer_gold_stock_master where Dealer_Code='".$Get_Indent_Details['Dealer_Code']."' and Product_Code='".$stock['Product_Code']."' order by SLN desc";
								$Qry_Data=mysqli_query($Mainclass->dbh,$Get_Last_Data);
								$Ft_Last_Balance=mysqli_fetch_assoc($Qry_Data);
								
								
							    $sql = "insert into gold_challan_details (Challan_No,Dealer_Code,Product_Code,Barcode,Quantity,Category_Code,Last_Balance,Challan_Date,Total_MRP,Total_Cost_Price,gold_purity,gold_wt,diamond_ct,solitaire_ct) VALUES ('".$Stn_No."','".$Get_Indent_Details['Dealer_Code']."','".$stock['Product_Code']."','".$stock['Ref_Product_Code']."','".$stock['Order_Quantity']."','".$stock['Product_Category']."','".$Ft_Last_Balance['Quantity']."','".date('Y-m-d H:i:s')."','".$stock['Price']."','".$stock['Total']."','".$stock['gold_purity']."','".$stock['gold_wt']."','".$stock['diamond_carat']."','".$stock['sollitaire_carat']."')";					
		


				
							    $q=mysqli_query($Mainclass->dbh,$sql) or die("Error in line".$i.mysqli_error());					

							    $r=mysqli_affected_rows($q);
								
								 $C_Qty=$C_Qty+$stock['Order_Quantity'];
							
							}

                             $Insert_dealer_master=mysqli_query($Mainclass->dbh,"insert into gold_challan_master(Challan_No,Dealer_Code,Challan_Date,Secret_Code,Quantity,CSV_Filename,Consignment_Stn) VALUES ('".$Stn_No."','".$Get_Indent_Details['Dealer_Code']."','".date('Y-m-d H:i:s')."','".$Secret_Code."','".$C_Qty."','','".$Get_Indent_Details['Consignment_Stn']."')");


                                
                              $NotificationTitle="New Stock Transfer Initiated.";
							  $NotificationDtl="New Product Transfer has been initiated with STN no: ".$Stn_No.".";
							
							 // $Notification=$Mainclass->GenerateNotification($NotificationTitle,$NotificationDtl,$Get_Indent_Details['Dealer_Code']);
							 // $Stock_Mail=$Mainclass->Stock_Update_Mail($Stn_No,$Get_Indent_Details['Dealer_Code']);
							  
							  
							  
							  //===================update stock=======================//
							  
							    $SqlOrderDtl="select * from gold_indent_details where Order_No='".$Order_Id."'";
								$Get_Order_Details=mysqli_query($Mainclass->dbh,$SqlOrderDtl);

									   foreach($Get_Order_Details as $Details)
										{
											// $PMSDCondition=" Product_Code ='".$Details['Product_Code']."'"; 
											// $PMSD_QTY=$Mainclass->Challan_Details('product_master',$PMSDCondition);
											 $PMSD_QTY=$Mainclass->product_det('gold_products_master',$Details['Product_Code']);
											 
											 $UpQty=$PMSD_QTY['Stock'] - $Details['Order_Quantity'];
											 
											  $UpdateStock="update gold_products_master set Stock='".$UpQty."' where Productid='".$Details['Product_Code']."'";
											  mysqli_query($Mainclass->dbh,$UpdateStock);
										
										}
							  
}
?>

