in reply to Membership Dues Application bugs

I would first calculate the daily/monthly rate for the month that falls in a particular time period, then find out how many users logged on during that month, then distribute the cost evenly to the users. The following is a sample I quickly pulled together to show how to calculate daily rate. To calculate the monthly rate, say, you just need to accumulate the totals for the month.

use strict; use Date::Calc qw(:all); use Data::Dumper; my $rates = [ ['01/01/1990', '09/01/2003', 105], ['08/01/2003', '10/01/2003', 190], ['10/01/2003', '01/01/2007', 140], ]; my $new_rates = initrates($rates); # print Dumper($new_rates); my %date = ( '07/01/2003', 20, # date, no. user '09/01/2003', 10, '10/01/2003', 20, '12/12/2003', 12); foreach (sort keys %date) { my $rate = getdailyrate($_); my $per_user = sprintf "%.2f", $rate / $date{$_}; print "Daily rate for $_ is $rate, $date{$_} users, every user pay + \$$per_user\n"; } # date in dd/mm/yy sub getdailyrate { my $datestr = shift; my ($y, $m, $d) = getymd($datestr); my $date = Mktime($y, $m, $d, 0, 0, 0); foreach(@$new_rates) { return $_->[3] if $date >= $_->[0] && $date < $_->[1]; } return(0); } sub initrates { my $rates = shift; my @rates; foreach (@$rates) { my ($y1, $m1, $d1) = getymd($_->[0]); my ($y2, $m2, $d2) = getymd($_->[1]); my $total = $_->[2]; my $dd = Delta_Days($y1,$m1,$d1,$y2,$m2,$d2); my $rate = sprintf "%.02f", $total / $dd; # print "$dd days - wanted $total - rate \$$rate per day\n"; my $t1 = Mktime($y1, $m1, $d1, 0, 0, 0); my $t2 = Mktime($y2, $m2, $d2, 0, 0, 0); push @rates, [ $t1, $t2, $total, $rate ]; } return \@rates; } sub getymd { my $datestr = shift; my ($day, $mon, $year) = $datestr =~ m[(\d+)/(\d+)/(\d+)]; # or Pa +rse_Date return ($year, $mon, $day); }
And the output is -
Daily rate for 07/01/2003 is 0.02, 20 users, every user pay $0.00 Daily rate for 09/01/2003 is 95.00, 10 users, every user pay $9.50 Daily rate for 10/01/2003 is 0.10, 20 users, every user pay $0.01 Daily rate for 12/12/2003 is 0.10, 12 users, every user pay $0.01
Note that I have $0.00 in the output, that's due to the rounding with sprintf (for printing only). Remove the sprintf and you will get more accurate daily per person payment amount.

Replies are listed 'Best First'.
Re: Re: Membership Dues Application bugs
by caedes (Pilgrim) on Dec 08, 2003 at 15:50 UTC
    This just might be the different viewpoint that I was looking for. I think your idea is conceptually simpler. I'm going to study your code snippet and get back to you if I implement it. Thanks.

    -caedes