Андрей (andy) wrote in changelog,
Андрей
andy
changelog

[ljcom] r10863: LJSUP-9157 (Self promo)

Committer: ailyin
LJSUP-9157 (Self promo)
U   trunk/bin/upgrading/en_LJ.dat
U   trunk/bin/upgrading/update-db-local.pl
U   trunk/cgi-bin/LJ/Console/Command/SelfPromoCancel.pm
U   trunk/cgi-bin/LJ/Pay/Payment/PayItem/SelfPromo.pm
U   trunk/cgi-bin/LJ/Pay/Payment/PayItem.pm
U   trunk/cgi-bin/LJ/Pay/Payment.pm
U   trunk/cgi-bin/LJ/Pay/SelfPromo.pm
U   trunk/cgi-bin/LJ/Widget/Shop/Cart.pm
U   trunk/cgi-bin/LJ/Widget/Shop/History.pm
Modified: trunk/bin/upgrading/en_LJ.dat
===================================================================
--- trunk/bin/upgrading/en_LJ.dat	2011-08-15 09:39:40 UTC (rev 10862)
+++ trunk/bin/upgrading/en_LJ.dat	2011-08-15 09:45:21 UTC (rev 10863)
@@ -10355,16 +10355,20 @@
 
 wallet.widget.history.des.donate=Sent for an <a href="[[url]]">entry</a> to [[to]]
 
+wallet.widget.history.des.order=<a href="[[cart_link]]">Order in LJ Shop</a>
+
 wallet.widget.history.des.rcvd=Received from [[from]]
 
 wallet.widget.history.des.rcvd_donate=You got tokens for an <a href="[[url]]">entry</a> from [[from]]
 
 wallet.widget.history.des.rcvd_royalty=Royalty payment [[percent]]% for [[from]]' purchase in <a href="[[app_url]]">[[app_name]]</a>
 
+wallet.widget.history.des.selfpromo_refund=SelfPromo refund from [[buyer]]
+
+wallet.widget.history.des.selfpromo_refund.generic=SelfPromo refund
+
 wallet.widget.history.des.sent=Sent to [[to]]
 
-wallet.widget.history.des.order=<a href="[[cart_link]]">Order in LJ Shop</a>
-
 wallet.widget.history.status.in_progress=In Progress
 
 wallet.widget.history.status.success=Success

Modified: trunk/bin/upgrading/update-db-local.pl
===================================================================
--- trunk/bin/upgrading/update-db-local.pl	2011-08-15 09:39:40 UTC (rev 10862)
+++ trunk/bin/upgrading/update-db-local.pl	2011-08-15 09:45:21 UTC (rev 10863)
@@ -1706,6 +1706,7 @@
         `exptime`   int(11) NOT NULL default '0',
         `cost`      int(11) NOT NULL default '0',
         `active`    int(11) NOT NULL default '1',
+        `finished`  int(11) NOT NULL default '0',
         PRIMARY KEY  (`promoid`)
     ) ENGINE=InnoDB
 });
@@ -2213,6 +2214,13 @@
             "ALTER TABLE friendstimes ADD column groupmask INT(10) UNSIGNED NOT NULL DEFAULT 1 after jitemid");
     }
 
+    unless (column_type('selfpromo', 'finished')) {
+        do_alter(
+            'selfpromo',
+            'ALTER TABLE selfpromo ADD COLUMN finished INT NOT NULL DEFAULT 0',
+        );
+    }
+
 });
 
 1;  # true

Modified: trunk/cgi-bin/LJ/Console/Command/SelfPromoCancel.pm
===================================================================
--- trunk/cgi-bin/LJ/Console/Command/SelfPromoCancel.pm	2011-08-15 09:39:40 UTC (rev 10862)
+++ trunk/cgi-bin/LJ/Console/Command/SelfPromoCancel.pm	2011-08-15 09:45:21 UTC (rev 10863)
@@ -41,6 +41,13 @@
         unless $entry->journalid == $promoted_entry->journalid
             && $entry->jitemid == $promoted_entry->jitemid;
 
+    my $admin = LJ::get_remote();
+    LJ::statushistory_add(
+        $entry->poster, $admin,
+        'selfpromo',
+        'admin cancel: ' . $entry->url,
+    );
+
     LJ::Pay::SelfPromo->admin_cancel_promo( $entry, LJ::get_remote() );
 
     $self->info( $entry_url . ' cancelled successfully' );

Modified: trunk/cgi-bin/LJ/Pay/Payment/PayItem/SelfPromo.pm
===================================================================
--- trunk/cgi-bin/LJ/Pay/Payment/PayItem/SelfPromo.pm	2011-08-15 09:39:40 UTC (rev 10862)
+++ trunk/cgi-bin/LJ/Pay/Payment/PayItem/SelfPromo.pm	2011-08-15 09:45:21 UTC (rev 10863)
@@ -52,6 +52,7 @@
 }
 
 sub is_tangible {1}
+sub allow_repeat_cart {0}
 
 sub calculate_price {
     my ($self) = @_;

Modified: trunk/cgi-bin/LJ/Pay/Payment/PayItem.pm
===================================================================
--- trunk/cgi-bin/LJ/Pay/Payment/PayItem.pm	2011-08-15 09:39:40 UTC (rev 10862)
+++ trunk/cgi-bin/LJ/Pay/Payment/PayItem.pm	2011-08-15 09:45:21 UTC (rev 10863)
@@ -1001,4 +1001,10 @@
     return LJ::load_userid($self->get_rcptid);
 }
 
+sub allow_repeat_cart {
+    my ($self) = @_;
+
+    return 1;
+}
+
 1;

Modified: trunk/cgi-bin/LJ/Pay/Payment.pm
===================================================================
--- trunk/cgi-bin/LJ/Pay/Payment.pm	2011-08-15 09:39:40 UTC (rev 10862)
+++ trunk/cgi-bin/LJ/Pay/Payment.pm	2011-08-15 09:45:21 UTC (rev 10863)
@@ -2085,4 +2085,17 @@
     return \@ret;
 }
 
+sub allow_repeat {
+    my ($self) = @_;
+
+    return 0 unless $self->get_forwhat eq 'cart';
+    return 0 unless $self->cart_paid;
+
+    foreach my $it ( $self->get_items ) {
+        return 0 unless $it->allow_repeat_cart;
+    }
+
+    return 1;
+}
+
 1;

Modified: trunk/cgi-bin/LJ/Pay/SelfPromo.pm
===================================================================
--- trunk/cgi-bin/LJ/Pay/SelfPromo.pm	2011-08-15 09:39:40 UTC (rev 10862)
+++ trunk/cgi-bin/LJ/Pay/SelfPromo.pm	2011-08-15 09:45:21 UTC (rev 10863)
@@ -353,6 +353,7 @@
 
 # !$promoid => currently promoted entry
 # $promoid  => $promotion with promoid=$promoid
+# called from LJ::Pay::Payment::PayItem::SelfPromo
 sub current_entry_info {
     my ( $class, $promoid ) = @_;
 
@@ -392,11 +393,12 @@
     };
 }
 
-# also called from elsewhere: PayItem::SelfPromo
 # this one pretty much tries to satisfy the PayItem's needs:
 #
 # * it dies if we're trying to replace an active entry in promo
 # * it doesn't send any notifications
+#
+# called from LJ::Pay::Payment::PayItem::SelfPromo
 sub activate_entry {
     my ( $class, $entry, $cost ) = @_;
 
@@ -424,11 +426,16 @@
 
     $class->clear_memcache;
 
-    $class->log( $entry->poster, 'activate' );
+    $class->log(
+        $entry->poster,
+        'entry_url' => $entry->url,
+        'event' => 'activate',
+    );
 }
 
 # supported opts: reason, details, entry, refund
-# also called from elsewhere: PayItem::SelfPromo
+#
+# called from LJ::Pay::Payment::PayItem::SelfPromo
 sub deactivate_entry {
     my ( $class, %opts ) = @_;
 
@@ -446,10 +453,11 @@
 
     $class->deactivate_promo(
         $info->{'promoid'}, $entry->poster,
-        'reason'  => $opts{'reason'},
-        'details' => $opts{'details'},
-        'entry'   => $opts{'entry'},
-        'refund'  => $opts{'refund'},
+        'reason'    => $opts{'reason'},
+        'details'   => $opts{'details'},
+        'entry'     => $opts{'entry'},
+        'entry_url' => $entry->url,
+        'refund'    => $opts{'refund'},
     );
 }
 
@@ -457,10 +465,16 @@
 sub deactivate_promo {
     my ( $class, $promoid, $poster, %opts ) = @_;
 
-    $class->db_update_promo( $promoid, 'active' => 0 );
+    $class->db_update_promo( $promoid, 'active' => 0, 'finished' => time );
     $class->clear_memcache;
 
-    $class->log( $poster, 'deactivate', $opts{'reason'}, $opts{'details'} );
+    $class->log(
+        $poster,
+        'entry_url' => $opts{'entry_url'},
+        'event'     => 'deactivate',
+        'reason'    => $opts{'reason'},
+        'details'   => $opts{'details'},
+    );
 }
 
 # supported opts:
@@ -637,10 +651,24 @@
     return LJ::Pay::SelfPromo::Lock->new;
 }
 
+# supported opts: entry_url, event, reason, details
 sub log {
-    my ( $class, $poster, $event, $reason, $details ) = @_;
+    my ( $class, $poster, %opts ) = @_;
 
-    # TODO: log to statushistory
+    my $message = '';
+
+    $message .= 'entry: ' . $opts{'entry_url'} . '; event: ' . $opts{'event'};
+
+    if ( my $reason = $opts{'reason'} ) {
+        if ( my $details = $opts{'details'} ) {
+            $reason .= '/' . $details;
+        }
+
+        $message .= ' (' . $reason . ')';
+    }
+
+    my $systemu = LJ::load_user('system');
+    return LJ::statushistory_add( $poster, $systemu, 'selfpromo', $message );
 }
 
 package LJ::Pay::SelfPromo::Error;
@@ -677,3 +705,78 @@
 }
 
 1;
+
+__END__
+
+=head1 NAME
+
+LJ::Pay::SelfPromo
+
+=head1 DESCRIPTION
+
+The module for handling SelfPromo functionality in the Shop.
+
+=head1 PUBLIC FUNCTIONS
+
+=over 2
+
+=item $class->buyout_cost()
+
+Return information about how much tokens one needs to buy out a SelfPromo
+place right now.
+
+=item $class->is_entry_eligible( $entry, $promoter, \$reason )
+
+Return a boolean value indicating whether $promoter can promote $entry;
+in case they can not, $reason will contain explanation why. Refer to the
+source code for the list of reasons.
+
+=item $class->buyout( $entry, $promoter, $price )
+
+Try and buy out the SelfPromo place for the specified $price. In case
+it is not possible, this one raises an exception; please refer to
+LJ::Widget::Shop::View::SelfPromo for an example handling code.
+
+This one refunds money to the previous promoter if there was one.
+
+=item $class->admin_cancel_promo( $entry, $admin )
+
+Cancel promotion of the $entry, refunding part of the money the promoter
+paid for it.
+
+=item $class->withdraw_entry($entry)
+
+Withdraw the indicated entry from promotion, without refund.
+
+=item $class->current_entry()
+
+Return an LJ::Entry object for the currently-promoted entry.
+
+=item $class->check_current_entry()
+
+Check whether the currently promoted entry can still be there, and remove
+it from there if it can't. This one is ran every minute by an admin script.
+
+=back
+
+=head1 SEE ALSO
+
+=over 2
+
+=item LJSUP-9157
+
+=item LJ::Pay::Payment::PayItem::SelfPromo
+
+=item LJ::Widget::Shop::View::SelfPromo
+
+=item LJ::Console::Command::SelfPromoCancel
+
+=item bin/maint/selfpromo.pl aka bin/ljmaint.pl selfpromo_check
+
+=item the 'selfpromo' DB table
+
+=item templates/Shop/SelfPromo*.tmpl
+
+=item LJ::PersonalStats
+
+=back

Modified: trunk/cgi-bin/LJ/Widget/Shop/Cart.pm
===================================================================
--- trunk/cgi-bin/LJ/Widget/Shop/Cart.pm	2011-08-15 09:39:40 UTC (rev 10862)
+++ trunk/cgi-bin/LJ/Widget/Shop/Cart.pm	2011-08-15 09:45:21 UTC (rev 10863)
@@ -191,7 +191,7 @@
         %$table_params,
         'coupons' => \@coupons,
         'method_groups' => \@method_groups,
-        'allow_repeat' => $cart->get_forwhat eq 'cart' && $self->cart_paid,
+        'allow_repeat' => $cart->allow_repeat,
         'show_wallet_warning' => $show_wallet_warning,
     };
 }
@@ -342,7 +342,7 @@
     # repeat order
     if ($act eq 'repeat') {
         $self->ml_error('shop.cart.error.repeat_unavailable')
-            unless $self->cart_paid || !$self->cart_valid;
+            unless $cart->allow_repeat;
         $self->assert_cart_nonrecbill;
 
         # try to load the "current" cart from the cookies; if it doesn't exist,

Modified: trunk/cgi-bin/LJ/Widget/Shop/History.pm
===================================================================
--- trunk/cgi-bin/LJ/Widget/Shop/History.pm	2011-08-15 09:39:40 UTC (rev 10862)
+++ trunk/cgi-bin/LJ/Widget/Shop/History.pm	2011-08-15 09:45:21 UTC (rev 10863)
@@ -102,95 +102,140 @@
     );
 
     my @ret;
-    foreach my $logitem (@$log) {
-        my $time = $logitem->time_end;
 
-        my $des;
-        my $token_xfer = 0;
-        if ($logitem->action eq ACTION_ADD) {
-            my $buyer_userid = $logitem->cart->get_userid;
-            my $cart = $logitem->cart;
-            my $is_royalty = LJ::Pay::Payment::PayItem::Royalty->present_in($cart);
-            if ($logitem->userid == $buyer_userid && !$is_royalty) {
-                $des = LJ::Lang::ml('wallet.widget.history.des.buy');
-            } else {
-                if ($logitem->item->get_anon || !$buyer_userid) {
-                    $des = LJ::Lang::ml('wallet.widget.history.des.anon');
-                } else {
-                    my $buyer = LJ::load_userid($buyer_userid);
-                    if (LJ::Pay::Payment::PayItem::Donate->present_in($cart)) {
-                        my $entry;
-                        my $vars = $cart->payvar_get_all;
-                        if ( $vars->{'donate_jid'} && $vars->{'donate_jitemid'} ) {
-                            my $journal = LJ::load_userid( $vars->{'donate_jid'} );
-                            $entry = LJ::Entry->new( $journal, 'jitemid' => $vars->{'donate_jitemid'} )
-                                if $journal;
-                        }
-                        $des = LJ::Lang::ml('wallet.widget.history.des.rcvd_donate', {
-                            'from' => $buyer->ljuser_display,
-                            'url'  => ( $entry && $entry->valid ) ? $entry->url : '',
-                        });
-                    } elsif ($is_royalty) { 
-                        my $app_payment = LJ::API::Payments::AppPayment->new(cart_obj => $cart);
-                        my $app = $app_payment->app;
-                        my $item = $cart->get_item(LJ::Pay::Payment::PayItem::Royalty->item);
-                        $des = LJ::Lang::ml('wallet.widget.history.des.rcvd_royalty', {
-                            'percent' => $item->get_subitem,
-                            'app_name' => $app->name,
-                            'app_url' => $app->href,
-                            'from' => $buyer->ljuser_display,
-                        });
-                    } else {
-                        $des = LJ::Lang::ml('wallet.widget.history.des.rcvd', {
-                            'from' => $buyer->ljuser_display,
-                        });
-                    }
+    my $describe_logitem = sub {
+        my ($logitem) = @_;
+
+        my $cart = $logitem->cart;
+
+        my %it;
+        my @classes = qw( Royalty Donate WalletTokens AppItem SelfPromo );
+        foreach my $class ( @classes ) {
+            my $class_full = 'LJ::Pay::Payment::PayItem::' . $class;
+            $it{$class} = $cart->get_item( $class_full->item );
+        }
+
+        if ( $logitem->action eq ACTION_ADD ) {
+            my $buyer_userid = $cart->get_userid;
+            my $buyer = LJ::load_userid($buyer_userid);
+
+            if ( my $it = $it{'Royalty'} ) {
+                my $app_payment =
+                    LJ::API::Payments::AppPayment->new( 'cart_obj' => $cart );
+                my $app = $app_payment->app;
+
+                return LJ::Lang::ml('wallet.widget.history.des.rcvd_royalty', {
+                    'percent'   => $it->get_subitem,
+                    'app_name'  => $app->name,
+                    'app_url'   => $app->href,
+                    'from'      => $buyer->ljuser_display,
+                });
+            }
+
+            if ( my $it = $it{'SelfPromo'} ) {
+                if ( $it->get_prop('selfpromo_type') eq 'buyout' ) {
+                    return LJ::Lang::ml(
+                        'wallet.widget.history.des.selfpromo_refund',
+                        { 'buyer' => $buyer->ljuser_display }
+                    );
                 }
-                $token_xfer = 1;
+
+                return LJ::Lang::ml(
+                    'wallet.widget.history.des.selfpromo_refund.generic'
+                );
             }
-        } else {
-            my $cart = $logitem->cart;
-            if (LJ::Pay::Payment::PayItem::WalletTokens->present_in($cart)) {
-                my @items = $cart->get_items;
-                my $rcptid = $items[0]->get_rcptid;
+
+            if ( $logitem->userid == $buyer_userid ) {
+                return LJ::Lang::ml('wallet.widget.history.des.buy');
+            }
+
+            if ( $logitem->item->get_anon || !$buyer_userid ) {
+                return LJ::Lang::ml('wallet.widget.history.des.anon');
+            }
+
+            if ( $it{'Donate'} ) {
+                my $entry_url = '';
+                my $vars = $cart->payvar_get_all;
+
+                if ( $vars->{'donate_jid'} && $vars->{'donate_jitemid'} ) {
+                    my $entry = LJ::Entry->new(
+                        $vars->{'donate_jid'},
+                        'jitemid' => $vars->{'donate_jitemid'},
+                    );
+
+                    $entry_url = $entry->url if $entry;
+                }
+
+                return LJ::Lang::ml('wallet.widget.history.des.rcvd_donate', {
+                    'from' => $buyer->ljuser_display,
+                    'url'  => $entry_url,
+                });
+            }
+
+            return LJ::Lang::ml('wallet.widget.history.des.rcvd', {
+                'from' => $buyer->ljuser_display,
+            });
+        }
+
+        if ( $logitem->action eq ACTION_REMOVE ) {
+            if ( my $it = $it{'Tokens'} ) {
+                my $rcptid = $it->get_rcptid;
                 my $rcpt = LJ::load_userid($rcptid);
-                $des = LJ::Lang::ml('wallet.widget.history.des.sent', {
+                return LJ::Lang::ml('wallet.widget.history.des.sent', {
                     'to' => $rcpt->ljuser_display,
                 });
-            } elsif (LJ::Pay::Payment::PayItem::Donate->present_in($cart)) {
-                my @items = $cart->get_items;
-                my $rcptid = $items[0]->get_rcptid;
+            }
+
+            if ( my $it = $it{'Donate'} ) {
+                my $rcptid = $it->get_rcptid;
                 my $rcpt = LJ::load_userid($rcptid);
-                my $entry;
+                my $entry_url = '';
                 my $vars = $cart->payvar_get_all;
+
                 if ( $vars->{'donate_jid'} && $vars->{'donate_jitemid'} ) {
-                    my $journal = LJ::load_userid( $vars->{'donate_jid'} );
-                    $entry = LJ::Entry->new( $journal, 'jitemid' => $vars->{'donate_jitemid'} )
-                        if $journal;
+                    my $entry = LJ::Entry->new(
+                        $vars->{'donate_jid'},
+                        'jitemid' => $vars->{'donate_jitemid'},
+                    );
+
+                    $entry_url = $entry->url if $entry;
                 }
-                $des = LJ::Lang::ml('wallet.widget.history.des.donate', {
+
+                return LJ::Lang::ml('wallet.widget.history.des.donate', {
                     'to' => $rcpt->ljuser_display,
-                    'url' => ( $entry && $entry->valid ) ? $entry->url : '',
+                    'url' => $entry_url,
                 });
-            } elsif (LJ::Pay::Payment::PayItem::AppItem->present_in($cart)) {
-                my @items = $cart->get_items;
-                my $app = $items[0]->get_app;
+            }
+
+            if ( my $it = $it{'AppItem'} ) {
+                my $app = $it->get_app;
                 my $lj_app = ($app ? $app->ljapp_display : 'NO APPLICATION');
                 my $cart_link = $cart->cart_link;
-                $des = LJ::Lang::ml('wallet.widget.history.des.appitem', {
+                return LJ::Lang::ml('wallet.widget.history.des.appitem', {
                     'lj_app' => $lj_app,
                     'app_url' => $app->href,
                     'app_name' => $app->name,
                     'cart_link' => $cart_link,
                 });
-            } else {
-                my $cart_link = $cart->cart_link;
-                $des = LJ::Lang::ml('wallet.widget.history.des.order', {
-                    'cart_link' => $cart_link,
-                });
             }
+
+            my $cart_link = $cart->cart_link;
+            return LJ::Lang::ml('wallet.widget.history.des.order', {
+                'cart_link' => $cart_link,
+            });
         }
 
+    };
+
+    foreach my $logitem (@$log) {
+        my $time = $logitem->time_end;
+
+        my $des = $describe_logitem->($logitem);
+
+        my $token_xfer = 
+            $logitem->action eq ACTION_ADD &&
+            $logitem->userid != $logitem->cart->get_userid;
+
         my $amt = $logitem->qty;
         $amt = -$amt if $logitem->action eq ACTION_REMOVE;
 

Tags: andy, dat, ljcom, pl, pm
Subscribe
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 0 comments