3 # This file is part of Koha.
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
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.
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.
22 use C4::Bookseller qw/GetBookSellerFromId/;
25 use C4::Dates qw/format_date/;
29 use C4::Search qw/enabled_staff_search_views/;
30 use Date::Calc qw/Today Day_of_Year Week_of_Year Add_Delta_Days/;
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');
40 if ( $op and $op eq "close" ) {
41 C4::Serials::CloseSubscription( $subscriptionid );
42 } elsif ( $op and $op eq "reopen" ) {
43 C4::Serials::ReopenSubscription( $subscriptionid );
46 my $subs = GetSubscription($subscriptionid);
48 $subs->{enddate} = GetExpirationDate($subscriptionid);
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");
55 DelSubscription($subscriptionid);
56 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=serials-home.pl\"></html>";
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...)
64 # Permission needed if it is a deletion (del) : delete_subscription
65 # Permission needed otherwise : *
66 my $permission = ($op eq "del") ? "delete_subscription" : "*";
68 ($template, $loggedinuser, $cookie)
69 = get_template_and_user({template_name => "serials/subscription-detail.tmpl",
73 flagsrequired => {serials => $permission},
77 $$subs{enddate} ||= GetExpirationDate($subscriptionid);
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");
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); }
96 $issueconfirmed = "1";
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>";
105 my $hasRouting = check_routing($subscriptionid);
107 (undef, $cookie, undef, undef)
108 = checkauth($query, 0, {catalogue => 1}, "intranet");
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 ?
112 for my $date ( qw(startdate enddate firstacquidate histstartdate histenddate) ) {
113 $$subs{$date} = format_date($$subs{$date}) if $date && $$subs{$date};
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};
121 my $frequency = C4::Serials::Frequency::GetSubscriptionFrequency($subs->{periodicity});
122 my $numberpattern = C4::Serials::Numberpattern::GetSubscriptionNumberpattern($subs->{numberpattern});
124 my $default_bib_view = get_default_view();
126 my ( $order, $bookseller, $tmpl_infos );
127 if ( defined $subscriptionid ) {
128 my $lastOrderNotReceived = GetLastOrderNotReceivedFromSubscriptionid $subscriptionid;
129 my $lastOrderReceived = GetLastOrderReceivedFromSubscriptionid $subscriptionid;
130 if ( defined $lastOrderNotReceived ) {
131 my $basket = GetBasket $lastOrderNotReceived->{basketno};
132 my $bookseller = GetBookSellerFromId $basket->{booksellerid};
133 ( $tmpl_infos->{valuegsti_ordered}, $tmpl_infos->{valuegste_ordered} ) = get_value_with_gst_params ( $lastOrderNotReceived->{ecost}, $lastOrderNotReceived->{gstrate}, $bookseller );
134 $tmpl_infos->{valuegsti_ordered} = sprintf( "%.2f", $tmpl_infos->{valuegsti_ordered} );
135 $tmpl_infos->{valuegste_ordered} = sprintf( "%.2f", $tmpl_infos->{valuegste_ordered} );
136 $tmpl_infos->{budget_name_ordered} = GetBudgetName $lastOrderNotReceived->{budget_id};
137 $tmpl_infos->{basketno} = $lastOrderNotReceived->{basketno};
138 $tmpl_infos->{ordered_exists} = 1;
140 if ( defined $lastOrderReceived ) {
141 my $basket = GetBasket $lastOrderReceived->{basketno};
142 my $bookseller = GetBookSellerFromId $basket->{booksellerid};
143 ( $tmpl_infos->{valuegsti_spent}, $tmpl_infos->{valuegste_spent} ) = get_value_with_gst_params ( $lastOrderReceived->{unitprice}, $lastOrderReceived->{gstrate}, $bookseller );
144 $tmpl_infos->{valuegsti_spent} = sprintf( "%.2f", $tmpl_infos->{valuegsti_spent} );
145 $tmpl_infos->{valuegste_spent} = sprintf( "%.2f", $tmpl_infos->{valuegste_spent} );
146 $tmpl_infos->{budget_name_spent} = GetBudgetName $lastOrderReceived->{budget_id};
147 $tmpl_infos->{invoiceid} = $lastOrderReceived->{invoiceid};
148 $tmpl_infos->{spent_exists} = 1;
153 subscriptionid => $subscriptionid,
154 serialslist => \@serialslist,
155 hasRouting => $hasRouting,
156 routing => C4::Context->preference("RoutingSerials"),
157 totalissues => $totalissues,
158 hemisphere => $hemisphere,
159 cannotedit =>(C4::Context->preference('IndependentBranches') &&
160 C4::Context->userenv &&
161 C4::Context->userenv->{flags} % 2 !=1 &&
162 C4::Context->userenv->{branch} && $subs->{branchcode} &&
163 (C4::Context->userenv->{branch} ne $subs->{branchcode})),
164 frequency => $frequency,
165 numberpattern => $numberpattern,
166 has_X => ($numberpattern->{'numberingmethod'} =~ /{X}/) ? 1 : 0,
167 has_Y => ($numberpattern->{'numberingmethod'} =~ /{Y}/) ? 1 : 0,
168 has_Z => ($numberpattern->{'numberingmethod'} =~ /{Z}/) ? 1 : 0,
169 intranetstylesheet => C4::Context->preference('intranetstylesheet'),
170 intranetcolorstylesheet => C4::Context->preference('intranetcolorstylesheet'),
171 irregular_issues => scalar @irregular_issues,
172 default_bib_view => $default_bib_view,
173 (uc(C4::Context->preference("marcflavour"))) => 1,
174 show_acquisition_details => defined $tmpl_infos->{ordered_exists} || defined $tmpl_infos->{spent_exists} ? 1 : 0,
175 basketno => $order->{basketno},
179 output_html_with_http_headers $query, $cookie, $template->output;
181 sub get_default_view {
182 my $defaultview = C4::Context->preference('IntranetBiblioDefaultView');
183 my %views = C4::Search::enabled_staff_search_views();
184 if ( $defaultview eq 'isbd' && $views{can_view_ISBD} ) {
187 elsif ( $defaultview eq 'marc' && $views{can_view_MARC} ) {
190 elsif ( $defaultview eq 'labeled_marc' && $views{can_view_labeledMARC} ) {
191 return 'labeledMARCdetail';
196 sub get_value_with_gst_params {
199 my $bookseller = shift;
200 if ( $bookseller->{listincgst} ) {
201 return ( $value, $value / ( 1 + $gstrate ) );
203 return ( $value * ( 1 + $gstrate ), $value );
210 my $bookseller = shift;
211 if ( $bookseller->{invoiceincgst} ) {
212 return $value / ( 1 + $gstrate );
221 my $bookseller = shift;
222 if ( $bookseller->{invoiceincgst} ) {
223 return $value / ( 1 + $gstrate ) * $gstrate;
225 return $value * ( 1 + $gstrate ) - $value;