Bug 7143: Follow up patch for Updating about.tt
[koha.git] / serials / subscription-detail.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 2 of the License, or (at your option) any later
8 # version.
9 #
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License along
15 # with Koha; if not, write to the Free Software Foundation, Inc.,
16 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
18 use strict;
19 use warnings;
20 use CGI;
21 use C4::Auth;
22 use C4::Koha;
23 use C4::Dates qw/format_date/;
24 use C4::Serials;
25 use C4::Output;
26 use C4::Context;
27 use Date::Calc qw/Today Day_of_Year Week_of_Year Add_Delta_Days/;
28 use Carp;
29
30 my $query = new CGI;
31 my $op = $query->param('op') || q{};
32 my $issueconfirmed = $query->param('issueconfirmed');
33 my $dbh = C4::Context->dbh;
34 my ($template, $loggedinuser, $cookie, $hemisphere);
35 my $subscriptionid = $query->param('subscriptionid');
36 my $subs = GetSubscription($subscriptionid);
37
38 $subs->{enddate} = GetExpirationDate($subscriptionid);
39
40 if ($op && $op eq 'del') {
41         if ($subs->{'cannotedit'}){
42                 carp "Attempt to delete subscription $subscriptionid by ".C4::Context->userenv->{'id'}." not allowed";
43                 print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");
44         }
45         DelSubscription($subscriptionid);
46         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=serials-home.pl\"></html>";
47         exit;
48 }
49 my ($routing, @routinglist) = getroutinglist($subscriptionid);
50 my ($totalissues,@serialslist) = GetSerials($subscriptionid);
51 $totalissues-- if $totalissues; # the -1 is to have 0 if this is a new subscription (only 1 issue)
52 # the subscription must be deletable if there is NO issues for a reason or another (should not happend, but...)
53
54 # Permission needed if it is a deletion (del) : delete_subscription
55 # Permission needed otherwise : *
56 my $permission = ($op eq "del") ? "delete_subscription" : "*";
57
58 ($template, $loggedinuser, $cookie)
59 = get_template_and_user({template_name => "serials/subscription-detail.tmpl",
60                 query => $query,
61                 type => "intranet",
62                 authnotrequired => 0,
63                 flagsrequired => {serials => $permission},
64                 debug => 1,
65                 });
66
67 $$subs{enddate} ||= GetExpirationDate($subscriptionid);
68
69 if ($op eq 'del') {
70         if ($$subs{'cannotedit'}){
71                 carp "Attempt to delete subscription $subscriptionid by ".C4::Context->userenv->{'id'}." not allowed";
72                 print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");
73                 exit;
74         }
75         
76     # Asking for confirmation if the subscription has not strictly expired yet or if it has linked issues
77     my $strictlyexpired = HasSubscriptionStrictlyExpired($subscriptionid);
78     my $linkedissues = CountIssues($subscriptionid);
79     my $countitems   = HasItems($subscriptionid);
80     if ($strictlyexpired == 0 || $linkedissues > 0 || $countitems>0) {
81                 $template->param(NEEDSCONFIRMATION => 1);
82                 if ($strictlyexpired == 0) { $template->param("NOTEXPIRED" => 1); }
83                 if ($linkedissues     > 0) { $template->param("LINKEDISSUES" => 1); }
84                 if ($countitems       > 0) { $template->param("LINKEDITEMS"  => 1); }
85     } else {
86                 $issueconfirmed = "1";
87     }
88     # If it's ok to delete the subscription, we do so
89     if ($issueconfirmed eq "1") {
90                 &DelSubscription($subscriptionid);
91                 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=serials-home.pl\"></html>";
92                 exit;
93     }
94 }
95 my $hasRouting = check_routing($subscriptionid);
96
97 my ($user, $sessionID, $flags);
98 ($user, $cookie, $sessionID, $flags)
99     = checkauth($query, 0, {catalogue => 1}, "intranet");
100
101 # COMMENT hdl : IMHO, we should think about passing more and more data hash to template->param rather than duplicating code a new coding Guideline ?
102
103 for my $date qw(startdate enddate firstacquidate histstartdate histenddate){
104     $$subs{$date}      = format_date($$subs{$date}) if $date && $$subs{$date};
105 }
106 $subs->{location} = GetKohaAuthorisedValueLib("LOC",$subs->{location});
107 $subs->{abouttoexpire}  = abouttoexpire($subs->{subscriptionid});
108 $template->param(%{ $subs });
109 $template->param(biblionumber_for_new_subscription => $subs->{bibnum});
110 my @irregular_issues = split /,/, $subs->{irregularity};
111
112 if (! $subs->{numberpattern}) {
113     $subs->{numberpattern} = q{};
114 }
115 if (! $subs->{dow}) {
116     $subs->{dow} = q{};
117 }
118 if (! $subs->{periodicity}) {
119     $subs->{periodicity} = '0';
120 }
121 my $default_bib_view = get_default_view();
122 $template->param(
123         subscriptionid => $subscriptionid,
124     serialslist => \@serialslist,
125     hasRouting  => $hasRouting,
126     totalissues => $totalissues,
127     hemisphere => $hemisphere,
128     cannotedit =>(C4::Context->preference('IndependantBranches') &&
129                 C4::Context->userenv &&
130                 C4::Context->userenv->{flags} % 2 !=1  &&
131                 C4::Context->userenv->{branch} && $subs->{branchcode} &&
132                 (C4::Context->userenv->{branch} ne $subs->{branchcode})),
133     'periodicity' . $subs->{periodicity} => 1,
134     'arrival' . $subs->{dow} => 1,
135     'numberpattern' . $subs->{numberpattern} => 1,
136     intranetstylesheet => C4::Context->preference('intranetstylesheet'),
137     intranetcolorstylesheet => C4::Context->preference('intranetcolorstylesheet'),
138     irregular_issues => scalar @irregular_issues,
139     default_bib_view => $default_bib_view,
140     );
141
142 output_html_with_http_headers $query, $cookie, $template->output;
143
144 sub get_default_view {
145     my $defaultview = C4::Context->preference('IntranetBiblioDefaultView');
146     my $views = { C4::Search::enabled_staff_search_views };
147     if ($defaultview eq 'isbd' && $views->{can_view_ISBD}) {
148         return 'ISBDdetail';
149     } elsif  ($defaultview eq 'marc' && $views->{can_view_MARC}) {
150         return 'MARCdetail';
151     } elsif  ($defaultview eq 'labeled_marc' && $views->{can_view_labeledMARC}) {
152         return 'labeledMARCdetail';
153     } else {
154         return 'detail';
155     }
156 }