employee (otaker / access_user) commissioning, RT#6991
authorivan <ivan>
Tue, 30 Mar 2010 02:53:12 +0000 (02:53 +0000)
committerivan <ivan>
Tue, 30 Mar 2010 02:53:12 +0000 (02:53 +0000)
FS/FS/part_event/Action/Mixin/credit_pkg.pm [new file with mode: 0644]
FS/FS/part_event/Action/pkg_agent_credit.pm [new file with mode: 0644]
FS/FS/part_event/Action/pkg_agent_credit_pkg.pm [new file with mode: 0644]
FS/FS/part_event/Action/pkg_employee_credit_pkg.pm [new file with mode: 0644]
FS/FS/part_event/Action/pkg_referral_credit.pm
FS/FS/part_event/Action/pkg_referral_credit_pkg.pm
FS/FS/part_pkg.pm
httemplate/browse/access_user.html
httemplate/edit/access_user.html
httemplate/elements/search-cust_main.html
httemplate/elements/tr-search-cust_main.html [new file with mode: 0644]

diff --git a/FS/FS/part_event/Action/Mixin/credit_pkg.pm b/FS/FS/part_event/Action/Mixin/credit_pkg.pm
new file mode 100644 (file)
index 0000000..aeda92f
--- /dev/null
@@ -0,0 +1,63 @@
+package FS::part_event::Action::Mixin::credit_pkg;
+
+use strict;
+
+sub eventtable_hashref {
+  { 'cust_pkg' => 1 };
+}
+
+sub option_fields {
+  ( 
+    'reasonnum' => { 'label'        => 'Credit reason',
+                     'type'         => 'select-reason',
+                     'reason_class' => 'R',
+                   },
+    'percent'   => { 'label'   => 'Percent',
+                     'type'    => 'input-percentage',
+                     'default' => '100',
+                   },
+    'what' => { 'label'   => 'Of',
+                'type'    => 'select',
+                #add additional ways to specify in the package def
+                'options' => [ qw( base_recur_permonth unit_setup recur_cost_permonth setup_cost ) ],
+                'labels'  => { 'base_recur_permonth' => 'Base monthly fee',
+                               'unit_setup'          => 'Setup fee',
+                               'recur_cost_permonth' => 'Monthly cost',
+                               'setup_cost'          => 'Setup cost',
+                             },
+              },
+  );
+
+}
+
+#my %no_cust_pkg = ( 'setup_cost' => 1 );
+
+sub _calc_credit {
+  my( $self, $cust_pkg ) = @_;
+
+  my $cust_main = $self->cust_main($cust_pkg);
+
+  my $part_pkg = $cust_pkg->part_pkg;
+
+  my $what = $self->option('what');
+
+  #false laziness w/Condition/cust_payments_pkg.pm
+  if ( $what =~ /_permonth$/ ) { #huh.  yuck.
+    if ( $part_pkg->freq !~ /^\d+$/ ) {
+      die 'WARNING: Not crediting for package '. $cust_pkg->pkgnum.
+          ' ( customer '. $cust_pkg->custnum. ')'.
+          ' - credits not (yet) available for '.
+          ' packages with '. $part_pkg->freq_pretty. ' frequency';
+    }
+  }
+
+  my $percent = $self->option('percent');
+
+  #my @arg = $no_cust_pkg{$what} ? () : ($cust_pkg);
+  my @arg = ($what eq 'setup_cost') ? () : ($cust_pkg);
+
+  sprintf('%.2f', $part_pkg->$what(@arg) * $percent / 100 );
+
+}
+
+1;
diff --git a/FS/FS/part_event/Action/pkg_agent_credit.pm b/FS/FS/part_event/Action/pkg_agent_credit.pm
new file mode 100644 (file)
index 0000000..2502738
--- /dev/null
@@ -0,0 +1,38 @@
+package FS::part_event::Action::pkg_agent_credit;
+
+use strict;
+use base qw( FS::part_event::Action::pkg_referral_credit );
+
+sub description { 'Credit the agent a specific amount'; }
+
+#a little false laziness w/pkg_referral_credit
+sub do_action {
+  my( $self, $cust_pkg ) = @_;
+
+  my $cust_main = $self->cust_main($cust_pkg);
+
+  my $agent = $cust_main->agent;
+  return "No customer record for agent ". $agent->agent
+    unless $agent->agent_custnum;
+
+  my $agent_cust_main = $agent->agent_cust_main;
+    #? or return "No customer record for agent ". $agent->agent;
+
+  my $amount    = $self->_calc_credit($cust_pkg);
+  return '' unless $amount > 0;
+
+  my $reasonnum = $self->option('reasonnum');
+
+  my $error = $agent_cust_main->credit(
+    $amount, 
+    \$reasonnum,
+    'addlinfo' =>
+      'for customer #'. $cust_main->display_custnum. ': '.$cust_main->name,
+  );
+  die "Error crediting customer ". $agent_cust_main->custnum.
+      " for agent commission: $error"
+    if $error;
+
+}
+
+1;
diff --git a/FS/FS/part_event/Action/pkg_agent_credit_pkg.pm b/FS/FS/part_event/Action/pkg_agent_credit_pkg.pm
new file mode 100644 (file)
index 0000000..b3e1181
--- /dev/null
@@ -0,0 +1,9 @@
+package FS::part_event::Action::pkg_agent_credit_pkg;
+
+use strict;
+use base qw( FS::part_event::Action::Mixin::credit_pkg
+             FS::part_event::Action::pkg_agent_credit );
+
+sub description { 'Credit the agent an amount based on the referred package'; }
+
+1;
diff --git a/FS/FS/part_event/Action/pkg_employee_credit_pkg.pm b/FS/FS/part_event/Action/pkg_employee_credit_pkg.pm
new file mode 100644 (file)
index 0000000..e3b867f
--- /dev/null
@@ -0,0 +1,9 @@
+package FS::part_event::Action::pkg_employee_credit_pkg;
+
+use strict;
+use base qw( FS::part_event::Action::Mixin::credit_pkg
+             FS::part_event::Action::pkg_employee_credit );
+
+sub description { 'Credit the ordering employee an amount based on the referred package'; }
+
+1;
index 98d9820..da872e7 100644 (file)
@@ -22,7 +22,6 @@ sub option_fields {
 
 }
 
-#a little false laziness w/pkg_referral_credit_pkg
 sub do_action {
   my( $self, $cust_pkg ) = @_;
 
@@ -36,7 +35,9 @@ sub do_action {
   return 'Referring customer is cancelled'
     if $referring_cust_main->status eq 'cancelled';
 
-  my $amount    = $self->_calc_referral_credit($cust_pkg);
+  my $amount    = $self->_calc_credit($cust_pkg);
+  return '' unless $amount > 0;
+
   my $reasonnum = $self->option('reasonnum');
 
   my $error = $referring_cust_main->credit(
@@ -51,7 +52,7 @@ sub do_action {
 
 }
 
-sub _calc_referral_credit {
+sub _calc_credit {
   my( $self, $cust_pkg ) = @_;
 
   $self->option('amount');
index eb9b510..667c4ce 100644 (file)
@@ -1,58 +1,9 @@
 package FS::part_event::Action::pkg_referral_credit_pkg;
 
 use strict;
-use base qw( FS::part_event::Action::pkg_referral_credit );
+use base qw( FS::part_event::Action::Mixin::credit_pkg
+             FS::part_event::Action::pkg_referral_credit );
 
 sub description { 'Credit the referring customer an amount based on the referred package'; }
 
-#sub eventtable_hashref {
-#  { 'cust_pkg' => 1 };
-#}
-
-sub option_fields {
-  ( 
-    'reasonnum' => { 'label'        => 'Credit reason',
-                     'type'         => 'select-reason',
-                     'reason_class' => 'R',
-                   },
-    'percent'   => { 'label'   => 'Percent',
-                     'type'    => 'input-percentage',
-                     'default' => '100',
-                   },
-    'what' => { 'label'   => 'Of',
-                'type'    => 'select',
-                #also add some way to specify in the package def, no?
-                'options' => [ qw( base_recur_permonth ) ],
-                'labels'  => { 'base_recur_permonth' => 'Base monthly fee', },
-              },
-  );
-
-}
-
-sub _calc_referral_credit {
-  my( $self, $cust_pkg ) = @_;
-
-  my $cust_main = $self->cust_main($cust_pkg);
-
-  my $part_pkg = $cust_pkg->part_pkg;
-
-  my $what = $self->option('what');
-
-  #false laziness w/Condition/cust_payments_pkg.pm
-  if ( $what eq 'base_recur_permonth' ) { #huh.  yuck.
-    if ( $part_pkg->freq !~ /^\d+$/ ) {
-      die 'WARNING: Not crediting customer '. $cust_main->referral_custnum.
-          ' for package '. $cust_pkg->pkgnum.
-          ' ( customer '. $cust_pkg->custnum. ')'.
-          ' - Referral credits not (yet) available for '.
-          ' packages with '. $part_pkg->freq_pretty. ' frequency';
-    }
-  }
-
-  my $percent = $self->option('percent');
-
-  sprintf('%.2f', $part_pkg->$what($cust_pkg) * $percent / 100 );
-
-}
-
 1;
index 46f4e72..276889d 100644 (file)
@@ -1179,6 +1179,18 @@ sub calc_units  { 0; }
 #fallback for everything except bulk.pm
 sub hide_svc_detail { 0; }
 
+=item recur_cost_permonth CUST_PKG
+
+recur_cost divided by freq (only supported for monthly and longer frequencies)
+
+=cut
+
+sub recur_cost_permonth {
+  my($self, $cust_pkg) = @_;
+  return 0 unless $self->freq =~ /^\d+$/ && $self->freq > 0;
+  sprintf('%.2f', $self->recur_cost / $self->freq );
+}
+
 =item format OPTION DATA
 
 Returns data formatted according to the function 'format' described
index 321025b..3162e3a 100644 (file)
@@ -49,13 +49,29 @@ my $groups_sub = sub {
 
 };
 
+my $cust_sub = sub {
+  my $access_user = shift;
+  $access_user->user_custnum ? $access_user->user_cust_main->name : '';
+};
+my $cust_link = [ $p.'view/cust_main.cgi?custnum=', 'user_custnum' ];
+
 my $count_query = 'SELECT COUNT(*) FROM access_user';
 
 my $link = [ $p.'edit/access_user.html?', 'usernum' ];
 
-my @header = ( '#',       'Username', 'Full name', 'Groups'    );
-my @fields = ( 'usernum', 'username', 'name',      $groups_sub );
-my $align = 'rlll';
-my @links = ( $link, $link, $link, '' );
+my @header = ( '#',       'Username', 'Full name', 'Groups',    'Customer' );
+my @fields = ( 'usernum', 'username', 'name',      $groups_sub, $cust_sub, );
+my $align = 'rllll';
+my @links = ( $link, $link, $link, '', $cust_link );
+
+#if ( FS::Conf->new->config('ticket_system') ) {
+#  push @header, 'Ticketing';
+#  push @fields, sub {
+#    my $access_user = shift;
+#
+#  };
+#  $align .= 'l';
+#  push @links, '';
+#}
 
 </%init>
index 73488ef..1f52b47 100644 (file)
@@ -7,16 +7,18 @@
                                { field=>'_password2', type=>'password' },
                                'last',
                                'first',
+                               { field=>'user_custnum', type=>'search-cust_main', },
                                { field=>'disabled', type=>'checkbox', value=>'Y' },
                              ],
                  'labels' => { 
-                               'usernum'   => 'User number',
-                               'username'  => 'Username',
-                               '_password' => 'Password',
-                               '_password2'=> 'Re-enter Password',
-                               'last'      => 'Last name',
-                               'first'     => 'First name',
-                               'disabled'  => 'Disable employee',
+                               'usernum'      => 'User number',
+                               'username'     => 'Username',
+                               '_password'    => 'Password',
+                               '_password2   '=> 'Re-enter Password',
+                               'last'         => 'Last name',
+                               'first'        => 'First name',
+                               'user_custnum' => 'Customer (optional)',
+                               'disabled'     => 'Disable employee',
                              },
                  'edit_callback' => sub { my( $c, $o ) = @_; 
                                           $o->set('_password', '');
index ef0d22c..23c4369 100644 (file)
@@ -3,36 +3,37 @@
 Example:
 
   include( '/elements/search-cust_main.html,
-             'field_name'  => 'custnum',
+             'field'       => 'custnum',
+             #slightly deprecated old synonym for field#'field_name'=>'custnum',
              'find_button' => 1, #add a "find" button to the field
              'curr_value'  => 54, #current value
              'value        => 32, #deprecated synonym for curr_value
   );
 
 </%doc>
-<INPUT TYPE="hidden" NAME="<% $opt{'field_name'} %>" VALUE="<% $value %>">
+<INPUT TYPE="hidden" NAME="<% $field %>" VALUE="<% $value %>">
 
 <!-- some false laziness w/ misc/batch-cust_pay.html, though not as bad as i'd thought at first... -->
 
 <INPUT TYPE = "text"
-       NAME = "<% $opt{'field_name'} %>_search"
-       ID   = "<% $opt{'field_name'} %>_search"
+       NAME = "<% $field %>_search"
+       ID   = "<% $field %>_search"
        SIZE = "32"
        VALUE="<% $cust_main ? $cust_main->name : '(cust #, name or company)' %>"
-       onFocus="clearhint_<% $opt{'field_name'} %>_search(this);"
-       onClick="clearhint_<% $opt{'field_name'} %>_search(this);"
-       onChange="smart_<% $opt{'field_name'} %>_search(this);"
+       onFocus="clearhint_<% $field %>_search(this);"
+       onClick="clearhint_<% $field %>_search(this);"
+       onChange="smart_<% $field %>_search(this);"
 >
 
 % if ( $opt{'find_button'} ) {
     <INPUT TYPE    = "button"
            VALUE   = 'Find',
-           NAME    = "<% $opt{'field_name'} %>_findbutton"
-           onClick = "smart_<% $opt{'field_name'} %>_search(this.form.<% $opt{'field_name'} %>_search);"
+           NAME    = "<% $field %>_findbutton"
+           onClick = "smart_<% $field %>_search(this.form.<% $field %>_search);"
     >
 % }
 
-<SELECT NAME="<% $opt{'field_name'} %>_select" ID="<% $opt{'field_name'} %>_select" STYLE="color:#ff0000; display:none" onChange="select_<% $opt{'field_name'} %>(this);">
+<SELECT NAME="<% $field %>_select" ID="<% $field %>_select" STYLE="color:#ff0000; display:none" onChange="select_<% $field %>(this);">
 </SELECT>
 
 <% include('/elements/xmlhttp.html',
@@ -43,7 +44,7 @@ Example:
 
 <SCRIPT TYPE="text/javascript">
 
-  function clearhint_<% $opt{'field_name'} %>_search (what) {
+  function clearhint_<% $field %>_search (what) {
 
     what.style.color = '#000000';
 
@@ -55,7 +56,7 @@ Example:
 
   }
 
-  function smart_<% $opt{'field_name'} %>_search(what) {
+  function smart_<% $field %>_search(what) {
 
     var customer = what.value;
 
@@ -73,11 +74,11 @@ Example:
     what.style.color= '#000000';
     what.style.backgroundColor = '#dddddd';
 
-    var customer_select = document.getElementById('<% $opt{'field_name'} %>_select');
+    var customer_select = document.getElementById('<% $field %>_select');
 
     //alert("search for customer " + customer);
 
-    function <% $opt{'field_name'} %>_search_update(customers) {
+    function <% $field %>_search_update(customers) {
 
       //alert('customers returned: ' + customers);
 
@@ -88,7 +89,7 @@ Example:
 
       if ( customerArray.length == 0 ) {
 
-        what.form.<% $opt{'field_name'} %>.value = '';
+        what.form.<% $field %>.value = '';
 
         what.value = 'Customer not found: ' + what.value;
         what.style.color = '#ff0000';
@@ -100,7 +101,7 @@ Example:
 
         //alert('one customer found: ' + customerArray[0]);
 
-        what.form.<% $opt{'field_name'} %>.value = customerArray[0][0];
+        what.form.<% $field %>.value = customerArray[0][0];
         what.value = customerArray[0][1];
 
         what.style.display = '';
@@ -129,17 +130,17 @@ Example:
 
     }
 
-    smart_search( customer, <% $opt{'field_name'} %>_search_update );
+    smart_search( customer, <% $field %>_search_update );
 
 
   }
 
-  function select_<% $opt{'field_name'} %> (what) {
+  function select_<% $field %> (what) {
 
     var custnum = what.options[what.selectedIndex].value;
     var customer = what.options[what.selectedIndex].text;
 
-    var customer_obj = document.getElementById('<% $opt{'field_name'} %>_search');
+    var customer_obj = document.getElementById('<% $field %>_search');
 
     if ( custnum == '' ) {
       //what.style.color = '#ff0000';
@@ -154,7 +155,7 @@ Example:
 
     } else {
     
-      what.form.<% $opt{'field_name'} %>.value = custnum;
+      what.form.<% $field %>.value = custnum;
 
       customer_obj.value = customer;
       customer_obj.style.color = '#000000';
@@ -177,7 +178,8 @@ Example:
 <%init>
 
 my( %opt ) = @_;
-$opt{'field_name'} ||= 'custnum';
+
+my $field = $opt{'field'} || $opt{'field_name'} || 'custnum';
 
 my $value = $opt{'curr_value'} || $opt{'value'};
 
diff --git a/httemplate/elements/tr-search-cust_main.html b/httemplate/elements/tr-search-cust_main.html
new file mode 100644 (file)
index 0000000..9df91a1
--- /dev/null
@@ -0,0 +1,15 @@
+<% include('tr-td-label.html', @_ ) %>
+
+  <TD <% $colspan %> <% $cell_style %> ID="<% $opt{input_id} || $opt{id}.'_input0' %>"><% include('search-cust_main.html', @_ ) %></TD>
+
+</TR>
+
+<%init>
+
+my %opt = @_;
+
+my $cell_style = $opt{'cell_style'} ? 'STYLE="'. $opt{'cell_style'}. '"' : '';
+
+my $colspan = $opt{'colspan'} ? 'COLSPAN="'.$opt{'colspan'}.'"' : '';
+
+</%init>