support hours 'usage' tracking for our own internal use (#1733)
[freeside.git] / httemplate / misc / process / timeworked.html
diff --git a/httemplate/misc/process/timeworked.html b/httemplate/misc/process/timeworked.html
new file mode 100644 (file)
index 0000000..078d645
--- /dev/null
@@ -0,0 +1,47 @@
+% if ($error) {
+<% $cgi->redirect(popurl(2). "timeworked.html?". $cgi->query_string) %>
+% } else {
+<% $cgi->redirect(popurl(3). "search/timeworked.html") %>
+% }
+<%init>
+
+my %multipliers = map { /^custnum(\d+)$/; ($cgi->param("custnum$1") => $cgi->param("multiplier$1")); }
+                      grep /^custnum\d+$/, $cgi->param;
+
+my @svc_acct_rt_transaction;
+foreach my $transaction (
+  map { /^transactionid(\d+)$/; $1; } grep /^transactionid\d+$/, $cgi->param
+) {
+  my $seconds = $cgi->param("seconds$transaction");
+  my %seconds = map { $_ => sprintf("%.0f", $seconds * $multipliers{$_}) } 
+                (keys %multipliers);
+  my $sum = 0;
+  my $count = 0;
+  foreach (values %seconds) {
+    $sum += $_;
+    $count++;
+  }
+
+  #fudge in some time if we're close
+  if (abs($seconds-$sum) <= $count) {
+    my $adjustment = $seconds-$sum;
+    foreach (keys %seconds) {       # explicitly choose one?
+      $seconds{$_} += $adjustment;
+      last;
+    }
+  }
+
+  foreach my $customer ( grep {$seconds{$_}} keys %seconds ) {
+    push @svc_acct_rt_transaction, new FS::svc_acct_rt_transaction {
+      'custnum'        => $customer,
+      'transaction_id' => $transaction,
+      'seconds'        => $seconds{$customer},
+    };
+  }
+
+}
+
+my $error = FS::svc_acct_rt_transaction->batch_insert(@svc_acct_rt_transaction);
+$cgi->param('error', $error) if $error;
+
+</%init>