Bug 21049: Fix value of material select for Rancor 007 widget
[koha.git] / serials / subscription-detail.pl
blobfb8721cf8eaaa9373e74538bd2213ef48387d1ab
1 #!/usr/bin/perl
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>.
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::Serials;
25 use C4::Output;
26 use C4::Context;
27 use C4::Search qw/enabled_staff_search_views/;
29 use Koha::AuthorisedValues;
30 use Koha::DateUtils;
31 use Koha::Acquisition::Bookseller;
33 use Date::Calc qw/Today Day_of_Year Week_of_Year Add_Delta_Days/;
34 use Carp;
36 my $query = new CGI;
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",
56 query => $query,
57 type => "intranet",
58 authnotrequired => 0,
59 flagsrequired => {serials => $permission},
60 debug => 1,
61 });
64 my $subs = GetSubscription($subscriptionid);
66 output_and_exit( $query, $cookie, $template, 'unknown_subscription')
67 unless $subs;
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)
74 if ($op eq 'del') {
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");
78 exit;
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); }
90 } else {
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");
97 exit;
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 } )
109 if $subs->{$date};
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 $additional_fields = Koha::AdditionalField->all( { tablename => 'subscription' } );
124 for my $field ( @$additional_fields ) {
125 if ( $field->{authorised_value_category} ) {
126 $field->{authorised_value_choices} = GetAuthorisedValues( $field->{authorised_value_category} );
129 $template->param( additional_fields_for_subscription => $additional_fields );
131 # FIXME Do we want to hide canceled orders?
132 my $orders = Koha::Acquisition::Orders->search( { subscriptionid => $subscriptionid }, { order_by => [ { -desc => 'timestamp' }, \[ "field(orderstatus, 'ordered', 'partial', 'complete')" ] ] } );
133 my $orders_grouped;
134 while ( my $o = $orders->next ) {
135 if ( $o->ordernumber == $o->parent_ordernumber ) {
136 $orders_grouped->{$o->parent_ordernumber}->{datereceived} = $o->datereceived;
137 $orders_grouped->{$o->parent_ordernumber}->{orderstatus} = $o->orderstatus;
138 $orders_grouped->{$o->parent_ordernumber}->{basket} = $o->basket;
140 $orders_grouped->{$o->parent_ordernumber}->{quantity} += $o->quantity;
141 $orders_grouped->{$o->parent_ordernumber}->{ecost_tax_excluded} += sprintf('%.2f', $o->ecost_tax_excluded * $o->quantity);
142 $orders_grouped->{$o->parent_ordernumber}->{ecost_tax_included} += sprintf('%.2f', $o->ecost_tax_included * $o->quantity);
143 $orders_grouped->{$o->parent_ordernumber}->{unitprice_tax_excluded} += sprintf('%.2f', $o->unitprice_tax_excluded * $o->quantity);
144 $orders_grouped->{$o->parent_ordernumber}->{unitprice_tax_included} += sprintf('%.2f', $o->unitprice_tax_included * $o->quantity);
145 push @{$orders_grouped->{$o->parent_ordernumber}->{orders}}, $o;
148 $template->param(
149 subscriptionid => $subscriptionid,
150 serialslist => \@serialslist,
151 hasRouting => $hasRouting,
152 routing => C4::Context->preference("RoutingSerials"),
153 totalissues => $totalissues,
154 cannotedit => (not C4::Serials::can_edit_subscription( $subs )),
155 frequency => $frequency,
156 numberpattern => $numberpattern,
157 has_X => ($numberpattern->{'numberingmethod'} =~ /{X}/) ? 1 : 0,
158 has_Y => ($numberpattern->{'numberingmethod'} =~ /{Y}/) ? 1 : 0,
159 has_Z => ($numberpattern->{'numberingmethod'} =~ /{Z}/) ? 1 : 0,
160 intranetstylesheet => C4::Context->preference('intranetstylesheet'),
161 intranetcolorstylesheet => C4::Context->preference('intranetcolorstylesheet'),
162 irregular_issues => scalar @irregular_issues,
163 default_bib_view => $default_bib_view,
164 orders_grouped => $orders_grouped,
165 (uc(C4::Context->preference("marcflavour"))) => 1,
168 output_html_with_http_headers $query, $cookie, $template->output;
170 sub get_default_view {
171 my $defaultview = C4::Context->preference('IntranetBiblioDefaultView');
172 my %views = C4::Search::enabled_staff_search_views();
173 if ( $defaultview eq 'isbd' && $views{can_view_ISBD} ) {
174 return 'ISBDdetail';
176 elsif ( $defaultview eq 'marc' && $views{can_view_MARC} ) {
177 return 'MARCdetail';
179 elsif ( $defaultview eq 'labeled_marc' && $views{can_view_labeledMARC} ) {
180 return 'labeledMARCdetail';
182 return 'detail';