Bug 34478: serials/routing-preview.pl
[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::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 );
23 use C4::Context;
24 use C4::Search qw( enabled_staff_search_views );
25
26 use Koha::AdditionalFields;
27 use Koha::AuthorisedValues;
28 use Koha::DateUtils qw( output_pref );
29 use Koha::Acquisition::Bookseller;
30 use Koha::Subscriptions;
31
32 use Carp qw( carp );
33
34 use Koha::SharedContent;
35
36 my $query = CGI->new;
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');
41 my $subscription = Koha::Subscriptions->find( $subscriptionid );
42
43 if ( $op and $op eq "cud-close" ) {
44     C4::Serials::CloseSubscription( $subscriptionid );
45 } elsif ( $op and $op eq "cud-reopen" ) {
46     C4::Serials::ReopenSubscription( $subscriptionid );
47 }
48
49 # the subscription must be deletable if there is NO issues for a reason or another (should not happened, but...)
50
51 # Permission needed if it is a deletion (del) : delete_subscription
52 # Permission needed otherwise : *
53 my $permission = ($op eq "cud-del") ? "delete_subscription" : "*";
54
55 my ($template, $loggedinuser, $cookie)
56 = get_template_and_user({template_name => "serials/subscription-detail.tt",
57                 query => $query,
58                 type => "intranet",
59                 flagsrequired => {serials => $permission},
60                 });
61
62 my $subs = GetSubscription($subscriptionid);
63
64 output_and_exit( $query, $cookie, $template, 'unknown_subscription')
65     unless $subs;
66
67 $subs->{enddate} ||= GetExpirationDate($subscriptionid);
68
69 my ($totalissues,@serialslist) = GetSerials($subscriptionid);
70 $totalissues-- if $totalissues; # the -1 is to have 0 if this is a new subscription (only 1 issue)
71
72 if ($op eq 'cud-del') {
73     if ($$subs{'cannotedit'}){
74         carp "Attempt to delete subscription $subscriptionid by ".C4::Context->userenv->{'id'}." not allowed";
75         print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");
76         exit;
77     }
78
79     # Asking for confirmation if the subscription has not strictly expired yet or if it has linked issues
80     my $strictlyexpired = HasSubscriptionStrictlyExpired($subscriptionid);
81     my $linkedissues = CountIssues($subscriptionid);
82     my $countitems   = HasItems($subscriptionid);
83     if ($strictlyexpired == 0 || $linkedissues > 0 || $countitems>0) {
84         $template->param(NEEDSCONFIRMATION => 1);
85         if ($strictlyexpired == 0) { $template->param("NOTEXPIRED" => 1); }
86         if ($linkedissues     > 0) { $template->param("LINKEDISSUES" => 1); }
87         if ($countitems       > 0) { $template->param("LINKEDITEMS"  => 1); }
88     } else {
89         $issueconfirmed = "1";
90     }
91     # If it's ok to delete the subscription, we do so
92     if ($issueconfirmed eq "1") {
93         &DelSubscription($subscriptionid);
94         print $query->redirect("/cgi-bin/koha/serials/serials-home.pl");
95         exit;
96     }
97 }
98 elsif ( $op and $op eq "share" ) {
99     my $mana_language = $query->param('mana_language');
100     my $result = Koha::SharedContent::send_entity($mana_language, $loggedinuser, $subscriptionid, 'subscription');
101     $template->param( mana_code => $result->{msg} );
102     $subs->{mana_id} = $result->{id};
103 }
104
105 my $hasRouting = check_routing($subscriptionid);
106
107 (undef, $cookie, undef, undef)
108     = checkauth($query, 0, {catalogue => 1}, "intranet");
109
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 ?
111
112 for my $date ( qw(startdate enddate firstacquidate histstartdate histenddate) ) {
113     $subs->{$date} = output_pref( { str => $subs->{$date}, dateonly => 1 } )
114         if $subs->{$date};
115 }
116 my $av = Koha::AuthorisedValues->search({ category => 'LOC', authorised_value => $subs->{location} });
117 $subs->{location} = $av->count ? $av->next->lib : '';
118 $av = Koha::AuthorisedValues->search({ category => 'CCODE', authorised_value => $subs->{ccode} });
119 $subs->{ccode} = $av->count ? $av->next->lib : '';
120 $subs->{abouttoexpire}  = abouttoexpire($subs->{subscriptionid});
121 $template->param(%{ $subs });
122 $template->param(biblionumber_for_new_subscription => $subs->{bibnum});
123 my @irregular_issues = split /;/, $subs->{irregularity};
124
125 my $frequency = C4::Serials::Frequency::GetSubscriptionFrequency($subs->{periodicity});
126 my $numberpattern = C4::Serials::Numberpattern::GetSubscriptionNumberpattern($subs->{numberpattern});
127
128 $template->param(
129     available_additional_fields => Koha::AdditionalFields->search( { tablename => 'subscription' } ),
130     additional_field_values => { map { $_->field->name => $_->value } $subscription->additional_field_values->as_list },
131 );
132
133 # FIXME Do we want to hide canceled orders?
134 my $orders = Koha::Acquisition::Orders->search( { subscriptionid => $subscriptionid }, { order_by => [ { -desc => 'timestamp' }, \[ "field(orderstatus, 'ordered', 'partial', 'complete')" ] ] } );
135 my $orders_grouped;
136 while ( my $o = $orders->next ) {
137     if ( $o->ordernumber == $o->parent_ordernumber ) {
138         $orders_grouped->{$o->parent_ordernumber}->{datereceived} = $o->datereceived;
139         $orders_grouped->{$o->parent_ordernumber}->{orderstatus} = $o->orderstatus;
140         $orders_grouped->{$o->parent_ordernumber}->{basket} = $o->basket;
141     }
142     $orders_grouped->{$o->parent_ordernumber}->{quantity} += $o->quantity;
143     $orders_grouped->{$o->parent_ordernumber}->{ecost_tax_excluded} += sprintf('%.2f', $o->ecost_tax_excluded * $o->quantity);
144     $orders_grouped->{$o->parent_ordernumber}->{ecost_tax_included} += sprintf('%.2f', $o->ecost_tax_included * $o->quantity);
145     $orders_grouped->{$o->parent_ordernumber}->{unitprice_tax_excluded} += sprintf('%.2f', $o->unitprice_tax_excluded * $o->quantity);
146     $orders_grouped->{$o->parent_ordernumber}->{unitprice_tax_included} += sprintf('%.2f', $o->unitprice_tax_included * $o->quantity);
147     push @{$orders_grouped->{$o->parent_ordernumber}->{orders}}, $o;
148 }
149
150 $template->param(
151     subscription => $subscription,
152     subscriptionid => $subscriptionid,
153     serialslist => \@serialslist,
154     hasRouting  => $hasRouting,
155     routing => C4::Context->preference("RoutingSerials"),
156     totalissues => $totalissues,
157     cannotedit => (not C4::Serials::can_edit_subscription( $subs )),
158     frequency => $frequency,
159     numberpattern => $numberpattern,
160     has_X           => ($numberpattern->{'numberingmethod'} =~ /{X}/) ? 1 : 0,
161     has_Y           => ($numberpattern->{'numberingmethod'} =~ /{Y}/) ? 1 : 0,
162     has_Z           => ($numberpattern->{'numberingmethod'} =~ /{Z}/) ? 1 : 0,
163     intranetstylesheet => C4::Context->preference('intranetstylesheet'),
164     intranetcolorstylesheet => C4::Context->preference('intranetcolorstylesheet'),
165     irregular_issues => scalar @irregular_issues,
166     orders_grouped => $orders_grouped,
167     (uc(C4::Context->preference("marcflavour"))) => 1,
168     mana_comments => $subs->{comments},
169 );
170
171 output_html_with_http_headers $query, $cookie, $template->output;