Bug 16699: Reference new x-primitives in currently defined objects
[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 use Koha::DateUtils;
29
30 use Koha::Acquisition::Bookseller;
31
32 use Date::Calc qw/Today Day_of_Year Week_of_Year Add_Delta_Days/;
33 use Carp;
34
35 my $query = new CGI;
36 my $op = $query->param('op') || q{};
37 my $issueconfirmed = $query->param('issueconfirmed');
38 my $dbh = C4::Context->dbh;
39 my $subscriptionid = $query->param('subscriptionid');
40
41 if ( $op and $op eq "close" ) {
42     C4::Serials::CloseSubscription( $subscriptionid );
43 } elsif ( $op and $op eq "reopen" ) {
44     C4::Serials::ReopenSubscription( $subscriptionid );
45 }
46
47 # the subscription must be deletable if there is NO issues for a reason or another (should not happend, but...)
48
49 # Permission needed if it is a deletion (del) : delete_subscription
50 # Permission needed otherwise : *
51 my $permission = ($op eq "del") ? "delete_subscription" : "*";
52
53 my ($template, $loggedinuser, $cookie)
54 = get_template_and_user({template_name => "serials/subscription-detail.tt",
55                 query => $query,
56                 type => "intranet",
57                 authnotrequired => 0,
58                 flagsrequired => {serials => $permission},
59                 debug => 1,
60                 });
61
62
63 my $subs = GetSubscription($subscriptionid);
64 $subs->{enddate} ||= GetExpirationDate($subscriptionid);
65
66 my ($totalissues,@serialslist) = GetSerials($subscriptionid);
67 $totalissues-- if $totalissues; # the -1 is to have 0 if this is a new subscription (only 1 issue)
68
69 if ($op eq 'del') {
70         if ($$subs{'cannotedit'}){
71                 carp "Attempt to delete subscription $subscriptionid by ".C4::Context->userenv->{'id'}." not allowed";
72                 print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");
73                 exit;
74         }
75         
76     # Asking for confirmation if the subscription has not strictly expired yet or if it has linked issues
77     my $strictlyexpired = HasSubscriptionStrictlyExpired($subscriptionid);
78     my $linkedissues = CountIssues($subscriptionid);
79     my $countitems   = HasItems($subscriptionid);
80     if ($strictlyexpired == 0 || $linkedissues > 0 || $countitems>0) {
81                 $template->param(NEEDSCONFIRMATION => 1);
82                 if ($strictlyexpired == 0) { $template->param("NOTEXPIRED" => 1); }
83                 if ($linkedissues     > 0) { $template->param("LINKEDISSUES" => 1); }
84                 if ($countitems       > 0) { $template->param("LINKEDITEMS"  => 1); }
85     } else {
86                 $issueconfirmed = "1";
87     }
88     # If it's ok to delete the subscription, we do so
89     if ($issueconfirmed eq "1") {
90                 &DelSubscription($subscriptionid);
91         print $query->redirect("/cgi-bin/koha/serials/serials-home.pl");
92         exit;
93     }
94 }
95 my $hasRouting = check_routing($subscriptionid);
96
97 (undef, $cookie, undef, undef)
98     = checkauth($query, 0, {catalogue => 1}, "intranet");
99
100 # COMMENT hdl : IMHO, we should think about passing more and more data hash to template->param rather than duplicating code a new coding Guideline ?
101
102 for my $date ( qw(startdate enddate firstacquidate histstartdate histenddate) ) {
103     $subs->{$date} = output_pref( { str => $subs->{$date}, dateonly => 1 } )
104         if $subs->{$date};
105 }
106 $subs->{location} = GetKohaAuthorisedValueLib("LOC",$subs->{location});
107 $subs->{abouttoexpire}  = abouttoexpire($subs->{subscriptionid});
108 $template->param(%{ $subs });
109 $template->param(biblionumber_for_new_subscription => $subs->{bibnum});
110 my @irregular_issues = split /;/, $subs->{irregularity};
111
112 my $frequency = C4::Serials::Frequency::GetSubscriptionFrequency($subs->{periodicity});
113 my $numberpattern = C4::Serials::Numberpattern::GetSubscriptionNumberpattern($subs->{numberpattern});
114
115 my $default_bib_view = get_default_view();
116
117 my ( $order, $bookseller, $tmpl_infos );
118 if ( defined $subscriptionid ) {
119     my $lastOrderNotReceived = GetLastOrderNotReceivedFromSubscriptionid $subscriptionid;
120     my $lastOrderReceived = GetLastOrderReceivedFromSubscriptionid $subscriptionid;
121     if ( defined $lastOrderNotReceived ) {
122         my $basket = GetBasket $lastOrderNotReceived->{basketno};
123         my $bookseller = Koha::Acquisition::Bookseller->fetch({ id => $basket->{booksellerid} });
124         ( $tmpl_infos->{valuegsti_ordered}, $tmpl_infos->{valuegste_ordered} ) = get_value_with_gst_params ( $lastOrderNotReceived->{ecost}, $lastOrderNotReceived->{gstrate}, $bookseller );
125         $tmpl_infos->{valuegsti_ordered} = sprintf( "%.2f", $tmpl_infos->{valuegsti_ordered} );
126         $tmpl_infos->{valuegste_ordered} = sprintf( "%.2f", $tmpl_infos->{valuegste_ordered} );
127         $tmpl_infos->{budget_name_ordered} = GetBudgetName $lastOrderNotReceived->{budget_id};
128         $tmpl_infos->{basketno} = $lastOrderNotReceived->{basketno};
129         $tmpl_infos->{ordered_exists} = 1;
130     }
131     if ( defined $lastOrderReceived ) {
132         my $basket = GetBasket $lastOrderReceived->{basketno};
133         my $bookseller = Koha::Acquisition::Bookseller->fetch({ id => $basket->{booksellerid} });
134         ( $tmpl_infos->{valuegsti_spent}, $tmpl_infos->{valuegste_spent} ) = get_value_with_gst_params ( $lastOrderReceived->{unitprice}, $lastOrderReceived->{gstrate}, $bookseller );
135         $tmpl_infos->{valuegsti_spent} = sprintf( "%.2f", $tmpl_infos->{valuegsti_spent} );
136         $tmpl_infos->{valuegste_spent} = sprintf( "%.2f", $tmpl_infos->{valuegste_spent} );
137         $tmpl_infos->{budget_name_spent} = GetBudgetName $lastOrderReceived->{budget_id};
138         $tmpl_infos->{invoiceid} = $lastOrderReceived->{invoiceid};
139         $tmpl_infos->{spent_exists} = 1;
140     }
141 }
142
143 my $additional_fields = Koha::AdditionalField->all( { tablename => 'subscription' } );
144 for my $field ( @$additional_fields ) {
145     if ( $field->{authorised_value_category} ) {
146         $field->{authorised_value_choices} = GetAuthorisedValues( $field->{authorised_value_category} );
147     }
148 }
149 $template->param( additional_fields_for_subscription => $additional_fields );
150
151 $template->param(
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     default_bib_view => $default_bib_view,
167     (uc(C4::Context->preference("marcflavour"))) => 1,
168     show_acquisition_details => defined $tmpl_infos->{ordered_exists} || defined $tmpl_infos->{spent_exists} ? 1 : 0,
169     basketno => $order->{basketno},
170     %$tmpl_infos,
171 );
172
173 output_html_with_http_headers $query, $cookie, $template->output;
174
175 sub get_default_view {
176     my $defaultview = C4::Context->preference('IntranetBiblioDefaultView');
177     my %views       = C4::Search::enabled_staff_search_views();
178     if ( $defaultview eq 'isbd' && $views{can_view_ISBD} ) {
179         return 'ISBDdetail';
180     }
181     elsif ( $defaultview eq 'marc' && $views{can_view_MARC} ) {
182         return 'MARCdetail';
183     }
184     elsif ( $defaultview eq 'labeled_marc' && $views{can_view_labeledMARC} ) {
185         return 'labeledMARCdetail';
186     }
187     return 'detail';
188 }
189
190 sub get_value_with_gst_params {
191     my $value = shift;
192     my $gstrate = shift;
193     my $bookseller = shift;
194     if ( $bookseller->{listincgst} ) {
195         return ( $value, $value / ( 1 + $gstrate ) );
196     } else {
197         return ( $value * ( 1 + $gstrate ), $value );
198     }
199 }
200
201 sub get_gste {
202     my $value = shift;
203     my $gstrate = shift;
204     my $bookseller = shift;
205     if ( $bookseller->{invoiceincgst} ) {
206         return $value / ( 1 + $gstrate );
207     } else {
208         return $value;
209     }
210 }
211
212 sub get_gst {
213     my $value = shift;
214     my $gstrate = shift;
215     my $bookseller = shift;
216     if ( $bookseller->{invoiceincgst} ) {
217         return $value / ( 1 + $gstrate ) * $gstrate;
218     } else {
219         return $value * ( 1 + $gstrate ) - $value;
220     }
221 }