Bug 18013 - acqui/transferorder.pl typo in find method
[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 Koha::Acquisition::Booksellers;
31
32 my $input = new CGI;
33 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
34     {   template_name   => "acqui/transferorder.tt",
35         query           => $input,
36         type            => "intranet",
37         flagsrequired   => { acquisition => 'order_manage' },
38     }
39 );
40
41 my $dbh = C4::Context->dbh;
42
43 my $bookselleridfrom    = $input->param('bookselleridfrom');
44 my $ordernumber     = $input->param('ordernumber');
45 my $bookselleridto  = $input->param('bookselleridto');
46 my $basketno        = $input->param('basketno');
47 my $op              = $input->param('op');
48 my $query           = $input->param('query');
49
50 my $order = GetOrder($ordernumber);
51 my $basketfromname = '';
52 if($order) {
53     my $basket = GetBasket($order->{basketno});
54     $basketfromname = $basket->{basketname};
55     $bookselleridfrom = $basket->{booksellerid} if $basket;
56 }
57
58 my $booksellerfrom = Koha::Acquisition::Booksellers->find( $bookselleridfrom );
59 my $booksellerfromname;
60 if($booksellerfrom){
61     $booksellerfromname = $booksellerfrom->name;
62 }
63 my $booksellerto = Koha::Acquisition::Booksellers->find( $bookselleridto );
64 my $booksellertoname;
65 if($booksellerto){
66     $booksellertoname = $booksellerto->name;
67 }
68
69
70 if( $basketno && $ordernumber) {
71     # Transfer order and exit
72     my $order = GetOrder( $ordernumber );
73     my $basket = GetBasket($order->{basketno});
74     my $booksellerfrom = Koha::Acquisition::Booksellers->find( $basket->{booksellerid} );
75     my $bookselleridfrom = $booksellerfrom->id;
76
77     TransferOrder($ordernumber, $basketno);
78
79     $template->param(transferred => 1)
80 } elsif ( $bookselleridto && $ordernumber) {
81     # Show open baskets for this bookseller
82     my $order = GetOrder( $ordernumber );
83     my $basketfrom = GetBasket( $order->{basketno} );
84     my $booksellerfrom = Koha::Acquisition::Booksellers->find( $basketfrom->{booksellerid} );
85     $booksellerfromname = $booksellerfrom->name;
86     my $baskets = GetBasketsByBookseller( $bookselleridto );
87     my $basketscount = scalar @$baskets;
88     my @basketsloop = ();
89     for( my $i = 0 ; $i < $basketscount ; $i++ ){
90         my %line;
91         %line = %{ $baskets->[$i] };
92         my $createdby = GetMember(borrowernumber => $line{authorisedby});
93         $line{createdby} = "$createdby->{surname}, $createdby->{firstname}";
94         push @basketsloop, \%line unless $line{closedate};
95     }
96     $template->param(
97         show_baskets => 1,
98         basketsloop => \@basketsloop,
99         basketfromname => $basketfrom->{basketname},
100     );
101 } elsif ( $bookselleridfrom && !defined $ordernumber) {
102     # Show pending orders
103     my $pendingorders = SearchOrders({
104         booksellerid => $bookselleridfrom,
105         pending      => 1,
106     });
107     my $orderscount = scalar @$pendingorders;
108     my @ordersloop = ();
109     for( my $i = 0 ; $i < $orderscount ; $i++ ){
110         my %line;
111         %line = %{ $pendingorders->[$i] };
112         push @ordersloop, \%line;
113     }
114     $template->param(
115         ordersloop  => \@ordersloop,
116     );
117 } else {
118     # Search for booksellers to transfer from/to
119     $op = '' unless $op;
120     if( $op eq "do_search" ) {
121         my @booksellers = Koha::Acquisition::Booksellers->search({ name => $query });
122         $template->param(
123             query => $query,
124             do_search => 1,
125             booksellersloop => \@booksellers,
126         );
127     }
128 }
129
130 $template->param(
131     bookselleridfrom    => $bookselleridfrom,
132     booksellerfromname  => $booksellerfromname,
133     bookselleridto      => $bookselleridto,
134     booksellertoname    => $booksellertoname,
135     ordernumber         => $ordernumber,
136     basketno            => $basketno,
137     basketfromname      => $basketfromname,
138 );
139
140 output_html_with_http_headers $input, $cookie, $template->output;
141