session = session(); } public function product_index($manufacturer_id = null) { if(!$this->session->has('logged_user')) { return redirect()->to(base_url()); } if ($manufacturer_id) { $ProductModel = new ProductModel(); $ManufacturerModel = new ManufacturerModel(); $products = $ProductModel->where('manufacturer_id',$manufacturer_id)->orderBy('product_name')->findAll(); $manufacturer = $ManufacturerModel->where('manufacturer_id',$manufacturer_id)->select('manufacturer_name')->first(); $data['products']=$products; $data['manufacturer_id'] = $manufacturer_id; $data['manufacturer_name'] = $manufacturer['manufacturer_name']; return view('product_list',$data); } else { $ProductModel = new ProductModel(); $products = $ProductModel->get_products($this->session->get('logged_user_branch_id')); $data['products']=$products; // echo "
";
// print_r($products);die;
$data['manufacturer_id'] = $manufacturer_id;
return view('product_list',$data);
}
}
public function new_product($product_id)
{
if(!$this->session->has('logged_user')) { return redirect()->to(base_url()); }
if ($product_id === '0') {
$data['products'] = [];
$ProductModel = new ProductModel();
$tax=$ProductModel->getTax();
$data['tax']=$tax;
$BikemakeModel = new BikemakeModel();
$data['make'] = $BikemakeModel->where('business_id', $this->session->get('logged_user_business_id'))->orderBy('make')->findAll();
$VendorModel = new VendorModel();
$data['vendors']=$VendorModel->orderBy('vendor_name')->findAll();
$ManufacturerModel = new ManufacturerModel();
$data['manufacturers']=$ManufacturerModel->where('business_id', $this->session->get('logged_user_business_id'))->orderBy('manufacturer_name')->findAll();
$data['page_name']="Add Product";
$data['vendorsProduct'] =[];
} else if ($product_id !== '0') {
$data['page_name']="Edit Product";
$ProductModel = new ProductModel();
$products=$ProductModel->where('product_id', $product_id)->get()->getRowArray();
$products['make_id'] = json_decode($products['make_id'], true);
$products['models_id'] = json_decode($products['models_id'], true);
$data['products']=$products;
$BikemakeModel = new BikemakeModel();
$data['make'] = $BikemakeModel->where('business_id', $this->session->get('logged_user_business_id'))->orderBy('make')->findAll();
$BikemodelsModel = new BikemodelsModel();
$make_id = $products['make_id'];
$BikemodelsModel = new BikemodelsModel();
$models = [];
if( $products['make_id'] != null) {
foreach ($products['make_id'] as $make_id) {
// $models[] = $BikemodelsModel->where('make_id', $make_id)->findAll();
$models[] = $BikemodelsModel->where('make_id', $make_id)->orderBy('model_name')->findAll();
}
}
$data['models'] = $models;
$tax=$ProductModel->getTax();
$data['tax']=$tax;
$VendorModel = new VendorModel();
$data['vendors']=$VendorModel->orderBy('vendor_name')->findAll();
$ManufacturerModel = new ManufacturerModel();
$data['manufacturers']=$ManufacturerModel->where('business_id', $this->session->get('logged_user_business_id'))->orderBy('manufacturer_name', 'ASC')->findAll();
$vendorIds = json_decode($products['vendor'], true);
$vendors = [];
$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['product_id'] = $product_id;
// echo "";
// print_r($data['manufacturers']);die;
echo view('product_form',$data);
}
public function add_product()
{
if(!$this->session->has('logged_user')) { return redirect()->to(base_url()); }
$ProductModel = new ProductModel();
$tax= $this->request->getPost('tax')/2;
// print_r($this->request->getPost());die;
if ($this->request->getPost('per') != 'ml') {
$ml = 0;
}else{
$ml = $this->request->getPost('ml');
}
$data = [
'product_name' => $this->request->getPost('productname'),
'product_category' => $this->request->getPost('productcategory'),
'mfr_part_no' => $this->request->getPost('mfr_part_no'),
'unit_price' => $this->request->getPost('unit_price'),
// 'commision_rate' => $this->request->getPost('commision_rate'),
'sgst' => $tax,
'cgst' => $tax,
'purchase_cost' => $this->request->getPost('purchase_cost'),
'qty_stock'=> $this->request->getPost('qty_in_stock'),
'qty_in_demand'=> $this->request->getPost('qty_in_demand'),
'description' => $this->request->getPost('description'),
'vendor' =>json_encode( $this->request->getPost('vendor')),
'purchase_order_level' => $this->request->getPost('purchase_order_level'),
'reorder_level' => $this->request->getPost('reorder_level'),
'quality' => $this->request->getPost('quality'),
'make_id' => json_encode($this->request->getPost('make')),
'models_id' => json_encode($this->request->getPost('model')),
'rack_number' => $this->request->getPost('rack_number'),
'manufacturer_id' => $this->request->getPost('manufacturer'),
'prefered_vendor' => $this->request->getPost('prefered_vendor'),
'specification' => $this->request->getPost('specification'),
'hsn_sac' => $this->request->getPost('hsn_sac'),
'isactive' => 1 ,
'branch_id' => $this->session->get('logged_user_branch_id'),
'total_amount' => $this->request->getPost('total_amount'),
'per' => $this->request->getPost('per'),
'ml' => $ml
];
$product_id = $this->request->getPost('product_id');
if (!empty($product_id)) {
$ProductModel->update($product_id, $data);
} else {
$ProductModel->insert($data);
$product_id = $ProductModel->getInsertID();
}
$orderNumber = 'TM' . str_pad($product_id, 8, '0', STR_PAD_LEFT);
// Update sales order with generated order number
$ProductModel->update($product_id, ['sku_id' => $orderNumber]);
return redirect()->to('product_index');
}
public function delete_product($product_id)
{
if(!$this->session->has('logged_user')) { return redirect()->to(base_url()); }
$model = new ProductModel();
$where = ['product_id' => $product_id, 'isactive' => 1 , 'branch_id' => $this->session->get('logged_user_branch_id')]; // Assuming 'isactive' is a column in your database
$existingProduct = $model->where($where)->first();
if ($existingProduct) {
$data['isactive'] = 0;
if ($model->update($product_id, $data)) {
session()->setFlashdata('success', 'Deleted successfully.');
$this->logger->info("Users: has been Inactivated successfully. Inactivated ID = ".$product_id);
}
}
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 = [];
foreach ($model_ids as $modelKey => $modelValue)
{
foreach ($Vendors as $vendorsKey => $vendorsValue)
{
print_r($vendorsValue);die;
$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([]);
}
}else {
return $this->response->setJSON([]);
}
}
public function get_models()
{
$make_ids = $this->request->getPost('make_id'); // Assuming you are sending make_id via POST
$BikemodelsModel = new BikemodelsModel(); // Adjust this according to your actual model name
$formatted_models = [];
// Iterate over each selected make ID
if ($make_ids) {
foreach ($make_ids as $make_id) {
// Fetch models based on the selected make ID
$models = $BikemodelsModel->where('make_id', $make_id)->where('business_id', $this->session->get('logged_user_business_id'))->orderBy('model_name')->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;
}
}
}
return $this->response->setJSON($formatted_models);
}
public function save_make() {
$response = []; // Initialize an empty array to hold the response data
// Assuming BikemakeModel and BikemodelsModel are properly defined
$BikemakeModel = new BikemakeModel();
$BikemodelsModel = new BikemodelsModel();
$make = $this->request->getPost('make');
$model = $this->request->getPost('model');
// Check if both make and model are provided
if (!empty($make) && !empty($model)) {
$data = [
'make' => $make,
'business_id' => $this->session->get('logged_user_business_id'),
];
// Insert the make into the database
$BikemakeModel->insert($data);
$make_id = $BikemakeModel->getInsertID();
$modelData = [
'model_name' => $model,
'make_id' => $make_id,
'business_id' => $this->session->get('logged_user_business_id'),
];
// Insert the model into the database
$BikemodelsModel->insert($modelData);
// Set success status in the response
$response['success'] = true;
} else {
// If make or model is empty, set success status to false
$response['success'] = false;
}
// 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() {
$response = [];
$BikemodelsModel = new BikemodelsModel();
$make_id = $this->request->getPost('make_id');
$model = $this->request->getPost('modelvalue');
if (!empty($make_id) && !empty($model)) {
$modelData = [
'model_name' => $model,
'make_id' => $make_id,
'business_id' => $this->session->get('logged_user_business_id'),
];
$model_id = $BikemodelsModel->insert($modelData);
$modelData = $BikemodelsModel->where('make_id',$make_id)->findAll();
$response['success'] = true;
$response['model_id'] = $model_id;
$response['model_value'] = $model;
} else {
$response['success'] = false;
}
return $this->response->setJSON($response);
}
public function save_manufacturer(){
$response = [];
$ManufacturerModel = new ManufacturerModel();
$manufacturer_name = $this->request->getPost('manufacturer_name');
$quality = $this->request->getPost('quality');
if (!empty($manufacturer_name) && !empty($quality)) {
$modelData = [
'manufacturer_name' => $manufacturer_name,
'quality' => $quality,
'business_id'=> $this->session->get('logged_user_business_id')
];
$manufacturer_id = $ManufacturerModel->insert($modelData);
$modelData = $ManufacturerModel->where('business_id', $this->session->get('logged_user_business_id'))->orderBy('manufacturer_name')->findAll();
$response['success'] = true;
$response['manufacturer_id'] = $manufacturer_id;
$response['quality'] = $quality;
$response['modelData'] = $modelData;
} else {
$response['success'] = false;
}
return $this->response->setJSON($response);
}
public function product_list_for_service(){
$ProductModel = new ProductModel();
// Assuming you have a model for products and one for manufacturers
$products = $ProductModel
->select('products.*, manufacturer.manufacturer_name')
->join('manufacturer', 'manufacturer.manufacturer_id = products.manufacturer_id')
->where('products.branch_id', $this->session->get('logged_user_branch_id'))
->findAll();
return $this->response->setJSON($products);
}
public function status_product()
{
try {
$product_id = $this->request->getPost('product_id');
$isactive = $this->request->getPost('isactive');
$ProductModel = new ProductModel();
$data = [
'isactive' => $isactive
];
$updated = $ProductModel->update($product_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);
}
}
}