GIF89a; CRX
KBHT HEHE
Server IP : 172.26.0.195  /  Your IP : 18.224.31.82
Web Server : Apache
System : Linux 43-205-77-33.cprapid.com 3.10.0-1160.119.1.el7.tuxcare.els2.x86_64 #1 SMP Mon Jul 15 12:09:18 UTC 2024 x86_64
User : jnclnmuac ( 1026)
PHP Version : 8.0.30
Disable Function : NONE
MySQL : OFF  |  cURL : ON  |  WGET : ON  |  Perl : ON  |  Python : ON
Directory (0755) :  /home/jnclnmuac/public_html/web/pdf/../css/../../cas/application/controllers/admin/

[  Home  ][  C0mmand  ][  Upload File  ]

Current File : /home/jnclnmuac/public_html/web/pdf/../css/../../cas/application/controllers/admin/Inventory.php
<?php

/**
 * Controller Class For Handling All Requests Related To Inventories
 *
 * @author Softpro India Pvt. Ltd.
 */
defined('BASEPATH') OR exit('No direct script access allowed');

class Inventory extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('admin/InventoryManagement');
        $this->load->model('admin/StockAndPurchaseManagement');
    }

    public function index() {
        if ($this->sessionvalidator->isLoggedIn() && $this->sessionvalidator->isAccessGranted()) {
            $viewData['allInventoryCategories'] = $this->InventoryManagement->getAllInventoryCategories()->result();
            $this->load->view('admin/stockAndInventory/inventoryCategories', $viewData);
        } else {
            redirect("admin/");
        }
    }

    public function createInventoryCategory() {
        if ($this->sessionvalidator->isLoggedIn()) {
            $this->load->view('admin/stockAndInventory/createInventoryCategory');
        } else {
            redirect("admin/");
        }
    }

    public function saveNewInventoryCategory() {
        if ($this->sessionvalidator->isLoggedIn()) {
            $this->form_validation->set_rules('invCatName', 'Category Name', 'trim|required', array('required' => 'Inventory Category Name Can Not Be Blank.'));
            $this->form_validation->set_rules('invCatShortName', 'Category Short Name', 'trim|required', array('required' => 'Inventory Category Short Name Can Not Be Blank.'));
            $queryByInventoryCategoryName = $this->InventoryManagement->getInventoryCategoryInfoByName(trim($this->input->post('invCatName')));
            $inventoryCategoryInfoByName = $queryByInventoryCategoryName->result();
            $queryByInventoryCategoryShortName = $this->InventoryManagement->getInventoryCategoryInfoByShortName(trim($this->input->post('invCatShortName')));
            $inventoryCategoryByShortName = $queryByInventoryCategoryShortName->result();
            if ($this->form_validation->run() == FALSE) {
                $this->createInventoryCategory();
            } else if (sizeof($inventoryCategoryInfoByName)) {
                $this->session->set_flashdata('errorMessage', "An Inventory Category With This Name Already Exits. Please choose A Different Name.");
                $this->createInventoryCategory();
            } else if (sizeof($inventoryCategoryByShortName)) {
                $this->session->set_flashdata('errorMessage', "An Inventory Category With This Short Name Already Exits. Please Choose A Different Abbreviation.");
                $this->createInventoryCategory();
            } else {
                $newInventoryCategoryInfo = array(
                    'icm_name' => addslashes(trim($this->input->post('invCatName'))),
                    'icm_short_name' => addslashes(trim($this->input->post('invCatShortName'))),
                    'icm_description' => addslashes(trim($this->input->post('invCatDescription'))),
                    'icm_added_on' => date("Y-m-d H:i:s"),
                    'icm_added_by' => $this->session->userdata("adminData")["smember_id"],
                    'icm_updated_on' => date("Y-m-d H:i:s"),
                    'icm_updated_by' => $this->session->userdata("adminData")["smember_id"]
                );
                if ($this->InventoryManagement->createNewInventoryCategory($newInventoryCategoryInfo)) {
                    $this->session->set_flashdata('successMessage', 'Inventory Category Created Successfully. <a href="' . site_url("admin/Inventory/createInventorySubCategory") . '">Click Here</a> To Add Inventory Sub-Category.');
                    redirect("admin/Inventory");
                } else {
                    $this->session->set_flashdata('errorMessage', 'An Error Occured While Creating Inventory Category. Try Later.');
                    redirect("admin/Inventory");
                }
            }
        } else {
            redirect("admin/");
        }
    }

    public function editInventoryCategory($icm_id) {
        if ($this->sessionvalidator->isLoggedIn()) {
            $viewData['inventoryCategoryInfo'] = $this->InventoryManagement->getInventoryCategoryInfoBy($icm_id)->result()[0];
            $this->load->view('admin/stockAndInventory/editInventoryCategory', $viewData);
        } else {
            redirect("admin/");
        }
    }

    public function updateInventoryCategory() {
        if ($this->sessionvalidator->isLoggedIn()) {
            $icm_id = $this->input->post('icmId');
            $this->form_validation->set_rules('invCatName', 'Category Name', 'trim|required', array('required' => 'Inventory Category Name Can Not Be Blank.'));
            $this->form_validation->set_rules('invCatShortName', 'Category Short Name', 'trim|required', array('required' => 'Inventory Category Short Name Can Not Be Blank.'));
            if ($this->form_validation->run() == FALSE) {
                $this->editInventoryCategory($icm_id);
            } else if (!$this->InventoryManagement->isInventoryCategoryNameSafeUpdate($icm_id, trim($this->input->post('invCatName')))) {
                $this->session->set_flashdata('errorMessage', "An Inventory Category With This Name (" . trim($this->input->post('invCatName')) . ") Already Exits. Please choose A Different Name.");
                $this->editInventoryCategory($icm_id);
            } else if (!$this->InventoryManagement->isInventoryCategoryShortNameSafeUpdate($icm_id, trim($this->input->post('invCatShortName')))) {
                $this->session->set_flashdata('errorMessage', "An Inventory Category With This Short Name (" . trim($this->input->post('invCatShortName')) . ") Already Exits. Please Choose A Different Abbreviation.");
                $this->editInventoryCategory($icm_id);
            } else {
                $inventoryCategoryInfo = array(
                    'icm_id' => $icm_id,
                    'icm_name' => addslashes(trim($this->input->post('invCatName'))),
                    'icm_short_name' => addslashes(trim($this->input->post('invCatShortName'))),
                    'icm_description' => addslashes(trim($this->input->post('invCatDescription'))),
                    'icm_updated_on' => date("Y-m-d H:i:s"),
                    'icm_updated_by' => $this->session->userdata("adminData")["smember_id"]
                );
                if ($this->InventoryManagement->updateInventoryCategoryInfo($inventoryCategoryInfo)) {
                    $this->session->set_flashdata('successMessage', 'Inventory Category Updated Successfully.');
                    redirect("admin/Inventory");
                } else {
                    $this->session->set_flashdata('errorMessage', 'An Error Occured While Updating Inventory Category. Try Later.');
                    redirect("admin/Inventory");
                }
            }
        } else {
            redirect("admin/");
        }
    }

    public function toggleInventoryCategoryStatus($icm_id, $toUpdateStatus) {
        if ($this->sessionvalidator->isLoggedIn()) {
            $inventoryCategoryUpdateData = array(
                'icm_id' => $icm_id,
                'icm_updated_on' => date("Y-m-d H:i:s"),
                'icm_updated_by' => $this->session->userdata("adminData")["smember_id"],
                'icm_active_status' => $toUpdateStatus
            );
            if ($this->InventoryManagement->updateInventoryCategoryInfo($inventoryCategoryUpdateData)) {
                $this->session->set_flashdata('successMessage', 'Inventory Category Status Updated Successfully.');
                redirect("admin/Inventory");
            } else {
                $this->session->set_flashdata('errorMessage', 'Some Error Occurred While Updating Inventory Category Status. Try Later.');
                redirect("admin/Inventory");
            }
        } else {
            redirect("admin/");
        }
    }

    public function deleteInventoryCategory($icm_id) {
        if ($this->sessionvalidator->isLoggedIn()) {
            $inventoryCategoryUpdateData = array(
                'icm_id' => $icm_id,
                'icm_updated_on' => date("Y-m-d H:i:s"),
                'icm_updated_by' => $this->session->userdata("adminData")["smember_id"],
                'icm_delete_status' => 'T'
            );
            if ($this->InventoryManagement->updateInventoryCategoryInfo($inventoryCategoryUpdateData)) {
                $this->session->set_flashdata('successMessage', 'Inventory Category Deleted Successfully.');
                redirect("admin/Inventory");
            } else {
                $this->session->set_flashdata('errorMessage', 'Some Error Occurred While Deleting Inventory Category. Try Later.');
                redirect("admin/Inventory");
            }
        } else {
            redirect("admin/");
        }
    }

    public function undoDeleteInventoryCategory($icm_id) {
        if ($this->sessionvalidator->isLoggedIn()) {
            $inventoryCategoryUpdateData = array(
                'icm_id' => $icm_id,
                'icm_updated_on' => date("Y-m-d H:i:s"),
                'icm_updated_by' => $this->session->userdata("adminData")["smember_id"],
                'icm_delete_status' => 'F'
            );
            if ($this->InventoryManagement->updateInventoryCategoryInfo($inventoryCategoryUpdateData)) {
                $this->session->set_flashdata('successMessage', 'Inventory Category Recovered Successfully.');
                redirect("admin/Inventory");
            } else {
                $this->session->set_flashdata('errorMessage', 'Some Error Occurred While Recovering Inventory Category. Try Later.');
                redirect("admin/Inventory");
            }
        } else {
            redirect("admin/");
        }
    }

    /* Functions For Inventory Sub-Categories */

    public function inventorySubCategories() {
        if ($this->sessionvalidator->isLoggedIn() && $this->sessionvalidator->isAccessGranted()) {
            $viewData['allInventorySubCategories'] = $this->InventoryManagement->getAllInventorySubCategories()->result();
            $this->load->view('admin/stockAndInventory/inventorySubCategories', $viewData);
        } else {
            redirect("admin/");
        }
    }

    public function createInventorySubCategory() {
        if ($this->sessionvalidator->isLoggedIn()) {
            $viewData['allInventoryCategories'] = $this->InventoryManagement->getAllNonDeletedActiveInventoryCategories()->result();
            $this->load->view('admin/stockAndInventory/createInventorySubCategory', $viewData);
        } else {
            redirect("admin/");
        }
    }

    public function saveNewInventorySubCategory() {
        if ($this->sessionvalidator->isLoggedIn()) {
            $this->form_validation->set_rules('invSubCatName', 'Sub-Category Name', 'trim|required', array('required' => 'Inventory Sub-Category Name Can Not Be Blank.'));
            $this->form_validation->set_rules('invSubCatShortName', 'Sub-Category Short Name', 'trim|required', array('required' => 'Inventory Sub-Category Short Name Can Not Be Blank.'));
            $this->form_validation->set_rules('invCategory', 'Category Name', 'trim|required', array('required' => 'Please Select Main Category Under Which This Sub-Category Will Be Created.'));
            $this->form_validation->set_rules('invSubCatComsume', 'Sub-Category Consumable', 'trim|required', array('required' => 'Please Choose If This Sub-Category Is Consumable.'));
            $queryByInventorySubCategoryName = $this->InventoryManagement->getInventorySubCategoryInfoByName(trim($this->input->post('invSubCatName')));
            $inventorySubCategoryInfoByName = $queryByInventorySubCategoryName->result();
            $queryByInventorySubCategoryShortName = $this->InventoryManagement->getInventorySubCategoryInfoByShortName(trim($this->input->post('invSubCatShortName')));
            $inventorySubCategoryInfoByShortName = $queryByInventorySubCategoryShortName->result();
            if ($this->form_validation->run() == FALSE) {
                $this->createInventorySubCategory();
            } else if (sizeof($inventorySubCategoryInfoByName)) {
                $this->session->set_flashdata('errorMessage', "An Inventory Sub-Category With This Name Already Exits. Please choose A Different Name.");
                $this->createInventorySubCategory();
            } else if (sizeof($inventorySubCategoryInfoByShortName)) {
                $this->session->set_flashdata('errorMessage', "An Inventory Sub-Category With This Short Name Already Exits. Please Choose A Different Abbreviation.");
                $this->createInventorySubCategory();
            } else {
                $newInventorySubCategoryInfo = array(
                    'iscm_name' => addslashes(trim($this->input->post('invSubCatName'))),
                    'iscm_short_name' => addslashes(trim($this->input->post('invSubCatShortName'))),
                    'icm_id' => addslashes(trim($this->input->post('invCategory'))),
                    'iscm_is_consumable' => addslashes(trim($this->input->post('invSubCatComsume'))),
                    'iscm_description' => addslashes(trim($this->input->post('invSubCatDescription'))),
                    'iscm_added_on' => date("Y-m-d H:i:s"),
                    'iscm_added_by' => $this->session->userdata("adminData")["smember_id"],
                    'iscm_updated_on' => date("Y-m-d H:i:s"),
                    'iscm_updated_by' => $this->session->userdata("adminData")["smember_id"]
                );
                if ($this->InventoryManagement->createNewInventorySubCategory($newInventorySubCategoryInfo)) {
                    $this->session->set_flashdata('successMessage', 'Inventory Sub-Category Created Successfully.');
                    redirect("admin/Inventory/inventorySubCategories");
                } else {
                    $this->session->set_flashdata('errorMessage', 'An Error Occured While Creating Inventory Sub-Category. Try Later.');
                    redirect("admin/Inventory/inventorySubCategories");
                }
            }
        } else {
            redirect("admin/");
        }
    }

    public function editInventorySubCategory($iscm_id) {
        if ($this->sessionvalidator->isLoggedIn()) {
            $viewData['allInventoryCategories'] = $this->InventoryManagement->getAllNonDeletedActiveInventoryCategories()->result();
            $viewData['inventorySubCategoryInfo'] = $this->InventoryManagement->getInventorySubCategoryInfoBy($iscm_id)->result()[0];
            $this->load->view('admin/stockAndInventory/editInventorySubCategory', $viewData);
        } else {
            redirect("admin/");
        }
    }

    public function updateInventorySubCategory() {
        if ($this->sessionvalidator->isLoggedIn()) {
            $iscm_id = $this->input->post('iscmId');
            $this->form_validation->set_rules('invSubCatName', 'Sub-Category Name', 'trim|required', array('required' => 'Inventory Sub-Category Name Can Not Be Blank.'));
            $this->form_validation->set_rules('invSubCatShortName', 'Sub-Category Short Name', 'trim|required', array('required' => 'Inventory Sub-Category Short Name Can Not Be Blank.'));
            $this->form_validation->set_rules('invCategory', 'Category Name', 'trim|required', array('required' => 'Please Select Main Category Under Which This Sub-Category Will Be Created.'));
            $this->form_validation->set_rules('invSubCatComsume', 'Sub-Category Consumable', 'trim|required', array('required' => 'Please Choose If This Sub-Category Is Consumable.'));
            if ($this->form_validation->run() == FALSE) {
                $this->editInventorySubCategory($iscm_id);
            } else if (!$this->InventoryManagement->isInventorySubCategoryNameSafeUpdate($iscm_id, trim($this->input->post('invSubCatName')))) {
                $this->session->set_flashdata('errorMessage', "An Inventory Sub-Category With This Name (" . trim($this->input->post('invSubCatName')) . ") Already Exits. Please choose A Different Name.");
                $this->editInventorySubCategory($iscm_id);
            } else if (!$this->InventoryManagement->isInventorySubCategoryShortNameSafeUpdate($iscm_id, trim($this->input->post('invSubCatShortName')))) {
                $this->session->set_flashdata('errorMessage', "An Inventory Sub-Category With This Short Name (" . trim($this->input->post('invSubCatShortName')) . ") Already Exits. Please Choose A Different Abbreviation.");
                $this->editInventorySubCategory($iscm_id);
            } else {
                $inventorySubCategoryInfo = array(
                    'iscm_id' => $iscm_id,
                    'iscm_name' => addslashes(trim($this->input->post('invSubCatName'))),
                    'iscm_short_name' => addslashes(trim($this->input->post('invSubCatShortName'))),
                    'icm_id' => addslashes(trim($this->input->post('invCategory'))),
                    'iscm_is_consumable' => addslashes(trim($this->input->post('invSubCatComsume'))),
                    'iscm_description' => addslashes(trim($this->input->post('invSubCatDescription'))),
                    'iscm_updated_on' => date("Y-m-d H:i:s"),
                    'iscm_updated_by' => $this->session->userdata("adminData")["smember_id"]
                );
                if ($this->InventoryManagement->updateInventorySubCategoryInfo($inventorySubCategoryInfo)) {
                    $this->session->set_flashdata('successMessage', 'Inventory Sub-Category Updated Successfully.');
                    redirect("admin/Inventory/inventorySubCategories");
                } else {
                    $this->session->set_flashdata('errorMessage', 'An Error Occured While Updating Inventory Sub-Category. Try Later.');
                    redirect("admin/Inventory/inventorySubCategories");
                }
            }
        } else {
            redirect("admin/");
        }
    }

    public function toggleInventorySubCategoryStatus($iscm_id, $toUpdateStatus) {
        if ($this->sessionvalidator->isLoggedIn()) {
            $inventorySubCategoryUpdateData = array(
                'iscm_id' => $iscm_id,
                'iscm_updated_on' => date("Y-m-d H:i:s"),
                'iscm_updated_by' => $this->session->userdata("adminData")["smember_id"],
                'iscm_active_status' => $toUpdateStatus
            );
            if ($this->InventoryManagement->updateInventorySubCategoryInfo($inventorySubCategoryUpdateData)) {
                $this->session->set_flashdata('successMessage', 'Inventory Sub-Category Status Updated Successfully.');
                redirect("admin/Inventory/inventorySubCategories");
            } else {
                $this->session->set_flashdata('errorMessage', 'Some Error Occurred While Updating Inventory Sub-Category Status. Try Later.');
                redirect("admin/Inventory/inventorySubCategories");
            }
        } else {
            redirect("admin/");
        }
    }

    public function deleteInventorySubCategory($iscm_id) {
        if ($this->sessionvalidator->isLoggedIn()) {
            $inventorySubCategoryUpdateData = array(
                'iscm_id' => $iscm_id,
                'iscm_updated_on' => date("Y-m-d H:i:s"),
                'iscm_updated_by' => $this->session->userdata("adminData")["smember_id"],
                'iScm_delete_status' => 'T'
            );
            if ($this->InventoryManagement->updateInventorySubCategoryInfo($inventorySubCategoryUpdateData)) {
                $this->session->set_flashdata('successMessage', 'Inventory Sub-Category Deleted Successfully.');
                redirect("admin/Inventory/inventorySubCategories");
            } else {
                $this->session->set_flashdata('errorMessage', 'Some Error Occurred While Deleting Inventory Sub-Category. Try Later.');
                redirect("admin/Inventory/inventorySubCategories");
            }
        } else {
            redirect("admin/");
        }
    }

    public function undoDeleteInventorySubCategory($iscm_id) {
        if ($this->sessionvalidator->isLoggedIn()) {
            $inventorySubCategoryUpdateData = array(
                'iscm_id' => $iscm_id,
                'iscm_updated_on' => date("Y-m-d H:i:s"),
                'iscm_updated_by' => $this->session->userdata("adminData")["smember_id"],
                'iScm_delete_status' => 'F'
            );
            if ($this->InventoryManagement->updateInventorySubCategoryInfo($inventorySubCategoryUpdateData)) {
                $this->session->set_flashdata('successMessage', 'Inventory Sub-Category Recovered Successfully.');
                redirect("admin/Inventory/inventorySubCategories");
            } else {
                $this->session->set_flashdata('errorMessage', 'Some Error Occurred While Recovering Inventory Sub-Category. Try Later.');
                redirect("admin/Inventory/inventorySubCategories");
            }
        } else {
            redirect("admin/");
        }
    }

    public function getActiveNonDeletedAssetsByCategory() {
        $category = $_POST['category'];
        $query = $this->InventoryManagement->getNonDeletedActiveSubCategoriesUnderCategory($category);
        $assetList = $query->result();
        $options = "<option value=''>Select Asset</option>";
        for ($i = 0; $i < (sizeof($assetList)); $i++) {
            $options .= "<option value=" . $assetList[$i]->iscm_id . ">" . stripslashes($assetList[$i]->iscm_name) . "</option>";
        }
        $responseData = array(
            'csrfName' => $this->security->get_csrf_token_name(),
            'csrfHash' => $this->security->get_csrf_hash(),
            'asset_list' => $options
        );
        echo json_encode($responseData);
    }

    public function getActiveNonDeletedAssetsByCategorySelected() {
        $category = $_POST['category'];
        $subCategory = $_POST['sub_category'];
        $query = $this->InventoryManagement->getNonDeletedActiveSubCategoriesUnderCategory($category);
        $assetList = $query->result();
        $options = "<option value=''>Select Asset</option>";
        for ($i = 0; $i < (sizeof($assetList)); $i++) {
            $selected = ($assetList[$i]->iscm_id == $subCategory) ? "selected" : "";
            $options .= "<option value=" . $assetList[$i]->iscm_id . " " . $selected . ">" . stripslashes($assetList[$i]->iscm_name) . "</option>";
        }
        $responseData = array(
            'csrfName' => $this->security->get_csrf_token_name(),
            'csrfHash' => $this->security->get_csrf_hash(),
            'asset_list' => $options
        );
        echo json_encode($responseData);
    }

    /* Functions For Inventory Purchase Heads */

    public function purchaseHeads() {
        if ($this->sessionvalidator->isLoggedIn() && $this->sessionvalidator->isAccessGranted()) {
            $viewData['allPurchaseHeads'] = $this->InventoryManagement->getAllPurchaseHeads()->result();
            $this->load->view('admin/stockAndInventory/purchaseHeads', $viewData);
        } else {
            redirect("admin/");
        }
    }

    public function createPurchaseHead() {
        if ($this->sessionvalidator->isLoggedIn()) {
            $this->load->view('admin/stockAndInventory/createPurchaseHead');
        } else {
            redirect("admin/");
        }
    }

    public function saveNewPurchaseHead() {
        if ($this->sessionvalidator->isLoggedIn()) {
            $this->form_validation->set_rules('pHeadName', 'Purchase Head Name', 'trim|required', array('required' => 'Purchase Head Name Can Not Be Blank.'));
            $this->form_validation->set_rules('pHeadShortName', 'Purchase Head Short Name', 'trim|required', array('required' => 'Purchase Head Short Name Can Not Be Blank.'));
            $queryByPHeadName = $this->InventoryManagement->getPurchaseHeadInfoByName(trim($this->input->post('pHeadName')));
            $pHeadInfoByName = $queryByPHeadName->result();
            $queryByInventoryCategoryShortName = $this->InventoryManagement->getPurchaseHeadInfoByShortName(trim($this->input->post('pHeadShortName')));
            $inventoryCategoryByShortName = $queryByInventoryCategoryShortName->result();
            if ($this->form_validation->run() == FALSE) {
                $this->createPurchaseHead();
            } else if (sizeof($pHeadInfoByName)) {
                $this->session->set_flashdata('errorMessage', "A Purchase Head With This Name Already Exits. Please Choose A Different Name.");
                $this->createPurchaseHead();
            } else if (sizeof($inventoryCategoryByShortName)) {
                $this->session->set_flashdata('errorMessage', "A Purchase Head With This Short Name Already Exits. Please Choose A Different Abbreviation.");
                $this->createPurchaseHead();
            } else {
                $newPurchaseHeadInfo = array(
                    'iphm_name' => addslashes(trim($this->input->post('pHeadName'))),
                    'iphm_short_name' => addslashes(trim($this->input->post('pHeadShortName'))),
                    'iphm_description' => addslashes(trim($this->input->post('pHeadDescription'))),
                    'iphm_added_on' => date("Y-m-d H:i:s"),
                    'iphm_added_by' => $this->session->userdata("adminData")["smember_id"],
                    'iphm_updated_on' => date("Y-m-d H:i:s"),
                    'iphm_updated_by' => $this->session->userdata("adminData")["smember_id"]
                );
                if ($this->InventoryManagement->createNewPurchaseHead($newPurchaseHeadInfo)) {
                    $this->session->set_flashdata('successMessage', 'Purchase Head Created Successfully.');
                    redirect("admin/Inventory/purchaseHeads");
                } else {
                    $this->session->set_flashdata('errorMessage', 'An Error Occured While Creating Purchase Head. Try Later.');
                    redirect(current_url());
                }
            }
        } else {
            redirect("admin/");
        }
    }

    public function editPurchaseHead($iphm_id) {
        if ($this->sessionvalidator->isLoggedIn()) {
            $viewData['purchaseHeadInfo'] = $this->InventoryManagement->getPurchaseHeadInfBy($iphm_id)->result()[0];
            $this->load->view('admin/stockAndInventory/editPurchaseHead', $viewData);
        } else {
            redirect("admin/");
        }
    }

    public function updatePurchaseHead() {
        if ($this->sessionvalidator->isLoggedIn()) {
            $pHeadId = trim($this->input->post('pHeadId'));
            $this->form_validation->set_rules('pHeadName', 'Purchase Head Name', 'trim|required', array('required' => 'Purchase Head Name Can Not Be Blank.'));
            $this->form_validation->set_rules('pHeadShortName', 'Purchase Head Short Name', 'trim|required', array('required' => 'Purchase Head Short Name Can Not Be Blank.'));
            if ($this->form_validation->run() == FALSE) {
                $this->editPurchaseHead($pHeadId);
            } else if (!$this->InventoryManagement->isPurchaseHeadNameSafeUpdate($pHeadId, trim($this->input->post('pHeadName')))) {
                $this->session->set_flashdata('errorMessage', "A Purchase Head With This Name (" . trim($this->input->post('pHeadName')) . ") Already Exits. Please Choose A Different Name.");
                $this->editPurchaseHead($pHeadId);
            } else if (!$this->InventoryManagement->isPurchaseHeadShortNameSafeUpdate($pHeadId, trim($this->input->post('pHeadShortName')))) {
                $this->session->set_flashdata('errorMessage', "A Purchase Head With This Short Name (" . trim($this->input->post('pHeadShortName')) . ") Already Exits. Please Choose A Different Abbreviation.");
                $this->editPurchaseHead($pHeadId);
            } else {
                $purchaseHeadInfo = array(
                    'iphm_id' => $pHeadId,
                    'iphm_name' => addslashes(trim($this->input->post('pHeadName'))),
                    'iphm_short_name' => addslashes(trim($this->input->post('pHeadShortName'))),
                    'iphm_description' => addslashes(trim($this->input->post('pHeadDescription'))),
                    'iphm_updated_on' => date("Y-m-d H:i:s"),
                    'iphm_updated_by' => $this->session->userdata("adminData")["smember_id"]
                );
                if ($this->InventoryManagement->updatePurchaseHeadInfo($purchaseHeadInfo)) {
                    $this->session->set_flashdata('successMessage', 'Purchase Head Updated Successfully.');
                    redirect("admin/Inventory/purchaseHeads");
                } else {
                    $this->session->set_flashdata('errorMessage', 'An Error Occured While Updating Purchase Head. Try Later.');
                    redirect(current_url());
                }
            }
        } else {
            redirect("admin/");
        }
    }

    public function togglePurchaseHeadStatus($iphm_id, $toUpdateStatus) {
        if ($this->sessionvalidator->isLoggedIn()) {
            $projectHeadUpdateData = array(
                'iphm_id' => $iphm_id,
                'iphm_updated_on' => date("Y-m-d H:i:s"),
                'iphm_updated_by' => $this->session->userdata("adminData")["smember_id"],
                'iphm_active_status' => $toUpdateStatus
            );
            if ($this->InventoryManagement->updatePurchaseHeadInfo($projectHeadUpdateData)) {
                $this->session->set_flashdata('successMessage', 'Purchase Head Status Updated Successfully.');
                redirect("admin/Inventory/purchaseHeads");
            } else {
                $this->session->set_flashdata('errorMessage', 'Some Error Occurred While Updating Project Head Status. Try Later.');
                redirect(current_url());
            }
        } else {
            redirect("admin/");
        }
    }

    public function deletePurchaseHead($iphm_id) {
        if ($this->sessionvalidator->isLoggedIn()) {
            $projectHeadUpdateData = array(
                'iphm_id' => $iphm_id,
                'iphm_updated_on' => date("Y-m-d H:i:s"),
                'iphm_updated_by' => $this->session->userdata("adminData")["smember_id"],
                'iphm_delete_status' => 'T'
            );
            if ($this->InventoryManagement->updatePurchaseHeadInfo($projectHeadUpdateData)) {
                $this->session->set_flashdata('successMessage', 'Purchase Head Deleted Successfully.');
                redirect("admin/Inventory/purchaseHeads");
            } else {
                $this->session->set_flashdata('errorMessage', 'Some Error Occurred While Deleting Project Head. Try Later.');
                redirect(current_url());
            }
        } else {
            redirect("admin/");
        }
    }

    public function undoDeletePurchaseHead($iphm_id) {
        if ($this->sessionvalidator->isLoggedIn()) {
            $projectHeadUpdateData = array(
                'iphm_id' => $iphm_id,
                'iphm_updated_on' => date("Y-m-d H:i:s"),
                'iphm_updated_by' => $this->session->userdata("adminData")["smember_id"],
                'iphm_delete_status' => 'F'
            );
            if ($this->InventoryManagement->updatePurchaseHeadInfo($projectHeadUpdateData)) {
                $this->session->set_flashdata('successMessage', 'Purchase Head Recovered Successfully.');
                redirect("admin/Inventory/purchaseHeads");
            } else {
                $this->session->set_flashdata('errorMessage', 'Some Error Occurred While Recovering Purchase Head. Try Later.');
                redirectc(current_url());
            }
        } else {
            redirect("admin/");
        }
    }

    public function getAllInvoicesUnderPurchaseHeads() {
        $iphm_id = $_POST['iphm_id'];
        $query = $this->StockAndPurchaseManagement->getPurchaseInfoByPurchaseHead($iphm_id);
        $invoiceList = $query->result();
        $options = "<option value=''>Select Invoice</option>";
        for ($i = 0; $i < (sizeof($invoiceList)); $i++) {
            $options .= "<option value=" . $invoiceList[$i]->pm_id . ">" . stripslashes($invoiceList[$i]->pm_invoice_number) . " Dated " . date("d-m-Y", strtotime($invoiceList[$i]->pm_invoice_date)) . "</option>";
        }
        $responseData = array(
            'csrfName' => $this->security->get_csrf_token_name(),
            'csrfHash' => $this->security->get_csrf_hash(),
            'invoice_list' => $options
        );
        echo json_encode($responseData);
    }

    public function getAllInvoicesUnderPurchaseHeadsSelected() {
        $iphm_id = $_POST['iphm_id'];
        $pm_id = $_POST['pm_id'];
        $query = $this->StockAndPurchaseManagement->getPurchaseInfoByPurchaseHead($iphm_id);
        $invoiceList = $query->result();
        $options = "<option value=''>Select Invoice</option>";
        for ($i = 0; $i < (sizeof($invoiceList)); $i++) {
            $selected = ($invoiceList[$i]->pm_id == $pm_id) ? "selected" : "";
            $options .= "<option value=" . $invoiceList[$i]->pm_id . " " . $selected . ">" . stripslashes($invoiceList[$i]->pm_invoice_number) . " Dated " . date("d-m-Y", strtotime($invoiceList[$i]->pm_invoice_date)) . "</option>";
        }
        $responseData = array(
            'csrfName' => $this->security->get_csrf_token_name(),
            'csrfHash' => $this->security->get_csrf_hash(),
            'invoice_list' => $options
        );
        echo json_encode($responseData);
    }

    /* Functions For Inventory Purchase Heads */
}

KBHT - 2023