<?php
/**
 * Created by PhpStorm.
 * User: sunusi
 * Date: 2019-01-24
 * Time: 19:40
 */

session_start();

require_once getcwd() . "/../functions/db.class.php";
require_once getcwd() . "/../functions/form.data.php";
require_once getcwd() . "/../functions/robot.php";


if (isset($_POST["patientId"])) {

    $query = new db();
    /*
     * adding other information to the array
     */
    $_POST["userId"] = $_SESSION["YUZA_ID"];
    $_POST["receiptNo"] = genReceiptNo();

    $SQLPayments = SQLStatement("udut_payments", $_POST);
    $PatientReferrals = move_files($_FILES, "patientReferralLetter", "../referral/");

    /*
     * Saving the payment details
     */
    if (!$query->execute($SQLPayments)) {
        echo "Problem saving payment details <br>";
    }

    $payment = $query->getOne("SELECT * FROM udut_payments ORDER BY paymentId DESC LIMIT 1");
    $paymentId = $payment['paymentId'];

    $visit = array("paymentId" => $paymentId, "patientId" => $_POST["patientId"]);
    $SQLVisits = SQLStatement("udut_visits", $visit);

    /*
    * Saving the visit details
    */
    if (!$query->execute($SQLVisits)) {
        echo "Problem saving visit details <br>";
    }


    header("location: ../about-patient/" .$_POST["patientId"]);

} else {
    header("location: ../add-service/".$_POST["patientId"]);
}