function bl_create_payment_order($order)
{
    global $wpdb;
    if (empty($order) || !is_array($order)) {
        return false;
    }
    if ($curr_record = bl_get_payment_order($order["bl_order_id"])) {
        return $wpdb->query("UPDATE " . BEPRO_LISTINGS_ORDERS_TABLE_NAME . " SET feature_id = " . $order["feature_id"] . ", cust_user_id = '" . $order["cust_user_id"] . "', bepro_cart_id = '" . $order["bepro_cart_id"] . "', status = '" . $order["status"] . "', feature_type = '" . $order["feature_type"] . "', expires = '" . @$order["expires"] . "', date_paid = '" . @$order["date_paid"] . "' WHERE bl_order_id = " . $order["bl_order_id"]);
    } else {
        return $wpdb->query("INSERT INTO " . BEPRO_LISTINGS_ORDERS_TABLE_NAME . " (bl_order_id, feature_id, cust_user_id, bepro_cart_id, status, feature_type, date_paid, expires) VALUES(" . $order["bl_order_id"] . "," . $order["feature_id"] . "," . $order["cust_user_id"] . ",'" . $order["bepro_cart_id"] . "','" . $order["status"] . "'," . $order["feature_type"] . ",'" . $order["date_paid"] . "','" . $order["expires"] . "')");
    }
}
function bl_order_general_meta($post)
{
    $order = bl_get_payment_order($post->ID);
    echo '<div id="bl_order_general_meta">
			' . (@$order->feature_type ? "<input type='hidden' name='feature_type' value='" . $order->feature_type . "'>" : "") . '
			' . (@$order->status ? "<input type='hidden' name='status' value='" . $order->status . "'>" : "") . '
			<span class="form_label">Cust User ID</span><input type="text" name="cust_user_id" value="' . @$order->cust_user_id . '" ' . (@$order->cust_user_id ? "readonly" : "") . '/> <br />

			<span class="form_label">' . __("Status", "bepro-listings") . '</span><select name="status" ' . (@$order->status ? "disabled='disabled'" : "") . '> 

				<option value="">' . __("Select", "bepro-listings") . '</option>

				<option value="1" ' . (@$order->status == 1 ? "selected='selected'" : "") . '>' . __("Paid", "bepro-listings") . '</option>

				<option value="2" ' . (@$order->status == 2 ? "selected='selected'" : "") . '>' . __("Pending", "bepro-listings") . '</option>

				<option value="3" ' . (@$order->status == 3 ? "selected='selected'" : "") . '>' . __("Failed", "bepro-listings") . '</option>

			</select><br />

			<span class="form_label">BePro Cart ID</span><input type="text" name="bepro_cart_id"  value="' . @$order->bepro_cart_id . '" ' . @$order->bepro_cart_id . '" ' . (@$order->bepro_cart_id ? "readonly" : "") . '/> <br />

			<span class="form_label">Feature Type</span><select name="feature_type" ' . (@$order->feature_type ? "disabled='disabled'" : "") . '> 

				<option value="">' . __("Select", "bepro-listings") . '</option>

				<option value="1" ' . (@$order->feature_type == 1 ? "selected='selected'" : "") . '>' . __("Category Listing", "bepro-listings") . '</option>

				<option value="2" ' . (@$order->feature_type == 2 ? "selected='selected'" : "") . '>' . __("Package", "bepro-listings") . '</option>

			</select><br />

			<span class="form_label">' . __("Feature ID", "bepro-listings") . '</span><input type="text" name="feature_id"  value="' . @$order->feature_id . '" ' . @$order->feature_id . '" ' . (@$order->feature_id ? "readonly" : "") . '/> <br />

			<span class="form_label">' . __("Date Paid", "bepro-listings") . '</span><input class="bl_date_input" type="text" name="date_paid"  value="' . (@$order->date_paid && @$order->date_paid != "0000-00-00 00:00:00" ? $order->date_paid : "") . '" /> 
			<input type="hidden" name="bpl_save_order" value="1">
		</div>

		';
}
function bl_form_package_field($bl_order_id = null, $return_this = false, $selected = false)
{
    $data = get_option("bepro_listings");
    $order = false;
    $return_text = "";
    $user_id = get_current_user_id();
    if (!$selected && is_numeric($_POST["bpl_package"])) {
        $selected = $_POST["bpl_package"];
    } else {
        if ($bl_order_id) {
            $order = bl_get_payment_order($bl_order_id);
            //allow user to change which package this listing is associated with but show its selected
            if (@is_numeric($order->feature_id)) {
                $selected = $order->feature_id;
            }
        }
    }
    $return_text .= '<div id="flat_fee">';
    if (@$data["require_payment"] && $data["require_payment"] == 2) {
        $packages = get_posts(array("post_type" => "bpl_packages"));
        if (!$packages || sizeof($packages) < 1) {
            return;
        }
        $return_text .= '<h3>' . __("Available Packages", "bepro-listings") . '</h3>';
        foreach ($packages as $package) {
            $num_listings = get_post_meta($package->ID, "num_package_listings", true);
            $duration = get_post_meta($package->ID, "package_duration", true);
            $cost = get_post_meta($package->ID, "package_cost", true);
            $vacant = bl_get_vacant_order_id($user_id, 2, $package->ID, false);
            $listings_left = "";
            if ($vacant) {
                global $wpdb;
                $exiting_listings = $wpdb->get_row("SELECT count(*) as num_listings FROM " . $wpdb->prefix . BEPRO_LISTINGS_TABLE_NAME . " as bl WHERE bl.bl_order_id =" . $vacant);
                $exiting_listings = !empty($exiting_listings) ? $exiting_listings->num_listings : "";
                $listings_left = is_numeric($exiting_listings) ? $num_listings - $exiting_listings : "";
                $listings_left = is_numeric($listings_left) && $listings_left > 0 ? "(" . $listings_left . " " . __("Remaining", "bepro-listings") . ")" : "";
            }
            //this is a package, we need to know how many listings are possible
            if (!$num_listings || !is_numeric($num_listings) || $num_listings < 1) {
                return;
            }
            $package_div[] = array();
            $return_text .= '<div class="package_option"><input type="radio" name="bpl_package" id="package_sel_' . $package->ID . '" value="' . $package->ID . '" ' . (@$order && $order->feature_id == $package->ID ? "checked='checked'" : "") . ' ' . (@$selected && $selected == $package->ID ? "checked='checked'" : "") . '> <span class="package_head">' . $package->post_title . ' ' . $listings_left . '</span>
				<span class="package_options">
					<ul>
						<li># ' . __("Days", "bepro-listings") . ' ' . $duration . '</li>
						<li># ' . __("Listings", "bepro-listings") . ' ' . $num_listings . '</li>
						<li>' . __("Cost", "bepro-listings") . ' ' . $data["currency_sign"] . $cost . '</li>
					</ul>
				<span>
				<span class="package_details">' . $package->post_content . '<span>
				</div>';
        }
    }
    $return_text .= '</div><div style="clear:both"></div>';
    if ($return_this) {
        return $return_text;
    }
    echo $return_text;
}