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";
137 my $percheck = $mult / scalar(@checks);
139 foreach my $check ( @checks ) {
142 $error = $job->update_statustext(int( $offset + ($done+.33) *$percheck ));
144 $dbh->rollback if $oldAutoCommit;
149 my @current_svc = $self->part_export->svc_x;
150 #warn "current: ". scalar(@current_svc). " $current_svc[0]\n";
153 $error = $job->update_statustext(int( $offset + ($done+.67) *$percheck ));
155 $dbh->rollback if $oldAutoCommit;
160 my @new_svc = $self->part_svc->svc_x;
161 #warn "new: ". scalar(@new_svc). " $new_svc[0]\n";
164 $error = $job->update_statustext(int( $offset + ($done+1) *$percheck ));
166 $dbh->rollback if $oldAutoCommit;
171 my $method = $check->{'method'};
172 my %cur_svc = map { $_->$method() => $_ } @current_svc;
173 my @dup_svc = grep { $cur_svc{$_->$method()} } @new_svc;
174 #my @diff_customer = grep {
175 # $_->cust_pkg->custnum != $cur_svc{$_->$method()}->cust_pkg->custnum
180 if ( @dup_svc ) { #aye, that's the rub
181 #error out for now, eventually accept different options of adjustments
182 # to make to allow us to continue forward
183 $dbh->rollback if $oldAutoCommit;
185 my @diff_customer_svc = grep {
186 my $cust_pkg = $_->cust_svc->cust_pkg;
187 my $custnum = $cust_pkg ? $cust_pkg->custnum : 0;
188 my $other_cust_pkg = $cur_svc{$_->$method()}->cust_svc->cust_pkg;
189 my $other_custnum = $other_cust_pkg ? $other_cust_pkg->custnum : 0;
190 $custnum != $other_custnum;
193 my $label = $check->{'label'};
194 my $sortby = $check->{'sortby'};
195 return "Can't export ".
196 $self->part_svc->svcpart.':'.$self->part_svc->svc. " service to ".
197 $self->part_export->exportnum.':'.$self->part_export->exporttype.
198 ' on '. $self->part_export->machine.
199 ' : '. scalar(@dup_svc). " duplicate $label".
200 ' ('. scalar(@diff_customer_svc). " from different customers)".
201 ": ". join(', ', sort $sortby map { $_->$method() } @dup_svc )
202 #": ". join(', ', sort $sortby map { $_->$method() } @diff_customer_svc )
209 } #end of duplicate check, whew
211 $error = $self->SUPER::insert;
213 $dbh->rollback if $oldAutoCommit;
217 # if ( $self->part_svc->svcdb eq 'svc_acct' ) {
219 # if ( $self->part_export->nodomain =~ /^Y/i ) {
221 # select username from svc_acct where svcpart = $svcpart
222 # group by username having count(*) > 1;
226 # select username, domain
228 # join svc_domain on ( svc_acct.domsvc = svc_domain.svcnum )
229 # group by username, domain having count(*) > 1;
233 # } elsif ( $self->part_svc->svcdb eq 'svc_domain' ) {
235 # #similar but easier domain checking one
240 # map { $_->part_svc }
241 # grep { $_->svcpart != $self->svcpart }
242 # $self->part_export->export_svc;
244 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
250 Delete this record from the database.
254 # the delete method can be inherited from FS::Record
256 =item replace OLD_RECORD
258 Replaces the OLD_RECORD with this one in the database. If there is an error,
259 returns the error, otherwise returns false.
263 # the replace method can be inherited from FS::Record
267 Checks all fields to make sure this is a valid record. If there is
268 an error, returns the error, otherwise returns false. Called by the insert
273 # the check method should currently be supplied - FS::Record contains some
274 # data checking routines
279 $self->ut_numbern('exportsvcnum')
280 || $self->ut_number('exportnum')
281 || $self->ut_foreign_key('exportnum', 'part_export', 'exportnum')
282 || $self->ut_number('svcpart')
283 || $self->ut_foreign_key('svcpart', 'part_svc', 'svcpart')
284 || $self->SUPER::check
290 Returns the FS::part_export object (see L<FS::part_export>).
296 qsearchs( 'part_export', { 'exportnum' => $self->exportnum } );
301 Returns the FS::part_svc object (see L<FS::part_svc>).
307 qsearchs( 'part_svc', { 'svcpart' => $self->svcpart } );
316 L<FS::part_export>, L<FS::part_svc>, L<FS::Record>, schema.html from the base