PageRenderTime 26ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/class/companyclass.php

https://github.com/samsonnguyen/CPSC471carinsurance
PHP | 280 lines | 266 code | 11 blank | 3 comment | 38 complexity | d0aceb95894ca3c24478a1b0686945e1 MD5 | raw file
  1. <?php
  2. class company {
  3. var $error;
  4. var $errorIndex = 0;
  5. function addNewCompany($array) {
  6. $keys = array_keys($array); //Return the keys of the array;
  7. $sql = "INSERT INTO `Company` ("; //Set the first part of the SQL query
  8. for ($i=0; $i<count($keys); $i++) {
  9. if ($i==(count($keys)-1)){//last value, do not include the comma
  10. $sql = $sql."`".$keys[$i]."`";
  11. } else {
  12. $sql = $sql."`".$keys[$i]."`,";
  13. }
  14. }
  15. $sql = $sql.") VALUES (";
  16. for ($i=0; $i<count($keys); $i++) {
  17. if ($i==(count($keys)-1)){ //last value, do not include the comma
  18. $sql = $sql."'".$array[$keys[$i]]."');";
  19. } else {
  20. $sql = $sql."'".$array[$keys[$i]]."',";
  21. }
  22. }
  23. mysql_query($sql) or die(mysql_error());
  24. return true;
  25. }
  26. function deleteCompany($companyno){
  27. $sql="DELETE FROM `Company` WHERE Commercial_License_No='$companyno'";
  28. mysql_query($sql) or die(mysql_error());
  29. return true;
  30. }
  31. static function getAllCompanies($selection){
  32. $companies = mysql_query("SELECT * FROM `Company`");// or die(mysql_error());
  33. if($companies != null){
  34. while($info = mysql_fetch_array($companies)){
  35. print("<option value=\"");
  36. if (($info['Commercial_License_No']!=null) && ($info['Commercial_License_No']!=0)){
  37. if($selection != null && $selection == $info['Commercial_License_No'])
  38. print($info['Commercial_License_No']."\" selected=\"selected\"> [ ");
  39. else
  40. print($info['Commercial_License_No']."\"> [ ");
  41. // Assume size 10 for license
  42. print(str_pad($info['Commercial_License_No'],10,"0",STR_PAD_LEFT)." ]");
  43. print(" Name: ");
  44. print($info['CName']);
  45. print("</option>");
  46. } else {
  47. print("-1\">");
  48. print("ERROR");
  49. print("</option>");
  50. }
  51. }
  52. } else {
  53. print("<option value=\"\" selected=\"selected\">None Exist</option>");
  54. }
  55. }
  56. function searchCompanies($companyno,$policyid,$info){
  57. $flag = '0';
  58. $sql = "SELECT * FROM `Company` WHERE ";
  59. // Company Number
  60. if($companyno != null && $companyno != '0') {
  61. $sql = $sql."Commercial_License_No='$companyno' ";
  62. $flag = '1';
  63. }
  64. // Policy Number
  65. if($policyid != null && $policyid != '0') {
  66. $sql = $sql."Policy_No='$policyid' ";
  67. $flag = '1';
  68. }
  69. // Company Information
  70. if($info != null) {
  71. $keys = array_keys($info);
  72. for ($i = 0; $i< count($keys); $i++){
  73. if ($i==0){
  74. $sql = $sql." ".$keys[$i]." LIKE '".$info[$keys[$i]]."'";
  75. } else {
  76. $sql = $sql." AND '".$keys[$i]."' LIKE '".$info[$keys[$i]]."'";
  77. }
  78. }
  79. $flag = '1'; // This is probably unneeded but just in case.
  80. }
  81. if($flag == '0') { // Nothing was entered
  82. $sql = "SELECT * FROM `Company`";
  83. }
  84. return mysql_query($sql);
  85. }
  86. function display2DArray($result){
  87. $flag = '0';
  88. if($result==null) {
  89. print "No results were found!<br />";
  90. return;
  91. } else {
  92. while($info = mysql_fetch_array($result)){
  93. if($flag == '0') {
  94. $flag = '1';
  95. print "<table class=\"company\"><tr>";
  96. print "<td><b>Com Lic Number</b></td>";
  97. print "<td><b>Name</b></td>";
  98. print "<td><b>Address</b></td>";
  99. print "<td><b>City</b></td>";
  100. print "<td><b>Postal Code</b></td>";
  101. print "<td><b>Province</b></td>";
  102. print "<td><b>Phone</b></td>";
  103. print "<td><b>Manager</b></td>";
  104. print "<td><b>Policy</b></td>";
  105. print "</tr>";
  106. }
  107. Print "<tr>";
  108. if (($info['Commercial_License_No']!=null) && ($info['Commercial_License_No']!=0)){
  109. print "<td><a href='company.php?action=update&company=".$info['Commercial_License_No']."'>".$info['Commercial_License_No']."</a>\n</td>";
  110. } else {
  111. print "<td>".$info['Commercial_License_No']."</td>";
  112. }
  113. print "<td>".$info['CName']."</td>";
  114. print "<td>".$info['Address']."</td>";
  115. print "<td>".$info['City']."</td>";
  116. print "<td>".$info['PostalCode']."</td>";
  117. print "<td>".$info['Province']."</td>";
  118. print "<td>".$info['Phone']."</td>";
  119. print "<td>".$info['Manager']."</td>";
  120. print "<td><a href='policy.php?action=update&policy=".$info['Policy_No']."&type=0'>".$info['Policy_No']."</a>\n</td>";
  121. $this->printOptions($info['Commercial_License_No']);
  122. print "</tr>";
  123. }
  124. print "</table>";
  125. }
  126. if($flag == '0') {
  127. print "No results were found!<br />";
  128. }
  129. }
  130. function listCompanies($offset,$limit){
  131. $type = 1; // PRIVATE
  132. $sql = "SELECT * FROM `Company` ORDER BY Commercial_License_No ASC LIMIT $offset, $limit";
  133. $result = mysql_query($sql);
  134. print "<table class=\"company\"><tr>";
  135. print "<td><b>Com Lic Number</b></td>";
  136. print "<td><b>Name</b></td>";
  137. print "<td><b>Address</b></td>";
  138. print "<td><b>City</b></td>";
  139. print "<td><b>Postal Code</b></td>";
  140. print "<td><b>Province</b></td>";
  141. print "<td><b>Phone</b></td>";
  142. print "<td><b>Manager</b></td>";
  143. print "<td><b>Policy</b></td>";
  144. print "</tr>";
  145. while($info = mysql_fetch_array($result)){
  146. Print "<tr>";
  147. if (($info['Commercial_License_No']!=null) && ($info['Commercial_License_No']!=0)){
  148. print "<td><a href='company.php?action=update&company=".$info['Commercial_License_No']."'>".$info['Commercial_License_No']."</a>\n</td>";
  149. } else {
  150. print "<td>".$info['Commercial_License_No']."</td>";
  151. }
  152. print "<td>".$info['CName']."</td>";
  153. print "<td>".$info['Address']."</td>";
  154. print "<td>".$info['City']."</td>";
  155. print "<td>".$info['PostalCode']."</td>";
  156. print "<td>".$info['Province']."</td>";
  157. print "<td>".$info['Phone']."</td>";
  158. print "<td>".$info['Manager']."</td>";
  159. print "<td><a href='policy.php?action=update&policy=".$info['Policy_No']."&type=0'>".$info['Policy_No']."</a>\n</td>";
  160. $this->printOptions($info['Commercial_License_No']);
  161. print "</tr>";
  162. }
  163. print "</table>";
  164. }
  165. function printOptions($companyno){
  166. print "<td><a href=\"company.php?action=remove&company=$companyno\">X</a></td><td> <a href=\"company.php?action=update&company=$companyno\">Edit</a></td>\n";
  167. }
  168. function printUpdateForm($companyno){
  169. $sql = "SELECT * FROM `Company` WHERE Commercial_License_No='$companyno'";
  170. $result = mysql_query($sql) or die(mysql_error());
  171. $info = mysql_fetch_array($result,MYSQL_ASSOC);
  172. include 'includes/editcompany.php';
  173. }
  174. function updateCompany($companyno, $array){
  175. if($this->companyExists($companyno) == false) {
  176. print("Error, Company doesn't exist!");
  177. return false;
  178. }
  179. $sql="UPDATE `Company` SET ";
  180. $keys = array_keys($array); //Return the keys of the array, use first element;
  181. for ($i=0; $i<count($keys); $i++){
  182. if ($i==(count($keys)-1)){ //last value, omit the comma
  183. $sql = $sql.$keys[$i]."='".$array[$keys[$i]]."'";
  184. } else {
  185. $sql = $sql.$keys[$i]."='".$array[$keys[$i]]."', ";
  186. }
  187. }
  188. $sql = $sql." WHERE Commercial_License_No='$companyno'";
  189. print $sql."<br />\n";
  190. mysql_query($sql) or die(mysql_error());
  191. $newpolicyno = $array['Policy_No'];
  192. Client::updateCompanyPolicy($companyno, $newpolicyno);
  193. return true;//return true
  194. }
  195. function companyExists($companyno){
  196. $sql = "SELECT * FROM `Company` WHERE Commercial_License_No='$companyno'";
  197. $result = mysql_query($sql);
  198. // Mysql_num_row is counting table row
  199. $count = mysql_num_rows($result);
  200. if ($count == 1){//Client exists
  201. return true;
  202. }//ID should be unique, therefore only one can be returned
  203. return false;
  204. }
  205. function totalCompanies(){
  206. $temp = mysql_query("SELECT * FROM Company") or die(mysql_error());
  207. return mysql_num_rows($temp);
  208. }
  209. /**
  210. * Validate form data, returns true if all data meets the criteria
  211. * $array = claim
  212. * $array2 = Thirdpartyinfo
  213. * $array3 = Claims
  214. */
  215. function validateData($array){
  216. $errorFlag=true;
  217. if (trim($array['Commercial_License_No'])==""){
  218. $this->appendErrorMsg("Commercial License Number required");
  219. $errorFlag = false;
  220. }
  221. if (trim($array['CName'])==''){
  222. $this->appendErrorMsg("Company Name is required");
  223. $errorFlag = false;
  224. }
  225. if (trim($array['Manager'])==''){
  226. $this->appendErrorMsg("Manager name is required");
  227. $errorFlag = false;
  228. }
  229. if (trim($array['PostalCode'])==''){
  230. $this->appendErrorMsg("Postal Code is required");
  231. $errorFlag = false;
  232. } else if (!preg_match("/^[A-Za-z]{1}\d{1}[A-Za-z]{1}\d{1}[A-Za-z]{1}\d{1}$/",$array['PostalCode'])){
  233. $this->appendErrorMsg("Postal Code should be in the format A1B 2C3");
  234. $errorFlag = false;
  235. }
  236. if (trim($array['Phone'])==''){
  237. $this->appendErrorMsg("Phone number is required");
  238. $errorFlag = false;
  239. } else if (!preg_match("/^[0-9]{10}$/",$array['Phone'])){
  240. $this->appendErrorMsg("Phone number must be in the format xxx-xxx-xxxx or xxxxxxxxx");
  241. $errorFlag = false;
  242. }
  243. return $errorFlag;
  244. }
  245. /**
  246. * Append a message to display if validation fails
  247. * @param unknown_type $string
  248. */
  249. function appendErrorMsg($string){
  250. $this->error[$this->errorIndex] = $string;
  251. $this->errorIndex++;
  252. }
  253. /**
  254. * Display validation error messages
  255. */
  256. function displayError(){
  257. print "<div class=\"validationerror\">";
  258. for ($i=0;$i<count($this->error);$i++){
  259. println($this->error[$i]);
  260. }
  261. print "</div>";
  262. }
  263. } //CLOSE policy class
  264. ?>