]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Circ/StatCat.pm
somehow forgot to add the billing_type field
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Application / Circ / StatCat.pm
1 # ---------------------------------------------------------------
2 # Copyright (C) 2005  Georgia Public Library Service 
3 # Bill Erickson <highfalutin@gmail.com>
4
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
9
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 # ---------------------------------------------------------------
15
16 package OpenILS::Application::Circ::StatCat;
17 use base qw/OpenSRF::Application/;
18 use strict; use warnings;
19
20 use OpenSRF::EX qw/:try/;
21 use OpenILS::Application::AppUtils;
22 my $apputils = "OpenILS::Application::AppUtils";
23
24
25
26 __PACKAGE__->register_method(
27         method  => "retrieve_stat_cats",
28         api_name        => "open-ils.circ.stat_cat.actor.retrieve.all");
29
30 __PACKAGE__->register_method(
31         method  => "retrieve_stat_cats",
32         api_name        => "open-ils.circ.stat_cat.asset.retrieve.all");
33
34 # retrieves all of the stat cats for a given org unit
35 # if no orgid, user_session->home_ou is used
36
37 sub retrieve_stat_cats {
38         my( $self, $client, $user_session, $orgid ) = @_;
39
40         my $method = "open-ils.storage.ranged.fleshed.actor.stat_cat.all.atomic"; 
41         if( $self->api_name =~ /asset/ ) {
42                 $method = "open-ils.storage.ranged.fleshed.asset.stat_cat.all.atomic"; 
43         }
44
45
46         my $user_obj = $apputils->check_user_session($user_session); 
47         if(!$orgid) { $orgid = $user_obj->home_ou; }
48
49         return $apputils->simple_scalar_request(
50                                 "open-ils.storage", $method, $orgid );
51 }
52
53
54
55 __PACKAGE__->register_method(
56         method  => "retrieve_ranged_intersect_stat_cats",
57         api_name        => "open-ils.circ.stat_cat.asset.multirange.intersect.retrieve");
58
59 sub retrieve_ranged_intersect_stat_cats {
60         my( $self, $client, $user_session, $orglist ) = @_;
61
62         my $user_obj = $apputils->check_user_session($user_session); 
63         if(!$orglist) { $orglist = [ $user_obj->home_ou ]; }
64
65         # uniquify, yay!
66         my %hash = map { ($_ => 1) } @$orglist;
67         $orglist = [ keys %hash ];
68
69         warn "range: @$orglist\n";
70
71         my      $method = "open-ils.storage.multiranged.intersect.fleshed.asset.stat_cat.all.atomic";
72         return $apputils->simple_scalar_request(
73                                 "open-ils.storage", $method, $orglist );
74 }
75
76
77 __PACKAGE__->register_method(
78         method  => "retrieve_ranged_union_stat_cats",
79         api_name        => "open-ils.circ.stat_cat.asset.multirange.union.retrieve");
80
81 sub retrieve_ranged_union_stat_cats {
82         my( $self, $client, $user_session, $orglist ) = @_;
83
84         my      $method = "open-ils.storage.multiranged.union.fleshed.asset.stat_cat.all.atomic";
85         use Data::Dumper;
86         warn "Retrieving stat_cats with method $method and orgs " . Dumper($orglist) . "\n";
87
88         my $user_obj = $apputils->check_user_session($user_session); 
89         if(!$orglist) { $orglist = [ $user_obj->home_ou ]; }
90
91         # uniquify, yay!
92         my %hash = map { ($_ => 1) } @$orglist;
93         $orglist = [ keys %hash ];
94
95         warn "range: @$orglist\n";
96
97         return $apputils->simple_scalar_request(
98                                 "open-ils.storage", $method, $orglist );
99 }
100
101
102
103
104
105 __PACKAGE__->register_method(
106         method  => "stat_cat_create",
107         api_name        => "open-ils.circ.stat_cat.asset.create");
108
109 __PACKAGE__->register_method(
110         method  => "stat_cat_create",
111         api_name        => "open-ils.circ.stat_cat.actor.create");
112
113 sub stat_cat_create {
114         my( $self, $client, $user_session, $stat_cat ) = @_;
115
116         if(!$stat_cat) {
117                 throw OpenSRF::EX::ERROR
118                         ("stat_cat.*.create requires a stat_cat object");
119         }
120
121         my $user_obj = $apputils->check_user_session($user_session); 
122         my $orgid = $user_obj->home_ou();
123
124         my $method = "open-ils.storage.direct.actor.stat_cat.create";
125         my $entry_create = "open-ils.storage.direct.actor.stat_cat_entry.create";
126
127         if($self->api_name =~ /asset/) {
128                 $method = "open-ils.storage.direct.asset.stat_cat.create";
129                 $entry_create = "open-ils.storage.direct.asset.stat_cat_entry.create";
130         }
131
132         my $session = $apputils->start_db_session();
133         my $newid = _create_stat_cat($session, $stat_cat, $method);
134
135         for my $entry (@{$stat_cat->entries}) {
136                 $entry->stat_cat($newid);
137                 _create_stat_entry($session, $entry, $entry_create);
138         }
139
140         $apputils->commit_db_session($session);
141
142         warn "Stat cat creation successful with id $newid\n";
143
144         if( $self->api_name =~ /asset/ ) {
145                 return _flesh_asset_cat($newid, $orgid);
146         } else {
147                 return _flesh_user_cat($newid, $orgid);
148         }
149
150 }
151
152
153 sub _flesh_user_cat {
154         my $id = shift;
155         my $orgid = shift;
156
157         my $session = OpenSRF::AppSession->create("open-ils.storage");
158         my $cat = $session->request(
159                 "open-ils.storage.direct.actor.stat_cat.retrieve",
160                 $id )->gather(1);
161
162         $cat->entries( 
163                 $session->request(
164                         "open-ils.storage.ranged.actor.stat_cat_entry.search.stat_cat.atomic",
165                         $orgid, $id )->gather(1) );
166
167         return $cat;
168 }
169
170
171 sub _flesh_asset_cat {
172         my $id = shift;
173         my $orgid = shift;
174
175         my $session = OpenSRF::AppSession->create("open-ils.storage");
176         my $cat = $session->request(
177                 "open-ils.storage.direct.asset.stat_cat.retrieve",
178                 $id )->gather(1);
179
180         $cat->entries( 
181                 $session->request(
182                         "open-ils.storage.ranged.asset.stat_cat_entry.search.stat_cat.atomic",
183                         $orgid,  $id )->gather(1) );
184
185         return $cat;
186
187 }
188
189
190 sub _create_stat_cat {
191         my( $session, $stat_cat, $method) = @_;
192         warn "Creating new stat cat with name " . $stat_cat->name . "\n";
193         $stat_cat->clear_id();
194         my $req = $session->request( $method, $stat_cat );
195         my $id = $req->gather(1);
196         if(!$id) {
197                 throw OpenSRF::EX::ERROR 
198                 ("Error creating new statistical category"); }
199
200         warn "Stat cat create returned id $id\n";
201         return $id;
202 }
203
204
205 sub _create_stat_entry {
206         my( $session, $stat_entry, $method) = @_;
207
208         warn "Creating new stat entry with value " . $stat_entry->value . "\n";
209         $stat_entry->clear_id();
210
211         my $req = $session->request($method, $stat_entry);
212         my $id = $req->gather(1);
213
214         warn "Stat entry " . Dumper($stat_entry) . "\n";        
215         
216         if(!$id) {
217                 throw OpenSRF::EX::ERROR 
218                 ("Error creating new stat cat entry"); }
219
220         warn "Stat cat entry create returned id $id\n";
221         return $id;
222 }
223
224
225 __PACKAGE__->register_method(
226         method  => "update_stat_entry",
227         api_name        => "open-ils.circ.stat_cat.actor.entry.update");
228
229 __PACKAGE__->register_method(
230         method  => "update_stat_entry",
231         api_name        => "open-ils.circ.stat_cat.asset.entry.update");
232
233 sub update_stat_entry {
234         my( $self, $client, $user_session, $entry ) = @_;
235
236         my $user_obj = $apputils->check_user_session($user_session); 
237
238         my $method = "open-ils.storage.direct.actor.stat_cat_entry.update";
239         if($self->api_name =~ /asset/) {
240                 $method = "open-ils.storage.direct.asset.stat_cat_entry.update";
241         }
242
243         my $session = $apputils->start_db_session();
244         my $req = $session->request($method, $entry); 
245         my $status = $req->gather(1);
246         $apputils->commit_db_session($session);
247         warn "stat cat entry with value " . $entry->value . " updated with status $status\n";
248         return 1;
249 }
250
251
252 __PACKAGE__->register_method(
253         method  => "update_stat",
254         api_name        => "open-ils.circ.stat_cat.actor.update");
255
256 __PACKAGE__->register_method(
257         method  => "update_stat",
258         api_name        => "open-ils.circ.stat_cat.asset.update");
259
260 sub update_stat {
261         my( $self, $client, $user_session, $cat ) = @_;
262
263         my $user_obj = $apputils->check_user_session($user_session); 
264
265         my $method = "open-ils.storage.direct.actor.stat_cat.update";
266         if($self->api_name =~ /asset/) {
267                 $method = "open-ils.storage.direct.asset.stat_cat.update";
268         }
269
270         my $session = $apputils->start_db_session();
271         my $req = $session->request($method, $cat); 
272         my $status = $req->gather(1);
273         $apputils->commit_db_session($session);
274         warn "stat cat with id " . $cat->id . " updated with status $status\n";
275         return 1;
276 }
277
278
279 __PACKAGE__->register_method(
280         method  => "create_stat_entry",
281         api_name        => "open-ils.circ.stat_cat.actor.entry.create");
282
283 __PACKAGE__->register_method(
284         method  => "create_stat_entry",
285         api_name        => "open-ils.circ.stat_cat.asset.entry.create");
286
287 sub create_stat_entry {
288         my( $self, $client, $user_session, $entry ) = @_;
289
290         my $user_obj = $apputils->check_user_session($user_session); 
291
292         $entry->clear_id();
293         use Data::Dumper;
294         warn Dumper($entry);
295
296         my $method = "open-ils.storage.direct.actor.stat_cat_entry.create";
297         if($self->api_name =~ /asset/) {
298                 $method = "open-ils.storage.direct.asset.stat_cat_entry.create";
299         }
300
301         my $session = $apputils->start_db_session();
302         my $req = $session->request($method, $entry); 
303         my $status = $req->gather(1);
304         $apputils->commit_db_session($session);
305
306         warn "stat cat entry with id " . $status . " updated with status $status\n";
307         return $status;
308 }
309
310
311
312 __PACKAGE__->register_method(
313         method  => "create_stat_map",
314         api_name        => "open-ils.circ.stat_cat.actor.user_map.create");
315
316 __PACKAGE__->register_method(
317         method  => "create_stat_map",
318         api_name        => "open-ils.circ.stat_cat.asset.copy_map.create");
319
320 sub create_stat_map {
321         my( $self, $client, $user_session, $map ) = @_;
322
323         my $user_obj = $apputils->check_user_session($user_session); 
324
325         warn "Creating stat_cat_map\n";
326
327         $map->clear_id();
328
329         my $method = "open-ils.storage.direct.actor.stat_cat_entry_user_map.create";
330         my $ret = "open-ils.storage.direct.actor.stat_cat_entry_user_map.retrieve";
331         if($self->api_name =~ /asset/) {
332                 $method = "open-ils.storage.direct.asset.stat_cat_entry_copy_map.create";
333                 $ret = "open-ils.storage.direct.asset.stat_cat_entry_copy_map.retrieve";
334         }
335
336         my $session = $apputils->start_db_session();
337         my $req = $session->request($method, $map); 
338         my $newid = $req->gather(1);
339         warn "Created new stat cat map with id $newid\n";
340         $apputils->commit_db_session($session);
341
342         return $apputils->simple_scalar_request( "open-ils.storage", $ret, $newid );
343
344 }
345
346
347 __PACKAGE__->register_method(
348         method  => "update_stat_map",
349         api_name        => "open-ils.circ.stat_cat.actor.user_map.update");
350
351 __PACKAGE__->register_method(
352         method  => "update_stat_map",
353         api_name        => "open-ils.circ.stat_cat.asset.copy_map.update");
354
355 sub update_stat_map {
356         my( $self, $client, $user_session, $map ) = @_;
357
358         my $user_obj = $apputils->check_user_session($user_session); 
359
360         warn "Updating stat_cat_map\n";
361
362         my $method = "open-ils.storage.direct.actor.stat_cat_entry_user_map.update";
363         if($self->api_name =~ /asset/) {
364                 $method = "open-ils.storage.direct.asset.stat_cat_entry_copy_map.update";
365         }
366
367         my $session = $apputils->start_db_session();
368         my $req = $session->request($method, $map); 
369         my $newid = $req->gather(1);
370         warn "Updated new stat cat map with id $newid\n";
371         $apputils->commit_db_session($session);
372
373         return $newid;
374 }
375
376
377
378 __PACKAGE__->register_method(
379         method  => "retrieve_maps",
380         api_name        => "open-ils.circ.stat_cat.actor.user_map.retrieve");
381
382 __PACKAGE__->register_method(
383         method  => "retrieve_maps",
384         api_name        => "open-ils.circ.stat_cat.asset.copy_map.retrieve");
385
386 sub retrieve_maps {
387         my( $self, $client, $user_session, $target ) = @_;
388
389         my $user_obj = $apputils->check_user_session($user_session); 
390
391         my      $method = "open-ils.storage.direct.asset.stat_cat_entry_copy_map.search.owning_copy.atomic";
392         if($self->api_name =~ /actor/ ) {
393                 if(!$target) { $target = $user_obj->id; }
394                 $method = "open-ils.storage.direct.actor.stat_cat_entry_user_map.search.target_usr.atomic";
395         }
396
397         return $apputils->simple_scalar_request("open-ils.storage", $method, $target);
398 }
399
400
401
402
403 __PACKAGE__->register_method(
404         method  => "delete_stats",
405         api_name        => "open-ils.circ.stat_cat.actor.delete");
406
407 __PACKAGE__->register_method(
408         method  => "delete_stats",
409         api_name        => "open-ils.circ.stat_cat.asset.delete");
410
411 sub delete_stats {
412         my( $self, $client, $user_session, $target ) = @_;
413         my $user_obj = $apputils->check_user_session($user_session); 
414         my $session = OpenSRF::AppSession->create("open-ils.storage");
415         my $type = "actor";
416         if($self->api_name =~ /asset/) { $type = "asset"; }
417         return _delete_stats($session, $target, $type);
418 }
419
420 sub _delete_stats {
421         my( $session, $stat, $type) = @_;
422
423         my      $method = "open-ils.storage.direct.asset.stat_cat.delete";
424         if($type =~ /actor/ ) {
425                 $method = "open-ils.storage.direct.actor.stat_cat.delete";
426         }
427         return $session->request($method, $stat)->gather(1);
428 }
429
430
431
432 __PACKAGE__->register_method(
433         method  => "delete_entry",
434         api_name        => "open-ils.circ.stat_cat.actor.entry.delete");
435
436 __PACKAGE__->register_method(
437         method  => "delete_entry",
438         api_name        => "open-ils.circ.stat_cat.asset.entry.delete");
439
440 sub delete_entry {
441         my( $self, $client, $user_session, $target ) = @_;
442         my $user_obj = $apputils->check_user_session($user_session); 
443         my $session = OpenSRF::AppSession->create("open-ils.storage");
444         my $type = "actor";
445         if($self->api_name =~ /asset/) { $type = "asset"; }
446         return _delete_entry($session, $target, $type);
447 }
448
449 sub _delete_entry {
450         my( $session, $stat_entry, $type) = @_;
451
452         my      $method = "open-ils.storage.direct.asset.stat_cat_entry.delete";
453         if($type =~ /actor/ ) {
454                 $method = "open-ils.storage.direct.actor.stat_cat_entry.delete";
455         }
456
457         return $session->request($method, $stat_entry)->gather(1);
458 }
459
460
461
462 1;