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
= ?
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;
373 my $budget_name = &GetBudgetName($budget_id);
375 get the budget_name for a given budget_id
380 my ( $budget_id ) = @_;
381 my $dbh = C4
::Context
->dbh;
382 my $sth = $dbh->prepare(
389 $sth->execute($budget_id);
390 return $sth->fetchrow_array;
393 =head2 GetBudgetAuthCats
395 my $auth_cats = &GetBudgetAuthCats($budget_period_id);
397 Return the list of authcat for a given budget_period_id
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 if $sort1_authcat;
411 $authcats{$sort2_authcat}=1 if $sort2_authcat;
413 return [ sort keys %authcats ];
416 # -------------------------------------------------------------------
417 sub GetBudgetPeriods
{
418 my ($filters,$orderby) = @_;
420 my $rs = Koha
::Database
->new()->schema->resultset('Aqbudgetperiod');
421 $rs = $rs->search( $filters, { order_by
=> $orderby } );
422 $rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
425 # -------------------------------------------------------------------
426 sub GetBudgetPeriod
{
427 my ($budget_period_id) = @_;
428 my $dbh = C4
::Context
->dbh;
429 ## $total = number of records linked to the record that must be deleted
431 ## get information about the record that will be deleted
433 if ($budget_period_id) {
434 $sth = $dbh->prepare( qq|
437 WHERE budget_period_id
=?
|
439 $sth->execute($budget_period_id);
440 } else { # ACTIVE BUDGET
441 $sth = $dbh->prepare(qq|
444 WHERE budget_period_active
=1 |
448 my $data = $sth->fetchrow_hashref;
453 my ($budget_period_id) = @_;
454 my $dbh = C4
::Context
->dbh;
455 ; ## $total = number of records linked to the record that must be deleted
458 ## get information about the record that will be deleted
459 my $sth = $dbh->prepare(qq|
462 WHERE budget_period_id
=?
|
464 return $sth->execute($budget_period_id);
467 # -------------------------------------------------------------------
468 sub ModBudgetPeriod
{
469 my ($budget_period) = @_;
470 my $result = Koha
::Database
->new()->schema->resultset('Aqbudgetperiod')->find($budget_period);
471 return unless($result);
473 $result = $result->update($budget_period);
474 return $result->in_storage;
477 # -------------------------------------------------------------------
478 sub GetBudgetHierarchy
{
479 my ( $budget_period_id, $branchcode, $owner ) = @_;
481 my $dbh = C4
::Context
->dbh;
483 SELECT aqbudgets
.*, aqbudgetperiods
.budget_period_active
, aqbudgetperiods
.budget_period_description
,
484 b
.firstname as budget_owner_firstname
, b
.surname as budget_owner_surname
, b
.borrowernumber as budget_owner_borrowernumber
486 LEFT JOIN borrowers b on b
.borrowernumber
= aqbudgets
.budget_owner_id
487 JOIN aqbudgetperiods USING
(budget_period_id
)|;
490 # show only period X if requested
491 if ($budget_period_id) {
492 push @where_strings," aqbudgets.budget_period_id = ?";
493 push @bind_params, $budget_period_id;
495 # show only budgets owned by me, my branch or everyone
499 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
="")))};
500 push @bind_params, ( $owner, $branchcode );
502 push @where_strings, ' (budget_owner_id = ? OR budget_owner_id IS NULL or budget_owner_id ="") ';
503 push @bind_params, $owner;
507 push @where_strings," (budget_branchcode =? or budget_branchcode is NULL OR budget_branchcode='')";
508 push @bind_params, $branchcode;
511 $query.=" WHERE ".join(' AND ', @where_strings) if @where_strings;
512 $debug && warn $query,join(",",@bind_params);
513 my $sth = $dbh->prepare($query);
514 $sth->execute(@bind_params);
517 # create hash with budget_id has key
518 while ( my $data = $sth->fetchrow_hashref ) {
519 $links{ $data->{'budget_id'} } = $data;
522 # link child to parent
524 foreach my $budget ( sort { $a->{budget_code
} cmp $b->{budget_code
} } values %links ) {
525 my $child = $links{$budget->{budget_id
}};
526 if ( $child->{'budget_parent_id'} ) {
527 my $parent = $links{ $child->{'budget_parent_id'} };
529 unless ( $parent->{'children'} ) {
530 # init child arrayref
531 $parent->{'children'} = [];
534 push @
{ $parent->{'children'} }, $child;
537 push @first_parents, $child;
542 foreach my $first_parent (@first_parents) {
543 _add_budget_children
(\
@sort, $first_parent, 0);
546 # Get all the budgets totals in as few queries as possible
547 my $hr_budget_spent = $dbh->selectall_hashref(q
|
548 SELECT aqorders
.budget_id
, aqbudgets
.budget_parent_id
,
549 SUM
( COALESCE
(unitprice_tax_included
, ecost_tax_included
) * quantity
) AS budget_spent
550 FROM aqorders JOIN aqbudgets USING
(budget_id
)
551 WHERE quantityreceived
> 0 AND datecancellationprinted IS NULL
554 my $hr_budget_ordered = $dbh->selectall_hashref(q
|
555 SELECT aqorders
.budget_id
, aqbudgets
.budget_parent_id
,
556 SUM
(ecost_tax_included
* quantity
) AS budget_ordered
557 FROM aqorders JOIN aqbudgets USING
(budget_id
)
558 WHERE quantityreceived
= 0 AND datecancellationprinted IS NULL
561 my $hr_budget_spent_shipment = $dbh->selectall_hashref(q
|
562 SELECT shipmentcost_budgetid as budget_id
,
563 SUM
(shipmentcost
) as shipmentcost
565 WHERE closedate IS NOT NULL
566 GROUP BY shipmentcost_budgetid
568 my $hr_budget_ordered_shipment = $dbh->selectall_hashref(q
|
569 SELECT shipmentcost_budgetid as budget_id
,
570 SUM
(shipmentcost
) as shipmentcost
572 WHERE closedate IS NULL
573 GROUP BY shipmentcost_budgetid
577 foreach my $budget (@sort) {
578 if ( not defined $budget->{budget_parent_id
} ) {
579 _recursiveAdd
( $budget, undef, $hr_budget_spent, $hr_budget_spent_shipment, $hr_budget_ordered, $hr_budget_ordered_shipment );
586 my ($budget, $parent, $hr_budget_spent, $hr_budget_spent_shipment, $hr_budget_ordered, $hr_budget_ordered_shipment ) = @_;
588 foreach my $child (@
{$budget->{children
}}){
589 _recursiveAdd
($child, $budget, $hr_budget_spent, $hr_budget_spent_shipment, $hr_budget_ordered, $hr_budget_ordered_shipment );
592 $budget->{budget_spent
} += $hr_budget_spent->{$budget->{budget_id
}}->{budget_spent
};
593 $budget->{budget_spent
} += $hr_budget_spent_shipment->{$budget->{budget_id
}}->{shipmentcost
};
594 $budget->{budget_ordered
} += $hr_budget_ordered->{$budget->{budget_id
}}->{budget_ordered
};
595 $budget->{budget_ordered
} += $hr_budget_ordered_shipment->{$budget->{budget_id
}}->{shipmentcost
};
597 $budget->{total_spent
} += $budget->{budget_spent
};
598 $budget->{total_ordered
} += $budget->{budget_ordered
};
601 $parent->{total_spent
} += $budget->{total_spent
};
602 $parent->{total_ordered
} += $budget->{total_ordered
};
606 # Recursive method to add a budget and its chidren to an array
607 sub _add_budget_children
{
610 $budget->{budget_level
} = shift;
612 my $children = $budget->{'children'} || [];
613 return unless @
$children; # break recursivity
614 foreach my $child (@
$children) {
615 _add_budget_children
($res, $child, $budget->{budget_level
} + 1);
619 # -------------------------------------------------------------------
623 return unless ($budget);
625 my $resultset = Koha
::Database
->new()->schema->resultset('Aqbudget');
626 return $resultset->create($budget)->id;
629 # -------------------------------------------------------------------
632 my $result = Koha
::Database
->new()->schema->resultset('Aqbudget')->find($budget);
633 return unless($result);
635 $result = $result->update($budget);
636 return $result->in_storage;
639 # -------------------------------------------------------------------
641 my ($budget_id) = @_;
642 my $dbh = C4
::Context
->dbh;
643 my $sth = $dbh->prepare("delete from aqbudgets where budget_id=?");
644 my $rc = $sth->execute($budget_id);
649 # -------------------------------------------------------------------
653 &GetBudget($budget_id);
655 get a specific budget
660 my ( $budget_id ) = @_;
661 my $dbh = C4
::Context
->dbh;
667 my $sth = $dbh->prepare($query);
668 $sth->execute( $budget_id );
669 my $result = $sth->fetchrow_hashref;
673 # -------------------------------------------------------------------
675 =head2 GetBudgetByOrderNumber
677 &GetBudgetByOrderNumber($ordernumber);
679 get a specific budget by order number
683 sub GetBudgetByOrderNumber
{
684 my ( $ordernumber ) = @_;
685 my $dbh = C4
::Context
->dbh;
688 FROM aqbudgets, aqorders
690 AND aqorders.budget_id = aqbudgets.budget_id
692 my $sth = $dbh->prepare($query);
693 $sth->execute( $ordernumber );
694 my $result = $sth->fetchrow_hashref;
698 =head2 GetBudgetReport
700 &GetBudgetReport( [$budget_id] );
702 Get all orders for a specific budget, without cancelled orders.
704 Returns an array of hashrefs.
708 # --------------------------------------------------------------------
709 sub GetBudgetReport
{
710 my ( $budget_id ) = @_;
711 my $dbh = C4
::Context
->dbh;
713 SELECT o.*, b.budget_name
715 INNER JOIN aqorders o
716 ON b.budget_id = o.budget_id
718 AND (o.orderstatus != "cancelled")
719 ORDER BY b.budget_name';
721 my $sth = $dbh->prepare($query);
722 $sth->execute( $budget_id );
725 while ( my $data = $sth->fetchrow_hashref ) {
726 push( @results, $data );
731 =head2 GetBudgetsByActivity
733 &GetBudgetsByActivity( $budget_period_active );
735 Get all active or inactive budgets, depending of the value
743 # --------------------------------------------------------------------
744 sub GetBudgetsByActivity
{
745 my ( $budget_period_active ) = @_;
746 my $dbh = C4
::Context
->dbh;
749 FROM aqbudgetperiods bp
750 INNER JOIN aqbudgets b
751 ON bp.budget_period_id = b.budget_period_id
752 WHERE bp.budget_period_active=?
754 my $sth = $dbh->prepare($query);
755 $sth->execute( $budget_period_active );
757 while ( my $data = $sth->fetchrow_hashref ) {
758 push( @results, $data );
762 # --------------------------------------------------------------------
764 =head2 GetBudgetsReport
766 &GetBudgetsReport( [$activity] );
768 Get all but cancelled orders for all funds.
770 If the optionnal activity parameter is passed, returns orders for active/inactive budgets only.
775 Returns an array of hashrefs.
779 sub GetBudgetsReport
{
781 my $dbh = C4
::Context
->dbh;
783 SELECT o.*, b.budget_name
784 FROM aqbudgetperiods bp
785 INNER JOIN aqbudgets b
786 ON bp.budget_period_id = b.budget_period_id
787 INNER JOIN aqorders o
788 ON b.budget_id = o.budget_id ';
790 $query .= 'WHERE bp.budget_period_active=? ';
792 $query .= 'AND (o.orderstatus != "cancelled")
793 ORDER BY b.budget_name';
795 my $sth = $dbh->prepare($query);
797 $sth->execute($activity);
803 while ( my $data = $sth->fetchrow_hashref ) {
804 push( @results, $data );
809 =head2 GetBudgetByCode
811 my $budget = &GetBudgetByCode($budget_code);
813 Retrieve all aqbudgets fields as a hashref for the budget that has
818 sub GetBudgetByCode
{
819 my ( $budget_code ) = @_;
821 my $dbh = C4
::Context
->dbh;
825 JOIN aqbudgetperiods USING
(budget_period_id
)
826 WHERE budget_code
= ?
827 ORDER BY budget_period_active DESC
, budget_id DESC
830 my $sth = $dbh->prepare( $query );
831 $sth->execute( $budget_code );
832 return $sth->fetchrow_hashref;
835 =head2 GetBudgetHierarchySpent
837 my $spent = GetBudgetHierarchySpent( $budget_id );
839 Gets the total spent of the level and sublevels of $budget_id
843 sub GetBudgetHierarchySpent
{
844 my ( $budget_id ) = @_;
845 my $dbh = C4
::Context
->dbh;
846 my $children_ids = $dbh->selectcol_arrayref(q
|
849 WHERE budget_parent_id
= ?
852 my $total_spent = GetBudgetSpent
( $budget_id );
853 for my $child_id ( @
$children_ids ) {
854 $total_spent += GetBudgetHierarchySpent
( $child_id );
859 =head2 GetBudgetHierarchyOrdered
861 my $ordered = GetBudgetHierarchyOrdered( $budget_id );
863 Gets the total ordered of the level and sublevels of $budget_id
867 sub GetBudgetHierarchyOrdered
{
868 my ( $budget_id ) = @_;
869 my $dbh = C4
::Context
->dbh;
870 my $children_ids = $dbh->selectcol_arrayref(q
|
873 WHERE budget_parent_id
= ?
876 my $total_ordered = GetBudgetOrdered
( $budget_id );
877 for my $child_id ( @
$children_ids ) {
878 $total_ordered += GetBudgetHierarchyOrdered
( $child_id );
880 return $total_ordered;
885 &GetBudgets($filter, $order_by);
891 # -------------------------------------------------------------------
893 my ($filters, $orderby) = @_;
894 $orderby = 'budget_name' unless($orderby);
896 my $rs = Koha
::Database
->new()->schema->resultset('Aqbudget');
897 $rs = $rs->search( $filters, { order_by
=> $orderby } );
898 $rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
902 =head2 GetBudgetUsers
904 my @borrowernumbers = &GetBudgetUsers($budget_id);
906 Return the list of borrowernumbers linked to a budget
911 my ($budget_id) = @_;
913 my $dbh = C4
::Context
->dbh;
915 SELECT borrowernumber
916 FROM aqbudgetborrowers
919 my $sth = $dbh->prepare($query);
920 $sth->execute($budget_id);
923 while (my ($borrowernumber) = $sth->fetchrow_array) {
924 push @borrowernumbers, $borrowernumber
927 return @borrowernumbers;
930 =head2 ModBudgetUsers
932 &ModBudgetUsers($budget_id, @borrowernumbers);
934 Modify the list of borrowernumbers linked to a budget
939 my ($budget_id, @budget_users_id) = @_;
941 return unless $budget_id;
943 my $dbh = C4
::Context
->dbh;
944 my $query = "DELETE FROM aqbudgetborrowers WHERE budget_id = ?";
945 my $sth = $dbh->prepare($query);
946 $sth->execute($budget_id);
949 INSERT INTO aqbudgetborrowers
(budget_id
, borrowernumber
)
952 $sth = $dbh->prepare($query);
953 foreach my $borrowernumber (@budget_users_id) {
954 next unless $borrowernumber;
955 $sth->execute($budget_id, $borrowernumber);
959 sub CanUserUseBudget
{
960 my ($borrower, $budget, $userflags) = @_;
962 if (not ref $borrower) {
963 $borrower = Koha
::Patrons
->find( $borrower );
964 return 0 unless $borrower;
965 $borrower = $borrower->unblessed;
967 if (not ref $budget) {
968 $budget = GetBudget
($budget);
971 return 0 unless ($borrower and $budget);
973 if (not defined $userflags) {
974 $userflags = C4
::Auth
::getuserflags
($borrower->{flags
},
975 $borrower->{userid
});
978 unless ($userflags->{superlibrarian
}
979 || (ref $userflags->{acquisition
}
980 && $userflags->{acquisition
}->{budget_manage_all
})
981 || (!ref $userflags->{acquisition
} && $userflags->{acquisition
}))
983 if (not exists $userflags->{acquisition
}) {
987 if (!ref $userflags->{acquisition
} && !$userflags->{acquisition
}) {
991 # Budget restricted to owner
992 if ( $budget->{budget_permission
} == 1 ) {
993 if ( $budget->{budget_owner_id
}
994 and $budget->{budget_owner_id
} != $borrower->{borrowernumber
} )
1000 # Budget restricted to owner, users and library
1001 elsif ( $budget->{budget_permission
} == 2 ) {
1002 my @budget_users = GetBudgetUsers
( $budget->{budget_id
} );
1006 $budget->{budget_owner_id
}
1007 and $budget->{budget_owner_id
} !=
1008 $borrower->{borrowernumber
}
1009 or not $budget->{budget_owner_id
}
1011 and ( 0 == grep { $borrower->{borrowernumber
} == $_ }
1013 and defined $budget->{budget_branchcode
}
1014 and $budget->{budget_branchcode
} ne
1015 C4
::Context
->userenv->{branch
}
1022 # Budget restricted to owner and users
1023 elsif ( $budget->{budget_permission
} == 3 ) {
1024 my @budget_users = GetBudgetUsers
( $budget->{budget_id
} );
1027 $budget->{budget_owner_id
}
1028 and $budget->{budget_owner_id
} !=
1029 $borrower->{borrowernumber
}
1030 or not $budget->{budget_owner_id
}
1032 and ( 0 == grep { $borrower->{borrowernumber
} == $_ }
1044 sub CanUserModifyBudget
{
1045 my ($borrower, $budget, $userflags) = @_;
1047 if (not ref $borrower) {
1048 $borrower = Koha
::Patrons
->find( $borrower );
1049 return 0 unless $borrower;
1050 $borrower = $borrower->unblessed;
1052 if (not ref $budget) {
1053 $budget = GetBudget
($budget);
1056 return 0 unless ($borrower and $budget);
1058 if (not defined $userflags) {
1059 $userflags = C4
::Auth
::getuserflags
($borrower->{flags
},
1060 $borrower->{userid
});
1063 unless ($userflags->{superlibrarian
}
1064 || (ref $userflags->{acquisition
}
1065 && $userflags->{acquisition
}->{budget_manage_all
})
1066 || (!ref $userflags->{acquisition
} && $userflags->{acquisition
}))
1068 if (!CanUserUseBudget
($borrower, $budget, $userflags)) {
1072 if (ref $userflags->{acquisition
}
1073 && !$userflags->{acquisition
}->{budget_modify
}) {
1082 my ($value, $increment) = @_;
1084 if ($increment && $increment != 0) {
1085 $value = int($value / $increment) * $increment;
1091 =head2 CloneBudgetPeriod
1093 my $new_budget_period_id = CloneBudgetPeriod({
1094 budget_period_id => $budget_period_id,
1095 budget_period_startdate => $budget_period_startdate,
1096 budget_period_enddate => $budget_period_enddate,
1097 mark_original_budget_as_inactive => 1n
1098 reset_all_budgets => 1,
1101 Clone a budget period with all budgets.
1102 If the mark_origin_budget_as_inactive is set (0 by default),
1103 the original budget will be marked as inactive.
1105 If the reset_all_budgets is set (0 by default), all budget (fund)
1106 amounts will be reset.
1110 sub CloneBudgetPeriod
{
1112 my $budget_period_id = $params->{budget_period_id
};
1113 my $budget_period_startdate = $params->{budget_period_startdate
};
1114 my $budget_period_enddate = $params->{budget_period_enddate
};
1115 my $budget_period_description = $params->{budget_period_description
};
1116 my $amount_change_percentage = $params->{amount_change_percentage
};
1117 my $amount_change_round_increment = $params->{amount_change_round_increment
};
1118 my $mark_original_budget_as_inactive =
1119 $params->{mark_original_budget_as_inactive
} || 0;
1120 my $reset_all_budgets = $params->{reset_all_budgets
} || 0;
1122 my $budget_period = GetBudgetPeriod
($budget_period_id);
1124 $budget_period->{budget_period_startdate
} = $budget_period_startdate;
1125 $budget_period->{budget_period_enddate
} = $budget_period_enddate;
1126 $budget_period->{budget_period_description
} = $budget_period_description;
1127 # The new budget (budget_period) should be active by default
1128 $budget_period->{budget_period_active
} = 1;
1130 if ($amount_change_percentage) {
1131 my $total = $budget_period->{budget_period_total
};
1132 $total += $total * $amount_change_percentage / 100;
1133 $total = _round
($total, $amount_change_round_increment);
1134 $budget_period->{budget_period_total
} = $total;
1137 my $original_budget_period_id = $budget_period->{budget_period_id
};
1138 delete $budget_period->{budget_period_id
};
1139 my $new_budget_period_id = AddBudgetPeriod
( $budget_period );
1141 my $budgets = GetBudgetHierarchy
($budget_period_id);
1142 CloneBudgetHierarchy
(
1144 budgets
=> $budgets,
1145 new_budget_period_id
=> $new_budget_period_id
1149 if ($mark_original_budget_as_inactive) {
1152 budget_period_id
=> $budget_period_id,
1153 budget_period_active
=> 0,
1158 if ( $reset_all_budgets ) {
1159 my $budgets = GetBudgets
({ budget_period_id
=> $new_budget_period_id });
1160 for my $budget ( @
$budgets ) {
1161 $budget->{budget_amount
} = 0;
1162 ModBudget
( $budget );
1164 } elsif ($amount_change_percentage) {
1165 my $budgets = GetBudgets
({ budget_period_id
=> $new_budget_period_id });
1166 for my $budget ( @
$budgets ) {
1167 my $amount = $budget->{budget_amount
};
1168 $amount += $amount * $amount_change_percentage / 100;
1169 $amount = _round
($amount, $amount_change_round_increment);
1170 $budget->{budget_amount
} = $amount;
1171 ModBudget
( $budget );
1175 return $new_budget_period_id;
1178 =head2 CloneBudgetHierarchy
1180 CloneBudgetHierarchy({
1181 budgets => $budgets,
1182 new_budget_period_id => $new_budget_period_id;
1185 Clone a budget hierarchy.
1189 sub CloneBudgetHierarchy
{
1191 my $budgets = $params->{budgets
};
1192 my $new_budget_period_id = $params->{new_budget_period_id
};
1193 next unless @
$budgets or $new_budget_period_id;
1195 my $children_of = $params->{children_of
};
1196 my $new_parent_id = $params->{new_parent_id
};
1198 my @first_level_budgets =
1199 ( not defined $children_of )
1200 ?
map { ( not $_->{budget_parent_id
} ) ?
$_ : () } @
$budgets
1201 : map { ( $_->{budget_parent_id
} == $children_of ) ?
$_ : () } @
$budgets;
1203 # get only the columns of aqbudgets
1204 my @columns = Koha
::Database
->new()->schema->source('Aqbudget')->columns;
1206 for my $budget ( sort { $a->{budget_id
} <=> $b->{budget_id
} }
1207 @first_level_budgets )
1211 { map { join( ' ', @columns ) =~ /$_/ ?
( $_ => $budget->{$_} ) : () }
1213 my $new_budget_id = AddBudget
(
1217 budget_parent_id
=> $new_parent_id,
1218 budget_period_id
=> $new_budget_period_id
1221 CloneBudgetHierarchy
(
1223 budgets
=> $budgets,
1224 new_budget_period_id
=> $new_budget_period_id,
1225 children_of
=> $budget->{budget_id
},
1226 new_parent_id
=> $new_budget_id
1234 my $report = MoveOrders({
1235 from_budget_period_id => $from_budget_period_id,
1236 to_budget_period_id => $to_budget_period_id,
1239 Move orders from one budget period to another.
1245 my $from_budget_period_id = $params->{from_budget_period_id
};
1246 my $to_budget_period_id = $params->{to_budget_period_id
};
1247 my $move_remaining_unspent = $params->{move_remaining_unspent
};
1249 if not $from_budget_period_id
1250 or not $to_budget_period_id
1251 or $from_budget_period_id == $to_budget_period_id;
1253 # Can't move orders to an inactive budget (budgetperiod)
1254 my $budget_period = GetBudgetPeriod
($to_budget_period_id);
1255 return unless $budget_period->{budget_period_active
};
1258 my $dbh = C4
::Context
->dbh;
1259 my $sth_update_aqorders = $dbh->prepare(
1263 WHERE ordernumber
= ?
1266 my $sth_update_budget_amount = $dbh->prepare(
1269 SET budget_amount
= ?
1273 my $from_budgets = GetBudgetHierarchy
($from_budget_period_id);
1274 for my $from_budget (@
$from_budgets) {
1275 my $new_budget_id = $dbh->selectcol_arrayref(
1279 WHERE budget_period_id
= ?
1281 |, {}, $to_budget_period_id, $from_budget->{budget_code
}
1283 $new_budget_id = $new_budget_id->[0];
1284 my $new_budget = GetBudget
( $new_budget_id );
1285 unless ( $new_budget ) {
1289 budget
=> $from_budget,
1290 error
=> 'budget_code_not_exists',
1294 my $orders_to_move = C4
::Acquisition
::SearchOrders
(
1296 budget_id
=> $from_budget->{budget_id
},
1302 for my $order (@
$orders_to_move) {
1303 $sth_update_aqorders->execute( $new_budget->{budget_id
}, $order->{ordernumber
} );
1304 push @orders_moved, $order;
1307 my $unspent_moved = 0;
1308 if ($move_remaining_unspent) {
1309 my $spent = GetBudgetHierarchySpent
( $from_budget->{budget_id
} );
1310 my $unspent = $from_budget->{budget_amount
} - $spent;
1311 my $new_budget_amount = $new_budget->{budget_amount
};
1312 if ( $unspent > 0 ) {
1313 $new_budget_amount += $unspent;
1314 $unspent_moved = $unspent;
1316 $new_budget->{budget_amount
} = $new_budget_amount;
1317 $sth_update_budget_amount->execute( $new_budget_amount,
1318 $new_budget->{budget_id
} );
1323 budget
=> $new_budget,
1324 orders_moved
=> \
@orders_moved,
1326 unspent_moved
=> $unspent_moved,
1332 END { } # module clean-up code here (global destructor)
1339 Koha Development Team <http://koha-community.org/>