Bug 9978: Replace license header with the correct license (GPLv3+)
[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
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19 use CGI qw ( -utf8 );
20 use C4::Acquisition;
21 use C4::Auth;
22 use C4::Budgets;
23 use C4::Koha;
24 use C4::Dates qw/format_date/;
25 use C4::Serials;
26 use C4::Output;
27 use C4::Context;
28 use C4::Search qw/enabled_staff_search_views/;
29
30 use Koha::Acquisition::Bookseller;
31
32 use Date::Calc qw/Today Day_of_Year Week_of_Year Add_Delta_Days/;
33 use Carp;
34
35 my $query = new CGI;
36 my $op = $query->param('op') || q{};
37 my $issueconfirmed = $query->param('issueconfirmed');
38 my $dbh = C4::Context->dbh;
39 my $subscriptionid = $query->param('subscriptionid');
40
41 if ( $op and $op eq "close" ) {
42     C4::Serials::CloseSubscription( $subscriptionid );
43 } elsif ( $op and $op eq "reopen" ) {
44     C4::Serials::ReopenSubscription( $subscriptionid );
45 }
46
47 # the subscription must be deletable if there is NO issues for a reason or another (should not happend, but...)
48
49 # Permission needed if it is a deletion (del) : delete_subscription
50 # Permission needed otherwise : *
51 my $permission = ($op eq "del") ? "delete_subscription" : "*";
52
53 my ($template, $loggedinuser, $cookie)
54 = get_template_and_user({template_name => "serials/subscription-detail.tt",
55                 query => $query,
56                 type => "intranet",
57                 authnotrequired => 0,
58                 flagsrequired => {serials => $permission},
59                 debug => 1,
60                 });
61
62
63 my $subs = GetSubscription($subscriptionid);
64 $subs->{enddate} ||= GetExpirationDate($subscriptionid);
65
66 my ($totalissues,@serialslist) = GetSerials($subscriptionid);
67 $totalissues-- if $totalissues; # the -1 is to have 0 if this is a new subscription (only 1 issue)
68
69 if ($op eq 'del') {
70         if ($$subs{'cannotedit'}){
71                 carp "Attempt to delete subscription $subscriptionid by ".C4::Context->userenv->{'id'}." not allowed";
72                 print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");
73                 exit;
74         }
75         
76     # Asking for confirmation if the subscription has not strictly expired yet or if it has linked issues
77     my $strictlyexpired = HasSubscriptionStrictlyExpired($subscriptionid);
78     my $linkedissues = CountIssues($subscriptionid);
79     my $countitems   = HasItems($subscriptionid);
80     if ($strictlyexpired == 0 || $linkedissues > 0 || $countitems>0) {
81                 $template->param(NEEDSCONFIRMATION => 1);
82                 if ($strictlyexpired == 0) { $template->param("NOTEXPIRED" => 1); }
83                 if ($linkedissues     > 0) { $template->param("LINKEDISSUES" => 1); }
84                 if ($countitems       > 0) { $template->param("LINKEDITEMS"  => 1); }
85     } else {
86                 $issueconfirmed = "1";
87     }
88     # If it's ok to delete the subscription, we do so
89     if ($issueconfirmed eq "1") {
90                 &DelSubscription($subscriptionid);
91         print $query->redirect("/cgi-bin/koha/serials/serials-home.pl");
92         exit;
93     }
94 }
95 my $hasRouting = check_routing($subscriptionid);
96
97 (undef, $cookie, undef, undef)
98     = checkauth($query, 0, {catalogue => 1}, "intranet");
99
100 # COMMENT hdl : IMHO, we should think about passing more and more data hash to template->param rather than duplicating code a new coding Guideline ?
101
102 for my $date ( qw(startdate enddate firstacquidate histstartdate histenddate) ) {
103     $$subs{$date}      = format_date($$subs{$date}) if $date && $$subs{$date};
104 }
105 $subs->{location} = GetKohaAuthorisedValueLib("LOC",$subs->{location});
106 $subs->{abouttoexpire}  = abouttoexpire($subs->{subscriptionid});
107 $template->param(%{ $subs });
108 $template->param(biblionumber_for_new_subscription => $subs->{bibnum});
109 my @irregular_issues = split /;/, $subs->{irregularity};
110
111 my $frequency = C4::Serials::Frequency::GetSubscriptionFrequency($subs->{periodicity});
112 my $numberpattern = C4::Serials::Numberpattern::GetSubscriptionNumberpattern($subs->{numberpattern});
113
114 my $default_bib_view = get_default_view();
115
116 my ( $order, $bookseller, $tmpl_infos );
117 if ( defined $subscriptionid ) {
118     my $lastOrderNotReceived = GetLastOrderNotReceivedFromSubscriptionid $subscriptionid;
119     my $lastOrderReceived = GetLastOrderReceivedFromSubscriptionid $subscriptionid;
120     if ( defined $lastOrderNotReceived ) {
121         my $basket = GetBasket $lastOrderNotReceived->{basketno};
122         my $bookseller = Koha::Acquisition::Bookseller->fetch({ id => $basket->{booksellerid} });
123         ( $tmpl_infos->{valuegsti_ordered}, $tmpl_infos->{valuegste_ordered} ) = get_value_with_gst_params ( $lastOrderNotReceived->{ecost}, $lastOrderNotReceived->{gstrate}, $bookseller );
124         $tmpl_infos->{valuegsti_ordered} = sprintf( "%.2f", $tmpl_infos->{valuegsti_ordered} );
125         $tmpl_infos->{valuegste_ordered} = sprintf( "%.2f", $tmpl_infos->{valuegste_ordered} );
126         $tmpl_infos->{budget_name_ordered} = GetBudgetName $lastOrderNotReceived->{budget_id};
127         $tmpl_infos->{basketno} = $lastOrderNotReceived->{basketno};
128         $tmpl_infos->{ordered_exists} = 1;
129     }
130     if ( defined $lastOrderReceived ) {
131         my $basket = GetBasket $lastOrderReceived->{basketno};
132         my $bookseller = Koha::Acquisition::Bookseller->fetch({ id => $basket->{booksellerid} });
133         ( $tmpl_infos->{valuegsti_spent}, $tmpl_infos->{valuegste_spent} ) = get_value_with_gst_params ( $lastOrderReceived->{unitprice}, $lastOrderReceived->{gstrate}, $bookseller );
134         $tmpl_infos->{valuegsti_spent} = sprintf( "%.2f", $tmpl_infos->{valuegsti_spent} );
135         $tmpl_infos->{valuegste_spent} = sprintf( "%.2f", $tmpl_infos->{valuegste_spent} );
136         $tmpl_infos->{budget_name_spent} = GetBudgetName $lastOrderReceived->{budget_id};
137         $tmpl_infos->{invoiceid} = $lastOrderReceived->{invoiceid};
138         $tmpl_infos->{spent_exists} = 1;
139     }
140 }
141
142 $template->param(
143     subscriptionid => $subscriptionid,
144     serialslist => \@serialslist,
145     hasRouting  => $hasRouting,
146     routing => C4::Context->preference("RoutingSerials"),
147     totalissues => $totalissues,
148     cannotedit => (not C4::Serials::can_edit_subscription( $subs )),
149     frequency => $frequency,
150     numberpattern => $numberpattern,
151     has_X           => ($numberpattern->{'numberingmethod'} =~ /{X}/) ? 1 : 0,
152     has_Y           => ($numberpattern->{'numberingmethod'} =~ /{Y}/) ? 1 : 0,
153     has_Z           => ($numberpattern->{'numberingmethod'} =~ /{Z}/) ? 1 : 0,
154     intranetstylesheet => C4::Context->preference('intranetstylesheet'),
155     intranetcolorstylesheet => C4::Context->preference('intranetcolorstylesheet'),
156     irregular_issues => scalar @irregular_issues,
157     default_bib_view => $default_bib_view,
158     (uc(C4::Context->preference("marcflavour"))) => 1,
159     show_acquisition_details => defined $tmpl_infos->{ordered_exists} || defined $tmpl_infos->{spent_exists} ? 1 : 0,
160     basketno => $order->{basketno},
161     %$tmpl_infos,
162 );
163
164 output_html_with_http_headers $query, $cookie, $template->output;
165
166 sub get_default_view {
167     my $defaultview = C4::Context->preference('IntranetBiblioDefaultView');
168     my %views       = C4::Search::enabled_staff_search_views();
169     if ( $defaultview eq 'isbd' && $views{can_view_ISBD} ) {
170         return 'ISBDdetail';
171     }
172     elsif ( $defaultview eq 'marc' && $views{can_view_MARC} ) {
173         return 'MARCdetail';
174     }
175     elsif ( $defaultview eq 'labeled_marc' && $views{can_view_labeledMARC} ) {
176         return 'labeledMARCdetail';
177     }
178     return 'detail';
179 }
180
181 sub get_value_with_gst_params {
182     my $value = shift;
183     my $gstrate = shift;
184     my $bookseller = shift;
185     if ( $bookseller->{listincgst} ) {
186         return ( $value, $value / ( 1 + $gstrate ) );
187     } else {
188         return ( $value * ( 1 + $gstrate ), $value );
189     }
190 }
191
192 sub get_gste {
193     my $value = shift;
194     my $gstrate = shift;
195     my $bookseller = shift;
196     if ( $bookseller->{invoiceincgst} ) {
197         return $value / ( 1 + $gstrate );
198     } else {
199         return $value;
200     }
201 }
202
203 sub get_gst {
204     my $value = shift;
205     my $gstrate = shift;
206     my $bookseller = shift;
207     if ( $bookseller->{invoiceincgst} ) {
208         return $value / ( 1 + $gstrate ) * $gstrate;
209     } else {
210         return $value * ( 1 + $gstrate ) - $value;
211     }
212 }