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