CasperSecurity
<?php
namespace App\Http\Livewire\Projects;
use App\Models\Client;
use App\Models\CreateTender;
use App\Models\EmployeeRegistration;
use App\Models\Location;
use App\Models\ManageProject;
use App\Models\ProjectDocument;
use App\Models\SettingsLocation;
use App\Models\TenderVeriety;
use App\Models\User;
use App\Models\WorkOrders;
use Carbon\Carbon;
use Illuminate\Support\Facades\Auth;
use Livewire\Component;
use Livewire\WithFileUploads;
use Livewire\WithPagination;
use function Spatie\Ignition\ErrorPage\jsonEncode;
use function Symfony\Component\Finder\size;
class ManageProjectLivewire extends Component
{
use WithPagination, WithFileUploads;
protected $paginationTheme = 'bootstrap';
public $page_name = "Manage Project";
public $search;
public $perPage = 10;
public $orderBy = 'id';
public $orderAsc = '1';
public $project_name, $client_id, $location_id, $work_order_no, $project_variety_id, $project_start_date, $project_end_date, $billing_date, $project_document_id, $project_document, $created_by, $modified_by, $status = true, $clients,
$tenderVeriety, $workOrderMessage;
public $modelId;
public $showmodal = false;
public $successmsg = "The project data has been saved successfully.";
public $updatemsg = "The project data has been updated successfully.";
public $deletemsg = "The project data has been deleted successfully.";
public $errormsg = "Sorry !!! Something went wrong. Please Try Again.";
protected $listeners = ['delete'];
public $rules = [
'project_name' => 'required',
'client_id' => 'required',
'location_id' => 'required',
'work_order_no' => 'required',
'project_variety_id' => 'required',
'project_start_date' => 'required|date',
'project_end_date' => 'required|date|after_or_equal:project_start_date',
'billing_date' => 'required|date',
];
public $messages = [
'project_name.required' => 'The project name is required.',
'client_id.required' => 'This field is required.',
'location_id.required' => 'Please select a city.',
'work_order_no.required' => 'The word order number is required.',
'project_variety_id.required' => 'This field is required.',
'project_start_date.required' => 'The project start date is required.',
'project_start_date.date' => 'The project start date must be a valid date.',
'project_end_date.required' => 'The project end date is required.',
'project_end_date.date' => 'The project end date must be a valid date.',
'project_end_date.after_or_equal' => 'The project end date must be after or equal to the start date.',
'billing_date.required' => 'The billing date is required.',
'billing_date.date' => 'The billing date must be a valid date.',
];
public function updatingSearch()
{
$this->resetPage();
}
public function showmodalclick()
{
$this->cleanVar();
$this->resetValidation();
$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->project_name = null;
$this->client_id = null;
$this->location_id = null;
$this->work_order_no = null;
$this->project_variety_id = null;
$this->project_start_date = null;
$this->project_end_date = null;
$this->billing_date = null;
$this->project_document_id = null;
$this->project_document = null;
$this->created_by = null;
$this->modified_by = null;
$this->selectedDocuments = [];
$this->documents = [];
$this->upload_document = [];
$this->status = null;
$this->showmodal = false;
}
public function modelData()
{
if ($this->modelId) {
$this->modified_by = Auth::user()->id;
$this->created_by = null;
} else {
$this->created_by = Auth::user()->id;
$this->modified_by = null;
}
if (sizeof($this->selectedDocuments) > 0) {
foreach ($this->upload_document as $key => $upload) {
$this->upload_document[$key] = $upload->store('project_document_upload');
}
}
$tableData = [
'project_name' => $this->project_name,
'client_id' => $this->client_id,
'location_id' => $this->location_id,
'work_order_no' => $this->work_order_no,
'project_variety_id' => $this->project_variety_id,
'project_start_date' => $this->project_start_date,
'project_end_date' => $this->project_end_date,
'billing_date' => $this->billing_date,
'project_document_id' => sizeof($this->selectedDocuments) > 0 ? json_encode($this->selectedDocuments) : null,
'project_document' => sizeof($this->upload_document) > 0 ? json_encode($this->upload_document) : null,
'created_by' => $this->created_by,
'modified_by' => $this->modified_by,
'status' => $this->modelId ? $this->status : 1,
];
return $tableData;
}
public function loadData($id)
{
$response = ManageProject::find($id);
if ($response) {
$this->project_name = $response['project_name'];
$this->client_id = $response['client_id'];
$this->location_id = $response['location_id'];
$this->work_order_no = $response['work_order_no'];
$this->project_variety_id = $response['project_variety_id'];
$this->project_start_date = $response['project_start_date'];
$this->project_end_date = $response['project_end_date'];
$this->billing_date = $response['billing_date'];
$this->created_by = $response['created_by'];
$this->modified_by = $response['modified_by'];
$this->status = $response['status'];
$this->project_document_id = $response['project_document_id'];
$this->project_document = $response['project_document'];
if ($this->project_document_id != null) {
foreach (json_decode($this->project_document_id) as $key => $projdoc) {
$this->documents[$key] = true;
}
foreach (json_decode($this->project_document) as $key => $upload) {
$this->upload_document[$key] = $upload;
}
}
}
}
public $documentName = [];
public function documentClick($id){
$this->project_document_id = [];
$this->project_document = [];
$this->project_document = [];
$response = ManageProject::select('project_document_id','project_document')->find($id);
$this->project_document_id = $response ? json_decode($response->project_document_id,true) : [];
$this->project_document = $response ? json_decode($response->project_document,true) : [];
if($this->project_document_id != null){
foreach ($this->project_document_id as $key=>$id){
$documentName = ProjectDocument::select('project_doc_name')->find($id);
$this->documentName[$key] = $documentName ? $documentName->project_doc_name : null;
}
}
}
public function updatedWorkOrderNo($val)
{
$this->workOrderMessage = null;
$this->client_id = null;
$this->project_variety_id = null;
if (!$val) {
return;
}
$workOrder = WorkOrders::where('work_order_no', $val)->latest()->first();
if (!$workOrder) {
$this->workOrderMessage = 'No work order detail found';
return;
}
$this->client_id = $workOrder->client_id;
$tenderId = $workOrder->tender_id;
if ($tenderId) {
$tenderDetails = CreateTender::find($tenderId);
//dd($tenderDetails);
if ($tenderDetails) {
$tenderVerity = TenderVeriety::select('id', 'variety_name')->where('status', 1)->find($tenderDetails->tender_veriety_id);
if ($tenderVerity) {
$this->project_variety_id = $tenderVerity->id;
$this->project_name=$tenderDetails->tender_name;
//dd( $this->project_name);
}
}
}
}
public function validateFields()
{
$rules = [
'project_name' => 'required',
'client_id' => 'required',
'location_id' => 'required',
'work_order_no' => 'required',
'project_variety_id' => 'required',
'project_start_date' => 'required|date',
'project_end_date' => 'required|date|after_or_equal:project_start_date',
'billing_date' => 'required|date',
];
$messages = [
'project_name.required' => 'The project name is required.',
'client_id.required' => 'This field is required.',
'location_id.required' => 'Please select a city.',
'work_order_no.required' => 'The word order number is required.',
'project_variety_id.required' => 'This field is required.',
'project_start_date.required' => 'The project start date is required.',
'project_start_date.date' => 'The project start date must be a valid date.',
'project_end_date.required' => 'The project end date is required.',
'project_end_date.date' => 'The project end date must be a valid date.',
'project_end_date.after_or_equal' => 'The project end date must be after or equal to the start date.',
'billing_date.required' => 'The billing date is required.',
'billing_date.date' => 'The billing date must be a valid date.',
];
if (!empty($this->documents)) {
foreach ($this->documents as $i => $document) {
$rules["upload_document.$i"] = 'required';
$messages["upload_document.$i.required"] = 'Upload documents';
}
}
$this->validate($rules, $messages);
}
public function save()
{
$this->validateFields();
$response = ManageProject::create($this->modelData());
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()
{
$this->validate($this->rules, $this->messages);
$response = ManageProject::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 = ManageProject::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 $projectDocuments = [], $documents = [], $selectedDocuments = [], $upload_document = [];
public function documentAdd($documentId, $key)
{
if (isset($this->documents[$key]) && $this->documents[$key]) {
$this->selectedDocuments[$key] = $documentId;
} else {
unset($this->selectedDocuments[$key]);
}
}
public function read()
{
$manage = ManageProject::search($this->search)->orderBy($this->orderBy, $this->orderAsc ? 'asc' : 'desc')->paginate($this->perPage);
return $manage;
}
public function mount()
{
$this->clients = Client::select('id', 'client_name')->where('status', 1)->get();
$this->tenderVeriety = TenderVeriety::select('id', 'variety_name')->where('status', 1)->get();
}
public function render()
{
$dataTables = [];
$dataTables = $this->read();
foreach ($dataTables as $key => $data) {
$location = SettingsLocation::find($data->location_id);
$dataTables[$key]['location_name'] = $location->location_name;
$client = Client::select('client_name')->find($data->client_id);
$dataTables[$key]['client_name'] = $client ? $client->client_name : 'N/A';
$tenderVerity = TenderVeriety::select('variety_name')->find($data->project_variety_id);
$dataTables[$key]['variety_name'] = $tenderVerity ? $tenderVerity->variety_name : 'N/A';
/*if ($data['project_document_id']) {
$temp = [];
foreach (json_decode($data['project_document_id']) as $doc) {
$temp[] = ProjectDocument::select('project_doc_name')->find($doc);
}
$dataTables[$key]['project_doc_name'] = sizeof($temp) > 0 ? $temp : null;
}*/
}
$locations = SettingsLocation::select('id', 'location_name')->get();
$this->projectDocuments = ProjectDocument::select('id', 'project_doc_name')->get();
return view('livewire.projects.manage-project-livewire', compact('dataTables', 'locations'));
}
}