1 package FS::cust_pkg_reason;
5 use FS::Record qw( qsearch qsearchs );
11 FS::cust_pkg_reason - Object methods for cust_pkg_reason records
15 use FS::cust_pkg_reason;
17 $record = new FS::cust_pkg_reason \%hash;
18 $record = new FS::cust_pkg_reason { 'column' => 'value' };
20 $error = $record->insert;
22 $error = $new_record->replace($old_record);
24 $error = $record->delete;
26 $error = $record->check;
30 An FS::cust_pkg_reason object represents a relationship between a cust_pkg
31 and a reason, for example cancellation or suspension reasons.
32 FS::cust_pkg_reason inherits from FS::Record. The following fields are
37 =item num - primary key
56 Creates a new cust_pkg_reason. To add the example to the database, see
59 Note that this stores the hash reference, not a distinct copy of the hash it
60 points to. You can ask the object for a copy with the I<hash> method.
64 sub table { 'cust_pkg_reason'; }
68 Adds this record to the database. If there is an error, returns the error,
69 otherwise returns false.
75 Delete this record from the database.
79 =item replace OLD_RECORD
81 Replaces the OLD_RECORD with this one in the database. If there is an error,
82 returns the error, otherwise returns false.
88 Checks all fields to make sure this is a valid cust_pkg_reason. If there is
89 an error, returns the error, otherwise returns false. Called by the insert
98 $self->ut_numbern('num')
99 || $self->ut_number('pkgnum')
100 || $self->ut_number('reasonnum')
101 || $self->ut_enum('action', [ 'A', 'C', 'E', 'S' ])
102 || $self->ut_text('otaker')
103 || $self->ut_numbern('date')
105 return $error if $error;
112 Returns the reason (see L<FS::reason>) associated with this cust_pkg_reason.
118 qsearchs( 'reason', { 'reasonnum' => $self->reasonnum } );
123 Returns the text of the reason (see L<FS::reason>) associated with this
129 my $reason = shift->reason;
130 $reason ? $reason->reason : '';
135 # Used by FS::Upgrade to migrate to a new database.
138 use FS::h_cust_pkg_reason;
139 use FS::Schema qw(dbdef);
141 sub _upgrade_data { # class method
142 my ($class, %opts) = @_;
144 return '' unless dbdef->table('cust_pkg_reason')->column('action');
147 " AND ( history_action = 'replace_old' OR history_action = 'replace_new' )";
150 my @unmigrated = qsearch('cust_pkg_reason', { 'action' => '' } );
151 foreach ( @unmigrated ) {
152 # we could create h_cust_pkg_reason and h_cust_pkg_reason packages
153 @FS::h_cust_pkg::ISA = qw( FS::h_Common FS::cust_pkg );
154 sub FS::h_cust_pkg::table { 'h_cust_pkg' };
155 @FS::h_cust_pkg_reason::ISA = qw( FS::h_Common FS::cust_pkg_reason );
156 sub FS::h_cust_pkg_reason::table { 'h_cust_pkg_reason' };
158 my @history_cust_pkg_reason = qsearch( 'h_cust_pkg_reason', { $_->hash } );
160 next unless scalar(@history_cust_pkg_reason) == 1;
162 my $hashref = { pkgnum => $_->pkgnum,
163 history_date => $history_cust_pkg_reason[0]->history_date,
166 my @history = qsearch({ table => 'h_cust_pkg',
168 extra_sql => $action_replace,
169 order_by => 'ORDER BY history_action',
173 while (scalar(@history) < 2 && $fuzz < 3) {
174 $hashref->{history_date}++;
176 push @history, qsearch({ table => 'h_cust_pkg',
178 extra_sql => $action_replace,
179 order_by => 'ORDER BY history_action',
183 next unless scalar(@history) == 2;
185 my @new = grep { $_->history_action eq 'replace_new' } @history;
186 my @old = grep { $_->history_action eq 'replace_old' } @history;
188 next if (scalar(@new) == 2 || scalar(@old) == 2);
190 if ( !$old[0]->get('cancel') && $new[0]->get('cancel') ) {
192 }elsif( !$old[0]->susp && $new[0]->susp ){
194 }elsif( $new[0]->expire &&
195 (!$old[0]->expire || !$old[0]->expire != $new[0]->expire )
198 $_->date($new[0]->expire);
199 }elsif( $new[0]->adjourn &&
200 (!$old[0]->adjourn || $old[0]->adjourn != $new[0]->adjourn )
203 $_->date($new[0]->adjourn);
206 my $error = $_->replace
209 die $error if $error;
214 #remove nullability if scalar(@migrated) - $count == 0 && ->column('action');
216 #seek expirations/adjourns without reason
217 foreach my $field qw( expire adjourn cancel susp ) {
219 "LEFT JOIN h_cust_pkg ON ".
220 "(cust_pkg_reason.pkgnum = h_cust_pkg.pkgnum AND".
221 " cust_pkg_reason.date = h_cust_pkg.$field AND".
222 " history_action = 'replace_new')";
224 my $extra_sql = 'AND h_cust_pkg.pkgnum IS NULL';
226 my @unmigrated = qsearch({ table => 'cust_pkg_reason',
227 hashref => { action => uc(substr($field,0,1)) },
228 addl_from => $addl_from,
229 select => 'cust_pkg_reason.*',
230 extra_sql => $extra_sql,
232 foreach ( @unmigrated ) {
233 # we could create h_cust_pkg_reason and h_cust_pkg_reason packages
234 @FS::h_cust_pkg::ISA = qw( FS::h_Common FS::cust_pkg );
235 sub FS::h_cust_pkg::table { 'h_cust_pkg' };
237 my $hashref = { pkgnum => $_->pkgnum,
238 history_date => $_->date,
241 my @history = qsearch({ table => 'h_cust_pkg',
243 extra_sql => $action_replace,
244 order_by => 'ORDER BY history_action',
248 while (scalar(@history) < 2 && $fuzz < 3) {
249 $hashref->{history_date}++;
251 push @history, qsearch({ table => 'h_cust_pkg',
253 extra_sql => $action_replace,
254 order_by => 'ORDER BY history_action',
258 next unless scalar(@history) == 2;
260 my @new = grep { $_->history_action eq 'replace_new' } @history;
261 my @old = grep { $_->history_action eq 'replace_old' } @history;
263 next if (scalar(@new) == 2 || scalar(@old) == 2);
265 $_->date($new[0]->get($field))
266 if ( $new[0]->get($field) &&
267 ( !$old[0]->get($field) ||
268 $old[0]->get($field) != $new[0]->get($field)
272 my $error = $_->replace
275 die $error if $error;
279 #seek cancels/suspends without reason, but with expire/adjourn reason
280 foreach my $field qw( cancel susp ) {
282 my %precursor_map = ( 'cancel' => 'expire', 'susp' => 'adjourn' );
283 my $precursor = $precursor_map{$field};
284 my $preaction = uc(substr($precursor,0,1));
285 my $action = uc(substr($field,0,1));
287 "LEFT JOIN cust_pkg_reason ON ".
288 "(cust_pkg.pkgnum = cust_pkg_reason.pkgnum AND".
289 " cust_pkg.$precursor = cust_pkg_reason.date AND".
290 " cust_pkg_reason.action = '$preaction') ".
291 "LEFT JOIN cust_pkg_reason AS target ON ".
292 "(cust_pkg.pkgnum = target.pkgnum AND".
293 " cust_pkg.$field = target.date AND".
294 " target.action = '$action')"
297 my $extra_sql = "WHERE target.pkgnum IS NULL AND ".
298 "cust_pkg.$field IS NOT NULL AND ".
299 "cust_pkg.$field < cust_pkg.$precursor + 86400 AND ".
300 "cust_pkg_reason.action = '$preaction'";
302 my @unmigrated = qsearch({ table => 'cust_pkg',
304 select => 'cust_pkg.*',
305 addl_from => $addl_from,
306 extra_sql => $extra_sql,
308 foreach ( @unmigrated ) {
309 my $cpr = new FS::cust_pkg_reason { $_->last_cust_pkg_reason($precursor)->hash, 'num' => '' };
310 $cpr->date($_->get($field));
311 $cpr->action($action);
313 my $error = $cpr->insert;
314 die $error if $error;
326 Here be termites. Don't use on wooden computers.
330 L<FS::Record>, schema.html from the base documentation.