customer tags, RT#9192
authorivan <ivan>
Fri, 16 Jul 2010 23:45:24 +0000 (23:45 +0000)
committerivan <ivan>
Fri, 16 Jul 2010 23:45:24 +0000 (23:45 +0000)
FS/FS/AccessRight.pm
FS/FS/Record.pm
FS/FS/cust_main.pm
FS/FS/cust_tag.pm
httemplate/edit/cust_main/top_misc.html
httemplate/edit/part_tag.html
httemplate/edit/process/cust_main.cgi
httemplate/elements/select-cust_tag.html [new file with mode: 0644]
httemplate/elements/tr-select-cust_tag.html [new file with mode: 0644]
httemplate/view/cust_main/misc.html

index 9e79754..92c4d22 100644 (file)
@@ -108,6 +108,7 @@ tie my %rights, 'Tie::IxHash',
     'View customer',
     #'View Customer | View tickets',
     'Edit customer',
+    'Edit customer tags',
     'Edit referring customer',
     'View customer history',
     'Cancel customer',
index f3dead1..cd5e2d4 100644 (file)
@@ -795,6 +795,17 @@ sub setfield {
   $self->set(@_);
 }
 
+=item exists COLUMN
+
+Returns true if the column/field/key COLUMN exists.
+
+=cut
+
+sub exists {
+  my($self,$field) = @_;
+  exists($self->{Hash}->{$field});
+}
+
 =item AUTLOADED METHODS
 
 $record->column is a synonym for $record->get('column');
index d8f525e..84bd6a0 100644 (file)
@@ -57,6 +57,7 @@ use FS::cust_tax_location;
 use FS::part_pkg_taxrate;
 use FS::agent;
 use FS::cust_main_invoice;
+use FS::cust_tag;
 use FS::cust_credit_bill;
 use FS::cust_bill_pay;
 use FS::prepay_credit;
@@ -473,6 +474,30 @@ sub insert {
     $self->invoicing_list( $invoicing_list );
   }
 
+  warn "  setting customer tags\n"
+    if $DEBUG > 1;
+
+  foreach my $tagnum ( @{ $self->tagnum || [] } ) {
+    my $cust_tag = new FS::cust_tag { 'tagnum'  => $tagnum,
+                                      'custnum' => $self->custnum };
+    my $error = $cust_tag->insert;
+    if ( $error ) {
+      $dbh->rollback if $oldAutoCommit;
+      return $error;
+    }
+  }
+
+  if ( $invoicing_list ) {
+    $error = $self->check_invoicing_list( $invoicing_list );
+    if ( $error ) {
+      $dbh->rollback if $oldAutoCommit;
+      #return "checking invoicing_list (transaction rolled back): $error";
+      return $error;
+    }
+    $self->invoicing_list( $invoicing_list );
+  }
+
+
   warn "  setting cust_main_exemption\n"
     if $DEBUG > 1;
 
@@ -1356,23 +1381,13 @@ sub delete {
     }
   }
 
-  foreach my $cust_main_invoice ( #(email invoice destinations, not invoices)
-    qsearch( 'cust_main_invoice', { 'custnum' => $self->custnum } )
-  ) {
-    my $error = $cust_main_invoice->delete;
-    if ( $error ) {
-      $dbh->rollback if $oldAutoCommit;
-      return $error;
-    }
-  }
-
-  foreach my $cust_main_exemption (
-    qsearch( 'cust_main_exemption', { 'custnum' => $self->custnum } )
-  ) {
-    my $error = $cust_main_exemption->delete;
-    if ( $error ) {
-      $dbh->rollback if $oldAutoCommit;
-      return $error;
+  foreach my $table (qw( cust_main_invoice cust_main_exemption cust_tag )) {
+    foreach my $record ( qsearch( 'table', { 'custnum' => $self->custnum } ) ) {
+      my $error = $record->delete;
+      if ( $error ) {
+        $dbh->rollback if $oldAutoCommit;
+        return $error;
+      }
     }
   }
 
@@ -1478,6 +1493,28 @@ sub replace {
     $self->invoicing_list( $invoicing_list );
   }
 
+  if ( $self->exists('tagnum') ) { #so we don't delete these on edit by accident
+
+    #this could be more efficient than deleting and re-inserting, if it matters
+    foreach my $cust_tag (qsearch('cust_tag', {'custnum'=>$self->custnum} )) {
+      my $error = $cust_tag->delete;
+      if ( $error ) {
+        $dbh->rollback if $oldAutoCommit;
+        return $error;
+      }
+    }
+    foreach my $tagnum ( @{ $self->tagnum || [] } ) {
+      my $cust_tag = new FS::cust_tag { 'tagnum'  => $tagnum,
+                                        'custnum' => $self->custnum };
+      my $error = $cust_tag->insert;
+      if ( $error ) {
+        $dbh->rollback if $oldAutoCommit;
+        return $error;
+      }
+    }
+
+  }
+
   my %options = @param;
 
   my $tax_exemption = delete $options{'tax_exemption'};
@@ -2475,6 +2512,31 @@ sub agent_name {
   $self->agent->agent;
 }
 
+=item cust_tag
+
+Returns any tags associated with this customer, as FS::cust_tag objects,
+or an empty list if there are no tags.
+
+=cut
+
+sub cust_tag {
+  my $self = shift;
+  qsearch('cust_tag', { 'custnum' => $self->custnum } );
+}
+
+=item part_tag
+
+Returns any tags associated with this customer, as FS::part_tag objects,
+or an empty list if there are no tags.
+
+=cut
+
+sub part_tag {
+  my $self = shift;
+  map $_->part_tag, $self->cust_tag; 
+}
+
+
 =item cust_class
 
 Returns the customer class, as an FS::cust_class object, or the empty string
index 1d174d1..5dfd156 100644 (file)
@@ -2,7 +2,9 @@ package FS::cust_tag;
 
 use strict;
 use base qw( FS::Record );
-use FS::Record qw( qsearch qsearchs );
+use FS::Record qw( qsearchs );
+use FS::cust_main;
+use FS::part_tag;
 
 =head1 NAME
 
@@ -112,6 +114,25 @@ sub check {
   $self->SUPER::check;
 }
 
+=item cust_main
+
+=cut
+
+sub cust_main {
+  my $self = shift;
+  qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
+}
+
+=item part_tag
+
+=cut
+
+sub part_tag {
+  my $self = shift;
+  qsearchs( 'part_tag', { 'tagnum' => $self->tagnum } );
+}
+
+
 =back
 
 =head1 BUGS
index 36add37..916bac0 100644 (file)
@@ -1,5 +1,12 @@
 <% &ntable("#cccccc") %>
 
+%# tags
+<% include('/elements/tr-select-cust_tag.html',
+             'custnum' => $custnum,
+             'cgi'     => $cgi,
+          )
+%>
+
 %# agent
 <% include('/elements/tr-select-agent.html', 
               'curr_value'    => $cust_main->agentnum,
index 938e342..56ba567 100644 (file)
 die "access denied"
   unless $FS::CurrentUser::CurrentUser->access_right('Configuration');
 
+#FF6666 red
+#FF9966 orange
+#FFFF66 yellow
+#66FF66 green
+#66FFFF cyan?
+#6666FF blue
+#FF66FF purple?  looks more like pink.  CC66FF?
+
 </%init>
index 1311ba4..3158d7b 100755 (executable)
@@ -73,6 +73,8 @@ if ( defined($cgi->param('same')) && $cgi->param('same') eq "Y" ) {
   );
 }
 
+$new->tagnum( [ $cgi->param('tagnum') ] );
+
 my %usedatetime = ( 'birthdate' => 1 );
 
 foreach my $dfield (qw( birthdate signupdate )) {
diff --git a/httemplate/elements/select-cust_tag.html b/httemplate/elements/select-cust_tag.html
new file mode 100644 (file)
index 0000000..61d4dca
--- /dev/null
@@ -0,0 +1,20 @@
+<% include( '/elements/select-table.html',
+                 'table'         => 'part_tag',
+                 'name_col'      => 'tagname', #tagname - tagdesc??
+                 'multiple'      => 1,
+                 #'value'         => $agentnum || '',
+                 #'agent_virt'    => 1,
+                 'hashref'       => { 'disabled' => '' },
+                 'order_by'      => ' ORDER BY tagname',
+                 %opt,
+             )
+%>
+<%init>
+
+my %opt = @_;
+#my $agentnum = $opt{'curr_value'} || $opt{'value'};
+
+$opt{'records'} = delete $opt{'part_tag'}
+  if $opt{'part_tag'};
+
+</%init>
diff --git a/httemplate/elements/tr-select-cust_tag.html b/httemplate/elements/tr-select-cust_tag.html
new file mode 100644 (file)
index 0000000..d88f3a8
--- /dev/null
@@ -0,0 +1,46 @@
+% if ( $curuser->access_right('Edit customer tags') && @part_tag ) {
+
+  <TR>
+    <TD ALIGN="right"><% $opt{'label'} || 'Tags' %></TD>
+    <TD>
+      <% include( '/elements/select-cust_tag.html',
+                     'curr_value' => \@curr_tagnum,
+                     'part_tag'   => \@part_tag,
+                     %opt,
+                 )
+      %>
+    </TD>
+  </TR>
+
+% } else {
+
+%   foreach my $tagnum (@curr_tagnum) {
+      <INPUT TYPE="hidden" NAME="tagnum" VALUE="<% $tagnum %>">
+%   }
+
+% }
+<%init>
+
+my $curuser = $FS::CurrentUser::CurrentUser;
+
+my %opt = @_;
+my $cgi = $opt{'cgi'};
+
+my @curr_tagnum = ();
+if ( $cgi->param('error') ) {
+  @curr_tagnum = $cgi->param('tagnum');
+} elsif ( $opt{'custnum'} ) {
+  @curr_tagnum = map $_->tagnum,
+                     qsearch('cust_tag', { 'custnum' => $opt{'custnum'} } );
+}
+
+my $extra_sql = "WHERE disabled IS NULL OR disabled = '' ";
+$extra_sql .= ' OR tagnum IN ('. join(',', @curr_tagnum). ')' if @curr_tagnum;
+
+my @part_tag = qsearch({
+  'table'     => 'part_tag',
+  'hashref'   => {},
+  'extra_sql' => $extra_sql,
+});
+
+</%init>
index d383e4a..2243647 100644 (file)
   <TD BGCOLOR="#ffffff"><FONT COLOR="#<% $cust_main->statuscolor %>"><B><% ucfirst($cust_main->status) %></B></FONT></TD>
 </TR>
 
+% my @part_tag = $cust_main->part_tag;
+% if ( @part_tag ) {
+<TR>
+  <TD ALIGN="right">Tags</TD>
+  <TD BGCOLOR="#ffffff">
+%   foreach my $part_tag ( @part_tag ) {
+      <FONT <% length($part_tag->tagcolor)
+                 ? 'STYLE="background-color:#'.$part_tag->tagcolor.'"'
+                 : '' %>
+      ><% $part_tag->tagname.': '. $part_tag->tagdesc |h %></FONT>
+      <BR>
+%   }
+  </TD>
+</TR>
+% }
+
 %unless ( scalar(@agentnums) == 1
 %         && !$curuser->access_right('View customers of all agents')  ) {
 %  my $agent = qsearchs('agent',{ 'agentnum' => $cust_main->agentnum } );