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