the_mechanic/app/Controllers/Vendor.php
2025-01-08 06:06:15 +00:00

218 lines
8.5 KiB
PHP
Executable File

<?php
namespace App\Controllers;
use App\Models\VendorModel;
use App\Models\UsersModel;
use App\Models\BikemakeModel;
use App\Models\BikemodelsModel;
use App\Models\ManufacturerModel;
use App\Models\ProductModel;
use App\Models\ProductCategoryModel;
use CodeIgniter\API\ResponseTrait;
class Vendor extends BaseController
{
public $session;
use ResponseTrait;
protected $UsersModel;
public function __construct()
{
$this->session = session();
$this->UsersModel = new UsersModel();
}
public function vendor_index()
{
if(!$this->session->has('logged_user')) { return redirect()->to(base_url()); }
$VendorModel = new VendorModel();
$vendor = $VendorModel->where('branch_id',$this->session->get('logged_user_branch_id'))->orderBy('vendor_name')->findAll();
$data['vendor']=$vendor;
return view('vendor_list',$data);
}
public function new_vendor($vendor_id)
{
if(!$this->session->has('logged_user')) { return redirect()->to(base_url()); }
if ($vendor_id === '0') {
$data['vendor'] = [];
$data['page_name'] = "Add Vendor";
$ManufacturerModel = new ManufacturerModel();
$data['manufacturers']=$ManufacturerModel->where('business_id', $this->session->get('logged_user_business_id'))->findAll();
$ProductCategoryModel = new ProductCategoryModel();
$data['productcategory'] = $ProductCategoryModel->orderBy('product_category_id')->findAll();
} else {
$VendorModel = new VendorModel();
$data['vendor'] = $VendorModel->where('vendor_id', $vendor_id)->get()->getRowArray();
$ManufacturerModel = new ManufacturerModel();
$data['manufacturers']=$ManufacturerModel->where('business_id', $this->session->get('logged_user_business_id'))->findAll();
$ProductCategoryModel = new ProductCategoryModel();
$data['productcategory'] = $ProductCategoryModel->orderBy('product_category_id')->findAll();
$data['page_name'] = "Edit Vendor";
}
$data['vendor_id'] = $vendor_id;
return view('vendor_form', $data);
}
public function add_vendor()
{
if(!$this->session->has('logged_user')) { return redirect()->to(base_url()); }
$VendorModel = new VendorModel();
$vendor_name = $this->request->getPost('vendor_name');
$vendor_id = $this->request->getPost('vendor_id');
if ($vendor_id>0) {
$vendor = $VendorModel->where(['vendor_name' => $vendor_name, 'isactive' => 1])
->where('vendor_id !=', $vendor_id)
->findAll();
} else {
$vendor = $VendorModel->where(['vendor_name' => $vendor_name, 'isactive' => 1])->findAll();
}
if (!empty($vendor)) {
session()->setFlashdata('warning', 'Vendor Name Already Exist.');
$this->logger->info("Vendor: Name Already Exist ID = ".$vendor_id);
return redirect()->to('vendor_index');
}
$data = [
'vendor_name' => $vendor_name,
'gstnumber' => $this->request->getPost('gstnumber'),
'vendor_email' => $this->request->getPost('email'),
'address' => $this->request->getPost('address'),
'city' => $this->request->getPost('city'),
'state' => $this->request->getPost('state'),
'postal_code' => $this->request->getPost('postalcode') != '' ? $this->request->getPost('postalcode') : NULL,
'country' => $this->request->getPost('country'),
'vendortype' => $this->request->getPost('vendortype'),
'category' => $this->request->getPost('category'),
'contact_person_name1' => $this->request->getPost('contact_person_name1'),
'contact_person_name2' => $this->request->getPost('contact_person_name2'),
'contact_person_name3' => $this->request->getPost('contact_person_name3'),
'contact_person_mobile1' => $this->request->getPost('contact_person_mobile1'),
'contact_person_mobile2' => $this->request->getPost('contact_person_mobile2'),
'contact_person_mobile3' => $this->request->getPost('contact_person_mobile3'),
'contact_person_designation1' => $this->request->getPost('contact_person_designation1'),
'contact_person_designation2' => $this->request->getPost('contact_person_designation2'),
'contact_person_designation3' => $this->request->getPost('contact_person_designation3'),
'manufacturer_id' => json_encode($this->request->getPost('manufacturer')),
'isactive' => 1 ,
'branch_id' => $this->session->get('logged_user_branch_id'),
];
$vendor_id = $this->request->getPost('vendor_id');
if (!empty($vendor_id)) {
$VendorModel->update($vendor_id, $data);
} else {
$VendorModel->insert($data);
}
return redirect()->to('vendor_index');
}
public function delete_vendor($vendor_id)
{
if(!$this->session->has('logged_user')) { return redirect()->to(base_url()); }
$model = new VendorModel();
$where = ['vendor_id' => $vendor_id, 'isactive' => 1 , 'branch_id' => $this->session->get('logged_user_branch_id')]; // Assuming 'isactive' is a column in your database
$existingBusiness = $model->where($where)->first();
if ($existingBusiness) {
$data['isactive'] = 0;
if ($model->update($vendor_id, $data)) {
session()->setFlashdata('success', 'Deleted successfully.');
$this->logger->info("Users: has been Inactivated successfully. Inactivated ID = ".$vendor_id);
}
}
return redirect()->to('vendor_index');
}
public function fetch_models()
{
// Retrieve the selected make IDs from the AJAX request
$selected_makes = $this->request->getPost('selected_makes');
// Load the BikemakeModel to fetch models
$BikemodelModel = new BikemodelsModel();
// Initialize an empty array to store formatted model data
$formatted_models = [];
// Iterate over each selected make ID
foreach ($selected_makes as $make_id) {
// Fetch models based on the selected make ID
$models = $BikemodelModel->where('make_id', $make_id)->findAll();
// Iterate over each fetched model and format it as needed
foreach ($models as $model) {
// Create an associative array with 'model_id' and 'model_name' properties
$formatted_model = [
'model_id' => $model['model_id'],
'model_name' => $model['model_name']
// Add other properties if needed
];
// Push the formatted model into the array
$formatted_models[] = $formatted_model;
}
}
// Send the formatted models as JSON response back to the client
return $this->response->setJSON($formatted_models);
}
public function get_product_by_vendor($vendor_id){
$productModel = new ProductModel();
$reorder = $productModel->select('products.*')
->where('products.qty_stock <= products.purchase_order_level')
->where('products.prefered_vendor',$vendor_id)
->findAll();
return json_encode($reorder);
}
public function status_vendor()
{
try {
$vendor_id = $this->request->getPost('vendor_id');
$isactive = $this->request->getPost('isactive');
$VendorModel = new VendorModel();
$data = [
'isactive' => $isactive
];
$updated = $VendorModel->update($vendor_id, $data);
if ($updated) {
$result = $data;
return $this->respond(['status' => 'success','code' => 200],200);
} else {
$result = "No Match's";
return $this->respond(['status' => 'failed','code' => 404],404);
}
} catch (\Exception $exception) {
return $this->respond(['status' => 'failed','code' => 500,'data' => $exception],500);
}
}
}