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