<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST');
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Authorization");
include '../../../config/index.php';
$data = array();

$sql_1 = "SELECT * FROM QuarkySold  WHERE Customer='Parent'  ";
$result_1 = mysqli_query($conn, $sql_1);
if (mysqli_num_rows($result_1) > 0) {
    while ($row_1 = mysqli_fetch_assoc($result_1)) {
        $QuarkyCode = "";
        $MaximumStudents = "";
        $RegisteredStudents = "";
        $LimitReached = "";
        $p_name = "";
        $p_last_name = "";
        $p_email = "";
        $p_phone = "";
        $PaymentStatus = "";
        $AmountPaid = "";
        $NoKits = "";
        $TotalAmountInvoices =0;
        $sql_d = "SELECT * FROM QuarkyKits WHERE QuarkyKitID='" . $row_1["QuarkyKitID"] . "'";
        $result_d = mysqli_query($conn, $sql_d);
        if (mysqli_num_rows($result_d) > 0) {
            while ($row_d = mysqli_fetch_assoc($result_d)) {
                $QuarkyCode = $row_d['QuarkyCode'];
                $MaximumStudents = $row_d['MaximumStudents'];
                $RegisteredStudents = $row_d['RegisteredStudents'];
                $LimitReached = $row_d['LimitReached'];
            }
        }
        $sql_d = "SELECT * FROM parents WHERE parent_id='" . $row_1["CustomerID"] . "'";
        $result_d = mysqli_query($conn, $sql_d);
        if (mysqli_num_rows($result_d) > 0) {
            while ($row_d = mysqli_fetch_assoc($result_d)) {
                $p_name = $row_d['p_name'];
                $p_last_name = $row_d['p_last_name'];
                $p_email = $row_d['p_email'];
                $p_phone = $row_d['p_phone'];

            }
        }
        $QuarkyInvoices=array();
        $sql_d = "SELECT * FROM QuarkyInvoices WHERE QuarkySoldID='" . $row_1["QuarkySoldID"] . "'";
        $result_d = mysqli_query($conn, $sql_d);
        if (mysqli_num_rows($result_d) > 0) {
            while ($row_d = mysqli_fetch_assoc($result_d)) {
                $phone;
                $amount  ;
                $CheckoutRequestID  ;
                $MerchantRequestID ;
                $ResultCode ;
                $ResultDesc ;
                $sql_d10 = "SELECT * FROM QuarkyMpesa WHERE QuarkyInvoicesID='" . $row_d["QuarkyInvoicesID"] . "'";
                $result_d10 = mysqli_query($conn, $sql_d10);
                if (mysqli_num_rows($result_d10) > 0) {
                    while ($row_d10 = mysqli_fetch_assoc($result_d10)) {
                        $phone = $row_d10['phone'];
                        $amount = $row_d10['amount'];
                        $CheckoutRequestID = $row_d10['CheckoutRequestID'];
                        $MerchantRequestID = $row_d10['MerchantRequestID'];
                        $ResultCode = $row_d10['ResultCode'];
                        $ResultDesc = $row_d10['ResultDesc'];
                        
                    }
                }
                $PaymentStatus = $row_d['Status'];
                $NoKits = $row_d['NoKits'];
                $AmountPaid = $row_d['Price'];
                $TotalAmountInvoices =$TotalAmountInvoices+ $row_d['Price'];
                $curl_post_data = array(
                    'phone' => $phone,
                    'amount' => $amount,
                    'CheckoutRequestID' => $CheckoutRequestID,
                    'MerchantRequestID' => $MerchantRequestID,
                    'ResultCode' => $ResultCode,
                    'ResultDesc' => $ResultDesc,
                    'QuarkyInvoicesID' => $row_d["QuarkyInvoicesID"],
                    'QuarkySoldID' => $row_d["QuarkySoldID"],
                    'NoKits' => $row_d["NoKits"],
                    'Price' => $row_d["Price"],
                    'invoice_no' => $row_d["invoice_no"],
                    'invoice_date' => $row_d["invoice_date"],
                    'invoice_due_date' => $row_d["invoice_due_date"],
                    'Status' => $row_d["Status"], 
                );
                array_push($QuarkyInvoices, $curl_post_data);

            }
        }
        $curl_post_data = array(
            'p_name' => $p_name,
            'p_last_name' => $p_last_name,
            'p_email' => $p_email,
            'p_phone' => $p_phone,
            'QuarkyCode' => $QuarkyCode,
            'MaximumStudents' => $MaximumStudents,
            'RegisteredStudents' => $RegisteredStudents,
            'LimitReached' => $LimitReached,
            'PaymentStatus' => $PaymentStatus,
            'NoKits' => $NoKits,
            'AmountPaid' => $AmountPaid,
            'TotalAmountInvoices' => $TotalAmountInvoices,
            'QuarkySoldID' => $row_1["QuarkySoldID"],
            'QuarkyKitID' => $row_1["QuarkyKitID"],
            'Status' =>  $row_1["Status"],
            'DefaultPrice' =>  $row_1["DefaultPrice"],
            'Shipping' =>  $row_1["Shipping"],
            'Totalprice' =>  $row_1["Totalprice"],
            'Customer' => $row_1["Customer"],
            'CustomerID' => $row_1["CustomerID"],
            'Delivery' => $row_1["Delivery"],
            'CustomerLocation' => $row_1["CustomerLocation"],
            'CustomerPhoneNumber' => $row_1["CustomerPhoneNumber"],
            'DeliveryMedium' => $row_1["DeliveryMedium"],
            'DeliveryPersonName' => $row_1["DeliveryPersonName"],
            'DeliveryPhoneNumber' => $row_1["DeliveryPhoneNumber"],
            'Delivered' => $row_1["Delivered"],
            'DeliveryTime' => $row_1["DeliveryTime"],
            'dated' => $row_1["dated"],
            'QuarkyInvoices' => $QuarkyInvoices,
        );
        array_push($data, $curl_post_data);
    }
}






echo json_encode($data);
?>