-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathclients.php
More file actions
183 lines (153 loc) · 6.8 KB
/
clients.php
File metadata and controls
183 lines (153 loc) · 6.8 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
/**
*
* @package wpcable
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
class wpcable_clients {
public $tables = array();
public function __construct() {
global $wpdb;
$this->tables = array(
'transactions' => $wpdb->prefix . 'codeable_transactions',
'clients' => $wpdb->prefix . 'codeable_clients',
'amounts' => $wpdb->prefix . 'codeable_amounts',
);
}
public function get_clients( $from_month = '', $from_year = '', $to_month = '', $to_year = '' ) {
$clients = array();
$firstdate = '';
$lastdate = '';
$wpcable_stats = new wpcable_stats();
// get first and last task if no date is set
if ( $from_month == '' && $from_year == '' ) {
$get_first_task = $wpcable_stats->get_first_task();
$firstdate = $get_first_task['dateadded'];
} else {
$firstdate = $from_year . '-' . $from_month . '-01';
}
if ( $to_month == '' && $to_year == '' ) {
$get_last_task = $wpcable_stats->get_last_task();
$lastdate = $get_last_task['dateadded'];
} else {
$lastdate = $to_year . '-' . $to_month . '-' . date( 't', strtotime( $to_year . '-' . $to_month . '-01 23:59:59' ) );
}
$query = '
SELECT
*
FROM
' . $this->tables['transactions'] . '
LEFT JOIN ' . $this->tables['clients'] . '
ON
' . $this->tables['transactions'] . '.client_id = ' . $this->tables['clients'] . '.client_id
LEFT JOIN ' . $this->tables['amounts'] . '
ON
' . $this->tables['transactions'] . '.task_ID = ' . $this->tables['amounts'] . ".task_ID
WHERE
`description` = 'task_completion' or `description` = 'ad_hoc_expert_credit' OR `description` = 'partial_refund'
AND (dateadded BETWEEN '" . $firstdate . "' AND '" . $lastdate . "')
";
// check cache
$cache_key = 'clients_' . $firstdate . '_' . $lastdate;
$result = $this->check_cache( $cache_key, $query );
// echo '<pre>'.print_r($result, true).'</pre>';
$client_variance = array();
// loop transactions
foreach ( $result as $tr ) {
// init indexes
if ( ! isset( $clients['clients'][ $tr['client_id'] ]['total_tasks'] ) ) {
$clients['clients'][ $tr['client_id'] ]['total_tasks'] = 0;
}
if ( ! isset( $clients['clients'][ $tr['client_id'] ]['revenue'] ) ) {
$clients['clients'][ $tr['client_id'] ]['revenue'] = 0;
}
if ( ! isset( $clients['clients'][ $tr['client_id'] ]['tasks'] ) ) {
$clients['clients'][ $tr['client_id'] ]['tasks'] = 0;
}
if ( ! isset( $clients['clients'][ $tr['client_id'] ]['total_tasks'] ) ) {
$clients['clients'][ $tr['client_id'] ]['total_tasks'] = 0;
}
if ( ! isset( $clients['clients'][ $tr['client_id'] ]['tasks'] ) ) {
$clients['clients'][ $tr['client_id'] ]['tasks'] = 0;
}
if ( ! isset( $clients['clients'][ $tr['client_id'] ]['subtasks'] ) ) {
$clients['clients'][ $tr['client_id'] ]['subtasks'] = 0;
}
if ( ! isset( $clients['totals']['refunds'] ) ) {
$clients['totals']['refunds'] = 0;
}
if ( ! isset( $clients['totals']['completed'] ) ) {
$clients['totals']['completed'] = 0;
}
if ( ! isset( $clients['totals']['subtasks'] ) ) {
$clients['totals']['subtasks'] = 0;
}
if ( ! isset( $clients['totals']['tasks'] ) ) {
$clients['totals']['tasks'] = 0;
}
$clients['clients'][ $tr['client_id'] ]['client_id'] = $tr['client_id'];
$clients['clients'][ $tr['client_id'] ]['revenue'] = ( strpos( $tr['description'], 'refund' ) !== false ? $clients['clients'][ $tr['client_id'] ]['revenue'] : $clients['clients'][ $tr['client_id'] ]['revenue'] + $tr['credit_revenue_amount'] );
$clients['clients'][ $tr['client_id'] ]['full_name'] = $tr['full_name'];
$clients['clients'][ $tr['client_id'] ]['role'] = $tr['role'];
$clients['clients'][ $tr['client_id'] ]['avatar'] = $tr['large'];
$clients['clients'][ $tr['client_id'] ]['total_tasks'] = $clients['clients'][ $tr['client_id'] ]['total_tasks'] + 1;
$clients['clients'][ $tr['client_id'] ]['tasks'] = ( strpos( $tr['task_type'], 'subtask' ) === false ? $clients['clients'][ $tr['client_id'] ]['tasks'] + 1 : $clients['clients'][ $tr['client_id'] ]['tasks'] );
$clients['clients'][ $tr['client_id'] ]['subtasks'] = ( strpos( $tr['task_type'], 'subtask' ) !== false ? $clients['clients'][ $tr['client_id'] ]['subtasks'] + 1 : $clients['clients'][ $tr['client_id'] ]['subtasks'] );
$clients['clients'][ $tr['client_id'] ]['last_sign_in_at'] = $tr['last_sign_in_at'];
$clients['clients'][ $tr['client_id'] ]['timezone_offset'] = $tr['timezone_offset'];
$clients['totals']['refunds'] = ( strpos( $tr['description'], 'refund' ) !== false ? $clients['totals']['refunds'] + 1 : $clients['totals']['refunds'] );
$clients['totals']['completed'] = ( strpos( $tr['description'], 'refund' ) === false ? $clients['totals']['completed'] + 1 : $clients['totals']['completed'] );
$clients['totals']['subtasks'] = ( strpos( $tr['task_type'], 'subtask' ) !== false ? $clients['totals']['subtasks'] + 1 : $clients['totals']['subtasks'] );
$clients['totals']['tasks'] = ( strpos( $tr['task_type'], 'subtask' ) === false ? $clients['totals']['tasks'] + 1 : $clients['totals']['tasks'] );
$clients['clients'][ $tr['client_id'] ]['transactions'][] = array(
'id' => $tr['id'],
'description' => $tr['description'],
'dateadded' => $tr['dateadded'],
'fee_percentage' => $tr['fee_percentage'],
'fee_amount' => $tr['fee_amount'],
'task_type' => $tr['task_type'],
'task_id' => $tr['task_id'],
'task_title' => $tr['task_title'],
'parent_task_id' => $tr['parent_task_id'],
'preferred' => $tr['preferred'],
'pro' => $tr['pro'],
'revenue' => $tr['credit_revenue_amount'],
'is_refund' => ( strpos( $tr['description'], 'refund' ) !== false ? 1 : 0 ),
);
// foreach($clients['clients'] as $client) {
// foreach($client['transactions'] as $tra) {
// $client_variance[$client['client_id']][] = $tra['revenue'];
// }
// $clients['clients'][$client['client_id']]['variance'] = $this->standard_deviation($client_variance[$client['client_id']]);
// }
}
return $clients;
}
public function standard_deviation( $sample ) {
if ( is_array( $sample ) ) {
$mean = array_sum( $sample ) / count( $sample );
foreach ( $sample as $key => $num ) {
$devs[ $key ] = pow( $num - $mean, 2 );
}
return sqrt( array_sum( $devs ) / ( count( $devs ) - 1 ) );
}
}
/**
* Checks and sets cached data
*
* @since 0.0.6
* @author Justin Frydman
*
* @param bool $key The unique cache key
* @param bool $query The query to check
*
* @return mixed The raw or cached data
* @throws Exception
*/
private function check_cache( $key = false, $query = false ) {
$cache = new wpcable_cache( $key, $query );
return $cache->check();
}
}