Skip to content

Instantly share code, notes, and snippets.

@Critter
Created May 15, 2024 19:23
Show Gist options
  • Save Critter/353185d03f1b45be2190217126556157 to your computer and use it in GitHub Desktop.
Save Critter/353185d03f1b45be2190217126556157 to your computer and use it in GitHub Desktop.
civi prorated membership
function proratedmembership_civicrm_buildAmount($pageType, &$form, &$amount) {
if (!empty($form->get('mid'))) return;
$year = date("Y");
$today = date('Y-m-d');
$q = 0;
$holder = [];
$q1 = array(
'label' => 1,
'start' => date('Y-m-d', mktime(0, 0, 0, 1, 1, $year)),
'end' => date('Y-m-d', mktime(0, 0, 0, 3, date('t', mktime(0, 0, 0, 3, 1, $year)), $year))
);
$holder[] = $q1;
$q2 = array(
'label' => 2,
'start' => date('Y-m-d', mktime(0, 0, 0, 4, 1, $year)),
'end' => date('Y-m-d', mktime(0, 0, 0, 6, date('t', mktime(0, 0, 0, 3, 1, $year)), $year))
);
$holder[] = $q2;
$q3 = array(
'label' => 3,
'start' => date('Y-m-d', mktime(0, 0, 0, 7, 1, $year)),
'end' => date('Y-m-d', mktime(0, 0, 0, 9, date('t', mktime(0, 0, 0, 3, 1, $year)), $year))
);
$holder[] = $q3;
$q4 = array(
'label' => 4,
'start' => date('Y-m-d', mktime(0, 0, 0, 10, 1, $year)),
'end' => date('Y-m-d', mktime(0, 0, 0, 12, date('t', mktime(0, 0, 0, 3, 1, $year)), $year))
);
$holder[] = $q4;
foreach ($holder as $quarter)
{
if($today >= $quarter["start"] && $today <= $quarter["end"] )
{
$q = $quarter["label"];
break;
}
}
$priceSetId = $form->get('priceSetId');
if (!empty($priceSetId)) {
$feeBlock = &$amount;
if (!is_array($feeBlock) || empty($feeBlock)) {
return;
}
if ($pageType == 'membership') {
// echo "<pre style='font-size:9px'>";var_dump($feeBlock);echo "</pre>";die();
foreach ($feeBlock as &$fee) {
if (!is_array($fee['options'])) {
continue;
}
// echo "<pre style='font-size:9px'>";var_dump($fee['options']);echo "</pre>";die();
foreach ($fee['options'] as &$option) {
// echo "<pre style='font-size:9px'>";var_dump($option);echo "</pre>";die();
if($option['name'] != "Community" && $option['name'] != "Professional" && $option['name'] != "life" ) {
continue;
}
else {
if ($option['amount'] > 0) {
$originalAmount = round($option['amount']);
$discountAmount = null;
switch ($q) {
case 1:
//$option['amount'] = $option['amount'];
break;
case 2:
$discountAmount = $option['amount'] * .25;
$option['amount'] = $option['amount'] - ($option['amount'] * .25);
$option['label'] = $option['label'] . "($" . $originalAmount . " - Q2 Prorated: $". $discountAmount . ")";
break;
case 3:
$discountAmount = $option['amount'] * .5;
$option['amount'] = $option['amount'] - ($option['amount'] * .5);
$option['label'] = $option['label'] . "($" . $originalAmount . " - Q3 Prorated: $". $discountAmount . ")";
break;
case 4:
$discountAmount = $option['amount'] * .75;
$option['amount'] = $option['amount'] - ($option['amount'] * .75);
$option['label'] = $option['label'] . "($" . $originalAmount . " - Q4 Prorated: $". $discountAmount . ")";
break;
}
}
}
}
}
// FIXME: Somewhere between 4.7.15 and 4.7.23 the above stopped working and we have to do the following to make the confirm page show the correct amount.
$form->_priceSet['fields'] = $feeBlock;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment