Bug 24240: Move form closing tag outside of conditional
[koha.git] / acqui / duplicate_orders.pl
bloba4a02fc628fab800094f7de67c14b59e49c924ee
1 #!/usr/bin/perl
3 # Copyright 2018 Koha Development Team
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 use Modern::Perl;
22 use CGI qw ( -utf8 );
24 use C4::Auth;
25 use C4::Output;
26 use C4::Acquisition qw(GetHistory);
27 use C4::Budgets qw(GetBudgetPeriods GetBudgetHierarchy CanUserUseBudget);
28 use Koha::Acquisition::Baskets;
29 use Koha::Acquisition::Currencies;
30 use Koha::Acquisition::Orders;
31 use Koha::DateUtils qw(dt_from_string output_pref);
33 my $input = new CGI;
34 my $basketno = $input->param('basketno');
35 my $op = $input->param('op') || 'search'; # search, select, batch_edit
37 my ( $template, $loggedinuser, $cookie, $userflags ) = get_template_and_user(
39 template_name => "acqui/duplicate_orders.tt",
40 query => $input,
41 type => "intranet",
42 authnotrequired => 0,
43 flagsrequired => { acquisition => 'order_manage' },
47 my $basket = Koha::Acquisition::Baskets->find($basketno);
49 output_and_exit( $input, $cookie, $template, 'unknown_basket' )
50 unless $basket;
52 my $vendor = $basket->bookseller;
53 my $patron = Koha::Patrons->find($loggedinuser)->unblessed;
55 my $filters = {
56 basket => scalar $input->param('basket'),
57 title => scalar $input->param('title'),
58 author => scalar $input->param('author'),
59 isbn => scalar $input->param('isbn'),
60 name => scalar $input->param('name'),
61 ean => scalar $input->param('ean'),
62 basketgroupname => scalar $input->param('basketgroupname'),
63 booksellerinvoicenumber => scalar $input->param('booksellerinvoicenumber'),
64 budget => scalar $input->param('budget'),
65 orderstatus => scalar $input->param('orderstatus'),
66 ordernumber => scalar $input->param('ordernumber'),
67 search_children_too => scalar $input->param('search_children_too'),
68 created_by => scalar $input->multi_param('created_by'),
70 my $from_placed_on =
71 eval { dt_from_string( scalar $input->param('from') ) } || dt_from_string;
72 my $to_placed_on =
73 eval { dt_from_string( scalar $input->param('to') ) } || dt_from_string;
75 unless ( $input->param('from') ) {
76 # Fill the form with year-1
77 $from_placed_on->set_time_zone('floating')->subtract( years => 1 );
79 $filters->{from_placed_on} =
80 output_pref( { dt => $from_placed_on, dateformat => 'iso', dateonly => 1 } ),
81 $filters->{to_placed_on} =
82 output_pref( { dt => $to_placed_on, dateformat => 'iso', dateonly => 1 } ),
84 my ( @result_order_loop, @selected_order_loop );
85 my @ordernumbers = split ',', scalar $input->param('ordernumbers') || '';
86 if ( $op eq 'select' ) {
87 @result_order_loop = map {
88 my $order = $_;
89 ( grep { /^$order->{ordernumber}$/ } @ordernumbers ) ? () : $order
90 } @{ C4::Acquisition::GetHistory(%$filters) };
92 @selected_order_loop =
93 scalar @ordernumbers
94 ? @{ C4::Acquisition::GetHistory( ordernumbers => \@ordernumbers ) }
95 : ();
97 elsif ( $op eq 'batch_edit' ) {
98 @ordernumbers = $input->multi_param('ordernumber');
100 # build budget list
101 my $budget_loop = [];
102 my $budgets_hierarchy = GetBudgetHierarchy;
103 foreach my $r ( @{$budgets_hierarchy} ) {
104 next
105 unless ( C4::Budgets::CanUserUseBudget( $patron, $r, $userflags ) );
106 if ( !defined $r->{budget_amount} || $r->{budget_amount} == 0 ) {
107 next;
109 push @{$budget_loop},
111 b_id => $r->{budget_id},
112 b_txt => $r->{budget_name},
113 b_code => $r->{budget_code},
114 b_sort1_authcat => $r->{'sort1_authcat'},
115 b_sort2_authcat => $r->{'sort2_authcat'},
116 b_active => $r->{budget_period_active},
119 @{$budget_loop} =
120 sort { uc( $a->{b_txt} ) cmp uc( $b->{b_txt} ) } @{$budget_loop};
122 my @currencies = Koha::Acquisition::Currencies->search;
123 $template->param(
124 currencies => \@currencies,
125 budget_loop => $budget_loop,
128 elsif ( $op eq 'do_duplicate' ) {
129 my @fields_to_copy = $input->multi_param('copy_existing_value');
131 my $default_values;
132 for my $field (
133 qw(currency budget_id order_internalnote order_vendornote sort1 sort2 ))
135 next if grep { /^$field$/ } @fields_to_copy;
136 $default_values->{$field} = $input->param("all_$field");
139 @ordernumbers = $input->multi_param('ordernumber');
140 my @new_ordernumbers;
141 for my $ordernumber (@ordernumbers) {
142 my $original_order = Koha::Acquisition::Orders->find($ordernumber);
143 next unless $original_order;
144 my $new_order =
145 $original_order->duplicate_to( $basket, $default_values );
146 push @new_ordernumbers, $new_order->ordernumber;
149 my $new_orders =
150 C4::Acquisition::GetHistory( ordernumbers => \@new_ordernumbers );
151 $template->param( new_orders => $new_orders );
152 $op = 'duplication_done';
155 my $budgetperiods = C4::Budgets::GetBudgetPeriods;
156 my $bp_loop = $budgetperiods;
157 for my $bp ( @{$budgetperiods} ) {
158 my $hierarchy = C4::Budgets::GetBudgetHierarchy( $$bp{budget_period_id} );
159 for my $budget ( @{$hierarchy} ) {
160 $$budget{budget_display_name} =
161 sprintf( "%s", ">" x $$budget{depth} . $$budget{budget_name} );
163 $$bp{hierarchy} = $hierarchy;
166 $template->param(
167 basket => $basket,
168 vendor => $vendor,
169 filters => $filters,
170 result_order_loop => \@result_order_loop,
171 selected_order_loop => \@selected_order_loop,
172 bp_loop => $bp_loop,
173 ordernumbers => \@ordernumbers,
174 op => $op,
177 output_html_with_http_headers $input, $cookie, $template->output;