MT2743 : add filter on subscription branch to subscription claims
[koha.git] / serials / claims.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 strict;
19 use warnings;
20 use CGI;
21 use C4::Auth;
22 use C4::Serials;
23 use C4::Acquisition;
24 use C4::Output;
25 use C4::Bookseller;
26 use C4::Context;
27 use C4::Letters;
28 use C4::Branch;    # GetBranches GetBranchesLoop
29
30 my $input = CGI->new;
31
32 my $serialid = $input->param('serialid');
33 my $op = $input->param('op');
34 my $claimletter = $input->param('claimletter');
35 my $supplierid = $input->param('supplierid');
36 my $suppliername = $input->param('suppliername');
37 my $order = $input->param('order');
38 my $supplierlist = GetSuppliersWithLateIssues();
39 if ($supplierid) {
40     foreach my $s ( @{$supplierlist} ) {
41         if ($s->{id} == $supplierid ) {
42             $s->{selected} = 1;
43             last;
44         }
45     }
46 }
47
48 # open template first (security & userenv set here)
49 my ($template, $loggedinuser, $cookie)
50 = get_template_and_user({template_name => 'serials/claims.tmpl',
51             query => $input,
52             type => 'intranet',
53             authnotrequired => 0,
54             flagsrequired => {serials => 'claim_serials'},
55             debug => 1,
56             });
57
58 my @suploop;
59 for ( sort {$supplierlist{$a} cmp $supplierlist{$b} } keys %supplierlist ) {
60     my ($count, @dummy) = GetLateOrMissingIssues($_, "", $order);
61     push @suploop, {
62         id       => $_,
63         name     => $supplierlist{$_},
64         count    => $count,
65         selected => $_ == $supplierid,
66     };
67 }
68
69 my $letters = GetLetters('claimissues');
70 my @letters;
71 foreach (keys %{$letters}){
72     push @letters ,{code=>$_,name=> $letters->{$_}};
73 }
74
75 my $letter=((scalar(@letters)>1) || ($letters[0]->{name}||$letters[0]->{code}));
76 my  @missingissues;
77 my @supplierinfo;
78 if ($supplierid) {
79     @missingissues = GetLateOrMissingIssues($supplierid,$serialid,$order);
80     @supplierinfo=GetBookSeller($supplierid);
81 }
82
83 my $branchloop = GetBranchesLoop();
84 unshift @$branchloop, {value=> 'all',name=>''};
85
86 my $preview=0;
87 if($op && $op eq 'preview'){
88     $preview = 1;
89 } else {
90     my @serialnums=$input->param('serialid');
91     if (@serialnums) { # i.e. they have been flagged to generate claims
92         SendAlerts('claimissues',\@serialnums,$input->param("letter_code"));
93         my $cntupdate=UpdateClaimdateIssues(\@serialnums);
94         ### $cntupdate SHOULD be equal to scalar(@$serialnums)
95     }
96 }
97 $template->param('letters'=>\@letters,'letter'=>$letter);
98 $template->param(
99         order =>$order,
100         suploop => \@suploop,
101         phone => $supplierinfo[0]->{phone},
102         booksellerfax => $supplierinfo[0]->{booksellerfax},
103         bookselleremail => $supplierinfo[0]->{bookselleremail},
104         preview => $preview,
105         missingissues => \@missingissues,
106         supplierid => $supplierid,
107         claimletter => $claimletter,
108         supplierloop => \@supplierinfo,
109         branchloop   => $branchloop,
110         dateformat    => C4::Context->preference("dateformat"),
111         DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
112         );
113 output_html_with_http_headers $input, $cookie, $template->output;