<?php
include('db_connection.php');
$Mainclass=new DB_Controller();

$Order=$_REQUEST['Order'];
$Curr_Date=date('Y-m-d');
$Date1=$Curr_Date." 00:00:00";
$Date2=$Curr_Date." 23:59:59";
$Dealer=$_COOKIE['O_Code'];

$Get_Details=$Mainclass->dbh->prepare("SELECT Barcode FROM gold_billing_order_details WHERE Order_No=?");
$Get_Details->bind_param("s",$Order);
$Get_Details->execute();
$Details_Res=$Get_Details->get_result();
if($Details_Res->num_rows>0)
{
	while($FT_Product=$Details_Res->fetch_assoc())
	{
		$Barcode=$FT_Product['Barcode'];
		
		
		$Get_Details_From_Master=$Mainclass->dbh->prepare("SELECT pm.ProductCategory,pm.GoldWeight,pm.GoldPurity,pm.total_stone_price,pm.total_diamond_price,pm.total_sollitaire_price,pm.MakingChargesType,pm.MakingChargesValue,pm.collection_id,grm.Selling_Rate FROM gold_products_master pm LEFT JOIN gold_rate_master grm ON pm.GoldPurity=grm.Karat WHERE grm.Entry_Date BETWEEN ? AND ? AND pm.Barcode=?");
$Get_Details_From_Master->bind_param("sss",$Date1,$Date2,$Barcode);
$Get_Details_From_Master->execute();
$Product_Res=$Get_Details_From_Master->get_result()->fetch_assoc();
$Get_Details_From_Master->close();


$Stone_Value=$Product_Res['total_stone_price'];
$Diamond_Value=$Product_Res['total_diamond_price'];
$Solitaire_Value=$Product_Res['total_sollitaire_price'];
$Making_Charge_Type=$Product_Res['MakingChargesType'];
$Making_Charge=$Product_Res['MakingChargesValue'];
$Gold_Wt=$Product_Res['GoldWeight'];
$Gold_Rate=$Product_Res['Selling_Rate'];
$Category=$Product_Res['ProductCategory']!=''?$Product_Res['ProductCategory']:"";
$Collection=$Product_Res['collection_id']!=''?$Product_Res['collection_id']:"";


$Making_Charge_Disc=0;
$Diamond_Disc=0;
$Solitaire_Disc=0;
$Stone_Disc=0;


if($Category!='')
{

$Get_Discount_Category=$Mainclass->dbh->prepare("SELECT gdm.SLN, gdm.Making_Charge_Disc, gdm.Diamond_Disc, gdm.Solitaire_Disc, gdm.Stone_Disc, gdm.Offer_On
FROM gold_discount_master gdm
INNER JOIN gold_category_discount_dealers ccd ON gdm.SLN = ccd.Category_Discount_Id
INNER JOIN gold_category_discount_product_cat cdp ON gdm.SLN = cdp.Category_Discount_Id
WHERE ? BETWEEN gdm.From_Date AND gdm.To_Date
  AND ccd.Dealer_Code = ?
  AND gdm.Active = 'Yes'
  AND cdp.Product_Category = ?");

$Get_Discount_Category->bind_param("sss",$Curr_Date,$Dealer,$Category);
$Get_Discount_Category->execute();
$Cat_Disc_Res=$Get_Discount_Category->get_result();
$Cat_Discount=$Cat_Disc_Res->fetch_assoc();
$Get_Discount_Category->close();

if($Cat_Disc_Res->num_rows>0)
{
	$Making_Charge_Disc=$Cat_Discount['Making_Charge_Disc'];
	$Diamond_Disc=$Cat_Discount['Diamond_Disc'];
	$Solitaire_Disc=$Cat_Discount['Solitaire_Disc'];
	$Stone_Disc=$Cat_Discount['Stone_Disc'];
}
}

if($Cat_Disc_Res->num_rows==0 && $Collection!='')
{
$Get_Discount_Collection=$Mainclass->dbh->prepare("SELECT gdm.SLN, gdm.Making_Charge_Disc, gdm.Diamond_Disc, gdm.Solitaire_Disc, gdm.Stone_Disc, gdm.Offer_On FROM gold_discount_master gdm INNER JOIN gold_category_discount_dealers ccd ON gdm.SLN = ccd.Category_Discount_Id INNER JOIN gold_category_discount_collection cdc ON gdm.SLN = cdc.Category_Discount_Id WHERE ? BETWEEN gdm.From_Date AND gdm.To_Date AND ccd.Dealer_Code = ? AND gdm.Active = 'Yes' AND cdc.Collection_Id = ?");

$Get_Discount_Collection->bind_param("sss",$Curr_Date,$Dealer,$Collection);
$Get_Discount_Collection->execute();
$Collect_Disc_Res=$Get_Discount_Collection->get_result();
$Collect_Discount=$Collect_Disc_Res->fetch_assoc();
$Get_Discount_Collection->close();

if($Collect_Disc_Res->num_rows>0)
{
	$Making_Charge_Disc=$Collect_Discount['Making_Charge_Disc'];
	$Diamond_Disc=$Collect_Discount['Diamond_Disc'];
	$Solitaire_Disc=$Collect_Discount['Solitaire_Disc'];
	$Stone_Disc=$Collect_Discount['Stone_Disc'];
}
}






$Today_Gold_Rate=$Gold_Rate;
	$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;
	}
	
//========Sale Discount===========//
$making_discount = $Making_Charge_Val*($Making_Charge_Disc/100);
$stone_discount = $Stone_Value*($Stone_Disc/100);
$diamond_discount = $Diamond_Value*($Diamond_Disc/100);
$solitaire_discount = $Solitaire_Value*($Solitaire_Disc/100);

$Total_Sale_Disc=$making_discount+$stone_discount+$diamond_discount+$solitaire_discount;


//====Taxable_Amount=====//
$Amt_Bf_Tax=$Total_Gold_Value+$Making_Charge_Val+$Stone_Value+$Diamond_Value+$Solitaire_Value-$Total_Sale_Disc;

$GST=3;
$Cgst = round(($GST / 2 * $Amt_Bf_Tax) / 100, 2);
$Sgst = round(($GST / 2 * $Amt_Bf_Tax) / 100, 2);
$GST_Amount = $Cgst + $Sgst;	
$Net_Price = $Amt_Bf_Tax + $GST_Amount;


//===========Update Billing Details======//

$Update_Details_Sql=$Mainclass->dbh->prepare("UPDATE gold_billing_order_details SET Total_Price=?,GST=?,Discounted_Amount=?,making_discount_percent=?,making_discount_amt=?,stone_discount_percent=?,stone_discount_amt=?,diamond_discount_percent=?,diamond_discount_amt=?,solitaire_discount_percent=?,solitaire_discount_amt=?,Sale_Discount=? WHERE Barcode=? AND Order_No=?");
$Update_Details_Sql->bind_param("ssssssssssssss",$Net_Price,$GST_Amount,$Amt_Bf_Tax,$Making_Charge_Disc,$making_discount,$Stone_Disc,$stone_discount,$Diamond_Disc,$diamond_discount,$Solitaire_Disc,$solitaire_discount,$Total_Sale_Disc,$Barcode,$Order);
$Update_Details_Sql->execute();
$Update_Details_Sql->close();
	
		
		
	}
	$Get_Details->close();

	
}

$Get_Aggregate_Val=$Mainclass->dbh->prepare("SELECT SUM(Total_Price) AS tnet,SUM(GST) AS tgst,SUM(Discounted_Amount) AS tdamt,SUM(making_discount_amt) AS tmcdisc, SUM(stone_discount_amt) AS tstonedisc, SUM(diamond_discount_amt) AS tdiadisc, SUM(solitaire_discount_amt) AS tsolidisc,SUM(Sale_Discount) AS tsaledisc,SUM(product_price) AS Tproduct_Val,SUM(total_making_charge) AS Tmakingcharge FROM gold_billing_order_details WHERE Order_No=?");
$Get_Aggregate_Val->bind_param("s",$Order);
$Get_Aggregate_Val->execute();
$Aggregate_Res=$Get_Aggregate_Val->get_result()->fetch_assoc();
$Get_Aggregate_Val->close();

$Total_Net=$Aggregate_Res['tnet'];
$Total_GST=$Aggregate_Res['tgst'];
$Total_Discounted_Amt=$Aggregate_Res['tdamt'];
$Total_MC_Disc=$Aggregate_Res['tmcdisc'];
$Total_Stone_Disc=$Aggregate_Res['tstonedisc'];
$Total_Diamond_Disc=$Aggregate_Res['tdiadisc'];
$Total_Solitaire_Disc=$Aggregate_Res['tsolidisc'];
$Total_Sale_Disc=$Aggregate_Res['tsaledisc'];
$Total_Product_Val=$Aggregate_Res['Tproduct_Val'];
$Total_Making_Charge=$Aggregate_Res['Tmakingcharge'];

$Update_Master=$Mainclass->dbh->prepare("UPDATE gold_billing_order_master SET Net_Total=?,GST=?,Discounted_Amount=?,making_discount_amt=?,stone_discount_amt=?,diamond_discount_amt=?,solitaire_discount_amt=?,Sale_Discount=?,total_product_price=?,total_making_charge=?,making_discount_percent=?,stone_discount_percent=?,diamond_discount_percent=?,solitaire_discount_percent=? WHERE Order_No=?");
$Update_Master->bind_param("sssssssssssssss",$Total_Net,$Total_GST,$Total_Discounted_Amt,$Total_MC_Disc,$Total_Stone_Disc,$Total_Diamond_Disc,$Total_Solitaire_Disc,$Total_Sale_Disc,$Total_Product_Val,$Total_Making_Charge,$Making_Charge_Disc,$Stone_Disc,$Diamond_Disc,$Solitaire_Disc,$Order);
$Update_Master->execute();
$Update_Master->close();

?>