add new conditions: package balances and N times, RT#11834
authorivan <ivan>
Wed, 23 Mar 2011 05:59:17 +0000 (05:59 +0000)
committerivan <ivan>
Wed, 23 Mar 2011 05:59:17 +0000 (05:59 +0000)
FS/FS/part_event/Condition/pkg_balance.pm [new file with mode: 0644]
FS/FS/part_event/Condition/pkg_balance_under.pm [new file with mode: 0644]
FS/FS/part_event/Condition/times.pm [new file with mode: 0644]

diff --git a/FS/FS/part_event/Condition/pkg_balance.pm b/FS/FS/part_event/Condition/pkg_balance.pm
new file mode 100644 (file)
index 0000000..2d4a89c
--- /dev/null
@@ -0,0 +1,36 @@
+package FS::part_event::Condition::pkg_balance;
+
+use strict;
+use FS::cust_main;
+
+use base qw( FS::part_event::Condition );
+
+sub description { 'Package balance'; }
+
+
+sub option_fields {
+  (
+    'balance' => { 'label'      => 'Balance over',
+                   'type'       => 'money',
+                   'value'      => '0.00', #default
+                 },
+  );
+}
+
+sub eventtable_hashref {
+  { 'cust_pkg' => 1, };
+}
+
+sub condition {
+  my($self, $cust_pkg) = @_;
+
+  my $cust_main = $self->cust_main($cust_pkg);
+
+  my $over = $self->option('balance');
+  $over = 0 unless length($over);
+
+  $cust_main->balance_pkgnum($cust_pkg->pkgnum) > $over;
+}
+
+1;
+
diff --git a/FS/FS/part_event/Condition/pkg_balance_under.pm b/FS/FS/part_event/Condition/pkg_balance_under.pm
new file mode 100644 (file)
index 0000000..6f46dd6
--- /dev/null
@@ -0,0 +1,35 @@
+package FS::part_event::Condition::pkg_balance_under;
+
+use strict;
+use FS::cust_main;
+
+use base qw( FS::part_event::Condition );
+
+sub description { 'Package balance (under)'; }
+
+sub option_fields {
+  (
+    'balance' => { 'label'      => 'Balance under (or equal to)',
+                   'type'       => 'money',
+                   'value'      => '0.00', #default
+                 },
+  );
+}
+
+sub eventtable_hashref {
+  { 'cust_pkg' => 1, };
+}
+
+sub condition {
+  my($self, $cust_pkg) = @_;
+
+  my $cust_main = $self->cust_main($cust_pkg);
+
+  my $under = $self->option('balance');
+  $under = 0 unless length($under);
+
+  $cust_main->balance_pkgnum($cust_pkg->pkgnum) <= $under;
+}
+
+1;
+
diff --git a/FS/FS/part_event/Condition/times.pm b/FS/FS/part_event/Condition/times.pm
new file mode 100644 (file)
index 0000000..e4410bb
--- /dev/null
@@ -0,0 +1,56 @@
+package FS::part_event::Condition::times;
+
+use strict;
+use FS::Record qw( qsearch );
+use FS::part_event;
+use FS::cust_event;
+
+use base qw( FS::part_event::Condition );
+
+sub description { "Run this event the specified number of times"; }
+
+sub option_fields {
+  (
+    'run_times'  => { label=>'Interval', type=>'text', value=>'1', },
+  );
+}
+
+sub condition {
+  my($self, $object, %opt) = @_;
+
+  my $obj_pkey = $object->primary_key;
+  my $tablenum = $object->$obj_pkey();
+  my @existing = qsearch( {
+    'table'     => 'cust_event',
+    'hashref'   => {
+                     'eventpart' => $self->eventpart,
+                     'tablenum'  => $tablenum,
+                     'status'    => { op=>'!=', value=>'failed' },
+                   },
+    'extra_sql' => ( $opt{'cust_event'}->eventnum =~ /^(\d+)$/
+                       ? " AND eventnum != $1 "
+                       : ''
+                   ),
+  } );
+
+  scalar(@existing) <= $self->option('run_times');
+
+}
+
+sub condition_sql {
+  my( $class, $table ) = @_;
+
+  my %tablenum = %{ FS::part_event->eventtable_pkey_sql };
+
+  my $existing = "( SELECT COUNT(*) FROM cust_event
+                      WHERE cust_event.eventpart = part_event.eventpart
+                        AND cust_event.tablenum = $tablenum{$table}
+                        AND status != 'failed'
+                  )";
+
+  "$existing <= ". $class->condition_sql_option('run_times');
+
+}
+
+1;