]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/Application/Circ/StatCat.pm
Merge branch 'master' of git.evergreen-ils.org:Evergreen-DocBook into doc_consolidati...
[Evergreen.git] / Open-ILS / src / perlmods / lib / OpenILS / Application / Circ / StatCat.pm
1 # ---------------------------------------------------------------
2 # Copyright (C) 2006  Georgia Public Library Service 
3 # Bill Erickson <billserickson@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/OpenILS::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 my $U = $apputils;
25
26
27
28 __PACKAGE__->register_method(
29         method  => "retrieve_stat_cat_list",
30         argc    => 1,
31         api_name        => "open-ils.circ.stat_cat.actor.retrieve.batch");
32
33 __PACKAGE__->register_method(
34         method  => "retrieve_stat_cat_list",
35         argc    => 1,
36         api_name        => "open-ils.circ.stat_cat.asset.retrieve.batch");
37
38 # retrieves all of the stat cats for a given org unit
39 # if no orgid, user_session->home_ou is used
40
41 sub retrieve_stat_cat_list {
42         my( $self, $client, $user_session, @sc ) = @_;
43
44         if (ref($sc[0])) {
45                 @sc = @{$sc[0]};
46         }
47
48         my $method = "open-ils.storage.fleshed.actor.stat_cat.retrieve.batch.atomic"; 
49         if( $self->api_name =~ /asset/ ) {
50                 $method = "open-ils.storage.fleshed.asset.stat_cat.retrieve.batch.atomic"; 
51         }
52
53         my($user_obj, $evt) = $apputils->checkses($user_session); 
54     return $evt if $evt;
55
56         my $cats = $apputils->simple_scalar_request(
57                                 "open-ils.storage", $method, @sc);
58
59         return [ sort { $a->name cmp $b->name } @$cats ];
60 }
61
62 __PACKAGE__->register_method(
63         method  => "retrieve_stat_cats",
64         api_name        => "open-ils.circ.stat_cat.actor.retrieve.all");
65
66 __PACKAGE__->register_method(
67         method  => "retrieve_stat_cats",
68         api_name        => "open-ils.circ.stat_cat.asset.retrieve.all");
69
70 # retrieves all of the stat cats for a given org unit
71 # if no orgid, user_session->home_ou is used
72
73 sub retrieve_stat_cats {
74         my( $self, $client, $user_session, $orgid ) = @_;
75
76         my $method = "open-ils.storage.ranged.fleshed.actor.stat_cat.all.atomic"; 
77         if( $self->api_name =~ /asset/ ) {
78                 $method = "open-ils.storage.ranged.fleshed.asset.stat_cat.all.atomic"; 
79         }
80
81         my($user_obj, $evt) = $apputils->checkses($user_session); 
82     return $evt if $evt;
83
84         if(!$orgid) { $orgid = $user_obj->home_ou; }
85         my $cats = $apputils->simple_scalar_request(
86                                 "open-ils.storage", $method, $orgid );
87
88         return [ sort { $a->name cmp $b->name } @$cats ];
89 }
90
91
92 __PACKAGE__->register_method(
93         method  => "retrieve_ranged_intersect_stat_cats",
94         api_name        => "open-ils.circ.stat_cat.asset.multirange.intersect.retrieve");
95
96 sub retrieve_ranged_intersect_stat_cats {
97         my( $self, $client, $user_session, $orglist ) = @_;
98
99         my($user_obj, $evt) = $apputils->checkses($user_session); 
100     return $evt if $evt;
101
102         if(!$orglist) { $orglist = [ $user_obj->home_ou ]; }
103
104         # uniquify, yay!
105         my %hash = map { ($_ => 1) } @$orglist;
106         $orglist = [ keys %hash ];
107
108         warn "range: @$orglist\n";
109
110         my      $method = "open-ils.storage.multiranged.intersect.fleshed.asset.stat_cat.all.atomic";
111         return $apputils->simple_scalar_request(
112                                 "open-ils.storage", $method, $orglist );
113 }
114
115
116 __PACKAGE__->register_method(
117         method  => "retrieve_ranged_union_stat_cats",
118         api_name        => "open-ils.circ.stat_cat.asset.multirange.union.retrieve");
119
120 sub retrieve_ranged_union_stat_cats {
121         my( $self, $client, $user_session, $orglist ) = @_;
122
123         my      $method = "open-ils.storage.multiranged.union.fleshed.asset.stat_cat.all.atomic";
124         use Data::Dumper;
125         warn "Retrieving stat_cats with method $method and orgs " . Dumper($orglist) . "\n";
126
127         my($user_obj, $evt) = $apputils->checkses($user_session); 
128     return $evt if $evt;
129
130         if(!$orglist) { $orglist = [ $user_obj->home_ou ]; }
131
132         # uniquify, yay!
133         my %hash = map { ($_ => 1) } @$orglist;
134         $orglist = [ keys %hash ];
135
136         warn "range: @$orglist\n";
137
138         return $apputils->simple_scalar_request(
139                                 "open-ils.storage", $method, $orglist );
140 }
141
142
143
144 __PACKAGE__->register_method(
145         method  => "stat_cat_create",
146         api_name        => "open-ils.circ.stat_cat.asset.create");
147
148 __PACKAGE__->register_method(
149         method  => "stat_cat_create",
150         api_name        => "open-ils.circ.stat_cat.actor.create");
151
152 sub stat_cat_create {
153         my( $self, $client, $user_session, $stat_cat ) = @_;
154
155         my $method = "open-ils.storage.direct.actor.stat_cat.create";
156         my $entry_create = "open-ils.storage.direct.actor.stat_cat_entry.create";
157         my $default_entry_create = "open-ils.storage.direct.actor.stat_cat_entry_default.create";
158         my $perm = 'CREATE_PATRON_STAT_CAT';
159         my $eperm = 'CREATE_PATRON_STAT_CAT_ENTRY';
160         my $edperm = 'CREATE_PATRON_STAT_CAT_ENTRY_DEFAULT';
161
162         if($self->api_name =~ /asset/) {
163                 $method = "open-ils.storage.direct.asset.stat_cat.create";
164                 $entry_create = "open-ils.storage.direct.asset.stat_cat_entry.create";
165                 $perm = 'CREATE_COPY_STAT_CAT_ENTRY';
166         }
167
168         #my $user_obj = $apputils->check_user_session($user_session); 
169         #my $orgid = $user_obj->home_ou();
170         my( $user_obj, $evt ) = $apputils->checkses($user_session);
171         return $evt if $evt;
172         $evt = $apputils->check_perms($user_obj->id, $stat_cat->owner, $perm);
173         return $evt if $evt;
174
175         if($stat_cat->entries) {
176                 $evt = $apputils->check_perms($user_obj->id, $stat_cat->owner, $eperm);
177                 return $evt if $evt;
178         }
179
180
181         my $session = $apputils->start_db_session();
182         $apputils->set_audit_info($session, $user_session, $user_obj->id, $user_obj->wsid);
183         my $newid = _create_stat_cat($session, $stat_cat, $method);
184
185         if( ref($stat_cat->entries) ) {
186                 for my $entry (@{$stat_cat->entries}) {
187                         $entry->stat_cat($newid);
188                         my $entry_id = _create_stat_entry($session, $entry, $entry_create);
189                         if( $self->api_name =~ /actor/ && ref($entry->default_entries) ) {
190                                 $evt = $apputils->check_perms($user_obj->id, $stat_cat->owner, $edperm);
191                                 return $evt if $evt;
192
193                                 for my $default_entry (@{$entry->default_entries}) {
194                                         $default_entry->stat_cat_entry($entry_id);
195                                         _create_stat_entry_default($session, $default_entry, $default_entry_create);
196                                 }
197                         }
198                 }
199         }
200
201         $apputils->commit_db_session($session);
202
203         $logger->debug("Stat cat creation successful with id $newid");
204
205         my $orgid = $user_obj->home_ou;
206         if( $self->api_name =~ /asset/ ) {
207                 return _flesh_asset_cat($newid, $orgid);
208         } else {
209                 return _flesh_user_cat($newid, $orgid);
210         }
211 }
212
213
214 sub _flesh_user_cat {
215         my $id = shift;
216         my $orgid = shift;
217
218         my $session = OpenSRF::AppSession->create("open-ils.storage");
219         my $cat = $session->request(
220                 "open-ils.storage.direct.actor.stat_cat.retrieve",
221                 $id )->gather(1);
222
223         $cat->entries( 
224                 $session->request(
225                         "open-ils.storage.ranged.actor.stat_cat_entry.search.stat_cat.atomic",
226                         $orgid, $id )->gather(1) );
227
228         return $cat;
229 }
230
231
232 sub _flesh_asset_cat {
233         my $id = shift;
234         my $orgid = shift;
235
236         my $session = OpenSRF::AppSession->create("open-ils.storage");
237         my $cat = $session->request(
238                 "open-ils.storage.direct.asset.stat_cat.retrieve",
239                 $id )->gather(1);
240
241         $cat->entries( 
242                 $session->request(
243                         "open-ils.storage.ranged.asset.stat_cat_entry.search.stat_cat.atomic",
244                         $orgid,  $id )->gather(1) );
245
246         return $cat;
247
248 }
249
250
251 sub _create_stat_cat {
252         my( $session, $stat_cat, $method) = @_;
253         warn "Creating new stat cat with name " . $stat_cat->name . "\n";
254         $stat_cat->clear_id();
255         my $req = $session->request( $method, $stat_cat );
256         my $id = $req->gather(1);
257         if(!$id) {
258                 throw OpenSRF::EX::ERROR 
259                 ("Error creating new statistical category"); }
260
261         warn "Stat cat create returned id $id\n";
262         return $id;
263 }
264
265
266 sub _create_stat_entry {
267         my( $session, $stat_entry, $method) = @_;
268
269         warn "Creating new stat entry with value " . $stat_entry->value . "\n";
270         $stat_entry->clear_id();
271
272         my $req = $session->request($method, $stat_entry);
273         my $id = $req->gather(1);
274
275         warn "Stat entry " . Dumper($stat_entry) . "\n";        
276         
277         if(!$id) {
278                 throw OpenSRF::EX::ERROR 
279                 ("Error creating new stat cat entry"); }
280
281         warn "Stat cat entry create returned id $id\n";
282         return $id;
283 }
284
285
286 __PACKAGE__->register_method(
287         method  => "update_stat_entry",
288         api_name        => "open-ils.circ.stat_cat.actor.entry.update");
289
290 __PACKAGE__->register_method(
291         method  => "update_stat_entry",
292         api_name        => "open-ils.circ.stat_cat.asset.entry.update");
293
294 sub update_stat_entry {
295         my( $self, $client, $user_session, $entry ) = @_;
296
297
298         my $method = "open-ils.storage.direct.actor.stat_cat_entry.update";
299         my $perm = 'UPDATE_PATRON_STAT_CAT_ENTRY';
300         if($self->api_name =~ /asset/) {
301                 $method = "open-ils.storage.direct.asset.stat_cat_entry.update";
302                 $perm = 'UPDATE_COPY_STAT_CAT_ENTRY';
303         }
304
305         my( $user_obj, $evt )  = $apputils->checkses($user_session); 
306         return $evt if $evt;
307         $evt = $apputils->check_perms( $user_obj->id, $entry->owner, $perm );
308         return $evt if $evt;
309
310         my $session = $apputils->start_db_session();
311         $apputils->set_audit_info($session, $user_session, $user_obj->id, $user_obj->wsid);
312         my $req = $session->request($method, $entry); 
313         my $status = $req->gather(1);
314         $apputils->commit_db_session($session);
315         warn "stat cat entry with value " . $entry->value . " updated with status $status\n";
316         return 1;
317 }
318
319 sub _update_stat_entry_default {
320     my( $session, $default_entry, $method) = @_;
321
322     warn "Updating new default stat entry for stat_cat " . $default_entry->stat_cat . 
323         " for org unit " . $default_entry->owner .
324         " with new entry id " . $default_entry->stat_cat_entry . "\n";
325
326     my $req = $session->request($method, $default_entry);
327     my $status = $req->gather(1);
328
329     warn "Default stat entry " . Dumper($default_entry) . "\n"; 
330         
331     if(!$status) {
332         throw OpenSRF::EX::ERROR 
333         ("Error updating default stat cat entry"); }
334
335     warn "Default stat cat entry update returned status $status\n";
336     return $status;
337 }
338
339 __PACKAGE__->register_method(
340         method  => "update_stat",
341         api_name        => "open-ils.circ.stat_cat.actor.update");
342
343 __PACKAGE__->register_method(
344         method  => "update_stat",
345         api_name        => "open-ils.circ.stat_cat.asset.update");
346
347 sub update_stat {
348         my( $self, $client, $user_session, $cat ) = @_;
349
350         my $method = "open-ils.storage.direct.actor.stat_cat.update";
351         my $perm = 'UPDATE_PATRON_STAT_CAT';
352         if($self->api_name =~ /asset/) {
353                 $method = "open-ils.storage.direct.asset.stat_cat.update";
354                 $perm = 'UPDATE_COPY_STAT_CAT';
355         }
356
357         my( $user_obj, $evt )  = $apputils->checkses($user_session); 
358         return $evt if $evt;
359         $evt = $apputils->check_perms( $user_obj->id, $cat->owner, $perm );
360         return $evt if $evt;
361
362         my $session = $apputils->start_db_session();
363         $apputils->set_audit_info($session, $user_session, $user_obj->id, $user_obj->wsid);
364         my $req = $session->request($method, $cat); 
365         my $status = $req->gather(1);
366         $apputils->commit_db_session($session);
367         warn "stat cat with id " . $cat->id . " updated with status $status\n";
368         return 1;
369 }
370
371
372 __PACKAGE__->register_method(
373         method  => "create_stat_entry",
374         api_name        => "open-ils.circ.stat_cat.actor.entry.create");
375
376 __PACKAGE__->register_method(
377         method  => "create_stat_entry",
378         api_name        => "open-ils.circ.stat_cat.asset.entry.create");
379
380 sub create_stat_entry {
381         my( $self, $client, $user_session, $entry ) = @_;
382
383         my $method = "open-ils.storage.direct.actor.stat_cat_entry.create";
384         my $default_entry_create = "open-ils.storage.direct.actor.stat_cat_entry_default.create";
385         my $default_entry_update = "open-ils.storage.direct.actor.stat_cat_entry_default.update";
386         my $perm = 'CREATE_PATRON_STAT_CAT_ENTRY';
387         my $edperm = 'CREATE_PATRON_STAT_CAT_ENTRY_DEFAULT';
388         my $edperm_update = 'UPDATE_PATRON_STAT_CAT_ENTRY_DEFAULT';
389         my $type = 'actor';
390         if($self->api_name =~ /asset/) {
391                 $method = "open-ils.storage.direct.asset.stat_cat_entry.create";
392                 $perm = 'CREATE_COPY_STAT_CAT_ENTRY';
393                 $type = 'asset';
394         }
395
396         my( $user_obj, $evt )  = $apputils->checkses($user_session); 
397         return $evt if $evt;
398         $evt = $apputils->check_perms( $user_obj->id, $entry->owner, $perm );
399         return $evt if $evt;
400
401         my $session = $apputils->start_db_session();
402         $apputils->set_audit_info($session, $user_session, $user_obj->id, $user_obj->wsid);
403         my $newid = _create_stat_entry($session, $entry, $method);
404
405         if( $self->api_name =~ /actor/ && ref($entry->default_entries) ) {
406                 $evt = $apputils->check_perms($user_obj->id, $entry->owner, $edperm);
407                 return $evt if $evt;
408
409                 for my $default_entry (@{$entry->default_entries}) {
410                         $default_entry->stat_cat_entry($newid);
411                         my $target;
412                         ($target, $evt) = $apputils->fetch_stat_cat_entry_default_by_stat_cat_and_org($type, 
413                                                                                                         $default_entry->stat_cat, 
414                                                                                                         $default_entry->owner);
415                         if( $target ) {
416                                 $evt = $apputils->check_perms($user_obj->id, $default_entry->owner, $edperm_update);
417                                 return $evt if $evt;
418                                 $target->stat_cat_entry($newid);
419                                 _update_stat_entry_default($session, $target, $default_entry_update);
420                         } else {
421                                 _create_stat_entry_default($session, $default_entry, $default_entry_create);
422                         }
423                 }
424         }
425
426         $apputils->commit_db_session($session);
427
428         $logger->info("created stat cat entry $newid");
429
430         return $newid;
431 }
432
433 __PACKAGE__->register_method(
434     method => "create_stat_entry_default",
435     api_name => "open-ils.circ.stat_cat.actor.entry.default.create");
436
437
438 sub create_stat_entry_default {
439     my( $self, $client, $user_session, $default_entry ) = @_;
440
441     my $create_method = "open-ils.storage.direct.actor.stat_cat_entry_default.create";
442     my $update_method = "open-ils.storage.direct.actor.stat_cat_entry_default.update";
443     my $create_perm = 'UPDATE_PATRON_STAT_CAT_ENTRY_DEFAULT';
444     my $update_perm = 'CREATE_PATRON_STAT_CAT_ENTRY_DEFAULT';
445     my $type = 'actor';
446     my ($target, $id);
447
448     my( $user_obj, $evt )  = $apputils->checkses($user_session); 
449     return $evt if $evt;
450
451     my $session = $apputils->start_db_session();
452
453     ($target, $evt) = $apputils->fetch_stat_cat_entry_default_by_stat_cat_and_org(
454         $type, 
455         $default_entry->stat_cat, 
456         $default_entry->owner);
457     if( $target ) {
458         $evt = $apputils->check_perms($user_obj->id, $default_entry->owner, $update_perm);
459         return $evt if $evt;
460         $id = $target->id;
461         $default_entry->id($id);
462         _update_stat_entry_default($session, $default_entry, $update_method);
463         $logger->info("updated stat cat default entry $id");
464     } else {
465         $evt = $apputils->check_perms($user_obj->id, $default_entry->owner, $create_perm);
466         return $evt if $evt;
467         $id = _create_stat_entry_default($session, $default_entry, $create_method);
468         $logger->info("created stat cat default entry $id");
469     }
470
471     $apputils->commit_db_session($session);
472
473     return $id;
474 }
475
476 sub _create_stat_entry_default {
477     my( $session, $stat_entry_default, $method) = @_;
478
479     warn "Creating new default stat entry for stat_cat " . $stat_entry_default->stat_cat . "\n";
480     $stat_entry_default->clear_id();
481
482     my $req = $session->request($method, $stat_entry_default);
483     my $id = $req->gather(1);
484
485     warn "Default stat entry " . Dumper($stat_entry_default) . "\n";    
486
487     if(!$id) {
488         throw OpenSRF::EX::ERROR 
489         ("Error creating new default stat cat entry"); }
490
491     warn "Default stat cat entry create returned id $id\n";
492     return $id;
493 }
494
495 __PACKAGE__->register_method(
496         method  => "create_stat_map",
497         api_name        => "open-ils.circ.stat_cat.actor.user_map.create");
498
499 __PACKAGE__->register_method(
500         method  => "create_stat_map",
501         api_name        => "open-ils.circ.stat_cat.asset.copy_map.create");
502
503 sub create_stat_map {
504         my( $self, $client, $user_session, $map ) = @_;
505
506
507         my ( $evt, $copy, $volume, $patron, $user_obj );
508
509         my $method = "open-ils.storage.direct.actor.stat_cat_entry_user_map.create";
510         my $ret = "open-ils.storage.direct.actor.stat_cat_entry_user_map.retrieve";
511         my $perm = 'CREATE_PATRON_STAT_CAT_ENTRY_MAP';
512         my $perm_org;
513
514         if($self->api_name =~ /asset/) {
515                 $method = "open-ils.storage.direct.asset.stat_cat_entry_copy_map.create";
516                 $ret = "open-ils.storage.direct.asset.stat_cat_entry_copy_map.retrieve";
517                 $perm = 'CREATE_COPY_STAT_CAT_ENTRY_MAP';
518                 ( $copy, $evt ) = $apputils->fetch_copy($map->owning_copy);
519                 return $evt if $evt;
520                 ( $volume, $evt ) = $apputils->fetch_callnumber($copy->call_number);
521                 return $evt if $evt;
522                 $perm_org = $volume->owning_lib;
523
524         } else {
525                 ($patron, $evt) = $apputils->fetch_user($map->target_usr);
526                 return $evt if $evt;
527                 $perm_org = $patron->home_ou;
528         }
529
530         ( $user_obj, $evt )  = $apputils->checkses($user_session); 
531         return $evt if $evt;
532         $evt = $apputils->check_perms( $user_obj->id, $perm_org, $perm );
533         return $evt if $evt;
534
535         $logger->debug( $user_obj->id . " creating new stat cat map" );
536
537         $map->clear_id();
538
539         my $session = $apputils->start_db_session();
540         $apputils->set_audit_info($session, $user_session, $user_obj->id, $user_obj->wsid);
541         my $req = $session->request($method, $map); 
542         my $newid = $req->gather(1);
543         warn "Created new stat cat map with id $newid\n";
544         $apputils->commit_db_session($session);
545
546         return $apputils->simple_scalar_request( "open-ils.storage", $ret, $newid );
547
548 }
549
550
551 __PACKAGE__->register_method(
552         method  => "update_stat_map",
553         api_name        => "open-ils.circ.stat_cat.actor.user_map.update");
554
555 __PACKAGE__->register_method(
556         method  => "update_stat_map",
557         api_name        => "open-ils.circ.stat_cat.asset.copy_map.update");
558
559 sub update_stat_map {
560         my( $self, $client, $user_session, $map ) = @_;
561
562         my ( $evt, $copy, $volume, $patron, $user_obj );
563
564         my $method = "open-ils.storage.direct.actor.stat_cat_entry_user_map.update";
565         my $perm = 'UPDATE_PATRON_STAT_ENTRY_MAP';
566         my $perm_org;
567
568         if($self->api_name =~ /asset/) {
569                 $method = "open-ils.storage.direct.asset.stat_cat_entry_copy_map.update";
570                 $perm = 'UPDATE_COPY_STAT_ENTRY_MAP';
571                 ( $copy, $evt ) = $apputils->fetch_copy($map->owning_copy);
572                 return $evt if $evt;
573                 ( $volume, $evt ) = $apputils->fetch_callnumber($copy->call_number);
574                 return $evt if $evt;
575                 $perm_org = $volume->owning_lib;
576
577         } else {
578                 ($patron, $evt) = $apputils->fetch_user($map->target_usr);
579                 return $evt if $evt;
580                 $perm_org = $patron->home_ou;
581         }
582
583
584         ( $user_obj, $evt )  = $apputils->checkses($user_session); 
585         return $evt if $evt;
586         $evt = $apputils->check_perms( $user_obj->id, $perm_org, $perm );
587         return $evt if $evt;
588
589
590         my $session = $apputils->start_db_session();
591         $apputils->set_audit_info($session, $user_session, $user_obj->id, $user_obj->wsid);
592         my $req = $session->request($method, $map); 
593         my $newid = $req->gather(1);
594         warn "Updated new stat cat map with id $newid\n";
595         $apputils->commit_db_session($session);
596
597         return $newid;
598 }
599
600
601
602 __PACKAGE__->register_method(
603         method  => "retrieve_maps",
604         api_name        => "open-ils.circ.stat_cat.actor.user_map.retrieve");
605
606 __PACKAGE__->register_method(
607         method  => "retrieve_maps",
608         api_name        => "open-ils.circ.stat_cat.asset.copy_map.retrieve");
609
610 sub retrieve_maps {
611         my( $self, $client, $user_session, $target ) = @_;
612
613
614         my( $user_obj, $evt ) = $apputils->checkses($user_session); 
615         return $evt if $evt;
616
617         my      $method = "open-ils.storage.direct.asset.stat_cat_entry_copy_map.search.owning_copy.atomic";
618         if($self->api_name =~ /actor/ ) {
619                 if(!$target) { $target = $user_obj->id; }
620                 $method = "open-ils.storage.direct.actor.stat_cat_entry_user_map.search.target_usr.atomic";
621         }
622
623         return $apputils->simple_scalar_request("open-ils.storage", $method, $target);
624 }
625
626
627
628
629 __PACKAGE__->register_method(
630         method  => "delete_stats",
631         api_name        => "open-ils.circ.stat_cat.actor.delete");
632
633 __PACKAGE__->register_method(
634         method  => "delete_stats",
635         api_name        => "open-ils.circ.stat_cat.asset.delete");
636
637 sub delete_stats {
638         my( $self, $client, $user_session, $target ) = @_;
639         
640         my $cat;
641
642         my $type = "actor";
643         my $perm = 'DELETE_PATRON_STAT_CAT';
644         if($self->api_name =~ /asset/) { 
645                 $type = "asset"; 
646                 $perm = 'DELETE_COPY_STAT_CAT';
647         }
648
649         my( $user_obj, $evt )  = $apputils->checkses($user_session); 
650         return $evt if $evt;
651
652         ( $cat, $evt ) = $apputils->fetch_stat_cat( $type, $target );
653         return $evt if $evt;
654
655         $evt = $apputils->check_perms( $user_obj->id, $cat->owner, $perm );
656         return $evt if $evt;
657
658         my $session = OpenSRF::AppSession->create("open-ils.storage");
659         return _delete_stats($session, $target, $type);
660 }
661
662 sub _delete_stats {
663         my( $session, $stat, $type) = @_;
664
665         my      $method = "open-ils.storage.direct.asset.stat_cat.delete";
666         if($type =~ /actor/ ) {
667                 $method = "open-ils.storage.direct.actor.stat_cat.delete";
668         }
669         return $session->request($method, $stat)->gather(1);
670 }
671
672
673
674 __PACKAGE__->register_method(
675         method  => "delete_entry",
676         api_name        => "open-ils.circ.stat_cat.actor.entry.delete");
677
678 __PACKAGE__->register_method(
679         method  => "delete_entry",
680         api_name        => "open-ils.circ.stat_cat.asset.entry.delete");
681
682 sub delete_entry {
683         my( $self, $client, $user_session, $target ) = @_;
684
685         my $type = "actor";
686         my $perm = 'DELETE_PATRON_STAT_CAT_ENTRY';
687         if($self->api_name =~ /asset/) { 
688                 $type = "asset"; 
689                 $perm = 'DELETE_COPY_STAT_CAT_ENTRY';
690         }
691
692         my $entry;
693         my( $user_obj, $evt )  = $apputils->checkses($user_session); 
694         return $evt if $evt;
695
696         ( $entry, $evt ) = $apputils->fetch_stat_cat_entry( $type, $target );
697         return $evt if $evt;
698
699         $evt = $apputils->check_perms( $user_obj->id, $entry->owner, $perm );
700         return $evt if $evt;
701
702         my $session = OpenSRF::AppSession->create("open-ils.storage");
703         return _delete_entry($session, $target, $type);
704 }
705
706 sub _delete_entry {
707         my( $session, $stat_entry, $type) = @_;
708
709         my      $method = "open-ils.storage.direct.asset.stat_cat_entry.delete";
710         if($type =~ /actor/ ) {
711                 $method = "open-ils.storage.direct.actor.stat_cat_entry.delete";
712         }
713
714         return $session->request($method, $stat_entry)->gather(1);
715 }
716
717
718 __PACKAGE__->register_method(
719     method => "delete_entry_default",
720     api_name => "open-ils.circ.stat_cat.actor.entry.default.delete");
721
722 sub delete_entry_default {
723     my( $self, $client, $user_session, $target ) = @_;
724
725     my $type = "actor";
726     my $perm = 'DELETE_PATRON_STAT_CAT_ENTRY_DEFAULT';
727
728     my $default_entry;
729     my( $user_obj, $evt )  = $apputils->checkses($user_session); 
730     return $evt if $evt;
731
732     ( $default_entry, $evt ) = $apputils->fetch_stat_cat_entry_default( $type, $target );
733     return $evt if $evt;
734
735     $evt = $apputils->check_perms( $user_obj->id, $default_entry->owner, $perm );
736     return $evt if $evt;
737
738     my $session = OpenSRF::AppSession->create("open-ils.storage");
739     return _delete_entry_default($session, $target, $type);
740 }
741
742 sub _delete_entry_default {
743     my( $session, $stat_entry, $type) = @_;
744
745     my $method = "open-ils.storage.direct.actor.stat_cat_entry_default.delete";
746     if($type =~ /asset/ ) {
747         $method = "open-ils.storage.direct.asset.stat_cat_entry_default.delete";
748     }
749
750     return $session->request($method, $stat_entry)->gather(1);
751 }
752
753 __PACKAGE__->register_method(
754         method => 'fetch_stats_by_copy',
755         api_name        => 'open-ils.circ.asset.stat_cat_entries.fleshed.retrieve_by_copy',
756 );
757
758
759 sub fetch_stats_by_copy {
760         my( $self, $conn, $args ) = @_;
761
762         my @entries;
763
764         if( $$args{public} ) {
765                 my $maps = $U->cstorereq(
766                         'open-ils.cstore.direct.asset.stat_cat_entry_copy_map.search.atomic', { owning_copy => $$args{copyid} });
767
768
769                 warn "here\n";
770                 for my $map (@$maps) {
771
772                         warn "map ".$map->id."\n";
773                         warn "map ".$map->stat_cat_entry."\n";
774
775                         my $entry = $U->cstorereq(
776                                 'open-ils.cstore.direct.asset.stat_cat_entry.retrieve', $map->stat_cat_entry);
777
778                         warn "Found entry ".$entry->id."\n";
779
780                         my $cat = $U->cstorereq(
781                                 'open-ils.cstore.direct.asset.stat_cat.retrieve', $entry->stat_cat );
782                         $entry->stat_cat( $cat );
783                         push( @entries, $entry );
784                 }
785         }
786
787         return \@entries;
788 }
789
790 __PACKAGE__->register_method(
791     method => 'retrieve_entry_default',
792     api_name => "open-ils.circ.stat_cat.actor.entry_default.ancestor_default",
793 );
794
795 sub retrieve_entry_default {
796     my( $self, $client, $user_session, $orgid, $stat_cat ) = @_;
797         
798     my $method = "open-ils.storage.actor.stat_cat_entry_default.ancestor.retrieve.atomic";
799
800     return $apputils->simple_scalar_request( "open-ils.storage", $method, $orgid, $stat_cat);
801 }
802
803
804
805 1;