Created
September 24, 2019 10:47
-
-
Save jeroneemou/ae1861a76a681adb408a483d251a9c1f to your computer and use it in GitHub Desktop.
Calculate Customer points
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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