PageRenderTime 21ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/portable/inventory_move.php

http://tracmor.googlecode.com/
PHP | 218 lines | 174 code | 22 blank | 22 comment | 44 complexity | 6108b6d8c1d81cdc904833447f0ba5a6 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. $arrCheckedInventoryCodeLocationQuantity = "";
  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. $arrInventoryCodeLocationQuantity = array_unique(explode('#',$_POST['result']));
  20. $blnError = false;
  21. $arrCheckedInventoryCodeLocationQuantity = array();
  22. foreach ($arrInventoryCodeLocationQuantity as $strInventoryCodeLocationQuantity) {
  23. $blnErrorCurrentInventory = false;
  24. list($strInventoryModelCode, $strSourceLocation, $intQuantity) = split('[|]',$strInventoryCodeLocationQuantity,3);
  25. if ($strInventoryModelCode && $strSourceLocation && $intQuantity) {
  26. $intNewInventoryLocationId = 0;
  27. // Begin error checking
  28. // Load the inventory model object based on the inventory_model_code submitted
  29. $objInventoryModel = InventoryModel::LoadByInventoryModelCode($strInventoryModelCode);
  30. if ($objInventoryModel) {
  31. // Load the array of InventoryLocations based on the InventoryModelId of the InventoryModel object
  32. $InventorySourceLocationArray = InventoryLocation::LoadArrayByInventoryModelIdLocations($objInventoryModel->InventoryModelId);
  33. if ($InventorySourceLocationArray) {
  34. $blnErrorCurrentInventory = true;
  35. foreach ($InventorySourceLocationArray as $InventoryLocation) {
  36. if ($InventoryLocation->Quantity != 0) {
  37. if (strtoupper($InventoryLocation->__toString()) == strtoupper($strSourceLocation)) {
  38. $blnErrorCurrentInventory = false;
  39. $intNewInventoryLocationId = $InventoryLocation->InventoryLocationId;
  40. $objNewInventoryLocation = $InventoryLocation;
  41. }
  42. }
  43. }
  44. if ($blnErrorCurrentInventory) {
  45. $strWarning .= $strInventoryModelCode." - There is no source location for that inventory code.<br />";
  46. $blnError = true;
  47. }
  48. }
  49. else {
  50. $blnError = true;
  51. $blnErrorCurrentInventory = true;
  52. $strWarning .= $strInventoryModelCode." - There is no source location for that inventory code.<br />";
  53. }
  54. }
  55. else {
  56. $blnError = true;
  57. $blnErrorCurrentInventory = true;
  58. $strWarning .= $strInventoryModelCode." - That is not a valid inventory code.<br />";
  59. }
  60. if (isset($objInventoryLocationArray)) {
  61. foreach ($objInventoryLocationArray as $objInventoryLocation) {
  62. if ($objInventoryLocation && $objInventoryLocation->InventoryLocationId == $intNewInventoryLocationId) {
  63. $blnError = true;
  64. $blnErrorCurrentInventory = true;
  65. $strWarning .= $strInventoryModelCode." - That Inventory has already been added.<br />";
  66. }
  67. }
  68. }
  69. if (!$blnError) {
  70. // This should not be possible because the list is populated with existing InventoryLocations
  71. if (!($objNewInventoryLocation instanceof InventoryLocation)) {
  72. $strWarning .= $strInventoryModelCode." - That Inventory location does not exist.<br />";
  73. $blnErrorCurrentInventory = true;
  74. $blnError = true;
  75. }
  76. elseif (!ctype_digit($intQuantity) || $intQuantity <= 0) {
  77. $strWarning .= $strInventoryModelCode." - That is not a valid quantity.<br />";
  78. $blnErrorCurrentInventory = true;
  79. $blnError = true;
  80. }
  81. // Move
  82. elseif ($objNewInventoryLocation->Quantity < $intQuantity) {
  83. $strWarning .= $strInventoryModelCode." - Quantity moved cannot exceed quantity available.<br />";
  84. $blnErrorCurrentInventory = true;
  85. $blnError = true;
  86. }
  87. }
  88. if (!$blnError && $objNewInventoryLocation instanceof InventoryLocation) {
  89. $objNewInventoryLocation->intTransactionQuantity = $intQuantity;
  90. $objInventoryLocationArray[] = $objNewInventoryLocation;
  91. }
  92. if (!$blnErrorCurrentInventory) {
  93. $arrCheckedInventoryCodeLocationQuantity[] = $strInventoryCodeLocationQuantity;
  94. }
  95. }
  96. else {
  97. if (!ctype_digit($intQuantity) || $intQuantity <= 0) {
  98. $strWarning .= $strInventoryModelCode." - That is not a valid quantity.<br />";
  99. $blnError = true;
  100. }
  101. }
  102. }
  103. if (isset($objInventoryLocationArray)) {
  104. // Destination Location must match an existing location
  105. $strDestinationLocation = $_POST['destination_location'];
  106. if (!($objDestinationLocation = Location::LoadByShortDescription($strDestinationLocation))) {
  107. $blnError = true;
  108. $strWarning .= $strDestinationLocation." - Destination Location does not exist.<br />";
  109. }
  110. else {
  111. foreach ($objInventoryLocationArray as $objInventoryLocation) {
  112. if ($objInventoryLocation->LocationId == $objDestinationLocation->LocationId) {
  113. $strWarning .= $strInventoryModelCode." - Cannot move inventory from a location to the same location.<br />";
  114. $blnError = true;
  115. }
  116. }
  117. }
  118. if (!$blnError) {
  119. // Create the new transaction object and save it
  120. $objTransaction = new Transaction();
  121. $objTransaction->EntityQtypeId = EntityQtype::Inventory;
  122. $objTransaction->TransactionTypeId = 1; // Move
  123. $objTransaction->Save();
  124. // Assign different source and destinations depending on transaction type
  125. foreach ($objInventoryLocationArray as $objInventoryLocation) {
  126. // Move
  127. $SourceLocationId = $objInventoryLocation->LocationId;
  128. $DestinationLocationId = $objDestinationLocation->LocationId;
  129. // Remove the inventory quantity from the source for moves and take outs
  130. //$objInventoryLocation->Quantity = $objInventoryLocation->Quantity - $objInventoryLocation->intTransactionQuantity;
  131. $objInventoryLocation->Quantity = $objInventoryLocation->GetVirtualAttribute('actual_quantity') - $objInventoryLocation->intTransactionQuantity;
  132. $objInventoryLocation->Save();
  133. // Add the new quantity where it belongs for moves and restocks
  134. $objNewInventoryLocation = InventoryLocation::LoadByLocationIdInventoryModelId($DestinationLocationId, $objInventoryLocation->InventoryModelId);
  135. if ($objNewInventoryLocation) {
  136. $objNewInventoryLocation->Quantity = $objNewInventoryLocation->Quantity + $objInventoryLocation->intTransactionQuantity;
  137. }
  138. else {
  139. $objNewInventoryLocation = new InventoryLocation();
  140. $objNewInventoryLocation->InventoryModelId = $objInventoryLocation->InventoryModelId;
  141. $objNewInventoryLocation->Quantity = $objInventoryLocation->intTransactionQuantity;
  142. }
  143. $objNewInventoryLocation->LocationId = $DestinationLocationId;
  144. $objNewInventoryLocation->Save();
  145. // Create the new InventoryTransaction object and save it
  146. $objInventoryTransaction = new InventoryTransaction();
  147. $objInventoryTransaction->InventoryLocationId = $objNewInventoryLocation->InventoryLocationId;
  148. $objInventoryTransaction->TransactionId = $objTransaction->TransactionId;
  149. $objInventoryTransaction->Quantity = $objInventoryLocation->intTransactionQuantity;
  150. $objInventoryTransaction->SourceLocationId = $SourceLocationId;
  151. $objInventoryTransaction->DestinationLocationId = $DestinationLocationId;
  152. $objInventoryTransaction->Save();
  153. }
  154. $strWarning .= "Your transaction has successfully completed<br /><a href='index.php'>Main Menu</a> | <a href='inventory_menu.php'>Inventory Menu</a><br />";
  155. //Remove that flag when transaction is compelete or exists some errors
  156. unset($_SESSION['intUserAccountId']);
  157. $blnTransactionComplete = true;
  158. $arrCheckedInventoryCodeLocationQuantity = "";
  159. }
  160. }
  161. if ($blnError) {
  162. $strWarning .= "This transaction has not been completed.<br />";
  163. }
  164. if (is_array($arrCheckedInventoryCodeLocationQuantity)) {
  165. foreach ($arrCheckedInventoryCodeLocationQuantity as $strInventoryCodeLocationQuantity) {
  166. list($strInventoryModelCode, $strSourceLocation, $intQuantity) = split('[|]',$strInventoryCodeLocationQuantity,3);
  167. $strJavaScriptCode .= "AddInventoryPost('".$strInventoryModelCode."','".$strSourceLocation."','".$intQuantity."');";
  168. }
  169. }
  170. }
  171. $strTitle = "Move Inventory";
  172. $strBodyOnLoad = "document.getElementById('inventory_code').focus();".$strJavaScriptCode;
  173. require_once('./includes/header.inc.php');
  174. ?>
  175. <div id="warning"><?php echo $strWarning; ?></div>
  176. <?php
  177. if (!isset($blnTransactionComplete) || !$blnTransactionComplete) {
  178. ?>
  179. Inventory Code: <input type="text" id="inventory_code" size="20">
  180. <br /><br />
  181. Source Location: <input type="text" id="source_location" size="20">
  182. <br /><br />
  183. Quantity: <input type="text" id="quantity" size="10" onkeypress="javascript:if(event.keyCode=='13') AddInventory();">
  184. <input type="button" value="Add" onclick="javascript:AddInventory();">
  185. <br /><br />
  186. <form method="post" name="main_form" onsubmit="javascript:return CompleteMoveInventory();">
  187. <input type="hidden" name="method" value="complete_transaction">
  188. <input type="hidden" name="result" value="">
  189. Destination Location: <input type="text" name="destination_location" onkeypress="javascript:if(event.keyCode=='13') CompleteMoveInventory();" size="20">
  190. <input type="submit" value="Complete Move">
  191. </form>
  192. <div id="output"></div>
  193. <?php
  194. }
  195. require_once('./includes/footer.inc.php');
  196. ?>