$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 .= ""; my $coup_amt = $dollar_value * $need; $body .= "$LJ::SITENAMESHORT merchandise. p?>"; $body .= "Note: A coupon is only valid for one use, so be sure that your order's "; $body .= " value is greater than or equal to \$$coup_amt USD. p?>"; return; } # claim form $body .= "
"; $body .= ""; $body .= "clothing coupons "; $body .= "for every \$$dollar_req USD spent on service items in our store. "; $body .= "Store purchases are cumulative, so you don't have to spend \$$dollar_req at once -- separate "; $body .= "purchases can be saved up until there's enough to generate a coupon. p?>"; $body .= ""; $body .= ""; if ($need > 0) { my $s = $total == 1 ? "" : "s"; $body .= ""; my $s = $need == 1 ? "" : "s"; $body .= "
"; $body .= LJ::html_submit('submit' => "Claim $need Coupon$s"); $body .= "
"; } else { my $goal = sprintf("%0.02f", $dollar_req - ($amt % $dollar_req)); if ($have == 0) { $body .= "services from our store. p?>"; } else { my $s = $have == 1 ? "" : "s"; $body .= ""; } } if ($have > 0) { $body .= ""; $body .= "coupon retrieval page. p?>"; } $body .= "
"; return; } _code?> body=> page?>