PageRenderTime 58ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/library/edihistory/edih_archive.php

https://bitbucket.org/openemr/openemr
PHP | 1307 lines | 844 code | 118 blank | 345 comment | 236 complexity | 4139c71e61d545fa188d576d4f1f6420 MD5 | raw file
Possible License(s): Apache-2.0, AGPL-1.0, GPL-2.0, LGPL-3.0, BSD-3-Clause, Unlicense, MPL-2.0, GPL-3.0, LGPL-2.1
  1. <?php
  2. /**
  3. * edih_archive.php
  4. * Purpose: to archive old entries in the csv files and old files
  5. *
  6. * @package OpenEMR
  7. * @subpackage ediHistory
  8. * @link https://www.open-emr.org
  9. * @author Kevin McCormick
  10. * @copyright Copyright (c) 2016 Kevin McCormick Longview, Texas
  11. * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
  12. */
  13. // a security measure to prevent direct web access to this file
  14. // must be accessed through the main calling script ibr_history.php
  15. // from admin at rune-city dot com; found in php manual
  16. //if (!defined('SITE_IN')) die('Direct access not allowed!');
  17. //
  18. // required functions
  19. //require_once("$srcdir/edihistory/test_edih_csv_inc.php");
  20. //
  21. // constant DS = DIRECTORY_SEPARATOR
  22. /**
  23. * Report on edi_history
  24. *
  25. * @uses csv_parameters()
  26. * @uses csv_assoc_array()
  27. *
  28. * @param string archive date in CCYYMMDD format
  29. *
  30. * @return array array[i] = filename
  31. */
  32. function edih_archive_report($period = '')
  33. {
  34. //
  35. $str_html = '';
  36. $chkdt = '';
  37. $strdt = '';
  38. // edih_archive_date returns empty string if no period
  39. $tper = edih_archive_date($period);
  40. $chkdt = ($tper) ? $tper : 'None';
  41. $strdt = ($tper) ? substr($chkdt, 0, 4) . '-' . substr($chkdt, 4, 2) . '-' . substr($chkdt, 6, 2) : 'None';
  42. //
  43. csv_edihist_log("edih_archive_report: creating archive report with date $chkdt");
  44. //
  45. $bdir = csv_edih_basedir();
  46. $params = csv_parameters('ALL');
  47. if (!is_array($params) && count($params)) {
  48. csv_edihist_log("edih_archive_report: invalid csv_parameters");
  49. return "<p>There was an error creating the report.</p>";
  50. }
  51. //
  52. $str_html .= "<h3>Report on edi files using archive date " . text($strdt) . "</h3>" . PHP_EOL;
  53. foreach ($params as $key => $param) {
  54. $old_ct = 0;
  55. $clm_ct = 0;
  56. $dir_ct = 0;
  57. $dir_sz = 0;
  58. $row_ct = 0;
  59. $dir_kb = '';
  60. $subdir_ct = 0;
  61. $fntp = '';
  62. $fntp_ct = 0;
  63. //
  64. $tp = $param['type'];
  65. $fdir = $param['directory'];
  66. //
  67. if (is_dir($fdir)) {
  68. $dir_ar = scandir($fdir);
  69. if (is_array($dir_ar) && ((count($dir_ar) - 2) > 0)) {
  70. $str_html .= "<H3><em>Type</em> " . text($tp) . "</H3>" . PHP_EOL;
  71. $str_html .= "<ul>" . PHP_EOL;
  72. $dir_ct = count($dir_ar);
  73. foreach ($dir_ar as $fn) {
  74. if ($fn == 'README.txt') {
  75. $dir_ct--;
  76. continue;
  77. }
  78. if (substr($fn, 0, 1) == '.') {
  79. $dir_ct--;
  80. continue;
  81. }
  82. if (is_dir($fdir . DS . $fn)) {
  83. $subdir_ct++;
  84. $dir_ct--;
  85. continue;
  86. }
  87. $dir_sz += filesize($fdir . DS . $fn);
  88. }
  89. //
  90. $csv_ar = csv_assoc_array($tp, 'file');
  91. $row_ct = (is_array($csv_ar)) ? count($csv_ar) : 0;
  92. if ($row_ct) {
  93. foreach ($csv_ar as $row) {
  94. // tally amount of claim transactions in the files
  95. if ($tp == 'f837') {
  96. $clm_ct += $row['Claim_ct'];
  97. }
  98. if ($tp == 'f277') {
  99. $clm_ct += ($row['Accept'] + $row['Reject']);
  100. }
  101. if ($tp == 'f835') {
  102. $clm_ct += $row['Claim_ct'];
  103. }
  104. // check for duplicates
  105. // here assume that files with multiple rows are consecutive
  106. if ($fntp !== $row['FileName']) {
  107. $fntp = $row['FileName'];
  108. // count files that would be archived
  109. if (($chkdt != 'None') && strcmp($row['Date'], $chkdt) < 0) {
  110. $old_ct++;
  111. }
  112. $fntp_ct++;
  113. }
  114. }
  115. //
  116. $dir_kb = csv_convert_bytes($dir_sz);
  117. } else {
  118. csv_edihist_log("edih_archive_report: error $tp file count $dir_ct with csv rows $row_ct");
  119. }
  120. //
  121. $mis = ($fntp_ct == $dir_ct ) ? "(check/eft count or ISA--IEA count)" : "(mismatch csv $fntp_ct dir $dir_ct)";
  122. //
  123. if ($tp == 'f837') {
  124. $str_html .= "<li><em>Note:</em> 837 Claims files are not archived </li>" . PHP_EOL;
  125. }
  126. //
  127. $str_html .= "<li>files directory has " . text($dir_ct) . " files, using " . text($dir_kb) . " </li>" . PHP_EOL;
  128. $str_html .= ($subdir_ct && ($tp != 'f837')) ? "<li> <em>warning</em> found " . text($subdir_ct) . " sub-directories</li>" . PHP_EOL : "";
  129. $str_html .= "<li>files csv table has " . text($row_ct) . " rows " . text($mis) . "</li>" . PHP_EOL;
  130. $str_html .= ($clm_ct) ? "<li>there are " . text($clm_ct) . " claim transactions counted</li>" . PHP_EOL : "";
  131. $str_html .= ($old_ct) ? "<li>Archive date " . text($strdt) . " would archive " . text($old_ct) . " files </li>" . PHP_EOL : "";
  132. $str_html .= "</ul>" . PHP_EOL;
  133. } else {
  134. $str_html .= "<p><em>Type</em> <b>" . text($tp) . "</b> <br /> -- empty " . text($tp) . " file directory</p>" . PHP_EOL;
  135. }
  136. } else {
  137. $str_html .= "<p><em>warning</em> <b>" . text($tp) . "</b> file directory does not exist</p>" . PHP_EOL;
  138. }
  139. }
  140. $str_html .= "<p>Report end</p>" . PHP_EOL;
  141. //
  142. return $str_html;
  143. }
  144. /**
  145. * Format the date used in comparisons
  146. *
  147. * @param string period from select list e.g. 6m, 12m
  148. *
  149. * @return string archive date in CCYYMMDD format
  150. */
  151. function edih_archive_date($period)
  152. {
  153. //
  154. $dtpd2 = '';
  155. if (!$period) {
  156. return $dtpd2;
  157. }
  158. $is_period = preg_match('/\d{1,2}(?=m)/', $period, $matches);
  159. //
  160. if (count($matches)) {
  161. $gtdt = getdate();
  162. //
  163. if (strpos($period, 'm')) {
  164. // take the number part of 'period'
  165. // so modstr will be '-N month'
  166. $modstr = '-' . $matches[0] . ' month';
  167. $dtstr1 = $gtdt['mon'] . '/01/' . $gtdt['year'];
  168. } elseif (strpos($period, 'y')) {
  169. $modstr = '-' . $matches[0] . ' year';
  170. $dtstr1 = $gtdt['mon'] . '/01/' . $gtdt['year'];
  171. } else {
  172. csv_edihist_log("edih_archive_date: incorrect date period $period");
  173. return false;
  174. }
  175. //
  176. if ($modstr) {
  177. $dtpd1 = date_create($dtstr1);
  178. $dtm = date_modify($dtpd1, $modstr);
  179. $dtpd2 = $dtm->format('Ymd');
  180. } else {
  181. csv_edihist_log("edih_archive_date: failed to parse $period");
  182. return false;
  183. }
  184. } else {
  185. csv_edihist_log("edih_archive_date: invalid argment $period");
  186. return false;
  187. }
  188. // the testing date in CCYYMMDD format
  189. return $dtpd2;
  190. }
  191. /**
  192. * Create an array of file names to be archived
  193. * The 'Date' column in the files_[type].csv file
  194. * is compared to the archive date. If the date is less
  195. * than the archive date, the "FileName' value is copied
  196. *
  197. * @param array csv file rows array
  198. * @param string archive date in CCYYMMDD format
  199. *
  200. * @return array array[i] = filename
  201. */
  202. function edih_archive_filenames($csv_ar, $archive_date)
  203. {
  204. //
  205. if ($archive_date && strlen($archive_date) == 8 && is_numeric($archive_date)) {
  206. $testdate = (string)$archive_date;
  207. } else {
  208. csv_edihist_log("edih_archive_filenames: invalid archive date $archive_date");
  209. return false;
  210. }
  211. //
  212. if (!is_array($csv_ar) || !count($csv_ar)) {
  213. csv_edihist_log("edih_archive_filenames: failed to get csv file array $file_type");
  214. return false;
  215. }
  216. //
  217. $fn_ar = array();
  218. foreach ($csv_ar as $row) {
  219. if (strcmp($row['Date'], $archive_date) < 0) {
  220. $fn_ar[] = $row['FileName'];
  221. }
  222. }
  223. $ret_ar = (count($fn_ar)) ? array_values(array_unique($fn_ar)) : $fn_ar;
  224. //
  225. return $ret_ar;
  226. }
  227. /**
  228. * Create a new csv array by omitting rows which reference
  229. * a file name that is to be archived
  230. *
  231. * @uses csv_file_type()
  232. * @uses csv_assoc_array()
  233. *
  234. * @param string the file type
  235. * @param string the csv type file or claim
  236. * @param array the array of archived file names and retained file names
  237. *
  238. * @return array
  239. */
  240. function edih_archive_csv_split($csv_ar, $filename_array)
  241. {
  242. //
  243. if (!is_array($filename_array) || !count($filename_array)) {
  244. csv_edihist_log('csv_archive_table; invalid filename array');
  245. return false;
  246. }
  247. //
  248. if (is_array($csv_ar) && count($csv_ar)) {
  249. csv_edihist_log("edih_archive_csv_split: csv rows " . count($csv_ar) . " old files " . count($filename_array));
  250. } else {
  251. csv_edihist_log("edih_archive_csv_split: failed to get csv file array");
  252. return false;
  253. }
  254. //
  255. // if the to be archived file name is in the row,
  256. // do not copy it to the new csv array
  257. $arch_ar = array();
  258. $arch_ar['arch'] = array();
  259. $arch_ar['keep'] = array();
  260. //
  261. foreach ($csv_ar as $row) {
  262. if (in_array($row['FileName'], $filename_array)) {
  263. $arch_ar['arch'][] = $row;
  264. } else {
  265. $arch_ar['keep'][] = $row;
  266. }
  267. }
  268. //
  269. csv_edihist_log("edih_archive_csv_split: 'arch' array rows " . count($arch_ar['arch']));
  270. csv_edihist_log("edih_archive_csv_split: 'keep' array rows " . count($arch_ar['keep']));
  271. //
  272. return $arch_ar;
  273. }
  274. /**
  275. * Creates a zip archive of the files in the $filename_ar array and
  276. * returns the path/name of the archive or FALSE on error
  277. *
  278. * @param array $parameters array for file type from csv_parameters
  279. * @param array $filename_ar array of filenames to be archived
  280. * @param string $archive_date date of archive to be incorporated in archive file name
  281. *
  282. * @return bool result of zipArchive functions
  283. */
  284. function edih_archive_create_zip($parameters, $filename_ar, $archive_date, $archive_filename)
  285. {
  286. // we deal with possible maximum files issues by chunking the $fn_ar array
  287. //
  288. $ft = $parameters['type'];
  289. $fdir = $parameters['directory'];
  290. $tmp_dir = csv_edih_tmpdir();
  291. // archive csv rows -- same name as from edih_archive_main
  292. // $fn_files_arch = $tmp_dir.DS.'arch_'.basename($files_csv);
  293. $files_csv_arch = 'arch_' . basename($parameters['files_csv']);
  294. // $fn_claims_arch = $tmp_dir.DS.'arch_'.basename($claim_csv);
  295. $claims_csv_arch = 'arch_' . basename($parameters['claims_csv']);
  296. //
  297. $f_max = 200;
  298. $fn_ar2 = array();
  299. // to handle possibility of more than 200 files in the archive
  300. // use the 'chunk' method
  301. if (count($filename_ar) > $f_max) {
  302. $fn_ar2 = array_chunk($filename_ar, $f_max);
  303. } else {
  304. $fn_ar2[] = $filename_ar;
  305. }
  306. //
  307. $zip_name = $tmp_dir . DS . $archive_filename;
  308. csv_edihist_log("edih_archive_create_zip: using $zip_name");
  309. //
  310. $zip_obj = new ZipArchive();
  311. csv_edihist_log("edih_archive_create_zip: now opening archive $archive_filename");
  312. if (is_file($zip_name)) {
  313. $isOK = $zip_obj->open($zip_name, ZipArchive::CHECKCONS);
  314. if ($isOK) {
  315. if ($zip_obj->locateName($ft) === false) {
  316. $isOK = $zip_obj->addEmptyDir($ft);
  317. if (!$isOK) {
  318. csv_edihist_log("edih_archive_create_zip: adding $ft ZipArchive error $msg");
  319. return $isOK;
  320. }
  321. }
  322. } else {
  323. $msg = $zip_obj->getStatusString();
  324. csv_edihist_log("edih_archive_create_zip: $ft ZipArchive error $msg");
  325. return $isOK;
  326. }
  327. } else {
  328. $isOK = $zip_obj->open($zip_name, ZipArchive::CREATE);
  329. $isOK = $zip_obj->addEmptyDir('csv');
  330. $isOK = $zip_obj->addEmptyDir($ft);
  331. $zip_obj->setArchiveComment("edi_history archive prior to $archive_date");
  332. }
  333. // we are working with the open archive
  334. // now add the old csv files to the archive
  335. if (is_file($tmp_dir . DS . $files_csv_arch)) {
  336. csv_edihist_log("edih_archive_create_zip: now adding $files_csv_arch to archive");
  337. $isOK = $zip_obj->addFile($tmp_dir . DS . $files_csv_arch, 'csv' . DS . $files_csv_arch);
  338. }
  339. if (is_file($tmp_dir . DS . $claims_csv_arch)) {
  340. csv_edihist_log("edih_archive_create_zip: now adding $claims_csv_arch to archive");
  341. $isOK = $zip_obj->addFile($tmp_dir . DS . $claims_csv_arch, 'csv' . DS . $claims_csv_arch);
  342. }
  343. // close zip archive
  344. csv_edihist_log("edih_archive_create_zip: now closing archive");
  345. $isOK = $zip_obj->close();
  346. if ($isOK !== true) {
  347. $msg = $zip_obj->getStatusString();
  348. csv_edihist_log("edih_archive_create_zip: $ft ZipArchive error $msg");
  349. return $isOK;
  350. }
  351. // $fn_ar2[i][j]
  352. csv_edihist_log("edih_archive_create_zip: with file name groups " . count($fn_ar2));
  353. foreach ($fn_ar2 as $fnz) {
  354. // reopen the zip archive on each loop so the open file count is controlled
  355. if (is_file($zip_name)) {
  356. csv_edihist_log("edih_archive_create_zip: now opening archive");
  357. $isOK = $zip_obj->open($zip_name, ZipArchive::CHECKCONS);
  358. }
  359. //
  360. if ($isOK === true) {
  361. // we are working with the open archive
  362. // now add the old x12 files to the archive
  363. csv_edihist_log("edih_archive_create_zip: now adding $ft files to archive");
  364. foreach ($fnz as $fz) {
  365. if ($fz == '.' || $fz == '..') {
  366. continue;
  367. }
  368. if (is_file($fdir . DS . $fz) && is_readable($fdir . DS . $fz)) {
  369. $isOK = $zip_obj->addFile($fdir . DS . $fz, $ft . DS . $fz);
  370. } else {
  371. // possible that file is in csv table, but not in directory?
  372. $msg = $zip_obj->getStatusString();
  373. csv_edihist_log("edih_archive_create_zip: error adding file $fz zipArchive: $msg");
  374. }
  375. } // end foreach($fnz as $fz)
  376. // close zip object for next iteration of chunked array
  377. csv_edihist_log("edih_archive_create_zip: now closing archive");
  378. $isOK = $zip_obj->close();
  379. // errors on close would be non-existing file added or something else
  380. if ($isOK !== true) {
  381. $msg = $zip_obj->getStatusString();
  382. csv_edihist_log("edih_archive_create_zip: $ft ZipArchive error $msg");
  383. //
  384. return $isOK;
  385. }
  386. } else {
  387. // ZipArchive open() failed -- try to get the error message and return false
  388. $msg = $zip_obj->getStatusString();
  389. csv_edihist_log("edih_archive_create_zip: $ft ZipArchive failed $msg");
  390. return $isOK;
  391. }// end if ($isOK)
  392. //
  393. } // end foreach($fn_ar2 as $fnz)
  394. //
  395. return $isOK;
  396. }
  397. /**
  398. * Archived files have been included in archive file
  399. * so we move the files to the archive tmp directory, for later deletion
  400. *
  401. * @param array parameters array for type
  402. * @param array filename array
  403. *
  404. * @return int count of moved files
  405. */
  406. function edih_archive_move_old($parameters, $filename_ar)
  407. {
  408. //
  409. if (!is_array($filename_ar) || !count($filename_ar)) {
  410. return false;
  411. }
  412. if (!is_array($parameters) || !count($parameters)) {
  413. return false;
  414. }
  415. //
  416. clearstatcache(true);
  417. //
  418. $fnct = 0;
  419. $fn_ar_ct = count($filename_ar);
  420. $ft = $parameters['type'];
  421. $fdir = $parameters['directory'];
  422. $fdir = realpath($fdir);
  423. $rndir = csv_edih_tmpdir() . DS . $ft;
  424. //
  425. if (is_dir($fdir)) {
  426. csv_edihist_log("edih_archive_delete_old: $ft dir OK");
  427. if (is_dir($rndir) || mkdir($rndir)) {
  428. $rndir = realpath($rndir);
  429. csv_edihist_log("edih_archive_delete_old: $ft move dir OK");
  430. $isOK = true;
  431. } else {
  432. csv_edihist_log("edih_archive_delete_old: $ft move dir error");
  433. $isOK = false;
  434. }
  435. } else {
  436. csv_edihist_log("edih_archive_delete_old: $ft dir error");
  437. $isOK = false;
  438. }
  439. //
  440. if ($isOK) {
  441. csv_edihist_log("edih_archive_delete_old: $ft old file count $fn_ar_ct");
  442. foreach ($filename_ar as $fn) {
  443. // if we have added the file to the archive, remove it from the storage directory
  444. // but keep the /history/tmp file copy for now
  445. if (is_file($fdir . DS . $fn)) {
  446. $isrn = rename($fdir . DS . $fn, $rndir . DS . $fn);
  447. }
  448. //
  449. if ($isrn) {
  450. $fnct++;
  451. } else {
  452. csv_edihist_log("edih_archive_delete_old: $ft failed to move $fn");
  453. }
  454. }
  455. } else {
  456. csv_edihist_log("edih_archive_delete_old: $ft directory error for files or tmp");
  457. }
  458. //
  459. return $fnct;
  460. }
  461. /**
  462. * create associative array from archive csv file
  463. *
  464. * @uses edih_archive_csv_array()
  465. *
  466. * @param string
  467. * @param string
  468. * @param string optional filepath
  469. *
  470. * @return array
  471. */
  472. function edih_archive_csv_array($filetype, $csv_type, $filepath = '')
  473. {
  474. //
  475. $str_out = '';
  476. $csv_ar = array();
  477. $tmpdir = csv_edih_tmpdir();
  478. $tmpcsv = $tmpdir . DS . 'csv';
  479. //
  480. $csvtp = (strpos($csv_type, 'aim')) ? 'claims' : 'files';
  481. //
  482. if (is_file($filepath)) {
  483. $csv_arch_path = $filepath;
  484. } else {
  485. $csv_arch_path = $tmpcsv . DS . 'arch_' . $csvtp . '_' . $filetype . '.csv';
  486. }
  487. //
  488. $ct = 0;
  489. $row = 0;
  490. $ky = -1;
  491. // relies on first row being header or column names
  492. if (($fh = fopen($csv_arch_path, "rb")) !== false) {
  493. while (($data = fgetcsv($fh, 2048, ",")) !== false) {
  494. if (is_null($data)) {
  495. continue;
  496. }
  497. if ($row) {
  498. for ($i = 0; $i < $ct; $i++) {
  499. $csv_ar[$ky][$h[$i]] = $data[$i];
  500. }
  501. } else {
  502. $ct = count($data);
  503. $h = $data;
  504. }
  505. $row++;
  506. $ky++;
  507. }
  508. fclose($fh);
  509. } else {
  510. // invalid file path
  511. csv_edihist_log('edih_archive_csv_array; invalid file path ' . $csv_arch_path);
  512. return false;
  513. }
  514. //
  515. return $csv_ar;
  516. }
  517. /**
  518. * combine the csv file in the archive with the current csv file
  519. *
  520. * @uses edih_archive_csv_array()
  521. *
  522. * @param string
  523. * @param string
  524. *
  525. * @return string
  526. */
  527. function edih_archive_csv_combine($filetype, $csvtype)
  528. {
  529. //
  530. $str_out = '';
  531. $hdr_ar = array();
  532. $bdir = csv_edih_basedir();
  533. $tmpdir = csv_edih_tmpdir();
  534. $tmpcsv = $tmpdir . DS . 'csv';
  535. //
  536. $csvtp = (strpos($csvtype, 'aim')) ? 'claims' : 'files';
  537. $csv_arch_file = $tmpcsv . DS . 'arch_' . $csvtp . '_' . $filetype . '.csv';
  538. $csv_new_file = $tmpdir . DS . 'cmb_' . $csvtp . '_' . $filetype . '.csv';
  539. //
  540. // arrays used to eliminate duplicate rows
  541. $dup_ar = $dup_unique = $dup_keys = array();
  542. // combine files by combining arrays and writing a tmp file
  543. // get the present csv file contents
  544. $car1 = csv_assoc_array($filetype, $csvtp);
  545. // get the archived csv contents
  546. if (is_file($csv_arch_file)) {
  547. $car2 = edih_archive_csv_array($filetype, $csvtp, $csv_arch_file);
  548. }
  549. // possibility of empty arrays if no data rows in a csv file
  550. $hdrc1 = (is_array($car1) && count($car1)) ? array_keys($car1[0]) : array();
  551. $hdrc2 = (is_array($car2) && count($car2)) ? array_keys($car2[0]) : array();
  552. if (count($hdrc1) && ($hdrc1 === $hdrc2)) {
  553. $hdr_ar = $hdrc1;
  554. } elseif (empty($hdrc1) && count($hdrc2)) {
  555. $hdr_ar = $hdrc2;
  556. } elseif (empty($hdrc2) && count($hdrc1)) {
  557. $hdr_ar = $hdrc1;
  558. } else {
  559. // array mismatch error (impossible?)
  560. csv_edihist_log("edih_archive_csv_combine: $filetype $csvtp array header mismatch");
  561. // just use the current csv file
  562. $hdr_ar = csv_table_header($filetype, $csvtp);
  563. $car_cmb_unique = $car1;
  564. // debug
  565. if (count($hdrc1)) {
  566. $dbg_str = '';
  567. foreach ($hdrc1 as $h) {
  568. $dbg_str .= $h . ' ';
  569. }
  570. csv_edihist_log("edih_archive_csv_combine: $csvtp car1 header $dbg_str");
  571. } else {
  572. csv_edihist_log("edih_archive_csv_combine: $csvtp car1 header empty");
  573. }
  574. if (count($hdrc2)) {
  575. $dbg_str = '';
  576. foreach ($hdrc2 as $h) {
  577. $dbg_str .= $h . ' ';
  578. }
  579. csv_edihist_log("edih_archive_csv_combine: $csvtp car2 header $dbg_str");
  580. } else {
  581. csv_edihist_log("edih_archive_csv_combine: $csvtp car2 header empty");
  582. }
  583. //
  584. }
  585. // if the arrays checked out
  586. if (!isset($car_cmb_unique)) {
  587. // if we have archive csv rows
  588. if (is_array($car1) && is_array($car2)) {
  589. // put the archive rows first
  590. $car_cmb = array_merge($car2, $car1);
  591. // now eliminate duplicates
  592. if ($csvtp == 'files') {
  593. if ($filetype == 'f835') {
  594. $ky = 'Trace';
  595. } else {
  596. $ky = 'Control';
  597. }
  598. // array_column() php v5.5
  599. foreach ($car_cmb as $idx => $row) {
  600. $dup_ar[$idx] = $row[$ky];
  601. }
  602. csv_edihist_log("edih_archive_csv_combine: $csvtp array row count " . count($dup_ar));
  603. $dup_unique = array_unique($dup_ar);
  604. $dup_keys = array_keys($dup_unique);
  605. csv_edihist_log("edih_archive_csv_combine: $csvtp index row count " . count($dup_keys));
  606. foreach ($dup_keys as $k) {
  607. $car_cmb_unique[] = $car_cmb[$k];
  608. }
  609. csv_edihist_log("edih_archive_csv_combine: $csvtp combined row count " . count($car_cmb_unique));
  610. } elseif ($csvtp == 'claims') {
  611. $ct = count($hdr_ar);
  612. $ftxt = $csvtp . ' array' . PHP_EOL;
  613. foreach ($car_cmb as $idx => $row) {
  614. $r_str = '';
  615. for ($i = 0; $i < $ct; $i++) {
  616. $r_str .= $row[$hdr_ar[$i]];
  617. }
  618. $dup_ar[$idx] = $r_str;
  619. $ftxt .= $r_str . PHP_EOL;
  620. }
  621. csv_edihist_log("edih_archive_csv_combine: $csvtp array row count " . count($dup_ar));
  622. file_put_contents($tmpdir . DS . 'archive' . DS . 'claimstr.txt', $ftxt);
  623. //
  624. $dup_unique = array_unique($dup_ar);
  625. $dup_keys = array_keys($dup_unique);
  626. csv_edihist_log("edih_archive_csv_combine: $csvtp index row count " . count($dup_keys));
  627. foreach ($dup_keys as $k) {
  628. $car_cmb_unique[] = $car_cmb[$k];
  629. }
  630. csv_edihist_log("edih_archive_csv_combine: $csvtp combined row count " . count($car_cmb_unique));
  631. } else {
  632. $car_cmb_unique = $car_cmb;
  633. }
  634. } else {
  635. csv_edihist_log("edih_archive_csv_combine: array keys mismatch $filetype");
  636. }
  637. } else {
  638. csv_edihist_log("edih_archive_csv_combine: error reading archived csv " . $csvtp . "_" . $filetype . ".csv");
  639. }
  640. $rwct = 0;
  641. $fh = fopen($csv_new_file, 'wb');
  642. if ($fh) {
  643. fputcsv($fh, $hdr_ar);
  644. $rwct++;
  645. //
  646. foreach ($car_cmb_unique as $row) {
  647. fputcsv($fh, $row);
  648. $rwct++;
  649. }
  650. // close new csv file
  651. fclose($fh);
  652. } else {
  653. csv_edihist_log("edih_archive_csv_combine: failed to open $filetype new csv file");
  654. }
  655. return $rwct;
  656. }
  657. /**
  658. * Unpack an existing archive and restore it to current csv records
  659. * and replace the files in the respective directories
  660. *
  661. * @uses edih_archive_csv_combine
  662. * @param string
  663. *
  664. * @return string
  665. */
  666. function edih_archive_restore($archive_name)
  667. {
  668. //
  669. $str_out = '';
  670. $bdir = csv_edih_basedir();
  671. $tmpdir = csv_edih_tmpdir();
  672. $archdir = $bdir . DS . 'archive';
  673. //
  674. if (is_file($archdir . DS . $archive_name)) {
  675. $arch = realpath($archdir . DS . $archive_name);
  676. $str_out .= "Archive: restoring " . text($archive_name) . "<br />";
  677. csv_edihist_log("edih_archive_restore: restoring $archive_name");
  678. } else {
  679. $str_out = "Archive: restore archive bad file name " . text($archive_name) . " <br />";
  680. csv_edihist_log("edih_archive_restore: restore archive bad file name $archive_name");
  681. return $str_out;
  682. }
  683. //
  684. $zip_obj = new ZipArchive();
  685. // open archive (ZipArchive::CHECKCONS the ZipArchive::CREATE is supposedly necessary for microsoft)
  686. //$res = $zip_obj->open($arch, ZipArchive::CHECKCONS);
  687. if ($zip_obj->open($arch, ZipArchive::CHECKCONS) === true) {
  688. $f_ct = $zip_obj->numFiles;
  689. $str_out .= "Extracting " . text($f_ct) . " items from " . text($archive_name) . " <br />";
  690. csv_edihist_log("edih_archive_restore: Extracting $f_ct items from $archive_name");
  691. $isOK = $zip_obj->extractTo($tmpdir);
  692. if (!$isOK) {
  693. $msg = $zip_obj->getStatusString();
  694. csv_edihist_log("edih_archive_restore: error extracting archive");
  695. $str_out .= "Archive: error extracting archive " . text($archive_name) . " <br />";
  696. $str_out .= "zipArchive: " . text($msg) . " <br />";
  697. return $str_out;
  698. }
  699. } else {
  700. $msg = $zip_obj->getStatusString();
  701. csv_edihist_log("edih_archive_restore: error opening archive");
  702. $str_out .= "Archive: error opening archive <br />" . PHP_EOL;
  703. $str_out .= "zipArchive: " . text($msg) . " <br />";
  704. return $str_out;
  705. }
  706. // now traverse the tmpdir and replace things
  707. // we should have tmp/csv/files_[ftype].csv claims_[ftype].csv
  708. // tmp/[ftype]/x12_filenames
  709. $arch_ar = scandir($tmpdir);
  710. $tpstr = '';
  711. foreach ($arch_ar as $fa) {
  712. if ($fa == '.' || $fa == '..') {
  713. continue;
  714. }
  715. if (is_dir($tmpdir . DS . $fa)) {
  716. if ($fa == 'csv') {
  717. continue;
  718. }
  719. // if a /history/ftype dir exists
  720. if (is_dir($bdir . DS . $fa)) {
  721. $type_ar[] = $fa;
  722. $tpstr .= "$fa ";
  723. }
  724. } else {
  725. continue;
  726. }
  727. }
  728. //
  729. csv_edihist_log("edih_archive_restore: types in archive $tpstr");
  730. $str_out .= "Archive: types in archive " . text($tpstr) . " <br />" . PHP_EOL;
  731. //
  732. foreach ($type_ar as $ft) {
  733. $str_out .= "Archive: now restoring " . text($ft) . "<br />" . PHP_EOL;
  734. csv_edihist_log("edih_archive_restore: now restoring $ft");
  735. //
  736. $frows = edih_archive_csv_combine($ft, 'file');
  737. csv_edihist_log("edih_archive_restore: files_$ft csv combined rows $frow");
  738. $crows = edih_archive_csv_combine($ft, 'claim');
  739. csv_edihist_log("edih_archive_restore: claims_$ft csv combined rows $frow");
  740. //
  741. $file_ar = scandir($tmpdir . DS . $ft);
  742. foreach ($file_ar as $fn) {
  743. if ($fn == '.' || $fn == '..') {
  744. continue;
  745. }
  746. if (is_file($tmpdir . DS . $ft . DS . $fn)) {
  747. $rn = rename($tmpdir . DS . $ft . DS . $fn, $bdir . DS . $ft . DS . $fn);
  748. if (!$rn) {
  749. $str_out .= " -- error restoring " . text($ft) . DS . text($fn) . "<br />" . PHP_EOL;
  750. csv_edihist_log("edih_archive_restore: error restoring " . $ft . DS . $fn);
  751. }
  752. }
  753. }
  754. // this will catch the csv files for the particulat type
  755. $str_out .= "Archive: now replacing csv tables for " . text($ft) . "<br />" . PHP_EOL;
  756. csv_edihist_log("edih_archive_restore: now replacing csv tables for $ft");
  757. //
  758. $rnf = rename($tmpdir . DS . 'cmb_files_' . $ft . '.csv', $bdir . DS . 'csv' . DS . 'files_' . $ft . '.csv');
  759. $rnc = rename($tmpdir . DS . 'cmb_claims_' . $ft . '.csv', $bdir . DS . 'csv' . DS . 'claims_' . $ft . '.csv');
  760. $str_out .= ($rnf) ? "" : " -- error restoring files_" . text($ft) . ".csv <br />" . PHP_EOL;
  761. $str_out .= ($rnc) ? "" : " -- error restoring claims_" . text($ft) . ".csv <br />" . PHP_EOL;
  762. }
  763. //
  764. csv_edihist_log("edih_archive_restore: now removing archive file");
  765. $str_out .= "Archive: now removing archive file <br />" . PHP_EOL;
  766. $rm = unlink($arch);
  767. if (!$rm) {
  768. csv_edihist_log("edih_archive_restore: error removing $archdir.DS.$archive_name");
  769. $str_out .= ($rnf) ? "" : " -- error removing " . text($archdir) . "." . DS . "." . text($archive_name) . PHP_EOL;
  770. }
  771. //
  772. //edih_archive_cleanup($arch_fn, $tp_ar);
  773. csv_edihist_log("edih_archive_restore: now removing temporary files");
  774. $str_out .= "Archive: now removing temporary files <br />" . PHP_EOL;
  775. //
  776. $is_clear = csv_clear_tmpdir();
  777. if ($is_clear) {
  778. $str_out .= "Archive: temporary files removed. Process complete.<br />" . PHP_EOL;
  779. } else {
  780. $str_out .= "Archive: still some files in /history/tmp/. Process complete.<br />" . PHP_EOL;
  781. }
  782. //
  783. return $str_out;
  784. }
  785. /**
  786. * restores files from the tmp dir if the archive process needs to be aborted
  787. *
  788. * @uses csv_edih_basedir()
  789. * @uses csv_edih_tmpdir()
  790. * @uses csv_parameters()
  791. *
  792. * @return string
  793. */
  794. function edih_archive_undo()
  795. {
  796. //
  797. // archive process creates files in /history/tmp
  798. // /tmp/old_files_[type].csv copy of pre-archive csv record
  799. // /tmp/old_claims_[type].csv copy of pre-archive csv record
  800. // /tmp/new_files_[type].csv csv record of non-archived files
  801. // /tmp/new_claims_[type].csv csv record of non-archived files
  802. // /tmp/arch_files_[type].csv csv record of archived files (to be put in zip file)
  803. // /tmp/arch_claims_[type].csv csv record of archived files (to be put in zip file)
  804. // /tmp/[type]/filename_to_be_archived all the archived files for [type]
  805. //
  806. $str_out = '';
  807. $bdir = csv_edih_basedir();
  808. $tmpdir = csv_edih_tmpdir();
  809. $archdir = $bdir . DS . 'archive';
  810. //
  811. $params = csv_parameters("ALL");
  812. $types_ar = array_keys($params);
  813. //
  814. csv_edihist_log("edih_archive_undo: restoring prior csv files files");
  815. foreach ($types_ar as $ft) {
  816. if (is_file($tmpdir . DS . 'old_files_' . $ft . '.csv')) {
  817. $rn = rename($tmpdir . DS . 'old_files_' . $ft . '.csv', $bdir . DS . 'csv' . DS . 'files_' . $ft . '.csv');
  818. if ($rn) {
  819. csv_edihist_log("edih_archive_undo: restored prior files_$ft ");
  820. } else {
  821. csv_edihist_log("edih_archive_undo: restore failed for prior files_$ft ");
  822. }
  823. }
  824. if (is_file($tmpdir . DS . 'old_claims_' . $ft . '.csv')) {
  825. $rn = rename($tmpdir . DS . 'old_claims_' . $ft . '.csv', $bdir . DS . 'csv' . DS . 'claims_' . $ft . '.csv');
  826. if ($rn) {
  827. csv_edihist_log("edih_archive_undo: restored prior claimss_$ft ");
  828. } else {
  829. csv_edihist_log("edih_archive_undo: restore failed for prior claims_$ft ");
  830. }
  831. }
  832. }
  833. $arch_ar = scandir($tmpdir);
  834. foreach ($arch_ar as $fa) {
  835. if ($fa == "." && $fa == "..") {
  836. continue;
  837. }
  838. if (is_dir($tmpdir . DS . $fa)) {
  839. if (in_array($fa, $types_ar)) {
  840. $fpath = $params[$fa]['directory'];
  841. if ($dh = opendir($tmpdir . DS . $fa)) {
  842. $str_out .= "Archive: undo restoring " . text($fa) . " files<br />" . PHP_EOL;
  843. csv_edihist_log("edih_archive_undo: restoring $fa files");
  844. while (false !== ($entry = readdir($dh))) {
  845. if ($entry != "." && $entry != "..") {
  846. if (is_file($fpath . DS . $entry)) {
  847. // file was not moved
  848. } else {
  849. rename($tmpdir . DS . $fa . DS . $entry, $fpath . DS . $entry);
  850. }
  851. }
  852. }
  853. closedir($dh);
  854. }
  855. }
  856. }
  857. }
  858. return $str_out;
  859. }
  860. /**
  861. * After the archive is created, the csv record needs to be re-written so the archived
  862. * files are not in the csv file and hence, not searched for.
  863. *
  864. * @uses csv_table_header()
  865. *
  866. * @param string $csv_path the tmp csv file path is expected
  867. * @param array $row_array the data rows to be written (an associative array)
  868. *
  869. * @return integer count the rows written
  870. */
  871. function edih_archive_rewrite_csv($csv_path, $csv_keys, $row_array)
  872. {
  873. // @param string $csv_path -- the tmp csv file path is expected
  874. // @param array $heading_ar -- the column heading for the csv file
  875. // @param array $row_array -- the data rows to be written
  876. //
  877. // count characters written -- returned by fputcsv
  878. $ocwct = 0;
  879. $rwct = 0;
  880. //
  881. if (is_array($row_array)) {
  882. csv_edihist_log("edih_archive_rewrite_csv: row array count " . count($row_array));
  883. } else {
  884. csv_edihist_log("edih_archive_rewrite_csv: row array not array");
  885. }
  886. //
  887. if (is_array($row_array) && is_array($csv_keys)) {
  888. if (count($csv_keys)) {
  889. $h_ar = $csv_keys;
  890. }
  891. } else {
  892. csv_edihist_log("edih_archive_rewrite_csv: invalid row array");
  893. return $rwct;
  894. }
  895. //$csv_path should end with /history/tmp/[arch|keep]_[files|claims]_[type].csv
  896. // with 'w' flag, place the file pointer at the beginning of the file
  897. // and truncate the file to zero length.
  898. // If the file does not exist, attempt to create it.
  899. $fh3 = fopen($csv_path, 'wb');
  900. if ($fh3) {
  901. // write the heading row first
  902. $ocwct += fputcsv($fh3, $h_ar);
  903. // wrote heading, now add rows
  904. foreach ($row_array as $row) {
  905. $ocwct += fputcsv($fh3, $row);
  906. $rwct++;
  907. }
  908. fclose($fh3);
  909. csv_edihist_log("edih_archive_rewrite_csv: wrote " . count($row_array) . " rows to " . basename($csv_path));
  910. } else {
  911. csv_edihist_log("edih_archive_rewrite_csv: failed to open $csv_path");
  912. }
  913. return $rwct;
  914. }
  915. /**
  916. * cleanup archived files after archive created
  917. *
  918. * @param string name of archive file
  919. * @param array array of types included in archive
  920. *
  921. * @return string
  922. */
  923. function edih_archive_cleanup($archivename, $types_ar)
  924. {
  925. //
  926. $str_out = '';
  927. //
  928. if (is_array($types_ar) && count($types_ar)) {
  929. $tdirs = $types_ar;
  930. } else {
  931. csv_edihist_log("edih_archive_cleanup: no types in file types list");
  932. $str_out = "no types in file types list" . PHP_EOL;
  933. return $str_out;
  934. }
  935. $bdir = csv_edih_basedir();
  936. $tmpdir = csv_edih_tmpdir();
  937. $archivedir = $bdir . DS . 'archive';
  938. $fct = 0;
  939. // move archive file to archive directory
  940. csv_edihist_log("edih_archive_cleanup: now clearing temporary files");
  941. $str_out .= "Archive: now clearing temporary files<br />" . PHP_EOL;
  942. // delete archived files from edih tmp dir
  943. foreach ($tdirs as $td) {
  944. csv_edihist_log("edih_archive_cleanup: cleaning up for $td");
  945. if (is_dir($tmpdir . DS . $td)) {
  946. $fn_ar = scandir($tmpdir . DS . $td);
  947. foreach ($fn_ar as $fn) {
  948. if ($fn == '.' || $fn == '..') {
  949. continue;
  950. }
  951. if (is_file($tmpdir . DS . $td . DS . $fn)) {
  952. $ul = unlink($tmpdir . DS . $td . DS . $fn);
  953. if (!$ul) {
  954. csv_edihist_log("edih_archive_cleanup: error removing file $fn");
  955. $str_out .= "<p>edih_archive_cleanup: error removing file " . text($td) . DS . text($fn) . "</p>";
  956. } else {
  957. $fct++;
  958. }
  959. }
  960. }
  961. // try to remove the now empty directory
  962. csv_edihist_log("edih_archive_cleanup: removing tmp $td");
  963. rmdir($tmpdir . DS . $td);
  964. }
  965. csv_edihist_log("edih_archive_cleanup: removed $fct files from $td");
  966. }
  967. return $str_out;
  968. }
  969. /**
  970. * The main function in this edih_archive.php script. This function gets the parameters array
  971. * from csv_parameters() and calls the archiving functions on each type of file
  972. * in the parameters array.
  973. *
  974. * @uses edih_archive_date()
  975. * @uses csv_edih_basedir()
  976. * @uses csv_parameters()
  977. * @uses csv_edih_tmpdir()
  978. * @uses csv_table_header()
  979. * @uses edih_archive_filenames()
  980. * @uses edih_archive_rewrite_csv()
  981. * @uses edih_archive_csv_split()
  982. * @uses edih_archive_create_zip()
  983. *
  984. * @param string from select drop-down 6m, 12m, 18m, etc
  985. *
  986. * @return string descriptive message in html format
  987. */
  988. function edih_archive_main($period)
  989. {
  990. //
  991. $out_html = '';
  992. if ($period) {
  993. $archive_date = edih_archive_date($period);
  994. if ($archive_date) {
  995. $archive_dir = csv_edih_basedir() . DS . 'archive';
  996. $tmp_dir = csv_edih_tmpdir();
  997. $arch_fn = $archive_date . '_archive.zip';
  998. $params = csv_parameters();
  999. } else {
  1000. csv_edihist_log("edih_archive_main: error creating archive date from $period");
  1001. $out_html = "Error creating archive date from " . text($period) . "<br />" . PHP_EOL;
  1002. }
  1003. } else {
  1004. $out_html = "Archive period invalid.<br />" . PHP_EOL;
  1005. return $out_html;
  1006. }
  1007. //
  1008. if (is_dir($archive_dir)) {
  1009. if (is_file($archive_dir . DS . $arch_fn)) {
  1010. csv_edihist_log("edih_archive_main: archive file $arch_fn already exists");
  1011. $out_html = "Archive: archive file " . text($arch_fn) . " already exists<br />" . PHP_EOL;
  1012. return $out_html;
  1013. }
  1014. } else {
  1015. // should have been created at setup
  1016. if (!mkdir($archive_dir, 0755)) {
  1017. csv_edihist_log("edih_archive_main: archive directory does not exist");
  1018. $out_html = "Archive: archive directory does not exist<br />" . PHP_EOL;
  1019. return $out_html;
  1020. }
  1021. }
  1022. //
  1023. foreach ($params as $k => $p) {
  1024. //
  1025. $ft = $p['type']; // could be $k
  1026. //
  1027. if ($ft == 'f837') {
  1028. csv_edihist_log("edih_archive_main: 837 Claims files are not archived");
  1029. continue;
  1030. }
  1031. $fdir = $p['directory'];
  1032. $scan = scandir($fdir);
  1033. if (!$scan || count($scan) < 3) {
  1034. continue;
  1035. }
  1036. //
  1037. $files_csv = $p['files_csv'];
  1038. $claims_csv = $p['claims_csv'];
  1039. $date_col = $p['filedate'];
  1040. $fncol = 'FileName';
  1041. //
  1042. // create three csv file paths 'old_', 'arch_', and 'keep_'
  1043. // files csv temporary names
  1044. $fn_files_old = $tmp_dir . DS . 'old_' . basename($files_csv);
  1045. $fn_files_arch = $tmp_dir . DS . 'arch_' . basename($files_csv);
  1046. $fn_files_keep = $tmp_dir . DS . 'keep_' . basename($files_csv);
  1047. // claims csv temporary names
  1048. $fn_claims_old = $tmp_dir . DS . 'old_' . basename($claims_csv);
  1049. $fn_claims_arch = $tmp_dir . DS . 'arch_' . basename($claims_csv);
  1050. $fn_claims_keep = $tmp_dir . DS . 'keep_' . basename($claims_csv);
  1051. // table headings
  1052. $fh_ar = csv_table_header($ft, 'file');
  1053. $ch_ar = csv_table_header($ft, 'claim');
  1054. // copy existing csv files -- continue to next type if no files_csv
  1055. $iscpc = $iscpf = false;
  1056. if (is_file($files_csv)) {
  1057. $iscpf = copy($files_csv, $fn_files_old);
  1058. csv_edihist_log("edih_archive_main: copy $ft files csv to tmp dir");
  1059. } else {
  1060. csv_edihist_log("edih_archive_main: $ft files csv does not exist");
  1061. continue;
  1062. }
  1063. if (is_file($claims_csv)) {
  1064. $iscpc = copy($claims_csv, $fn_claims_old);
  1065. csv_edihist_log("edih_archive_main: copy $ft claims csv to tmp dir");
  1066. } else {
  1067. if ($ft == 'f997') {
  1068. // there may be no 997 type claims records, so create a dummy file
  1069. $fh = fopen($fn_claims_old, 'wb');
  1070. if ($fh) {
  1071. fputcsv($fh, $ch_ar);
  1072. fclose($fh);
  1073. }
  1074. } else {
  1075. csv_edihist_log("edih_archive_main: $ft claims csv does not exist");
  1076. continue;
  1077. }
  1078. }
  1079. //
  1080. if (!$iscpf || !$iscpc) {
  1081. csv_edihist_log("edih_archive_csv_old: copy to tmp dir failed for csv file $ft");
  1082. $out_html = "Archive temporary files operation failed ... aborting <br />" . PHP_EOL;
  1083. // need to call archive_undo()
  1084. $out_html .= edih_archive_undo();
  1085. return $out_html;
  1086. }
  1087. // get the csv data
  1088. $csv_files_ar = csv_assoc_array($ft, 'file');
  1089. $csv_claims_ar = csv_assoc_array($ft, 'claim');
  1090. // get filenames to be archived
  1091. $fn_ar = array();
  1092. $tp_ar = array();
  1093. $fn_ar = edih_archive_filenames($csv_files_ar, $archive_date);
  1094. if (count($fn_ar)) {
  1095. // add type to list
  1096. $tp_ar[] = $ft;
  1097. // get the old and new csv row arrays for files_csv
  1098. $arch_new = edih_archive_csv_split($csv_files_ar, $fn_ar);
  1099. if ($arch_new) {
  1100. if (isset($arch_new['keep'])) {
  1101. // write the new
  1102. $frws = edih_archive_rewrite_csv($fn_files_keep, $fh_ar, $arch_new['keep']);
  1103. $out_html .= "type " . text($ft) . " keep files_csv file with " . text($frws) . " rows<br />";
  1104. }
  1105. if (isset($arch_new['arch'])) {
  1106. // write the old
  1107. $frws2 = edih_archive_rewrite_csv($fn_files_arch, $fh_ar, $arch_new['arch']);
  1108. $out_html .= "type " . text($ft) . " archive files_csv file with " . text($frws2) . " rows<br />";
  1109. }
  1110. } else {
  1111. $out_html .= "type $ft error creating new files_csv tables";
  1112. }
  1113. // repeat for claims_csv
  1114. $arch_new = edih_archive_csv_split($csv_claims_ar, $fn_ar);
  1115. if ($arch_new) {
  1116. if (isset($arch_new['keep'])) {
  1117. // write the new
  1118. $crws = edih_archive_rewrite_csv($fn_claims_keep, $ch_ar, $arch_new['keep']);
  1119. $out_html .= "type " . text($ft) . " keep claims_csv file with " . text($crws) . " rows<br />";
  1120. }
  1121. if (isset($arch_new['arch'])) {
  1122. // write the old
  1123. $crws = edih_archive_rewrite_csv($fn_claims_arch, $ch_ar, $arch_new['arch']);
  1124. $out_html .= "type " . text($ft) . " archive claims_csv file with " . text($crws) . " rows<br />";
  1125. }
  1126. } else {
  1127. $out_html .= "type " . text($ft) . " error creating new claims csv tables<br />";
  1128. }
  1129. // now the csv_records are in files
  1130. // file records in $fn_files_arch $fn_files_keep
  1131. // claim records in $fn_claims_arch $fn_claims_new
  1132. //
  1133. // create a zip archive
  1134. // zf is result of zipArchive functions true or false
  1135. $zf = edih_archive_create_zip($p, $fn_ar, $archive_date, $arch_fn);
  1136. //
  1137. // delete archived files
  1138. if ($zf) {
  1139. // replace the csv files
  1140. $rn = rename($fn_files_keep, $files_csv);
  1141. if ($rn) {
  1142. csv_edihist_log("edih_archive_main: replaced $files_csv");
  1143. } else {
  1144. csv_edihist_log("edih_archive_main: error trying to replace $files_csv");
  1145. }
  1146. $rn = rename($fn_claims_keep, $claims_csv);
  1147. if ($rn) {
  1148. csv_edihist_log("edih_archive_main: replaced $claims_csv");
  1149. } else {
  1150. csv_edihist_log("edih_archive_main: error trying to replace $claims_csv");
  1151. }
  1152. // move archive files to tmpdir/ftype
  1153. // $rndir = mkdir($tmpdir.DS.$fdir);
  1154. csv_edihist_log("edih_archive_main: $ft now moving old files ");
  1155. $del = edih_archive_move_old($p, $fn_ar);
  1156. $out_html .= "Archive moved " . text($del . " " . $ft) . " type files<br />" . PHP_EOL;
  1157. //
  1158. } else {
  1159. csv_edihist_log("edih_archive_main: type $ft error in creating archive");
  1160. $out_html .= "type " . text($ft) . " error in creating archive<br />" . PHP_EOL;
  1161. }
  1162. } else {
  1163. csv_edihist_log("edih_archive_main: search found no type $ft files older than $period");
  1164. $out_html .= "Archive: type " . text($ft) . " archive found no files older than " . text($period) . "<br />" . PHP_EOL;
  1165. }
  1166. } // end foreach($params as $k=>$p)
  1167. //
  1168. if (is_file($tmp_dir . DS . $arch_fn)) {
  1169. $rn = rename($tmp_dir . DS . $arch_fn, $archive_dir . DS . $arch_fn);
  1170. $cm = chmod($archive_dir . DS . $arch_fn, 0400);
  1171. if ($rn) {
  1172. csv_edihist_log("edih_archive_main: moved $arch_fn to archive directory");
  1173. } else {
  1174. csv_edihist_log("edih_archive_main: error moving archive file $arch_fn");
  1175. $out_html .= "<p>edih_archive_main: error moving archive file " . text($arch_fn) . "</p>";
  1176. }
  1177. } else {
  1178. csv_edihist_log("edih_archive_main: is_file false $tmp_dir.DS.$arch_fn");
  1179. }
  1180. //edih_archive_cleanup($arch_fn, $tp_ar);
  1181. $is_clear = csv_clear_tmpdir();
  1182. if ($is_clear) {
  1183. $out_html .= "Archive: temporary files removed. Process complete.<br />" . PHP_EOL;
  1184. } else {
  1185. $out_html .= "Archive: still some files in /history/tmp/. Process complete.<br />" . PHP_EOL;
  1186. }
  1187. //
  1188. return $out_html;
  1189. }