a0de03b63e9566da537518d19a2e6ae8409cf5b6
[freeside.git] / FS / FS / part_export.pm
1 package FS::part_export;
2
3 use strict;
4 use vars qw( @ISA );
5 use FS::Record qw( qsearch qsearchs dbh );
6 use FS::part_svc;
7 use FS::part_export_option;
8
9 @ISA = qw(FS::Record);
10
11 =head1 NAME
12
13 FS::part_export - Object methods for part_export records
14
15 =head1 SYNOPSIS
16
17   use FS::part_export;
18
19   $record = new FS::part_export \%hash;
20   $record = new FS::part_export { 'column' => 'value' };
21
22   ($new_record, $options) = $template_recored->clone( $svcpart );
23
24   $error = $record->insert( { 'option' => 'value' } );
25   $error = $record->insert( \$options );
26
27   $error = $new_record->replace($old_record);
28
29   $error = $record->delete;
30
31   $error = $record->check;
32
33 =head1 DESCRIPTION
34
35 An FS::part_export object represents an export of Freeside data to an external
36 provisioning system.  FS::part_export inherits from FS::Record.  The following
37 fields are currently supported:
38
39 =over 4
40
41 =item exportnum - primary key
42
43 =item svcpart - Service definition (see L<FS::part_svc>) to which this export applies
44
45 =item machine - Machine name 
46
47 =item exporttype - Export type
48
49 =item nodomain - blank or "Y" : usernames are exported to this service with no domain
50
51 =back
52
53 =head1 METHODS
54
55 =over 4
56
57 =item new HASHREF
58
59 Creates a new export.  To add the export to the database, see L<"insert">.
60
61 Note that this stores the hash reference, not a distinct copy of the hash it
62 points to.  You can ask the object for a copy with the I<hash> method.
63
64 =cut
65
66 # the new method can be inherited from FS::Record, if a table method is defined
67
68 sub table { 'part_export'; }
69
70 =item clone SVCPART
71
72 An alternate constructor.  Creates a new export by duplicating an existing
73 export.  The given svcpart is assigned to the new export.
74
75 Returns a list consisting of the new export object and a hashref of options.
76
77 =cut
78
79 sub clone {
80   my $self = shift;
81   my $class = ref($self);
82   my %hash = $self->hash;
83   $hash{'exportnum'} = '';
84   $hash{'svcpart'} = shift;
85   ( $class->new( \%hash ),
86     { map { $_->optionname => $_->optionvalue }
87         qsearch('part_export_option', { 'exportnum' => $self->exportnum } )
88     }
89   );
90 }
91
92 =item insert HASHREF
93
94 Adds this record to the database.  If there is an error, returns the error,
95 otherwise returns false.
96
97 If a hash reference of options is supplied, part_export_option records are
98 created (see L<FS::part_export_option>).
99
100 =cut
101
102 #false laziness w/queue.pm
103 sub insert {
104   my $self = shift;
105   local $SIG{HUP} = 'IGNORE';
106   local $SIG{INT} = 'IGNORE';
107   local $SIG{QUIT} = 'IGNORE';
108   local $SIG{TERM} = 'IGNORE';
109   local $SIG{TSTP} = 'IGNORE';
110   local $SIG{PIPE} = 'IGNORE';
111
112   my $oldAutoCommit = $FS::UID::AutoCommit;
113   local $FS::UID::AutoCommit = 0;
114   my $dbh = dbh;
115
116   my $error = $self->SUPER::insert;
117   if ( $error ) {
118     $dbh->rollback if $oldAutoCommit;
119     return $error;
120   }
121
122   my $options = shift;
123   foreach my $optionname ( keys %{$options} ) {
124     my $part_export_option = new FS::part_export_option ( {
125       'optionname'  => $optionname,
126       'optionvalue' => $options->{$optionname},
127     } );
128     $error = $part_export_option->insert;
129     if ( $error ) {
130       $dbh->rollback if $oldAutoCommit;
131       return $error;
132     }
133   }
134
135   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
136
137   '';
138
139 };
140
141 =item delete
142
143 Delete this record from the database.
144
145 =cut
146
147 # the delete method can be inherited from FS::Record
148
149 =item replace OLD_RECORD
150
151 Replaces the OLD_RECORD with this one in the database.  If there is an error,
152 returns the error, otherwise returns false.
153
154 =cut
155
156 # the replace method can be inherited from FS::Record
157
158 =item check
159
160 Checks all fields to make sure this is a valid export.  If there is
161 an error, returns the error, otherwise returns false.  Called by the insert
162 and replace methods.
163
164 =cut
165
166 sub check {
167   my $self = shift;
168   my $error = 
169     $self->ut_numbern('exportnum')
170     || $self->ut_number('svcpart')
171     || $self->ut_alpha('exporttype')
172   ;
173   return $error if $error;
174
175   return "Unknown svcpart: ". $self->svcpart
176     unless qsearchs( 'part_svc', { 'svcpart' => $self->svcpart } );
177
178   $self->machine =~ /^([\w\-\.]*)$/
179     or return "Illegal machine: ". $self->machine;
180   $self->machine($1);
181
182   $self->nodomain =~ /^(Y?)$/ or return "Illegal nodomain: ". $self->nodomain;
183   $self->nodomain($1);
184
185   #check exporttype?
186
187   ''; #no error
188 }
189
190 =item part_svc
191
192 Returns the service definition (see L<FS::part_svc>) for this export.
193
194 =cut
195
196 sub part_svc {
197   my $self = shift;
198   qsearchs('part_svc', { svcpart => $self->svcpart } );
199 }
200
201 =back
202
203 =head1 BUGS
204
205 Probably.
206
207 =head1 SEE ALSO
208
209 L<FS::part_export_option>, L<FS::part_svc>, L<FS::svc_acct>, L<FS::svc_domain>,
210 L<FS::svc_forward>, L<FS::Record>, schema.html from the base documentation.
211
212 =cut
213
214 1;
215