<?php

/*
 * @copyright   Copyright (C) 2015 Gamuza Technologies (http://www.gamuza.com.br/)
 * @author     Eneias Ramos de Melo <*****@*****.**>
 */
$installer = new Mage_Catalog_Model_Resource_Setup('pagarme_setup');
$installer->startSetup();
if (!$installer->getAttribute(Mage_Catalog_Model_Product::ENTITY, 'pagarme_subscription_plan', 'attribute_id')) {
    $installer->addAttribute(Mage_Catalog_Model_Product::ENTITY, 'pagarme_subscription_plan', array('type' => 'int', 'input' => 'select', 'backend' => '', 'frontend' => '', 'label' => 'Plano da Pagarme', 'class' => '', 'source' => 'pagarme/product_attribute_plan', 'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL, 'visible' => true, 'required' => false, 'user_defined' => false, 'default' => '', 'searchable' => false, 'filterable' => false, 'comparable' => false, 'visible_on_front' => false, 'unique' => false, 'apply_to' => '', 'is_configurable' => false, 'used_in_product_listing' => true, 'option' => array('values' => array())));
    $attributeId = $installer->getAttributeId(Mage_Catalog_Model_Product::ENTITY, 'pagarme_subscription_plan');
    $defaultSetId = $installer->getAttributeSetId(Mage_Catalog_Model_Product::ENTITY, 'default');
    $installer->addAttributeGroup(Mage_Catalog_Model_Product::ENTITY, $defaultSetId, 'Pagarme');
    //find out the id of the new group
    $groupId = $installer->getAttributeGroup(Mage_Catalog_Model_Product::ENTITY, $defaultSetId, 'Pagarme', 'attribute_group_id');
    //assign the attribute to the group and set
    if ($attributeId > 0) {
        $installer->addAttributeToSet(Mage_Catalog_Model_Product::ENTITY, $defaultSetId, $groupId, $attributeId);
    }
    $attributes = array('price', 'special_price', 'special_from_date', 'special_to_date', 'minimal_price', 'cost', 'tier_price', 'weight', 'tax_class_id');
    foreach ($attributes as $attributeCode) {
        $applyTo = explode(',', $installer->getAttribute(Mage_Catalog_Model_Product::ENTITY, $attributeCode, 'apply_to'));
        if (!in_array('subscription', $applyTo)) {
            $applyTo[] = 'subscription';
            $installer->updateAttribute(Mage_Catalog_Model_Product::ENTITY, $attributeCode, 'apply_to', join(',', $applyTo));
        }
    }
}
function addSubscriptionPlans($installer)
{
    $table = $installer->getTable('pagarme_subscription_plans');