/modules/invoice/include.php

https://github.com/MyITCRM/myitcrm1 · PHP · 268 lines · 161 code · 57 blank · 50 comment · 27 complexity · dd8efad7666e388a4d77c38fda5b30c9 MD5 · raw file

  1. <?php
  2. #########################################
  3. # Display Open Invoice #
  4. #########################################
  5. function display_open_invoice($db, $page_no, $smarty)
  6. {
  7. global $smarty;
  8. // Define the number of results per page
  9. $max_results = 25;
  10. // Figure out the limit for the Execute based
  11. // on the current page number.
  12. $from = (($page_no * $max_results) - $max_results);
  13. $sql = "SELECT " . PRFX . "TABLE_INVOICE.*,
  14. " . PRFX . "TABLE_CUSTOMER. CUSTOMER_DISPLAY_NAME, CUSTOMER_ADDRESS, CUSTOMER_CITY, CUSTOMER_STATE, CUSTOMER_ZIP, CUSTOMER_PHONE, CUSTOMER_WORK_PHONE, CUSTOMER_MOBILE_PHONE, CUSTOMER_EMAIL, CUSTOMER_TYPE, CUSTOMER_FIRST_NAME, CUSTOMER_LAST_NAME, CREATE_DATE, LAST_ACTIVE ,
  15. " . PRFX . "TABLE_EMPLOYEE.*
  16. FROM " . PRFX . "TABLE_INVOICE
  17. LEFT JOIN " . PRFX . "TABLE_CUSTOMER ON (" . PRFX . "TABLE_INVOICE.CUSTOMER_ID = " . PRFX . "TABLE_CUSTOMER.CUSTOMER_ID)
  18. LEFT JOIN " . PRFX . "TABLE_EMPLOYEE ON (" . PRFX . "TABLE_INVOICE.EMPLOYEE_ID = " . PRFX . "TABLE_EMPLOYEE.EMPLOYEE_ID)
  19. WHERE INVOICE_PAID=" . $db->qstr(0) . " ORDER BY INVOICE_ID DESC LIMIT $from, $max_results";
  20. if (!$rs = $db->Execute($sql)) {
  21. force_page('core', 'error&error_msg=MySQL Error: ' . $db->ErrorMsg() . '&menu=1&type=database');
  22. exit;
  23. } else {
  24. $invoice_arr = $rs->GetArray();
  25. }
  26. // Figure out the total number of results in DB:
  27. $q = "SELECT COUNT(*) as Num FROM " . PRFX . "TABLE_INVOICE WHERE INVOICE_PAID=" . $db->qstr(0);
  28. if (!$results = $db->Execute($q)) {
  29. force_page('core', 'error&error_msg=MySQL Error: ' . $db->ErrorMsg() . '&menu=1&type=database');
  30. exit;
  31. }
  32. if (!$total_results = $results->FetchRow()) {
  33. force_page('core', 'error&error_msg=MySQL Error: ' . $db->ErrorMsg() . '&menu=1&type=database');
  34. exit;
  35. } else {
  36. $smarty->assign('total_results', $total_results['Num']);
  37. }
  38. // Figure out the total number of pages. Always round up using ceil()
  39. $total_pages = ceil($total_results["Num"] / $max_results);
  40. $smarty->assign('total_pages', $total_pages);
  41. // Assign the first page
  42. if ($page_no > 1) {
  43. $prev = ($page_no - 1);
  44. }
  45. // Build Next Link
  46. if ($page_no < $total_pages) {
  47. $next = ($page_no + 1);
  48. }
  49. $smarty->assign('name', $name);
  50. $smarty->assign('page_no', $page_no);
  51. $smarty->assign("previous", $prev);
  52. $smarty->assign("next", $next);
  53. return $invoice_arr;
  54. }
  55. ########################################
  56. # Paid Invoices #
  57. ########################################
  58. function display_paid_invoice($db, $page_no, $smarty)
  59. {
  60. global $smarty;
  61. // Define the number of results per page
  62. $max_results = 25;
  63. // Figure out the limit for the Execute based
  64. // on the current page number.
  65. $from = (($page_no * $max_results) - $max_results);
  66. $sql = "SELECT " . PRFX . "TABLE_INVOICE.*,
  67. " . PRFX . "TABLE_CUSTOMER. CUSTOMER_DISPLAY_NAME, CUSTOMER_ADDRESS, CUSTOMER_CITY, CUSTOMER_STATE, CUSTOMER_ZIP, CUSTOMER_PHONE, CUSTOMER_WORK_PHONE, CUSTOMER_MOBILE_PHONE, CUSTOMER_EMAIL, CUSTOMER_TYPE, CUSTOMER_FIRST_NAME, CUSTOMER_LAST_NAME, CREATE_DATE, LAST_ACTIVE ,
  68. " . PRFX . "TABLE_EMPLOYEE.*
  69. FROM " . PRFX . "TABLE_INVOICE
  70. LEFT JOIN " . PRFX . "TABLE_CUSTOMER ON (" . PRFX . "TABLE_INVOICE.CUSTOMER_ID = " . PRFX . "TABLE_CUSTOMER.CUSTOMER_ID)
  71. LEFT JOIN " . PRFX . "TABLE_EMPLOYEE ON (" . PRFX . "TABLE_INVOICE.EMPLOYEE_ID = " . PRFX . "TABLE_EMPLOYEE.EMPLOYEE_ID)
  72. WHERE INVOICE_PAID=" . $db->qstr(1) . " ORDER BY INVOICE_ID DESC LIMIT $from, $max_results";
  73. if (!$rs = $db->Execute($sql)) {
  74. force_page('core', 'error&error_msg=MySQL Error: ' . $db->ErrorMsg() . '&menu=1&type=database');
  75. exit;
  76. } else {
  77. $invoice_arr = $rs->GetArray();
  78. }
  79. // Figure out the total number of results in DB:
  80. $q = "SELECT COUNT(*) as Num FROM " . PRFX . "TABLE_INVOICE WHERE INVOICE_PAID=" . $db->qstr(1);
  81. if (!$results = $db->Execute($q)) {
  82. force_page('core', 'error&error_msg=MySQL Error: ' . $db->ErrorMsg() . '&menu=1&type=database');
  83. exit;
  84. }
  85. if (!$total_results = $results->FetchRow()) {
  86. force_page('core', 'error&error_msg=MySQL Error: ' . $db->ErrorMsg() . '&menu=1&type=database');
  87. exit;
  88. } else {
  89. $smarty->assign('total_results', $total_results['Num']);
  90. }
  91. // Figure out the total number of pages. Always round up using ceil()
  92. $total_pages = ceil($total_results["Num"] / $max_results);
  93. $smarty->assign('total_pages', $total_pages);
  94. // Assign the first page
  95. if ($page_no > 1) {
  96. $prev = ($page_no - 1);
  97. }
  98. // Build Next Link
  99. if ($page_no < $total_pages) {
  100. $next = ($page_no + 1);
  101. }
  102. $smarty->assign('name', $name);
  103. $smarty->assign('page_no', $page_no);
  104. $smarty->assign("previous", $prev);
  105. $smarty->assign("next", $next);
  106. return $invoice_arr;
  107. }
  108. ##########################################
  109. # xml2php Gateway #
  110. # Loads language file up as a php array #
  111. ##########################################
  112. function gateway_xml2php($module)
  113. {
  114. global $smarty;
  115. //$file = FILE_ROOT."language".SEP.$module.SEP.LANG ;
  116. $file = FILE_ROOT . "language" . SEP . LANG;
  117. $xml_parser = xml_parser_create();
  118. if (!($fp = fopen($file, 'r'))) {
  119. die('unable to open XML');
  120. }
  121. $contents = fread($fp, filesize($file));
  122. fclose($fp);
  123. xml_parse_into_struct($xml_parser, $contents, $arr_vals);
  124. xml_parser_free($xml_parser);
  125. $xmlarray = array();
  126. foreach ($arr_vals as $things) {
  127. if ($things['tag'] != 'TRANSLATE' && $things['value'] != "") {
  128. $ttag = strtolower($things['tag']);
  129. $tvalue = $things['value'];
  130. $xmlarray[$ttag] = $tvalue;
  131. }
  132. }
  133. return $xmlarray;
  134. }
  135. #####################################
  136. # Delete Labour Record #
  137. #####################################
  138. $labourID = $VAR['labourID'];
  139. function delete_labour_record($db, $labourID)
  140. {
  141. $sql = "DELETE FROM " . PRFX . "TABLE_INVOICE_LABOR WHERE INVOICE_LABOR_ID=" . $db->qstr($labourID);
  142. if (!$rs = $db->Execute($sql)) {
  143. force_page('core', 'error&error_msg=MySQL Error: ' . $db->ErrorMsg() . '&menu=1&type=database');
  144. exit;
  145. } else {
  146. return true;
  147. }
  148. }
  149. #####################################
  150. # Delete Parts Record #
  151. #####################################
  152. function delete_parts_record($db, $partsID)
  153. {
  154. $sql = "DELETE FROM " . PRFX . "TABLE_INVOICE_PARTS WHERE INVOICE_PARTS_ID=" . $db->qstr($partsID);
  155. if (!$rs = $db->Execute($sql)) {
  156. force_page('core', 'error&error_msg=MySQL Error: ' . $db->ErrorMsg() . '&menu=1&type=database');
  157. exit;
  158. } else {
  159. return true;
  160. }
  161. }
  162. #####################################
  163. # Delete Invoice #
  164. #####################################
  165. function delete_invoice($db, $invoice_id, $customer_id, $login)
  166. {
  167. //Actual Deletion Function from Invoice Table
  168. $q = "DELETE FROM ".PRFX."TABLE_INVOICE WHERE INVOICE_ID=".$db->qstr($invoice_id);
  169. if (!$rs = $db->Execute($q)) {
  170. force_page('core', 'error&error_msg=MySQL Error: ' . $db->ErrorMsg() . '&menu=1&type=database');
  171. exit;
  172. } else {
  173. return true;
  174. }
  175. // TODO - Add transaction log to database
  176. /*
  177. $q = "INSERT INTO ".PRFX."TABLE_TRANSACTION ( TRANSACTION_ID, DATE, TYPE, INVOICE_ID, WORKORDER_ID, CUSTOMER_ID, MEMO, AMOUNT ) VALUES,
  178. ( NULL, ".$db->qstr(time()).",'6',".$db->qstr($invoice_id).",'0',".$db->qstr($customer_id).",'Invoice Deleted By ".$db->qstr($login).",'0.00');";
  179. if (!$rs = $db->Execute($q)) {
  180. force_page('core', 'error&error_msg=MySQL Error: ' . $db->ErrorMsg() . '&menu=1&type=database');
  181. exit;
  182. }*/
  183. }
  184. #####################################
  185. # Sum Labour Sub Totals #
  186. #####################################
  187. function labour_sub_total_sum($db, $invoiceID)
  188. {
  189. $q = "SELECT SUM(INVOICE_LABOR_SUBTOTAL) AS labour_sub_total_sum FROM " . PRFX . "TABLE_INVOICE_LABOR WHERE INVOICE_ID=" . $db->qstr($invoiceID);
  190. if (!$rs = $db->Execute($q)) {
  191. echo 'Error: ' . $db->ErrorMsg();
  192. die;
  193. }
  194. $labour_sub_total_sum = $rs->fields['labour_sub_total_sum'];
  195. return $labour_sub_total_sum;
  196. }
  197. #####################################
  198. # Sum Parts Sub Total #
  199. #####################################
  200. function parts_sub_total_sum($db, $invoiceID)
  201. {
  202. $q = "SELECT SUM(INVOICE_PARTS_SUBTOTAL) AS parts_sub_total_sum FROM " . PRFX . "TABLE_INVOICE_PARTS WHERE INVOICE_ID=" . $db->qstr($invoiceID);
  203. if (!$rs = $db->Execute($q)) {
  204. echo 'Error: ' . $db->ErrorMsg();
  205. die;
  206. }
  207. $parts_sub_total_sum = $rs->fields['parts_sub_total_sum'];
  208. return $parts_sub_total_sum;
  209. }
  210. ?>