You are here

commerce_stock_local.install in Commerce Stock 8

Contains install and update functions for commerce stock local.

File

modules/local_storage/commerce_stock_local.install
View source
<?php

/**
 * @file
 * Contains install and update functions for commerce stock local.
 */
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\commerce_stock_local\Entity\StockLocation;

/**
 * Implements hook_schema().
 */
function commerce_stock_local_schema() {
  $schema['commerce_stock_transaction_type'] = [
    'description' => 'Commerce Stock transaction types',
    'fields' => [
      'id' => [
        'description' => 'Transaction type ID',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ],
      'name' => [
        'description' => 'Transaction type name',
        'type' => 'varchar_ascii',
        'not null' => TRUE,
        'default' => '',
        'length' => 128,
      ],
      'parent_id' => [
        'description' => 'Parent transaction type',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ],
    ],
    'primary key' => [
      'id',
    ],
    'foreign keys' => [
      'parent_type' => [
        'table' => 'commerce_stock_transaction_type',
        'columns' => [
          'parent_id' => 'id',
        ],
      ],
    ],
  ];
  $schema['commerce_stock_transaction'] = [
    'description' => 'Stores inventory transactions form commerce stock.',
    'fields' => [
      'id' => [
        'description' => 'Transaction ID',
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ],
      'entity_id' => [
        'description' => 'Purchasable entity ID',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ],
      'entity_type' => [
        'description' => 'Purchasable entity type',
        'type' => 'varchar_ascii',
        'length' => EntityTypeInterface::ID_MAX_LENGTH,
        'not null' => TRUE,
      ],
      'qty' => [
        'description' => 'Transaction quantity',
        'type' => 'numeric',
        'size' => 'normal',
        'not null' => TRUE,
        'default' => 0,
        'precision' => 10,
        'scale' => 2,
      ],
      'location_id' => [
        'description' => 'Transaction location ID',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ],
      'location_zone' => [
        'description' => 'Transaction location zone',
        'type' => 'varchar_ascii',
        'length' => 28,
        'not null' => TRUE,
        'default' => '',
      ],
      'unit_cost' => [
        'description' => 'Amount paid per unit',
        'type' => 'numeric',
        'size' => 'normal',
        'precision' => 19,
        'scale' => 6,
      ],
      'currency_code' => [
        'description' => 'The currency code.',
        'type' => 'varchar',
        'length' => 3,
      ],
      'transaction_time' => [
        'type' => 'int',
        'not null' => TRUE,
        'description' => 'The date & time of the transaction',
      ],
      'transaction_type_id' => [
        'description' => 'Transaction type ID',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ],
      // Metadata.
      'related_tid' => [
        'description' => 'Related transaction ID',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => FALSE,
      ],
      'related_oid' => [
        'description' => 'Related order ID',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => FALSE,
      ],
      'related_uid' => [
        'description' => 'Related user ID',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => FALSE,
      ],
      'data' => [
        'type' => 'blob',
        'not null' => FALSE,
        'size' => 'big',
        'description' => 'Serialized data array',
      ],
    ],
    'primary key' => [
      'id',
    ],
    'indexes' => [
      'entity_id__entity_type__location_id' => [
        'entity_id',
        'entity_type',
        'location_id',
      ],
    ],
    'foreign keys' => [
      'location' => [
        'table' => 'commerce_stock_location',
        'columns' => [
          'location_id' => 'location_id',
        ],
      ],
      'transaction_type' => [
        'table' => 'commerce_stock_transaction_type',
        'columns' => [
          'transaction_type_id' => 'id',
        ],
      ],
      'related_transaction' => [
        'table' => 'commerce_stock_transaction',
        'columns' => [
          'related_tid' => 'id',
        ],
      ],
      'related_order' => [
        'table' => 'commerce_order',
        'columns' => [
          'related_oid' => 'order_id',
        ],
      ],
      'related_user' => [
        'table' => 'users',
        'columns' => [
          'related_uid' => 'uid',
        ],
      ],
    ],
  ];
  $schema['commerce_stock_location_level'] = [
    'description' => 'Stock Level at a location.',
    'fields' => [
      'location_id' => [
        'description' => 'The location ID',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ],
      'entity_id' => [
        'description' => 'Purchasable entity ID',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ],
      'entity_type' => [
        'description' => 'Purchasable entity type',
        'type' => 'varchar_ascii',
        'length' => EntityTypeInterface::ID_MAX_LENGTH,
        'not null' => TRUE,
      ],
      'qty' => [
        'description' => 'The quantity',
        'type' => 'numeric',
        'size' => 'normal',
        'not null' => TRUE,
        'default' => 0,
        'precision' => 10,
        'scale' => 2,
      ],
      'last_transaction_id' => [
        'description' => 'The last transaction that was used to calculate the total quantity',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ],
    ],
    'primary key' => [
      'location_id',
      'entity_id',
      'entity_type',
    ],
    'foreign keys' => [
      'location' => [
        'table' => 'commerce_stock_location',
        'columns' => [
          'location_id' => 'location_id',
        ],
      ],
    ],
  ];
  return $schema;
}

/**
 * Implements hook_install().
 */
function commerce_stock_local_install() {
  $db = \Drupal::database();

  // Add core transaction types.
  $db
    ->insert('commerce_stock_transaction_type')
    ->fields([
    'id' => 1,
    'name' => 'Stock in',
    'parent_id' => 1,
  ])
    ->execute();
  $db
    ->insert('commerce_stock_transaction_type')
    ->fields([
    'id' => 2,
    'name' => 'Stock Out',
    'parent_id' => 2,
  ])
    ->execute();

  // Add sub transaction types.
  $db
    ->insert('commerce_stock_transaction_type')
    ->fields([
    'id' => 4,
    'name' => 'Sale',
    'parent_id' => 2,
  ])
    ->execute();
  $db
    ->insert('commerce_stock_transaction_type')
    ->fields([
    'id' => 5,
    'name' => 'Return',
    'parent_id' => 1,
  ])
    ->execute();
  $db
    ->insert('commerce_stock_transaction_type')
    ->fields([
    'id' => 6,
    'name' => 'New Stock',
    'parent_id' => 1,
  ])
    ->execute();
  $db
    ->insert('commerce_stock_transaction_type')
    ->fields([
    'id' => 7,
    'name' => 'Move From',
    'parent_id' => 2,
  ])
    ->execute();
  $db
    ->insert('commerce_stock_transaction_type')
    ->fields([
    'id' => 8,
    'name' => 'Move To',
    'parent_id' => 1,
  ])
    ->execute();
  $defaultStockLocation = StockLocation::create([
    'name' => 'Main',
    'status' => TRUE,
    'type' => "default",
  ]);
  $defaultStockLocation
    ->save();
}

/**
 * Adds 'entity_type' column to commerce_stock_transaction table.
 */
function commerce_stock_local_update_8001() {
  $spec = [
    'description' => 'Purchasable entity type',
    'type' => 'varchar_ascii',
    'length' => EntityTypeInterface::ID_MAX_LENGTH,
    'not null' => FALSE,
  ];
  $schema = \Drupal::database()
    ->schema();
  $schema
    ->addField('commerce_stock_transaction', 'entity_type', $spec);
}

/**
 * Adds 'entity_type' column to commerce_stock_location_level table.
 */
function commerce_stock_local_update_8002() {
  $spec = [
    'description' => 'Purchasable entity type',
    'type' => 'varchar_ascii',
    'length' => EntityTypeInterface::ID_MAX_LENGTH,
    'not null' => FALSE,
  ];
  $schema = \Drupal::database()
    ->schema();
  $schema
    ->addField('commerce_stock_location_level', 'entity_type', $spec);
  $spec1 = [
    'description' => 'The currency code',
    'type' => 'varchar',
    'length' => 3,
  ];
  $schema1 = \Drupal::database()
    ->schema();
  $schema1
    ->addField('commerce_stock_transaction', 'currency_code', $spec1);
}

/**
 * Update commerce_stock_location_level table.
 *
 * Change the 'entity_type' column to not allow null values.
 * Add 'entity_type' column to primary key of commerce_stock_location_level
 * table and initialize 'entity_type' column for existing records.
 */
function commerce_stock_local_update_8003() {
  $table = 'commerce_stock_location_level';
  $index = 'PRIMARY';
  $field = 'entity_type';
  $spec = [
    'description' => 'Purchasable entity type',
    'type' => 'varchar_ascii',
    'length' => EntityTypeInterface::ID_MAX_LENGTH,
    'not null' => TRUE,
  ];
  $schema = \Drupal::database()
    ->schema();
  if ($schema) {
    if ($schema
      ->tableExists($table)) {

      // Initialize entity_type column for existing records in
      // commerce_stock_location_level table.
      \Drupal::database()
        ->update($table)
        ->fields([
        'entity_type' => 'commerce_product_variation',
      ])
        ->isNull($field)
        ->execute();

      // Change the 'entity_type' column spec to not allow null values.
      $schema
        ->changeField($table, $field, $field, $spec);

      // Add new entity_type column to primary key of
      // commerce_stock_location_level table.
      if ($schema
        ->indexExists($table, $index)) {
        $schema
          ->dropIndex($table, $index);
      }
      $schema
        ->addPrimaryKey($table, [
        'location_id',
        'entity_id',
        'entity_type',
      ]);
    }

    // Update the 'entity_type' column in 'commerce_stock_transaction' table.
    $table = 'commerce_stock_transaction';
    if ($schema
      ->tableExists($table)) {
      \Drupal::database()
        ->update($table)
        ->fields([
        'entity_type' => 'commerce_product_variation',
      ])
        ->isNull($field)
        ->execute();
      $schema
        ->changeField($table, $field, $field, $spec);
    }
  }
}

/**
 * Removes the 'canonical' link from StockLocationType entity definitions.
 */
function commerce_stock_local_update_8004() {
  $definition_update_manager = \Drupal::entityDefinitionUpdateManager();
  $entity_type = $definition_update_manager
    ->getEntityType('commerce_stock_location_type');
  $links = $entity_type
    ->getLinkTemplates();
  unset($links['canonical']);
  $entity_type
    ->set('links', $links);
  $definition_update_manager
    ->updateEntityType($entity_type);
}

/**
 * Add index to entity_id field in commerce_stock_transaction table.
 */
function commerce_stock_local_update_8005() {
  $spec = [
    'fields' => [
      'entity_id' => [
        'description' => 'Purchasable entity ID',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ],
      'entity_type' => [
        'description' => 'Purchasable entity type',
        'type' => 'varchar_ascii',
        'length' => EntityTypeInterface::ID_MAX_LENGTH,
        'not null' => TRUE,
      ],
      'location_id' => [
        'description' => 'Transaction location ID',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ],
    ],
    'indexes' => [
      'entity_id__entity_type__location_id' => [
        'entity_id',
        'entity_type',
        'location_id',
      ],
    ],
  ];
  $schema = \Drupal::database()
    ->schema();
  $index_name = 'entity_id__entity_type__location_id';
  $fields = [
    'entity_id',
    'entity_type',
    'location_id',
  ];
  $schema
    ->addIndex('commerce_stock_transaction', $index_name, $fields, $spec);
}

/**
 * Add "uid" field to the "StockLocation" entity if missing.
 */
function commerce_stock_local_update_8006() {
  $entityDefinitionUpdateManager = \Drupal::entityDefinitionUpdateManager();
  if ($entityDefinitionUpdateManager
    ->getFieldStorageDefinition('uid', 'commerce_stock_location') === NULL) {
    $field = BaseFieldDefinition::create('entity_reference')
      ->setLabel(new TranslatableMarkup('User ID'))
      ->setSetting('target_type', 'user')
      ->setTranslatable(TRUE)
      ->setDefaultValueCallback(StockLocation::class . '::getDefaultEntityOwner');
    $entityDefinitionUpdateManager
      ->installFieldStorageDefinition('uid', 'commerce_stock_location', 'commerce_stock_local', $field);
  }
}

Functions

Namesort descending Description
commerce_stock_local_install Implements hook_install().
commerce_stock_local_schema Implements hook_schema().
commerce_stock_local_update_8001 Adds 'entity_type' column to commerce_stock_transaction table.
commerce_stock_local_update_8002 Adds 'entity_type' column to commerce_stock_location_level table.
commerce_stock_local_update_8003 Update commerce_stock_location_level table.
commerce_stock_local_update_8004 Removes the 'canonical' link from StockLocationType entity definitions.
commerce_stock_local_update_8005 Add index to entity_id field in commerce_stock_transaction table.
commerce_stock_local_update_8006 Add "uid" field to the "StockLocation" entity if missing.