CasperSecurity
<?php
namespace App\Http\Livewire\Tendermanagement\Tendertab;
use Livewire\Component;
use Livewire\WithPagination;
use Livewire\WithFileUploads;
use App\Models\AssignTenderDocument;
use App\Models\CreateTender;
use App\Models\TenderDocument;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Auth;
class TenderDocumentRequired extends Component
{
use WithPagination;
use WithFileUploads;
protected $paginationTheme = 'bootstrap';
public $page_name="Document Required For Tender";
public $search;
public $perPage=10;
public $orderBy='id';
public $orderAsc='1';
public $tender_id,$doc_sl_no,$tender_document_id,$doc_path,$created_by,$modified_by,$tender_name;
public $modelid;
public $showmodal=false;
public $showdoc=false;
public $documentPath;
public $successmsg="The Document has been saved successfully.";
public $updatemsg="The Document has been updated successfully.";
public $deletemsg="The Document has been deleted successfully.";
public $duplicateerrormsg="Sorry !!! Document 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=[
// 'tender_id'=>'required',
'tender_document_id'=>'required',
// 'doc_path'=>'required',
];
public $updatedrules=[
// 'tender_id'=>'required',
'tender_document_id'=>'required',
//'doc_path'=>'required',
];
public $messages=[
// 'tender_id.required'=>'This field is required',
'tender_document_id.required'=>'This field is required',
];
public function updatingSearch()
{
$this->resetPage();
}
public function showmodalclick(){
if(session('up_tender_id')){
$res=AssignTenderDocument::where('tender_id',session('up_tender_id'))->get();
}
elseif(session('saved_tender_id')){
$res=AssignTenderDocument::where('tender_id',session('saved_tender_id'))->get();
}
if(sizeof($res)==0){
$this->doc_sl_no=1;
}
else{
$this->doc_sl_no=sizeof($res)+1;
}
$this->resetValidation();
$this->showmodal=true;
$this->cleanVar();
//$this->dispatchBrowserEvent('livewire:load');
}
public function showdocclick(){
$this->resetValidation();
/// $this->documentPath = $documentPath;
$this->showdoc=true;
//$this->dispatchBrowserEvent('livewire:load');
}
public function closemodalclick(){
$this->resetValidation();
$this->showmodal=false;
$this->cleanVar();
}
public function closedocclick(){
$this->resetValidation();
$this->showdoc=false;
$this->cleanVar();
}
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->tender_document_id=null;
$this->doc_path=null;
}
public function loadData($id)
{
$response = AssignTenderDocument::find($id);
if ($response) {
$this->tender_id = $response['tender_id'];
$this->doc_sl_no = $response['doc_sl_no'];
$this->tender_document_id = $response['tender_document_id'];
$this->doc_path = $response['doc_path'];
//$this->activity_status = $response['activity_status'];
}
}
public function modelData()
{
if ($this->doc_path && $this->doc_path instanceof \Illuminate\Http\UploadedFile) {
$this->doc_path = $this->doc_path->store('organisationLogo');
} else {
$this->doc_path = null;
}
$res = AssignTenderDocument::where('id', $this->modelid)->first();
if ($res) {
$this->tender_id = session('up_tender_id');
$this->doc_sl_no = $res->doc_sl_no;
$data = [
'tender_id' => $this->tender_id,
'doc_sl_no' => $this->doc_sl_no,
'tender_document_id' => $this->tender_document_id,
'doc_path' => $this->doc_path,
'modified_by' => Auth::user()->id,
];
} else {
$this->tender_id = $this->tender_id ?: session('up_tender_id');
$this->tender_document_id = $this->tender_document_id ?: 0;
$this->doc_sl_no = $this->doc_sl_no ?: 1;
$data = [
'tender_id' => $this->tender_id,
'doc_sl_no' => $this->doc_sl_no,
'tender_document_id' => $this->tender_document_id,
'doc_path' => $this->doc_path,
'created_by' => Auth::user()->id,
];
}
\Log::info('AssignTenderDocument modelData:', $data);
return $data;
}
public function save()
{
// Validate the incoming data
$this->validate($this->rules, $this->messages);
// Loop through each selected document ID
foreach ($this->tender_document_id as $documentId) {
// Check if this document is already assigned to the tender (to prevent duplicates)
$avail = AssignTenderDocument::where('tender_id', $this->tender_id)
->where('tender_document_id', $documentId)
->whereNull('deleted_at')
->first();
if ($avail) {
// If the document already exists, show the error message and break out of the loop
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'error',
'message' => 'Sorry!!! Document already assigned. Try with a different document.'
]);
return; // Stop further processing and exit the function if a duplicate is found
} else {
// Prepare the data for saving
$this->tender_document_id = $documentId; // Set the current document ID for the save
// Save the document using the correct data model for each document
$response = AssignTenderDocument::create($this->modelData());
if (!$response) {
// If saving fails for any document, show an error message and stop further processing
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'error',
'message' => 'Error occurred while saving the document.'
]);
return; // Stop further processing if any document fails to save
}
}
}
// After all documents are processed and saved, clean up and show success message
$this->cleanVar();
$this->dispatchBrowserEvent('closeOffCanvas');
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'success',
'message' => 'Documents saved successfully.'
]);
}
public function update()
{
$this->validate($this->updatedrules, $this->messages);
$response = AssignTenderDocument::find($this->modelid);
if ($response) {
$response->update($this->modelData());
if ($response) {
$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 = AssignTenderDocument::find($this->modelid);
if ($response) {
if ($response->delete()) {
$res=AssignTenderDocument::where('tender_id',$this->tender_id)->orderBy('doc_sl_no','asc')->get();
foreach($res as $key => $r){
$r->doc_sl_no=$key+1;
$r->save();
}
$this->cleanVar();
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'success',
'message' => $this->deletemsg
]);
} else {
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'error',
'message' => $this->errormsg
]);
}
}
}
public function read()
{
$document = AssignTenderDocument::where('tender_id',$this->tender_id)
->orderBy($this->orderBy, $this->orderAsc ? 'asc' : 'desc')->paginate($this->perPage);
return $document;
}
public function render()
{
//dd('');
if(session('up_tender_id')){
$sessiondata=session('up_tender_id');
}
else{
$sessiondata=session('saved_tender_id');
}
$this->tender_id=$sessiondata;
$tender_det=CreateTender::where('id',$this->tender_id)->first();
if($tender_det){
$this->tender_name= $tender_det->tender_no;
}
$tenderdocuments=TenderDocument::where('status',1)->get();
$documents=$this->read();
return view('livewire.tendermanagement.tendertab.tender-document-required',compact('documents','tenderdocuments'));
}
}