fix A/R report
[freeside.git] / httemplate / search / svc_broadband-map.html
1 <& /elements/header.html, 'Broadband Search Results' &>
2   
3 <& elements/gmap.html, features => \@features, overlays => \@overlays &>
4
5 <& /elements/footer.html &>
6 <%init>
7
8 die "access denied" unless
9   $FS::CurrentUser::CurrentUser->access_right('List services');
10
11 my $conf = new FS::Conf;
12
13 my @features; # geoJSON structure
14
15 # accept all the search logic from svc_broadband.cgi...
16 my %search_hash;
17 if ( $cgi->param('magic') eq 'unlinked' ) {
18   %search_hash = ( 'unlinked' => 1 );
19 } else {
20   foreach (qw( custnum agentnum svcpart cust_fields )) {
21     $search_hash{$_} = $cgi->param($_) if $cgi->param($_);
22   }
23   foreach (qw(pkgpart routernum towernum sectornum)) {
24     $search_hash{$_} = [ $cgi->param($_) ] if $cgi->param($_);
25   }
26 }
27
28 if ( $cgi->param('sortby') =~ /^(\w+)$/ ) {
29   $search_hash{'order_by'} = "ORDER BY $1";
30 }
31
32 my $sql_query = FS::svc_broadband->search(\%search_hash);
33
34 my %routerbyblock = ();
35
36 my @rows = qsearch($sql_query);
37 my %sectors;
38 my %towers;
39 my %tower_coord;
40 my %tower_bounds;
41 foreach my $svc_broadband (@rows) {
42   # don't try to show it if coords aren't set
43   next if !$svc_broadband->latitude || !$svc_broadband->longitude;
44   # coerce coordinates to numbers
45   my @coord = (
46     $svc_broadband->longitude + 0,
47     $svc_broadband->latitude + 0,
48   );
49   push @coord, $svc_broadband->altitude + 0
50     if length($svc_broadband->altitude); # it's optional
51
52   my $svcnum = $svc_broadband->svcnum;
53   my $color = $svc_broadband->addr_status_color;
54
55   push @features,
56   {
57     id        => 'svc_broadband/'.$svcnum,
58     geometry  => {
59       type        => 'Point',
60       coordinates => \@coord,
61     },
62     properties => {
63       #content => include('.svc_broadband', $svc_broadband),
64       url   => $fsurl . 'view/svc_broadband-popup.html?' . $svcnum,
65       style => {
66         icon => {
67           fillColor => $color,
68         },
69       },
70     },
71   };
72   # look up tower location and draw connecting line
73   next if !$svc_broadband->sectornum;
74   my $sector = $sectors{$svc_broadband->sectornum} ||= $svc_broadband->tower_sector;
75   my $towernum = $sector->towernum;
76   my $tower = $towers{$towernum};
77
78   if (!$tower) {
79     $tower = $towers{$towernum} = $sector->tower;
80     $tower_coord{$towernum} =
81       [ $tower->longitude + 0,
82         $tower->latitude + 0,
83         ($tower->altitude || 0) + 0,
84       ];
85
86   }
87
88   if ( $tower->latitude and $tower->longitude ) {
89     push @features,
90     {
91       geometry => {
92         type        => 'LineString',
93         coordinates => [ \@coord, $tower_coord{$towernum} ],
94       },
95       properties  => {
96         style       => {
97           strokeColor  => $color,
98           strokeWeight => 1,
99         },
100       },
101     };
102
103     # also extend tower's ROI to include this point
104     # (this is experimental; might get better results using the centroid of
105     # all connected services or something)
106     my $bounds = $tower_bounds{$towernum} ||= {
107       east => $tower->longitude,
108       west => $tower->longitude,
109       north => $tower->latitude,
110       south => $tower->latitude,
111     };
112     if ($coord[0] > $bounds->{east}) {
113       $bounds->{east} = $coord[0];
114     } elsif ($coord[0] < $bounds->{west}) {
115       $bounds->{west} = $coord[0];
116     }
117     if ($coord[1] > $bounds->{north}) {
118       $bounds->{north} = $coord[1]
119     } elsif ($coord[1] < $bounds->{south}) {
120       $bounds->{south} = $coord[1]
121     }
122
123   } # if tower has coords
124 } # foreach $svc_broadband
125
126 foreach my $tower (values(%towers)) {
127   my $towernum = $tower->towernum;
128   my $bounds = $tower_bounds{$towernum};
129   # add some padding for easier reading
130   my $dx = 0.1 * ($bounds->{east} - $bounds->{west});
131   my $dy = 0.1 * ($bounds->{north} - $bounds->{south});
132   $bounds->{east} += $dx; 
133   $bounds->{west} -= $dx;
134   $bounds->{north} += $dy;
135   $bounds->{south} -= $dy;
136   push @features,
137   {
138     id        => 'tower/'.$towernum,
139     geometry  => {
140       type        => 'Point',
141       coordinates => $tower_coord{$towernum},
142     },
143     properties => {
144       style     => {
145         icon => {
146           path        => undef,
147           url         => $fsurl.'images/antenna-square-21x51.png',
148           anchor      => { x => 10, y => 4 }
149         },
150       },
151       content   => include('.tower', $tower),
152       bounds    => $tower_bounds{$towernum},
153     },
154   };
155 }
156
157 my @overlays;
158 foreach my $sector (values %sectors) {
159   if ( length($sector->image) > 0 ) {
160     my $o = {
161       url => $fsurl.'view/sector_map-png.cgi?' . $sector->sectornum
162     };
163     foreach (qw(south north west east)) {
164       $o->{$_} = $sector->get($_) + 0;
165     }
166     push @overlays, $o;
167   };
168 };
169
170 </%init>
171 <%def .tower>
172 % my $tower = shift;
173 % my $can_edit = $FS::CurrentUser::CurrentUser->access_right('Configuration');
174 <H3>
175 % if ( $can_edit ) {
176   <a target="_blank" href="<% $fsurl %>edit/tower.html?<% $tower->towernum %>">
177 % }
178 Tower #<% $tower->towernum %> | <% $tower->towername %>
179 % if ( $can_edit ) {
180   </a>
181 % }
182 </H3>
183 </%def>