-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathsalesUpload.php
More file actions
244 lines (207 loc) · 9.03 KB
/
salesUpload.php
File metadata and controls
244 lines (207 loc) · 9.03 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
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
<?php
/*==============================================================================
* (C) Copyright 2020 John J Kauflin, All rights reserved.
*----------------------------------------------------------------------------
* DESCRIPTION: Upload county sales file and update sales table
*----------------------------------------------------------------------------
* Modification History
* 2020-10-01 JJK Initial version - using this instead of getSalesFromCounty.php
* 2020-12-21 JJK Re-factored to use jjklogin package
* 2021-04-24 JJK Corrected filename bug by looking at the file in the ZIP
* and updated error handling by replacing exit() with throw()
* 2021-05-08 JJK Corrected sales table insert issue
* 2022-05-13 JJK Modified to accept the monthly and weekly sales files
* by just looking for a single file in the ZIP
* 2023-02-17 JJK Refactor for non-static jjklogin class and settings from DB
*============================================================================*/
// Define a super global constant for the log file (this will be in scope for all functions)
define("LOG_FILE", "./php.log");
require_once 'vendor/autoload.php';
// Figure out how many levels up to get to the "public_html" root folder
$webRootDirOffset = substr_count(strstr(dirname(__FILE__),"public_html"),DIRECTORY_SEPARATOR) + 1;
// Get settings and credentials from a file in a directory outside of public_html
// (assume a settings file in the "external_includes" folder one level up from "public_html")
$extIncludePath = dirname(__FILE__, $webRootDirOffset+1).DIRECTORY_SEPARATOR.'external_includes'.DIRECTORY_SEPARATOR;
require_once $extIncludePath.'hoadbSecrets.php';
require_once $extIncludePath.'jjkloginSettings.php';
// Common functions
require_once 'php_secure/commonUtil.php';
// Common database functions and table record classes
require_once 'php_secure/hoaDbCommon.php';
use \jkauflin\jjklogin\LoginAuth;
$adminRec = new AdminRec();
try {
$loginAuth = new LoginAuth($hostJJKLogin, $dbadminJJKLogin, $passwordJJKLogin, $dbnameJJKLogin);
$userRec = $loginAuth->getUserRec();
if ($userRec->userName == null || $userRec->userName == '') {
throw new Exception('User is NOT logged in', 500);
}
if ($userRec->userLevel < 1) {
throw new Exception('User is NOT authorized (contact Administrator)', 500);
}
$adminRec->result = "Not Valid";
$adminRec->message = "";
$adminRec->userName = $userRec->userName;
$adminRec->userLevel = $userRec->userLevel;
$adminLevel = $userRec->userLevel;
if ($adminLevel < 2) {
throw new Exception('You do not have permissions for this function', 500);
}
$fileName = $_FILES['uploadFilename']['name'];
if (empty($fileName)) {
throw new Exception('No file selected', 500);
}
// get uploaded file's extension
$ext = strtolower(pathinfo($fileName, PATHINFO_EXTENSION));
$tmp_file = $_FILES['uploadFilename']['tmp_name'];
//move_uploaded_file(file, dest)
if (!file_exists($tmp_file)) {
throw new Exception('Upload file not found', 500);
}
// Check if upload file is parent ZIP or individual residental sales CSV
$file = null;
if ($ext == 'zip') {
$zipFile = new ZipArchive();
if (!$zipFile->open($tmp_file)) {
throw new Exception('Failed to open uploaded ZIP file', 500);
}
for ($i = 0; $i < $zipFile->numFiles; $i++) {
// Get the file name in the zip
$fileInZip = $zipFile->getNameIndex($i);
// Look for the Residential sales file in the Zip collection (if there are more than 1 files)
if (strstr($fileInZip,'_RES.csv')) {
break;
}
}
$file = $zipFile->getStream($fileInZip);
if (!$file) {
throw new Exception("Failed to open file in ZIP, file = $fileInZip", 500);
}
} else {
// Open the uploaded file from the temporary location
$file = fopen($tmp_file, "r");
}
if (!$file) {
throw new Exception('Unable to open file', 500);
}
//$action = getParamVal("action");
//--------------------------------------------------------------------------------------------------------
// Create connection to the database
//--------------------------------------------------------------------------------------------------------
$conn = getConn($host, $dbadmin, $password, $dbname);
// Loop through all the records in the downloaded sales file and compare with HOA database parcels
$sendMessage = false;
$addToOutput = false;
$outputStr = '';
$recCnt = 0;
$hoaRecsFound = 0;
$newSalesFound = 0;
$saleDate = '';
while(!feof($file))
{
$recCnt = $recCnt + 1;
// 1st record of CSV files are the column names so just skip them
if ($recCnt == 1) {
$salesRecCoumnArray = fgetcsv($file);
continue;
}
$salesRecArray = fgetcsv($file);
if (!$salesRecArray) {
continue;
}
$parcelId = $salesRecArray[0];
// Check if the Parcel Id from the sales record matches any in our HOA database
// (and get the Sales record associated with the Sales Date)
// sales now included in this query and in hoaRec
$saleDate = $salesRecArray[2];
$hoaRec = getHoaRec($conn,$parcelId,"","",$saleDate);
if (empty($hoaRec->Parcel_ID)) {
// If the parcel id is not found in the HOA db, then just skip to the next one
continue;
}
$hoaRecsFound = $hoaRecsFound + 1;
$hoaOwnerRec = $hoaRec->ownersList[0];
$addToOutput = false;
// If the Sales record was not found, insert one
if ( sizeof($hoaRec->salesList) < 1) {
$newSalesFound = $newSalesFound + 1;
$stmt = $conn->prepare("INSERT INTO hoa_sales VALUES (?,?,?,?,?,?,?,?,?,?,?,?,CURRENT_TIMESTAMP,?,?,?,CURRENT_TIMESTAMP,?); ");
$NotificationFlag = 'Y';
$ProcessedFlag = 'N';
$lastChangedby = 'system';
$WelcomeSent = 'X';
$stmt->bind_param("ssssssssssssssss",
$salesRecArray[0],
$salesRecArray[1],
$salesRecArray[2],
$salesRecArray[3],
$salesRecArray[4],
$salesRecArray[5],
$salesRecArray[6],
$salesRecArray[7],
$salesRecArray[8],
$salesRecArray[9],
$salesRecArray[10],
$salesRecArray[11],
$NotificationFlag,
$ProcessedFlag,
$lastChangedby,
$WelcomeSent
);
$stmt->execute();
$stmt->close();
$hoaSalesRec = getHoaSalesRec($conn,$hoaRec->Parcel_ID,$saleDate);
$addToOutput = true;
} else {
$hoaSalesRec = $hoaRec->salesList[0];
// If the sales record is found but there was no notification, send an email
if ($hoaSalesRec->NotificationFlag == 'N') {
$addToOutput = true;
}
}
if ($addToOutput) {
$sendMessage = true;
$outputStr .= '<p><table border=1 class="evenLineHighlight"><tbody>';
$outputStr .= '<tr><th>Parcel Id:</th><td>' . $parcelId . '</td></tr>';
$outputStr .= '<tr><th>Parcel Location:</th><td><b>' . $hoaRec->Parcel_Location . '</b></td></tr>';
$outputStr .= '<tr><th>Old Owner:</th><td>' . $hoaSalesRec->OLDOWN . '</td></tr>';
$outputStr .= '<tr><th>HOA Owner:</th><td>' . $hoaOwnerRec->Owner_Name1 . ' ' . $hoaOwnerRec->Owner_Name2 . '</td></tr>';
$outputStr .= '<tr><th>New Owner1:</th><td>' . $hoaSalesRec->OWNERNAME1 . '</td></tr>';
$outputStr .= '<tr><th>Mailing Name1:</th><td>' . $hoaSalesRec->MAILINGNAME1 . '</td></tr>';
$outputStr .= '<tr><th>Mailing Name2:</th><td>' . $hoaSalesRec->MAILINGNAME2 . '</td></tr>';
$outputStr .= '<tr><th>Sale Date:</th><td>' . $hoaSalesRec->SALEDT . '</td></tr>';
$outputStr .= '</tbody></table></p>';
}
//$outputStr .= '<br>' . $valArray[0];
} // End of while(!feof($file))
fclose($file);
$fromEmailAddress = getConfigValDB($conn,"fromEmailAddress");
$salesReportEmailList = getConfigValDB($conn,"salesReportEmailList");
// Close db connection
$conn->close();
if ($sendMessage) {
$subject = 'HOA Residential Sales';
$messageStr = '<h2>HOA Residential Sales</h2>' . $outputStr;
$sendMailSuccess = sendHtmlEMail($salesReportEmailList,$subject,$messageStr,$fromEmailAddress);
if (!$sendMailSuccess) {
// If fail to send email maybe go back and update the default Y flag back to N ?
}
}
$adminRec->message = "County Sales file processed successfully (Check Sales Report) </br>".
" Total records = $recCnt, HOA records found = $hoaRecsFound, New HOA sales found = $newSalesFound";
$adminRec->result = "Valid";
echo json_encode($adminRec);
} catch(Exception $e) {
//error_log(date('[Y-m-d H:i] '). "in " . basename(__FILE__,".php") . ", Exception = " . $e->getMessage() . PHP_EOL, 3, LOG_FILE);
$adminRec->message = $e->getMessage();
$adminRec->result = "Not Valid";
/*
echo json_encode(
array(
'error' => $e->getMessage(),
'error_code' => $e->getCode()
)
);
*/
echo json_encode($adminRec);
}