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 Koha
::Acquisition
::Invoice
::Adjustments
;
27 use vars
qw(@ISA @EXPORT);
35 &GetBudgetByOrderNumber
49 GetBudgetHierarchySpent
50 GetBudgetHierarchyOrdered
70 &CheckBudgetParentPerm
77 # ----------------------------BUDGETS.PM-----------------------------";
79 =head1 FUNCTIONS ABOUT BUDGETS
84 my ( $authcat, @hide_cols ) = @_;
85 my $dbh = C4
::Context
->dbh;
87 my $sth1 = $dbh->prepare(
89 UPDATE aqbudgets_planning SET display
= 0
93 foreach my $authvalue (@hide_cols) {
94 # $sth1->{TraceLevel} = 3;
95 $sth1->execute( $authcat, $authvalue );
100 my ( $authcat, $authvalue ) = @_;
102 my $dbh = C4
::Context
->dbh;
103 my $sth = $dbh->prepare(
105 SELECT count
(display
) as cnt from aqbudgets_planning
107 AND authvalue
= ?
and display
= 0 |
110 # $sth->{TraceLevel} = 3;
111 $sth->execute( $authcat, $authvalue );
112 my $res = $sth->fetchrow_hashref;
114 return $res->{cnt
} > 0 ?
0: 1
118 sub CheckBudgetParentPerm
{
119 my ( $budget, $borrower_id ) = @_;
120 my $depth = $budget->{depth
};
121 my $parent_id = $budget->{budget_parent_id
};
123 my $parent = GetBudget
($parent_id);
124 $parent_id = $parent->{budget_parent_id
};
125 if ( $parent->{budget_owner_id
} == $borrower_id ) {
133 sub AddBudgetPeriod
{
134 my ($budgetperiod) = @_;
135 return unless($budgetperiod->{budget_period_startdate
} && $budgetperiod->{budget_period_enddate
});
137 undef $budgetperiod->{budget_period_id
};
138 my $resultset = Koha
::Database
->new()->schema->resultset('Aqbudgetperiod');
139 return $resultset->create($budgetperiod)->id;
141 # -------------------------------------------------------------------
142 sub GetPeriodsCount
{
143 my $dbh = C4
::Context
->dbh;
144 my $sth = $dbh->prepare("
145 SELECT COUNT(*) AS sum FROM aqbudgetperiods ");
147 my $res = $sth->fetchrow_hashref;
148 return $res->{'sum'};
151 # -------------------------------------------------------------------
152 sub CheckBudgetParent
{
153 my ( $new_parent, $budget ) = @_;
154 my $new_parent_id = $new_parent->{'budget_id'};
155 my $budget_id = $budget->{'budget_id'};
156 my $dbh = C4
::Context
->dbh;
157 my $parent_id_tmp = $new_parent_id;
159 # check new-parent is not a child (or a child's child ;)
160 my $sth = $dbh->prepare(qq|
161 SELECT budget_parent_id FROM
162 aqbudgets where budget_id
= ?
| );
164 $sth->execute($parent_id_tmp);
165 my $res = $sth->fetchrow_hashref;
166 if ( $res->{'budget_parent_id'} == $budget_id ) {
169 if ( not defined $res->{'budget_parent_id'} ) {
172 $parent_id_tmp = $res->{'budget_parent_id'};
176 # -------------------------------------------------------------------
177 sub BudgetHasChildren
{
178 my ( $budget_id ) = @_;
179 my $dbh = C4
::Context
->dbh;
180 my $sth = $dbh->prepare(qq|
181 SELECT count
(*) as sum FROM aqbudgets
182 WHERE budget_parent_id
= ?
| );
183 $sth->execute( $budget_id );
184 my $sum = $sth->fetchrow_hashref;
185 return $sum->{'sum'};
188 sub GetBudgetChildren
{
189 my ( $budget_id ) = @_;
190 my $dbh = C4
::Context
->dbh;
191 return $dbh->selectall_arrayref(q
|
192 SELECT
* FROM aqbudgets
193 WHERE budget_parent_id
= ?
194 |, { Slice
=> {} }, $budget_id );
197 sub SetOwnerToFundHierarchy
{
198 my ( $budget_id, $borrowernumber ) = @_;
200 my $budget = GetBudget
( $budget_id );
201 $budget->{budget_owner_id
} = $borrowernumber;
202 ModBudget
( $budget );
203 my $children = GetBudgetChildren
( $budget_id );
204 for my $child ( @
$children ) {
205 SetOwnerToFundHierarchy
( $child->{budget_id
}, $borrowernumber );
209 # -------------------------------------------------------------------
210 sub GetBudgetsPlanCell
{
211 my ( $cell, $period, $budget ) = @_;
213 my $dbh = C4
::Context
->dbh;
214 if ( $cell->{'authcat'} eq 'MONTHS' ) {
215 # get the actual amount
216 $sth = $dbh->prepare( qq|
218 SELECT SUM
(ecost_tax_included
) AS actual FROM aqorders
219 WHERE budget_id
= ? AND
220 entrydate like
"$cell->{'authvalue'}%" |
222 $sth->execute( $cell->{'budget_id'} );
223 } elsif ( $cell->{'authcat'} eq 'BRANCHES' ) {
224 # get the actual amount
225 $sth = $dbh->prepare( qq|
227 SELECT SUM
(ecost_tax_included
) FROM aqorders
228 LEFT JOIN aqorders_items
229 ON
(aqorders
.ordernumber
= aqorders_items
.ordernumber
)
231 ON
(aqorders_items
.itemnumber
= items
.itemnumber
)
232 WHERE budget_id
= ? AND homebranch
= ?
| );
234 $sth->execute( $cell->{'budget_id'}, $cell->{'authvalue'} );
235 } elsif ( $cell->{'authcat'} eq 'ITEMTYPES' ) {
236 # get the actual amount
237 $sth = $dbh->prepare( qq|
239 SELECT SUM
( ecost_tax_included
* quantity
) AS actual
240 FROM aqorders JOIN biblioitems
241 ON
(biblioitems
.biblionumber
= aqorders
.biblionumber
)
242 WHERE aqorders
.budget_id
= ?
and itemtype
= ?
|
244 $sth->execute( $cell->{'budget_id'},
245 $cell->{'authvalue'} );
247 # ELSE GENERIC ORDERS SORT1/SORT2 STAT COUNT.
249 # get the actual amount
250 $sth = $dbh->prepare( qq|
252 SELECT SUM
(ecost_tax_included
* quantity
) AS actual
254 JOIN aqbudgets ON
(aqbudgets
.budget_id
= aqorders
.budget_id
)
255 WHERE aqorders
.budget_id
= ? AND
256 ((aqbudgets
.sort1_authcat
= ? AND sort1
=?
) OR
257 (aqbudgets
.sort2_authcat
= ? AND sort2
=?
)) |
259 $sth->execute( $cell->{'budget_id'},
260 $budget->{'sort1_authcat'},
261 $cell->{'authvalue'},
262 $budget->{'sort2_authcat'},
266 $actual = $sth->fetchrow_array;
268 # get the estimated amount
269 $sth = $dbh->prepare( qq|
271 SELECT estimated_amount AS estimated
, display FROM aqbudgets_planning
272 WHERE budget_period_id
= ? AND
277 $sth->execute( $cell->{'budget_period_id'},
278 $cell->{'budget_id'},
279 $cell->{'authvalue'},
284 my $res = $sth->fetchrow_hashref;
285 # my $display = $res->{'display'};
286 my $estimated = $res->{'estimated'};
289 return $actual, $estimated;
292 # -------------------------------------------------------------------
294 my ( $budget_plan, $budget_period_id, $authcat ) = @_;
295 my $dbh = C4
::Context
->dbh;
296 foreach my $buds (@
$budget_plan) {
297 my $lines = $buds->{lines
};
298 my $sth = $dbh->prepare( qq|
299 DELETE FROM aqbudgets_planning
300 WHERE budget_period_id
= ? AND
304 #delete a aqplan line of cells, then insert new cells,
305 # these could be UPDATES rather than DEL/INSERTS...
306 $sth->execute( $budget_period_id, $lines->[0]{budget_id
} , $authcat );
308 foreach my $cell (@
$lines) {
309 my $sth = $dbh->prepare( qq|
311 INSERT INTO aqbudgets_planning
313 budget_period_id
= ?
,
315 estimated_amount
= ?
,
319 $cell->{'budget_id'},
320 $cell->{'budget_period_id'},
322 $cell->{'estimated_amount'},
323 $cell->{'authvalue'},
329 # -------------------------------------------------------------------
331 my ($budget_id) = @_;
332 my $dbh = C4
::Context
->dbh;
333 # unitprice_tax_included should always been set here
334 # we should not need to retrieve ecost_tax_included
335 my $sth = $dbh->prepare(qq|
336 SELECT SUM
( COALESCE
(unitprice_tax_included
, ecost_tax_included
) * quantity
) AS sum FROM aqorders
337 WHERE budget_id
= ? AND
338 quantityreceived
> 0 AND
339 datecancellationprinted IS NULL
341 $sth->execute($budget_id);
342 my $sum = 0 + $sth->fetchrow_array;
344 $sth = $dbh->prepare(qq|
345 SELECT SUM
(shipmentcost
) AS sum
347 WHERE shipmentcost_budgetid
= ?
350 $sth->execute($budget_id);
351 my ($shipmentcost_sum) = $sth->fetchrow_array;
352 $sum += $shipmentcost_sum;
354 my $adjustments = Koha
::Acquisition
::Invoice
::Adjustments
->search({budget_id
=> $budget_id, closedate
=> { '!=' => undef } },{ join => 'invoiceid' });
355 while ( my $adj = $adjustments->next ){
356 $sum += $adj->adjustment;
362 # -------------------------------------------------------------------
363 sub GetBudgetOrdered
{
364 my ($budget_id) = @_;
365 my $dbh = C4
::Context
->dbh;
366 my $sth = $dbh->prepare(qq|
367 SELECT SUM
(ecost_tax_included
* quantity
) AS sum FROM aqorders
368 WHERE budget_id
= ? AND
369 quantityreceived
= 0 AND
370 datecancellationprinted IS NULL
372 $sth->execute($budget_id);
373 my $sum = 0 + $sth->fetchrow_array;
375 my $adjustments = Koha
::Acquisition
::Invoice
::Adjustments
->search({budget_id
=> $budget_id, encumber_open
=> 1, closedate
=> undef},{ join => 'invoiceid' });
376 while ( my $adj = $adjustments->next ){
377 $sum += $adj->adjustment;
385 my $budget_name = &GetBudgetName($budget_id);
387 get the budget_name for a given budget_id
392 my ( $budget_id ) = @_;
393 my $dbh = C4
::Context
->dbh;
394 my $sth = $dbh->prepare(
401 $sth->execute($budget_id);
402 return $sth->fetchrow_array;
405 =head2 GetBudgetAuthCats
407 my $auth_cats = &GetBudgetAuthCats($budget_period_id);
409 Return the list of authcat for a given budget_period_id
413 sub GetBudgetAuthCats
{
414 my ($budget_period_id) = shift;
415 # now, populate the auth_cats_loop used in the budget planning button
416 # we must retrieve all auth values used by at least one budget
417 my $dbh = C4
::Context
->dbh;
418 my $sth=$dbh->prepare("SELECT sort1_authcat,sort2_authcat FROM aqbudgets WHERE budget_period_id=?");
419 $sth->execute($budget_period_id);
421 while (my ($sort1_authcat,$sort2_authcat) = $sth->fetchrow) {
422 $authcats{$sort1_authcat}=1 if $sort1_authcat;
423 $authcats{$sort2_authcat}=1 if $sort2_authcat;
425 return [ sort keys %authcats ];
428 # -------------------------------------------------------------------
429 sub GetBudgetPeriods
{
430 my ($filters,$orderby) = @_;
432 my $rs = Koha
::Database
->new()->schema->resultset('Aqbudgetperiod');
433 $rs = $rs->search( $filters, { order_by
=> $orderby } );
434 $rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
437 # -------------------------------------------------------------------
438 sub GetBudgetPeriod
{
439 my ($budget_period_id) = @_;
440 my $dbh = C4
::Context
->dbh;
441 ## $total = number of records linked to the record that must be deleted
443 ## get information about the record that will be deleted
445 if ($budget_period_id) {
446 $sth = $dbh->prepare( qq|
449 WHERE budget_period_id
=?
|
451 $sth->execute($budget_period_id);
452 } else { # ACTIVE BUDGET
453 $sth = $dbh->prepare(qq|
456 WHERE budget_period_active
=1 |
460 my $data = $sth->fetchrow_hashref;
465 my ($budget_period_id) = @_;
466 my $dbh = C4
::Context
->dbh;
467 ; ## $total = number of records linked to the record that must be deleted
470 ## get information about the record that will be deleted
471 my $sth = $dbh->prepare(qq|
474 WHERE budget_period_id
=?
|
476 return $sth->execute($budget_period_id);
479 # -------------------------------------------------------------------
480 sub ModBudgetPeriod
{
481 my ($budget_period) = @_;
482 my $result = Koha
::Database
->new()->schema->resultset('Aqbudgetperiod')->find($budget_period);
483 return unless($result);
485 $result = $result->update($budget_period);
486 return $result->in_storage;
489 # -------------------------------------------------------------------
490 sub GetBudgetHierarchy
{
491 my ( $budget_period_id, $branchcode, $owner ) = @_;
493 my $dbh = C4
::Context
->dbh;
495 SELECT aqbudgets
.*, aqbudgetperiods
.budget_period_active
, aqbudgetperiods
.budget_period_description
,
496 b
.firstname as budget_owner_firstname
, b
.surname as budget_owner_surname
, b
.borrowernumber as budget_owner_borrowernumber
498 LEFT JOIN borrowers b on b
.borrowernumber
= aqbudgets
.budget_owner_id
499 JOIN aqbudgetperiods USING
(budget_period_id
)|;
502 # show only period X if requested
503 if ($budget_period_id) {
504 push @where_strings," aqbudgets.budget_period_id = ?";
505 push @bind_params, $budget_period_id;
507 # show only budgets owned by me, my branch or everyone
511 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
="")))};
512 push @bind_params, ( $owner, $branchcode );
514 push @where_strings, ' (budget_owner_id = ? OR budget_owner_id IS NULL or budget_owner_id ="") ';
515 push @bind_params, $owner;
519 push @where_strings," (budget_branchcode =? or budget_branchcode is NULL OR budget_branchcode='')";
520 push @bind_params, $branchcode;
523 $query.=" WHERE ".join(' AND ', @where_strings) if @where_strings;
524 $debug && warn $query,join(",",@bind_params);
525 my $sth = $dbh->prepare($query);
526 $sth->execute(@bind_params);
529 # create hash with budget_id has key
530 while ( my $data = $sth->fetchrow_hashref ) {
531 $links{ $data->{'budget_id'} } = $data;
534 # link child to parent
536 foreach my $budget ( sort { $a->{budget_code
} cmp $b->{budget_code
} } values %links ) {
537 my $child = $links{$budget->{budget_id
}};
538 if ( $child->{'budget_parent_id'} ) {
539 my $parent = $links{ $child->{'budget_parent_id'} };
541 unless ( $parent->{'children'} ) {
542 # init child arrayref
543 $parent->{'children'} = [];
546 push @
{ $parent->{'children'} }, $child;
549 push @first_parents, $child;
554 foreach my $first_parent (@first_parents) {
555 _add_budget_children
(\
@sort, $first_parent, 0);
558 # Get all the budgets totals in as few queries as possible
559 my $hr_budget_spent = $dbh->selectall_hashref(q
|
560 SELECT aqorders
.budget_id
, aqbudgets
.budget_parent_id
,
561 SUM
( COALESCE
(unitprice_tax_included
, ecost_tax_included
) * quantity
) AS budget_spent
562 FROM aqorders JOIN aqbudgets USING
(budget_id
)
563 WHERE quantityreceived
> 0 AND datecancellationprinted IS NULL
564 GROUP BY budget_id
, budget_parent_id
566 my $hr_budget_ordered = $dbh->selectall_hashref(q
|
567 SELECT aqorders
.budget_id
, aqbudgets
.budget_parent_id
,
568 SUM
(ecost_tax_included
* quantity
) AS budget_ordered
569 FROM aqorders JOIN aqbudgets USING
(budget_id
)
570 WHERE quantityreceived
= 0 AND datecancellationprinted IS NULL
571 GROUP BY budget_id
, budget_parent_id
573 my $hr_budget_spent_shipment = $dbh->selectall_hashref(q
|
574 SELECT shipmentcost_budgetid as budget_id
,
575 SUM
(shipmentcost
) as shipmentcost
577 WHERE closedate IS NOT NULL
578 GROUP BY shipmentcost_budgetid
580 my $hr_budget_ordered_shipment = $dbh->selectall_hashref(q
|
581 SELECT shipmentcost_budgetid as budget_id
,
582 SUM
(shipmentcost
) as shipmentcost
584 WHERE closedate IS NULL
585 GROUP BY shipmentcost_budgetid
589 foreach my $budget (@sort) {
590 if ( not defined $budget->{budget_parent_id
} ) {
591 _recursiveAdd
( $budget, undef, $hr_budget_spent, $hr_budget_spent_shipment, $hr_budget_ordered, $hr_budget_ordered_shipment );
598 my ($budget, $parent, $hr_budget_spent, $hr_budget_spent_shipment, $hr_budget_ordered, $hr_budget_ordered_shipment ) = @_;
600 foreach my $child (@
{$budget->{children
}}){
601 _recursiveAdd
($child, $budget, $hr_budget_spent, $hr_budget_spent_shipment, $hr_budget_ordered, $hr_budget_ordered_shipment );
604 $budget->{budget_spent
} += $hr_budget_spent->{$budget->{budget_id
}}->{budget_spent
};
605 $budget->{budget_spent
} += $hr_budget_spent_shipment->{$budget->{budget_id
}}->{shipmentcost
};
606 $budget->{budget_ordered
} += $hr_budget_ordered->{$budget->{budget_id
}}->{budget_ordered
};
607 $budget->{budget_ordered
} += $hr_budget_ordered_shipment->{$budget->{budget_id
}}->{shipmentcost
};
609 $budget->{total_spent
} += $budget->{budget_spent
};
610 $budget->{total_ordered
} += $budget->{budget_ordered
};
613 $parent->{total_spent
} += $budget->{total_spent
};
614 $parent->{total_ordered
} += $budget->{total_ordered
};
618 # Recursive method to add a budget and its chidren to an array
619 sub _add_budget_children
{
622 $budget->{budget_level
} = shift;
624 my $children = $budget->{'children'} || [];
625 return unless @
$children; # break recursivity
626 foreach my $child (@
$children) {
627 _add_budget_children
($res, $child, $budget->{budget_level
} + 1);
631 # -------------------------------------------------------------------
635 return unless ($budget);
637 undef $budget->{budget_encumb
} if $budget->{budget_encumb
} eq '';
638 undef $budget->{budget_owner_id
} if $budget->{budget_owner_id
} eq '';
639 my $resultset = Koha
::Database
->new()->schema->resultset('Aqbudget');
640 return $resultset->create($budget)->id;
643 # -------------------------------------------------------------------
646 my $result = Koha
::Database
->new()->schema->resultset('Aqbudget')->find($budget);
647 return unless($result);
649 undef $budget->{budget_encumb
} if $budget->{budget_encumb
} eq '';
650 undef $budget->{budget_owner_id
} if $budget->{budget_owner_id
} eq '';
651 $result = $result->update($budget);
652 return $result->in_storage;
655 # -------------------------------------------------------------------
657 my ($budget_id) = @_;
658 my $dbh = C4
::Context
->dbh;
659 my $sth = $dbh->prepare("delete from aqbudgets where budget_id=?");
660 my $rc = $sth->execute($budget_id);
665 # -------------------------------------------------------------------
669 &GetBudget($budget_id);
671 get a specific budget
676 my ( $budget_id ) = @_;
677 my $dbh = C4
::Context
->dbh;
683 my $sth = $dbh->prepare($query);
684 $sth->execute( $budget_id );
685 my $result = $sth->fetchrow_hashref;
689 # -------------------------------------------------------------------
691 =head2 GetBudgetByOrderNumber
693 &GetBudgetByOrderNumber($ordernumber);
695 get a specific budget by order number
699 sub GetBudgetByOrderNumber
{
700 my ( $ordernumber ) = @_;
701 my $dbh = C4
::Context
->dbh;
704 FROM aqbudgets, aqorders
706 AND aqorders.budget_id = aqbudgets.budget_id
708 my $sth = $dbh->prepare($query);
709 $sth->execute( $ordernumber );
710 my $result = $sth->fetchrow_hashref;
714 =head2 GetBudgetReport
716 &GetBudgetReport( [$budget_id] );
718 Get all orders for a specific budget, without cancelled orders.
720 Returns an array of hashrefs.
724 # --------------------------------------------------------------------
725 sub GetBudgetReport
{
726 my ( $budget_id ) = @_;
727 my $dbh = C4
::Context
->dbh;
729 SELECT o.*, b.budget_name
731 INNER JOIN aqorders o
732 ON b.budget_id = o.budget_id
734 AND (o.orderstatus != "cancelled")
735 ORDER BY b.budget_name';
737 my $sth = $dbh->prepare($query);
738 $sth->execute( $budget_id );
741 while ( my $data = $sth->fetchrow_hashref ) {
742 push( @results, $data );
747 =head2 GetBudgetsByActivity
749 &GetBudgetsByActivity( $budget_period_active );
751 Get all active or inactive budgets, depending of the value
759 # --------------------------------------------------------------------
760 sub GetBudgetsByActivity
{
761 my ( $budget_period_active ) = @_;
762 my $dbh = C4
::Context
->dbh;
765 FROM aqbudgetperiods bp
766 INNER JOIN aqbudgets b
767 ON bp.budget_period_id = b.budget_period_id
768 WHERE bp.budget_period_active=?
770 my $sth = $dbh->prepare($query);
771 $sth->execute( $budget_period_active );
773 while ( my $data = $sth->fetchrow_hashref ) {
774 push( @results, $data );
778 # --------------------------------------------------------------------
780 =head2 GetBudgetsReport
782 &GetBudgetsReport( [$activity] );
784 Get all but cancelled orders for all funds.
786 If the optionnal activity parameter is passed, returns orders for active/inactive budgets only.
791 Returns an array of hashrefs.
795 sub GetBudgetsReport
{
797 my $dbh = C4
::Context
->dbh;
799 SELECT o.*, b.budget_name
800 FROM aqbudgetperiods bp
801 INNER JOIN aqbudgets b
802 ON bp.budget_period_id = b.budget_period_id
803 INNER JOIN aqorders o
804 ON b.budget_id = o.budget_id ';
806 $query .= 'WHERE bp.budget_period_active=? ';
808 $query .= 'AND (o.orderstatus != "cancelled")
809 ORDER BY b.budget_name';
811 my $sth = $dbh->prepare($query);
813 $sth->execute($activity);
819 while ( my $data = $sth->fetchrow_hashref ) {
820 push( @results, $data );
825 =head2 GetBudgetByCode
827 my $budget = &GetBudgetByCode($budget_code);
829 Retrieve all aqbudgets fields as a hashref for the budget that has
834 sub GetBudgetByCode
{
835 my ( $budget_code ) = @_;
837 my $dbh = C4
::Context
->dbh;
841 JOIN aqbudgetperiods USING
(budget_period_id
)
842 WHERE budget_code
= ?
843 ORDER BY budget_period_active DESC
, budget_id DESC
846 my $sth = $dbh->prepare( $query );
847 $sth->execute( $budget_code );
848 return $sth->fetchrow_hashref;
851 =head2 GetBudgetHierarchySpent
853 my $spent = GetBudgetHierarchySpent( $budget_id );
855 Gets the total spent of the level and sublevels of $budget_id
859 sub GetBudgetHierarchySpent
{
860 my ( $budget_id ) = @_;
861 my $dbh = C4
::Context
->dbh;
862 my $children_ids = $dbh->selectcol_arrayref(q
|
865 WHERE budget_parent_id
= ?
868 my $total_spent = GetBudgetSpent
( $budget_id );
869 for my $child_id ( @
$children_ids ) {
870 $total_spent += GetBudgetHierarchySpent
( $child_id );
875 =head2 GetBudgetHierarchyOrdered
877 my $ordered = GetBudgetHierarchyOrdered( $budget_id );
879 Gets the total ordered of the level and sublevels of $budget_id
883 sub GetBudgetHierarchyOrdered
{
884 my ( $budget_id ) = @_;
885 my $dbh = C4
::Context
->dbh;
886 my $children_ids = $dbh->selectcol_arrayref(q
|
889 WHERE budget_parent_id
= ?
892 my $total_ordered = GetBudgetOrdered
( $budget_id );
893 for my $child_id ( @
$children_ids ) {
894 $total_ordered += GetBudgetHierarchyOrdered
( $child_id );
896 return $total_ordered;
901 &GetBudgets($filter, $order_by);
907 # -------------------------------------------------------------------
909 my ($filters, $orderby) = @_;
910 $orderby = 'budget_name' unless($orderby);
912 my $rs = Koha
::Database
->new()->schema->resultset('Aqbudget');
913 $rs = $rs->search( $filters, { order_by
=> $orderby } );
914 $rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
918 =head2 GetBudgetUsers
920 my @borrowernumbers = &GetBudgetUsers($budget_id);
922 Return the list of borrowernumbers linked to a budget
927 my ($budget_id) = @_;
929 my $dbh = C4
::Context
->dbh;
931 SELECT borrowernumber
932 FROM aqbudgetborrowers
935 my $sth = $dbh->prepare($query);
936 $sth->execute($budget_id);
939 while (my ($borrowernumber) = $sth->fetchrow_array) {
940 push @borrowernumbers, $borrowernumber
943 return @borrowernumbers;
946 =head2 ModBudgetUsers
948 &ModBudgetUsers($budget_id, @borrowernumbers);
950 Modify the list of borrowernumbers linked to a budget
955 my ($budget_id, @budget_users_id) = @_;
957 return unless $budget_id;
959 my $dbh = C4
::Context
->dbh;
960 my $query = "DELETE FROM aqbudgetborrowers WHERE budget_id = ?";
961 my $sth = $dbh->prepare($query);
962 $sth->execute($budget_id);
965 INSERT INTO aqbudgetborrowers
(budget_id
, borrowernumber
)
968 $sth = $dbh->prepare($query);
969 foreach my $borrowernumber (@budget_users_id) {
970 next unless $borrowernumber;
971 $sth->execute($budget_id, $borrowernumber);
975 sub CanUserUseBudget
{
976 my ($borrower, $budget, $userflags) = @_;
978 if (not ref $borrower) {
979 $borrower = Koha
::Patrons
->find( $borrower );
980 return 0 unless $borrower;
981 $borrower = $borrower->unblessed;
983 if (not ref $budget) {
984 $budget = GetBudget
($budget);
987 return 0 unless ($borrower and $budget);
989 if (not defined $userflags) {
990 $userflags = C4
::Auth
::getuserflags
($borrower->{flags
},
991 $borrower->{userid
});
994 unless ($userflags->{superlibrarian
}
995 || (ref $userflags->{acquisition
}
996 && $userflags->{acquisition
}->{budget_manage_all
})
997 || (!ref $userflags->{acquisition
} && $userflags->{acquisition
}))
999 if (not exists $userflags->{acquisition
}) {
1003 if (!ref $userflags->{acquisition
} && !$userflags->{acquisition
}) {
1007 # Budget restricted to owner
1008 if ( $budget->{budget_permission
} == 1 ) {
1009 if ( $budget->{budget_owner_id
}
1010 and $budget->{budget_owner_id
} != $borrower->{borrowernumber
} )
1016 # Budget restricted to owner, users and library
1017 elsif ( $budget->{budget_permission
} == 2 ) {
1018 my @budget_users = GetBudgetUsers
( $budget->{budget_id
} );
1022 $budget->{budget_owner_id
}
1023 and $budget->{budget_owner_id
} !=
1024 $borrower->{borrowernumber
}
1025 or not $budget->{budget_owner_id
}
1027 and ( 0 == grep { $borrower->{borrowernumber
} == $_ }
1029 and defined $budget->{budget_branchcode
}
1030 and $budget->{budget_branchcode
} ne
1031 C4
::Context
->userenv->{branch
}
1038 # Budget restricted to owner and users
1039 elsif ( $budget->{budget_permission
} == 3 ) {
1040 my @budget_users = GetBudgetUsers
( $budget->{budget_id
} );
1043 $budget->{budget_owner_id
}
1044 and $budget->{budget_owner_id
} !=
1045 $borrower->{borrowernumber
}
1046 or not $budget->{budget_owner_id
}
1048 and ( 0 == grep { $borrower->{borrowernumber
} == $_ }
1060 sub CanUserModifyBudget
{
1061 my ($borrower, $budget, $userflags) = @_;
1063 if (not ref $borrower) {
1064 $borrower = Koha
::Patrons
->find( $borrower );
1065 return 0 unless $borrower;
1066 $borrower = $borrower->unblessed;
1068 if (not ref $budget) {
1069 $budget = GetBudget
($budget);
1072 return 0 unless ($borrower and $budget);
1074 if (not defined $userflags) {
1075 $userflags = C4
::Auth
::getuserflags
($borrower->{flags
},
1076 $borrower->{userid
});
1079 unless ($userflags->{superlibrarian
}
1080 || (ref $userflags->{acquisition
}
1081 && $userflags->{acquisition
}->{budget_manage_all
})
1082 || (!ref $userflags->{acquisition
} && $userflags->{acquisition
}))
1084 if (!CanUserUseBudget
($borrower, $budget, $userflags)) {
1088 if (ref $userflags->{acquisition
}
1089 && !$userflags->{acquisition
}->{budget_modify
}) {
1098 my ($value, $increment) = @_;
1100 if ($increment && $increment != 0) {
1101 $value = int($value / $increment) * $increment;
1107 =head2 CloneBudgetPeriod
1109 my $new_budget_period_id = CloneBudgetPeriod({
1110 budget_period_id => $budget_period_id,
1111 budget_period_startdate => $budget_period_startdate,
1112 budget_period_enddate => $budget_period_enddate,
1113 mark_original_budget_as_inactive => 1n
1114 reset_all_budgets => 1,
1117 Clone a budget period with all budgets.
1118 If the mark_origin_budget_as_inactive is set (0 by default),
1119 the original budget will be marked as inactive.
1121 If the reset_all_budgets is set (0 by default), all budget (fund)
1122 amounts will be reset.
1126 sub CloneBudgetPeriod
{
1128 my $budget_period_id = $params->{budget_period_id
};
1129 my $budget_period_startdate = $params->{budget_period_startdate
};
1130 my $budget_period_enddate = $params->{budget_period_enddate
};
1131 my $budget_period_description = $params->{budget_period_description
};
1132 my $amount_change_percentage = $params->{amount_change_percentage
};
1133 my $amount_change_round_increment = $params->{amount_change_round_increment
};
1134 my $mark_original_budget_as_inactive =
1135 $params->{mark_original_budget_as_inactive
} || 0;
1136 my $reset_all_budgets = $params->{reset_all_budgets
} || 0;
1138 my $budget_period = GetBudgetPeriod
($budget_period_id);
1140 $budget_period->{budget_period_startdate
} = $budget_period_startdate;
1141 $budget_period->{budget_period_enddate
} = $budget_period_enddate;
1142 $budget_period->{budget_period_description
} = $budget_period_description;
1143 # The new budget (budget_period) should be active by default
1144 $budget_period->{budget_period_active
} = 1;
1146 if ($amount_change_percentage) {
1147 my $total = $budget_period->{budget_period_total
};
1148 $total += $total * $amount_change_percentage / 100;
1149 $total = _round
($total, $amount_change_round_increment);
1150 $budget_period->{budget_period_total
} = $total;
1153 my $original_budget_period_id = $budget_period->{budget_period_id
};
1154 delete $budget_period->{budget_period_id
};
1155 my $new_budget_period_id = AddBudgetPeriod
( $budget_period );
1157 my $budgets = GetBudgetHierarchy
($budget_period_id);
1158 CloneBudgetHierarchy
(
1160 budgets
=> $budgets,
1161 new_budget_period_id
=> $new_budget_period_id
1165 if ($mark_original_budget_as_inactive) {
1168 budget_period_id
=> $budget_period_id,
1169 budget_period_active
=> 0,
1174 if ( $reset_all_budgets ) {
1175 my $budgets = GetBudgets
({ budget_period_id
=> $new_budget_period_id });
1176 for my $budget ( @
$budgets ) {
1177 $budget->{budget_amount
} = 0;
1178 ModBudget
( $budget );
1180 } elsif ($amount_change_percentage) {
1181 my $budgets = GetBudgets
({ budget_period_id
=> $new_budget_period_id });
1182 for my $budget ( @
$budgets ) {
1183 my $amount = $budget->{budget_amount
};
1184 $amount += $amount * $amount_change_percentage / 100;
1185 $amount = _round
($amount, $amount_change_round_increment);
1186 $budget->{budget_amount
} = $amount;
1187 ModBudget
( $budget );
1191 return $new_budget_period_id;
1194 =head2 CloneBudgetHierarchy
1196 CloneBudgetHierarchy({
1197 budgets => $budgets,
1198 new_budget_period_id => $new_budget_period_id;
1201 Clone a budget hierarchy.
1205 sub CloneBudgetHierarchy
{
1207 my $budgets = $params->{budgets
};
1208 my $new_budget_period_id = $params->{new_budget_period_id
};
1209 next unless @
$budgets or $new_budget_period_id;
1211 my $children_of = $params->{children_of
};
1212 my $new_parent_id = $params->{new_parent_id
};
1214 my @first_level_budgets =
1215 ( not defined $children_of )
1216 ?
map { ( not $_->{budget_parent_id
} ) ?
$_ : () } @
$budgets
1217 : map { ( $_->{budget_parent_id
} == $children_of ) ?
$_ : () } @
$budgets;
1219 # get only the columns of aqbudgets
1220 my @columns = Koha
::Database
->new()->schema->source('Aqbudget')->columns;
1222 for my $budget ( sort { $a->{budget_id
} <=> $b->{budget_id
} }
1223 @first_level_budgets )
1227 { map { join( ' ', @columns ) =~ /$_/ ?
( $_ => $budget->{$_} ) : () }
1229 delete $tidy_budget->{timestamp
};
1230 my $new_budget_id = AddBudget
(
1234 budget_parent_id
=> $new_parent_id,
1235 budget_period_id
=> $new_budget_period_id
1238 CloneBudgetHierarchy
(
1240 budgets
=> $budgets,
1241 new_budget_period_id
=> $new_budget_period_id,
1242 children_of
=> $budget->{budget_id
},
1243 new_parent_id
=> $new_budget_id
1251 my $report = MoveOrders({
1252 from_budget_period_id => $from_budget_period_id,
1253 to_budget_period_id => $to_budget_period_id,
1256 Move orders from one budget period to another.
1262 my $from_budget_period_id = $params->{from_budget_period_id
};
1263 my $to_budget_period_id = $params->{to_budget_period_id
};
1264 my $move_remaining_unspent = $params->{move_remaining_unspent
};
1266 if not $from_budget_period_id
1267 or not $to_budget_period_id
1268 or $from_budget_period_id == $to_budget_period_id;
1270 # Can't move orders to an inactive budget (budgetperiod)
1271 my $budget_period = GetBudgetPeriod
($to_budget_period_id);
1272 return unless $budget_period->{budget_period_active
};
1275 my $dbh = C4
::Context
->dbh;
1276 my $sth_update_aqorders = $dbh->prepare(
1280 WHERE ordernumber
= ?
1283 my $sth_update_budget_amount = $dbh->prepare(
1286 SET budget_amount
= ?
1290 my $from_budgets = GetBudgetHierarchy
($from_budget_period_id);
1291 for my $from_budget (@
$from_budgets) {
1292 my $new_budget_id = $dbh->selectcol_arrayref(
1296 WHERE budget_period_id
= ?
1298 |, {}, $to_budget_period_id, $from_budget->{budget_code
}
1300 $new_budget_id = $new_budget_id->[0];
1301 my $new_budget = GetBudget
( $new_budget_id );
1302 unless ( $new_budget ) {
1306 budget
=> $from_budget,
1307 error
=> 'budget_code_not_exists',
1311 my $orders_to_move = C4
::Acquisition
::SearchOrders
(
1313 budget_id
=> $from_budget->{budget_id
},
1319 for my $order (@
$orders_to_move) {
1320 $sth_update_aqorders->execute( $new_budget->{budget_id
}, $order->{ordernumber
} );
1321 push @orders_moved, $order;
1324 my $unspent_moved = 0;
1325 if ($move_remaining_unspent) {
1326 my $spent = GetBudgetHierarchySpent
( $from_budget->{budget_id
} );
1327 my $unspent = $from_budget->{budget_amount
} - $spent;
1328 my $new_budget_amount = $new_budget->{budget_amount
};
1329 if ( $unspent > 0 ) {
1330 $new_budget_amount += $unspent;
1331 $unspent_moved = $unspent;
1333 $new_budget->{budget_amount
} = $new_budget_amount;
1334 $sth_update_budget_amount->execute( $new_budget_amount,
1335 $new_budget->{budget_id
} );
1340 budget
=> $new_budget,
1341 orders_moved
=> \
@orders_moved,
1343 unspent_moved
=> $unspent_moved,
1349 END { } # module clean-up code here (global destructor)
1356 Koha Development Team <http://koha-community.org/>