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