How do I work out how to apply the family ticket discount, then charge all remaining tickets at their relevant cost (i.e. adult tickets cost more than child tickets) ?
Here's what I have so far (seems to work, but not 100% confident about it (php))
# Work out how many pairs of people there are
$numAdultPairs = floor($oForm->adult / 2);
$numChildPairs = floor($oForm->child / 2);
# work out the number of matching pairs for child / adult
if ( $numAdultPairs > $numChildPairs ) {
$numberOfFamilyTickets = $numAdultPairs - $numChildPairs;
} else if ( $numAdultPairs < $numChildPairs ){
$numberOfFamilyTickets = $numChildPairs - $numAdultPairs;
} else if ( $numAdultPairs == $numChildPairs ) {
$numberOfFamilyTickets = $numAdultPairs;
}
# work out the remaining tickets required
$remainingAdult = $oForm->adult % 2;
$remainingChild = $oForm->child % 2;
I don't think your approach actually works. Assume you have 6 adults and 4 children. In this case your first if
would be true and you would end up with 3 - 2 = 1
family ticket. The correct result would be two family tickets and two adult tickets, though.
You want the lowest number of pairs to determine the amount of family tickets. Try something like this:
$numberOfFamilyTickets = min($numAdultPairs, $numChildPairs);
$remainingAdult = $oForm->adult - 2 * $numberOfFamilyTickets;
$remainingChild = $oForm->child - 2 * $numberOfFamilyTickets;
Assuming you make sure that $oForm->adult
and $oForm->child
are non-negative.
Something along these lines (pseudocode):
adultPairs = totalAdults / 2
childPairs = totalChildren / 2
familyPairs = min(adiltPairs, childPairs)
adults = totalAdults - familyPairs * 2
children = totalChildren - familyPairs * 2
Something like this perhaps..
minfamilies = min(NumberOfAdults , NumberOfChildren)/2
RemaningAdults = NumberOfAdults - (minfamilies * 2)
RemainingChildren = NumberOfChildren - (minfamilies * 2)
EDIT:Redundant comment. The above two comments were well justified , i just jumped in :D