50075a97ccf28ac26744fc62c717d04a36879651
[freeside.git] / FS / FS / sched_item.pm
1 package FS::sched_item;
2 use base qw( FS::Record );
3
4 use strict;
5 use FS::Record qw( qsearch qsearchs dbh );
6 use FS::access_user;
7 use FS::sched_avail;
8
9 =head1 NAME
10
11 FS::sched_item - Object methods for sched_item records
12
13 =head1 SYNOPSIS
14
15   use FS::sched_item;
16
17   $record = new FS::sched_item \%hash;
18   $record = new FS::sched_item { 'column' => 'value' };
19
20   $error = $record->insert;
21
22   $error = $new_record->replace($old_record);
23
24   $error = $record->delete;
25
26   $error = $record->check;
27
28 =head1 DESCRIPTION
29
30 An FS::sched_item object represents an schedulable item, such as an installer,
31 meeting room or truck.  FS::sched_item inherits from FS::Record.  The following
32 fields are currently supported:
33
34 =over 4
35
36 =item itemnum
37
38 primary key
39
40 =item usernum
41
42 usernum
43
44 =item disabled
45
46 disabled
47
48
49 =back
50
51 =head1 METHODS
52
53 =over 4
54
55 =item new HASHREF
56
57 Creates a new item.  To add the item to the database, see L<"insert">.
58
59 Note that this stores the hash reference, not a distinct copy of the hash it
60 points to.  You can ask the object for a copy with the I<hash> method.
61
62 =cut
63
64 sub table { 'sched_item'; }
65
66 =item insert
67
68 Adds this record to the database.  If there is an error, returns the error,
69 otherwise returns false.
70
71 =item delete
72
73 Delete this record from the database.
74
75 =item replace OLD_RECORD
76
77 Replaces the OLD_RECORD with this one in the database.  If there is an error,
78 returns the error, otherwise returns false.
79
80 =item check
81
82 Checks all fields to make sure this is a valid item.  If there is
83 an error, returns the error, otherwise returns false.  Called by the insert
84 and replace methods.
85
86 =cut
87
88 sub check {
89   my $self = shift;
90
91   my $error = 
92     $self->ut_numbern('itemnum')
93     || $self->ut_foreign_keyn('usernum', 'access_user', 'usernum')
94     || $self->ut_enum('disabled', [ '', 'Y' ])
95   ;
96   return $error if $error;
97
98   $self->SUPER::check;
99 }
100
101 =item name
102
103 Returns a name for this item; either the name of the associated employee (see
104 L<FS::access_user), or the itemname field.
105
106 =cut
107
108 sub name {
109   my $self = shift;
110   my $access_user = $self->access_user;
111   $access_user ? $access_user->name : $self->itemname;
112 }
113
114 sub access_user {
115   my $self = shift;
116   qsearchs( 'access_user', { 'usernum' => $self->usernum } );
117 }
118
119 sub sched_avail {
120   my $self = shift;
121   qsearch( 'sched_avail', { 'itemnum' => $self->itemnum } );
122 }
123
124
125 =item replace_sched_avail SCHED_AVAIL, ...
126
127 Replaces the existing availability schedule with the list of passed-in
128 FS::sched_avail objects
129
130 =cut
131
132 sub replace_sched_avail {
133   my( $self, @new_sched_avail ) = @_;
134
135   my $oldAutoCommit = $FS::UID::AutoCommit;
136   local $FS::UID::AutoCommit = 0;
137   my $dbh = dbh;
138
139   foreach my $old_sched_avail ( $self->sched_avail ) {
140     my $error = $old_sched_avail->delete;
141     if ( $error ) {
142       $dbh->rollback if $oldAutoCommit;
143       return $error;
144     }
145   }
146
147   foreach my $new_sched_avail ( @new_sched_avail ) {
148     $new_sched_avail->itemnum( $self->itemnum );
149     my $error = $new_sched_avail->insert;
150     if ( $error ) {
151       $dbh->rollback if $oldAutoCommit;
152       return $error;
153     }
154   }
155
156   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
157
158   '';
159
160 }
161
162 =back
163
164 =head1 BUGS
165
166 =head1 SEE ALSO
167
168 L<FS::access_user>, L<FS::sched_avail>, L<FS::Record>
169
170 =cut
171
172 1;
173