/modules/billing/proc_gift.php

https://github.com/MyITCRM/myitcrm1 · PHP · 223 lines · 171 code · 35 blank · 17 comment · 35 complexity · c87154291d8b2fcaeb782969a49b8e27 MD5 · raw file

  1. <?php
  2. $amount = $VAR['gift_amount'];
  3. $gift_code = $VAR['gift_code'];
  4. $customer_id = $VAR['customer_id'];
  5. $invoice_id = $VAR['invoice_id'];
  6. $workorder_id = $VAR['workorder_id'];
  7. $date = time();
  8. /* check for valid code */
  9. $q = "SELECT * FROM ".PRFX."GIFT_CERT WHERE GIFT_CODE LIKE".$db->qstr( $gift_code );
  10. if(!$rs = $db->execute($q)) {
  11. force_page('core', 'error&error_msg=MySQL Error: '.$db->ErrorMsg().'&menu=1&type=database');
  12. exit;
  13. }
  14. if($rs->fields['GIFT_ID'] == '') {
  15. force_page('billing','new&wo_id='.$workorder_id.'&customer_id='.$customer_id.'&invoice_id='.$invoice_id.'&page_title=Billing&error_msg=Not a valid gift code.');
  16. }
  17. $gift_expire = $rs->fields['EXPIRE'];
  18. $gift_amount = $rs->fields['AMOUNT'];
  19. $gift_active = $rs->fields['ACTIVE'];
  20. $gift_id = $rs->fields['GIFT_ID'];
  21. /* do some checks to see if it is a valid gift certificate */
  22. /* check active */
  23. if($gift_active != 1) {
  24. force_page('billing','new&wo_id='.$workorder_id.'&customer_id='.$customer_id.'&invoice_id='.$invoice_id.'&page_title=Billing&error_msg=This gift certificate is not active');
  25. exit;
  26. }
  27. /* check if expired */
  28. if($gift_expire < $date) {
  29. force_page('billing','new&wo_id='.$workorder_id.'&customer_id='.$customer_id.'&invoice_id='.$invoice_id.'&page_title=Billing&error_msg=This gift certificate is expired.');
  30. exit;
  31. }
  32. /* get invoice details */
  33. $q = "SELECT * FROM ".PRFX."TABLE_INVOICE WHERE INVOICE_ID=".$db->qstr($invoice_id);
  34. if(!$rs = $db->execute($q)) {
  35. force_page('core', 'error&error_msg=MySQL Error: '.$db->ErrorMsg().'&menu=1');
  36. exit;
  37. }
  38. $invoice_details = $rs->FetchRow();
  39. //Check to see if we are processing more then required
  40. if($invoice_details['BALANCE'] < $gift_amount){
  41. force_page('billing', 'new&wo_id='.$workorder_id.'&customer_id='.$customer_id.' &invoice_id='.$invoice_id.'&error_msg= You can not bill more than the amount of the invoice.');
  42. exit;
  43. }
  44. /* check if this is a partial payment */
  45. if($invoice_details['INVOICE_AMOUNT'] > $gift_amount){
  46. if($invoice_details['balance'] > 0 ) {
  47. $balance = $invoice_details['balance'] - $gift_amount;
  48. } else {
  49. $balance = $invoice_details['INVOICE_AMOUNT'] - $gift_amount;
  50. }
  51. $paid_amount = $gift_amount + $invoice_details['PAID_AMOUNT'];
  52. $balance = sprintf("%01.2f", $balance);
  53. if($balance == 0 ) {
  54. $flag = 1;
  55. } else {
  56. $flag = 0;
  57. }
  58. /* insert Transaction */
  59. $gift_amount = number_format($gift_amount, 2,'.', '');
  60. $balance = number_format($balance, 2,'.', '');
  61. $memo = "Partial Gift Certificate Payment Made of $currency_sym$gift_amount, Balance due: $currency_sym$balance, ID: $gift_code";
  62. $q = "INSERT INTO ".PRFX."TABLE_TRANSACTION SET
  63. DATE = ".$db->qstr(time()).",
  64. TYPE = '3',
  65. INVOICE_ID = ".$db->qstr($invoice_id).",
  66. WORKORDER_ID = ".$db->qstr($workorder_id).",
  67. CUSTOMER_ID = ".$db->qstr($customer_id).",
  68. MEMO = ".$db->qstr($memo).",
  69. AMOUNT = ".$db->qstr($gift_amount);
  70. if(!$rs = $db->execute($q)) {
  71. force_page('core', 'error&error_msg=MySQL Error: '.$db->ErrorMsg().'&menu=1');
  72. exit;
  73. }
  74. /* update the invoice */
  75. if($balance == 0 ) {
  76. $q = "UPDATE ".PRFX."TABLE_INVOICE SET
  77. PAID_DATE = ".$db->qstr(time()).",
  78. INVOICE_PAID = ".$db->qstr($flag).",
  79. PAID_AMOUNT = ".$db->qstr($paid_amount).",
  80. balance = ".$db->qstr($balance).",
  81. INVOICE_PAID ='1' WHERE INVOICE_ID = ".$db->qstr($invoice_id);
  82. } else {
  83. $q = "UPDATE ".PRFX."TABLE_INVOICE SET
  84. PAID_DATE = ".$db->qstr(time()).",
  85. INVOICE_PAID = ".$db->qstr($flag).",
  86. PAID_AMOUNT = ".$db->qstr($paid_amount).",
  87. balance = ".$db->qstr($balance)." WHERE INVOICE_ID = ".$db->qstr($invoice_id);
  88. }
  89. if(!$rs = $db->execute($q)) {
  90. force_page('core', 'error&error_msg=MySQL Error: '.$db->ErrorMsg().'&menu=1');
  91. exit;
  92. }
  93. /* update work order */
  94. $q = "INSERT INTO ".PRFX."TABLE_WORK_ORDER_STATUS SET
  95. WORK_ORDER_ID = ".$db->qstr($workorder_id).",
  96. WORK_ORDER_STATUS_DATE = ".$db->qstr(time()).",
  97. WORK_ORDER_STATUS_NOTES = ".$db->qstr($memo).",
  98. WORK_ORDER_STATUS_ENTER_BY = ".$db->qstr($_SESSION['login_id']);
  99. if(!$rs = $db->execute($q)) {
  100. force_page('core', 'error&error_msg=MySQL Error: '.$db->ErrorMsg().'&menu=1');
  101. exit;
  102. }
  103. /* update if balance = 0 */
  104. if($balance == 0 ) {
  105. $q = "UPDATE ".PRFX."TABLE_WORK_ORDER SET
  106. WORK_ORDER_STATUS = '6',
  107. WORK_ORDER_CURRENT_STATUS = '8'
  108. WHERE WORK_ORDER_ID =".$db->qstr($workorder_id);
  109. if(!$rs = $db->execute($q)) {
  110. force_page('core', 'error&error_msg=MySQL Error: '.$db->ErrorMsg().'&menu=1');
  111. exit;
  112. }
  113. }
  114. } else {
  115. /* full payment made */
  116. if($invoice_details['INVOICE_AMOUNT'] < $gift_amount) {
  117. /* update gift amnount and set paid amount full */
  118. $remain_gift = $gift_amount - $invoice_details['INVOICE_AMOUNT'];
  119. $q = "UPDATE ".PRFX."GIFT_CERT SET AMOUNT=".$db->qstr( $remain_gift )." WHERE GIFT_ID=".$db->qstr( $gift_id );
  120. if(!$rs = $db->execute($q)) {
  121. force_page('core', 'error&error_msg=MySQL Error: '.$db->ErrorMsg().'&menu=1');
  122. exit;
  123. }
  124. $gift_amount = $invoice_details['INVOICE_AMOUNT'];
  125. $flag = 1;
  126. }
  127. if($invoice_details['INVOICE_AMOUNT'] = $gift_amount){
  128. /* insert Transaction */
  129. $gift_amount = number_format($gift_amount, 2,'.', '');
  130. $memo = "Full Gift Certificate Payment Made of $currency_sym$gift_amount, ID: $gift_code";
  131. $q = "INSERT INTO ".PRFX."TABLE_TRANSACTION SET
  132. DATE = ".$db->qstr(time()).",
  133. TYPE = '3',
  134. INVOICE_ID = ".$db->qstr($invoice_id).",
  135. WORKORDER_ID = ".$db->qstr($workorder_id).",
  136. CUSTOMER_ID = ".$db->qstr($customer_id).",
  137. MEMO = ".$db->qstr($memo).",
  138. AMOUNT = ".$db->qstr($gift_amount);
  139. if(!$rs = $db->execute($q)) {
  140. force_page('core', 'error&error_msg=MySQL Error: '.$db->ErrorMsg().'&menu=1');
  141. exit;
  142. }
  143. /* update the invoice */
  144. $q = "UPDATE ".PRFX."TABLE_INVOICE SET
  145. PAID_DATE = ".$db->qstr(time()).",
  146. PAID_AMOUNT = ".$db->qstr($gift_amount).",
  147. INVOICE_PAID = '1',
  148. balance = ".$db->qstr(0.00)."
  149. WHERE INVOICE_ID = ".$db->qstr($invoice_id);
  150. if(!$rs = $db->execute($q)) {
  151. force_page('core', 'error&error_msg=MySQL Error: '.$db->ErrorMsg().'&menu=1');
  152. exit;
  153. }
  154. /* update work order */
  155. $q = "INSERT INTO ".PRFX."TABLE_WORK_ORDER_STATUS SET
  156. WORK_ORDER_ID = ".$db->qstr($workorder_id).",
  157. WORK_ORDER_STATUS_DATE = ".$db->qstr(time()).",
  158. WORK_ORDER_STATUS_NOTES = ".$db->qstr($memo).",
  159. WORK_ORDER_STATUS_ENTER_BY = ".$db->qstr($_SESSION['login_id']);
  160. if(!$rs = $db->execute($q)) {
  161. force_page('core', 'error&error_msg=MySQL Error: '.$db->ErrorMsg().'&menu=1');
  162. exit;
  163. }
  164. $q = "UPDATE ".PRFX."TABLE_WORK_ORDER SET
  165. WORK_ORDER_STATUS = '6',
  166. WORK_ORDER_CURRENT_STATUS = '8'
  167. WHERE WORK_ORDER_ID =".$db->qstr($workorder_id);
  168. if(!$rs = $db->execute($q)) {
  169. force_page('core', 'error&error_msg=MySQL Error: '.$db->ErrorMsg().'&menu=1');
  170. exit;
  171. }
  172. }
  173. }
  174. /* update gift certificate */
  175. $q = "UPDATE ".PRFX."GIFT_CERT SET";
  176. if($flag != 1) {
  177. $q .= "ACTIVE =". $db->qstr( 0 ).",";
  178. } else {
  179. $q .= "DATE_REDEMED =". $db->qstr( time() ).",
  180. INVOICE_ID =". $db->qstr( $invoice_id )."
  181. WHERE GIFT_ID=".$db->qstr( $gift_id );
  182. }
  183. if(!$rs = $db->execute($q)) {
  184. force_page('core', 'error&error_msg=MySQL Error: '.$db->ErrorMsg().'&menu=1&type=database');
  185. exit;
  186. }
  187. force_page('invoice', "view&invoice_id=$invoice_id&customer_id=$customer_id");
  188. ?>