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
29 $VERSION = 3.07.00.049;
35 &GetBudgetByOrderNumber
46 GetBudgetHierarchySpent
47 GetBudgetHierarchyOrdered
67 &CheckBudgetParentPerm
74 # ----------------------------BUDGETS.PM-----------------------------";
77 =head1 FUNCTIONS ABOUT BUDGETS
82 my ( $authcat, @hide_cols ) = @_;
83 my $dbh = C4::Context->dbh;
85 my $sth1 = $dbh->prepare(
87 UPDATE aqbudgets_planning SET display = 0
91 foreach my $authvalue (@hide_cols) {
92 # $sth1->{TraceLevel} = 3;
93 $sth1->execute( $authcat, $authvalue );
98 my ( $authcat, $authvalue ) = @_;
100 my $dbh = C4::Context->dbh;
101 my $sth = $dbh->prepare(
103 SELECT count(display) as cnt from aqbudgets_planning
105 AND authvalue = ? and display = 0 |
108 # $sth->{TraceLevel} = 3;
109 $sth->execute( $authcat, $authvalue );
110 my $res = $sth->fetchrow_hashref;
112 return $res->{cnt} > 0 ? 0: 1
116 sub CheckBudgetParentPerm {
117 my ( $budget, $borrower_id ) = @_;
118 my $depth = $budget->{depth};
119 my $parent_id = $budget->{budget_parent_id};
121 my $parent = GetBudget($parent_id);
122 $parent_id = $parent->{budget_parent_id};
123 if ( $parent->{budget_owner_id} == $borrower_id ) {
131 sub AddBudgetPeriod {
132 my ($budgetperiod) = @_;
133 return unless($budgetperiod->{budget_period_startdate} && $budgetperiod->{budget_period_enddate});
135 my $resultset = Koha::Database->new()->schema->resultset('Aqbudgetperiod');
136 return $resultset->create($budgetperiod)->id;
138 # -------------------------------------------------------------------
139 sub GetPeriodsCount {
140 my $dbh = C4::Context->dbh;
141 my $sth = $dbh->prepare("
142 SELECT COUNT(*) AS sum FROM aqbudgetperiods ");
144 my $res = $sth->fetchrow_hashref;
145 return $res->{'sum'};
148 # -------------------------------------------------------------------
149 sub CheckBudgetParent {
150 my ( $new_parent, $budget ) = @_;
151 my $new_parent_id = $new_parent->{'budget_id'};
152 my $budget_id = $budget->{'budget_id'};
153 my $dbh = C4::Context->dbh;
154 my $parent_id_tmp = $new_parent_id;
156 # check new-parent is not a child (or a child's child ;)
157 my $sth = $dbh->prepare(qq|
158 SELECT budget_parent_id FROM
159 aqbudgets where budget_id = ? | );
161 $sth->execute($parent_id_tmp);
162 my $res = $sth->fetchrow_hashref;
163 if ( $res->{'budget_parent_id'} == $budget_id ) {
166 if ( not defined $res->{'budget_parent_id'} ) {
169 $parent_id_tmp = $res->{'budget_parent_id'};
173 # -------------------------------------------------------------------
174 sub BudgetHasChildren {
175 my ( $budget_id ) = @_;
176 my $dbh = C4::Context->dbh;
177 my $sth = $dbh->prepare(qq|
178 SELECT count(*) as sum FROM aqbudgets
179 WHERE budget_parent_id = ? | );
180 $sth->execute( $budget_id );
181 my $sum = $sth->fetchrow_hashref;
182 return $sum->{'sum'};
185 sub GetBudgetChildren {
186 my ( $budget_id ) = @_;
187 my $dbh = C4::Context->dbh;
188 return $dbh->selectall_arrayref(q|
189 SELECT * FROM aqbudgets
190 WHERE budget_parent_id = ?
191 |, { Slice => {} }, $budget_id );
194 sub SetOwnerToFundHierarchy {
195 my ( $budget_id, $borrowernumber ) = @_;
197 my $budget = GetBudget( $budget_id );
198 $budget->{budget_owner_id} = $borrowernumber;
199 ModBudget( $budget );
200 my $children = GetBudgetChildren( $budget_id );
201 for my $child ( @$children ) {
202 SetOwnerToFundHierarchy( $child->{budget_id}, $borrowernumber );
206 # -------------------------------------------------------------------
207 sub GetBudgetsPlanCell {
208 my ( $cell, $period, $budget ) = @_;
210 my $dbh = C4::Context->dbh;
211 if ( $cell->{'authcat'} eq 'MONTHS' ) {
212 # get the actual amount
213 $sth = $dbh->prepare( qq|
215 SELECT SUM(ecost) AS actual FROM aqorders
216 WHERE budget_id = ? AND
217 entrydate like "$cell->{'authvalue'}%" |
219 $sth->execute( $cell->{'budget_id'} );
220 } elsif ( $cell->{'authcat'} eq 'BRANCHES' ) {
221 # get the actual amount
222 $sth = $dbh->prepare( qq|
224 SELECT SUM(ecost) FROM aqorders
225 LEFT JOIN aqorders_items
226 ON (aqorders.ordernumber = aqorders_items.ordernumber)
228 ON (aqorders_items.itemnumber = items.itemnumber)
229 WHERE budget_id = ? AND homebranch = ? | );
231 $sth->execute( $cell->{'budget_id'}, $cell->{'authvalue'} );
232 } elsif ( $cell->{'authcat'} eq 'ITEMTYPES' ) {
233 # get the actual amount
234 $sth = $dbh->prepare( qq|
236 SELECT SUM( ecost * quantity) AS actual
237 FROM aqorders JOIN biblioitems
238 ON (biblioitems.biblionumber = aqorders.biblionumber )
239 WHERE aqorders.budget_id = ? and itemtype = ? |
241 $sth->execute( $cell->{'budget_id'},
242 $cell->{'authvalue'} );
244 # ELSE GENERIC ORDERS SORT1/SORT2 STAT COUNT.
246 # get the actual amount
247 $sth = $dbh->prepare( qq|
249 SELECT SUM(ecost * quantity) AS actual
251 JOIN aqbudgets ON (aqbudgets.budget_id = aqorders.budget_id )
252 WHERE aqorders.budget_id = ? AND
253 ((aqbudgets.sort1_authcat = ? AND sort1 =?) OR
254 (aqbudgets.sort2_authcat = ? AND sort2 =?)) |
256 $sth->execute( $cell->{'budget_id'},
257 $budget->{'sort1_authcat'},
258 $cell->{'authvalue'},
259 $budget->{'sort2_authcat'},
263 $actual = $sth->fetchrow_array;
265 # get the estimated amount
266 $sth = $dbh->prepare( qq|
268 SELECT estimated_amount AS estimated, display FROM aqbudgets_planning
269 WHERE budget_period_id = ? AND
274 $sth->execute( $cell->{'budget_period_id'},
275 $cell->{'budget_id'},
276 $cell->{'authvalue'},
281 my $res = $sth->fetchrow_hashref;
282 # my $display = $res->{'display'};
283 my $estimated = $res->{'estimated'};
286 return $actual, $estimated;
289 # -------------------------------------------------------------------
291 my ( $budget_plan, $budget_period_id, $authcat ) = @_;
292 my $dbh = C4::Context->dbh;
293 foreach my $buds (@$budget_plan) {
294 my $lines = $buds->{lines};
295 my $sth = $dbh->prepare( qq|
296 DELETE FROM aqbudgets_planning
297 WHERE budget_period_id = ? AND
301 #delete a aqplan line of cells, then insert new cells,
302 # these could be UPDATES rather than DEL/INSERTS...
303 $sth->execute( $budget_period_id, $lines->[0]{budget_id} , $authcat );
305 foreach my $cell (@$lines) {
306 my $sth = $dbh->prepare( qq|
308 INSERT INTO aqbudgets_planning
310 budget_period_id = ?,
312 estimated_amount = ?,
316 $cell->{'budget_id'},
317 $cell->{'budget_period_id'},
319 $cell->{'estimated_amount'},
320 $cell->{'authvalue'},
326 # -------------------------------------------------------------------
328 my ($budget_id) = @_;
329 my $dbh = C4::Context->dbh;
330 my $sth = $dbh->prepare(qq|
331 SELECT SUM( COALESCE(unitprice, ecost) * quantity ) AS sum FROM aqorders
332 WHERE budget_id = ? AND
333 quantityreceived > 0 AND
334 datecancellationprinted IS NULL
336 $sth->execute($budget_id);
337 my $sum = $sth->fetchrow_array;
339 $sth = $dbh->prepare(qq|
340 SELECT SUM(shipmentcost) AS sum
342 WHERE shipmentcost_budgetid = ?
343 AND closedate IS NOT NULL
345 $sth->execute($budget_id);
346 my ($shipmentcost_sum) = $sth->fetchrow_array;
347 $sum += $shipmentcost_sum;
352 # -------------------------------------------------------------------
353 sub GetBudgetOrdered {
354 my ($budget_id) = @_;
355 my $dbh = C4::Context->dbh;
356 my $sth = $dbh->prepare(qq|
357 SELECT SUM(ecost * quantity) AS sum FROM aqorders
358 WHERE budget_id = ? AND
359 quantityreceived = 0 AND
360 datecancellationprinted IS NULL
362 $sth->execute($budget_id);
363 my $sum = $sth->fetchrow_array;
365 $sth = $dbh->prepare(qq|
366 SELECT SUM(shipmentcost) AS sum
368 WHERE shipmentcost_budgetid = ?
369 AND closedate IS NULL
371 $sth->execute($budget_id);
372 my ($shipmentcost_sum) = $sth->fetchrow_array;
373 $sum += $shipmentcost_sum;
380 my $budget_name = &GetBudgetName($budget_id);
382 get the budget_name for a given budget_id
387 my ( $budget_id ) = @_;
388 my $dbh = C4::Context->dbh;
389 my $sth = $dbh->prepare(
396 $sth->execute($budget_id);
397 return $sth->fetchrow_array;
400 # -------------------------------------------------------------------
401 sub GetBudgetAuthCats {
402 my ($budget_period_id) = shift;
403 # now, populate the auth_cats_loop used in the budget planning button
404 # we must retrieve all auth values used by at least one budget
405 my $dbh = C4::Context->dbh;
406 my $sth=$dbh->prepare("SELECT sort1_authcat,sort2_authcat FROM aqbudgets WHERE budget_period_id=?");
407 $sth->execute($budget_period_id);
409 while (my ($sort1_authcat,$sort2_authcat) = $sth->fetchrow) {
410 $authcats{$sort1_authcat}=1;
411 $authcats{$sort2_authcat}=1;
414 foreach (sort keys %authcats) {
415 push @auth_cats_loop,{ authcat => $_ };
417 return \@auth_cats_loop;
420 # -------------------------------------------------------------------
421 sub GetBudgetPeriods {
422 my ($filters,$orderby) = @_;
424 my $rs = Koha::Database->new()->schema->resultset('Aqbudgetperiod');
425 $rs = $rs->search( $filters, { order_by => $orderby } );
426 $rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
429 # -------------------------------------------------------------------
430 sub GetBudgetPeriod {
431 my ($budget_period_id) = @_;
432 my $dbh = C4::Context->dbh;
433 ## $total = number of records linked to the record that must be deleted
435 ## get information about the record that will be deleted
437 if ($budget_period_id) {
438 $sth = $dbh->prepare( qq|
441 WHERE budget_period_id=? |
443 $sth->execute($budget_period_id);
444 } else { # ACTIVE BUDGET
445 $sth = $dbh->prepare(qq|
448 WHERE budget_period_active=1 |
452 my $data = $sth->fetchrow_hashref;
456 # -------------------------------------------------------------------
458 my ($budget_period_id) = @_;
459 my $dbh = C4::Context->dbh;
460 ; ## $total = number of records linked to the record that must be deleted
463 ## get information about the record that will be deleted
464 my $sth = $dbh->prepare(qq|
467 WHERE budget_period_id=? |
469 return $sth->execute($budget_period_id);
472 # -------------------------------------------------------------------
473 sub ModBudgetPeriod {
474 my ($budget_period) = @_;
475 my $result = Koha::Database->new()->schema->resultset('Aqbudgetperiod')->find($budget_period);
476 return unless($result);
478 $result = $result->update($budget_period);
479 return $result->in_storage;
482 # -------------------------------------------------------------------
483 sub GetBudgetHierarchy {
484 my ( $budget_period_id, $branchcode, $owner ) = @_;
486 my $dbh = C4::Context->dbh;
488 SELECT aqbudgets.*, aqbudgetperiods.budget_period_active, aqbudgetperiods.budget_period_description
490 JOIN aqbudgetperiods USING (budget_period_id)|;
493 # show only period X if requested
494 if ($budget_period_id) {
495 push @where_strings," aqbudgets.budget_period_id = ?";
496 push @bind_params, $budget_period_id;
498 # show only budgets owned by me, my branch or everyone
502 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="")))};
503 push @bind_params, ( $owner, $branchcode );
505 push @where_strings, ' (budget_owner_id = ? OR budget_owner_id IS NULL or budget_owner_id ="") ';
506 push @bind_params, $owner;
510 push @where_strings," (budget_branchcode =? or budget_branchcode is NULL)";
511 push @bind_params, $branchcode;
514 $query.=" WHERE ".join(' AND ', @where_strings) if @where_strings;
515 $debug && warn $query,join(",",@bind_params);
516 my $sth = $dbh->prepare($query);
517 $sth->execute(@bind_params);
520 # create hash with budget_id has key
521 while ( my $data = $sth->fetchrow_hashref ) {
522 $links{ $data->{'budget_id'} } = $data;
525 # link child to parent
527 foreach my $budget ( sort { $a->{budget_code} cmp $b->{budget_code} } values %links ) {
528 my $child = $links{$budget->{budget_id}};
529 if ( $child->{'budget_parent_id'} ) {
530 my $parent = $links{ $child->{'budget_parent_id'} };
532 unless ( $parent->{'children'} ) {
533 # init child arrayref
534 $parent->{'children'} = [];
537 push @{ $parent->{'children'} }, $child;
540 push @first_parents, $child;
545 foreach my $first_parent (@first_parents) {
546 _add_budget_children(\@sort, $first_parent);
549 foreach my $budget (@sort) {
550 $budget->{budget_spent} = GetBudgetSpent( $budget->{budget_id} );
551 $budget->{budget_ordered} = GetBudgetOrdered( $budget->{budget_id} );
552 $budget->{total_spent} = GetBudgetHierarchySpent( $budget->{budget_id} );
553 $budget->{total_ordered} = GetBudgetHierarchyOrdered( $budget->{budget_id} );
558 # Recursive method to add a budget and its chidren to an array
559 sub _add_budget_children {
563 my $children = $budget->{'children'} || [];
564 return unless @$children; # break recursivity
565 foreach my $child (@$children) {
566 _add_budget_children($res, $child);
570 # -------------------------------------------------------------------
574 return unless ($budget);
576 my $resultset = Koha::Database->new()->schema->resultset('Aqbudget');
577 return $resultset->create($budget)->id;
580 # -------------------------------------------------------------------
583 my $result = Koha::Database->new()->schema->resultset('Aqbudget')->find($budget);
584 return unless($result);
586 $result = $result->update($budget);
587 return $result->in_storage;
590 # -------------------------------------------------------------------
592 my ($budget_id) = @_;
593 my $dbh = C4::Context->dbh;
594 my $sth = $dbh->prepare("delete from aqbudgets where budget_id=?");
595 my $rc = $sth->execute($budget_id);
602 &GetBudget($budget_id);
604 get a specific budget
608 # -------------------------------------------------------------------
610 my ( $budget_id ) = @_;
611 my $dbh = C4::Context->dbh;
617 my $sth = $dbh->prepare($query);
618 $sth->execute( $budget_id );
619 my $result = $sth->fetchrow_hashref;
623 =head2 GetBudgetByOrderNumber
625 &GetBudgetByOrderNumber($ordernumber);
627 get a specific budget by order number
631 # -------------------------------------------------------------------
632 sub GetBudgetByOrderNumber {
633 my ( $ordernumber ) = @_;
634 my $dbh = C4::Context->dbh;
637 FROM aqbudgets, aqorders
639 AND aqorders.budget_id = aqbudgets.budget_id
641 my $sth = $dbh->prepare($query);
642 $sth->execute( $ordernumber );
643 my $result = $sth->fetchrow_hashref;
647 =head2 GetBudgetByCode
649 my $budget = &GetBudgetByCode($budget_code);
651 Retrieve all aqbudgets fields as a hashref for the budget that has
656 sub GetBudgetByCode {
657 my ( $budget_code ) = @_;
659 my $dbh = C4::Context->dbh;
663 WHERE budget_code = ?
664 ORDER BY budget_id DESC
667 my $sth = $dbh->prepare( $query );
668 $sth->execute( $budget_code );
669 return $sth->fetchrow_hashref;
672 =head2 GetBudgetHierarchySpent
674 my $spent = GetBudgetHierarchySpent( $budget_id );
676 Gets the total spent of the level and sublevels of $budget_id
680 sub GetBudgetHierarchySpent {
681 my ( $budget_id ) = @_;
682 my $dbh = C4::Context->dbh;
683 my $children_ids = $dbh->selectcol_arrayref(q|
686 WHERE budget_parent_id = ?
689 my $total_spent = GetBudgetSpent( $budget_id );
690 for my $child_id ( @$children_ids ) {
691 $total_spent += GetBudgetHierarchySpent( $child_id );
696 =head2 GetBudgetHierarchyOrdered
698 my $ordered = GetBudgetHierarchyOrdered( $budget_id );
700 Gets the total ordered of the level and sublevels of $budget_id
704 sub GetBudgetHierarchyOrdered {
705 my ( $budget_id ) = @_;
706 my $dbh = C4::Context->dbh;
707 my $children_ids = $dbh->selectcol_arrayref(q|
710 WHERE budget_parent_id = ?
713 my $total_ordered = GetBudgetOrdered( $budget_id );
714 for my $child_id ( @$children_ids ) {
715 $total_ordered += GetBudgetHierarchyOrdered( $child_id );
717 return $total_ordered;
722 &GetBudgets($filter, $order_by);
728 # -------------------------------------------------------------------
730 my ($filters, $orderby) = @_;
731 $orderby = 'budget_name' unless($orderby);
733 my $rs = Koha::Database->new()->schema->resultset('Aqbudget');
734 $rs = $rs->search( $filters, { order_by => $orderby } );
735 $rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
739 =head2 GetBudgetUsers
741 my @borrowernumbers = &GetBudgetUsers($budget_id);
743 Return the list of borrowernumbers linked to a budget
748 my ($budget_id) = @_;
750 my $dbh = C4::Context->dbh;
752 SELECT borrowernumber
753 FROM aqbudgetborrowers
756 my $sth = $dbh->prepare($query);
757 $sth->execute($budget_id);
760 while (my ($borrowernumber) = $sth->fetchrow_array) {
761 push @borrowernumbers, $borrowernumber
764 return @borrowernumbers;
767 =head2 ModBudgetUsers
769 &ModBudgetUsers($budget_id, @borrowernumbers);
771 Modify the list of borrowernumbers linked to a budget
776 my ($budget_id, @budget_users_id) = @_;
778 return unless $budget_id;
780 my $dbh = C4::Context->dbh;
781 my $query = "DELETE FROM aqbudgetborrowers WHERE budget_id = ?";
782 my $sth = $dbh->prepare($query);
783 $sth->execute($budget_id);
786 INSERT INTO aqbudgetborrowers (budget_id, borrowernumber)
789 $sth = $dbh->prepare($query);
790 foreach my $borrowernumber (@budget_users_id) {
791 next unless $borrowernumber;
792 $sth->execute($budget_id, $borrowernumber);
796 sub CanUserUseBudget {
797 my ($borrower, $budget, $userflags) = @_;
799 if (not ref $borrower) {
800 $borrower = C4::Members::GetMember(borrowernumber => $borrower);
802 if (not ref $budget) {
803 $budget = GetBudget($budget);
806 return 0 unless ($borrower and $budget);
808 if (not defined $userflags) {
809 $userflags = C4::Auth::getuserflags($borrower->{flags},
810 $borrower->{userid});
813 unless ($userflags->{superlibrarian}
814 || (ref $userflags->{acquisition}
815 && $userflags->{acquisition}->{budget_manage_all})
816 || (!ref $userflags->{acquisition} && $userflags->{acquisition}))
818 if (not exists $userflags->{acquisition}) {
822 if (!ref $userflags->{acquisition} && !$userflags->{acquisition}) {
826 # Budget restricted to owner
827 if ( $budget->{budget_permission} == 1 ) {
828 if ( $budget->{budget_owner_id}
829 and $budget->{budget_owner_id} != $borrower->{borrowernumber} )
835 # Budget restricted to owner, users and library
836 elsif ( $budget->{budget_permission} == 2 ) {
837 my @budget_users = GetBudgetUsers( $budget->{budget_id} );
841 $budget->{budget_owner_id}
842 and $budget->{budget_owner_id} !=
843 $borrower->{borrowernumber}
844 or not $budget->{budget_owner_id}
846 and ( 0 == grep { $borrower->{borrowernumber} == $_ }
848 and defined $budget->{budget_branchcode}
849 and $budget->{budget_branchcode} ne
850 C4::Context->userenv->{branch}
857 # Budget restricted to owner and users
858 elsif ( $budget->{budget_permission} == 3 ) {
859 my @budget_users = GetBudgetUsers( $budget->{budget_id} );
862 $budget->{budget_owner_id}
863 and $budget->{budget_owner_id} !=
864 $borrower->{borrowernumber}
865 or not $budget->{budget_owner_id}
867 and ( 0 == grep { $borrower->{borrowernumber} == $_ }
879 sub CanUserModifyBudget {
880 my ($borrower, $budget, $userflags) = @_;
882 if (not ref $borrower) {
883 $borrower = C4::Members::GetMember(borrowernumber => $borrower);
885 if (not ref $budget) {
886 $budget = GetBudget($budget);
889 return 0 unless ($borrower and $budget);
891 if (not defined $userflags) {
892 $userflags = C4::Auth::getuserflags($borrower->{flags},
893 $borrower->{userid});
896 unless ($userflags->{superlibrarian}
897 || (ref $userflags->{acquisition}
898 && $userflags->{acquisition}->{budget_manage_all})
899 || (!ref $userflags->{acquisition} && $userflags->{acquisition}))
901 if (!CanUserUseBudget($borrower, $budget, $userflags)) {
905 if (ref $userflags->{acquisition}
906 && !$userflags->{acquisition}->{budget_modify}) {
915 my ($value, $increment) = @_;
917 if ($increment && $increment != 0) {
918 $value = int($value / $increment) * $increment;
924 =head2 CloneBudgetPeriod
926 my $new_budget_period_id = CloneBudgetPeriod({
927 budget_period_id => $budget_period_id,
928 budget_period_startdate => $budget_period_startdate,
929 budget_period_enddate => $budget_period_enddate,
930 mark_original_budget_as_inactive => 1n
931 reset_all_budgets => 1,
934 Clone a budget period with all budgets.
935 If the mark_origin_budget_as_inactive is set (0 by default),
936 the original budget will be marked as inactive.
938 If the reset_all_budgets is set (0 by default), all budget (fund)
939 amounts will be reset.
943 sub CloneBudgetPeriod {
945 my $budget_period_id = $params->{budget_period_id};
946 my $budget_period_startdate = $params->{budget_period_startdate};
947 my $budget_period_enddate = $params->{budget_period_enddate};
948 my $budget_period_description = $params->{budget_period_description};
949 my $amount_change_percentage = $params->{amount_change_percentage};
950 my $amount_change_round_increment = $params->{amount_change_round_increment};
951 my $mark_original_budget_as_inactive =
952 $params->{mark_original_budget_as_inactive} || 0;
953 my $reset_all_budgets = $params->{reset_all_budgets} || 0;
955 my $budget_period = GetBudgetPeriod($budget_period_id);
957 $budget_period->{budget_period_startdate} = $budget_period_startdate;
958 $budget_period->{budget_period_enddate} = $budget_period_enddate;
959 $budget_period->{budget_period_description} = $budget_period_description;
960 # The new budget (budget_period) should be active by default
961 $budget_period->{budget_period_active} = 1;
963 if ($amount_change_percentage) {
964 my $total = $budget_period->{budget_period_total};
965 $total += $total * $amount_change_percentage / 100;
966 $total = _round($total, $amount_change_round_increment);
967 $budget_period->{budget_period_total} = $total;
970 my $original_budget_period_id = $budget_period->{budget_period_id};
971 delete $budget_period->{budget_period_id};
972 my $new_budget_period_id = AddBudgetPeriod( $budget_period );
974 my $budgets = GetBudgetHierarchy($budget_period_id);
975 CloneBudgetHierarchy(
978 new_budget_period_id => $new_budget_period_id
982 if ($mark_original_budget_as_inactive) {
985 budget_period_id => $budget_period_id,
986 budget_period_active => 0,
991 if ( $reset_all_budgets ) {
992 my $budgets = GetBudgets({ budget_period_id => $new_budget_period_id });
993 for my $budget ( @$budgets ) {
994 $budget->{budget_amount} = 0;
995 ModBudget( $budget );
997 } elsif ($amount_change_percentage) {
998 my $budgets = GetBudgets({ budget_period_id => $new_budget_period_id });
999 for my $budget ( @$budgets ) {
1000 my $amount = $budget->{budget_amount};
1001 $amount += $amount * $amount_change_percentage / 100;
1002 $amount = _round($amount, $amount_change_round_increment);
1003 $budget->{budget_amount} = $amount;
1004 ModBudget( $budget );
1008 return $new_budget_period_id;
1011 =head2 CloneBudgetHierarchy
1013 CloneBudgetHierarchy({
1014 budgets => $budgets,
1015 new_budget_period_id => $new_budget_period_id;
1018 Clone a budget hierarchy.
1022 sub CloneBudgetHierarchy {
1024 my $budgets = $params->{budgets};
1025 my $new_budget_period_id = $params->{new_budget_period_id};
1026 next unless @$budgets or $new_budget_period_id;
1028 my $children_of = $params->{children_of};
1029 my $new_parent_id = $params->{new_parent_id};
1031 my @first_level_budgets =
1032 ( not defined $children_of )
1033 ? map { ( not $_->{budget_parent_id} ) ? $_ : () } @$budgets
1034 : map { ( $_->{budget_parent_id} == $children_of ) ? $_ : () } @$budgets;
1036 # get only the columns of aqbudgets
1037 my @columns = Koha::Database->new()->schema->source('Aqbudget')->columns;
1039 for my $budget ( sort { $a->{budget_id} <=> $b->{budget_id} }
1040 @first_level_budgets )
1044 { map { join( ' ', @columns ) =~ /$_/ ? ( $_ => $budget->{$_} ) : () }
1046 my $new_budget_id = AddBudget(
1050 budget_parent_id => $new_parent_id,
1051 budget_period_id => $new_budget_period_id
1054 CloneBudgetHierarchy(
1056 budgets => $budgets,
1057 new_budget_period_id => $new_budget_period_id,
1058 children_of => $budget->{budget_id},
1059 new_parent_id => $new_budget_id
1067 my $report = MoveOrders({
1068 from_budget_period_id => $from_budget_period_id,
1069 to_budget_period_id => $to_budget_period_id,
1072 Move orders from one budget period to another.
1078 my $from_budget_period_id = $params->{from_budget_period_id};
1079 my $to_budget_period_id = $params->{to_budget_period_id};
1080 my $move_remaining_unspent = $params->{move_remaining_unspent};
1082 if not $from_budget_period_id
1083 or not $to_budget_period_id
1084 or $from_budget_period_id == $to_budget_period_id;
1086 # Can't move orders to an inactive budget (budgetperiod)
1087 my $budget_period = GetBudgetPeriod($to_budget_period_id);
1088 return unless $budget_period->{budget_period_active};
1091 my $dbh = C4::Context->dbh;
1092 my $sth_update_aqorders = $dbh->prepare(
1096 WHERE ordernumber = ?
1099 my $sth_update_budget_amount = $dbh->prepare(
1102 SET budget_amount = ?
1106 my $from_budgets = GetBudgetHierarchy($from_budget_period_id);
1107 for my $from_budget (@$from_budgets) {
1108 my $new_budget_id = $dbh->selectcol_arrayref(
1112 WHERE budget_period_id = ?
1114 |, {}, $to_budget_period_id, $from_budget->{budget_code}
1116 $new_budget_id = $new_budget_id->[0];
1117 my $new_budget = GetBudget( $new_budget_id );
1118 unless ( $new_budget ) {
1122 budget => $from_budget,
1123 error => 'budget_code_not_exists',
1127 my $orders_to_move = C4::Acquisition::SearchOrders(
1129 budget_id => $from_budget->{budget_id},
1135 for my $order (@$orders_to_move) {
1136 $sth_update_aqorders->execute( $new_budget->{budget_id}, $order->{ordernumber} );
1137 push @orders_moved, $order;
1140 my $unspent_moved = 0;
1141 if ($move_remaining_unspent) {
1142 my $spent = GetBudgetHierarchySpent( $from_budget->{budget_id} );
1143 my $unspent = $from_budget->{budget_amount} - $spent;
1144 my $new_budget_amount = $new_budget->{budget_amount};
1145 if ( $unspent > 0 ) {
1146 $new_budget_amount += $unspent;
1147 $unspent_moved = $unspent;
1149 $new_budget->{budget_amount} = $new_budget_amount;
1150 $sth_update_budget_amount->execute( $new_budget_amount,
1151 $new_budget->{budget_id} );
1156 budget => $new_budget,
1157 orders_moved => \@orders_moved,
1159 unspent_moved => $unspent_moved,
1165 END { } # module clean-up code here (global destructor)
1172 Koha Development Team <http://koha-community.org/>