RT# 75817 - updated UI to be more intuitive
[freeside.git] / httemplate / misc / send-report.html
1 <%doc>
2
3 Parameters:
4
5 - reportname: the report name (per FS::report_batch)
6
7 </%doc>
8 <& /elements/header-popup.html, { title => $report_info->{name} } &>
9 <script type="text/javascript">
10
11 $().ready(function() {
12   var agent_info = <% encode_json(\%agent) %>;
13
14   $('#agentnum').on('change', function() {
15     var agentnum = this.value;
16     if ( agent_info[agentnum] ) {
17       $('#msgnum').prop('value',         agent_info[agentnum]['msgnum']);
18       $('#beginning_text').prop('value', agent_info[agentnum]['beginning']);
19       $('#ending_text').prop('value',    agent_info[agentnum]['ending']);
20     } else {
21       $('#msgnum').prop('value',         '');
22       $('#beginning_text').prop('value', '');
23       $('#ending_text').prop('value',    '');
24     }
25   });
26
27   $('#agentnum').trigger('change');
28
29 });
30
31 </script>
32 <FORM NAME="OneTrueForm" ACTION="process/send-report.html" METHOD="POST">
33
34 <table class="inv">
35   <input type="hidden" name="reportname" value="<% $cgi->param('reportname') |h %>">
36
37   <& /elements/tr-select-agent.html &>
38
39   <& /elements/tr-td-label.html, label => emt('Message template') &>
40     <TD>
41       <& /elements/select-msg_template.html, field => 'msgnum' &>
42     </TD>
43   </TR>
44
45   <& /elements/tr-input-beginning_ending.html &>
46
47   <& /elements/progress-init.html,
48     'OneTrueForm',
49     [ qw( reportname msgnum agentnum beginning ending ) ],
50     $p.'misc/process/send-report.html',
51     { message => 'Reports sent',
52       url => $cgi->referer }
53   &>
54
55 </table>
56
57 <INPUT TYPE="button" onclick="process()" VALUE="<% emt('Send reports') %>" />
58 </FORM>
59
60 <style>
61 table.grid {
62   border-collapse: collapse;
63   margin-top: 1ex;
64   margin-left: auto;
65   margin-right: auto;
66 }
67 .grid caption {
68   font-weight: bold;
69   margin-bottom: 0.5ex;
70 }
71 .grid th,td {
72   padding-left: 3px;
73   padding-right: 3px;
74   padding-bottom: 2px;
75   border: none;
76   empty-cells: show;
77 }
78 .grid th {
79   border-bottom: 1px solid #999999;
80   font-size: 90%;
81   vertical-align: bottom;
82 }
83 </style>
84
85 % if ( @report_history ) {
86 <hr>
87 <table class="grid">
88 <caption><% emt('Report history') %></caption>
89 <thead>
90   <th>Agent</th>
91   <th>Sent on</th>
92   <th colspan=2>Date range</th>
93   <th>User</th>
94 </thead>
95 <tbody>
96 %   my $row = 0;
97 %   foreach my $report (@report_history) {
98 %   my $agent = ($report->agentnum ?
99 %                 $report->agent->agent : 'All agents');
100   <tr class="row<% $row % 2 %>">
101     <td><% $agent %></td>
102     <td><% time2str($date_format, $report->send_date) %></td>
103     <td><% time2str($date_format, $report->sdate) %></td>
104     <td><% time2str($date_format, $report->edate) %></td>
105     <td><% $report->access_user->username %></td>
106   </tr>
107 %   $row++;
108 %   }
109 </tbody>
110 </table>
111 % }
112
113 <& /elements/footer.html &>
114
115 <%init>
116
117 die "access denied"
118   unless $FS::CurrentUser::CurrentUser->access_right('Send reports to customers');
119
120 $cgi->param('reportname') =~ /^(\w+)$/
121   or die "bad reportname";
122 my $reportname = $1;
123 my $report_info = $FS::report_batch::sendable_reports{$reportname}
124   or die "bad reportname";
125
126 my $date_format = FS::Conf->new->config('date_format') || '%x';
127
128 my @report_history = qsearch({
129   table     => 'report_batch',
130   hashref   => { reportname => $reportname },
131   order_by  => ' ORDER BY send_date DESC',
132 });
133
134 # defaults per agent that could be selected for the report
135 my %agent;
136
137 foreach my $report ( @report_history ) {
138   my $agentnum = $report->agentnum;
139   next if $agent{$agentnum};
140
141   # estimate the width of the report period, in months
142   my $last_sdate = DateTime->from_epoch( epoch => $report->sdate );
143   my $last_edate = DateTime->from_epoch( epoch => $report->edate );
144
145   my $days = $last_sdate->delta_days( $last_edate )->delta_days;
146   my $months = sprintf('%.0f', $days / 6) / 5;
147
148   my $next_sdate = $last_edate->clone->add(days => 1);
149   my $next_edate = $next_sdate->clone;
150   if ( $months >= 1 ) { # then treat as an interval in months
151     $next_edate->add( months => sprintf('%.0f', $months) );
152     $next_edate->subtract(days => 1);
153   } else { # treat as a number of days
154     $next_edate->add( days => $days );
155   }
156
157   my $name = $agentnum ? FS::agent->by_key($agentnum)->agent : 'All agents';
158   $agent{$agentnum} = {
159     name      => $name,
160     beginning => $next_sdate->strftime($date_format),
161     ending    => $next_edate->strftime($date_format),
162     msgnum    => $report->msgnum,
163   };
164 }
165
166 </%init>