From 28fe55efa74ddd04bd5057f7465ea2e7973d9eaa Mon Sep 17 00:00:00 2001 From: bitbucket Date: Tue, 9 Apr 2024 12:00:47 +0530 Subject: [PATCH] GWM:CHANGES-in-product-module-vendor-selection --- app/Controllers/Products.php | 48 +++++++++++++++++++++++++++++++----- app/Views/product_form.php | 5 ++-- 2 files changed, 45 insertions(+), 8 deletions(-) diff --git a/app/Controllers/Products.php b/app/Controllers/Products.php index 4918b0d..f76fa8a 100644 --- a/app/Controllers/Products.php +++ b/app/Controllers/Products.php @@ -128,15 +128,51 @@ class Products extends BaseController { $model_ids = $this->request->getPost('model_ids'); - + $make_id = $this->request->getPost('make_id'); $vendorModel = new VendorModel(); - $vendors = $vendorModel->get_vendors_by_model( $model_ids); + $Vendors = $vendorModel->where('branch_id',$this->session->get('logged_user_branch_id'))->findAll(); + if($Vendors){ + + $data = []; - if ($vendors) { + foreach ($model_ids as $modelKey => $modelValue) + { + foreach ($Vendors as $vendorsKey => $vendorsValue) + { + $modelsArray = json_decode($vendorsValue['model'], true); + if (is_array($modelsArray)) + { + $findIfModelExist = array_search($modelValue, $modelsArray); + if ($findIfModelExist !== false) + { + array_push($data,$vendorsValue); + } + } + } + } + + if (count($data)) { + + $arrayOfArrays = array_map(function($obj) { + return (array) $obj; + }, $data); + + // Remove duplicates based on string representation of each object + $uniqueArray = array_map('unserialize', array_unique(array_map('serialize', $arrayOfArrays))); + + // Convert the unique array of associative arrays back to an array of objects + $uniqueObjectsArray = array_map(function($arr) { + return (object) $arr; + }, $uniqueArray); + + return $this->response->setJSON($uniqueArray); + + } else { - return $this->response->setJSON($vendors); - } else { - + return $this->response->setJSON([]); + } + }else { + return $this->response->setJSON([]); } } diff --git a/app/Views/product_form.php b/app/Views/product_form.php index 237b6aa..cc44005 100644 --- a/app/Views/product_form.php +++ b/app/Views/product_form.php @@ -222,14 +222,15 @@ $(document).ready(function() { $('#model').change(function() { var model_ids = $('#model').val(); // Get the selected model IDs -console.log(model_ids); + var make_id = $('#make').val(); + console.log(model_ids); // AJAX request to get vendors based on the selected model ID $.ajax({ url: '', // URL to your CodeIgniter controller method type: 'POST', dataType: 'json', - data: { model_ids: model_ids }, // Send the selected make ID and model IDs to the controller + data: { model_ids: model_ids , make_id : make_id }, // Send the selected make ID and model IDs to the controller success: function(response) { // Update vendor dropdown based on the response if (response) {