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);
33 &GetBudgetByOrderNumber
47 GetBudgetHierarchySpent
48 GetBudgetHierarchyOrdered
68 &CheckBudgetParentPerm
75 # ----------------------------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_tax_included
) 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_tax_included
) 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_tax_included
* 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_tax_included
* 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 # unitprice_tax_included should always been set here
331 # we should not need to retrieve ecost_tax_included
332 my $sth = $dbh->prepare(qq|
333 SELECT SUM
( COALESCE
(unitprice_tax_included
, ecost_tax_included
) * quantity
) AS sum FROM aqorders
334 WHERE budget_id
= ? AND
335 quantityreceived
> 0 AND
336 datecancellationprinted IS NULL
338 $sth->execute($budget_id);
339 my $sum = $sth->fetchrow_array;
341 $sth = $dbh->prepare(qq|
342 SELECT SUM
(shipmentcost
) AS sum
344 WHERE shipmentcost_budgetid
= ?
345 AND closedate IS NOT NULL
347 $sth->execute($budget_id);
348 my ($shipmentcost_sum) = $sth->fetchrow_array;
349 $sum += $shipmentcost_sum;
354 # -------------------------------------------------------------------
355 sub GetBudgetOrdered
{
356 my ($budget_id) = @_;
357 my $dbh = C4
::Context
->dbh;
358 my $sth = $dbh->prepare(qq|
359 SELECT SUM
(ecost_tax_included
* quantity
) AS sum FROM aqorders
360 WHERE budget_id
= ? AND
361 quantityreceived
= 0 AND
362 datecancellationprinted IS NULL
364 $sth->execute($budget_id);
365 my $sum = $sth->fetchrow_array;
367 $sth = $dbh->prepare(qq|
368 SELECT SUM
(shipmentcost
) AS sum
370 WHERE shipmentcost_budgetid
= ?
371 AND closedate IS NULL
373 $sth->execute($budget_id);
374 my ($shipmentcost_sum) = $sth->fetchrow_array;
375 $sum += $shipmentcost_sum;
382 my $budget_name = &GetBudgetName($budget_id);
384 get the budget_name for a given budget_id
389 my ( $budget_id ) = @_;
390 my $dbh = C4
::Context
->dbh;
391 my $sth = $dbh->prepare(
398 $sth->execute($budget_id);
399 return $sth->fetchrow_array;
402 =head2 GetBudgetAuthCats
404 my $auth_cats = &GetBudgetAuthCats($budget_period_id);
406 Return the list of authcat for a given budget_period_id
410 sub GetBudgetAuthCats
{
411 my ($budget_period_id) = shift;
412 # now, populate the auth_cats_loop used in the budget planning button
413 # we must retrieve all auth values used by at least one budget
414 my $dbh = C4
::Context
->dbh;
415 my $sth=$dbh->prepare("SELECT sort1_authcat,sort2_authcat FROM aqbudgets WHERE budget_period_id=?");
416 $sth->execute($budget_period_id);
418 while (my ($sort1_authcat,$sort2_authcat) = $sth->fetchrow) {
419 $authcats{$sort1_authcat}=1 if $sort1_authcat;
420 $authcats{$sort2_authcat}=1 if $sort2_authcat;
422 return [ sort keys %authcats ];
425 # -------------------------------------------------------------------
426 sub GetBudgetPeriods
{
427 my ($filters,$orderby) = @_;
429 my $rs = Koha
::Database
->new()->schema->resultset('Aqbudgetperiod');
430 $rs = $rs->search( $filters, { order_by
=> $orderby } );
431 $rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
434 # -------------------------------------------------------------------
435 sub GetBudgetPeriod
{
436 my ($budget_period_id) = @_;
437 my $dbh = C4
::Context
->dbh;
438 ## $total = number of records linked to the record that must be deleted
440 ## get information about the record that will be deleted
442 if ($budget_period_id) {
443 $sth = $dbh->prepare( qq|
446 WHERE budget_period_id
=?
|
448 $sth->execute($budget_period_id);
449 } else { # ACTIVE BUDGET
450 $sth = $dbh->prepare(qq|
453 WHERE budget_period_active
=1 |
457 my $data = $sth->fetchrow_hashref;
462 my ($budget_period_id) = @_;
463 my $dbh = C4
::Context
->dbh;
464 ; ## $total = number of records linked to the record that must be deleted
467 ## get information about the record that will be deleted
468 my $sth = $dbh->prepare(qq|
471 WHERE budget_period_id
=?
|
473 return $sth->execute($budget_period_id);
476 # -------------------------------------------------------------------
477 sub ModBudgetPeriod
{
478 my ($budget_period) = @_;
479 my $result = Koha
::Database
->new()->schema->resultset('Aqbudgetperiod')->find($budget_period);
480 return unless($result);
482 $result = $result->update($budget_period);
483 return $result->in_storage;
486 # -------------------------------------------------------------------
487 sub GetBudgetHierarchy
{
488 my ( $budget_period_id, $branchcode, $owner ) = @_;
490 my $dbh = C4
::Context
->dbh;
492 SELECT aqbudgets
.*, aqbudgetperiods
.budget_period_active
, aqbudgetperiods
.budget_period_description
494 JOIN aqbudgetperiods USING
(budget_period_id
)|;
497 # show only period X if requested
498 if ($budget_period_id) {
499 push @where_strings," aqbudgets.budget_period_id = ?";
500 push @bind_params, $budget_period_id;
502 # show only budgets owned by me, my branch or everyone
506 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
="")))};
507 push @bind_params, ( $owner, $branchcode );
509 push @where_strings, ' (budget_owner_id = ? OR budget_owner_id IS NULL or budget_owner_id ="") ';
510 push @bind_params, $owner;
514 push @where_strings," (budget_branchcode =? or budget_branchcode is NULL)";
515 push @bind_params, $branchcode;
518 $query.=" WHERE ".join(' AND ', @where_strings) if @where_strings;
519 $debug && warn $query,join(",",@bind_params);
520 my $sth = $dbh->prepare($query);
521 $sth->execute(@bind_params);
524 # create hash with budget_id has key
525 while ( my $data = $sth->fetchrow_hashref ) {
526 $links{ $data->{'budget_id'} } = $data;
529 # link child to parent
531 foreach my $budget ( sort { $a->{budget_code
} cmp $b->{budget_code
} } values %links ) {
532 my $child = $links{$budget->{budget_id
}};
533 if ( $child->{'budget_parent_id'} ) {
534 my $parent = $links{ $child->{'budget_parent_id'} };
536 unless ( $parent->{'children'} ) {
537 # init child arrayref
538 $parent->{'children'} = [];
541 push @
{ $parent->{'children'} }, $child;
544 push @first_parents, $child;
549 foreach my $first_parent (@first_parents) {
550 _add_budget_children
(\
@sort, $first_parent, 0);
553 foreach my $budget (@sort) {
554 $budget->{budget_spent
} = GetBudgetSpent
( $budget->{budget_id
} );
555 $budget->{budget_ordered
} = GetBudgetOrdered
( $budget->{budget_id
} );
556 $budget->{total_spent
} = GetBudgetHierarchySpent
( $budget->{budget_id
} );
557 $budget->{total_ordered
} = GetBudgetHierarchyOrdered
( $budget->{budget_id
} );
562 # Recursive method to add a budget and its chidren to an array
563 sub _add_budget_children
{
566 $budget->{budget_level
} = shift;
568 my $children = $budget->{'children'} || [];
569 return unless @
$children; # break recursivity
570 foreach my $child (@
$children) {
571 _add_budget_children
($res, $child, $budget->{budget_level
} + 1);
575 # -------------------------------------------------------------------
579 return unless ($budget);
581 my $resultset = Koha
::Database
->new()->schema->resultset('Aqbudget');
582 return $resultset->create($budget)->id;
585 # -------------------------------------------------------------------
588 my $result = Koha
::Database
->new()->schema->resultset('Aqbudget')->find($budget);
589 return unless($result);
591 $result = $result->update($budget);
592 return $result->in_storage;
595 # -------------------------------------------------------------------
597 my ($budget_id) = @_;
598 my $dbh = C4
::Context
->dbh;
599 my $sth = $dbh->prepare("delete from aqbudgets where budget_id=?");
600 my $rc = $sth->execute($budget_id);
605 # -------------------------------------------------------------------
609 &GetBudget($budget_id);
611 get a specific budget
616 my ( $budget_id ) = @_;
617 my $dbh = C4
::Context
->dbh;
623 my $sth = $dbh->prepare($query);
624 $sth->execute( $budget_id );
625 my $result = $sth->fetchrow_hashref;
629 # -------------------------------------------------------------------
631 =head2 GetBudgetByOrderNumber
633 &GetBudgetByOrderNumber($ordernumber);
635 get a specific budget by order number
639 sub GetBudgetByOrderNumber
{
640 my ( $ordernumber ) = @_;
641 my $dbh = C4
::Context
->dbh;
644 FROM aqbudgets, aqorders
646 AND aqorders.budget_id = aqbudgets.budget_id
648 my $sth = $dbh->prepare($query);
649 $sth->execute( $ordernumber );
650 my $result = $sth->fetchrow_hashref;
654 =head2 GetBudgetReport
656 &GetBudgetReport( [$budget_id] );
658 Get all orders for a specific budget, without cancelled orders.
660 Returns an array of hashrefs.
664 # --------------------------------------------------------------------
665 sub GetBudgetReport
{
666 my ( $budget_id ) = @_;
667 my $dbh = C4
::Context
->dbh;
669 SELECT o.*, b.budget_name
671 INNER JOIN aqorders o
672 ON b.budget_id = o.budget_id
674 AND (o.orderstatus != "cancelled")
675 ORDER BY b.budget_name';
677 my $sth = $dbh->prepare($query);
678 $sth->execute( $budget_id );
681 while ( my $data = $sth->fetchrow_hashref ) {
682 push( @results, $data );
687 =head2 GetBudgetsByActivity
689 &GetBudgetsByActivity( $budget_period_active );
691 Get all active or inactive budgets, depending of the value
699 # --------------------------------------------------------------------
700 sub GetBudgetsByActivity
{
701 my ( $budget_period_active ) = @_;
702 my $dbh = C4
::Context
->dbh;
705 FROM aqbudgetperiods bp
706 INNER JOIN aqbudgets b
707 ON bp.budget_period_id = b.budget_period_id
708 WHERE bp.budget_period_active=?
710 my $sth = $dbh->prepare($query);
711 $sth->execute( $budget_period_active );
713 while ( my $data = $sth->fetchrow_hashref ) {
714 push( @results, $data );
718 # --------------------------------------------------------------------
720 =head2 GetBudgetsReport
722 &GetBudgetsReport( [$activity] );
724 Get all but cancelled orders for all funds.
726 If the optionnal activity parameter is passed, returns orders for active/inactive budgets only.
731 Returns an array of hashrefs.
735 sub GetBudgetsReport
{
737 my $dbh = C4
::Context
->dbh;
739 SELECT o.*, b.budget_name
740 FROM aqbudgetperiods bp
741 INNER JOIN aqbudgets b
742 ON bp.budget_period_id = b.budget_period_id
743 INNER JOIN aqorders o
744 ON b.budget_id = o.budget_id ';
746 $query .= 'WHERE bp.budget_period_active=? ';
748 $query .= 'AND (o.orderstatus != "cancelled")
749 ORDER BY b.budget_name';
751 my $sth = $dbh->prepare($query);
753 $sth->execute($activity);
759 while ( my $data = $sth->fetchrow_hashref ) {
760 push( @results, $data );
765 =head2 GetBudgetByCode
767 my $budget = &GetBudgetByCode($budget_code);
769 Retrieve all aqbudgets fields as a hashref for the budget that has
774 sub GetBudgetByCode
{
775 my ( $budget_code ) = @_;
777 my $dbh = C4
::Context
->dbh;
781 WHERE budget_code
= ?
782 ORDER BY budget_id DESC
785 my $sth = $dbh->prepare( $query );
786 $sth->execute( $budget_code );
787 return $sth->fetchrow_hashref;
790 =head2 GetBudgetHierarchySpent
792 my $spent = GetBudgetHierarchySpent( $budget_id );
794 Gets the total spent of the level and sublevels of $budget_id
798 sub GetBudgetHierarchySpent
{
799 my ( $budget_id ) = @_;
800 my $dbh = C4
::Context
->dbh;
801 my $children_ids = $dbh->selectcol_arrayref(q
|
804 WHERE budget_parent_id
= ?
807 my $total_spent = GetBudgetSpent
( $budget_id );
808 for my $child_id ( @
$children_ids ) {
809 $total_spent += GetBudgetHierarchySpent
( $child_id );
814 =head2 GetBudgetHierarchyOrdered
816 my $ordered = GetBudgetHierarchyOrdered( $budget_id );
818 Gets the total ordered of the level and sublevels of $budget_id
822 sub GetBudgetHierarchyOrdered
{
823 my ( $budget_id ) = @_;
824 my $dbh = C4
::Context
->dbh;
825 my $children_ids = $dbh->selectcol_arrayref(q
|
828 WHERE budget_parent_id
= ?
831 my $total_ordered = GetBudgetOrdered
( $budget_id );
832 for my $child_id ( @
$children_ids ) {
833 $total_ordered += GetBudgetHierarchyOrdered
( $child_id );
835 return $total_ordered;
840 &GetBudgets($filter, $order_by);
846 # -------------------------------------------------------------------
848 my ($filters, $orderby) = @_;
849 $orderby = 'budget_name' unless($orderby);
851 my $rs = Koha
::Database
->new()->schema->resultset('Aqbudget');
852 $rs = $rs->search( $filters, { order_by
=> $orderby } );
853 $rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
857 =head2 GetBudgetUsers
859 my @borrowernumbers = &GetBudgetUsers($budget_id);
861 Return the list of borrowernumbers linked to a budget
866 my ($budget_id) = @_;
868 my $dbh = C4
::Context
->dbh;
870 SELECT borrowernumber
871 FROM aqbudgetborrowers
874 my $sth = $dbh->prepare($query);
875 $sth->execute($budget_id);
878 while (my ($borrowernumber) = $sth->fetchrow_array) {
879 push @borrowernumbers, $borrowernumber
882 return @borrowernumbers;
885 =head2 ModBudgetUsers
887 &ModBudgetUsers($budget_id, @borrowernumbers);
889 Modify the list of borrowernumbers linked to a budget
894 my ($budget_id, @budget_users_id) = @_;
896 return unless $budget_id;
898 my $dbh = C4
::Context
->dbh;
899 my $query = "DELETE FROM aqbudgetborrowers WHERE budget_id = ?";
900 my $sth = $dbh->prepare($query);
901 $sth->execute($budget_id);
904 INSERT INTO aqbudgetborrowers
(budget_id
, borrowernumber
)
907 $sth = $dbh->prepare($query);
908 foreach my $borrowernumber (@budget_users_id) {
909 next unless $borrowernumber;
910 $sth->execute($budget_id, $borrowernumber);
914 sub CanUserUseBudget
{
915 my ($borrower, $budget, $userflags) = @_;
917 if (not ref $borrower) {
918 $borrower = C4
::Members
::GetMember
(borrowernumber
=> $borrower);
920 if (not ref $budget) {
921 $budget = GetBudget
($budget);
924 return 0 unless ($borrower and $budget);
926 if (not defined $userflags) {
927 $userflags = C4
::Auth
::getuserflags
($borrower->{flags
},
928 $borrower->{userid
});
931 unless ($userflags->{superlibrarian
}
932 || (ref $userflags->{acquisition
}
933 && $userflags->{acquisition
}->{budget_manage_all
})
934 || (!ref $userflags->{acquisition
} && $userflags->{acquisition
}))
936 if (not exists $userflags->{acquisition
}) {
940 if (!ref $userflags->{acquisition
} && !$userflags->{acquisition
}) {
944 # Budget restricted to owner
945 if ( $budget->{budget_permission
} == 1 ) {
946 if ( $budget->{budget_owner_id
}
947 and $budget->{budget_owner_id
} != $borrower->{borrowernumber
} )
953 # Budget restricted to owner, users and library
954 elsif ( $budget->{budget_permission
} == 2 ) {
955 my @budget_users = GetBudgetUsers
( $budget->{budget_id
} );
959 $budget->{budget_owner_id
}
960 and $budget->{budget_owner_id
} !=
961 $borrower->{borrowernumber
}
962 or not $budget->{budget_owner_id
}
964 and ( 0 == grep { $borrower->{borrowernumber
} == $_ }
966 and defined $budget->{budget_branchcode
}
967 and $budget->{budget_branchcode
} ne
968 C4
::Context
->userenv->{branch
}
975 # Budget restricted to owner and users
976 elsif ( $budget->{budget_permission
} == 3 ) {
977 my @budget_users = GetBudgetUsers
( $budget->{budget_id
} );
980 $budget->{budget_owner_id
}
981 and $budget->{budget_owner_id
} !=
982 $borrower->{borrowernumber
}
983 or not $budget->{budget_owner_id
}
985 and ( 0 == grep { $borrower->{borrowernumber
} == $_ }
997 sub CanUserModifyBudget
{
998 my ($borrower, $budget, $userflags) = @_;
1000 if (not ref $borrower) {
1001 $borrower = C4
::Members
::GetMember
(borrowernumber
=> $borrower);
1003 if (not ref $budget) {
1004 $budget = GetBudget
($budget);
1007 return 0 unless ($borrower and $budget);
1009 if (not defined $userflags) {
1010 $userflags = C4
::Auth
::getuserflags
($borrower->{flags
},
1011 $borrower->{userid
});
1014 unless ($userflags->{superlibrarian
}
1015 || (ref $userflags->{acquisition
}
1016 && $userflags->{acquisition
}->{budget_manage_all
})
1017 || (!ref $userflags->{acquisition
} && $userflags->{acquisition
}))
1019 if (!CanUserUseBudget
($borrower, $budget, $userflags)) {
1023 if (ref $userflags->{acquisition
}
1024 && !$userflags->{acquisition
}->{budget_modify
}) {
1033 my ($value, $increment) = @_;
1035 if ($increment && $increment != 0) {
1036 $value = int($value / $increment) * $increment;
1042 =head2 CloneBudgetPeriod
1044 my $new_budget_period_id = CloneBudgetPeriod({
1045 budget_period_id => $budget_period_id,
1046 budget_period_startdate => $budget_period_startdate,
1047 budget_period_enddate => $budget_period_enddate,
1048 mark_original_budget_as_inactive => 1n
1049 reset_all_budgets => 1,
1052 Clone a budget period with all budgets.
1053 If the mark_origin_budget_as_inactive is set (0 by default),
1054 the original budget will be marked as inactive.
1056 If the reset_all_budgets is set (0 by default), all budget (fund)
1057 amounts will be reset.
1061 sub CloneBudgetPeriod
{
1063 my $budget_period_id = $params->{budget_period_id
};
1064 my $budget_period_startdate = $params->{budget_period_startdate
};
1065 my $budget_period_enddate = $params->{budget_period_enddate
};
1066 my $budget_period_description = $params->{budget_period_description
};
1067 my $amount_change_percentage = $params->{amount_change_percentage
};
1068 my $amount_change_round_increment = $params->{amount_change_round_increment
};
1069 my $mark_original_budget_as_inactive =
1070 $params->{mark_original_budget_as_inactive
} || 0;
1071 my $reset_all_budgets = $params->{reset_all_budgets
} || 0;
1073 my $budget_period = GetBudgetPeriod
($budget_period_id);
1075 $budget_period->{budget_period_startdate
} = $budget_period_startdate;
1076 $budget_period->{budget_period_enddate
} = $budget_period_enddate;
1077 $budget_period->{budget_period_description
} = $budget_period_description;
1078 # The new budget (budget_period) should be active by default
1079 $budget_period->{budget_period_active
} = 1;
1081 if ($amount_change_percentage) {
1082 my $total = $budget_period->{budget_period_total
};
1083 $total += $total * $amount_change_percentage / 100;
1084 $total = _round
($total, $amount_change_round_increment);
1085 $budget_period->{budget_period_total
} = $total;
1088 my $original_budget_period_id = $budget_period->{budget_period_id
};
1089 delete $budget_period->{budget_period_id
};
1090 my $new_budget_period_id = AddBudgetPeriod
( $budget_period );
1092 my $budgets = GetBudgetHierarchy
($budget_period_id);
1093 CloneBudgetHierarchy
(
1095 budgets
=> $budgets,
1096 new_budget_period_id
=> $new_budget_period_id
1100 if ($mark_original_budget_as_inactive) {
1103 budget_period_id
=> $budget_period_id,
1104 budget_period_active
=> 0,
1109 if ( $reset_all_budgets ) {
1110 my $budgets = GetBudgets
({ budget_period_id
=> $new_budget_period_id });
1111 for my $budget ( @
$budgets ) {
1112 $budget->{budget_amount
} = 0;
1113 ModBudget
( $budget );
1115 } elsif ($amount_change_percentage) {
1116 my $budgets = GetBudgets
({ budget_period_id
=> $new_budget_period_id });
1117 for my $budget ( @
$budgets ) {
1118 my $amount = $budget->{budget_amount
};
1119 $amount += $amount * $amount_change_percentage / 100;
1120 $amount = _round
($amount, $amount_change_round_increment);
1121 $budget->{budget_amount
} = $amount;
1122 ModBudget
( $budget );
1126 return $new_budget_period_id;
1129 =head2 CloneBudgetHierarchy
1131 CloneBudgetHierarchy({
1132 budgets => $budgets,
1133 new_budget_period_id => $new_budget_period_id;
1136 Clone a budget hierarchy.
1140 sub CloneBudgetHierarchy
{
1142 my $budgets = $params->{budgets
};
1143 my $new_budget_period_id = $params->{new_budget_period_id
};
1144 next unless @
$budgets or $new_budget_period_id;
1146 my $children_of = $params->{children_of
};
1147 my $new_parent_id = $params->{new_parent_id
};
1149 my @first_level_budgets =
1150 ( not defined $children_of )
1151 ?
map { ( not $_->{budget_parent_id
} ) ?
$_ : () } @
$budgets
1152 : map { ( $_->{budget_parent_id
} == $children_of ) ?
$_ : () } @
$budgets;
1154 # get only the columns of aqbudgets
1155 my @columns = Koha
::Database
->new()->schema->source('Aqbudget')->columns;
1157 for my $budget ( sort { $a->{budget_id
} <=> $b->{budget_id
} }
1158 @first_level_budgets )
1162 { map { join( ' ', @columns ) =~ /$_/ ?
( $_ => $budget->{$_} ) : () }
1164 my $new_budget_id = AddBudget
(
1168 budget_parent_id
=> $new_parent_id,
1169 budget_period_id
=> $new_budget_period_id
1172 CloneBudgetHierarchy
(
1174 budgets
=> $budgets,
1175 new_budget_period_id
=> $new_budget_period_id,
1176 children_of
=> $budget->{budget_id
},
1177 new_parent_id
=> $new_budget_id
1185 my $report = MoveOrders({
1186 from_budget_period_id => $from_budget_period_id,
1187 to_budget_period_id => $to_budget_period_id,
1190 Move orders from one budget period to another.
1196 my $from_budget_period_id = $params->{from_budget_period_id
};
1197 my $to_budget_period_id = $params->{to_budget_period_id
};
1198 my $move_remaining_unspent = $params->{move_remaining_unspent
};
1200 if not $from_budget_period_id
1201 or not $to_budget_period_id
1202 or $from_budget_period_id == $to_budget_period_id;
1204 # Can't move orders to an inactive budget (budgetperiod)
1205 my $budget_period = GetBudgetPeriod
($to_budget_period_id);
1206 return unless $budget_period->{budget_period_active
};
1209 my $dbh = C4
::Context
->dbh;
1210 my $sth_update_aqorders = $dbh->prepare(
1214 WHERE ordernumber
= ?
1217 my $sth_update_budget_amount = $dbh->prepare(
1220 SET budget_amount
= ?
1224 my $from_budgets = GetBudgetHierarchy
($from_budget_period_id);
1225 for my $from_budget (@
$from_budgets) {
1226 my $new_budget_id = $dbh->selectcol_arrayref(
1230 WHERE budget_period_id
= ?
1232 |, {}, $to_budget_period_id, $from_budget->{budget_code
}
1234 $new_budget_id = $new_budget_id->[0];
1235 my $new_budget = GetBudget
( $new_budget_id );
1236 unless ( $new_budget ) {
1240 budget
=> $from_budget,
1241 error
=> 'budget_code_not_exists',
1245 my $orders_to_move = C4
::Acquisition
::SearchOrders
(
1247 budget_id
=> $from_budget->{budget_id
},
1253 for my $order (@
$orders_to_move) {
1254 $sth_update_aqorders->execute( $new_budget->{budget_id
}, $order->{ordernumber
} );
1255 push @orders_moved, $order;
1258 my $unspent_moved = 0;
1259 if ($move_remaining_unspent) {
1260 my $spent = GetBudgetHierarchySpent
( $from_budget->{budget_id
} );
1261 my $unspent = $from_budget->{budget_amount
} - $spent;
1262 my $new_budget_amount = $new_budget->{budget_amount
};
1263 if ( $unspent > 0 ) {
1264 $new_budget_amount += $unspent;
1265 $unspent_moved = $unspent;
1267 $new_budget->{budget_amount
} = $new_budget_amount;
1268 $sth_update_budget_amount->execute( $new_budget_amount,
1269 $new_budget->{budget_id
} );
1274 budget
=> $new_budget,
1275 orders_moved
=> \
@orders_moved,
1277 unspent_moved
=> $unspent_moved,
1283 END { } # module clean-up code here (global destructor)
1290 Koha Development Team <http://koha-community.org/>