如何在函数中优化in_array?
我尝试过isset,但是不起作用。
public function index() {
$this->checkPermission();
$banks = Bankaccount::all();
$clients = Role::whereRoleSlug('client')
->firstOrFail()
->users;
$projects = Project::whereProjectStatus('active')->get();
$adminBanks = Bankaccount::where('bank_user_id','=',null)->get();
$roles = Role::whereIn('role_slug',['administrator','accountant'])
->pluck('role_id')
->toArray();
$cash = 0;
$payments = Payment::wherePaymentBy('cash')->get();
/*->filter(function ($payment) {
return in_array($payment->activity->activityBy->id,$roles)
});*/
foreach ($payments as $index => $payment) {
if(in_array($payment->activity->activityBy->role_id,$roles)) {
if(in_array(strtolower($payment->payment_purpose),['employee_transfer','employee_refund','vendor_payment','vendor_refund','loan_payment','salary','office_deposit'])) {
$cash -= $payment->payment_amount;
}
else {
$cash += $payment->payment_amount;
}
}
}
// foreach ($payments as $index => $payment) {
// if (isset($payment->activity->activityBy->role_id,$roles)) {
// if (isset($payment['employee_transfer'],$payment['employee_refund'],$payment['vendor_payment'],$payment['vendor_refund'],$payment['loan_payment'],$payment['salary'],$payment['office_deposit'])) {
// $cash -= $payment->payment_amount;
//// dd($roles);
// } else {
// $cash += $payment->payment_amount;
// }
// }
// }
return view('admin.accounting.banks.index')
->with([
'adminBanks' => $adminBanks,'banks' => $banks,'clients' => $clients,'projects' => $projects,'cash' => $cash
]);
}
public function bankDetails($id) {
if(!Auth::user()->isAdmin() && !Auth::user()->isaccountant()) {
return redirectBackWithNotification('error','You are not authorised!');
}
if($id == 'cash') {
$projects = Project::select(['bsoft_projects.project_id','bsoft_projects.project_name'])->get();
return view('admin.accounting.banks.show')
->with([
'projects' => $projects
]);
}
$bank = Bankaccount::findOrFail($id);
if(!$bank->user) {
$payments = Payment::where('payment_from_bank_account',$bank->bank_id)
->orWhere('payment_to_bank_account',$bank->bank_id)
->get();
$balance = $bank->bank_balance;
}
else {
$payments = Payment::where('payment_from_bank_account',$bank->bank_id)
->orWhere('payment_to_user',$bank->user->id)
->orWhere('payment_from_user',$bank->user->id)
->get();
$balance = 0;
$exp = 0;
$inc = 0;
foreach ($payments as $payment) {
if($payment->payment_from_user == $bank->user->id) {
$exp += $payment->payment_amount;
}
elseif ($payment->payment_to_user == $bank->user->id) {
$inc += $payment->payment_amount;
}
}
$balance = $inc - $exp;
}
return view('admin.accounting.banks.show')
->with([
'bank' => $bank,'payments' => $payments,'balance' => $balance
]);
}
public function rechargeFromCustomer(Request $request) {
$this->checkPermission();
$validator = Validator::make($request->all(),[
'user_id' => ['required','numeric'],'bank_id' => ['nullable','project_id' => ['required','type' => ['required','string'],'amount' => ['required','date' => ['required','date'],]);
if($validator->fails()) {
return redirectBackWithValidationError($validator);
}
$payment = createNewPayment([
'type' => 'credit','to_user' => null,'from_user' => $request->post('user_id'),'to_bank_account' => (strtolower($request->post('type')) === 'bank' || strtolower($request->post('type')) === 'check')
? $request->post('bank_id') : null,'from_bank_account' => null,'amount' => $request->post('amount'),'project' => $request->post('project_id'),'purpose' => 'project_money','by' => $request->post('type'),'date' => $request->post('date'),'image' => null,'note' => $request->post('note')
]);
if(!$payment) {
return redirectBackWithNotification();
}
if(strtolower($request->post('type')) === 'bank' || $request->post('type') == 'check') {
$offBank = Bankaccount::findOrFail($request->post('bank_id'));
$offBank->bank_balance = $offBank->bank_balance + (float) $request->post('amount');
$offBank->save();
}
return redirectBackWithNotification('success','Client Money Successfully Received!');
}
public function storeaccount(Request $request) {
$this->checkPermission();
$validator = Validator::make($request->all(),[
'user_id' => ['nullable','name' => ['required','number' => ['required','bank' => ['required','branch' => ['required','balance' => ['required','accountFor' => ['required',]);
if($validator->fails()) {
return redirectBackWithValidationError($validator);
}
$bank = new Bankaccount();
$bank->bank_user_id = ($request->post('user_id')) ? $request->post('user_id') : null;
$bank->bank_account_name = $request->post('name');
$bank->bank_account_no = $request->post('number');
$bank->bank_name = $request->post('bank');
$bank->bank_branch = $request->post('branch');
$bank->bank_balance = $request->post('balance');
if(!$bank->save()) {
return redirectBackWithNotification();
}
addactivity('bank',$bank->bank_id,'Bank account Added');
return redirectBackWithNotification('success','Bank account Successfully Added!');
}
public function transferToEmployee(Request $request) {
$this->checkPermission();
$validator = Validator::make($request->all(),[
'employee_id' => ['required','bank_id' => ['nullable','project_id' => ['required','type' => ['required','amount' => ['required','date' => ['required',]);
if($validator->fails()) {
return redirectBackWithValidationError($validator);
}
if(strtolower($request->post('type')) !== 'cash' && $request->post('bank_id') === null) {
return redirectBackWithNotification('error','Bank account must be selected!');
}
$employee_id = $request->post('employee_id');
$employee_bank_id = null;
if(strpos($request->post('employee_id'),'@') !== false) {
$employee_id = Str::before($request->post('employee_id'),'@');
$employee_bank_id = Str::after($request->post('employee_id'),'@');
}
$paymentData = [
'type' => null,'to_user' => $employee_id,'from_user' => Auth::id(),'to_bank_account' => $employee_bank_id,'from_bank_account' => (strtolower($request->post('type')) !== 'cash') ? $request->post('bank_id') : null,'purpose' => 'employee_transfer','by' => strtolower($request->post('type')),'note' =>$request->post('note')
];
if(!createNewPayment($paymentData)) {
return redirectBackWithNotification();
}
if(strtolower($request->post('type')) === 'bank' || $request->post('payment_by') == 'check') {
$officeBank = Bankaccount::findOrFail($request->post('bank_id'));
$officeBank->bank_balance = $officeBank->bank_balance - (float) $request->post('amount');
$officeBank->save();
$employeeBank = Bankaccount::findOrFail($employee_bank_id);
$employeeBank->bank_balance = $employeeBank->bank_balance + (float) $request->post('amount');
$employeeBank->save();
}
return redirectBackWithNotification('success','Transfer successfully made!');
}
public function transferToOffice(Request $request) {
$this->checkPermission();
$validator = Validator::make($request->all(),]);
if($validator->fails()) {
return redirectBackWithValidationError($validator);
}
if(strtolower($request->post('type')) !== 'bank' && $request->post('bank_id') === null) {
return redirectBackWithNotification('error','@');
}
else {
$employee_bank_id = $this->createAutoGeneratedaccount($employee_id)->bank_id;
}
$paymentData = [
'type' => null,'from_user' => null,'from_bank_account' => (strtolower($request->post('type')) === 'bank') ? $request->post('bank_id') : null,'amount' => $request->post('amount') - ($request->post('amount') * 2),'purpose' => 'employee_refund','note' =>$request->post('note')
];
if(!createNewPayment($paymentData)) {
return redirectBackWithNotification();
}
if(strtolower($request->post('type')) === 'bank' || $request->post('payment_by') == 'check') {
$officeBank = Bankaccount::findOrFail($request->post('bank_id'));
$officeBank->bank_balance = $officeBank->bank_balance + (float) $request->post('amount');
$officeBank->save();
}
$employeeBank = Bankaccount::findOrFail($employee_bank_id);
$employeeBank->bank_balance = $employeeBank->bank_balance - (float) $request->post('amount');
$employeeBank->save();
return redirectBackWithNotification('success','Money successfully refunded!');
}
public function withdrawFromBank(Request $request) {
$this->checkPermission();
$validator = Validator::make($request->all(),[
'bank_id' => ['nullable',]);
if($validator->fails()) {
return redirectBackWithValidationError($validator);
}
$paymentData = [
'type' => null,'to_bank_account' => null,'from_bank_account' => $request->post('bank_id'),'project' => null,'purpose' => 'office_withdraw','by' => 'cash','note' =>$request->post('note')
];
if(!createNewPayment($paymentData)) {
return redirectBackWithNotification();
}
$officeBank = Bankaccount::findOrFail($request->post('bank_id'));
$officeBank->bank_balance = $officeBank->bank_balance - (float) $request->post('amount');
$officeBank->save();
return redirectBackWithNotification('success','Money successfully Withdrawn!');
}
public function depositToBank(Request $request) {
$this->checkPermission();
$validator = Validator::make($request->all(),'to_bank_account' => $request->post('bank_id'),'purpose' => 'office_deposit','note' =>$request->post('note')
];
if(!createNewPayment($paymentData)) {
return redirectBackWithNotification();
}
$officeBank = Bankaccount::findOrFail($request->post('bank_id'));
$officeBank->bank_balance = $officeBank->bank_balance + (float) $request->post('amount');
$officeBank->save();
return redirectBackWithNotification('success','Money successfully Deposited!');
}
public function income() {
$this->checkPermission();
$projects = Project::select(['bsoft_projects.project_id','bsoft_projects.project_name'])->get();
return view('admin.accounting.income')
->with([
'projects' => $projects
]);
}
public function expense() {
$this->checkPermission();
$projects = Project::select(['bsoft_projects.project_id','bsoft_projects.project_name'])->get();
return view('admin.accounting.expense')
->with([
'projects' => $projects
]);
}
public function getUsers(Request $request) {
$this->checkPermission();
$users = Role::whereRoleSlug($request->post('type'))->firstOrFail()
->users;
return view('admin.accounting.banks.ajax-users')
->with([
'users' => $users
]);
}
public function getclientProjects(Request $request) {
$this->checkPermission();
$projects = User::findOrFail($request->post('client_id'))->clientProjects()
->where('project_status','active')->get();
return view('admin.accounting.banks.ajax-projects')
->with([
'projects' => $projects
]);
}
public function getManagers(Request $request) {
$this->checkPermission();
$roles = Role::whereIn('role_slug',['manager'])
->pluck('role_id')
->toArray();
$users = Project::findOrFail($request->post('project_id'))->employees()
->whereIn('role_id',$roles)
->get();
return view('admin.accounting.banks.ajax-employees')
->with([
'users' => $users
]);
}
/**
* @return bool|\Illuminate\Http\RedirectResponse
*/
protected function checkPermission() {
$role = Auth::user()->role->role_slug;
if($role == 'administrator' || $role == 'accountant') {
return true;
}
return redirectBackWithNotification('error','Sorry! You Are Not Authorised!.');
}
protected function createAutoGeneratedaccount(int $id) {
$employee = User::findOrFail($id);
$bank = new Bankaccount();
$bank->bank_account_name = 'Auto Generated Bank account!';
$bank->bank_user_id = $employee->id;
$bank->save();
return $bank;
}
加载大约需要4 / 5m,我在localhost中尝试,但显示60s超时错误。我想在30s内加载此页面。我无法在in_array($payment->activity->activityBy->role_id,$roles)
和
in_array(strtolower($payment->payment_purpose),'office_deposit'])
主要是这两个如果发生加载时间错误。 我可以优化$ payment中的in_array吗?$ roles中的role_id有什么问题?以及为什么需要这么多时间来加载我的页面?
还有什么方法可以优化bankDetails功能,因为它还需要3 / 4m来加载全部,贷款和按项目的详细信息?