function mcs_uniqid($hash, $i = 1)
{
    global $wpdb;
    $sql = "SELECT hash FROM " . my_calendar_payments_table() . " WHERE hash='{$hash}'";
    if ($wpdb->get_var($sql)) {
        $hash = uniqid('E' . $i);
        mcs_uniqid($hash, $i++);
    } else {
        return $hash;
    }
}
function mcs_key_status($key)
{
    if (!$key) {
        return false;
    }
    global $wpdb;
    $sql = "SELECT quantity,status FROM " . my_calendar_payments_table() . " WHERE hash = '{$key}'";
    $key = $wpdb->get_row($sql);
    if (!$key) {
        $return = __('That payment key does not exist', 'my-calendar-submissions');
    } else {
        if ($key->quantity == 0) {
            $return = __('Your payment key has been used up!', 'my-calendar-submissions');
        } else {
            if ($key->status != 'Completed') {
                $return = sprintf(__('Your payment key status is "%s".', 'my-calendar-submissions'), $key->status);
            }
        }
    }
    return $return;
}
function mcs_update_database()
{
    $payments = "CREATE TABLE " . my_calendar_payments_table() . "  (\n\t\t\t`id` BIGINT( 20 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,\n\t\t\t`item_number` BIGINT( 20 ) NOT NULL ,\n\t\t\t`event_id` BIGINT( 20 ) NOT NULL ,\t\t\t\n\t\t\t`quantity` BIGINT( 20 ) NOT NULL ,\t\t\t\n\t\t\t`total` BIGINT( 20 ) NOT NULL ,\t\t\t\n\t\t\t`hash` VARCHAR( 255 ) NOT NULL ,\n\t\t\t`txn_id` VARCHAR( 255 ) NOT NULL ,\n\t\t\t`price` FLOAT( 10, 2 ) NOT NULL ,\n\t\t\t`fee` FLOAT( 10, 2 ) NOT NULL ,\n\t\t\t`status` VARCHAR( 255 ) NOT NULL ,\n\t\t\t`transaction_date` DATETIME NOT NULL ,\n\t\t\t`first_name` VARCHAR( 255 ) NOT NULL ,\n\t\t\t`last_name` VARCHAR( 255 ) NOT NULL ,\n\t\t\t`payer_email` VARCHAR( 255 ) NOT NULL\n\t\t\t) CHARACTER SET utf8 COLLATE utf8_unicode_ci;";
    require_once ABSPATH . 'wp-admin/includes/upgrade.php';
    dbDelta($payments);
}
function mcs_add_payment($post)
{
    global $wpdb;
    if (isset($post['mc-submit-payments'])) {
        $nonce = $_POST['_wpnonce'];
        if (!wp_verify_nonce($nonce, 'my-calendar-payments')) {
            return;
        }
        $quantity = (int) $post['quantity'];
        // admin email after submission
        $price = sprintf("%01.2f", $post['price']);
        // submitter email after submission
        $first_name = $post['first_name'];
        // subject line
        $last_name = $post['last_name'];
        $email = is_email($post['email']);
        $transaction_date = date('Y-m-d h:m:s', strtotime($post['transaction_date']));
        $uniqid = uniqid('E');
        $hash = mcs_uniqid($uniqid);
        $add = array('item_number' => 1, 'quantity' => $quantity, 'total' => $quantity, 'hash' => $hash, 'txn_id' => 'Manual Entry', 'price' => $price, 'fee' => '0.00', 'status' => 'Completed', 'transaction_date' => $transaction_date, 'first_name' => $first_name, 'last_name' => $last_name, 'payer_email' => $email);
        $formats = array('%d', '%d', '%d', '%s', '%s', '%f', '%f', '%s', '%s', '%s', '%s', '%s');
        $insert = $wpdb->insert(my_calendar_payments_table(), $add, $formats);
        if ($insert) {
            $notifications = mcs_send_notifications($first_name, $last_name, $email, $price, $hash, $quantity);
            return "<div class=\"updated\"><p><strong>" . __('New Payment Added', 'my-calendar-submissions') . "</strong></p></div>";
        } else {
            return "<div class=\"updated error\"><p><strong>" . __('New Payment was not added.', 'my-calendar-submissions') . "</strong></p></div>";
        }
    }
    return false;
}