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>.
24 use C4::Auth qw( get_template_and_user );
25 use C4::Output qw( output_and_exit output_html_with_http_headers );
26 use C4::Acquisition qw( GetHistory );
27 use C4::Budgets qw( GetBudgetHierarchy GetBudget CanUserUseBudget GetBudgetPeriods GetBudgetPeriod );
28 use Koha::Acquisition::Baskets;
29 use Koha::Acquisition::Currencies;
30 use Koha::Acquisition::Orders;
31 use Koha::DateUtils qw( dt_from_string );
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",
42 flagsrequired => { acquisition => 'order_manage' },
46 my $basket = Koha::Acquisition::Baskets->find($basketno);
48 output_and_exit( $input, $cookie, $template, 'unknown_basket' )
51 my $vendor = $basket->bookseller;
52 my $patron = Koha::Patrons->find($loggedinuser)->unblessed;
55 basket => scalar $input->param('basket'),
56 title => scalar $input->param('title'),
57 author => scalar $input->param('author'),
58 isbn => scalar $input->param('isbn'),
59 name => scalar $input->param('name'),
60 ean => scalar $input->param('ean'),
61 basketgroupname => scalar $input->param('basketgroupname'),
62 booksellerinvoicenumber => scalar $input->param('booksellerinvoicenumber'),
63 budget => scalar $input->param('budget'),
64 orderstatus => scalar $input->param('orderstatus'),
65 ordernumber => scalar $input->param('ordernumber'),
66 search_children_too => scalar $input->param('search_children_too'),
67 created_by => [$input->multi_param('created_by')]
71 eval { dt_from_string( scalar $input->param('from') ) } || dt_from_string;
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} = $from_placed_on;
80 $filters->{to_placed_on} = $to_placed_on;
82 my ( @result_order_loop, @selected_order_loop );
83 my @ordernumbers = split ',', scalar $input->param('ordernumbers') || '';
84 if ( $op eq 'select' ) {
85 @result_order_loop = map {
87 ( grep {$_ eq $order->{ordernumber}} @ordernumbers ) ? () : $order
88 } @{ C4::Acquisition::GetHistory(%$filters) };
90 @selected_order_loop =
92 ? @{ C4::Acquisition::GetHistory( ordernumbers => \@ordernumbers ) }
95 elsif ( $op eq 'batch_edit' ) {
96 @ordernumbers = $input->multi_param('ordernumber');
100 my $budgets_hierarchy = GetBudgetHierarchy;
101 foreach my $r ( @{$budgets_hierarchy} ) {
103 unless ( C4::Budgets::CanUserUseBudget( $patron, $r, $userflags ) );
105 push @{$budget_loop},
107 b_id => $r->{budget_id},
108 b_txt => $r->{budget_name},
109 b_code => $r->{budget_code},
110 b_sort1_authcat => $r->{'sort1_authcat'},
111 b_sort2_authcat => $r->{'sort2_authcat'},
112 b_active => $r->{budget_period_active},
116 sort { uc( $a->{b_txt} ) cmp uc( $b->{b_txt} ) } @{$budget_loop};
119 currencies => Koha::Acquisition::Currencies->search,
120 budget_loop => $budget_loop,
123 elsif ( $op eq 'do_duplicate' ) {
124 my @fields_to_copy = $input->multi_param('copy_existing_value');
128 qw(currency budget_id order_internalnote order_vendornote sort1 sort2 ))
130 next if grep { $_ eq $field } @fields_to_copy;
131 $default_values->{$field} = $input->param("all_$field");
134 @ordernumbers = $input->multi_param('ordernumber');
135 my @new_ordernumbers;
136 for my $ordernumber (@ordernumbers) {
137 my $original_order = Koha::Acquisition::Orders->find($ordernumber);
138 next unless $original_order;
140 $original_order->duplicate_to( $basket, $default_values );
141 push @new_ordernumbers, $new_order->ordernumber;
145 C4::Acquisition::GetHistory( ordernumbers => \@new_ordernumbers );
146 $template->param( new_orders => $new_orders );
147 $op = 'duplication_done';
150 my $budgetperiods = C4::Budgets::GetBudgetPeriods;
151 my $bp_loop = $budgetperiods;
152 for my $bp ( @{$budgetperiods} ) {
153 my $hierarchy = C4::Budgets::GetBudgetHierarchy( $$bp{budget_period_id} );
154 for my $budget ( @{$hierarchy} ) {
155 $$budget{budget_display_name} =
156 sprintf( "%s", ">" x $$budget{depth} . $$budget{budget_name} );
158 $$bp{hierarchy} = $hierarchy;
165 result_order_loop => \@result_order_loop,
166 selected_order_loop => \@selected_order_loop,
168 ordernumbers => \@ordernumbers,
172 output_html_with_http_headers $input, $cookie, $template->output;