View source
<?php
define('COMMERCE_PRICELIST_UNIX_TIME_APOCALYPSE', 2147390000);
function commerce_pricelist_menu() {
$items['admin/commerce/pricelist/commerce_pricelist_list'] = array(
'title' => 'Price lists',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'commerce_pricelist_draggable_form',
),
'access arguments' => array(
'administer commerce_pricelist_list entities',
),
'file' => 'includes/commerce_pricelist.admin.inc',
);
$items['admin/commerce/pricelist/commerce_pricelist_list/add'] = array(
'title' => 'Add a Price list',
'page callback' => 'commerce_pricelist_list_add',
'access arguments' => array(
'create commerce_pricelist_list entities',
),
'type' => MENU_LOCAL_ACTION,
'file' => 'includes/commerce_pricelist.admin.inc',
);
$items['admin/commerce/products/%commerce_product/add_price'] = array(
'title' => 'Add a Price list item',
'page callback' => 'commerce_pricelist_item_add',
'page arguments' => array(
3,
),
'access arguments' => array(
'edit any commerce_pricelist_list entity',
),
'file' => 'includes/commerce_pricelist.admin.inc',
'type' => MENU_CALLBACK,
);
$items['admin/commerce/pricelist/commerce_pricelist_list/%commerce_pricelist_list'] = array(
'title callback' => 'commerce_pricelist_list_title',
'title arguments' => array(
4,
),
'page callback' => 'commerce_pricelist_list_view',
'page arguments' => array(
4,
),
'access arguments' => array(
'view any commerce_pricelist_list entity',
),
'file' => 'includes/commerce_pricelist.admin.inc',
);
$items['admin/commerce/pricelist/commerce_pricelist_list/%commerce_pricelist_list/view'] = array(
'title' => 'View',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
'file' => 'includes/commerce_pricelist.admin.inc',
);
$items['admin/commerce/pricelist/commerce_pricelist_list/%commerce_pricelist_list/edit'] = array(
'title' => 'Edit',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'commerce_pricelist_list_form',
4,
),
'access arguments' => array(
'edit any commerce_pricelist_list entity',
),
'type' => MENU_LOCAL_TASK,
'file' => 'includes/commerce_pricelist.admin.inc',
);
$items['admin/commerce/pricelist/commerce_pricelist_item/%commerce_pricelist_item/edit'] = array(
'title' => 'Edit',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'commerce_pricelist_item_form',
4,
),
'access arguments' => array(
'edit any commerce_pricelist_list entity',
),
'file' => 'includes/commerce_pricelist.admin.inc',
);
$items['admin/commerce/pricelist/commerce_pricelist_list/%commerce_pricelist_list/add'] = array(
'title' => 'Add price',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'commerce_pricelist_item_form',
NULL,
4,
),
'access arguments' => array(
'edit any commerce_pricelist_list entity',
),
'type' => MENU_LOCAL_TASK,
'file' => 'includes/commerce_pricelist.admin.inc',
);
return $items;
}
function commerce_pricelist_entity_info() {
$info['commerce_pricelist_list'] = array(
'label' => t('Commerce Price List list'),
'entity class' => 'CommercePricelistList',
'controller class' => 'CommercePricelistListController',
'base table' => 'commerce_pricelist_list',
'uri callback' => 'commerce_pricelist_list_uri',
'access callback' => 'commerce_pricelist_entity_access',
'metadata controller class' => FALSE,
'fieldable' => FALSE,
'uuid' => TRUE,
'entity keys' => array(
'id' => 'list_id',
'uuid' => 'uuid',
'label' => 'title',
),
'bundle keys' => array(
'bundle' => 'list_id',
),
'bundles' => array(),
'static cache' => TRUE,
'view modes' => array(
'full' => array(
'label' => t('full'),
'custom settings' => FALSE,
),
),
);
$info['commerce_pricelist_item'] = array(
'label' => t('Commerce Price List item'),
'entity class' => 'CommercePricelistItem',
'controller class' => 'CommercePricelistItemController',
'base table' => 'commerce_pricelist_item',
'uri callback' => 'commerce_pricelist_item_uri',
'access callback' => 'commerce_pricelist_entity_access',
'metadata controller class' => FALSE,
'fieldable' => FALSE,
'uuid' => TRUE,
'bundle keys' => array(
'bundle' => 'item_id',
),
'bundles' => array(),
'entity keys' => array(
'id' => 'item_id',
'uuid' => 'uuid',
),
'static cache' => TRUE,
'view modes' => array(
'full' => array(
'label' => t('full'),
'custom settings' => FALSE,
),
),
);
return $info;
}
function commerce_pricelist_entity_property_info() {
$info = array();
$properties = array();
$properties['list_id'] = array(
'label' => t('List ID'),
'description' => t('The internal numeric ID of the pricelist.'),
'type' => 'integer',
'schema field' => 'list_id',
);
$properties['title'] = array(
'label' => t('Title'),
'description' => t('The title of the list.'),
'type' => 'text',
'setter callback' => 'entity_property_verbatim_set',
'required' => TRUE,
'schema field' => 'title',
);
$properties['uid'] = array(
'label' => t("Author"),
'type' => 'user',
'description' => t("The author of the pricelist."),
'setter callback' => 'entity_property_verbatim_set',
'required' => TRUE,
'schema field' => 'uid',
);
$properties['weight'] = array(
'label' => t('Weight'),
'type' => 'integer',
'setter callback' => 'entity_property_verbatim_set',
'description' => t("The weight of the list."),
'required' => TRUE,
'schema field' => 'weight',
);
$properties['status'] = array(
'label' => t('Status'),
'description' => t('Whether or not the pricelist is active.'),
'type' => 'boolean',
'setter callback' => 'entity_property_verbatim_set',
'schema field' => 'status',
);
$properties['created'] = array(
'label' => t('Date created'),
'description' => t('The date the pricelist was created.'),
'type' => 'date',
'setter callback' => 'entity_property_verbatim_set',
'schema field' => 'created',
);
$properties['changed'] = array(
'label' => t('Date updated'),
'description' => t('The date the pricelist was last updated.'),
'type' => 'date',
'schema field' => 'changed',
);
$info['commerce_pricelist_list']['properties'] = $properties;
$properties = array();
$properties['item_id'] = array(
'label' => t('List ID'),
'description' => t('The internal numeric ID of the pricelist the item belongs to.'),
'type' => 'integer',
'schema field' => 'item_id',
);
$properties['sku'] = array(
'label' => t('SKU'),
'description' => t('The human readable product SKU.'),
'type' => 'text',
'setter callback' => 'entity_property_verbatim_set',
'required' => TRUE,
'schema field' => 'sku',
);
$properties['pricelist_id'] = array(
'label' => t('List ID'),
'description' => t('The internal numeric ID of the pricelist the item belongs to.'),
'type' => 'commerce_pricelist_list',
'setter callback' => 'entity_property_verbatim_set',
'schema field' => 'pricelist_id',
);
$properties['valid_from'] = array(
'label' => t('Valid from date'),
'description' => t('The date item will be active from.'),
'type' => 'date',
'setter callback' => 'entity_property_verbatim_set',
'schema field' => 'valid_from',
);
$properties['valid_to'] = array(
'label' => t('Valid to date'),
'description' => t('The date item will be active to.'),
'type' => 'date',
'setter callback' => 'entity_property_verbatim_set',
'schema field' => 'valid_to',
);
$properties['quantity'] = array(
'label' => t('Quantity'),
'description' => t('Quantity associated with this pricelist item'),
'type' => 'decimal',
'setter callback' => 'entity_property_verbatim_set',
'required' => TRUE,
'schema field' => 'quantity',
);
$properties['price_amount'] = array(
'label' => t('Amount'),
'description' => t('The amount for this pricelist item.'),
'type' => 'integer',
'setter callback' => 'entity_property_verbatim_set',
'schema field' => 'price_amount',
);
$properties['currency_code'] = array(
'label' => t('Currency Code'),
'description' => t('The currency code for this pricelist item.'),
'type' => 'text',
'setter callback' => 'entity_property_verbatim_set',
'schema field' => 'currency_code',
);
$info['commerce_pricelist_item']['properties'] = $properties;
return $info;
}
function commerce_pricelist_commerce_price_component_type_info() {
return array(
'list_price' => array(
'title' => t('List price'),
'display_title' => t('Your pricelist difference'),
'weight' => -45,
),
);
}
function commerce_pricelist_permission() {
$permissions = array(
'administer commerce_pricelist_list entities' => array(
'title' => t('Administer commerce_pricelist_list entities'),
),
'view any commerce_pricelist_list entity' => array(
'title' => t('View any Price list entity'),
),
'edit any commerce_pricelist_list entity' => array(
'title' => t('Edit any Price list entity'),
),
'create commerce_pricelist_list entities' => array(
'title' => t('Create Price list Entities'),
),
);
return $permissions;
}
function commerce_pricelist_get_prices($sku, $time = FALSE) {
$cache_id = 'commerce_pricelist_get_prices_' . $sku;
$prices =& drupal_static($cache_id);
if (!isset($prices)) {
if ($cache = cache_get($cache_id)) {
$prices = $cache->data;
}
else {
if (!$time) {
$time = time();
}
$prices = array();
$pricelist_items = array();
$query = new EntityFieldQuery();
$query
->entityCondition('entity_type', 'commerce_pricelist_item')
->propertyCondition('sku', $sku, '=')
->propertyCondition('valid_from', $time, '<')
->propertyCondition('valid_to', $time, '>')
->propertyOrderBy('quantity', 'DESC');
$result = $query
->execute();
if (!empty($result['commerce_pricelist_item'])) {
$pricelist_items = commerce_pricelist_item_load_multiple(array_keys($result['commerce_pricelist_item']));
}
foreach ($pricelist_items as $item) {
$prices[$sku][$item->pricelist_id][$item->quantity] = array(
'currency_code' => $item->currency_code,
'amount' => $item->price_amount,
);
}
cache_set($cache_id, $prices, 'cache', time() + 300);
}
}
return $prices;
}
function commerce_pricelist_get_user_price($user, $sku, $quantity = 0, $time = FALSE) {
$price = array();
$user_pricelists = commerce_pricelist_get_user_pricelists($user);
if (empty($user_pricelists)) {
return $price;
}
$prices = commerce_pricelist_get_prices($sku, $time);
if (empty($prices)) {
return $price;
}
foreach ($user_pricelists as $pricelist) {
if (isset($prices[$sku][$pricelist->list_id])) {
foreach ($prices[$sku][$pricelist->list_id] as $quantity_limit => $price_item) {
if ($quantity >= $quantity_limit) {
$price = $price_item;
break 2;
}
}
}
}
return $price;
}
function commerce_pricelist_get_user_pricelists($user) {
$cache_id = 'commerce_pricelist_get_user_pricelists_' . $user->uid;
$pricelists =& drupal_static($cache_id);
if (!isset($pricelists)) {
if ($cache = cache_get($cache_id)) {
$pricelists = $cache->data;
}
else {
$pricelists = array();
$query = new EntityFieldQuery();
$query
->entityCondition('entity_type', 'commerce_pricelist_list')
->propertyCondition('status', 1, '=')
->propertyOrderBy('weight');
$result = $query
->execute();
if (!empty($result['commerce_pricelist_list'])) {
$pricelists = commerce_pricelist_list_load_multiple(array_keys($result['commerce_pricelist_list']));
}
drupal_alter('commerce_pricelists', $pricelists, $user);
foreach ($pricelists as $id => $pricelist) {
if (isset($pricelist->score) && $pricelist->score < 0) {
unset($pricelists[$id]);
}
}
cache_set($cache_id, $pricelists, 'cache', time() + 3600);
}
}
return $pricelists;
}
function commerce_pricelist_filter_pricelists(&$pricelists, $conditions) {
foreach ($pricelists as $id => $pricelist) {
$pass = FALSE;
foreach ($conditions as $key => $values) {
if (isset($pricelist->data['filter'][$key])) {
foreach ($values as $value) {
if (in_array($value, $pricelist->data['filter'][$key])) {
$pass = TRUE;
}
}
}
else {
$pass = FALSE;
break;
}
}
$pricelist->score = isset($pricelist->score) ? $pricelist->score : 0;
$pricelist->score += $pass ? 1 : -1;
}
}
function commerce_pricelist_list_filter_set(&$entity, $filter_key, $value) {
if (!isset($entity->data['filter'])) {
$entity->data['filter'] = array();
}
$entity->data['filter'][$filter_key] = $value;
}
function commerce_pricelist_list_filter_get($entity, $filter_key) {
return isset($entity->data['filter'][$filter_key]) ? $entity->data['filter'][$filter_key] : array();
}
function commerce_pricelist_entity_delete($entity, $type) {
if ($type == 'commerce_pricelist_list') {
$query = new EntityFieldQuery();
$query
->entityCondition('entity_type', 'commerce_pricelist_item')
->propertyCondition('pricelist_id', $entity->list_id, '=');
$result = $query
->execute();
if (!empty($result['commerce_pricelist_item'])) {
commerce_pricelist_item_delete_multiple($result['commerce_pricelist_item']);
}
}
}
function commerce_pricelist_theme() {
return array(
'commerce_pricelist_draggable_form' => array(
'render element' => 'form',
),
);
}
function commerce_pricelist_entity_access($op, $entity, $account, $entity_type) {
global $user;
$account = isset($account) ? $account : $user;
$entity_type = 'commerce_pricelist_list';
if ($op == 'view') {
return user_access('view any ' . $entity_type . ' entity', $account);
}
if ($op == 'create') {
return user_access('create ' . $entity_type . ' entities', $account);
}
if ($op == 'edit') {
return user_access('edit any ' . $entity_type . ' entity', $account);
}
if ($op == 'update') {
return user_access('edit any ' . $entity_type . ' entity', $account);
}
if ($op == 'delete') {
return user_access('edit any ' . $entity_type . ' entity', $account);
}
return FALSE;
}
function commerce_pricelist_form_commerce_product_ui_product_form_alter(&$form, &$form_state, $form_id) {
if (user_access('view any commerce_pricelist_list entity') && ($sku = $form['sku']['#default_value'])) {
module_load_include('inc', 'commerce_pricelist', 'includes/commerce_pricelist.admin');
$pricelists = commerce_pricelist_list_load_multiple();
$prices = array();
foreach ($pricelists as $pricelist) {
$table = commerce_pricelist_item_list_entities($pricelist->list_id, $sku);
if (isset($table['entity_table'])) {
$table['entity_table']['#prefix'] = check_plain($pricelist->title);
$prices[$pricelist->list_id] = $table;
}
}
$prices['actions'] = array(
'#type' => 'container',
'#attributes' => array(
'class' => array(
'form-actions',
'container-inline',
),
),
'#weight' => 100,
);
$options = array(
'attributes' => array(
'class' => array(
'button',
),
),
);
$prices['actions']['administer'] = array(
'#type' => 'item',
'#markup' => l(t('Administer price lists'), 'admin/commerce/pricelist/commerce_pricelist_list', $options),
'#weight' => 50,
);
$product = $form['#entity'];
$product_id = $product->product_id;
if ($product_id) {
$prices['actions']['add'] = array(
'#type' => 'item',
'#markup' => l(t('Add price'), 'admin/commerce/products/' . $product_id . '/add_price', $options + array(
'query' => array(
'destination' => current_path(),
),
)),
'#weight' => 49,
);
}
$form['commerce_pricelists'] = array(
'#type' => 'fieldset',
'#title' => t('Price lists'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#weight' => 100,
'prices' => $prices,
);
}
}
function _commerce_pricelist_display_date($value, $format = FALSE) {
if ($value < COMMERCE_PRICELIST_UNIX_TIME_APOCALYPSE && $value > 0 && is_numeric($value)) {
return $format ? format_date($value, 'custom', $format) : format_date($value, 'short');
}
return '';
}
class CommercePricelistItem extends Entity {
}
function commerce_pricelist_item_new($pricelist_id = NULL, $sku = NULL) {
return entity_create('commerce_pricelist_item', array(
'pricelist_id' => $pricelist_id,
'sku' => $sku,
));
}
function commerce_pricelist_item_save(&$entity) {
$result = entity_save('commerce_pricelist_item', $entity);
cache_clear_all('commerce_pricelist', 'cache', TRUE);
return $result;
}
function commerce_pricelist_item_delete($id) {
return commerce_pricelist_item_delete_multiple(array(
$id,
));
}
function commerce_pricelist_item_delete_multiple($ids) {
$result = entity_delete_multiple('commerce_pricelist_item', $ids);
cache_clear_all('commerce_pricelist', 'cache', TRUE);
return $result;
}
function commerce_pricelist_item_get_existing_id($pricelist_item) {
$item_id = db_query("SELECT item_id FROM {commerce_pricelist_item} " . "WHERE item_id != :item_id AND sku = :sku AND valid_to = :valid_to AND valid_from = :valid_from " . "AND quantity = :quantity AND pricelist_id = :pricelist_id", array(
':item_id' => $pricelist_item->item_id,
':sku' => $pricelist_item->sku,
':valid_from' => $pricelist_item->valid_from,
':valid_to' => $pricelist_item->valid_to,
':quantity' => $pricelist_item->quantity,
':pricelist_id' => $pricelist_item->pricelist_id,
))
->fetchField();
return $item_id;
}
function commerce_pricelist_item_title($entity) {
return t('Pricelist item');
}
function commerce_pricelist_item_load($entity_id = NULL, $reset = FALSE) {
$entity_ids = isset($entity_id) ? array(
$entity_id,
) : array();
$entity = commerce_pricelist_item_load_multiple($entity_ids, array(), $reset);
return $entity ? reset($entity) : FALSE;
}
function commerce_pricelist_item_load_multiple($entity_ids = FALSE, $conditions = array(), $reset = FALSE) {
return entity_load('commerce_pricelist_item', $entity_ids, $conditions, $reset);
}
function commerce_pricelist_item_uri($entity) {
return array(
'path' => 'admin/commerce/pricelist/commerce_pricelist/item/' . $entity->item_id,
);
}
class CommercePricelistList extends Entity {
}
function commerce_pricelist_list_new() {
return entity_create('commerce_pricelist_list', array());
}
function commerce_pricelist_list_save(&$entity) {
$result = entity_save('commerce_pricelist_list', $entity);
cache_clear_all('commerce_pricelist', 'cache', TRUE);
return $result;
}
function commerce_pricelist_list_delete($id) {
$result = entity_delete('commerce_pricelist_list', $id);
cache_clear_all('commerce_pricelist', 'cache', TRUE);
return $result;
}
function commerce_pricelist_list_load($list_id = NULL, $reset = FALSE) {
$list_id = isset($list_id) ? array(
$list_id,
) : array();
$entity = commerce_pricelist_list_load_multiple($list_id, array(), $reset);
return $entity ? reset($entity) : FALSE;
}
function commerce_pricelist_list_load_multiple($list_id = FALSE, $conditions = array(), $reset = FALSE) {
return entity_load('commerce_pricelist_list', $list_id, $conditions, $reset);
}
function commerce_pricelist_list_uri($entity) {
return array(
'path' => 'admin/commerce/pricelist/commerce_pricelist_list/' . $entity->list_id,
);
}
function commerce_pricelist_list_title($entity) {
return t('@item_title', array(
'@item_title' => $entity->title,
));
}