fix top subtotals on refund reports
[freeside.git] / httemplate / search / cust_attachment.html
1 <% include( 'elements/search.html',
2                  'title'       => 'Attachments',
3                  'menubar'     => '',
4                  'name'        => ($disabled ? 'deleted' : '') .' attachments',
5                  'html_init'   => include('/elements/init_overlib.html') .
6                     ($curuser->access_right('View deleted attachments') ? (
7                     selflink('Show '.($disabled ? 'active' : 'deleted'),
8                              show_deleted => (1-$disabled))) : ''),
9                  'html_form'   => 
10                     qq!<FORM NAME="attachForm" ACTION="$p/misc/cust_attachment.cgi" METHOD="POST">
11                     <INPUT TYPE="hidden" NAME="orderby" VALUE="$orderby">
12                     <INPUT TYPE="hidden" NAME="show_deleted" VALUE="$disabled">!
13                     , 
14                  'query'       => $query,
15                  'count_query' => $count_query,
16                  'header' => [ selflink('#',orderby => 'attachnum'),
17                                #selflink('Customer',orderby => 'custnum'),
18                                FS::UI::Web::cust_header(
19                                  $cgi->param('cust_fields')
20                                ),
21                                selflink('Date',orderby => '_date'),
22                                selflink('Filename',orderby => 'filename'),
23                                selflink('Size',orderby => 'length(body)'),
24                                selflink('Uploaded by',orderby => 'otaker'),
25                                selflink('Description',orderby => 'title'),
26                                '', # checkbox column
27                              ],
28                  'fields' => [
29                                'attachnum',
30                                #$sub_cust,
31                                \&FS::UI::Web::cust_fields,
32                                $sub_date,
33                                'filename',
34                                $sub_size,
35                                'otaker',
36                                'title',
37                                $sub_checkbox,
38                              ],
39                  'links' => [ '',
40                               ( map { $_ ne 'Cust. Status' ? $clink : '' }
41                                     FS::UI::Web::cust_header(
42                                       $cgi->param('cust_fields')
43                                     )
44                               ),
45                             ],
46                  'link_onclicks' => [
47                               '',
48                               ( map '', FS::UI::Web::cust_header(
49                                           $cgi->param('cust_fields')
50                                         )
51                               ),
52                               '',
53                               $sub_edit_link,
54                               ],
55                  'color' => [
56                               '',
57                               FS::UI::Web::cust_colors(),
58                             ],
59                  'style' => [
60                               '',
61                               FS::UI::Web::cust_styles(),
62                             ],
63                  'align'  => 'r'. FS::UI::Web::cust_aligns(),
64
65                  #'links' =>  [
66                  #              '',
67                  #              '',
68                  #              '',
69                  #              '',
70                  #              '',
71                  #              '', #$acct_link,
72                  #              '',
73                 'html_foot' => $sub_foot,
74              )
75
76 %>
77
78
79 <%init>
80
81 my $curuser = $FS::CurrentUser::CurrentUser;
82 die "access denied" if !$curuser->access_right('View attachments')
83                     or !$curuser->access_right('Browse attachments');
84
85 my $conf = new FS::Conf;
86
87 my $noactions = 1;
88 my $areboxes = 0;
89
90 my $disabled = 0;
91
92 if($cgi->param('show_deleted')) {
93   if ($curuser->access_right('View deleted attachments')) {
94     $disabled = 1;
95     if ($curuser->access_right('Purge attachment') or
96         $curuser->access_right('Undelete attachment')) {
97       $noactions = 0;
98     }
99   }
100   else {
101     die "access denied";
102   }
103 }
104 else {
105   if ($curuser->access_right('Delete attachment')) {
106     $noactions = 0;
107   }
108 }
109
110 my $orderby = $cgi->param('orderby') || 'custnum';
111
112 my $extra_sql = '';
113 if ( $cgi->param('agentnum') && $cgi->param('agentnum') =~ /^(\d+)$/ ) {
114   $extra_sql = " AND agentnum = $1 ";
115 }
116
117 my $query = {
118     table     => 'cust_attachment',
119     hashref   => { disabled => $disabled ? { op => '>', value => 0 } : '', },
120     order_by  => "ORDER BY $orderby",
121     addl_from => 'JOIN cust_main USING (custnum)',
122     extra_sql => "$extra_sql AND ". $curuser->agentnums_sql,
123 };
124
125 my $count_query = 'SELECT COUNT(*) FROM cust_attachment'
126   . ' JOIN cust_main USING (custnum)'
127   . ' WHERE ' . $curuser->agentnums_sql
128   . ' AND disabled ' . ( $disabled ? '> 0' : 'IS NULL' );
129
130 #still need to report on attachments with deleted customer?  did we ever?
131 #my $sub_cust = sub {
132 #  my $c = qsearchs('cust_main', { custnum => shift->custnum } );
133 #  return $c ? encode_entities($c->name) : '<FONT COLOR="red"><B>(not found)</B></FONT>';
134 #};
135
136 my $clink = [ $p.'view/cust_main.cgi?', 'custnum' ];
137
138 my $sub_date = sub {
139   time2str("%b %o, %Y", shift->_date);
140 };
141
142 my $sub_size = sub {
143   my $size = shift->size;
144   return $size if $size < 1024;
145   return int($size/1024).'K' if $size < 1048576;
146   return int($size/1048576).'M';
147 };
148
149 my $sub_checkbox = sub {
150   return '' if $noactions;
151   my $attach = shift;
152   my $attachnum = $attach->attachnum;
153   $areboxes = 1;
154   return qq!<INPUT NAME="attachnum$attachnum" TYPE="checkbox" VALUE="1">!;
155 };
156
157 my $sub_edit_link = sub {
158   my $attach = shift;
159   my $attachnum = $attach->attachnum;
160   my $custnum = $attach->custnum;
161   return include('/elements/popup_link_onclick.html',
162            action => popurl(2).'edit/cust_main_attach.cgi?'.
163                      "custnum=$custnum;attachnum=$attachnum",
164            actionlabel => 'Edit attachment properties',
165            width  => 510,
166            height => 315,
167            frame  => 'top',
168            );
169 };
170
171 sub selflink {
172   my $label = shift;
173   my %new_param = @_;
174   my $param = $cgi->Vars;
175   my %old_param = %$param;
176   @{$param}{keys(%new_param)} = values(%new_param);
177   my $link = '<a href="'.$cgi->self_url.'">'.$label.'</a>';
178   %$param = %old_param;
179   return $link;
180 }
181
182 sub confirm {
183   my $action = shift;
184   my $onclick = "return(confirm('$action all selected files?'))";
185   return qq!onclick="$onclick"!;
186 }
187
188 my $sub_foot = sub {
189   return '' if ($noactions or !$areboxes);
190   my $foot = 
191 '<BR><INPUT TYPE="button" VALUE="Select all" onClick="setAll(true)">
192 <INPUT TYPE="button" VALUE="Unselect all" onClick="setAll(false)">';
193   if ($disabled) {
194     if ($curuser->access_right('Undelete attachment')) {
195       $foot .= '<BR><INPUT TYPE="submit" NAME="action" VALUE="Undelete selected">';
196     }
197     if ($curuser->access_right('Purge attachment')) {
198       $foot .= '<BR><INPUT TYPE="submit" NAME="action" VALUE="Purge selected" '.confirm('Purge').'>';
199     }
200   }
201   else {
202     $foot .= '<BR><INPUT TYPE="submit" NAME="action" VALUE="Delete selected" '.confirm('Delete').'>';
203   }
204   $foot .= 
205 '<SCRIPT TYPE="text/javascript">
206   function setAll(setTo) { 
207     theForm = document.attachForm;
208     for (i=0,n=theForm.elements.length;i<n;i++)
209       if (theForm.elements[i].name.indexOf("attachnum") != -1)
210         theForm.elements[i].checked = setTo;
211   }
212 </SCRIPT>';
213   return $foot;
214 };
215
216 </%init>