CasperSecurity
<?php
namespace App\Http\Livewire\Tendermanagement;
use Livewire\Component;
use App\Models\CreateTender;
use App\Models\AssignTenderActivity;
use App\Models\RoleUser;
use App\Support\Collection;
use Livewire\WithPagination;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
class CreateTenderLivewire extends Component
{
use WithPagination;
protected $paginationTheme = 'bootstrap';
public $page_names="Manage Tenders";
public $page_name="Tender";
public $search;
public $perPage=10;
public $orderBy='id';
public $orderAsc='1';
public $client_name,$client_category_id,$client_address,$contact_name,$contact_mobile,$contact_mail,$status;
public $by_tender_status=1;
public $modelid;
protected $listeners = ['delete'];
public $activeTab='tender';
public $showmodal=false;
// public $tabindex=false;
// public function switchTab($tabName)
// {
// if($tabName){
// $this->activeTab = $tabName;
// }
// }
public $selectedTender;
public function openView($id)
{
// Redirect to the tender view route with the tender ID
return redirect()->route('tender.view', ['id' => $id]);
}
public $progress_bar;
public function updateclick($id){
$roleofuserExists = DB::table('role_user')
->where('user_id', Auth::user()->id)
->whereIn('role_id', [1, 2])
->exists();
$res=CreateTender::where('id',$id)->first();
$assigned_user= $activity_user_Array=explode(',',$res->tender_assigned_to);
if(in_array(Auth::user()->id, $assigned_user)){
//dd($res->tender_status);
if($res->tender_status>6){
// dd(1);
$this->dispatchBrowserEvent('showsuccessmsg',[
'type'=>'error',
'message'=>'This could not be modified further after awarded or lost'
]);
}
else{
// dd(2);
session(['up_tender_id' => $id]);
$this->emit('childDataUpdated');
return redirect('Tenders/TenderIndex');
}
}
elseif($roleofuserExists ||$res->created_by== Auth::user()->id){
if($res->tender_status>6){
// dd(1);
$this->dispatchBrowserEvent('showsuccessmsg',[
'type'=>'error',
'message'=>'This could not be modified further after awarded or lost'
]);
}
else{
// dd(2);
session(['up_tender_id' => $id]);
$this->emit('childDataUpdated');
return redirect('Tenders/TenderIndex');
}
}
else{
$this->dispatchBrowserEvent('showsuccessmsg',[
'type'=>'error',
'message'=>'You can not Update the data as tender is not assigned to you'
]);
}
}
public function deleteclick($id){
$res=CreateTender::where('id',$id)->first();
$assigned_user= $activity_user_Array=explode(',',$res->tender_assigned_to);
if(in_array(Auth::user()->id, $assigned_user)){
//dd($res->tender_status);
if($res->tender_status>6){
// dd(1);
$this->dispatchBrowserEvent('showsuccessmsg',[
'type'=>'error',
'message'=>'This could not be modified further after awarded or lost'
]);
}
elseif(Auth::user()->id ==1 || Auth::user()->id==2){
if($res->tender_status>6){
// dd(1);
$this->dispatchBrowserEvent('showsuccessmsg',[
'type'=>'error',
'message'=>'This could not be modified further after awarded or lost'
]);
}
else{
// dd(2);
session(['up_tender_id' => $id]);
$this->emit('childDataUpdated');
return redirect('Tenders/TenderIndex');
}
}
else{
$this->modelid=$id;
$this->dispatchBrowserEvent('confirmdelete',[
'message'=>'Are you sure want to delete this data ?',
'funcname'=>'delete'
]);
}
}
else{
$this->dispatchBrowserEvent('showsuccessmsg',[
'type'=>'error',
'message'=>'You can not Delete the data as tender is not assigned to you'
]);
}
}
public function delete(){
$response=CreateTender::find($this->modelid);
if($response){
$response->delete();
$this->dispatchBrowserEvent('showsuccessmsg',[
'type'=>'success',
'message'=>'The data has been deleted successfully'
]);
}
}
public function showtabindex(){
return redirect('Tenders/TenderIndex');
}
public function managetender($id){
session(['manage_tender_id' => $id]);
return redirect('Tenders/ManageTenderActivity');
}
public function showmodalclick(){
$this->resetValidation();
$this->showmodal=true;
//$this->dispatchBrowserEvent('livewire:load');
}
public function closemodalclick(){
$this->resetValidation();
$this->showmodal=false;
}
public function read(){
$tenderdetail=[];
$roleofuserExists = DB::table('role_user')
->where('user_id', Auth::user()->id)
->whereIn('role_id', [1, 2])
->exists();
if(Auth::user()->id==1 ||Auth::user()->id==2 || $roleofuserExists){
$tenderdetail=CreateTender::search($this->search) ->when($this->by_tender_status, function ($query) {
return $query->where('tender_status', $this->by_tender_status);
})->orderBy($this->orderBy, $this->orderAsc ? 'asc' : 'desc')->get();
}
else{
$all=CreateTender::search($this->search)->where('status',1) ->when($this->by_tender_status, function ($query) {
return $query->where('tender_status', $this->by_tender_status);
})->orderBy($this->orderBy, $this->orderAsc ? 'asc' : 'desc')->get();
foreach($all as $a){
$idarray=explode(',',$a->tender_assigned_to);
if(in_array(Auth::user()->id,$idarray)){
array_push($tenderdetail,$a);
}
elseif ($a->created_by==Auth::user()->id){
array_push($tenderdetail,$a);
}
else{
$assigned_tender=AssignTenderActivity::where('tender_id',$a->id)->get();
foreach($assigned_tender as $assigned){
$activity_user_Array=explode(',',$assigned->activity_assigned_to);
if(in_array(Auth::user()->id,$activity_user_Array)){
array_push($tenderdetail,$a);
}
}
}
}
}
if($tenderdetail){
return (new Collection($tenderdetail))->paginate($this->perPage);
}else{
return (new Collection([]))->paginate($this->perPage);
}
return $tenderdetail;
}
public function render()
{
session()->forget('up_tender_id');
session()->forget('saved_tender_id');
session()->forget('session_tab');
$tenderdetails=$this->read();
//$access=CreateTendr::whereIn('tender_assigned_to')
//dd($tenderdetails);
return view('livewire.tendermanagement.create-tender-livewire',compact('tenderdetails'));
}
}