[freeside-commits] freeside/FS/FS/part_event/Condition balance_age.pm, 1.2, 1.3 balance.pm, 1.1, 1.2 balance_under.pm, 1.1, 1.2 cust_bill_age.pm, 1.2, 1.3 cust_bill_has_service.pm, 1.4, 1.5 cust_bill_owed.pm, 1.2, 1.3 cust_bill_owed_under.pm, 1.1, 1.2 cust_payments.pm, NONE, 1.1 has_referral_custnum.pm, NONE, 1.1 once_percust.pm, NONE, 1.1 pkg_age.pm, NONE, 1.1 pkg_notchange.pm, NONE, 1.1 pkg_pkgpart.pm, NONE, 1.1 pkg_recurring.pm, NONE, 1.1 pkg_unless_pkgpart.pm, NONE, 1.1

Ivan,,, ivan at wavetail.420.am
Sat Nov 22 14:17:30 PST 2008


Update of /home/cvs/cvsroot/freeside/FS/FS/part_event/Condition
In directory wavetail.420.am:/tmp/cvs-serv30261/FS/FS/part_event/Condition

Modified Files:
	balance_age.pm balance.pm balance_under.pm cust_bill_age.pm 
	cust_bill_has_service.pm cust_bill_owed.pm 
	cust_bill_owed_under.pm 
Added Files:
	cust_payments.pm has_referral_custnum.pm once_percust.pm 
	pkg_age.pm pkg_notchange.pm pkg_pkgpart.pm pkg_recurring.pm 
	pkg_unless_pkgpart.pm 
Log Message:
referral credits overhaul, use billing events, agents can self-configure, limit to once-per-customer, depend on any time from referred package, referred customer payment, specific packages, partial staged credits, RT#3983

Index: cust_bill_owed_under.pm
===================================================================
RCS file: /home/cvs/cvsroot/freeside/FS/FS/part_event/Condition/cust_bill_owed_under.pm,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- cust_bill_owed_under.pm	1 Aug 2007 22:24:39 -0000	1.1
+++ cust_bill_owed_under.pm	22 Nov 2008 22:17:28 -0000	1.2
@@ -43,7 +43,7 @@
 
   my $owed_sql = FS::cust_bill->owed_sql;
 
-  "$owed_sql <= $under";
+  "$owed_sql <= CAST( $under AS numeric )";
 }
 
 1;

--- NEW FILE: pkg_notchange.pm ---
package FS::part_event::Condition::pkg_notchange;

use strict;

use base qw( FS::part_event::Condition );
use FS::Record qw( qsearch );

sub description {
  'Package is a new order, not a change';
}

sub eventtable_hashref {
    { 'cust_main' => 0,
      'cust_bill' => 0,
      'cust_pkg'  => 1,
    };
}

sub condition {
  my( $self, $cust_pkg ) = @_;

  ! $cust_pkg->change_date;

}

sub condition_sql {
  '( cust_pkg.change_date IS NULL OR cust_pkg.change_date = 0 )';
}

1;


--- NEW FILE: pkg_age.pm ---
package FS::part_event::Condition::pkg_age;

use strict;
use base qw( FS::part_event::Condition );
use FS::Record qw( qsearch );

sub description {
  'Package Age';
}

sub eventtable_hashref {
    { 'cust_main' => 0,
      'cust_bill' => 0,
      'cust_pkg'  => 1,
    };
}

#something like this
sub option_fields {
  (
    'age'  =>  { 'label'   => 'Package date age',
                 'type'    => 'freq',
               },
    'field' => { 'label'   => 'Compare date',
                 'type'    => 'select',
                 'options' =>
                   [qw( setup last_bill bill adjourn susp expire cancel )],
                 'labels'  => {
                   'setup'     => 'Setup date',
                   'last_bill' => 'Last bill date',
                   'bill'      => 'Next bill date',
                   'adjourn'   => 'Adjournment date',
                   'susp'      => 'Suspension date',
                   'expire'    => 'Expiration date',
                   'cancel'    => 'Cancellation date',
                 },
               },
  );
}

sub condition {
  my( $self, $cust_pkg, %opt ) = @_;

  my $age = $self->option_age_from('age', $opt{'time'} );

  my $pkg_date = $cust_pkg->get( $self->option('field') );

  $pkg_date && $pkg_date <= $age;

}

#XXX write me for efficiency
#sub condition_sql {
#
#}

1;


--- NEW FILE: once_percust.pm ---
package FS::part_event::Condition::once_percust;

use strict;
use FS::Record qw( qsearch );
use FS::part_event;
use FS::cust_event;

use base qw( FS::part_event::Condition );

sub description { "Don't run more than once per customer"; }

sub eventtable_hashref {
    { 'cust_main' => 0,
      'cust_bill' => 1,
      'cust_pkg'  => 1,
    };
}

sub condition {
  my($self, $object, %opt) = @_;

  my $obj_pkey = $object->primary_key;
  my $obj_table = $object->table;
  my $custnum = $object->custnum;

  my @where = (
    "tablenum IN ( SELECT $obj_pkey FROM $obj_table WHERE custnum = $custnum )"
  );
  if ( $opt{'cust_event'}->eventnum =~ /^(\d+)$/ ) {
    push @where, " eventnum != $1 ";
  }
  my $extra_sql = ' AND '. join(' AND ', @where);
 
  my @existing = qsearch( {
    'table'     => 'cust_event',
    'hashref'   => {
                     'eventpart' => $self->eventpart,
                     #'tablenum'  => $tablenum,
                     'status'    => { op=>'!=', value=>'failed' },
                   },
    'extra_sql' => $extra_sql,
  } );

  ! scalar(@existing);

}

#XXX test?
sub condition_sql {
  my( $self, $table ) = @_;

  my %pkey = %{ FS::part_event->eventtable_pkey };

  my $pkey = $pkey{$table};

  "0 = ( SELECT COUNT(*) FROM cust_event
           WHERE cust_event.eventpart = part_event.eventpart
             AND cust_event.tablenum IN (
               SELECT $pkey FROM $table AS once_percust
                 WHERE once_percust.custnum = cust_main.custnum )
             AND status != 'failed'
       )
  ";

}

1;

--- NEW FILE: pkg_pkgpart.pm ---
package FS::part_event::Condition::pkg_pkgpart;

use strict;

use base qw( FS::part_event::Condition );

sub description { 'Package definitions'; }

sub eventtable_hashref {
    { 'cust_main' => 0,
      'cust_bill' => 0,
      'cust_pkg'  => 1,
    };
}

sub option_fields {
  ( 
    'if_pkgpart' => { 'label'    => 'Only packages: ',
                      'type'     => 'select-part_pkg',
                      'multiple' => 1,
                    },
  );
}

sub condition {
  my( $self, $cust_pkg) = @_;

  #XXX test
  my $if_pkgpart = $self->option('if_pkgpart') || {};
  $if_pkgpart->{ $cust_pkg->pkgpart };

}

#XXX 
#sub condition_sql {
#
#}

1;

Index: balance_age.pm
===================================================================
RCS file: /home/cvs/cvsroot/freeside/FS/FS/part_event/Condition/balance_age.pm,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- balance_age.pm	29 Oct 2007 10:31:31 -0000	1.2
+++ balance_age.pm	22 Nov 2008 22:17:27 -0000	1.3
@@ -1,9 +1,6 @@
 package FS::part_event::Condition::balance_age;
 
-require 5.006;
 use strict;
-use Time::Local qw(timelocal_nocheck);
-
 use base qw( FS::part_event::Condition );
 
 sub description { 'Customer balance age'; }
@@ -28,29 +25,9 @@
   my $over = $self->option('balance');
   $over = 0 unless length($over);
 
-  #false laziness w/cust_bill_age
-  my $time = $opt{'time'};
-  my $age = $self->option('age');
-  $age = '0m' unless length($age);
-
-  my ($sec,$min,$hour,$mday,$mon,$year) = (localtime($time) )[0,1,2,3,4,5];
-  if ( $age =~ /^(\d+)m$/i ) {
-    $mon -= $1;
-    until ( $mon >= 0 ) { $mon += 12; $year--; }
-  } elsif ( $age =~ /^(\d+)y$/i ) {
-    $year -= $1;
-  } elsif ( $age =~ /^(\d+)w$/i ) {
-    $mday -= $1 * 7;
-  } elsif ( $age =~ /^(\d+)d$/i ) {
-    $mday -= $1;
-  } elsif ( $age =~ /^(\d+)h$/i ) {
-    $hour -= $hour;
-  } else {
-    die "unparsable age: $age";
-  }
-  my $age_date = timelocal_nocheck($sec,$min,$hour,$mday,$mon,$year);
+  my $age = $self->option_age_from('age', $opt{'time'} );
 
-  $cust_main->balance_date($age_date) > $over;
+  $cust_main->balance_date($age) > $over;
 }
 
 sub condition_sql {
@@ -61,7 +38,7 @@
 
   my $balance_sql = FS::cust_main->balance_date_sql( $age );
 
-  "$balance_sql > $over";
+  "$balance_sql > CAST( $over AS numeric )";
 }
 
 sub order_sql {

Index: balance.pm
===================================================================
RCS file: /home/cvs/cvsroot/freeside/FS/FS/part_event/Condition/balance.pm,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- balance.pm	1 Aug 2007 22:24:39 -0000	1.1
+++ balance.pm	22 Nov 2008 22:17:27 -0000	1.2
@@ -40,7 +40,7 @@
 
   my $balance_sql = FS::cust_main->balance_sql;
 
-  "$balance_sql > $over";
+  "$balance_sql > CAST( $over AS numeric )";
 
 }
 

--- NEW FILE: cust_payments.pm ---
package FS::part_event::Condition::cust_payments;

use strict;
use base qw( FS::part_event::Condition );

sub description { 'Customer total payments'; }

sub option_fields {
  (
    'over' => { 'label'      => 'Customer total payments at least',
                'type'       => 'money',
                'value'      => '0.00', #default
              },
  );
}

sub condition {
  my($self, $object) = @_;

  my $cust_main = $self->cust_main($object);

  my $over = $self->option('over');
  $over = 0 unless length($over);

  $cust_main->total_paid >= $over;

}

#XXX add for efficiency.  could use cust_main::total_paid_sql
#use FS::cust_main;
#sub condition_sql {
#  my( $class, $table ) = @_;
#
#  my $over = $class->condition_sql_option('balance');
#
#  my $balance_sql = FS::cust_main->balance_sql;
#
#  "$balance_sql > $over";
#
#}

1;


Index: cust_bill_owed.pm
===================================================================
RCS file: /home/cvs/cvsroot/freeside/FS/FS/part_event/Condition/cust_bill_owed.pm,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- cust_bill_owed.pm	8 Oct 2007 21:06:30 -0000	1.2
+++ cust_bill_owed.pm	22 Nov 2008 22:17:28 -0000	1.3
@@ -48,7 +48,7 @@
 
   my $owed_sql = FS::cust_bill->owed_sql;
 
-  "$owed_sql > $over";
+  "$owed_sql > CAST( $over AS numeric )";
 }
 
 1;

--- NEW FILE: pkg_unless_pkgpart.pm ---
package FS::part_event::Condition::pkg_unless_pkgpart;

use strict;

use base qw( FS::part_event::Condition );

sub description { 'Except package definitions'; }

sub eventtable_hashref {
    { 'cust_main' => 0,
      'cust_bill' => 0,
      'cust_pkg'  => 1,
    };
}

sub option_fields {
  ( 
    'unless_pkgpart' => { 'label'    => 'Except packages: ',
                          'type'     => 'select-part_pkg',
                          'multiple' => 1,
                        },
  );
}

sub condition {
  my( $self, $cust_pkg) = @_;

  #XXX test
  my $unless_pkgpart = $self->option('unless_pkgpart') || {};
  ! $unless_pkgpart->{ $cust_pkg->pkgpart };

}

#XXX
#sub condition_sql {
#
#}

1;

--- NEW FILE: pkg_recurring.pm ---
package FS::part_event::Condition::pkg_recurring;

use strict;

use base qw( FS::part_event::Condition );

sub description { 'Package is recurring'; }

sub eventtable_hashref {
    { 'cust_main' => 0,
      'cust_bill' => 0,
      'cust_pkg'  => 1,
    };
}

sub condition {
  my( $self, $cust_pkg ) = @_;

  $cust_pkg->part_pkg->freq !~ /^0+\D?$/; #just in case, probably just != '0'

}


#XXX  join part_pkg USING (pkgpart) 
#  part_pkg.freq != '0'
#sub condition_sql {
#
#}

1;


--- NEW FILE: has_referral_custnum.pm ---
package FS::part_event::Condition::has_referral_custnum;

use strict;
use FS::cust_main;

use base qw( FS::part_event::Condition );

sub description { 'Customer has a referring customer'; }

sub condition {
  my($self, $object) = @_;

  my $cust_main = $self->cust_main($object);

  $cust_main->referral_custnum;
}

sub condition_sql {
  #my( $class, $table ) = @_;

  "cust_main.referral_custnum IS NOT NULL";
}

1;

Index: balance_under.pm
===================================================================
RCS file: /home/cvs/cvsroot/freeside/FS/FS/part_event/Condition/balance_under.pm,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- balance_under.pm	1 Aug 2007 22:24:39 -0000	1.1
+++ balance_under.pm	22 Nov 2008 22:17:27 -0000	1.2
@@ -34,7 +34,7 @@
 
   my $balance_sql = FS::cust_main->balance_sql;
 
-  "$balance_sql <= $under";
+  "$balance_sql <= CAST( $under AS numeric )";
 
 }
 

Index: cust_bill_age.pm
===================================================================
RCS file: /home/cvs/cvsroot/freeside/FS/FS/part_event/Condition/cust_bill_age.pm,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- cust_bill_age.pm	29 Oct 2007 10:31:31 -0000	1.2
+++ cust_bill_age.pm	22 Nov 2008 22:17:27 -0000	1.3
@@ -1,14 +1,9 @@
 package FS::part_event::Condition::cust_bill_age;
 
-require 5.006;
 use strict;
-use Time::Local qw(timelocal_nocheck);
-
 use base qw( FS::part_event::Condition );
 
-sub description {
-  'Invoice age';
-}
+sub description { 'Invoice age'; }
 
 sub eventtable_hashref {
     { 'cust_main' => 0,
@@ -17,10 +12,8 @@
     };
 }
 
-#something like this
 sub option_fields {
   (
-    #'days' => { label=>'Days', size=>3, },
     'age' => { label=>'Age', type=>'freq', },
   );
 }
@@ -28,34 +21,12 @@
 sub condition {
   my( $self, $cust_bill, %opt ) = @_;
 
-  #false laziness w/balance_age
-  my $time = $opt{'time'};
-  my $age = $self->option('age');
-  $age = '0m' unless length($age);
-
-  my ($sec,$min,$hour,$mday,$mon,$year) = (localtime($time) )[0,1,2,3,4,5];
-  if ( $age =~ /^(\d+)m$/i ) {
-    $mon -= $1;
-    until ( $mon >= 0 ) { $mon += 12; $year--; }
-  } elsif ( $age =~ /^(\d+)y$/i ) {
-    $year -= $1;
-  } elsif ( $age =~ /^(\d+)w$/i ) {
-    $mday -= $1 * 7;
-  } elsif ( $age =~ /^(\d+)d$/i ) {
-    $mday -= $1;
-  } elsif ( $age =~ /^(\d+)h$/i ) {
-    $hour -= $hour;
-  } else {
-    die "unparsable age: $age";
-  }
-  my $age_date = timelocal_nocheck($sec,$min,$hour,$mday,$mon,$year);
+  my $age = $self->option_age_from('age', $opt{'time'} );
 
-  $cust_bill->_date <= $age_date;
+  $cust_bill->_date <= $age;
 
 }
 
-#                            and seconds <= $time - cust_bill._date
-
 sub condition_sql {
   my( $class, $table, %opt ) = @_;
 

Index: cust_bill_has_service.pm
===================================================================
RCS file: /home/cvs/cvsroot/freeside/FS/FS/part_event/Condition/cust_bill_has_service.pm,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -d -r1.4 -r1.5
--- cust_bill_has_service.pm	25 Aug 2008 20:33:58 -0000	1.4
+++ cust_bill_has_service.pm	22 Nov 2008 22:17:27 -0000	1.5
@@ -45,7 +45,7 @@
      FROM cust_bill_pkg cbp, cust_svc cs
     WHERE cbp.invnum = cust_bill.invnum
       AND cs.pkgnum = cbp.pkgnum
-      AND cs.svcpart = $servicenum
+      AND cs.svcpart = CAST( $servicenum AS integer )
   )
   |;
   return $sql;



More information about the freeside-commits mailing list