| Server IP : 103.161.17.216 / Your IP : 216.73.216.1 Web Server : nginx/1.18.0 System : Linux tipsysaigoncharming 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64 User : www-data ( 33) PHP Version : 7.4.3-4ubuntu2.29 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare, MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : ON Directory : /var/www/app.ansachsongkhoe.net/app/ |
Upload File : |
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use App\Utils\Util;
use DB;
use App\BusinessLocation;
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, $closed = false, $show_balance = false)
{
$query = Account::where('business_id', $business_id);
$permitted_locations = auth()->user()->permitted_locations();
$account_ids = [];
if ($permitted_locations != 'all') {
$locations = BusinessLocation::where('business_id', $business_id)
->whereIn('id', $permitted_locations)
->get();
foreach ($locations as $location) {
if (!empty($location->default_payment_accounts)) {
$default_payment_accounts = json_decode($location->default_payment_accounts, true);
foreach ($default_payment_accounts as $key => $account) {
if (!empty($account['is_enabled']) && !empty($account['account'])) {
$account_ids[] = $account['account'];
}
}
}
}
$account_ids = array_unique($account_ids);
}
if ($permitted_locations != 'all') {
$query->whereIn('accounts.id', $account_ids);
}
$can_access_account = auth()->user()->can('account.access');
if ($can_access_account && $show_balance) {
$query->leftjoin('account_transactions as AT', function ($join) {
$join->on('AT.account_id', '=', 'accounts.id');
$join->whereNull('AT.deleted_at');
})
->select('accounts.name',
'accounts.id',
DB::raw("SUM( IF(AT.type='credit', amount, -1*amount) ) as balance")
)->groupBy('accounts.id');
}
if (!$closed) {
$query->where('is_closed', 0);
}
$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');
}
}