From d29b455cf176358089f98e2970beaab88195bc62 Mon Sep 17 00:00:00 2001 From: Ivan Kohler Date: Sun, 25 Aug 2013 01:40:28 -0700 Subject: [PATCH] continue sales person work: customer and package selection, commissions, reporting. RT#23402 --- FS/FS/part_event.pm | 1 + .../Action/Mixin/credit_sales_pkg_class.pm | 1 + FS/FS/part_event/Condition/pkg_age_Common.pm | 9 +++- FS/FS/part_event/Condition/pkg_age_before_sales.pm | 58 ++++++++++++++++++++++ FS/FS/part_event_condition.pm | 3 +- 5 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 FS/FS/part_event/Condition/pkg_age_before_sales.pm diff --git a/FS/FS/part_event.pm b/FS/FS/part_event.pm index b7371c9ab..a740bb836 100644 --- a/FS/FS/part_event.pm +++ b/FS/FS/part_event.pm @@ -583,6 +583,7 @@ sub actions { ( map { $_ => $actions{$_} } sort { $actions{$a}->{'default_weight'}<=>$actions{$b}->{'default_weight'} } + # || $actions{$a}->{'description'} cmp $actions{$b}->{'description'} } $class->all_actions( $eventtable ) ); diff --git a/FS/FS/part_event/Action/Mixin/credit_sales_pkg_class.pm b/FS/FS/part_event/Action/Mixin/credit_sales_pkg_class.pm index 666de1378..fc7a3e322 100644 --- a/FS/FS/part_event/Action/Mixin/credit_sales_pkg_class.pm +++ b/FS/FS/part_event/Action/Mixin/credit_sales_pkg_class.pm @@ -3,6 +3,7 @@ use base qw( FS::part_event::Action::Mixin::credit_pkg ); use strict; use FS::Record qw(qsearchs); +use FS::sales_pkg_class; sub option_fields { my $class = shift; diff --git a/FS/FS/part_event/Condition/pkg_age_Common.pm b/FS/FS/part_event/Condition/pkg_age_Common.pm index 0f3b9efb4..726b01d70 100644 --- a/FS/FS/part_event/Condition/pkg_age_Common.pm +++ b/FS/FS/part_event/Condition/pkg_age_Common.pm @@ -40,7 +40,7 @@ sub option_fields { sub condition { my( $self, $cust_pkg, %opt ) = @_; - my $age = $self->option_age_from('age', $opt{'time'} ); + my $age = $self->pkg_age_age( $cust_pkg, %opt ); my $pkg_date = $cust_pkg->get( $self->option('field') ); @@ -48,6 +48,13 @@ sub condition { } +sub pkg_age_age { + my( $self, $cust_pkg, %opt ); + $self->option_age_from('age', $opt{'time'} ); +} + +#doesn't work if you override pkg_age_age, +# so if you do, override this with at least a stub that returns 'true' sub condition_sql { my( $class, $table, %opt ) = @_; my $age = $class->condition_sql_option_age_from('age', $opt{'time'}); diff --git a/FS/FS/part_event/Condition/pkg_age_before_sales.pm b/FS/FS/part_event/Condition/pkg_age_before_sales.pm new file mode 100644 index 000000000..4ad56fa10 --- /dev/null +++ b/FS/FS/part_event/Condition/pkg_age_before_sales.pm @@ -0,0 +1,58 @@ +package FS::part_event::Condition::pkg_age_before_sales; +use base qw( FS::part_event::Condition::pkg_age_before ); + +use strict; +use Time::Local qw( timelocal_nocheck ); +use FS::Record qw( qsearchs ); +use FS::sales_pkg_class; + +sub description { 'Package age younger than sales person commission duration'; } + +sub option_fields { + my $class = shift; + my %option_fields = $class->SUPER::option_fields(); + + delete $option_fields{'age'}; + + $option_fields{'cust_main_sales'} = { + 'label' => "Compare to the customer sales person if there is no package sales person", + 'type' => 'checkbox', + 'value' => 'Y', + }; + + %option_fields; +} + +sub pkg_age_age { + my( $self, $cust_pkg, %opt ); + + my $salesnum = $cust_pkg->salesnum; + $salesnum ||= $self->cust_main($cust_pkg)->salesnum + if $self->option('cust_main_sales'); + + return 0 unless $salesnum; + + my $sales_pkg_class = qsearchs( 'sales_pkg_class', { + 'salesnum' => $salesnum, + 'classnum' => $cust_pkg->part_pkg->classnum, + }); + + my $commission_duration = $sales_pkg_class->commission_duration; + return 0 unless $commission_duration =~ /^\s*(\d+)\s*$/; + + #false laziness w/Condition::option_age_from, but just months + + my $time = $opt{'time'}; + my ($sec,$min,$hour,$mday,$mon,$year) = (localtime($time) )[0,1,2,3,4,5]; + $mon -= $commission_duration; + until ( $mon >= 0 ) { $mon += 12; $year--; } + + timelocal_nocheck($sec,$min,$hour,$mday,$mon,$year); +} + +#no working condition_sql for this comparison yet, don't want pkg_age_Common's +sub condition_sql { + 'true'; +} + +1; diff --git a/FS/FS/part_event_condition.pm b/FS/FS/part_event_condition.pm index d550680f7..78aa3b1a9 100644 --- a/FS/FS/part_event_condition.pm +++ b/FS/FS/part_event_condition.pm @@ -224,8 +224,7 @@ sub conditions { my( $class, $eventtable ) = @_; ( map { $_ => $conditions{$_} } -# sort { $conditions{$a}->{'default_weight'}<=>$conditions{$b}->{'default_weight'} } -# sort by ? + sort {$conditions{$a}->{'description'} cmp $conditions{$b}->{'description'}} $class->all_conditionnames( $eventtable ) ); -- 2.11.0