]> git.koha-community.org Git - koha.git/blob - acqui/duplicate_orders.pl
Bug 31631: (follow-up) Use CalculateFundValuesIncludingTax when modifying existing...
[koha.git] / acqui / duplicate_orders.pl
1 #!/usr/bin/perl
2
3 # Copyright 2018 Koha Development Team
4 #
5 # This file is part of Koha.
6 #
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.
11 #
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.
16 #
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>.
19
20 use Modern::Perl;
21
22 use CGI qw ( -utf8 );
23
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 );
32
33 my $input    = CGI->new;
34 my $basketno = $input->param('basketno');
35 my $op       = $input->param('op') || 'search';    # search, select, batch_edit
36
37 my ( $template, $loggedinuser, $cookie, $userflags ) = get_template_and_user(
38     {
39         template_name   => "acqui/duplicate_orders.tt",
40         query           => $input,
41         type            => "intranet",
42         flagsrequired   => { acquisition => 'order_manage' },
43     }
44 );
45
46 my $basket = Koha::Acquisition::Baskets->find($basketno);
47
48 output_and_exit( $input, $cookie, $template, 'unknown_basket' )
49   unless $basket;
50
51 my $vendor = $basket->bookseller;
52 my $patron = Koha::Patrons->find($loggedinuser)->unblessed;
53
54 my $filters = {
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')]
68 };
69
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;
74
75 unless ( $input->param('from') ) {
76     # Fill the form with year-1
77     $from_placed_on->set_time_zone('floating')->subtract( years => 1 );
78 }
79 $filters->{from_placed_on} = $from_placed_on;
80 $filters->{to_placed_on}   = $to_placed_on;
81
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 {
86         my $order = $_;
87         ( grep {$_ eq $order->{ordernumber}} @ordernumbers ) ? () : $order
88     } @{ C4::Acquisition::GetHistory(%$filters) };
89
90     @selected_order_loop =
91       scalar @ordernumbers
92       ? @{ C4::Acquisition::GetHistory( ordernumbers => \@ordernumbers ) }
93       : ();
94 }
95 elsif ( $op eq 'batch_edit' ) {
96     @ordernumbers = $input->multi_param('ordernumber');
97
98     # build budget list
99     my $budget_loop       = [];
100     my $budgets_hierarchy = GetBudgetHierarchy;
101     foreach my $r ( @{$budgets_hierarchy} ) {
102         next
103           unless ( C4::Budgets::CanUserUseBudget( $patron, $r, $userflags ) );
104
105         push @{$budget_loop},
106           {
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},
113           };
114     }
115     @{$budget_loop} =
116       sort { uc( $a->{b_txt} ) cmp uc( $b->{b_txt} ) } @{$budget_loop};
117
118     $template->param(
119         currencies  => Koha::Acquisition::Currencies->search,
120         budget_loop => $budget_loop,
121     );
122 }
123 elsif ( $op eq 'do_duplicate' ) {
124     my @fields_to_copy = $input->multi_param('copy_existing_value');
125
126     my $default_values;
127     for my $field (
128         qw(currency budget_id order_internalnote order_vendornote sort1 sort2 ))
129     {
130         next if grep { $_ eq $field } @fields_to_copy;
131         $default_values->{$field} = $input->param("all_$field");
132     }
133
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;
139         my $new_order =
140           $original_order->duplicate_to( $basket, $default_values );
141         push @new_ordernumbers, $new_order->ordernumber;
142     }
143
144     my $new_orders =
145       C4::Acquisition::GetHistory( ordernumbers => \@new_ordernumbers );
146     $template->param( new_orders => $new_orders );
147     $op = 'duplication_done';
148 }
149
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} );
157     }
158     $$bp{hierarchy} = $hierarchy;
159 }
160
161 $template->param(
162     basket              => $basket,
163     vendor              => $vendor,
164     filters             => $filters,
165     result_order_loop   => \@result_order_loop,
166     selected_order_loop => \@selected_order_loop,
167     bp_loop             => $bp_loop,
168     ordernumbers        => \@ordernumbers,
169     op                  => $op,
170 );
171
172 output_html_with_http_headers $input, $cookie, $template->output;