
<?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 = second_transaction.customer && status!='deleted')as cusname,(select productdetail.productname from productdetail where productdetail.productcode = second_transaction.product && status!='deleted' )as proname,(select productdetail.unit from productdetail where productdetail.productcode = second_transaction.product && status!='deleted')as unit,(select ticket.tripno from ticket where ticket.ticket = second_transaction.ticket && status!='deleted' )as trip from second_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'];
	$exittime=$repshow['time'];
    $dateval = date_create($datechk);
    $date = date_format($dateval,"d/m/Y");
$customer = $repshow['customer'];
$vehicleno = $repshow['vehicleno'];

$mode = $repshow['modeof'];
$delivery = $repshow['delivery'];
$remark = $repshow['remark'];
$employee = $repshow['editby'];

$ticket_code = $repshow['ticket'];
$ticket_trip=mysqli_query($connect,"select tripno,date,time from ticket where status !='deleted' && ticket='$ticket_code'");
while($tkt_show = mysqli_fetch_array($ticket_trip))
{
   
    $trip_no = $tkt_show['tripno'];
    $entry_date = $tkt_show['date'];
    $entry_time = $tkt_show['time'];
}


$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_cus = $reppro['price'];
    $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)["Indate"=>$entry_date,"Intime"=>$entry_time,"Exitdate"=>$repshow['date'],"Exittime"=>$exittime,"Vehno"=>$repshow['vehicleno'],"InvoiceCode"=>$repshow['salescode'],"Cust"=>$repshow['cusname'],"GSTNo"=>$repcus['gst'],"Prod"=>$repshow['proname'],"Qty"=>$repshow['netqty'],"CusPrice"=>$price_cus,"Grossprice"=>number_format($inclu,2,'.',''),"Gross"=>number_format($sub,2,'.',''),"tax"=>$reppro['tax'],"cgst"=>number_format($cgst5,2,'.',''),"sgst"=>number_format($sgst5,2,'.',''),"igst"=>number_format($igst5,2,'.',''),"Total"=>number_format($repshow['totalamount'],2,'.',''),"Mode"=>$mode,"Delivery"=>$delivery,"Remarks"=>$remark,"Employee"=>$employee];
}
}
}


$sql_mini_statement = "select modeof,ifnull(sum(totalamount),0) as netamt,count(modeof) as modeno from  second_transaction where status!='deleted'  && date between '$fromd' and '$tod'  Group by modeof";
//echo $sql_mini_statement;
$conn_sql_mini = mysqli_query($connect,$sql_mini_statement);
$st_sn_no = 0;
while($mini_statement_dis=mysqli_fetch_array($conn_sql_mini)){
    $st_sn_no +=1; 
    
    $mini_statement_out[]=(object)['sno'=>$st_sn_no,"modeof"=>$mini_statement_dis['modeof'],"tamt"=>$mini_statement_dis['netamt'],"modeno"=>$mini_statement_dis['modeno']]; 
}


$output_final = (object)["products"=>$product_list,"Cummulative"=>$mini_statement_out];
   
//echo json_encode($product_list);
  echo json_encode($output_final); 

?>
