PageRenderTime 21ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/LANE/connect.php

https://github.com/joelbrock/is4c_nofc
PHP | 447 lines | 303 code | 87 blank | 57 comment | 85 complexity | c330e97939a6a39b5ef22642da8bfe57 MD5 | raw file
  1. <?php
  2. /*******************************************************************************
  3. Copyright 2001, 2004 Wedge Community Co-op
  4. This file is part of IS4C.
  5. IS4C is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. IS4C is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. in the file license.txt along with IS4C; if not, write to the Free Software
  15. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. *********************************************************************************/
  17. if (!function_exists("setglobalflags")) include("loadconfig.php");
  18. if (!function_exists("pinghost")) include("lib.php");
  19. if (!function_exists("wmdiscount")) include("prehkeys.php");
  20. /***********************************************************************************************
  21. Functions transcribed from connect.asp on 07.13.03 by Brandon.
  22. ***********************************************************************************************/
  23. function tDataConnect()
  24. {
  25. $connection = sql_connect("127.0.0.1", $_SESSION["localUser"], $_SESSION["localPass"]);
  26. $dbID = sql_select_db($_SESSION["tDatabase"], $connection);
  27. return $connection;
  28. }
  29. function pDataConnect()
  30. {
  31. $connection = sql_connect("127.0.0.1", $_SESSION["localUser"], $_SESSION["localPass"]);
  32. sql_select_db($_SESSION["pDatabase"], $connection);
  33. return $connection;
  34. }
  35. function mDataConnect()
  36. {
  37. $_SESSION["standalone"] = 1;
  38. if ($_SESSION["remoteDBMS"] == "mssql") {
  39. if ($connection = mssql_connect($_SESSION["mServer"], $_SESSION["mUser"], $_SESSION["mPass"])) {
  40. if ($connection) {
  41. if (mssql_select_db($_SESSION["mDatabase"], $connection)) {
  42. $_SESSION["standalone"] = 0;
  43. }
  44. }
  45. }
  46. }
  47. else {
  48. if ($connection = mysql_connect($_SESSION["mServer"], $_SESSION["mUser"], $_SESSION["mPass"])) {
  49. if ($connection) {
  50. if (mysql_select_db($_SESSION["mDatabase"], $connection)) {
  51. $_SESSION["standalone"] = 0;
  52. }
  53. }
  54. }
  55. }
  56. if ($_SESSION["standalone"] == 0) {
  57. return $connection;
  58. }
  59. else {
  60. return 0;
  61. }
  62. }
  63. function cDataConnect()
  64. {
  65. $connection = mysql_connect($_SESSION["ccServer"], $_SESSION["ccUser"], $_SESSION["ccPass"]);
  66. mysql_select_db($_SESSION["ccDatabase"], $connection);
  67. return $connection;
  68. }
  69. // ----------getsubtotals()----------
  70. // getsubtotals() updates the values held in our session variables.
  71. function getsubtotals() {
  72. $query = "SELECT * FROM subtotals";
  73. $connection = tDataConnect();
  74. $result = sql_query($query, $connection);
  75. $row = sql_fetch_array($result);
  76. if (!$row || $row["LastID"] == 0) {
  77. $_SESSION["LastID"] = 0;
  78. $_SESSION["runningTotal"] = 0;
  79. $_SESSION["subtotal"] = 0;
  80. $_SESSION["taxTotal"] = 0;
  81. $_SESSION["discounttotal"] = 0;
  82. $_SESSION["tenderTotal"] = 0;
  83. $_SESSION["memSpecial"] = 0;
  84. $_SESSION["staffSpecial"] = 0;
  85. $_SESSION["percentDiscount"] = 0;
  86. $_SESSION["transDiscount"] = 0;
  87. $_SESSION["fsTaxExempt"] = 0;
  88. $_SESSION["fsEligible"] = 0;
  89. $_SESSION["ttlflag"] = 0;
  90. $_SESSION["fntlflag"] = 0;
  91. $_SESSION["memCouponTTL"] = 0;
  92. $_SESSION["refundTotal"] = 0;
  93. $_SESSION["chargeTotal"] = 0;
  94. $_SESSION["ccTotal"] = 0;
  95. $_SESSION["memChargeTotal"] = 0;
  96. $_SESSION["madCoup"] = 0;
  97. $_SESSION["scTaxTotal"] = 0;
  98. $_SESSION["scDiscount"] = 0;
  99. $_SESSION["paymentTotal"] = 0;
  100. $_SESSION['patronageTotal'] = 0;
  101. $_SESSION['NBdisc'] = 0;
  102. $_SESSION['memID'] = 0;
  103. setglobalflags(0);
  104. }
  105. else {
  106. $_SESSION["LastID"] = (double) $row["LastID"];
  107. $_SESSION["memberID"] = trim($row["card_no"]);
  108. $_SESSION["runningTotal"] = (double) $row["runningTotal"];
  109. $_SESSION["taxTotal"] = (double) $row["taxTotal"];
  110. $_SESSION["discounttotal"] = (double) $row["discountTTL"];
  111. // $_SESSION["madCoup"]= (double) $row["madCoupon"];
  112. $_SESSION["memSpecial"] = (double) $row["memSpecial"];
  113. $_SESSION["staffSpecial"] = (double) $row["staffSpecial"];
  114. $_SESSION["tenderTotal"] = (double) $row["tenderTotal"];
  115. $_SESSION["percentDiscount"] = (double) $row["percentDiscount"];
  116. $_SESSION["transDiscount"] = (double) $row["transDiscount"];
  117. $_SESSION["scDiscount"] = (double) $row["scDiscount"];
  118. $_SESSION["scTaxTotal"] = (double) $row["scTaxTotal"];
  119. $_SESSION["fsTaxExempt"] = (double) $row["fsTaxExempt"];
  120. $_SESSION["fsEligible"] = (double) $row["fsEligible"];
  121. $_SESSION["memCouponTTL"] = -1 * ((double) $row["couponTotal"]) + ((double) $row["memCoupon"]);
  122. $_SESSION["refundTotal"] = (double) $row["refundTotal"];
  123. $_SESSION["chargeTotal"] = (double) $row["chargeTotal"];
  124. $_SESSION["ccTotal"] = (double) $row["ccTotal"];
  125. $_SESSION["paymentTotal"] = (double) $row["paymentTotal"];
  126. $_SESSION["memChargeTotal"] = (double) $row["memChargeTotal"];
  127. $_SESSION["discountableTotal"] = (double) $row["discountableTotal"];
  128. $_SESSION["patronageTotal"] = (double) $row["discountableTotal"];
  129. }
  130. if (isset($_SESSION["memberID"]) && $_SESSION["runningTotal"] > 0) {
  131. if ($_SESSION["SSI"] != 0 && ($_SESSION["isStaff"] == 3 || $_SESSION["isStaff"] == 6)) wmdiscount();
  132. }
  133. if ($_SESSION["sc"] == 1) {
  134. $_SESSION["taxTotal"] = $_SESSION["scTaxTotal"];
  135. }
  136. if ( $_SESSION["TaxExempt"] == 1 ) {
  137. $_SESSION["taxable"] = 0;
  138. $_SESSION["taxTotal"] = 0;
  139. $_SESSION["fsTaxable"] = 0;
  140. $_SESSION["fsTaxExempt"] = 0;
  141. }
  142. $_SESSION["subtotal"] = number_format($_SESSION["runningTotal"] - $_SESSION["transDiscount"], 2);
  143. $_SESSION["amtdue"] = $_SESSION["runningTotal"] - $_SESSION["transDiscount"] + $_SESSION["taxTotal"];
  144. if ( $_SESSION["fsEligible"] > $_SESSION["subtotal"] ) {
  145. $_SESSION["fsEligible"] = $_SESSION["subtotal"];
  146. }
  147. sql_close($connection);
  148. }
  149. // ----------gettransno($CashierNo /int)----------
  150. //
  151. // Given $CashierNo, gettransno() will look up the number of the most recent transaction.
  152. function gettransno($CashierNo) {
  153. $database = $_SESSION["tDatabase"];
  154. $register_no = $_SESSION["laneno"];
  155. $query = "SELECT max(trans_no) as maxtransno from localtranstoday where emp_no = '"
  156. .$CashierNo."' and register_no = '"
  157. .$register_no."' GROUP by register_no, emp_no";
  158. $connection = tDataConnect();
  159. $result = sql_query($query, $connection);
  160. $row = sql_fetch_array($result);
  161. if (!$row || !$row["maxtransno"]) {
  162. $trans_no = 1;
  163. }
  164. else {
  165. $trans_no = $row["maxtransno"] + 1;
  166. }
  167. sql_close($connection);
  168. return $trans_no;
  169. }
  170. // ------------------------------------------------------------------
  171. function testremote() {
  172. /*
  173. if ($_SESSION["remoteDBMS"] == "mysql") {
  174. if (file_exists("/pos/is4c/tmp/mysql.sock")) $intConnected = 1;
  175. else $intConnected = 0;
  176. }
  177. else {
  178. */
  179. $intConnected = pinghost($_SESSION["mServer"]);
  180. // }
  181. if ($intConnected == 1) {
  182. uploadtoServer();
  183. }
  184. else {
  185. $_SESSION["standalone"] = 1;
  186. }
  187. return ($_SESSION["standalone"] + 1) % 2;
  188. }
  189. // ------------------------------------------------------------------
  190. function testcc() {
  191. $ccConnected = pinghost($_SESSION["ccServer"]);
  192. if ($ccConnected == 1) {
  193. $cn = mysql_connect($_SESSION["ccServer"],'sa');
  194. //mysql_select_db('is4cc',$cn);
  195. //$query1 = "SELECT * FROM globalvalues";
  196. //$result1 = mysql_query($query1);
  197. if($cn){
  198. $_SESSION["ccMysql"]=1;
  199. }else{
  200. $_SESSION["ccMysql"]=0;
  201. }
  202. }else{
  203. $_SESSION["ccMysql"]=0;
  204. }
  205. }
  206. // ------------------------------------------------------------------
  207. function uploadtoServer()
  208. {
  209. $uploaded = 0;
  210. if ($_SESSION["DBMS"] == "mssql") {
  211. $strUploadDTrans = "insert ".trim($_SESSION["mServer"]).".".trim($_SESSION["mDatabase"]).".dbo.dtransactions select * from dtransactions";
  212. $strUploadAlog = "insert ".trim($_SESSION["mServer"]).".".trim($_SESSION["mDatabase"]).".dbo.alog select * from alog";
  213. $strUploadsuspended = "insert ".trim($_SESSION["mServer"]).".".trim($_SESSION["mDatabase"]).".dbo.suspended select * from suspended";
  214. $connect = tDataConnect();
  215. if ( sql_query($strUploadDTrans, $connect) ) {
  216. sql_query("truncate table dtransactions", $connect);
  217. sql_query($strUploadAlog, $connect);
  218. sql_query("truncate table alog", $connect);
  219. sql_query($strUploadsuspended, $connect);
  220. sql_query("truncate table suspended", $connect);
  221. $uploaded = 1;
  222. $_SESSION["standalone"] = 0;
  223. } else {
  224. $uploaded = 0;
  225. $_SESSION["standalone"] = 1;
  226. }
  227. }
  228. else {
  229. $uploaded = uploadtable("dtransactions");
  230. if ($uploaded == 1) {
  231. uploadtable("suspended");
  232. uploadtable("alog");
  233. $_SESSION["standalone"] = 0;
  234. } else {
  235. $uploaded = 0;
  236. $_SESSION["standalone"] = 1;
  237. }
  238. }
  239. return $uploaded;
  240. }
  241. function uploadtable($table) {
  242. $output = "";
  243. // openlog("is4c_connect", LOG_PID | LOG_PERROR, LOG_LOCAL0);
  244. if ($_SESSION["localPass"] == "") {
  245. $localpass = "";
  246. }
  247. else {
  248. $localpass = "-p".$_SESSION["localPass"];
  249. }
  250. if ($_SESSION["mPass"] == "") {
  251. $serverpass = "";
  252. }
  253. else {
  254. $serverpass = "-p".$_SESSION["mPass"];
  255. }
  256. $upload = "mysqldump -u ".$_SESSION['localUser']." ".$localpass." -t ".$_SESSION['tDatabase']." ".$table
  257. ." | mysql -h ".$_SESSION['mServer']." -u ".$_SESSION["mUser"]." ".$serverpass." ".$_SESSION['mDatabase']." 2>&1";
  258. // echo $upload;
  259. exec($upload, $aResult);
  260. $error = 0;
  261. $output = 0;
  262. foreach ($aResult as $errormsg) {
  263. if ($errormsg && strlen($errormsg) > 0) {
  264. $output = $output."\n".$errormsg;
  265. $error = 1;
  266. }
  267. }
  268. if ($error == 1) {
  269. syslog(LOG_WARNING, "uploadtable($table) failed; rc: errormsg: '$output'");
  270. return 0;
  271. } else {
  272. $t_conn = tDataConnect();
  273. mysql_query("TRUNCATE TABLE ".$table, $t_conn);
  274. return 1;
  275. }
  276. }
  277. /* acg
  278. function wmupdate($memID,$hours) {
  279. exec('mysql -u root -h '.$_SESSION['mServer'].' -e "update is4c_op.custdata set SSI = (SSI - '.$hours.') where id = '.$memID.';"');
  280. exec('mysqldump -u root -h '.$_SESSION['mServer'].' is4c_op custdata | mysql -u root '.$_SESSION['pDatabase']);
  281. }
  282. */
  283. // ------------------------ Data Abstraction ----------------------------
  284. function sql_connect($server, $user, $pass) {
  285. if ($_SESSION["DBMS"] == "mssql") {
  286. $connection = mssql_connect($server, $user, $pass);
  287. } else {
  288. $connection = mysql_connect($server, $user, $pass) or die(mysql_error());
  289. }
  290. return $connection;
  291. }
  292. function sql_select_db($db, $connection) {
  293. if ($_SESSION["DBMS"] == "mssql") {
  294. $selectdb = mssql_select_db($db, $connection);
  295. } else {
  296. $selectdb = mysql_select_db($db, $connection) or die(mysql_error());
  297. }
  298. return $selectdb;
  299. }
  300. function sql_query($query, $connection)
  301. {
  302. if ($_SESSION["DBMS"] == "mssql") {
  303. $result = mssql_query($query, $connection);
  304. } else {
  305. $result = mysql_query($query, $connection) or die(mysql_error());
  306. }
  307. return $result;
  308. }
  309. function sql_num_rows($result) {
  310. if ($_SESSION["DBMS"] == "mssql") {
  311. $num_rows = mssql_num_rows($result);
  312. } else {
  313. $num_rows = mysql_num_rows($result);
  314. }
  315. if (!$num_rows) $num_rows = 0;
  316. return $num_rows;
  317. }
  318. function sql_fetch_array($result) {
  319. if ($_SESSION["DBMS"] == "mssql") {
  320. $row = mssql_fetch_array($result);
  321. } else {
  322. $row = mysql_fetch_array($result);
  323. }
  324. return $row;
  325. }
  326. function sql_fetch_row($result) {
  327. if ($_SESSION["DBMS"] == "mssql") {
  328. $row = mssql_fetch_row($result);
  329. } else {
  330. $row = mysql_fetch_row($result);
  331. }
  332. return $row;
  333. }
  334. function sql_close($connection) {
  335. if ($_SESSION["DBMS"] == "mssql") {
  336. $close = mssql_close($connection);
  337. } else {
  338. $close = "";
  339. // $close = mysql_close($connection) or die(mysql_error());
  340. }
  341. return $close;
  342. }
  343. function sql_error($connection) {
  344. if ($_SESSION["DBMS"] == "mssql") {
  345. $error = "";
  346. } else {
  347. $error = mysql_error($connection);
  348. }
  349. return $error;
  350. }
  351. ?>