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'; }
65 Adds this record to the database. If there is an error, returns the error,
66 otherwise returns false.
74 local $SIG{HUP} = 'IGNORE';
75 local $SIG{INT} = 'IGNORE';
76 local $SIG{QUIT} = 'IGNORE';
77 local $SIG{TERM} = 'IGNORE';
78 local $SIG{TSTP} = 'IGNORE';
79 local $SIG{PIPE} = 'IGNORE';
81 my $oldAutoCommit = $FS::UID::AutoCommit;
82 local $FS::UID::AutoCommit = 0;
85 $error = $self->check;
86 return $error if $error;
88 #check for duplicates!
90 my $svcdb = $self->part_svc->svcdb;
91 if ( $svcdb eq 'svc_acct' ) {
93 if ( $self->part_export->nodomain =~ /^Y/i ) {
97 sortby => sub { $a cmp $b },
101 label => 'username@domain',
104 my($auser, $adomain) = split('@', $a);
105 my($buser, $bdomain) = split('@', $b);
106 $adomain cmp $bdomain || $auser cmp $buser;
111 unless ( $self->part_svc->part_svc_column('uid')->columnflag eq 'F' ) {
115 sortby => sub { $a <=> $b },
119 } elsif ( $svcdb eq 'svc_domain' ) {
123 sortby => sub { $a cmp $b },
126 warn "WARNING: No duplicate checking done on merge of $svcdb exports";
129 foreach my $check ( @checks ) {
130 my @current_svc = $self->part_export->svc_x;
131 #warn "current: ". scalar(@current_svc). " $current_svc[0]\n";
132 my @new_svc = $self->part_svc->svc_x;
133 #warn "new: ". scalar(@new_svc). " $new_svc[0]\n";
134 my $method = $check->{'method'};
135 my %cur_svc = map { $_->$method() => $_ } @current_svc;
136 my @dup_svc = grep { $cur_svc{$_->$method()} } @new_svc;
137 #my @diff_customer = grep {
138 # $_->cust_pkg->custnum != $cur_svc{$_->$method()}->cust_pkg->custnum
143 if ( @dup_svc ) { #aye, that's the rub
144 #error out for now, eventually accept different options of adjustments
145 # to make to allow us to continue forward
146 $dbh->rollback if $oldAutoCommit;
148 my @diff_customer_svc = grep {
149 my $cust_pkg = $_->cust_svc->cust_pkg;
150 my $custnum = $cust_pkg ? $cust_pkg->custnum : 0;
151 my $other_cust_pkg = $cur_svc{$_->$method()}->cust_svc->cust_pkg;
152 my $other_custnum = $other_cust_pkg ? $other_cust_pkg->custnum : 0;
153 $custnum != $other_custnum;
156 my $label = $check->{'label'};
157 my $sortby = $check->{'sortby'};
158 return "Can't export ".
159 $self->part_svc->svcpart.':'.$self->part_svc->svc. " service to ".
160 $self->part_export->exportnum.':'.$self->part_export->exporttype.
161 ' on '. $self->part_export->machine.
162 ' : '. scalar(@dup_svc). " duplicate $label".
163 ' ('. scalar(@diff_customer_svc). " from different customers)".
164 #": ". join(', ', sort $sortby map { $_->$method() } @dup_svc )
165 ": ". join(', ', sort $sortby map { $_->$method() } @diff_customer_svc )
170 #end of duplicate check, whew
172 $error = $self->SUPER::insert;
174 $dbh->rollback if $oldAutoCommit;
178 # if ( $self->part_svc->svcdb eq 'svc_acct' ) {
180 # if ( $self->part_export->nodomain =~ /^Y/i ) {
182 # select username from svc_acct where svcpart = $svcpart
183 # group by username having count(*) > 1;
187 # select username, domain
189 # join svc_domain on ( svc_acct.domsvc = svc_domain.svcnum )
190 # group by username, domain having count(*) > 1;
194 # } elsif ( $self->part_svc->svcdb eq 'svc_domain' ) {
196 # #similar but easier domain checking one
201 # map { $_->part_svc }
202 # grep { $_->svcpart != $self->svcpart }
203 # $self->part_export->export_svc;
205 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
211 Delete this record from the database.
215 # the delete method can be inherited from FS::Record
217 =item replace OLD_RECORD
219 Replaces the OLD_RECORD with this one in the database. If there is an error,
220 returns the error, otherwise returns false.
224 # the replace method can be inherited from FS::Record
228 Checks all fields to make sure this is a valid record. If there is
229 an error, returns the error, otherwise returns false. Called by the insert
234 # the check method should currently be supplied - FS::Record contains some
235 # data checking routines
240 $self->ut_numbern('exportsvcnum')
241 || $self->ut_number('exportnum')
242 || $self->ut_foreign_key('exportnum', 'part_export', 'exportnum')
243 || $self->ut_number('svcpart')
244 || $self->ut_foreign_key('svcpart', 'part_svc', 'svcpart')
245 || $self->SUPER::check
251 Returns the FS::part_export object (see L<FS::part_export>).
257 qsearchs( 'part_export', { 'exportnum' => $self->exportnum } );
262 Returns the FS::part_svc object (see L<FS::part_svc>).
268 qsearchs( 'part_svc', { 'svcpart' => $self->svcpart } );
277 L<FS::part_export>, L<FS::part_svc>, L<FS::Record>, schema.html from the base