index.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. include_once "../include/DB.php";
  3. $UserID="";
  4. $RiderID="";
  5. if (isset($_POST["UserID"]) && !empty($_POST["UserID"])) {
  6. $UserID=$_POST["UserID"];
  7. }
  8. if (isset($_POST["RiderID"]) && !empty($_POST["RiderID"])) {
  9. $RiderID=$_POST["RiderID"];
  10. }
  11. $sql = "SELECT *,
  12. user.CreatedAt AS UserCreatedAt,
  13. user.UpdatedAt AS UserUpdatedAt,
  14. rider.CreatedAt AS RaiderCreatedAt,
  15. rider.UpdatedAt AS RaiderUpdatedAt,
  16. sref.TotalFreeSwapCount AS SwapReferTotalFreeSwapCount,
  17. sref.UsedSwapCount AS SwapReferUsedSwapCount,
  18. sref.AvailableSwapCount AS SwapReferAvailableSwapCount,
  19. sref.CreatedAt AS SwapReferCreatedAt,
  20. sref.UpdatedAt AS SwapReferUpdatedAt,
  21. srech.TotalRechargedSwapCount AS SwapRechargeTotalRechargedSwapCount,
  22. srech.UsedSwapCount AS SwapRechargeUsedSwapCount,
  23. srech.AvailableSwapCount AS SwapRechargeAvailableSwapCount,
  24. srech.CreatedAt AS SwapRechargeCreatedAt,
  25. srech.UpdatedAt AS SwapRechargeUpdatedAt
  26. FROM user user
  27. JOIN rider rider ON user.UserID=rider.UserID
  28. JOIN swaprecharge srech ON rider.RiderID=srech.RiderID
  29. JOIN swaprefer sref ON rider.RiderID=sref.RiderID
  30. WHERE user.UserID='$UserID' OR rider.RiderID='$RiderID'";
  31. $result = $conn->query($sql);
  32. if ($result!== false && $result->num_rows > 0) {
  33. $listArray = [];
  34. // output data of each row
  35. while($row = $result->fetch_assoc()) {
  36. $RiderID=$row["RiderID"];
  37. $swapsql = "SELECT *,
  38. swap.CreatedAt AS SwapCreatedAt,
  39. swap.UpdatedAt AS SwapUpdatedAt
  40. FROM swap swap WHERE swap.RiderID='$RiderID' AND swap.CreatedAt BETWEEN '$monthFirstDate' AND '$monthLastDate' ";
  41. $swapresult = $conn->query($swapsql);
  42. $swaplistArray= [];
  43. if ($swapresult!== false && $swapresult->num_rows > 0) {
  44. while($swaprow = $swapresult->fetch_assoc()) {
  45. $swapvalue = array(
  46. "SwapID"=>(int)$swaprow["SwapID"],
  47. "RiderID"=>$swaprow["RiderID"],
  48. "SwapReferID"=>$swaprow["SwapReferID"],
  49. "SwapRechargeID"=>$swaprow["SwapRechargeID"],
  50. "ReturnBatterID"=>$swaprow["ReturnBatterID"],
  51. "SwappedBatterID"=>$swaprow["SwappedBatterID"],
  52. "IsFreeSwap"=>$swaprow["IsFreeSwap"],
  53. "SwapCreatedAt"=>$swaprow["SwapCreatedAt"],
  54. "SwapUpdatedAt"=>$swaprow["SwapUpdatedAt"],
  55. );
  56. $swaplistArray[]= $swapvalue;
  57. }
  58. }
  59. $totalFreeForMonth=2;
  60. $PaidSwapCount =array_count_values(array_column($swaplistArray, 'IsFreeSwap'))["0"]??0;
  61. $FreeSwapCount =array_count_values(array_column($swaplistArray, 'IsFreeSwap'))["1"]??0;
  62. $AvailableFreeCount = $totalFreeForMonth-$FreeSwapCount;
  63. $swapCount = count($swaplistArray);
  64. $TotalRemainingSwapCount=$totalFreeForMonth+(int)$row["SwapReferTotalFreeSwapCount"]+(int)$row["SwapRechargeTotalRechargedSwapCount"];
  65. $RemainingUsedSwapCount=$FreeSwapCount+(int)$row["SwapReferUsedSwapCount"]+(int)$row["SwapRechargeUsedSwapCount"];
  66. $RemainingAvailableSwapCount=$AvailableFreeCount+(int)$row["SwapReferAvailableSwapCount"]+(int)$row["SwapRechargeAvailableSwapCount"];
  67. // Declare an array
  68. $value = array(
  69. "RaiderData"=>array(
  70. "RiderID"=>$row["RiderID"],
  71. "RaiderCreatedAt"=>$row["RaiderCreatedAt"],
  72. "RaiderUpdatedAt"=>$row["RaiderUpdatedAt"],
  73. ),
  74. "UserData"=>array(
  75. "UserID"=>(int)$row["UserID"],
  76. "UserName"=>$row["UserName"],
  77. "PhoneNumber"=>$row["PhoneNumber"],
  78. "UserCreatedAt"=>$row["UserCreatedAt"],
  79. "UserUpdatedAt"=>$row["UserUpdatedAt"],
  80. ),
  81. "SwapData"=>array(
  82. "RemainingData"=>array(
  83. "Total"=>$TotalRemainingSwapCount,
  84. "Used"=>$RemainingUsedSwapCount,
  85. "Available"=>$RemainingAvailableSwapCount,
  86. ),
  87. "ReferData"=>array(
  88. "SwapReferID"=>(int)$row["SwapReferID"],
  89. "Total"=>(int)$row["SwapReferTotalFreeSwapCount"],
  90. "Used"=>(int)$row["SwapReferUsedSwapCount"],
  91. "Available"=>(int)$row["SwapReferAvailableSwapCount"],
  92. "SwapReferCreatedAt"=>$row["SwapReferCreatedAt"],
  93. "SwapReferUpdatedAt"=>$row["SwapReferUpdatedAt"],
  94. ),
  95. "RechargeData"=>array(
  96. "SwapRechargeID"=>(int)$row["SwapRechargeID"],
  97. "Total"=>(int)$row["SwapRechargeTotalRechargedSwapCount"],
  98. "Used"=>(int)$row["SwapRechargeUsedSwapCount"],
  99. "Available"=>(int)$row["SwapRechargeAvailableSwapCount"],
  100. "SwapRechargeCreatedAt"=>$row["SwapRechargeCreatedAt"],
  101. "SwapRechargeUpdatedAt"=>$row["SwapRechargeUpdatedAt"],
  102. ),
  103. "FreeData"=>array(
  104. "Total"=>$totalFreeForMonth,
  105. "Used"=>$FreeSwapCount,
  106. "Available"=>$AvailableFreeCount,
  107. ),
  108. "HistoryData"=>array(
  109. "TotalCount"=>$swapCount,
  110. "FreeSwapCount"=>$FreeSwapCount,
  111. "PaidSwapCount"=>$PaidSwapCount,
  112. "History"=>$swaplistArray
  113. )
  114. ),
  115. "Message"=>"Date successfully get"
  116. );
  117. $listArray[]= $value;
  118. }
  119. $count = count($listArray);
  120. if($count==1){
  121. // Use json_encode() function
  122. $json = json_encode($listArray[0]);
  123. }else {
  124. // Use json_encode() function
  125. $json = json_encode($listArray);
  126. }
  127. } else {
  128. http_response_code(404);
  129. $errorMesage= $conn->error;
  130. if ($errorMesage=="") {
  131. $errorMesage = "UserID not available";
  132. if (empty($_GET["UserID"])) {
  133. $errorMesage = "UserID is empty";
  134. }
  135. }
  136. // Declare an array with error message
  137. $value = array(
  138. "Message"=>"Failed to get data",
  139. "Error"=>$errorMesage
  140. );
  141. // Use json_encode() function
  142. $json = json_encode($value);
  143. }
  144. // Display the output
  145. echo($json);
  146. $conn->close();