PageRenderTime 52ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/Scenario/Clientapp/model/homePageModel.php

#
PHP | 260 lines | 141 code | 42 blank | 77 comment | 28 complexity | 9ed0823ee158a15719367641a4e7708a MD5 | raw file
  1. <?php
  2. /**
  3. *
  4. * Copyright (c) 2009, Persistent Systems Limited
  5. *
  6. * Redistribution and use, with or without modification, are permitted
  7. * provided that the following conditions are met:
  8. * - Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * - Neither the name of Persistent Systems Limited nor the names of its contributors
  11. * may be used to endorse or promote products derived from this software
  12. * without specific prior written permission.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  15. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  16. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  17. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  18. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  19. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  20. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  21. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  22. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  23. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  24. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. /**
  27. * classes/AtomParser.php
  28. */
  29. require_once 'classes/AtomParser.php';
  30. /**
  31. * HomePageModel Class to process functionality of showing graph on home page
  32. * @access public
  33. */
  34. class HomePageModel
  35. {
  36. /**
  37. * Class constructor.
  38. * @access public
  39. *
  40. */
  41. public function __construct(){}
  42. /**
  43. * This function validate form field data such as From Year and To year
  44. * @access public
  45. * @param array $postFields Home Page Form Field data
  46. * @return array $errors Array of error messages if any
  47. */
  48. public function validateFormFields($postFields = null)
  49. {
  50. $errors = array();
  51. try{
  52. if(($postFields['listFromYear'] > $postFields['listToYear']) && $postFields['rdoGroupFilter'] === 'year'){
  53. $errors[] = "Start year must be less than end year.";
  54. }
  55. } catch (Exception $e){
  56. throw($e);
  57. }
  58. return $errors;
  59. }
  60. /**
  61. * The function to get region list
  62. * @access public
  63. *
  64. * @return array $regionListArr Array of result and error
  65. */
  66. public function getRegionList()
  67. {
  68. global $httpRequestObj;
  69. $regionListArr = array();
  70. $requestInfoArr = array();
  71. try{
  72. $url = REGIONS_SERVICE_URL;
  73. $httpVerb = 'GET';
  74. //$headerValue = "WRAPv0.8" . " " . urldecode($_SESSION['token']);
  75. $token = "WRAP" . " " . 'access_token="' . urldecode($_SESSION['token']) . '"';
  76. $headerValue = $token;
  77. $head['X-Authorization'] = $headerValue;
  78. $requestInfoArr['http_method'] = $httpVerb;
  79. $requestInfoArr['url'] = $url;
  80. if(!empty($head)){
  81. foreach($head as $key => $value){
  82. $requestInfoArr[$key] = urlencode($value);
  83. }
  84. }
  85. //Write Audit Log Details
  86. LoggerClass::writeLogEntry(REQUEST_TO_SALESDASHBOARD_SERVICE.'-'.GET_REGIONS, $requestInfoArr);
  87. $regionsFeedData = $httpRequestObj->sendRequest($httpVerb, $url ,array() ,$head);
  88. $httpCode = $regionsFeedData['http_code'];
  89. $response = $regionsFeedData['response'];
  90. //Write Audit Log Details
  91. LoggerClass::writeLogEntry(RESPONSE_FROM_SALESDASHBOARD_SERVICE.'-'.GET_REGIONS, $response);
  92. if ($httpCode == '202'){
  93. $regionObj = new AtomParser($response);
  94. $regionListArr['result'] = $regionObj->parseString();
  95. } else {
  96. $regionListArr['error'] = 'Error Code '.$httpCode.':'.getStatusCodeMessage($httpCode).' For get regions';
  97. }
  98. } catch (Exception $e){
  99. throw($e);
  100. }
  101. return $regionListArr;
  102. }
  103. /**
  104. * The function to get product list
  105. * @access public
  106. *
  107. * @return array $productsArr Array of result and error
  108. */
  109. public function getProductList()
  110. {
  111. global $httpRequestObj;
  112. $productsArr = array();
  113. $requestInfoArr = array();
  114. try{
  115. $url = PRODUCTS_SERVICE_URL;
  116. $httpVerb = 'GET';
  117. //$headerValue = "WRAPv0.8" . " " . urldecode($_SESSION['token']);
  118. $token = "WRAP" . " " . 'access_token="' . urldecode($_SESSION['token']) . '"';
  119. $headerValue = $token;
  120. $head['X-Authorization'] = $headerValue;
  121. $requestInfoArr['http_method'] = $httpVerb;
  122. $requestInfoArr['url'] = $url;
  123. if(!empty($head)){
  124. foreach($head as $key => $value){
  125. $requestInfoArr[$key] = urlencode($value);
  126. }
  127. }
  128. //Write Audit Log Details
  129. LoggerClass::writeLogEntry(REQUEST_TO_SALESDASHBOARD_SERVICE.'-'.GET_PRODUCTS, $requestInfoArr);
  130. $productsFeedData = $httpRequestObj->sendRequest($httpVerb, $url,array(), $head);
  131. $httpCode = $productsFeedData['http_code'];
  132. $response = $productsFeedData['response'];
  133. //Write Audit Log Details
  134. LoggerClass::writeLogEntry(RESPONSE_FROM_SALESDASHBOARD_SERVICE.'-'.GET_PRODUCTS,$response);
  135. if ($httpCode == '202'){
  136. $productObj = new AtomParser($response);
  137. $productsArr['result'] = $productObj->parseString();
  138. } else {
  139. $productsArr['error'] = 'Error Code '.$httpCode.':'.getStatusCodeMessage($httpCode).' for get products';
  140. }
  141. } catch (Exception $e){
  142. throw($e);
  143. }
  144. return $productsArr;
  145. }
  146. /**
  147. * The function to consolidated sales order data
  148. * @access public
  149. *
  150. * @param integer $productId Product Id
  151. * @param integer $regionId Region Id
  152. * @param integer $year Year
  153. * @param integer $fromYear From Year
  154. * @param integer $toYear To Year
  155. * @param integer $isYearlyOrMonthly Monthly/Yearly option
  156. * @return array $salesDataArr Array of result and error
  157. */
  158. public function getConsolidatedSalesData($productId="", $regionId ="",$year = "2009", $fromYear="", $toYear="", $isYearlyOrMonthly="monthly")
  159. {
  160. global $httpRequestObj;
  161. $salesDataArr = array();
  162. $requestInfoArr = array();
  163. $qryString = "";
  164. try {
  165. //Prepare URL Query String
  166. if (isset($productId) && $productId > 0){
  167. $qryString .= "/productid/".$productId."/";
  168. }
  169. if (isset($regionId) && $regionId > 0){
  170. $qryString .= "/regionid/".$regionId."/";
  171. }
  172. if($isYearlyOrMonthly == "monthly"){
  173. if (isset($year) && $year > 0){
  174. $qryString .= "/year/".$year."/";
  175. }
  176. }
  177. if($isYearlyOrMonthly == "yearly"){
  178. if (isset($fromYear) && $fromYear > 0){
  179. $qryString .= "/fromyear/".$fromYear."/";
  180. }
  181. if (isset($toYear) && $toYear > 0){
  182. $qryString .= "/toyear/".$toYear."/";
  183. }
  184. }
  185. $httpVerb = 'GET';
  186. $url = SALES_DATA_SERVICE_URL.$qryString;
  187. //$headerValue = "WRAPv0.8" . " " . urldecode($_SESSION['token']);
  188. $token = "WRAP" . " " . 'access_token="' . urldecode($_SESSION['token']) . '"';
  189. $headerValue = $token;
  190. $head['X-Authorization'] = $headerValue;
  191. $requestInfoArr['http_method'] = $httpVerb;
  192. $requestInfoArr['url'] = $url;
  193. if(!empty($head)){
  194. foreach($head as $key => $value){
  195. $requestInfoArr[$key] = urlencode($value);
  196. }
  197. }
  198. //Write Audit Log Details
  199. LoggerClass::writeLogEntry(REQUEST_TO_SALESDASHBOARD_SERVICE.'-'.GET_SALESDATA, $requestInfoArr);
  200. $salesFeedData = $httpRequestObj->sendRequest($httpVerb, $url, array(), $head);
  201. $httpCode = $salesFeedData['http_code'];
  202. $response = $salesFeedData['response'];
  203. //Write Audit Log Details
  204. LoggerClass::writeLogEntry(RESPONSE_FROM_SALESDASHBOARD_SERVICE.'-'.GET_SALESDATA, $response);
  205. //Parse response
  206. if ($httpCode == '202'){
  207. $salesObj = new AtomParser($response);
  208. $salesDataArr['result'] = $salesObj->parseString();
  209. } else {
  210. $salesDataArr['error'] = $httpCode;
  211. }
  212. } catch (Exception $e){
  213. throw($e);
  214. }
  215. return $salesDataArr;
  216. }
  217. }
  218. ?>