+=head2 CloseSubscription
+Close a subscription given a subscriptionid
+=cut
+sub CloseSubscription {
+ my ( $subscriptionid ) = @_;
+ return unless $subscriptionid;
+ my $dbh = C4::Context->dbh;
+ my $sth = $dbh->prepare( qq{
+ UPDATE subscription
+ SET closed = 1
+ WHERE subscriptionid = ?
+ } );
+ $sth->execute( $subscriptionid );
+
+ # Set status = missing when status = stopped
+ $sth = $dbh->prepare( qq{
+ UPDATE serial
+ SET status = 8
+ WHERE subscriptionid = ?
+ AND status = 1
+ } );
+ $sth->execute( $subscriptionid );
+}
+
+=head2 ReopenSubscription
+Reopen a subscription given a subscriptionid
+=cut
+sub ReopenSubscription {
+ my ( $subscriptionid ) = @_;
+ return unless $subscriptionid;
+ my $dbh = C4::Context->dbh;
+ my $sth = $dbh->prepare( qq{
+ UPDATE subscription
+ SET closed = 0
+ WHERE subscriptionid = ?
+ } );
+ $sth->execute( $subscriptionid );
+
+ # Set status = expected when status = stopped
+ $sth = $dbh->prepare( qq{
+ UPDATE serial
+ SET status = 1
+ WHERE subscriptionid = ?
+ AND status = 8
+ } );
+ $sth->execute( $subscriptionid );
+}
+
+=head2 subscriptionCurrentlyOnOrder
+
+ $bool = subscriptionCurrentlyOnOrder( $subscriptionid );
+
+Return 1 if subscription is currently on order else 0.
+
+=cut
+
+sub subscriptionCurrentlyOnOrder {
+ my ( $subscriptionid ) = @_;
+ my $dbh = C4::Context->dbh;
+ my $query = qq|
+ SELECT COUNT(*) FROM aqorders
+ WHERE subscriptionid = ?
+ AND datereceived IS NULL
+ AND datecancellationprinted IS NULL
+ |;
+ my $sth = $dbh->prepare( $query );
+ $sth->execute($subscriptionid);
+ return $sth->fetchrow_array;
+}
+
+=head2 can_edit_subscription
+
+ $can = can_edit_subscription( $subscriptionid[, $userid] );
+
+Return 1 if the subscription is editable by the current logged user (or a given $userid), else 0.
+
+=cut
+
+sub can_edit_subscription {
+ my ( $subscription, $userid ) = @_;
+ return 0 unless C4::Context->userenv;
+ my $flags = C4::Context->userenv->{flags};
+ $userid ||= C4::Context->userenv->{'id'};
+ my $independent_branches = C4::Context->preference('IndependentBranches');
+ return 1 unless $independent_branches;
+ if( C4::Context->IsSuperLibrarian()
+ or C4::Auth::haspermission( $userid, {serials => 'superserials'}),
+ or C4::Auth::haspermission( $userid, {serials => 'edit_subscription'}),
+ or not defined $subscription->{branchcode}
+ or $subscription->{branchcode} eq ''
+ or $subscription->{branchcode} eq C4::Context->userenv->{'branch'}
+ ) {
+ return 1;
+ }
+ return 0;
+}
+