# return customer id, plan id and activation date for activated accounts and customers my $acc = $dbh->prepare(qq/ select accounts.customerid, accounts.planid, accounts.activationdate from accounts, customers where accounts.customerid = customers.id and accounts.terminationdate = NULL and customers.activated = 'Y' /); $acc->execute; while (my $res = $acc->fetchrow_hashref) { # Information regarding billing fees for each plan is stored in @plans where the array index correlates to the billing id my $plan = $plans[ $res->{'planid'} ]; # $delta is the number of days since account activation - note that @date holds todays date my $delta = Delta_Days((split '-', $res->{'activationdate'}), @date); if ($delta == 0) { if ($plan->{'setup'}) { # account was opened today - charge set up fee if necessary } } else { # Alternatively, if the plan has a regular billing period, calculated as number of billing periods per annum, calculate whether the client should be billed today if ($plan->{'fee'} && $plan->{'period'}) { my $calc = $delta / int (Days_in_Year((@date)[0..1]) / $plan->{'period'}); if ($calc == int($calc)) { # Client should be billed $plan->{'fee'} amount } } } } $acc->finish;