b69c898e86cde2662549d3d37c8e2156db38f223
[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") %>
5 % }
6 <%init>
7
8 die "access denied"
9   unless $FS::CurrentUser::CurrentUser->access_right('Time queue');
10
11 my %multipliers = map { /^custnum(\d+)$/; ($cgi->param("custnum$1") => $cgi->param("multiplier$1")); }
12                       grep /^custnum\d+$/, $cgi->param;
13
14 my @svc_acct_rt_transaction;
15 foreach my $transaction (
16   map { /^transactionid(\d+)$/; $1; } grep /^transactionid\d+$/, $cgi->param
17 ) {
18   my $seconds = $cgi->param("seconds$transaction");
19   my %seconds = map { $_ => sprintf("%.0f", $seconds * $multipliers{$_}) } 
20                 (keys %multipliers);
21   my $sum = 0;
22   my $count = 0;
23   foreach (values %seconds) {
24     $sum += $_;
25     $count++;
26   }
27
28   #fudge in some time if we're close
29   if (abs($seconds-$sum) <= $count) {
30     my $adjustment = $seconds-$sum;
31     foreach (keys %seconds) {       # explicitly choose one?
32       $seconds{$_} += $adjustment;
33       last;
34     }
35   }
36
37   foreach my $customer ( grep {$seconds{$_}} keys %seconds ) {
38     push @svc_acct_rt_transaction, new FS::svc_acct_rt_transaction {
39       'custnum'        => $customer,
40       'transaction_id' => $transaction,
41       'seconds'        => $seconds{$customer},
42     };
43   }
44
45 }
46
47 my $error = FS::svc_acct_rt_transaction->batch_insert(@svc_acct_rt_transaction);
48 $cgi->param('error', $error) if $error;
49
50 </%init>