1 package FS::export_svc;
5 use FS::Record qw( qsearch qsearchs dbh );
13 FS::export_svc - Object methods for export_svc records
19 $record = new FS::export_svc \%hash;
20 $record = new FS::export_svc { 'column' => 'value' };
22 $error = $record->insert;
24 $error = $new_record->replace($old_record);
26 $error = $record->delete;
28 $error = $record->check;
32 An FS::export_svc object links a service definition (see L<FS::part_svc>) to
33 an export (see L<FS::part_export>). FS::export_svc inherits from FS::Record.
34 The following fields are currently supported:
38 =item exportsvcnum - primary key
40 =item exportnum - export (see L<FS::part_export>)
42 =item svcpart - service definition (see L<FS::part_svc>)
52 Creates a new record. To add the record to the database, see L<"insert">.
54 Note that this stores the hash reference, not a distinct copy of the hash it
55 points to. You can ask the object for a copy with the I<hash> method.
59 # the new method can be inherited from FS::Record, if a table method is defined
61 sub table { 'export_svc'; }
63 =item insert [ JOB, OFFSET, MULTIPLIER ]
65 Adds this record to the database. If there is an error, returns the error,
66 otherwise returns false.
68 TODOC: JOB, OFFSET, MULTIPLIER
74 my( $job, $offset, $mult ) = ( '', 0, 100);
76 $offset = shift if @_;
79 local $SIG{HUP} = 'IGNORE';
80 local $SIG{INT} = 'IGNORE';
81 local $SIG{QUIT} = 'IGNORE';
82 local $SIG{TERM} = 'IGNORE';
83 local $SIG{TSTP} = 'IGNORE';
84 local $SIG{PIPE} = 'IGNORE';
86 my $oldAutoCommit = $FS::UID::AutoCommit;
87 local $FS::UID::AutoCommit = 0;
90 my $error = $self->check;
91 return $error if $error;
93 #check for duplicates!
95 my $svcdb = $self->part_svc->svcdb;
96 if ( $svcdb eq 'svc_acct' ) {
98 if ( $self->part_export->nodomain =~ /^Y/i ) {
100 label => 'usernames',
101 method => 'username',
102 sortby => sub { $a cmp $b },
106 label => 'username@domain',
109 my($auser, $adomain) = split('@', $a);
110 my($buser, $bdomain) = split('@', $b);
111 $adomain cmp $bdomain || $auser cmp $buser;
116 unless ( $self->part_svc->part_svc_column('uid')->columnflag eq 'F' ) {
120 sortby => sub { $a <=> $b },
124 } elsif ( $svcdb eq 'svc_domain' ) {
128 sortby => sub { $a cmp $b },
131 warn "WARNING: No duplicate checking done on merge of $svcdb exports";
135 my $percheck = $mult / scalar(@checks);
136 foreach my $check ( @checks ) {
139 $error = $job->update_statustext(int( $offset + ($done+.33) *$percheck ));
141 $dbh->rollback if $oldAutoCommit;
146 my @current_svc = $self->part_export->svc_x;
147 #warn "current: ". scalar(@current_svc). " $current_svc[0]\n";
150 $error = $job->update_statustext(int( $offset + ($done+.67) *$percheck ));
152 $dbh->rollback if $oldAutoCommit;
157 my @new_svc = $self->part_svc->svc_x;
158 #warn "new: ". scalar(@new_svc). " $new_svc[0]\n";
161 $error = $job->update_statustext(int( $offset + ($done+1) *$percheck ));
163 $dbh->rollback if $oldAutoCommit;
168 my $method = $check->{'method'};
169 my %cur_svc = map { $_->$method() => $_ } @current_svc;
170 my @dup_svc = grep { $cur_svc{$_->$method()} } @new_svc;
171 #my @diff_customer = grep {
172 # $_->cust_pkg->custnum != $cur_svc{$_->$method()}->cust_pkg->custnum
177 if ( @dup_svc ) { #aye, that's the rub
178 #error out for now, eventually accept different options of adjustments
179 # to make to allow us to continue forward
180 $dbh->rollback if $oldAutoCommit;
182 my @diff_customer_svc = grep {
183 my $cust_pkg = $_->cust_svc->cust_pkg;
184 my $custnum = $cust_pkg ? $cust_pkg->custnum : 0;
185 my $other_cust_pkg = $cur_svc{$_->$method()}->cust_svc->cust_pkg;
186 my $other_custnum = $other_cust_pkg ? $other_cust_pkg->custnum : 0;
187 $custnum != $other_custnum;
190 my $label = $check->{'label'};
191 my $sortby = $check->{'sortby'};
192 return "Can't export ".
193 $self->part_svc->svcpart.':'.$self->part_svc->svc. " service to ".
194 $self->part_export->exportnum.':'.$self->part_export->exporttype.
195 ' on '. $self->part_export->machine.
196 ' : '. scalar(@dup_svc). " duplicate $label".
197 ' ('. scalar(@diff_customer_svc). " from different customers)".
198 #": ". join(', ', sort $sortby map { $_->$method() } @dup_svc )
199 ": ". join(', ', sort $sortby map { $_->$method() } @diff_customer_svc )
206 #end of duplicate check, whew
208 $error = $self->SUPER::insert;
210 $dbh->rollback if $oldAutoCommit;
214 # if ( $self->part_svc->svcdb eq 'svc_acct' ) {
216 # if ( $self->part_export->nodomain =~ /^Y/i ) {
218 # select username from svc_acct where svcpart = $svcpart
219 # group by username having count(*) > 1;
223 # select username, domain
225 # join svc_domain on ( svc_acct.domsvc = svc_domain.svcnum )
226 # group by username, domain having count(*) > 1;
230 # } elsif ( $self->part_svc->svcdb eq 'svc_domain' ) {
232 # #similar but easier domain checking one
237 # map { $_->part_svc }
238 # grep { $_->svcpart != $self->svcpart }
239 # $self->part_export->export_svc;
241 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
247 Delete this record from the database.
251 # the delete method can be inherited from FS::Record
253 =item replace OLD_RECORD
255 Replaces the OLD_RECORD with this one in the database. If there is an error,
256 returns the error, otherwise returns false.
260 # the replace method can be inherited from FS::Record
264 Checks all fields to make sure this is a valid record. If there is
265 an error, returns the error, otherwise returns false. Called by the insert
270 # the check method should currently be supplied - FS::Record contains some
271 # data checking routines
276 $self->ut_numbern('exportsvcnum')
277 || $self->ut_number('exportnum')
278 || $self->ut_foreign_key('exportnum', 'part_export', 'exportnum')
279 || $self->ut_number('svcpart')
280 || $self->ut_foreign_key('svcpart', 'part_svc', 'svcpart')
281 || $self->SUPER::check
287 Returns the FS::part_export object (see L<FS::part_export>).
293 qsearchs( 'part_export', { 'exportnum' => $self->exportnum } );
298 Returns the FS::part_svc object (see L<FS::part_svc>).
304 qsearchs( 'part_svc', { 'svcpart' => $self->svcpart } );
313 L<FS::part_export>, L<FS::part_svc>, L<FS::Record>, schema.html from the base