<?php

/**
 * Implements hook_schema().
 */
function commerce_price_schema() {
  $schema = array();

  $schema['commerce_calculated_price'] = array(
    'description' => 'Stores pre-calculated dynamic prices.',
    'fields' => array(
      'module' => array(
        'description' => 'The name of the module performing the calculation.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'module_key' => array(
        'description' => 'A module specific key useful for indicating the context of a particular calculation, e.g. the IDs of Rules evaluated to produce the calculated price.',
        'type' => 'text',
        'size' => 'medium',
        'not null' => TRUE,
      ),
      'entity_type' => array(
        'description' => 'The type of entity this price belongs to.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'entity_id' => array(
        'description' => 'The entity ID of the object this price belongs to.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'field_name' => array(
        'description' => 'The name of the field the calculated price relates to.',
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
        'default' => '',
      ),
      'language' => array(
        'description' => 'The {languages}.language of the entity.',
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
        'default' => '',
      ),
      'delta' => array(
        'description' => 'The sequence number for this data item, used for multi-value fields',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'amount' => array(
        'description' => 'The price amount.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'currency_code' => array(
        'description' => 'The currency code for the price.',
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
      ),
      'data' => array(
        'description' => 'A serialized array of additional price data.',
        'type' => 'text',
        'size' => 'big',
        'serialize' => TRUE,
      ),
      'created' => array(
        'description' => 'The Unix timestamp when the price was calculated.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'indexes' => array(
      'module' => array('module'),
      'entity_type' => array('entity_type'),
      'entity_id' => array('entity_id'),
    ),
  );

  return $schema;
}

/**
 * Implements hook_field_schema().
 */
function commerce_price_field_schema($field) {
  if ($field['type'] == 'commerce_price') {
    return array(
      'columns' => array(
        'amount' => array(
          'description' => 'The price amount.',
          'type' => 'int',
          'not null' => TRUE,
          'default' => 0,
        ),
        'currency_code' => array(
          'description' => 'The currency code for the price.',
          'type' => 'varchar',
          'length' => 32,
          'not null' => TRUE,
        ),
        'data' => array(
          'description' => 'A serialized array of additional price data.',
          'type' => 'text',
          'size' => 'big',
          'not null' => FALSE,
          'serialize' => TRUE,
        ),
      ),
      'indexes' => array(
        'currency_price' => array('amount', 'currency_code'),
      ),
    );
  }
}

/**
 * Implements hook_uninstall().
 */
function commerce_price_uninstall() {
  // Delete any price fields.
  module_load_include('module', 'commerce');
  commerce_delete_fields('commerce_price');
}

/**
 * Fix invalid data values on price fields if necessary.
 */
function commerce_price_update_7100() {
  $fields = field_info_fields();
  $message = '';

  foreach ($fields as $field_name => $field) {
    // Only update fields stored in the default sql storage type in order to
    // speed up the process.
    if ($field['type'] == 'commerce_price' && $field['storage']['module'] == 'field_sql_storage') {
      db_query("UPDATE {field_data_" . $field_name . "} SET " . $field_name . "_data = :data WHERE " . $field_name . "_data = 'Array';", array(':data' => NULL));
      db_query("UPDATE {field_revision_" . $field_name . "} SET " . $field_name . "_data = :data WHERE " . $field_name . "_data = 'Array';", array(':data' => NULL));

      $message = t('Price fields were cleaned of invalid data values as necessary.');
    }
  }

  if (!empty($message)) {
    return $message;
  }
}
