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 under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #use warnings; FIXME - Bug 2505
25 use vars
qw($VERSION @ISA @EXPORT);
28 # set the version for version checking
29 $VERSION = 3.07.00.049;
35 &GetBudgetByOrderNumber
46 GetBudgetHierarchySpent
47 GetBudgetHierarchyOrdered
72 &CheckBudgetParentPerm
79 # ----------------------------BUDGETS.PM-----------------------------";
82 =head1 FUNCTIONS ABOUT BUDGETS
87 my ( $authcat, @hide_cols ) = @_;
88 my $dbh = C4
::Context
->dbh;
90 my $sth1 = $dbh->prepare(
92 UPDATE aqbudgets_planning SET display
= 0
96 foreach my $authvalue (@hide_cols) {
97 # $sth1->{TraceLevel} = 3;
98 $sth1->execute( $authcat, $authvalue );
103 my ( $authcat, $authvalue ) = @_;
105 my $dbh = C4
::Context
->dbh;
106 my $sth = $dbh->prepare(
108 SELECT count
(display
) as cnt from aqbudgets_planning
110 AND authvalue
= ?
and display
= 0 |
113 # $sth->{TraceLevel} = 3;
114 $sth->execute( $authcat, $authvalue );
115 my $res = $sth->fetchrow_hashref;
117 return $res->{cnt
} > 0 ?
0: 1
121 sub CheckBudgetParentPerm
{
122 my ( $budget, $borrower_id ) = @_;
123 my $depth = $budget->{depth
};
124 my $parent_id = $budget->{budget_parent_id
};
126 my $parent = GetBudget
($parent_id);
127 $parent_id = $parent->{budget_parent_id
};
128 if ( $parent->{budget_owner_id
} == $borrower_id ) {
136 sub AddBudgetPeriod
{
137 my ($budgetperiod) = @_;
138 return unless($budgetperiod->{budget_period_startdate
} && $budgetperiod->{budget_period_enddate
});
140 my $resultset = Koha
::Database
->new()->schema->resultset('Aqbudgetperiod');
141 return $resultset->create($budgetperiod)->id;
143 # -------------------------------------------------------------------
144 sub GetPeriodsCount
{
145 my $dbh = C4
::Context
->dbh;
146 my $sth = $dbh->prepare("
147 SELECT COUNT(*) AS sum FROM aqbudgetperiods ");
149 my $res = $sth->fetchrow_hashref;
150 return $res->{'sum'};
153 # -------------------------------------------------------------------
154 sub CheckBudgetParent
{
155 my ( $new_parent, $budget ) = @_;
156 my $new_parent_id = $new_parent->{'budget_id'};
157 my $budget_id = $budget->{'budget_id'};
158 my $dbh = C4
::Context
->dbh;
159 my $parent_id_tmp = $new_parent_id;
161 # check new-parent is not a child (or a child's child ;)
162 my $sth = $dbh->prepare(qq|
163 SELECT budget_parent_id FROM
164 aqbudgets where budget_id
= ?
| );
166 $sth->execute($parent_id_tmp);
167 my $res = $sth->fetchrow_hashref;
168 if ( $res->{'budget_parent_id'} == $budget_id ) {
171 if ( not defined $res->{'budget_parent_id'} ) {
174 $parent_id_tmp = $res->{'budget_parent_id'};
178 # -------------------------------------------------------------------
179 sub BudgetHasChildren
{
180 my ( $budget_id ) = @_;
181 my $dbh = C4
::Context
->dbh;
182 my $sth = $dbh->prepare(qq|
183 SELECT count
(*) as sum FROM aqbudgets
184 WHERE budget_parent_id
= ?
| );
185 $sth->execute( $budget_id );
186 my $sum = $sth->fetchrow_hashref;
187 return $sum->{'sum'};
190 sub GetBudgetChildren
{
191 my ( $budget_id ) = @_;
192 my $dbh = C4
::Context
->dbh;
193 return $dbh->selectall_arrayref(q
|
194 SELECT
* FROM aqbudgets
195 WHERE budget_parent_id
= ?
196 |, { Slice
=> {} }, $budget_id );
199 sub SetOwnerToFundHierarchy
{
200 my ( $budget_id, $borrowernumber ) = @_;
202 my $budget = GetBudget
( $budget_id );
203 $budget->{budget_owner_id
} = $borrowernumber;
204 ModBudget
( $budget );
205 my $children = GetBudgetChildren
( $budget_id );
206 for my $child ( @
$children ) {
207 SetOwnerToFundHierarchy
( $child->{budget_id
}, $borrowernumber );
211 # -------------------------------------------------------------------
212 sub GetBudgetsPlanCell
{
213 my ( $cell, $period, $budget ) = @_;
215 my $dbh = C4
::Context
->dbh;
216 if ( $cell->{'authcat'} eq 'MONTHS' ) {
217 # get the actual amount
218 $sth = $dbh->prepare( qq|
220 SELECT SUM
(ecost
) AS actual FROM aqorders
221 WHERE budget_id
= ? AND
222 entrydate like
"$cell->{'authvalue'}%" |
224 $sth->execute( $cell->{'budget_id'} );
225 } elsif ( $cell->{'authcat'} eq 'BRANCHES' ) {
226 # get the actual amount
227 $sth = $dbh->prepare( qq|
229 SELECT SUM
(ecost
) FROM aqorders
230 LEFT JOIN aqorders_items
231 ON
(aqorders
.ordernumber
= aqorders_items
.ordernumber
)
233 ON
(aqorders_items
.itemnumber
= items
.itemnumber
)
234 WHERE budget_id
= ? AND homebranch
= ?
| );
236 $sth->execute( $cell->{'budget_id'}, $cell->{'authvalue'} );
237 } elsif ( $cell->{'authcat'} eq 'ITEMTYPES' ) {
238 # get the actual amount
239 $sth = $dbh->prepare( qq|
241 SELECT SUM
( ecost
* quantity
) AS actual
242 FROM aqorders JOIN biblioitems
243 ON
(biblioitems
.biblionumber
= aqorders
.biblionumber
)
244 WHERE aqorders
.budget_id
= ?
and itemtype
= ?
|
246 $sth->execute( $cell->{'budget_id'},
247 $cell->{'authvalue'} );
249 # ELSE GENERIC ORDERS SORT1/SORT2 STAT COUNT.
251 # get the actual amount
252 $sth = $dbh->prepare( qq|
254 SELECT SUM
(ecost
* quantity
) AS actual
256 JOIN aqbudgets ON
(aqbudgets
.budget_id
= aqorders
.budget_id
)
257 WHERE aqorders
.budget_id
= ? AND
258 ((aqbudgets
.sort1_authcat
= ? AND sort1
=?
) OR
259 (aqbudgets
.sort2_authcat
= ? AND sort2
=?
)) |
261 $sth->execute( $cell->{'budget_id'},
262 $budget->{'sort1_authcat'},
263 $cell->{'authvalue'},
264 $budget->{'sort2_authcat'},
268 $actual = $sth->fetchrow_array;
270 # get the estimated amount
271 $sth = $dbh->prepare( qq|
273 SELECT estimated_amount AS estimated
, display FROM aqbudgets_planning
274 WHERE budget_period_id
= ? AND
279 $sth->execute( $cell->{'budget_period_id'},
280 $cell->{'budget_id'},
281 $cell->{'authvalue'},
286 my $res = $sth->fetchrow_hashref;
287 # my $display = $res->{'display'};
288 my $estimated = $res->{'estimated'};
291 return $actual, $estimated;
294 # -------------------------------------------------------------------
296 my ( $budget_plan, $budget_period_id, $authcat ) = @_;
297 my $dbh = C4
::Context
->dbh;
298 foreach my $buds (@
$budget_plan) {
299 my $lines = $buds->{lines
};
300 my $sth = $dbh->prepare( qq|
301 DELETE FROM aqbudgets_planning
302 WHERE budget_period_id
= ? AND
306 #delete a aqplan line of cells, then insert new cells,
307 # these could be UPDATES rather than DEL/INSERTS...
308 $sth->execute( $budget_period_id, $lines->[0]{budget_id
} , $authcat );
310 foreach my $cell (@
$lines) {
311 my $sth = $dbh->prepare( qq|
313 INSERT INTO aqbudgets_planning
315 budget_period_id
= ?
,
317 estimated_amount
= ?
,
321 $cell->{'budget_id'},
322 $cell->{'budget_period_id'},
324 $cell->{'estimated_amount'},
325 $cell->{'authvalue'},
331 # -------------------------------------------------------------------
333 my ($budget_id) = @_;
334 my $dbh = C4
::Context
->dbh;
335 my $sth = $dbh->prepare(qq|
336 SELECT SUM
( COALESCE
(unitprice
, ecost
) * 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 = $sth->fetchrow_array;
344 $sth = $dbh->prepare(qq|
345 SELECT SUM
(shipmentcost
) AS sum
347 WHERE shipmentcost_budgetid
= ?
348 AND closedate IS NOT NULL
350 $sth->execute($budget_id);
351 my ($shipmentcost_sum) = $sth->fetchrow_array;
352 $sum += $shipmentcost_sum;
357 # -------------------------------------------------------------------
358 sub GetBudgetOrdered
{
359 my ($budget_id) = @_;
360 my $dbh = C4
::Context
->dbh;
361 my $sth = $dbh->prepare(qq|
362 SELECT SUM
(ecost
* quantity
) AS sum FROM aqorders
363 WHERE budget_id
= ? AND
364 quantityreceived
= 0 AND
365 datecancellationprinted IS NULL
367 $sth->execute($budget_id);
368 my $sum = $sth->fetchrow_array;
370 $sth = $dbh->prepare(qq|
371 SELECT SUM
(shipmentcost
) AS sum
373 WHERE shipmentcost_budgetid
= ?
374 AND closedate IS NULL
376 $sth->execute($budget_id);
377 my ($shipmentcost_sum) = $sth->fetchrow_array;
378 $sum += $shipmentcost_sum;
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 # -------------------------------------------------------------------
406 sub GetBudgetAuthCats
{
407 my ($budget_period_id) = shift;
408 # now, populate the auth_cats_loop used in the budget planning button
409 # we must retrieve all auth values used by at least one budget
410 my $dbh = C4
::Context
->dbh;
411 my $sth=$dbh->prepare("SELECT sort1_authcat,sort2_authcat FROM aqbudgets WHERE budget_period_id=?");
412 $sth->execute($budget_period_id);
414 while (my ($sort1_authcat,$sort2_authcat) = $sth->fetchrow) {
415 $authcats{$sort1_authcat}=1;
416 $authcats{$sort2_authcat}=1;
419 foreach (sort keys %authcats) {
420 push @auth_cats_loop,{ authcat
=> $_ };
422 return \
@auth_cats_loop;
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;
461 # -------------------------------------------------------------------
463 my ($budget_period_id) = @_;
464 my $dbh = C4
::Context
->dbh;
465 ; ## $total = number of records linked to the record that must be deleted
468 ## get information about the record that will be deleted
469 my $sth = $dbh->prepare(qq|
472 WHERE budget_period_id
=?
|
474 return $sth->execute($budget_period_id);
477 # -------------------------------------------------------------------
478 sub ModBudgetPeriod
{
479 my ($budget_period) = @_;
480 my $result = Koha
::Database
->new()->schema->resultset('Aqbudgetperiod')->find($budget_period);
481 return unless($result);
483 $result = $result->update($budget_period);
484 return $result->in_storage;
487 # -------------------------------------------------------------------
488 sub GetBudgetHierarchy
{
489 my ( $budget_period_id, $branchcode, $owner ) = @_;
491 my $dbh = C4
::Context
->dbh;
493 SELECT aqbudgets
.*, aqbudgetperiods
.budget_period_active
, aqbudgetperiods
.budget_period_description
495 JOIN aqbudgetperiods USING
(budget_period_id
)|;
498 # show only period X if requested
499 if ($budget_period_id) {
500 push @where_strings," aqbudgets.budget_period_id = ?";
501 push @bind_params, $budget_period_id;
503 # show only budgets owned by me, my branch or everyone
507 qq{ (budget_owner_id
= ? OR budget_branchcode
= ? OR
((budget_branchcode IS NULL
or budget_branchcode
="") AND
(budget_owner_id IS NULL OR budget_owner_id
="")))};
508 push @bind_params, ( $owner, $branchcode );
510 push @where_strings, ' (budget_owner_id = ? OR budget_owner_id IS NULL or budget_owner_id ="") ';
511 push @bind_params, $owner;
515 push @where_strings," (budget_branchcode =? or budget_branchcode is NULL)";
516 push @bind_params, $branchcode;
519 $query.=" WHERE ".join(' AND ', @where_strings) if @where_strings;
520 $debug && warn $query,join(",",@bind_params);
521 my $sth = $dbh->prepare($query);
522 $sth->execute(@bind_params);
525 # create hash with budget_id has key
526 while ( my $data = $sth->fetchrow_hashref ) {
527 $links{ $data->{'budget_id'} } = $data;
530 # link child to parent
532 foreach ( sort keys %links ) {
533 my $child = $links{$_};
534 if ( $child->{'budget_parent_id'} ) {
535 my $parent = $links{ $child->{'budget_parent_id'} };
537 unless ( $parent->{'children'} ) {
538 # init child arrayref
539 $parent->{'children'} = [];
542 push @
{ $parent->{'children'} }, $child;
545 push @first_parents, $child;
550 foreach my $first_parent (@first_parents) {
551 _add_budget_children
(\
@sort, $first_parent);
554 foreach my $budget (@sort) {
555 $budget->{budget_spent
} = GetBudgetSpent
( $budget->{budget_id
} );
556 $budget->{budget_ordered
} = GetBudgetOrdered
( $budget->{budget_id
} );
557 $budget->{total_spent
} = GetBudgetHierarchySpent
( $budget->{budget_id
} );
558 $budget->{total_ordered
} = GetBudgetHierarchyOrdered
( $budget->{budget_id
} );
563 # Recursive method to add a budget and its chidren to an array
564 sub _add_budget_children
{
568 my $children = $budget->{'children'} || [];
569 return unless @
$children; # break recursivity
570 foreach my $child (@
$children) {
571 _add_budget_children
($res, $child);
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);
607 &GetBudget($budget_id);
609 get a specific budget
613 # -------------------------------------------------------------------
615 my ( $budget_id ) = @_;
616 my $dbh = C4
::Context
->dbh;
622 my $sth = $dbh->prepare($query);
623 $sth->execute( $budget_id );
624 my $result = $sth->fetchrow_hashref;
628 =head2 GetBudgetByOrderNumber
630 &GetBudgetByOrderNumber($ordernumber);
632 get a specific budget by order number
636 # -------------------------------------------------------------------
637 sub GetBudgetByOrderNumber
{
638 my ( $ordernumber ) = @_;
639 my $dbh = C4
::Context
->dbh;
642 FROM aqbudgets, aqorders
644 AND aqorders.budget_id = aqbudgets.budget_id
646 my $sth = $dbh->prepare($query);
647 $sth->execute( $ordernumber );
648 my $result = $sth->fetchrow_hashref;
652 =head2 GetBudgetByCode
654 my $budget = &GetBudgetByCode($budget_code);
656 Retrieve all aqbudgets fields as a hashref for the budget that has
661 sub GetBudgetByCode
{
662 my ( $budget_code ) = @_;
664 my $dbh = C4
::Context
->dbh;
668 WHERE budget_code
= ?
669 ORDER BY budget_id DESC
672 my $sth = $dbh->prepare( $query );
673 $sth->execute( $budget_code );
674 return $sth->fetchrow_hashref;
677 =head2 GetBudgetHierarchySpent
679 my $spent = GetBudgetHierarchySpent( $budget_id );
681 Gets the total spent of the level and sublevels of $budget_id
685 sub GetBudgetHierarchySpent
{
686 my ( $budget_id ) = @_;
687 my $dbh = C4
::Context
->dbh;
688 my $children_ids = $dbh->selectcol_arrayref(q
|
691 WHERE budget_parent_id
= ?
694 my $total_spent = GetBudgetSpent
( $budget_id );
695 for my $child_id ( @
$children_ids ) {
696 $total_spent += GetBudgetHierarchySpent
( $child_id );
701 =head2 GetBudgetHierarchyOrdered
703 my $ordered = GetBudgetHierarchyOrdered( $budget_id );
705 Gets the total ordered of the level and sublevels of $budget_id
709 sub GetBudgetHierarchyOrdered
{
710 my ( $budget_id ) = @_;
711 my $dbh = C4
::Context
->dbh;
712 my $children_ids = $dbh->selectcol_arrayref(q
|
715 WHERE budget_parent_id
= ?
718 my $total_ordered = GetBudgetOrdered
( $budget_id );
719 for my $child_id ( @
$children_ids ) {
720 $total_ordered += GetBudgetHierarchyOrdered
( $child_id );
722 return $total_ordered;
727 &GetBudgets($filter, $order_by);
733 # -------------------------------------------------------------------
735 my ($filters, $orderby) = @_;
736 $orderby = 'budget_name' unless($orderby);
738 my $rs = Koha
::Database
->new()->schema->resultset('Aqbudget');
739 $rs = $rs->search( $filters, { order_by
=> $orderby } );
740 $rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
744 =head2 GetBudgetUsers
746 my @borrowernumbers = &GetBudgetUsers($budget_id);
748 Return the list of borrowernumbers linked to a budget
753 my ($budget_id) = @_;
755 my $dbh = C4
::Context
->dbh;
757 SELECT borrowernumber
758 FROM aqbudgetborrowers
761 my $sth = $dbh->prepare($query);
762 $sth->execute($budget_id);
765 while (my ($borrowernumber) = $sth->fetchrow_array) {
766 push @borrowernumbers, $borrowernumber
769 return @borrowernumbers;
772 =head2 ModBudgetUsers
774 &ModBudgetUsers($budget_id, @borrowernumbers);
776 Modify the list of borrowernumbers linked to a budget
781 my ($budget_id, @budget_users_id) = @_;
783 return unless $budget_id;
785 my $dbh = C4
::Context
->dbh;
786 my $query = "DELETE FROM aqbudgetborrowers WHERE budget_id = ?";
787 my $sth = $dbh->prepare($query);
788 $sth->execute($budget_id);
791 INSERT INTO aqbudgetborrowers
(budget_id
, borrowernumber
)
794 $sth = $dbh->prepare($query);
795 foreach my $borrowernumber (@budget_users_id) {
796 next unless $borrowernumber;
797 $sth->execute($budget_id, $borrowernumber);
801 sub CanUserUseBudget
{
802 my ($borrower, $budget, $userflags) = @_;
804 if (not ref $borrower) {
805 $borrower = C4
::Members
::GetMember
(borrowernumber
=> $borrower);
807 if (not ref $budget) {
808 $budget = GetBudget
($budget);
811 return 0 unless ($borrower and $budget);
813 if (not defined $userflags) {
814 $userflags = C4
::Auth
::getuserflags
($borrower->{flags
},
815 $borrower->{userid
});
818 unless ($userflags->{superlibrarian
}
819 || (ref $userflags->{acquisition
}
820 && $userflags->{acquisition
}->{budget_manage_all
})
821 || (!ref $userflags->{acquisition
} && $userflags->{acquisition
}))
823 if (not exists $userflags->{acquisition
}) {
827 if (!ref $userflags->{acquisition
} && !$userflags->{acquisition
}) {
831 # Budget restricted to owner
832 if ( $budget->{budget_permission
} == 1 ) {
833 if ( $budget->{budget_owner_id
}
834 and $budget->{budget_owner_id
} != $borrower->{borrowernumber
} )
840 # Budget restricted to owner, users and library
841 elsif ( $budget->{budget_permission
} == 2 ) {
842 my @budget_users = GetBudgetUsers
( $budget->{budget_id
} );
846 $budget->{budget_owner_id
}
847 and $budget->{budget_owner_id
} !=
848 $borrower->{borrowernumber
}
849 or not $budget->{budget_owner_id
}
851 and ( 0 == grep { $borrower->{borrowernumber
} == $_ }
853 and defined $budget->{budget_branchcode
}
854 and $budget->{budget_branchcode
} ne
855 C4
::Context
->userenv->{branch
}
862 # Budget restricted to owner and users
863 elsif ( $budget->{budget_permission
} == 3 ) {
864 my @budget_users = GetBudgetUsers
( $budget->{budget_id
} );
867 $budget->{budget_owner_id
}
868 and $budget->{budget_owner_id
} !=
869 $borrower->{borrowernumber
}
870 or not $budget->{budget_owner_id
}
872 and ( 0 == grep { $borrower->{borrowernumber
} == $_ }
884 sub CanUserModifyBudget
{
885 my ($borrower, $budget, $userflags) = @_;
887 if (not ref $borrower) {
888 $borrower = C4
::Members
::GetMember
(borrowernumber
=> $borrower);
890 if (not ref $budget) {
891 $budget = GetBudget
($budget);
894 return 0 unless ($borrower and $budget);
896 if (not defined $userflags) {
897 $userflags = C4
::Auth
::getuserflags
($borrower->{flags
},
898 $borrower->{userid
});
901 unless ($userflags->{superlibrarian
}
902 || (ref $userflags->{acquisition
}
903 && $userflags->{acquisition
}->{budget_manage_all
})
904 || (!ref $userflags->{acquisition
} && $userflags->{acquisition
}))
906 if (!CanUserUseBudget
($borrower, $budget, $userflags)) {
910 if (ref $userflags->{acquisition
}
911 && !$userflags->{acquisition
}->{budget_modify
}) {
919 # -------------------------------------------------------------------
923 @currencies = &GetCurrencies;
925 Returns the list of all known currencies.
927 C<$currencies> is a array; its elements are references-to-hash, whose
928 keys are the fields from the currency table in the Koha database.
933 my $dbh = C4
::Context
->dbh;
938 my $sth = $dbh->prepare($query);
941 while ( my $data = $sth->fetchrow_hashref ) {
942 push( @results, $data );
947 # -------------------------------------------------------------------
950 my $dbh = C4
::Context
->dbh;
952 SELECT * FROM currency where active = '1' ";
953 my $sth = $dbh->prepare($query);
955 my $r = $sth->fetchrow_hashref;
961 &ModCurrencies($currency, $newrate);
963 Sets the exchange rate for C<$currency> to be C<$newrate>.
968 my ( $currency, $rate ) = @_;
969 my $dbh = C4
::Context
->dbh;
974 my $sth = $dbh->prepare($query);
975 $sth->execute( $rate, $currency );
978 # -------------------------------------------------------------------
980 =head2 ConvertCurrency
982 $foreignprice = &ConvertCurrency($currency, $localprice);
984 Converts the price C<$localprice> to foreign currency C<$currency> by
985 dividing by the exchange rate, and returns the result.
987 If no exchange rate is found, e is one to one.
991 sub ConvertCurrency
{
992 my ( $currency, $price ) = @_;
993 my $dbh = C4
::Context
->dbh;
999 my $sth = $dbh->prepare($query);
1000 $sth->execute($currency);
1001 my $cur = ( $sth->fetchrow_array() )[0];
1005 return ( $price / $cur );
1009 =head2 CloneBudgetPeriod
1011 my $new_budget_period_id = CloneBudgetPeriod({
1012 budget_period_id => $budget_period_id,
1013 budget_period_startdate => $budget_period_startdate,
1014 budget_period_enddate => $budget_period_enddate,
1015 mark_original_budget_as_inactive => 1n
1016 reset_all_budgets => 1,
1019 Clone a budget period with all budgets.
1020 If the mark_origin_budget_as_inactive is set (0 by default),
1021 the original budget will be marked as inactive.
1023 If the reset_all_budgets is set (0 by default), all budget (fund)
1024 amounts will be reset.
1028 sub CloneBudgetPeriod
{
1030 my $budget_period_id = $params->{budget_period_id
};
1031 my $budget_period_startdate = $params->{budget_period_startdate
};
1032 my $budget_period_enddate = $params->{budget_period_enddate
};
1033 my $budget_period_description = $params->{budget_period_description
};
1034 my $mark_original_budget_as_inactive =
1035 $params->{mark_original_budget_as_inactive
} || 0;
1036 my $reset_all_budgets = $params->{reset_all_budgets
} || 0;
1038 my $budget_period = GetBudgetPeriod
($budget_period_id);
1040 $budget_period->{budget_period_startdate
} = $budget_period_startdate;
1041 $budget_period->{budget_period_enddate
} = $budget_period_enddate;
1042 $budget_period->{budget_period_description
} = $budget_period_description;
1043 # The new budget (budget_period) should be active by default
1044 $budget_period->{budget_period_active
} = 1;
1045 my $original_budget_period_id = $budget_period->{budget_period_id
};
1046 delete $budget_period->{budget_period_id
};
1047 my $new_budget_period_id = AddBudgetPeriod
( $budget_period );
1049 my $budgets = GetBudgetHierarchy
($budget_period_id);
1050 CloneBudgetHierarchy
(
1052 budgets
=> $budgets,
1053 new_budget_period_id
=> $new_budget_period_id
1057 if ($mark_original_budget_as_inactive) {
1060 budget_period_id
=> $budget_period_id,
1061 budget_period_active
=> 0,
1066 if ( $reset_all_budgets ) {
1067 my $budgets = GetBudgets
({ budget_period_id
=> $new_budget_period_id });
1068 for my $budget ( @
$budgets ) {
1069 $budget->{budget_amount
} = 0;
1070 ModBudget
( $budget );
1074 return $new_budget_period_id;
1077 =head2 CloneBudgetHierarchy
1079 CloneBudgetHierarchy({
1080 budgets => $budgets,
1081 new_budget_period_id => $new_budget_period_id;
1084 Clone a budget hierarchy.
1088 sub CloneBudgetHierarchy
{
1090 my $budgets = $params->{budgets
};
1091 my $new_budget_period_id = $params->{new_budget_period_id
};
1092 next unless @
$budgets or $new_budget_period_id;
1094 my $children_of = $params->{children_of
};
1095 my $new_parent_id = $params->{new_parent_id
};
1097 my @first_level_budgets =
1098 ( not defined $children_of )
1099 ?
map { ( not $_->{budget_parent_id
} ) ?
$_ : () } @
$budgets
1100 : map { ( $_->{budget_parent_id
} == $children_of ) ?
$_ : () } @
$budgets;
1102 # get only the columns of aqbudgets
1103 my @columns = Koha
::Database
->new()->schema->source('Aqbudget')->columns;
1105 for my $budget ( sort { $a->{budget_id
} <=> $b->{budget_id
} }
1106 @first_level_budgets )
1110 { map { join( ' ', @columns ) =~ /$_/ ?
( $_ => $budget->{$_} ) : () }
1112 my $new_budget_id = AddBudget
(
1116 budget_parent_id
=> $new_parent_id,
1117 budget_period_id
=> $new_budget_period_id
1120 CloneBudgetHierarchy
(
1122 budgets
=> $budgets,
1123 new_budget_period_id
=> $new_budget_period_id,
1124 children_of
=> $budget->{budget_id
},
1125 new_parent_id
=> $new_budget_id
1133 my $report = MoveOrders({
1134 from_budget_period_id => $from_budget_period_id,
1135 to_budget_period_id => $to_budget_period_id,
1138 Move orders from one budget period to another.
1144 my $from_budget_period_id = $params->{from_budget_period_id
};
1145 my $to_budget_period_id = $params->{to_budget_period_id
};
1146 my $move_remaining_unspent = $params->{move_remaining_unspent
};
1148 if not $from_budget_period_id
1149 or not $to_budget_period_id
1150 or $from_budget_period_id == $to_budget_period_id;
1152 # Can't move orders to an inactive budget (budgetperiod)
1153 my $budget_period = GetBudgetPeriod
($to_budget_period_id);
1154 return unless $budget_period->{budget_period_active
};
1157 my $dbh = C4
::Context
->dbh;
1158 my $sth_update_aqorders = $dbh->prepare(
1162 WHERE ordernumber
= ?
1165 my $sth_update_budget_amount = $dbh->prepare(
1168 SET budget_amount
= ?
1172 my $from_budgets = GetBudgetHierarchy
($from_budget_period_id);
1173 for my $from_budget (@
$from_budgets) {
1174 my $new_budget_id = $dbh->selectcol_arrayref(
1178 WHERE budget_period_id
= ?
1180 |, {}, $to_budget_period_id, $from_budget->{budget_code
}
1182 $new_budget_id = $new_budget_id->[0];
1183 my $new_budget = GetBudget
( $new_budget_id );
1184 unless ( $new_budget ) {
1188 budget
=> $from_budget,
1189 error
=> 'budget_code_not_exists',
1193 my $orders_to_move = C4
::Acquisition
::SearchOrders
(
1195 budget_id
=> $from_budget->{budget_id
},
1201 for my $order (@
$orders_to_move) {
1202 $sth_update_aqorders->execute( $new_budget->{budget_id
}, $order->{ordernumber
} );
1203 push @orders_moved, $order;
1206 my $unspent_moved = 0;
1207 if ($move_remaining_unspent) {
1208 my $spent = GetBudgetHierarchySpent
( $from_budget->{budget_id
} );
1209 my $unspent = $from_budget->{budget_amount
} - $spent;
1210 my $new_budget_amount = $new_budget->{budget_amount
};
1211 if ( $unspent > 0 ) {
1212 $new_budget_amount += $unspent;
1213 $unspent_moved = $unspent;
1215 $new_budget->{budget_amount
} = $new_budget_amount;
1216 $sth_update_budget_amount->execute( $new_budget_amount,
1217 $new_budget->{budget_id
} );
1222 budget
=> $new_budget,
1223 orders_moved
=> \
@orders_moved,
1225 unspent_moved
=> $unspent_moved,
1231 END { } # module clean-up code here (global destructor)
1238 Koha Development Team <http://koha-community.org/>