View source
<?php
define('MERCI_CHECKOUT_REFERENCE', 'field_merci_checkout');
define('MERCI_LINE_ITEM_REFERENCE_ALIAS', 'field_merci_line_item');
define('MERCI_FEE', 'merci_late_fee');
function merci_commerce_merci_line_item_validate($line_item, $form, &$form_state) {
$line_item_wrapper = entity_metadata_wrapper('merci_line_item', $line_item);
$product_id = $line_item_wrapper->{MERCI_RESOURCE_REFERENCE}
->getIdentifier();
$query = new EntityFieldQuery();
$query
->entityCondition('entity_type', 'node')
->entityCondition('bundle', MERCI_RESOURCE_DISPLAY_BUNDLE)
->fieldCondition('field_crp_product_reference', 'product_id', $product_id);
$result = $query
->execute();
if (isset($result['node'])) {
$nids = array_keys($result['node']);
$merci_resource_reference = reset($nids);
}
$line_item_wrapper->{MERCI_RESOURCE_DISPLAY}
->set($merci_resource_reference);
$line_item_wrapper->line_item_label
->set($line_item_wrapper->{MERCI_RESOURCE_REFERENCE}->sku
->value());
}
function merci_commerce_commerce_customer_profile_type_info_alter(&$profile_types) {
foreach ($profile_types as $key => $profile_type) {
$profile_types[$key]['label_callback'] = 'merci_commerce_commerce_customer_profile_default_label';
}
}
function merci_commerce_commerce_customer_profile_default_label($profile) {
$label = '';
if (!empty($profile->commerce_customer_address)) {
$profile_wrapper = entity_metadata_wrapper('commerce_customer_profile', $profile);
if (isset($profile_wrapper->field_full_name)) {
$label = $profile_wrapper->field_full_name
->value();
}
}
if (empty($label)) {
$label = $profile->profile_id;
}
return $label;
}
function merci_commerce_form_commerce_order_ui_order_form_alter(&$form, &$form_state, $form_id) {
$form['commerce_customer_billing']['#access'] = FALSE;
$form['commerce_customer_homeroom']['#access'] = FALSE;
$form['commerce_customer_organization']['#access'] = FALSE;
$form['customer_profile_type_ui']['#access'] = FALSE;
if (empty($form_state['line_item_add'])) {
$form['commerce_line_items']['und']['actions']['line_item_type'] = array(
'#type' => 'value',
'#value' => MERCI_FEE,
);
}
$form[MERCI_CHECKOUT_REFERENCE]['#access'] = FALSE;
$checkout_field = field_view_field($form['#entity_type'], $form['#entity'], MERCI_CHECKOUT_REFERENCE, 'default');
$form['checkout'] = array(
'#type' => 'fieldset',
'#title' => 'Checkout',
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'data' => array(
'#markup' => drupal_render($checkout_field),
),
);
}
function merci_commerce_commerce_line_item_type_info() {
return array(
MERCI_FEE => array(
'name' => t('MERCI Late Fee'),
'product' => TRUE,
'description' => t('Adds a late fee for a checked out resource.'),
'base' => 'merci_commerce_line_item',
),
);
}
function merci_commerce_line_item_title($line_item) {
$merci_line_item = entity_load_single('merci_line_item', $line_item->data['merci_line_item_id']);
$merci_line_item_wrapper = entity_metadata_wrapper('merci_line_item', $merci_line_item);
return t('!display_title (!resource_title): Quantity !quantity', array(
'!display_title' => $merci_line_item_wrapper->{MERCI_RESOURCE_DISPLAY}
->label(),
'!resource_title' => $merci_line_item_wrapper
->label(),
'!quantity' => (int) $line_item->quantity,
));
}
function merci_commerce_line_item_configuration($line_item_type) {
commerce_product_line_item_configuration($line_item_type);
}
function merci_commerce_line_item_options($line_item_type) {
return $options;
}
function merci_commerce_line_item_add_form($element, &$form_state) {
$form = array();
$query = db_select('commerce_product', 'c');
$query
->addField('c', 'product_id', 'product_id');
$query
->addField('c', 'title', 'title');
$query
->condition('c.type', 'product');
$result = $query
->execute();
$options = array();
foreach ($result as $record) {
$options[$record->product_id] = $record->title;
}
$form['fee_type_id'] = array(
'#type' => 'select',
'#title' => t('Fee type'),
'#description' => t('Select the type of fee to add.'),
'#options' => $options,
);
$entity = $form_state['complete form']['#entity'];
$entity_type = $form_state['complete form']['#entity_type'];
$entity_wrapper = entity_metadata_wrapper($entity_type, $entity);
$merci_line_items = $entity_wrapper->{MERCI_CHECKOUT_REFERENCE}->{MERCI_LINE_ITEM_REFERENCE};
$options = array();
foreach ($merci_line_items
->getIterator() as $delta => $merci_line_item) {
$options[$merci_line_item
->getIdentifier()] = t('!display_title (!resource_title)', array(
'!display_title' => $merci_line_item->{MERCI_RESOURCE_DISPLAY}
->label(),
'!resource_title' => $merci_line_item
->label(),
));
}
$form['merci_line_item_id'] = array(
'#type' => 'select',
'#title' => t('Resource'),
'#description' => t('Select the resource this fee applies to.'),
'#options' => $options,
'#required' => TRUE,
);
$order = $element['#entity'];
$line_item = commerce_line_item_new(MERCI_FEE, $order->order_id);
return $form;
}
function merci_commerce_line_item_add_form_submit(&$line_item, $element, &$form_state, $form) {
$product = entity_load_single('commerce_product', $element['actions']['fee_type_id']['#value']);
$merci_line_item = entity_load_single('merci_line_item', $element['actions']['merci_line_item_id']['#value']);
merci_commerce_line_item_populate($line_item, $product, $merci_line_item);
}
function merci_commerce_line_item_populate(&$line_item, $product, $merci_line_item) {
$merci_line_item_wrapper = entity_metadata_wrapper('merci_line_item', $merci_line_item);
$line_item->data['merci_line_item_id'] = $merci_line_item_wrapper
->getIdentifier();
$resource_wrapper = $merci_line_item_wrapper->{MERCI_RESOURCE_DISPLAY};
switch ($product->sku) {
case 'late_fee':
$field_late_fine = $resource_wrapper->field_late_fine
->value();
$product->commerce_price = empty($field_late_fine['amount']) ? $product->commerce_price : $resource_wrapper
->value()->field_late_fine;
break;
case 'replacement_fee':
$field_replacement_cost = $resource_wrapper->field_replacement_cost
->value();
$product->commerce_price = is_null($field_replacement_cost['amount']) ? $product->commerce_price : $resource_wrapper
->value()->field_replacement_cost;
break;
default:
break;
}
commerce_product_line_item_populate($line_item, $product);
}
function merci_commerce_merci_fields_info() {
$fields = array();
module_load_include('inc', 'merci_commerce', 'merci_commerce.fields');
$field_bases = merci_commerce_fields();
$instances = merci_commerce_instances();
foreach ($field_bases as $field_name => $field) {
$fields[$field_name] = array(
'field' => $field,
'instance' => $instances[$field_name],
);
}
return $fields;
}
function merci_commerce_views_data_alter(&$data) {
foreach ($data as $table => $fields) {
foreach ($fields as $key => $value) {
if (is_array($value) and array_key_exists('filter', $value) and $value['filter']['handler'] == 'views_handler_filter_string') {
$data[$table][$key]['filter']['handler'] = 'merci_commerce_handler_filter_string';
}
}
}
}
function merci_commerce_field_formatter_info() {
return array(
'merci_commerce_reference_view' => array(
'label' => t('Product View'),
'description' => t('Display the products showing checkout status.'),
'field types' => array(
'commerce_product_reference',
),
),
);
}
function merci_commerce_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$result = array();
$element['sums'] = array();
$element['products'] = array();
$line_item_ids = array();
switch ($display['type']) {
case 'merci_commerce_reference_view':
$display_tmp = $display;
$display_tmp['type'] = 'commerce_product_reference_sku_link';
$result = commerce_product_reference_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display_tmp);
foreach ($items as $delta => $item) {
$product_ids[$delta] = $item['product_id'];
}
$query = new EntityFieldQuery();
$query
->entityCondition('entity_type', 'merci_line_item')
->fieldCondition(MERCI_CHECKOUT_STATUS, 'value', 'checked out', '=')
->fieldCondition(MERCI_RESOURCE_REFERENCE, 'target_id', $product_ids, 'IN');
$query_result = $query
->execute();
$statuses = array();
if (isset($query_result['merci_line_item'])) {
$line_items_nids = array_keys($query_result['merci_line_item']);
$line_items = entity_load('merci_line_item', $line_items_nids);
foreach ($line_items as $line_item) {
$product_id = $line_item->{MERCI_RESOURCE_REFERENCE}['und'][0]['target_id'];
$status = $line_item->{MERCI_CHECKOUT_STATUS}['und'][0]['value'];
$statuses[array_search($product_id, $product_ids)] = $line_item->entity_id;
}
}
$products = entity_load('commerce_product', $product_ids);
$sum = array();
foreach ($product_ids as $delta => $product_id) {
$markup = drupal_render($result[$delta]);
if (array_key_exists($delta, $statuses)) {
$checkout = entity_load_single('merci_reservation', $statuses[$delta]);
$checkout_wrapper = entity_metadata_wrapper('merci_reservation', $checkout);
$patron = $checkout_wrapper->merci_customer_billing
->value();
$homeroom = $checkout_wrapper->merci_customer_homeroom
->value();
$organization = $checkout_wrapper->merci_customer_organization
->value();
unset($location);
if ($patron) {
$location = $checkout_wrapper->merci_customer_billing->field_full_name
->value();
}
if ($organization) {
$location = $checkout_wrapper->merci_customer_organization->field_full_name
->value();
}
if ($homeroom) {
$room = $checkout_wrapper->merci_customer_homeroom->field_full_name
->value();
$location = isset($location) ? $location . '-' . $room : $room;
}
$result[$delta] = array(
'#type' => 'link',
'#title' => $location,
'#href' => 'merci_reservation/' . $statuses[$delta] . '/edit',
);
$result[$delta] = $markup . ' (' . drupal_render($result[$delta]) . ')';
if (!array_key_exists('Checked out', $sum)) {
$sum['Checked out'] = 1;
}
else {
$sum['Checked out']++;
}
}
else {
$product = $products[$product_ids[$delta]];
$product_wrapper = entity_metadata_wrapper('commerce_product', $product);
$status = $product_wrapper->merci_resource_status
->value();
if (empty($status)) {
$status = 'available';
}
if (!array_key_exists($status, $sum)) {
$sum[$status] = 1;
}
else {
$sum[$status]++;
}
if ($status != 'available') {
$result[$delta] = $markup . ' (' . ucfirst($status) . ')';
}
else {
$result[$delta] = $markup;
}
}
}
foreach ($sum as $key => $value) {
$element['sums'][$key] = ucfirst($key) . ': ' . $value;
}
$element['sums'] = array(
'#markup' => implode(', ', $element['sums']),
);
$element['products'] = array(
'#markup' => implode('<br> ', $result),
);
break;
}
return $element;
}