CasperSecurity
<?php
namespace App\Http\Livewire\Finance;
use App\Support\Collection;
use Livewire\Component;
use App\Models\Bank;
use App\Models\LedgerHead;
use App\Models\LedgerheadType;
use Livewire\WithPagination;
class BankLivewire extends Component
{
use WithPagination;
protected $paginationTheme = 'bootstrap';
public $search;
public $perPage=10;
public $orderBy='id';
public $orderAsc='1';
public $bank_account_no,$bank_name,$branch_name,$upi_no,$ledger_head,$bank_address,$ifsc_code,$micr_code,$contact_person_name,$contact_person_mobile,$status,$data;
public $modelid;
public $showmodal=false;
// public $ledgerhead;
public $successmsg="The Bank data has been saved successfully.";
public $updatemsg="The Bank data has been updated successfully.";
public $deletemsg="The Bank data has been deleted successfully.";
public $duplicateerrormsg="Sorry !!! Bank 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=[
'bank_account_no'=>'required',
'bank_name'=>'required',
'branch_name'=>'required',
'ledger_head'=>'required',
];
public $updatedrules=[
'bank_account_no'=>'required',
'bank_name'=>'required',
'branch_name'=>'required',
'ledger_head'=>'required',
];
public $messages=[
'bank_account_no.required'=>'This field is required',
'bank_name.required'=>'This field is required',
'branch_name.required'=>'This field is required',
'ledger_head.required'=>'This field is required',
];
public function updatingSearch()
{
$this->resetPage();
}
public function showmodalclick(){
$this->cleanVar();
$this->resetValidation();
$this->showmodal=true;
}
public function resetvalid()
{
$this->resetValidation();
}
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->bank_account_no=null;
$this->bank_name=null;
$this->branch_name=null;
$this->bank_address=null;
$this->ifsc_code=null;
$this->micr_code=null;
$this->upi_no=null;
$this->contact_person_name=null;
$this->contact_person_mobile=null;
$this->ledger_head=null;
$this->status=null;
}
public function loadData($id){
$response=Bank::find($id);
if($response){
$this->bank_account_no=$response['bank_account_no'];
$this->bank_name=$response['bank_name'];
$this->branch_name=$response['branch_name'];
$this->bank_address=$response['bank_address'];
$this->ifsc_code=$response['ifsc_code'];
$this->micr_code=$response['micr_code'];
$this->upi_no=$response['upi_no'];
$this->contact_person_name=$response['contact_person_name'];
$this->contact_person_mobile=$response['contact_person_mobile'];
$this->ledger_head=$response['ledger_head_id'];
$this->status=$response['status'] == 1?true:false;
}
}
public function save(){
$this->validate($this->rules,$this->messages);
$response=Bank::where('bank_account_no',$this->bank_account_no)->latest()->first();
if($response){
$this->dispatchBrowserEvent('showsuccessmsg',[
'type'=>'error',
'message'=>$this->duplicateerrormsg
]);
}else {
$response = Bank::create([
'bank_account_no'=>$this->bank_account_no,
'bank_name'=>$this->bank_name,
'branch_name'=>$this->branch_name,
'bank_address'=>$this->bank_address,
'ifsc_code'=>$this->ifsc_code,
'micr_code'=>$this->micr_code,
'upi_no'=>$this->upi_no,
'contact_person_name'=>$this->contact_person_name,
'contact_person_mobile'=>$this->contact_person_mobile,
'ledger_head_id'=>$this->ledger_head,
]);
//dd($response);
if ($response) {
$this->cleanVar();
$this->dispatchBrowserEvent('closemodal');
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'success',
'message' => $this->successmsg
]);
} else {
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'error',
'message' => $this->errormsg
]);
}
}
}
public function update(){
$this->validate($this->updatedrules,$this->messages);
$response=Bank::find($this->modelid);
if($response){
$response->update([
'bank_account_no'=>$this->bank_account_no,
'bank_name'=>$this->bank_name,
'branch_name'=>$this->branch_name,
'bank_address'=>$this->bank_address,
'ifsc_code'=>$this->ifsc_code,
'micr_code'=>$this->micr_code,
'upi_no'=>$this->upi_no,
'contact_person_name'=>$this->contact_person_name,
'contact_person_mobile'=>$this->contact_person_mobile,
'ledger_head_id'=>$this->ledger_head,
'status'=>$this->status == false?0:1,
]);
if($response){
$this->cleanVar();
$this->dispatchBrowserEvent('closemodal');
$this->dispatchBrowserEvent('showsuccessmsg',[
'type'=>'success',
'message'=>$this->updatemsg
]);
}else{
// $this->cleanVar();
$this->dispatchBrowserEvent('showsuccessmsg',[
'type'=>'error',
'message'=>$this->errormsg
]);
}
}
}
public function delete(){
$response=Bank::find($this->modelid);
if($response){
if($response->delete()){
$this->cleanVar();
$this->dispatchBrowserEvent('closemodal');
$this->dispatchBrowserEvent('showsuccessmsg',[
'type'=>'success',
'message'=>$this->deletemsg
]);
}else{
$this->dispatchBrowserEvent('showsuccessmsg',[
'type'=>'error',
'message'=>$this->errormsg
]);
}
}
}
public function read(){
// $this->ledgerhead=LedgerHead::where('status',1)->get();
$banks=Bank::where('bank_name','like','%'.$this->search.'%')->orWhere('bank_account_no','like','%'.$this->search.'%')->orWhere('branch_name','like','%'.$this->search.'%')
->orderBy($this->orderBy, $this->orderAsc ? 'asc' : 'desc')
->paginate($this->perPage);
if(sizeof($banks)>0) {
$req_det=[];
foreach ($banks as $key => $data) {
$ledger = LedgerHead::where('id', $data->ledger_head_id)->first();
if ($ledger) {
$data['ledger_head_name'] = $ledger->ledger_head_name;
array_push($req_det,$data);
} else {
$data['ledger_head_name'] = "NA";
array_push($req_det,$data);
}
}
$req_det=new Collection($req_det);
//dd()
$banks=$req_det->paginate($this->perPage);
}
return $banks;
}
public function render()
{
$datatable=$this->read();
// dd($datatable);
//dd($datatable);
// $led_head_name=LedgerheadType::where('ledger_type','Bank')->first();
$ledgertype=LedgerheadType::where('ledger_type','Bank')->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.bank-livewire',compact('datatable','ledgerhead','ledgertype'));
}
}