Collecting total prices on a recipe form using field collections, JQuery / AHAH
(function($) {
Drupal.behaviors.recipesForm = {
attach: function (context, settings) {
$(".field-name-field-recipe-quantity input[type=text]").focus(function() {
$ingredient = $(this).parent().parent().parent().prev().find("select");
nid = $ingredient.find('option:selected').val();
//console.log(nid);
$.get('/ingredient/price/get/' + nid , null, updateCost);
});
$(".field-name-field-recipe-quantity input[type=text]").blur(function() {
total_cost = 0;
selling_price = 0;
cost_per_kg = 0;
$quantity = $(this);
quantity_val = $(this).val();
if (quantity_val && cost_per_kg) {
var item = $(".field-name-field-recipe-cost");
$cost = $(this).parent().parent().parent().parent().find(item).find("input[type=text]");
cost_val = quantity_val * cost_per_kg;
//console.log(quantity_val);
//console.log(cost_per_kg);
//console.log(cost_val.toFixed(2));
$cost.val(cost_val.toFixed(2));
}
$('.field-name-field-recipe-cost').each(function() {
//console.log(this);
//console.log($(this).find("input[type=text]"));
//console.log($(this).find("input[type=text]").val());
cost = $(this).find("input[type=text]").val();
total_cost = total_cost + parseFloat(cost);
});
//console.log(total_cost);
$total_cost = $('.field-name-field-total-cost').find("input[type=text]");
$total_cost.val(total_cost.toFixed(2));
selling_price = total_cost * 3.6;
console.log(selling_price);
$selling_price = $('.field-name-field-selling-price').find("input[type=text]");
$selling_price.val(selling_price.toFixed(2));
});
$(".field-name-field-recipe-ingredient select").change(function() {
nid = $(this).find('option:selected').val();
$.get('/ingredient/price/get/' + nid , null, updateCost);
var $item = $(".field-name-field-recipe-quantity");
$quantity = $(this).parent().parent().parent().find($item).find("input[type=text]");
//console.log($quantity);
$quantity.val(0);
var $item = $(".field-name-field-recipe-cost");
$cost = $(this).parent().parent().parent().find($item).find("input[type=text]");
$cost.val(0);
});
}
};
var updateCost = function(response) {
cost_per_kg = response['data'];
//console.log(cost_per_kg);
}
})(jQuery);
/**
* Implements hook_menu().
*/
function mymodule_menu() {
$items['ingredient/price/get'] = array(
'page callback' => '_mymodule_get_ingredient_price_ajax',
'type' => MENU_CALLBACK,
'access arguments' => array('access content'),
);
return $items;
}
/**
* Callback to return JSON encoded ingredient price for given nid.
*/
function _mymodule_get_ingredient_price_ajax($nid) {
$node = node_load($nid);
//print_r($node->field_cost_per_kg['und'][0]['value']);
$cost_per_kg = $node->field_cost_per_kg['und'][0]['value'];
drupal_json_output(array('status' => 0, 'data' => $cost_per_kg));
drupal_exit();
}
function _mymodule_get_ingredient_price($nid) {
$node = node_load($nid);
$cost_per_kg = $node->field_cost_per_kg['und'][0]['value'];
return $cost_per_kg;
}
function mymodule_form_recipe_sheet_node_form_alter(&$form, &$form_state, $form_id) {
//dsm($form_id);
//dsm($form);
if(arg(0) == 'node' && arg(2) == 'edit'){
$total_cost = 0;
$selling_price = 0;
foreach ($form['field_collection_ingredients']['und'] as $key => &$value) {
if (is_numeric($key)) {
//dsm($key);
//Get the node id of each ingredient.
//dsm($value);
//dsm($value['field_recipe_ingredient']['und']['#default_value'][0]);
$nid = $value['field_recipe_ingredient']['und']['#default_value'][0];
$ingredient_price = _mymodule_get_ingredient_price($nid);
//dsm($ingredient_price);
//dsm($value['field_recipe_quantity']['und'][0]['value']['#default_value']);
$quantity = $value['field_recipe_quantity']['und'][0]['value']['#default_value'];
$cost = $ingredient_price * $quantity;
$value['field_recipe_cost']['und'][0]['value']['#default_value'] = round($cost, 2);
$total_cost += $cost;
// BUG: sets the individual cost values to zero
//$value['field_recipe_cost']["#disabled"] = TRUE;
}
}
$selling_price = $total_cost * 3.6;
$form['field_total_cost']['und'][0]['value']['#default_value'] = round($total_cost, 2);
$form['field_selling_price']['und'][0]['value']['#default_value'] = round($selling_price, 2);
}
// BUG: sets the total cost value to zero
//$form['field_total_cost']["#disabled"] = TRUE;
drupal_add_js(drupal_get_path('module', 'mymodule') .'/scripts/mymodule.js');
}
Tags: