first try at duplicate checking on new export associations
[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
64
65 Adds this record to the database.  If there is an error, returns the error,
66 otherwise returns false.
67
68 =cut
69
70 sub insert {
71   my $self = shift;
72   my $error;
73
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';
80
81   my $oldAutoCommit = $FS::UID::AutoCommit;
82   local $FS::UID::AutoCommit = 0;
83   my $dbh = dbh;
84
85   $error = $self->check;
86   return $error if $error;
87
88   #check for duplicates!
89
90   my $label = '';
91   my $method = '';
92   my $svcdb = $self->part_svc->svcdb;
93   if ( $svcdb eq 'svc_acct' ) { #XXX AND UID!  sheesh @method or %method not $method
94     if ( $self->part_export->nodomain =~ /^Y/i ) {
95       $label = 'usernames';
96       $method = 'username';
97     } else {
98       $label = 'username@domain';
99       $method = 'email';
100     }
101   } elsif ( $svcdb eq 'domain' ) {
102     $label = 'domains';
103     $method = 'domain';
104   } else {
105     warn "WARNING: XXX fill in this warning";
106   }
107
108   if ( $method ) {
109     my @current_svc = $self->part_export->svc_x;
110     my @new_svc = $self->part_svc->svc_x;
111     my %cur_svc = map { $_->$method() => 1 } @current_svc;
112     my @dup_svc = grep { $cur_svc{$_->method()} } @new_svc;
113
114     if ( @dup_svc ) { #aye, that's the rub
115       #error out for now, eventually accept different options of adjustments
116       # to make to allow us to continue forward
117       $dbh->rollback if $oldAutoCommit;
118       return "Can't export ".
119              $self->part_svc->svcpart.':'.$self->part_svc->svc. " service to ".
120              $self->part_export->exportnum.':'.$self->exporttype.
121                ' on '. $self->machine.
122              " : Duplicate $label: ".
123                join(', ', sort map { $_->method() } @dup_svc );
124              #XXX eventually a sort sub so usernames and domains are default alpha, username@domain is domain first then username, and uid is numeric
125     }
126   }
127
128   #end of duplicate check, whew
129
130   $error = $self->SUPER::insert;
131   if ( $error ) {
132     $dbh->rollback if $oldAutoCommit;
133     return $error;
134   }
135
136 #  if ( $self->part_svc->svcdb eq 'svc_acct' ) {
137 #
138 #    if ( $self->part_export->nodomain =~ /^Y/i ) {
139 #
140 #      select username from svc_acct where svcpart = $svcpart
141 #        group by username having count(*) > 1;
142 #
143 #    } else {
144 #
145 #      select username, domain
146 #        from   svc_acct
147 #          join svc_domain on ( svc_acct.domsvc = svc_domain.svcnum )
148 #        group by username, domain having count(*) > 1;
149 #
150 #    }
151 #
152 #  } elsif ( $self->part_svc->svcdb eq 'svc_domain' ) {
153 #
154 #    #similar but easier domain checking one
155 #
156 #  } #etc.?
157 #
158 #  my @services =
159 #    map  { $_->part_svc }
160 #    grep { $_->svcpart != $self->svcpart }
161 #         $self->part_export->export_svc;
162
163   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
164   ''; #no error
165 }
166
167 =item delete
168
169 Delete this record from the database.
170
171 =cut
172
173 # the delete method can be inherited from FS::Record
174
175 =item replace OLD_RECORD
176
177 Replaces the OLD_RECORD with this one in the database.  If there is an error,
178 returns the error, otherwise returns false.
179
180 =cut
181
182 # the replace method can be inherited from FS::Record
183
184 =item check
185
186 Checks all fields to make sure this is a valid record.  If there is
187 an error, returns the error, otherwise returns false.  Called by the insert
188 and replace methods.
189
190 =cut
191
192 # the check method should currently be supplied - FS::Record contains some
193 # data checking routines
194
195 sub check {
196   my $self = shift;
197
198   $self->ut_numbern('exportsvcnum')
199     || $self->ut_number('exportnum')
200     || $self->ut_foreign_key('exportnum', 'part_export', 'exportnum')
201     || $self->ut_number('svcpart')
202     || $self->ut_foreign_key('svcpart', 'part_svc', 'svcpart')
203     || $self->SUPER::check
204   ;
205 }
206
207 =item part_export
208
209 Returns the FS::part_export object (see L<FS::part_export>).
210
211 =cut
212
213 sub part_export {
214   my $self = shift;
215   qsearchs( 'part_export', { 'exportnum' => $self->exportnum } );
216 }
217
218 =item part_svc
219
220 Returns the FS::part_svc object (see L<FS::part_svc>).
221
222 =cut
223
224 sub part_svc {
225   my $self = shift;
226   qsearchs( 'part_svc', { 'svcpart' => $self->svcpart } );
227 }
228
229 =back
230
231 =head1 BUGS
232
233 =head1 SEE ALSO
234
235 L<FS::part_export>, L<FS::part_svc>, L<FS::Record>, schema.html from the base
236 documentation.
237
238 =cut
239
240 1;
241