fu: Close-up of Fu, bringing a scoop of water to her mouth (Default)
fu ([personal profile] fu) wrote in [site community profile] changelog2010-06-23 10:39 am

[dw-free] /admin/pay should allow the addition of paid time in arbitrary increments

[commit: http://hg.dwscoalition.org/dw-free/rev/4c40adbf5220]

http://bugs.dwscoalition.org/show_bug.cgi?id=2040

Allow site admins to add paid time in more granular increments, increase
month limit, and manually edit the expiration date.

Patch by [personal profile] catness.

Files modified:
  • cgi-bin/DW/Pay.pm
  • htdocs/admin/pay/index.bml
--------------------------------------------------------------------------------
diff -r 571367be2966 -r 4c40adbf5220 cgi-bin/DW/Pay.pm
--- a/cgi-bin/DW/Pay.pm	Tue Jun 22 16:46:37 2010 -0500
+++ b/cgi-bin/DW/Pay.pm	Wed Jun 23 18:45:52 2010 +0800
@@ -334,6 +334,7 @@ sub expire_user {
 #   uuserid     required    user object or userid to set paid status for
 #   class       required    type of account to be using (_account_type)
 #   months      required    how many months to grant, 99 = perm
+#   days        required    how many days (in addition to months) to grant
 #
 # RETURN: undef on error, else 1 on success.
 #
@@ -350,12 +351,18 @@ sub add_paid_time {
     return error( ERR_FATAL, 'Invalid type, no typeid found.' )
         unless $typeid;
 
-    my $months = shift();
+    my ( $months, $days ) = @_;
     return error( ERR_FATAL, 'Invalid value for months.' )
-        unless $months > 0 && $months <= 12 || $months == 99;
+        unless $months >= 0 && $months <= 99;
+
+    return error( ERR_FATAL, 'Invalid value for days.' )
+        unless $days >= 0 && $days <= 31;
+
+    return error( ERR_FATAL, 'Empty time increment')
+        unless $months > 0 || $days > 0 ;
 
     # okay, let's see what the user is right now to decide what to do
-    my ( $newtypeid, $amonths, $asecs ) = ( $typeid, $months, 0 );
+    my ( $newtypeid, $amonths, $adays, $asecs ) = ( $typeid, $months, $days, 0 );
     my $permanent = $months == 99 ? 1 : 0;
 
     # if they have a $ps hashref, they have or had paid time at some point
@@ -402,6 +409,7 @@ sub add_paid_time {
         # at this point we can ignore whatever they have in $ps, as we're going
         # overwrite it with our own stuff
     }
+    $asecs += $adays * 86400;
 
     # so at this point, we can do whatever we're supposed to do
     my $rv = DW::Pay::update_paid_status( $u,
@@ -530,6 +538,43 @@ sub update_paid_status {
         $sclient->insert_jobs( TheSchwartz::Job->new_from_array( 'DW::Worker::Sphinx::Copier', { userid => $u->id } ) );
     }
 
+    return 1;
+}
+
+################################################################################
+# DW::Pay::edit_expiration_datetime
+#
+# ARGUMENTS: uuserid, expiration datetime
+#
+#   uuserid     required    user object or userid to set paid status for
+#   datetime    required    new expiration datetime
+#
+# RETURN: undef on error, else 1 on success.
+#
+#
+sub edit_expiration_datetime {
+    DW::Pay::clear_error();
+
+    my $u = LJ::want_user( shift() )
+        or return error( ERR_FATAL, "Invalid/not a user object." );
+    my $datetime = shift();
+
+    my $ps = DW::Pay::get_paid_status( $u );
+    return error( ERR_FATAL, "Can't set expiration date for this type of account" )
+        if $ps->{expiresin} <= 0 || $ps->{permanent};
+
+    my $dbh = DW::Pay::get_db_writer()
+        or return error( ERR_TEMP, "Unable to get db writer." );
+
+    my $row = $dbh->selectrow_hashref( "SELECT UNIX_TIMESTAMP(?) AS datetime, ? < NOW() AS expired", undef, $datetime, $datetime );
+
+    return error ( ERR_FATAL, "Invalid expiration date/time" ) unless $row->{datetime} ;
+    return error ( ERR_FATAL, "Expiration date/time is in the past" ) if $row->{expired};
+    return error ( ERR_FATAL, "Expiration date/time is unchanged" ) if $row->{datetime} == $ps->{expiretime};
+
+    $dbh->do( q{UPDATE dw_paidstatus SET expiretime=? WHERE userid=?}, undef, $row->{datetime}, $u->id );
+    return error( ERR_FATAL, "Database error: " . $dbh->errstr )
+        if $dbh->err;
     return 1;
 }
 
diff -r 571367be2966 -r 4c40adbf5220 htdocs/admin/pay/index.bml
--- a/htdocs/admin/pay/index.bml	Tue Jun 22 16:46:37 2010 -0500
+++ b/htdocs/admin/pay/index.bml	Wed Jun 23 18:45:52 2010 +0800
@@ -22,11 +22,25 @@
             unless LJ::check_form_auth();
 
         if ( $POST{givetime} ) {
-            my ( $who, $type, $months ) = ( $POST{user}, $POST{type}, $POST{months} );
+            my ( $who, $type, $months, $days, $datetime ) = ( $POST{user}, $POST{type}, $POST{months}, $POST{days}, $POST{datetime} );
             my $u = LJ::load_user( $who )
                 or return "invalid user";
+
+            if ( $POST{submit} eq 'Edit' ) {
+              return "Invalid date/time format" unless $datetime =~ /^\d\d\d\d\-\d\d\-\d\d *( \d\d(:\d\d){1,2})?$/;
+              my $rv = DW::Pay::edit_expiration_datetime( $u, $datetime );
+              if ( $rv ) {
+                  LJ::statushistory_add( $u, $remote, 'paidstatus',
+                                         "Admin override: edit expiration date/time: $datetime" );
+                  return BML::redirect( "$LJ::SITEROOT/admin/pay/index?view=$u->{user}" );
+              }
+              return "Error: " . DW::Pay::error_text() . "\n";
+            } 
+
             return "invalid type"
                 unless $type =~ /^(?:seed|premium|paid|expire)$/;
+            $months = 0 if $months eq '';
+            $days = 0 if $days eq '';
             $months = 99 if $type eq 'seed';
 
             if ( $type eq 'expire' ) {
@@ -38,10 +52,10 @@
                 }
                     
             } else {
-                my $rv = DW::Pay::add_paid_time( $u, $type, $months );
+                my $rv = DW::Pay::add_paid_time( $u, $type, $months, $days );
                 if ( $rv ) {
                     LJ::statushistory_add( $u, $remote, 'paidstatus',
-                                           "Admin override: gave paid time to user: months=$months type=$type" );
+                                           "Admin override: gave paid time to user: months=$months days=$days type=$type" );
                     return BML::redirect( "$LJ::SITEROOT/admin/pay/index?view=$u->{user}" );
                 }
             }
@@ -60,6 +74,7 @@
             unless $u;
 
         my $ps = DW::Pay::get_paid_status( $u );
+        my $body_date = '';
         if ( $ps ) {
             $body .= '<h2>Paid Status</h2>';
             $body .= LJ::ljuser( $u ) . ": " . DW::Pay::type_name( $ps->{typeid} );
@@ -73,7 +88,12 @@
                     my $exp = LJ::ago_text( $ps->{expiresin} );
                     $exp =~ s/ ago//;
                     $body .= "; expires $expt (<strong>$exp</strong>).";
+                    $body_date = <<EOF;
 
+<tr><td>Or edit expiration date:</td>
+<td align="center"><input type="text" name="datetime" maxlength="20" size="20" value="$expt"/></td>
+<td>    <input type="submit" name="submit" value="Edit" /></td></tr>
+EOF
                 } else {
                     $body .= '; <strong>expired</strong>.';
                 }
@@ -88,16 +108,22 @@
 <br /><br />
     <form method="post"><input type="hidden" name="givetime" value="1">
     <input type="hidden" name="user" value="$u->{user}">$auth
-Give Paid Time:
+<table border='0' cellpadding='4'>
+<tr><td>Give Paid Time:
     <select name="type">
         <option value="paid">Paid Account</option>
         <option value="premium">Paid Premium Account</option>
         <option value="seed">Seed Account</option>
         <option value="blank"></option>
         <option value="expire">FORCE EXPIRATION</option>
-    </select>
-    for <input type="text" name="months" maxlength="2" size="3" /> months
-    <input type="submit" value="Give!" />
+    </select></td>
+<td align="center">
+        for <input type="text" name="months" maxlength="2" size="3" /> months, 
+        <input type="text" name="days" maxlength="2" size="3" /> days
+</td>
+<td><input type="submit" name="submit" value="Give!" /></td></tr>
+$body_date
+</table>
 </form>
 <p><a href="/admin/statushistory?user=$u->{user}">View statushistory for user.</a></p>
 <h2>View Carts</h2>
--------------------------------------------------------------------------------