Advertisement
Guest User

Untitled

a guest
Dec 25th, 2011
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.74 KB | None | 0 0
  1. <?php
  2. public function getVoucher($accountid, $offerid, $userid){ // this function is used for creating voucher for selected offers
  3.  
  4. $sql = "SELECT * FROM offers where offerId = '".$offerid."'";
  5. $stmt = $this->db->query($sql);
  6. $offer_results = $stmt->fetchall();
  7. //check if offer valid
  8. $offer_status = $offer_results['0']['status'];
  9. $points_needed = $offer_results['0']['pointsNeeded'];//from offers table
  10.  
  11. if($offer_status != 1){//TODO:check this comparison
  12. $aResultData = array('data'=>array('error'=>'Offer Not valid'), 'headerMsg'=>'HTTP/1.1 400 Offer Not valid');
  13. return $aResultData;
  14. }
  15. //block credits table for user
  16.  
  17. $credit_block_check_stmt = $this->db->query("SELECT max(status) FROM credits where accountId = '".$accountid."'");
  18. $credit_block_check_results = $credit_block_check_stmt->fetchall();
  19. $block_status = $credit_block_check_results['0']['max(status)'];//from offers table
  20.  
  21.  
  22. if($block_status == 2){
  23. $aResultData = array('data'=>array(), 'headerMsg'=>'HTTP/1.1 400 Another transaction in progress for user');
  24. return $aResultData;
  25. }
  26.  
  27.  
  28. $credit_block_stmt = $this->db->query("Update credits set status = 2 where accountId = '".$accountid."'");
  29. $sSqlCheckStmt = "SELECT sum(availablePoints) as availPoints FROM credits where accountId ='$accountid' and expiryDate >= sysdate()";
  30. error_log(__FUNCTION__.' : line '.__LINE__.' : ' .$sSqlCheckStmt);
  31. $credit_check_stmt = $this->db->query($sSqlCheckStmt);
  32. $credit_check_results = $credit_check_stmt->fetchall();
  33. $pointsAvailable = $credit_check_results['0']['availPoints'];
  34.  
  35.  
  36. error_log("Points needed ".$points_needed ." Points avail : ".$pointsAvailable);
  37.  
  38. #$pointsAvailable = 155554;
  39. if($points_needed > $pointsAvailable){
  40. //unblock credits before exit
  41. $credit_unblock_stmt = $this->db->query("Update credits set status = 1 where accountId = '".$accountid."'"); // will be used either when user does not have sufficient points or when points have been succesfully deducted
  42. //check if user has points for offer
  43. $aResultData = array('data'=>array('error'=>'Insufficient points accrued'), 'headerMsg'=>'HTTP/1.1 400 Insufficient points accrued');
  44. return $aResultData;
  45. }
  46.  
  47. //create voucher -- start
  48. $voucher_store = $offer_results['0']['storeId'];
  49. $voucher_name = $offer_results['0']['name'];
  50. $voucher_desc = $offer_results['0']['description'];
  51. $validity = $offer_results['0']['voucherSpan'];
  52. $redeemTime = $offer_results['0']['redeemInterval'];
  53. $partnerId = $offer_results['0']['partnerId'];
  54. $pointsProgramId = $offer_results['0']['pointsProgramId'];
  55. $pointsNeeded = $offer_results['0']['pointsNeeded'];
  56.  
  57.  
  58. $sSql1 = "Insert into vouchers (voucherId, offerId, accountId, validStoreid, name, description, dateCreated, expiryDate, validity, redeemInterval, status) VALUES (uuid(), '$offerid', '$accountid','$voucher_store','$voucher_name','$voucher_desc', current_date(), date_add(current_date(), INTERVAL $validity DAY), $validity, $redeemTime, 1)";
  59. $create_voucher_stmt = $this->db->query($sSql1);
  60. error_log(__FUNCTION__.' : line '.__LINE__.' : ' .$sSql1);
  61.  
  62.  
  63. $voucherid_get_stmt = $this->db->query("SELECT max(voucherId) as id FROM vouchers where accountId = '".$accountid."'");
  64. $voucherid_get_results = $voucherid_get_stmt->fetchall();
  65. $voucherid = $voucherid_get_results['0']['id'];
  66.  
  67. //create voucher -- end
  68.  
  69. // creating transaction --- start
  70.  
  71. $sSqlT1 = "Insert into transactions (txnId, txnDateTime, partnerId, pointsProgramId, points, extTxnId) VALUES (uuid(), now(), '$partnerId', '$pointsProgramId',-$pointsNeeded, '$voucherid')";
  72. $create_voucher_txn_stmt = $this->db->query($sSqlT1);
  73. error_log(__FUNCTION__.' : line '.__LINE__.' : ' .$sSqlT1);
  74.  
  75. try{ // If transactions successfully added...
  76. $sSqlT2 = "select max(txnId) as txnId from transactions where extTxnId='$voucherid'";
  77. $select_Txn_stmt = $this->db->query($sSqlT2);
  78. $txnid_get_results = $select_Txn_stmt->fetchall();
  79. $txnId = $txnid_get_results['0']['txnId'];
  80. error_log(__FUNCTION__.' : line '.__LINE__.' : ' .$sSqlT2);
  81. }
  82. catch(Exception $e){
  83. error_log('Exception in '.__FUNCTION__.' : line '.__LINE__.' : '.$e->getMessage());
  84. }
  85.  
  86.  
  87.  
  88.  
  89. $sSqlT3 = "Insert into transfers (transferId, txnId, accountId, points) VALUES (uuid(), '$txnId', '$accountid', -$pointsNeeded)";
  90. $create_transfer_stmt = $this->db->query($sSqlT3);
  91. error_log(__FUNCTION__.' : line '.__LINE__.' : ' .$sSqlT3);
  92.  
  93. try{ // If transfers successfully added...
  94. $sSqlT4 = "select max(transferId) as transferId from transfers where accountId='$accountid'";
  95. $select_Transfer_stmt = $this->db->query($sSqlT4);
  96. $transferid_get_results = $select_Transfer_stmt->fetchall();
  97. $transferId = $transferid_get_results['0']['transferId'];
  98. error_log(__FUNCTION__.' : line '.__LINE__.' : ' .$sSqlT4);
  99. }
  100. catch(Exception $e){
  101. error_log('Exception in '.__FUNCTION__.' : line '.__LINE__.' : '.$e->getMessage());
  102. }
  103.  
  104. // creating transaction --- end
  105.  
  106. //use points - creating creadits
  107. try{
  108. $sSqlCredits = "SELECT creditId , pointsProgramId, availablePoints, txnStoreId FROM credits where accountId = '".$accountid."' and expiryDate >= current_date() and availablePoints > 0 order by expiryDate asc";
  109. error_log(__FUNCTION__.' : line '.__LINE__.' : ' .$sSqlCredits);
  110. $valid_credit_list_stmt = $this->db->query($sSqlCredits);
  111. $valid_credit_list_results = $valid_credit_list_stmt->fetchall();
  112. }
  113. catch(Exception $e){
  114. error_log('Exception in '.__FUNCTION__.' : line '.__LINE__.' : '.$e->getMessage());
  115. }
  116.  
  117. $count = 0;
  118. while($points_needed > 0){
  119. $avPts = $valid_credit_list_results[$count]['availablePoints'];
  120. $creditId = $valid_credit_list_results[$count]['creditId'];
  121. $programID = $valid_credit_list_results[$count]['pointsProgramId'];
  122. $txnStoreId = $valid_credit_list_results[$count]['txnStoreId'];
  123.  
  124. if($points_needed >= $avPts){
  125. $debitAmt = 0 - $avPts;
  126. $pointsAvailable = $pointsAvailable - $avPts;
  127. $points_needed = $points_needed - $avPts;
  128. $updatedAvailablePts = 0;
  129. }
  130. else{
  131. $debitAmt = 0 - $points_needed;
  132. $pointsAvailable = $pointsAvailable - $points_needed;
  133. $updatedAvailablePts = $avPts - $points_needed;
  134. $points_needed = 0;
  135. }
  136. try{
  137. $sSql2 = "Update credits set availablePoints = '".$updatedAvailablePts."' where creditId = '".$creditId."'";
  138. $credit_update_stmt = $this->db->query($sSql2);
  139. error_log(__FUNCTION__.' : line '.__LINE__.' : ' .$sSql2);
  140. }
  141. catch(Exception $e){
  142. error_log('Exception in '.__FUNCTION__.' : line '.__LINE__.' : '.$e->getMessage());
  143. }
  144.  
  145. try{
  146. $sSql3 = "Insert into credits (creditId, transferId, accountId, points, currBalance, creditDate, pointsProgramId, txnStoreId, linkedCreditId, status) VALUES (uuid(), '$transferId', '$accountid', '$debitAmt', '$pointsAvailable', current_date(), '$programID', '$txnStoreId', '$creditId', '1')";
  147. $debit_insert_stmt = $this->db->query($sSql3);
  148. error_log(__FUNCTION__.' : line '.__LINE__.' : ' .$sSql3);
  149. }
  150. catch(Exception $e){
  151. error_log('Exception in '.__FUNCTION__.' : line '.__LINE__.' : '.$e->getMessage());
  152. }
  153. $count = $count+1;
  154. }
  155.  
  156. try{
  157. //unblock credits before exit
  158. $credit_unblock_stmt = $this->db->query("Update credits set status = 1 where accountId = '".$accountid."'");
  159. }
  160. catch(Exception $e){
  161. error_log('Exception in '.__FUNCTION__.' : line '.__LINE__.' : '.$e->getMessage());
  162. }
  163.  
  164. $aResultData = array('data'=>array('status' => '1'), 'headerMsg'=>'HTTP/1.1 200 buy Voucher Response');
  165. return $aResultData;
  166. }//end of getVoucher function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement