01752e1b70daab25469fc93fb014d0adbede98ac
[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;category=$category") %>
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 $category = $cgi->param('category') ) =~ /^\w*$/
14   or die 'illegal category';#no need for nice error messages for XSS, just avoid
15
16 my @acct_rt_transaction;
17 foreach my $transaction (
18   map { /^transactionid(\d+)$/; $1; } grep /^transactionid\d+$/, $cgi->param
19 ) {
20   my $s = "multiplier${transaction}_";
21   my %multipliers = map { /^$s(\d+)$/; $1 => $cgi->param("$s$1"); }
22                       grep /^$s\d+$/, $cgi->param;
23   my $msum = 0;
24   foreach(values %multipliers) {$msum += $_};
25
26   my $seconds = $cgi->param("seconds$transaction");
27   my %seconds = 
28        map { $_ => sprintf("%.0f", $seconds * $multipliers{$_} / $msum) } 
29          (keys %multipliers);
30   my $sum = 0;
31   my $count = 0;
32   foreach (values %seconds) {
33     $sum += $_;
34     $count++;
35   }
36
37   #fudge in some time if we're close
38   if (abs($seconds-$sum) <= $count) {
39     my $adjustment = $seconds-$sum;
40     foreach (keys %seconds) {       # explicitly choose one?
41       $seconds{$_} += $adjustment;
42       last;
43     }
44   } else {
45     die "unexpectedly cannot apportion time";
46   }
47
48   foreach my $customer ( grep {$seconds{$_}} keys %seconds ) {
49     push @acct_rt_transaction, new FS::acct_rt_transaction {
50       'custnum'        => $customer,
51       'transaction_id' => $transaction,
52       'seconds'        => $seconds{$customer},
53       'support'        => int( $seconds{$customer} * $msum ),
54     };
55   }
56
57 }
58
59 my $error = FS::acct_rt_transaction->batch_insert(@acct_rt_transaction);
60 $cgi->param('error', $error) if $error;
61
62 </%init>