CasperSecurity
<?php
namespace App\Http\Livewire\Hrms;
use App\Imports\LeaveRequestImport;
use App\Imports\OdRequestImport;
use App\Imports\OtRequestImport;
use App\Imports\TravelRequestImport;
use App\Exports\EmployeeWageExport;
use App\Imports\EmployeeWageImport;
use Illuminate\Http\Request;
use Livewire\Component;
use Livewire\WithFileUploads;
use Livewire\WithPagination;
use Maatwebsite\Excel\Facades\Excel;
class ImportDocuemntsLivewire extends Component
{
use WithPagination;
use WithFileUploads;
protected $paginationTheme = 'bootstrap';
public $employee_id;
public $search;
public $perPage = 10;
public $orderBy = 'id';
public $orderAsc = '1';
public $leave_employee;
public $od_employee;
public $ot_employee;
public $travel_employee;
public $employee_wages;
public $month_year;
public function exportWage()
{
if (!$this->month_year) {
$this->dispatchBrowserEvent('showsuccessmsg',[
'type'=>'error',
'message'=>"Enter Month first"
]);
return; // You can return early if the validation fails
}
// Split the month_year value into month and year
list($year, $month)= explode('-', $this->month_year);
// Create the export instance with the selected month and year
$export = new EmployeeWageExport($month, $year);
// Download the Excel file
$this->dispatchBrowserEvent('showsuccessmsg',[
'type'=>'success',
'message'=>"The format has been exported"
]);
$this->dispatchBrowserEvent('closeModal');
return Excel::download($export, 'employee_wage_details.xlsx');
}
public function importWageSheet()
{
// Ensure the file is provided
if (!$this->month_year) {
session()->flash('error', 'Please select a month.');
return;
}
// Split the month_year value into month and year
list($year, $month) = explode('-', $this->month_year);
if (!$this->employee_wages) {
return response()->json(['message' => 'No file provided'], 400);
}
// Check if the month_year is provided
if (!$this->month_year) {
return response()->json(['message' => 'Month and Year are required'], 400);
}
// Extract month and year from the month_year input (YYYY-MM)
list($year, $month) = explode('-', $this->month_year);
// Ensure the file has a valid extension
$file = $this->employee_wages;
$extension = $file->getClientOriginalExtension();
if (in_array($extension, ['xlsx', 'xls', 'csv'])) {
// Pass month and year along with the file to the import class
Excel::import(new EmployeeWageImport($month, $year), $file);
return response()->json(['message' => 'Employee wages imported successfully']);
} else {
return response()->json(['message' => 'Invalid file type. Only .xlsx, .xls, or .csv are allowed'], 400);
}
}
public function leaveUpload(){
$this->validate(
[
'leave_employee'=>'required'
],
[
'leave_employee.required'=>'This filed is required'
]
);
$response=Excel::import(new LeaveRequestImport(), $this->leave_employee);
if ($response) {
//session()->flash('success', $this->successmsg );
$this->dispatchBrowserEvent('closemodal');
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'success',
'title' => 'DATA UPLOADED SUCCESSFULLY',
'message' => 'LeaveRequest Uploaded Successfully',
]);
} else {
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'error',
'title' => 'Error',
'message' => 'Something went wrong',
]);
}
}
public function odUpload(){
$this->validate(
[
'od_employee'=>'required'
],
[
'od_employee.required'=>'This filed is required'
]
);
$response=Excel::import(new OdRequestImport(), $this->od_employee);
if ($response) {
//session()->flash('success', $this->successmsg );
$this->dispatchBrowserEvent('closemodal');
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'success',
'title' => 'DATA UPLOADED SUCCESSFULLY',
'message' => 'OdRequest Uploaded Successfully',
]);
} else {
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'error',
'title' => 'Error',
'message' => 'Something went wrong',
]);
}
}
public function otUpload(){
$this->validate(
[
'ot_employee'=>'required'
],
[
'ot_employee.required'=>'This filed is required'
]
);
$response=Excel::import(new OtRequestImport(), $this->ot_employee);
if ($response) {
//session()->flash('success', $this->successmsg );
$this->dispatchBrowserEvent('closemodal');
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'success',
'title' => 'DATA UPLOADED SUCCESSFULLY',
'message' => 'OtRequest Uploaded Successfully',
]);
} else {
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'error',
'title' => 'Error',
'message' => 'Something went wrong',
]);
}
}
public function travelUpload(){
$this->validate(
[
'travel_employee'=>'required'
],
[
'travel_employee.required'=>'This filed is required'
]
);
$response=Excel::import(new TravelRequestImport(), $this->travel_employee);
if ($response) {
//session()->flash('success', $this->successmsg );
$this->dispatchBrowserEvent('closemodal');
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'success',
'title' => 'DATA UPLOADED SUCCESSFULLY',
'message' => 'TravelRequest Uploaded Successfully',
]);
} else {
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'error',
'title' => 'Error',
'message' => 'Something went wrong',
]);
}
}
public function render()
{
return view('livewire.hrms.import-docuemnts-livewire');
}
}