the_mechanic/app/Controllers/Service.php
venba-Inspriron-3558 3a2456b629 FIX_minor Bugs
2025-11-13 17:54:23 +05:30

374 lines
15 KiB
PHP
Executable File

<?php
namespace App\Controllers;
use App\Models\ServicesModel;
use App\Models\BikemakeModel;
use App\Models\VendorModel;
use App\Models\BikemodelsModel;
use App\Models\ProductModel;
use App\Models\AdditionalProductNameModal;
use App\Models\ComplaintModel;
use CodeIgniter\API\ResponseTrait;
class Service extends BaseController
{
public $session;
protected $additionalProductNameModal;
protected $ServicesModel;
protected $BikemakeModel;
protected $vendorModel;
protected $BikemodelsModel;
protected $ProductModel;
protected $ComplaintModel;
use ResponseTrait;
public function __construct()
{
$this->session = session();
$this->additionalProductNameModal = new AdditionalProductNameModal();
$this->ServicesModel = new ServicesModel();
$this->BikemakeModel = new BikemakeModel();
$this->vendorModel = new VendorModel();
$this->BikemodelsModel = new BikemodelsModel();
$this->ProductModel = new ProductModel();
$this->ComplaintModel = new ComplaintModel();
}
public function service_index()
{
$services = $this->ServicesModel->select('services.*,cubic_capacity.value')
->join('cubic_capacity', 'cubic_capacity.id = services.cubic_capacity_id','left')
->where('services.branch_id',$this->session->get('logged_user_branch_id'))
->orderBy('services.service_name','ASC')->findAll();
// foreach ($services as &$service) {
// // Convert JSON strings to arrays
// $makes_ids = json_decode($service['makes_id'], true) ?? [];
// $models_ids = json_decode($service['models_id'], true) ?? []; -->
// // Fetch make names
// $makeNames = [];
// foreach ($makes_ids as $make_id) {
// if (!empty($make_id)) { // Check if $make_id is not null or empty
// $make = $this->BikemakeModel->find($make_id);
// if ($make) {
// $makeNames[] = $make['make'];
// }
// }
// }
// // Fetch model names
// $modelNames = [];
// foreach ($models_ids as $model_id) {
// if (!empty($model_id)) { // Check if $model_id is not null or empty
// $model = $this->BikemodelsModel->find($model_id);
// if ($model) {
// $modelNames[] = $model['model_name'];
// }
// }
// }
// // Replace IDs with names
// $service['makes_names'] = implode(', ', $makeNames);
// $service['models_names'] = implode(', ', $modelNames);
// }
$data['services']=$services;
return view('service_list',$data);
}
public function new_service($service_id)
{
// $data['product'] = $this->ProductModel->select('products.*, manufacturer.manufacturer_name')
// ->join('manufacturer', 'manufacturer.manufacturer_id = products.manufacturer_id')
// ->where('JSON_CONTAINS(products.models_id, \'["0"]\')', null, false)
// ->where('products.isactive', 1)
// ->findAll();
$data['tax'] = $this->ServicesModel->getTax();
$data['cubiccapacity']= $this->ComplaintModel->getCubicCapacityName();
$data['product'] = [];
if ($service_id === '0') {
$data['page_name'] = "Add Service";
$data['services'] = [];
$data['make'] = $this->BikemakeModel->where('business_id', $this->session->get('logged_user_business_id'))->findAll();
$data['models'] = [];
$data['service'] = $this->ServicesModel->where('isactive',1)->where('category','0')->where('branch_id',$this->session->get('logged_user_branch_id'))->findAll();
} else if ($service_id !== '0') {
$data['page_name'] = "Edit Service";
$services = $this->ServicesModel->where('service_id', $service_id)->get()->getRowArray();
// $services['models_id'] = json_decode($services['models_id'], true);
// $services['makes_id'] = json_decode($services['makes_id'], true);
// $models = [];
// if( $services['makes_id'] != null) {
// foreach ($services['makes_id'] as $make_id) {
// $models[] = $this->BikemodelsModel->where('make_id', $make_id)->findAll();
// }
// }
// $data['models'] = $models;
// $data['make'] = $this->BikemakeModel->where('business_id', $this->session->get('logged_user_business_id'))->findAll();
$data['services'] = $services;
$data['service'] = $this->ServicesModel->where('isactive',1)->where('category','0')->where('branch_id',$this->session->get('logged_user_branch_id'))->findAll();
$ccid = json_decode($services['cubic_capacity_id'], true);
if ($services['category'] == 0) {
$data['product'] = $this->ProductModel
->select('products.*, manufacturer.manufacturer_name')
->join('manufacturer', 'manufacturer.manufacturer_id = products.manufacturer_id', 'left')
->where('products.isactive', 1)
->findAll();
} elseif ($services['category'] == 1) {
$data['product'] = $this->get_product_by_cc($ccid);
}
// dd($data);
}
$data['service_id'] = $service_id;
echo view('service_form',$data);
}
public function add_service()
{
$tax= $this->request->getPost('tax')/2;
// $final_price = ($this->request->getPost('productCost') ? $this->request->getPost('productCost') : 0) +
// ($this->request->getPost('serviceCost') ? $this->request->getPost('serviceCost') : 0) +
// ($this->request->getPost('labour_cost_With_Tax') ? $this->request->getPost('labour_cost_With_Tax') : 0);
// print $final_price;
// print_r($this->request->getPost());
// $make_ids = $this->request->getPost('make');
// $model_ids = $this->request->getPost('model');
// $model_json = json_encode($model_ids);
// $make_json = json_encode($make_ids);
$productIds = $this->request->getPost('product_id');
$subServiceIds = $this->request->getPost('sub_service_id');
$data = [
'service_name' => $this->request->getPost('servicename'),
'category' => $this->request->getPost('category'),
'product_id' => !empty($productIds) ? json_encode($productIds) : null,
'sub_service_id' => !empty($subServiceIds) ? json_encode($subServiceIds) : null,
'cgst' => $tax,
'sgst' => $tax,
'tax' => $this->request->getPost('tax'),
'wg_labour_cost' => $this->request->getPost('wg_labour_cost'),
'wog_labour_cost' => $this->request->getPost('wog_labour_cost'),
'wg_price' => $this->request->getPost('wg_price_withtax'),
'wog_price' => $this->request->getPost('wog_price_withtax'),
// 'price' => $final_price,
// 'makes_id' => $make_json,
// 'models_id' => $model_json,
'description' => $this->request->getPost('description'),
'isactive' => 1 ,
'cubic_capacity_id' => $this->request->getPost('cubiccapacity'),
];
$service_id = $this->request->getPost('service_id');
if (!empty($service_id))
{
$this->ServicesModel->update($service_id, $data);
} else {
$data['branch_id'] = $this->session->get('logged_user_branch_id');
$this->ServicesModel->insert($data);
}
return redirect()->to('service_index');
}
public function delete_service($service_id)
{
$where = ['service_id' => $service_id, 'isactive' => 1]; // Assuming 'isactive' is a column in your database
$existingService = $this->ServicesModel->where($where)->first();
if ($existingService) {
$data['isactive'] = 0;
if ($this->ServicesModel->update($service_id, $data)) {
session()->setFlashdata('success', 'Deleted successfully.');
$this->logger->info("Users: has been Inactivated successfully. Inactivated ID = ".$service_id);
}
}
return redirect()->to('service_index');
}
public function get_vendors_by_model()
{
$make_id = $this->request->getPost('make_id');
$model_ids = $this->request->getPost('model_ids');
$vendors = $this->vendorModel->get_vendors_by_model($make_id, $model_ids);
if ($vendors) {
return $this->response->setJSON($vendors);
} else {
return $this->response->setJSON([]);
}
}
// be carefully we used in add-ajax call and edit - directphp array
public function get_product_by_cc($id = null)
{
$cc_id = $this->request->isAJAX()
? $this->request->getPost('cc_id')
: $id;
// $encodedCCId = json_encode([$cc_id]);
$encodedCCId = json_encode([(string)$cc_id]);
// $encodedCCId = json_encode(["2"]);
// echo $encodedCCId;die;
$product = $this->ProductModel
->select('products.*, manufacturer.manufacturer_name')
->join('manufacturer', 'manufacturer.manufacturer_id = products.manufacturer_id', 'left')
->where('products.isactive', 1)
->where('JSON_VALID(products.cubic_centimeter_id) > 0', null, false)
->where("JSON_CONTAINS(products.cubic_centimeter_id, '{$encodedCCId}')", null, false)
->findAll();
// $this->ProductModel->getLastQuery()->getQuery();
return $this->request->isAJAX()
? $this->response->setJSON($product)
: $product;
}
// public function get_product_by_cc(){
// $cc_id = $this->request->getPost('cc_id');
// $products = [];
// $model_ids = $this->BikemodelsModel->select('model_id')->where('cubic_capacity_id', $cc_id)->asArray()->findAll();
// $mid[] = 0; // default zero. because general..
// foreach($model_ids as $model){
// $mid[] = $model['model_id'];
// }
// $products = [];
// foreach ($mid as $id) {
// $sql = "SELECT products.*, manufacturer.manufacturer_name
// FROM products
// JOIN manufacturer ON manufacturer.manufacturer_id = products.manufacturer_id
// WHERE JSON_CONTAINS(products.models_id, ?)
// AND products.isactive = 1";
// $result = $this->ProductModel->query($sql, ['"' . $id . '"'])->getResult();
// $products = array_merge($products, $result);
// }
// return $this->response->setJSON($products);
// }
// public function get_product_by_models(){
// $make_id = $this->request->getPost('make_id');
// $model_ids = $this->request->getPost('model_ids');
// $products = [];
// array_push($model_ids,'0');
// foreach ($model_ids as $modelId) {
// $product = $this->additionalProductNameModal->select('products.*, manufacturer.manufacturer_name, product_names.product_names_id, product_names.additional_product_name')
// ->join('products', 'products.product_id = product_names.product_id')
// ->join('manufacturer', 'manufacturer.manufacturer_id = products.manufacturer_id')
// ->where('JSON_CONTAINS(products.models_id, \'["' . $modelId . '"]\')', null, false)
// ->where('products.isactive', 1)
// ->findAll();
// $products = array_merge($products, $product);
// }
// $products = array_unique($products, SORT_REGULAR);
// return $this->response->setJSON($products);
// }
// public function get_models()
// {
// $selected_makes = $this->request->getPost('selected_makes');
// // Initialize an empty array to store formatted model data
// $formatted_models = [];
// // Iterate over each selected make ID
// if ($selected_makes) {
// foreach ($selected_makes as $make_id) {
// // Fetch models based on the selected make ID
// $models = $this->BikemodelsModel->where('make_id', $make_id)->where('business_id', $this->session->get('logged_user_business_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 = [
// 'make_id' => $model['make_id'],
// '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;
// }
// }
// }
// // print_r($formatted_models);die;
// // Send the formatted models as JSON response back to the client
// return $this->response->setJSON($formatted_models);
// }
public function status_service()
{
try {
$service_id = $this->request->getPost('service_id');
$isactive = $this->request->getPost('isactive');
$data = [
'isactive' => $isactive
];
$updated = $this->ServicesModel->update($service_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);
}
}
}