Bug 15152: t/db_dependent/Reports_Guided.t should not depend on existing data
[srvgit] / 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 my $basketfromname = '';
53 if($order) {
54     my $basket = GetBasket($order->{basketno});
55     $basketfromname = $basket->{basketname};
56     $bookselleridfrom = $basket->{booksellerid} if $basket;
57 }
58
59 my $booksellerfrom = Koha::Acquisition::Bookseller->fetch({ id => $bookselleridfrom });
60 my $booksellerfromname;
61 if($booksellerfrom){
62     $booksellerfromname = $booksellerfrom->{name};
63 }
64 my $booksellerto = Koha::Acquisition::Bookseller->fetch({ id => $bookselleridto });
65 my $booksellertoname;
66 if($booksellerto){
67     $booksellertoname = $booksellerto->{name};
68 }
69
70
71 if( $basketno && $ordernumber) {
72     # Transfer order and exit
73     my $order = GetOrder( $ordernumber );
74     my $basket = GetBasket($order->{basketno});
75     my $booksellerfrom = Koha::Acquisition::Bookseller->fetch({ id => $basket->{booksellerid} });
76     my $bookselleridfrom = $booksellerfrom->{id};
77
78     TransferOrder($ordernumber, $basketno);
79
80     $template->param(transferred => 1)
81 } elsif ( $bookselleridto && $ordernumber) {
82     # Show open baskets for this bookseller
83     my $order = GetOrder( $ordernumber );
84     my $basketfrom = GetBasket( $order->{basketno} );
85     my $booksellerfrom = Koha::Acquisition::Bookseller->fetch({ id => $basketfrom->{booksellerid} });
86     $booksellerfromname = $booksellerfrom->{name};
87     my $baskets = GetBasketsByBookseller( $bookselleridto );
88     my $basketscount = scalar @$baskets;
89     my @basketsloop = ();
90     for( my $i = 0 ; $i < $basketscount ; $i++ ){
91         my %line;
92         %line = %{ $baskets->[$i] };
93         my $createdby = GetMember(borrowernumber => $line{authorisedby});
94         $line{createdby} = "$createdby->{surname}, $createdby->{firstname}";
95         push @basketsloop, \%line unless $line{closedate};
96     }
97     $template->param(
98         show_baskets => 1,
99         basketsloop => \@basketsloop,
100         basketfromname => $basketfrom->{basketname},
101     );
102 } elsif ( $bookselleridfrom && !defined $ordernumber) {
103     # Show pending orders
104     my $pendingorders = SearchOrders({
105         booksellerid => $bookselleridfrom,
106         pending      => 1,
107     });
108     my $orderscount = scalar @$pendingorders;
109     my @ordersloop = ();
110     for( my $i = 0 ; $i < $orderscount ; $i++ ){
111         my %line;
112         %line = %{ $pendingorders->[$i] };
113         push @ordersloop, \%line;
114     }
115     $template->param(
116         ordersloop  => \@ordersloop,
117     );
118 } else {
119     # Search for booksellers to transfer from/to
120     $op = '' unless $op;
121     if( $op eq "do_search" ) {
122         my @booksellers = Koha::Acquisition::Bookseller->search({ name => $query });
123         $template->param(
124             query => $query,
125             do_search => 1,
126             booksellersloop => \@booksellers,
127         );
128     }
129 }
130
131 $template->param(
132     bookselleridfrom    => $bookselleridfrom,
133     booksellerfromname  => $booksellerfromname,
134     bookselleridto      => $bookselleridto,
135     booksellertoname    => $booksellertoname,
136     ordernumber         => $ordernumber,
137     basketno            => $basketno,
138     basketfromname      => $basketfromname,
139 );
140
141 output_html_with_http_headers $input, $cookie, $template->output;
142