PageRenderTime 26ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/app/Http/Controllers/CampaignController.php

https://bitbucket.org/generaltechnology/sticar-trip
PHP | 388 lines | 339 code | 43 blank | 6 comment | 8 complexity | a388dde5310b6aedee66ba4c2d4e4cfc MD5 | raw file
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use \Carbon\Carbon;
  5. use DB;
  6. class CampaignController extends Controller
  7. {
  8. public function __construct()
  9. {
  10. $this->data = [
  11. 'startToday' => Carbon::now()->format('Y-m-d 00:00:00'),
  12. 'endToday' => Carbon::now()->format('Y-m-d 23:59:59'),
  13. 'now' => Carbon::now()->format('Y-m-d H:i:s')
  14. ];
  15. }
  16. public function index(Request $request)
  17. {
  18. $campaignId = $request->id;
  19. $campaign = (
  20. DB::table('Campaign')
  21. ->select([
  22. 'campaignId',
  23. 'campaignName',
  24. 'startDate',
  25. 'endDate'
  26. ])
  27. ->where('campaignId', $campaignId)
  28. ->where('status', 'active')
  29. ->orderBy('createdAt', 'ASC')
  30. ->first()
  31. );
  32. $contractIds = (
  33. DB::table('Contract')
  34. ->where('campaignId', $campaign->campaignId)
  35. ->where('status', 'approved')
  36. ->groupBy('contractId')
  37. ->pluck('contractId')
  38. );
  39. $trips = (
  40. DB::table('Trip')
  41. ->join('User', 'Trip.driverId', '=', 'User.userId')
  42. ->select(DB::raw('
  43. User.userId as driverId,
  44. User.fullname as driverName,
  45. User.email as driverEmail,
  46. SUM(Trip.totalCredit) as totalCredit,
  47. SUM(Trip.totalDistance) as totalDistance,
  48. SUM(Trip.totalImpression) as totalImpression
  49. '))
  50. ->whereIn('Trip.contractId', $contractIds)
  51. ->where('Trip.latitude', '!=', 0)
  52. ->where('Trip.longitude', '!=', 0)
  53. ->where('Trip.status', '!=', 'closed')
  54. ->where('Trip.createdAt', '>=', $this->data['startToday'])
  55. ->orderBy('User.fullname', 'ASC')
  56. ->groupBy(['Trip.driverId', 'Trip.contractId'])
  57. ->get()
  58. );
  59. $data = [
  60. 'title' => $campaign->campaignName,
  61. 'campaignId' => $campaign->campaignId,
  62. 'online' => count($trips),
  63. 'total' => count($contractIds),
  64. 'percentage' => round(count($trips) / count($contractIds) * 100),
  65. 'trips' => $trips
  66. ];
  67. return view('campaign.index', $data);
  68. }
  69. public function start(Request $request)
  70. {
  71. $campaign = (
  72. DB::table('Campaign')
  73. ->select([
  74. 'campaignId',
  75. 'areaId'
  76. ])
  77. ->where('campaignId', $request->id)
  78. ->where('status', 'active')
  79. ->orderBy('createdAt', 'ASC')
  80. ->first()
  81. );
  82. $lola = (
  83. DB::table('Lola')
  84. ->where('areaId', $campaign->areaId)
  85. ->first()
  86. );
  87. if ($lola) {
  88. $contractIds = (
  89. DB::table('Contract')
  90. ->where('campaignId', $campaign->campaignId)
  91. ->where('status', 'approved')
  92. ->groupBy('contractId')
  93. ->pluck('contractId')
  94. );
  95. // START:stop trip before today
  96. $beforeTodayIds = (
  97. DB::table('Trip')
  98. ->whereIn('contractId', $contractIds)
  99. ->where('status', '!=', 'closed')
  100. ->where('createdAt', '<', $this->data['startToday'])
  101. ->pluck('tripId')
  102. );
  103. DB::table('Trip')
  104. ->whereIn('tripId', $beforeTodayIds)
  105. ->update([
  106. 'status' => 'closed'
  107. ]);
  108. // END:stop trip before today
  109. $todayOnTrips = (
  110. DB::table('Trip')
  111. ->select(DB::raw('
  112. Trip.driverId,
  113. Trip.contractId,
  114. SUM(Trip.totalCredit) as totalCredit,
  115. SUM(Trip.totalDistance) as totalDistance,
  116. SUM(Trip.totalImpression) as totalImpression
  117. '))
  118. ->whereIn('Trip.contractId', $contractIds)
  119. ->where('Trip.latitude', '!=', 0)
  120. ->where('Trip.longitude', '!=', 0)
  121. ->where('Trip.status', '!=', 'closed')
  122. ->where('Trip.createdAt', '>=', $this->data['startToday'])
  123. ->groupBy(['Trip.driverId', 'Trip.contractId'])
  124. ->get()
  125. );
  126. // START: stop trip today with lola = 0
  127. $todayLola0Ids = (
  128. DB::table('Trip')
  129. ->whereIn('contractId', $contractIds)
  130. ->whereNotIn('contractId', $todayOnTrips->pluck('contractId'))
  131. ->where('status', '!=', 'closed')
  132. ->where('createdAt', '>=', $this->data['startToday'])
  133. ->pluck('tripId')
  134. );
  135. DB::table('Trip')
  136. ->whereIn('tripId', $todayLola0Ids)
  137. ->update([
  138. 'status' => 'closed'
  139. ]);
  140. // END: stop trip today with lola = 0
  141. $limit = round(count($contractIds) * $request->percentage / 100) - count($todayOnTrips);
  142. $insertTrips = (
  143. DB::table('Contract')
  144. ->select([
  145. 'driverId',
  146. 'contractId'
  147. ])
  148. ->where('campaignId', $campaign->campaignId)
  149. ->whereNotIn('contractId', $todayOnTrips->pluck('contractId'))
  150. ->where('status', 'approved')
  151. ->inRandomOrder()
  152. ->limit($limit <= 0 ? 0 : $limit)
  153. ->get()
  154. );
  155. if (count($insertTrips)) {
  156. foreach ($insertTrips as $key => $insertTrip) {
  157. DB::table('Trip')->insert([
  158. 'tripUUID' => DB::select('SELECT UUID() AS UUID')[0]->UUID,
  159. // 'latitude' => -(rand(abs($lola->latMin) * 1000000, abs($lola->latMax) * 1000000) / 1000000),
  160. // 'longitude' => rand(abs($lola->longMin) * 1000000, abs($lola->longMax) * 1000000) / 1000000,
  161. 'latitude' => rand($lola->latMin * 1000000, $lola->latMax * 1000000) / 1000000,
  162. 'longitude' => rand($lola->longMin * 1000000, $lola->longMax * 1000000) / 1000000,
  163. 'driverId' => $insertTrip->driverId,
  164. 'contractId' => $insertTrip->contractId,
  165. 'status' => 'ongoing',
  166. 'createdAt' => $this->data['now']
  167. ]);
  168. }
  169. }
  170. $data = [
  171. 'message' => 'Success',
  172. 'status' => true
  173. ];
  174. } else {
  175. $data = [
  176. 'message' => 'Failed Start Trip, LOLA does not exists.',
  177. 'status' => false
  178. ];
  179. }
  180. return response()->json($data);
  181. }
  182. public function report(Request $request)
  183. {
  184. $campaignId = $request->id;
  185. $campaign = (
  186. DB::table('Campaign')
  187. ->select([
  188. 'campaignId',
  189. 'campaignName',
  190. 'startDate',
  191. 'endDate'
  192. ])
  193. ->where('campaignId', $campaignId)
  194. ->where('status', 'active')
  195. ->orderBy('createdAt', 'ASC')
  196. ->first()
  197. );
  198. $contractIds = (
  199. DB::table('Contract')
  200. ->where('campaignId', $campaign->campaignId)
  201. ->where('status', 'approved')
  202. ->groupBy('contractId')
  203. ->pluck('contractId')
  204. );
  205. $reports = (
  206. DB::table('Trip')
  207. ->select(DB::raw('
  208. DATE(createdAt) as date,
  209. SUM(Trip.totalDistance) as totalDistance
  210. '))
  211. ->whereIn('contractId', $contractIds)
  212. ->where('createdAt', '>=', $campaign->startDate)
  213. ->where('createdAt', '<', $campaign->endDate)
  214. ->groupBy(DB::raw('
  215. DATE(createdAt)
  216. '))
  217. ->get()
  218. );
  219. $data = [
  220. 'title' => $campaign->campaignName,
  221. 'campaignId' => $campaign->campaignId,
  222. 'maxDate' => Carbon::now()->format('Y-m-d'),
  223. 'startDate' => $campaign->startDate,
  224. 'endDate' => $campaign->endDate,
  225. 'reports' => $reports
  226. ];
  227. return view('campaign.report', $data);
  228. }
  229. public function inject(Request $request)
  230. {
  231. $date = $request->date;
  232. $startDate = Carbon::createFromFormat('Y-m-d H:i:s', $date . ' 00:00:00')->toDateTimeString();
  233. $endDate = Carbon::createFromFormat('Y-m-d H:i:s', $date . ' 23:59:59')->toDateTimeString();
  234. $campaign = (
  235. DB::table('Campaign')
  236. ->select([
  237. 'campaignId',
  238. 'areaId'
  239. ])
  240. ->where('campaignId', $request->id)
  241. ->where('status', 'active')
  242. ->orderBy('createdAt', 'ASC')
  243. ->first()
  244. );
  245. $lola = (
  246. DB::table('Lola')
  247. ->where('areaId', $campaign->areaId)
  248. ->first()
  249. );
  250. if ($lola) {
  251. $wrappingCredit = (
  252. DB::table('Wrapping')
  253. ->where('campaignId', $campaign->campaignId)
  254. ->where('status', 'active')
  255. ->pluck('credit')
  256. ->first()
  257. );
  258. $allContracts = (
  259. DB::table('Contract')
  260. ->select([
  261. 'contractId',
  262. 'driverId'
  263. ])
  264. ->where('campaignId', $campaign->campaignId)
  265. ->where('status', 'approved')
  266. ->groupBy('contractId')
  267. ->get()
  268. );
  269. $contractExists = (
  270. DB::table('Trip')
  271. ->select(DB::raw('
  272. contractId,
  273. SUM(totalDistance) as totalDistance
  274. '))
  275. ->whereIn('contractId', $allContracts->pluck('contractId'))
  276. ->where('createdAt', '>=', $startDate)
  277. ->where('createdAt', '<=', $endDate)
  278. ->groupBy('contractId')
  279. ->get()
  280. );
  281. $contractNotExists = (
  282. DB::table('Contract')
  283. ->select([
  284. 'contractId',
  285. 'driverId'
  286. ])
  287. ->whereNotIn('contractId', $contractExists->pluck('contractId'))
  288. ->where('campaignId', $campaign->campaignId)
  289. ->where('status', 'approved')
  290. ->groupBy('contractId')
  291. ->get()
  292. );
  293. foreach ($contractExists as $key => $contractExist) {
  294. $totalDistance = mt_rand(50000, 150000) - intval($contractExist->totalDistance);
  295. $totalCredit = round($totalDistance * intval($wrappingCredit) / 1000);
  296. $totalImpression = round($totalDistance * intval($wrappingCredit) * (mt_rand(1000, 3000) / 1000) / 1000);
  297. $driverId = DB::table('Contract')->where('contractId', $contractExist->contractId)->pluck('driverId')->first();
  298. $contractId = $contractExist->contractId;
  299. if ($totalDistance > 0 && $totalCredit > 0 && $totalImpression > 0 ) {
  300. DB::table('Trip')->insert([
  301. 'tripUUID' => DB::select('SELECT UUID() AS UUID')[0]->UUID,
  302. 'totalCredit' => $totalCredit,
  303. 'totalDistance' => $totalDistance,
  304. 'totalImpression' => $totalImpression,
  305. 'latitude' => -(rand(abs($lola->latMin) * 1000000, abs($lola->latMax) * 1000000) / 1000000),
  306. 'longitude' => rand(abs($lola->longMin) * 1000000, abs($lola->longMax) * 1000000) / 1000000,
  307. 'driverId' => $driverId,
  308. 'contractId' => $contractId,
  309. 'status' => 'closed',
  310. 'createdAt' => date("Y-m-d H:i:s", mt_rand(strtotime($startDate), strtotime($endDate)))
  311. ]);
  312. }
  313. }
  314. foreach ($contractNotExists as $key => $contractNotExist) {
  315. $totalDistance = mt_rand(50000, 150000);
  316. $totalCredit = round($totalDistance * intval($wrappingCredit) / 1000);
  317. $totalImpression = round($totalDistance * intval($wrappingCredit) * (mt_rand(1000, 3000) / 1000) / 1000);
  318. $driverId = $contractNotExist->driverId;
  319. $contractId = $contractNotExist->contractId;
  320. DB::table('Trip')->insert([
  321. 'tripUUID' => DB::select('SELECT UUID() AS UUID')[0]->UUID,
  322. 'totalCredit' => $totalCredit,
  323. 'totalDistance' => $totalDistance,
  324. 'totalImpression' => $totalImpression,
  325. 'latitude' => -(rand(abs($lola->latMin) * 1000000, abs($lola->latMax) * 1000000) / 1000000),
  326. 'longitude' => rand(abs($lola->longMin) * 1000000, abs($lola->longMax) * 1000000) / 1000000,
  327. 'driverId' => $driverId,
  328. 'contractId' => $contractId,
  329. 'status' => 'closed',
  330. 'createdAt' => date("Y-m-d H:i:s", mt_rand(strtotime($startDate), strtotime($endDate)))
  331. ]);
  332. }
  333. $data = [
  334. 'message' => 'success',
  335. 'status' => true
  336. ];
  337. } else {
  338. $data = [
  339. 'message' => 'Failed Start Trip, LOLA does not exists.',
  340. 'status' => false
  341. ];
  342. }
  343. return response()->json($data);
  344. }
  345. }