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