Merge remote-tracking branch 'origin/new/bug_7412'
[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 my $subs = GetSubscription($subscriptionid);
38
39 $subs->{enddate} = GetExpirationDate($subscriptionid);
40
41 if ($op && $op eq 'del') {
42         if ($subs->{'cannotedit'}){
43                 carp "Attempt to delete subscription $subscriptionid by ".C4::Context->userenv->{'id'}." not allowed";
44                 print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");
45         }
46         DelSubscription($subscriptionid);
47         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=serials-home.pl\"></html>";
48         exit;
49 }
50
51 my ($totalissues,@serialslist) = GetSerials($subscriptionid);
52 $totalissues-- if $totalissues; # the -1 is to have 0 if this is a new subscription (only 1 issue)
53 # the subscription must be deletable if there is NO issues for a reason or another (should not happend, but...)
54
55 # Permission needed if it is a deletion (del) : delete_subscription
56 # Permission needed otherwise : *
57 my $permission = ($op eq "del") ? "delete_subscription" : "*";
58
59 ($template, $loggedinuser, $cookie)
60 = get_template_and_user({template_name => "serials/subscription-detail.tmpl",
61                 query => $query,
62                 type => "intranet",
63                 authnotrequired => 0,
64                 flagsrequired => {serials => $permission},
65                 debug => 1,
66                 });
67
68 $$subs{enddate} ||= GetExpirationDate($subscriptionid);
69
70 if ($op eq 'del') {
71         if ($$subs{'cannotedit'}){
72                 carp "Attempt to delete subscription $subscriptionid by ".C4::Context->userenv->{'id'}." not allowed";
73                 print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");
74                 exit;
75         }
76         
77     # Asking for confirmation if the subscription has not strictly expired yet or if it has linked issues
78     my $strictlyexpired = HasSubscriptionStrictlyExpired($subscriptionid);
79     my $linkedissues = CountIssues($subscriptionid);
80     my $countitems   = HasItems($subscriptionid);
81     if ($strictlyexpired == 0 || $linkedissues > 0 || $countitems>0) {
82                 $template->param(NEEDSCONFIRMATION => 1);
83                 if ($strictlyexpired == 0) { $template->param("NOTEXPIRED" => 1); }
84                 if ($linkedissues     > 0) { $template->param("LINKEDISSUES" => 1); }
85                 if ($countitems       > 0) { $template->param("LINKEDITEMS"  => 1); }
86     } else {
87                 $issueconfirmed = "1";
88     }
89     # If it's ok to delete the subscription, we do so
90     if ($issueconfirmed eq "1") {
91                 &DelSubscription($subscriptionid);
92                 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=serials-home.pl\"></html>";
93                 exit;
94     }
95 }
96 my $hasRouting = check_routing($subscriptionid);
97
98 (undef, $cookie, undef, undef)
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
123 my ( $order, $bookseller, $tmpl_infos );
124 # FIXME = see http://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=5335#c52
125 #if ( defined $subscriptionid ) {
126 #    my $lastOrderNotReceived = GetLastOrderNotReceivedFromSubscriptionid $subscriptionid;
127 #    my $lastOrderReceived = GetLastOrderReceivedFromSubscriptionid $subscriptionid;
128 #    if ( defined $lastOrderNotReceived ) {
129 #        my $basket = GetBasket $lastOrderNotReceived->{basketno};
130 #        my $bookseller = GetBookSellerFromId $basket->{booksellerid};
131 #        ( $tmpl_infos->{valuegsti_ordered}, $tmpl_infos->{valuegste_ordered} ) = get_value_with_gst_params ( $lastOrderNotReceived->{ecost}, $lastOrderNotReceived->{gstrate}, $bookseller );
132 #        $tmpl_infos->{valuegsti_ordered} = sprintf( "%.2f", $tmpl_infos->{valuegsti_ordered} );
133 #        $tmpl_infos->{valuegste_ordered} = sprintf( "%.2f", $tmpl_infos->{valuegste_ordered} );
134 #        $tmpl_infos->{budget_name_ordered} = GetBudgetName $lastOrderNotReceived->{budget_id};
135 #        $tmpl_infos->{basketno} = $lastOrderNotReceived->{basketno};
136 #        $tmpl_infos->{ordered_exists} = 1;
137 #    }
138 #    if ( defined $lastOrderReceived ) {
139 #        my $basket = GetBasket $lastOrderReceived->{basketno};
140 #        my $bookseller = GetBookSellerFromId $basket->{booksellerid};
141 #        ( $tmpl_infos->{valuegsti_spent}, $tmpl_infos->{valuegste_spent} ) = get_value_with_gst_params ( $lastOrderReceived->{unitprice}, $lastOrderReceived->{gstrate}, $bookseller );
142 #        $tmpl_infos->{valuegsti_spent} = sprintf( "%.2f", $tmpl_infos->{valuegsti_spent} );
143 #        $tmpl_infos->{valuegste_spent} = sprintf( "%.2f", $tmpl_infos->{valuegste_spent} );
144 #        $tmpl_infos->{budget_name_spent} = GetBudgetName $lastOrderReceived->{budget_id};
145 #        $tmpl_infos->{invoicenumber} = $lastOrderReceived->{booksellerinvoicenumber};
146 #        $tmpl_infos->{spent_exists} = 1;
147 #    }
148 #}
149
150 $template->param(
151         subscriptionid => $subscriptionid,
152     serialslist => \@serialslist,
153     hasRouting  => $hasRouting,
154     routing => C4::Context->preference("RoutingSerials"),
155     totalissues => $totalissues,
156     hemisphere => $hemisphere,
157     cannotedit =>(C4::Context->preference('IndependantBranches') &&
158                 C4::Context->userenv &&
159                 C4::Context->userenv->{flags} % 2 !=1  &&
160                 C4::Context->userenv->{branch} && $subs->{branchcode} &&
161                 (C4::Context->userenv->{branch} ne $subs->{branchcode})),
162     'periodicity' . $subs->{periodicity} => 1,
163     'arrival' . $subs->{dow} => 1,
164     'numberpattern' . $subs->{numberpattern} => 1,
165     intranetstylesheet => C4::Context->preference('intranetstylesheet'),
166     intranetcolorstylesheet => C4::Context->preference('intranetcolorstylesheet'),
167     irregular_issues => scalar @irregular_issues,
168     default_bib_view => $default_bib_view,
169     (uc(C4::Context->preference("marcflavour"))) => 1,
170     show_acquisition_details => defined $tmpl_infos->{ordered_exists} || defined $tmpl_infos->{spent_exists} ? 1 : 0,
171     );
172
173 output_html_with_http_headers $query, $cookie, $template->output;
174
175 sub get_default_view {
176     my $defaultview = C4::Context->preference('IntranetBiblioDefaultView');
177     my %views       = C4::Search::enabled_staff_search_views();
178     if ( $defaultview eq 'isbd' && $views{can_view_ISBD} ) {
179         return 'ISBDdetail';
180     }
181     elsif ( $defaultview eq 'marc' && $views{can_view_MARC} ) {
182         return 'MARCdetail';
183     }
184     elsif ( $defaultview eq 'labeled_marc' && $views{can_view_labeledMARC} ) {
185         return 'labeledMARCdetail';
186     }
187     return 'detail';
188 }