FEAT_ADDITIONAL_PRODUCT_NAME : RV

This commit is contained in:
VENKATESHWARAN 2024-10-09 13:54:33 +05:30
parent d5c20cd5ce
commit 67ac9d4ff3
5 changed files with 262 additions and 115 deletions

View File

@ -3,25 +3,27 @@
namespace App\Controllers;
use App\Models\ProductModel;
use App\Models\BikemakeModel;
use App\Models\VendorModel;
use App\Models\BikemodelsModel;
use App\Models\ManufacturerModel;
use App\Models\ProductCategoryModel;
use App\Models\BikemakeModel;
use App\Models\VendorModel;
use App\Models\BikemodelsModel;
use App\Models\ManufacturerModel;
use App\Models\ProductCategoryModel;
use App\Models\AdditionalProductNameModal;
use CodeIgniter\API\ResponseTrait;
class Products extends BaseController
{
{
protected $additionalProductNameModal;
public $session;
use ResponseTrait;
public function __construct()
{
$this->session = session();
$this->additionalProductNameModal = new AdditionalProductNameModal();
}
public function product_index($manufacturer_id = null)
@ -53,11 +55,6 @@ class Products extends BaseController
}
public function new_product($product_id)
{
@ -80,7 +77,9 @@ class Products extends BaseController
$productcategory = $ProductCategoryModel->orderBy('product_category_id')->findAll();
$data['productcategory']=$productcategory;
$data['vendorsProduct'] =[];
} else if ($product_id !== '0') {
$data['page_name']="Edit Product";
$ProductModel = new ProductModel();
$products=$ProductModel->where('product_id', $product_id)->get()->getRowArray();
@ -118,35 +117,43 @@ class Products extends BaseController
$vendors = [];
$VendorModel = new VendorModel();
foreach ($vendorIds as $vendorId) {
$vendor = $VendorModel->find($vendorId);
if ($vendor) {
$vendors[] = ['id' => $vendor['vendor_id'], 'name' => $vendor['vendor_name']];
$VendorModel = new VendorModel();
foreach ($vendorIds as $vendorId) {
$vendor = $VendorModel->find($vendorId);
if ($vendor) {
$vendors[] = ['id' => $vendor['vendor_id'], 'name' => $vendor['vendor_name']];
}
}
$data['vendorsProduct'] = $vendors;
// print_r($data['vendors']);die;
$preferredVendorId = $products['prefered_vendor'];
// Pass the preferred vendor ID to the view
$data['preferredVendorId'] = $preferredVendorId; // print_r($data);die;
// print_r($data);die;
}
$data['vendorsProduct'] = $vendors;
// print_r($data['vendors']);die;
$preferredVendorId = $products['prefered_vendor'];
// Pass the preferred vendor ID to the view
$data['preferredVendorId'] = $preferredVendorId; // print_r($data);die;
// print_r($data);die;
}
$data['product_id'] = $product_id;
$ProductCategoryModel = new ProductCategoryModel();
$productcategory = $ProductCategoryModel->orderBy('product_category_id')->findAll();
$additionalProdutName = $this->additionalProductNameModal->where('product_id', $product_id)->orderBy('product_names_id')->findAll();
$data['productcategory']=$productcategory;
// print_r($data);die;
$data['additionalProdutName']=$additionalProdutName;
// dd($data);
// print_r($data);die;
// echo "<pre>";
// print_r($data['manufacturers']);die;
echo view('product_form',$data);
}
public function add_product()
{
{
// dd($this->request->getPost());
if(!$this->session->has('logged_user')) { return redirect()->to(base_url()); }
$ProductModel = new ProductModel();
@ -198,16 +205,19 @@ 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');
// print_r($data);die;
if (!empty($product_id)) {
$ProductModel->update($product_id, $data);
} else {
$ProductModel->insert($data);
$product_id = $ProductModel->getInsertID();
}
//for insert or update Additional Product Names
$this->insertOrUpdateAdditionalProductName($additional_product_name, $product_names_id, $product_id, $data['product_name']);
$orderNumber = 'TM' . str_pad($product_id, 8, '0', STR_PAD_LEFT);
@ -236,65 +246,61 @@ class Products extends BaseController
return redirect()->to('product_index');
}
// Retrieve the make_id from the AJAX request
public function get_vendors_by_model()
{
$model_ids = $this->request->getPost('model_ids');
$make_id = $this->request->getPost('make_id');
$vendorModel = new VendorModel();
$Vendors = $vendorModel->where('branch_id',$this->session->get('logged_user_branch_id'))->findAll();
if($Vendors){
$data = [];
// Retrieve the make_id from the AJAX request
public function get_vendors_by_model()
{
$model_ids = $this->request->getPost('model_ids');
$make_id = $this->request->getPost('make_id');
$vendorModel = new VendorModel();
$Vendors = $vendorModel->where('branch_id',$this->session->get('logged_user_branch_id'))->findAll();
if($Vendors){
$data = [];
foreach ($model_ids as $modelKey => $modelValue)
foreach ($model_ids as $modelKey => $modelValue)
{
foreach ($Vendors as $vendorsKey => $vendorsValue)
{
foreach ($Vendors as $vendorsKey => $vendorsValue)
print_r($vendorsValue);die;
$modelsArray = json_decode($vendorsValue['model'], true);
if (is_array($modelsArray))
{
print_r($vendorsValue);die;
$modelsArray = json_decode($vendorsValue['model'], true);
if (is_array($modelsArray))
$findIfModelExist = array_search($modelValue, $modelsArray);
if ($findIfModelExist !== false)
{
$findIfModelExist = array_search($modelValue, $modelsArray);
if ($findIfModelExist !== false)
{
array_push($data,$vendorsValue);
}
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([]);
}
}else {
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([]);
}
}else {
return $this->response->setJSON([]);
}
}
public function get_models()
{
$make_ids = $this->request->getPost('make_id'); // Assuming you are sending make_id via POST
@ -328,10 +334,8 @@ class Products extends BaseController
return $this->response->setJSON($formatted_models);
}
public function save_make() {
public function save_make()
{
$response = []; // Initialize an empty array to hold the response data
// Assuming BikemakeModel and BikemodelsModel are properly defined
@ -372,13 +376,16 @@ class Products extends BaseController
// Send JSON response back to the client
return $this->response->setJSON($response);
}
public function get_makes()
{
$BikeMakeModel = new BikeMakeModel(); // Adjust this according to your actual model name
$makes = $BikeMakeModel->findAll();
return $this->response->setJSON($makes);
}
public function save_model() {
public function save_model()
{
$response = [];
$BikemodelsModel = new BikemodelsModel();
@ -409,9 +416,8 @@ class Products extends BaseController
return $this->response->setJSON($response);
}
public function save_manufacturer(){
public function save_manufacturer()
{
$response = [];
$ManufacturerModel = new ManufacturerModel();
@ -443,7 +449,8 @@ class Products extends BaseController
return $this->response->setJSON($response);
}
public function product_list_for_service(){
public function product_list_for_service()
{
$ProductModel = new ProductModel();
// Assuming you have a model for products and one for manufacturers
$products = $ProductModel
@ -455,7 +462,6 @@ class Products extends BaseController
return $this->response->setJSON($products);
}
public function status_product()
{
@ -481,4 +487,44 @@ class Products extends BaseController
}
}
//----VENKAT------------------------------------------------------------------------------------------------
public function insertOrUpdateAdditionalProductName($product_names, $product_name_id, $product_id, $first_product_name)
{
$firstProductNameData = [
'product_id' => $product_id,
'additional_product_name' => $first_product_name,
];
// Delete existing records for the given product ID
$this->additionalProductNameModal->where('product_id', $product_id)->delete();
// Insert the first product name (if any)
$this->additionalProductNameModal->insert($firstProductNameData);
$productNameData = [];
// If there are additional product names, loop through and prepare them
if (!empty($product_names) && count($product_names) > 0) {
foreach ($product_names as $name) {
if (!empty($name)) {
$productNameData[] = [
'product_id' => $product_id,
'additional_product_name' => $name,
];
}
}
// Insert additional product names in batch if there are valid entries
if (!empty($productNameData) && count($product_names) > 0) {
$this->additionalProductNameModal->insertBatch($productNameData);
}
}
return true;
}
}

View File

@ -11,22 +11,27 @@ use App\Models\BikemakeModel;
use App\Models\VehicleModel;
use App\Models\BusinessModel;
use App\Models\BranchModel;
use App\Models\AdditionalProductNameModal;
use Mpdf\Mpdf;
use CodeIgniter\API\ResponseTrait;
use App\Helpers\InvNoHelper;
class Sales extends BaseController
{
public $session;
use ResponseTrait;
protected $BikemodelsModel;
protected $BikemakeModel;
protected $additionalProductNameModal;
public function __construct()
{
$this->session = session();
$this->BikemodelsModel = new BikemodelsModel();
$this->BikemakeModel = new BikemakeModel();
$this->additionalProductNameModal = new AdditionalProductNameModal();
}
public function sales_order_index()
@ -41,7 +46,6 @@ class Sales extends BaseController
return view('sales_list',$data);
}
public function new_sale($sales_order_id)
{
@ -71,8 +75,7 @@ class Sales extends BaseController
} else if ($sales_order_id !== '0')
{
} else if ($sales_order_id !== '0') {
$data['page_name']="Edit Sale Order";
@ -98,27 +101,33 @@ class Sales extends BaseController
// Load the ProductModel
$productModel = new ProductModel();
// Query the database to find the product based on make_id and model_id
$makemodelproduct = $productModel->select('products.*, manufacturer.manufacturer_name')
// Query the database to find the product based on make_id and model_id
$makeproduct = $this->additionalProductNameModal->select('products.*, manufacturer.manufacturer_name, 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(make_id, \'' . $encodedMakeId . '\')', null, false)
->where('JSON_CONTAINS(models_id, \'' . $encodedModelId . '\')', null, false)
->where('qty_stock >',0)
->where('products.isactive', 1)
->findAll();
$encodedMakeId = json_encode(['0']);
$encodedModelId = json_encode(['0']);
$common_product = $this->additionalProductNameModal->select('products.*, manufacturer.manufacturer_name, 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(make_id, \'' . $encodedMakeId . '\')', null, false)
->where('JSON_CONTAINS(models_id, \'' . $encodedModelId . '\')', null, false)
->where('qty_stock >',0)
->where('products.isactive', 1)
->findAll();
$encodedMakeId1 = json_encode(['0']);
$encodedModelId1 = json_encode(['0']);
$common_product = $productModel->select('products.*, manufacturer.manufacturer_name')
->join('manufacturer', 'manufacturer.manufacturer_id = products.manufacturer_id')
->where('JSON_CONTAINS(make_id, \'' . $encodedMakeId1 . '\')', null, false)
->where('JSON_CONTAINS(models_id, \'' . $encodedModelId1 . '\')', null, false)
->where('qty_stock >',0)
->where('products.isactive',1)
->findAll();
$product = array_merge($common_product, $makemodelproduct);
$product = array_merge($common_product, $makeproduct);
$data['product']=$product;
$data['product']=$product;
$SalesProductOrderModel = new SalesOrderProductModel();
$sales_product=$SalesProductOrderModel->where('sales_order_id', $sales_order_id)->findAll();
$data['sales_product']=$sales_product;
@ -139,7 +148,6 @@ class Sales extends BaseController
return view('sales_order_form',$data);
}
public function add_sales()
{
if(!$this->session->has('logged_user')) { return redirect()->to(base_url()); }
@ -518,7 +526,8 @@ class Sales extends BaseController
// Query the database to find the product based on make_id and model_id
$makeproduct = $productModel->select('products.*, manufacturer.manufacturer_name')
$makeproduct = $this->additionalProductNameModal->select('products.*, manufacturer.manufacturer_name, 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(make_id, \'' . $encodedMakeId . '\')', null, false)
->where('JSON_CONTAINS(models_id, \'' . $encodedModelId . '\')', null, false)
@ -529,15 +538,18 @@ class Sales extends BaseController
$encodedMakeId = json_encode(['0']);
$encodedModelId = json_encode(['0']);
$common_product = $productModel->select('products.*, manufacturer.manufacturer_name')
$common_product = $this->additionalProductNameModal->select('products.*, manufacturer.manufacturer_name, 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(make_id, \'' . $encodedMakeId . '\')', null, false)
->where('JSON_CONTAINS(models_id, \'' . $encodedModelId . '\')', null, false)
->where('qty_stock >',0)
->where('products.isactive', 1)
->findAll();
$product = array_merge($common_product, $makeproduct);
$modelName = $this->BikemodelsModel->where('model_id',$modelId)->get()->getRow()->model_name;
$makeName = $this->BikemakeModel->where('make_id',$makeId)->get()->getRow()->make;
if($product){

View File

@ -0,0 +1,25 @@
<?php
namespace App\Models;
use CodeIgniter\Model;
class AdditionalProductNameModal extends Model
{
protected $table = 'product_names';
protected $primaryKey = 'id';
protected $useAutoIncrement = true;
protected $returnType = 'array';
protected $useSoftDeletes = false;
protected $protectFields = true;
protected $allowedFields = [
'product_names_id',
'product_id',
'additional_product_name',
'isactive',
'created_at',
'updated_at',
];
}

View File

@ -232,7 +232,9 @@
<label for="description" class="col-form-label">Description</label>
<textarea class="form-control" name="description" placeholder="Description"value="<?= isset($products['description']) ? $products['description'] : '' ?>" ></textarea>
</div>
</div>
</div><br>
<h4 class="header-title">Additional Product Names :</h4><br>
<div id="dynamic-form-container"></div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
@ -364,7 +366,33 @@ $(document).ready(function() {
$("input[name='hiddenproductcategory']").val($(this).val());
});
//---------VENKAT-----------------------------------------------------------------------------------
// for Additional Product Name Edit Purpose
var additionalProductName = <?= json_encode($additionalProdutName) ?>;
// console.log(additionalProductName);
// console.log(additionalProductName.length);
if (Array.isArray(additionalProductName) && additionalProductName.length > 0) {
var firstProductName = '<?= isset($products['product_name']) ? $products['product_name'] : '' ?>';
console.log('firstProductName', firstProductName);
additionalProductName.forEach(function(item) {
if (firstProductName !== item.additional_product_name) {
addHTMLInput(item); // for if edit
}
});
if(additionalProductName.length == 1){
addHTMLInput()
}
} else {
addHTMLInput(); // for else add
}
// end of Additional Product Name Edit Purpose
//------------------------------------------------------------------------------------------------
});
/** CALCULATE TOTAL AMOUNT ( UNIT PRICE + TAX ) */
@ -863,4 +891,40 @@ $('#saveMakeBtn').click(function() {
.select2-container--default .select2-selection--single .select2-selection__arrow{
top:4px;
}
</style>
</style>
<!-- Script for add multiple product name -->
<script>
function addHTMLInput(data = null)
{
const container = document.getElementById('dynamic-form-container');
const newRow = document.createElement('div');
newRow.className = 'form-row dynamic-form-row';
newRow.innerHTML = `
<input value="${data != null && data.product_names_id != null ? data.product_names_id : ''}" type="hidden" name="product_names_id[]" >
<div class="form-group col-md-6">
<input value="${data != null && data.additional_product_name != null ? data.additional_product_name : ''}" type="text" class="form-control" id="additional_product_name" name="additional_product_name[]" placeholder="Enter Additional Product name">
</div>
<div class="form-group col-md-6">
<a class="btn btn-danger waves-effect waves-light" onclick="removeHTMLInput(this)">x</a>
<a class="btn btn-primary waves-effect waves-light mr-1" onclick="addHTMLInput(this)">+</a>
</div>
`;
container.appendChild(newRow);
}
function removeHTMLInput(element)
{
const container = document.getElementById('dynamic-form-container');
const rows = container.querySelectorAll('.dynamic-form-row');
if (rows.length > 1) {
const row = element.closest('.dynamic-form-row');
row.remove();
}
}
</script>

View File

@ -196,7 +196,7 @@
<?php if ((int)$value["isactive"] === 1) : ?>
<option value="<?= $value["product_id"] ?>"
<?= isset($salechild['product_id']) && $salechild['product_id'] == $value["product_id"] ? 'selected' : '' ?>>
<?= $value["product_name"].$value_ml.' - '.$value["manufacturer_name"]; ?>
<?= $value["additional_product_name"].$value_ml.' - '.$value["manufacturer_name"]; ?>
</option>
<?php endif; ?>
<?php endforeach; ?>
@ -721,7 +721,7 @@ $(document).ready(function() {
var product = productarray[i];
if(!selected_pr.includes(product.product_id))
{
newRow += '<option value="' + product.product_id + '" >' + product.product_name + value_ml + " - " + product.manufacturer_name+
newRow += '<option value="' + product.product_id + '" >' + product.additional_product_name + value_ml + " - " + product.manufacturer_name+
'</option>';
}
}