]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/Application/Actor/ClosedDates.pm
46337f5be174019c351c0fc28b6a9d7d6badb371
[working/Evergreen.git] / Open-ILS / src / perlmods / lib / OpenILS / Application / Actor / ClosedDates.pm
1 package OpenILS::Application::Actor::ClosedDates;
2 use base 'OpenILS::Application';
3 use strict; use warnings;
4 use OpenSRF::EX qw(:try);
5 use OpenILS::Utils::CStoreEditor q/:funcs/;
6
7 sub initialize { return 1; }
8
9 __PACKAGE__->register_method( 
10     method => 'fetch_dates',
11     api_name    => 'open-ils.actor.org_unit.closed.retrieve.all',
12     signature   => q/
13         Retrieves a list of closed date object IDs
14     /
15 );
16
17 sub fetch_dates {
18     my( $self, $conn, $auth, $args ) = @_;
19
20     my $e = new_editor(authtoken=>$auth);
21     return $e->event unless $e->checkauth;
22
23     my $org = $$args{orgid} || $e->requestor->ws_ou;
24     my @date = localtime;
25     my $start = $$args{start_date} ||  #default to today 
26         ($date[5] + 1900) .'-'. ($date[4] + 1) .'-'. $date[3];
27     my $end = $$args{end_date} || '3000-01-01'; # Y3K, here I come..
28
29     my $dates = $e->search_actor_org_unit_closed_date( 
30         { 
31             close_start => { ">=" => $start }, 
32             close_end   => { "<=" => $end },
33             org_unit        => $org,
34         }, { idlist     => $$args{idlist} } ) or return $e->event;
35
36     if(!$$args{idlist} and @$dates) {
37         $dates = [ sort { $a->close_start cmp $b->close_start } @$dates ];
38     }
39
40     return $dates;
41 }
42
43 __PACKAGE__->register_method( 
44     method => 'fetch_date',
45     api_name    => 'open-ils.actor.org_unit.closed.retrieve',
46     signature   => q/
47         Retrieves a single date object
48     /
49 );
50
51 sub fetch_date {
52     my( $self, $conn, $auth, $id ) = @_;
53     my $e = new_editor(authtoken=>$auth);
54     return $e->event unless $e->checkauth;
55     my $date = $e->retrieve_actor_org_unit_closed_date($id) or return $e->event;
56     return $date;
57 }
58
59
60 __PACKAGE__->register_method( 
61     method => 'delete_date',
62     api_name    => 'open-ils.actor.org_unit.closed.delete',
63     signature   => q/
64         Removes a single date object
65     /
66 );
67
68 sub delete_date {
69     my( $self, $conn, $auth, $id ) = @_;
70     my $e = new_editor(authtoken=>$auth, xact => 1);
71     return $e->die_event unless $e->checkauth;
72     my $date = $e->retrieve_actor_org_unit_closed_date($id) or return $e->die_event;
73     return $e->die_event unless $e->allowed(
74         'actor.org_unit.closed_date.delete', $date->org_unit);
75     $e->delete_actor_org_unit_closed_date($date) or return $e->die_event;
76     $e->commit;
77     return 1;
78 }
79
80
81
82
83 __PACKAGE__->register_method( 
84     method => 'create_date',
85     api_name    => 'open-ils.actor.org_unit.closed.create',
86     signature   => q/
87         Creates a new org closed data
88     /
89 );
90
91 sub create_date {
92     my( $self, $conn, $auth, $date ) = @_;
93
94     my $e = new_editor(authtoken=>$auth, xact =>1);
95     return $e->die_event unless $e->checkauth;
96     
97     return $e->die_event unless $e->allowed(
98         'actor.org_unit.closed_date.create', $date->org_unit);
99
100     $e->create_actor_org_unit_closed_date($date) or return $e->die_event;
101
102     my $newobj = $e->retrieve_actor_org_unit_closed_date($date->id)
103         or return $e->die_event;
104
105     $e->commit;
106     return $newobj;
107 }
108
109
110 __PACKAGE__->register_method(
111     method => 'edit_date',
112     api_name    => 'open-ils.actor.org_unit.closed.update',
113     signature   => q/
114         Updates a closed date object
115     /
116 );
117
118 sub edit_date {
119     my( $self, $conn, $auth, $date ) = @_;
120     my $e = new_editor(authtoken=>$auth, xact =>1);
121     return $e->die_event unless $e->checkauth;
122     
123     # First make sure they have the right to update the selected date object
124     my $odate = $e->retrieve_actor_org_unit_closed_date($date->id) 
125         or return $e->die_event;
126
127     return $e->die_event unless $e->allowed(
128         'actor.org_unit.closed_date.update', $odate->org_unit);
129
130     $e->update_actor_org_unit_closed_date($date) or return $e->die_event;
131     $e->commit;
132
133     return 1;
134 }
135
136
137 __PACKAGE__->register_method(
138     method  => 'closed_dates_overlap',
139     api_name    => 'open-ils.actor.org_unit.closed_date.overlap',
140     signature   => q/
141         Returns an object with 'start' and 'end' fields 
142         start is the first day the org is open going backwards from 
143         'date'.  end is the next day the org is open going
144         forward from 'date'.
145         @param orgid The org unit in question
146         @param date The date to search
147     /
148 );
149 sub closed_dates_overlap {
150     my( $self, $conn, $auth, $orgid, $date ) = @_;
151     my $e = new_editor(authtoken=>$auth);
152     return $e->event unless $e->checkauth;
153     return $e->request(
154         'open-ils.storage.actor.org_unit.closed_date.overlap', $orgid, $date );
155 }
156
157
158
159
160 1;