redeem.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php
  2. include "Connection.php";
  3. require_once(dirname(__FILE__)."/_api/global.php");
  4. $conn = new Connection();
  5. if(isset($_REQUEST["funName"])) {
  6. $db = $conn->getConnection();
  7. $fun = $_REQUEST["funName"];
  8. switch($fun) {
  9. case "redeemlist":
  10. case "approveredeem":
  11. case "redeemapprovedlist":
  12. $fun();
  13. break;
  14. }
  15. }
  16. // REDEEM LIST
  17. function redeemlist($type=0) {
  18. global $conn;
  19. try {
  20. $lang = (isset($_REQUEST["lang"]))?$_REQUEST["lang"]:'en_us';
  21. $pn = $_REQUEST["pn"];
  22. $nr = $_REQUEST["nr"];
  23. $key = escape_str($_REQUEST["key"]);
  24. // $_SESSION["userid"]=1;
  25. if(isset($_SESSION["userid"])){
  26. if($type==1)
  27. {
  28. $econd=" AND a.is_approved=1";
  29. }else
  30. {
  31. $econd=" AND a.is_approved=0";
  32. }
  33. $query_str = "SELECT SQL_CALC_FOUND_ROWS a.*,u.`fname`,u.`lname`,u.`emailid`,u.`mobileno`,u.`dealer_code` FROM ".TABLE_USER_WALLET_APPROVE." a JOIN ".TABLE_USER." u ON a.smpin=u.smpin AND (u.role>7 AND u.role!=14) WHERE u.is_deleted = 0 ".$econd;
  34. if (!empty($key)) {
  35. $query_str .= " and ((a.smpin like '%".$key."%') or (u.fname like '%".$key."%') or (u.lname like '%".$key."%') ) ";
  36. }
  37. if (!empty($pn) and !empty($nr)) {
  38. // Navigation
  39. $query_str .= " limit " . (($pn - 1) * $nr) . ", " . $nr;
  40. }
  41. $query = $conn->query($query_str);
  42. // Total records
  43. $rows_query = $conn->query("select FOUND_ROWS() as total_records");
  44. $rows_result = $rows_query->fetchAll(PDO::FETCH_OBJ);
  45. $total_records = $rows_result[0]->total_records;
  46. $arr['total_records'] = $total_records;
  47. $userlist=$query->fetchAll(PDO::FETCH_OBJ);
  48. $dealer_column=array('dealer_name','city','zone');
  49. if(count($userlist)>0)
  50. {
  51. foreach($userlist as $mkey=>$res)
  52. {
  53. //GET DEALER DETAILS
  54. $check_dealer_str = "SELECT `dealer_name`, `city`, `zone` FROM `".DMS_DEALER."` WHERE dealer_code='".$res->dealer_code."' LIMIT 1";
  55. $check_result_arr=$conn->getQueryValue($check_dealer_str);
  56. $userlist[$mkey]->dealer=new stdClass();
  57. if(count($check_result_arr)>0)
  58. {
  59. $userlist[$mkey]->dealer=$check_result_arr[0];
  60. }else
  61. {
  62. foreach($dealer_column as $col)
  63. {
  64. $userlist[$mkey]->dealer->$col='';
  65. }
  66. }
  67. }
  68. }
  69. $arr['users'] = $userlist;
  70. if(count($arr) != 0) {
  71. $conn->message(true,$conn->getMessage($lang,'redeem','success1'),$arr );
  72. }else {
  73. $conn->message(false,$conn->getMessage($lang,'redeem','error2'));
  74. }
  75. }else {
  76. $conn->message(false,$conn->getMessage($lang,'redeem','error1'));
  77. }
  78. }
  79. catch(PDOException $e) {
  80. $conn->message(false,$conn->getMessage($lang,'redeem','error4'));
  81. }
  82. }
  83. function redeemapprovedlist() {
  84. redeemlist(1); //approved list
  85. }
  86. // REDEEM LIST
  87. function approveredeem () {
  88. global $conn;
  89. try {
  90. //print_r($_REQUEST);
  91. $lang = (isset($_REQUEST["lang"]))?$_REQUEST["lang"]:'en_us';
  92. if(isset($_SESSION["userid"]))
  93. {
  94. $rid_arr=array();
  95. $rid = escape_str($_REQUEST["id"]);
  96. if($rid!='')
  97. {
  98. $rid_arr=@explode(',',$rid);
  99. foreach($rid_arr as $ids)
  100. {
  101. if($ids!='')
  102. {
  103. if(!is_numeric($ids)){ $conn->message(false,$conn->getMessage($lang,'redeem','error3')); }
  104. }
  105. }
  106. }
  107. //VALIDATION CHECK
  108. if(!( $rid!='' && count($rid_arr)>0 ))
  109. {
  110. $conn->message(false,$conn->getMessage($lang,'redeem','error3'));
  111. }
  112. $check_count_str = "SELECT COUNT(id) as tcount FROM `".TABLE_USER_WALLET_APPROVE."` WHERE is_approved=0 AND id IN(".@implode(',',$rid_arr).") LIMIT 1";
  113. $check_result_arr=$conn->getQueryValue($check_count_str);
  114. if(count($rid_arr)!= $check_result_arr[0]->tcount)
  115. {
  116. $conn->message(false,$conn->getMessage($lang,'redeem','error3'));
  117. }
  118. $get_approval_str = "SELECT a.*,u.userid FROM `".TABLE_USER_WALLET_APPROVE."` a JOIN ".TABLE_USER." u ON a.smpin=u.smpin AND (u.role>7 AND u.role!=14) WHERE u.is_deleted = 0 AND a.is_approved=0 AND a.id IN(".@implode(',',$rid_arr).") ";
  119. $get_approval_arr=$conn->getQueryValue($get_approval_str);
  120. $smpin_arr=array();
  121. if(count($get_approval_arr)>0)
  122. {
  123. foreach($get_approval_arr as $res)
  124. {
  125. $conn->executeQuery("UPDATE ".TABLE_USER_WALLET_APPROVE." SET is_approved='1',approved_id='".$_SESSION["userid"]."',modified_on='".NOW."' WHERE id='".$res->id."'");
  126. $smpin_arr[]=$res->smpin;
  127. $lvl_points=$res->lvl_points;
  128. $token=TIMETOKEN.$res->userid;
  129. $conn->executeQuery("INSERT INTO ".TABLE_USER_WALLET_HISTORY."( `smpin`, `type`, `type_id`, `description`, `lvl_points`, `amount`, `transaction_type`, `created_on`, `modified_on`) VALUES ('".$res->smpin."','redeem','".$token."','REDEEM POINTS','".$lvl_points."','".$res->amount."' ,'C','".NOW."','".NOW."')");
  130. //UPDATE POINT IN USER
  131. $conn->executeQuery("UPDATE ".TABLE_USER_WALLET." SET amount=amount+".$res->amount." WHERE smpin='".$res->smpin."' ");
  132. }
  133. //TRIGGER NOTIFICATION
  134. sendapprovalNotification($smpin_arr);
  135. }
  136. $conn->message(true,$conn->getMessage($lang,'redeem','success1'));
  137. }else{
  138. $conn->message(false,$conn->getMessage($lang,'redeem','error1'));
  139. }
  140. }
  141. catch(PDOException $e) {
  142. $conn->message(false,$conn->getMessage($lang,'redeem','error4'));
  143. }
  144. }
  145. function sendapprovalNotification ($smpin_arr=array() )
  146. {
  147. global $conn;
  148. if(count($smpin_arr)>0)
  149. {
  150. $nobj=new stdClass();
  151. $notificationtitle='Redemptions point added in wallet';
  152. $notificationtext='Redemptions point approved and amount is added to wallet';
  153. $notificationtype='7';
  154. $notificationdata=json_encode(new stdClass());
  155. $smpin_quotes = array_map("quotes_array", $smpin_arr);
  156. //CHECK USER EXIST FOR NOTIFICATION
  157. $check_user_str = " SELECT userid FROM ".TABLE_USER." WHERE status='1' AND is_deleted='0' AND smpin IN (".@implode(',',$smpin_quotes).") AND role>7 AND role!=14";
  158. $check_user_arr = $conn->getQueryValue($check_user_str);
  159. $user_arr=array();
  160. if(count($check_user_arr)>0)
  161. {
  162. foreach ($check_user_arr as $res_user)
  163. {
  164. $user_arr[]=$res_user->userid;
  165. }
  166. //INSERT NOTITFICATION
  167. $conn->executeQuery("INSERT INTO `".TABLE_NOTIFICATION."` ( `notificationtitle`, `notificationtext`, `notificationdata`, `notificationtype`, `status`, `publisheddate`, `created_on`, `modified_on`) VALUES ('".$notificationtitle."','".escape_str($notificationtext)."','".$notificationdata."','".$notificationtype."' ,'1','".NOW."','".NOW."','".NOW."')");
  168. $notificationid=$conn->lastinsetid();
  169. $data=array('notification_id'=>$notificationid,'title'=>$assessment_arr[0]->assessmentname,'message'=>$notificationtext,'notificationtype'=>$notificationtype,'notificationdata'=>$notificationdata,'created_date'=>NOW);
  170. if(count($user_arr)>0)
  171. {
  172. $ins_arr=array();
  173. foreach($user_arr as $res)
  174. {
  175. $ins_arr[]=" ('".$notificationid."', '".$res."', '0', '".NOW."')";
  176. }
  177. $insert_chunk=array_chunk($ins_arr, 1000);
  178. foreach($insert_chunk as $chunk_ins)
  179. {
  180. $conn->executeQuery("INSERT INTO ".TABLE_NOTIFICATION_USER."(`notificationid`, `userid`, `status`, `created_on`) VALUES ".@implode(',',$chunk_ins)." ON DUPLICATE KEY UPDATE `status` =0");
  181. }
  182. $usertokenarr['ios']=array();
  183. $usertokenarr['android']=array();
  184. $user_device_str = "SELECT ud.devicetype,ud.token FROM `".TABLE_USER_DEVICES."` as ud JOIN (SELECT userid FROM ".TABLE_NOTIFICATION_USER." WHERE notificationid='".$notificationid."') as u ON ud.userid=u.userid WHERE ud.is_deleted='0' AND (devicetype='ios' OR devicetype='android') AND token!=''";
  185. $user_device_arr = $conn->getQueryValue($user_device_str);
  186. if(count($user_device_arr)>0)
  187. {
  188. foreach($user_device_arr as $res_user)
  189. {
  190. if(strtolower($res_user->devicetype)=='ios')
  191. {
  192. $usertokenarr['ios'][]=$res_user->token;
  193. }else if(strtolower($res_user->devicetype)=='android')
  194. {
  195. $usertokenarr['android'][]=$res_user->token;
  196. }
  197. }
  198. // $conn->sendUserNotification($data,$usertokenarr);
  199. }
  200. }
  201. }
  202. }
  203. }
  204. ?>