loadXLS.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. <?php
  2. include 'phpexcel/PHPExcel.php';
  3. include 'db.inc.php';
  4. ini_set('memory_limit', '256M');
  5. //for reference!
  6. $columns_to_fields = array(
  7. 'Division' => 'division',
  8. 'Consignee' => 'consignee',
  9. 'Called In' => 'date_called_in',
  10. 'Dispatch #' => 'dispatch_number',
  11. 'Shipper' => 'shipper',
  12. 'Ready' => 'date_ready',
  13. 'Address 1' => 'address_1',
  14. 'Address 2' => 'address_2',
  15. 'City' => 'city',
  16. 'ST' => 'state',
  17. 'Zip' => 'zip',
  18. 'Phone' => 'phone',
  19. 'Ready Time' => 'time_ready',
  20. 'Close Time' => 'time_close',
  21. 'Pickup Time' => 'time_pickup',
  22. 'Depart Time' => 'time_depart',
  23. 'BOL Delivered' => 'bol_delivered',
  24. 'Dispatch Refere' => 'dispatch_reference',
  25. 'Ctns' => 'ctns',
  26. 'Weight' => 'weight',
  27. 'Cube' => 'cube'
  28. );
  29. $fields_by_col = array();
  30. $id_company = 1;
  31. //TODO: scan respective directories for new files
  32. processFile('../test.xls', $id_company);
  33. function processFile($filename, $id_company)
  34. {
  35. global $dbh;
  36. if(!file_exists($filename))
  37. {
  38. echo "file doesn't exist\n";
  39. return;
  40. }
  41. $file_size = filesize($filename);
  42. $file_hash = md5_file($filename);
  43. $params = array(
  44. ':id_company' => $id_company,
  45. ':filename' => $filename,
  46. ':file_size' => $file_size,
  47. ':file_hash' => $file_hash
  48. );
  49. //first try to find if the file exists
  50. $stmt_file = $dbh->prepare("SELECT id_file
  51. FROM file
  52. WHERE id_company = :id_company
  53. AND filename = :filename
  54. AND md5_hash = :file_hash
  55. AND size = :file_size");
  56. $stmt_file->execute($params);
  57. $rows = $stmt_file->fetchAll(PDO::FETCH_ASSOC);
  58. if(count($rows))
  59. {
  60. $id_file = $rows[0]['id_file'];
  61. echo "re-running file #$id_file\n";
  62. }
  63. else
  64. {
  65. //report file processed
  66. $stmt_file = $dbh->prepare("INSERT INTO file (id_company, filename, md5_hash, size)
  67. VALUES (:id_company, :filename, :file_hash, :file_size)");
  68. if(!$stmt_file->execute($params))
  69. {
  70. echo "\nPDO::errorInfo():\n";
  71. print_r($dbh->errorInfo());
  72. return;
  73. }
  74. $id_file = $dbh->lastInsertId();
  75. echo "processing new file #$id_file\n";
  76. }
  77. //TODO: check if we already processed this file
  78. $stmt_row = $dbh->prepare("INSERT INTO file_row (
  79. id_file, row_number,
  80. division, consignee, date_called_in, dispatch_number, shipper, date_ready,
  81. address_1, address_2, city, state, zip, phone,
  82. time_ready, time_close, time_pickup, time_depart, bol_delivered,
  83. dispatch_reference,
  84. ctns, weight, cube
  85. ) VALUES (
  86. :id_file, :row_number,
  87. :division, :consignee, STR_TO_DATE(:date_called_in, '%m/%d/%Y'), :dispatch_number, :shipper, STR_TO_DATE(:date_ready, '%m/%d/%Y'),
  88. :address_1, :address_2, :city, :state, :zip, :phone,
  89. :time_ready, :time_close, :time_pickup, :time_depart, :bol_delivered,
  90. :dispatch_reference,
  91. :ctns, :weight, :cube
  92. )
  93. ON DUPLICATE KEY UPDATE
  94. division = :division, consignee = :consignee, date_called_in = STR_TO_DATE(:date_called_in, '%m/%d/%Y'), dispatch_number = :dispatch_number, shipper = :shipper, date_ready = STR_TO_DATE(:date_ready, '%m/%d/%Y'),
  95. address_1 = :address_1, address_2 = :address_2, city = :city, state = :state, zip = :zip, phone = :phone,
  96. time_ready = :time_ready, time_close = :time_close, time_pickup = :time_pickup, time_depart = :time_depart, bol_delivered = :bol_delivered,
  97. dispatch_reference = :dispatch_reference,
  98. ctns = :ctns, weight = :weight, cube = :cube");
  99. if(!$stmt_row)
  100. {
  101. echo "\nPDO::errorInfo():\n";
  102. print_r($dbh->errorInfo());
  103. return;
  104. }
  105. $num_rows = 0;
  106. try
  107. {
  108. //this is necessary to dramatically reduce the memory footprint
  109. $cacheMethod = PHPExcel_CachedObjectStorageFactory::cache_to_phpTemp;
  110. $cacheSettings = array( ' memoryCacheSize ' => '8MB');
  111. PHPExcel_Settings::setCacheStorageMethod($cacheMethod, $cacheSettings);
  112. // figure out which type it is
  113. $type = PHPExcel_IOFactory::identify($filename);
  114. // create an excel reader object
  115. $reader = PHPExcel_IOFactory::createReader($type);
  116. $reader->setReadDataOnly(false);
  117. echo "Report detected as: $type\n";
  118. // load the file
  119. $excel = $reader->load($filename);
  120. // pull the 'call details' worksheet, which should be the only one
  121. $sheet = $excel->getSheet(0);
  122. // count the number of rows and columns
  123. $num_rows = $sheet->getHighestRow();
  124. $last_column = $sheet->getHighestColumn();
  125. // let's keep track of how many succeeded and failed
  126. $num_ok = 0;
  127. $num_fail = 0;
  128. $num_skip = 0;
  129. // pull column names
  130. $column_names = $sheet->rangeToArray('A1:' . $last_column . '1', NULL, TRUE, FALSE);
  131. $columns_by_names = array();
  132. //flip the column names to reflect the indexes
  133. foreach($column_names[0] as $index => $name)
  134. if($name && strlen(trim($name)))
  135. $columns_by_names[$name] = $index;
  136. // go through and read each row
  137. for($r = 2; $r <= $num_rows; $r++)
  138. {
  139. $range = 'A' . $r . ':' . $last_column . $r;
  140. echo "processing row #$r...";
  141. // read row of data into array
  142. $row_array = $sheet->rangeToArray($range, NULL, TRUE, TRUE);
  143. if(!count($row_array))
  144. {
  145. echo "no data\n";
  146. continue;
  147. }
  148. //pull the row out and format the nulls as needed
  149. $row = $row_array[0];
  150. for($c = 0; $c < count($row); $c++)
  151. $row[$c] = $row[$c] && strlen(trim($row[$c])) ? trim($row[$c]) : null;
  152. //skip if no data for first three columns
  153. if(!$row[0] && !$row[1] && !$row[2])
  154. {
  155. echo "SKIP\n";
  156. $num_skip++;
  157. continue;
  158. }
  159. //bind appropriate values
  160. $params = array();
  161. $params[':id_file'] = $id_file;
  162. $params[':row_number'] = $r;
  163. $params[':division'] = $row[0];
  164. $params[':consignee'] = $row[1];
  165. $params[':date_called_in'] = $row[2];
  166. $params[':dispatch_number'] = $row[3];
  167. $params[':shipper'] = $row[4];
  168. $params[':date_ready'] = $row[5];
  169. $params[':address_1'] = $row[6];
  170. $params[':address_2'] = $row[7];
  171. $params[':city'] = $row[8];
  172. $params[':state'] = $row[9];
  173. $params[':zip'] = $row[10];
  174. $params[':phone'] = $row[11];
  175. $params[':time_ready'] = $row[14] ? DateTime::createFromFormat('H:i A', $row[14])->format('H:i:s') : null;
  176. $params[':time_close'] = $row[15] ? DateTime::createFromFormat('H:i A', $row[15])->format('H:i:s') : null;
  177. $params[':time_pickup'] = $row[16] ? DateTime::createFromFormat('H:i A', $row[16])->format('H:i:s') : null;
  178. $params[':time_depart'] = $row[17] ? DateTime::createFromFormat('H:i A', $row[17])->format('H:i:s') : null;
  179. $params[':bol_delivered'] = $row[18];
  180. $params[':dispatch_reference'] = $row[35];
  181. $params[':ctns'] = $row[44];
  182. $params[':weight'] = $row[45];
  183. $params[':cube'] = $row[46];
  184. try
  185. {
  186. $stmt_row->execute($params);
  187. echo "OK\n";
  188. $num_ok++;
  189. }
  190. catch(Exception $e)
  191. {
  192. echo 'FAIL: ' . $e->getMessage() . "\n";
  193. $num_fail++;
  194. }
  195. }
  196. }
  197. catch(Exception $e)
  198. {
  199. echo "Uncaught fatal error while attempting to process the downloaded report: " . $e->getMessage() . "\n";
  200. }
  201. //update the final status
  202. $stmt_file_status = $dbh->prepare("UPDATE file SET
  203. num_rows = :num_rows,
  204. num_ok = :num_ok,
  205. num_fail = :num_fail,
  206. num_skip = :num_skip
  207. WHERE id_file = :id_file");
  208. $params = array(
  209. ':id_file' => $id_file,
  210. ':num_rows' => $num_rows-1, //subtract one because of the column header
  211. ':num_ok' => $num_ok,
  212. ':num_fail' => $num_fail,
  213. ':num_skip' => $num_skip
  214. );
  215. $stmt_file_status->execute($params);
  216. }
  217. ?>