|
Server : Apache/2.4.41 (Ubuntu) System : Linux vmi1525618.contaboserver.net 5.4.0-105-generic #119-Ubuntu SMP Mon Mar 7 18:49:24 UTC 2022 x86_64 User : www-data ( 33) PHP Version : 8.2.12 Disable Function : NONE Directory : /var/www/peoplebee.in/___accounts-admin/app/Exports/ |
Upload File : |
<?php
namespace App\Exports;
use Illuminate\Support\Facades\DB;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithStyles;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use App\Models\HRMS\Salary;
class TaxReport implements FromCollection, WithHeadings, WithStyles
{
private $admin_id, $department_id, $branch_id, $start_date, $end_date;
public function __construct($admin_id)
{
$this->admin_id = $admin_id;
}
public function collection()
{
$this->department_id = isset($_GET["department_id"]) ? $_GET["department_id"] : null;
$this->branch_id = isset($_GET["branch_id"]) ? $_GET["branch_id"] : null;
$this->start_date = isset($_GET["date_from"]) ? $_GET["date_from"] : null;
$this->end_date = isset($_GET["date_to"]) ? $_GET["date_to"] : null;
$query = Salary::where("salaries.admin_id", $this->admin_id)
->where("salaries.status", "paid")
->join('employees', 'employees.user_unique_id', '=', 'salaries.user_unique_id')
->select(
'employees.employee_id as employee_code',
'employees.name as employee_name',
'salaries.tax'
);
if ($this->department_id) {
$query->where('salaries.department_id', $this->department_id);
}
if ($this->branch_id) {
$query->where('salaries.branch_id', $this->branch_id);
}
if ($this->start_date && $this->end_date) {
$query->whereDate('salaries.created_at', '>=', $this->start_date)
->whereDate('salaries.created_at', '<=', $this->end_date);
}
$salaries = $query->orderBy('salaries.created_at', 'DESC')->get();
// Add total row at the end
$totalTax = $salaries->sum('tax');
$salaries->push([
'employee_code' => 'TOTAL',
'employee_name' => '',
'tax' => $totalTax
]);
return $salaries;
}
public function headings(): array
{
return [
"Employee ID",
"Employee Name",
"Tax Amount"
];
}
public function styles(Worksheet $sheet)
{
// Get the last row number
$lastRow = $sheet->getHighestRow();
return [
// Style the header row
1 => [
'font' => ['bold' => true],
'fill' => [
'fillType' => \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID,
'startColor' => ['argb' => 'FFD9D9D9']
]
],
// Style the total row
$lastRow => [
'font' => ['bold' => true],
'fill' => [
'fillType' => \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID,
'startColor' => ['argb' => 'FFF2F2F2']
]
],
// Format tax amount column
'C' => [
'numberFormat' => ['formatCode' => '#,##0.00']
],
// Format total tax amount cell
'C' . $lastRow => [
'numberFormat' => ['formatCode' => '#,##0.00'],
'font' => ['bold' => true]
]
];
}
}