CasperSecurity
<?php
namespace App\Http\Livewire\Report;
use App\Models\Organisation;
use App\Models\PayrollItems;
use App\Models\SalaryCalculation;
use Carbon\Carbon;
use Livewire\Component;
class EarningReportLivewire extends Component
{
public $search;
public $perPage = 10;
public $orderBy = 'id';
public $orderAsc = '1';
public $from_date, $to_date;
public function mount()
{
$this->from_date = Carbon::now()->startOfMonth()->format('Y-m-d');
$this->to_date = Carbon::now()->endOfMonth()->format('Y-m-d');
$this->orderBy = 'id';
}
public function closemodalclick()
{
$this->resetValidation();
$this->datatable = [];
}
public function read()
{
$query = 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')
->whereBetween('salary_calculations.created_at', [$this->from_date, $this->to_date])
->where('salary_calculations.status', '1');
if ($this->search !== null) {
$query->where(function ($q) {
$q->where('employee_registrations.name', 'like', '%' . $this->search . '%')
->orWhere('employee_registrations.employee_id', 'like', '%' . $this->search . '%');
});
}
$salary_calculations = $query->get();
$totals = [];
$grandTotalEarnings = 0;
//dd($grandTotalEarnings);
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,
'earnings' => [],
];
}
$pay_component = PayrollItems::where('payroll_item_short_name', $payroll_item_name)->first();
if ($pay_component && $pay_component->payroll_item_type === 'EARNING') {
if (!isset($totals[$emp_id]['earnings'][$pay_component->payroll_item])) {
$totals[$emp_id]['earnings'][$pay_component->payroll_item] = 0;
}
$totals[$emp_id]['earnings'][$pay_component->payroll_item] += $amount;
}
}
foreach ($totals as & $data) {
$data['Total Earnings'] = array_sum($data['earnings']);
$grandTotalEarnings += $data['Total Earnings'];
}
return [
'datatable' => array_values($totals),
'grandTotalEarnings' => $grandTotalEarnings,
];
}
public function render()
{
$results = $this->read();
$datatable = $results['datatable'];
$grandTotalEarnings = $results['grandTotalEarnings'];
$org = Organisation::latest()->first();
return view('livewire.report.earning-report-livewire', compact('datatable', 'org','grandTotalEarnings'));
}
}