<?php
include('db_connection.php');
$Mainclass=new DB_Controller();
$SLN=$_REQUEST['per'];



	$SqlOrderDtl="select * from gold_indent_details where SLN='".$SLN."'";
	$RsOrderDtl=mysqli_query($Mainclass->dbh,$SqlOrderDtl);
	$CountIndentDtl=mysqli_num_rows($RsOrderDtl);
	$FchIndentDtl=mysqli_fetch_array($RsOrderDtl);
	
	$UpQty=$FchIndentDtl['Order_Quantity']-1;
	
	 //$Price=$FchIndentDtl['Price'];
	 //$Order_Total=$Price*$UpQty;
	 
	 $Product_Id=$FchIndentDtl['Product_Code'];
	 $Get_Product=$Mainclass->product_det('gold_product_master',$Product_Id);
	 
	 
	 $Dealer_Code=$_REQUEST['dealer'];
	
	/* $FetchCatCon=" Category= '".$Get_Product['Product_Category']."'";
     $Field_Needed="SLN,Category";
     $CategoryDtl=$Mainclass->Fetch_needed_data_condition('product_category_master',$Field_Needed,$FetchCatCon);*/
	
	
	 //$GetDealerMargin=$Mainclass->GetDealerMarginAmt($CategoryDtl['SLN'],$Dealer_Code);
	 
	 
	$Gold_Wt=$Get_Product['GoldWeight']; 
	$Stone_Value=$Get_Product['total_stone_price'];
    $Diamond_Value=$Get_Product['total_diamond_price']; 
	$Solitaire_Value=$Get_Product['total_sollitaire_price']; 
	$Making_Charge_Type=$Get_Product['MakingChargesType'];
	$Making_Charge=$Get_Product['MakingChargesValue'];
    $Wastage_Cost=$Get_Product['WastedCost'];
	$Gold_Purity=$Get_Product['GoldPurity'];
	 
	 $Today_Gold_Rate=$Mainclass->today_gold_rate($Gold_Purity,$_REQUEST['dealer']);
	 
	   if($Diamond_Value>0)
		{
			$GetDiamondMargin=$Mainclass->GetDealerMarginAmt('Diamond',$Dealer_Code);
		}
		else{
			$GetDiamondMargin=0;
		}
		
		if($Stone_Value>0)
		{
			$GetStoneMargin=$Mainclass->GetDealerMarginAmt('Stone',$Dealer_Code);
		}
		else{
			$GetStoneMargin=0;
		}
		if($Solitaire_Value>0)
		{
			$GetSolitaireMargin=$Mainclass->GetDealerMarginAmt('Solitaire',$Dealer_Code);
		}
		else{
			$GetSolitaireMargin=0;
		}
	    
	     $MakingChargMargin=$Mainclass->GetDealerMarginAmt('Making Charg',$Dealer_Code);
	 
	    
	$Total_Gold_Value=$Gold_Wt*$Today_Gold_Rate;
	if($Making_Charge_Type=="Flat")
	{
		$Making_Charge_Val=$Making_Charge;
	}
	if($Making_Charge_Type=="Percentage")
	{
		$Making_Charge_Val=($Total_Gold_Value*$Making_Charge)/100;
	}
	if($Making_Charge_Type=="Rate per gram")
	{
		$Making_Charge_Val=$Making_Charge * $Gold_Wt;
	}	 
	 
    $StonePrice=$Diamond_Value+$Stone_Value+$Solitaire_Value;
	//====Taxable_Amount=====//
	$ProductPrice=$Total_Gold_Value+$Making_Charge_Val+$StonePrice+$Wastage_Cost;	
    // echo $Get_Product['Price'];

	//$DMPrice=$Get_Product['Price']*$GetDealerMargin/100;
	//$DMProductPrice=$Get_Product['Price']-$DMPrice;
	
	$DiamondMarginValue=$Diamond_Value*$GetDiamondMargin/100;
	$DiamondMarginValue=number_format((float)$DiamondMarginValue, 2, '.', '');
	
	$StoneMarginValue=$Stone_Value*$GetStoneMargin/100;
	$StoneMarginValue=number_format((float)$StoneMarginValue, 2, '.', '');
	
	$SolitaireMarginValue=$Solitaire_Value*$GetSolitaireMargin/100;
	$SolitaireMarginValue=number_format((float)$SolitaireMarginValue, 2, '.', '');
	
	
	$MakingChargeMarginValue=$Making_Charge_Val*$MakingChargMargin/100;
	$MakingChargeMarginValue=number_format((float)$MakingChargeMarginValue, 2, '.', '');
	
	
	$DMDiamond=$Diamond_Value-$DiamondMarginValue;
	$DMStone=$Stone_Value-$StoneMarginValue;
	$DMSolitaire=$Solitaire_Value-$SolitaireMarginValue;
	$DMMakingCharge=$Making_Charge_Val-$MakingChargeMarginValue;
	
	
	$Stone=$DMDiamond+$DMStone+$DMSolitaire;
	//====Taxable_Amount=====//
	$Amt_Bf_Tax=$Total_Gold_Value+$DMMakingCharge+$Stone+$Wastage_Cost;	
	
	
	
	
	//$GST=$Mainclass->Get_GST_Product('product_gst_master',$Get_Product['HSN_Code']);
	   $GST=3;
	
	    //$Total_Product_Cost_Price=$DMProductPrice*$UpQty;
	    $Total_Product_Cost_Price=$Amt_Bf_Tax*$Qty;
		
		//$Discount=$DMPrice*$UpQty;
		 $Discount=($DiamondMarginValue+$StoneMarginValue+$SolitaireMarginValue+$MakingChargeMarginValue)*$Qty;
		
		 $XR=($GST * $Total_Product_Cost_Price)/100; 

		 $GST_Amount= $XR;		
		 $Total_Price=$Total_Product_Cost_Price;
		 $Net_Price=$Total_Price + $GST_Amount;
	 
	 
	 /*$FetchDealerCon=" Order_Code= '".$_REQUEST['dealer']."'";
     $Field_Needed="State,Dealer_Type";
     $DeaerDtl=$Mainclass->Fetch_needed_data_condition('dealer_master',$Field_Needed,$FetchDealerCon);*/
	 
	 $DeaerDtl=$Mainclass->Fetch_individual_data('dealer_master',$Field_Needed,$FetchDealerCon);
	 
	  if( strtoupper($DeaerDtl['Dealer_Type'])=="OWNED" && strtoupper($DeaerDtl['State'])=="WEST BENGAL")
	  {
	         $CGST=0;
			 $SGST=0;
			 $IGST=0;
	  
	  }
	  else{
	 
		  if(strtoupper($DeaerDtl['State'])=="WEST BENGAL")
		 {
			 $GST_Div=$GST_Amount/2;
			 $CGST=$GST_Div;
			 $SGST=$GST_Div;
		 }
		 else{
			 $IGST=$GST_Amount;
		 }
	 
	 }
	 
	 if($UpQty>=1)
	 {
	 
	 //  $UpdateIndDtl="update indent_details set Order_Quantity='".$UpQty."',Order_Total='".$Order_Total."' where SLN='".$SLN."'";
	   $UpdateIndDtl="update gold_indent_details set Order_Quantity='".$UpQty."',Order_Total='".$Net_Price."' ,Discount='".$Discount."',GST='".$GST_Amount."',CGST='".$CGST."',SGST='".$SGST."',IGST='".$IGST."' where SLN='".$SLN."'";
	   mysqli_query($Mainclass->dbh,$UpdateIndDtl);
	 }
	 else{
	 
	    $UpdateIndDtl="delete from gold_indent_details  where SLN='".$SLN."'";
	    mysqli_query($Mainclass->dbh,$UpdateIndDtl);
	 
	 }


           //$Sql="select sum(Order_Total) as Totl_Amt, sum(Order_Quantity) as Total_Qty, sum(GST) as GST,sum(CGST) as CGST,sum(SGST) as SGST,sum(IGST) as IGST,sum(Discount) as  Discount from indent_details where Order_No='".$FchIndentDtl['Order_No']."'";
		   $Sql="select sum(Price) as TotalPrice,sum(Discount) as TotalDiscount, sum(Total) as Gross_Total,sum(Order_Total) as Net_Amt, sum(Order_Quantity) as Total_Qty,sum(GST) as GST,sum(CGST) as CGST,sum(SGST) as SGST,sum(IGST) as IGST,sum(Product_Weight) as Product_Weight  from indent_details where Order_No='".$FchIndentDtl['Order_No']."'";
           $RS=mysqli_query($Mainclass->dbh,$Sql);
           $ROW=mysqli_fetch_array($RS);
		  
		  //$UpdateOrder="update indent_master set Total_Quantity='".$ROW['Total_Qty']."',Net_Total='".$ROW['Totl_Amt']."',Discount='".$ROW['Discount']."',GST='".$ROW['GST']."',CGST='".$ROW['CGST']."',SGST='".$ROW['SGST']."',IGST='".$ROW['IGST']."' where Order_No='".$FchIndentDtl['Order_No']."'";
		   $UpdateOrder="update gold_indent_master set Total_Quantity='".$ROW['Total_Qty']."',Total='".$ROW['TotalPrice']."',Discount='".$ROW['TotalDiscount']."',Gross_Total='".$ROW['Gross_Total']."',Net_Total='".$ROW['Net_Amt']."',GST='".$ROW['GST']."',CGST='".$ROW['CGST']."',SGST='".$ROW['SGST']."',IGST='".$ROW['IGST']."',Toal_Product_Weight='".$ROW['Product_Weight']."' where Order_No='".$FchIndentDtl['Order_No']."'";
		  
		  
		   mysqli_query($Mainclass->dbh,$UpdateOrder);
?>



