1 package BSE::TB::Subscription;
5 @ISA = qw/Squirrel::Row/;
7 our $VERSION = "1.001";
10 return qw/subscription_id text_id title description max_lapsed/;
13 sub primary { 'subscription_id' }
15 # call as a method for edits
17 my ($self, $cfg) = @_;
19 my @subs = BSE::TB::Subscriptions->all;
21 @subs = grep $_->{subscription_id} != $self->{subscription_id}, @subs;
23 my $notsubid_match = join '|', map $_->{text_id}, @subs;
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' },
37 text_id => { description=>"Identifier",
38 rules=>'required;identifier;notsubid' },
39 title => { description=>"Title",
41 description => { description=>"Description" },
42 max_lapsed => { description => 'Max lapsed',
43 rules => 'required;natural', },
48 my ($self, $rmsg) = @_;
50 # can only remove if no products use it and no existing orders refer
52 if ($self->product_count) {
53 $$rmsg = "There are products using this subscription, it cannot be deleted"
57 if ($self->order_item_count) {
58 $$rmsg = "There are orders that include this subscription, it cannot be deleted"
69 my ($row) = BSE::DB->query(subscriptionProductCount =>
70 $self->{subscription_id}, $self->{subscription_id});
75 sub order_item_count {
78 my ($row) = BSE::DB->query(subscriptionOrderItemCount =>
79 $self->{subscription_id});
84 sub dependent_products {
87 require BSE::TB::Products;
88 BSE::TB::Products->getSpecial(subscriptionDependent => $self->{subscription_id},
89 $self->{subscription_id});
95 BSE::DB->query(subscriptionOrderSummary=>$self->{subscription_id});
98 sub subscribed_user_summary {
101 BSE::DB->query(subscriptionUserSummary => $self->{subscription_id});
104 my @expiry_cols = qw(subscription_id siteuser_id started_at ends_at);
106 sub update_user_expiry {
107 my ($self, $user, $cfg) = @_;
109 my $debug = $cfg->entry('debug', 'subscription_expiry', 0);
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});
117 require BSE::TB::Subscription::Calc;
119 my @periods = BSE::TB::Subscription::Calc->calculate_period
122 my $period = $periods[-1];
125 BSE::DB->run(removeUserSubscribed => $self->{subscription_id},
129 BSE::DB->run(addUserSubscribed => $self->{subscription_id},
130 $user->{id}, $period->{start}, $period->{end},
131 $period->{max_lapsed});
134 # user not subscribed in any way
135 BSE::DB->run(removeUserSubscribed => $self->{subscription_id},