<?php 
include "../config.php";
$ticket=$_POST['ticket'];
//echo $customer;
if ($ticket != '')
{
$sql = "select *,(select productname from productdetail where productdetail.productcode = ticket.product && status!='deleted')as productname,(select customerdetails.name from customerdetails where customerdetails.code = ticket.customer && status!='deleted')as customername,(select customerdetails.gst from customerdetails where customerdetails.code = ticket.customer && status!='deleted')as gstin,CURRENT_DATE() as curdate from ticket where ticket='$ticket'&& status!='deleted'";
//echo $sql;
$conn=mysqli_query($connect,$sql);
while($row=mysqli_fetch_array($conn))
{
      
           $vehicle = $row['vehicleno'];
           $chakka = $row['chakka'];
           $customer = $row['customer'];
           $customer_name = $row['customername'];
           $product = $row['product'];
           $product_name = $row['productname'];
           $empty_weight = $row['emptyweight'];
           $load_type = $row['loadtype'];
           $cft = $row['cft'];
           $ori_cft = $row['ori_cft'];
           $delivery = $row['delivery'];
           $date_ori = $row['curdate'];
          $sql = "select productdetail.productcode,productdetail.price,unittokg from productdetail where productdetail.productcode = '$product' && productdetail.status !='deleted'";
		  $gstin=$row['gstin'];
           //echo $sql;
           $conn=mysqli_query($connect,$sql);
           while($row=mysqli_fetch_array($conn))
           {
               
                    $proprice = $row['price'];
                    $conversion=$row['unittokg'];
              
           }
           $cuspro = "SELECT customerrate.price,customerrate.price_est,customerrate.productname from customerrate where customerrate.customer = '$customer' && customerrate.productcode = '$product' && status !='deleted'";
           $connn=mysqli_query($connect,$cuspro);
           while($roww=mysqli_fetch_array($connn))
           {
               
                    $propricecus = $roww['price'];
                    $propricecus_est = $roww['price_est'];
              
           }
           $limit = "select blockdate,purchaseopening,salesopening,creditlimit from customerdetails where code='$customer' && status!='deleted'";
$limit_conn=mysqli_query($connect,$limit);
while($row_limit=mysqli_fetch_array($limit_conn))
{
      
            $block_date= $row_limit['blockdate'];
            if($block_date!='-'){
             $date_change = date_create($block_date);
             $date_format = date_format($date_change,"d-m-Y");
            }
              $purchase_opening = $row_limit['purchaseopening'];
              $sales_opening = $row_limit['salesopening'];
              $credit_value = $row_limit['creditlimit'];
}             
if($load_type == "Load"){
    $purchase_balance = mysqli_query($connect,"select ifnull(sum(totalamount),0)as purchase from purchasedetails where customer='$customer'&& status!='deleted'");
$dis_purchase = mysqli_fetch_assoc($purchase_balance);
$purchase_value = $dis_purchase['purchase'];
$cash_balance = mysqli_query($connect,"select ifnull(sum(amount),0)as amount,ifnull(sum(otheramount),0)as otheramount,ifnull(sum(advance),0)as advance from debitdetails where customer='$customer'&& status!='deleted'");
$dis_cash = mysqli_fetch_assoc($cash_balance);
$cash_value = $dis_cash['amount'];
$other_value = $dis_cash['otheramount'];
$advance_value = $dis_cash['advance'];
$advance_amount = mysqli_query($connect,"select ifnull(sum(advance),0)as advance from advance where customer='$customer'&& status!='deleted'");
$advance_dis = mysqli_fetch_assoc($advance_amount);
$advance_total = $advance_dis['advance']-$dis_cash['advance'];
$advance_total_amount = $advance_dis['advance'];
    $limit =$advance_total-$purchase_opening-$purchase_value+$dis_cash['amount']+$dis_cash['otheramount']+$dis_cash['advance'];
}
if($load_type == "Empty" || $load_type == "Return"){
    $cash_balance = mysqli_query($connect,"select ifnull(sum(amount),0)as amount,ifnull(sum(otheramount),0)as otheramount from creditdetails where customer='$customer'&& status!='deleted'");
$dis_cash = mysqli_fetch_assoc($cash_balance);
$cash_value = $dis_cash['amount'];
$other_value = $dis_cash['otheramount'];
    $net_amount = mysqli_query($connect,"select ifnull(sum(totalamount),0)as totalamt from second_transaction where customer='$customer' && status!='deleted'");
    $net_amt = mysqli_fetch_assoc($net_amount);
    $total_amount = $net_amt['totalamt'];
    $net_amount_est = mysqli_query($connect,"select ifnull(sum(totalamount),0)as totalamt from second_transaction_est where customer='$customer' && status!='deleted'");
    $net_amt_est = mysqli_fetch_assoc($net_amount_est);
    $total_amount_est = $net_amt_est['totalamt'];
    $advance_total_amount = 0;
    $limit = $credit_value == '-' ? '-' : $credit_value-$sales_opening-$total_amount-$total_amount_est+$cash_value+$other_value;
}
$block = $block_date;
$product_price = empty($propricecus)?$proprice:$propricecus;
$product_price_est = empty($propricecus_est)?$proprice:$propricecus_est;
           
}
echo $vehicle."||".$chakka."||".$customer."||".$product."||".$product_price."||".$empty_weight."||".$block."||".$limit."||".$load_type."||".$product_name."||".$cft."||".$ori_cft."||".$customer_name."||".$conversion."||".$delivery."||".$date_ori."||".$advance_total_amount."||".$product_price.'||'.$gstin;
}
else
{
 echo "";
}

?>
