PageRenderTime 51ms CodeModel.GetById 7ms RepoModel.GetById 0ms app.codeStats 0ms

/portable/inventory_restock.php

http://tracmor.googlecode.com/
PHP | 182 lines | 139 code | 22 blank | 21 comment | 32 complexity | 9caadedf91d4d49543afffd1e96acd0f 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. $arrCheckedInventoryCodeQuantity = "";
  11. $strJavaScriptCode = "";
  12. if ($_POST && $_POST['method'] == 'complete_transaction') {
  13. /*
  14. Run error checking on the array of asset codes and the destination location
  15. If there are no errors, then you will add the transaction to the database.
  16. That will include an entry in the Transaction and Asset Transaction table.
  17. You will also have to change the asset.location_id to the destination location
  18. */
  19. $arrInventoryCodeQuantity = array_unique(explode('#',$_POST['result']));
  20. $blnError = false;
  21. $arrCheckedInventoryCodeQuantity = array();
  22. foreach ($arrInventoryCodeQuantity as $strInventoryCodeQuantity) {
  23. $blnErrorCurrentInventory = false;
  24. list($strInventoryModelCode, $intQuantity) = split('[|]',$strInventoryCodeQuantity,2);
  25. if ($strInventoryModelCode && $intQuantity) {
  26. // Begin error checking
  27. // Load the inventory model object based on the inventory_model_code submitted
  28. $objNewInventoryModel = InventoryModel::LoadByInventoryModelCode($strInventoryModelCode);
  29. if (!$objNewInventoryModel) {
  30. $blnError = true;
  31. $blnErrorCurrentInventory = true;
  32. $strWarning .= $strInventoryModelCode." - That is not a valid inventory code.<br />";
  33. }
  34. else {
  35. $intInventoryModelId = $objNewInventoryModel->InventoryModelId;
  36. }
  37. if (isset($objInventoryLocationArray) && !$blnErrorCurrentInventory) {
  38. foreach ($objInventoryLocationArray as $objInventoryLocation) {
  39. if ($objInventoryLocation && $objInventoryLocation->InventoryModelId == $intInventoryModelId) {
  40. $blnError = true;
  41. $blnErrorCurrentInventory = true;
  42. $strWarning .= $strInventoryModelCode." - That Inventory has already been added.<br />";
  43. }
  44. }
  45. }
  46. if (!$blnError) {
  47. // Create a new InventoryLocation for the time being
  48. // Before saving we will check to see if it already exists
  49. $objNewInventoryLocation = new InventoryLocation();
  50. $objNewInventoryLocation->InventoryModelId = $objNewInventoryModel->InventoryModelId;
  51. $objNewInventoryLocation->Quantity = 0;
  52. // LocationID = 4 is 'New Inventory' Location
  53. $objNewInventoryLocation->LocationId = 4;
  54. // This should not be possible because the list is populated with existing InventoryLocations
  55. if (!($objNewInventoryLocation instanceof InventoryLocation)) {
  56. $strWarning .= $strInventoryModelCode." - That Inventory location does not exist.<br />";
  57. $blnErrorCurrentInventory = true;
  58. $blnError = true;
  59. }
  60. elseif (!ctype_digit($intQuantity) || $intQuantity <= 0) {
  61. $strWarning .= $strInventoryModelCode." - That is not a valid quantity.<br />";
  62. $blnErrorCurrentInventory = true;
  63. $blnError = true;
  64. }
  65. }
  66. if (!$blnError && $objNewInventoryLocation instanceof InventoryLocation) {
  67. $objNewInventoryLocation->intTransactionQuantity = $intQuantity;
  68. $objInventoryLocationArray[] = $objNewInventoryLocation;
  69. }
  70. if (!$blnErrorCurrentInventory) {
  71. $arrCheckedInventoryCodeQuantity[] = $strInventoryCodeQuantity;
  72. }
  73. }
  74. else {
  75. if (!ctype_digit($intQuantity) || $intQuantity <= 0) {
  76. $strWarning .= $strInventoryModelCode." - That is not a valid quantity.<br />";
  77. $blnError = true;
  78. }
  79. }
  80. }
  81. if (isset($objInventoryLocationArray)) {
  82. // Destination Location must match an existing location
  83. $strDestinationLocation = $_POST['destination_location'];
  84. if (!($objDestinationLocation = Location::LoadByShortDescription($strDestinationLocation))) {
  85. $blnError = true;
  86. $strWarning .= $strDestinationLocation." - Destination Location does not exist.<br />";
  87. }
  88. if (!$blnError) {
  89. // Create the new transaction object and save it
  90. $objTransaction = new Transaction();
  91. $objTransaction->EntityQtypeId = EntityQtype::Inventory;
  92. $objTransaction->TransactionTypeId = 4; // Restock
  93. $objTransaction->Save();
  94. // Assign different source and destinations depending on transaction type
  95. foreach ($objInventoryLocationArray as $objInventoryLocation) {
  96. // Restock
  97. $SourceLocationId = 4;
  98. $DestinationLocationId = $objDestinationLocation->LocationId;
  99. // Add the new quantity where it belongs for moves and restocks
  100. $objNewInventoryLocation = InventoryLocation::LoadByLocationIdInventoryModelId($DestinationLocationId, $objInventoryLocation->InventoryModelId);
  101. if ($objNewInventoryLocation) {
  102. $objNewInventoryLocation->Quantity = $objNewInventoryLocation->Quantity + $objInventoryLocation->intTransactionQuantity;
  103. }
  104. else {
  105. $objNewInventoryLocation = new InventoryLocation();
  106. $objNewInventoryLocation->InventoryModelId = $objInventoryLocation->InventoryModelId;
  107. $objNewInventoryLocation->Quantity = $objInventoryLocation->intTransactionQuantity;
  108. }
  109. $objNewInventoryLocation->LocationId = $DestinationLocationId;
  110. $objNewInventoryLocation->Save();
  111. // Create the new InventoryTransaction object and save it
  112. $objInventoryTransaction = new InventoryTransaction();
  113. $objInventoryTransaction->InventoryLocationId = $objNewInventoryLocation->InventoryLocationId;
  114. $objInventoryTransaction->TransactionId = $objTransaction->TransactionId;
  115. $objInventoryTransaction->Quantity = $objInventoryLocation->intTransactionQuantity;
  116. $objInventoryTransaction->SourceLocationId = $SourceLocationId;
  117. $objInventoryTransaction->DestinationLocationId = $DestinationLocationId;
  118. $objInventoryTransaction->Save();
  119. }
  120. $strWarning .= "Your transaction has successfully completed<br /><a href='index.php'>Main Menu</a> | <a href='inventory_menu.php'>Inventory Menu</a><br />";
  121. //Remove that flag when transaction is compelete or exists some errors
  122. unset($_SESSION['intUserAccountId']);
  123. $blnTransactionComplete = true;
  124. $arrCheckedInventoryCodeQuantity = "";
  125. }
  126. }
  127. if ($blnError) {
  128. $strWarning .= "This transaction has not been completed.<br />";
  129. }
  130. if (is_array($arrCheckedInventoryCodeQuantity)) {
  131. foreach ($arrCheckedInventoryCodeQuantity as $strInventoryCodeQuantity) {
  132. list($strInventoryModelCode, $intQuantity) = split('[|]',$strInventoryCodeQuantity,2);
  133. $strJavaScriptCode .= "AddInventoryQuantityPost('".$strInventoryModelCode."','".$intQuantity."');";
  134. }
  135. }
  136. }
  137. $strTitle = "Restock Inventory";
  138. $strBodyOnLoad = "document.getElementById('inventory_code').focus();".$strJavaScriptCode;
  139. require_once('./includes/header.inc.php');
  140. ?>
  141. <div id="warning"><?php echo $strWarning; ?></div>
  142. <?php
  143. if (!isset($blnTransactionComplete) || !$blnTransactionComplete) {
  144. ?>
  145. Inventory Code: <input type="text" id="inventory_code" size="20">
  146. <br /><br />
  147. Quantity: <input type="text" id="quantity" size="10" onkeypress="javascript:if(event.keyCode=='13') AddInventoryQuantity();">
  148. <input type="button" value="Add" onclick="javascript:AddInventoryQuantity();">
  149. <br /><br />
  150. <form method="post" name="main_form" onsubmit="javascript:return CompleteMoveInventory();">
  151. <input type="hidden" name="method" value="complete_transaction">
  152. <input type="hidden" name="result" value="">
  153. Destination Location: <input type="text" name="destination_location" onkeypress="javascript:if(event.keyCode=='13') CompleteMoveInventory();" size="20">
  154. <input type="submit" value="Complete Move">
  155. </form>
  156. <div id="output"></div>
  157. <?php
  158. }
  159. require_once('./includes/footer.inc.php');
  160. ?>