PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/admin/modules/stock_take/st_upload.php

https://github.com/hendrowicaksono/s3st12-soap
PHP | 110 lines | 72 code | 7 blank | 31 comment | 10 complexity | b6640ad7d1977f7bd4ee1fff1a9f2a75 MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright (C) 2007,2008 Arie Nugraha (dicarve@yahoo.com)
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. *
  19. */
  20. /* Stock Take Upload */
  21. // main system configuration
  22. require '../../../sysconfig.inc.php';
  23. // start the session
  24. require SENAYAN_BASE_DIR.'admin/default/session.inc.php';
  25. require SENAYAN_BASE_DIR.'admin/default/session_check.inc.php';
  26. // privileges checking
  27. $can_read = utility::havePrivilege('stock_take', 'r');
  28. $can_write = utility::havePrivilege('stock_take', 'w');
  29. if (!($can_read AND $can_write)) {
  30. die('<div class="errorBox">'.__('You don\'t have enough privileges to access this area!').'</div>');
  31. }
  32. // check if there is any active stock take proccess
  33. $stk_query = $dbs->query('SELECT * FROM stock_take WHERE is_active=1');
  34. if ($stk_query->num_rows < 1) {
  35. echo '<div class="errorBox">'.__('NO stock taking proccess initialized yet!').'</div>';
  36. die();
  37. }
  38. // file upload
  39. if (isset($_POST['stUpload']) && isset($_FILES['stFile'])) {
  40. require SIMBIO_BASE_DIR.'simbio_FILE/simbio_file_upload.inc.php';
  41. // create upload object
  42. $upload = new simbio_file_upload();
  43. $upload->setAllowableFormat(array('.txt'));
  44. $upload->setMaxSize($sysconf['max_upload']*1024);
  45. $upload->setUploadDir(FILES_UPLOAD_DIR);
  46. // upload the file and change all space characters to underscore
  47. $upload_status = $upload->doUpload('stFile');
  48. if ($upload_status == UPLOAD_SUCCESS) {
  49. // write log
  50. utility::writeLogs($dbs, 'staff', $_SESSION['uid'], 'stock_take', $_SESSION['realname'].' upload stock take file '.$upload->new_filename);
  51. // open file
  52. $stfile = @fopen(FILES_UPLOAD_DIR.$upload->new_filename, 'r');
  53. if (!$stfile) {
  54. echo '<script type="text/javascript">'."\n";
  55. echo 'parent.$(\'stUploadMsg\').update(\'Failed to open stock take file '.$upload->new_filename.'. Please check permission for directory '.FILES_UPLOAD_DIR.'\');'."\n";
  56. echo 'parent.$(\'stUploadMsg\').className = \'errorBox\';'."\n";
  57. echo 'parent.$(\'stUploadMsg\').setStyle( {display: \'block\'} );'."\n";
  58. echo '</script>';
  59. exit();
  60. }
  61. // start loop
  62. $i = 0;
  63. while (!feof($stfile)) {
  64. $curr_time = date('Y-m-d H:i:s');
  65. $item_code = fgetss($stfile, 512);
  66. $item_code = trim($item_code);
  67. if (!$item_code) {
  68. continue;
  69. }
  70. $update = @$dbs->query("UPDATE LOW_PRIORITY stock_take_item SET status='e', checked_by='".$_SESSION['realname']."', last_update='".$curr_time."' WHERE item_code='$item_code'");
  71. $update = @$dbs->query("UPDATE LOW_PRIORITY stock_take SET total_item_lost=total_item_lost-1 WHERE is_active=1");
  72. $i++;
  73. }
  74. fclose($stfile);
  75. // message
  76. echo '<script type="text/javascript">'."\n";
  77. echo 'parent.$(\'stUploadMsg\').update(\''.__('Succesfully upload stock take file').$upload->new_filename.', <b>'.$i.'</b>'.__(' item codes scanned!').'\');'."\n"; //mfc
  78. echo 'parent.$(\'stUploadMsg\').setStyle( {display: \'block\'} );'."\n";
  79. echo '</script>';
  80. } else {
  81. // write log
  82. utility::writeLogs($dbs, 'staff', $_SESSION['uid'], 'stock_take', 'ERROR : '.$_SESSION['realname'].' FAILED TO upload stock take file '.$upload->new_filename.', with error ('.$upload->error.')');
  83. echo '<script type="text/javascript">'."\n";
  84. echo 'parent.$(\'stUploadMsg\').update(\'Failed to upload stock take file! <div>Error : '.$upload->error.'</div>\');'."\n";
  85. echo 'parent.$(\'stUploadMsg\').className = \'errorBox\';'."\n";
  86. echo 'parent.$(\'stUploadMsg\').setStyle( {display: \'block\'} );'."\n";
  87. echo '</script>';
  88. }
  89. exit();
  90. }
  91. ?>
  92. <fieldset class="menuBox">
  93. <div class="menuBoxInner stockTakeIcon">
  94. <?php echo __('STOCK TAKE UPLOAD - Upload a plain text file (.txt) containing list of Item Code to stock take. Each Item Code separated by line.'); ?><hr />
  95. <form name="uploadForm" method="post" enctype="multipart/form-data" action="<?php echo MODULES_WEB_ROOT_DIR.'stock_take/st_upload.php'; ?>" target="uploadAction" style="display: inline;">
  96. <?php echo __(' File'); //mfc ?>: <input type="file" name="stFile" id="stFile" /> Maximum <?php echo $sysconf['max_upload']; ?> KB
  97. <div style="margin: 3px;"><input type="submit" name="stUpload" id="stUpload" value="<?php echo __('Upload File'); ?>" class="button" />
  98. <iframe name="uploadAction" style="width: 0; height: 0; visibility: hidden;"></iframe>
  99. </div>
  100. </form>
  101. </div>
  102. </fieldset>
  103. <div id="stUploadMsg" class="infoBox" style="display: none;">&nbsp;</div>&nbsp;