| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- <?php
- include_once "../include/DB.php";
- $UserID="";
- $RiderID="";
- if (isset($_POST["UserID"]) && !empty($_POST["UserID"])) {
- $UserID=$_POST["UserID"];
- }
- if (isset($_POST["RiderID"]) && !empty($_POST["RiderID"])) {
- $RiderID=$_POST["RiderID"];
- }
- $sql = "SELECT *,
- user.CreatedAt AS UserCreatedAt,
- user.UpdatedAt AS UserUpdatedAt,
- rider.CreatedAt AS RaiderCreatedAt,
- rider.UpdatedAt AS RaiderUpdatedAt,
- sref.TotalFreeSwapCount AS SwapReferTotalFreeSwapCount,
- sref.UsedSwapCount AS SwapReferUsedSwapCount,
- sref.AvailableSwapCount AS SwapReferAvailableSwapCount,
- sref.CreatedAt AS SwapReferCreatedAt,
- sref.UpdatedAt AS SwapReferUpdatedAt,
- srech.TotalRechargedSwapCount AS SwapRechargeTotalRechargedSwapCount,
- srech.UsedSwapCount AS SwapRechargeUsedSwapCount,
- srech.AvailableSwapCount AS SwapRechargeAvailableSwapCount,
- srech.CreatedAt AS SwapRechargeCreatedAt,
- srech.UpdatedAt AS SwapRechargeUpdatedAt
- FROM user user
- JOIN rider rider ON user.UserID=rider.UserID
- JOIN swaprecharge srech ON rider.RiderID=srech.RiderID
- JOIN swaprefer sref ON rider.RiderID=sref.RiderID
- WHERE user.UserID='$UserID' OR rider.RiderID='$RiderID'";
- $result = $conn->query($sql);
- if ($result!== false && $result->num_rows > 0) {
- $listArray = [];
- // output data of each row
- while($row = $result->fetch_assoc()) {
- $RiderID=$row["RiderID"];
- $swapsql = "SELECT *,
- swap.CreatedAt AS SwapCreatedAt,
- swap.UpdatedAt AS SwapUpdatedAt
- FROM swap swap WHERE swap.RiderID='$RiderID' AND swap.CreatedAt BETWEEN '$monthFirstDate' AND '$monthLastDate' ";
- $swapresult = $conn->query($swapsql);
- $swaplistArray= [];
- if ($swapresult!== false && $swapresult->num_rows > 0) {
- while($swaprow = $swapresult->fetch_assoc()) {
-
- $swapvalue = array(
- "SwapID"=>(int)$swaprow["SwapID"],
- "RiderID"=>$swaprow["RiderID"],
- "SwapReferID"=>$swaprow["SwapReferID"],
- "SwapRechargeID"=>$swaprow["SwapRechargeID"],
- "ReturnBatterID"=>$swaprow["ReturnBatterID"],
- "SwappedBatterID"=>$swaprow["SwappedBatterID"],
- "IsFreeSwap"=>$swaprow["IsFreeSwap"],
- "SwapCreatedAt"=>$swaprow["SwapCreatedAt"],
- "SwapUpdatedAt"=>$swaprow["SwapUpdatedAt"],
- );
- $swaplistArray[]= $swapvalue;
- }
- }
- $totalFreeForMonth=2;
- $PaidSwapCount =array_count_values(array_column($swaplistArray, 'IsFreeSwap'))["0"]??0;
- $FreeSwapCount =array_count_values(array_column($swaplistArray, 'IsFreeSwap'))["1"]??0;
- $AvailableFreeCount = $totalFreeForMonth-$FreeSwapCount;
- $swapCount = count($swaplistArray);
- $TotalRemainingSwapCount=$totalFreeForMonth+(int)$row["SwapReferTotalFreeSwapCount"]+(int)$row["SwapRechargeTotalRechargedSwapCount"];
- $RemainingUsedSwapCount=$FreeSwapCount+(int)$row["SwapReferUsedSwapCount"]+(int)$row["SwapRechargeUsedSwapCount"];
- $RemainingAvailableSwapCount=$AvailableFreeCount+(int)$row["SwapReferAvailableSwapCount"]+(int)$row["SwapRechargeAvailableSwapCount"];
- // Declare an array
- $value = array(
- "RaiderData"=>array(
- "RiderID"=>$row["RiderID"],
- "RaiderCreatedAt"=>$row["RaiderCreatedAt"],
- "RaiderUpdatedAt"=>$row["RaiderUpdatedAt"],
- ),
- "UserData"=>array(
- "UserID"=>(int)$row["UserID"],
- "UserName"=>$row["UserName"],
- "PhoneNumber"=>$row["PhoneNumber"],
- "UserCreatedAt"=>$row["UserCreatedAt"],
- "UserUpdatedAt"=>$row["UserUpdatedAt"],
- ),
- "SwapData"=>array(
- "RemainingData"=>array(
- "Total"=>$TotalRemainingSwapCount,
- "Used"=>$RemainingUsedSwapCount,
- "Available"=>$RemainingAvailableSwapCount,
- ),
- "ReferData"=>array(
- "SwapReferID"=>(int)$row["SwapReferID"],
- "Total"=>(int)$row["SwapReferTotalFreeSwapCount"],
- "Used"=>(int)$row["SwapReferUsedSwapCount"],
- "Available"=>(int)$row["SwapReferAvailableSwapCount"],
- "SwapReferCreatedAt"=>$row["SwapReferCreatedAt"],
- "SwapReferUpdatedAt"=>$row["SwapReferUpdatedAt"],
- ),
- "RechargeData"=>array(
- "SwapRechargeID"=>(int)$row["SwapRechargeID"],
- "Total"=>(int)$row["SwapRechargeTotalRechargedSwapCount"],
- "Used"=>(int)$row["SwapRechargeUsedSwapCount"],
- "Available"=>(int)$row["SwapRechargeAvailableSwapCount"],
- "SwapRechargeCreatedAt"=>$row["SwapRechargeCreatedAt"],
- "SwapRechargeUpdatedAt"=>$row["SwapRechargeUpdatedAt"],
- ),
- "FreeData"=>array(
- "Total"=>$totalFreeForMonth,
- "Used"=>$FreeSwapCount,
- "Available"=>$AvailableFreeCount,
- ),
- "HistoryData"=>array(
- "TotalCount"=>$swapCount,
- "FreeSwapCount"=>$FreeSwapCount,
- "PaidSwapCount"=>$PaidSwapCount,
- "History"=>$swaplistArray
- )
- ),
- "Message"=>"Date successfully get"
- );
-
- $listArray[]= $value;
- }
- $count = count($listArray);
- if($count==1){
- // Use json_encode() function
- $json = json_encode($listArray[0]);
- }else {
- // Use json_encode() function
- $json = json_encode($listArray);
- }
- } else {
- http_response_code(404);
- $errorMesage= $conn->error;
- if ($errorMesage=="") {
- $errorMesage = "UserID not available";
- if (empty($_GET["UserID"])) {
- $errorMesage = "UserID is empty";
- }
- }
- // Declare an array with error message
- $value = array(
- "Message"=>"Failed to get data",
- "Error"=>$errorMesage
- );
- // Use json_encode() function
- $json = json_encode($value);
- }
- // Display the output
- echo($json);
- $conn->close();
|