-
Notifications
You must be signed in to change notification settings - Fork 257
/
Budgets.pm
1445 lines (1206 loc) · 43.7 KB
/
Budgets.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package C4::Budgets;
# Copyright 2000-2002 Katipo Communications
#
# This file is part of Koha.
#
# Koha is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# Koha is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Koha; if not, see <http://www.gnu.org/licenses>.
use Modern::Perl;
use JSON;
use C4::Context;
use Koha::Database;
use Koha::Patrons;
use Koha::Acquisition::Invoice::Adjustments;
use C4::Acquisition;
use C4::Log qw(logaction);
our (@ISA, @EXPORT_OK);
BEGIN {
require Exporter;
@ISA = qw(Exporter);
@EXPORT_OK = qw(
GetBudget
GetBudgetByOrderNumber
GetBudgetByCode
GetBudgets
BudgetsByActivity
GetBudgetsReport
GetBudgetReport
GetBudgetsByActivity
GetBudgetHierarchy
AddBudget
ModBudget
DelBudget
GetBudgetSpent
GetBudgetOrdered
GetBudgetName
GetPeriodsCount
GetBudgetHierarchySpent
GetBudgetHierarchyOrdered
GetBudgetUsers
ModBudgetUsers
CanUserUseBudget
CanUserModifyBudget
GetBudgetPeriod
GetBudgetPeriods
ModBudgetPeriod
AddBudgetPeriod
DelBudgetPeriod
ModBudgetPlan
GetBudgetsPlanCell
AddBudgetPlanValue
GetBudgetAuthCats
BudgetHasChildren
GetBudgetChildren
SetOwnerToFundHierarchy
CheckBudgetParent
CheckBudgetParentPerm
HideCols
GetCols
CloneBudgetPeriod
CloneBudgetHierarchy
MoveOrders
FieldsForCalculatingFundValues
);
}
# ----------------------------BUDGETS.PM-----------------------------";
=head1 FUNCTIONS ABOUT BUDGETS
=cut
sub HideCols {
my ( $authcat, @hide_cols ) = @_;
my $dbh = C4::Context->dbh;
my $sth1 = $dbh->prepare(
qq|
UPDATE aqbudgets_planning SET display = 0
WHERE authcat = ?
AND authvalue = ? |
);
foreach my $authvalue (@hide_cols) {
# $sth1->{TraceLevel} = 3;
$sth1->execute( $authcat, $authvalue );
}
}
sub GetCols {
my ( $authcat, $authvalue ) = @_;
my $dbh = C4::Context->dbh;
my $sth = $dbh->prepare(
qq|
SELECT count(display) as cnt from aqbudgets_planning
WHERE authcat = ?
AND authvalue = ? and display = 0 |
);
# $sth->{TraceLevel} = 3;
$sth->execute( $authcat, $authvalue );
my $res = $sth->fetchrow_hashref;
return $res->{cnt} > 0 ? 0: 1
}
sub CheckBudgetParentPerm {
my ( $budget, $borrower_id ) = @_;
my $depth = $budget->{depth};
my $parent_id = $budget->{budget_parent_id};
while ($depth) {
my $parent = GetBudget($parent_id);
$parent_id = $parent->{budget_parent_id};
if ( $parent->{budget_owner_id} == $borrower_id ) {
return 1;
}
$depth--
}
return 0;
}
sub AddBudgetPeriod {
my ($budgetperiod) = @_;
return unless($budgetperiod->{budget_period_startdate} && $budgetperiod->{budget_period_enddate});
undef $budgetperiod->{budget_period_id};
my $resultset = Koha::Database->new()->schema->resultset('Aqbudgetperiod');
return $resultset->create($budgetperiod)->id;
}
# -------------------------------------------------------------------
sub GetPeriodsCount {
my $dbh = C4::Context->dbh;
my $sth = $dbh->prepare("
SELECT COUNT(*) AS sum FROM aqbudgetperiods ");
$sth->execute();
my $res = $sth->fetchrow_hashref;
return $res->{'sum'};
}
# -------------------------------------------------------------------
sub CheckBudgetParent {
my ( $new_parent, $budget ) = @_;
my $new_parent_id = $new_parent->{'budget_id'};
my $budget_id = $budget->{'budget_id'};
my $dbh = C4::Context->dbh;
my $parent_id_tmp = $new_parent_id;
# check new-parent is not a child (or a child's child ;)
my $sth = $dbh->prepare(qq|
SELECT budget_parent_id FROM
aqbudgets where budget_id = ? | );
while (1) {
$sth->execute($parent_id_tmp);
my $res = $sth->fetchrow_hashref;
if ( $res->{'budget_parent_id'} == $budget_id ) {
return 1;
}
if ( not defined $res->{'budget_parent_id'} ) {
return 0;
}
$parent_id_tmp = $res->{'budget_parent_id'};
}
}
# -------------------------------------------------------------------
sub BudgetHasChildren {
my ( $budget_id ) = @_;
my $dbh = C4::Context->dbh;
my $sth = $dbh->prepare(qq|
SELECT count(*) as sum FROM aqbudgets
WHERE budget_parent_id = ? | );
$sth->execute( $budget_id );
my $sum = $sth->fetchrow_hashref;
return $sum->{'sum'};
}
sub GetBudgetChildren {
my ( $budget_id ) = @_;
my $dbh = C4::Context->dbh;
return $dbh->selectall_arrayref(q|
SELECT * FROM aqbudgets
WHERE budget_parent_id = ?
|, { Slice => {} }, $budget_id );
}
sub SetOwnerToFundHierarchy {
my ( $budget_id, $borrowernumber ) = @_;
my $budget = GetBudget( $budget_id );
$budget->{budget_owner_id} = $borrowernumber;
ModBudget( $budget );
my $children = GetBudgetChildren( $budget_id );
for my $child ( @$children ) {
SetOwnerToFundHierarchy( $child->{budget_id}, $borrowernumber );
}
}
# -------------------------------------------------------------------
sub GetBudgetsPlanCell {
my ( $cell, $period, $budget ) = @_; #FIXME we don't use $period
my ($actual, $sth);
my $dbh = C4::Context->dbh;
my $roundsql = C4::Acquisition::get_rounding_sql(qq|ecost_tax_included|);
if ( $cell->{'authcat'} eq 'MONTHS' ) {
# get the actual amount
# FIXME we should consider quantity
$sth = $dbh->prepare( qq|
SELECT SUM(| . $roundsql . qq|) AS actual FROM aqorders
WHERE budget_id = ? AND
entrydate like "$cell->{'authvalue'}%" |
);
$sth->execute( $cell->{'budget_id'} );
} elsif ( $cell->{'authcat'} eq 'BRANCHES' ) {
# get the actual amount
# FIXME we should consider quantity
$sth = $dbh->prepare( qq|
SELECT SUM(| . $roundsql . qq|) FROM aqorders
LEFT JOIN aqorders_items
ON (aqorders.ordernumber = aqorders_items.ordernumber)
LEFT JOIN items
ON (aqorders_items.itemnumber = items.itemnumber)
WHERE budget_id = ? AND homebranch = ? | );
$sth->execute( $cell->{'budget_id'}, $cell->{'authvalue'} );
} elsif ( $cell->{'authcat'} eq 'ITEMTYPES' ) {
# get the actual amount
$sth = $dbh->prepare( qq|
SELECT SUM( | . $roundsql . qq| * quantity) AS actual
FROM aqorders JOIN biblioitems
ON (biblioitems.biblionumber = aqorders.biblionumber )
WHERE aqorders.budget_id = ? and itemtype = ? |
);
$sth->execute( $cell->{'budget_id'},
$cell->{'authvalue'} );
}
# ELSE GENERIC ORDERS SORT1/SORT2 STAT COUNT.
else {
# get the actual amount
$sth = $dbh->prepare( qq|
SELECT SUM(| . $roundsql . qq| * quantity) AS actual
FROM aqorders
JOIN aqbudgets ON (aqbudgets.budget_id = aqorders.budget_id )
WHERE aqorders.budget_id = ? AND
((aqbudgets.sort1_authcat = ? AND sort1 =?) OR
(aqbudgets.sort2_authcat = ? AND sort2 =?)) |
);
$sth->execute( $cell->{'budget_id'},
$budget->{'sort1_authcat'},
$cell->{'authvalue'},
$budget->{'sort2_authcat'},
$cell->{'authvalue'}
);
}
$actual = $sth->fetchrow_array;
# get the estimated amount
$sth = $dbh->prepare( qq|
SELECT estimated_amount AS estimated, display FROM aqbudgets_planning
WHERE budget_period_id = ? AND
budget_id = ? AND
authvalue = ? AND
authcat = ? |
);
$sth->execute( $cell->{'budget_period_id'},
$cell->{'budget_id'},
$cell->{'authvalue'},
$cell->{'authcat'},
);
my $res = $sth->fetchrow_hashref;
# my $display = $res->{'display'};
my $estimated = $res->{'estimated'};
return $actual, $estimated;
}
# -------------------------------------------------------------------
sub ModBudgetPlan {
my ( $budget_plan, $budget_period_id, $authcat ) = @_;
my $dbh = C4::Context->dbh;
foreach my $buds (@$budget_plan) {
my $lines = $buds->{lines};
my $sth = $dbh->prepare( qq|
DELETE FROM aqbudgets_planning
WHERE budget_period_id = ? AND
budget_id = ? AND
authcat = ? |
);
#delete a aqplan line of cells, then insert new cells,
# these could be UPDATES rather than DEL/INSERTS...
$sth->execute( $budget_period_id, $lines->[0]{budget_id} , $authcat );
foreach my $cell (@$lines) {
my $sth = $dbh->prepare( qq|
INSERT INTO aqbudgets_planning
SET budget_id = ?,
budget_period_id = ?,
authcat = ?,
estimated_amount = ?,
authvalue = ? |
);
$sth->execute(
$cell->{'budget_id'},
$cell->{'budget_period_id'},
$cell->{'authcat'},
$cell->{'estimated_amount'},
$cell->{'authvalue'},
);
}
}
}
# -------------------------------------------------------------------
sub GetBudgetSpent {
my ($budget_id) = @_;
my $dbh = C4::Context->dbh;
# Choose correct unitprice and ecost fields
my ( $unitprice_field, $ecost_field ) = FieldsForCalculatingFundValues();
my $sth = $dbh->prepare(qq|
SELECT SUM( | . C4::Acquisition::get_rounding_sql("COALESCE($unitprice_field, $ecost_field)") . qq| * quantity ) AS sum FROM aqorders
WHERE budget_id = ? AND
quantityreceived > 0 AND
datecancellationprinted IS NULL
|);
$sth->execute($budget_id);
my $sum = ( $sth->fetchrow_array || 0 ) + 0;
$sth = $dbh->prepare(qq|
SELECT SUM(shipmentcost) AS sum
FROM aqinvoices
WHERE shipmentcost_budgetid = ?
|);
$sth->execute($budget_id);
my ($shipmentcost_sum) = $sth->fetchrow_array;
$sum += ( $shipmentcost_sum || 0 ) + 0;
my $adjustments = Koha::Acquisition::Invoice::Adjustments->search({budget_id => $budget_id, closedate => { '!=' => undef } },{ join => 'invoiceid' });
while ( my $adj = $adjustments->next ){
$sum += $adj->adjustment;
}
return $sum;
}
# -------------------------------------------------------------------
sub GetBudgetOrdered {
my ($budget_id) = @_;
my $dbh = C4::Context->dbh;
# Get correct unitprice and ecost prices as possible
my ( $unitprice_field, $ecost_field ) = FieldsForCalculatingFundValues();
my $sth = $dbh->prepare(qq|
SELECT SUM(| . C4::Acquisition::get_rounding_sql(qq|$ecost_field|) . qq| * quantity) AS sum FROM aqorders
WHERE budget_id = ? AND
quantityreceived = 0 AND
datecancellationprinted IS NULL
|);
$sth->execute($budget_id);
my $sum = ( $sth->fetchrow_array || 0 ) + 0;
my $adjustments = Koha::Acquisition::Invoice::Adjustments->search({budget_id => $budget_id, encumber_open => 1, closedate => undef},{ join => 'invoiceid' });
while ( my $adj = $adjustments->next ){
$sum += $adj->adjustment;
}
return $sum;
}
=head2 GetBudgetName
my $budget_name = &GetBudgetName($budget_id);
get the budget_name for a given budget_id
=cut
sub GetBudgetName {
my ( $budget_id ) = @_;
my $dbh = C4::Context->dbh;
my $sth = $dbh->prepare(
qq|
SELECT budget_name
FROM aqbudgets
WHERE budget_id = ?
|);
$sth->execute($budget_id);
return $sth->fetchrow_array;
}
=head2 GetBudgetAuthCats
my $auth_cats = &GetBudgetAuthCats($budget_period_id);
Return the list of authcat for a given budget_period_id
=cut
sub GetBudgetAuthCats {
my ($budget_period_id) = shift;
# now, populate the auth_cats_loop used in the budget planning button
# we must retrieve all auth values used by at least one budget
my $dbh = C4::Context->dbh;
my $sth=$dbh->prepare("SELECT sort1_authcat,sort2_authcat FROM aqbudgets WHERE budget_period_id=?");
$sth->execute($budget_period_id);
my %authcats;
while (my ($sort1_authcat,$sort2_authcat) = $sth->fetchrow) {
$authcats{$sort1_authcat}=1 if $sort1_authcat;
$authcats{$sort2_authcat}=1 if $sort2_authcat;
}
return [ sort keys %authcats ];
}
# -------------------------------------------------------------------
sub GetBudgetPeriods {
my ($filters,$orderby) = @_;
my $rs = Koha::Database->new()->schema->resultset('Aqbudgetperiod');
$rs = $rs->search( $filters, { order_by => $orderby } );
$rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
return [ $rs->all ];
}
# -------------------------------------------------------------------
sub GetBudgetPeriod {
my ($budget_period_id) = @_;
my $dbh = C4::Context->dbh;
my $sth = $dbh->prepare( qq|
SELECT *
FROM aqbudgetperiods
WHERE budget_period_id=? |
);
$sth->execute($budget_period_id);
return $sth->fetchrow_hashref;
}
sub DelBudgetPeriod{
my ($budget_period_id) = @_;
my $dbh = C4::Context->dbh;
; ## $total = number of records linked to the record that must be deleted
my $total = 0;
## get information about the record that will be deleted
my $sth = $dbh->prepare(qq|
DELETE
FROM aqbudgetperiods
WHERE budget_period_id=? |
);
return $sth->execute($budget_period_id);
}
# -------------------------------------------------------------------
sub ModBudgetPeriod {
my ($budget_period) = @_;
my $result = Koha::Database->new()->schema->resultset('Aqbudgetperiod')->find($budget_period);
return unless($result);
$result = $result->update($budget_period);
return $result->in_storage;
}
# -------------------------------------------------------------------
sub GetBudgetHierarchy {
my ( $budget_period_id, $branchcode, $owner, $skiptotals ) = @_;
my @bind_params;
my $dbh = C4::Context->dbh;
my $query = qq|
SELECT aqbudgets.*, aqbudgetperiods.budget_period_active, aqbudgetperiods.budget_period_description,
b.firstname as budget_owner_firstname, b.surname as budget_owner_surname, b.borrowernumber as budget_owner_borrowernumber
FROM aqbudgets
LEFT JOIN borrowers b on b.borrowernumber = aqbudgets.budget_owner_id
JOIN aqbudgetperiods USING (budget_period_id)|;
my @where_strings;
# show only period X if requested
if ($budget_period_id) {
push @where_strings," aqbudgets.budget_period_id = ?";
push @bind_params, $budget_period_id;
}
# show only budgets owned by me, my branch or everyone
if ($owner) {
if ($branchcode) {
push @where_strings,
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="")))};
push @bind_params, ( $owner, $branchcode );
} else {
push @where_strings, ' (budget_owner_id = ? OR budget_owner_id IS NULL or budget_owner_id ="") ';
push @bind_params, $owner;
}
} else {
if ($branchcode) {
push @where_strings," (budget_branchcode =? or budget_branchcode is NULL OR budget_branchcode='')";
push @bind_params, $branchcode;
}
}
$query.=" WHERE ".join(' AND ', @where_strings) if @where_strings;
my $sth = $dbh->prepare($query);
$sth->execute(@bind_params);
my %links;
# create hash with budget_id has key
while ( my $data = $sth->fetchrow_hashref ) {
$links{ $data->{'budget_id'} } = $data;
}
# link child to parent
my @first_parents;
foreach my $budget ( sort { $a->{budget_code} cmp $b->{budget_code} } values %links ) {
my $child = $links{$budget->{budget_id}};
if ( $child->{'budget_parent_id'} ) {
my $parent = $links{ $child->{'budget_parent_id'} };
if ($parent) {
unless ( $parent->{'children'} ) {
# init child arrayref
$parent->{'children'} = [];
}
# add as child
push @{ $parent->{'children'} }, $child;
}
} else {
push @first_parents, $child;
}
}
my @sort = ();
foreach my $first_parent (@first_parents) {
_add_budget_children(\@sort, $first_parent, 0);
}
# Get correct unitprice and ecost prices as possible
my ( $unitprice_field, $ecost_field ) = FieldsForCalculatingFundValues();
if (!$skiptotals) {
# Get all the budgets totals in as few queries as possible
my $hr_budget_spent = $dbh->selectall_hashref(q|
SELECT aqorders.budget_id, aqbudgets.budget_parent_id,
SUM( | . C4::Acquisition::get_rounding_sql(qq|COALESCE($unitprice_field, $ecost_field)|) . q| * quantity ) AS budget_spent
FROM aqorders JOIN aqbudgets USING (budget_id)
WHERE quantityreceived > 0 AND datecancellationprinted IS NULL
GROUP BY budget_id, budget_parent_id
|, 'budget_id');
my $hr_budget_ordered = $dbh->selectall_hashref(q|
SELECT aqorders.budget_id, aqbudgets.budget_parent_id,
SUM( | . C4::Acquisition::get_rounding_sql(qq|$ecost_field|) . q| * quantity) AS budget_ordered
FROM aqorders JOIN aqbudgets USING (budget_id)
WHERE quantityreceived = 0 AND datecancellationprinted IS NULL
GROUP BY budget_id, budget_parent_id
|, 'budget_id');
my $hr_budget_spent_shipment = $dbh->selectall_hashref(q|
SELECT shipmentcost_budgetid as budget_id,
SUM(shipmentcost) as shipmentcost
FROM aqinvoices
GROUP BY shipmentcost_budgetid
|, 'budget_id');
my $hr_budget_spent_adjustment = $dbh->selectall_hashref(q|
SELECT budget_id,
SUM(adjustment) as adjustments
FROM aqinvoice_adjustments
JOIN aqinvoices USING (invoiceid)
WHERE closedate IS NOT NULL
GROUP BY budget_id
|, 'budget_id');
my $hr_budget_ordered_adjustment = $dbh->selectall_hashref(q|
SELECT budget_id,
SUM(adjustment) as adjustments
FROM aqinvoice_adjustments
JOIN aqinvoices USING (invoiceid)
WHERE closedate IS NULL AND encumber_open = 1
GROUP BY budget_id
|, 'budget_id');
foreach my $budget (@sort) {
if ( not defined $budget->{budget_parent_id} ) {
_recursiveAdd( $budget, undef, $hr_budget_spent, $hr_budget_spent_shipment, $hr_budget_ordered, $hr_budget_spent_adjustment, $hr_budget_ordered_adjustment );
}
}
}
return \@sort;
}
sub _recursiveAdd {
my ($budget, $parent, $hr_budget_spent, $hr_budget_spent_shipment, $hr_budget_ordered, $hr_budget_spent_adjustment, $hr_budget_ordered_adjustment ) = @_;
foreach my $child (@{$budget->{children}}){
_recursiveAdd($child, $budget, $hr_budget_spent, $hr_budget_spent_shipment, $hr_budget_ordered, $hr_budget_spent_adjustment, $hr_budget_ordered_adjustment );
}
$budget->{budget_spent} += $hr_budget_spent->{$budget->{budget_id}}->{budget_spent} || 0;
$budget->{budget_spent} += $hr_budget_spent_shipment->{$budget->{budget_id}}->{shipmentcost} || 0;
$budget->{budget_spent} += $hr_budget_spent_adjustment->{$budget->{budget_id}}->{adjustments} || 0;
$budget->{budget_ordered} += $hr_budget_ordered->{$budget->{budget_id}}->{budget_ordered} || 0;
$budget->{budget_ordered} += $hr_budget_ordered_adjustment->{$budget->{budget_id}}->{adjustments} || 0;
$budget->{total_spent} += $budget->{budget_spent};
$budget->{total_ordered} += $budget->{budget_ordered};
if ($parent) {
$parent->{total_spent} += $budget->{total_spent};
$parent->{total_ordered} += $budget->{total_ordered};
}
}
# Recursive method to add a budget and its chidren to an array
sub _add_budget_children {
my $res = shift;
my $budget = shift;
$budget->{budget_level} = shift;
push @$res, $budget;
my $children = $budget->{'children'} || [];
return unless @$children; # break recursivity
foreach my $child (@$children) {
_add_budget_children($res, $child, $budget->{budget_level} + 1);
}
}
# -------------------------------------------------------------------
# FIXME Must be replaced by Koha::Acquisition::Fund->store
sub AddBudget {
my ($budget) = @_;
return unless ($budget);
undef $budget->{budget_encumb} if defined $budget->{budget_encumb} && $budget->{budget_encumb} eq '';
undef $budget->{budget_owner_id} if defined $budget->{budget_owner_id} && $budget->{budget_owner_id} eq '';
my $resultset = Koha::Database->new()->schema->resultset('Aqbudget');
my $id = $resultset->create($budget)->id;
# Log the addition
if (C4::Context->preference("AcquisitionLog")) {
my $infos = {
budget_amount => $budget->{budget_amount},
budget_encumb => $budget->{budget_encumb},
budget_expend => $budget->{budget_expend}
};
logaction(
'ACQUISITIONS',
'CREATE_FUND',
$id,
encode_json($infos)
);
}
return $id;
}
# -------------------------------------------------------------------
# FIXME Must be replaced by Koha::Acquisition::Fund->store
sub ModBudget {
my ($budget) = @_;
my $result = Koha::Database->new()->schema->resultset('Aqbudget')->find($budget);
return unless($result);
# Log this modification
if (C4::Context->preference("AcquisitionLog")) {
my $infos = {
budget_amount_new => $budget->{budget_amount},
budget_encumb_new => $budget->{budget_encumb},
budget_expend_new => $budget->{budget_expend},
budget_amount_old => $result->budget_amount,
budget_encumb_old => $result->budget_encumb,
budget_expend_old => $result->budget_expend,
budget_amount_change => 0 - ($result->budget_amount - $budget->{budget_amount})
};
logaction(
'ACQUISITIONS',
'MODIFY_FUND',
$budget->{budget_id},
encode_json($infos)
);
}
undef $budget->{budget_encumb} if defined $budget->{budget_encumb} && $budget->{budget_encumb} eq '';
undef $budget->{budget_owner_id} if defined $budget->{budget_owner_id} && $budget->{budget_owner_id} eq '';
$result = $result->update($budget);
return $result->in_storage;
}
# -------------------------------------------------------------------
# FIXME Must be replaced by Koha::Acquisition::Fund->delete
sub DelBudget {
my ($budget_id) = @_;
my $dbh = C4::Context->dbh;
my $sth = $dbh->prepare("delete from aqbudgets where budget_id=?");
my $rc = $sth->execute($budget_id);
# Log the deletion
if (C4::Context->preference("AcquisitionLog")) {
logaction(
'ACQUISITIONS',
'DELETE_FUND',
$budget_id
);
}
return $rc;
}
# -------------------------------------------------------------------
=head2 GetBudget
&GetBudget($budget_id);
get a specific budget
=cut
sub GetBudget {
my ( $budget_id ) = @_;
my $dbh = C4::Context->dbh;
my $query = "
SELECT *
FROM aqbudgets
WHERE budget_id=?
";
my $sth = $dbh->prepare($query);
$sth->execute( $budget_id );
my $result = $sth->fetchrow_hashref;
return $result;
}
# -------------------------------------------------------------------
=head2 GetBudgetByOrderNumber
&GetBudgetByOrderNumber($ordernumber);
get a specific budget by order number
=cut
sub GetBudgetByOrderNumber {
my ( $ordernumber ) = @_;
my $dbh = C4::Context->dbh;
my $query = "
SELECT aqbudgets.*
FROM aqbudgets, aqorders
WHERE ordernumber=?
AND aqorders.budget_id = aqbudgets.budget_id
";
my $sth = $dbh->prepare($query);
$sth->execute( $ordernumber );
my $result = $sth->fetchrow_hashref;
return $result;
}
=head2 GetBudgetReport
&GetBudgetReport( [$budget_id] );
Get all orders for a specific budget, without cancelled orders.
Returns an array of hashrefs.
=cut
# --------------------------------------------------------------------
sub GetBudgetReport {
my ( $budget_id ) = @_;
my $dbh = C4::Context->dbh;
my $query = '
SELECT o.*, b.budget_name
FROM aqbudgets b
INNER JOIN aqorders o
ON b.budget_id = o.budget_id
WHERE b.budget_id=?
AND (o.orderstatus != "cancelled")
ORDER BY b.budget_name';
my $sth = $dbh->prepare($query);
$sth->execute( $budget_id );
my @results = ();
while ( my $data = $sth->fetchrow_hashref ) {
push( @results, $data );
}
return @results;
}
=head2 GetBudgetsByActivity
&GetBudgetsByActivity( $budget_period_active );
Get all active or inactive budgets, depending of the value
of the parameter.
1 = active
0 = inactive
=cut
# --------------------------------------------------------------------
sub GetBudgetsByActivity {
my ( $budget_period_active ) = @_;
my $dbh = C4::Context->dbh;
my $query = "
SELECT DISTINCT b.*
FROM aqbudgetperiods bp
INNER JOIN aqbudgets b
ON bp.budget_period_id = b.budget_period_id
WHERE bp.budget_period_active=?
";
my $sth = $dbh->prepare($query);
$sth->execute( $budget_period_active );
my @results = ();
while ( my $data = $sth->fetchrow_hashref ) {
push( @results, $data );
}
return @results;
}
# --------------------------------------------------------------------
=head2 GetBudgetsReport
&GetBudgetsReport( [$activity] );
Get all but cancelled orders for all funds.
If the optionnal activity parameter is passed, returns orders for active/inactive budgets only.
active = 1
inactive = 0
Returns an array of hashrefs.
=cut
sub GetBudgetsReport {
my ($activity) = @_;
my $dbh = C4::Context->dbh;
my $query = '
SELECT o.*, b.budget_name
FROM aqbudgetperiods bp
INNER JOIN aqbudgets b
ON bp.budget_period_id = b.budget_period_id
INNER JOIN aqorders o
ON b.budget_id = o.budget_id ';
if ( $activity && $activity ne '' ) {
$query .= 'WHERE bp.budget_period_active=? ';
}
$query .= 'AND (o.orderstatus != "cancelled")
ORDER BY b.budget_name';
my $sth = $dbh->prepare($query);
if ( $activity && $activity ne '' ) {
$sth->execute($activity);
}
else{
$sth->execute;
}
my @results = ();
while ( my $data = $sth->fetchrow_hashref ) {
push( @results, $data );
}
return @results;
}
=head2 GetBudgetByCode
my $budget = &GetBudgetByCode($budget_code);
Retrieve all aqbudgets fields as a hashref for the budget that has
given budget_code
=cut
sub GetBudgetByCode {
my ( $budget_code ) = @_;
my $dbh = C4::Context->dbh;
my $query = qq{
SELECT aqbudgets.*
FROM aqbudgets
JOIN aqbudgetperiods USING (budget_period_id)
WHERE budget_code = ?
ORDER BY budget_period_active DESC, budget_id DESC
LIMIT 1
};
my $sth = $dbh->prepare( $query );
$sth->execute( $budget_code );
return $sth->fetchrow_hashref;
}
=head2 GetBudgetHierarchySpent
my $spent = GetBudgetHierarchySpent( $budget_id );
Gets the total spent of the level and sublevels of $budget_id
=cut
sub GetBudgetHierarchySpent {
my ( $budget_id ) = @_;
my $dbh = C4::Context->dbh;
my $children_ids = $dbh->selectcol_arrayref(q|
SELECT budget_id
FROM aqbudgets
WHERE budget_parent_id = ?
|, {}, $budget_id );
my $total_spent = GetBudgetSpent( $budget_id );
for my $child_id ( @$children_ids ) {
$total_spent += GetBudgetHierarchySpent( $child_id );
}
return $total_spent;
}
=head2 GetBudgetHierarchyOrdered
my $ordered = GetBudgetHierarchyOrdered( $budget_id );
Gets the total ordered of the level and sublevels of $budget_id
=cut
sub GetBudgetHierarchyOrdered {
my ( $budget_id ) = @_;
my $dbh = C4::Context->dbh;
my $children_ids = $dbh->selectcol_arrayref(q|
SELECT budget_id
FROM aqbudgets
WHERE budget_parent_id = ?
|, {}, $budget_id );
my $total_ordered = GetBudgetOrdered( $budget_id );
for my $child_id ( @$children_ids ) {
$total_ordered += GetBudgetHierarchyOrdered( $child_id );
}
return $total_ordered;
}
=head2 GetBudgets
&GetBudgets($filter, $order_by);
gets all budgets
=cut
# -------------------------------------------------------------------
sub GetBudgets {
my ($filters, $orderby) = @_;
$orderby = 'budget_name' unless($orderby);
my $rs = Koha::Database->new()->schema->resultset('Aqbudget');
$rs = $rs->search( $filters, { order_by => $orderby } );
$rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
return [ $rs->all ];
}
=head2 GetBudgetUsers
my @borrowernumbers = &GetBudgetUsers($budget_id);
Return the list of borrowernumbers linked to a budget
=cut
sub GetBudgetUsers {
my ($budget_id) = @_;
my $dbh = C4::Context->dbh;
my $query = qq{
SELECT borrowernumber
FROM aqbudgetborrowers
WHERE budget_id = ?
};
my $sth = $dbh->prepare($query);
$sth->execute($budget_id);
my @borrowernumbers;