<?php 
ob_start();
session_start();
include "../config/config.php";
include "../config/session.php";

$condition='';
$get_pro_id = $_POST['pro_id'];

if($get_pro_id!=""){
	$condition.=" and pr.id=".$get_pro_id;
}


$statement = $pdo->prepare("SELECT pr.*,tbl_units.name as unit_name, tbl_units.short_name ,
tbl_products_name.name as products_name,
tbl_wastage.percentage as wastage_percentage,
tbl_ids_purity.short_name as k_short_name,
tbl_ids_purity.name as k_name, pr.pro_ids
FROM `tbl_product_variants` pr
LEFT JOIN tbl_units ON tbl_units.id=pr.unit
LEFT JOIN tbl_ids_purity ON tbl_ids_purity.id=pr.purity
LEFT JOIN tbl_products_name ON tbl_products_name.id=pr.product_name
LEFT JOIN tbl_wastage ON tbl_wastage.id=pr.wastage
WHERE 1=1 and pr.status=1 $condition");
    $statement->execute();
    $emp_data = $statement->fetchAll(PDO::FETCH_ASSOC);
    $totalData = $statement->rowCount();
    
    
    
    
    
    
    if($totalData>0){
    
    $pro_ids = $emp_data[0]['pro_ids'];
    $purity = $emp_data[0]['purity'];
    
    if($pro_ids==1){
        $rate_cond = " and purity_id=$purity";
    }else if($pro_ids==2){
        $rate_cond = " and purity_id=$purity";
    }else if($pro_ids==4){
        $rate_cond = " and purity_id=$purity";
    }else{
        $rate_cond = "";
    }
    
    // Rate Calculation Starts Here
    $statement_price = $pdo->prepare("SELECT * FROM `tbl_prices` WHERE product_id=? $rate_cond");
    $statement_price->execute(array($pro_ids));
    $price_data = $statement_price->fetchAll(PDO::FETCH_ASSOC);
    $price_totalData = $statement_price->rowCount(); 
        
    $price_amount = $price_data[0]['amount'];
    // Rate Calculation Endss Here
    
    
    
    
    
    
    $variant_product_id = $emp_data[0]['id'];
    $quantity = $emp_data[0]['quantity'];
    
    
    $cross_weight = $emp_data[0]['cross_weight'];
    $stone_weight = $emp_data[0]['stone_weight'];
    $stone_cost = $emp_data[0]['stone_cost'];
    $net_weight = $emp_data[0]['net_weight'];
    
    
    
    if($emp_data[0]['wastage_percentage']==""){
        $wastage_percentage = 0;
    }else{
        $wastage_percentage = $emp_data[0]['wastage_percentage'];
    }
    
    if($emp_data[0]['unit']==1){
        $stone_amount = $stone_cost;
        $product_amount = $net_weight*$price_amount;
    }else if($emp_data[0]['unit']==2){
        $stone_amount = $stone_cost;
        $product_amount = $net_weight*$price_amount;
    } 
    
    $wastage_amount = ($product_amount*$wastage_percentage)/100;
    $total = $product_amount+$stone_amount+$wastage_amount;
    
    
    $purity_id = $emp_data[0]['purity'];
    
    
    
?>


<tr class="porow">
    
    
    
    <td style="">
        <b><?=$emp_data[0]['products_name']?> - <?=$emp_data[0]['hsn_code']?></b>
        <input type="hidden" name="sale_present_or_not[]" value="0">
        <input type="hidden" name="product_id[]" class="product_ids" value="<?=$variant_product_id?>">
        <input type="hidden" name="product_name[]" class="form-control" value="<?=$emp_data[0]['products_name']?>" id="product_name" placeholder="Product Name" required>
        <input type="hidden" name="quantity[]" class="form-control qty" value="<?=$quantity?>" id="product_name" placeholder="Qty" required data-stockqty="<?=$emp_data[0]['quantity']?>">
        <input type="hidden" name="gold_amount[]" class="form-control price" value="<?=$product_amount?>" id="gold_amount_<?=$variant_product_id?>"  style="padding: 0px;">
        <input type="hidden" name="stone_amount[]" class="form-control stone_amount_<?=$purity_id?>" value="<?=$stone_cost?>" id="stone_amount_<?=$variant_product_id?>"  style="padding: 0px;">
        <input type="hidden" name="wastage_amount[]" class="form-control wastage wastage_amount_<?=$purity_id?>" value="<?=$wastage_amount?>" id="wastage_amount_<?=$variant_product_id?>"  style="padding: 0px;">
        <input type="hidden" name="sub_total[]" class="form-control total" value="<?=$total?>" id="total_<?=$variant_product_id?>" style="padding: 0px;" >
        <input type="hidden" name="product_gm_amount[]" class="form-control pro_gm_amount" value="<?=$price_amount?>" >
        <input type="hidden" name="stone_gm_amount[]" class="form-control stone_gm_amount" value="<?=$stone_amount?>" >
        <input type="hidden" name="cross_weight[]" class="form-control cross_wt cross_wt_<?=$purity_id?>" id="cross_wt_<?=$purity_id?>" value="<?=$cross_weight?>" >
        <input type="hidden" name="stone_weight[]" class="form-control stone_wt stone_wt_<?=$purity_id?>" id="stone_wt_<?=$purity_id?>" value="<?=$stone_weight?>" >
        <input type="hidden" name="net_weight[]" class="form-control net_wt net_wt_<?=$purity_id?>" id="net_wt_<?=$purity_id?>" value="<?=$net_weight?>" >
        
        <?php
        if($pro_ids==3){
            echo '<input type="hidden" name="" id="" value="'.$total.'" class="form-control totals" readonly>';
        }
        ?>
    </td>

    <td>
        <b><?=$emp_data[0]['k_short_name']?></b>
    </td>

    <td>
        <b><?=$wastage_percentage?> %</b>
    </td>
    
    <td>
        <b><?=$cross_weight?> - <?=$stone_weight?> - <?=$net_weight?></b>
    </td>
    <td style="">
        <b>
            <span style="color: green;"><i class="fa fa-inr"></i> <?=number_format($price_amount,2)?></span>
        </b>
    </td>
    <td>
        <b><?=number_format($product_amount,2)?></b>
    </td>
    <td>
        <b><?=number_format($stone_cost,2)?></b>
    </td>
    <td>
        <b><?=number_format($wastage_amount,2)?></b>
    </td>
    <td>
        <b><i class="fa fa-inr"></i> <?=number_format($total,2)?></b>
    </td>
    <td>
        <span class="btn btn-danger btn-sm remove_product" style="padding: 5px;"><i class="bi bi-trash3-fill"></i></span>
    </td>
</tr>

<?php } ?>

