-
Notifications
You must be signed in to change notification settings - Fork 0
/
pledge_breaks.php
210 lines (181 loc) · 8.02 KB
/
pledge_breaks.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
<?php
/**
* A Sample file to demmonstrate how to demonstrate one way to pull pledge break
* data and move it to a remote MS SQL Server.
*/
// Manage database credentials in this file
include('config.inc');
include('vendor/autoload.php');
date_default_timezone_set('UTC');
use Alleg\DatabaseConnection;
// Determines current drive
$drive_selected = 'MAY2013PLEDGE';
// The name of the table in the remote database that will store pledge totals
$destination_table = 'PledgeBreaks';
$current_date = date("Y-m-d H:i:s");
$total_dollars = 0.00;
$total_pledges = 0;
$all_break_dollars = 0.00;
$all_break_pledges = 0;
$alleg_conn = new DatabaseConnection($alleg_dsn,$alleg_username,$alleg_password,$alleg_server,$alleg_database);
$web_conn = new DatabaseConnection($webdb_dsn,$webdb_username,$webdb_password,$webdb_server,$webdb_database);
// determine relevant source codes for the current pledge drive
$sql = "select distinct cpdsrc from oocampd WITH (NOLOCK) where cpdcod = '{$drive_selected}'";
$rs = $alleg_conn->executeQuery($sql);
$count = 0;
while (!$rs->EOF) {
// Format the first record differently
if ($count == 0) {
$drive_source_codes = "'" . trim($rs->fields('cpdsrc')) . "'";
}
$drive_source_codes = $drive_source_codes . ", '" . trim($rs->fields('cpdsrc')) . "'";
$count += 1;
$rs->MoveNext();
}
// Determine break info
// NOTE: The CASE/CAST statement is needed below becuase without it SQL Server
// returns breps as a SQL Server variant data type, which the SQL Server
// Driver for PHP does not support. For more information, see, for example,
// bit.ly/m8FUrm and http://msdn.microsoft.com/en-us/library/aa223955(v=sql.80).aspx.
$sql = "SELECT bkcode, bkcall, bkdtyy, bkdtmm, bkdtdd, bkdesc, bktim, bkdur,
CASE WHEN breps IS NULL THEN '' ELSE CAST(breps as varchar) END AS breps,
CAST(CAST(bkdtmm AS char) + '/' + CAST(bkdtdd AS char) + '/' + CAST(bkdtyy AS char) AS datetime) AS Date
FROM oobreak WITH (NOLOCK)
WHERE bkcall IN ({$drive_source_codes})
ORDER BY bkdtyy, bkdtmm, bkdtdd, bkcode";
$rs = $alleg_conn->executeQuery($sql);
while (!$rs->EOF) {
// Fix Allegiance end times to make them useable
$start_time = $rs->fields('bktim');
$end_time = $start_time;
$end_hour = (int) $start_time / 10000;
$end_hour = $end_hour * 10000;
$end_minute = $end_time - $end_hour;
$bk_dur_hour = (int) $rs->fields('bkdur') / 10000;
$bk_dur_hour = $bk_dur_hour * 10000;
$bk_dur_minute = $rs->fields('bkdur') - $bk_dur_hour;
$end_minute = $end_minute + $bk_dur_minute;
while ($end_minute > 5900) {
$end_minute = $end_minute - 6000;
$bk_dur_hour = $bk_dur_hour + 10000;
}
$end_time = $end_hour + $bk_dur_hour + $end_minute;
$real_start_time = $start_time;
$real_end_time = $end_time;
if ($end_time >= 240000) {
$end_time = 240000;
$real_end_time = $end_time;
}
$bkcall = trim($rs->fields('bkcall'));
$bkdtyy = trim($rs->fields('bkdtyy'));
$bkdtmm = trim($rs->fields('bkdtmm'));
$bkdtdd = trim($rs->fields('bkdtdd'));
$pledge_date = ($bkdtyy * 10000) + ($bkdtmm * 100) + $bkdtdd;
$break_code = trim($rs->fields('bkcode'));
// Get pledge data from the Campaign table
$sql2 = "SELECT CASE WHEN sum(plamt) IS NULL THEN 0
ELSE CAST(sum(plamt) as decimal) END AS totaldollars,
CASE WHEN count(*) IS NULL THEN 0 ELSE CAST(count(*) AS int)
END AS totalpledges
FROM Campaign WITH (NOLOCK)
WHERE cmpcod = '{$drive_selected}'
AND CpdSrc LIKE '%{$bkcall}%'
AND plgyy = {$bkdtyy}
AND plgmm = {$bkdtmm}
AND plgdd = {$bkdtdd}
AND (([break] = '{$break_code}') OR
([break] = '' AND wttime >= {$real_start_time}
AND wttime < {$real_end_time}))";
$rs2 = $alleg_conn->executeQuery($sql2);
// Add pledge & dollar totals for individual break
$total_dollars += $rs2->fields('totaldollars');
$total_pledges += $rs2->fields('totalpledges');
// Get pledge data from the CampaignTrn table
$sql2 = "SELECT CASE WHEN sum(plamt) IS NULL THEN 0
ELSE CAST(sum(plamt) as decimal) END AS totaldollars,
CASE WHEN count(*) IS NULL THEN 0 ELSE CAST(count(*) as int)
END AS totalpledges
FROM CampaignTrn WITH (NOLOCK)
WHERE cpdcod = '{$drive_selected}'
AND CpdSrc like '%{$bkcall}%'
AND PledgeDate = $pledge_date
AND (([break] = '{$break_code}') OR
([break] = '' AND wttime >= {$real_start_time}
AND wttime < {$real_end_time}))";
$rs2 = $alleg_conn->executeQuery($sql2);
// Add pledge & dollar totals for individual break
$total_dollars += $rs2->fields('totaldollars');
$total_pledges += $rs2->fields('totalpledges');
// Add to total count of all pledges and total dollars
$all_break_pledges += $total_pledges;
$all_break_dollars += $total_dollars;
// Add break information
$break_description = $rs->fields('bkdesc');
$break_description = str_replace("'", "`", $break_description);
$break_time = $rs->fields('bktim');
if (strlen($break_time) == 5) {
$break_time = substr($break_time, 0, 1) . ":" . substr($break_time, 1, 2);
}
if (strlen($break_time) == 6) {
$break_time = substr($break_time, 0, 2) . ":" . substr($break_time, 2, 2);
}
if ($rs->fields('Date')) {
$break_date = $rs->fields('Date');
}
else{
$break_date = nothing;
}
if($rs->Fields('breps') != '') {
// Breps contains both the pledge goal, followed by a semi-colon, followed by the dollar goal
$pledge_and_dollar_goal = $rs->Fields('breps');
$semi_colon_position = strpos($rs->Fields('breps'), ';');
$break_pledge_goal = substr($pledge_and_dollar_goal, 0, $semi_colon_position);
$break_dollar_goal = substr($pledge_and_dollar_goal, $semi_colon_position+1, strlen($pledge_and_dollar_goal));
}
else {
$break_pledge_goal = 0;
}
// Insert data into remote database
$sql_ext = "insert into {$destination_table} (Drive, BreakNumber, BreakDescription, BreakDate,
BreakTime, TotalDollars, TotalPledges, LastUpdated, BreakPledgeGoal, BreakDollarGoal)
values ('{$drive_selected}', '{$break_code}', '{$break_description}',
'{$break_date}', '{$break_time}', '{$total_dollars}', '{$total_pledges}',
'{$current_date}', {$break_pledge_goal}, {$break_dollar_goal})";
$rs_ext = $web_conn->executeQuery($sql_ext);
// Clear variables
$break_code = "";
$break_description = "";
$break_time = "";
// Clear pledes totals for the individual break
$total_dollars = 0;
$total_pledges= 0;
$rs->MoveNext();
}
// Use these variables to store total dollars and pledges
$all_dollars = 0;
$all_pledges = 0;
// Get totals from the Campaign table
$camp_rs = $alleg_conn->getCampaignTotals('Campaign', $drive_selected);
$all_dollars += $camp_rs->fields('totaldollars');
$all_pledges += $camp_rs->fields('totalpledges');
// Get totals from the CampaignTrn table
$camptrn_rs = $alleg_conn->getCampaignTotals('CampaignTrn', $drive_selected);
$all_dollars += $camptrn_rs->fields('totaldollars');
$all_pledges += $camptrn_rs->fields('totalpledges');
// At this point $all_break_dollars/pledges are the break totals and
// The "unmarked" totals are basically eveything else.
$unmarked_pledge_total = ($all_dollars - $all_break_dollars);
$unmarked_pledge_count = ($all_pledges - $all_break_pledges);
// add non-break/unmarked totals
$sql_ext = "INSERT INTO {$destination_table} (Drive, BreakNumber,
BreakDescription, BreakDate, BreakTime, TotalDollars, TotalPledges,
LastUpdated, BreakPledgeGoal, BreakDollarGoal)
VALUES ('{$drive_selected}', 999, 'Pre/Post-Drive/Challenges',
'{$current_date}', 999, {$unmarked_pledge_total},
{$unmarked_pledge_count}, '{$current_date}', 0, 0)";
$rs_ext = $web_conn->executeQuery($sql_ext);
// clear old pledge breaks
$sql_ext = "DELETE FROM {$destination_table}
WHERE LastUpdated < '{$current_date}'
AND Drive = '{$drive_selected}'";
$rs_ext = $web_conn->executeQuery($sql_ext);