3 # This file is part of Koha.
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.
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.
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>.
27 use C4::Search qw/enabled_staff_search_views/;
29 use Koha::AuthorisedValues;
31 use Koha::Acquisition::Bookseller;
33 use Date::Calc qw/Today Day_of_Year Week_of_Year Add_Delta_Days/;
37 my $op = $query->param('op') || q{};
38 my $issueconfirmed = $query->param('issueconfirmed');
39 my $dbh = C4::Context->dbh;
40 my $subscriptionid = $query->param('subscriptionid');
42 if ( $op and $op eq "close" ) {
43 C4::Serials::CloseSubscription( $subscriptionid );
44 } elsif ( $op and $op eq "reopen" ) {
45 C4::Serials::ReopenSubscription( $subscriptionid );
48 # the subscription must be deletable if there is NO issues for a reason or another (should not happened, but...)
50 # Permission needed if it is a deletion (del) : delete_subscription
51 # Permission needed otherwise : *
52 my $permission = ($op eq "del") ? "delete_subscription" : "*";
54 my ($template, $loggedinuser, $cookie)
55 = get_template_and_user({template_name => "serials/subscription-detail.tt",
59 flagsrequired => {serials => $permission},
64 my $subs = GetSubscription($subscriptionid);
66 output_and_exit( $query, $cookie, $template, 'unknown_subscription')
69 $subs->{enddate} ||= GetExpirationDate($subscriptionid);
71 my ($totalissues,@serialslist) = GetSerials($subscriptionid);
72 $totalissues-- if $totalissues; # the -1 is to have 0 if this is a new subscription (only 1 issue)
75 if ($$subs{'cannotedit'}){
76 carp "Attempt to delete subscription $subscriptionid by ".C4::Context->userenv->{'id'}." not allowed";
77 print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");
81 # Asking for confirmation if the subscription has not strictly expired yet or if it has linked issues
82 my $strictlyexpired = HasSubscriptionStrictlyExpired($subscriptionid);
83 my $linkedissues = CountIssues($subscriptionid);
84 my $countitems = HasItems($subscriptionid);
85 if ($strictlyexpired == 0 || $linkedissues > 0 || $countitems>0) {
86 $template->param(NEEDSCONFIRMATION => 1);
87 if ($strictlyexpired == 0) { $template->param("NOTEXPIRED" => 1); }
88 if ($linkedissues > 0) { $template->param("LINKEDISSUES" => 1); }
89 if ($countitems > 0) { $template->param("LINKEDITEMS" => 1); }
91 $issueconfirmed = "1";
93 # If it's ok to delete the subscription, we do so
94 if ($issueconfirmed eq "1") {
95 &DelSubscription($subscriptionid);
96 print $query->redirect("/cgi-bin/koha/serials/serials-home.pl");
100 my $hasRouting = check_routing($subscriptionid);
102 (undef, $cookie, undef, undef)
103 = checkauth($query, 0, {catalogue => 1}, "intranet");
105 # COMMENT hdl : IMHO, we should think about passing more and more data hash to template->param rather than duplicating code a new coding Guideline ?
107 for my $date ( qw(startdate enddate firstacquidate histstartdate histenddate) ) {
108 $subs->{$date} = output_pref( { str => $subs->{$date}, dateonly => 1 } )
111 my $av = Koha::AuthorisedValues->search({ category => 'LOC', authorised_value => $subs->{location} });
112 $subs->{location} = $av->count ? $av->next->lib : '';
113 $subs->{abouttoexpire} = abouttoexpire($subs->{subscriptionid});
114 $template->param(%{ $subs });
115 $template->param(biblionumber_for_new_subscription => $subs->{bibnum});
116 my @irregular_issues = split /;/, $subs->{irregularity};
118 my $frequency = C4::Serials::Frequency::GetSubscriptionFrequency($subs->{periodicity});
119 my $numberpattern = C4::Serials::Numberpattern::GetSubscriptionNumberpattern($subs->{numberpattern});
121 my $default_bib_view = get_default_view();
123 my ( $order, $bookseller, $tmpl_infos );
124 if ( defined $subscriptionid ) {
125 my $lastOrderNotReceived = GetLastOrderNotReceivedFromSubscriptionid $subscriptionid;
126 my $lastOrderReceived = GetLastOrderReceivedFromSubscriptionid $subscriptionid;
127 if ( defined $lastOrderNotReceived ) {
128 my $basket = GetBasket $lastOrderNotReceived->{basketno};
129 my $bookseller = Koha::Acquisition::Booksellers->find( $basket->{booksellerid} );
130 ( $tmpl_infos->{value_tax_included_ordered}, $tmpl_infos->{value_tax_excluded_ordered} ) = get_value_with_gst_params ( $lastOrderNotReceived->{ecost}, $lastOrderNotReceived->{tax_rate}, $bookseller );
131 $tmpl_infos->{value_tax_included_ordered} = sprintf( "%.2f", $tmpl_infos->{value_tax_included_ordered} );
132 $tmpl_infos->{value_tax_excluded_ordered} = sprintf( "%.2f", $tmpl_infos->{value_tax_excluded_ordered} );
133 $tmpl_infos->{budget_name_ordered} = GetBudgetName $lastOrderNotReceived->{budget_id};
134 $tmpl_infos->{basketno} = $lastOrderNotReceived->{basketno};
135 $tmpl_infos->{ordered_exists} = 1;
137 if ( defined $lastOrderReceived ) {
138 my $basket = GetBasket $lastOrderReceived->{basketno};
139 my $bookseller = Koha::Acquisition::Booksellers->find( $basket->{booksellerid} );
140 ( $tmpl_infos->{value_tax_included_spent}, $tmpl_infos->{value_tax_excluded_spent} ) = get_value_with_gst_params ( $lastOrderReceived->{unitprice}, $lastOrderReceived->{tax_rate}, $bookseller );
141 $tmpl_infos->{value_tax_included_spent} = sprintf( "%.2f", $tmpl_infos->{value_tax_included_spent} );
142 $tmpl_infos->{value_tax_excluded_spent} = sprintf( "%.2f", $tmpl_infos->{value_tax_excluded_spent} );
143 $tmpl_infos->{budget_name_spent} = GetBudgetName $lastOrderReceived->{budget_id};
144 $tmpl_infos->{invoiceid} = $lastOrderReceived->{invoiceid};
145 $tmpl_infos->{spent_exists} = 1;
149 my $additional_fields = Koha::AdditionalField->all( { tablename => 'subscription' } );
150 for my $field ( @$additional_fields ) {
151 if ( $field->{authorised_value_category} ) {
152 $field->{authorised_value_choices} = GetAuthorisedValues( $field->{authorised_value_category} );
155 $template->param( additional_fields_for_subscription => $additional_fields );
158 subscriptionid => $subscriptionid,
159 serialslist => \@serialslist,
160 hasRouting => $hasRouting,
161 routing => C4::Context->preference("RoutingSerials"),
162 totalissues => $totalissues,
163 cannotedit => (not C4::Serials::can_edit_subscription( $subs )),
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 {
198 my $tax_rate = shift;
199 my $bookseller = shift;
200 if ( $bookseller->listincgst ) {
201 return ( $value, $value / ( 1 + $tax_rate ) );
203 return ( $value * ( 1 + $tax_rate ), $value );
207 sub get_tax_excluded {
209 my $tax_rate = shift;
210 my $bookseller = shift;
211 if ( $bookseller->invoiceincgst ) {
212 return $value / ( 1 + $tax_rate );
220 my $tax_rate = shift;
221 my $bookseller = shift;
222 if ( $bookseller->invoiceincgst ) {
223 return $value / ( 1 + $tax_rate ) * $tax_rate;
225 return $value * ( 1 + $tax_rate ) - $value;