Guest User

Untitled

a guest
Nov 15th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.42 KB | None | 0 0
  1. public function store(Request $request)
  2. {
  3.  
  4. $this->validatedData = Validator::make($request->all(), [
  5. 'user_id' => 'required',
  6. 'lelang_id' => 'required',
  7. 'harga_bid' => 'required'
  8.  
  9. ]);
  10.  
  11. $this->user_id = $request['user_id'];
  12. $this->lelang_id = $request['lelang_id'];
  13. $this->harga_bid = $request['harga_bid'];
  14.  
  15.  
  16. $transLelang = TransaksiLelang::find($this->lelang_id);
  17. $maxBid = $transLelang['maksimum_bid'];
  18. $prosesLelang = ProsesLelang::where('lelang_id', $this->lelang_id)
  19. ->where('status', '2')
  20. ->get();
  21. $checkTotLelang = $prosesLelang->count();
  22.  
  23. $checkAsBid = ProsesLelang::where('user_id', $this->user_id)
  24. ->where('lelang_id', $this->lelang_id)
  25. ->get();
  26.  
  27.  
  28. // if user as bid
  29. if ($checkAsBid->count() > 0) {
  30.  
  31. //get balance dan validasi saldo
  32. $balance1 = IkanPay::where('user_id', $this->user_id)->sum('nominal');
  33. $balance2 = IkanPay::where('user_id', $this->user_id)->where('lelang_id', $this->lelang_id)->sum('nominal');
  34. $totalBalance = $balance1 + abs($balance2);
  35.  
  36. $lastBid = ProsesLelang::where('user_id',$this->user_id)
  37. ->where('lelang_id',$this->lelang_id)
  38. ->where('status','2')
  39. ->first();
  40.  
  41. if ($this->harga_bid <= $lastBid['harga_bid']){
  42. return response()->json([
  43. 'success' => 'masukkan nilai lebih'
  44. ]);
  45. }
  46.  
  47.  
  48.  
  49. if ($this->harga_bid > $totalBalance or $this->harga_bid < $transLelang['harga']) {
  50.  
  51. return response()->json([
  52. 'success' => false,
  53. 'error' => 'saldo_kurang',
  54.  
  55. ]);
  56. }
  57.  
  58. $checkfirst = ProsesLelang::where('user_id', $this->user_id)
  59. ->where('lelang_id', $this->lelang_id)
  60. ->first();
  61.  
  62. if ($checkfirst == null) {
  63. $proses = new ProsesLelang();
  64. $proses->user_id = $this->user_id;
  65. $proses->lelang_id = $this->lelang_id;
  66. $proses->harga_bid = $this->harga_bid;
  67. $proses->status = 2;
  68. $proses->save();
  69.  
  70. $saveBalance = new IkanPay();
  71. $saveBalance->user_id = $this->user_id;
  72. $saveBalance->lelang_id = $this->lelang_id;
  73. $saveBalance->nominal = -$this->harga_bid;
  74. $saveBalance->transaction = "Proses Tawar Ikan #" . $this->lelang_id;
  75. $saveBalance->type = 2;
  76. $saveBalance->status = 1;
  77. $saveBalance->save();
  78.  
  79.  
  80. return $this->returnValidator();
  81. } else {
  82.  
  83. $changeBalance = IkanPay::where('user_id', $this->user_id)
  84. ->where('lelang_id', $this->lelang_id)
  85. ->orderBy('nominal', 'ASC')
  86. ->first();
  87.  
  88. // foreach ($changeBalance as $cb) {
  89. $checkID = $changeBalance->id;
  90. $ipay = IkanPay::find($checkID);
  91. $ipay->status = 3;
  92. $ipay->update();
  93.  
  94. $saveBalance = new IkanPay();
  95. $saveBalance->user_id = $this->user_id;
  96. $saveBalance->lelang_id = $this->lelang_id;
  97. $saveBalance->nominal = abs($changeBalance->nominal);
  98. $saveBalance->transaction = "Cancel Tawar Ikan #" . $this->lelang_id;
  99. $saveBalance->type = 2;
  100. $saveBalance->status = 1;
  101. $saveBalance->save();
  102.  
  103. // }
  104.  
  105. $checkProses = ProsesLelang::where('user_id', $this->user_id)
  106. ->where('lelang_id', $this->lelang_id)
  107. ->orderBy('created_at', 'DESC')
  108. ->first();
  109.  
  110. // foreach ($checkProses as $cp) {
  111. $checkPro = $checkProses->id;
  112. $cps = ProsesLelang::find($checkPro);
  113. $cps->status = 3;
  114. $cps->update();
  115.  
  116. $proses = new ProsesLelang();
  117. $proses->user_id = $this->user_id;
  118. $proses->lelang_id = $this->lelang_id;
  119. $proses->harga_bid = $this->harga_bid;
  120. $proses->status = 2;
  121. $proses->save();
  122.  
  123. $saveBalance = new IkanPay();
  124. $saveBalance->user_id = $this->user_id;
  125. $saveBalance->lelang_id = $this->lelang_id;
  126. $saveBalance->nominal = -$this->harga_bid;
  127. $saveBalance->transaction = "Proses Tawar Ikan #" . $this->lelang_id;
  128. $saveBalance->type = 1;
  129. $saveBalance->status = 1;
  130. $saveBalance->save();
  131. // }
  132. //ini
  133. return response()->json([
  134. 'success' => true,
  135. 'error' => 'false',
  136. 'user_id' => $this->user_id,
  137. 'lelang_id' => $this->lelang_id,
  138. 'harga_bid' => $this->harga_bid
  139.  
  140. ],
  141. $this->successStatus);
  142.  
  143. }
  144. }
  145.  
  146. // if user max bid
  147. if ($checkTotLelang >= $maxBid) {
  148. return response()->json([
  149. 'success' => false,
  150. 'error' => 'mancapai_max_bid',
  151.  
  152. ]);
  153. } else {
  154.  
  155. //get balance dan validasi saldo
  156. $balance1 = IkanPay::where('user_id', $this->user_id)->sum('nominal');
  157. $balance2 = IkanPay::where('user_id', $this->user_id)->where('lelang_id', $this->lelang_id)->sum('nominal');
  158. $totalBalance = $balance1 + abs($balance2);
  159.  
  160. if ($this->harga_bid > $totalBalance or $this->harga_bid < $transLelang['harga']) {
  161. return response()->json([
  162. 'success' => false,
  163. 'error' => 'saldo_kurang',
  164.  
  165. ]);
  166. }
  167.  
  168.  
  169. $checkfirst = ProsesLelang::where('user_id', $this->user_id)
  170. ->where('lelang_id', $this->lelang_id)
  171. ->first();
  172.  
  173. if ($checkfirst == null) {
  174. $proses = new ProsesLelang();
  175. $proses->user_id = $this->user_id;
  176. $proses->lelang_id = $this->lelang_id;
  177. $proses->harga_bid = $this->harga_bid;
  178. $proses->status = 2;
  179. $proses->save();
  180.  
  181. $saveBalance = new IkanPay();
  182. $saveBalance->user_id = $this->user_id;
  183. $saveBalance->lelang_id = $this->lelang_id;
  184. $saveBalance->nominal = -$this->harga_bid;
  185. $saveBalance->transaction = "Proses Tawar Ikan #" . $this->lelang_id;
  186. $saveBalance->type = 2;
  187. $saveBalance->status = 1;
  188. $saveBalance->save();
  189.  
  190.  
  191. return response()->json([
  192. 'success' => true,
  193. 'error' => 'false',
  194. 'user_id' => $this->user_id,
  195. 'lelang_id' => $this->lelang_id,
  196. 'harga_bid' => $this->harga_bid
  197.  
  198. ],
  199. $this->successStatus);
  200. } else {
  201.  
  202. $changeBalance = IkanPay::where('user_id', $this->user_id)
  203. ->where('lelang_id', $this->lelang_id)
  204. ->orderBy('nominal', 'ASC')
  205. ->first();
  206.  
  207. // foreach ($changeBalance as $cb) {
  208. $checkID = $changeBalance->id;
  209. $ipay = IkanPay::find($checkID);
  210. $ipay->status = 3;
  211. $ipay->update();
  212.  
  213. $saveBalance = new IkanPay();
  214. $saveBalance->user_id = $this->user_id;
  215. $saveBalance->lelang_id = $this->lelang_id;
  216. $saveBalance->nominal = abs($changeBalance->nominal);
  217. $saveBalance->transaction = "Cancel Tawar Ikan #" . $this->lelang_id;
  218. $saveBalance->type = 2;
  219. $saveBalance->status = 1;
  220. $saveBalance->save();
  221.  
  222.  
  223. $checkProses = ProsesLelang::where('user_id', $this->user_id)
  224. ->where('lelang_id', $this->lelang_id)
  225. ->orderBy('created_at', 'DESC')
  226. ->first();
  227.  
  228. $checkPro = $checkProses->id;
  229. $cps = ProsesLelang::find($checkPro);
  230. $cps->status = 3;
  231. $cps->update();
  232.  
  233. $proses = new ProsesLelang();
  234. $proses->user_id = $this->user_id;
  235. $proses->lelang_id = $this->lelang_id;
  236. $proses->harga_bid = $this->harga_bid;
  237. $proses->status = 2;
  238. $proses->save();
  239.  
  240. $saveBalance = new IkanPay();
  241. $saveBalance->user_id = $this->user_id;
  242. $saveBalance->lelang_id = $this->lelang_id;
  243. $saveBalance->nominal = -$this->harga_bid;
  244. $saveBalance->transaction = "Proses Tawar Ikan #" . $this->lelang_id;
  245. $saveBalance->type = 1;
  246. $saveBalance->status = 1;
  247. $saveBalance->save();
  248. // }
  249.  
  250. return response()->json([
  251. 'success' => true,
  252. 'error' => 'false',
  253. 'user_id' => $this->user_id,
  254. 'lelang_id' => $this->lelang_id,
  255. 'harga_bid' => $this->harga_bid
  256.  
  257. ],
  258. $this->successStatus);
  259.  
  260. }
  261.  
  262. }
  263.  
  264.  
  265. }
Add Comment
Please, Sign In to add comment