Root Zanli
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
home
/
o5t6x7pgljbm
/
public_html
/
app
/
Http
/
Controllers
/
Api
/
Filename :
SPWalletController.php
back
Copy
<?php namespace App\Http\Controllers\Api; use Illuminate\Http\Request; use App\Http\Controllers\Controller; use App\Models\SPWallet; use App\Models\SPWalletTransaction; use App\Models\User; use Exception; use Illuminate\Support\Facades\Log; use Helpers; use Illuminate\Support\Str; /** * !Important! * All functionality around SP wallet needs to be done using SPWalletController only. * SPWallet and SPWalletTransaction models should not be used outside of SPWalletController. */ class SPWalletController extends Controller { /** * returns SPWallet as array with balance, used to get from any other controller */ public function getSPWallet(User $user){ return $this->getSPWalletPvt($user)->toArray(); } public function getEarnedSp(User $user){ $wallet = $this->getSPWalletPvt($user); if(isset($wallet)) return $wallet->getEarnedSp(); else return 0; } /** * returns SPWallet Object with balance */ private function getSPWalletPvt(User $user){ $spWallet = SPWallet::where('user_id', $user->user_id)->first(); if(!isset($spWallet)){ throw new Exception("SPWallet not found for user_id: $user->user_id"); } $spWallet['balance'] = 0; $spWalletTransaction = SPWalletTransaction::where('sp_wallet_id', $spWallet->sp_wallet_id) ->orderBy('created_at', 'desc') ->first(); if(isset($spWalletTransaction)) $spWallet['balance'] = $spWalletTransaction->balance; return $spWallet; } public function canCreditSP(User $user, $points){ $spWallet = $this->getSPWalletPvt($user); if($spWallet->balance >= $points) return true; else return false; } /** * $transactionDetails = array( * 'transaction_title', 'ref_table', 'ref_table_id', 'description' * ); */ public function creditSP(User $user, $points, $transactionDetails = array()){ $spWallet = $this->getSPWalletPvt($user); $transactionId = Str::uuid()->toString(); $spWalletTransaction = SPWalletTransaction::create([ 'transaction_id' => $transactionId, 'sp_wallet_id' => $spWallet->sp_wallet_id, 'transaction_title' => $transactionDetails['transaction_title'], 'transaction_type' => 'CREDIT', 'ref_table' => $transactionDetails['ref_table'], 'ref_table_id' => $transactionDetails['ref_table_id'], 'sp' => $points, 'sp_credit' => $points, 'sp_debit' => 0, 'balance' => ($spWallet->balance - $points), 'entry_date_time'=> date('Y-m-d H:i:s'), 'description' => $transactionDetails['description'], ]); $spWalletArray = $spWallet->toArray(); $spWalletArray['spWalletTransaction'] = $spWalletTransaction->toArray(); $spWalletArray['balance'] = $spWalletTransaction->balance; return $spWalletArray; } /** * $transactionDetails = array( * 'transaction_title', 'ref_table', 'ref_table_id', 'description' * ); */ public function debitSP(User $user, $points, $transactionDetails = array()){ $spWallet = $this->getSPWalletPvt($user); $transactionId = Str::uuid()->toString(); $spWalletTransaction = SPWalletTransaction::create([ 'transaction_id' => $transactionId, 'sp_wallet_id' => $spWallet->sp_wallet_id, 'transaction_title' => $transactionDetails['transaction_title'], 'transaction_type' => 'DEBIT', 'ref_table' => $transactionDetails['ref_table'], 'ref_table_id' => $transactionDetails['ref_table_id'], 'sp' => $points, 'sp_credit' => 0, 'sp_debit' => $points, 'balance' => ($spWallet->balance + $points), 'entry_date_time'=> date('Y-m-d H:i:s'), 'description' => $transactionDetails['description'], ]); $spWalletArray['balance'] = $spWalletTransaction->balance; return $spWalletTransaction->toArray(); } } ?>