Skip to content

Instantly share code, notes, and snippets.

@jeroneemou
Created September 24, 2019 10:47
Show Gist options
  • Save jeroneemou/ae1861a76a681adb408a483d251a9c1f to your computer and use it in GitHub Desktop.
Save jeroneemou/ae1861a76a681adb408a483d251a9c1f to your computer and use it in GitHub Desktop.
Calculate Customer points
<?php
protected function calculateLoyaltyPoints($Loyalty_Points, $Loyalty_Participant): int
{
if (!isset($Loyalty_Points->SN_Loyalty_Points)) {
return 0;
}
if (!$Loyalty_Participant) {
return 0;
}
if (!is_array($Loyalty_Points->SN_Loyalty_Points)) {
$history[] = $Loyalty_Points->SN_Loyalty_Points;
} else {
$history = $Loyalty_Points->SN_Loyalty_Points;
}
$points = 0;
$today = new \DateTime();
foreach ($history as $item) {
$expiration = new \DateTime($item->Expiration_Date);
if ($expiration < $today) {
continue;
}
// redeemed points, not processed in NAV yet
if ((int)$item->Points < 0) {
$points += (int)$item->Points;
continue;
}
if ($item->Positive) {
$points += (int)$item->Points;
} else {
$points -= (int)$item->Points;
}
}
return $points;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment