1 package FS::export_svc;
5 use FS::Record qw( qsearch qsearchs dbh );
8 use FS::svc_export_machine;
10 @ISA = qw(FS::Record);
14 FS::export_svc - Object methods for export_svc records
20 $record = new FS::export_svc \%hash;
21 $record = new FS::export_svc { 'column' => 'value' };
23 $error = $record->insert;
25 $error = $new_record->replace($old_record);
27 $error = $record->delete;
29 $error = $record->check;
33 An FS::export_svc object links a service definition (see L<FS::part_svc>) to
34 an export (see L<FS::part_export>). FS::export_svc inherits from FS::Record.
35 The following fields are currently supported:
39 =item exportsvcnum - primary key
41 =item exportnum - export (see L<FS::part_export>)
43 =item svcpart - service definition (see L<FS::part_svc>)
53 Creates a new record. To add the record to the database, see L<"insert">.
55 Note that this stores the hash reference, not a distinct copy of the hash it
56 points to. You can ask the object for a copy with the I<hash> method.
60 # the new method can be inherited from FS::Record, if a table method is defined
62 sub table { 'export_svc'; }
64 =item insert [ JOB, OFFSET, MULTIPLIER ]
66 Adds this record to the database. If there is an error, returns the error,
67 otherwise returns false.
69 TODOC: JOB, OFFSET, MULTIPLIER
75 my( $job, $offset, $mult ) = ( '', 0, 100);
77 $offset = shift if @_;
80 local $SIG{HUP} = 'IGNORE';
81 local $SIG{INT} = 'IGNORE';
82 local $SIG{QUIT} = 'IGNORE';
83 local $SIG{TERM} = 'IGNORE';
84 local $SIG{TSTP} = 'IGNORE';
85 local $SIG{PIPE} = 'IGNORE';
87 my $oldAutoCommit = $FS::UID::AutoCommit;
88 local $FS::UID::AutoCommit = 0;
91 my $error = $self->check;
92 return $error if $error;
94 #check for duplicates!
96 my $svcdb = $self->part_svc->svcdb;
97 if ( $svcdb eq 'svc_acct' ) {
99 if ( $self->part_export->nodomain =~ /^Y/i ) {
101 label => 'usernames',
102 method => 'username',
103 sortby => sub { $a cmp $b },
107 label => 'username@domain',
110 my($auser, $adomain) = split('@', $a);
111 my($buser, $bdomain) = split('@', $b);
112 $adomain cmp $bdomain || $auser cmp $buser;
117 unless ( $self->part_svc->part_svc_column('uid')->columnflag eq 'F' ) {
121 sortby => sub { $a <=> $b },
125 } elsif ( $svcdb eq 'svc_domain' ) {
129 sortby => sub { $a cmp $b },
132 warn "WARNING: No duplicate checking done on merge of $svcdb exports";
138 my $percheck = $mult / scalar(@checks);
140 foreach my $check ( @checks ) {
143 $error = $job->update_statustext(int( $offset + ($done+.33) *$percheck ));
145 $dbh->rollback if $oldAutoCommit;
150 my @current_svc = $self->part_export->svc_x;
151 #warn "current: ". scalar(@current_svc). " $current_svc[0]\n";
154 $error = $job->update_statustext(int( $offset + ($done+.67) *$percheck ));
156 $dbh->rollback if $oldAutoCommit;
161 my @new_svc = $self->part_svc->svc_x;
162 #warn "new: ". scalar(@new_svc). " $new_svc[0]\n";
165 $error = $job->update_statustext(int( $offset + ($done+1) *$percheck ));
167 $dbh->rollback if $oldAutoCommit;
172 my $method = $check->{'method'};
173 my %cur_svc = map { $_->$method() => $_ } @current_svc;
174 my @dup_svc = grep { $cur_svc{$_->$method()} } @new_svc;
175 #my @diff_customer = grep {
176 # $_->cust_pkg->custnum != $cur_svc{$_->$method()}->cust_pkg->custnum
181 if ( @dup_svc ) { #aye, that's the rub
182 #error out for now, eventually accept different options of adjustments
183 # to make to allow us to continue forward
184 $dbh->rollback if $oldAutoCommit;
186 my @diff_customer_svc = grep {
187 my $cust_pkg = $_->cust_svc->cust_pkg;
188 my $custnum = $cust_pkg ? $cust_pkg->custnum : 0;
189 my $other_cust_pkg = $cur_svc{$_->$method()}->cust_svc->cust_pkg;
190 my $other_custnum = $other_cust_pkg ? $other_cust_pkg->custnum : 0;
191 $custnum != $other_custnum;
194 my $label = $check->{'label'};
195 my $sortby = $check->{'sortby'};
196 return "Can't export ".
197 $self->part_svc->svcpart.':'.$self->part_svc->svc. " service to ".
198 $self->part_export->exportnum.':'.$self->part_export->exporttype.
199 ' on '. $self->part_export->machine.
200 ' : '. scalar(@dup_svc). " duplicate $label".
201 ' ('. scalar(@diff_customer_svc). " from different customers)".
202 ": ". join(', ', sort $sortby map { $_->$method() } @dup_svc )
203 #": ". join(', ', sort $sortby map { $_->$method() } @diff_customer_svc )
210 } #end of duplicate check, whew
212 $error = $self->SUPER::insert;
214 my $part_export = $self->part_export;
215 if ( !$error and $part_export->default_machine ) {
216 foreach my $cust_svc ( $self->part_svc->cust_svc ) {
217 my $svc_export_machine = FS::svc_export_machine->new({
218 'exportnum' => $self->exportnum,
219 'svcnum' => $cust_svc->svcnum,
220 'machinenum' => $part_export->default_machine,
222 $error ||= $svc_export_machine->insert;
227 $dbh->rollback if $oldAutoCommit;
231 # if ( $self->part_svc->svcdb eq 'svc_acct' ) {
233 # if ( $self->part_export->nodomain =~ /^Y/i ) {
235 # select username from svc_acct where svcpart = $svcpart
236 # group by username having count(*) > 1;
240 # select username, domain
242 # join svc_domain on ( svc_acct.domsvc = svc_domain.svcnum )
243 # group by username, domain having count(*) > 1;
247 # } elsif ( $self->part_svc->svcdb eq 'svc_domain' ) {
249 # #similar but easier domain checking one
254 # map { $_->part_svc }
255 # grep { $_->svcpart != $self->svcpart }
256 # $self->part_export->export_svc;
258 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
264 Delete this record from the database.
271 my $oldAutoCommit = $FS::UID::AutoCommit;
272 local $FS::UID::AutoCommit = 0;
274 my $error = $self->SUPER::delete;
275 foreach ($self->svc_export_machine) {
276 $error ||= $_->delete;
279 $dbh->rollback if $oldAutoCommit;
282 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
286 =item replace OLD_RECORD
288 Replaces the OLD_RECORD with this one in the database. If there is an error,
289 returns the error, otherwise returns false.
293 # the replace method can be inherited from FS::Record
297 Checks all fields to make sure this is a valid record. If there is
298 an error, returns the error, otherwise returns false. Called by the insert
303 # the check method should currently be supplied - FS::Record contains some
304 # data checking routines
309 $self->ut_numbern('exportsvcnum')
310 || $self->ut_number('exportnum')
311 || $self->ut_foreign_key('exportnum', 'part_export', 'exportnum')
312 || $self->ut_number('svcpart')
313 || $self->ut_foreign_key('svcpart', 'part_svc', 'svcpart')
314 || $self->SUPER::check
320 Returns the FS::part_export object (see L<FS::part_export>).
326 qsearchs( 'part_export', { 'exportnum' => $self->exportnum } );
331 Returns the FS::part_svc object (see L<FS::part_svc>).
337 qsearchs( 'part_svc', { 'svcpart' => $self->svcpart } );
340 =item svc_export_machine
342 Returns all export hostname records (L<FS::svc_export_machine>) for this
343 combination of svcpart and exportnum.
347 sub svc_export_machine {
350 'table' => 'svc_export_machine',
351 'select' => 'svc_export_machine.*',
352 'addl_from' => 'JOIN cust_svc USING (svcnum)',
353 'hashref' => { 'exportnum' => $self->exportnum },
354 'extra_sql' => ' AND cust_svc.svcpart = '.$self->svcpart,
364 L<FS::part_export>, L<FS::part_svc>, L<FS::Record>, schema.html from the base