CasperSecurity
<?php
namespace App\Http\Livewire\Finance;
use App\Models\LedgerHead;
use App\Models\TdsSection;
use App\Models\TdsSectionRate;
use Livewire\Component;
use Livewire\WithPagination;
use Carbon\Carbon;
class TdsSectionLivewire extends Component
{
use WithPagination;
protected $paginationTheme = 'bootstrap';
public $search;
public $perPage=10;
public $orderBy='id';
public $orderAsc='1';
public $tds_section_code,$tds_natureof_payment,$ledger_head_id,$status,$data;
public $tds_section_id,$tds_financial_year;
public $tds_individual_rate = 0;
public $tds_company_rate = 0;
public $tds_nopan_rate = 0;
public $tds_cutoff_amount = 0;
public $modelid;
public $showmodal=false;
public $section_code_message = false;
public $fix = false;
public $ind_rate = false;
public $cmp_value = false;
public $pan_value = false;
public $deleteid;
public $successmsg="The TDS Section data has been saved successfully.";
public $updatemsg="The TDS Section data has been updated successfully.";
public $deletemsg="The TDS Section data has been deleted successfully.";
public $duplicateerrormsg="Sorry !!! TDS Section code has already exists in our database. Please Try Another Name";
public $errormsg="Sorry !!! Something went wrong. Please Try Again.";
protected $listeners = ['delete'];
public $rules=[
'tds_section_code'=>'required',
'tds_natureof_payment'=>'required',
'ledger_head_id'=>'required',
];
public $updatedrules=[
'tds_section_code'=>'required',
'tds_natureof_payment'=>'required',
'ledger_head_id'=>'required',
'status'=>'required',
];
public $messages=[
'tds_section_code.required'=>'This field is required',
'tds_natureof_payment.required'=>'This field is required',
'ledger_head_id.required'=>'This field is required',
];
public $tdsrules=[
'tds_section_id'=>'required',
'tds_financial_year'=>'required',
'tds_individual_rate'=>'required',
'tds_company_rate'=>'required',
'tds_nopan_rate'=>'required',
'tds_cutoff_amount'=>'required',
];
public $tdsmessages=[
'tds_section_id'=>'This field is required',
'tds_financial_year'=>'This field is required',
'tds_individual_rate'=>'This field is required',
'tds_company_rate'=>'This field is required',
'tds_nopan_rate'=>'This field is required',
'tds_cutoff_amount'=>'This field is required',
];
public function updatedTdsSectionCode($code){
if($code != null){
$chk_tds = TdsSection::where('tds_section_code', $code)->select('tds_section_code')->latest()->first();
if($chk_tds != null){
$this->section_code_message = true;
}else{
$this->section_code_message = false;
}
}else{
$this->section_code_message = false;
}
}
public function updatedTdsIndividualRate($indiv_rate){
if($indiv_rate != null){
$this->tds_individual_rate = $this->tds_individual_rate>0?abs($this->tds_individual_rate):0;
if($this->tds_individual_rate>100){
$this->tds_individual_rate = 0;
$this->ind_rate = true;
}else{
$this->ind_rate = false;
}
}else{
$this->ind_rate = false;
}
}
public function updatedTdsCompanyRate($cmp_rate){
if($cmp_rate != null){
$this->tds_company_rate = $this->tds_company_rate>0?abs($this->tds_company_rate):0;
if($this->tds_company_rate>100){
$this->tds_company_rate = 0;
$this->cmp_value = true;
}else{
$this->cmp_value = false;
}
}else{
$this->cmp_value = false;
}
}
public function updatedTdsNopanRate($pan_rate){
if($pan_rate != null){
$this->tds_nopan_rate = $this->tds_nopan_rate>0?abs($this->tds_nopan_rate):0;
if($this->tds_nopan_rate>100){
$this->tds_nopan_rate = 0;
$this->pan_value = true;
}else{
$this->pan_value = false;
}
}else{
$this->pan_value = false;
}
}
public function updatedTdsCutoffAmount($cutamt){
if($cutamt != null){
$this->tds_cutoff_amount = $this->tds_cutoff_amount>0?abs($this->tds_cutoff_amount):0;
}
}
public function resetvalid(){
$this->resetValidation();
$this->dispatchBrowserEvent('hide_tds_modal');
}
public function updatingSearch()
{
$this->resetPage();
}
public function showmodalclick(){
$this->modelid=null;
$this->tds_section_code=null;
$this->tds_natureof_payment=null;
$this->ledger_head_id=null;
$this->resetValidation();
$this->showmodal=true;
}
public function closemodalclick(){
$this->modelid=null;
$this->cleanVar();
$this->resetValidation();
$this->showmodal=false;
$this->redirect('/Finance/TDSSection');
}
public function updateclick($id){
if($id){
$this->modelid=$id;
$this->loadData($this->modelid);
$this->showmodal=true;
}
}
public function deleteclick($id){
if($id != null){
$this->deleteid=$id;
$this->dispatchBrowserEvent('confirmdelete',[
'message'=>'Are you sure want to delete this data ?',
'funcname'=>'delete'
]);
}
}
public function cleanVar(){
// $this->modelid=null;
$this->tds_financial_year=null;
$this->tds_individual_rate=null;
$this->tds_company_rate=null;
$this->tds_nopan_rate=null;
$this->tds_cutoff_amount=null;
$this->status=null;
}
public function loadData($id){
$response=TdsSection::find($id);
if($response){
$this->tds_section_code=$response['tds_section_code'];
$this->tds_natureof_payment=$response['tds_natureof_payment'];
$this->ledger_head_id=$response['ledger_head_id'];
$this->status=$response['status'];
}
}
public function save()
{
$this->validate($this->rules, $this->messages);
$response = null;
if ( $this->tds_cutoff_amount != null && $this->tds_financial_year != null) {
$data = TdsSection::where('tds_section_code', $this->tds_section_code)->first();
if ($data == null && $this->modelid == null) {
$response = TdsSection::create([
'tds_section_code' => $this->tds_section_code,
'tds_natureof_payment' => $this->tds_natureof_payment,
'ledger_head_id' => $this->ledger_head_id,
]);
} else {
if ($data != null) {
$check_year = TdsSectionRate::where('tds_section_id', $data['id'])->where('tds_financial_year', $this->tds_financial_year)->latest()->first();
} else {
$check_year = TdsSectionRate::where('tds_section_id', $this->modelid)->where('tds_financial_year', $this->tds_financial_year)->latest()->first();
}
if ($check_year == null) {
$sectionRate = TdsSectionRate::create([
'tds_section_id' => $data != null ? $data['id'] : $this->modelid,
'tds_financial_year' => $this->tds_financial_year,
'tds_individual_rate' => $this->tds_individual_rate,
'tds_company_rate' => $this->tds_company_rate,
'tds_nopan_rate' => $this->tds_nopan_rate,
'tds_cutoff_amount' => $this->tds_cutoff_amount,
]);
$this->fix = true;
$this->cleanVar();
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'success',
'message' => $this->successmsg
]);
} else {
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'error',
'message' => 'Financial Year ' . $this->tds_financial_year . ' already exist.'
]);
}
}
if ($response) {
/* $this->validate($this->tdsrules, $this->tdsmessages);*/
$sectionRate = TdsSectionRate::create([
'tds_section_id' => $response->id,
'tds_financial_year' => $this->tds_financial_year,
'tds_individual_rate' => $this->tds_individual_rate,
'tds_company_rate' => $this->tds_company_rate,
'tds_nopan_rate' => $this->tds_nopan_rate,
'tds_cutoff_amount' => $this->tds_cutoff_amount,
]);
if ($sectionRate) {
$this->fix = true;
$this->cleanVar();
// $this->dispatchBrowserEvent('closeOffCanvas');
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'success',
'message' => $this->successmsg
]);
} else {
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'error',
'message' => $this->errormsg
]);
}
}
}else {
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'error',
'message' => 'Enter Financial Year and Amount'
]);
}
}
/* public function tdsratesave(){
{
// dd('');
$this->validate($this->tdsrules,$this->tdsmessages);
$response=TdsSectionRate::where('tds_financial_year',$this->tds_financial_year)->first();
//dd($response);
if($response){
$this->dispatchBrowserEvent('showsuccessmsg',[
'type'=>'error',
'message'=>$this->duplicateerrormsg
]);
}else {
$response = TdsSectionRate::create([
'tds_financial_year'=>$this->tds_financial_year,
'tds_individual_rate'=>$this->tds_individual_rate,
'tds_company_rate'=>$this->tds_company_rate,
'tds_nopan_rate'=>$this->tds_nopan_rate,
'tds_cutoff_amount'=>$this->tds_cutoff_amount,
]);
//dd($response);
}
}
}*/
public function update(){
$this->validate($this->updatedrules,$this->messages);
//dd($this);
$response=TdsSection::find($this->modelid);
if($response){
$response->update([
'tds_section_code'=>$this->tds_section_code,
'tds_natureof_payment'=>$this->tds_natureof_payment,
'ledger_head_id'=>$this->ledger_head_id,
'status'=>$this->status,
]);
if($response){
// $this->cleanVar();
// $this->dispatchBrowserEvent('closeOffCanvas');
$this->dispatchBrowserEvent('showsuccessmsg',[
'type'=>'success',
'message'=>$this->updatemsg
]);
}else{
// $this->cleanVar();
$this->dispatchBrowserEvent('showsuccessmsg',[
'type'=>'error',
'message'=>$this->errormsg
]);
}
}
}
public function delete(){
$response=TdsSectionRate::find($this->deleteid);
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(){
// $this->ledgerhead=LedgerHead::where('status',1)->get();
$tds_sections=TdsSection::search($this->search)
->orderBy($this->orderBy, $this->orderAsc ? 'asc' : 'desc')
->paginate($this->perPage);
return $tds_sections;
}
public function render()
{
$current_year = Carbon::now()->year;
$previous_year = Carbon::now()->subYear()->year;
$datatable = $this->read();
$ledgerhead=LedgerHead::where('status',1)->get();
$tdstable = [];
$section_data = null;
if($this->tds_section_code != null){
$section_data = TdsSection::where('tds_section_code', $this->tds_section_code)->first();
if($section_data != null){
$tdstable=TdsSectionRate::where('tds_section_id',$section_data['id'])->get();
}elseif ($this->modelid != null){
$tdstable=TdsSectionRate::where('tds_section_id',$this->modelid)->get();
}
}
return view('livewire.finance.tds-section-livewire',compact('datatable','ledgerhead','tdstable', 'current_year', 'previous_year'));
}
}