function ym_firesale_ppp_packs()
{
    global $wpdb;
    // do all packs!
    foreach (ym_get_bundles() as $pack) {
        $pack_id = $pack->id;
        // get base data from the table
        $sql = 'SELECT original_cost FROM ' . $wpdb->ym_app_ppp_pack . '
			WHERE pack_id = ' . $pack_id;
        $fire_original_cost = $wpdb->get_var($sql);
        $base_cost = number_format($pack->cost, 2);
        $fire_original_cost = $fire_original_cost ? $fire_original_cost : 0;
        $is_currently_firesale = $fire_original_cost ? TRUE : FALSE;
        if ($ym_firesale_id = ym_firesale_exists($pack_id, YM_APP_TYPE_PACK)) {
            // a fire sale exists
            $tier_data = ym_firesale_get_current_tier($ym_firesale_id);
            $tier_data = $tier_data[0];
            if (!$fire_original_cost) {
                // starting a firesale
                $sql = 'INSERT INTO ' . $wpdb->ym_app_ppp_pack . '(pack_id, original_cost) VALUES (' . $pack_id . ', ' . $fire_original_cost . ')';
                $wpdb->query($sql);
                ym_firesale_log(array('doing' => 'StartingPricingModel', 'packId' => $pack_id, 'tierId' => $tier_data->fire_tier_id, 'tierPacket' => $tier_data));
                ym_fire_sale_start($tier_data->fire_id);
            }
            // cost update
            if ($base_price != $tier_data->fire_price) {
                if ($tier_data) {
                    // packs stores in pence/cents base unit
                    $pack->cost = str_replace('.', '', $tier_data->fire_price);
                    ym_firesale_log(array('doing' => 'TierChange', 'packId' => $pack_id, 'newPrice' => $tier_data->fire_price, 'currentPrice' => $base_price, 'tierId' => $tier_data->fire_tier_id, 'tierPacket' => $tier_data));
                    ym_firesale_tier_log($tier_data->fire_tier_id);
                    ym_fire_tier_start($tier_data->fire_tier_id);
                } else {
                    // no tier
                    $pack->cost = str_replace('.', '', $fire_original_cost);
                    $sql = 'DELETE FROM ' . $wpdb->ym_app_ppp_pack . ' WHERE pack_id = ' . $pack_id;
                    $wpdb->query($sql);
                    ym_firesale_log(array('doing' => 'EndPricingModel', 'packId' => $pack_id, 'newPrice' => $fire_original_cost, 'currentPrice' => $base_price, 'tierId' => $tier_data->fire_tier_id, 'tierPacket' => $tier_data));
                }
            }
        } else {
            if ($is_currently_firesale == 1) {
                // firesale needs resetting
                $pack->cost = $fire_original_cost;
                ym_firesale_log(array('doing' => 'EndPricingModel', 'packId' => $pack_id, 'newPrice' => $fire_original_cost, 'currentPrice' => $base_price));
            }
        }
        // update the ppp pack
        //		$all_packs[$key] = $pack;
        $pack->cost = number_format($pack->cost, 2, '.', '') * 100;
        //store in pence
        $sql = 'UPDATE ' . $wpdb->prefix . 'ym_post_pack SET cost = ' . $pack->cost . ' WHERE id = ' . $pack->id;
        $wpdb->query($sql);
    }
}
function ym_firesale_subs()
{
    // get all subs
    $packs = ym_get_packs();
    foreach ($packs as $key => $pack) {
        $pack_id = $pack['id'];
        $base_price = $pack['cost'];
        $fire_original_cost = isset($pack['pre_firesale_cost']) ? $pack['pre_firesale_cost'] : FALSE;
        $is_currently_firesale = isset($pack['on_fire_sale']) ? $pack['on_fire_sale'] : FALSE;
        if ($ym_firesale_id = ym_firesale_exists($pack_id, YM_APP_TYPE_SUB)) {
            // a fire sale exists for this pack
            $tier_data = ym_firesale_get_current_tier($ym_firesale_id);
            $tier_data = $tier_data[0];
            if (!$fire_original_cost) {
                // starting a fire sale
                $pack['pre_firesale_cost'] = $base_price;
                ym_firesale_log(array('doing' => 'StartingPricingModel', 'packId' => $pack_id, 'tierId' => $tier_data->fire_tier_id, 'tierPacket' => $tier_data));
                ym_fire_sale_start($tier_data->fire_id);
            }
            // check for tier change
            if ($base_price != $tier_data->fire_price) {
                if ($tier_data) {
                    $pack['cost'] = $tier_data->fire_price;
                    $pack['on_fire_sale'] = TRUE;
                    ym_firesale_log(array('doing' => 'TierChange', 'packId' => $pack_id, 'newPrice' => $tier_data->fire_price, 'currentPrice' => $base_price, 'tierId' => $tier_data->fire_tier_id, 'tierPacket' => $tier_data));
                    ym_firesale_tier_log($tier_data->fire_tier_id);
                    ym_fire_tier_start($tier_data->fire_tier_id);
                } else {
                    // no tier
                    $pack['cost'] = $fire_original_cost;
                    unset($pack['pre_firesale_cost']);
                    unset($pack['on_fire_sale']);
                    ym_firesale_log(array('doing' => 'EndPricingModel', 'packId' => $pack_id, 'newPrice' => $fire_original_cost, 'currentPrice' => $base_price, 'tierId' => $tier_data->fire_tier_id, 'tierPacket' => $tier_data));
                }
            }
        } else {
            if ($is_currently_firesale == 1) {
                // firesale needs resetting as it has ended
                $pack['cost'] = $fire_original_cost;
                unset($pack['pre_firesale_cost']);
                unset($pack['on_fire_sale']);
                ym_firesale_log(array('doing' => 'EndPricingModel', 'packId' => $pack_id, 'newPrice' => $fire_original_cost, 'currentPrice' => $base_price));
            }
        }
        // store the pack
        $packs[$key] = $pack;
    }
    // store the packs
    $obj_packs = get_option('ym_packs');
    $obj_packs->packs = $packs;
    update_option('ym_packs', $obj_packs);
}
function ym_firesale_ppp($button_code, $code, $post_id)
{
    if (ym_post_is_purchasable()) {
        // post cane be bought, continue
        $post_id = get_the_ID();
        // current post data
        $base_price = get_post_meta($post_id, '_ym_post_purchasable_cost', TRUE);
        $fire_original_cost = get_post_meta($post_id, '_ym_fire_original_cost', TRUE);
        $is_currently_firesale = get_post_meta($post_id, '_ym_fire_active', TRUE);
        if ($ym_firesale_id = ym_firesale_exists($post_id, YM_APP_TYPE_POST)) {
            // a fire sale exists for this ppp
            $tier_data = ym_firesale_get_current_tier($ym_firesale_id);
            $tier_data = $tier_data[0];
            // check the post meta is correct
            if (!get_post_meta($post_id, '_ym_fire_original_cost')) {
                // no base cost
                // post is starting to go on firesale
                // store the original cost
                update_post_meta($post_id, '_ym_fire_original_cost', $base_price);
                ym_firesale_log(array('doing' => 'StartingPricingModel', 'postId' => $post_id, 'tierId' => $tier_data->fire_tier_id, 'tierPacket' => $tier_data));
                ym_fire_sale_start($tier_data->fire_id);
            }
            // check cost update if needed
            if ($base_price != $tier_data->fire_price) {
                if ($tier_data) {
                    update_post_meta($post_id, '_ym_post_purchasable_cost', $tier_data->fire_price);
                    update_post_meta($post_id, '_ym_fire_active', TRUE);
                    ym_firesale_log(array('doing' => 'TierChange', 'postId' => $post_id, 'newPrice' => $tier_data->fire_price, 'currentPrice' => $base_price, 'tierId' => $tier_data->fire_tier_id, 'tierPacket' => $tier_data));
                    ym_firesale_tier_log($tier_data->fire_tier_id);
                    ym_fire_tier_start($tier_data->fire_tier_id);
                } else {
                    // no tier
                    update_post_meta($post_id, '_ym_post_purchasable_cost', $fire_original_cost);
                    delete_post_meta($post_id, '_ym_fire_original_cost');
                    update_post_meta($post_id, '_ym_fire_active', false);
                    ym_firesale_log(array('doing' => 'EndPricingModel', 'postId' => $post_id, 'newPrice' => $fire_original_cost, 'currentPrice' => $base_price, 'tierId' => $tier_data->fire_tier_id, 'tierPacket' => $tier_data));
                }
            }
        } else {
            if ($is_currently_firesale == 1) {
                // firesale needs resetting as it has ended
                update_post_meta($post_id, '_ym_post_purchasable_cost', $fire_original_cost);
                delete_post_meta($post_id, '_ym_fire_original_cost');
                update_post_meta($post_id, '_ym_fire_active', false);
                ym_firesale_log(array('doing' => 'EndPricingModel', 'postId' => $post_id, 'newPrice' => $fire_original_cost, 'currentPrice' => $base_price));
            }
        }
    }
    return $button_code;
}