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

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1); 
error_reporting(E_ALL);

//$Get_Challan_Products="SELECT Product_Code,Barcode,gold_purity,gold_wt,diamond_ct,solitaire_ct"

$Get_Challan_Products=$Mainclass->dbh->prepare("SELECT 
        dgm.Barcode 
    FROM 
        dealer_gold_stock_master dgm
    LEFT JOIN 
        gold_products_master gpm ON dgm.Barcode = gpm.Barcode
    WHERE 
        dgm.Quantity > 0 
        AND gpm.SLN IS NULL");
$Get_Challan_Products->execute();
$data_res=$Get_Challan_Products->get_result();
$barcodes_to_fetch = [];
$i=1;
while($ft_res=$data_res->fetch_assoc())
{
	$barcodes_to_fetch[] = $ft_res['Barcode'];
	//$Barcode=$ft_res['Barcode'];	
	//echo $i." ".$Barcode."<br>";
}
$Get_Challan_Products->close();

$placeholders = implode(',', array_fill(0, count($barcodes_to_fetch), '?'));

	$Get_Product_Details=$Mainclass->dbh->prepare("SELECT gid.Ref_Product_Code AS Barcode,gid.Product_Code,gid.Product_Category,gid.gold_wt,gid.gold_purity,gid.no_diamond,gid.diamond_carat,gid.diamond_rate,gid.no_stone,gid.stone_price,gid.total_stone_value,gid.no_solitaire,gid.sollitaire_carat,gid.sollitaire_value,gid.making_charge_type,gid.making_charge_val,gpm.Metal_Type,gpm.ParentId,gpm.ProductName,gpm.HSNCode,gpm.StoneName,gpm.Diamond_Creation_Type,gpm.DiamondQuality,gpm.DiamondColor,gpm.DiamondType,gpm.Color,gpm.Solitaire_Creation_Type,gpm.SolitaireQuality,gpm.SolitaireColor,gpm.SolitaireType FROM gold_indent_details gid LEFT JOIN gold_products_master gpm ON gid.Product_Code=gpm.Productid WHERE gid.Ref_Product_Code IN ($placeholders)");
	$types = str_repeat('s', count($barcodes_to_fetch));
	$Get_Product_Details->bind_param($types, ...$barcodes_to_fetch);
	$Get_Product_Details->execute();
	$Details_Res=$Get_Product_Details->get_result();
	$productdetails_map = [];
	while($ft_details=$Details_Res->fetch_assoc())
	{
		$Barcode = $ft_details['Barcode'];
		$added_on=date('Y-m-d H:i:s');
		if (!isset($productdetails_map[$Barcode])) {
        $productdetails_map[$Barcode] = [];
    }
		
		
		
		
		$productdetails_map[$Barcode][]=[
			"Metal_Type"=>$ft_details['Metal_Type'],
			"ProductCategory"=>$ft_details['Product_Category'],
			"GoldWeight"=>$ft_details['gold_wt'],
			"ParentId"=>$ft_details['ParentId'],
			"ProductName"=>$ft_details['ProductName'],
			"Productid"=>$ft_details['Product_Code'],
			"Barcode"=>$Barcode,
			"HSNCode"=>$ft_details['HSNCode'],
			"GoldPurity"=>$ft_details['gold_purity'],
			"StoneName"=>$ft_details['StoneName'],
			"No_Stone"=>$ft_details['no_stone'],
			"StoneValue"=>$ft_details['stone_price'],
			"total_stone_price"=>$ft_details['total_stone_value'],
			"Diamond_Creation_Type"=>$ft_details['Diamond_Creation_Type']!=''?$ft_details['Diamond_Creation_Type']:'',
			"DiamondQuality"=>$ft_details['DiamondQuality']!=''?$ft_details['DiamondQuality']:'',
			"DiamondColor"=>$ft_details['DiamondColor']!=''?$ft_details['DiamondColor']:'',
			"DiamondType"=>$ft_details['DiamondType']!=''?$ft_details['DiamondType']:'',
			"No_Diamond"=>$ft_details['no_diamond'],
			"DiamondValue"=>$ft_details['diamond_rate'],
			"Diamond_Carat"=>$ft_details['diamond_carat'],
			"Solitaire_Creation_Type"=>$ft_details['Solitaire_Creation_Type']!=''?$ft_details['Solitaire_Creation_Type']:'',
			"SolitaireQuality"=>$ft_details['SolitaireQuality']!=''?$ft_details['SolitaireQuality']:'',
			"SolitaireColor"=>$ft_details['SolitaireColor']!=''?$ft_details['SolitaireColor']:'',
			"SolitaireType"=>$ft_details['SolitaireType']!=''?$ft_details['SolitaireType']:'',
			"No_Solitaire"=>$ft_details['no_solitaire'],
			"Solitaire_Value"=>$ft_details['sollitaire_value'],
			"sollitaire_Carat"=>$ft_details['sollitaire_carat'],
			"MakingChargesType"=>$ft_details['making_charge_type'],
			"MakingChargesValue"=>$ft_details['making_charge_val'],
			"has_stone"=>$ft_details['no_stone']>'0'?'Yes':'No',
			"has_diamond"=>$ft_details['no_diamond']>'0'?'Yes':'No',
			"has_solitaire"=>$ft_details['no_solitaire']>'0'?'Yes':'No',
			"Active"=>'Yes',
			"Color"=>$ft_details['Color']!=''?$ft_details['Color']:'',
			"added_on"=>$added_on,
		];
	}
	$Get_Product_Details->close();
	
$i++;

/*foreach ($productdetails_map as $Barcode => $details_array) {
	echo "<pre>";
print_r($details_array);
	echo "</pre>";
	//echo $details_array[0]['Barcode']."-".$details_array[0]['Color']."<br>";
}
*/
$Insert_Product=$Mainclass->dbh->prepare("INSERT INTO gold_products_master (Metal_Type,ProductCategory,GoldWeight,ParentId,ProductName,Productid,Barcode,HSNCode,GoldPurity,Stock,StoneName,No_Stone,StoneValue,total_stone_price,Diamond_Creation_Type,DiamondQuality,DiamondColor,DiamondType,No_Diamond,DiamondValue,Diamond_Carat,Solitaire_Creation_Type,SolitaireQuality,SolitaireColor,SolitaireType,No_Solitaire,Solitaire_Value,sollitaire_Carat,MakingChargesType,MakingChargesValue,has_stone,has_diamond,has_solitaire,Added_On,Active,Color) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
$Insert_Product->bind_param('sssssssssisissssssissssssissssssssss',$metal_type,$category,$gold_wt,$parentid,$prdname,$prdid,$barcode,$hsn,$purity,$stock,$stonename,$no_stone,$stone_val,$t_stone_price,$diamond_creation_type,$diamond_quality,$dia_color,$dia_type,$no_diamond,$diamond_val,$dia_carat,$sol_creation_type,$sol_quality,$sol_color,$sol_type,$no_solitaire,$sol_val,$sol_carat,$making_type,$making_charge,$has_stone,$has_dia,$has_solitaire,$added,$active,$color);
foreach ($productdetails_map as $Barcode => $details_array) {    
	$metal_type=$details_array[0]['Metal_Type'];
	$category=$details_array[0]['ProductCategory'];
	$gold_wt=$details_array[0]['GoldWeight'];
	$parentid=$details_array[0]['ParentId'];
	$prdname=$details_array[0]['ProductName'];
	$prdid=$details_array[0]['Productid'];
	$barcode=$details_array[0]['Barcode'];
	$hsn=$details_array[0]['HSNCode'];
	$purity=$details_array[0]['GoldPurity'];
	$stock=0;
	$stonename=$details_array[0]['StoneName'];
	$no_stone=$details_array[0]['No_Stone'];
	$stone_val=$details_array[0]['StoneValue'];
	$t_stone_price=$details_array[0]['total_stone_price'];
	$diamond_creation_type=$details_array[0]['Diamond_Creation_Type'];
	$diamond_quality=$details_array[0]['DiamondQuality'];
	$dia_color=$details_array[0]['DiamondColor'];
	$dia_type=$details_array[0]['DiamondType'];
	$no_diamond=$details_array[0]['No_Diamond'];
	$diamond_val=$details_array[0]['DiamondValue'];
	$dia_carat=$details_array[0]['Diamond_Carat'];
	$sol_creation_type=$details_array[0]['Solitaire_Creation_Type'];
	$sol_quality=$details_array[0]['SolitaireQuality'];
	$sol_color=$details_array[0]['SolitaireColor'];
	$sol_type=$details_array[0]['SolitaireType'];
	$no_solitaire=$details_array[0]['No_Solitaire'];
	$sol_val=$details_array[0]['Solitaire_Value'];
	$sol_carat=$details_array[0]['sollitaire_Carat'];
	$making_type=$details_array[0]['MakingChargesType'];
	$making_charge=$details_array[0]['MakingChargesValue'];
	$has_stone=$details_array[0]['has_stone'];
	$has_dia=$details_array[0]['has_diamond'];
	$has_solitaire=$details_array[0]['has_solitaire'];
	$active=$details_array[0]['Active'];
	$color=$details_array[0]['Color'];
	$added=$details_array[0]['added_on'];
	
	if(!$Insert_Product->execute())
	{
		die("Execute failed: " . $Insert_Product->error);
	}	
}
$Insert_Product->close();

echo "done";
?>