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