Fix FSF address in directory basket/
[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 with
15 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
16 # Suite 330, Boston, MA  02111-1307 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 ($template, $loggedinuser, $cookie)
39 = get_template_and_user({template_name => "serials/subscription-detail.tmpl",
40                 query => $query,
41                 type => "intranet",
42                 authnotrequired => 0,
43                 flagsrequired => {serials => 1},
44                 debug => 1,
45                 });
46
47 $$subs{enddate} ||= GetExpirationDate($subscriptionid);
48
49 if ($op eq 'del') {
50         if ($$subs{'cannotedit'}){
51                 carp "Attempt to delete subscription $subscriptionid by ".C4::Context->userenv->{'id'}." not allowed";
52                 print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");
53                 exit;
54         }
55         
56     # Asking for confirmation if the subscription has not strictly expired yet or if it has linked issues
57     my $strictlyexpired = HasSubscriptionStrictlyExpired($subscriptionid);
58     my $linkedissues = CountIssues($subscriptionid);
59     my $countitems   = HasItems($subscriptionid);
60     if ($strictlyexpired == 0 || $linkedissues > 0 || $countitems>0) {
61                 $template->param(NEEDSCONFIRMATION => 1);
62                 if ($strictlyexpired == 0) { $template->param("NOTEXPIRED" => 1); }
63                 if ($linkedissues     > 0) { $template->param("LINKEDISSUES" => 1); }
64                 if ($countitems       > 0) { $template->param("LINKEDITEMS"  => 1); }
65     } else {
66                 $issueconfirmed = "1";
67     }
68     # If it's ok to delete the subscription, we do so
69     if ($issueconfirmed eq "1") {
70                 &DelSubscription($subscriptionid);
71                 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=serials-home.pl\"></html>";
72                 exit;
73     }
74 }
75 my $hasRouting = check_routing($subscriptionid);
76 my ($totalissues,@serialslist) = GetSerials($subscriptionid);
77 $totalissues-- if $totalissues; # the -1 is to have 0 if this is a new subscription (only 1 issue)
78 # the subscription must be deletable if there is NO issues for a reason or another (should not happend, but...)
79
80 my ($user, $sessionID, $flags);
81 ($user, $cookie, $sessionID, $flags)
82     = checkauth($query, 0, {catalogue => 1}, "intranet");
83
84 # COMMENT hdl : IMHO, we should think about passing more and more data hash to template->param rather than duplicating code a new coding Guideline ?
85
86 for my $date qw(startdate enddate firstacquidate histstartdate histenddate){
87     $$subs{$date}      = format_date($$subs{$date}) if $date && $$subs{$date};
88 }
89 $subs->{abouttoexpire}  = abouttoexpire($subs->{subscriptionid});
90
91 $template->param($subs);
92 $template->param(biblionumber_for_new_subscription => $subs->{bibnum});
93 my @irregular_issues = split /,/, $subs->{irregularity};
94
95 if (! $subs->{numberpattern}) {
96     $subs->{numberpattern} = q{};
97 }
98 if (! $subs->{dow}) {
99     $subs->{dow} = q{};
100 }
101 if (! $subs->{periodicity}) {
102     $subs->{periodicity} = '0';
103 }
104 $template->param(
105         subscriptionid => $subscriptionid,
106     serialslist => \@serialslist,
107     hasRouting  => $hasRouting,
108     totalissues => $totalissues,
109     hemisphere => $hemisphere,
110     cannotedit =>(C4::Context->preference('IndependantBranches') &&
111                 C4::Context->userenv &&
112                 C4::Context->userenv->{flags} % 2 !=1  &&
113                 C4::Context->userenv->{branch} && $subs->{branchcode} &&
114                 (C4::Context->userenv->{branch} ne $subs->{branchcode})),
115     'periodicity' . $subs->{periodicity} => 1,
116     'arrival' . $subs->{dow} => 1,
117     'numberpattern' . $subs->{numberpattern} => 1,
118     intranetstylesheet => C4::Context->preference('intranetstylesheet'),
119     intranetcolorstylesheet => C4::Context->preference('intranetcolorstylesheet'),
120     irregular_issues => scalar @irregular_issues,
121     );
122
123 output_html_with_http_headers $query, $cookie, $template->output;