-
Notifications
You must be signed in to change notification settings - Fork 0
/
ResupplyReport.php
313 lines (276 loc) · 9.97 KB
/
ResupplyReport.php
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
<?php
/**
* Resupply Reminder Report
*
* Jira ticket
* https://hungryfishmedia.jira.com/browse/tech-4919
*
* Google Doc
* https://docs.google.com/a/hungryfishmedia.com/spreadsheet/ccc?key=0An5sG1ZluV4GdFRocFpNM21PRldSeThLZHB0QmVVakE#gid=0
*
* Dependencies
* ItemsHelper
* Chron
*
* @author Graham Schmidt
* @created 2013-03-18
*/
class ResupplyReport {
private $bucket = array();
private $o_log = '';
public function __construct($date_start, $date_end)
{
self::generateBuckets($date_start, $date_end);
}
public function olog($m)
{
$this->o_log .= $m;
}
public function pLog()
{
return $this->o_log;
}
public function buildMembersAll($date_start, $date_end)
{
// validation
self::validateDates($date_start, $date_end);
$statuses = "'Canceled', 'Pending Resupply', 'Active', 'Awaiting Processing', 'Completed'";
$sql = "
SELECT
od.order_id AS order_id
, from_unixtime(od.date_ordered) as date_ordered
, from_unixtime(od.next_bill) AS next_bill
, NOW() as now
, od.source
, od.status as status
, ld.first_name
, ld.last_name
, ld.email_address
, od.recur_skus as sku_str
, od.onetime_skus
, od.product_id
FROM orders od
LEFT JOIN leads ld USING (lead_id)
WHERE 1
AND od.date_ordered >= unix_timestamp('{$date_start}')
AND od.date_ordered <= unix_timestamp('{$date_end}')
AND od.status IN ({$statuses})
AND od.source = 'Resupply Reminder'
AND (ld.email_address NOT LIKE '%hungryfish%')
ORDER BY od.product_id, od.recur_skus
";
global $finder_db;
try
{
$res = $finder_db->query($sql);
$res->tossIfNoRows();
self::oLog($res->getSQL());
return $res->fetchAllRows();
}
catch (fNoRowsException $e)
{
return null;
}
catch (Exception $e)
{
Chron::oLog($e->getMessage());
return $e->getMessage();
}
}
/**
* Group result set in SKUs
*/
public function generateBuckets($date_start, $date_end)
{
$rows = self::buildMembersAll($date_start, $date_end);
$res = array();
foreach ($rows as $row)
{
// count each order for the product
self::addToBucket('pid', $row['product_id']);
// need to count a person for each SKU on their order
$skus = self::skuToArray($row['sku_str']);
if (!empty($skus))
{
foreach ($skus as $sku => $qty)
{
self::addToBucket('sku', $sku);
// active members
if ($row['next_bill'] > $row['now'])
{
self::addToBucket('active_members_sku', $row['product_id'] . "-" . $sku);
}
// canceled members
if ($row['status'] == 'Canceled')
{
self::addToBucket('canceled', $row['product_id'] . "-" . $sku);
}
// completed a purchase or are on continuity
if ( in_array($row['status'], array('Active')) )
{
self::addToBucket('sku_qty_purchased', $sku, $qty);
self::addToBucket('purchased', $row['product_id'] . "-" . $sku);
}
}
}
// for completed purchases (no recur SKUs)
$onetime_skus = self::skuToArray($row['onetime_skus']);
if (empty($skus)
&& !empty($onetime_skus)
) {
foreach ($onetime_skus as $sku => $qty)
{
// completed a purchase or are on continuity
if ( in_array($row['status'], array('Awaiting Processing', 'Completed')) )
{
self::addToBucket('sku_qty_purchased', $sku, $qty);
self::addToBucket('purchased', $row['product_id'] . "-" . $sku);
}
}
}
}
}
/**
* Return a bucket count
*/
public function getBucket($type)
{
return (isset($this->bucket[$type])
? $this->bucket[$type]
: null
);
}
/**
* Return the overal report data structure
*/
public function getReportData()
{
$data = array();
$ppoints = array();
if ($pids = $this->getBucket('pid'))
{
$active_members_sku = $this->getBucket('active_members_sku');
foreach ($pids as $pid => $count)
{
unset($pp);
$pp = ItemsHelper::getRecurPricePoints($pid, 1, true);
$ppoints[$pid] = $pp[$pid];
}
$skus = $this->getBucket('sku');
$sku_qty_purchased = $this->getBucket('sku_qty_purchased');
$purchased = $this->getBucket('purchased');
$canceled = $this->getBucket('canceled');
// create main data struct
foreach ($ppoints as $pid => $price_points)
{
foreach ($price_points as $sku => $sku_details)
{
$data[$pid][$sku] = array(
'total_members' => $skus[$sku],
'purchased' => $purchased[$pid.'-'.$sku],
'active_members' => $active_members_sku[$pid.'-'.$sku],
'canceled' => $canceled[$pid.'-'.$sku],
'sku' => $sku,
'price_point' => $price_points[$sku]['price_point'],
'price' => $price_points[$sku]['price'],
'revenue' => ($purchased[$pid.'-'.$sku] * $price_points[$sku]['price'] * $sku_qty_purchased[$sku]),
);
// calculate totals for each $pid here
(!isset($sku_totals[$pid]['total_members']) ? $sku_totals[$pid]['total_members'] = 0 : '');
(!isset($sku_totals[$pid]['purchased']) ? $sku_totals[$pid]['purchased'] = 0 : '');
(!isset($sku_totals[$pid]['active_members']) ? $sku_totals[$pid]['active_members'] = 0 : '');
(!isset($sku_totals[$pid]['canceled']) ? $sku_totals[$pid]['canceled'] = 0 : '');
(!isset($sku_totals[$pid]['revenue']) ? $sku_totals[$pid]['revenue'] = 0 : '');
$sku_totals[$pid]['total_members'] += $skus[$sku];
$sku_totals[$pid]['purchased'] += $purchased[$pid.'-'.$sku];
$sku_totals[$pid]['active_members'] += $active_members_sku[$pid.'-'.$sku];
$sku_totals[$pid]['canceled'] += $canceled[$pid.'-'.$sku];
$sku_totals[$pid]['revenue'] += ($purchased[$pid.'-'.$sku] * $price_points[$sku]['price'] * $sku_qty_purchased[$sku]);
}
$data[$pid]['totals'] = array(
'name' => 'TOTAL',
'total_members' => $sku_totals[$pid]['total_members'],
'purchased' => $sku_totals[$pid]['purchased'],
'active_members' => $sku_totals[$pid]['active_members'],
'canceled' => $sku_totals[$pid]['canceled'],
'revenue' => $sku_totals[$pid]['revenue'],
);
}
}
return $data;
}
/**
* Add a count to a bucket
*/
private function addToBucket($b_key = 'sku', $key, $inc = 1)
{
if (empty($key))
return;
if (!isset($this->bucket[$b_key][$key]))
{
$this->bucket[$b_key][$key] = 0;
}
$this->bucket[$b_key][$key] += $inc;
}
/**
* Turn a SKU string into an array
*/
private function skuToArray($sku_str)
{
if (empty($sku_str))
{
return null;
}
$skus = array();
$skus_x = explode('|', $sku_str);
foreach ($skus_x as $sku_x)
{
// skip empty element at end
if (empty($sku_x))
{
continue;
}
$skq = explode(',', $sku_x);
// key -> SKU
// val -> Quantity
$skus[$skq[0]] = (isset($skq[1]) ? $skq[1] : '');
}
return $skus;
}
/**
* Validate date params
*/
private function validateDates($date_start, $date_end)
{
// validate dates
try {
// properly formed dates
self::validDate($date_start, 'Start');
self::validDate($date_end, 'End');
// end date greater than start date
if ($date_end < $date_start)
{
throw new Exception("The end date must be ahead of the start date.");
}
// dates can't be more than 4 months apart
if ( (strtotime($date_end) - strtotime($date_start)) > 10368000 )
{
throw new Exception("Date range limited to 4 months.");
}
}
catch (Exception $e)
{
throw new Exception($e->getMessage());
}
}
/**
* Validate a date string
*/
private function validDate($date, $type = 'Start')
{
// date end validation
if (!preg_match('/^\d{4}-\d{2}-\d{2}$/', $date)) {
throw new Exception("Please provide a {$type} date. It must be in the format yyyy-mm-dd.");
}
}
}