diff --git a/app/Controllers/Products.php b/app/Controllers/Products.php index 9845943..46b92a4 100755 --- a/app/Controllers/Products.php +++ b/app/Controllers/Products.php @@ -111,6 +111,7 @@ class Products extends BaseController $productcategory = $this->ProductCategoryModel->orderBy('product_category_id')->findAll(); $data['product_id'] = 0; + $data['products'] = []; $data['additionalProdutName'] = []; @@ -237,6 +238,7 @@ class Products extends BaseController $product_id = $this->request->getPost('product_id'); $additional_product_name = $this->request->getPost('additional_product_name'); $product_names_id = $this->request->getPost('product_names_id'); + $model_ids = $this->request->getPost('fk_model_id'); // print_r($data);die; if (!empty($product_id)) { @@ -247,7 +249,7 @@ class Products extends BaseController } //for insert or update Additional Product Names - $this->insertOrUpdateAdditionalProductName($additional_product_name, $product_names_id, $product_id, $data['product_name']); + $this->insertOrUpdateAdditionalProductName($additional_product_name, $product_names_id, $model_ids, $product_id, $data['product_name']); $orderNumber = 'TM' . str_pad($product_id, 8, '0', STR_PAD_LEFT); @@ -520,39 +522,39 @@ class Products extends BaseController //----VENKAT------------------------------------------------------------------------------------------------ - public function insertOrUpdateAdditionalProductName($product_names, $product_name_id, $product_id, $first_product_name) + public function insertOrUpdateAdditionalProductName($product_names, $product_name_id,$model_ids, $product_id, $first_product_name) { // Fetch existing product names for the given product ID $product_name_data = $this->additionalProductNameModal->where('product_id', $product_id) ->orderBy('product_names_id', 'asc') ->findAll(); $product_name_count = count($product_name_data); - // Check if there are no existing product names and insert the first product name - if ($product_name_count == 0 && !empty($first_product_name)) { + // if ($product_name_count == 0 && !empty($first_product_name)) { - $firstProductNameData = [ - 'product_id' => $product_id, - 'additional_product_name' => $first_product_name, - ]; + // $firstProductNameData = [ + // 'product_id' => $product_id, + // 'additional_product_name' => $first_product_name, + // 'model_id' => 0 + // ]; - // Insert the first product name - $this->additionalProductNameModal->insert($firstProductNameData); + // // Insert the first product name + // $this->additionalProductNameModal->insert($firstProductNameData); - } else { + // } else { - // Update the first product name if it has changed - if ($product_name_data[0]['additional_product_name'] != $first_product_name) { - $firstProductNameUpdateData = [ - 'additional_product_name' => $first_product_name, - ]; + // // Update the first product name if it has changed + // if ($product_name_data[0]['additional_product_name'] != $first_product_name) { + // $firstProductNameUpdateData = [ + // 'additional_product_name' => $first_product_name, + // ]; - $this->additionalProductNameModal - ->where('product_names_id', $product_name_data[0]['product_names_id']) - ->set($firstProductNameUpdateData) - ->update(); - } - } + // $this->additionalProductNameModal + // ->where('product_names_id', $product_name_data[0]['product_names_id']) + // ->set($firstProductNameUpdateData) + // ->update(); + // } + // } $productNameData = []; @@ -563,6 +565,7 @@ class Products extends BaseController 'product_id' => $product_id, 'additional_product_name' => $name, 'product_names_id' => $product_name_id[$index] ?? null, + 'model_id' => $model_ids[$index] ?? null ]; } } @@ -585,6 +588,30 @@ class Products extends BaseController if (!empty($updateData)) { $this->additionalProductNameModal->updateBatch($updateData, 'product_names_id'); } + + if (!empty($product_name_id)) { + + $filtering_ids = []; + + for ($y = 0; $y < count($product_name_data); $y++) { + $filtering_ids[$y] = $product_name_data[$y]['product_names_id']; + } + + + if (!empty($filtering_ids)) { + $A = $filtering_ids; + $B = $product_name_id; + $missing_ids = array_diff($A, $B); + + + if (!empty($missing_ids)) { + $this->additionalProductNameModal + ->whereIn('product_names_id', $missing_ids) + ->delete(); + } + } + } + } return true; diff --git a/app/Controllers/Sales.php b/app/Controllers/Sales.php index 1529673..bb4d20f 100755 --- a/app/Controllers/Sales.php +++ b/app/Controllers/Sales.php @@ -506,9 +506,6 @@ class Sales extends BaseController } - - - public function getProducts() { if(!$this->session->has('logged_user')) { return redirect()->to(base_url()); } @@ -516,6 +513,36 @@ class Sales extends BaseController $makeId = $this->request->getPost('make_id'); $modelId = $this->request->getPost('model_id'); + + $modelName = $this->BikemodelsModel->where('model_id',$modelId)->get()->getRow()->model_name; + $makeName = $this->BikemakeModel->where('make_id',$makeId)->get()->getRow()->make; + + $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') + ->whereIn('product_names.model_id',[$modelId, 0]) + ->where('qty_stock >',0) + ->where('product_names.isactive', 1) + ->findAll(); + + if($product){ + // Send JSON response + return $this->response->setJSON(['product'=>$product , 'modelName'=>$modelName ,'makeName'=>$makeName] ); + } else { + // If product is not found, return an empty response or appropriate message + return $this->response->setJSON(['product'=>[] , 'modelName'=>$modelName ,'makeName'=>$makeName]); + } + } + + + + public function getProducts_old() + { + if(!$this->session->has('logged_user')) { return redirect()->to(base_url()); } + // Retrieve make_id and model_id from the request + $makeId = $this->request->getPost('make_id'); + $modelId = $this->request->getPost('model_id'); + // Encode the modelId before searching $encodedMakeId = json_encode([$makeId]); $encodedModelId = json_encode([$modelId]); diff --git a/app/Controllers/Vehicle.php b/app/Controllers/Vehicle.php index f0e7942..89db4d3 100755 --- a/app/Controllers/Vehicle.php +++ b/app/Controllers/Vehicle.php @@ -68,7 +68,7 @@ class Vehicle extends BaseController } // dd($vehicle); $data['vehicle_id'] = $vehicle_id; - $data['makeData'] =$this->bikeMakeModel->where('business_id', $this->session->get('logged_user_business_id'))->findAll(); + $data['makeData'] =$this->bikeMakeModel->where('make_id != ', 0)->where('business_id', $this->session->get('logged_user_business_id'))->findAll(); return view('vehicle_form',$data); } diff --git a/app/Models/AdditionalProductNameModal.php b/app/Models/AdditionalProductNameModal.php index 57bf17a..1ea8d25 100644 --- a/app/Models/AdditionalProductNameModal.php +++ b/app/Models/AdditionalProductNameModal.php @@ -15,7 +15,7 @@ class AdditionalProductNameModal extends Model protected $allowedFields = [ 'product_names_id', 'product_id', - 'make_id_for_additional_name', + 'model_id', 'additional_product_name', 'isactive', 'created_at', diff --git a/app/Views/product_form.php b/app/Views/product_form.php index 6abadf2..c081a32 100755 --- a/app/Views/product_form.php +++ b/app/Views/product_form.php @@ -457,8 +457,8 @@ $("#model").change(function () {
- -
@@ -530,12 +530,50 @@ $(document).ready(function(){ $(document).ready(function() { + var makeData = ; $('#make').change(function() { var make_id = $(this).val(); // Get the selected make ID make_value = $('option:selected', this).text(); var models_id = $('#model').val(); - $('#makeSelect').empty(); - $('#makeSelect').append(''); + if (make_id.includes('0')) { + + $('#make').empty(); + $('#make').append('') + if (makeData.length > 0) { + makeData.forEach(function(make) { + if(make.make_id == 0){ + $('#make').append('') + }else{ + $('#make').append(''); + } + }); + $('#model').trigger('change'); + } + make_id = ['0']; + } else { + // Enable all options if 'General' is not selected + $('#make option').each(function () { + $(this).prop('disabled', false); // Enable the option + }); + } + $('#makeSelect').empty(); // Clear existing options + console.log(make_id); + + // Assuming make is an array of objects + var filteredMakes = makeData.filter(function(item) { + return make_id.includes(String(item.make_id)); // Check if make_id matches in the array + }); + + // Check if filteredMakes has any results + if (filteredMakes.length > 0) { + filteredMakes.forEach(function(make) { + $('#makeSelect').append(''); + }); + } else { + console.log('No matching makes found!'); + } + + // $('#makeSelect').append(''); $('#makeSelectMessage').hide(); // AJAX request to get models based on the selected make ID @@ -640,6 +678,8 @@ $('#qty_in_stock').on('keyup change', function() { $(document).ready(function() { // Check if products data is available (for editing) var productsData = ; + var addtProductsData = ; + if (productsData) { // Set selected manufacturer $('#manufacturer').val(productsData.manufacturer_id); @@ -648,6 +688,32 @@ $('#qty_in_stock').on('keyup change', function() { // Set selected quality $('#quality').val(productsData.quality); + // ******************* // + if (productsData.make_id) { + if (productsData.make_id.includes('0')) { + // Set the value of the select element to '0' + $('#make').val('0'); + + // Disable all other options except the selected one + $('#make option').each(function () { + if ($(this).val() !== '0') { + $(this).prop('disabled', true); + } + }); + } + } + // ******************* // + if(productsData.models_id){ + $('#model').trigger('change'); + if(addtProductsData){ + addtProductsData.forEach(function(product) { + $('#additional_product_name_' + product.model_id).val(product.additional_product_name); + $('#product_names_id_' + product.model_id).val(product.product_names_id); + }); + } + } + + } });