SHARE
TWEET

Untitled

a guest Dec 3rd, 2019 78 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public function update(Request $request, Transaction $transaction)
  2.     {
  3.         $request->validate([
  4.             'status' => 'required',
  5.         ]);
  6.        
  7.         \DB::beginTransaction();
  8.         $transaction->update($request->all());
  9.        
  10.         if ($transaction->status == 'TERIMA') {
  11.             $point = $this->rangePoint($transaction->total_harga);
  12.             $coin = $this->rangeCoin($transaction->total_harga);
  13.             if($transaction->user->point){
  14.                 $transaction->user->point->update([
  15.                     'point'=>$transaction->user->point->point + $point,
  16.                 ]);
  17.             } else {
  18.                 $transaction->user->point()->create([
  19.                     'point'=>$point
  20.                 ]);
  21.             }
  22.             if ($transaction->user->coin) {
  23.                 $transaction->user->coin->update([
  24.                     'coin'=>$transaction->user->coin->coin + $coin,
  25.                 ]);
  26.             } else {
  27.                 $transaction->user->coin()->create([
  28.                     'coin'=>$coin,
  29.                 ]);
  30.             }
  31.             HistoryCoin::create([
  32.                 'user_id' =>$transaction->user->id,
  33.                 'transaction_id'=>$transaction->id,
  34.                 'type'=>'TRANSAKSI',
  35.                 'status'=>'income',
  36.                 'coin'=> $coin,
  37.                 ]);
  38.             HistoryPoint::create([
  39.                 'user_id' => $transaction->user->id,
  40.                 'transaction_id' => $transaction->id,
  41.                 'type' => 'TRANSAKSI',
  42.                 'status' => 'income',
  43.                 'point' => $point,
  44.                 ]);
  45.                
  46.             $this->notif($transaction->user->fcm, 'Transaksi anda telah diterima dan selesai');
  47.         } else {
  48.             if ($transaction->status != 'BATAL') {
  49.                 if (($transaction->status == 'PROSES_KIRIM') && empty($transaction->resi)) return response()->json(['errors' => ['status' => ['Resi harus diupdate']]], 422);
  50.                 $this->notif($transaction->user->fcm, 'Transaksi anda sedang dalam ' . str_replace('_', ' ', $request->status) . ($transaction->status == 'PROSES_KIRIM' ? (' dengan Resi ' . $transaction->resi) : ''));
  51.             }
  52.             if ($transaction->status == 'BATAL') {
  53.                 $this->notif($transaction->user->fcm, 'Transaksi anda dibatalkan');
  54.                 foreach ($transaction->details as $key => $value) {
  55.                     $value->product->update([
  56.                         'stock' => $value->product->stock + $value->total_item
  57.                     ]);
  58.                 }
  59.             }
  60.         }
  61.         \DB::commit();
  62.         return response()->json(['success' => 'Data ' . $transaction->kode_trx . ' berhasil diupdate status  ' . $transaction->status ]);
  63.     }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top