= 4) { $startYear = $year; $endYear = $year + 1; } else { $startYear = $year - 1; $endYear = $year; } return $startYear; // You can return $startYear . '-' . substr($endYear, -2); if needed } $data = []; $dealer_id = $_SESSION['dealer_id']; $get_prod = "SELECT * FROM tbl_dealer_order WHERE dealer_id='$dealer_id' ORDER BY created_at ASC"; $get_bill = mysqli_query($conn, $get_prod); // Track invoice counts per financial year $invoice_counters = []; while ($set_bill = mysqli_fetch_array($get_bill, MYSQLI_ASSOC)) { // Get state or country name $state = ''; if ($set_bill['cust_country'] != 99) { $get_sta = mysqli_query($conn, "SELECT * FROM shop_country WHERE country_id='" . $set_bill['cust_country'] . "'"); if (mysqli_num_rows($get_sta) > 0) { $set_sta = mysqli_fetch_array($get_sta, MYSQLI_ASSOC); $state = $set_sta['name']; } } else { $get_sta = mysqli_query($conn, "SELECT * FROM tbl_state WHERE stateid='" . $set_bill['cust_state'] . "'"); if (mysqli_num_rows($get_sta) > 0) { $set_sta = mysqli_fetch_array($get_sta, MYSQLI_ASSOC); $state = $set_sta['state']; } } // Get total amount $get_amou = mysqli_query($conn, "SELECT SUM(price * qty) as amount FROM tbl_dealer_sales WHERE bill_id='" . $set_bill['bill_id'] . "' AND dealer_id='$dealer_id'"); $set_amou = mysqli_fetch_array($get_amou, MYSQLI_ASSOC); $total_amount = $set_amou['amount']; // Apply discount if any if ($set_bill['cust_discount'] != 0) { $disc_price = ($total_amount * $set_bill['cust_discount']) / 100; $flat_amount = $total_amount - $disc_price; $amount = number_format((float)$flat_amount, 2, '.', ''); } else { $amount = number_format((float)$total_amount, 2, '.', ''); } // Get financial year $fin_year = getFinancialYear($set_bill['created_at']); // Maintain running invoice number per financial year if (!isset($invoice_counters[$fin_year])) { $invoice_counters[$fin_year] = 1; } else { $invoice_counters[$fin_year]++; } $invoice_number = $fin_year . '-' . str_pad($invoice_counters[$fin_year], 4, '0', STR_PAD_LEFT); $url = '' . $invoice_number . ''; $data[] = array( 'bill_id' => $url, 'name' => $set_bill['cust_name'], 'state' => $state, 'phone' => $set_bill['cust_phone'], 'amount' => $amount, 'order_date' => date('d-m-Y', strtotime($set_bill['created_at'])), 'id' => $set_bill['id'] ); } if (!empty($data)) { echo json_encode(array("aaData" => $data)); } else { echo json_encode(array("aaData" => [])); } ?>