CasperSecurity
<?php
namespace App\Http\Livewire\Finance;
use App\Models\LedgerHead;
use App\Models\LedgerheadType;
use App\Models\PayrollItem;
use App\Models\SalaryJournalComponent;
use App\Models\Loan;
use App\Models\SalaryJournalConfiguration;
use Livewire\Component;
use Livewire\WithPagination;
class SalaryJournalConfigurationLivewire extends Component
{
use WithPagination;
protected $paginationTheme = 'bootstrap';
public $search;
public $perPage = 10;
public $orderBy = 'id';
public $orderAsc = '1';
public $description,$ledger_head,$ledger_head_name,$ledger_type_id, $type, $earning_deduction,$salary_loan, $data;
public $status = false;
public $modelid;
public $component_id = [];
public $components=[];
public $loan_id = [];
public $showmodal = false;
public $loans = [];
public $successmsg = "The Salary Journal Configuration has been saved successfully.";
public $updatemsg = "The Salary Journal Configuration has been updated successfully.";
public $deletemsg = "The Salary Journal Configuration data has been deleted successfully.";
public $duplicateerrormsg = "Sorry !!! Salary Journal Configuration name has already exists in our database. Please Try Another Name";
public $errormsg = "Sorry !!! Something went wrong. Please Try Again.";
protected $listeners = ['delete'];
public $rules = [
'description' => 'required|max:255',
'ledger_head'=>'required|',
'type' => 'required',
'earning_deduction' => 'required',
'salary_loan' => 'required',
];
public $updatedrules = [
'description' => 'required|max:255',
'ledger_head'=>'required|',
'type' => 'required',
'earning_deduction' => 'required',
'salary_loan' => 'required',
];
public $messages = [
'description.required' => 'This field is required',
'ledger_type_id'=>'This field is required',
'ledger_head' => 'This field is required',
'type.required' => 'This field is required',
];
public function resetvalid(){
$this->resetValidation();
}
public function updatingSearch()
{
$this->resetPage();
}
public function showmodalclick()
{
$this->cleanVar();
$this->resetValidation();
$this->showmodal = true;
//$this->dispatchBrowserEvent('salary_journal_modal_show');
}
public function closemodalclick()
{
$this->resetValidation();
$this->showmodal = false;
}
public function updateclick($id)
{
$this->modelid = $id;
$this->loadData($this->modelid);
$this->showmodal = true;
//$this->dispatchBrowserEvent('salary_journal_modal_show');
}
public function deleteclick($id)
{
$this->modelid = $id;
$this->dispatchBrowserEvent('confirmdelete', [
'message' => 'Are you sure want to delete this data ?',
'funcname' => 'delete'
]);
}
public function cleanVar()
{
$this->modelid = null;
$this->status = null;
$this->description = null;
$this->ledger_head = null;
$this->type = null;
$this->salary_loan = null;
$this->earning_deduction = null;
$this->component_id = [];
$this->loan_id = [];
}
public function loadData($id)
{
$response = SalaryJournalConfiguration::find($id);
if ($response) {
$this->description = $response['description'];
$this->ledger_head = $response['ledger_head_id'];
$this->type = $response['type'];
$this->earning_deduction = $response['earning_deduction'];
$this->salary_loan = $response['salary_loan'];
//$this->status = $response['status'];
$this->status = $response['status'] == 1?true:false;
$salary_component = SalaryJournalComponent::where('salary_journal_configuration_id', $response['id'])->where('status',1)->pluck('salary_component_id');
if($this->salary_loan == 'Salary' && sizeof($salary_component)>0){
$salary_component = $salary_component->toArray();
$this->updatedEarningDeduction('Salary');
$this->dispatchBrowserEvent('load_salary_comp');
$this->component_id = $salary_component;
}elseif($this->salary_loan == 'Loan' && sizeof($salary_component)>0){
$salary_component = $salary_component->toArray();
$this->updatedSalaryLoan('Loan');
$this->dispatchBrowserEvent('load_loan_comp');
$this->loan_id = $salary_component;
}
}
}
public function save()
{
$this->validate($this->rules, $this->messages);
$response = SalaryJournalConfiguration::create([
'description' => $this->description,
'ledger_head_id' => $this->ledger_head,
'type' => $this->type,
'earning_deduction' => $this->earning_deduction,
'salary_loan' => $this->salary_loan,
]);
/*if ($response) {
$this->cleanVar();
$this->dispatchBrowserEvent('closeOffCanvas');
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'success',
'message' => $this->successmsg
]);
} else {
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'error',
'message' => $this->errormsg
]);
}*/
if ($response) {
if (sizeof($this->component_id) > 0) {
foreach ($this->component_id as $cmpnt_id) {
$component = SalaryJournalComponent::create([
'salary_journal_configuration_id' => $response['id'],
'salary_component_id' => $cmpnt_id,
]);
}
} elseif (sizeof($this->loan_id) > 0) {
foreach ($this->loan_id as $ln_id) {
$component = SalaryJournalComponent::create([
'salary_journal_configuration_id' => $response['id'],
'salary_component_id' => $ln_id,
]);
}
}
$this->cleanVar();
$this->dispatchBrowserEvent('closeOffCanvas');
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'success',
'message' => $this->successmsg
]);
}
}
public function update()
{
$this->validate($this->updatedrules, $this->messages);
$response = SalaryJournalConfiguration::find($this->modelid);
if ($response) {
$response->update([
'description' => $this->description,
'ledger_head_id' => $this->ledger_head,
'type' => $this->type,
'earning_deduction' => $this->earning_deduction,
'salary_loan' => $this->salary_loan,
/*'status' => $this->status,*/
'status' => $this->status == false ? 0 : 1,
]);
/*if ($response) {
$this->cleanVar();
$this->dispatchBrowserEvent('closeOffCanvas');
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'success',
'message' => $this->updatemsg
]);
} else {
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'error',
'message' => $this->errormsg
]);
}*/
if ($response) {
$salary_component_data = SalaryJournalComponent::where('salary_journal_configuration_id', $response['id'])->where('status', 1)->update(['status' => 0]);
if (sizeof($this->component_id) > 0) {
foreach ($this->component_id as $cmpnt_id) {
$component = SalaryJournalComponent::create([
'salary_journal_configuration_id' => $response['id'],
'salary_component_id' => $cmpnt_id,
]);
}
} elseif (sizeof($this->loan_id) > 0) {
foreach ($this->loan_id as $ln_id) {
$component = SalaryJournalComponent::create([
'salary_journal_configuration_id' => $response['id'],
'salary_component_id' => $ln_id,
]);
}
}
$this->cleanVar();
$this->dispatchBrowserEvent('closeOffCanvas');
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'success',
'message' => $this->updatemsg
]);
} else {
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'error',
'message' => $this->errormsg
]);
}
}
}
public function delete()
{
$response = SalaryJournalConfiguration::find($this->modelid);
if ($response) {
if ($response->delete()) {
$this->cleanVar();
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'success',
'message' => $this->deletemsg
]);
} else {
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'error',
'message' => $this->errormsg
]);
}
}
}
public function read()
{
$salary_journal_configurations = SalaryJournalConfiguration::search($this->search)
->where('status',1)
->orderBy($this->orderBy, $this->orderAsc ? 'asc' : 'desc')
->paginate($this->perPage);
return $salary_journal_configurations;
}
public function render()
{
$datatable = [];
$datatable = $this->read();
$ledgertype=LedgerheadType::where('ledger_type','Others','Employee')->latest()->first();
/*$ledgerhead=LedgerHead::where('status',1)->where('ledger_type_id',$ledgertype->id)->get();*/
if ($ledgertype) {
$ledgerhead = LedgerHead::where('status', 1)
->where('ledger_type_id', $ledgertype->id)
->get();
} else {
$ledgerhead = [];
}
return view('livewire.finance.salary-journal-configuration-livewire', compact('datatable', 'ledgerhead','ledgertype'));
}
}