t('fixed charges.')
);
$methods['fixed']['general'] = array(
'#title' => t('Fixed charge.'),
);
return $methods;
}
/**
* _flexicharge_form_elements hook.
*
* This function is called so that the charge provider can add
* required fields to the new charge form.
*
*/
function fixed_flexicharge_form_elements(&$form, $op, $method) {
switch ($method) {
case 'general':
$chid = arg(4);
$charge = _flexicharge_load_charges($chid);
$countries = store_build_countries();
array_unshift($countries, t('Please choose...'));
$qty_country = $charge->settings['qty_country'] ? $charge->settings['qty_country'] : 3;
$form['elements']['my_settings'] = array(
'#type' => 'fieldset',
'#title' => t('Fixed Custom'),
'#description' => t('Choose custom shipping fees for selected countries.'),
'#collapsible' => FALSE,
'#collapsed' => FALSE,
'#tree' => TRUE,
);
/* ACTIVE */
$form['elements']['my_settings']['active'] = array(
'#type' => 'radios',
'#title' => t('Active'),
'#description' => t('Use this to bypass module'),
'#default_value' => $charge->settings['active'],
'#options' => array(0=>t('Inactive'), 1=>t('Active')),
'#size' => 8,
);
/* DEFAULT RULE */
$form['elements']['my_settings']['default'] = array(
'#type' => 'fieldset',
'#title' => t('Default Rule'),
'#description' => t('Default rule if no rule matches.'),
'#collapsible' => FALSE,
'#collapsed' => FALSE,
'#tree' => TRUE,
);
$form['elements']['my_settings']['default']['below'] = array(
'#type' => 'textfield',
'#title' => t('Value1 <'),
'#size' => 4,
'#default_value' => $charge->settings['default']['below'],
'#prefix' => '
| ',
'#suffix' => ' | ',
);
$form['elements']['my_settings']['default']['middle'] = array(
'#type' => 'textfield',
'#title' => t('Reference'),
'#size' => 4,
'#default_value' => $charge->settings['default']['middle'],
'#prefix' => '',
'#suffix' => ' | ',
);
$form['elements']['my_settings']['default']['over'] = array(
'#type' => 'textfield',
'#title' => t('< Value2'),
'#size' => 4,
'#default_value' => $charge->settings['default']['over'],
'#prefix' => '',
'#suffix' => ' |
',
);
/* CUSTOM RULES */
$form['elements']['my_settings']['country'] = array(
'#type' => 'fieldset',
'#title' => t('Custom Rules'),
'#description' => t('If total is below "Value" field, "Add <" will be added to final total.
If total is over "Value" field, "< Add" will be added to final total.
Accepts decimals and percentages ex: -12.34, 5.4%'),
'#collapsible' => FALSE,
'#collapsed' => FALSE,
'#tree' => TRUE,
);
for($x=0;$x<($qty_country);$x++) {
$form['elements']['my_settings']['country'][$x]['code'] = array(
'#type' => 'select',
'#title' => t('Country'),
'#default_value' => $charge->settings['country'][$x]['code'],
'#options' => $countries,
'#description' => null,
'#multiple' => false,
'#required' => false,
'#prefix' => ($x==0 ? '' : '').'| ',
'#suffix' => ' | ',
);
$form['elements']['my_settings']['country'][$x]['below'] = array(
'#type' => 'textfield',
'#title' => t('Value1 <'),
'#size' => 4,
'#default_value' => $charge->settings['country'][$x]['below'],
'#prefix' => '',
'#suffix' => ' | ',
);
$form['elements']['my_settings']['country'][$x]['middle'] = array(
'#type' => 'textfield',
'#title' => t('Reference'),
'#size' => 4,
'#default_value' => $charge->settings['country'][$x]['middle'],
'#prefix' => '',
'#suffix' => ' | ',
);
$form['elements']['my_settings']['country'][$x]['over'] = array(
'#type' => 'textfield',
'#title' => t('< Value2'),
'#size' => 4,
'#default_value' => $charge->settings['country'][$x]['over'],
'#prefix' => '',
'#suffix' => ' |
',
);
}
/* COUNTRY AMOUNT */
$form['elements']['my_settings']['qty_country'] = array(
'#type' => 'textfield',
'#title' => t('Rules Count'),
'#description' => t('Use this to reajust the number of countries displayed here. Entering a number lower than the current one will DELETE the actual data related.'),
'#default_value' => $charge->settings['qty_country'],
'#size' => 8,
'#prefix'=> '
',
);
/* KILL FIELDS */
unset($form['module']['operator']);
unset($form['module']['rate']);
unset($form['module']['ptypes']);
unset($form['module']['roles']);
break;
}
}
/**
* implementation of hook_flexicharge_form_validate.
*
* This function is called so that the charge provider can
* validate the fields of a new charge on submission. The charge's
* own form elements can be found at $form['module']['elements'].
*
*/
function fixed_flexicharge_form_validate($form_id, $form) {
switch ($method) {
case 'general':
}
return $form;
}
/**
* Implementation of hook_flexicharge_calculate.
*
* We are just catching the 'review' operation of the checkoutapi.
* This is where most misc calculations will want to work. On the
* review screen.
*
*/
function fixed_flexicharge_review(&$txn, $charge) {
switch ($charge->method) {
case 'general':
$misc = array(
'type' => 'fl_'. $charge->chid,
'description' => $charge->display,
'operator' => $charge->operator,
'rate' => $charge->rate,
'subtotal_before' => $charge->subtotal_before,
'subtotal_after' => $charge->subtotal_before,
'already_added' => $charge->already_added,
'weight' => $charge->position,
'method' => $charge->method,
'settings' => $charge->settings,
'callback' => 'general_fixed_flexicharge_calculate',
);
break;
}
return $misc;
}
/**
* 'General' charge calculation
*
*/
function general_fixed_flexicharge_calculate(&$txn, $misc, $total) {
if($misc->settings['active']) {
/* TXN MATCH COUNTRY RULE */
foreach ($misc->settings['country'] as $id=>$data) {
if($txn->address['shipping']->country == $data['code']) {
$rule = $data;
}
}
/* DEFAULT RULE */
if(!$rule) {
$rule = $misc->settings['default'];
}
/* GET RULE TO USE */
if($total < $rule['middle']) {
$add = $rule['below'];
}else if($total >= $rule['middle']) {
$add = $rule['over'];
}
/* PERCENT */
if(ereg("%",$add)) {
$rate = $total * ($add/100);
/* DECIMAL */
}else{
$rate = $add;
}
return $rate;
}
}