3 # Copyright 2000-2002 Katipo Communications
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>.
21 #use warnings; FIXME - Bug 2505
25 use vars qw(@ISA @EXPORT);
28 # set the version for version checking
34 &GetBudgetByOrderNumber
45 GetBudgetHierarchySpent
46 GetBudgetHierarchyOrdered
66 &CheckBudgetParentPerm
73 # ----------------------------BUDGETS.PM-----------------------------";
76 =head1 FUNCTIONS ABOUT BUDGETS
81 my ( $authcat, @hide_cols ) = @_;
82 my $dbh = C4::Context->dbh;
84 my $sth1 = $dbh->prepare(
86 UPDATE aqbudgets_planning SET display = 0
90 foreach my $authvalue (@hide_cols) {
91 # $sth1->{TraceLevel} = 3;
92 $sth1->execute( $authcat, $authvalue );
97 my ( $authcat, $authvalue ) = @_;
99 my $dbh = C4::Context->dbh;
100 my $sth = $dbh->prepare(
102 SELECT count(display) as cnt from aqbudgets_planning
104 AND authvalue = ? and display = 0 |
107 # $sth->{TraceLevel} = 3;
108 $sth->execute( $authcat, $authvalue );
109 my $res = $sth->fetchrow_hashref;
111 return $res->{cnt} > 0 ? 0: 1
115 sub CheckBudgetParentPerm {
116 my ( $budget, $borrower_id ) = @_;
117 my $depth = $budget->{depth};
118 my $parent_id = $budget->{budget_parent_id};
120 my $parent = GetBudget($parent_id);
121 $parent_id = $parent->{budget_parent_id};
122 if ( $parent->{budget_owner_id} == $borrower_id ) {
130 sub AddBudgetPeriod {
131 my ($budgetperiod) = @_;
132 return unless($budgetperiod->{budget_period_startdate} && $budgetperiod->{budget_period_enddate});
134 my $resultset = Koha::Database->new()->schema->resultset('Aqbudgetperiod');
135 return $resultset->create($budgetperiod)->id;
137 # -------------------------------------------------------------------
138 sub GetPeriodsCount {
139 my $dbh = C4::Context->dbh;
140 my $sth = $dbh->prepare("
141 SELECT COUNT(*) AS sum FROM aqbudgetperiods ");
143 my $res = $sth->fetchrow_hashref;
144 return $res->{'sum'};
147 # -------------------------------------------------------------------
148 sub CheckBudgetParent {
149 my ( $new_parent, $budget ) = @_;
150 my $new_parent_id = $new_parent->{'budget_id'};
151 my $budget_id = $budget->{'budget_id'};
152 my $dbh = C4::Context->dbh;
153 my $parent_id_tmp = $new_parent_id;
155 # check new-parent is not a child (or a child's child ;)
156 my $sth = $dbh->prepare(qq|
157 SELECT budget_parent_id FROM
158 aqbudgets where budget_id = ? | );
160 $sth->execute($parent_id_tmp);
161 my $res = $sth->fetchrow_hashref;
162 if ( $res->{'budget_parent_id'} == $budget_id ) {
165 if ( not defined $res->{'budget_parent_id'} ) {
168 $parent_id_tmp = $res->{'budget_parent_id'};
172 # -------------------------------------------------------------------
173 sub BudgetHasChildren {
174 my ( $budget_id ) = @_;
175 my $dbh = C4::Context->dbh;
176 my $sth = $dbh->prepare(qq|
177 SELECT count(*) as sum FROM aqbudgets
178 WHERE budget_parent_id = ? | );
179 $sth->execute( $budget_id );
180 my $sum = $sth->fetchrow_hashref;
181 return $sum->{'sum'};
184 sub GetBudgetChildren {
185 my ( $budget_id ) = @_;
186 my $dbh = C4::Context->dbh;
187 return $dbh->selectall_arrayref(q|
188 SELECT * FROM aqbudgets
189 WHERE budget_parent_id = ?
190 |, { Slice => {} }, $budget_id );
193 sub SetOwnerToFundHierarchy {
194 my ( $budget_id, $borrowernumber ) = @_;
196 my $budget = GetBudget( $budget_id );
197 $budget->{budget_owner_id} = $borrowernumber;
198 ModBudget( $budget );
199 my $children = GetBudgetChildren( $budget_id );
200 for my $child ( @$children ) {
201 SetOwnerToFundHierarchy( $child->{budget_id}, $borrowernumber );
205 # -------------------------------------------------------------------
206 sub GetBudgetsPlanCell {
207 my ( $cell, $period, $budget ) = @_;
209 my $dbh = C4::Context->dbh;
210 if ( $cell->{'authcat'} eq 'MONTHS' ) {
211 # get the actual amount
212 $sth = $dbh->prepare( qq|
214 SELECT SUM(ecost) AS actual FROM aqorders
215 WHERE budget_id = ? AND
216 entrydate like "$cell->{'authvalue'}%" |
218 $sth->execute( $cell->{'budget_id'} );
219 } elsif ( $cell->{'authcat'} eq 'BRANCHES' ) {
220 # get the actual amount
221 $sth = $dbh->prepare( qq|
223 SELECT SUM(ecost) FROM aqorders
224 LEFT JOIN aqorders_items
225 ON (aqorders.ordernumber = aqorders_items.ordernumber)
227 ON (aqorders_items.itemnumber = items.itemnumber)
228 WHERE budget_id = ? AND homebranch = ? | );
230 $sth->execute( $cell->{'budget_id'}, $cell->{'authvalue'} );
231 } elsif ( $cell->{'authcat'} eq 'ITEMTYPES' ) {
232 # get the actual amount
233 $sth = $dbh->prepare( qq|
235 SELECT SUM( ecost * quantity) AS actual
236 FROM aqorders JOIN biblioitems
237 ON (biblioitems.biblionumber = aqorders.biblionumber )
238 WHERE aqorders.budget_id = ? and itemtype = ? |
240 $sth->execute( $cell->{'budget_id'},
241 $cell->{'authvalue'} );
243 # ELSE GENERIC ORDERS SORT1/SORT2 STAT COUNT.
245 # get the actual amount
246 $sth = $dbh->prepare( qq|
248 SELECT SUM(ecost * quantity) AS actual
250 JOIN aqbudgets ON (aqbudgets.budget_id = aqorders.budget_id )
251 WHERE aqorders.budget_id = ? AND
252 ((aqbudgets.sort1_authcat = ? AND sort1 =?) OR
253 (aqbudgets.sort2_authcat = ? AND sort2 =?)) |
255 $sth->execute( $cell->{'budget_id'},
256 $budget->{'sort1_authcat'},
257 $cell->{'authvalue'},
258 $budget->{'sort2_authcat'},
262 $actual = $sth->fetchrow_array;
264 # get the estimated amount
265 $sth = $dbh->prepare( qq|
267 SELECT estimated_amount AS estimated, display FROM aqbudgets_planning
268 WHERE budget_period_id = ? AND
273 $sth->execute( $cell->{'budget_period_id'},
274 $cell->{'budget_id'},
275 $cell->{'authvalue'},
280 my $res = $sth->fetchrow_hashref;
281 # my $display = $res->{'display'};
282 my $estimated = $res->{'estimated'};
285 return $actual, $estimated;
288 # -------------------------------------------------------------------
290 my ( $budget_plan, $budget_period_id, $authcat ) = @_;
291 my $dbh = C4::Context->dbh;
292 foreach my $buds (@$budget_plan) {
293 my $lines = $buds->{lines};
294 my $sth = $dbh->prepare( qq|
295 DELETE FROM aqbudgets_planning
296 WHERE budget_period_id = ? AND
300 #delete a aqplan line of cells, then insert new cells,
301 # these could be UPDATES rather than DEL/INSERTS...
302 $sth->execute( $budget_period_id, $lines->[0]{budget_id} , $authcat );
304 foreach my $cell (@$lines) {
305 my $sth = $dbh->prepare( qq|
307 INSERT INTO aqbudgets_planning
309 budget_period_id = ?,
311 estimated_amount = ?,
315 $cell->{'budget_id'},
316 $cell->{'budget_period_id'},
318 $cell->{'estimated_amount'},
319 $cell->{'authvalue'},
325 # -------------------------------------------------------------------
327 my ($budget_id) = @_;
328 my $dbh = C4::Context->dbh;
329 my $sth = $dbh->prepare(qq|
330 SELECT SUM( COALESCE(unitprice, ecost) * quantity ) AS sum FROM aqorders
331 WHERE budget_id = ? AND
332 quantityreceived > 0 AND
333 datecancellationprinted IS NULL
335 $sth->execute($budget_id);
336 my $sum = $sth->fetchrow_array;
338 $sth = $dbh->prepare(qq|
339 SELECT SUM(shipmentcost) AS sum
341 WHERE shipmentcost_budgetid = ?
342 AND closedate IS NOT NULL
344 $sth->execute($budget_id);
345 my ($shipmentcost_sum) = $sth->fetchrow_array;
346 $sum += $shipmentcost_sum;
351 # -------------------------------------------------------------------
352 sub GetBudgetOrdered {
353 my ($budget_id) = @_;
354 my $dbh = C4::Context->dbh;
355 my $sth = $dbh->prepare(qq|
356 SELECT SUM(ecost * quantity) AS sum FROM aqorders
357 WHERE budget_id = ? AND
358 quantityreceived = 0 AND
359 datecancellationprinted IS NULL
361 $sth->execute($budget_id);
362 my $sum = $sth->fetchrow_array;
364 $sth = $dbh->prepare(qq|
365 SELECT SUM(shipmentcost) AS sum
367 WHERE shipmentcost_budgetid = ?
368 AND closedate IS NULL
370 $sth->execute($budget_id);
371 my ($shipmentcost_sum) = $sth->fetchrow_array;
372 $sum += $shipmentcost_sum;
379 my $budget_name = &GetBudgetName($budget_id);
381 get the budget_name for a given budget_id
386 my ( $budget_id ) = @_;
387 my $dbh = C4::Context->dbh;
388 my $sth = $dbh->prepare(
395 $sth->execute($budget_id);
396 return $sth->fetchrow_array;
399 # -------------------------------------------------------------------
400 sub GetBudgetAuthCats {
401 my ($budget_period_id) = shift;
402 # now, populate the auth_cats_loop used in the budget planning button
403 # we must retrieve all auth values used by at least one budget
404 my $dbh = C4::Context->dbh;
405 my $sth=$dbh->prepare("SELECT sort1_authcat,sort2_authcat FROM aqbudgets WHERE budget_period_id=?");
406 $sth->execute($budget_period_id);
408 while (my ($sort1_authcat,$sort2_authcat) = $sth->fetchrow) {
409 $authcats{$sort1_authcat}=1;
410 $authcats{$sort2_authcat}=1;
413 foreach (sort keys %authcats) {
414 push @auth_cats_loop,{ authcat => $_ };
416 return \@auth_cats_loop;
419 # -------------------------------------------------------------------
420 sub GetBudgetPeriods {
421 my ($filters,$orderby) = @_;
423 my $rs = Koha::Database->new()->schema->resultset('Aqbudgetperiod');
424 $rs = $rs->search( $filters, { order_by => $orderby } );
425 $rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
428 # -------------------------------------------------------------------
429 sub GetBudgetPeriod {
430 my ($budget_period_id) = @_;
431 my $dbh = C4::Context->dbh;
432 ## $total = number of records linked to the record that must be deleted
434 ## get information about the record that will be deleted
436 if ($budget_period_id) {
437 $sth = $dbh->prepare( qq|
440 WHERE budget_period_id=? |
442 $sth->execute($budget_period_id);
443 } else { # ACTIVE BUDGET
444 $sth = $dbh->prepare(qq|
447 WHERE budget_period_active=1 |
451 my $data = $sth->fetchrow_hashref;
455 # -------------------------------------------------------------------
457 my ($budget_period_id) = @_;
458 my $dbh = C4::Context->dbh;
459 ; ## $total = number of records linked to the record that must be deleted
462 ## get information about the record that will be deleted
463 my $sth = $dbh->prepare(qq|
466 WHERE budget_period_id=? |
468 return $sth->execute($budget_period_id);
471 # -------------------------------------------------------------------
472 sub ModBudgetPeriod {
473 my ($budget_period) = @_;
474 my $result = Koha::Database->new()->schema->resultset('Aqbudgetperiod')->find($budget_period);
475 return unless($result);
477 $result = $result->update($budget_period);
478 return $result->in_storage;
481 # -------------------------------------------------------------------
482 sub GetBudgetHierarchy {
483 my ( $budget_period_id, $branchcode, $owner ) = @_;
485 my $dbh = C4::Context->dbh;
487 SELECT aqbudgets.*, aqbudgetperiods.budget_period_active, aqbudgetperiods.budget_period_description
489 JOIN aqbudgetperiods USING (budget_period_id)|;
492 # show only period X if requested
493 if ($budget_period_id) {
494 push @where_strings," aqbudgets.budget_period_id = ?";
495 push @bind_params, $budget_period_id;
497 # show only budgets owned by me, my branch or everyone
501 qq{ (budget_owner_id = ? OR budget_branchcode = ? OR ((budget_branchcode IS NULL or budget_branchcode="") AND (budget_owner_id IS NULL OR budget_owner_id="")))};
502 push @bind_params, ( $owner, $branchcode );
504 push @where_strings, ' (budget_owner_id = ? OR budget_owner_id IS NULL or budget_owner_id ="") ';
505 push @bind_params, $owner;
509 push @where_strings," (budget_branchcode =? or budget_branchcode is NULL)";
510 push @bind_params, $branchcode;
513 $query.=" WHERE ".join(' AND ', @where_strings) if @where_strings;
514 $debug && warn $query,join(",",@bind_params);
515 my $sth = $dbh->prepare($query);
516 $sth->execute(@bind_params);
519 # create hash with budget_id has key
520 while ( my $data = $sth->fetchrow_hashref ) {
521 $links{ $data->{'budget_id'} } = $data;
524 # link child to parent
526 foreach my $budget ( sort { $a->{budget_code} cmp $b->{budget_code} } values %links ) {
527 my $child = $links{$budget->{budget_id}};
528 if ( $child->{'budget_parent_id'} ) {
529 my $parent = $links{ $child->{'budget_parent_id'} };
531 unless ( $parent->{'children'} ) {
532 # init child arrayref
533 $parent->{'children'} = [];
536 push @{ $parent->{'children'} }, $child;
539 push @first_parents, $child;
544 foreach my $first_parent (@first_parents) {
545 _add_budget_children(\@sort, $first_parent);
548 foreach my $budget (@sort) {
549 $budget->{budget_spent} = GetBudgetSpent( $budget->{budget_id} );
550 $budget->{budget_ordered} = GetBudgetOrdered( $budget->{budget_id} );
551 $budget->{total_spent} = GetBudgetHierarchySpent( $budget->{budget_id} );
552 $budget->{total_ordered} = GetBudgetHierarchyOrdered( $budget->{budget_id} );
557 # Recursive method to add a budget and its chidren to an array
558 sub _add_budget_children {
562 my $children = $budget->{'children'} || [];
563 return unless @$children; # break recursivity
564 foreach my $child (@$children) {
565 _add_budget_children($res, $child);
569 # -------------------------------------------------------------------
573 return unless ($budget);
575 my $resultset = Koha::Database->new()->schema->resultset('Aqbudget');
576 return $resultset->create($budget)->id;
579 # -------------------------------------------------------------------
582 my $result = Koha::Database->new()->schema->resultset('Aqbudget')->find($budget);
583 return unless($result);
585 $result = $result->update($budget);
586 return $result->in_storage;
589 # -------------------------------------------------------------------
591 my ($budget_id) = @_;
592 my $dbh = C4::Context->dbh;
593 my $sth = $dbh->prepare("delete from aqbudgets where budget_id=?");
594 my $rc = $sth->execute($budget_id);
601 &GetBudget($budget_id);
603 get a specific budget
607 # -------------------------------------------------------------------
609 my ( $budget_id ) = @_;
610 my $dbh = C4::Context->dbh;
616 my $sth = $dbh->prepare($query);
617 $sth->execute( $budget_id );
618 my $result = $sth->fetchrow_hashref;
622 =head2 GetBudgetByOrderNumber
624 &GetBudgetByOrderNumber($ordernumber);
626 get a specific budget by order number
630 # -------------------------------------------------------------------
631 sub GetBudgetByOrderNumber {
632 my ( $ordernumber ) = @_;
633 my $dbh = C4::Context->dbh;
636 FROM aqbudgets, aqorders
638 AND aqorders.budget_id = aqbudgets.budget_id
640 my $sth = $dbh->prepare($query);
641 $sth->execute( $ordernumber );
642 my $result = $sth->fetchrow_hashref;
646 =head2 GetBudgetByCode
648 my $budget = &GetBudgetByCode($budget_code);
650 Retrieve all aqbudgets fields as a hashref for the budget that has
655 sub GetBudgetByCode {
656 my ( $budget_code ) = @_;
658 my $dbh = C4::Context->dbh;
662 WHERE budget_code = ?
663 ORDER BY budget_id DESC
666 my $sth = $dbh->prepare( $query );
667 $sth->execute( $budget_code );
668 return $sth->fetchrow_hashref;
671 =head2 GetBudgetHierarchySpent
673 my $spent = GetBudgetHierarchySpent( $budget_id );
675 Gets the total spent of the level and sublevels of $budget_id
679 sub GetBudgetHierarchySpent {
680 my ( $budget_id ) = @_;
681 my $dbh = C4::Context->dbh;
682 my $children_ids = $dbh->selectcol_arrayref(q|
685 WHERE budget_parent_id = ?
688 my $total_spent = GetBudgetSpent( $budget_id );
689 for my $child_id ( @$children_ids ) {
690 $total_spent += GetBudgetHierarchySpent( $child_id );
695 =head2 GetBudgetHierarchyOrdered
697 my $ordered = GetBudgetHierarchyOrdered( $budget_id );
699 Gets the total ordered of the level and sublevels of $budget_id
703 sub GetBudgetHierarchyOrdered {
704 my ( $budget_id ) = @_;
705 my $dbh = C4::Context->dbh;
706 my $children_ids = $dbh->selectcol_arrayref(q|
709 WHERE budget_parent_id = ?
712 my $total_ordered = GetBudgetOrdered( $budget_id );
713 for my $child_id ( @$children_ids ) {
714 $total_ordered += GetBudgetHierarchyOrdered( $child_id );
716 return $total_ordered;
721 &GetBudgets($filter, $order_by);
727 # -------------------------------------------------------------------
729 my ($filters, $orderby) = @_;
730 $orderby = 'budget_name' unless($orderby);
732 my $rs = Koha::Database->new()->schema->resultset('Aqbudget');
733 $rs = $rs->search( $filters, { order_by => $orderby } );
734 $rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
738 =head2 GetBudgetUsers
740 my @borrowernumbers = &GetBudgetUsers($budget_id);
742 Return the list of borrowernumbers linked to a budget
747 my ($budget_id) = @_;
749 my $dbh = C4::Context->dbh;
751 SELECT borrowernumber
752 FROM aqbudgetborrowers
755 my $sth = $dbh->prepare($query);
756 $sth->execute($budget_id);
759 while (my ($borrowernumber) = $sth->fetchrow_array) {
760 push @borrowernumbers, $borrowernumber
763 return @borrowernumbers;
766 =head2 ModBudgetUsers
768 &ModBudgetUsers($budget_id, @borrowernumbers);
770 Modify the list of borrowernumbers linked to a budget
775 my ($budget_id, @budget_users_id) = @_;
777 return unless $budget_id;
779 my $dbh = C4::Context->dbh;
780 my $query = "DELETE FROM aqbudgetborrowers WHERE budget_id = ?";
781 my $sth = $dbh->prepare($query);
782 $sth->execute($budget_id);
785 INSERT INTO aqbudgetborrowers (budget_id, borrowernumber)
788 $sth = $dbh->prepare($query);
789 foreach my $borrowernumber (@budget_users_id) {
790 next unless $borrowernumber;
791 $sth->execute($budget_id, $borrowernumber);
795 sub CanUserUseBudget {
796 my ($borrower, $budget, $userflags) = @_;
798 if (not ref $borrower) {
799 $borrower = C4::Members::GetMember(borrowernumber => $borrower);
801 if (not ref $budget) {
802 $budget = GetBudget($budget);
805 return 0 unless ($borrower and $budget);
807 if (not defined $userflags) {
808 $userflags = C4::Auth::getuserflags($borrower->{flags},
809 $borrower->{userid});
812 unless ($userflags->{superlibrarian}
813 || (ref $userflags->{acquisition}
814 && $userflags->{acquisition}->{budget_manage_all})
815 || (!ref $userflags->{acquisition} && $userflags->{acquisition}))
817 if (not exists $userflags->{acquisition}) {
821 if (!ref $userflags->{acquisition} && !$userflags->{acquisition}) {
825 # Budget restricted to owner
826 if ( $budget->{budget_permission} == 1 ) {
827 if ( $budget->{budget_owner_id}
828 and $budget->{budget_owner_id} != $borrower->{borrowernumber} )
834 # Budget restricted to owner, users and library
835 elsif ( $budget->{budget_permission} == 2 ) {
836 my @budget_users = GetBudgetUsers( $budget->{budget_id} );
840 $budget->{budget_owner_id}
841 and $budget->{budget_owner_id} !=
842 $borrower->{borrowernumber}
843 or not $budget->{budget_owner_id}
845 and ( 0 == grep { $borrower->{borrowernumber} == $_ }
847 and defined $budget->{budget_branchcode}
848 and $budget->{budget_branchcode} ne
849 C4::Context->userenv->{branch}
856 # Budget restricted to owner and users
857 elsif ( $budget->{budget_permission} == 3 ) {
858 my @budget_users = GetBudgetUsers( $budget->{budget_id} );
861 $budget->{budget_owner_id}
862 and $budget->{budget_owner_id} !=
863 $borrower->{borrowernumber}
864 or not $budget->{budget_owner_id}
866 and ( 0 == grep { $borrower->{borrowernumber} == $_ }
878 sub CanUserModifyBudget {
879 my ($borrower, $budget, $userflags) = @_;
881 if (not ref $borrower) {
882 $borrower = C4::Members::GetMember(borrowernumber => $borrower);
884 if (not ref $budget) {
885 $budget = GetBudget($budget);
888 return 0 unless ($borrower and $budget);
890 if (not defined $userflags) {
891 $userflags = C4::Auth::getuserflags($borrower->{flags},
892 $borrower->{userid});
895 unless ($userflags->{superlibrarian}
896 || (ref $userflags->{acquisition}
897 && $userflags->{acquisition}->{budget_manage_all})
898 || (!ref $userflags->{acquisition} && $userflags->{acquisition}))
900 if (!CanUserUseBudget($borrower, $budget, $userflags)) {
904 if (ref $userflags->{acquisition}
905 && !$userflags->{acquisition}->{budget_modify}) {
914 my ($value, $increment) = @_;
916 if ($increment && $increment != 0) {
917 $value = int($value / $increment) * $increment;
923 =head2 CloneBudgetPeriod
925 my $new_budget_period_id = CloneBudgetPeriod({
926 budget_period_id => $budget_period_id,
927 budget_period_startdate => $budget_period_startdate,
928 budget_period_enddate => $budget_period_enddate,
929 mark_original_budget_as_inactive => 1n
930 reset_all_budgets => 1,
933 Clone a budget period with all budgets.
934 If the mark_origin_budget_as_inactive is set (0 by default),
935 the original budget will be marked as inactive.
937 If the reset_all_budgets is set (0 by default), all budget (fund)
938 amounts will be reset.
942 sub CloneBudgetPeriod {
944 my $budget_period_id = $params->{budget_period_id};
945 my $budget_period_startdate = $params->{budget_period_startdate};
946 my $budget_period_enddate = $params->{budget_period_enddate};
947 my $budget_period_description = $params->{budget_period_description};
948 my $amount_change_percentage = $params->{amount_change_percentage};
949 my $amount_change_round_increment = $params->{amount_change_round_increment};
950 my $mark_original_budget_as_inactive =
951 $params->{mark_original_budget_as_inactive} || 0;
952 my $reset_all_budgets = $params->{reset_all_budgets} || 0;
954 my $budget_period = GetBudgetPeriod($budget_period_id);
956 $budget_period->{budget_period_startdate} = $budget_period_startdate;
957 $budget_period->{budget_period_enddate} = $budget_period_enddate;
958 $budget_period->{budget_period_description} = $budget_period_description;
959 # The new budget (budget_period) should be active by default
960 $budget_period->{budget_period_active} = 1;
962 if ($amount_change_percentage) {
963 my $total = $budget_period->{budget_period_total};
964 $total += $total * $amount_change_percentage / 100;
965 $total = _round($total, $amount_change_round_increment);
966 $budget_period->{budget_period_total} = $total;
969 my $original_budget_period_id = $budget_period->{budget_period_id};
970 delete $budget_period->{budget_period_id};
971 my $new_budget_period_id = AddBudgetPeriod( $budget_period );
973 my $budgets = GetBudgetHierarchy($budget_period_id);
974 CloneBudgetHierarchy(
977 new_budget_period_id => $new_budget_period_id
981 if ($mark_original_budget_as_inactive) {
984 budget_period_id => $budget_period_id,
985 budget_period_active => 0,
990 if ( $reset_all_budgets ) {
991 my $budgets = GetBudgets({ budget_period_id => $new_budget_period_id });
992 for my $budget ( @$budgets ) {
993 $budget->{budget_amount} = 0;
994 ModBudget( $budget );
996 } elsif ($amount_change_percentage) {
997 my $budgets = GetBudgets({ budget_period_id => $new_budget_period_id });
998 for my $budget ( @$budgets ) {
999 my $amount = $budget->{budget_amount};
1000 $amount += $amount * $amount_change_percentage / 100;
1001 $amount = _round($amount, $amount_change_round_increment);
1002 $budget->{budget_amount} = $amount;
1003 ModBudget( $budget );
1007 return $new_budget_period_id;
1010 =head2 CloneBudgetHierarchy
1012 CloneBudgetHierarchy({
1013 budgets => $budgets,
1014 new_budget_period_id => $new_budget_period_id;
1017 Clone a budget hierarchy.
1021 sub CloneBudgetHierarchy {
1023 my $budgets = $params->{budgets};
1024 my $new_budget_period_id = $params->{new_budget_period_id};
1025 next unless @$budgets or $new_budget_period_id;
1027 my $children_of = $params->{children_of};
1028 my $new_parent_id = $params->{new_parent_id};
1030 my @first_level_budgets =
1031 ( not defined $children_of )
1032 ? map { ( not $_->{budget_parent_id} ) ? $_ : () } @$budgets
1033 : map { ( $_->{budget_parent_id} == $children_of ) ? $_ : () } @$budgets;
1035 # get only the columns of aqbudgets
1036 my @columns = Koha::Database->new()->schema->source('Aqbudget')->columns;
1038 for my $budget ( sort { $a->{budget_id} <=> $b->{budget_id} }
1039 @first_level_budgets )
1043 { map { join( ' ', @columns ) =~ /$_/ ? ( $_ => $budget->{$_} ) : () }
1045 my $new_budget_id = AddBudget(
1049 budget_parent_id => $new_parent_id,
1050 budget_period_id => $new_budget_period_id
1053 CloneBudgetHierarchy(
1055 budgets => $budgets,
1056 new_budget_period_id => $new_budget_period_id,
1057 children_of => $budget->{budget_id},
1058 new_parent_id => $new_budget_id
1066 my $report = MoveOrders({
1067 from_budget_period_id => $from_budget_period_id,
1068 to_budget_period_id => $to_budget_period_id,
1071 Move orders from one budget period to another.
1077 my $from_budget_period_id = $params->{from_budget_period_id};
1078 my $to_budget_period_id = $params->{to_budget_period_id};
1079 my $move_remaining_unspent = $params->{move_remaining_unspent};
1081 if not $from_budget_period_id
1082 or not $to_budget_period_id
1083 or $from_budget_period_id == $to_budget_period_id;
1085 # Can't move orders to an inactive budget (budgetperiod)
1086 my $budget_period = GetBudgetPeriod($to_budget_period_id);
1087 return unless $budget_period->{budget_period_active};
1090 my $dbh = C4::Context->dbh;
1091 my $sth_update_aqorders = $dbh->prepare(
1095 WHERE ordernumber = ?
1098 my $sth_update_budget_amount = $dbh->prepare(
1101 SET budget_amount = ?
1105 my $from_budgets = GetBudgetHierarchy($from_budget_period_id);
1106 for my $from_budget (@$from_budgets) {
1107 my $new_budget_id = $dbh->selectcol_arrayref(
1111 WHERE budget_period_id = ?
1113 |, {}, $to_budget_period_id, $from_budget->{budget_code}
1115 $new_budget_id = $new_budget_id->[0];
1116 my $new_budget = GetBudget( $new_budget_id );
1117 unless ( $new_budget ) {
1121 budget => $from_budget,
1122 error => 'budget_code_not_exists',
1126 my $orders_to_move = C4::Acquisition::SearchOrders(
1128 budget_id => $from_budget->{budget_id},
1134 for my $order (@$orders_to_move) {
1135 $sth_update_aqorders->execute( $new_budget->{budget_id}, $order->{ordernumber} );
1136 push @orders_moved, $order;
1139 my $unspent_moved = 0;
1140 if ($move_remaining_unspent) {
1141 my $spent = GetBudgetHierarchySpent( $from_budget->{budget_id} );
1142 my $unspent = $from_budget->{budget_amount} - $spent;
1143 my $new_budget_amount = $new_budget->{budget_amount};
1144 if ( $unspent > 0 ) {
1145 $new_budget_amount += $unspent;
1146 $unspent_moved = $unspent;
1148 $new_budget->{budget_amount} = $new_budget_amount;
1149 $sth_update_budget_amount->execute( $new_budget_amount,
1150 $new_budget->{budget_id} );
1155 budget => $new_budget,
1156 orders_moved => \@orders_moved,
1158 unspent_moved => $unspent_moved,
1164 END { } # module clean-up code here (global destructor)
1171 Koha Development Team <http://koha-community.org/>