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
26 use vars qw(@ISA @EXPORT);
34 &GetBudgetByOrderNumber
48 GetBudgetHierarchySpent
49 GetBudgetHierarchyOrdered
69 &CheckBudgetParentPerm
76 # ----------------------------BUDGETS.PM-----------------------------";
78 =head1 FUNCTIONS ABOUT BUDGETS
83 my ( $authcat, @hide_cols ) = @_;
84 my $dbh = C4::Context->dbh;
86 my $sth1 = $dbh->prepare(
88 UPDATE aqbudgets_planning SET display = 0
92 foreach my $authvalue (@hide_cols) {
93 # $sth1->{TraceLevel} = 3;
94 $sth1->execute( $authcat, $authvalue );
99 my ( $authcat, $authvalue ) = @_;
101 my $dbh = C4::Context->dbh;
102 my $sth = $dbh->prepare(
104 SELECT count(display) as cnt from aqbudgets_planning
106 AND authvalue = ? and display = 0 |
109 # $sth->{TraceLevel} = 3;
110 $sth->execute( $authcat, $authvalue );
111 my $res = $sth->fetchrow_hashref;
113 return $res->{cnt} > 0 ? 0: 1
117 sub CheckBudgetParentPerm {
118 my ( $budget, $borrower_id ) = @_;
119 my $depth = $budget->{depth};
120 my $parent_id = $budget->{budget_parent_id};
122 my $parent = GetBudget($parent_id);
123 $parent_id = $parent->{budget_parent_id};
124 if ( $parent->{budget_owner_id} == $borrower_id ) {
132 sub AddBudgetPeriod {
133 my ($budgetperiod) = @_;
134 return unless($budgetperiod->{budget_period_startdate} && $budgetperiod->{budget_period_enddate});
136 my $resultset = Koha::Database->new()->schema->resultset('Aqbudgetperiod');
137 return $resultset->create($budgetperiod)->id;
139 # -------------------------------------------------------------------
140 sub GetPeriodsCount {
141 my $dbh = C4::Context->dbh;
142 my $sth = $dbh->prepare("
143 SELECT COUNT(*) AS sum FROM aqbudgetperiods ");
145 my $res = $sth->fetchrow_hashref;
146 return $res->{'sum'};
149 # -------------------------------------------------------------------
150 sub CheckBudgetParent {
151 my ( $new_parent, $budget ) = @_;
152 my $new_parent_id = $new_parent->{'budget_id'};
153 my $budget_id = $budget->{'budget_id'};
154 my $dbh = C4::Context->dbh;
155 my $parent_id_tmp = $new_parent_id;
157 # check new-parent is not a child (or a child's child ;)
158 my $sth = $dbh->prepare(qq|
159 SELECT budget_parent_id FROM
160 aqbudgets where budget_id = ? | );
162 $sth->execute($parent_id_tmp);
163 my $res = $sth->fetchrow_hashref;
164 if ( $res->{'budget_parent_id'} == $budget_id ) {
167 if ( not defined $res->{'budget_parent_id'} ) {
170 $parent_id_tmp = $res->{'budget_parent_id'};
174 # -------------------------------------------------------------------
175 sub BudgetHasChildren {
176 my ( $budget_id ) = @_;
177 my $dbh = C4::Context->dbh;
178 my $sth = $dbh->prepare(qq|
179 SELECT count(*) as sum FROM aqbudgets
180 WHERE budget_parent_id = ? | );
181 $sth->execute( $budget_id );
182 my $sum = $sth->fetchrow_hashref;
183 return $sum->{'sum'};
186 sub GetBudgetChildren {
187 my ( $budget_id ) = @_;
188 my $dbh = C4::Context->dbh;
189 return $dbh->selectall_arrayref(q|
190 SELECT * FROM aqbudgets
191 WHERE budget_parent_id = ?
192 |, { Slice => {} }, $budget_id );
195 sub SetOwnerToFundHierarchy {
196 my ( $budget_id, $borrowernumber ) = @_;
198 my $budget = GetBudget( $budget_id );
199 $budget->{budget_owner_id} = $borrowernumber;
200 ModBudget( $budget );
201 my $children = GetBudgetChildren( $budget_id );
202 for my $child ( @$children ) {
203 SetOwnerToFundHierarchy( $child->{budget_id}, $borrowernumber );
207 # -------------------------------------------------------------------
208 sub GetBudgetsPlanCell {
209 my ( $cell, $period, $budget ) = @_;
211 my $dbh = C4::Context->dbh;
212 if ( $cell->{'authcat'} eq 'MONTHS' ) {
213 # get the actual amount
214 $sth = $dbh->prepare( qq|
216 SELECT SUM(ecost_tax_included) AS actual FROM aqorders
217 WHERE budget_id = ? AND
218 entrydate like "$cell->{'authvalue'}%" |
220 $sth->execute( $cell->{'budget_id'} );
221 } elsif ( $cell->{'authcat'} eq 'BRANCHES' ) {
222 # get the actual amount
223 $sth = $dbh->prepare( qq|
225 SELECT SUM(ecost_tax_included) FROM aqorders
226 LEFT JOIN aqorders_items
227 ON (aqorders.ordernumber = aqorders_items.ordernumber)
229 ON (aqorders_items.itemnumber = items.itemnumber)
230 WHERE budget_id = ? AND homebranch = ? | );
232 $sth->execute( $cell->{'budget_id'}, $cell->{'authvalue'} );
233 } elsif ( $cell->{'authcat'} eq 'ITEMTYPES' ) {
234 # get the actual amount
235 $sth = $dbh->prepare( qq|
237 SELECT SUM( ecost_tax_included * quantity) AS actual
238 FROM aqorders JOIN biblioitems
239 ON (biblioitems.biblionumber = aqorders.biblionumber )
240 WHERE aqorders.budget_id = ? and itemtype = ? |
242 $sth->execute( $cell->{'budget_id'},
243 $cell->{'authvalue'} );
245 # ELSE GENERIC ORDERS SORT1/SORT2 STAT COUNT.
247 # get the actual amount
248 $sth = $dbh->prepare( qq|
250 SELECT SUM(ecost_tax_included * quantity) AS actual
252 JOIN aqbudgets ON (aqbudgets.budget_id = aqorders.budget_id )
253 WHERE aqorders.budget_id = ? AND
254 ((aqbudgets.sort1_authcat = ? AND sort1 =?) OR
255 (aqbudgets.sort2_authcat = ? AND sort2 =?)) |
257 $sth->execute( $cell->{'budget_id'},
258 $budget->{'sort1_authcat'},
259 $cell->{'authvalue'},
260 $budget->{'sort2_authcat'},
264 $actual = $sth->fetchrow_array;
266 # get the estimated amount
267 $sth = $dbh->prepare( qq|
269 SELECT estimated_amount AS estimated, display FROM aqbudgets_planning
270 WHERE budget_period_id = ? AND
275 $sth->execute( $cell->{'budget_period_id'},
276 $cell->{'budget_id'},
277 $cell->{'authvalue'},
282 my $res = $sth->fetchrow_hashref;
283 # my $display = $res->{'display'};
284 my $estimated = $res->{'estimated'};
287 return $actual, $estimated;
290 # -------------------------------------------------------------------
292 my ( $budget_plan, $budget_period_id, $authcat ) = @_;
293 my $dbh = C4::Context->dbh;
294 foreach my $buds (@$budget_plan) {
295 my $lines = $buds->{lines};
296 my $sth = $dbh->prepare( qq|
297 DELETE FROM aqbudgets_planning
298 WHERE budget_period_id = ? AND
302 #delete a aqplan line of cells, then insert new cells,
303 # these could be UPDATES rather than DEL/INSERTS...
304 $sth->execute( $budget_period_id, $lines->[0]{budget_id} , $authcat );
306 foreach my $cell (@$lines) {
307 my $sth = $dbh->prepare( qq|
309 INSERT INTO aqbudgets_planning
311 budget_period_id = ?,
313 estimated_amount = ?,
317 $cell->{'budget_id'},
318 $cell->{'budget_period_id'},
320 $cell->{'estimated_amount'},
321 $cell->{'authvalue'},
327 # -------------------------------------------------------------------
329 my ($budget_id) = @_;
330 my $dbh = C4::Context->dbh;
331 # unitprice_tax_included should always been set here
332 # we should not need to retrieve ecost_tax_included
333 my $sth = $dbh->prepare(qq|
334 SELECT SUM( COALESCE(unitprice_tax_included, ecost_tax_included) * quantity ) AS sum FROM aqorders
335 WHERE budget_id = ? AND
336 quantityreceived > 0 AND
337 datecancellationprinted IS NULL
339 $sth->execute($budget_id);
340 my $sum = $sth->fetchrow_array;
342 $sth = $dbh->prepare(qq|
343 SELECT SUM(shipmentcost) AS sum
345 WHERE shipmentcost_budgetid = ?
346 AND closedate IS NOT NULL
348 $sth->execute($budget_id);
349 my ($shipmentcost_sum) = $sth->fetchrow_array;
350 $sum += $shipmentcost_sum;
355 # -------------------------------------------------------------------
356 sub GetBudgetOrdered {
357 my ($budget_id) = @_;
358 my $dbh = C4::Context->dbh;
359 my $sth = $dbh->prepare(qq|
360 SELECT SUM(ecost_tax_included * quantity) AS sum FROM aqorders
361 WHERE budget_id = ? AND
362 quantityreceived = 0 AND
363 datecancellationprinted IS NULL
365 $sth->execute($budget_id);
366 my $sum = $sth->fetchrow_array;
368 $sth = $dbh->prepare(qq|
369 SELECT SUM(shipmentcost) AS sum
371 WHERE shipmentcost_budgetid = ?
372 AND closedate IS NULL
374 $sth->execute($budget_id);
375 my ($shipmentcost_sum) = $sth->fetchrow_array;
376 $sum += $shipmentcost_sum;
383 my $budget_name = &GetBudgetName($budget_id);
385 get the budget_name for a given budget_id
390 my ( $budget_id ) = @_;
391 my $dbh = C4::Context->dbh;
392 my $sth = $dbh->prepare(
399 $sth->execute($budget_id);
400 return $sth->fetchrow_array;
403 =head2 GetBudgetAuthCats
405 my $auth_cats = &GetBudgetAuthCats($budget_period_id);
407 Return the list of authcat for a given budget_period_id
411 sub GetBudgetAuthCats {
412 my ($budget_period_id) = shift;
413 # now, populate the auth_cats_loop used in the budget planning button
414 # we must retrieve all auth values used by at least one budget
415 my $dbh = C4::Context->dbh;
416 my $sth=$dbh->prepare("SELECT sort1_authcat,sort2_authcat FROM aqbudgets WHERE budget_period_id=?");
417 $sth->execute($budget_period_id);
419 while (my ($sort1_authcat,$sort2_authcat) = $sth->fetchrow) {
420 $authcats{$sort1_authcat}=1 if $sort1_authcat;
421 $authcats{$sort2_authcat}=1 if $sort2_authcat;
423 return [ sort keys %authcats ];
426 # -------------------------------------------------------------------
427 sub GetBudgetPeriods {
428 my ($filters,$orderby) = @_;
430 my $rs = Koha::Database->new()->schema->resultset('Aqbudgetperiod');
431 $rs = $rs->search( $filters, { order_by => $orderby } );
432 $rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
435 # -------------------------------------------------------------------
436 sub GetBudgetPeriod {
437 my ($budget_period_id) = @_;
438 my $dbh = C4::Context->dbh;
439 ## $total = number of records linked to the record that must be deleted
441 ## get information about the record that will be deleted
443 if ($budget_period_id) {
444 $sth = $dbh->prepare( qq|
447 WHERE budget_period_id=? |
449 $sth->execute($budget_period_id);
450 } else { # ACTIVE BUDGET
451 $sth = $dbh->prepare(qq|
454 WHERE budget_period_active=1 |
458 my $data = $sth->fetchrow_hashref;
463 my ($budget_period_id) = @_;
464 my $dbh = C4::Context->dbh;
465 ; ## $total = number of records linked to the record that must be deleted
468 ## get information about the record that will be deleted
469 my $sth = $dbh->prepare(qq|
472 WHERE budget_period_id=? |
474 return $sth->execute($budget_period_id);
477 # -------------------------------------------------------------------
478 sub ModBudgetPeriod {
479 my ($budget_period) = @_;
480 my $result = Koha::Database->new()->schema->resultset('Aqbudgetperiod')->find($budget_period);
481 return unless($result);
483 $result = $result->update($budget_period);
484 return $result->in_storage;
487 # -------------------------------------------------------------------
488 sub GetBudgetHierarchy {
489 my ( $budget_period_id, $branchcode, $owner ) = @_;
491 my $dbh = C4::Context->dbh;
493 SELECT aqbudgets.*, aqbudgetperiods.budget_period_active, aqbudgetperiods.budget_period_description
495 JOIN aqbudgetperiods USING (budget_period_id)|;
498 # show only period X if requested
499 if ($budget_period_id) {
500 push @where_strings," aqbudgets.budget_period_id = ?";
501 push @bind_params, $budget_period_id;
503 # show only budgets owned by me, my branch or everyone
507 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="")))};
508 push @bind_params, ( $owner, $branchcode );
510 push @where_strings, ' (budget_owner_id = ? OR budget_owner_id IS NULL or budget_owner_id ="") ';
511 push @bind_params, $owner;
515 push @where_strings," (budget_branchcode =? or budget_branchcode is NULL OR budget_branchcode='')";
516 push @bind_params, $branchcode;
519 $query.=" WHERE ".join(' AND ', @where_strings) if @where_strings;
520 $debug && warn $query,join(",",@bind_params);
521 my $sth = $dbh->prepare($query);
522 $sth->execute(@bind_params);
525 # create hash with budget_id has key
526 while ( my $data = $sth->fetchrow_hashref ) {
527 $links{ $data->{'budget_id'} } = $data;
530 # link child to parent
532 foreach my $budget ( sort { $a->{budget_code} cmp $b->{budget_code} } values %links ) {
533 my $child = $links{$budget->{budget_id}};
534 if ( $child->{'budget_parent_id'} ) {
535 my $parent = $links{ $child->{'budget_parent_id'} };
537 unless ( $parent->{'children'} ) {
538 # init child arrayref
539 $parent->{'children'} = [];
542 push @{ $parent->{'children'} }, $child;
545 push @first_parents, $child;
550 foreach my $first_parent (@first_parents) {
551 _add_budget_children(\@sort, $first_parent, 0);
554 foreach my $budget (@sort) {
555 $budget->{budget_spent} = GetBudgetSpent( $budget->{budget_id} );
556 $budget->{budget_ordered} = GetBudgetOrdered( $budget->{budget_id} );
557 $budget->{total_spent} = GetBudgetHierarchySpent( $budget->{budget_id} );
558 $budget->{total_ordered} = GetBudgetHierarchyOrdered( $budget->{budget_id} );
563 # Recursive method to add a budget and its chidren to an array
564 sub _add_budget_children {
567 $budget->{budget_level} = shift;
569 my $children = $budget->{'children'} || [];
570 return unless @$children; # break recursivity
571 foreach my $child (@$children) {
572 _add_budget_children($res, $child, $budget->{budget_level} + 1);
576 # -------------------------------------------------------------------
580 return unless ($budget);
582 my $resultset = Koha::Database->new()->schema->resultset('Aqbudget');
583 return $resultset->create($budget)->id;
586 # -------------------------------------------------------------------
589 my $result = Koha::Database->new()->schema->resultset('Aqbudget')->find($budget);
590 return unless($result);
592 $result = $result->update($budget);
593 return $result->in_storage;
596 # -------------------------------------------------------------------
598 my ($budget_id) = @_;
599 my $dbh = C4::Context->dbh;
600 my $sth = $dbh->prepare("delete from aqbudgets where budget_id=?");
601 my $rc = $sth->execute($budget_id);
606 # -------------------------------------------------------------------
610 &GetBudget($budget_id);
612 get a specific budget
617 my ( $budget_id ) = @_;
618 my $dbh = C4::Context->dbh;
624 my $sth = $dbh->prepare($query);
625 $sth->execute( $budget_id );
626 my $result = $sth->fetchrow_hashref;
630 # -------------------------------------------------------------------
632 =head2 GetBudgetByOrderNumber
634 &GetBudgetByOrderNumber($ordernumber);
636 get a specific budget by order number
640 sub GetBudgetByOrderNumber {
641 my ( $ordernumber ) = @_;
642 my $dbh = C4::Context->dbh;
645 FROM aqbudgets, aqorders
647 AND aqorders.budget_id = aqbudgets.budget_id
649 my $sth = $dbh->prepare($query);
650 $sth->execute( $ordernumber );
651 my $result = $sth->fetchrow_hashref;
655 =head2 GetBudgetReport
657 &GetBudgetReport( [$budget_id] );
659 Get all orders for a specific budget, without cancelled orders.
661 Returns an array of hashrefs.
665 # --------------------------------------------------------------------
666 sub GetBudgetReport {
667 my ( $budget_id ) = @_;
668 my $dbh = C4::Context->dbh;
670 SELECT o.*, b.budget_name
672 INNER JOIN aqorders o
673 ON b.budget_id = o.budget_id
675 AND (o.orderstatus != "cancelled")
676 ORDER BY b.budget_name';
678 my $sth = $dbh->prepare($query);
679 $sth->execute( $budget_id );
682 while ( my $data = $sth->fetchrow_hashref ) {
683 push( @results, $data );
688 =head2 GetBudgetsByActivity
690 &GetBudgetsByActivity( $budget_period_active );
692 Get all active or inactive budgets, depending of the value
700 # --------------------------------------------------------------------
701 sub GetBudgetsByActivity {
702 my ( $budget_period_active ) = @_;
703 my $dbh = C4::Context->dbh;
706 FROM aqbudgetperiods bp
707 INNER JOIN aqbudgets b
708 ON bp.budget_period_id = b.budget_period_id
709 WHERE bp.budget_period_active=?
711 my $sth = $dbh->prepare($query);
712 $sth->execute( $budget_period_active );
714 while ( my $data = $sth->fetchrow_hashref ) {
715 push( @results, $data );
719 # --------------------------------------------------------------------
721 =head2 GetBudgetsReport
723 &GetBudgetsReport( [$activity] );
725 Get all but cancelled orders for all funds.
727 If the optionnal activity parameter is passed, returns orders for active/inactive budgets only.
732 Returns an array of hashrefs.
736 sub GetBudgetsReport {
738 my $dbh = C4::Context->dbh;
740 SELECT o.*, b.budget_name
741 FROM aqbudgetperiods bp
742 INNER JOIN aqbudgets b
743 ON bp.budget_period_id = b.budget_period_id
744 INNER JOIN aqorders o
745 ON b.budget_id = o.budget_id ';
747 $query .= 'WHERE bp.budget_period_active=? ';
749 $query .= 'AND (o.orderstatus != "cancelled")
750 ORDER BY b.budget_name';
752 my $sth = $dbh->prepare($query);
754 $sth->execute($activity);
760 while ( my $data = $sth->fetchrow_hashref ) {
761 push( @results, $data );
766 =head2 GetBudgetByCode
768 my $budget = &GetBudgetByCode($budget_code);
770 Retrieve all aqbudgets fields as a hashref for the budget that has
775 sub GetBudgetByCode {
776 my ( $budget_code ) = @_;
778 my $dbh = C4::Context->dbh;
782 WHERE budget_code = ?
783 ORDER BY budget_id DESC
786 my $sth = $dbh->prepare( $query );
787 $sth->execute( $budget_code );
788 return $sth->fetchrow_hashref;
791 =head2 GetBudgetHierarchySpent
793 my $spent = GetBudgetHierarchySpent( $budget_id );
795 Gets the total spent of the level and sublevels of $budget_id
799 sub GetBudgetHierarchySpent {
800 my ( $budget_id ) = @_;
801 my $dbh = C4::Context->dbh;
802 my $children_ids = $dbh->selectcol_arrayref(q|
805 WHERE budget_parent_id = ?
808 my $total_spent = GetBudgetSpent( $budget_id );
809 for my $child_id ( @$children_ids ) {
810 $total_spent += GetBudgetHierarchySpent( $child_id );
815 =head2 GetBudgetHierarchyOrdered
817 my $ordered = GetBudgetHierarchyOrdered( $budget_id );
819 Gets the total ordered of the level and sublevels of $budget_id
823 sub GetBudgetHierarchyOrdered {
824 my ( $budget_id ) = @_;
825 my $dbh = C4::Context->dbh;
826 my $children_ids = $dbh->selectcol_arrayref(q|
829 WHERE budget_parent_id = ?
832 my $total_ordered = GetBudgetOrdered( $budget_id );
833 for my $child_id ( @$children_ids ) {
834 $total_ordered += GetBudgetHierarchyOrdered( $child_id );
836 return $total_ordered;
841 &GetBudgets($filter, $order_by);
847 # -------------------------------------------------------------------
849 my ($filters, $orderby) = @_;
850 $orderby = 'budget_name' unless($orderby);
852 my $rs = Koha::Database->new()->schema->resultset('Aqbudget');
853 $rs = $rs->search( $filters, { order_by => $orderby } );
854 $rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
858 =head2 GetBudgetUsers
860 my @borrowernumbers = &GetBudgetUsers($budget_id);
862 Return the list of borrowernumbers linked to a budget
867 my ($budget_id) = @_;
869 my $dbh = C4::Context->dbh;
871 SELECT borrowernumber
872 FROM aqbudgetborrowers
875 my $sth = $dbh->prepare($query);
876 $sth->execute($budget_id);
879 while (my ($borrowernumber) = $sth->fetchrow_array) {
880 push @borrowernumbers, $borrowernumber
883 return @borrowernumbers;
886 =head2 ModBudgetUsers
888 &ModBudgetUsers($budget_id, @borrowernumbers);
890 Modify the list of borrowernumbers linked to a budget
895 my ($budget_id, @budget_users_id) = @_;
897 return unless $budget_id;
899 my $dbh = C4::Context->dbh;
900 my $query = "DELETE FROM aqbudgetborrowers WHERE budget_id = ?";
901 my $sth = $dbh->prepare($query);
902 $sth->execute($budget_id);
905 INSERT INTO aqbudgetborrowers (budget_id, borrowernumber)
908 $sth = $dbh->prepare($query);
909 foreach my $borrowernumber (@budget_users_id) {
910 next unless $borrowernumber;
911 $sth->execute($budget_id, $borrowernumber);
915 sub CanUserUseBudget {
916 my ($borrower, $budget, $userflags) = @_;
918 if (not ref $borrower) {
919 $borrower = Koha::Patrons->find( $borrower )->unblessed;
921 if (not ref $budget) {
922 $budget = GetBudget($budget);
925 return 0 unless ($borrower and $budget);
927 if (not defined $userflags) {
928 $userflags = C4::Auth::getuserflags($borrower->{flags},
929 $borrower->{userid});
932 unless ($userflags->{superlibrarian}
933 || (ref $userflags->{acquisition}
934 && $userflags->{acquisition}->{budget_manage_all})
935 || (!ref $userflags->{acquisition} && $userflags->{acquisition}))
937 if (not exists $userflags->{acquisition}) {
941 if (!ref $userflags->{acquisition} && !$userflags->{acquisition}) {
945 # Budget restricted to owner
946 if ( $budget->{budget_permission} == 1 ) {
947 if ( $budget->{budget_owner_id}
948 and $budget->{budget_owner_id} != $borrower->{borrowernumber} )
954 # Budget restricted to owner, users and library
955 elsif ( $budget->{budget_permission} == 2 ) {
956 my @budget_users = GetBudgetUsers( $budget->{budget_id} );
960 $budget->{budget_owner_id}
961 and $budget->{budget_owner_id} !=
962 $borrower->{borrowernumber}
963 or not $budget->{budget_owner_id}
965 and ( 0 == grep { $borrower->{borrowernumber} == $_ }
967 and defined $budget->{budget_branchcode}
968 and $budget->{budget_branchcode} ne
969 C4::Context->userenv->{branch}
976 # Budget restricted to owner and users
977 elsif ( $budget->{budget_permission} == 3 ) {
978 my @budget_users = GetBudgetUsers( $budget->{budget_id} );
981 $budget->{budget_owner_id}
982 and $budget->{budget_owner_id} !=
983 $borrower->{borrowernumber}
984 or not $budget->{budget_owner_id}
986 and ( 0 == grep { $borrower->{borrowernumber} == $_ }
998 sub CanUserModifyBudget {
999 my ($borrower, $budget, $userflags) = @_;
1001 if (not ref $borrower) {
1002 $borrower = Koha::Patrons->find( $borrower )->unblessed;
1004 if (not ref $budget) {
1005 $budget = GetBudget($budget);
1008 return 0 unless ($borrower and $budget);
1010 if (not defined $userflags) {
1011 $userflags = C4::Auth::getuserflags($borrower->{flags},
1012 $borrower->{userid});
1015 unless ($userflags->{superlibrarian}
1016 || (ref $userflags->{acquisition}
1017 && $userflags->{acquisition}->{budget_manage_all})
1018 || (!ref $userflags->{acquisition} && $userflags->{acquisition}))
1020 if (!CanUserUseBudget($borrower, $budget, $userflags)) {
1024 if (ref $userflags->{acquisition}
1025 && !$userflags->{acquisition}->{budget_modify}) {
1034 my ($value, $increment) = @_;
1036 if ($increment && $increment != 0) {
1037 $value = int($value / $increment) * $increment;
1043 =head2 CloneBudgetPeriod
1045 my $new_budget_period_id = CloneBudgetPeriod({
1046 budget_period_id => $budget_period_id,
1047 budget_period_startdate => $budget_period_startdate,
1048 budget_period_enddate => $budget_period_enddate,
1049 mark_original_budget_as_inactive => 1n
1050 reset_all_budgets => 1,
1053 Clone a budget period with all budgets.
1054 If the mark_origin_budget_as_inactive is set (0 by default),
1055 the original budget will be marked as inactive.
1057 If the reset_all_budgets is set (0 by default), all budget (fund)
1058 amounts will be reset.
1062 sub CloneBudgetPeriod {
1064 my $budget_period_id = $params->{budget_period_id};
1065 my $budget_period_startdate = $params->{budget_period_startdate};
1066 my $budget_period_enddate = $params->{budget_period_enddate};
1067 my $budget_period_description = $params->{budget_period_description};
1068 my $amount_change_percentage = $params->{amount_change_percentage};
1069 my $amount_change_round_increment = $params->{amount_change_round_increment};
1070 my $mark_original_budget_as_inactive =
1071 $params->{mark_original_budget_as_inactive} || 0;
1072 my $reset_all_budgets = $params->{reset_all_budgets} || 0;
1074 my $budget_period = GetBudgetPeriod($budget_period_id);
1076 $budget_period->{budget_period_startdate} = $budget_period_startdate;
1077 $budget_period->{budget_period_enddate} = $budget_period_enddate;
1078 $budget_period->{budget_period_description} = $budget_period_description;
1079 # The new budget (budget_period) should be active by default
1080 $budget_period->{budget_period_active} = 1;
1082 if ($amount_change_percentage) {
1083 my $total = $budget_period->{budget_period_total};
1084 $total += $total * $amount_change_percentage / 100;
1085 $total = _round($total, $amount_change_round_increment);
1086 $budget_period->{budget_period_total} = $total;
1089 my $original_budget_period_id = $budget_period->{budget_period_id};
1090 delete $budget_period->{budget_period_id};
1091 my $new_budget_period_id = AddBudgetPeriod( $budget_period );
1093 my $budgets = GetBudgetHierarchy($budget_period_id);
1094 CloneBudgetHierarchy(
1096 budgets => $budgets,
1097 new_budget_period_id => $new_budget_period_id
1101 if ($mark_original_budget_as_inactive) {
1104 budget_period_id => $budget_period_id,
1105 budget_period_active => 0,
1110 if ( $reset_all_budgets ) {
1111 my $budgets = GetBudgets({ budget_period_id => $new_budget_period_id });
1112 for my $budget ( @$budgets ) {
1113 $budget->{budget_amount} = 0;
1114 ModBudget( $budget );
1116 } elsif ($amount_change_percentage) {
1117 my $budgets = GetBudgets({ budget_period_id => $new_budget_period_id });
1118 for my $budget ( @$budgets ) {
1119 my $amount = $budget->{budget_amount};
1120 $amount += $amount * $amount_change_percentage / 100;
1121 $amount = _round($amount, $amount_change_round_increment);
1122 $budget->{budget_amount} = $amount;
1123 ModBudget( $budget );
1127 return $new_budget_period_id;
1130 =head2 CloneBudgetHierarchy
1132 CloneBudgetHierarchy({
1133 budgets => $budgets,
1134 new_budget_period_id => $new_budget_period_id;
1137 Clone a budget hierarchy.
1141 sub CloneBudgetHierarchy {
1143 my $budgets = $params->{budgets};
1144 my $new_budget_period_id = $params->{new_budget_period_id};
1145 next unless @$budgets or $new_budget_period_id;
1147 my $children_of = $params->{children_of};
1148 my $new_parent_id = $params->{new_parent_id};
1150 my @first_level_budgets =
1151 ( not defined $children_of )
1152 ? map { ( not $_->{budget_parent_id} ) ? $_ : () } @$budgets
1153 : map { ( $_->{budget_parent_id} == $children_of ) ? $_ : () } @$budgets;
1155 # get only the columns of aqbudgets
1156 my @columns = Koha::Database->new()->schema->source('Aqbudget')->columns;
1158 for my $budget ( sort { $a->{budget_id} <=> $b->{budget_id} }
1159 @first_level_budgets )
1163 { map { join( ' ', @columns ) =~ /$_/ ? ( $_ => $budget->{$_} ) : () }
1165 my $new_budget_id = AddBudget(
1169 budget_parent_id => $new_parent_id,
1170 budget_period_id => $new_budget_period_id
1173 CloneBudgetHierarchy(
1175 budgets => $budgets,
1176 new_budget_period_id => $new_budget_period_id,
1177 children_of => $budget->{budget_id},
1178 new_parent_id => $new_budget_id
1186 my $report = MoveOrders({
1187 from_budget_period_id => $from_budget_period_id,
1188 to_budget_period_id => $to_budget_period_id,
1191 Move orders from one budget period to another.
1197 my $from_budget_period_id = $params->{from_budget_period_id};
1198 my $to_budget_period_id = $params->{to_budget_period_id};
1199 my $move_remaining_unspent = $params->{move_remaining_unspent};
1201 if not $from_budget_period_id
1202 or not $to_budget_period_id
1203 or $from_budget_period_id == $to_budget_period_id;
1205 # Can't move orders to an inactive budget (budgetperiod)
1206 my $budget_period = GetBudgetPeriod($to_budget_period_id);
1207 return unless $budget_period->{budget_period_active};
1210 my $dbh = C4::Context->dbh;
1211 my $sth_update_aqorders = $dbh->prepare(
1215 WHERE ordernumber = ?
1218 my $sth_update_budget_amount = $dbh->prepare(
1221 SET budget_amount = ?
1225 my $from_budgets = GetBudgetHierarchy($from_budget_period_id);
1226 for my $from_budget (@$from_budgets) {
1227 my $new_budget_id = $dbh->selectcol_arrayref(
1231 WHERE budget_period_id = ?
1233 |, {}, $to_budget_period_id, $from_budget->{budget_code}
1235 $new_budget_id = $new_budget_id->[0];
1236 my $new_budget = GetBudget( $new_budget_id );
1237 unless ( $new_budget ) {
1241 budget => $from_budget,
1242 error => 'budget_code_not_exists',
1246 my $orders_to_move = C4::Acquisition::SearchOrders(
1248 budget_id => $from_budget->{budget_id},
1254 for my $order (@$orders_to_move) {
1255 $sth_update_aqorders->execute( $new_budget->{budget_id}, $order->{ordernumber} );
1256 push @orders_moved, $order;
1259 my $unspent_moved = 0;
1260 if ($move_remaining_unspent) {
1261 my $spent = GetBudgetHierarchySpent( $from_budget->{budget_id} );
1262 my $unspent = $from_budget->{budget_amount} - $spent;
1263 my $new_budget_amount = $new_budget->{budget_amount};
1264 if ( $unspent > 0 ) {
1265 $new_budget_amount += $unspent;
1266 $unspent_moved = $unspent;
1268 $new_budget->{budget_amount} = $new_budget_amount;
1269 $sth_update_budget_amount->execute( $new_budget_amount,
1270 $new_budget->{budget_id} );
1275 budget => $new_budget,
1276 orders_moved => \@orders_moved,
1278 unspent_moved => $unspent_moved,
1284 END { } # module clean-up code here (global destructor)
1291 Koha Development Team <http://koha-community.org/>