]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Circ/StatCat.pm
user stat cats in the user editor
[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::Utils::Logger qw($logger);
21 use OpenSRF::EX qw/:try/;
22 use OpenILS::Application::AppUtils;
23 my $apputils = "OpenILS::Application::AppUtils";
24
25
26
27 __PACKAGE__->register_method(
28         method  => "retrieve_stat_cat_list",
29         argc    => 1,
30         api_name        => "open-ils.circ.stat_cat.actor.retrieve.batch");
31
32 __PACKAGE__->register_method(
33         method  => "retrieve_stat_cat_list",
34         argc    => 1,
35         api_name        => "open-ils.circ.stat_cat.asset.retrieve.batch");
36
37 # retrieves all of the stat cats for a given org unit
38 # if no orgid, user_session->home_ou is used
39
40 sub retrieve_stat_cat_list {
41         my( $self, $client, $user_session, @sc ) = @_;
42
43         if (ref($sc[0])) {
44                 @sc = @{$sc[0]};
45         }
46
47         my $method = "open-ils.storage.fleshed.actor.stat_cat.retrieve.batch.atomic"; 
48         if( $self->api_name =~ /asset/ ) {
49                 $method = "open-ils.storage.fleshed.asset.stat_cat.retrieve.batch.atomic"; 
50         }
51
52         my $user_obj = $apputils->check_user_session($user_session); 
53
54         my $cats = $apputils->simple_scalar_request(
55                                 "open-ils.storage", $method, @sc);
56
57         return [ sort { $a->name cmp $b->name } @$cats ];
58 }
59
60 __PACKAGE__->register_method(
61         method  => "retrieve_stat_cats",
62         api_name        => "open-ils.circ.stat_cat.actor.retrieve.all");
63
64 __PACKAGE__->register_method(
65         method  => "retrieve_stat_cats",
66         api_name        => "open-ils.circ.stat_cat.asset.retrieve.all");
67
68 # retrieves all of the stat cats for a given org unit
69 # if no orgid, user_session->home_ou is used
70
71 sub retrieve_stat_cats {
72         my( $self, $client, $user_session, $orgid ) = @_;
73
74         my $method = "open-ils.storage.ranged.fleshed.actor.stat_cat.all.atomic"; 
75         if( $self->api_name =~ /asset/ ) {
76                 $method = "open-ils.storage.ranged.fleshed.asset.stat_cat.all.atomic"; 
77         }
78
79         my $user_obj = $apputils->check_user_session($user_session); 
80         if(!$orgid) { $orgid = $user_obj->home_ou; }
81         my $cats = $apputils->simple_scalar_request(
82                                 "open-ils.storage", $method, $orgid );
83
84         return [ sort { $a->name cmp $b->name } @$cats ];
85 }
86
87
88 __PACKAGE__->register_method(
89         method  => "retrieve_ranged_intersect_stat_cats",
90         api_name        => "open-ils.circ.stat_cat.asset.multirange.intersect.retrieve");
91
92 sub retrieve_ranged_intersect_stat_cats {
93         my( $self, $client, $user_session, $orglist ) = @_;
94
95         my $user_obj = $apputils->check_user_session($user_session); 
96         if(!$orglist) { $orglist = [ $user_obj->home_ou ]; }
97
98         # uniquify, yay!
99         my %hash = map { ($_ => 1) } @$orglist;
100         $orglist = [ keys %hash ];
101
102         warn "range: @$orglist\n";
103
104         my      $method = "open-ils.storage.multiranged.intersect.fleshed.asset.stat_cat.all.atomic";
105         return $apputils->simple_scalar_request(
106                                 "open-ils.storage", $method, $orglist );
107 }
108
109
110 __PACKAGE__->register_method(
111         method  => "retrieve_ranged_union_stat_cats",
112         api_name        => "open-ils.circ.stat_cat.asset.multirange.union.retrieve");
113
114 sub retrieve_ranged_union_stat_cats {
115         my( $self, $client, $user_session, $orglist ) = @_;
116
117         my      $method = "open-ils.storage.multiranged.union.fleshed.asset.stat_cat.all.atomic";
118         use Data::Dumper;
119         warn "Retrieving stat_cats with method $method and orgs " . Dumper($orglist) . "\n";
120
121         my $user_obj = $apputils->check_user_session($user_session); 
122         if(!$orglist) { $orglist = [ $user_obj->home_ou ]; }
123
124         # uniquify, yay!
125         my %hash = map { ($_ => 1) } @$orglist;
126         $orglist = [ keys %hash ];
127
128         warn "range: @$orglist\n";
129
130         return $apputils->simple_scalar_request(
131                                 "open-ils.storage", $method, $orglist );
132 }
133
134
135
136 __PACKAGE__->register_method(
137         method  => "stat_cat_create",
138         api_name        => "open-ils.circ.stat_cat.asset.create");
139
140 __PACKAGE__->register_method(
141         method  => "stat_cat_create",
142         api_name        => "open-ils.circ.stat_cat.actor.create");
143
144 sub stat_cat_create {
145         my( $self, $client, $user_session, $stat_cat ) = @_;
146
147         my $method = "open-ils.storage.direct.actor.stat_cat.create";
148         my $entry_create = "open-ils.storage.direct.actor.stat_cat_entry.create";
149         my $perm = 'CREATE_PATRON_STAT_CAT';
150         my $eperm = 'CREATE_PATRON_STAT_CAT_ENTRY';
151
152         if($self->api_name =~ /asset/) {
153                 $method = "open-ils.storage.direct.asset.stat_cat.create";
154                 $entry_create = "open-ils.storage.direct.asset.stat_cat_entry.create";
155                 $perm = 'CREATE_COPY_STAT_CAT_ENTRY';
156         }
157
158         #my $user_obj = $apputils->check_user_session($user_session); 
159         #my $orgid = $user_obj->home_ou();
160         my( $user_obj, $evt ) = $apputils->checkses($user_session);
161         return $evt if $evt;
162         $evt = $apputils->check_perms($user_obj->id, $stat_cat->owner, $perm);
163         return $evt if $evt;
164
165         if($stat_cat->entries) {
166                 $evt = $apputils->check_perms($user_obj->id, $stat_cat->owner, $eperm);
167                 return $evt if $evt;
168         }
169
170
171         my $session = $apputils->start_db_session();
172         my $newid = _create_stat_cat($session, $stat_cat, $method);
173
174         if( ref($stat_cat->entries) ) {
175                 for my $entry (@{$stat_cat->entries}) {
176                         $entry->stat_cat($newid);
177                         _create_stat_entry($session, $entry, $entry_create);
178                 }
179         }
180
181         $apputils->commit_db_session($session);
182
183         $logger->debug("Stat cat creation successful with id $newid");
184
185         my $orgid = $user_obj->home_ou;
186         if( $self->api_name =~ /asset/ ) {
187                 return _flesh_asset_cat($newid, $orgid);
188         } else {
189                 return _flesh_user_cat($newid, $orgid);
190         }
191 }
192
193
194 sub _flesh_user_cat {
195         my $id = shift;
196         my $orgid = shift;
197
198         my $session = OpenSRF::AppSession->create("open-ils.storage");
199         my $cat = $session->request(
200                 "open-ils.storage.direct.actor.stat_cat.retrieve",
201                 $id )->gather(1);
202
203         $cat->entries( 
204                 $session->request(
205                         "open-ils.storage.ranged.actor.stat_cat_entry.search.stat_cat.atomic",
206                         $orgid, $id )->gather(1) );
207
208         return $cat;
209 }
210
211
212 sub _flesh_asset_cat {
213         my $id = shift;
214         my $orgid = shift;
215
216         my $session = OpenSRF::AppSession->create("open-ils.storage");
217         my $cat = $session->request(
218                 "open-ils.storage.direct.asset.stat_cat.retrieve",
219                 $id )->gather(1);
220
221         $cat->entries( 
222                 $session->request(
223                         "open-ils.storage.ranged.asset.stat_cat_entry.search.stat_cat.atomic",
224                         $orgid,  $id )->gather(1) );
225
226         return $cat;
227
228 }
229
230
231 sub _create_stat_cat {
232         my( $session, $stat_cat, $method) = @_;
233         warn "Creating new stat cat with name " . $stat_cat->name . "\n";
234         $stat_cat->clear_id();
235         my $req = $session->request( $method, $stat_cat );
236         my $id = $req->gather(1);
237         if(!$id) {
238                 throw OpenSRF::EX::ERROR 
239                 ("Error creating new statistical category"); }
240
241         warn "Stat cat create returned id $id\n";
242         return $id;
243 }
244
245
246 sub _create_stat_entry {
247         my( $session, $stat_entry, $method) = @_;
248
249         warn "Creating new stat entry with value " . $stat_entry->value . "\n";
250         $stat_entry->clear_id();
251
252         my $req = $session->request($method, $stat_entry);
253         my $id = $req->gather(1);
254
255         warn "Stat entry " . Dumper($stat_entry) . "\n";        
256         
257         if(!$id) {
258                 throw OpenSRF::EX::ERROR 
259                 ("Error creating new stat cat entry"); }
260
261         warn "Stat cat entry create returned id $id\n";
262         return $id;
263 }
264
265
266 __PACKAGE__->register_method(
267         method  => "update_stat_entry",
268         api_name        => "open-ils.circ.stat_cat.actor.entry.update");
269
270 __PACKAGE__->register_method(
271         method  => "update_stat_entry",
272         api_name        => "open-ils.circ.stat_cat.asset.entry.update");
273
274 sub update_stat_entry {
275         my( $self, $client, $user_session, $entry ) = @_;
276
277
278         my $method = "open-ils.storage.direct.actor.stat_cat_entry.update";
279         my $perm = 'UPDATE_PATRON_STAT_CAT_ENTRY';
280         if($self->api_name =~ /asset/) {
281                 $method = "open-ils.storage.direct.asset.stat_cat_entry.update";
282                 $perm = 'UPDATE_COPY_STAT_CAT_ENTRY';
283         }
284
285         my( $user_obj, $evt )  = $apputils->checkses($user_session); 
286         return $evt if $evt;
287         $evt = $apputils->check_perms( $user_obj->id, $entry->owner, $perm );
288         return $evt if $evt;
289
290         my $session = $apputils->start_db_session();
291         my $req = $session->request($method, $entry); 
292         my $status = $req->gather(1);
293         $apputils->commit_db_session($session);
294         warn "stat cat entry with value " . $entry->value . " updated with status $status\n";
295         return 1;
296 }
297
298
299 __PACKAGE__->register_method(
300         method  => "update_stat",
301         api_name        => "open-ils.circ.stat_cat.actor.update");
302
303 __PACKAGE__->register_method(
304         method  => "update_stat",
305         api_name        => "open-ils.circ.stat_cat.asset.update");
306
307 sub update_stat {
308         my( $self, $client, $user_session, $cat ) = @_;
309
310         my $method = "open-ils.storage.direct.actor.stat_cat.update";
311         my $perm = 'UPDATE_PATRON_STAT_CAT';
312         if($self->api_name =~ /asset/) {
313                 $method = "open-ils.storage.direct.asset.stat_cat.update";
314                 $perm = 'UPDATE_COPY_STAT_CAT';
315         }
316
317         my( $user_obj, $evt )  = $apputils->checkses($user_session); 
318         return $evt if $evt;
319         $evt = $apputils->check_perms( $user_obj->id, $cat->owner, $perm );
320         return $evt if $evt;
321
322         my $session = $apputils->start_db_session();
323         my $req = $session->request($method, $cat); 
324         my $status = $req->gather(1);
325         $apputils->commit_db_session($session);
326         warn "stat cat with id " . $cat->id . " updated with status $status\n";
327         return 1;
328 }
329
330
331 __PACKAGE__->register_method(
332         method  => "create_stat_entry",
333         api_name        => "open-ils.circ.stat_cat.actor.entry.create");
334
335 __PACKAGE__->register_method(
336         method  => "create_stat_entry",
337         api_name        => "open-ils.circ.stat_cat.asset.entry.create");
338
339 sub create_stat_entry {
340         my( $self, $client, $user_session, $entry ) = @_;
341
342         my $method = "open-ils.storage.direct.actor.stat_cat_entry.create";
343         my $perm = 'CREATE_PATRON_STAT_CAT_ENTRY';
344         if($self->api_name =~ /asset/) {
345                 $method = "open-ils.storage.direct.asset.stat_cat_entry.create";
346                 $perm = 'CREATE_COPY_STAT_CAT_ENTRY';
347         }
348
349         my( $user_obj, $evt )  = $apputils->checkses($user_session); 
350         return $evt if $evt;
351         $evt = $apputils->check_perms( $user_obj->id, $entry->owner, $perm );
352         return $evt if $evt;
353
354         $entry->clear_id();
355         my $session = $apputils->start_db_session();
356         my $req = $session->request($method, $entry); 
357         my $status = $req->gather(1);
358         $apputils->commit_db_session($session);
359
360         warn "stat cat entry with id " . $status . " updated with status $status\n";
361         return $status;
362 }
363
364
365
366 __PACKAGE__->register_method(
367         method  => "create_stat_map",
368         api_name        => "open-ils.circ.stat_cat.actor.user_map.create");
369
370 __PACKAGE__->register_method(
371         method  => "create_stat_map",
372         api_name        => "open-ils.circ.stat_cat.asset.copy_map.create");
373
374 sub create_stat_map {
375         my( $self, $client, $user_session, $map ) = @_;
376
377
378         my ( $evt, $copy, $volume, $patron, $user_obj );
379
380         my $method = "open-ils.storage.direct.actor.stat_cat_entry_user_map.create";
381         my $ret = "open-ils.storage.direct.actor.stat_cat_entry_user_map.retrieve";
382         my $perm = 'CREATE_PATRON_STAT_CAT_ENTRY_MAP';
383         my $perm_org;
384
385         if($self->api_name =~ /asset/) {
386                 $method = "open-ils.storage.direct.asset.stat_cat_entry_copy_map.create";
387                 $ret = "open-ils.storage.direct.asset.stat_cat_entry_copy_map.retrieve";
388                 $perm = 'CREATE_COPY_STAT_CAT_ENTRY_MAP';
389                 ( $copy, $evt ) = $apputils->fetch_copy($map->owning_copy);
390                 return $evt if $evt;
391                 ( $volume, $evt ) = $apputils->fetch_callnumber($copy->call_number);
392                 return $evt if $evt;
393                 $perm_org = $volume->owning_lib;
394
395         } else {
396                 ($patron, $evt) = $apputils->fetch_user($map->target_usr);
397                 return $evt if $evt;
398                 $perm_org = $patron->home_ou;
399         }
400
401         ( $user_obj, $evt )  = $apputils->checkses($user_session); 
402         return $evt if $evt;
403         $evt = $apputils->check_perms( $user_obj->id, $perm_org, $perm );
404         return $evt if $evt;
405
406         $logger->debug( $user_obj->id . " creating new stat cat map" );
407
408         $map->clear_id();
409
410         my $session = $apputils->start_db_session();
411         my $req = $session->request($method, $map); 
412         my $newid = $req->gather(1);
413         warn "Created new stat cat map with id $newid\n";
414         $apputils->commit_db_session($session);
415
416         return $apputils->simple_scalar_request( "open-ils.storage", $ret, $newid );
417
418 }
419
420
421 __PACKAGE__->register_method(
422         method  => "update_stat_map",
423         api_name        => "open-ils.circ.stat_cat.actor.user_map.update");
424
425 __PACKAGE__->register_method(
426         method  => "update_stat_map",
427         api_name        => "open-ils.circ.stat_cat.asset.copy_map.update");
428
429 sub update_stat_map {
430         my( $self, $client, $user_session, $map ) = @_;
431
432         my ( $evt, $copy, $volume, $patron, $user_obj );
433
434         my $method = "open-ils.storage.direct.actor.stat_cat_entry_user_map.update";
435         my $perm = 'UPDATE_PATRON_STAT_ENTRY_MAP';
436         my $perm_org;
437
438         if($self->api_name =~ /asset/) {
439                 $method = "open-ils.storage.direct.asset.stat_cat_entry_copy_map.update";
440                 $perm = 'UPDATE_COPY_STAT_ENTRY_MAP';
441                 ( $copy, $evt ) = $apputils->fetch_copy($map->owning_copy);
442                 return $evt if $evt;
443                 ( $volume, $evt ) = $apputils->fetch_callnumber($copy->call_number);
444                 return $evt if $evt;
445                 $perm_org = $volume->owning_lib;
446
447         } else {
448                 ($patron, $evt) = $apputils->fetch_user($map->target_usr);
449                 return $evt if $evt;
450                 $perm_org = $patron->home_ou;
451         }
452
453
454         ( $user_obj, $evt )  = $apputils->checkses($user_session); 
455         return $evt if $evt;
456         $evt = $apputils->check_perms( $user_obj->id, $perm_org, $perm );
457         return $evt if $evt;
458
459
460         my $session = $apputils->start_db_session();
461         my $req = $session->request($method, $map); 
462         my $newid = $req->gather(1);
463         warn "Updated new stat cat map with id $newid\n";
464         $apputils->commit_db_session($session);
465
466         return $newid;
467 }
468
469
470
471 __PACKAGE__->register_method(
472         method  => "retrieve_maps",
473         api_name        => "open-ils.circ.stat_cat.actor.user_map.retrieve");
474
475 __PACKAGE__->register_method(
476         method  => "retrieve_maps",
477         api_name        => "open-ils.circ.stat_cat.asset.copy_map.retrieve");
478
479 sub retrieve_maps {
480         my( $self, $client, $user_session, $target ) = @_;
481
482
483         my( $user_obj, $evt ) = $apputils->checkses($user_session); 
484         return $evt if $evt;
485
486         my      $method = "open-ils.storage.direct.asset.stat_cat_entry_copy_map.search.owning_copy.atomic";
487         if($self->api_name =~ /actor/ ) {
488                 if(!$target) { $target = $user_obj->id; }
489                 $method = "open-ils.storage.direct.actor.stat_cat_entry_user_map.search.target_usr.atomic";
490         }
491
492         return $apputils->simple_scalar_request("open-ils.storage", $method, $target);
493 }
494
495
496
497
498 __PACKAGE__->register_method(
499         method  => "delete_stats",
500         api_name        => "open-ils.circ.stat_cat.actor.delete");
501
502 __PACKAGE__->register_method(
503         method  => "delete_stats",
504         api_name        => "open-ils.circ.stat_cat.asset.delete");
505
506 sub delete_stats {
507         my( $self, $client, $user_session, $target ) = @_;
508         
509         my $cat;
510
511         my $type = "actor";
512         my $perm = 'DELETE_PATRON_STAT_CAT';
513         if($self->api_name =~ /asset/) { 
514                 $type = "asset"; 
515                 $perm = 'DELETE_COPY_STAT_CAT';
516         }
517
518         my( $user_obj, $evt )  = $apputils->checkses($user_session); 
519         return $evt if $evt;
520
521         ( $cat, $evt ) = $apputils->fetch_stat_cat( $type, $target );
522         return $evt if $evt;
523
524         $evt = $apputils->check_perms( $user_obj->id, $cat->owner, $perm );
525         return $evt if $evt;
526
527         my $session = OpenSRF::AppSession->create("open-ils.storage");
528         return _delete_stats($session, $target, $type);
529 }
530
531 sub _delete_stats {
532         my( $session, $stat, $type) = @_;
533
534         my      $method = "open-ils.storage.direct.asset.stat_cat.delete";
535         if($type =~ /actor/ ) {
536                 $method = "open-ils.storage.direct.actor.stat_cat.delete";
537         }
538         return $session->request($method, $stat)->gather(1);
539 }
540
541
542
543 __PACKAGE__->register_method(
544         method  => "delete_entry",
545         api_name        => "open-ils.circ.stat_cat.actor.entry.delete");
546
547 __PACKAGE__->register_method(
548         method  => "delete_entry",
549         api_name        => "open-ils.circ.stat_cat.asset.entry.delete");
550
551 sub delete_entry {
552         my( $self, $client, $user_session, $target ) = @_;
553
554         my $type = "actor";
555         my $perm = 'DELETE_PATRON_STAT_CAT_ENTRY';
556         if($self->api_name =~ /asset/) { 
557                 $type = "asset"; 
558                 $perm = 'DELETE_COPY_STAT_CAT_ENTRY';
559         }
560
561         my $entry;
562         my( $user_obj, $evt )  = $apputils->checkses($user_session); 
563         return $evt if $evt;
564
565         ( $entry, $evt ) = $apputils->fetch_stat_cat_entry( $type, $target );
566         return $evt if $evt;
567
568         $evt = $apputils->check_perms( $user_obj->id, $entry->owner, $perm );
569         return $evt if $evt;
570
571         my $session = OpenSRF::AppSession->create("open-ils.storage");
572         return _delete_entry($session, $target, $type);
573 }
574
575 sub _delete_entry {
576         my( $session, $stat_entry, $type) = @_;
577
578         my      $method = "open-ils.storage.direct.asset.stat_cat_entry.delete";
579         if($type =~ /actor/ ) {
580                 $method = "open-ils.storage.direct.actor.stat_cat_entry.delete";
581         }
582
583         return $session->request($method, $stat_entry)->gather(1);
584 }
585
586
587
588 1;