Created
August 20, 2020 18:54
-
-
Save djaiss/8b0420330e378994bbe7508a368b66e1 to your computer and use it in GitHub Desktop.
Exemple of service in PHP for Laravel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Services\Company\Employee\Expense; | |
use Carbon\Carbon; | |
use App\Helpers\MoneyHelper; | |
use App\Jobs\NotifyEmployee; | |
use App\Jobs\LogAccountAudit; | |
use App\Services\BaseService; | |
use App\Jobs\LogEmployeeAudit; | |
use App\Models\Company\Expense; | |
use App\Models\Company\Employee; | |
use Illuminate\Support\Collection; | |
use Illuminate\Support\Facades\Log; | |
use App\Models\Company\ExpenseCategory; | |
use App\Jobs\ConvertAmountFromOneCurrencyToCompanyCurrency; | |
class CreateExpense extends BaseService | |
{ | |
private Expense $expense; | |
private Employee $employee; | |
private array $data; | |
private Collection $managers; | |
/** | |
* Get the validation rules that apply to the service. | |
* | |
* @return array | |
*/ | |
public function rules(): array | |
{ | |
return [ | |
'company_id' => 'required|integer|exists:companies,id', | |
'author_id' => 'required|integer|exists:employees,id', | |
'employee_id' => 'required|integer|exists:employees,id', | |
'expense_category_id' => 'nullable|integer|exists:expense_categories,id', | |
'title' => 'required|string|max:255', | |
'amount' => 'required|integer', | |
'currency' => 'required|string|max:255', | |
'description' => 'nullable|string|max:65535', | |
'expensed_at' => 'required|date', | |
'is_dummy' => 'nullable|boolean', | |
]; | |
} | |
/** | |
* Create an expense. | |
* | |
* @param array $data | |
* @return Expense | |
*/ | |
public function execute(array $data): Expense | |
{ | |
$this->data = $data; | |
$this->saveExpense(); | |
$this->log(); | |
return $this->expense; | |
} | |
/** | |
* Actually create the expense. | |
*/ | |
private function saveExpense(): void | |
{ | |
$this->expense = Expense::create([ | |
'company_id' => $this->data['company_id'], | |
'employee_id' => $this->data['employee_id'], | |
'employee_name' => $this->employee->name, | |
'expense_category_id' => $this->valueOrNull($this->data, 'expense_category_id'), | |
'title' => $this->data['title'], | |
'amount' => $this->data['amount'], | |
'currency' => $this->data['currency'], | |
'description' => $this->valueOrNull($this->data, 'description'), | |
'expensed_at' => $this->data['expensed_at'], | |
'status' => $this->managers->count() > 0 ? Expense::AWAITING_MANAGER_APPROVAL : Expense::AWAITING_ACCOUTING_APPROVAL, | |
'is_dummy' => $this->valueOrFalse($this->data, 'is_dummy'), | |
]); | |
} | |
/** | |
* Add audit logs. | |
*/ | |
private function log(): void | |
{ | |
LogAccountAudit::dispatch([ | |
'company_id' => $this->data['company_id'], | |
'action' => 'expense_created', | |
'author_id' => $this->author->id, | |
'author_name' => $this->author->name, | |
'audited_at' => Carbon::now(), | |
'objects' => json_encode([ | |
'expense_id' => $this->expense->id, | |
'expense_title' => $this->expense->title, | |
'expense_amount' => MoneyHelper::format($this->expense->amount, $this->expense->currency), | |
'expensed_at' => $this->expense->expensed_at, | |
]), | |
'is_dummy' => $this->valueOrFalse($this->data, 'is_dummy'), | |
])->onQueue('low'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment