summaryrefslogtreecommitdiff
path: root/httemplate/search/timeworked.html
blob: 12c7da043683c861b0f0272d7ad3e486b621754c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<& elements/search.html,
                 'title'       => 'Time Worked',
                 'name'        => 'time',
		 'html_form'   => $html_form,
                 'query'       => $query,
                 'count_query' => $count_query,
                 'header' => [ '#',
                               'Ticket',
                               'Date',
                               'Time',
                               '', # checkbox column
                             ],
                 'fields' => [ sub { shift->[0] },
                               sub { encode_entities(shift->[1]) },
                               sub { shift->[2] },
                               sub { my $seconds = shift->[3];
                                     (($seconds < 0) ? '-' : '') .
                                     concise(duration($seconds));
                                   },
                               sub {
                                 my $row = shift;
                                 my $seconds = $row->[3];
                                 my $id = $row->[4];
                                 qq!<INPUT NAME="transactionid$id" TYPE="checkbox" VALUE="1">!.
                                 qq!<INPUT NAME="seconds$id" TYPE="hidden" VALUE="$seconds">!;
                               },
                             ],
                 'links' => [
                   $link,
                   $link,
                   '',
                   '',
                   '',
                 ],
                 'html_foot' => $html_foot,
             
&>
<%init>

die "access denied"
  unless $FS::CurrentUser::CurrentUser->access_right('Time queue');

my @groupby = ();

my $transactiontime = "
  CASE Transactions.Type WHEN 'Set'
    THEN (  CASE WHEN NewValue = '' THEN 0 ELSE TO_NUMBER(NewValue,'999999') END
           -TO_NUMBER(OldValue, '999999')
          ) * 60
    ELSE TimeTaken*60
  END
";

push @groupby, qw( Transactions.Type NewValue OldValue TimeTaken );

my $appliedtimeclause = "COALESCE (SUM(acct_rt_transaction.seconds), 0)";

my $appliedtimeselect = "
  COALESCE(
            ( SELECT SUM(seconds) FROM acct_rt_transaction
                WHERE transaction_id = Transactions.id
            ),
            0
          )
";

push @groupby, "Transactions.id";

my $wheretimeleft = "$transactiontime != $appliedtimeselect";

push @groupby, "Tickets.id";
push @groupby, "Tickets.Subject";
push @groupby, "Transactions.Created";

my $groupby = join(',', @groupby);

my $where = "
  WHERE ObjectType='RT::Ticket'
    AND ( ( Transactions.Type='Set' AND Field='TimeWorked' )
          OR Transactions.Type IN ( 'Create', 'Comment', 'Correspond', 'Touch' )
        )
    AND $wheretimeleft
";
    #AND $wheretimeleft

my $str2time_sql = str2time_sql;
my $closing      = str2time_sql_closing;


my($begin, $end) = FS::UI::Web::parse_beginning_ending($cgi);
$where .= " AND $str2time_sql Transactions.Created $closing >= $begin ".
          " AND $str2time_sql Transactions.Created $closing <= $end ";

my $html_form =
  qq( <FORM NAME="timeForm" ACTION="${p}misc/timeworked.html" METHOD="POST"> );
if ($cgi->param('category') =~ /^(\w+)$/) {
  $where .= " AND ocfv_TimeType.Content = '$1'";
  $html_form .= qq( <INPUT TYPE="hidden" NAME="category" VALUE="$1"> );
}

my $from = "
    FROM Transactions
      JOIN Tickets ON Transactions.ObjectId = Tickets.id
      LEFT JOIN acct_rt_transaction
        ON Transactions.id = acct_rt_transaction.transaction_id
      LEFT JOIN (
        SELECT DISTINCT ON (ObjectId)
          ObjectId, Content
        FROM ObjectCustomFieldValues
          JOIN CustomFields
            ON (ObjectCustomFieldValues.CustomField = CustomFields.Id)
        WHERE CustomFields.Name = 'TimeType'
          AND ObjectCustomFieldValues.ObjectType = 'RT::Ticket'
          AND ObjectCustomFieldValues.Disabled = 0
        ORDER BY ObjectId ASC, ObjectCustomFieldValues.LastUpdated DESC
      ) AS ocfv_TimeType ON (Tickets.Id = ocfv_TimeType.ObjectId)
";

my $query = "
  SELECT Tickets.id, Tickets.Subject,
         TO_CHAR(Transactions.Created, 'Dy Mon DD HH24:MI:SS YYYY'),
         $transactiontime-$appliedtimeclause,
         Transactions.id
    $from
    $where
    GROUP BY $groupby
    ORDER BY Transactions.Created
";

my $count_query = "SELECT COUNT(*) $from $where";

my $link = [ "${p}rt/Ticket/Display.html?id=", sub { shift->[0]; } ];

my $html_foot = qq'
  <INPUT TYPE="hidden" NAME="begin" VALUE="$begin">
  <INPUT TYPE="hidden" NAME="end"   VALUE="$end">
  <BR>
  <INPUT TYPE="button" VALUE="select all" onClick="setAll(true)">
  <INPUT TYPE="button" VALUE="unselect all" onClick="setAll(false)">
  <BR>
  <INPUT TYPE="submit" NAME="action" VALUE="Assign to accounts"><BR>
  <SCRIPT TYPE="text/javascript">
    function setAll(setTo) {
      theForm = document.timeForm;
      for (i=0,n=theForm.elements.length;i<n;i++)
        if (theForm.elements[i].name.indexOf("transactionid") != -1)
          theForm.elements[i].checked = setTo;
    }
  </SCRIPT>
';

</%init>