<?php
include('db_connection.php');
$Mainclass=new DB_Controller();
$Product_Id=$_REQUEST['Product_Id'];
$Barcode=$_REQUEST['Barcode'];
$Member_Type=$_REQUEST['Mem_Ty'];
$Bill_Date=date('Y-m-d H:i:s');
$Donation="10";
if($_REQUEST['Order']=='')
{
	$Order_Id=$Mainclass->Generate_Gold_Id();
	$Field='Order_No,Order_Date,Dealer_Code,Member,Donation,Customer_Contact';
	$Values="'".$Order_Id."','".$Bill_Date."','".$_COOKIE['O_Code']."','".$Member_Type."','".$Donation."','".$_REQUEST['mobile']."'";
	$Insert_Master=$Mainclass->Insert_Data('gold_billing_order_master',$Field,$Values);
}
else
{
	$Order_Id=$_REQUEST['Order'];
}

$Get_Product=$Mainclass->Fetch_individual_data("gold_products_master","*","Barcode='".$Barcode."' and Active='Yes'");

$Get_Category=$Mainclass->Fetch_individual_data('gold_product_category_master','*',"SLN='".$Get_Product['ProductCategory']."'");

//$Check_Product_In_Details=$Mainclass->Challan_Details('gold_billing_order_details',"Order_No='".$Order_Id."' and Product_Code='".$Product_Id."'");

$Barcode=$Get_Product['Barcode'];
$Product_Name=$Get_Product['ProductName'];
$Prd_Category=$Get_Category['Category'];
$Gold_Wt=$Get_Product['GoldWeight'];
$Gold_Purity=$Get_Product['GoldPurity'];
$Today_Gold_Rate=$Mainclass->today_gold_customer_rate($Gold_Purity);
$Stone_Value=$Get_Product['total_stone_price'];
$Diamond_Value=$Get_Product['Diamond_Carat']*$Get_Product['DiamondValue'];
$Solitair_Value=$Get_Product['sollitaire_Carat']*$Get_Product['Solitaire_Value'];
$Making_Charge_Type=$Get_Product['MakingChargesType'];
$Making_Charge=$Get_Product['MakingChargesValue'];
$Wastage_Cost=$Get_Product['WastedCost'];
$HSN=$Get_Product['HSNCode'];



$Vendor_Name=$Get_Product['Vendor_Name'];
$VendorDesignCode=$Get_Product['VendorDesignCode'];
$Metal_Type=$Get_Product['Metal_Type'];
$StoneName=$Get_Product['StoneName'];
$No_Stone=$Get_Product['No_Stone'];
$StoneValue=$Get_Product['StoneValue'];
$Diamond_Creation_Type=$Get_Product['Diamond_Creation_Type'];
$DiamondQuality=$Get_Product['DiamondQuality'];
$DiamondColor=$Get_Product['DiamondColor'];
$DiamondType=$Get_Product['DiamondType'];
$No_Diamond=$Get_Product['No_Diamond'];
$DiamondValue=$Get_Product['DiamondValue'];
$Diamond_Carat=$Get_Product['Diamond_Carat'];
$Solitaire_Creation_Type=$Get_Product['Solitaire_Creation_Type'];
$SolitaireQuality=$Get_Product['SolitaireQuality'];
$SolitaireColor=$Get_Product['SolitaireColor'];
$SolitaireType=$Get_Product['SolitaireType'];
$No_Solitaire=$Get_Product['No_Solitaire'];
$Solitaire_Value=$Get_Product['Solitaire_Value'];
$sollitaire_Carat=$Get_Product['sollitaire_Carat'];
$Has_Size_Variation=$Get_Product['Has_Size_Variation'];
$Product_Size=$Get_Product['Product_Size'];
$Remark=$Get_Product['Remark'];
$has_stone=$Get_Product['has_stone'];
$has_diamond=$Get_Product['has_diamond'];
$has_solitaire=$Get_Product['has_solitaire'];
$IGINo=$Get_Product['IGINo'];
$Certifying_Organization=$Get_Product['Certifying_Organization'];



$Quantity='1';
if(strtoupper($Get_Product['Metal_Type'])=='GOLD')
{
	$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;
	}
    $Making_Charge_Val;
	$GST='3';
	/*$making_charge_gst=($Making_Charge_Val*$GST)/100;	
	$Stone=$Diamond_Value+$Stone_Value;
	$stone_gst=($Stone*$GST)/100;*/

	$Stone=$Diamond_Value+$Stone_Value+$Solitair_Value;

	//====Taxable_Amount=====//
	$Amt_Bf_Tax=$Total_Gold_Value+$Making_Charge_Val+$Stone+$Wastage_Cost;	
		
	$Cgst=round(($GST/2 * $Amt_Bf_Tax)/100,2);
    $Sgst=round(($GST/2 * $Amt_Bf_Tax)/100,2);
    $XR=$Cgst+$Sgst;	
		
	//$GST_Amount=($Amt_Bf_Tax*$GST)/100;
	$GST_Amount=$XR;
	
	$Net_Price=$Amt_Bf_Tax+$GST_Amount;
	$Product_Val=$Total_Gold_Value+$Stone;

	//insert into order details//
	// $Field2='Order_No,Order_Date,Dealer_Code,Product_Name,Product_Code,Quantity,Price_Rate,Price,Total_Price,GST,HSN_Code,GST_Rate,Member_Discount_Percent,Member_Discount,Sale_Discount_Percent,Sale_Discount,Product_Discount_Percent,Product_Discount,Dealer_Discount_Percent,Dealer_Discount,Product_Catagory,Product_Type,Birthday_Discount,Aniversary_Discount,Birthday_Discount_Percent,Aniversary_Discount_Percent,Discounted_Amount,Barcode,gold_rate,gold_wt,gold_purity,diamond_price,stone_price,making_charge_type,making_charge_val,wastage_cost,total_making_charge,product_price,solitaire_price';
	// $Values2="'".$Order_Id."','".$Bill_Date."','".$_COOKIE['O_Code']."','".$Product_Name."','".$Product_Id."',".$Quantity.",'','','".$Net_Price."','".$GST_Amount."','".$HSN."','".$GST."','','','','','','','','','".$Prd_Category."','".$Get_Category['Metal_Type']."','','','','','".$Amt_Bf_Tax."','".$Barcode."','".$Today_Gold_Rate."','".$Gold_Wt."','".$Gold_Purity."','".$Diamond_Value."','".$Stone_Value."','".$Making_Charge_Type."','".$Making_Charge."','".$Wastage_Cost."','".$Making_Charge_Val."','".$Product_Val."','".$Solitair_Value."'";
	// $Insert_Master=$Mainclass->Insert_Data("gold_billing_order_details",$Field2,$Values2);

// 	$Field2 = 'Order_No,Order_Date,Dealer_Code,Product_Name,Product_Code,Quantity,Total_Price,GST,HSN_Code,GST_Rate,Member_Discount_Percent,Member_Discount,Sale_Discount_Percent,Sale_Discount,Product_Discount_Percent,Product_Discount,Dealer_Discount_Percent,Dealer_Discount,Product_Catagory,Product_Type,Birthday_Discount,Aniversary_Discount,Birthday_Discount_Percent,Aniversary_Discount_Percent,Discounted_Amount,Barcode,gold_rate,gold_wt,gold_purity,diamond_price,stone_price,making_charge_type,making_charge_val,wastage_cost,total_making_charge,product_price,solitaire_price,Vendor_Name,VendorDesignCode,Metal_Type,StoneName,No_Stone,StoneValue,Diamond_Creation_Type,DiamondQuality,DiamondColor,DiamondType,No_Diamond,DiamondValue,Diamond_Carat,Solitaire_Creation_Type,SolitaireQuality,SolitaireColor,SolitaireType,No_Solitaire,Solitaire_Value,sollitaire_Carat,Has_Size_Variation,Product_Size,Remark,has_stone,has_diamond,has_solitaire,IGINo,Certifying_Organization,Total_Gold_Value';

// $Values2 = "'".$Order_Id."','".$Bill_Date."','".$_COOKIE['O_Code']."','".$Product_Name."','".$Product_Id."',".$Quantity.",'".$Net_Price."','".$GST_Amount."','".$HSN."','".$GST."','','','','','','','','','".$Prd_Category."','".$Get_Category['Metal_Type']."','','','','','".$Amt_Bf_Tax."','".$Barcode."','".$Today_Gold_Rate."','".$Gold_Wt."','".$Gold_Purity."','".$Diamond_Value."','".$Stone_Value."','".$Making_Charge_Type."','".$Making_Charge."','".$Wastage_Cost."','".$Making_Charge_Val."','".$Product_Val."','".$Solitair_Value."','".$Vendor_Name."','".$VendorDesignCode."','".$Metal_Type."','".$StoneName."','".$No_Stone."','".$StoneValue."','".$Diamond_Creation_Type."','".$DiamondQuality."','".$DiamondColor."','".$DiamondType."','".$No_Diamond."','".$DiamondValue."','".$Diamond_Carat."','".$Solitaire_Creation_Type."','".$SolitaireQuality."','".$SolitaireColor."','".$SolitaireType."','".$No_Solitaire."','".$Solitaire_Value."','".$sollitaire_Carat."','".$Has_Size_Variation."','".$Product_Size."','".$Remark."','".$has_stone."','".$has_diamond."','".$has_solitaire."','".$IGINo."','".$Certifying_Organization."','".$Total_Gold_Value."'";

// $Insert_Master = $Mainclass->Insert_Data("gold_billing_order_details", $Field2, $Values2);



$Field2 = 'Order_No,Order_Date,Dealer_Code,Product_Name,Product_Code,Quantity,Total_Price,
GST,HSN_Code,GST_Rate,Product_Catagory,Product_Type,Discounted_Amount,Barcode,gold_rate,
gold_wt,gold_purity,diamond_price,stone_price,making_charge_type,making_charge_val,
wastage_cost,total_making_charge,product_price,solitaire_price,Vendor_Name,VendorDesignCode,
Metal_Type,StoneName,No_Stone,StoneValue,Diamond_Creation_Type,DiamondQuality,DiamondColor,
DiamondType,No_Diamond,DiamondValue,Diamond_Carat,Solitaire_Creation_Type,SolitaireQuality,
SolitaireColor,SolitaireType,No_Solitaire,Solitaire_Value,sollitaire_Carat,Has_Size_Variation,
Product_Size,Remark,has_stone,has_diamond,has_solitaire,IGINo,Certifying_Organization,Total_Gold_Value';

$Values2 = "'".$Order_Id."','".$Bill_Date."','".$_COOKIE['O_Code']."','".$Product_Name."','".$Product_Id."',".$Quantity.",'".$Net_Price."',
'".$GST_Amount."','".$HSN."','".$GST."','".$Prd_Category."','".$Metal_Type."','".$Amt_Bf_Tax."','".$Barcode."','".$Today_Gold_Rate."',
'".$Gold_Wt."','".$Gold_Purity."','".$Diamond_Value."','".$Stone_Value."','".$Making_Charge_Type."','".$Making_Charge."',
'".$Wastage_Cost."','".$Making_Charge_Val."','".$Product_Val."','".$Solitair_Value."','".$Vendor_Name."','".$VendorDesignCode."',
'".$Metal_Type."','".$StoneName."','".$No_Stone."','".$StoneValue."','".$Diamond_Creation_Type."','".$DiamondQuality."','".$DiamondColor."',
'".$DiamondType."','".$No_Diamond."','".$DiamondValue."','".$Diamond_Carat."','".$Solitaire_Creation_Type."','".$SolitaireQuality."',
'".$SolitaireColor."','".$SolitaireType."','".$No_Solitaire."','".$Solitaire_Value."','".$sollitaire_Carat."','".$Has_Size_Variation."',
'".$Product_Size."','".$Remark."','".$has_stone."','".$has_diamond."','".$has_solitaire."','".$IGINo."','".$Certifying_Organization."','".$Total_Gold_Value."'";

$Mainclass->Insert_Data("gold_billing_order_details", $Field2, $Values2);


	
$Err=0;	
}
 echo $Order_Id."*".$Err;
?>