Bug 24812: (bug 24081 follow-up) Add permission description
[koha.git] / acqui / duplicate_orders.pl
blobeb975da03561ea558846a3493cdb519bf44ff522
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 => [$input->multi_param('created_by')]
71 my $from_placed_on =
72 eval { dt_from_string( scalar $input->param('from') ) } || dt_from_string;
73 my $to_placed_on =
74 eval { dt_from_string( scalar $input->param('to') ) } || dt_from_string;
76 unless ( $input->param('from') ) {
77 # Fill the form with year-1
78 $from_placed_on->set_time_zone('floating')->subtract( years => 1 );
80 $filters->{from_placed_on} =
81 output_pref( { dt => $from_placed_on, dateformat => 'iso', dateonly => 1 } ),
82 $filters->{to_placed_on} =
83 output_pref( { dt => $to_placed_on, dateformat => 'iso', dateonly => 1 } ),
85 my ( @result_order_loop, @selected_order_loop );
86 my @ordernumbers = split ',', scalar $input->param('ordernumbers') || '';
87 if ( $op eq 'select' ) {
88 @result_order_loop = map {
89 my $order = $_;
90 ( grep {$_ eq $order->{ordernumber}} @ordernumbers ) ? () : $order
91 } @{ C4::Acquisition::GetHistory(%$filters) };
93 @selected_order_loop =
94 scalar @ordernumbers
95 ? @{ C4::Acquisition::GetHistory( ordernumbers => \@ordernumbers ) }
96 : ();
98 elsif ( $op eq 'batch_edit' ) {
99 @ordernumbers = $input->multi_param('ordernumber');
101 # build budget list
102 my $budget_loop = [];
103 my $budgets_hierarchy = GetBudgetHierarchy;
104 foreach my $r ( @{$budgets_hierarchy} ) {
105 next
106 unless ( C4::Budgets::CanUserUseBudget( $patron, $r, $userflags ) );
107 if ( !defined $r->{budget_amount} || $r->{budget_amount} == 0 ) {
108 next;
110 push @{$budget_loop},
112 b_id => $r->{budget_id},
113 b_txt => $r->{budget_name},
114 b_code => $r->{budget_code},
115 b_sort1_authcat => $r->{'sort1_authcat'},
116 b_sort2_authcat => $r->{'sort2_authcat'},
117 b_active => $r->{budget_period_active},
120 @{$budget_loop} =
121 sort { uc( $a->{b_txt} ) cmp uc( $b->{b_txt} ) } @{$budget_loop};
123 my @currencies = Koha::Acquisition::Currencies->search;
124 $template->param(
125 currencies => \@currencies,
126 budget_loop => $budget_loop,
129 elsif ( $op eq 'do_duplicate' ) {
130 my @fields_to_copy = $input->multi_param('copy_existing_value');
132 my $default_values;
133 for my $field (
134 qw(currency budget_id order_internalnote order_vendornote sort1 sort2 ))
136 next if grep { $_ eq $field } @fields_to_copy;
137 $default_values->{$field} = $input->param("all_$field");
140 @ordernumbers = $input->multi_param('ordernumber');
141 my @new_ordernumbers;
142 for my $ordernumber (@ordernumbers) {
143 my $original_order = Koha::Acquisition::Orders->find($ordernumber);
144 next unless $original_order;
145 my $new_order =
146 $original_order->duplicate_to( $basket, $default_values );
147 push @new_ordernumbers, $new_order->ordernumber;
150 my $new_orders =
151 C4::Acquisition::GetHistory( ordernumbers => \@new_ordernumbers );
152 $template->param( new_orders => $new_orders );
153 $op = 'duplication_done';
156 my $budgetperiods = C4::Budgets::GetBudgetPeriods;
157 my $bp_loop = $budgetperiods;
158 for my $bp ( @{$budgetperiods} ) {
159 my $hierarchy = C4::Budgets::GetBudgetHierarchy( $$bp{budget_period_id} );
160 for my $budget ( @{$hierarchy} ) {
161 $$budget{budget_display_name} =
162 sprintf( "%s", ">" x $$budget{depth} . $$budget{budget_name} );
164 $$bp{hierarchy} = $hierarchy;
167 $template->param(
168 basket => $basket,
169 vendor => $vendor,
170 filters => $filters,
171 result_order_loop => \@result_order_loop,
172 selected_order_loop => \@selected_order_loop,
173 bp_loop => $bp_loop,
174 ordernumbers => \@ordernumbers,
175 op => $op,
178 output_html_with_http_headers $input, $cookie, $template->output;