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>.
20 use C4::Auth qw( get_template_and_user checkauth );
21 use C4::Serials qw( CloseSubscription ReopenSubscription GetSubscription GetExpirationDate GetSerials HasSubscriptionStrictlyExpired CountIssues HasItems DelSubscription check_routing abouttoexpire can_edit_subscription );
22 use C4::Output qw( output_and_exit output_html_with_http_headers );
24 use C4::Search qw( enabled_staff_search_views );
26 use Koha::AdditionalFields;
27 use Koha::AuthorisedValues;
28 use Koha::DateUtils qw( output_pref );
29 use Koha::Acquisition::Bookseller;
30 use Koha::Subscriptions;
34 use Koha::SharedContent;
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",
58 flagsrequired => {serials => $permission},
61 my $subs = GetSubscription($subscriptionid);
63 output_and_exit( $query, $cookie, $template, 'unknown_subscription')
66 $subs->{enddate} ||= GetExpirationDate($subscriptionid);
68 my ($totalissues,@serialslist) = GetSerials($subscriptionid);
69 $totalissues-- if $totalissues; # the -1 is to have 0 if this is a new subscription (only 1 issue)
72 if ($$subs{'cannotedit'}){
73 carp "Attempt to delete subscription $subscriptionid by ".C4::Context->userenv->{'id'}." not allowed";
74 print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");
78 # Asking for confirmation if the subscription has not strictly expired yet or if it has linked issues
79 my $strictlyexpired = HasSubscriptionStrictlyExpired($subscriptionid);
80 my $linkedissues = CountIssues($subscriptionid);
81 my $countitems = HasItems($subscriptionid);
82 if ($strictlyexpired == 0 || $linkedissues > 0 || $countitems>0) {
83 $template->param(NEEDSCONFIRMATION => 1);
84 if ($strictlyexpired == 0) { $template->param("NOTEXPIRED" => 1); }
85 if ($linkedissues > 0) { $template->param("LINKEDISSUES" => 1); }
86 if ($countitems > 0) { $template->param("LINKEDITEMS" => 1); }
88 $issueconfirmed = "1";
90 # If it's ok to delete the subscription, we do so
91 if ($issueconfirmed eq "1") {
92 &DelSubscription($subscriptionid);
93 print $query->redirect("/cgi-bin/koha/serials/serials-home.pl");
97 elsif ( $op and $op eq "share" ) {
98 my $mana_language = $query->param('mana_language');
99 my $result = Koha::SharedContent::send_entity($mana_language, $loggedinuser, $subscriptionid, 'subscription');
100 $template->param( mana_code => $result->{msg} );
101 $subs->{mana_id} = $result->{id};
104 my $hasRouting = check_routing($subscriptionid);
106 (undef, $cookie, undef, undef)
107 = checkauth($query, 0, {catalogue => 1}, "intranet");
109 # 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 for my $date ( qw(startdate enddate firstacquidate histstartdate histenddate) ) {
112 $subs->{$date} = output_pref( { str => $subs->{$date}, dateonly => 1 } )
115 my $av = Koha::AuthorisedValues->search({ category => 'LOC', authorised_value => $subs->{location} });
116 $subs->{location} = $av->count ? $av->next->lib : '';
117 $av = Koha::AuthorisedValues->search({ category => 'CCODE', authorised_value => $subs->{ccode} });
118 $subs->{ccode} = $av->count ? $av->next->lib : '';
119 $subs->{abouttoexpire} = abouttoexpire($subs->{subscriptionid});
120 $template->param(%{ $subs });
121 $template->param(biblionumber_for_new_subscription => $subs->{bibnum});
122 my @irregular_issues = split /;/, $subs->{irregularity};
124 my $frequency = C4::Serials::Frequency::GetSubscriptionFrequency($subs->{periodicity});
125 my $numberpattern = C4::Serials::Numberpattern::GetSubscriptionNumberpattern($subs->{numberpattern});
127 my $subscription_object = Koha::Subscriptions->find( $subscriptionid );
129 available_additional_fields => Koha::AdditionalFields->search( { tablename => 'subscription' } ),
130 additional_field_values => {
131 map { $_->field->name => $_->value }
132 $subscription_object->additional_field_values->as_list
136 # FIXME Do we want to hide canceled orders?
137 my $orders = Koha::Acquisition::Orders->search( { subscriptionid => $subscriptionid }, { order_by => [ { -desc => 'timestamp' }, \[ "field(orderstatus, 'ordered', 'partial', 'complete')" ] ] } );
139 while ( my $o = $orders->next ) {
140 if ( $o->ordernumber == $o->parent_ordernumber ) {
141 $orders_grouped->{$o->parent_ordernumber}->{datereceived} = $o->datereceived;
142 $orders_grouped->{$o->parent_ordernumber}->{orderstatus} = $o->orderstatus;
143 $orders_grouped->{$o->parent_ordernumber}->{basket} = $o->basket;
145 $orders_grouped->{$o->parent_ordernumber}->{quantity} += $o->quantity;
146 $orders_grouped->{$o->parent_ordernumber}->{ecost_tax_excluded} += sprintf('%.2f', $o->ecost_tax_excluded * $o->quantity);
147 $orders_grouped->{$o->parent_ordernumber}->{ecost_tax_included} += sprintf('%.2f', $o->ecost_tax_included * $o->quantity);
148 $orders_grouped->{$o->parent_ordernumber}->{unitprice_tax_excluded} += sprintf('%.2f', $o->unitprice_tax_excluded * $o->quantity);
149 $orders_grouped->{$o->parent_ordernumber}->{unitprice_tax_included} += sprintf('%.2f', $o->unitprice_tax_included * $o->quantity);
150 push @{$orders_grouped->{$o->parent_ordernumber}->{orders}}, $o;
154 subscriptionid => $subscriptionid,
155 serialslist => \@serialslist,
156 hasRouting => $hasRouting,
157 routing => C4::Context->preference("RoutingSerials"),
158 totalissues => $totalissues,
159 cannotedit => (not C4::Serials::can_edit_subscription( $subs )),
160 frequency => $frequency,
161 numberpattern => $numberpattern,
162 has_X => ($numberpattern->{'numberingmethod'} =~ /{X}/) ? 1 : 0,
163 has_Y => ($numberpattern->{'numberingmethod'} =~ /{Y}/) ? 1 : 0,
164 has_Z => ($numberpattern->{'numberingmethod'} =~ /{Z}/) ? 1 : 0,
165 intranetstylesheet => C4::Context->preference('intranetstylesheet'),
166 intranetcolorstylesheet => C4::Context->preference('intranetcolorstylesheet'),
167 irregular_issues => scalar @irregular_issues,
168 orders_grouped => $orders_grouped,
169 (uc(C4::Context->preference("marcflavour"))) => 1,
170 mana_comments => $subs->{comments},
173 output_html_with_http_headers $query, $cookie, $template->output;