summaryrefslogtreecommitdiff
path: root/rt/share/html/Elements/CustomerFields
blob: d5419d21373e5c41dbe6855804b67820d2c2dc9f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<%doc>
All accessible Freeside customer fields fields go in here.  Those of 
them outside cust_main also need to go in RT::URI::freeside::Internal
(where they should be pulled into CustomerInfo).  Nothing should need 
to go in RT::Tickets_Overlay; it already resolves "Customer.foo" as 
"cust_main.foo", and "Customer.cust_bar.foo" as "JOIN cust_bar using 
(custnum) ... cust_bar.foo".

About the keys:
- 'Value' makes the field a search criterion.  This also requires 'Op'.
  See Search/Elements/PickBasics.
- 'Display' makes it an output column, and is either the cust_main 
  field, the CustomerInfo key, or a coderef that takes the RT::Ticket
  as an argument.
- 'OrderBy' makes it a sort key, and must be set to an RT-SQL field
  name to sort by.
</%doc>
<%once>

my @customer_fields = ( # ordered
  {
    # custnum
    Name    => 'Customer',
    Label   => 'Customer',
    Display => sub {
                my $Ticket = shift;
                my @return = ();
                foreach my $c (ticket_cust_resolvers($Ticket)) {
                    push @return, \'<A HREF="', $c->HREF, \'">',
                                  $c->AsString,
                                  \'</A>',
                                  \'<BR>';
                }
                pop @return;
                @return;
              },
    OrderBy => 'Customer.Number',
  },
  {
    #Column name (format string)
    Name    => 'Agent',
    # Column heading/query builder name
    Label   => 'Agent',
    # Column value (coderef, cust_main field, or CustomerInfo key)
    Display => 'AgentName',
    # Query builder options
    # RT-SQL field, defaults to Name
    QueryName => 'Customer.agentnum',
    #QueryLabel => 'Agent' #defaults to Label
    Op      => equals_notequals,
    Value   => select_table('agent', 'agentnum', 'agent'),
    # RT-SQL sort key (if any)
    OrderBy => 'Customer.agentnum',
  },
  {
    Name    => 'CustomerClass',
    Label   => 'Customer Class',
    Display => 'CustomerClass',
    QueryName => 'Customer.classnum',
    Op      => equals_notequals,
    Value   => select_table('cust_class', 'classnum', 'classname'),
    OrderBy => 'Customer.classnum',
  },
  {
    Name    => 'AdvertisingSource',
    Label   => 'Advertising Source',
    Display => 'Referral',
    QueryName => 'Customer.refnum',
    Op      => equals_notequals,
    Value   => select_table('part_referral', 'refnum', 'referral'),
    OrderBy => 'Customer.refnum',
  },
  {
    Name    => 'BillingType',
    Label   => 'Billing Type',
    Display => 'BillingType',
    QueryName => 'Customer.payby',
    Op      => equals_notequals,
    Value   => {
      Type => 'select',
      Options => [ '' => '-',
        map { $_, FS::payby->longname($_) } FS::payby->cust_payby 
      ],
    },
  },
  {
    Name    => 'InvoiceEmail',
    Label   => 'Invoice Email',
    Display => 'InvoiceEmail',
    # query/sort needed?
  },
  {
    Name    => 'City',
    Label   => 'City',
    Display => 'city',
    OrderBy => 'Customer.city',
  },
  {
    Name    => 'State',
    Label   => 'State',
    Display => 'state',
    OrderBy => 'Customer.state',
  },
  {
    Name    => 'CustomerTags',
    Label   => '',
    Display => sub {
                my $Ticket = shift;
                my @return = ();
                foreach my $c (ticket_cust_resolvers($Ticket)) {
                  foreach my $t (@{ $c->CustomerInfo->{CustomerTags} }) {
                    push @return, \'<SPAN style="background-color:#',
                    $t->{'color'},
                    \';">&nbsp;',
                    $t->{'name'},
                    \'&nbsp;</SPAN>',
                    \'&nbsp;'
                    ;
                  }
                  pop @return;
                  push @return, \'<BR>';
                }
                pop @return;
                @return;
              },
    QueryName => 'Customer.cust_tag.tagnum',
    QueryLabel => 'Tag',
    Op      => equals_notequals,
    Value   => select_table('part_tag', 'tagnum', 'tagname'),
    OrderBy => '',
  },
);

#helper subs
#Op      
sub equals_notequals {
  return {
      Type => 'component',
      Path => '/Elements/SelectBoolean',
      Arguments => { TrueVal=> '=', FalseVal=> '!=' },
  }
}

#Value
sub select_table {
  my ($table, $value_col, $name_col, $hashref) = @_;
  $hashref ||= { disabled => '' }; # common case
  return {
    Type => 'select',
    Options => [ 
      '' => '-',
      map { $_->$value_col, $_->$name_col }
      qsearch($table, $hashref)
    ],
  }
}

sub ticket_cust_resolvers {
    my $Ticket = shift;
    my @Customers = map { $_->TargetURI->Resolver->CustomerResolver }
                      @{ $Ticket->Customers->ItemsArrayRef };
    # this can contain cust_svc links, careful
    # uniq
    my %seen = map { $_->URI => $_ } @Customers;
    values %seen;
}

sub cust_info_attribute { # the simple case of $resolver->CustomerInfo->{foo}
    my $attribute = shift;
    sub {
        my $Ticket = shift;
        my @return;
        foreach my $c (ticket_cust_resolvers($Ticket)) {
            push @return, $c->CustomerInfo->{$attribute}, '<BR>';
        }
        pop @return; #trailing <BR>
        @return;
    };
}

</%once>
<%init>

my $arg = shift;
if ( $arg eq 'Names' ) {
  return map { $_->{Name} } @customer_fields;
}
elsif ( $arg eq 'ColumnMap' ) {
  return map {
    my $f = $_;

    $f->{Name} => {
        title     => $f->{Label},
        attribute => $f->{OrderBy} || '',
        value     => ref($f->{Display}) eq 'CODE' ? 
                      $f->{Display} : 
                      cust_info_attribute($f->{Display})
      }
  } #map
  grep { exists $_->{Display} }
  @customer_fields;
}
elsif ( $arg eq 'Criteria' ) {
  return map {
    my $f = $_;
    # argument to Search/Elements/ConditionRow
    $f->{Condition} ||
    {
      Name  => ($f->{QueryName} || $f->{Name}),
      Field => ($f->{QueryLabel} || $f->{Label}),
      Op    => $f->{Op},
      Value => $f->{Value},
    }
  } #map
  grep { exists $_->{Condition} || exists $_->{Value} }
  @customer_fields;
}
else { die "unknown CustomerFields mode '$arg'\n"; }
</%init>