/**
 * wpsc_decrement_claimed_stock method
 *
 * @param float a price
 * @return string a price with a currency sign
 */
function wpsc_decrement_claimed_stock($purchase_log_id)
{
    // Processed
    $claimed_query = new WPSC_Claimed_Stock(array('cart_id' => $purchase_log_id));
    $all_claimed_stock = $claimed_query->get_purchase_log_claimed_stock();
    do_action('wpsc_pre_decrement_claimed_stock', $purchase_log_id, $claimed_query);
    if (!empty($all_claimed_stock)) {
        do_action('wpsc_decrement_claimed_stock_' . $all_claimed_stock[0]->processed, $purchase_log_id, $claimed_query);
        do_action('wpsc_decrement_claimed_stock', $purchase_log_id, $claimed_query);
        switch ($all_claimed_stock[0]->processed) {
            case 3:
            case 4:
            case 5:
                foreach ((array) $all_claimed_stock as $claimed_stock) {
                    $product = get_post($claimed_stock->product_id);
                    $current_stock = get_post_meta($product->ID, '_wpsc_stock', true);
                    $remaining_stock = $current_stock - $claimed_stock->stock_claimed;
                    update_product_meta($product->ID, 'stock', $remaining_stock);
                    $product_meta = get_product_meta($product->ID, 'product_metadata', true);
                    if ($remaining_stock < 1) {
                        // this is to make sure after upgrading to 3.8.9, products will have
                        // "notify_when_none_left" enabled by default if "unpublish_when_none_left"
                        // is enabled.
                        if (!isset($product_meta['notify_when_none_left'])) {
                            $product_meta['unpublish_when_none_left'] = 0;
                            if (!empty($product_meta['unpublish_when_none_left'])) {
                                $product_meta['unpublish_when_none_left'] = 1;
                                update_product_meta($product->ID, 'product_metadata', $product_meta);
                            }
                        }
                        $email_message = sprintf(__('The product "%s" is out of stock.', 'wp-e-commerce'), $product->post_title);
                        if (!empty($product_meta["unpublish_when_none_left"])) {
                            $result = wp_update_post(array('ID' => $product->ID, 'post_status' => 'draft'));
                            if ($result) {
                                $email_message = sprintf(__('The product "%s" is out of stock and has been unpublished.', 'wp-e-commerce'), $product->post_title);
                            }
                        }
                        if ($product_meta["notify_when_none_left"] == 1) {
                            wp_mail(get_option('purch_log_email'), sprintf(__('%s is out of stock', 'wp-e-commerce'), $product->post_title), $email_message);
                        }
                    }
                }
            case 6:
                $claimed_query = new WPSC_Claimed_Stock(array('cart_id' => $purchase_log_id));
                $claimed_query->clear_claimed_stock(0);
                break;
        }
    }
}