support multiple primary keys
[freeside.git] / FS / FS / export_svc.pm
1 package FS::export_svc;
2
3 use strict;
4 use vars qw( @ISA );
5 use FS::Record qw( qsearch qsearchs dbh );
6 use FS::part_export;
7 use FS::part_svc;
8
9 @ISA = qw(FS::Record);
10
11 =head1 NAME
12
13 FS::export_svc - Object methods for export_svc records
14
15 =head1 SYNOPSIS
16
17   use FS::export_svc;
18
19   $record = new FS::export_svc \%hash;
20   $record = new FS::export_svc { 'column' => 'value' };
21
22   $error = $record->insert;
23
24   $error = $new_record->replace($old_record);
25
26   $error = $record->delete;
27
28   $error = $record->check;
29
30 =head1 DESCRIPTION
31
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:
35
36 =over 4
37
38 =item exportsvcnum - primary key
39
40 =item exportnum - export (see L<FS::part_export>)
41
42 =item svcpart - service definition (see L<FS::part_svc>)
43
44 =back
45
46 =head1 METHODS
47
48 =over 4
49
50 =item new HASHREF
51
52 Creates a new record.  To add the record to the database, see L<"insert">.
53
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.
56
57 =cut
58
59 # the new method can be inherited from FS::Record, if a table method is defined
60
61 sub table { 'export_svc'; }
62
63 =item insert [ JOB, OFFSET, MULTIPLIER ]
64
65 Adds this record to the database.  If there is an error, returns the error,
66 otherwise returns false.
67
68 TODOC: JOB, OFFSET, MULTIPLIER
69
70 =cut
71
72 sub insert {
73   my $self = shift;
74   my( $job, $offset, $mult ) = ( '', 0, 100);
75   $job = shift if @_;
76   $offset = shift if @_;
77   $mult = shift if @_;
78
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';
85
86   my $oldAutoCommit = $FS::UID::AutoCommit;
87   local $FS::UID::AutoCommit = 0;
88   my $dbh = dbh;
89
90   my $error = $self->check;
91   return $error if $error;
92
93   #check for duplicates!
94   my @checks = ();
95   my $svcdb = $self->part_svc->svcdb;
96   if ( $svcdb eq 'svc_acct' ) {
97
98     if ( $self->part_export->nodomain =~ /^Y/i ) {
99       push @checks, {
100         label  => 'usernames',
101         method => 'username',
102         sortby => sub { $a cmp $b },
103       };
104     } else {
105       push @checks, {
106         label  => 'username@domain',
107         method => 'email',
108         sortby => sub {
109                         my($auser, $adomain) = split('@', $a);
110                         my($buser, $bdomain) = split('@', $b);
111                         $adomain cmp $bdomain || $auser cmp $buser;
112                       },
113       };
114     }
115
116     unless ( $self->part_svc->part_svc_column('uid')->columnflag eq 'F' ) {
117       push @checks, {
118         label  => 'uids',
119         method => 'uid',
120         sortby => sub { $a <=> $b },
121       };
122     }
123
124   } elsif ( $svcdb eq 'svc_domain' ) {
125     push @checks, {
126       label  => 'domains',
127       method => 'domain',
128       sortby => sub { $a cmp $b },
129     };
130   } else {
131     warn "WARNING: No duplicate checking done on merge of $svcdb exports";
132   }
133
134   my $done = 0;
135   my $percheck = $mult / scalar(@checks);
136   foreach my $check ( @checks ) {
137
138     if ( $job ) {
139       $error = $job->update_statustext(int( $offset + ($done+.33) *$percheck ));
140       if ( $error ) {
141         $dbh->rollback if $oldAutoCommit;
142         return $error;
143       }
144     }
145
146     my @current_svc = $self->part_export->svc_x;
147     #warn "current: ". scalar(@current_svc). " $current_svc[0]\n";
148
149     if ( $job ) {
150       $error = $job->update_statustext(int( $offset + ($done+.67) *$percheck ));
151       if ( $error ) {
152         $dbh->rollback if $oldAutoCommit;
153         return $error;
154       }
155     }
156
157     my @new_svc = $self->part_svc->svc_x;
158     #warn "new: ". scalar(@new_svc). " $new_svc[0]\n";
159
160     if ( $job ) {
161       $error = $job->update_statustext(int( $offset + ($done+1) *$percheck ));
162       if ( $error ) {
163         $dbh->rollback if $oldAutoCommit;
164         return $error;
165       }
166     }
167
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
173     #                         } @dup_svc;
174
175
176
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;
181
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;
188       } @dup_svc;
189
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 )
200              ;
201     }
202
203     $done++;
204   }
205
206   #end of duplicate check, whew
207
208   $error = $self->SUPER::insert;
209   if ( $error ) {
210     $dbh->rollback if $oldAutoCommit;
211     return $error;
212   }
213
214 #  if ( $self->part_svc->svcdb eq 'svc_acct' ) {
215 #
216 #    if ( $self->part_export->nodomain =~ /^Y/i ) {
217 #
218 #      select username from svc_acct where svcpart = $svcpart
219 #        group by username having count(*) > 1;
220 #
221 #    } else {
222 #
223 #      select username, domain
224 #        from   svc_acct
225 #          join svc_domain on ( svc_acct.domsvc = svc_domain.svcnum )
226 #        group by username, domain having count(*) > 1;
227 #
228 #    }
229 #
230 #  } elsif ( $self->part_svc->svcdb eq 'svc_domain' ) {
231 #
232 #    #similar but easier domain checking one
233 #
234 #  } #etc.?
235 #
236 #  my @services =
237 #    map  { $_->part_svc }
238 #    grep { $_->svcpart != $self->svcpart }
239 #         $self->part_export->export_svc;
240
241   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
242   ''; #no error
243 }
244
245 =item delete
246
247 Delete this record from the database.
248
249 =cut
250
251 # the delete method can be inherited from FS::Record
252
253 =item replace OLD_RECORD
254
255 Replaces the OLD_RECORD with this one in the database.  If there is an error,
256 returns the error, otherwise returns false.
257
258 =cut
259
260 # the replace method can be inherited from FS::Record
261
262 =item check
263
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
266 and replace methods.
267
268 =cut
269
270 # the check method should currently be supplied - FS::Record contains some
271 # data checking routines
272
273 sub check {
274   my $self = shift;
275
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
282   ;
283 }
284
285 =item part_export
286
287 Returns the FS::part_export object (see L<FS::part_export>).
288
289 =cut
290
291 sub part_export {
292   my $self = shift;
293   qsearchs( 'part_export', { 'exportnum' => $self->exportnum } );
294 }
295
296 =item part_svc
297
298 Returns the FS::part_svc object (see L<FS::part_svc>).
299
300 =cut
301
302 sub part_svc {
303   my $self = shift;
304   qsearchs( 'part_svc', { 'svcpart' => $self->svcpart } );
305 }
306
307 =back
308
309 =head1 BUGS
310
311 =head1 SEE ALSO
312
313 L<FS::part_export>, L<FS::part_svc>, L<FS::Record>, schema.html from the base
314 documentation.
315
316 =cut
317
318 1;
319