PageRenderTime 61ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/lib/lib.InvoiceSystem.php

https://github.com/toztech/php-invoice
PHP | 839 lines | 477 code | 293 blank | 69 comment | 94 complexity | 84951a455bff50bd21754a791157a247 MD5 | raw file
  1. <?
  2. /***************************************************************************
  3. InvoiceSystemLib.php
  4. ------------
  5. product : PHP Invoice
  6. version : 1.0 build 1 (Beta)
  7. released : Sunday September 7 2003
  8. copyright : Copyright &copy; 2001-2009 Jeremy Hubert
  9. email : support@illanti.com
  10. website : http://www.illanti.com
  11. The core of the invoice system.
  12. DO NOT EDIT unless you know what you are doing.
  13. ***************************************************************************/
  14. /*
  15. # -------- IGNORE EVERYTHING BELOW THIS LINE --------
  16. $call_home_to="secure.illanti.com";
  17. $installed_directory="/licence";
  18. $querystring="iaccess_ip=".base64_encode($_SERVER['SERVER_ADDR']);
  19. $querystring.="&iaccess_host=".base64_encode($_SERVER['SERVER_NAME']);
  20. $querystring.="&product_key=".urlencode("10f6f8c0f3fad50a21f4c6fc3a224337");
  21. $querystring.="&license=".urlencode($license)."\n\n";
  22. $fp=@fsockopen($call_home_to, 80, $errno, $errstr, 5);
  23. if (!$fp) { return 1; }
  24. else
  25. {
  26. $header="POST ".$installed_directory."/validate_internal.php HTTP/1.1\n";
  27. $header.="Host: ".$call_home_to."\n";
  28. $header.="Content-Length: ".strlen($querystring)."\n";
  29. $header.="Content-Type: application/x-www-form-urlencoded\n";
  30. $header.="Connection: Close\n\n";
  31. $header.=$querystring;
  32. fputs ($fp, $header);
  33. while (!feof($fp)) { $data.=@fgets($fp, 1024); }
  34. fclose ($fp);
  35. list($a, $b, $c)=explode("{{", $data);
  36. function parse_template($template)
  37. {
  38. $buffer="";
  39. $fh=file($template);
  40. foreach($fh as $line) { $buffer.=$line; }
  41. return $buffer;
  42. }
  43. $path_home="http://".$call_home_to.$installed_directory."/inc";
  44. if ($b==69)
  45. {
  46. echo parse_template($path_home."/license_invalid.tpl");
  47. exit;
  48. }
  49. else if ($b==2)
  50. {
  51. echo parse_template($path_home."/license_suspended.tpl");
  52. exit;
  53. }
  54. else if ($b==3)
  55. {
  56. echo parse_template($path_home."/license_expired.tpl");
  57. exit;
  58. }
  59. }
  60. */
  61. class InvoiceSystem {
  62. var $db;
  63. var $debug = false;
  64. var $dbprefix;
  65. var $adminid;
  66. var $rpp; // results per page
  67. var $def_rpp; // default results per page
  68. var $page; // current result page
  69. function InvoiceSystem(&$db,$adminid, $dbprefix,$rpp,$debug=false) {
  70. if(!$db) {
  71. die("Invalid db object passed to InvoiceSystem constructor");
  72. }
  73. $this->db = $db;
  74. $this->debug = $debug;
  75. $this->dbprefix = $dbprefix;
  76. $this->adminid = $adminid;
  77. $this->rpp = $rpp;
  78. $this->def_rpp = $rpp;
  79. }
  80. function setDebug($debug) {
  81. $this->debug = $debug;
  82. return true;
  83. }
  84. function setRPP($rpp=0) {
  85. if ($rpp) {
  86. $this->rpp = $rpp;
  87. } else {
  88. $this->rpp = $this->def_rpp;
  89. }
  90. return true;
  91. }
  92. function setPage($page) {
  93. $this->page = $page;
  94. return true;
  95. }
  96. function SqlError($query, $result)
  97. {
  98. trigger_error("MYSQL error: ".$result->getMessage().
  99. " in query:<br />".$query."<br />",E_USER_ERROR);
  100. }
  101. function doLogin($username,$password) {
  102. $query = "SELECT `clientid` , `parentclientid`, `username` , `firstname`, `email` , `ref` , `company` , `access`, `template`, `language` FROM " . $this->dbprefix . "client WHERE username = '" . $username . "' AND passwd = '" . $password . "'";
  103. $retVal = $this->db->getRow($query,null,DB_FETCHMODE_ASSOC);
  104. if(DB::isError($retVal)) $this->SqlError($query,$retVal);
  105. return $retVal;
  106. }
  107. function recoverPassword($method,$value) {
  108. $query = "SELECT `clientid` , `username` , `firstname`, `email` , `passwd` FROM " . $this->dbprefix . "client WHERE " . $method . " = '" . $value . "'";
  109. $retVal = $this->db->getRow($query,null,DB_FETCHMODE_ASSOC);
  110. if(DB::isError($retVal)) $this->SqlError($query,$retVal);
  111. return $retVal;
  112. }
  113. function FetchInvoices($page=0,$param='invoice_num',$where='1') {
  114. if (strtolower($param) == 'clientid') $param = 'i.' . $param;
  115. $query = "SELECT c.clientid,company,i.invoiceid, invoice_num,UNIX_TIMESTAMP(issue_date) as issue_date,UNIX_TIMESTAMP(due_date) as due_date,SUM(ii.cost*iii.qty) as cost , (SUM(ii.cost*iii.qty)+(shipping))*(tax/100) as tax, (SUM(ii.cost*iii.qty)+(shipping))*(tax2/100) as tax2, shipping , (IFNULL(SUM(ii.cost*iii.qty),0)+(shipping))*(1+(tax/100))*(1+(tax2/100)) as total,curr_status FROM " . $this->dbprefix . "invoice i INNER JOIN " . $this->dbprefix . "client c ON i.clientid = c.clientid LEFT JOIN " . $this->dbprefix . "invoice_invoiceitem as iii ON i.invoiceid = iii.invoiceid LEFT JOIN " . $this->dbprefix . "invoiceitem as ii ON ii.invoiceitemid = iii.invoiceitemid WHERE i.visible = '1' AND parentclientid = " . $this->adminid . " AND $where GROUP BY iii.invoiceid, i.invoiceid ORDER BY $param";
  116. $retVal = $this->db->getAll($query,null,DB_FETCHMODE_ASSOC);
  117. if(DB::isError($retVal)) $this->SqlError($query,$retVal);
  118. return $retVal;
  119. }
  120. function FetchInvoicesByClient($clientID,$param='invoice_num',$where='1') {
  121. $query = "SELECT i.invoiceid, invoice_num,UNIX_TIMESTAMP(issue_date) as issue_date,UNIX_TIMESTAMP(due_date) as due_date,SUM(ii.cost*iii.qty) as cost , (SUM(ii.cost*iii.qty)+(shipping))*(tax/100) as tax, (SUM(ii.cost*iii.qty)+(shipping))*(tax2/100) as tax2, shipping , (SUM(ii.cost*iii.qty)+(shipping))*(1+(tax/100))*(1+(tax2/100)) as total,curr_status FROM " . $this->dbprefix . "invoice i LEFT JOIN " . $this->dbprefix . "invoice_invoiceitem as iii USING ( invoiceid ) LEFT JOIN " . $this->dbprefix . "invoiceitem as ii USING ( invoiceitemid ) WHERE visible = '1' AND clientid = '" . $clientID . "' AND curr_status <> 'unsent' AND $where GROUP BY iii.invoiceid ORDER BY $param";
  122. $retVal = $this->db->getAll($query,null,DB_FETCHMODE_ASSOC);
  123. if(DB::isError($retVal)) $this->SqlError($query,$retVal);
  124. return $retVal;
  125. }
  126. function FetchInvoiceList($clientID=0) {
  127. if (!$clientID) {
  128. $where = '1';
  129. } else {
  130. $where = 'clientid = ' . $clientID;
  131. }
  132. $query = "SELECT invoiceid,invoice_num FROM " . $this->dbprefix . "invoice WHERE $where ORDER BY invoice_num";
  133. $retVal = $this->db->getAssoc($query,false,null,DB_FETCHMODE_ASSOC);
  134. if(DB::isError($retVal)) $this->SqlError($query,$retVal);
  135. return $retVal;
  136. }
  137. function FetchInvoiceClient($invoiceID) {
  138. $query = "SELECT clientID FROM " . $this->dbprefix . "invoice WHERE invoiceid = $invoiceID";
  139. $retVal = $this->db->getOne($query,null,DB_FETCHMODE_ASSOC);
  140. if(DB::isError($retVal)) $this->SqlError($query,$retVal);
  141. return $retVal;
  142. }
  143. function FetchInvoiceNum($invoiceID) {
  144. $query = "SELECT invoice_num FROM " . $this->dbprefix . "invoice WHERE invoiceid = $invoiceID";
  145. $retVal = $this->db->getOne($query,null,DB_FETCHMODE_ASSOC);
  146. if(DB::isError($retVal)) $this->SqlError($query,$retVal);
  147. return $retVal;
  148. }
  149. function FetchClientCompany($clientID) {
  150. $query = "SELECT company FROM " . $this->dbprefix . "client WHERE clientid = $clientID";
  151. $retVal = $this->db->getOne($query,null,DB_FETCHMODE_ASSOC);
  152. if(DB::isError($retVal)) $this->SqlError($query,$retVal);
  153. return $retVal;
  154. }
  155. function FetchClients($access='client',$display='active') {
  156. switch($display) {
  157. case 'all':
  158. $where = '1';
  159. break;
  160. case 'inactive':
  161. $where = "c.visible = '0'";
  162. break;
  163. default:
  164. $where = "c.visible = '1'";
  165. break;
  166. }
  167. $query = "SELECT c.`clientid` , `parentclientid` , `username` , `email` , `ref` , `company` , `firstname` , `lastname` , `contacttitle` , `phonenumber` , `faxnumber` , `term_days` , `def_terms` , `def_comments` , `account_num` , `template` , `language` , `access` , c.`visible`,count(invoiceID) as invoicecount FROM `" . $this->dbprefix . "client` as c LEFT JOIN `" . $this->dbprefix . "invoice` as i USING (clientid) WHERE $where AND access = '$access' AND parentclientid = " . $this->adminid . " GROUP BY c.clientID ORDER BY c.company";
  168. $retVal = $this->db->getAll($query,null,DB_FETCHMODE_ASSOC);
  169. if(DB::isError($retVal)) $this->SqlError($query,$retVal);
  170. return $retVal;
  171. }
  172. function FetchClientList() {
  173. $query = "SELECT clientid,company FROM " . $this->dbprefix . "client WHERE access = 'client' AND parentclientid = " . $this->adminid . " ORDER BY company";
  174. $retVal = $this->db->getAssoc($query,false,null,DB_FETCHMODE_ASSOC);
  175. if(DB::isError($retVal)) $this->SqlError($query,$retVal);
  176. return $retVal;
  177. }
  178. function FindNewInvoiceNumber($cid=0,$inum=0) {
  179. if (!$inum)
  180. $inum = $this->db->nextid($this->dbprefix . 'invoice_num');
  181. $query = "SELECT invoiceid,invoice_num FROM " . $this->dbprefix . "invoice INNER JOIN " . $this->dbprefix . "client USING ( clientid ) WHERE parentclientid=" . $this->adminid . " AND invoice_num='" . $inum . "'";
  182. $retVal = $this->db->getRow($query);
  183. if(DB::isError($retVal)) $this->SqlError($query,$retVal);
  184. if (count($retVal)) {
  185. if ($retVal[0]==$cid)
  186. return $inum;
  187. else
  188. return $this->FindNewInvoiceNumber($cid);
  189. } else {
  190. return $inum;
  191. }
  192. }
  193. function FetchClientOwner($clientID) {
  194. $query = "SELECT parentclientid FROM " . $this->dbprefix . "client WHERE clientid=" . $clientID;
  195. $retVal = $this->db->getOne($query);
  196. if(DB::isError($retVal)) $this->SqlError($query,$retVal);
  197. return $retVal;
  198. }
  199. function FetchClientDetails($clientID) {
  200. $query = "SELECT * FROM " . $this->dbprefix . "client WHERE clientid=" . $clientID;
  201. $retVal = $this->db->getRow($query,null,DB_FETCHMODE_ASSOC);
  202. if(DB::isError($retVal)) $this->SqlError($query,$retVal);
  203. return $retVal;
  204. }
  205. function FetchAdminDetails($adminID) {
  206. $query = "SELECT company,firstName,lastName,phoneNumber,faxNumber,address,logo,email,def_tax,def_tax2,term_days,def_terms,def_comments FROM " . $this->dbprefix . "client WHERE clientid=" . $adminID;
  207. $retVal = $this->db->getRow($query,null,DB_FETCHMODE_ASSOC);
  208. if(DB::isError($retVal)) $this->SqlError($query,$retVal);
  209. return $retVal;
  210. }
  211. function FetchInvoiceDetails($invoiceID) {
  212. $query = "SELECT i.invoiceid, invoice_num, clientid , UNIX_TIMESTAMP(issue_date) as issue_date,UNIX_TIMESTAMP(due_date) as due_date , comments , SUM(ii.cost*iii.qty) as cost , (SUM(ii.cost*iii.qty)+(shipping))*(tax/100) as calc_tax, (SUM(ii.cost*iii.qty)+(shipping))*(tax2/100) as calc_tax2, shipping, (IFNULL(SUM(ii.cost*iii.qty),0)+(shipping))*(1+(tax/100))*(1+(tax2/100)) as total, tax, tax2, curr_status, comments, terms FROM " . $this->dbprefix . "invoice i LEFT JOIN " . $this->dbprefix . "invoice_invoiceitem as iii USING ( invoiceid ) LEFT JOIN " . $this->dbprefix . "invoiceitem as ii USING ( invoiceitemid ) WHERE i.invoiceid = " . $invoiceID . " GROUP BY iii.invoiceid";
  213. $retVal = $this->db->getRow($query,null,DB_FETCHMODE_ASSOC);
  214. if(DB::isError($retVal)) $this->SqlError($query,$retVal);
  215. if (is_array($retVal))
  216. $retVal['items'] = $this->Fetchinvoiceitems($invoiceID);
  217. return $retVal;
  218. }
  219. function Fetchinvoiceitems($invoiceID) {
  220. $query = "SELECT iii.`invoiceitemid` , iii.`qty`, `details`, `cost` FROM `" . $this->dbprefix . "invoice_invoiceitem` iii INNER JOIN `" . $this->dbprefix . "invoiceitem` ii USING ( invoiceitemid ) WHERE 1 AND iii.`invoiceid` = " . $invoiceID;
  221. $retVal = $this->db->getAll($query,null,DB_FETCHMODE_ASSOC);
  222. if(DB::isError($retVal)) $this->SqlError($query,$retVal);
  223. if (!is_array($retVal)) $retVal = array();
  224. return $retVal;
  225. }
  226. function InsertClient($values) {
  227. if ($values['def_tax'] == '') {
  228. $values['def_tax'] = 'NULL';
  229. }
  230. if ($values['def_tax2'] == '') {
  231. $values['def_tax2'] = 'NULL';
  232. }
  233. if ($values['term_days'] == '') {
  234. $values['term_days'] = 'NULL';
  235. }
  236. $id = $this->db->nextid($this->dbprefix . 'client');
  237. $query = "INSERT INTO " . $this->dbprefix . "client (clientid, parentclientid, username, passwd, address, email, def_tax, def_tax2, ref , `firstName` , `lastName` , `contactTitle` , `company` , `phonenumber` , `faxnumber` , `url` , `logo` , `template` , `language` , `term_days` , `def_terms` , `def_comments` , `account_num` , `access` , `visible`) VALUES
  238. ($id," .
  239. addslashes($values['parentclientid']) . ",'" .
  240. addslashes($values['username']) . "','" .
  241. addslashes($values['passwd']) . "','" .
  242. addslashes($values['address']) . "','" .
  243. addslashes($values['email']) . "'," .
  244. addslashes($values['def_tax']) . "," .
  245. addslashes($values['def_tax2']) . ",'" .
  246. addslashes($values['ref']) . "','" .
  247. addslashes($values['firstname']) . "', '" .
  248. addslashes($values['lastname']) . "', '" .
  249. addslashes($values['contacttitle']) . "', '" .
  250. addslashes($values['company']) . "', '" .
  251. addslashes($values['phonenumber']) . "', '" .
  252. addslashes($values['faxnumber']) . "', '" .
  253. addslashes($values['url']) . "', '" .
  254. addslashes($values['logo']) . "', '" .
  255. addslashes($values['template']) . "', '" .
  256. addslashes($values['language']) . "', " .
  257. addslashes($values['term_days']) . ", '" .
  258. addslashes($values['def_terms']) . "', '" .
  259. addslashes($values['def_comments']) . "', '" .
  260. addslashes($values['account_num']) . "', '" .
  261. addslashes($values['access']) . "', '" . addslashes($values['visible']) . "')";
  262. $retVal = $this->db->query($query);
  263. if(DB::isError($retVal)) $this->SqlError($query,$retVal);
  264. return $id;
  265. }
  266. function UpdateClient($clientID,$values) {
  267. if ($values['def_tax'] == '') {
  268. $values['def_tax'] = 'NULL';
  269. }
  270. if ($values['def_tax2'] == '') {
  271. $values['def_tax2'] = 'NULL';
  272. }
  273. if ($values['term_days'] == '') {
  274. $values['term_days'] = 'NULL';
  275. }
  276. $query = "UPDATE " . $this->dbprefix . "client SET
  277. parentclientid=" . addslashes($values['parentclientid']) . ",
  278. username='" . addslashes($values['username']) . "',
  279. passwd='" . addslashes($values['passwd']) . "',
  280. address='" . addslashes($values['address']) . "',
  281. email='" . addslashes($values['email']) . "',
  282. def_tax=" . addslashes($values['def_tax']) . ",
  283. def_tax2=" . addslashes($values['def_tax2']) . ",
  284. ref='" . addslashes($values['ref']) . "',
  285. firstname='" . addslashes($values['firstname']) . "',
  286. lastname='" . addslashes($values['lastname']) . "',
  287. contacttitle='" . addslashes($values['contacttitle']) . "',
  288. company='" . addslashes($values['company']) . "',
  289. phonenumber='" . addslashes($values['phonenumber']) . "',
  290. faxnumber='" . addslashes($values['faxnumber']) . "',
  291. url='" . addslashes($values['url']) . "',
  292. logo='" . addslashes($values['logo']) . "',
  293. term_days=" . addslashes($values['term_days']) . ",
  294. def_terms='" . addslashes($values['def_terms']) . "',
  295. def_comments='" . addslashes($values['def_comments']) . "',
  296. account_num='" . addslashes($values['account_num']) . "',
  297. template='" . addslashes($values['template']) . "',
  298. language='" . addslashes($values['language']) . "',
  299. access='" . addslashes($values['access']) . "',
  300. visible='" . addslashes($values['visible']) . "'
  301. WHERE clientid=$clientID";
  302. $retVal = $this->db->query($query);
  303. if(DB::isError($retVal)) $this->SqlError($query,$retVal);
  304. return true;
  305. }
  306. function DeleteClient($clientID,$soft=true) {
  307. if ($soft) {
  308. $query = "UPDATE " . $this->dbprefix . "client SET visible = '0' WHERE clientid=" . $clientID;
  309. } else {
  310. $query = "DELETE FROM " . $this->dbprefix . "client WHERE clientid=" . $clientID;
  311. }
  312. $retVal = $this->db->query($query);
  313. if(DB::isError($retVal)) $this->SqlError($query,$retVal);
  314. return true;
  315. }
  316. function InsertInvoice(&$values) {
  317. $id = $this->db->nextid($this->dbprefix . 'invoice');
  318. if (!is_numeric($values['invoice_num']) || !$values['invoice_num'])
  319. $values['invoice_num'] = 0;
  320. $values['invoice_num'] = $this->FindNewInvoiceNumber($id,$values['invoice_num']);
  321. $query = "INSERT INTO " . $this->dbprefix . "invoice (invoiceid, clientid, issue_date, due_date, comments, cost, tax, tax2, shipping, curr_status, terms, invoice_num) VALUES ('$id','" . addslashes($values['clientid']) . "','" . date('Y-m-d h:i:s',strtotime($values['issue_date'])) . "','" . date('Y-m-d h:i:s',strtotime($values['due_date'])) . "','" . addslashes($values['comments']) . "','" . addslashes($values['cost']) . "','" . addslashes($values['tax']) . "','" . addslashes($values['tax2']) . "','" . addslashes($values['shipping']) . "','" . addslashes($values['curr_status']) . "','" . addslashes($values['terms']) . "','" . addslashes($values['invoice_num']) . "')";
  322. $retVal = $this->db->query($query);
  323. if(DB::isError($retVal)) $this->SqlError($query,$retVal);
  324. return $id; //array('id'=>$id,'num'=>$values['invoice_num']);
  325. }
  326. function Insertinvoiceitem($details,$cost) {
  327. $id = $this->db->nextid($this->dbprefix . 'invoiceitem');
  328. $query = "INSERT INTO " . $this->dbprefix . "invoiceitem (invoiceitemid, details, cost) VALUES ('$id','" . addslashes($details) . "','" . addslashes($cost) . "')";
  329. $retVal = $this->db->query($query);
  330. if(DB::isError($retVal)) $this->SqlError($query,$retVal);
  331. return $id;
  332. }
  333. function AddItemToInvoice($invoiceitemid,$invoiceid,$qty) {
  334. $query = "INSERT INTO " . $this->dbprefix . "invoice_invoiceitem (invoiceitemid, invoiceid, qty) VALUES ('$invoiceitemid','$invoiceid','$qty')";
  335. $retVal = $this->db->query($query);
  336. if(DB::isError($retVal)) $this->SqlError($query,$retVal);
  337. return true;
  338. }
  339. function DeleteInvoiceItem($invoiceitemID) {
  340. $query = "DELETE FROM " . $this->dbprefix . "invoiceitem WHERE invoiceitemid = " . $invoiceitemID;
  341. $retVal = $this->db->query($query);
  342. if(DB::isError($retVal)) $this->SqlError($query,$retVal);
  343. return true;
  344. }
  345. function RemoveItemFromInvoice($invoiceitemID,$invoiceID) {
  346. $query = "DELETE FROM " . $this->dbprefix . "invoiceitem WHERE invoiceitemid = " . $invoiceitemID;
  347. $retVal = $this->db->query($query);
  348. if(DB::isError($retVal)) $this->SqlError($query,$retVal);
  349. return true;
  350. }
  351. function DeleteInvoice($invoiceID,$soft=true) {
  352. if ($soft) {
  353. $query = "UPDATE " . $this->dbprefix . "invoice SET curr_status = 'void' WHERE invoiceid = " . $invoiceID;
  354. } else {
  355. $query = "DELETE FROM " . $this->dbprefix . "invoice WHERE invoiceid = " . $invoiceID;
  356. }
  357. $retVal = $this->db->query($query);
  358. if(DB::isError($retVal)) $this->SqlError($query,$retVal);
  359. return true;
  360. }
  361. function UpdateInvoice($invoiceID,$values) {
  362. $values['invoice_num'] = $this->FindNewInvoiceNumber($invoiceID,$values['invoice_num']);
  363. $query = "UPDATE " . $this->dbprefix . "invoice SET invoice_num='" . addslashes($values['invoice_num']) . "',clientid='" . addslashes($values['clientid']) . "',issue_date='" . date('Y-m-d h:i:s',strtotime($values['issue_date'])) . "', due_date='" . date('Y-m-d h:i:s',strtotime($values['due_date'])) . "', comments='" . addslashes($values['comments']) . "',terms='" . addslashes($values['terms']) . "',cost='" . addslashes($values['cost']) . "',tax='" . addslashes($values['tax']) . "',tax2='" . addslashes($values['tax2']) . "',shipping='" . addslashes($values['shipping']) . "',curr_status='" . addslashes(strtolower($values['curr_status'])) . "' WHERE invoiceid=$invoiceID";
  364. $retVal = $this->db->query($query);
  365. if(DB::isError($retVal)) $this->SqlError($query,$retVal);
  366. return true;
  367. }
  368. function UpdateInvoiceStatus($invoiceID,$status) {
  369. $query = "UPDATE " . $this->dbprefix . "invoice SET curr_status='" . addslashes($status) . "' WHERE invoiceid=$invoiceID";
  370. $retVal = $this->db->query($query);
  371. if(DB::isError($retVal)) $this->SqlError($query,$retVal);
  372. return true;
  373. }
  374. function LogItem($creator,$targetID,$targetType,$eventID,$details='') {
  375. $query = "INSERT INTO " . $this->dbprefix . "log (creator,targetid,targettype,eventid,details) VALUES (" . $creator . "," . $targetID . "," . $targetType . "," . $eventID . ",'" . $details . "');";
  376. $retVal = $this->db->query($query);
  377. if(DB::isError($retVal)) $this->SqlError($query,$retVal);
  378. return true;
  379. }
  380. function addEmailSend($clientID, $invoiceID, $emailaddress, $sendType) {
  381. $emailSendID = $this->db->nextid($this->dbprefix . 'emailsend');
  382. if ($invoiceID == 0) {
  383. $invoiceID == "NULL";
  384. }
  385. $query = "INSERT INTO `" . $this->dbprefix . "emailsend` ( `emailsendid` , `clientid` , `invoiceid` , `emailaddress` , `sendtype` , `datesent` , `opencount`) VALUES (" . $emailSendID . ", " . $clientID . ", " . $invoiceID . " , '" . $emailaddress . "' , " . $sendType . ", '" . date('Y-m-d H:i:s') . "', 0)";
  386. $retVal = $this->db->query($query);
  387. if(DB::isError($retVal)) $this->SqlError($query,$retVal);
  388. return $emailSendID;
  389. }
  390. function trackEmailSendOpen($emailSendID) {
  391. /*
  392. connect to the database and process opened issues
  393. */
  394. $sql = "SELECT count(emailsendid) FROM " . $this->dbprefix . "emailsend WHERE opencount > 0 AND emailsendid = " . $emailSendID;
  395. $retVal = $this->db->getOne($sql);
  396. if ($retVal) {
  397. $sql = "UPDATE " . $this->dbprefix . "emailsend SET opencount = opencount + 1, lastopened = '" . date('Y-m-d H:i:s') . "' WHERE emailsendid = " . $emailSendID;
  398. } else {
  399. $sql = "UPDATE " . $this->dbprefix . "emailsend SET opencount = 1, firstopened = '" . date('Y-m-d H:i:s') . "', lastopened = '" . date('Y-m-d H:i:s') . "' WHERE emailSendID = " . $emailSendID;
  400. }
  401. $retVal = $this->db->query($sql);
  402. if(DB::isError($retVal)) $this->SqlError($query,$retVal);
  403. return true;
  404. }
  405. function getEmailSendHistory($clientID=0) {
  406. $where = ($clientID) ? 'AND c.`clientid`=' . $clientID : '';
  407. $sql = "SELECT `emailsendid` , `company`, c.`clientid` , `invoiceid` , `emailaddress` , `sendtype` , UNIX_TIMESTAMP(datesent) as datesent , `opencount` , UNIX_TIMESTAMP(firstopened) as firstopened, UNIX_TIMESTAMP(lastopened) as lastopened FROM " . $this->dbprefix . "emailsend INNER JOIN `" . $this->dbprefix . "client` as c USING ( clientid ) WHERE parentclientid = " . $this->adminid . " $where ORDER BY emailsendid";
  408. $retVal = $this->db->getAll($sql,null,DB_FETCHMODE_ASSOC);
  409. if(DB::isError($retVal)) $this->SqlError($query,$retVal);
  410. return $retVal;
  411. }
  412. function FetchLogItems($date=null) {
  413. $query = "SELECT `logid` , client.`clientid` , `company` , `invoiceid` , `eventid` , UNIX_TIMESTAMP(`occured`) as occured , `details` FROM `" . $this->dbprefix . "log` INNER JOIN `client` USING ( `clientid` ) WHERE parentclientid = " . $this->adminid;
  414. $retVal = $this->db->getAll($query,null,DB_FETCHMODE_ASSOC);
  415. if(DB::isError($retVal)) $this->SqlError($query,$retVal);
  416. return $retVal;
  417. }
  418. function FetchLogItemsByClient($clientID=0,$date=null) {
  419. $query = "SELECT `logid` , `creator` , `company` , `targetid` , `targettype` , `eventid` , UNIX_TIMESTAMP(occured) as occured, `details` FROM `" . $this->dbprefix . "log` INNER JOIN `" . $this->dbprefix . "client` ON clientid = creator WHERE creator=" . $clientID . " AND parentclientid = " . $this->adminid;
  420. $retVal = $this->db->getAll($query,null,DB_FETCHMODE_ASSOC);
  421. if(DB::isError($retVal)) $this->SqlError($query,$retVal);
  422. return $retVal;
  423. }
  424. function FetchLogItems_Login($clientID=0,$date=null) {
  425. $query = "SELECT `logid` , `creator` , `targetid` , `targettype` , `eventid` , UNIX_TIMESTAMP(occured) as occured, `details`, `company` FROM `" . $this->dbprefix . "log` INNER JOIN `" . $this->dbprefix . "client` ON clientid = targetid WHERE eventid IN ( 10,11 ) AND parentclientid = " . $this->adminid;
  426. if ($clientID)
  427. $query .= " AND clientID = " . $clientID;
  428. $query .= " ORDER BY occured";
  429. $retVal = $this->db->getAll($query,null,DB_FETCHMODE_ASSOC);
  430. if(DB::isError($retVal)) $this->SqlError($query,$retVal);
  431. return $retVal;
  432. }
  433. function SearchClients($criteria, $qs, $access='client') {
  434. for ($i=0, $end=sizeof($qs), $subq=''; $i<$end; $i++) {
  435. $subq .= $criteria . " LIKE '%{$qs{$i}}%'";
  436. $subq .= ($i != ($end-1)) ? ' OR ' : '';
  437. }
  438. $query = "SELECT * FROM " . $this->dbprefix . "client WHERE ($subq) AND visible = '1' AND access = '$access' AND parentclientid = " . $this->adminid;
  439. $retVal = $this->db->getAll($query,null,DB_FETCHMODE_ASSOC);
  440. if(DB::isError($retVal)) $this->SqlError($query,$retVal);
  441. return $retVal;
  442. }
  443. function InsertNote($clientID, $content, $private) {
  444. if ($private) {
  445. $private = 'yes';
  446. } else {
  447. $private = 'no';
  448. }
  449. $query = "INSERT INTO " . $this->dbprefix . "note (s_clientid, r_clientid, content, posted, isprivate) VALUES (" . $this->adminid . "," . $clientID . ",'" . addslashes($content) . "',NOW(),'" . $private . "');";
  450. $retVal = $this->db->query($query);
  451. if(DB::isError($retVal)) $this->SqlError($query,$retVal);
  452. return true;
  453. }
  454. function DeleteNote($noteID) {
  455. $query = "DELETE FROM " . $this->dbprefix . "note WHERE noteid = " . $noteID;
  456. $retVal = $this->db->query($query);
  457. if(DB::isError($retVal)) $this->SqlError($query,$retVal);
  458. return true;
  459. }
  460. function GetNotes($clientID=null) {
  461. if (!$clientID) {
  462. $where = '1';
  463. } else {
  464. $where = 'r_clientid = ' . $clientID;
  465. }
  466. $query = "SELECT noteid, r_clientid, content, UNIX_TIMESTAMP(posted) as posted, isprivate FROM " . $this->dbprefix . "note WHERE " . $where . " ORDER BY posted DESC";
  467. $retVal = $this->db->getAll($query,null,DB_FETCHMODE_ASSOC);
  468. if(DB::isError($retVal)) $this->SqlError($query,$retVal);
  469. return $retVal;
  470. }
  471. function GetUserNotes() {
  472. $query = "SELECT noteid, r_clientid, content, UNIX_TIMESTAMP(posted) as posted, isprivate FROM " . $this->dbprefix . "note WHERE r_clientid = " . $this->adminid . " AND isprivate = 'no' ORDER BY posted DESC";
  473. $retVal = $this->db->getAll($query,null,DB_FETCHMODE_ASSOC);
  474. if(DB::isError($retVal)) $this->SqlError($query,$retVal);
  475. return $retVal;
  476. }
  477. function GetPayments($clientID) {
  478. if (!$clientID) {
  479. $where = '1';
  480. } else {
  481. $where = 'c.clientid = ' . $clientID;
  482. }
  483. $query = "SELECT `paymentid` , p.`invoiceid` , `invoice_num`, p.`clientid`, `company` , `amount` , `method` , UNIX_TIMESTAMP(made_on) as made_on FROM " . $this->dbprefix . "payment p INNER JOIN " . $this->dbprefix . "client c USING ( clientid ) LEFT JOIN " . $this->dbprefix . "invoice i ON i.invoiceid = p.invoiceid WHERE " . $where . " ORDER BY made_on DESC";
  484. $retVal = $this->db->getAll($query,null,DB_FETCHMODE_ASSOC);
  485. if(DB::isError($retVal)) $this->SqlError($query,$retVal);
  486. return $retVal;
  487. }
  488. function InsertPayment( $clientID, $invoiceID, $amount, $method) {
  489. $query = "INSERT INTO " . $this->dbprefix . "payment (clientid, invoiceid, amount, method, made_on) VALUES (" . $clientID . "," . $invoiceID . ",'" . $amount . "','" . addslashes($method) . "',NOW());";
  490. $retVal = $this->db->query($query);
  491. if(DB::isError($retVal)) $this->SqlError($query,$retVal);
  492. return true;
  493. }
  494. function DeletePayment($paymentID) {
  495. $query = "DELETE FROM " . $this->dbprefix . "payment WHERE paymentid = " . $paymentID;
  496. $retVal = $this->db->query($query);
  497. if(DB::isError($retVal)) $this->SqlError($query,$retVal);
  498. return true;
  499. }
  500. function FetchPaygates() {
  501. $query = "SELECT * FROM " . $this->dbprefix . "paygate ORDER BY company";
  502. $retVal = $this->db->getAll($query,null,DB_FETCHMODE_ASSOC);
  503. if(DB::isError($retVal)) $this->SqlError($query,$retVal);
  504. return $retVal;
  505. }
  506. function FetchRecurrances() {
  507. $query = "SELECT `recurranceid` , `invoiceid` , `days` , `months` , UNIX_TIMESTAMP(`until`) as until, action FROM `" . $this->dbprefix . "recurrance`";
  508. $retVal = $this->db->getAll($query,null,DB_FETCHMODE_ASSOC);
  509. if(DB::isError($retVal)) $this->SqlError($query,$retVal);
  510. return $retVal;
  511. }
  512. function EnablePaygate($paygateID,$enabled) {
  513. $status = ($enabled) ? 'yes' : 'no';
  514. $query = "UPDATE " . $this->dbprefix . "paygate SET enabled = '" . $status . "' WHERE paygateid = " . $paygateID;
  515. $retVal = $this->db->query($query);
  516. if(DB::isError($retVal)) $this->SqlError($query,$retVal);
  517. return true;
  518. }
  519. function AdminStats() {
  520. $query = "SELECT `logid` , `creator` , `targetid` , `targettype` , `eventid` , UNIX_TIMESTAMP(occured) as occured, `details`, `company` FROM `" . $this->dbprefix . "log` INNER JOIN `" . $this->dbprefix . "client` ON clientid = targetid WHERE eventid IN ( 10,11 ) AND `occured` >= '" . date('Y-m-d h:i:s',strtotime("1 day ago")) . "' AND parentclientid = " . $this->adminid;
  521. $retVal = $this->db->getAll($query,null,DB_FETCHMODE_ASSOC);
  522. if(DB::isError($retVal)) $this->SqlError($query,$retVal);
  523. $stats['logins'] = $retVal;
  524. $query = "SELECT `paymentid` , p.`invoiceid` , `invoice_num`, p.`clientid`, `company` , `amount` , `method` , UNIX_TIMESTAMP(made_on) as made_on FROM " . $this->dbprefix . "payment p INNER JOIN " . $this->dbprefix . "client c USING ( clientid ) LEFT JOIN " . $this->dbprefix . "invoice i ON i.invoiceid = p.invoiceid WHERE `made_on` >= '" . date('Y-m-d h:i:s',strtotime("1 day ago")) . "' ORDER BY made_on DESC";
  525. $retVal = $this->db->getAll($query,null,DB_FETCHMODE_ASSOC);
  526. if(DB::isError($retVal)) $this->SqlError($query,$retVal);
  527. $stats['payments'] = $retVal;
  528. $query = "SELECT (IFNULL( SUM( ii.cost * iii.qty ) , 0 ) + ( shipping ) ) * ( 1 + ( tax /100 ) ) * ( 1 + ( tax2 /100 ) ) AS total FROM " . $this->dbprefix . "invoice i INNER JOIN " . $this->dbprefix . "client c ON i.clientid = c.clientid LEFT JOIN " . $this->dbprefix . "invoice_invoiceitem AS iii ON i.invoiceid = iii.invoiceid LEFT JOIN " . $this->dbprefix . "invoiceitem AS ii ON ii.invoiceitemid = iii.invoiceitemid WHERE i.visible = '1' AND parentclientid = " . $this->adminid . " AND `curr_status` NOT LIKE 'fully paid'";
  529. $retVal = $this->db->getAll($query,null,DB_FETCHMODE_ASSOC);
  530. if(DB::isError($retVal)) $this->SqlError($query,$retVal);
  531. $stats['owed'] = $retVal;
  532. $query = "SELECT c.clientid,company,i.invoiceid, invoice_num,UNIX_TIMESTAMP(issue_date) as issue_date,UNIX_TIMESTAMP(due_date) as due_date,SUM(ii.cost*iii.qty) as cost , (SUM(ii.cost*iii.qty)+(shipping))*(tax/100) as tax, (SUM(ii.cost*iii.qty)+(shipping))*(tax2/100) as tax2, shipping , (IFNULL(SUM(ii.cost*iii.qty),0)+(shipping))*(1+(tax/100))*(1+(tax2/100)) as total,curr_status FROM " . $this->dbprefix . "invoice i INNER JOIN " . $this->dbprefix . "client c ON i.clientid = c.clientid LEFT JOIN " . $this->dbprefix . "invoice_invoiceitem as iii ON i.invoiceid = iii.invoiceid LEFT JOIN " . $this->dbprefix . "invoiceitem as ii ON ii.invoiceitemid = iii.invoiceitemid WHERE i.visible = '1' AND parentclientid = " . $this->adminid . " AND `curr_status` LIKE 'overdue' GROUP BY iii.invoiceid, i.invoiceid ORDER BY due_date";
  533. $retVal = $this->db->getAll($query,null,DB_FETCHMODE_ASSOC);
  534. if(DB::isError($retVal)) $this->SqlError($query,$retVal);
  535. $stats['overdue'] = $retVal;
  536. return $stats;
  537. }
  538. }
  539. ?>