Bug 15774: Add additional fields to order baskets
[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::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/;
28
29 use Koha::AuthorisedValues;
30 use Koha::DateUtils;
31 use Koha::Acquisition::Bookseller;
32
33 use Date::Calc qw/Today Day_of_Year Week_of_Year Add_Delta_Days/;
34 use Carp;
35
36 use Koha::SharedContent;
37
38 my $query = new CGI;
39 my $op = $query->param('op') || q{};
40 my $issueconfirmed = $query->param('issueconfirmed');
41 my $dbh = C4::Context->dbh;
42 my $subscriptionid = $query->param('subscriptionid');
43
44 if ( $op and $op eq "close" ) {
45     C4::Serials::CloseSubscription( $subscriptionid );
46 } elsif ( $op and $op eq "reopen" ) {
47     C4::Serials::ReopenSubscription( $subscriptionid );
48 }
49
50 # the subscription must be deletable if there is NO issues for a reason or another (should not happened, but...)
51
52 # Permission needed if it is a deletion (del) : delete_subscription
53 # Permission needed otherwise : *
54 my $permission = ($op eq "del") ? "delete_subscription" : "*";
55
56 my ($template, $loggedinuser, $cookie)
57 = get_template_and_user({template_name => "serials/subscription-detail.tt",
58                 query => $query,
59                 type => "intranet",
60                 authnotrequired => 0,
61                 flagsrequired => {serials => $permission},
62                 debug => 1,
63                 });
64
65 my $subs = GetSubscription($subscriptionid);
66
67 output_and_exit( $query, $cookie, $template, 'unknown_subscription')
68     unless $subs;
69
70 $subs->{enddate} ||= GetExpirationDate($subscriptionid);
71
72 my ($totalissues,@serialslist) = GetSerials($subscriptionid);
73 $totalissues-- if $totalissues; # the -1 is to have 0 if this is a new subscription (only 1 issue)
74
75 if ($op eq 'del') {
76     if ($$subs{'cannotedit'}){
77         carp "Attempt to delete subscription $subscriptionid by ".C4::Context->userenv->{'id'}." not allowed";
78         print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");
79         exit;
80     }
81
82     # Asking for confirmation if the subscription has not strictly expired yet or if it has linked issues
83     my $strictlyexpired = HasSubscriptionStrictlyExpired($subscriptionid);
84     my $linkedissues = CountIssues($subscriptionid);
85     my $countitems   = HasItems($subscriptionid);
86     if ($strictlyexpired == 0 || $linkedissues > 0 || $countitems>0) {
87         $template->param(NEEDSCONFIRMATION => 1);
88         if ($strictlyexpired == 0) { $template->param("NOTEXPIRED" => 1); }
89         if ($linkedissues     > 0) { $template->param("LINKEDISSUES" => 1); }
90         if ($countitems       > 0) { $template->param("LINKEDITEMS"  => 1); }
91     } else {
92         $issueconfirmed = "1";
93     }
94     # If it's ok to delete the subscription, we do so
95     if ($issueconfirmed eq "1") {
96         &DelSubscription($subscriptionid);
97         print $query->redirect("/cgi-bin/koha/serials/serials-home.pl");
98         exit;
99     }
100 }
101 elsif ( $op and $op eq "share" ) {
102     my $mana_language = $query->param('mana_language');
103     my $result = Koha::SharedContent::send_entity($mana_language, $loggedinuser, $subscriptionid, 'subscription');
104     $template->param( mana_code => $result->{msg} );
105     $subs->{mana_id} = $result->{id};
106 }
107
108 my $hasRouting = check_routing($subscriptionid);
109
110 (undef, $cookie, undef, undef)
111     = checkauth($query, 0, {catalogue => 1}, "intranet");
112
113 # COMMENT hdl : IMHO, we should think about passing more and more data hash to template->param rather than duplicating code a new coding Guideline ?
114
115 for my $date ( qw(startdate enddate firstacquidate histstartdate histenddate) ) {
116     $subs->{$date} = output_pref( { str => $subs->{$date}, dateonly => 1 } )
117         if $subs->{$date};
118 }
119 my $av = Koha::AuthorisedValues->search({ category => 'LOC', authorised_value => $subs->{location} });
120 $subs->{location} = $av->count ? $av->next->lib : '';
121 $subs->{abouttoexpire}  = abouttoexpire($subs->{subscriptionid});
122 $template->param(%{ $subs });
123 $template->param(biblionumber_for_new_subscription => $subs->{bibnum});
124 my @irregular_issues = split /;/, $subs->{irregularity};
125
126 my $frequency = C4::Serials::Frequency::GetSubscriptionFrequency($subs->{periodicity});
127 my $numberpattern = C4::Serials::Numberpattern::GetSubscriptionNumberpattern($subs->{numberpattern});
128
129 my $default_bib_view = get_default_view();
130
131 my $additional_fields = Koha::AdditionalField->all( { tablename => 'subscription' } );
132 for my $field ( @$additional_fields ) {
133     if ( $field->{authorised_value_category} ) {
134         $field->{authorised_value_choices} = GetAuthorisedValues( $field->{authorised_value_category} );
135     }
136 }
137 $template->param( additional_fields_for_subscription => $additional_fields );
138
139 # FIXME Do we want to hide canceled orders?
140 my $orders = Koha::Acquisition::Orders->search( { subscriptionid => $subscriptionid }, { order_by => [ { -desc => 'timestamp' }, \[ "field(orderstatus, 'ordered', 'partial', 'complete')" ] ] } );
141 my $orders_grouped;
142 while ( my $o = $orders->next ) {
143     if ( $o->ordernumber == $o->parent_ordernumber ) {
144         $orders_grouped->{$o->parent_ordernumber}->{datereceived} = $o->datereceived;
145         $orders_grouped->{$o->parent_ordernumber}->{orderstatus} = $o->orderstatus;
146         $orders_grouped->{$o->parent_ordernumber}->{basket} = $o->basket;
147     }
148     $orders_grouped->{$o->parent_ordernumber}->{quantity} += $o->quantity;
149     $orders_grouped->{$o->parent_ordernumber}->{ecost_tax_excluded} += sprintf('%.2f', $o->ecost_tax_excluded * $o->quantity);
150     $orders_grouped->{$o->parent_ordernumber}->{ecost_tax_included} += sprintf('%.2f', $o->ecost_tax_included * $o->quantity);
151     $orders_grouped->{$o->parent_ordernumber}->{unitprice_tax_excluded} += sprintf('%.2f', $o->unitprice_tax_excluded * $o->quantity);
152     $orders_grouped->{$o->parent_ordernumber}->{unitprice_tax_included} += sprintf('%.2f', $o->unitprice_tax_included * $o->quantity);
153     push @{$orders_grouped->{$o->parent_ordernumber}->{orders}}, $o;
154 }
155
156 $template->param(
157     subscriptionid => $subscriptionid,
158     serialslist => \@serialslist,
159     hasRouting  => $hasRouting,
160     routing => C4::Context->preference("RoutingSerials"),
161     totalissues => $totalissues,
162     cannotedit => (not C4::Serials::can_edit_subscription( $subs )),
163     frequency => $frequency,
164     numberpattern => $numberpattern,
165     has_X           => ($numberpattern->{'numberingmethod'} =~ /{X}/) ? 1 : 0,
166     has_Y           => ($numberpattern->{'numberingmethod'} =~ /{Y}/) ? 1 : 0,
167     has_Z           => ($numberpattern->{'numberingmethod'} =~ /{Z}/) ? 1 : 0,
168     intranetstylesheet => C4::Context->preference('intranetstylesheet'),
169     intranetcolorstylesheet => C4::Context->preference('intranetcolorstylesheet'),
170     irregular_issues => scalar @irregular_issues,
171     default_bib_view => $default_bib_view,
172     orders_grouped => $orders_grouped,
173     (uc(C4::Context->preference("marcflavour"))) => 1,
174     mana_comments => $subs->{comments},
175 );
176
177 output_html_with_http_headers $query, $cookie, $template->output;
178
179 sub get_default_view {
180     my $defaultview = C4::Context->preference('IntranetBiblioDefaultView');
181     my %views       = C4::Search::enabled_staff_search_views();
182     if ( $defaultview eq 'isbd' && $views{can_view_ISBD} ) {
183         return 'ISBDdetail';
184     }
185     elsif ( $defaultview eq 'marc' && $views{can_view_MARC} ) {
186         return 'MARCdetail';
187     }
188     elsif ( $defaultview eq 'labeled_marc' && $views{can_view_labeledMARC} ) {
189         return 'labeledMARCdetail';
190     }
191     return 'detail';
192 }