diff --git a/app/Controllers/Books.php b/app/Controllers/Books.php index 5a165bfb..97ba44b5 100755 --- a/app/Controllers/Books.php +++ b/app/Controllers/Books.php @@ -120,7 +120,6 @@ class Books extends BaseController if (empty($book_id)) { // It's an insert operation $data['isactive'] = 1; - $data['category'] = 1; $data['created_by'] = $session_uid; if ($BooksModel->insert($data)) { session()->setFlashdata('success', 'Book has been added successfully.'); @@ -172,7 +171,7 @@ class Books extends BaseController $this->logger->Info("BOOK: Images Going to Inactived = ".implode(" ",$bookImgIds)); $inactive_img_whereIn[0] = 'book_img_id'; $inactive_img_whereIn[1] = $bookImgIds; - $BooksModel->inactiveMissingDetails('book_images',$where, $inactive_img_whereIn); + $BooksModel->inactiveMissingDetails('book_images',$data,$where, $inactive_img_whereIn); } } $activeBookCategory = $BooksModel->getData('book_categories', $where); @@ -184,7 +183,7 @@ class Books extends BaseController $this->logger->Info("BOOK: Categories Going to Inactived = ".implode(" ",$bookCategoryIds)); $inactive_category_whereIn[0] = 'id'; $inactive_category_whereIn[1] = $bookCategoryIds; - $BooksModel->inactiveMissingDetails('book_categories',$where, $inactive_category_whereIn); + $BooksModel->inactiveMissingDetails('book_categories',$data,$where, $inactive_category_whereIn); } } diff --git a/app/Controllers/Scheme.php b/app/Controllers/Scheme.php index 91ede95e..665767f2 100644 --- a/app/Controllers/Scheme.php +++ b/app/Controllers/Scheme.php @@ -57,6 +57,7 @@ class Scheme extends BaseController ## For inserting/updating details of scheme public function insert_scheme() { + try{ helper('session'); $session_bid = get_business_id(); $session_uid = get_logged_user_id(); @@ -76,30 +77,32 @@ class Scheme extends BaseController if (empty($scheme_id)) { // It's an insert operation $data['isactive'] = 1; - $data['created_by'] = $session_uid; - $SchemeModel->insert($data); - // $last_inserted_id = 100; - $last_inserted_id = $SchemeModel->insertID(); - - // Retrieve the category name 'membership' - $category_name = 'membership'; - - // Find the category ID for 'membership' from the category table - $db = db_connect(); - $category = $db->table('category') - ->select('id') - ->where('name', $category_name) - ->get() - ->getRow(); - - // Check if the category exists - if ($category) { - // Insert data into book_categories table - $db->table('book_categories')->insert([ - 'book_id' => $last_inserted_id, - 'category_id' => $category->id - ]); + if ($SchemeModel->insert($data)) { + $last_inserted_id = $SchemeModel->insertID(); + // Retrieve the category name 'membership' + $category_name = 'membership'; + // Find the category ID for 'membership' from the category table + $db = db_connect(); + $category = $db->table('category') + ->select('id') + ->where('name', $category_name) + ->get() + ->getRow(); + // Check if the category exists + if ($category) { + // Insert data into book_categories table + $db->table('book_categories')->insert([ + 'book_id' => $last_inserted_id, + 'category_id' => $category->id, + 'created_by' => $session_uid + ]); + } + session()->setFlashdata('success', 'Scheme has been added successfully.'); + $this->logger->info("Scheme: has been added successfully. Inserted ID = " . $last_inserted_id); + } else { + session()->setFlashdata('error', 'Scheme could not be added. Please try again..'); + $this->logger->error("Scheme: Err could not be added. Please try again."); } } else { @@ -107,37 +110,59 @@ class Scheme extends BaseController $isactive = $this->request->getPost('isactive'); $data['isactive'] = ($isactive == 'on') ? 1 : 0; $data['updated_by'] = $session_uid; - $SchemeModel->update($scheme_id, $data); + if ($SchemeModel->update($scheme_id, $data)) { + session()->setFlashdata('success', 'Scheme has been updated successfully.'); + $this->logger->info("Scheme: has been updated successfully. Updated Scheme ID = " . $scheme_id); + } else { + session()->setFlashdata('error', 'Scheme update failed. Please try again.'); + $this->logger->error("Scheme: Err Failed to update ID =" . $scheme_id); + } } + } catch (\Exception $e) { + $this->logger->error("Scheme: Err Occur = ".$e->getMessage()." File = ". $e->getFile() . " Line = " . $e->getLine()); + session()->setFlashdata('error', 'Message: ' . $e->getMessage()); + } return redirect()->route('scheme_list'); } ## For delete the Scheme details (Which means inactive the details) public function delete_scheme($id) { - $SchemeModel = new SchemeModel(); - - // Check if the business ID exists - $existingScheme = $SchemeModel->find($id); - - if (!$existingScheme) { - // return redirect()->to(base_url('Business/index'))->with('error', 'Business not found.'); - return redirect()->route('scheme_list'); + try { + helper('session'); + $session_uid = get_logged_user_id(); + $SchemeModel = new SchemeModel(); + $where = ['book_id' => (int)$id, 'isactive =' => 1]; + // $existingScheme = $SchemeModel->find($id); + $existingScheme = $SchemeModel->where($where)->find($id); + if ($existingScheme) { + $this->logger->Info("Scheme: Going to Inactive ID = ".$id); + $data = ['isactive' => 0 , 'updated_by' => $session_uid]; + if ($SchemeModel->update($id, $data)) { + $activeSchemeCategory = $SchemeModel->getData('book_categories', $where); + if(!empty($activeSchemeCategory)){ + for ($z = 0; $z < count($activeSchemeCategory); $z++) { + $SchemeCategoryIds[$z] = $activeSchemeCategory[$z]->id; + } + if(!empty($SchemeCategoryIds)){ + $this->logger->Info("Scheme: Categories Going to Inactived = ".implode(" ",$SchemeCategoryIds)); + $inactive_category_whereIn[0] = 'id'; + $inactive_category_whereIn[1] = $SchemeCategoryIds; + $SchemeModel->inactiveMissingDetails('book_categories',$data,$where, $inactive_category_whereIn); + } + } + + session()->setFlashdata('success', 'Deleted successfully.'); + $this->logger->info("Scheme: has been Inactived successfully. Inactived ID = " . $id); + } else { + $this->logger->error("Scheme: Not able to Inactive ID =" . $id); + throw new \Exception("Data Not able to Deleted"); + } + } + } catch (\Exception $e) { + $this->logger->error("Scheme: Err Occur = ".$e->getMessage()." File = ". $e->getFile() . " Line = " . $e->getLine()); + session()->setFlashdata('error', 'Message: ' . $e->getMessage()); } - - helper('session'); - $session_uid = get_logged_user_id(); - // Delete the business record - $data['isactive'] = 0; - $data['updated_by'] = $session_uid; - //echo 'id='.$id; - print_r($data); - //die(); - $SchemeModel->update($id, $data); - // print_r($data);die(); - //$BusinessModel->delete($id); - - // return redirect()->to(base_url('Business/index'))->with('success', 'Business deleted successfully.'); return redirect()->route('scheme_list'); } } diff --git a/app/Models/BooksModel.php b/app/Models/BooksModel.php index 4a7d9ca2..98a6211d 100644 --- a/app/Models/BooksModel.php +++ b/app/Models/BooksModel.php @@ -71,9 +71,17 @@ public function getData($table, $where = null,$whereIn = null) return $query->get()->getResult(); } -public function inactiveMissingDetails($table_name,$where,$whereIn){ - $dataToUpdate = ['isactive' => 0]; - $this->db->table($table_name)->where($where)->whereIn($whereIn[0],$whereIn[1])->update($dataToUpdate); -} + public function inactiveMissingDetails($table_name,$dataToUpdate,$where = null,$whereIn = null) + { + $this->db->table($table_name); + $query = $this->db->table($table_name); + if ($where) { + $query->where($where); + } + if ($whereIn) { + $query->whereIn($whereIn[0], $whereIn[1]); + } + return $query->update($dataToUpdate); + } } diff --git a/app/Models/SchemeModel.php b/app/Models/SchemeModel.php index 513bc406..ab3e1fa5 100644 --- a/app/Models/SchemeModel.php +++ b/app/Models/SchemeModel.php @@ -9,7 +9,7 @@ class SchemeModel extends Model { protected $table = 'books'; protected $primaryKey = 'book_id'; - protected $allowedFields = ['book_id ','title','sku','description','price','features','duration','business_id','updated_by','category','isactive']; + protected $allowedFields = ['book_id ','title','sku','description','price','features','duration','business_id','updated_by','category','isactive','created_by']; public function insertScheme($data) { @@ -25,6 +25,7 @@ class SchemeModel extends Model $builder->join('category', 'category.id = book_categories.category_id', 'left'); $builder->where('books.business_id', $business_id); $builder->where('category.name', 'membership'); + $builder->where('books.isactive', 1); $query = $builder->get(); @@ -109,6 +110,32 @@ public function getAllSchemes() { return $this->findAll(); } + + public function getData($table, $where = null, $whereIn = null) + { + $query = $this->db->table($table); + if ($where) { + $query->where($where); + } + if ($whereIn) { + // $query->whereIn($column_name,$missingValues) + $query->whereIn($whereIn[0], $whereIn[1]); + } + return $query->get()->getResult(); + } + + public function inactiveMissingDetails($table_name,$dataToUpdate,$where = null,$whereIn = null) + { + $this->db->table($table_name); + $query = $this->db->table($table_name); + if ($where) { + $query->where($where); + } + if ($whereIn) { + $query->whereIn($whereIn[0], $whereIn[1]); + } + return $query->update($dataToUpdate); + } } diff --git a/app/Views/scheme_list.php b/app/Views/scheme_list.php index 8f25239f..372adbbe 100644 --- a/app/Views/scheme_list.php +++ b/app/Views/scheme_list.php @@ -7,6 +7,24 @@

+ getFlashdata('success') || session()->getFlashdata('error')) : ?> + getFlashdata('success')) : ?> + + + getFlashdata('error')) : ?> + + +