the_mechanic/app/Controllers/ProductCategory.php

119 lines
4.4 KiB
PHP

<?php
namespace App\Controllers;
use App\Models\ProductCategoryModel;
use CodeIgniter\API\ResponseTrait;
class ProductCategory extends BaseController
{
public $session;
use ResponseTrait;
public function __construct()
{
$this->session = session();
}
public function index()
{
$ProductCategoryModel = new ProductCategoryModel();
$productcategory = $ProductCategoryModel->orderBy('product_category_id')->findAll();
$data['productcategory'] = $productcategory;
return view('product_category_list',$data);
}
public function create_product_category($product_category_id)
{
$logged_user = $this->session->get('logged_user');
try {
$product_category_name = $this->request->getPost('product_category_name');
$ProductCategoryModel = new ProductCategoryModel();
$data = [
'product_category_name' => $product_category_name
];
if (!$product_category_id) {
try {
$data['business_id'] = $this->session->get('logged_user_business_id');
$data['created_by'] = $logged_user;
// Check for duplicate entry
$existing = $ProductCategoryModel->where('product_category_name', $product_category_name)->first();
if ($existing) {
return $this->respond([
'status' => 'failed',
'code' => 200,
'data' => 'Duplicate entry detected.'
], 200);
}
// Insert data if no duplicate found
$inserted = $ProductCategoryModel->insert($data);
} catch (\CodeIgniter\Database\Exceptions\DatabaseException $e) {
return $this->respond(['status' => 'failed', 'code' => 500, 'data' => $e->getMessage()], 500);
}
if ($inserted) {
$result = $data;
$product_category_list = $ProductCategoryModel->orderBy('product_category_id', 'asc')->findAll();
return $this->respond([
'status' => 'success',
'code' => 200,
'data' => $result,
'product_category_list' => $product_category_list
], 200);
} else {
$result = "Insert failed.";
return $this->respond(['status' => 'failed', 'code' => 400, 'data' => $result], 400);
}
}else{
$data['updated_by'] = $logged_user;
$updated = $ProductCategoryModel->update($product_category_id,$data);
if ($updated) {
$result = $data;
return $this->respond(['status' => 'success','code' => 200,'data' => $result],200);
} else {
$result = "No Match's";
return $this->respond(['status' => 'failed','code' => 404,'data' => $result],404);
}
}
} catch (\Exception $e) {
return $this->respond(['status' => 'failed','code' => 500,'data' => $e],500);
}
}
public function status_product_category()
{
try {
$product_category_id = $this->request->getPost('productcategory_id');
$isactive = $this->request->getPost('isactive');
$updated_by = $this->session->get('logged_user');
$ProductCategoryModel = new ProductCategoryModel();
$data = [
'isactive' => $isactive,
'updated_by' => $updated_by
];
$updated = $ProductCategoryModel->update($product_category_id, $data);
if ($updated) {
$result = $data;
return $this->respond(['status' => 'success','code' => 200,'data' => $result],200);
} else {
$result = "No Match's";
return $this->respond(['status' => 'failed','code' => 404,'data' => $result],404);
}
} catch (\Exception $exception) {
return $this->respond(['status' => 'failed','code' => 500,'data' => $exception],500);
}
}
}