Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public function store(Request $request)
- {
- $this->validatedData = Validator::make($request->all(), [
- 'user_id' => 'required',
- 'lelang_id' => 'required',
- 'harga_bid' => 'required'
- ]);
- $this->user_id = $request['user_id'];
- $this->lelang_id = $request['lelang_id'];
- $this->harga_bid = $request['harga_bid'];
- $transLelang = TransaksiLelang::find($this->lelang_id);
- $maxBid = $transLelang['maksimum_bid'];
- $prosesLelang = ProsesLelang::where('lelang_id', $this->lelang_id)
- ->where('status', '2')
- ->get();
- $checkTotLelang = $prosesLelang->count();
- $checkAsBid = ProsesLelang::where('user_id', $this->user_id)
- ->where('lelang_id', $this->lelang_id)
- ->get();
- // if user as bid
- if ($checkAsBid->count() > 0) {
- //get balance dan validasi saldo
- $balance1 = IkanPay::where('user_id', $this->user_id)->sum('nominal');
- $balance2 = IkanPay::where('user_id', $this->user_id)->where('lelang_id', $this->lelang_id)->sum('nominal');
- $totalBalance = $balance1 + abs($balance2);
- $lastBid = ProsesLelang::where('user_id',$this->user_id)
- ->where('lelang_id',$this->lelang_id)
- ->where('status','2')
- ->first();
- if ($this->harga_bid <= $lastBid['harga_bid']){
- return response()->json([
- 'success' => 'masukkan nilai lebih'
- ]);
- }
- if ($this->harga_bid > $totalBalance or $this->harga_bid < $transLelang['harga']) {
- return response()->json([
- 'success' => false,
- 'error' => 'saldo_kurang',
- ]);
- }
- $checkfirst = ProsesLelang::where('user_id', $this->user_id)
- ->where('lelang_id', $this->lelang_id)
- ->first();
- if ($checkfirst == null) {
- $proses = new ProsesLelang();
- $proses->user_id = $this->user_id;
- $proses->lelang_id = $this->lelang_id;
- $proses->harga_bid = $this->harga_bid;
- $proses->status = 2;
- $proses->save();
- $saveBalance = new IkanPay();
- $saveBalance->user_id = $this->user_id;
- $saveBalance->lelang_id = $this->lelang_id;
- $saveBalance->nominal = -$this->harga_bid;
- $saveBalance->transaction = "Proses Tawar Ikan #" . $this->lelang_id;
- $saveBalance->type = 2;
- $saveBalance->status = 1;
- $saveBalance->save();
- return $this->returnValidator();
- } else {
- $changeBalance = IkanPay::where('user_id', $this->user_id)
- ->where('lelang_id', $this->lelang_id)
- ->orderBy('nominal', 'ASC')
- ->first();
- // foreach ($changeBalance as $cb) {
- $checkID = $changeBalance->id;
- $ipay = IkanPay::find($checkID);
- $ipay->status = 3;
- $ipay->update();
- $saveBalance = new IkanPay();
- $saveBalance->user_id = $this->user_id;
- $saveBalance->lelang_id = $this->lelang_id;
- $saveBalance->nominal = abs($changeBalance->nominal);
- $saveBalance->transaction = "Cancel Tawar Ikan #" . $this->lelang_id;
- $saveBalance->type = 2;
- $saveBalance->status = 1;
- $saveBalance->save();
- // }
- $checkProses = ProsesLelang::where('user_id', $this->user_id)
- ->where('lelang_id', $this->lelang_id)
- ->orderBy('created_at', 'DESC')
- ->first();
- // foreach ($checkProses as $cp) {
- $checkPro = $checkProses->id;
- $cps = ProsesLelang::find($checkPro);
- $cps->status = 3;
- $cps->update();
- $proses = new ProsesLelang();
- $proses->user_id = $this->user_id;
- $proses->lelang_id = $this->lelang_id;
- $proses->harga_bid = $this->harga_bid;
- $proses->status = 2;
- $proses->save();
- $saveBalance = new IkanPay();
- $saveBalance->user_id = $this->user_id;
- $saveBalance->lelang_id = $this->lelang_id;
- $saveBalance->nominal = -$this->harga_bid;
- $saveBalance->transaction = "Proses Tawar Ikan #" . $this->lelang_id;
- $saveBalance->type = 1;
- $saveBalance->status = 1;
- $saveBalance->save();
- // }
- //ini
- return response()->json([
- 'success' => true,
- 'error' => 'false',
- 'user_id' => $this->user_id,
- 'lelang_id' => $this->lelang_id,
- 'harga_bid' => $this->harga_bid
- ],
- $this->successStatus);
- }
- }
- // if user max bid
- if ($checkTotLelang >= $maxBid) {
- return response()->json([
- 'success' => false,
- 'error' => 'mancapai_max_bid',
- ]);
- } else {
- //get balance dan validasi saldo
- $balance1 = IkanPay::where('user_id', $this->user_id)->sum('nominal');
- $balance2 = IkanPay::where('user_id', $this->user_id)->where('lelang_id', $this->lelang_id)->sum('nominal');
- $totalBalance = $balance1 + abs($balance2);
- if ($this->harga_bid > $totalBalance or $this->harga_bid < $transLelang['harga']) {
- return response()->json([
- 'success' => false,
- 'error' => 'saldo_kurang',
- ]);
- }
- $checkfirst = ProsesLelang::where('user_id', $this->user_id)
- ->where('lelang_id', $this->lelang_id)
- ->first();
- if ($checkfirst == null) {
- $proses = new ProsesLelang();
- $proses->user_id = $this->user_id;
- $proses->lelang_id = $this->lelang_id;
- $proses->harga_bid = $this->harga_bid;
- $proses->status = 2;
- $proses->save();
- $saveBalance = new IkanPay();
- $saveBalance->user_id = $this->user_id;
- $saveBalance->lelang_id = $this->lelang_id;
- $saveBalance->nominal = -$this->harga_bid;
- $saveBalance->transaction = "Proses Tawar Ikan #" . $this->lelang_id;
- $saveBalance->type = 2;
- $saveBalance->status = 1;
- $saveBalance->save();
- return response()->json([
- 'success' => true,
- 'error' => 'false',
- 'user_id' => $this->user_id,
- 'lelang_id' => $this->lelang_id,
- 'harga_bid' => $this->harga_bid
- ],
- $this->successStatus);
- } else {
- $changeBalance = IkanPay::where('user_id', $this->user_id)
- ->where('lelang_id', $this->lelang_id)
- ->orderBy('nominal', 'ASC')
- ->first();
- // foreach ($changeBalance as $cb) {
- $checkID = $changeBalance->id;
- $ipay = IkanPay::find($checkID);
- $ipay->status = 3;
- $ipay->update();
- $saveBalance = new IkanPay();
- $saveBalance->user_id = $this->user_id;
- $saveBalance->lelang_id = $this->lelang_id;
- $saveBalance->nominal = abs($changeBalance->nominal);
- $saveBalance->transaction = "Cancel Tawar Ikan #" . $this->lelang_id;
- $saveBalance->type = 2;
- $saveBalance->status = 1;
- $saveBalance->save();
- $checkProses = ProsesLelang::where('user_id', $this->user_id)
- ->where('lelang_id', $this->lelang_id)
- ->orderBy('created_at', 'DESC')
- ->first();
- $checkPro = $checkProses->id;
- $cps = ProsesLelang::find($checkPro);
- $cps->status = 3;
- $cps->update();
- $proses = new ProsesLelang();
- $proses->user_id = $this->user_id;
- $proses->lelang_id = $this->lelang_id;
- $proses->harga_bid = $this->harga_bid;
- $proses->status = 2;
- $proses->save();
- $saveBalance = new IkanPay();
- $saveBalance->user_id = $this->user_id;
- $saveBalance->lelang_id = $this->lelang_id;
- $saveBalance->nominal = -$this->harga_bid;
- $saveBalance->transaction = "Proses Tawar Ikan #" . $this->lelang_id;
- $saveBalance->type = 1;
- $saveBalance->status = 1;
- $saveBalance->save();
- // }
- return response()->json([
- 'success' => true,
- 'error' => 'false',
- 'user_id' => $this->user_id,
- 'lelang_id' => $this->lelang_id,
- 'harga_bid' => $this->harga_bid
- ],
- $this->successStatus);
- }
- }
- }
Add Comment
Please, Sign In to add comment