loadXLS.php 7.2 KB

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