CasperSecurity
<?php
namespace App\Http\Livewire\Report;
use App\Models\Designation;
use App\Models\EmployeeRegistration;
use App\Models\Organisation;
use App\Models\PayrollItems;
use App\Models\SalaryCalculation;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Livewire\Component;
class SalaryReportLivewire extends Component
{
public $search;
public $perPage = 10;
public $orderBy = 'id';
public $orderAsc = '1';
public $monthYear;
public $month;
public $year;
public $filter_by;
public $employee_name;
public $designation_name;
public function closemodalclick()
{
$this->resetValidation();
$this->datatable = [];
}
public function mount()
{
$this->monthYear = Carbon::now()->format('Y-m');
$this->parseMonthYear();
$this->orderBy = 'id';
}
public function updated($propertyName)
{
if ($propertyName === 'monthYear') {
$this->parseMonthYear();
}
}
private function parseMonthYear()
{
$date = Carbon::createFromFormat('Y-m', $this->monthYear);
$this->month = $date->format('m');
$this->year = $date->format('Y');
}
public function read()
{
$salary_calculations = SalaryCalculation::select(
'salary_calculations.*',
'employee_registrations.employee_id',
'employee_registrations.name',
'designations.designation_name'
)
->join('employee_registrations', 'salary_calculations.employee_id', '=', 'employee_registrations.id')
->join('designations', 'employee_registrations.designation', '=', 'designations.id')
->where('salary_calculations.month', $this->month)
->where('salary_calculations.year', $this->year)
->where('salary_calculations.status', '1');
if ($this->search != null) {
if ($this->filter_by == 'name') {
$salary_calculations->where(function($query) {
$query->where('employee_registrations.name', 'like', '%' . $this->search . '%');
});
} elseif ($this->filter_by == 'designation_name') {
$salary_calculations->where(function($query) {
$query->where('designations.designation_name', 'like', '%' . $this->search . '%');
});
}
}
$salary_calculations = $salary_calculations->get();
$totals = [];
$grandTotals = [
'Total Earnings' => 0,
'Total Deductions' => 0,
'Net Pay' => 0
];
foreach ($salary_calculations as $record) {
$emp_id = $record->employee_id;
$amount = $record->amount;
$payroll_item_name = $record->payroll_item_name;
if (!isset($totals[$emp_id])) {
$totals[$emp_id] = [
'employee_id' => $emp_id,
'name' => $record->name,
'designation_name' => $record->designation_name ?? 'N/A',
'Total Earnings' => 0,
'Total Deductions' => 0,
];
}
$pay_component = PayrollItems::where('payroll_item_short_name', $payroll_item_name)->latest()->first();
if ($pay_component) {
if ($pay_component->payroll_item_type === 'EARNING') {
$totals[$emp_id]['Total Earnings'] += $amount;
$grandTotals['Total Earnings'] += $amount;
} elseif ($pay_component->payroll_item_type === 'DEDUCTION') {
$totals[$emp_id]['Total Deductions'] += $amount;
$grandTotals['Total Deductions'] += $amount;
}
}
}
foreach ($totals as $emp_id => &$data) {
$data['in_hand'] = ($data['Total Earnings'] ?? 0) - ($data['Total Deductions'] ?? 0);
$grandTotals['Net Pay'] += $data['in_hand'];
}
return [
'datatable' => array_values($totals),
'grandTotals' => $grandTotals
];
}
public function render()
{
$results = $this->read();
$datatable = $results['datatable'];
$grandTotals = $results['grandTotals'];
$org = Organisation::latest()->first();
return view('livewire.report.salary-report-livewire', compact('datatable', 'org', 'grandTotals'));
}
}