ok

Mini Shell

Direktori : /home2/selectio/www/mm-tailor-billing/app/controllers/
Upload File :
Current File : /home2/selectio/www/mm-tailor-billing/app/controllers/Mmcategories.php

<?php
defined('BASEPATH') or exit('No direct script access allowed');

class Mmcategories extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();

        if (!$this->loggedIn) {
            redirect('login');
        }

        $this->load->library('form_validation');
        $this->load->model('mmcategories_model');
    }

    public function add()
    {
        if (!$this->Admin) {
            $this->session->set_flashdata('error', lang('access_denied'));
            redirect('pos');
        }

        $this->form_validation->set_rules('name', lang('category_name'), 'required');

        if ($this->form_validation->run() == true) {
            $data = ['code' => $this->input->post('code'), 'name' => $this->input->post('name'), 'category' => $this->input->post('category_id')];
        }

        if ($this->form_validation->run() == true && $this->mmcategories_model->addMmcategory($data)) {
            $this->session->set_flashdata('message', lang('mmcategories_added'));
            redirect('mmcategories');
        } else {
            $this->data['error']      = (validation_errors() ? validation_errors() : $this->session->flashdata('error'));
            $this->data['page_title'] = lang('add_mmcategories');
            $bc                       = [['link' => site_url('mmcategories'), 'page' => lang('mmcategories')], ['link' => '#', 'page' => lang('add_category')]];
            $meta                     = ['page_title' => lang('add_mmcategories'), 'bc' => $bc];
            $this->page_construct('mmcategories/add', $this->data, $meta);
        }
    }
    
    // MM Categories Sub Category Starts Here
    public function sub_add()
    {
        if (!$this->session->userdata('store_id')) {
            $this->session->set_flashdata('warning', lang('please_select_store'));
            redirect('stores');
        }
        $this->form_validation->set_rules('name', lang('category_name'), 'required');

        if ($this->form_validation->run() == true) {
            $data = ['sub_name' => $this->input->post('name'), 'category_id' => $this->input->post('category_id')];
        }

        if ($this->form_validation->run() == true && $this->mmcategories_model->addMmSubcategory($data)) {
            $this->session->set_flashdata('message', lang('mmsubcategories_added'));
            redirect('mmcategories/sub_list');
        } else {
            $this->data['error']      = (validation_errors() ? validation_errors() : $this->session->flashdata('error'));
            $this->data['mm_category'] = $this->site->getAllMMCategories();
            $this->data['page_title'] = lang('add_mmsubcategories');
            $bc                       = [['link' => site_url('mmcategories'), 'page' => lang('mmcategories')], ['link' => '#', 'page' => lang('add_mmsubcategories')]];
            $meta                     = ['page_title' => lang('add_mmsubcategories'), 'bc' => $bc];
            $this->page_construct('mmcategories/sub_add', $this->data, $meta);
        }
    }
    public function get_mm_sub_categories()
    {
        $this->load->library('datatables');
        $this->datatables->select('mm_sub_categories.id, sub_name, category_id, IF(tec_mm_sub_categories.category_id=tec_mm_categories.id,mm_categories.name,code) as cat_name');
        $this->datatables->from('mm_sub_categories');
        $this->datatables->join('mm_categories', 'mm_sub_categories.category_id = mm_categories.id');
        $this->datatables->edit_column('category_id', "<span class='btn btn-info btn-xs'>$4</span>", 'mm_sub_categories.id, sub_name, category_id, cat_name, name');
        $this->datatables->add_column('Actions', "<div class='text-center'><div class='btn-group'><a href='" . site_url('mmcategories/sub_edit/$1') . "' title='" . lang('edit_mmcategories') . "' class='tip btn btn-warning btn-xs'><i class='fa fa-edit'></i></a> <a href='" . site_url('mmcategories/sub_delete/$1') . "' onClick=\"return confirm('" . lang('alert_x_category') . "')\" title='" . lang('delete_mmcategories') . "' class='tip btn btn-danger btn-xs'><i class='fa fa-trash-o'></i></a></div></div>", 'id, sub_name, category_id');
        $this->datatables->unset_column('mm_sub_categories.id');
        echo $this->datatables->generate();
    }
    public function sub_list()
    {
        $this->data['error']      = (validation_errors() ? validation_errors() : $this->session->flashdata('error'));
        $this->data['mmsubcategories'] = $this->site->getAllMMCategories();
        $this->data['page_title'] = lang('mmcategories');
        $bc                       = [['link' => '#', 'page' => lang('mmsubcategories')]];
        $meta                     = ['page_title' => lang('mmsubcategories'), 'bc' => $bc];
        $this->page_construct('mmcategories/sub_list', $this->data, $meta);
    }
    public function sub_edit($id = null)
    {
        if (!$this->Admin) {
            $this->session->set_flashdata('error', lang('access_denied'));
            redirect('pos');
        }
        if ($this->input->get('id')) {
            $id = $this->input->get('id');
        }

        $this->form_validation->set_rules('name', lang('category_name'), 'required');

        if ($this->form_validation->run() == true) {
            $data = ['sub_name' => $this->input->post('name'),
                    'category_id' => $this->input->post('category_id')];
        }

        if ($this->form_validation->run() == true && $this->mmcategories_model->updateMmSubCategory($id, $data)) {
            $this->session->set_flashdata('message', lang('mmsubcategories_updated'));
            redirect('mmcategories/sub_list');
        } else {
            $this->data['error']      = (validation_errors() ? validation_errors() : $this->session->flashdata('error'));
            $this->data['mmsubcategories'] = $this->site->getMMSubCategoryByID($id);
            $this->data['mm_category'] = $this->site->getAllMMCategories();
            $this->data['page_title'] = lang('edit_mmsubcategories');
            $bc                       = [['link' => site_url('mmcategories'), 'page' => lang('mmcategories')], ['link' => '#', 'page' => lang('edit_mmsubcategories')]];
            $meta                     = ['page_title' => lang('edit_mmsubcategories'), 'bc' => $bc];
            $this->page_construct('mmcategories/sub_edit', $this->data, $meta);
        }
    }
    public function sub_delete($id = null)
    {
        if (DEMO) {
            $this->session->set_flashdata('error', lang('disabled_in_demo'));
            redirect($_SERVER['HTTP_REFERER'] ?? 'welcome');
        }
        if (!$this->Admin) {
            $this->session->set_flashdata('error', lang('access_denied'));
            redirect('pos');
        }
        if ($this->input->get('id')) {
            $id = $this->input->get('id');
        }

        if ($this->mmcategories_model->deleteMmSubCategory($id)) {
            $this->session->set_flashdata('message', lang('mmsubcategories_deleted'));
            redirect('mmcategories/sub_list');
        }
    }
    // MM Categories Sub Category Ends Here

    public function delete($id = null)
    {
        if (DEMO) {
            $this->session->set_flashdata('error', lang('disabled_in_demo'));
            redirect($_SERVER['HTTP_REFERER'] ?? 'welcome');
        }
        if (!$this->Admin) {
            $this->session->set_flashdata('error', lang('access_denied'));
            redirect('pos');
        }
        if ($this->input->get('id')) {
            $id = $this->input->get('id');
        }

        if ($this->mmcategories_model->deleteCategory($id)) {
            $this->session->set_flashdata('message', lang('mmcategories_deleted'));
            redirect('mmcategories');
        }
    }

    public function edit($id = null)
    {
        if (!$this->Admin) {
            $this->session->set_flashdata('error', lang('access_denied'));
            redirect('pos');
        }
        if ($this->input->get('id')) {
            $id = $this->input->get('id');
        }

        $this->form_validation->set_rules('name', lang('category_name'), 'required');

        if ($this->form_validation->run() == true) {
            $data = ['code' => $this->input->post('code'), 
                    'name' => $this->input->post('name'),
                    'category' => $this->input->post('category_id')];
        }

        if ($this->form_validation->run() == true && $this->mmcategories_model->updateCategory($id, $data)) {
            $this->session->set_flashdata('message', lang('mmcategories_updated'));
            redirect('mmcategories');
        } else {
            $this->data['error']      = (validation_errors() ? validation_errors() : $this->session->flashdata('error'));
            $this->data['mmcategories']   = $this->site->getMMCategoryByID($id);
            $this->data['page_title'] = lang('new_category');
            $bc                       = [['link' => site_url('mmcategories'), 'page' => lang('mmcategories')], ['link' => '#', 'page' => lang('edit_mmcategories')]];
            $meta                     = ['page_title' => lang('edit_mmcategories'), 'bc' => $bc];
            $this->page_construct('mmcategories/edit', $this->data, $meta);
        }
    }

    public function get_mm_categories()
    {
        $this->load->library('datatables');
        $this->datatables->select('id, code, name, category, IF(category="1","Expense-Category",IF(category="2","Income-Category",IF(category="3","Purchase-Category","Undefined"))) as cat_name');
        $this->datatables->from('mm_categories');
        $this->datatables->edit_column('category', "<span class='btn btn-info btn-xs'>$5</span>", 'id, code, name, category, cat_name');
        $this->datatables->add_column('Actions', "<div class='text-center'><div class='btn-group'><a href='" . site_url('mmcategories/edit/$1') . "' title='" . lang('edit_mmcategories') . "' class='tip btn btn-warning btn-xs'><i class='fa fa-edit'></i></a> <a href='" . site_url('mmcategories/delete/$1') . "' onClick=\"return confirm('" . lang('alert_x_category') . "')\" title='" . lang('delete_mmcategories') . "' class='tip btn btn-danger btn-xs'><i class='fa fa-trash-o'></i></a></div></div>", 'id, code, name, category');
        $this->datatables->unset_column('id');
        echo $this->datatables->generate();
    }

    public function import()
    {
        if (!$this->Admin) {
            $this->session->set_flashdata('error', lang('access_denied'));
            redirect('pos');
        }
        $this->load->helper('security');
        $this->form_validation->set_rules('userfile', lang('upload_file'), 'xss_clean');

        if ($this->form_validation->run() == true) {
            if (DEMO) {
                $this->session->set_flashdata('warning', lang('disabled_in_demo'));
                redirect('pos');
            }

            if (isset($_FILES['userfile'])) {
                $this->load->library('upload');

                $config['upload_path']   = 'uploads/';
                $config['allowed_types'] = 'csv';
                $config['max_size']      = '500';
                $config['overwrite']     = true;

                $this->upload->initialize($config);

                if (!$this->upload->do_upload()) {
                    $error = $this->upload->display_errors();
                    $this->session->set_flashdata('error', $error);
                    redirect('categories/import');
                }

                $csv = $this->upload->file_name;

                $arrResult = [];
                $handle    = fopen('uploads/' . $csv, 'r');
                if ($handle) {
                    while (($row = fgetcsv($handle, 1000, ',')) !== false) {
                        $arrResult[] = $row;
                    }
                    fclose($handle);
                }
                array_shift($arrResult);

                $keys = ['code', 'name'];

                $final = [];
                foreach ($arrResult as $key => $value) {
                    $final[] = array_combine($keys, $value);
                }

                if (sizeof($final) > 1001) {
                    $this->session->set_flashdata('error', lang('more_than_allowed'));
                    redirect('categories/import');
                }

                foreach ($final as $csv_pr) {
                    if ($this->site->getCategoryByCode($csv_pr['code'])) {
                        $this->session->set_flashdata('error', lang('check_category') . ' (' . $csv_pr['code'] . '). ' . lang('category_already_exist'));
                        redirect('categories/import');
                    }
                    $data[] = ['code' => $csv_pr['code'], 'name' => $csv_pr['name']];
                }
            }
        }

        if ($this->form_validation->run() == true && $this->categories_model->add_categories($data)) {
            $this->session->set_flashdata('message', lang('categories_added'));
            redirect('categories');
        } else {
            $this->data['error']      = (validation_errors() ? validation_errors() : $this->session->flashdata('error'));
            $this->data['page_title'] = lang('import_categories');
            $bc                       = [['link' => site_url('products'), 'page' => lang('products')], ['link' => site_url('categories'), 'page' => lang('categories')], ['link' => '#', 'page' => lang('import_categories')]];
            $meta                     = ['page_title' => lang('import_categories'), 'bc' => $bc];
            $this->page_construct('categories/import', $this->data, $meta);
        }
    }

    public function index()
    {
        $this->data['error']      = (validation_errors() ? validation_errors() : $this->session->flashdata('error'));
        $this->data['mmcategories'] = $this->site->getAllMMCategories();
        $this->data['page_title'] = lang('mmcategories');
        $bc                       = [['link' => '#', 'page' => lang('mmcategories')]];
        $meta                     = ['page_title' => lang('mmcategories'), 'bc' => $bc];
        $this->page_construct('mmcategories/index', $this->data, $meta);
    }
}

Zerion Mini Shell 1.0