<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type:application/json");
header("Access-Control-Allow-Origin-Methods:POST");
header("Access-Control-Allow-Headers:Access-Control-Allow-Headers,Content-Type,Access-Control-Allow-Origin-Methods,Authorization,X-Requested-With");

require_once("../conn.php");



$data = json_decode(file_get_contents("php://input"));


$userid=$data->userid;
$seasonid=$data->seasonid;
$grade_type="";
$data1=array();
// get grower locations
$bale_prices=array();
if ($userid!="") {
  


$sql11 = "Select  distinct buyer_grade from  warehousing_sold_bales_reclassification   where seasonid=$seasonid ORDER BY buyer_grade desc";

$result = $conn->query($sql11);
 
 if ($result->num_rows > 0) {
   // output data of each row
   while($row = $result->fetch_assoc()) {

    $buyer_grade=$row["buyer_grade"];
    $mass=0;
    $bale_prices=array();
    $grade_type="";



    $sql22 = "Select * from grades_description_categories join grades_description on grades_description.id=grades_description_categories.grades_descriptionid where grade='$buyer_grade' limit 1";
    $result22 = $conn->query($sql22);
     if ($result22->num_rows > 0) {
       // output data of each row
       while($row22 = $result22->fetch_assoc()) {
        // product id
        $grade_type=$row22["description"];
        
       }

     }


    $sql112 = "Select  id  from  warehousing_sold_bales_reclassification  where seasonid=$seasonid and buyer_grade='$buyer_grade' and (warehousing_sold_bales_reclassification.id) in (Select truck_to_processing_bales.warehousing_sold_bales_reclassificationid from truck_to_processing_bales)";
    $result1 = $conn->query($sql112);
    $bales_count=$result1->num_rows;


    $sql112 = "Select  warehousing_sold_bales_reclassification.id,mass  from  warehousing_sold_bales_reclassification join warehousing_sold_bales on warehousing_sold_bales_reclassification.warehousing_sold_balesid=warehousing_sold_bales.id  where warehousing_sold_bales_reclassification.seasonid=$seasonid and warehousing_sold_bales_reclassification.buyer_grade='$buyer_grade' and (warehousing_sold_bales_reclassification.id) in (Select truck_to_processing_bales.warehousing_sold_bales_reclassificationid from truck_to_processing_bales) ";
    $result1 = $conn->query($sql112);
    if ($result1->num_rows > 0) {
   // output data of each row
     while($row1 = $result1->fetch_assoc()) {

        $mass+=$row1['mass'];
       }
     }
      

     $sql112 = "Select distinct  price  from  warehousing_sold_bales_reclassification join warehousing_sold_bales on warehousing_sold_bales_reclassification.warehousing_sold_balesid=warehousing_sold_bales.id  where warehousing_sold_bales_reclassification.seasonid=$seasonid and warehousing_sold_bales_reclassification.buyer_grade='$buyer_grade' and (warehousing_sold_bales_reclassification.id) in (Select truck_to_processing_bales.warehousing_sold_bales_reclassificationid from truck_to_processing_bales)";
    $result1 = $conn->query($sql112);
    if ($result1->num_rows > 0) {
   // output data of each row
     while($row1 = $result1->fetch_assoc()) {

        $temp=array("prices"=>$row1["price"]);
        array_push($bale_prices,$temp);

       }
     }


     $temp=array("buyer_grade"=>$row["buyer_grade"],"grade_type"=>$grade_type,"bale_batch"=>"1","bales_count"=>$bales_count,"mass"=>$mass,"prices"=>$bale_prices);
    array_push($data1,$temp);


   }
 }





}

 echo json_encode($data1);


?>


