PageRenderTime 39ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/data_classes/InventoryTransaction.class.php

http://tracmor.googlecode.com/
PHP | 233 lines | 121 code | 17 blank | 95 comment | 13 complexity | 171e41d3812f5f4905e5cb882ac8c90a MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /*
  3. * Copyright (c) 2009, Tracmor, LLC
  4. *
  5. * This file is part of Tracmor.
  6. *
  7. * Tracmor is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * Tracmor is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with Tracmor; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. ?>
  22. <?php
  23. require(__DATAGEN_CLASSES__ . '/InventoryTransactionGen.class.php');
  24. /**
  25. * The InventoryTransaction class defined here contains any
  26. * customized code for the InventoryTransaction class in the
  27. * Object Relational Model. It represents the "inventory_transaction" table
  28. * in the database, and extends from the code generated abstract InventoryTransactionGen
  29. * class, which contains all the basic CRUD-type functionality as well as
  30. * basic methods to handle relationships and index-based loading.
  31. *
  32. * @package My Application
  33. * @subpackage DataObjects
  34. *
  35. */
  36. class InventoryTransaction extends InventoryTransactionGen {
  37. /**
  38. * Default "to string" handler
  39. * Allows pages to _p()/echo()/print() this object, and to define the default
  40. * way this object would be outputted.
  41. *
  42. * Can also be called directly via $objInventoryTransaction->__toString().
  43. *
  44. * @return string a nicely formatted string representation of this object
  45. */
  46. public function __toString() {
  47. return sprintf('InventoryTransaction Object %s', $this->intInventoryTransactionId);
  48. }
  49. /**
  50. * Returns a string denoting status of InventoryTransaction
  51. *
  52. * @return string either 'Received' or 'Pending'
  53. */
  54. public function __toStringStatus() {
  55. if ($this->blnReturnReceivedStatus()) {
  56. $strToReturn = 'Received';
  57. }
  58. else {
  59. $strToReturn = 'Pending';
  60. }
  61. return sprintf('%s', $strToReturn);
  62. }
  63. /**
  64. * Returns the SourceLocation of an InventoryTransaction if it exists
  65. * This was created in case an InventoryTransaction does not have a SourceLocation - it would generate an error in a datagrid
  66. *
  67. * @return string SourceLocation Short Description
  68. */
  69. public function __toStringSourceLocation() {
  70. if ($this->intSourceLocationId) {
  71. $strToReturn = $this->SourceLocation->__toString();
  72. }
  73. else {
  74. $strToReturn = null;
  75. }
  76. return sprintf('%s', $strToReturn);
  77. }
  78. /**
  79. * Returns the DestinationLocation of an InventoryTransaction if it exists
  80. * This was created because Pending Receipts do not have a Destination Location
  81. *
  82. * @return string DestinationLocation Short Description
  83. */
  84. public function __toStringDestinationLocation() {
  85. if ($this->intDestinationLocationId) {
  86. $strToReturn = $this->DestinationLocation->__toString();
  87. }
  88. else {
  89. $strToReturn = null;
  90. }
  91. return sprintf('%s', $strToReturn);
  92. }
  93. /**
  94. * Returns a boolean value - false if DestinationLocation is empty, true if it is not
  95. * InventoryTransactions with an empty DestinationLocation are Pending Receipts
  96. *
  97. * @return bool
  98. */
  99. public function blnReturnReceivedStatus() {
  100. if ($this->DestinationLocation) {
  101. return true;
  102. }
  103. else {
  104. return false;
  105. }
  106. }
  107. // This adds the created by and creation date before saving a new InventoryTransaction
  108. public function Save($blnForceInsert = false, $blnForceUpdate = false) {
  109. if ((!$this->__blnRestored) || ($blnForceInsert)) {
  110. $this->CreatedBy = QApplication::$objUserAccount->UserAccountId;
  111. $this->CreationDate = new QDateTime(QDateTime::Now);
  112. }
  113. else {
  114. $this->ModifiedBy = QApplication::$objUserAccount->UserAccountId;
  115. }
  116. parent::Save($blnForceInsert, $blnForceUpdate);
  117. }
  118. /**
  119. * Load an array of InventoryTransaction objects,
  120. * by Inventory ModelId
  121. * @param integer $intInventoryModelId
  122. * @param string $strOrderBy
  123. * @param string $strLimit
  124. * @param array $objExpansionMap map of referenced columns to be immediately expanded via early-binding
  125. * @return InventoryTransaction[]
  126. */
  127. public static function LoadArrayByInventoryModelId($intInventoryModelId, $strOrderBy = null, $strLimit = null, $objExpansionMap = null) {
  128. // Call to ArrayQueryHelper to Get Database Object and Get SQL Clauses
  129. InventoryTransaction::ArrayQueryHelper($strOrderBy, $strLimit, $strLimitPrefix, $strLimitSuffix, $strExpandSelect, $strExpandFrom, $objExpansionMap, $objDatabase);
  130. // Properly Escape All Input Parameters using Database->SqlVariable()
  131. $intInventoryModelId = $objDatabase->SqlVariable($intInventoryModelId, true);
  132. // Setup the SQL Query
  133. $strQuery = sprintf('
  134. SELECT
  135. %s
  136. `inventory_transaction`.`inventory_transaction_id` AS `inventory_transaction_id`,
  137. `inventory_transaction`.`inventory_location_id` AS `inventory_location_id`,
  138. `inventory_transaction`.`transaction_id` AS `transaction_id`,
  139. `inventory_transaction`.`quantity` AS `quantity`,
  140. `inventory_transaction`.`source_location_id` AS `source_location_id`,
  141. `inventory_transaction`.`destination_location_id` AS `destination_location_id`
  142. %s
  143. FROM
  144. `inventory_location` AS `inventory_location`,
  145. `inventory_transaction` AS `inventory_transaction`
  146. %s
  147. WHERE
  148. `inventory_location`.`inventory_model_id` %s
  149. AND `inventory_location`.`inventory_location_id` = `inventory_transaction`.`inventory_location_id`
  150. %s
  151. %s', $strLimitPrefix, $strExpandSelect, $strExpandFrom,
  152. $intInventoryModelId,
  153. $strOrderBy, $strLimitSuffix);
  154. // Perform the Query and Instantiate the Result
  155. $objDbResult = $objDatabase->Query($strQuery);
  156. return InventoryTransaction::InstantiateDbResult($objDbResult);
  157. }
  158. /**
  159. * Count InventoryTransactions
  160. * by InventoryModelId Index(es)
  161. * @param integer $intInventoryModelId
  162. * @return int
  163. */
  164. public static function CountByInventoryModelId($intInventoryModelId) {
  165. // Call to ArrayQueryHelper to Get Database Object and Get SQL Clauses
  166. InventoryTransaction::QueryHelper($objDatabase);
  167. // Properly Escape All Input Parameters using Database->SqlVariable()
  168. $intInventoryModelId = $objDatabase->SqlVariable($intInventoryModelId, true);
  169. // Setup the SQL Query
  170. $strQuery = sprintf('
  171. SELECT
  172. COUNT(inventory_transaction.inventory_transaction_id) AS row_count
  173. FROM
  174. `inventory_location`,
  175. `inventory_transaction`
  176. WHERE
  177. `inventory_location`.`inventory_model_id` %s
  178. AND `inventory_location`.`inventory_location_id` = `inventory_transaction`.`inventory_location_id`', $intInventoryModelId);
  179. // Perform the Query and Return the Count
  180. $objDbResult = $objDatabase->Query($strQuery);
  181. $strDbRow = $objDbResult->FetchRow();
  182. return QType::Cast($strDbRow[0], QType::Integer);
  183. }
  184. /**
  185. * Count InventoryTransactions
  186. * by InventoryModelId Index(es), but only those transactions that are Shipments or Receipts
  187. * @param integer $intAssetId
  188. * @param boolean $blnInclude - include only shipments and receipts or all other transactions
  189. * @return int
  190. */
  191. public static function CountShipmentReceiptByInventoryModelId($intInventoryModelId, $blnInclude = true) {
  192. // Call AssetTransaction::QueryCount to perform the CountByAssetId query
  193. if ($blnInclude) {
  194. $arrToReturn = InventoryTransaction::QueryCount(
  195. QQ::AndCondition(
  196. QQ::Equal(QQN::InventoryTransaction()->InventoryLocation->InventoryModelId, $intInventoryModelId),
  197. QQ::OrCondition(
  198. QQ::Equal(QQN::InventoryTransaction()->Transaction->TransactionTypeId, 6),
  199. QQ::Equal(QQN::InventoryTransaction()->Transaction->TransactionTypeId, 7)
  200. )
  201. )
  202. );
  203. }
  204. else {
  205. $arrToReturn = InventoryTransaction::QueryCount(
  206. QQ::AndCondition(
  207. QQ::Equal(QQN::InventoryTransaction()->InventoryLocation->InventoryModelId, $intInventoryModelId),
  208. QQ::NotEqual(QQN::InventoryTransaction()->Transaction->TransactionTypeId, 6),
  209. QQ::NotEqual(QQN::InventoryTransaction()->Transaction->TransactionTypeId, 7)
  210. )
  211. );
  212. }
  213. return $arrToReturn;
  214. }
  215. }
  216. ?>