/**
Magic Shortcode
*/
function ym_buy_button_content($atts)
{
    if (!is_user_logged_in()) {
        return '';
    }
    global $ym_sys, $ym_res, $ym_user;
    $user_id = $ym_user->ID;
    $mode = isset($atts['post_id']) ? TRUE : FALSE;
    $post_id = isset($atts['post_id']) ? $atts['post_id'] : get_the_id();
    // check if purchased
    if (!ym_post_is_purchasable($post_id) || ym_has_purchased_post($post_id, $user_id) || ym_user_has_access($post_id)) {
        if ($mode) {
            return '<p><a href="' . get_permalink($post_id) . '">' . __('You have purchased: ', 'ym') . get_the_title($post_id) . '</a></p>';
        }
        return '';
    }
    // check purchase limit
    $purchase_limit = get_post_meta($post_id, '_ym_post_purchasable_limit', true);
    $left = ym_post_available_count($post_id);
    if ($purchase_limit && $left <= 0) {
        $r = '<div style="margin-bottom:5px;width:100%;">' . $ym_res->msg_header . $ym_res->purchasable_at_limit . $ym_res->msg_footer . '</div>';
        return $r;
    }
    $cost = get_post_meta($post_id, '_ym_post_purchasable_cost', true);
    if (!$cost) {
        // bundle only
        $r = '<div style="margin-bottom:5px;width:100%;">' . $ym_res->msg_header . $ym_res->purchasable_pack_only . $ym_res->msg_footer . '</div>';
        return $r;
    }
    $selected_gateways = isset($atts['gateways']) ? explode('|', $atts['gateways']) : array();
    $hidecoupon = isset($atts['hidecoupon']) ? $atts['hidecoupon'] : FALSE;
    // default to TRUE is post id passed no post id default is false
    $title = isset($atts['showtitle']) ? $atts['showtitle'] : ($mode ? TRUE : FALSE);
    $url = get_permalink($post_id);
    if (isset($_POST['ym_buy_button_content'])) {
        $r = '<p>' . sprintf(__('You are purchasing post: %s', 'ym'), get_the_title($post_id)) . '</p>
<form action="" method="post" class="ym_buy_button_post">
<input type="hidden" name="ym_buy_button_content" value="1" />
<input type="hidden" name="ym_buy_button_args" value=\'' . json_encode($atts) . '\' />
<table>
	<tr><td>' . __('Purchasing: ', 'ym') . ' ' . get_the_title() . '</td><td>' . ym_get_currency() . ' ' . number_format($cost, 2) . '</td></tr>
	';
        $show_coupon = $hidecoupon ? FALSE : TRUE;
        $final_price = FALSE;
        if (isset($_POST['ym_buy_button_content_coupon'])) {
            $coupon = $_POST['ym_buy_button_content_coupon'];
            // validate
            if (FALSE !== ($value = ym_validate_coupon($coupon, 2))) {
                $type = ym_get_coupon_type($value);
                $r .= '<tr><td>' . sprintf(__('Valid Coupon Supplied: %s', 'ym'), $coupon) . '</td><td>';
                if ($type == 'sub_pack') {
                    $r .= __('However it is invalid for a Post Purchase', 'ym');
                } else {
                    if ($type == 'percent') {
                        $r .= __('Value: ', 'ym') . $value;
                        $final_price = $cost - substr($value, 0, -1) / 100 * $cost;
                    } else {
                        $r .= __('Fixed Price: ', 'ym') . $value;
                        $final_price = $cost - $value;
                    }
                }
                if (FALSE !== $final_price) {
                    $show_coupon = FALSE;
                    $r .= '</td></tr><tr><td>' . __('Cost after Coupon', 'ym') . '</td><td>' . number_format($final_price, 2);
                    ym_register_coupon_use($coupon, $user_id, 'buy_post_' . $post_id . '_' . $user_id);
                }
                $r .= '</td></tr>';
            } else {
                $r .= '<tr><td></td><td>' . sprintf(__('Invalid Coupon Supplied: %s', 'ym'), $coupon) . '</td></tr>';
            }
        }
        if ($show_coupon) {
            $r .= '<tr><td>' . __('Apply Coupon', 'ym') . '</td><td><input type="text" name="ym_buy_button_content_coupon" value="" /><input type="submit" name="ym_buy_button_content_apply_coupon" value="' . __('Apply Coupon', 'ym') . '" /></td></tr>';
        }
        if (FALSE === $final_price) {
            $final_price = $cost;
        }
        if ($final_price < 0) {
            $final_price = 0;
        }
        $r .= '
	<tr><td>' . __('Tax', 'ym') . '</td><td>';
        $vat = FALSE;
        if ($ym_sys->global_vat_applicable) {
            if ($ym_sys->vat_rate) {
                $vat = $ym_sys->vat_rate;
            }
        }
        if ($vat_rate = apply_filters('ym_vat_override', false, $user_id)) {
            $vat = $vat_rate;
        }
        $cost_with_vat = $final_price;
        if ($vat) {
            $r .= $vat . '%';
            $cost_with_vat = $vat / 100 * $final_price + $final_price;
        } else {
            $r .= __('None', 'ym');
            $cost_with_vat = $final_price;
        }
        $cost_with_vat = number_format($cost_with_vat, 2);
        $r .= '</td></tr>
		<tr><td>' . __('Total Cost', 'ym') . '</td><td>' . ym_get_currency() . ' ' . $cost_with_vat . '</td></tr>
</table>
</form>
';
        $test = $cost_with_vat * 100;
        if ($test == 0) {
            // free
            require_once YM_MODULES_DIR . 'ym_free.php';
            $obj = new ym_free();
            $r .= $obj->free_purchase('bundle', $bundle_id, $user_id);
            return $r;
        }
        //format
        $final_price = number_format($final_price, 2);
        // gateways
        global $ym_active_modules;
        if (sizeof($selected_gateways)) {
            $gateways = $selected_gateways;
        } else {
            $gateways = $ym_active_modules;
        }
        foreach ($gateways as $gateway) {
            if (in_array($gateway, $ym_active_modules)) {
                $obj = new $gateway();
                if (method_exists($obj, 'pay_per_post')) {
                    $r .= $obj->gen_buy_now_button($final_price, get_the_title($post_id), TRUE, $post_id);
                }
            }
        }
        return $r;
    }
    $r = '
<form action="' . $url . '" method="post" class="ym_buy_button_post_buynow">
	<fieldset>
	';
    $r .= '<p>';
    if ($title) {
        $r .= __('Purchase:', 'ym') . ' ' . get_the_title($post_id) . ' - ';
    }
    // show cost
    $r .= __('Cost:', 'ym') . ' ' . ym_get_currency() . ' ' . number_format($cost, 2);
    $r .= '</p>';
    $r .= '
		<input type="hidden" name="ym_buy_button_args" value=\'' . json_encode($atts) . '\' />
		<input type="submit" name="ym_buy_button_content" value="' . __('Buy Now', 'ym') . '" />
	</fieldset>
</form>
';
    return $r;
}
/**
Start Shortcode
*/
function ym_buy_button_bundle($atts)
{
    if (!is_user_logged_in()) {
        return '';
    }
    global $ym_sys, $ym_res, $ym_user;
    $user_id = $ym_user->ID;
    $bundle_id = isset($atts['bundle_id']) ? $atts['bundle_id'] : '';
    if (!$bundle_id) {
        return '';
    }
    // check if bundle exists
    $bundle = ym_get_bundle($bundle_id);
    if (!$bundle) {
        return '';
    }
    // check if bundle has content??
    // no allow a empty bundle
    $selected_gateways = isset($atts['gateways']) ? explode('|', $atts['gateways']) : array();
    $hidecoupon = isset($atts['hidecoupon']) ? $atts['hidecoupon'] : FALSE;
    $list_contents = isset($atts['list_contents']) ? $atts['list_contents'] : FALSE;
    $hide_purchased = isset($atts['hide_purchased']) ? $atts['hide_purchased'] : TRUE;
    $redirect_post = isset($atts['redirect']) ? $atts['redirect'] : FALSE;
    // check if purcahsed
    if (ym_has_purchased_bundle($bundle_id, $user_id)) {
        if ($hide_purchased) {
            return '';
        } else {
            $r = '<p>' . __('You have purchased: ', 'ym') . $bundle->name . '</p>';
            $r .= 'list contents is ' . $list_contents;
            if ($list_contents) {
                // show post titles of contents
                $posts = ym_get_bundle_posts($bundle_id);
                $r .= '<ul>';
                foreach ($posts as $post) {
                    $r .= '<li>';
                    $r .= '<a href="' . get_permalink($post->post_id) . '">';
                    $r .= get_the_title($post->post_id);
                    $r .= '</a>';
                    $r .= '</li>';
                }
                $r .= '</ul>';
            }
            return $r;
        }
    }
    // check if stock left
    $limit = $bundle->purchaselimit;
    $left = ym_bundle_available_count($bundle_id);
    if ($limit && $left <= 0) {
        $r = '<div style="margin-bottom:5px;width:100%;">' . $ym_res->msg_header . $ym_res->purchasable_bundle_at_limit . $ym_res->msg_footer . '</div>';
        return $r;
    }
    $cost = $bundle->cost;
    //	$cost = number_format($cost, 2);
    // get here
    $url = get_permalink();
    if (isset($_POST['ym_buy_button_bundle'])) {
        $r = '<p>' . sprintf(__('You are purchasing bundle: %s', 'ym'), $bundle->name) . '</p>
<form action="" method="post" class="ym_buy_button_bundle">
	<input type="hidden" name="ym_buy_button_bundle" value="1" />
	<input type="hidden" name="ym_buy_button_args" value=\'' . json_encode($atts) . '\' />
	<table>
		<tr><td>' . __('Purchasing: ', 'ym') . ' ' . $bundle->name . '</td><td>' . ym_get_currency() . ' ' . number_format($cost, 2) . '</td></tr>
		';
        $show_coupon = $hidecoupon ? FALSE : TRUE;
        $final_price = FALSE;
        if (isset($_POST['ym_buy_button_bundle_coupon'])) {
            $coupon = $_POST['ym_buy_button_bundle_coupon'];
            // validate
            if (FALSE !== ($value = ym_validate_coupon($coupon, 3))) {
                $type = ym_get_coupon_type($value);
                $r .= '<tr><td>' . sprintf(__('Valid Coupon Supplied: %s', 'ym'), $coupon) . '</td><td>';
                if ($type == 'sub_pack') {
                    $r .= __('However it is invalid for a Bundle Purchase', 'ym');
                } else {
                    if ($type == 'percent') {
                        $r .= __('Value: ', 'ym') . $value;
                        $final_price = $cost - substr($value, 0, -1) / 100 * $cost;
                    } else {
                        $r .= __('Fixed Price: ', 'ym') . $value;
                        $final_price = $cost - $value;
                    }
                }
                if (FALSE !== $final_price) {
                    $show_coupon = FALSE;
                    $r .= '</td></tr><tr><td>' . __('Cost after Coupon', 'ym') . '</td><td>' . number_format($final_price, 2);
                    ym_register_coupon_use($coupon, $user_id, 'buy_bundle_' . $bundle_id . '_' . $user_id);
                }
                $r .= '</td></tr>';
            } else {
                $r .= '<tr><td></td><td>' . sprintf(__('Invalid Coupon Supplied: %s', 'ym'), $coupon) . '</td></tr>';
            }
        }
        if ($show_coupon) {
            $r .= '<tr><td>' . __('Apply Coupon', 'ym') . '</td><td><input type="text" name="ym_buy_button_bundle_coupon" value="" /><input type="submit" name="ym_buy_button_bundle_apply_coupon" value="' . __('Apply Coupon', 'ym') . '" /></td></tr>';
        }
        if (FALSE === $final_price) {
            $final_price = $cost;
        }
        //Add some logic check in case the discount reduces below 0
        if ($final_price < 0) {
            $final_price = 0;
        }
        $r .= '
	<tr><td>' . __('Tax', 'ym') . '</td><td>';
        $vat = FALSE;
        if ($ym_sys->global_vat_applicable) {
            if ($ym_sys->vat_rate) {
                $vat = $ym_sys->vat_rate;
            }
        }
        if ($vat_rate = apply_filters('ym_vat_override', false, $user_id)) {
            $vat = $vat_rate;
        }
        $cost_with_vat = $final_price;
        if ($vat) {
            $r .= $vat . '%';
            $cost_with_vat = $vat / 100 * $final_price + $final_price;
        } else {
            $r .= __('None', 'ym');
            $cost_with_vat = $final_price;
        }
        $cost_with_vat = number_format($cost_with_vat, 2);
        $r .= '</td></tr>
		<tr><td>' . __('Total Cost', 'ym') . '</td><td>' . ym_get_currency() . ' ' . $cost_with_vat . '</td></tr>
	</table>
</form>';
        $test = $cost_with_vat * 100;
        if ($test == 0) {
            // free
            require_once YM_MODULES_DIR . 'ym_free.php';
            $obj = new ym_free();
            $r .= $obj->free_purchase('bundle', $bundle_id, $user_id);
            return $r;
        }
        //format
        $final_price = number_format($final_price, 2);
        // gateways
        global $ym_active_modules;
        if (sizeof($selected_gateways)) {
            $gateways = $selected_gateways;
        } else {
            $gateways = $ym_active_modules;
        }
        foreach ($gateways as $gateway) {
            if (in_array($gateway, $ym_active_modules)) {
                $obj = new $gateway();
                if (method_exists($obj, 'pay_per_post_bundle')) {
                    $r .= $obj->gen_buy_ppp_pack_button($final_price, $bundle_id, $bundle->name, TRUE);
                }
            }
        }
        return $r;
    }
    $r = '
<form action="' . $url . '" method="post" class="ym_buy_button_bundle_buynow">
	<fieldset>
		<p>' . __('Purchase Bundle:', 'ym') . ' ' . $bundle->name . ' - ' . __('Cost:', 'ym') . ' ' . ym_get_currency() . ' ' . number_format($cost, 2) . '</p>
		';
    if ($list_contents) {
        // show post titles of contents
        $posts = ym_get_bundle_posts($bundle->id);
        $r .= '<ul>';
        foreach ($posts as $post) {
            $r .= '<li>' . get_the_title($post->post_id) . '</li>';
        }
        $r .= '</ul>';
    }
    $r .= '
		<input type="hidden" name="ym_buy_button_args" value=\'' . json_encode($atts) . '\' />
		<input type="submit" name="ym_buy_button_bundle" value="' . __('Buy Now', 'ym') . '" />
	</fieldset>
</form>
';
    return $r;
}