You are here

commerce_eu_vat.module in Commerce European Union VAT 7

Same filename and directory in other branches
  1. 7.2 commerce_eu_vat.module

Code for the Commerce EU VAT.

File

commerce_eu_vat.module
View source
<?php

/**
 * @file
 * Code for the Commerce EU VAT.
 */

/**
 * Impliments hook_views_api()
 */
function commerce_eu_vat_views_api() {
  return array(
    'api' => 3,
    'template path' => drupal_get_path('module', 'commerce_eu_vat') . '/themes',
  );
}

/**
 * Impliments hook_commerce_tax_type_info()
 */
function commerce_eu_vat_commerce_tax_type_info() {
  $tax_types = array();
  $tax_types['eu_vat'] = array(
    'title' => t('EU VAT'),
    'display_title' => t('VAT'),
    'description' => t('European Union Value Added Tax'),
    'display_inclusive' => TRUE,
    'round_mode' => COMMERCE_ROUND_HALF_DOWN,
    'admin_list' => TRUE,
  );
  return $tax_types;
}

/**
 * Configure the product types defined by enabled modules.
 */
function commerce_eu_vat_configure_product_types() {
  foreach (commerce_product_types() as $type => $product_type) {
    commerce_eu_vat_configure_product_type($type);
  }
}

/**
 * Implements hook_modules_enabled().
 */
function commerce_eu_vat_modules_enabled($modules) {
  commerce_eu_vat_configure_product_fields($modules);
}

/**
 * Impliments hook_commerce_product_type_insert()
 */
function commerce_eu_vat_commerce_product_type_insert($product_type, $skip_reset) {
  commerce_eu_vat_configure_product_type($product_type['type']);
}

/**
 * Configures the fields on product types provided by other modules.
 *
 * @param $modules
 *   An array of module names whose product type fields should be configured;
 *   if left NULL, will default to all modules that implement
 *   hook_commerce_product_type_info().
 */
function commerce_eu_vat_configure_product_fields($modules = NULL) {

  // If no modules array is passed, recheck the fields for all product types
  // defined by enabled modules.
  if (empty($modules)) {
    $modules = module_implements('commerce_product_type_info');
  }

  // Loop through all the enabled modules.
  foreach ($modules as $module) {

    // If the module implements hook_commerce_product_type_info()...
    if (module_hook($module, 'commerce_product_type_info')) {
      $product_types = module_invoke($module, 'commerce_product_type_info');

      // Loop through and configure the product types defined by the module.
      foreach ($product_types as $type => $product_type) {
        commerce_eu_vat_configure_product_type($type);
      }
    }
  }
}

/**
 * Ensures a EU VAT rate field is present on a product type bundle.
 */
function commerce_eu_vat_configure_product_type($type) {
  commerce_eu_vat_create_instance('commerce_eu_vat_rate', 'commerce_product', $type, t('EU VAT Rate'), 0, 'calculated_sell_price');
}
function commerce_eu_vat_create_instance($field_name, $entity_type, $bundle, $label, $weight = 0, $calculation = FALSE, $display = array()) {

  // If a field type we know should exist isn't found, clear the Field cache.
  if (!field_info_field_types('commerce_tax_rate_reference')) {
    field_cache_clear();
  }

  // Look for or add the specified price field to the requested entity bundle.
  $field = field_info_field($field_name);
  $instance = field_info_instance($entity_type, $field_name, $bundle);
  if (empty($field)) {
    $field = array(
      'field_name' => $field_name,
      'type' => 'commerce_tax_rate_reference',
      'cardinality' => -1,
      'entity_types' => array(
        $entity_type,
      ),
      'translatable' => FALSE,
      'locked' => TRUE,
      'settings' => array(
        'tax_rate_types' => array(
          'eu_vat' => 'eu_vat',
        ),
      ),
    );
    $field = field_create_field($field);
  }
  if (empty($instance)) {
    $instance = array(
      'field_name' => $field_name,
      'entity_type' => $entity_type,
      'bundle' => $bundle,
      'label' => $label,
      'required' => FALSE,
      // Because this widget is locked, we need it to use the full price widget
      // since the currency option can't be adjusted at the moment.
      'widget' => array(
        'type' => 'options_select',
        'weight' => $weight,
        'settings' => array(),
      ),
      'display' => array(),
    );

    //     $entity_info = entity_get_info($entity_type);
    //     // Spoof the default view mode and node teaser so its display type is set.
    //     $entity_info['view modes'] += array(
    //       'default' => array(),
    //       'node_teaser' => array(),
    //     );
    //     foreach ($entity_info['view modes'] as $view_mode => $data) {
    //       $instance['display'][$view_mode] = $display + array(
    //         'label' => 'hidden',
    //         'type' => 'commerce_price_formatted_amount',
    //         'settings' => array(
    //           'calculation' => $calculation,
    //         ),
    //         'weight' => $weight,
    //       );
    //     }
    field_create_instance($instance);
  }
}
function _commerce_eu_vat_eu_countries() {

  // Necessary for country_get_list().
  require_once DRUPAL_ROOT . '/includes/locale.inc';
  $countries = country_get_list();

  //ISO 3166, see http://de.wikipedia.org/wiki/Umsatzsteuer-Identifikationsnummer
  $eu_country_codes = array(
    "AT",
    "BE",
    "BG",
    "CY",
    "CZ",
    "DE",
    "DK",
    "EE",
    "ES",
    "FI",
    "FR",
    "GB",
    "GR",
    "HU",
    "IE",
    "IT",
    "LT",
    "LU",
    "LV",
    "MT",
    "NL",
    "PL",
    "PT",
    "RO",
    "SE",
    "SI",
    "SK",
  );

  // Monoaco is considered as part of France for VAT
  $eu_country_codes[] = "MC";

  // Isle of Man is considered as part of the UK for VAT
  $eu_country_codes[] = "IM";

  //merge in country names from country_get_list();
  foreach ($eu_country_codes as $key => $value) {
    $eu_countries[$value] = $countries[$value];
  }
  return $eu_countries;
}
function _commerce_eu_vat_configure_place_of_supply(&$configs, $country) {
  if (variable_get('site_default_country', NULL) != $country) {
    $tax_rates = commerce_tax_rates();
    foreach ($tax_rates as $tax_rate) {
      if (strpos($tax_rate['type'], 'eu_vat') === 0 && !empty($configs[$tax_rate['rules_component']])) {
        if ($tax_rate['module'] == 'commerce_eu_vat_' . strtolower($country)) {
          $configs[$tax_rate['rules_component']]
            ->condition('component_commerce_eu_vat_rules_place_of_supply_' . strtolower($country), array(
            'commerce_order:select' => 'commerce-line-item:order',
          ));
        }
        else {
          $configs[$tax_rate['rules_component']]
            ->condition(rules_condition('component_commerce_eu_vat_rules_place_of_supply_' . strtolower($country), array(
            'commerce_order:select' => 'commerce-line-item:order',
          ))
            ->negate());
        }
      }
    }
  }
}