Bug 14358: Changing the module refreshes the page and resets library choice
[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 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');
41
42 if ( $op and $op eq "close" ) {
43     C4::Serials::CloseSubscription( $subscriptionid );
44 } elsif ( $op and $op eq "reopen" ) {
45     C4::Serials::ReopenSubscription( $subscriptionid );
46 }
47
48 # the subscription must be deletable if there is NO issues for a reason or another (should not happened, but...)
49
50 # Permission needed if it is a deletion (del) : delete_subscription
51 # Permission needed otherwise : *
52 my $permission = ($op eq "del") ? "delete_subscription" : "*";
53
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                 });
62
63
64 my $subs = GetSubscription($subscriptionid);
65
66 output_and_exit( $query, $cookie, $template, 'unknown_subscription')
67     unless $subs;
68
69 $subs->{enddate} ||= GetExpirationDate($subscriptionid);
70
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)
73
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;
79         }
80         
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";
92     }
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;
98     }
99 }
100 my $hasRouting = check_routing($subscriptionid);
101
102 (undef, $cookie, undef, undef)
103     = checkauth($query, 0, {catalogue => 1}, "intranet");
104
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 ?
106
107 for my $date ( qw(startdate enddate firstacquidate histstartdate histenddate) ) {
108     $subs->{$date} = output_pref( { str => $subs->{$date}, dateonly => 1 } )
109         if $subs->{$date};
110 }
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};
117
118 my $frequency = C4::Serials::Frequency::GetSubscriptionFrequency($subs->{periodicity});
119 my $numberpattern = C4::Serials::Numberpattern::GetSubscriptionNumberpattern($subs->{numberpattern});
120
121 my $default_bib_view = get_default_view();
122
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;
136     }
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;
146     }
147 }
148
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} );
153     }
154 }
155 $template->param( additional_fields_for_subscription => $additional_fields );
156
157 $template->param(
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},
176     %$tmpl_infos,
177 );
178
179 output_html_with_http_headers $query, $cookie, $template->output;
180
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} ) {
185         return 'ISBDdetail';
186     }
187     elsif ( $defaultview eq 'marc' && $views{can_view_MARC} ) {
188         return 'MARCdetail';
189     }
190     elsif ( $defaultview eq 'labeled_marc' && $views{can_view_labeledMARC} ) {
191         return 'labeledMARCdetail';
192     }
193     return 'detail';
194 }
195
196 sub get_value_with_gst_params {
197     my $value = shift;
198     my $tax_rate = shift;
199     my $bookseller = shift;
200     if ( $bookseller->listincgst ) {
201         return ( $value, $value / ( 1 + $tax_rate ) );
202     } else {
203         return ( $value * ( 1 + $tax_rate ), $value );
204     }
205 }
206
207 sub get_tax_excluded {
208     my $value = shift;
209     my $tax_rate = shift;
210     my $bookseller = shift;
211     if ( $bookseller->invoiceincgst ) {
212         return $value / ( 1 + $tax_rate );
213     } else {
214         return $value;
215     }
216 }
217
218 sub get_gst {
219     my $value = shift;
220     my $tax_rate = shift;
221     my $bookseller = shift;
222     if ( $bookseller->invoiceincgst ) {
223         return $value / ( 1 + $tax_rate ) * $tax_rate;
224     } else {
225         return $value * ( 1 + $tax_rate ) - $value;
226     }
227 }