
<?php 
include "../config.php";
error_reporting(0);
//$product = $_POST['product'];
$fromd = $_POST['fromd'];
$tod = $_POST['tod'];
    $datefr = date_create($fromd);
    $datefrom = date_format($datefr,"d/m/Y");
    $datet = date_create($tod);
    $dateto = date_format($datet,"d/m/Y");
//echo $customer;
// echo $fromd;
// echo $tod;   
$rep=mysqli_query($connect,"select *,(select customerdetails.name from customerdetails where customerdetails.code = return_transaction.customer && status!='deleted')as cusname,(select productdetail.productname from productdetail where productdetail.productcode = return_transaction.product && status!='deleted' )as proname,(select productdetail.unit from productdetail where productdetail.productcode = return_transaction.product && status!='deleted')as unit,(select ticket.tripno from ticket where ticket.ticket = return_transaction.ticket && status!='deleted' )as trip from return_transaction where date
between '$fromd' and '$tod' && status !='deleted'");
$pricee = 0;
$cgst51 = 0;
$cgst52 = 0;
$cgst91=0;
$cgst92=0;
$igst55=0;
$igst99=0;
$tax = 0;
$subtotal = 0;
$totalamountc = 0; 
while($repshow = mysqli_fetch_array($rep))

{
    $sno +=1;
    $datechk=$repshow['date'];
    $dateval = date_create($datechk);
    $date = date_format($dateval,"d/m/Y");
$customer = $repshow['customer'];
$cus=mysqli_query($connect,"select * from customerdetails where  code = '$customer' && status!='deleted'");

//echo "select * from customerdetails where  code = '$customer' && status!='deleted'";
while($repcus = mysqli_fetch_array($cus))
 {
    $state_name = $repcus['state'];
    $state_lower_case=strtolower(preg_replace("/ /i","",$state_name));
    $product = $repshow['product'];
$pro=mysqli_query($connect,"select * from productdetail where status !='deleted' && productcode = '$product'");
while($reppro = mysqli_fetch_array($pro))
{
    $price = $repshow['price'];
    $qty = $repshow['netqty'];
    $gst = $reppro['tax'];
    $inclu = $price * 100 /( 100 + $gst);
    $sub = $inclu * $qty;
    $tax = (0.01*$gst*$sub);
    if($gst == '5')
    {
        if($state_lower_case =="tamilnadu")
        {
        $cgst5 = $tax/2;
        $sgst5 = $tax/2;
        $cgst9 = "0";
        $sgst9 = "0"; 
        $igst5 = "0";
    }
    if($state_lower_case !="tamilnadu")
    {
    $cgst5 =0;
    $sgst5 = 0;
    $cgst9 = 0;
    $sgst9 = 0; 
    $igst5 = $tax;
}
    }
    if($gst == '18')
    {
        if($state_lower_case=="tamilnadu"){
        $cgst9 = $tax/2;
        $sgst9 = $tax/2;
        $cgst5 = "0";
        $sgst5 = "0";
        $igst9 = "0"; 
    } if($state_lower_case!="tamilnadu"){
        $cgst9 = 0;
        $sgst9 = 0;
        $cgst5 = 0;
        $sgst5 = 0;
        $igst9 = $tax; 
    }
        
    }
    $product = $repshow['product'];
  $pricee +=number_format($inclu,2,'.','') ;
  
  $cgst51 +=number_format($cgst5,2,'.','');
  $cgst52 += number_format($sgst5,2,'.','');
  $cgst91+=number_format($cgst9,2,'.','');
  $cgst92+=number_format($sgst9,2,'.','');
  $igst55+=number_format($igst5,2,'.','');
  $igst99+=number_format($igst9,2,'.','');
  
  $subtotal += number_format($sub,2,'.','');
  $totalamountc += number_format($repshow['totalamount'],2,'.',''); 

  $product_list[]=(object)["salescode"=>$repshow['salescode'],"date"=>$date,"customer"=>$repshow['cusname'],"gstno"=>$repcus['gst'],"state"=>$repcus['state'],"product"=>$repshow['cusname'],"product"=>$repshow['proname'],"hsn"=>$reppro['hsn'],"rate"=>number_format($inclu,2,'.',''),"quantity"=>$repshow['netqty'],"units"=>$repshow['unit'],"subtotal"=>number_format($sub,2,'.',''),"tax"=>$reppro['tax'],"cgst"=>number_format($cgst5,2,'.',''),"sgst"=>number_format($sgst5,2,'.',''),"igst"=>number_format($igst5,2,'.',''),"netamount"=>number_format($repshow['totalamount'],2,'.','')];
}
}
}
   
echo json_encode($product_list);
   

?>
