timeworked report: carry the date range through to the success redirect
[freeside.git] / httemplate / misc / process / timeworked.html
1 % if ($error) {
2 <% $cgi->redirect(popurl(2). "timeworked.html?". $cgi->query_string) %>
3 % } else {
4 <% $cgi->redirect(popurl(3). "search/timeworked.html?begin=$begin;end=$end") %>
5 % }
6 <%init>
7
8 die "access denied"
9   unless $FS::CurrentUser::CurrentUser->access_right('Time queue');
10
11 my($begin, $end) = FS::UI::Web::parse_beginning_ending($cgi);
12
13 my @acct_rt_transaction;
14 foreach my $transaction (
15   map { /^transactionid(\d+)$/; $1; } grep /^transactionid\d+$/, $cgi->param
16 ) {
17   my $s = "multiplier${transaction}_";
18   my %multipliers = map { /^$s(\d+)$/; $1 => $cgi->param("$s$1"); }
19                       grep /^$s\d+$/, $cgi->param;
20   my $msum = 0;
21   foreach(values %multipliers) {$msum += $_};
22
23   my $seconds = $cgi->param("seconds$transaction");
24   my %seconds = 
25        map { $_ => sprintf("%.0f", $seconds * $multipliers{$_} / $msum) } 
26          (keys %multipliers);
27   my $sum = 0;
28   my $count = 0;
29   foreach (values %seconds) {
30     $sum += $_;
31     $count++;
32   }
33
34   #fudge in some time if we're close
35   if (abs($seconds-$sum) <= $count) {
36     my $adjustment = $seconds-$sum;
37     foreach (keys %seconds) {       # explicitly choose one?
38       $seconds{$_} += $adjustment;
39       last;
40     }
41   } else {
42     die "unexpectedly cannot apportion time";
43   }
44
45   foreach my $customer ( grep {$seconds{$_}} keys %seconds ) {
46     push @acct_rt_transaction, new FS::acct_rt_transaction {
47       'custnum'        => $customer,
48       'transaction_id' => $transaction,
49       'seconds'        => $seconds{$customer},
50       'support'        => int( $seconds{$customer} * $msum ),
51     };
52   }
53
54 }
55
56 my $error = FS::acct_rt_transaction->batch_insert(@acct_rt_transaction);
57 $cgi->param('error', $error) if $error;
58
59 </%init>