Merge branch 'bug_9900' into 3.12-master
[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 C4::Search qw/enabled_staff_search_views/;
28 use Date::Calc qw/Today Day_of_Year Week_of_Year Add_Delta_Days/;
29 use Carp;
30
31 my $query = new CGI;
32 my $op = $query->param('op') || q{};
33 my $issueconfirmed = $query->param('issueconfirmed');
34 my $dbh = C4::Context->dbh;
35 my ($template, $loggedinuser, $cookie, $hemisphere);
36 my $subscriptionid = $query->param('subscriptionid');
37
38 if ( $op and $op eq "close" ) {
39     C4::Serials::CloseSubscription( $subscriptionid );
40 } elsif ( $op and $op eq "reopen" ) {
41     C4::Serials::ReopenSubscription( $subscriptionid );
42 }
43
44 my $subs = GetSubscription($subscriptionid);
45
46 $subs->{enddate} = GetExpirationDate($subscriptionid);
47
48 if ($op && $op eq 'del') {
49         if ($subs->{'cannotedit'}){
50                 carp "Attempt to delete subscription $subscriptionid by ".C4::Context->userenv->{'id'}." not allowed";
51                 print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");
52         }
53         DelSubscription($subscriptionid);
54         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=serials-home.pl\"></html>";
55         exit;
56 }
57
58 my ($totalissues,@serialslist) = GetSerials($subscriptionid);
59 $totalissues-- if $totalissues; # the -1 is to have 0 if this is a new subscription (only 1 issue)
60 # the subscription must be deletable if there is NO issues for a reason or another (should not happend, but...)
61
62 # Permission needed if it is a deletion (del) : delete_subscription
63 # Permission needed otherwise : *
64 my $permission = ($op eq "del") ? "delete_subscription" : "*";
65
66 ($template, $loggedinuser, $cookie)
67 = get_template_and_user({template_name => "serials/subscription-detail.tmpl",
68                 query => $query,
69                 type => "intranet",
70                 authnotrequired => 0,
71                 flagsrequired => {serials => $permission},
72                 debug => 1,
73                 });
74
75 $$subs{enddate} ||= GetExpirationDate($subscriptionid);
76
77 if ($op eq 'del') {
78         if ($$subs{'cannotedit'}){
79                 carp "Attempt to delete subscription $subscriptionid by ".C4::Context->userenv->{'id'}." not allowed";
80                 print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");
81                 exit;
82         }
83         
84     # Asking for confirmation if the subscription has not strictly expired yet or if it has linked issues
85     my $strictlyexpired = HasSubscriptionStrictlyExpired($subscriptionid);
86     my $linkedissues = CountIssues($subscriptionid);
87     my $countitems   = HasItems($subscriptionid);
88     if ($strictlyexpired == 0 || $linkedissues > 0 || $countitems>0) {
89                 $template->param(NEEDSCONFIRMATION => 1);
90                 if ($strictlyexpired == 0) { $template->param("NOTEXPIRED" => 1); }
91                 if ($linkedissues     > 0) { $template->param("LINKEDISSUES" => 1); }
92                 if ($countitems       > 0) { $template->param("LINKEDITEMS"  => 1); }
93     } else {
94                 $issueconfirmed = "1";
95     }
96     # If it's ok to delete the subscription, we do so
97     if ($issueconfirmed eq "1") {
98                 &DelSubscription($subscriptionid);
99                 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=serials-home.pl\"></html>";
100                 exit;
101     }
102 }
103 my $hasRouting = check_routing($subscriptionid);
104
105 (undef, $cookie, undef, undef)
106     = checkauth($query, 0, {catalogue => 1}, "intranet");
107
108 # COMMENT hdl : IMHO, we should think about passing more and more data hash to template->param rather than duplicating code a new coding Guideline ?
109
110 for my $date ( qw(startdate enddate firstacquidate histstartdate histenddate) ) {
111     $$subs{$date}      = format_date($$subs{$date}) if $date && $$subs{$date};
112 }
113 $subs->{location} = GetKohaAuthorisedValueLib("LOC",$subs->{location});
114 $subs->{abouttoexpire}  = abouttoexpire($subs->{subscriptionid});
115 $template->param(%{ $subs });
116 $template->param(biblionumber_for_new_subscription => $subs->{bibnum});
117 my @irregular_issues = split /,/, $subs->{irregularity};
118
119 my $frequency = C4::Serials::Frequency::GetSubscriptionFrequency($subs->{periodicity});
120 my $numberpattern = C4::Serials::Numberpattern::GetSubscriptionNumberpattern($subs->{numberpattern});
121
122 my $default_bib_view = get_default_view();
123
124 my ( $order, $bookseller, $tmpl_infos );
125 # FIXME = see http://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=5335#c52
126 #if ( defined $subscriptionid ) {
127 #    my $lastOrderNotReceived = GetLastOrderNotReceivedFromSubscriptionid $subscriptionid;
128 #    my $lastOrderReceived = GetLastOrderReceivedFromSubscriptionid $subscriptionid;
129 #    if ( defined $lastOrderNotReceived ) {
130 #        my $basket = GetBasket $lastOrderNotReceived->{basketno};
131 #        my $bookseller = GetBookSellerFromId $basket->{booksellerid};
132 #        ( $tmpl_infos->{valuegsti_ordered}, $tmpl_infos->{valuegste_ordered} ) = get_value_with_gst_params ( $lastOrderNotReceived->{ecost}, $lastOrderNotReceived->{gstrate}, $bookseller );
133 #        $tmpl_infos->{valuegsti_ordered} = sprintf( "%.2f", $tmpl_infos->{valuegsti_ordered} );
134 #        $tmpl_infos->{valuegste_ordered} = sprintf( "%.2f", $tmpl_infos->{valuegste_ordered} );
135 #        $tmpl_infos->{budget_name_ordered} = GetBudgetName $lastOrderNotReceived->{budget_id};
136 #        $tmpl_infos->{basketno} = $lastOrderNotReceived->{basketno};
137 #        $tmpl_infos->{ordered_exists} = 1;
138 #    }
139 #    if ( defined $lastOrderReceived ) {
140 #        my $basket = GetBasket $lastOrderReceived->{basketno};
141 #        my $bookseller = GetBookSellerFromId $basket->{booksellerid};
142 #        ( $tmpl_infos->{valuegsti_spent}, $tmpl_infos->{valuegste_spent} ) = get_value_with_gst_params ( $lastOrderReceived->{unitprice}, $lastOrderReceived->{gstrate}, $bookseller );
143 #        $tmpl_infos->{valuegsti_spent} = sprintf( "%.2f", $tmpl_infos->{valuegsti_spent} );
144 #        $tmpl_infos->{valuegste_spent} = sprintf( "%.2f", $tmpl_infos->{valuegste_spent} );
145 #        $tmpl_infos->{budget_name_spent} = GetBudgetName $lastOrderReceived->{budget_id};
146 #        $tmpl_infos->{invoicenumber} = $lastOrderReceived->{booksellerinvoicenumber};
147 #        $tmpl_infos->{spent_exists} = 1;
148 #    }
149 #}
150
151 $template->param(
152         subscriptionid => $subscriptionid,
153     serialslist => \@serialslist,
154     hasRouting  => $hasRouting,
155     routing => C4::Context->preference("RoutingSerials"),
156     totalissues => $totalissues,
157     hemisphere => $hemisphere,
158     cannotedit =>(C4::Context->preference('IndependantBranches') &&
159                 C4::Context->userenv &&
160                 C4::Context->userenv->{flags} % 2 !=1  &&
161                 C4::Context->userenv->{branch} && $subs->{branchcode} &&
162                 (C4::Context->userenv->{branch} ne $subs->{branchcode})),
163     frequency => $frequency,
164     numberpattern => $numberpattern,
165     has_X           => ($numberpattern->{'numberingmethod'} =~ /{X}/) ? 1 : 0,
166     has_Y           => ($numberpattern->{'numberingmethod'} =~ /{Y}/) ? 1 : 0,
167     has_Z           => ($numberpattern->{'numberingmethod'} =~ /{Z}/) ? 1 : 0,
168     intranetstylesheet => C4::Context->preference('intranetstylesheet'),
169     intranetcolorstylesheet => C4::Context->preference('intranetcolorstylesheet'),
170     irregular_issues => scalar @irregular_issues,
171     default_bib_view => $default_bib_view,
172     (uc(C4::Context->preference("marcflavour"))) => 1,
173     show_acquisition_details => defined $tmpl_infos->{ordered_exists} || defined $tmpl_infos->{spent_exists} ? 1 : 0,
174     );
175
176 output_html_with_http_headers $query, $cookie, $template->output;
177
178 sub get_default_view {
179     my $defaultview = C4::Context->preference('IntranetBiblioDefaultView');
180     my %views       = C4::Search::enabled_staff_search_views();
181     if ( $defaultview eq 'isbd' && $views{can_view_ISBD} ) {
182         return 'ISBDdetail';
183     }
184     elsif ( $defaultview eq 'marc' && $views{can_view_MARC} ) {
185         return 'MARCdetail';
186     }
187     elsif ( $defaultview eq 'labeled_marc' && $views{can_view_labeledMARC} ) {
188         return 'labeledMARCdetail';
189     }
190     return 'detail';
191 }