ssafronova (ssafronova) wrote in changelog,
ssafronova
ssafronova
changelog

[ljcom] r7560: LJSUP-4203: Userpic Add-on - LJSUP-4457:...

Committer: ssafronova
LJSUP-4203: Userpic Add-on - LJSUP-4457: move bulk code to class structure
fixed vgift bug
mostly refactored can_be_added for addons, depending on account level
finished additem section in pay/modify.bml

U   branches/shop/cgi-bin/LJ/Pay/Payment/PayItem/Addon/Boolean.pm
U   branches/shop/cgi-bin/LJ/Pay/Payment/PayItem/Addon/Sized.pm
U   branches/shop/cgi-bin/LJ/Pay/Payment/PayItem/Coppa.pm
U   branches/shop/cgi-bin/LJ/Pay/Payment/PayItem/Coupon.pm
U   branches/shop/cgi-bin/LJ/Pay/Payment/PayItem/PaidAccount.pm
U   branches/shop/cgi-bin/LJ/Pay/Payment/PayItem/Permanent.pm
U   branches/shop/cgi-bin/LJ/Pay/Payment/PayItem/Rename.pm
U   branches/shop/cgi-bin/LJ/Pay/Payment/PayItem/VGift.pm
U   branches/shop/cgi-bin/LJ/Pay/Payment/PayItem.pm
U   branches/shop/cgi-bin/LJ/VGift.pm
U   branches/shop/htdocs/pay/modify.bml
Modified: branches/shop/cgi-bin/LJ/Pay/Payment/PayItem/Addon/Boolean.pm
===================================================================
--- branches/shop/cgi-bin/LJ/Pay/Payment/PayItem/Addon/Boolean.pm	2009-08-11 07:01:19 UTC (rev 7559)
+++ branches/shop/cgi-bin/LJ/Pay/Payment/PayItem/Addon/Boolean.pm	2009-08-11 07:58:38 UTC (rev 7560)
@@ -2,6 +2,11 @@
 
 use base LJ::Pay::Payment::PayItem::Addon;
 
+sub need_paid             { return 1;        }
+sub having_account_levels { return ('perm'); }
+sub disallowed_account_levels { return ();   }
+sub can_be_added_to_special_cases { return 1; }
+
 sub is_valid_bool_bonus {
     my $self = shift;
     my ($item, $qty) = ($self->{item}, $self->{qty});
@@ -110,22 +115,11 @@
     return $LJ::Pay::Payment::PayItem::Addon::bonus{$self->{item}}->{items}->{$self->{qty}}->{'amount_rec'};
 }
 
-sub can_be_added {
+sub validate_user_input {
     my ($self, $cartobj, $errs_ref) = @_;
 
-    return 0 unless defined $LJ::Pay::Payment::PayItem::Addon::bonus{$self->{item}}->{items}->{$self->{qty}};
-
-    my $rcptuser = LJ::load_userid($self->{'rcptid'});
-    return 0 unless $rcptuser;
-
-    # can't buy paid time for permanent accounts
-    if ($rcptuser->in_class('perm')) {
-        my $dispname = $self->product_name("short");
-        $errs_ref->[0] = BML::ml('.error.cantbuyitemforperm', {'item' => "<b>$dispname</b>"});
-        return 0;
-    }
-
-    return 1;
+    return 1 if defined $LJ::Pay::Payment::PayItem::Addon::bonus{$self->{item}}->{items}->{$self->{qty}};
+    return 0;
 }
 
 # fill 'amt', and, may be, 'qty' field

Modified: branches/shop/cgi-bin/LJ/Pay/Payment/PayItem/Addon/Sized.pm
===================================================================
--- branches/shop/cgi-bin/LJ/Pay/Payment/PayItem/Addon/Sized.pm	2009-08-11 07:01:19 UTC (rev 7559)
+++ branches/shop/cgi-bin/LJ/Pay/Payment/PayItem/Addon/Sized.pm	2009-08-11 07:58:38 UTC (rev 7560)
@@ -233,7 +233,7 @@
 }
 
 sub can_be_added {
-    my ($self, $cartobj, $errs_ref) = @_;
+    my ($self, $cartobj, $errs_ref, $warn_ref) = @_;
 
     return 0 unless defined $LJ::Pay::Payment::PayItem::Addon::bonus{$self->{item}}->{items}->{$self->{subitem}}
         and defined $LJ::Pay::Payment::PayItem::Addon::bonus{$self->{item}}->{items}->{$self->{subitem}}->{qty}->{$self->{qty}};

Modified: branches/shop/cgi-bin/LJ/Pay/Payment/PayItem/Coppa.pm
===================================================================
--- branches/shop/cgi-bin/LJ/Pay/Payment/PayItem/Coppa.pm	2009-08-11 07:01:19 UTC (rev 7559)
+++ branches/shop/cgi-bin/LJ/Pay/Payment/PayItem/Coppa.pm	2009-08-11 07:58:38 UTC (rev 7560)
@@ -40,7 +40,7 @@
 }
 
 sub can_be_added {
-    my ($self, $cartobj, $errs_ref) = @_;
+    my ($self, $cartobj, $errs_ref, $warn_ref) = @_;
 
         # can't do coppa verifications except on $remote
 

Modified: branches/shop/cgi-bin/LJ/Pay/Payment/PayItem/Coupon.pm
===================================================================
--- branches/shop/cgi-bin/LJ/Pay/Payment/PayItem/Coupon.pm	2009-08-11 07:01:19 UTC (rev 7559)
+++ branches/shop/cgi-bin/LJ/Pay/Payment/PayItem/Coupon.pm	2009-08-11 07:58:38 UTC (rev 7560)
@@ -191,7 +191,7 @@
 }
 
 sub can_be_added {
-    my ($self, $cartobj, $errs_ref) = @_;
+    my ($self, $cartobj, $errs_ref, $warn_ref) = @_;
 
     return 1 if grep { $_ eq $qty } @LJ::Pay::Payment::PayItem::Coupon::coupon;
     return 0;

Modified: branches/shop/cgi-bin/LJ/Pay/Payment/PayItem/PaidAccount.pm
===================================================================
--- branches/shop/cgi-bin/LJ/Pay/Payment/PayItem/PaidAccount.pm	2009-08-11 07:01:19 UTC (rev 7559)
+++ branches/shop/cgi-bin/LJ/Pay/Payment/PayItem/PaidAccount.pm	2009-08-11 07:58:38 UTC (rev 7560)
@@ -341,7 +341,7 @@
 }
 
 sub can_be_added {
-    my ($self, $cartobj, $errs_ref) = @_;
+    my ($self, $cartobj, $errs_ref, $warn_ref) = @_;
 
     return 0 unless defined $LJ::Pay::Payment::PayItem::PaidAccount::account{$self->{'qty'}};
     return 0 unless $self->{'rcptid'};

Modified: branches/shop/cgi-bin/LJ/Pay/Payment/PayItem/Permanent.pm
===================================================================
--- branches/shop/cgi-bin/LJ/Pay/Payment/PayItem/Permanent.pm	2009-08-11 07:01:19 UTC (rev 7559)
+++ branches/shop/cgi-bin/LJ/Pay/Payment/PayItem/Permanent.pm	2009-08-11 07:58:38 UTC (rev 7560)
@@ -165,7 +165,7 @@
 }
 
 sub can_be_added {
-    my ($self, $cartobj, $errs_ref) = @_;
+    my ($self, $cartobj, $errs_ref, $warn_ref) = @_;
 
     return 0 unless LJ::conf_test($LJ::PERM_SALE);
 

Modified: branches/shop/cgi-bin/LJ/Pay/Payment/PayItem/Rename.pm
===================================================================
--- branches/shop/cgi-bin/LJ/Pay/Payment/PayItem/Rename.pm	2009-08-11 07:01:19 UTC (rev 7559)
+++ branches/shop/cgi-bin/LJ/Pay/Payment/PayItem/Rename.pm	2009-08-11 07:58:38 UTC (rev 7560)
@@ -76,7 +76,7 @@
 }
 
 sub can_be_added {
-    my ($self, $cartobj, $errs_ref) = @_;
+    my ($self, $cartobj, $errs_ref, $warn_ref) = @_;
 
     return 1;
 }

Modified: branches/shop/cgi-bin/LJ/Pay/Payment/PayItem/VGift.pm
===================================================================
--- branches/shop/cgi-bin/LJ/Pay/Payment/PayItem/VGift.pm	2009-08-11 07:01:19 UTC (rev 7559)
+++ branches/shop/cgi-bin/LJ/Pay/Payment/PayItem/VGift.pm	2009-08-11 07:58:38 UTC (rev 7560)
@@ -51,7 +51,7 @@
     my $self_class = ref $self || $self;
     $self = $self_class->SUPER::new_memonly(%args);
 
-    my $vg = LJ::Pay::ShopVGift->new (name => $args{subitem});
+    my $vg = LJ::Pay::ShopVGift->new(name => $args{subitem});
     $vg->inc if $args{amt} == 0;
 
     return $self;
@@ -104,7 +104,7 @@
 
     unless ($exist_giftid) {
         my $note  = $self->get_prop('vgift_note');
-        my $vg = LJ::Pay::ShopVGift->new($self->{subitem});
+        my $vg = LJ::Pay::ShopVGift->new(name => $self->{subitem});
 
         ##
         ## vgift_10 has id 249
@@ -249,7 +249,7 @@
 }
 
 sub can_be_added {
-    my ($self, $cartobj, $errs_ref) = @_;
+    my ($self, $cartobj, $errs_ref, $warn_ref) = @_;
 
     my $all_vgifts = LJ::Pay::ShopVGift->get_all_vgifts; # simple DB hashrefs, memcached e.t.c
     return 0 unless defined $all_vgifts->{by_name}->{$self->{subitem}};

Modified: branches/shop/cgi-bin/LJ/Pay/Payment/PayItem.pm
===================================================================
--- branches/shop/cgi-bin/LJ/Pay/Payment/PayItem.pm	2009-08-11 07:01:19 UTC (rev 7559)
+++ branches/shop/cgi-bin/LJ/Pay/Payment/PayItem.pm	2009-08-11 07:58:38 UTC (rev 7560)
@@ -795,11 +795,253 @@
     return (0, undef, undef); # not ok (may not mark item as 'done'), no mail message
 }
 
+sub need_paid             { return 0;  }
+sub having_account_levels { return (); }
+sub disallowed_account_levels { return (); }
+sub can_be_added_to_special_cases { return 0; } # base class method will work only on incorrect pay items, disallow them
+
 sub can_be_added {
-    my ($self, $cartobj, $errs_ref) = @_;
+    my ($self, $cartobj, $errs_ref, $warn_ref) = @_;
 
-    return 0; # no error text -> silent redirect to cart page
-        # it is ok because base class method will work only on incorrect pay items
+    return 0 unless $self->validate_user_input;
+
+    my $rcptuser = LJ::load_userid($self->{'rcptid'});
+    return 0 unless $rcptuser;
+
+    my $dbh = LJ::get_db_writer() or return 0;
+
+    my $dispname = $self->product_name("short");
+    my $acctname = LJ::LJcom::acct_name($rcptuser->caps, undef, { display => 'acctname' });
+    my $remote = LJ::get_remote();
+
+    # we will check account levels twice:
+    # first time for already installed levels - "in DB"
+    # second - for levels, carrying in the cart
+
+    # here is first check, for levels in DB
+
+    my @having = $self->having_account_levels;
+    foreach my $level (@having) {
+        if ($rcptuser->in_class($level)) {
+            $errs_ref->[0] = BML::ml('/pay/modify.bml.error.cantbuyitem_already_have', { 'item' => "<b>$dispname</b>", 'level' => $acctname } );
+            return 0;
+        }
+    }
+
+    my @disallowed = $self->disallowed_account_levels;
+    foreach my $level (@disallowed) {
+        if ($rcptuser->in_class($level)) {
+            $errs_ref->[0] = BML::ml('/pay/modify.bml.error.cantbuyitem_disallowed_acct_level', { 'item' => "<b>$dispname</b>", 'level' => $acctname } );
+            return 0;
+        }
+    }
+
+    # may be we will need this later, on accout levels checks
+    my $cart_bonus_months = $self->{'qty'};      # months of bonus feat. in cart
+    my $cart_paid_months = 0;                    # months of paidacct in cart
+    my $cart_bonus_start = $self->{'giveafter'}; # start of bonus feature in cart
+    my $cart_paid_start  = undef;                # start of paid account in cart
+    my $cart_perm_acct = 0;                      # can't buy bool bonus features for perms
+    if ($self->need_paid) {
+        # check to see if the user is trying to buy bonus features that extend
+        # past the expiration of their paid account... factor in any paid time
+        # currently in the cart as well.  if so, ask for confirmation
+
+        foreach my $it (@{$cartobj->{'items'}}) {
+            next unless $it->{'rcptid'} == $self->{'rcptid'};
+
+            my $ga = $it->{'giveafter'} || 0;
+
+            # bonus item
+            if ($it->{'item'} eq $self->{item}) {
+                $cart_bonus_months += $it->{'qty'};
+
+                # find which instance of this bonus feature takes effect first
+                if (! defined $cart_bonus_start ||
+                    $ga < $cart_bonus_start) {
+
+                    $cart_bonus_start = $ga;
+                }
+
+            # paid account
+            } elsif ($it->{'item'} eq "paidacct") {
+                $cart_paid_months += $it->{'qty'};
+
+                # find which paid account takes effect sooner
+                if (! defined $cart_paid_start ||
+                    $ga < $cart_paid_start) {
+
+                    $cart_paid_start = $ga;
+                }
+
+            } elsif ($it->{'item'} eq 'perm') {
+                $cart_perm_acct = 1;
+            }
+        }
+        $cart_paid_start ||= 0;
+
+        my $is_perm = ($cart_perm_acct || $rcptuser->in_class('perm'));
+
+        # return max of arguments, undef if none
+        my $max = sub { @_ ? (sort { $b <=> $a } @_)[0] : undef; };
+
+        my $db_paid_time = $is_perm ? $LJ::EndOfTime :
+            $dbh->selectrow_array("SELECT UNIX_TIMESTAMP(paiduntil) " .
+                                  "FROM paiduser WHERE userid=?",
+                                  undef, $self->{'rcptid'}) || 0;
+
+        # used in messages, $rcptuser is always defined here
+        my $utag = LJ::ljuser($rcptuser);
+
+        # no paid account at all
+        unless ($db_paid_time || $cart_paid_months) {
+            $errs_ref->[0] = BML::ml('.error.cantbuyextrasforfreeaccount', {'item' => "<b>$dispname</b>", 'user' => $utag});
+            return 0;
+        }
+
+        my $ga = $self->{'giveafter'} || 0;
+
+        # fix bonus giveafter to coincide with earliest paid account
+        if ($cart_paid_start > 0 &&
+            (! $ga && ! $db_paid_time || $ga > $db_paid_time && $ga < $cart_paid_start)) {
+
+            # can't adjust giveafter if recipient == remote
+            if ($remote && $remote->{'userid'} == $self->{'rcptid'}) {
+                $errs_ref->[0] = BML::ml('.error.paidwillexpire', {'item' => "<b>$dispname</b>"});
+                return 0;
+            }
+
+            if ($warn_ref) { # no acknoledgement phase yet...
+                # cart_paid_start is a localtime, convert it to gmtime in mysql format
+                # to post back to ourselves
+                $self->{'giveafter'} = LJ::mysql_time(Time::Local::timelocal(gmtime($cart_paid_start)));
+
+                # explain expiration
+                $$warn_ref = "<?h1 " . BML::ml('.deliverydateadjusted.header') . " h1?>" .
+                    "<?p " . BML::ml('.deliverydateadjusted.text1', {'item' => "<b>$dispname</b>", 'user' => $utag}) . " p?>" .
+                    "<?p " . BML::ml('.deliverydateadjusted.text2', {'item' => "<b>$dispname</b>", 'date' => "<i>$self->{'giveafter'} GMT</i>"}) . " p?>";
+
+                return 1; # no hard error -> need not silent redirect
+            }
+        }
+
+        # remove giveafter if no paid account in cart and it is after the
+        # expiration of their current paid account
+        if ($warn_ref && $db_paid_time > 0 && $ga > $db_paid_time &&
+            (! $cart_paid_months || $ga < $cart_paid_start)) { # no acknoledgement phase yet...
+
+            # get rid of giveafter date in memory, to be clean
+            $self->{'giveafter'} = 0;
+
+            # explain expiration
+            $$warn_ref = "<?h1 " . BML::ml('.deliverydatechangedtonow.header') . " h1?>" .
+                "<?p " . BML::ml('.deliverydatechangedtonow.text1', {'item' => "<b>$dispname</b>", 'user' => $utag}) . " p?>" .
+                "<?p " . BML::ml('.deliverydatechangedtonow.text2', {'item' => "<b>$dispname</b>"}) . " p?>";
+
+            return 1; # no hard error -> need not silent redirect
+        }
+
+        if ($warn_ref) { # no acknoledgement phase yet...
+            my $db_bonus_time =
+                $max->($dbh->selectrow_array("SELECT UNIX_TIMESTAMP(), " .
+                                             "UNIX_TIMESTAMP(expdate), " .
+                                             "UNIX_TIMESTAMP(NOW() + INTERVAL daysleft DAY) " .
+                                             "FROM paidexp WHERE userid=? AND item=?",
+                                             undef, $self->{'rcptid'}, $self->{item}));
+            $db_bonus_time ||= 0;
+
+            # the following check doesn't produce a warning in the case that there
+            # is paid time set to be applied at a later date, but the bonus feature
+            # being added is starting during the small amount of paid time still in
+            # the db which will expire before the delayed paid time will be applied.
+
+            #                    |--- won't catch if 2 months of bonus applied here
+            #                    V
+            # [DBTIME...[NOW] 2 DAYS] ... [NON-P-TIME] ... [2 MONTHS CART-P-TIME DELAYED]
+
+            # since this is just a warning mechanism, we won't kill ourselves trying
+            # to catch this situation
+
+
+            # we add $cart_paid_months to the $paid_base to determine if it is after
+            # when the bonus features will expire.  if so, no error.
+
+            my $paid_base  = $db_paid_time  || "NOW()";
+
+            # paid time in cart
+            if ($cart_paid_start && $cart_paid_months) {
+
+                # paid base is the max of when the cart paid time starts
+                # and when the db paid time ends, since cart paid time
+                # only applies once db paid time ends.
+
+                $paid_base = $max->($cart_paid_start, $db_paid_time);
+            }
+
+            # we add $cart_bonus_months to the $bonus_base to determine if it is after
+            # when the paid account will expire.  if so, error.
+            my $bonus_base = $db_bonus_time || "NOW()";
+
+            # bonus time in cart
+            if ($cart_bonus_start && $cart_bonus_months) {
+
+                # bonus base is the max of when the cart bonus time starts,
+                # and when the db bonus time ends, since the delivery date
+                # of the item currently being added is already figured in.
+
+                $bonus_base = $max->($cart_bonus_start, $db_bonus_time);
+            }
+
+            my $bonus_base_stamp = LJ::mysql_time($bonus_base);
+            my $paid_base_stamp  = LJ::mysql_time($paid_base);
+
+            # is bonus expiration before or on the same day as paid expiration?
+            my $allow = $dbh->selectrow_array
+                ("SELECT TO_DAYS(? + INTERVAL ? MONTH) <= TO_DAYS(? + INTERVAL ? MONTH)",
+                 undef, $bonus_base_stamp, $cart_bonus_months, $paid_base_stamp, $cart_paid_months);
+
+            unless ($allow) {
+
+                # explain expiration
+                $$warn_ref = "<?h1 " . BML::ml('.paidtimewillexpire.header') . " h1?>";
+                $$warn_ref .= "<?p " . BML::ml('.paidtimewillexpire.text1', {'item' => "<b>$dispname</b>", 'user' => $utag}) . " p?>";
+
+                if ($db_paid_time && $self->{'rcptid'} == $remote->{'rcptid'}) {
+                    $$warn_ref .= "<p>" . BML::ml('.paidtimewillexpire.text2_expires', {'user' => $utag,
+                                                    'date' => "<i>" . LJ::time_to_http($db_paid_time) . "</i>", 
+                                                    'item' => "<b>$dispname</b>"}) . "</p>";
+                } else {
+                    $$warn_ref .= "<p>" . BML::ml('.paidtimewillexpire.text2', {'user' => $utag, 'item' => "<b>$dispname</b>"}) . "</p>";
+                }
+
+                return 1; # no hard error -> need not silent redirect
+            }
+        }
+    }
+
+    # second check on levels - only cart paid and perm
+    # here we will give other error texts
+=comment Waiting spec from Sergey on paiduntil-giveafter analysis
+    # key is caps bit 'class'
+    my %bought_level = ( 'paid' => 
+
+    #my @having = $self->having_account_levels;
+    foreach my $level (@having) {
+        if ($rcptuser->in_class($level)) {
+            $errs_ref->[0] = BML::ml('/pay/modify.bml.error.cantbuyitem_already_have', { 'item' => "<b>$dispname</b>", 'level' => $acctname } );
+            return 0;
+        }
+    }
+
+    #my @disallowed = $self->disallowed_account_levels;
+    foreach my $level (@disallowed) {
+        if ($rcptuser->in_class($level)) {
+            $errs_ref->[0] = BML::ml('/pay/modify.bml.error.cantbuyitem_disallowed_acct_level', { 'item' => "<b>$dispname</b>", 'level' => $acctname } );
+            return 0;
+        }
+    }
+=cut
+    return $self->can_be_added_to_special_cases($cartobj, $errs_ref, $warn_ref);
 }
 
 # fill 'amt', and, may be, 'qty' field

Modified: branches/shop/cgi-bin/LJ/VGift.pm
===================================================================
--- branches/shop/cgi-bin/LJ/VGift.pm	2009-08-11 07:01:19 UTC (rev 7559)
+++ branches/shop/cgi-bin/LJ/VGift.pm	2009-08-11 07:58:38 UTC (rev 7560)
@@ -276,7 +276,7 @@
     my $all_vgifts = LJ::Pay::ShopVGift::get_all_vgifts();
     while (my $gift = $sth->fetchrow_hashref) {
         my $code = $all_vgifts->{by_id}->{$gift->{gifttype}}         or next;
-        my $cost = LJ::Pay::ShopVGift->new(id => $code)->price      or next;
+        my $cost = $code->price      or next;
         $sum += $cost;
 
         push @gifts, $gift;

Modified: branches/shop/htdocs/pay/modify.bml
===================================================================
--- branches/shop/htdocs/pay/modify.bml	2009-08-11 07:01:19 UTC (rev 7559)
+++ branches/shop/htdocs/pay/modify.bml	2009-08-11 07:58:38 UTC (rev 7560)
@@ -742,236 +742,38 @@
         # even more, we do not allow vgifts to have giveafter for remoteid == rcpt_id
 
         my @errs;
-        my $can_be_added = $item->can_be_added($cartobj, \@errs);
+        my $warn;
+        my $warn_ref = $POST{'action:additem:confirm'} ? undef : \$warn; # user have seen warning already? skip it now...
+        my $can_be_added = $item->can_be_added($cartobj, \@errs, $warn_ref);
         return BML::redirect("/pay/?c=$cart") if not $can_be_added and not @errs; # nothing to say? draw the cart silently
         return $err->(@errs) unless $can_be_added;
 
         return $err->($ML{'.error.cantspecifydateforself'})
             if $remote && $item->{'rcptid'} == $remote->{'userid'} && $POST{'giveafter_yyyy'};
 
-        my $err_msg = $item->set_giveafter($POST{giveafter_yyyy}, $POST{giveafter_mm}, $POST{giveafter_dd}, $rcptuser->timezone);    
-        return $err->($err_msg) if $err_msg;
+        if ($POST{'giveafter'} =~ /^\d+$/) {
+            $item->{'giveafter'} = $POST{'giveafter'} + 0;
+        } else {
+            my $err_msg = $item->set_giveafter($POST{giveafter_yyyy}, $POST{giveafter_mm}, $POST{giveafter_dd}, $rcptuser->timezone);    
+            return $err->($err_msg) if $err_msg;
+        }
 
-        # buying bonus-feature, lots of extra checks
-        if (LJ::Pay::is_bonus($itemname) && ! $POST{'action:additem:confirm'}) {
+        if ($warn and !$POST{'action:additem:confirm'}) {
 
-            # check to see if the user is trying to buy bonus features that extend
-            # past the expiration of their paid account... factor in any paid time
-            # currently in the cart as well.  if so, ask for confirmation
+            $POST{'giveafter'} = $item->{'giveafter'};
 
-            my $cart_bonus_months = $item->{'qty'};      # months of bonus feat. in cart
-            my $cart_paid_months = 0;                    # months of paidacct in cart
-            my $cart_bonus_start = $item->{'giveafter'}; # start of bonus feature in cart
-            my $cart_paid_start  = undef;                # start of paid account in cart
-            my $cart_perm_acct = 0;                      # can't buy bool bonus features for perms
+            # pass on post variables
+            $body .= "<form method='post' action='modify.bml'>";
+            $body .= LJ::html_hidden( map { $_ => $POST{$_} }
+                                      qw(otheruser paymeth action:additem for item cart
+                                         giveafter giveafter_yyyy giveafter_mm giveafter_dd anongift) );
 
-            foreach my $it (@{$cartobj->{'items'}}) {
-                next unless $it->{'rcptid'} == $item->{'rcptid'};
+            # explain expiration
+            $body .= $warn;
+            $body .= "<p align='center'>" . LJ::html_submit('action:additem:confirm', $ML{'.btn.acknowledged'}) . "</p>";
+            $body .= "</form>";
 
-                my $ga = $it->{'giveafter'} || 0;
-
-                # bonus item
-                if ($it->{'item'} eq $itemname) {
-                    $cart_bonus_months += $it->{'qty'};
-
-                    # find which instance of this bonus feature takes effect first
-                    if (! defined $cart_bonus_start ||
-                        $ga < $cart_bonus_start) {
-
-                        $cart_bonus_start = $ga;
-                    }
-
-                # paid account
-                } elsif ($it->{'item'} eq "paidacct") {
-                    $cart_paid_months += $it->{'qty'};
-
-                    # find which paid account takes effect sooner
-                    if (! defined $cart_paid_start ||
-                        $ga < $cart_paid_start) {
-
-                        $cart_paid_start = $ga;
-                    }
-
-                } elsif ($it->{'item'} eq 'perm') {
-                    $cart_perm_acct = 1;
-                }
-            }
-            $cart_paid_start ||= 0;
-
-            my $is_perm = ($cart_perm_acct || $rcptuser->in_class('perm'));
-
-            # give error if trying to buy bonus feature for permanent account
-            # -- but we do allow buying disk quota for permanent accounts
-            if (LJ::Pay::is_bonus($itemname, 'bool') &&  $is_perm) {
-
-                my $itemobj = LJ::Pay::Payment::PayItem->new_memonly(item => $item);
-                my $dispname = $itemobj->product_name("short");
-                return $err->(BML::ml('.error.cantbuyitemforperm', {'item' => "<b>$dispname</b>"}));
-            }
-
-            # return max of arguments, undef if none
-            my $max = sub { @_ ? (sort { $b <=> $a } @_)[0] : undef; };
-
-
-            my $db_paid_time = $is_perm ? $LJ::EndOfTime :
-                $dbh->selectrow_array("SELECT UNIX_TIMESTAMP(paiduntil) " .
-                                      "FROM paiduser WHERE userid=?",
-                                      undef, $item->{'rcptid'}) || 0;
-
-            # used in messages, $rcptuser is always defined here
-            my $utag = LJ::ljuser($rcptuser);
-
-            # no paid account at all
-            my $itemobj = LJ::Pay::Payment::PayItem->new_memonly(item => $item);
-            my $dispname = $itemobj->product_name("short");
-            unless ($db_paid_time || $cart_paid_months) {
-                return $err->(BML::ml('.error.cantbuyextrasforfreeaccount', {'item' => "<b>$dispname</b>", 'user' => $utag}));
-            }
-
-            my $ga = $item->{'giveafter'} || 0;
-
-            # fix bonus giveafter to coincide with earliest paid account
-            if ($cart_paid_start > 0 &&
-                (! $ga && ! $db_paid_time || $ga > $db_paid_time && $ga < $cart_paid_start)) {
-
-                # can't adjust giveafter if recipient == remote
-                if ($remote && $remote->{'userid'} == $item->{'rcptid'}) {
-                    return $err->(BML::ml('.error.paidwillexpire', {'item' => "<b>$dispname</b>"}));
-                }
-
-                # cart_paid_start is a localtime, convert it to gmtime in mysql format
-                # to post back to ourselves
-                $item->{'giveafter'} = LJ::mysql_time(Time::Local::timelocal(gmtime($cart_paid_start)));
-
-                $POST{'giveafter'} = $item->{'giveafter'};
-
-                # pass on post variables
-                $body .= "<form method='post' action='modify.bml'>";
-                $body .= LJ::html_hidden( map { $_ => $POST{$_} }
-                                          qw(redemail otheruser paymeth action:additem for item cart
-                                             giveafter giveafter_yyyy giveafter_mm giveafter_dd anongift) );
-
-                # explain expiration
-                my $utag = LJ::ljuser(LJ::load_userid($item->{'rcptid'}));
-                $body .= "<?h1 $ML{'.deliverydateadjusted.header'} h1?>" .
-                    "<?p " . BML::ml('.deliverydateadjusted.text1', {'item' => "<b>$dispname</b>", 'user' => $utag}) . " p?>" .
-                    "<?p " . BML::ml('.deliverydateadjusted.text2', {'item' => "<b>$dispname</b>", 'date' => "<i>$item->{'giveafter'} GMT</i>"}) . " p?>";
-
-                $body .= "<p align='center'>" . LJ::html_submit('action:additem:confirm', $ML{'.btn.acknowledged'}) . "</p>";
-                $body .= "</form>";
-
-                return;
-            }
-
-            # remove giveafter if no paid account in cart and it is after the
-            # expiration of their current paid account
-            if ($db_paid_time > 0 && $ga > $db_paid_time &&
-                (! $cart_paid_months || $ga < $cart_paid_start)) {
-
-                # get rid of giveafter date in memory, to be clean
-                delete $item->{'giveafter'};
-                delete $POST{'giveafter'};
-
-                # pass on post variables
-                $body .= "<form method='post' action='modify.bml'>";
-                $body .= LJ::html_hidden( map { $_ => $POST{$_} }
-                                          qw(redemail otheruser paymeth action:additem
-                                             for item cart anongift) );
-
-                # explain expiration
-                my $utag = LJ::ljuser(LJ::load_userid($item->{'rcptid'}));
-                $body .= "<?h1 $ML{'.deliverydatechangedtonow.header'} h1?>" .
-                    "<?p " . BML::ml('.deliverydatechangedtonow.text1', {'item' => "<b>$dispname</b>", 'user' => $utag}) . " p?>" .
-                    "<?p " . BML::ml('.deliverydatechangedtonow.text2', {'item' => "<b>$dispname</b>"}) . " p?>";
-
-                $body .= "<p align='center'>" . LJ::html_submit('action:additem:confirm', $ML{'.btn.acknowledged'}) . "</p>";
-                $body .= "</form>";
-
-                return;
-            }
-
-            my $db_bonus_time =
-                $max->($dbh->selectrow_array("SELECT UNIX_TIMESTAMP(), " .
-                                             "UNIX_TIMESTAMP(expdate), " .
-                                             "UNIX_TIMESTAMP(NOW() + INTERVAL daysleft DAY) " .
-                                             "FROM paidexp WHERE userid=? AND item=?",
-                                             undef, $item->{'rcptid'}, $itemname));
-            $db_bonus_time ||= 0;
-
-            # the following check doesn't produce a warning in the case that there
-            # is paid time set to be applied at a later date, but the bonus feature
-            # being added is starting during the small amount of paid time still in
-            # the db which will expire before the delayed paid time will be applied.
-
-            #                    |--- won't catch if 2 months of bonus applied here
-            #                    V
-            # [DBTIME...[NOW] 2 DAYS] ... [NON-P-TIME] ... [2 MONTHS CART-P-TIME DELAYED]
-
-            # since this is just a warning mechanism, we won't kill ourselves trying
-            # to catch this situation
-
-
-            # we add $cart_paid_months to the $paid_base to determine if it is after
-            # when the bonus features will expire.  if so, no error.
-
-            my $paid_base  = $db_paid_time  || "NOW()";
-
-            # paid time in cart
-            if ($cart_paid_start && $cart_paid_months) {
-
-                # paid base is the max of when the cart paid time starts
-                # and when the db paid time ends, since cart paid time
-                # only applies once db paid time ends.
-
-                $paid_base = $max->($cart_paid_start, $db_paid_time);
-            }
-
-            # we add $cart_bonus_months to the $bonus_base to determine if it is after
-            # when the paid account will expire.  if so, error.
-            my $bonus_base = $db_bonus_time || "NOW()";
-
-            # bonus time in cart
-            if ($cart_bonus_start && $cart_bonus_months) {
-
-                # bonus base is the max of when the cart bonus time starts,
-                # and when the db bonus time ends, since the delivery date
-                # of the item currently being added is already figured in.
-
-                $bonus_base = $max->($cart_bonus_start, $db_bonus_time);
-            }
-
-            my $bonus_base_stamp = LJ::mysql_time($bonus_base);
-            my $paid_base_stamp  = LJ::mysql_time($paid_base);
-
-            # is bonus expiration before or on the same day as paid expiration?
-            my $allow = $dbh->selectrow_array
-                ("SELECT TO_DAYS(? + INTERVAL ? MONTH) <= TO_DAYS(? + INTERVAL ? MONTH)",
-                 undef, $bonus_base_stamp, $cart_bonus_months, $paid_base_stamp, $cart_paid_months);
-
-            unless ($allow) {
-
-                # pass on post variables
-                $body .= "<form method='post' action='modify.bml'>";
-                $body .= LJ::html_hidden( map { $_ => $POST{$_} }
-                                          qw(redemail otheruser paymeth action:additem cart anongift
-                                             for item giveafter giveafter_yyyy giveafter_mm giveafter_dd) );
-
-                # explain expiration
-                my $utag = LJ::ljuser(LJ::load_userid($item->{'rcptid'}));
-                $body .= "<?h1 $ML{'.paidtimewillexpire.header'} h1?>";
-                $body .= "<?p " . BML::ml('.paidtimewillexpire.text1', {'item' => "<b>$dispname</b>", 'user' => $utag}) . " p?>";
-
-                if ($db_paid_time && $item->{'rcptid'} == $remote->{'rcptid'}) {
-                    $body .= "<p>" . BML::ml('.paidtimewillexpire.text2_expires', {'user' => $utag, 'date' => "<i>" . LJ::time_to_http($db_paid_time) . "</i>", 'item' => "<b>$dispname</b>"}) . "</p>";
-                } else {
-                    $body .= "<p>" . BML::ml('.paidtimewillexpire.text2', {'user' => $utag, 'item' => "<b>$dispname</b>"}) . "</p>";
-                }
-
-                $body .= "<p align='center'>" . LJ::html_submit('action:additem:confirm', $ML{'.btn.acknowledged'}) . "</p>";
-                $body .= "</form>";
-
-                return;
-            }
+            return;
         }
 
         # add the item

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