PageRenderTime 29ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/portable/asset_audit.php

http://tracmor.googlecode.com/
PHP | 251 lines | 220 code | 14 blank | 17 comment | 44 complexity | b022a821579fb63f98f979a1851b5df8 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. require_once('../includes/prepend.inc.php');
  3. // Check that the user is properly authenticated
  4. if (!isset($_SESSION['intUserAccountId'])) {
  5. // authenticate error
  6. QApplication::Redirect('./index.php');
  7. }
  8. else QApplication::$objUserAccount = UserAccount::Load($_SESSION['intUserAccountId']);
  9. $strWarning = "";
  10. $arrCheckedAssetCode = "";
  11. $strJavaScriptCode = "";
  12. $strCheckedLocationAsset = "";
  13. if ($_POST) {
  14. if ($_POST['method'] == 'complete_transaction') {
  15. $arrLocationAssetCode = array_unique(explode('|',$_POST['result']));
  16. $blnError = false;
  17. $intLocationIdArray = array();
  18. $intAssetIdArray = array();
  19. // Begin error checking for assets
  20. foreach ($arrLocationAssetCode as $strLocationAssetCode) {
  21. if ($strLocationAssetCode) {
  22. list($strLocation, $strAssetCodeArray) = split('[:]',$strLocationAssetCode,2);
  23. // Location must be exist
  24. $objDestinationLocation = Location::LoadByShortDescription($strLocation);
  25. if (!$objDestinationLocation) {
  26. $strWarning .= $strLocation." - Location does not exist.<br />";
  27. $blnError = true;
  28. }
  29. else {
  30. $intLocationIdArray[] = $objDestinationLocation->LocationId;
  31. $arrAssetCode = array_unique(explode('#',$strAssetCodeArray));
  32. foreach ($arrAssetCode as $strAssetCode) {
  33. $objNewAsset = Asset::LoadByAssetCode($strAssetCode);
  34. // Asset Code must be exist
  35. if (!($objNewAsset instanceof Asset)) {
  36. $blnError = true;
  37. $strWarning .= $strAssetCode." - That asset code does not exist.<br />";
  38. }
  39. elseif ($objNewAsset->ArchivedFlag) {
  40. $blnError = true;
  41. $strWarning .= $strAssetCode." - That asset code is invalid.<br />";
  42. }
  43. elseif ($objNewAsset->LinkedFlag) {
  44. $blnError = true;
  45. $strWarning .= $strAssetCode." - That asset is locked to parent asset " . $objNewAsset->ParentAssetCode . ".<br />";
  46. }
  47. elseif (!$blnError && $objNewAsset instanceof Asset) {
  48. $intAssetIdArray[] = $objNewAsset->AssetId;
  49. $objAuditScan = new AuditScan();
  50. $objAuditScan->LocationId = $objDestinationLocation->LocationId;
  51. $objAuditScan->EntityId = $objNewAsset->AssetId;
  52. $objAuditScan->Count = 1;
  53. if ($objDestinationLocation->LocationId != $objNewAsset->LocationId) {
  54. $objAuditScan->SystemCount = 0;
  55. }
  56. else {
  57. $objAuditScan->SystemCount = 1;
  58. }
  59. $objAuditScanArray[] = $objAuditScan;
  60. // Load an array of linked child assets and add to array
  61. $objLinkedChildAssetArray = Asset::LoadChildLinkedArrayByParentAssetId($objNewAsset->AssetId);
  62. if ($objLinkedChildAssetArray) {
  63. foreach ($objLinkedChildAssetArray as $objLinkedChildAsset) {
  64. $intAssetIdArray[] = $objLinkedChildAsset->AssetId;
  65. $objAuditScan = new AuditScan();
  66. $objAuditScan->LocationId = $objDestinationLocation->LocationId;
  67. $objAuditScan->EntityId = $objLinkedChildAsset->AssetId;
  68. $objAuditScan->Count = 1;
  69. if ($objDestinationLocation->LocationId != $objLinkedChildAsset->LocationId) {
  70. $objAuditScan->SystemCount = 0;
  71. }
  72. else {
  73. $objAuditScan->SystemCount = 1;
  74. }
  75. $objAuditScanArray[] = $objAuditScan;
  76. }
  77. }
  78. }
  79. }
  80. }
  81. }
  82. }
  83. // Submit
  84. if (!$blnError) {
  85. // Add missing assets that should have been at a location covered by the audit session but were not scanned
  86. if ($intLocationIdArray) {
  87. foreach ($intLocationIdArray as $intLocationId) {
  88. $objAssetLocationArray = Asset::LoadArrayByLocationId($intLocationId);
  89. if ($objAssetLocationArray) {
  90. foreach ($objAssetLocationArray as $objAsset) {
  91. if (!in_array($objAsset->AssetId, $intAssetIdArray)) {
  92. $objNewAuditScan = new AuditScan();
  93. $objNewAuditScan->LocationId = $intLocationId;
  94. $objNewAuditScan->Asset = $objAsset;
  95. $objNewAuditScan->Count = 0;
  96. $objNewAuditScan->SystemCount = 1;
  97. $objAuditScanArray[] = $objNewAuditScan;
  98. unset($objNewAuditScan);
  99. }
  100. }
  101. }
  102. }
  103. }
  104. try {
  105. // Get an instance of the database
  106. $objDatabase = QApplication::$Database[1];
  107. // Begin a MySQL Transaction to be either committed or rolled back
  108. $objDatabase->TransactionBegin();
  109. $objAudit = new Audit();
  110. $objAudit->EntityQtypeId = 1; // Asset
  111. $objAudit->Save();
  112. foreach ($objAuditScanArray as $objAuditScan) {
  113. $objAuditScan->AuditId = $objAudit->AuditId;
  114. $objAuditScan->Save();
  115. }
  116. $objDatabase->TransactionCommit();
  117. $strWarning .= "Your transaction has successfully completed<br /><a href='index.php'>Main Menu</a> | <a href='asset_menu.php'>Manage Assets</a><br />";
  118. //Remove that flag when transaction is compelete or exists some errors
  119. unset($_SESSION['intUserAccountId']);
  120. $blnTransactionComplete = true;
  121. }
  122. catch (QExtendedOptimisticLockingException $objExc) {
  123. // Rollback the database
  124. $objDatabase->TransactionRollback();
  125. }
  126. }
  127. }
  128. elseif ($_POST['method'] == 'next_location') {
  129. // Load locations that have already been added
  130. if ($_POST['main_result']) {
  131. $strCheckedLocationAsset = $_POST['main_result'];
  132. }
  133. $arrCheckedAssetCode = array();
  134. $arrAssetCode = array_unique(explode('#',$_POST['result']));
  135. $blnError = false;
  136. $arrCheckedAssetCode = array();
  137. // Begin error checking for assets
  138. foreach ($arrAssetCode as $strAssetCode) {
  139. if ($strAssetCode) {
  140. $objNewAsset = Asset::LoadByAssetCode($strAssetCode);
  141. if (!($objNewAsset instanceof Asset)) {
  142. $blnError = true;
  143. $strWarning .= $strAssetCode." - That asset code does not exist.<br />";
  144. }
  145. else {
  146. // Check a duplicate asset code
  147. if ($_POST['main_result'] && strstr($_POST['main_result'],$strAssetCode)) {
  148. $arrLocationAsset = explode('|',$_POST['main_result']);
  149. foreach ($arrLocationAsset as $strLocationAsset) {
  150. list($strLocation, $strAsset) = split('[:]',$strLocationAsset,2);
  151. if ($strAsset && strstr($strAsset,$strAssetCode)) {
  152. $blnError = true;
  153. $strWarning .= $strAssetCode." - That asset code has already been added.<br />";
  154. }
  155. }
  156. }
  157. else $arrCheckedAssetCode[] = $strAssetCode;
  158. }
  159. }
  160. else {
  161. $strWarning .= "Please enter an asset code.<br />";
  162. }
  163. }
  164. // Location must be exist
  165. $objDestinationLocation = Location::LoadByShortDescription($_POST['location']);
  166. if ($objDestinationLocation) {
  167. // Check a duplicate location
  168. if ($_POST['main_result'] && strstr($_POST['main_result'],$_POST['location'])) {
  169. $arrLocationAsset = explode('|',$_POST['main_result']);
  170. foreach ($arrLocationAsset as $strLocationAsset) {
  171. list($strLocation, $strAsset) = split('[:]',$strLocationAsset,2);
  172. if ($strAsset && strstr($strLocation,$_POST['location'])) {
  173. $blnError = true;
  174. $strWarning .= $_POST['location']." - That location has already been added.<br />";
  175. break;
  176. }
  177. }
  178. }
  179. }
  180. if (!$objDestinationLocation) {
  181. $strWarning .= $_POST['location']." - Location does not exist. Please provide another location.<br />";
  182. $blnError = true;
  183. }
  184. elseif (!$blnError) {
  185. $strWarning .= $_POST['location']." - Location added. Please provide another location or click 'Done'.<br />";
  186. if ($_POST['main_result']) {
  187. $strCheckedLocationAsset .= "|".$_POST['location'].":".$_POST['result'];
  188. }
  189. else {
  190. $strCheckedLocationAsset = $_POST['location'].":".$_POST['result'];
  191. }
  192. }
  193. if ($blnError && is_array($arrCheckedAssetCode)) {
  194. $strJavaScriptCode .= " strCheckedAssetCode = '".implode("#",$arrCheckedAssetCode)."';";
  195. $strJavaScriptCode .= " document.getElementById('location').value = '".$_POST['location']."';";
  196. }
  197. }
  198. }
  199. $strTitle = "Assets Audit";
  200. $strBodyOnLoad = "document.getElementById('location').disabled = false; document.getElementById('btn_add_location').disabled = false; document.getElementById('asset_code').disabled = true; document.getElementById('btn_add_asset').disabled = true; document.getElementById('location').focus();".$strJavaScriptCode;
  201. require_once('./includes/header.inc.php');
  202. ?>
  203. <div id="warning"><?php echo $strWarning; ?></div>
  204. <?php
  205. if (!isset($blnTransactionComplete) || !$blnTransactionComplete) {
  206. ?>
  207. <table border=0 style="padding-top:16px;">
  208. <tr>
  209. <td align="right"><h2>Location:</h2><input style="display:none;" type="button" value="Add Location" id="btn_add_location" onclick="javascript:AddAuditLocation();"></td>
  210. <td valign="top"><input type="text" id="location" onkeypress="javascript:if(event.keyCode=='13') AddAuditLocation();" style="width:170px;font-size:32;border:2px solid #AAAAAA;background-color:#FFFFFF;" onfocus="this.style.backgroundColor='lightyellow'" onblur="this.style.backgroundColor='#FFFFFF'"></td>
  211. </tr>
  212. <tr>
  213. <td align="right"><h2>Asset Code:</h2><input style="display:none" type="button" value="Add Asset" id="btn_add_asset" onclick="javascript:AddAuditAsset();" disabled></td>
  214. <td valign="top"><input type="text" id="asset_code" onkeypress="javascript:if(event.keyCode=='13') AddAuditAsset();" style="width:170px;font-size:32;border:2px solid #AAAAAA;background-color:#FFFFFF;" onfocus="this.style.backgroundColor='lightyellow'" onblur="this.style.backgroundColor='#FFFFFF'" disabled></td>
  215. </tr>
  216. <form method="post" name="nextlocation_form" onsubmit="javascript:return NextLocation();">
  217. <input type="hidden" name="method" value="next_location">
  218. <input type="hidden" name="result" value="">
  219. <input type="hidden" name="main_result" value="<?php echo $strCheckedLocationAsset; ?>">
  220. <input type="hidden" name="location" value="">
  221. <tr>
  222. <td colspan="2" align="center"><input type="submit" id="btn_next" value="Next Location" style="width:216px;height:56px;font-size:24;"></td>
  223. </tr>
  224. </form>
  225. <form method="post" name="main_form" onsubmit="javascript:return AssetsAuditDone();">
  226. <input type="hidden" name="method" value="complete_transaction">
  227. <input type="hidden" name="result" value="<?php echo $strCheckedLocationAsset; ?>">
  228. <tr>
  229. <td colspan="2" align="center"><input type="submit" value="Done" style="width:216px;height:56px;font-size:24;"></td>
  230. </tr>
  231. </form>
  232. </table><p>
  233. <div id="output" style="font-size:24;width:100%;border-top:1px solid #CCCCCC;"></div>
  234. <?php
  235. }
  236. require_once('./includes/footer.inc.php');
  237. ?>