$time_end) { $title = "Offer Expired"; $body = "Sorry, the 2004 Holidays Promotion offer expired at the end of December, GMT."; return; } my $err = sub { $title = "Error"; $body = LJ::bad_input(@_); return; }; # no authas for this page my $u = LJ::get_remote(); return $err->($ML{'error.noremote'}) unless $u; my $dbh = LJ::get_db_writer(); my $sys_id = LJ::get_userid("system") or die "No system id"; # form output $body = ""; # no suspended accounts if ($u->{statusvis} eq "S") { $body .= ""; $body .= ""; return; } # statusvis must be v, and journaltype C || P unless ($u->{statusvis} eq 'V' && $u->{journaltype} eq 'P') { $body .= ""; $body .= ""; return; } # Find the following payitems: # -- belong to this user # -- between $time_start and $time_end # -- checked out (used='Y') # -- not clothes (intangible ... coupons are allowed) # -- positive amount my $has_already = sub { return $dbh->selectrow_array ("SELECT COUNT(*) FROM statushistory WHERE userid=? AND adminid=? AND shtype=?", undef, $u->{userid}, $sys_id, $shtype)+0; }; my $amt = $dbh->selectrow_array ("SELECT SUM(pi.amt) FROM payments p, payitems pi WHERE p.userid=? AND pi.payid=p.payid " . "AND p.daterecv>=FROM_UNIXTIME(?) AND p.daterecv<=FROM_UNIXTIME(?) " . "AND p.used='Y' AND pi.amt>0 AND pi.item IN ('rename','paidacct','perm','userpic')", undef, $u->{userid}, $time_start, $time_end); my $total = POSIX::floor($amt/$dollar_req); my $have = $has_already->() + 0; my $need = $total - $have; # posting a form submission if (LJ::did_post() && $POST{submit} && $need > 0) { # get a lock on this userid my $lockkey = "$shtype:$u->{userid}"; LJ::get_lock($dbh, "global", $lockkey) or return $err->("Couldn't get lock: $lockkey"); # revalidate count inside of lock $have = $has_already->(); $need = $total - $have; my @tokens = (); foreach my $ct ($have+1..$total) { my ($tokenid, $token) = LJ::Pay::new_coupon('dollarofftan', $dollar_value, $u->{userid}, 0); unless ($tokenid && $token) { return $err->("There has been an error generating your coupon. Please try again later."); } push @tokens, $token; # log this action in statushistory. note that this is also what signifies the action # has already been performed LJ::statushistory_add ($u->{userid}, $sys_id, $shtype, "($ct) generated coupon: $token"); } # we're done working, release lock LJ::release_lock($dbh, "global", $lockkey); $body = ""; $body .= "