CasperSecurity
<?php
namespace App\Http\Livewire\Finance;
use App\Models\LedgerheadType;
use Livewire\Component;
use App\Models\AccountType;
use App\Models\AccountGroup;
use App\Models\LedgerHead;
use Livewire\WithPagination;
class AccountLedgerheadLivewire extends Component
{
use WithPagination;
protected $paginationTheme = 'bootstrap';
public $search;
public $perPage = 10;
public $orderBy = 'id';
public $orderAsc = '1';
public $status, $data;
public $modelid, $activeTab;
public $showmodal = false;
public $account_group_id, $ledger_head, $ledger_head_name, $account_group_name, $ledger_type_id, $group_details;
public $accountGroups = []; // Initialize accountGroups variable
public $successmsg = "The Payroll data has been saved successfully.";
public $updatemsg = "The Payroll item data has been updated successfully.";
public $deletemsg = "The Payroll item data has been deleted successfully.";
public $duplicateerrormsg = "Sorry !!! Payroll item name has already exists in our database. Please Try Another Name";
public $errormsg = "Sorry !!! Something went wrong. Please Try Again.";
public $rules = [
'account_group_name' => 'required',
'ledger_head' => 'required',
'ledger_head_name' => 'required',
'ledger_type_id' => 'required',
];
public $updatedrules = [
'account_group_id' => 'required|exists:account_groups,id', // Make sure this validation is added
'ledger_head' => 'required',
'ledger_head_name' => 'required',
'ledger_type_id' => 'required',
'status' => 'required',
];
public $messages = [
'account_group_name.required' => 'This field is required',
'ledger_head.required' => 'This field is required',
'ledger_head_name.required' => 'This field is required',
'ledger_type_id.required' => 'This field is required',
];
public function updatingSearch()
{
$this->resetPage();
}
public function showmodalclick($id, $name)
{
$this->account_group_id = $id;
$this->account_group_name = $name;
$ledger_head_count = LedgerHead::count() + 1;
$this->ledger_head = '10' . str_pad($ledger_head_count, 4, '0', STR_PAD_LEFT);
// Clean the variables and show modal
$this->cleanVar();
$this->showmodal = true;
}
public function closemodalclick()
{
$this->resetValidation();
$this->showmodal = false;
}
public function updateclick($id)
{
$this->modelid = $id;
$this->loadData($this->modelid);
$this->showmodal = true;
}
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->ledger_head_name = null;
$this->ledger_type_id = null;
$this->status = null;
}
public function loadData($id)
{
$response = LedgerHead::find($id);
if ($response) {
// Fetch the AccountGroup related to the current record
$accountGroup = AccountGroup::find($response->account_group_id);
if ($accountGroup) {
// Retrieve all Account Groups related to the same account_type_id
$this->account_group_id = $accountGroup->id; // Set the account group ID
// Fetch all account groups that have the same account_type_id
$this->accountGroups = AccountGroup::where('account_type_id', $accountGroup->account_type_id)
->get(); // Get all groups with the same account_type_id
}
// Assign the other fields
$this->ledger_head = $response->ledger_head;
$this->ledger_head_name = $response->ledger_head_name;
$this->ledger_type_id = $response->ledger_type_id;
$this->status = $response->status;
}
}
public function save()
{
$this->validate($this->rules, $this->messages);
$response = LedgerHead::create([
'account_group_id' => $this->account_group_id,
'ledger_head' => $this->ledger_head,
'ledger_head_name' => $this->ledger_head_name,
'ledger_type_id' => $this->ledger_type_id,
]);
if ($response) {
$this->cleanVar();
$this->dispatchBrowserEvent('closeOffCanvas');
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'success',
'message' => $this->successmsg
]);
} else {
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'error',
'message' => $this->errormsg
]);
}
}
public function update()
{
// Validate the inputs
$this->validate($this->updatedrules, $this->messages);
// Find the LedgerHead record by model ID
$response = LedgerHead::find($this->modelid);
if ($response) {
// Update the LedgerHead record with the new data
$updateData = [
'ledger_head' => $this->ledger_head,
'ledger_head_name' => $this->ledger_head_name,
'ledger_type_id' => $this->ledger_type_id,
'status' => $this->status,
'account_group_id' => $this->account_group_id, // Added the account_group_id update here
];
// Perform the update
$updated = $response->update($updateData);
if ($updated) {
// Clean up variables after successful update
$this->cleanVar();
// Dispatch events to close off-canvas and show success message
$this->dispatchBrowserEvent('closeOffCanvas');
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'success',
'message' => $this->updatemsg
]);
} else {
// Show error message if update failed
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'error',
'message' => $this->errormsg
]);
}
} else {
// If LedgerHead not found, show error
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'error',
'message' => 'LedgerHead record not found.'
]);
}
}
public function delete()
{
$response = LedgerHead::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 switchTab($id)
{
// dd($id);
$check = AccountType::where('id', $id)->first();
if ($check) {
$this->activeTab = $id;
session([
'session_tab' => $id,
]);
return redirect('Finance/groups');
}
}
/* public function ledgerheadClick($id){
$check=AccountGroup::where('id',$id)->first();
// dd($check);
if($check){
session([
'group_det'=>$id,
]);
return redirect('Finance/legder_head');
}
}*/
public function render()
{
// dd(session('group_det'));
if (session('group_det')) {
$this->activeTab = session('session_tab');
$accounttypes = AccountType::where('status', 1)->get();
$datatable = LedgerHead::search($this->search)->where('account_group_id', session('group_det'))
->orderBy($this->orderBy, $this->orderAsc ? 'asc' : 'desc')
->paginate($this->perPage);;
//dd($this->group_details);
$this->group_details = AccountGroup::where('id', session('group_det'))->first();
} else {
// dd(2);
$datatable = [];
$this->group_details = AccountGroup::where('id', session('group_det'))->first();
}
if (sizeof($datatable) > 0) {
foreach ($datatable as $key => $data) {
$ledger = LedgerheadType::where('id', $data->ledger_type_id)->first();
if ($ledger) {
$datatable[$key]['ledger_type_id'] = $ledger->ledger_type;
} else {
$datatable[$key]['ledger_type_id'] = "NA";
}
}
}
$ledgertype = LedgerheadType::where('status', 1)->get();
return view('livewire.finance.account-ledgerhead-livewire', compact('datatable', 'accounttypes', 'ledgertype'));
}
}