improve display of ticket service links, #17067
[freeside.git] / rt / share / html / Ticket / Elements / Customers
1 %# Copyright (c) 2004 Ivan Kohler <ivan-rt@420.am>
2 %#
3 %# This work is made available to you under the terms of Version 2 of
4 %# the GNU General Public License. A copy of that license should have
5 %# been provided with this software, but in any event can be snarfed
6 %# from www.gnu.org.
7 %# 
8 %# This work is distributed in the hope that it will be useful, but
9 %# WITHOUT ANY WARRANTY; without even the implied warranty of
10 %# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 %# General Public License for more details.
12 <%doc>
13 Provides resolver objects (RT::URI::freeside) for customer/service links 
14 to a specified ticket.
15
16 Do this:
17 %hash = $m->comp('Customers', Ticket => $ticket);
18
19 %hash contains four elements:
20 - "custnums", an arrayref of customer numbers that are linked to the ticket,
21   in order.
22 - "cust_main", a hashref of custnum => customer object, for each custnum.
23 - "cust_linked", a hashref of custnum => boolean flag.  If the flag is true,
24   then the customer is _explicitly_ linked (i.e. there is a 
25   'freeside://freeside/cust_main/' record in the Links table).  Otherwise,
26   the customer link is implied by a service link but doesn't exist in its
27   own right.
28 - "cust_svc", a hashref of custnum => an arrayref of service objects that 
29   are linked to the ticket and belong to that customer.
30 </%doc>
31 <%init>
32 my @custnums;
33 my %cust_main;
34 my %cust_svc;
35 my %cust_linked;
36 my $customers = $Ticket->Customers;
37 # ensure each customer is displayed only once
38 while (my $link = $customers->Next) {
39   my $uri = $link->Target;
40   if ( $uri =~ /cust_main\/(\d+)/ ) {
41     $cust_main{$1} = $link->TargetURI->Resolver;
42     $cust_linked{$1} = 1;
43   } elsif ( $uri =~ /cust_svc\/(\d+)/ ) {
44     my $svc = $link->TargetURI->Resolver;
45     my $cust = $svc->CustomerResolver;
46     my $custnum = $cust->{fspkey};
47     $cust_main{$custnum} ||= $cust;
48     $cust_svc{$custnum} ||= [];
49     push @{$cust_svc{$custnum}}, $svc;
50   }
51 }
52 @custnums = sort { $a <=> $b } keys %cust_main;
53 return (
54   'custnums'    => \@custnums,
55   'cust_main'   => \%cust_main,
56   'cust_svc'    => \%cust_svc,
57   'cust_linked' => \%cust_linked,
58 );
59 </%init>
60 <%ARGS>
61 $Ticket => undef
62 </%ARGS>