Edit file File name : Account.php Content :<?php namespace App; use App\Utils\Util; use DB; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Support\Facades\Log; class Account extends Model { use SoftDeletes; protected $guarded = ['id']; /** * The attributes that should be cast to native types. * * @var array */ protected $casts = [ 'account_details' => 'array', ]; public static function forDropdown($business_id, $prepend_none = false, $closed = false, $show_balance = false) { $query = Account::where('business_id', $business_id); $permitted_locations = auth()->user()->permitted_locations(); $permitted_accounts = auth()->user()->permitted_accounts(); // Initialize as empty arrays if null if (is_null($permitted_locations)) { $permitted_locations = []; } if (is_null($permitted_accounts)) { $permitted_accounts = []; } if ($permitted_accounts === 'all') { } else { if (!is_array($permitted_locations)) { $permitted_locations = json_decode($permitted_locations, true) ?: []; } if (!is_array($permitted_accounts)) { $permitted_accounts = json_decode($permitted_accounts, true) ?: []; } if (!empty($permitted_accounts)) { $query->whereIn('accounts.id', $permitted_accounts); } else { $query->whereIn('accounts.id', []); } } $can_access_account = auth()->user()->can('account.access'); if ($can_access_account && $show_balance) { $query->select('accounts.name', 'accounts.id', 'accounts.account_number', DB::raw("(SELECT SUM( IF(account_transactions.type='credit', amount, -1*amount) ) as balance from account_transactions where account_transactions.account_id = accounts.id AND deleted_at is NULL) as balance") ); } else { $query->select('accounts.name', 'accounts.id', 'accounts.account_number'); } if (!$closed) { $query->where('is_closed', 0); } $query->orderBy(DB::raw('CAST(account_number AS UNSIGNED)'), 'asc'); $accounts = $query->get(); $dropdown = []; // if ($prepend_none) { // $dropdown[''] = __('lang_v1.none'); // } $commonUtil = new Util; foreach ($accounts as $account) { $name = $account->name; if ($can_access_account && $show_balance) { $name .= ' ('.__('lang_v1.balance').': '.$commonUtil->num_f($account->balance).')'; } $dropdown[$account->id] = $name; } return $dropdown; } /** * Scope a query to only include not closed accounts. * * @param \Illuminate\Database\Eloquent\Builder $query * @return \Illuminate\Database\Eloquent\Builder */ public function scopeNotClosed($query) { return $query->where('is_closed', 0); } /** * Scope a query to only include non capital accounts. * * @param \Illuminate\Database\Eloquent\Builder $query * @return \Illuminate\Database\Eloquent\Builder */ // public function scopeNotCapital($query) // { // return $query->where(function ($q) { // $q->where('account_type', '!=', 'capital'); // $q->orWhereNull('account_type'); // }); // } public static function accountTypes() { return [ '' => __('account.not_applicable'), 'saving_current' => __('account.saving_current'), 'capital' => __('account.capital'), ]; } public function account_type() { return $this->belongsTo(\App\AccountType::class, 'account_type_id'); } } Save