Bug 11944: use CGI( -utf8 ) everywhere
[koha.git] / acqui / transferorder.pl
1 #!/usr/bin/perl
2
3 # Script to move an order from a bookseller to another
4
5 # Copyright 2011 BibLibre SARL
6 #
7 # This file is part of Koha.
8 #
9 # Koha is free software; you can redistribute it and/or modify it
10 # under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 3 of the License, or
12 # (at your option) any later version.
13 #
14 # Koha is distributed in the hope that it will be useful, but
15 # WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with Koha; if not, see <http://www.gnu.org/licenses>
21
22 use Modern::Perl;
23 use CGI qw ( -utf8 );
24
25 use C4::Auth;
26 use C4::Output;
27 use C4::Context;
28 use C4::Acquisition;
29 use C4::Members;
30 use C4::Dates qw/format_date_in_iso/;
31 use Date::Calc qw/Today/;
32
33 my $input = new CGI;
34 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
35     {   template_name   => "acqui/transferorder.tt",
36         query           => $input,
37         type            => "intranet",
38         flagsrequired   => { acquisition => 'order_manage' },
39     }
40 );
41
42 my $dbh = C4::Context->dbh;
43
44 my $bookselleridfrom    = $input->param('bookselleridfrom');
45 my $ordernumber     = $input->param('ordernumber');
46 my $bookselleridto  = $input->param('bookselleridto');
47 my $basketno        = $input->param('basketno');
48 my $op              = $input->param('op');
49 my $query           = $input->param('query');
50
51 my $order = GetOrder($ordernumber);
52 if($order) {
53     my $basket = GetBasket($order->{basketno});
54     $bookselleridfrom = $basket->{booksellerid} if $basket;
55 }
56
57 my $booksellerfrom = Koha::Acquisition::Bookseller->fetch({ id => $bookselleridfrom });
58 my $booksellerfromname;
59 if($booksellerfrom){
60     $booksellerfromname = $booksellerfrom->{name};
61 }
62 my $booksellerto = Koha::Acquisition::Bookseller->fetch({ id => $bookselleridto });
63 my $booksellertoname;
64 if($booksellerto){
65     $booksellertoname = $booksellerto->{name};
66 }
67
68 if( $basketno && $ordernumber) {
69     # Transfer order and exit
70     my $order = GetOrder( $ordernumber );
71     my $basket = GetBasket($order->{basketno});
72     my $booksellerfrom = Koha::Acquisition::Bookseller->fetch({ id => $basket->{booksellerid} });
73     my $bookselleridfrom = $booksellerfrom->{id};
74
75     TransferOrder($ordernumber, $basketno);
76
77     $template->param(transferred => 1)
78 } elsif ( $bookselleridto && $ordernumber) {
79     # Show open baskets for this bookseller
80     my $order = GetOrder( $ordernumber );
81     my $basketfrom = GetBasket( $order->{basketno} );
82     my $booksellerfrom = Koha::Acquisition::Bookseller->fetch({ id => $basketfrom->{booksellerid} });
83     $booksellerfromname = $booksellerfrom->{name};
84     my $baskets = GetBasketsByBookseller( $bookselleridto );
85     my $basketscount = scalar @$baskets;
86     my @basketsloop = ();
87     for( my $i = 0 ; $i < $basketscount ; $i++ ){
88         my %line;
89         %line = %{ $baskets->[$i] };
90         my $createdby = GetMember(borrowernumber => $line{authorisedby});
91         $line{createdby} = "$createdby->{surname}, $createdby->{firstname}";
92         push @basketsloop, \%line unless $line{closedate};
93     }
94     $template->param(
95         show_baskets => 1,
96         basketsloop => \@basketsloop,
97         basketfromname => $basketfrom->{basketname},
98     );
99 } elsif ( $bookselleridfrom && !defined $ordernumber) {
100     # Show pending orders
101     my $pendingorders = SearchOrders({
102         booksellerid => $bookselleridfrom,
103         pending      => 1,
104     });
105     my $orderscount = scalar @$pendingorders;
106     my @ordersloop = ();
107     for( my $i = 0 ; $i < $orderscount ; $i++ ){
108         my %line;
109         %line = %{ $pendingorders->[$i] };
110         push @ordersloop, \%line;
111     }
112     $template->param(
113         ordersloop  => \@ordersloop,
114     );
115 } else {
116     # Search for booksellers to transfer from/to
117     $op = '' unless $op;
118     if( $op eq "do_search" ) {
119         my @booksellers = Koha::Acquisition::Bookseller->search({ name => $query });
120         $template->param(
121             query => $query,
122             do_search => 1,
123             booksellersloop => \@booksellers,
124         );
125     }
126 }
127
128 $template->param(
129     bookselleridfrom    => $bookselleridfrom,
130     booksellerfromname  => $booksellerfromname,
131     bookselleridto      => $bookselleridto,
132     booksellertoname    => $booksellertoname,
133     ordernumber         => $ordernumber,
134     basketno            => $basketno,
135 );
136
137 output_html_with_http_headers $input, $cookie, $template->output;
138