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