1 package FS::cust_pkg_reason;
4 use vars qw( $ignore_empty_action );
5 use base qw( FS::otaker_Mixin FS::Record );
6 use FS::Record qw( qsearch qsearchs );
8 $ignore_empty_action = 0;
12 FS::cust_pkg_reason - Object methods for cust_pkg_reason records
16 use FS::cust_pkg_reason;
18 $record = new FS::cust_pkg_reason \%hash;
19 $record = new FS::cust_pkg_reason { 'column' => 'value' };
21 $error = $record->insert;
23 $error = $new_record->replace($old_record);
25 $error = $record->delete;
27 $error = $record->check;
31 An FS::cust_pkg_reason object represents a relationship between a cust_pkg
32 and a reason, for example cancellation or suspension reasons.
33 FS::cust_pkg_reason inherits from FS::Record. The following fields are
58 Creates a new cust_pkg_reason. To add the example to the database, see
61 Note that this stores the hash reference, not a distinct copy of the hash it
62 points to. You can ask the object for a copy with the I<hash> method.
66 sub table { 'cust_pkg_reason'; }
70 Adds this record to the database. If there is an error, returns the error,
71 otherwise returns false.
77 Delete this record from the database.
81 =item replace OLD_RECORD
83 Replaces the OLD_RECORD with this one in the database. If there is an error,
84 returns the error, otherwise returns false.
90 Checks all fields to make sure this is a valid cust_pkg_reason. If there is
91 an error, returns the error, otherwise returns false. Called by the insert
99 my @actions = ( 'A', 'C', 'E', 'S' );
100 push @actions, '' if $ignore_empty_action;
103 $self->ut_numbern('num')
104 || $self->ut_number('pkgnum')
105 || $self->ut_number('reasonnum')
106 || $self->ut_enum('action', \@actions)
107 || $self->ut_alphan('otaker')
108 || $self->ut_numbern('date')
110 return $error if $error;
117 Returns the reason (see L<FS::reason>) associated with this cust_pkg_reason.
123 qsearchs( 'reason', { 'reasonnum' => $self->reasonnum } );
128 Returns the text of the reason (see L<FS::reason>) associated with this
134 my $reason = shift->reason;
135 $reason ? $reason->reason : '';
140 # Used by FS::Upgrade to migrate to a new database.
143 use FS::h_cust_pkg_reason;
145 sub _upgrade_data { # class method
146 my ($class, %opts) = @_;
149 " AND ( history_action = 'replace_old' OR history_action = 'replace_new' )";
152 my @unmigrated = qsearch('cust_pkg_reason', { 'action' => '' } );
153 foreach ( @unmigrated ) {
155 my @history_cust_pkg_reason = qsearch( 'h_cust_pkg_reason', { $_->hash } );
157 next unless scalar(@history_cust_pkg_reason) == 1;
159 my $hashref = { pkgnum => $_->pkgnum,
160 history_date => $history_cust_pkg_reason[0]->history_date,
163 my @history = qsearch({ table => 'h_cust_pkg',
165 extra_sql => $action_replace,
166 order_by => 'ORDER BY history_action',
170 while (scalar(@history) < 2 && $fuzz < 3) {
171 $hashref->{history_date}++;
173 push @history, qsearch({ table => 'h_cust_pkg',
175 extra_sql => $action_replace,
176 order_by => 'ORDER BY history_action',
180 next unless scalar(@history) == 2;
182 my @new = grep { $_->history_action eq 'replace_new' } @history;
183 my @old = grep { $_->history_action eq 'replace_old' } @history;
185 next if (scalar(@new) == 2 || scalar(@old) == 2);
187 if ( !$old[0]->get('cancel') && $new[0]->get('cancel') ) {
189 }elsif( !$old[0]->susp && $new[0]->susp ){
191 }elsif( $new[0]->expire &&
192 (!$old[0]->expire || !$old[0]->expire != $new[0]->expire )
195 $_->date($new[0]->expire);
196 }elsif( $new[0]->adjourn &&
197 (!$old[0]->adjourn || $old[0]->adjourn != $new[0]->adjourn )
200 $_->date($new[0]->adjourn);
203 my $error = $_->replace
206 die $error if $error;
211 #remove nullability if scalar(@migrated) - $count == 0 && ->column('action');
213 #seek expirations/adjourns without reason
214 foreach my $field (qw( expire adjourn cancel susp )) {
216 "LEFT JOIN h_cust_pkg ON ".
217 "(cust_pkg_reason.pkgnum = h_cust_pkg.pkgnum AND".
218 " cust_pkg_reason.date = h_cust_pkg.$field AND".
219 " history_action = 'replace_new')";
221 my $extra_sql = 'AND h_cust_pkg.pkgnum IS NULL';
223 my @unmigrated = qsearch({ table => 'cust_pkg_reason',
224 hashref => { action => uc(substr($field,0,1)) },
225 addl_from => $addl_from,
226 select => 'cust_pkg_reason.*',
227 extra_sql => $extra_sql,
229 foreach ( @unmigrated ) {
231 my $hashref = { pkgnum => $_->pkgnum,
232 history_date => $_->date,
235 my @history = qsearch({ table => 'h_cust_pkg',
237 extra_sql => $action_replace,
238 order_by => 'ORDER BY history_action',
242 while (scalar(@history) < 2 && $fuzz < 3) {
243 $hashref->{history_date}++;
245 push @history, qsearch({ table => 'h_cust_pkg',
247 extra_sql => $action_replace,
248 order_by => 'ORDER BY history_action',
252 next unless scalar(@history) == 2;
254 my @new = grep { $_->history_action eq 'replace_new' } @history;
255 my @old = grep { $_->history_action eq 'replace_old' } @history;
257 next if (scalar(@new) == 2 || scalar(@old) == 2);
259 $_->date($new[0]->get($field))
260 if ( $new[0]->get($field) &&
261 ( !$old[0]->get($field) ||
262 $old[0]->get($field) != $new[0]->get($field)
266 my $error = $_->replace
269 die $error if $error;
273 #seek cancels/suspends without reason, but with expire/adjourn reason
274 foreach my $field (qw( cancel susp )) {
276 my %precursor_map = ( 'cancel' => 'expire', 'susp' => 'adjourn' );
277 my $precursor = $precursor_map{$field};
278 my $preaction = uc(substr($precursor,0,1));
279 my $action = uc(substr($field,0,1));
281 "LEFT JOIN cust_pkg_reason ON ".
282 "(cust_pkg.pkgnum = cust_pkg_reason.pkgnum AND".
283 " cust_pkg.$precursor = cust_pkg_reason.date AND".
284 " cust_pkg_reason.action = '$preaction') ".
285 "LEFT JOIN cust_pkg_reason AS target ON ".
286 "(cust_pkg.pkgnum = target.pkgnum AND".
287 " cust_pkg.$field = target.date AND".
288 " target.action = '$action')"
291 my $extra_sql = "WHERE target.pkgnum IS NULL AND ".
292 "cust_pkg.$field IS NOT NULL AND ".
293 "cust_pkg.$field < cust_pkg.$precursor + 86400 AND ".
294 "cust_pkg_reason.action = '$preaction'";
296 my @unmigrated = qsearch({ table => 'cust_pkg',
298 select => 'cust_pkg.*',
299 addl_from => $addl_from,
300 extra_sql => $extra_sql,
302 foreach ( @unmigrated ) {
303 my $cpr = new FS::cust_pkg_reason { $_->last_cust_pkg_reason($precursor)->hash, 'num' => '' };
304 $cpr->date($_->get($field));
305 $cpr->action($action);
307 my $error = $cpr->insert;
308 die $error if $error;
312 #still can't fill in an action? don't abort the upgrade
313 local($ignore_empty_action) = 1;
315 $class->_upgrade_otaker(%opts);
324 L<FS::Record>, schema.html from the base documentation.