]> git.imager.perl.org - bse.git/blob - site/cgi-bin/modules/BSE/TB/Subscription.pm
add support for storing custom metadata to cart/order items and options
[bse.git] / site / cgi-bin / modules / BSE / TB / Subscription.pm
1 package BSE::TB::Subscription;
2 use strict;
3 use Squirrel::Row;
4 use vars qw/@ISA/;
5 @ISA = qw/Squirrel::Row/;
6
7 our $VERSION = "1.001";
8
9 sub columns {
10   return qw/subscription_id text_id title description max_lapsed/;
11 }
12
13 sub primary { 'subscription_id' }
14
15 # call as a method for edits
16 sub valid_rules {
17   my ($self, $cfg) = @_;
18
19   my @subs = BSE::TB::Subscriptions->all;
20   if (ref $self) {
21     @subs = grep $_->{subscription_id} != $self->{subscription_id}, @subs;
22   }
23   my $notsubid_match = join '|', map $_->{text_id}, @subs;
24
25   return
26     (
27      identifier => { match => qr/^\w+$/,
28                    error => '$n must contain only letters and digits, and no spaces' },
29      notsubid => { nomatch => qr/^(?:$notsubid_match)$/,
30                  error => 'Duplicate identifier' },
31     );
32 }
33
34 sub valid_fields {
35   return
36     (
37      text_id => { description=>"Identifier", 
38                   rules=>'required;identifier;notsubid' },
39      title => { description=>"Title",
40                 required => 1 },
41      description => { description=>"Description" },
42      max_lapsed => { description => 'Max lapsed', 
43                      rules => 'required;natural', },
44     );
45 }
46
47 sub is_removable {
48   my ($self, $rmsg) = @_;
49
50   # can only remove if no products use it and no existing orders refer
51   # to it
52   if ($self->product_count) {
53     $$rmsg = "There are products using this subscription, it cannot be deleted"
54       if $rmsg;
55     return;
56   }
57   if ($self->order_item_count) {
58     $$rmsg = "There are orders that include this subscription, it cannot be deleted"
59       if $rmsg;
60     return;
61   }
62
63   return 1;
64 }
65
66 sub product_count {
67   my ($self) = @_;
68
69   my ($row) = BSE::DB->query(subscriptionProductCount => 
70                              $self->{subscription_id}, $self->{subscription_id});
71
72   return $row->{count};
73 }
74
75 sub order_item_count {
76   my ($self) = @_;
77
78   my ($row) = BSE::DB->query(subscriptionOrderItemCount => 
79                              $self->{subscription_id});
80
81   return $row->{count};
82 }
83
84 sub dependent_products {
85   my ($self) = @_;
86
87   require BSE::TB::Products;
88   BSE::TB::Products->getSpecial(subscriptionDependent => $self->{subscription_id}, 
89                        $self->{subscription_id});
90 }
91
92 sub order_summary {
93   my ($self) = @_;
94
95   BSE::DB->query(subscriptionOrderSummary=>$self->{subscription_id});
96 }
97
98 sub subscribed_user_summary {
99   my ($self) = @_;
100
101   BSE::DB->query(subscriptionUserSummary => $self->{subscription_id});
102 }
103
104 my @expiry_cols = qw(subscription_id siteuser_id started_at ends_at);
105
106 sub update_user_expiry {
107   my ($self, $user, $cfg) = @_;
108
109   my $debug = $cfg->entry('debug', 'subscription_expiry', 0);
110
111   # gather the orders/items this user has bought for this sub
112   my @sub_info = sort { $a->{orderDate} cmp $b->{orderDate} }
113     BSE::DB->query(subscriptionUserBought => 
114                    $self->{subscription_id}, $user->{id});
115
116   if (@sub_info) {
117     require BSE::TB::Subscription::Calc;
118
119     my @periods = BSE::TB::Subscription::Calc->calculate_period
120       ($debug, @sub_info);
121
122     my $period = $periods[-1];
123
124     # remove the old one
125     BSE::DB->run(removeUserSubscribed => $self->{subscription_id}, 
126                  $user->{id});
127
128     # put it back
129     BSE::DB->run(addUserSubscribed =>  $self->{subscription_id}, 
130                  $user->{id}, $period->{start}, $period->{end},
131                  $period->{max_lapsed});
132   }
133   else {
134     # user not subscribed in any way
135     BSE::DB->run(removeUserSubscribed => $self->{subscription_id}, 
136                  $user->{id});
137   }
138 }
139
140 1;