CasperSecurity
<?php
namespace App\Http\Livewire\Hrms;
use Livewire\Component;
use App\Models\PayrollItems;
use App\Models\EmployeeAttendance;
use App\Models\Designation;
use App\Models\SettingsCity;
use App\Models\SalaryCalculation;
use App\Models\EmployeeRegistration;
use App\Models\EmployeeWageExport;
use App\Exports\SalaryFormatExport;
use Maatwebsite\Excel\Facades\Excel;
use Livewire\WithFileUploads;
use Carbon\CarbonPeriod;
use Illuminate\Support\Carbon;
use Livewire\WithPagination;
use App\Imports\SalaryCalculationImport;
class SalaryCalculationLivewire extends Component
{
use WithPagination;
use WithFileUploads;
protected $paginationTheme = 'bootstrap';
public $modelid;
public $showmodal=false;
public $emp_name,$emp_designation,$emp_location,$emp_attendance,$uan_no,$ESI_no,$totaldays,$netPayInWords,$salary_info;
public $earning_array=[];
public $deduction_array=[];
public $filter_type,$search_info,$month;
public $gross_salary=0;
public $total_deduction=0;
public $net_salary=0;
public $sal_arr=[];
//Messages
public $errorsearchmsg="Search field can not be empty";
public function updatedMonth($value){
session(['selected_month' => $value]);
}
public function showmodalclick($id,$empid,$month,$year){
$this->modelid=$id;
//dd($this->modelid);
$this->resetValidation();
$this->showmodal=true;
$this->slipGeneration($empid,$month,$year);
}
public function closemodalclick(){
$this->resetValidation();
$this->showmodal=false;
}
public function cleanSlip(){
$this->emp_name=null;
$this->emp_designation=null;
$this->emp_location=null;
$this->emp_attendance=null;
$this->uan_no=null;
$this->ESI_no=null;
}
public function salary_format()
{
return Excel::download(new SalaryFormatExport, 'salary_format.xlsx');
}
public function getIndianCurrency($number)
{
$decimal = round($number - ($no = floor($number)), 2) * 100;
$hundred = null;
$digits_length = strlen($no);
$i = 0;
$str = [];
$words = [
0 => "",
1 => "One",
2 => "Two",
3 => "Three",
4 => "Four",
5 => "Five",
6 => "Six",
7 => "Seven",
8 => "Eight",
9 => "Nine",
10 => "Ten",
11 => "Eleven",
12 => "Twelve",
13 => "Thirteen",
14 => "Fourteen",
15 => "Fifteen",
16 => "Sixteen",
17 => "Seventeen",
18 => "Eighteen",
19 => "Nineteen",
20 => "Twenty",
30 => "Thirty",
40 => "Forty",
50 => "Fifty",
60 => "Sixty",
70 => "Seventy",
80 => "Eighty",
90 => "Ninety",
];
$digits = ["", "Hundred", "Thousand", "Lakh", "Crore"];
while ($i < $digits_length) {
$divider = $i == 2 ? 10 : 100;
$number = floor($no % $divider);
$no = floor($no / $divider);
$i += $divider == 10 ? 1 : 2;
if ($number) {
$plural = ($counter = count($str)) && $number > 9 ? "s" : null;
$hundred = $counter == 1 && $str[0] ? " and " : null;
$str[] =
$number < 21
? $words[$number] .
" " .
$digits[$counter] .
$plural .
" " .
$hundred
: $words[floor($number / 10) * 10] .
" " .
$words[$number % 10] .
" " .
$digits[$counter] .
$plural .
" " .
$hundred;
} else {
$str[] = null;
}
}
$Rupees = implode("", array_reverse($str));
$paise =
$decimal > 0
? "." .
($words[$decimal / 10] . " " . $words[$decimal % 10]) .
" Paise"
: "";
return ($Rupees ? $Rupees . "Rupee " : "") . $paise;
}
public function slipGeneration($id,$month,$year){
$this->cleanSlip();
$emp=EmployeeRegistration::where('employee_id',$id)->first();
$emp_desg=Designation::where('id',$emp->designation)->first();
$city=SettingsCity::where('id',$emp->city_id)->first();
//$attendance=EmployeeAttendance
$this->deduction_array=[];
$this->earning_array=[];
$this->total_deduction=0;
$this->gross_salary=0;
$slip_details=SalaryCalculation::where('employee_id',$emp->id)->where('month',$month)
->where('year',$year)->get();
$this->emp_name=$emp->name;
$this->emp_designation=$emp_desg->designation_name;
$this->emp_location=$city->city_name;
$this->emp_attendance=0;
$this->uan_no=null;
$this->ESI_no=null;
$startDate = Carbon::createFromFormat('m', $month)->startOfMonth()->format('Y-m-d');
$endDate = Carbon::createFromFormat('m', $month)->endOfMonth()->format('Y-m-d');
$period = CarbonPeriod::create($startDate, $endDate);
$this->totaldays=0;
foreach ($period as $date) {
//dd($emp->employee_id);
$this->totaldays=$this->totaldays+1;
$ispresent=EmployeeAttendance::where('attendance_id',$emp->employee_id)
->where('attendance_date',$date->toDateString())
->first();
if($ispresent){
$this->emp_attendance=$this->emp_attendance+1;
}
}
foreach($slip_details as $slip){
$pay_component=PayrollItems::where('payroll_item_short_name',$slip->payroll_item_name)->first();
if($pay_component){
if($pay_component->payroll_item_type=='EARNING'){
array_push($this->earning_array, [
"component" => $pay_component->payroll_item,
"amount" => $slip->amount,
]);
$this->gross_salary=$this->gross_salary+$slip->amount;
}
elseif($pay_component->payroll_item_type=='DEDUCTION'){
array_push($this->deduction_array, [
"component" => $pay_component->payroll_item,
"amount" => $slip->amount,
]);
$this->total_deduction=$this->total_deduction+$slip->amount;
}
}
}
$this->netPayInWords = $this->getIndianCurrency($this->gross_salary-$this->total_deduction);
$this->dispatchBrowserEvent('slip_modal');
//dd($this->earning_array,$this->deduction_array,$this->gross_salary,$this->total_deduction);
}
public function salary_upload(){
$this->validate([
'salary_info'=>'required',
],[
'salary_info.required'=>'Please Upload Attendance List'
]);
$response=Excel::import(new SalaryCalculationImport(), $this->salary_info);
//dd($response);
if($response) {
//session()->flash('success', $this->successmsg );
$this->dispatchBrowserEvent('closemodal');
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'success',
'title'=>'DATA UPLOADED SUCCESSFULLY',
'message' => 'Data Uploaded Successfully',
]);
}else{
$this->dispatchBrowserEvent('closemodal');
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'error',
'title'=>'Error',
'message' => 'Something went Wrong',
]);
}
}
public function salary_view(){
//dd('');
$this->sal_arr=[];
if($this->filter_type!=null){
if($this->filter_type=="by_employee_id"){
if($this->search_info){
$employees = EmployeeRegistration::where('employee_id', 'LIKE', '%' . $this->search_info . '%')->get();
}
else{
$this->dispatchBrowserEvent('closemodal');
$this->errorsearchmsg();
}
}
elseif($this->filter_type=="by_department"){
if($this->search_info){
$dept=Team::where('display_name', 'LIKE', '%' . $this->search_info . '%')->first();
if($dept){
$employees = EmployeeRegistration::where('team_id',$dept->id)->get();
}
else{
$employees=[];
}
}
else{
$this->dispatchBrowserEvent('closemodal');
$this->errorsearchmsg();
}
}
elseif($this->filter_type=="by_name"){
if($this->search_info){
$employees = EmployeeRegistration::where('employee_id', 'LIKE', '%' . $this->search_info . '%')->get();
}
else{
$this->dispatchBrowserEvent('closemodal');
$this->errorsearchmsg();
}
}
elseif($this->filter_type=="by_location"){
if($this->search_info){
$city = SettingsCity::where('city_name', 'LIKE', '%' . $this->search_info . '%')->first();
if($city){
$employees=EmployeeRegistration::where('location',$city->id);
}
else{
$employees=[];
}
}
else{
$this->dispatchBrowserEvent('closemodal');
$this->errorsearchmsg();
}
}
}
else{
$employees = EmployeeRegistration::where('status',1)->get();
}
//dd( $employees);
$month=Carbon::parse(session('selected_month'))->format('m');
$year=Carbon::parse(session('selected_month'))->format('Y');
$check=SalaryCalculation::where('month',$month)
->where('year',$year)->get();
$sal_arr=[];
foreach($employees as $emp){
$empsalcheck=SalaryCalculation::where('employee_id',$emp->id)
->where('month',$month)
->where('year',$year)
->get();
$earning=0;
$deduction=0;
if($empsalcheck){
foreach($empsalcheck as $sal){
$compdet=PayrollItems::where('payroll_item_short_name',$sal->payroll_item_name)->first();
//dd($sal->payroll_item_name);
if($compdet){
if($compdet->payroll_item_type=='EARNING'){
$earning=$earning+$sal->amount;
//dd($earning);
}
elseif($compdet->payroll_item_type=='DEDUCTION'){
$deduction=$deduction+$sal->amount;
}
}
}
$data=[
'employee_id'=>$emp->employee_id,
'name'=>$emp->name,
'month'=>$month,
'year'=>$year,
'earning'=>$earning,
'deduction'=>$deduction,
'in_hand'=>$earning-$deduction
];
array_push($this->sal_arr, $data);
}
}
// if(sizeof($check)>0){
// foreach($check as $check)
// }
//dd($this->sal_arr);
}
public function render()
{
$payrollItems = PayrollItems::where('status',1)->get();
//dd($payrollItems);
return view('livewire.hrms.salary-calculation-livewire',compact('payrollItems'));
}
}