Bug 10572: DBrev 3.13.00.037
[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 under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 2 of the License, or (at your option) any later
8 # version.
9 #
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License along
15 # with Koha; if not, write to the Free Software Foundation, Inc.,
16 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
18 use Modern::Perl;
19 use CGI;
20 use C4::Acquisition;
21 use C4::Auth;
22 use C4::Bookseller qw/GetBookSellerFromId/;
23 use C4::Budgets;
24 use C4::Koha;
25 use C4::Dates qw/format_date/;
26 use C4::Serials;
27 use C4::Output;
28 use C4::Context;
29 use C4::Search qw/enabled_staff_search_views/;
30 use Date::Calc qw/Today Day_of_Year Week_of_Year Add_Delta_Days/;
31 use Carp;
32
33 my $query = new CGI;
34 my $op = $query->param('op') || q{};
35 my $issueconfirmed = $query->param('issueconfirmed');
36 my $dbh = C4::Context->dbh;
37 my ($template, $loggedinuser, $cookie, $hemisphere);
38 my $subscriptionid = $query->param('subscriptionid');
39
40 if ( $op and $op eq "close" ) {
41     C4::Serials::CloseSubscription( $subscriptionid );
42 } elsif ( $op and $op eq "reopen" ) {
43     C4::Serials::ReopenSubscription( $subscriptionid );
44 }
45
46 my $subs = GetSubscription($subscriptionid);
47
48 $subs->{enddate} = GetExpirationDate($subscriptionid);
49
50 if ($op && $op eq 'del') {
51         if ($subs->{'cannotedit'}){
52                 carp "Attempt to delete subscription $subscriptionid by ".C4::Context->userenv->{'id'}." not allowed";
53                 print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");
54         }
55         DelSubscription($subscriptionid);
56         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=serials-home.pl\"></html>";
57         exit;
58 }
59
60 my ($totalissues,@serialslist) = GetSerials($subscriptionid);
61 $totalissues-- if $totalissues; # the -1 is to have 0 if this is a new subscription (only 1 issue)
62 # the subscription must be deletable if there is NO issues for a reason or another (should not happend, but...)
63
64 # Permission needed if it is a deletion (del) : delete_subscription
65 # Permission needed otherwise : *
66 my $permission = ($op eq "del") ? "delete_subscription" : "*";
67
68 ($template, $loggedinuser, $cookie)
69 = get_template_and_user({template_name => "serials/subscription-detail.tmpl",
70                 query => $query,
71                 type => "intranet",
72                 authnotrequired => 0,
73                 flagsrequired => {serials => $permission},
74                 debug => 1,
75                 });
76
77 $$subs{enddate} ||= GetExpirationDate($subscriptionid);
78
79 if ($op eq 'del') {
80         if ($$subs{'cannotedit'}){
81                 carp "Attempt to delete subscription $subscriptionid by ".C4::Context->userenv->{'id'}." not allowed";
82                 print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");
83                 exit;
84         }
85         
86     # Asking for confirmation if the subscription has not strictly expired yet or if it has linked issues
87     my $strictlyexpired = HasSubscriptionStrictlyExpired($subscriptionid);
88     my $linkedissues = CountIssues($subscriptionid);
89     my $countitems   = HasItems($subscriptionid);
90     if ($strictlyexpired == 0 || $linkedissues > 0 || $countitems>0) {
91                 $template->param(NEEDSCONFIRMATION => 1);
92                 if ($strictlyexpired == 0) { $template->param("NOTEXPIRED" => 1); }
93                 if ($linkedissues     > 0) { $template->param("LINKEDISSUES" => 1); }
94                 if ($countitems       > 0) { $template->param("LINKEDITEMS"  => 1); }
95     } else {
96                 $issueconfirmed = "1";
97     }
98     # If it's ok to delete the subscription, we do so
99     if ($issueconfirmed eq "1") {
100                 &DelSubscription($subscriptionid);
101                 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=serials-home.pl\"></html>";
102                 exit;
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}      = format_date($$subs{$date}) if $date && $$subs{$date};
114 }
115 $subs->{location} = GetKohaAuthorisedValueLib("LOC",$subs->{location});
116 $subs->{abouttoexpire}  = abouttoexpire($subs->{subscriptionid});
117 $template->param(%{ $subs });
118 $template->param(biblionumber_for_new_subscription => $subs->{bibnum});
119 my @irregular_issues = split /,/, $subs->{irregularity};
120
121 my $frequency = C4::Serials::Frequency::GetSubscriptionFrequency($subs->{periodicity});
122 my $numberpattern = C4::Serials::Numberpattern::GetSubscriptionNumberpattern($subs->{numberpattern});
123
124 my $default_bib_view = get_default_view();
125
126 my ( $order, $bookseller, $tmpl_infos );
127 if ( defined $subscriptionid ) {
128     my $lastOrderNotReceived = GetLastOrderNotReceivedFromSubscriptionid $subscriptionid;
129     my $lastOrderReceived = GetLastOrderReceivedFromSubscriptionid $subscriptionid;
130     if ( defined $lastOrderNotReceived ) {
131         my $basket = GetBasket $lastOrderNotReceived->{basketno};
132         my $bookseller = GetBookSellerFromId $basket->{booksellerid};
133         ( $tmpl_infos->{valuegsti_ordered}, $tmpl_infos->{valuegste_ordered} ) = get_value_with_gst_params ( $lastOrderNotReceived->{ecost}, $lastOrderNotReceived->{gstrate}, $bookseller );
134         $tmpl_infos->{valuegsti_ordered} = sprintf( "%.2f", $tmpl_infos->{valuegsti_ordered} );
135         $tmpl_infos->{valuegste_ordered} = sprintf( "%.2f", $tmpl_infos->{valuegste_ordered} );
136         $tmpl_infos->{budget_name_ordered} = GetBudgetName $lastOrderNotReceived->{budget_id};
137         $tmpl_infos->{basketno} = $lastOrderNotReceived->{basketno};
138         $tmpl_infos->{ordered_exists} = 1;
139     }
140     if ( defined $lastOrderReceived ) {
141         my $basket = GetBasket $lastOrderReceived->{basketno};
142         my $bookseller = GetBookSellerFromId $basket->{booksellerid};
143         ( $tmpl_infos->{valuegsti_spent}, $tmpl_infos->{valuegste_spent} ) = get_value_with_gst_params ( $lastOrderReceived->{unitprice}, $lastOrderReceived->{gstrate}, $bookseller );
144         $tmpl_infos->{valuegsti_spent} = sprintf( "%.2f", $tmpl_infos->{valuegsti_spent} );
145         $tmpl_infos->{valuegste_spent} = sprintf( "%.2f", $tmpl_infos->{valuegste_spent} );
146         $tmpl_infos->{budget_name_spent} = GetBudgetName $lastOrderReceived->{budget_id};
147         $tmpl_infos->{invoiceid} = $lastOrderReceived->{invoiceid};
148         $tmpl_infos->{spent_exists} = 1;
149     }
150 }
151
152 $template->param(
153     subscriptionid => $subscriptionid,
154     serialslist => \@serialslist,
155     hasRouting  => $hasRouting,
156     routing => C4::Context->preference("RoutingSerials"),
157     totalissues => $totalissues,
158     hemisphere => $hemisphere,
159     cannotedit =>(C4::Context->preference('IndependentBranches') &&
160                 C4::Context->userenv &&
161                 C4::Context->userenv->{flags} % 2 !=1  &&
162                 C4::Context->userenv->{branch} && $subs->{branchcode} &&
163                 (C4::Context->userenv->{branch} ne $subs->{branchcode})),
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 $gstrate = shift;
199     my $bookseller = shift;
200     if ( $bookseller->{listincgst} ) {
201         return ( $value, $value / ( 1 + $gstrate ) );
202     } else {
203         return ( $value * ( 1 + $gstrate ), $value );
204     }
205 }
206
207 sub get_gste {
208     my $value = shift;
209     my $gstrate = shift;
210     my $bookseller = shift;
211     if ( $bookseller->{invoiceincgst} ) {
212         return $value / ( 1 + $gstrate );
213     } else {
214         return $value;
215     }
216 }
217
218 sub get_gst {
219     my $value = shift;
220     my $gstrate = shift;
221     my $bookseller = shift;
222     if ( $bookseller->{invoiceincgst} ) {
223         return $value / ( 1 + $gstrate ) * $gstrate;
224     } else {
225         return $value * ( 1 + $gstrate ) - $value;
226     }
227 }