Bug 19528: Fix a few typos like corrosponding
[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 happend, 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 $subs->{enddate} ||= GetExpirationDate($subscriptionid);
66
67 my ($totalissues,@serialslist) = GetSerials($subscriptionid);
68 $totalissues-- if $totalissues; # the -1 is to have 0 if this is a new subscription (only 1 issue)
69
70 if ($op eq 'del') {
71         if ($$subs{'cannotedit'}){
72                 carp "Attempt to delete subscription $subscriptionid by ".C4::Context->userenv->{'id'}." not allowed";
73                 print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");
74                 exit;
75         }
76         
77     # Asking for confirmation if the subscription has not strictly expired yet or if it has linked issues
78     my $strictlyexpired = HasSubscriptionStrictlyExpired($subscriptionid);
79     my $linkedissues = CountIssues($subscriptionid);
80     my $countitems   = HasItems($subscriptionid);
81     if ($strictlyexpired == 0 || $linkedissues > 0 || $countitems>0) {
82                 $template->param(NEEDSCONFIRMATION => 1);
83                 if ($strictlyexpired == 0) { $template->param("NOTEXPIRED" => 1); }
84                 if ($linkedissues     > 0) { $template->param("LINKEDISSUES" => 1); }
85                 if ($countitems       > 0) { $template->param("LINKEDITEMS"  => 1); }
86     } else {
87                 $issueconfirmed = "1";
88     }
89     # If it's ok to delete the subscription, we do so
90     if ($issueconfirmed eq "1") {
91                 &DelSubscription($subscriptionid);
92         print $query->redirect("/cgi-bin/koha/serials/serials-home.pl");
93         exit;
94     }
95 }
96 my $hasRouting = check_routing($subscriptionid);
97
98 (undef, $cookie, undef, undef)
99     = checkauth($query, 0, {catalogue => 1}, "intranet");
100
101 # COMMENT hdl : IMHO, we should think about passing more and more data hash to template->param rather than duplicating code a new coding Guideline ?
102
103 for my $date ( qw(startdate enddate firstacquidate histstartdate histenddate) ) {
104     $subs->{$date} = output_pref( { str => $subs->{$date}, dateonly => 1 } )
105         if $subs->{$date};
106 }
107 my $av = Koha::AuthorisedValues->search({ category => 'LOC', authorised_value => $subs->{location} });
108 $subs->{location} = $av->count ? $av->next->lib : '';
109 $subs->{abouttoexpire}  = abouttoexpire($subs->{subscriptionid});
110 $template->param(%{ $subs });
111 $template->param(biblionumber_for_new_subscription => $subs->{bibnum});
112 my @irregular_issues = split /;/, $subs->{irregularity};
113
114 my $frequency = C4::Serials::Frequency::GetSubscriptionFrequency($subs->{periodicity});
115 my $numberpattern = C4::Serials::Numberpattern::GetSubscriptionNumberpattern($subs->{numberpattern});
116
117 my $default_bib_view = get_default_view();
118
119 my ( $order, $bookseller, $tmpl_infos );
120 if ( defined $subscriptionid ) {
121     my $lastOrderNotReceived = GetLastOrderNotReceivedFromSubscriptionid $subscriptionid;
122     my $lastOrderReceived = GetLastOrderReceivedFromSubscriptionid $subscriptionid;
123     if ( defined $lastOrderNotReceived ) {
124         my $basket = GetBasket $lastOrderNotReceived->{basketno};
125         my $bookseller = Koha::Acquisition::Booksellers->find( $basket->{booksellerid} );
126         ( $tmpl_infos->{value_tax_included_ordered}, $tmpl_infos->{value_tax_excluded_ordered} ) = get_value_with_gst_params ( $lastOrderNotReceived->{ecost}, $lastOrderNotReceived->{tax_rate}, $bookseller );
127         $tmpl_infos->{value_tax_included_ordered} = sprintf( "%.2f", $tmpl_infos->{value_tax_included_ordered} );
128         $tmpl_infos->{value_tax_excluded_ordered} = sprintf( "%.2f", $tmpl_infos->{value_tax_excluded_ordered} );
129         $tmpl_infos->{budget_name_ordered} = GetBudgetName $lastOrderNotReceived->{budget_id};
130         $tmpl_infos->{basketno} = $lastOrderNotReceived->{basketno};
131         $tmpl_infos->{ordered_exists} = 1;
132     }
133     if ( defined $lastOrderReceived ) {
134         my $basket = GetBasket $lastOrderReceived->{basketno};
135         my $bookseller = Koha::Acquisition::Booksellers->find( $basket->{booksellerid} );
136         ( $tmpl_infos->{value_tax_included_spent}, $tmpl_infos->{value_tax_excluded_spent} ) = get_value_with_gst_params ( $lastOrderReceived->{unitprice}, $lastOrderReceived->{tax_rate}, $bookseller );
137         $tmpl_infos->{value_tax_included_spent} = sprintf( "%.2f", $tmpl_infos->{value_tax_included_spent} );
138         $tmpl_infos->{value_tax_excluded_spent} = sprintf( "%.2f", $tmpl_infos->{value_tax_excluded_spent} );
139         $tmpl_infos->{budget_name_spent} = GetBudgetName $lastOrderReceived->{budget_id};
140         $tmpl_infos->{invoiceid} = $lastOrderReceived->{invoiceid};
141         $tmpl_infos->{spent_exists} = 1;
142     }
143 }
144
145 my $additional_fields = Koha::AdditionalField->all( { tablename => 'subscription' } );
146 for my $field ( @$additional_fields ) {
147     if ( $field->{authorised_value_category} ) {
148         $field->{authorised_value_choices} = GetAuthorisedValues( $field->{authorised_value_category} );
149     }
150 }
151 $template->param( additional_fields_for_subscription => $additional_fields );
152
153 $template->param(
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     default_bib_view => $default_bib_view,
169     (uc(C4::Context->preference("marcflavour"))) => 1,
170     show_acquisition_details => defined $tmpl_infos->{ordered_exists} || defined $tmpl_infos->{spent_exists} ? 1 : 0,
171     basketno => $order->{basketno},
172     %$tmpl_infos,
173 );
174
175 output_html_with_http_headers $query, $cookie, $template->output;
176
177 sub get_default_view {
178     my $defaultview = C4::Context->preference('IntranetBiblioDefaultView');
179     my %views       = C4::Search::enabled_staff_search_views();
180     if ( $defaultview eq 'isbd' && $views{can_view_ISBD} ) {
181         return 'ISBDdetail';
182     }
183     elsif ( $defaultview eq 'marc' && $views{can_view_MARC} ) {
184         return 'MARCdetail';
185     }
186     elsif ( $defaultview eq 'labeled_marc' && $views{can_view_labeledMARC} ) {
187         return 'labeledMARCdetail';
188     }
189     return 'detail';
190 }
191
192 sub get_value_with_gst_params {
193     my $value = shift;
194     my $tax_rate = shift;
195     my $bookseller = shift;
196     if ( $bookseller->listincgst ) {
197         return ( $value, $value / ( 1 + $tax_rate ) );
198     } else {
199         return ( $value * ( 1 + $tax_rate ), $value );
200     }
201 }
202
203 sub get_tax_excluded {
204     my $value = shift;
205     my $tax_rate = shift;
206     my $bookseller = shift;
207     if ( $bookseller->invoiceincgst ) {
208         return $value / ( 1 + $tax_rate );
209     } else {
210         return $value;
211     }
212 }
213
214 sub get_gst {
215     my $value = shift;
216     my $tax_rate = shift;
217     my $bookseller = shift;
218     if ( $bookseller->invoiceincgst ) {
219         return $value / ( 1 + $tax_rate ) * $tax_rate;
220     } else {
221         return $value * ( 1 + $tax_rate ) - $value;
222     }
223 }