Lines 29-65
Koha::REST::V1::Acquisitions::Funds
Link Here
|
29 |
|
29 |
|
30 |
=head1 API |
30 |
=head1 API |
31 |
|
31 |
|
32 |
=head2 Methods |
32 |
=head2 Class methods |
33 |
|
33 |
|
34 |
=head3 list_funds |
34 |
=head3 list |
35 |
|
35 |
|
36 |
Controller function that handles listing Funds |
36 |
Controller function that handles listing Funds |
37 |
|
37 |
|
38 |
=cut |
38 |
=cut |
39 |
|
39 |
|
40 |
sub list_funds { |
40 |
sub list { |
41 |
my $c = shift->openapi->valid_input or return; |
41 |
my $c = shift->openapi->valid_input or return; |
42 |
|
42 |
|
43 |
my $args = _to_model($c->req->params->to_hash); |
|
|
44 |
my $filter; |
45 |
|
46 |
for my $filter_param ( keys %$args ) { |
47 |
if ($args->{$filter_param}) { |
48 |
if ($filter_param eq "budget_owner_id") { |
49 |
# Perform an exact search on the borrowernumber |
50 |
$filter->{$filter_param} = { "=" => $args->{$filter_param} } |
51 |
} else { |
52 |
# And a "start with" search on the budget name |
53 |
$filter->{$filter_param} = { LIKE => $args->{$filter_param} . "%" } |
54 |
} |
55 |
} |
56 |
} |
57 |
|
58 |
return try { |
43 |
return try { |
59 |
my $funds = Koha::Acquisition::Funds->search($filter); |
44 |
|
|
|
45 |
my $funds_rs = Koha::Acquisition::Funds->new; |
46 |
my $funds = $c->objects->search( $funds_rs ); |
47 |
|
60 |
return $c->render( |
48 |
return $c->render( |
61 |
status => 200, |
49 |
status => 200, |
62 |
openapi => $funds->to_api |
50 |
openapi => $funds |
63 |
); |
51 |
); |
64 |
} |
52 |
} |
65 |
catch { |
53 |
catch { |
Lines 69-135
sub list_funds {
Link Here
|
69 |
} |
57 |
} |
70 |
else { |
58 |
else { |
71 |
return $c->render( status => 500, |
59 |
return $c->render( status => 500, |
72 |
openapi => { error => "Something went wrong, check the logs. $_ $filter" } ); |
60 |
openapi => { error => "Something went wrong, check the logs. $_" } ); |
73 |
} |
61 |
} |
74 |
}; |
62 |
}; |
75 |
} |
63 |
} |
76 |
|
64 |
|
77 |
=head3 _to_api |
|
|
78 |
|
79 |
Helper function that maps a Fund into |
80 |
the attribute names the exposed REST api spec. |
81 |
|
82 |
=cut |
83 |
|
84 |
sub _to_api { |
85 |
my $fund = shift; |
86 |
my $returnfund; |
87 |
$returnfund->{fund_id} = delete $fund->{budget_id}; |
88 |
$returnfund->{code} = delete $fund->{budget_code}; |
89 |
$returnfund->{name} = delete $fund->{budget_name}; |
90 |
$returnfund->{library_id} = delete $fund->{budget_branchcode}; |
91 |
$returnfund->{total_amount} = delete $fund->{budget_amount}; |
92 |
$returnfund->{warn_at_percentage} = delete $fund->{budget_encumb}; |
93 |
$returnfund->{warn_at_amount} = delete $fund->{budget_expend}; |
94 |
$returnfund->{notes} = delete $fund->{budget_notes}; |
95 |
$returnfund->{budget_id} = delete $fund->{budget_period_id}; |
96 |
$returnfund->{timestamp} = delete $fund->{timestamp}; |
97 |
$returnfund->{fund_owner_id} = delete $fund->{budget_owner_id}; |
98 |
$returnfund->{fund_access} = delete $fund->{budget_permission}; |
99 |
$returnfund->{statistic1_auth_value_category} = delete $fund->{sort1_authcat}; |
100 |
$returnfund->{statistic2_auth_value_category} = delete $fund->{sort2_authcat}; |
101 |
|
102 |
return $returnfund; |
103 |
} |
104 |
|
105 |
=head3 _to_model |
106 |
|
107 |
Helper function that maps REST api objects into Fund |
108 |
attribute names. |
109 |
|
110 |
=cut |
111 |
|
112 |
sub _to_model { |
113 |
my $fund = shift; |
114 |
my $returnfund; |
115 |
|
116 |
# Rename back |
117 |
$returnfund->{budget_id} = delete $fund->{fund_id}; |
118 |
$returnfund->{budget_code} = delete $fund->{code}; |
119 |
$returnfund->{budget_name} = delete $fund->{name}; |
120 |
$returnfund->{budget_branchcode} = delete $fund->{library_id}; |
121 |
$returnfund->{budget_amount} = delete $fund->{total_amount}; |
122 |
$returnfund->{budget_encumb} = delete $fund->{warn_at_percentage}; |
123 |
$returnfund->{budget_expend} = delete $fund->{warn_at_amount}; |
124 |
$returnfund->{budget_notes} = delete $fund->{notes}; |
125 |
$returnfund->{budget_period_id} = delete $fund->{budget_id}; |
126 |
$returnfund->{budget_owner_id} = delete $fund->{fund_owner_id}; |
127 |
$returnfund->{timestamp} = delete $fund->{timestamp}; |
128 |
$returnfund->{budget_permission} = delete $fund->{fund_access}; |
129 |
$returnfund->{sort1_authcat} = delete $fund->{statistic1_auth_value_category}; |
130 |
$returnfund->{sort2_authcat} = delete $fund->{statistic2_auth_value_category}; |
131 |
|
132 |
return $returnfund; |
133 |
} |
134 |
|
135 |
1; |
65 |
1; |