ok

Mini Shell

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

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

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

        if (!$this->loggedIn) {
            redirect('login');
        }
        if (!$this->session->userdata('store_id')) {
            $this->session->set_flashdata('warning', lang('please_select_store'));
            redirect('stores');
        }
        $this->load->library('form_validation');
        $this->load->model('purchases_model');
        $this->allowed_types = 'gif|jpg|png|pdf|doc|docx|xls|xlsx|zip';
    }

    public function add()
    {
        if (!$this->session->userdata('store_id')) {
            $this->session->set_flashdata('warning', lang('please_select_store'));
            redirect('stores');
        }
        if (!$this->Admin) {
            $this->session->set_flashdata('error', lang('access_denied'));
            redirect('pos');
        }
        $this->form_validation->set_rules('date', lang('date'), 'required');

        if ($this->form_validation->run() == true) {
            $total      = 0;
            $quantity   = 'quantity';
            $product_id = 'product_id';
            $unit_cost  = 'cost';
            $i          = isset($_POST['product_id']) ? sizeof($_POST['product_id']) : 0;
            for ($r = 0; $r < $i; $r++) {
                $item_id   = $_POST['product_id'][$r];
                $item_qty  = $_POST['quantity'][$r];
                $item_cost = $_POST['cost'][$r];
                if ($item_id && $item_qty && $unit_cost) {
                    if (!$this->purchases_model->getProductByID($item_id)) {
                        $this->session->set_flashdata('error', $this->lang->line('product_not_found') . ' ( ' . $item_id . ' ).');
                        redirect('purchases/add');
                    }

                    $products[] = [
                        'product_id' => $item_id,
                        'cost'       => $item_cost,
                        'quantity'   => $item_qty,
                        'subtotal'   => ($item_cost * $item_qty),
                    ];

                    $total += ($item_cost * $item_qty);
                }
            }

            if (!isset($products) || empty($products)) {
                $this->form_validation->set_rules('product', lang('order_items'), 'required');
            } else {
                krsort($products);
            }

            $data = [
                'date'        => $this->input->post('date'),
                'reference'   => $this->input->post('reference'),
                'category'    => $this->input->post('category'),
                'supplier_id' => $this->input->post('supplier'),
                'note'        => $this->input->post('note', true),
                'received'    => $this->input->post('received'),
                'total'       => $total,
                'created_by'  => $this->session->userdata('user_id'),
                'store_id'    => $this->session->userdata('store_id'),
                'paid'    => $this->input->post('paid_amount'),
                'balance'    => $this->input->post('balance_amount'),
                'payment_type'    => $this->input->post('payment_type'),
                'payment_status'    => $this->input->post('payment_status'),
                'payment_done_date'    => $this->input->post('payment_done_date'),
                
            ];

            if ($_FILES['userfile']['size'] > 0) {
                $this->load->library('upload');
                $config['upload_path']   = 'uploads/';
                $config['allowed_types'] = $this->allowed_types;
                $config['max_size']      = '2000';
                $config['overwrite']     = false;
                $config['encrypt_name']  = true;
                $this->upload->initialize($config);

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

                $data['attachment'] = $this->upload->file_name;
            }
            // $this->tec->print_arrays($data, $products);
        }

        if ($this->form_validation->run() == true && $this->purchases_model->addPurchase($data, $products)) {
            $this->session->set_userdata('remove_spo', 1);
            $this->session->set_flashdata('message', lang('purchase_added'));
            redirect('purchases');
        } else {
            $this->data['error']      = (validation_errors() ? validation_errors() : $this->session->flashdata('error'));
            $this->data['suppliers']  = $this->site->getAllSuppliers();
            $this->data['purchase_category'] = $this->site->getAllMmCategory(3);
            $this->data['page_title'] = lang('add_purchase');
            $bc                       = [['link' => site_url('purchases'), 'page' => lang('purchases')], ['link' => '#', 'page' => lang('add_purchase')]];
            $meta                     = ['page_title' => lang('add_purchase'), 'bc' => $bc];
            $this->page_construct('purchases/add', $this->data, $meta);
        }
    }

    /*public function add_expense()
    {
        if (!$this->session->userdata('store_id')) {
            $this->session->set_flashdata('warning', lang('please_select_store'));
            redirect('stores');
        }
        $this->load->helper('security');

        $this->form_validation->set_rules('amount', lang('amount'), 'required');
        $this->form_validation->set_rules('userfile', lang('attachment'), 'xss_clean');
        if ($this->form_validation->run() == true) {
            if ($this->Admin) {
                $date = trim($this->input->post('date'));
            } else {
                $date = date('Y-m-d H:i:s');
            }
            
            $reference=array();
            $amount=array();
            $reference = $this->input->post('reference');
            $amount = $this->input->post('amount');
            
            $data = [
                'date'       => $date,
                'reference'  => $this->input->post('reference') ? $this->input->post('reference') : $this->site->getReference('ex'),
                'amount'     => $this->input->post('amount'),
                'created_by' => $this->session->userdata('user_id'),
                'store_id'   => $this->session->userdata('store_id'),
                'note'       => $this->input->post('note', true),
                'category' => $this->input->post('category'),
                'sub_category' => $this->input->post('sub_category')
                ];

            if ($_FILES['userfile']['size'] > 0) {
                $this->load->library('upload');
                $config['upload_path']   = 'uploads/';
                $config['allowed_types'] = $this->allowed_types;
                $config['max_size']      = '2000';
                $config['overwrite']     = false;
                $config['encrypt_name']  = true;
                $this->upload->initialize($config);
                if (!$this->upload->do_upload()) {
                    $error = $this->upload->display_errors();
                    $this->session->set_flashdata('error', $error);
                    redirect($_SERVER['HTTP_REFERER']);
                }
                $photo              = $this->upload->file_name;
                $data['attachment'] = $photo;
            }

            //$this->tec->print_arrays($data);
        } elseif ($this->input->post('add_expense')) {
            $this->session->set_flashdata('error', validation_errors());
            redirect($_SERVER['HTTP_REFERER']);
        }
        if ($this->form_validation->run() == true && $this->purchases_model->addExpense($data)) {
            $this->session->set_flashdata('message', lang('expense_added'));
            redirect('purchases/expenses');
        } else {
            $this->data['error']      = (validation_errors() ? validation_errors() : $this->session->flashdata('error'));
            $this->data['expense_category'] = $this->site->getAllMmCategory(1);
            $this->data['page_title'] = lang('add_expense');
            $bc                       = [['link' => site_url('purchases'), 'page' => lang('purchases')], ['link' => site_url('purchases/expenses'), 'page' => lang('expenses')], ['link' => '#', 'page' => lang('add_expense')]];
            $meta                     = ['page_title' => lang('add_expense'), 'bc' => $bc];
            $this->page_construct('purchases/add_expense', $this->data, $meta);
        }
    }*/

    /* ---- Add Row Based Starts Here ---- */ 
   /* public function add_expense()
    {
        if (!$this->session->userdata('store_id')) {
            $this->session->set_flashdata('warning', lang('please_select_store'));
            redirect('stores');
        }
        $this->load->helper('security');*/

        //$this->form_validation->set_rules('amount', lang('amount'), 'required');
        //$this->form_validation->set_rules('userfile', lang('attachment'), 'xss_clean');
        
        /*if ($this->form_validation->run() == true) {
            error_log("Insert");*/
            /*$reference=array();
            $amount=array();
            $reference = $this->input->post('reference');
            $amount = $this->input->post('amount');*/
            
        /*} */
        /*elseif ($this->input->post('add_expense')) {
            $this->session->set_flashdata('error', validation_errors());
            redirect($_SERVER['HTTP_REFERER']);
        }*/
        /*if ($this->form_validation->run() == true && $this->purchases_model->addExpenseRowBased($reference,$amount)) {
            $this->session->set_flashdata('message', lang('expense_added'));
            redirect('purchases/expenses');
        } else {
            $this->data['error']      = (validation_errors() ? validation_errors() : $this->session->flashdata('error'));
            $this->data['expense_category'] = $this->site->getAllMmCategory(1);
            $this->data['page_title'] = lang('add_expense');
            $bc                       = [['link' => site_url('purchases'), 'page' => lang('purchases')], ['link' => site_url('purchases/expenses'), 'page' => lang('expenses')], ['link' => '#', 'page' => lang('add_expense')]];
            $meta                     = ['page_title' => lang('add_expense'), 'bc' => $bc];
            $this->page_construct('purchases/add_expense', $this->data, $meta);
        }
    }*/


    /* ---- Add Row Based Starts Here ---- */
    public function add_expense()
    {
        
        if (!$this->session->userdata('store_id')) {
            $this->session->set_flashdata('warning', lang('please_select_store'));
            redirect('stores');
        }
        
        
        if ($this->input->post('add_expense')) {
            
            $date=array();
            $reference=array();
            $amount=array();
            /*$category=array();*/
            $sub_category=array();
            $attachment=array();
            $notes=array();
            
            $date = $this->input->post('date');
            $reference = $this->input->post('reference');
            $amount = $this->input->post('amount');
            $category = $this->input->post('category');
            $sub_category = $this->input->post('sub_category');
            $attachment = $this->input->post('attachment');
            $notes = $this->input->post('note');
            $created_by = $this->session->userdata('user_id');
            
            
            $this->purchases_model->addExpenseRowBased($date,$amount,$category,$sub_category,$notes,$created_by);
            $this->session->set_flashdata('message', lang('expense_added'));
            redirect('purchases/expenses');
            } else {
                $this->data['error']      = (validation_errors() ? validation_errors() : $this->session->flashdata('error'));
                $this->data['expense_category'] = $this->site->getAllMmCategory(1);
                $this->data['page_title'] = lang('add_expense');
                $bc                       = [['link' => site_url('purchases'), 'page' => lang('purchases')], ['link' => site_url('purchases/expenses'), 'page' => lang('expenses')], ['link' => '#', 'page' => lang('add_expense')]];
                $meta                     = ['page_title' => lang('add_expense'), 'bc' => $bc];
                $this->page_construct('purchases/add_expense', $this->data, $meta);
            }
    }
    /* ---- Add Row Based 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->purchases_model->deletePurchase($id)) {
            $this->session->set_flashdata('message', lang('purchase_deleted'));
            redirect('purchases');
        }
    }

    public function delete_expense($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');
        }

        $expense = $this->purchases_model->getExpenseByID($id);
        if ($this->purchases_model->deleteExpense($id)) {
            if ($expense->attachment) {
                unlink($this->upload_path . $expense->attachment);
            }
            $this->session->set_flashdata('message', lang('expense_deleted'));
            redirect('purchases/expenses');
        }
    }

    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('date', lang('date'), 'required');

        if ($this->form_validation->run() == true) {
            $total      = 0;
            $quantity   = 'quantity';
            $product_id = 'product_id';
            $unit_cost  = 'cost';
            $i          = isset($_POST['product_id']) ? sizeof($_POST['product_id']) : 0;
            for ($r = 0; $r < $i; $r++) {
                $item_id   = $_POST['product_id'][$r];
                $item_qty  = $_POST['quantity'][$r];
                $item_cost = $_POST['cost'][$r];
                if ($item_id && $item_qty && $unit_cost) {
                    if (!$this->site->getProductByID($item_id)) {
                        $this->session->set_flashdata('error', $this->lang->line('product_not_found') . ' ( ' . $item_id . ' ).');
                        redirect('purchases/edit/' . $id);
                    }

                    $products[] = [
                        'product_id' => $item_id,
                        'cost'       => $item_cost,
                        'quantity'   => $item_qty,
                        'subtotal'   => ($item_cost * $item_qty),
                    ];

                    $total += ($item_cost * $item_qty);
                }
            }

            if (!isset($products) || empty($products)) {
                $this->form_validation->set_rules('product', lang('order_items'), 'required');
            } else {
                krsort($products);
            }

            $data = [
                'date'        => $this->input->post('date'),
                'reference'   => $this->input->post('reference'),
                'note'        => $this->input->post('note', true),
                'supplier_id' => $this->input->post('supplier'),
                'received'    => $this->input->post('received'),
                'total'       => $total,
                'paid'    => $this->input->post('paid_amount'),
                'balance'    => $this->input->post('balance_amount'),
                'payment_type'    => $this->input->post('payment_type'),
                'payment_status'    => $this->input->post('payment_status'),
                'payment_done_date'    => $this->input->post('payment_done_date'),
            ];

            if ($_FILES['userfile']['size'] > 0) {
                $this->load->library('upload');
                $config['upload_path']   = 'uploads/';
                $config['allowed_types'] = $this->allowed_types;
                $config['max_size']      = '2000';
                $config['overwrite']     = false;
                $config['encrypt_name']  = true;
                $this->upload->initialize($config);

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

                $data['attachment'] = $this->upload->file_name;
            }
            // $this->tec->print_arrays($data, $products);
        }

        if ($this->form_validation->run() == true && $this->purchases_model->updatePurchase($id, $data, $products)) {
            $this->session->set_userdata('remove_spo', 1);
            $this->session->set_flashdata('message', lang('purchase_updated'));
            redirect('purchases');
        } else {
            $this->data['purchase'] = $this->purchases_model->getPurchaseByID($id);
            $inv_items              = $this->purchases_model->getAllPurchaseItems($id);
            $c                      = rand(100000, 9999999);
            foreach ($inv_items as $item) {
                $row       = $this->site->getProductByID($item->product_id);
                $row->qty  = $item->quantity;
                $row->cost = $item->cost;
                $ri        = $this->Settings->item_addition ? $row->id : $c;
                $pr[$ri]   = ['id' => $ri, 'item_id' => $row->id, 'label' => $row->name . ' (' . $row->code . ')', 'row' => $row];
                $c++;
            }

            $this->data['items']      = json_encode($pr);
            $this->data['error']      = (validation_errors() ? validation_errors() : $this->session->flashdata('error'));
            $this->data['suppliers']  = $this->site->getAllSuppliers();
            $this->data['page_title'] = lang('edit_purchase');
            $bc                       = [['link' => site_url('purchases'), 'page' => lang('purchases')], ['link' => '#', 'page' => lang('edit_purchase')]];
            $meta                     = ['page_title' => lang('edit_purchase'), 'bc' => $bc];
            $this->page_construct('purchases/edit', $this->data, $meta);
        }
    }

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

        /*$this->form_validation->set_rules('reference', lang('reference'), 'required');*/
        $this->form_validation->set_rules('amount', lang('amount'), 'required');
        $this->form_validation->set_rules('userfile', lang('attachment'), 'xss_clean');
        if ($this->form_validation->run() == true) {
            if ($this->Admin) {
                $date = trim($this->input->post('date'));
            } else {
                $date = date('Y-m-d H:i:s');
            }
            $data = [
                'date'      => $date,
                'reference' => $this->input->post('reference'),
                'amount'    => $this->input->post('amount'),
                'note'      => $this->input->post('note', true),
                'category' => $this->input->post('category'),
                'sub_category' => $this->input->post('sub_category'),
            ];
            if ($_FILES['userfile']['size'] > 0) {
                $this->load->library('upload');
                $config['upload_path']   = 'uploads/';
                $config['allowed_types'] = $this->allowed_types;
                $config['max_size']      = '2000';
                $config['overwrite']     = false;
                $config['encrypt_name']  = true;
                $this->upload->initialize($config);
                if (!$this->upload->do_upload()) {
                    $error = $this->upload->display_errors();
                    $this->session->set_flashdata('error', $error);
                    redirect($_SERVER['HTTP_REFERER']);
                }
                $photo              = $this->upload->file_name;
                $data['attachment'] = $photo;
            }

            //$this->tec->print_arrays($data);
        } elseif ($this->input->post('edit_expense')) {
            $this->session->set_flashdata('error', validation_errors());
            redirect($_SERVER['HTTP_REFERER']);
        }

        if ($this->form_validation->run() == true && $this->purchases_model->updateExpense($id, $data)) {
            $this->session->set_flashdata('message', lang('expense_updated'));
            redirect('purchases/expenses');
        } else {
            $this->data['error']      = (validation_errors() ? validation_errors() : $this->session->flashdata('error'));
            $this->data['expense_category'] = $this->site->getAllMmCategory(1);
            $this->data['expense']    = $this->purchases_model->getExpenseByID($id);
            $this->data['page_title'] = lang('edit_expense');
            $bc                       = [['link' => site_url('purchases'), 'page' => lang('purchases')], ['link' => site_url('purchases/expenses'), 'page' => lang('expenses')], ['link' => '#', 'page' => lang('edit_expense')]];
            $meta                     = ['page_title' => lang('edit_expense'), 'bc' => $bc];
            $this->page_construct('purchases/edit_expense', $this->data, $meta);
        }
    }

    public function expense_note($id = null)
    {
        if (!$this->Admin) {
            if ($expense->created_by != $this->session->userdata('user_id')) {
                $this->session->set_flashdata('error', lang('access_denied'));
                redirect($_SERVER['HTTP_REFERER'] ?? 'pos');
            }
        }

        $expense                  = $this->purchases_model->getExpenseByID($id);
        $this->data['user']       = $this->site->getUser($expense->created_by);
        $this->data['expense']    = $expense;
        $this->data['page_title'] = $this->lang->line('expense_note');
        $this->load->view($this->theme . 'purchases/expense_note', $this->data);
    }

    /* ----------------------------------------------------------------- */

    public function expenses($id = null, $sub_category = null, $start_date = null, $end_date = null)
    {
        // Filter Starts Here 
        if ($this->input->post('category')) 
        {
            $sub_category = $this->input->post('sub_category') ? $this->input->post('sub_category') : NULL;
            $start_date = $this->input->post('start_date') ? $this->input->post('start_date') : NULL;
            $end_date = $this->input->post('end_date') ? $this->input->post('end_date') : NULL;
            $this->data['purchases'] = $this->purchases_model->getTotalPurchase($this->input->post('category'), $sub_category, $start_date, $end_date);
        }
        // Filter Ends Here
        
        $this->data['error']      = (validation_errors()) ? validation_errors() : $this->session->flashdata('error');
        $this->data['page_title'] = lang('expenses');
        $this->data['expense_category'] = $this->site->getAllMmCategory(1);
        $bc                       = [['link' => site_url('purchases'), 'page' => lang('purchases')], ['link' => '#', 'page' => lang('expenses')]];
        $meta                     = ['page_title' => lang('expenses'), 'bc' => $bc];
        $this->page_construct('purchases/expenses', $this->data, $meta);
    }

    public function get_expenses($user_id = null)
    {
        $category = $this->input->get('category') ? $this->input->get('category') : NULL;
        $sub_category = $this->input->get('sub_category') ? $this->input->get('sub_category') : NULL;
        $start_date = $this->input->get('start_date') ? $this->input->get('start_date') : NULL;
        $end_date = $this->input->get('end_date') ? $this->input->get('end_date') : NULL;
        
        $detail_link = anchor('purchases/expense_note/$1', '<i class="fa fa-file-text-o"></i> ' . lang('expense_note'), 'data-toggle="modal" data-target="#myModal2"');
        $edit_link   = anchor('purchases/edit_expense/$1', '<i class="fa fa-edit"></i> ' . lang('edit_expense'), 'data-toggle="modal" data-target="#myModal"');
        $delete_link = "<a href='#' class='po' title='<b>" . $this->lang->line('delete_expense') . "</b>' data-content=\"<p>"
            . lang('r_u_sure') . "</p><a class='btn btn-danger po-delete' href='" . site_url('purchases/delete_expense/$1') . "'>"
            . lang('i_m_sure') . "</a> <button class='btn po-close'>" . lang('no') . "</button>\"  rel='popover'><i class=\"fa fa-trash-o\"></i> "
            . lang('delete_expense') . '</a>';
        $action = '<div class="text-center"><div class="btn-group text-left">'
            . '<button type="button" class="btn btn-default btn-xs btn-primary dropdown-toggle" data-toggle="dropdown">'
            . lang('actions') . ' <span class="caret"></span></button>
        <ul class="dropdown-menu pull-right" role="menu">
            <li>' . $detail_link . '</li>
            <li>' . $edit_link . '</li>
            <li>' . $delete_link . '</li>
        </ul>
    </div></div>';

        $this->load->library('datatables');
        if ($this->db->dbdriver == 'sqlite3') {
            $this->datatables->select($this->db->dbprefix('expenses') . '.id as id, DATE_FORMAT(date, "%d-%m-%Y") as date, reference, amount, note, expenses.category, sub_category, IF(tec_expenses.category=tec_mm_categories.id,mm_categories.name,code) as cat_name, IF(tec_expenses.sub_category=tec_mm_sub_categories.id,mm_sub_categories.sub_name,sub_conf1) as sub_cat_name, (' . $this->db->dbprefix('users') . ".first_name || ' ' || " . $this->db->dbprefix('users') . '.last_name) as user, attachment', false);
        } else {
            $this->datatables->select($this->db->dbprefix('expenses') . '.id as id, DATE_FORMAT(date, "%d-%m-%Y") as date, reference, amount, note, expenses.category, sub_category, IF(tec_expenses.category=tec_mm_categories.id,mm_categories.name,code) as cat_name, IF(tec_expenses.sub_category=tec_mm_sub_categories.id,mm_sub_categories.sub_name,sub_conf1) as sub_cat_name, CONCAT(' . $this->db->dbprefix('users') . ".first_name, ' ', " . $this->db->dbprefix('users') . '.last_name) as user, attachment', false);
        }
        $this->datatables->from('expenses')
            ->join('users', 'users.id=expenses.created_by', 'left')
            ->group_by('expenses.id');
        
        $this->datatables->join('mm_categories', 'expenses.category = mm_categories.id','inner');
        $this->datatables->join('mm_sub_categories', 'expenses.sub_category = mm_sub_categories.id','inner');
        
        if (!$this->Admin && !$this->session->userdata('view_right')) {
            $this->datatables->where('created_by', $this->session->userdata('user_id'));
        }
        $this->datatables->where('expenses.store_id', $this->session->userdata('store_id'));
        $this->datatables->edit_column('category', "<span class='btn btn-info btn-xs'>$3</span>", 'mm_sub_categories.id, category, cat_name');
        $this->datatables->edit_column('sub_category', "<span class='btn btn-info btn-xs'>$4</span>", 'mm_sub_categories.id, category, cat_name, sub_cat_name');
        $this->datatables->add_column('Actions', "<div class='text-center'><div class='btn-group'><a href='" . site_url('purchases/expense_note/$1') . "' title='" . lang('expense_note') . "' class='tip btn btn-primary btn-xs' data-toggle='ajax-modal'><i class='fa fa-file-text-o'></i></a> <a href='" . site_url('purchases/edit_expense/$1') . "' title='" . lang('edit_expense') . "' class='tip btn btn-warning btn-xs'><i class='fa fa-edit'></i></a> <a href='" . site_url('purchases/delete_expense/$1') . "' onClick=\"return confirm('" . lang('alert_x_expense') . "')\" title='" . lang('delete_expense') . "' class='tip btn btn-danger btn-xs'><i class='fa fa-trash-o'></i></a></div></div>", 'id');
        $this->datatables->unset_column('id');
        
        if($category) { $this->datatables->where('expenses.category', $category); }
        if($sub_category) { $this->datatables->where('sub_category', $sub_category); }
        if($start_date) { $this->datatables->where('date >=', $start_date); }
        if($end_date) { $this->datatables->where('date <=', $end_date); }
        
        echo $this->datatables->generate();
    }

    public function get_purchases()
    {
        if (!$this->Admin) {
            $this->session->set_flashdata('error', lang('access_denied'));
            redirect('pos');
        }
        $this->load->library('datatables');
        $this->datatables->select('id,date, DATE_FORMAT(date, "%d-%m-%Y") as purchase_date, DATE_FORMAT(payment_done_date, "%d-%m-%Y") as payment_done_dates,reference, total, note, attachment ,paid, balance, payment_type, payment_status, supplier_id, IF(payment_type="1","Cash",IF(payment_type="2","Acc_Transfer",IF(payment_type="3","Online",IF(payment_type="4","UPI",IF(payment_type="5","Debit_Card",IF(payment_type="6","Credit_Card",payment_type)))))) as payment_type_name, (SELECT name FROM tec_suppliers WHERE tec_suppliers.id=tec_purchases.supplier_id) as supplier_name');
        $this->datatables->from('purchases');
        
        if (!$this->Admin && !$this->session->userdata('view_right')) {
            $this->datatables->where('created_by', $this->session->userdata('user_id'));
        }
        $this->datatables->where('store_id', $this->session->userdata('store_id'));
    //    $this->datatables->order_by("date", "desc");
        $this->datatables->add_column('Actions', "<div class='text-center'><div class='btn-group'><a href='" . site_url('purchases/view/$1') . "' title='" . lang('view_purchase') . "' class='tip btn btn-primary btn-xs' data-toggle='ajax-modal'><i class='fa fa-file-text-o'></i></a> <a href='" . site_url('purchases/edit/$1') . "' title='" . lang('edit_purchase') . "' class='tip btn btn-warning btn-xs'><i class='fa fa-edit'></i></a> <a href='" . site_url('purchases/delete/$1') . "' onClick=\"return confirm('" . lang('alert_x_purchase') . "')\" title='" . lang('delete_purchase') . "' class='tip btn btn-danger btn-xs'><i class='fa fa-trash-o'></i></a></div></div>", 'id');

        $this->datatables->unset_column('id');
        echo $this->datatables->generate();
    }

    public function index()
    {
        if (!$this->Admin) {
            $this->session->set_flashdata('error', lang('access_denied'));
            redirect('pos');
        }
        $this->data['error']      = (validation_errors() ? validation_errors() : $this->session->flashdata('error'));
        $this->data['purchase_category'] = $this->site->getAllMmCategory(3);
        $this->data['page_title'] = lang('purchases');
        $bc                       = [['link' => '#', 'page' => lang('purchases')]];
        $meta                     = ['page_title' => lang('purchases'), 'bc' => $bc];
        $this->page_construct('purchases/index', $this->data, $meta);
    }

    public function suggestions($id = null)
    {
        if ($id) {
            $row      = $this->site->getProductByID($id);
            $row->qty = 1;
            $pr       = ['id' => str_replace('.', '', microtime(true)), 'item_id' => $row->id, 'label' => $row->name . ' (' . $row->code . ')', 'row' => $row];
            echo json_encode($pr);
            die();
        }
        $term = $this->tec->parse_scale_barcode($this->input->get('term', true));
        if (is_array($term)) {
            $bqty   = $term['weight'] ?? null;
            $bprice = $term['price']  ?? null;
            $term   = $term['item_code'];
            $rows   = $this->purchases_model->getProductNames($term, null, true);
        }
        if (!$rows) {
            $bqty   = null;
            $bprice = null;
            $term   = $this->input->get('term', true);
            $rows   = $this->purchases_model->getProductNames($term);
        }
        if ($rows) {
            foreach ($rows as $row) {
                $row->qty = $bqty ?: ($bprice ? $bprice / $row->price : 1);
                $pr[]     = ['id' => str_replace('.', '', microtime(true)), 'item_id' => $row->id, 'label' => $row->name . ' (' . $row->code . ')', 'row' => $row];
            }
            echo json_encode($pr);
        } else {
            echo json_encode([['id' => 0, 'label' => lang('no_match_found'), 'value' => $term]]);
        }
    }

    public function view($id = null)
    {
        if (!$this->Admin) {
            $this->session->set_flashdata('error', lang('access_denied'));
            redirect('pos');
        }
        $this->data['purchase']   = $this->purchases_model->getPurchaseByID($id);
        $this->data['items']      = $this->purchases_model->getAllPurchaseItems($id);
        $this->data['error']      = (validation_errors() ? validation_errors() : $this->session->flashdata('error'));
        $this->data['page_title'] = lang('view_purchase');
        $this->load->view($this->theme . 'purchases/view', $this->data);
    }
    
    public function get_sub_category()
    {
        $category = $this->input->get('category');
        $data = $this->site->getAllMmSubCategory($category);
        $content='';
        if($data){
            foreach($data as $d){
                //$content.='<option value="'.$d->id.'">'.$d->sub_name.'</option>';
                $content.='<option value="'.$d->id.'">'.$d->sub_name.'</option>';
            }
        }else{
            $content.='<option value="">No Data Available</option>';
        }
        echo $content;
        
    }
    
    public function get_filter_sub_category()
    {
        $category = $this->input->get('category');
        $data = $this->site->getAllMmSubCategory($category);
        $content='<option value="">All Sub Category</option>';
        
        if($data){
            foreach($data as $d){
                $content.='<option value="'.$d->id.'">'.$d->sub_name.'</option>';
            }
        }else{
            $content.='<option value="">No Data Available</option>';
        }
        echo $content;
        
    }
    
    public function get_select_sub_category()
    {
        $id = $this->input->get('sub_category');
        $data = $this->site->getAllMmSubCategoryByID($id);
        $content='';
        if($data){
                $content.='<option value="'.$data->id.'">'.$data->sub_name.'</option>';
        }else{
            $content.='<option value="">No Data Available</option>';
        }
        echo $content;
    }
    
    
    
    
    
    // Income Starts Here
    /*public function add_income()
    {
        if (!$this->session->userdata('store_id')) {
            $this->session->set_flashdata('warning', lang('please_select_store'));
            redirect('stores');
        }
        $this->load->helper('security');

        $this->form_validation->set_rules('amount', lang('amount'), 'required');
        $this->form_validation->set_rules('userfile', lang('attachment'), 'xss_clean');
        if ($this->form_validation->run() == true) {
            if ($this->Admin) {
                $date = trim($this->input->post('date'));
            } else {
                $date = date('Y-m-d H:i:s');
            }
            $data = [
                'date'       => $date,
                'reference'  => $this->input->post('reference'),
                'amount'     => $this->input->post('amount'),
                'created_by' => $this->session->userdata('user_id'),
                'store_id'   => $this->session->userdata('store_id'),
                'note'       => $this->input->post('note', true),
                'category' => $this->input->post('category'),
                'sub_category' => $this->input->post('sub_category')
                ];

            if ($_FILES['userfile']['size'] > 0) {
                $this->load->library('upload');
                $config['upload_path']   = 'uploads/';
                $config['allowed_types'] = $this->allowed_types;
                $config['max_size']      = '2000';
                $config['overwrite']     = false;
                $config['encrypt_name']  = true;
                $this->upload->initialize($config);
                if (!$this->upload->do_upload()) {
                    $error = $this->upload->display_errors();
                    $this->session->set_flashdata('error', $error);
                    redirect($_SERVER['HTTP_REFERER']);
                }
                $photo              = $this->upload->file_name;
                $data['attachment'] = $photo;
            }

            //$this->tec->print_arrays($data);
        } elseif ($this->input->post('add_expense')) {
            $this->session->set_flashdata('error', validation_errors());
            redirect($_SERVER['HTTP_REFERER']);
        }

        if ($this->form_validation->run() == true && $this->purchases_model->addIncome($data)) {
            $this->session->set_flashdata('message', lang('income_added'));
            redirect('purchases/incomes');
        } else {
            $this->data['error']      = (validation_errors() ? validation_errors() : $this->session->flashdata('error'));
            $this->data['main_category'] = $this->site->getAllMmCategory(2);
            $this->data['page_title'] = lang('add_income');
            $bc                       = [['link' => site_url('purchases'), 'page' => lang('purchases')], ['link' => site_url('purchases/add_income'), 'page' => lang('incomes')], ['link' => '#', 'page' => lang('add_income')]];
            $meta                     = ['page_title' => lang('add_income'), 'bc' => $bc];
            $this->page_construct('purchases/add_income', $this->data, $meta);
        }
    }*/
    
    /* ---- Add Row Based Starts Here ---- */
    public function add_income()
    {
        
        if (!$this->session->userdata('store_id')) {
            $this->session->set_flashdata('warning', lang('please_select_store'));
            redirect('stores');
        }
        
        
        if ($this->input->post('add_income')) {
            
            $date=array();
            $reference=array();
            $amount=array();
            /*$category=array();*/
            $sub_category=array();
            $attachment=array();
            $notes=array();
            
            $date = $this->input->post('date');
            $reference = $this->input->post('reference');
            $amount = $this->input->post('amount');
            $category = $this->input->post('category');
            $sub_category = $this->input->post('sub_category');
            $attachment = $this->input->post('attachment');
            $notes = $this->input->post('note');
            $created_by = $this->session->userdata('user_id');
            
            
            $this->purchases_model->addIncomesRowBased($date,$amount,$category,$sub_category,$notes,$created_by);
            $this->session->set_flashdata('message', lang('income_added'));
            redirect('purchases/incomes');
            } else {
                $this->data['error']      = (validation_errors() ? validation_errors() : $this->session->flashdata('error'));
                $this->data['main_category'] = $this->site->getAllMmCategory(2);
                $this->data['page_title'] = lang('add_income');
                $bc                       = [['link' => site_url('purchases'), 'page' => lang('purchases')], ['link' => site_url('purchases/add_income'), 'page' => lang('incomes')], ['link' => '#', 'page' => lang('add_income')]];
                $meta                     = ['page_title' => lang('add_income'), 'bc' => $bc];
                $this->page_construct('purchases/add_income', $this->data, $meta);
            }
    }
    /* ---- Add Row Based Ends Here ---- */
    public function incomes($id = null, $sub_category = null, $start_date = null, $end_date = null)
    {
        // Filter Starts Here 
        if ($this->input->post('category')) 
        {
            $sub_category = $this->input->post('sub_category') ? $this->input->post('sub_category') : NULL;
            $start_date = $this->input->post('start_date') ? $this->input->post('start_date') : NULL;
            $end_date = $this->input->post('end_date') ? $this->input->post('end_date') : NULL;
            $this->data['purchases'] = $this->purchases_model->getTotalIncomes($this->input->post('category'), $sub_category, $start_date, $end_date);
        }
        // Filter Ends Here
        
        $this->data['error']      = (validation_errors()) ? validation_errors() : $this->session->flashdata('error');
        $this->data['page_title'] = lang('incomes');
        $this->data['expense_category'] = $this->site->getAllMmCategory(2);
        $bc                       = [['link' => site_url('purchases'), 'page' => lang('purchases')], ['link' => '#', 'page' => lang('incomes')]];
        $meta                     = ['page_title' => lang('incomes'), 'bc' => $bc];
        $this->page_construct('purchases/incomes', $this->data, $meta);
    }
    
    public function edit_income($id = null)
    {
        if (!$this->Admin) {
            $this->session->set_flashdata('error', lang('access_denied'));
            redirect('pos');
        }
        $this->load->helper('security');
        if ($this->input->get('id')) {
            $id = $this->input->get('id');
        }

        $this->form_validation->set_rules('reference', lang('reference'), 'required');
        $this->form_validation->set_rules('amount', lang('amount'), 'required');
        $this->form_validation->set_rules('userfile', lang('attachment'), 'xss_clean');
        if ($this->form_validation->run() == true) {
            if ($this->Admin) {
                $date = trim($this->input->post('date'));
            } else {
                $date = date('Y-m-d H:i:s');
            }
            $data = [
                'date'      => $date,
                'reference' => $this->input->post('reference'),
                'amount'    => $this->input->post('amount'),
                'note'      => $this->input->post('note', true),
                'category' => $this->input->post('category'),
                'sub_category' => $this->input->post('sub_category'),
            ];
            if ($_FILES['userfile']['size'] > 0) {
                $this->load->library('upload');
                $config['upload_path']   = 'uploads/';
                $config['allowed_types'] = $this->allowed_types;
                $config['max_size']      = '2000';
                $config['overwrite']     = false;
                $config['encrypt_name']  = true;
                $this->upload->initialize($config);
                if (!$this->upload->do_upload()) {
                    $error = $this->upload->display_errors();
                    $this->session->set_flashdata('error', $error);
                    redirect($_SERVER['HTTP_REFERER']);
                }
                $photo              = $this->upload->file_name;
                $data['attachment'] = $photo;
            }

            //$this->tec->print_arrays($data);
        } elseif ($this->input->post('edit_expense')) {
            $this->session->set_flashdata('error', validation_errors());
            redirect($_SERVER['HTTP_REFERER']);
        }

        if ($this->form_validation->run() == true && $this->purchases_model->updateIncomes($id, $data)) {
            $this->session->set_flashdata('message', lang('income_updated'));
            redirect('purchases/incomes');
        } else {
            $this->data['error']      = (validation_errors() ? validation_errors() : $this->session->flashdata('error'));
            $this->data['main_category'] = $this->site->getAllMmCategory(2);
            $this->data['income']    = $this->purchases_model->getIncomesByID($id);
            $this->data['page_title'] = lang('edit_income');
            $bc                       = [['link' => site_url('purchases'), 'page' => lang('purchases')], ['link' => site_url('purchases/incomes'), 'page' => lang('incomes')], ['link' => '#', 'page' => lang('edit_income')]];
            $meta                     = ['page_title' => lang('edit_income'), 'bc' => $bc];
            $this->page_construct('purchases/edit_income', $this->data, $meta);
        }
    }
    public function delete_income($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');
        }

        $expense = $this->purchases_model->getIncomesByID($id);
        if ($this->purchases_model->deleteIncomes($id)) {
            if ($expense->attachment) {
                unlink($this->upload_path . $expense->attachment);
            }
            $this->session->set_flashdata('message', lang('income_deleted'));
            redirect('purchases/incomes');
        }
    }
    
    public function get_incomes($user_id = null)
    {
        $category = $this->input->get('category') ? $this->input->get('category') : NULL;
        $sub_category = $this->input->get('sub_category') ? $this->input->get('sub_category') : NULL;
        $start_date = $this->input->get('start_date') ? $this->input->get('start_date') : NULL;
        $end_date = $this->input->get('end_date') ? $this->input->get('end_date') : NULL;
        
        $detail_link = anchor('purchases/expense_note/$1', '<i class="fa fa-file-text-o"></i> ' . lang('expense_note'), 'data-toggle="modal" data-target="#myModal2"');
        $edit_link   = anchor('purchases/edit_expense/$1', '<i class="fa fa-edit"></i> ' . lang('edit_expense'), 'data-toggle="modal" data-target="#myModal"');
        $delete_link = "<a href='#' class='po' title='<b>" . $this->lang->line('delete_expense') . "</b>' data-content=\"<p>"
            . lang('r_u_sure') . "</p><a class='btn btn-danger po-delete' href='" . site_url('purchases/delete_expense/$1') . "'>"
            . lang('i_m_sure') . "</a> <button class='btn po-close'>" . lang('no') . "</button>\"  rel='popover'><i class=\"fa fa-trash-o\"></i> "
            . lang('delete_expense') . '</a>';
        $action = '<div class="text-center"><div class="btn-group text-left">'
            . '<button type="button" class="btn btn-default btn-xs btn-primary dropdown-toggle" data-toggle="dropdown">'
            . lang('actions') . ' <span class="caret"></span></button>
        <ul class="dropdown-menu pull-right" role="menu">
            <li>' . $detail_link . '</li>
            <li>' . $edit_link . '</li>
            <li>' . $delete_link . '</li>
        </ul>
    </div></div>';

        $this->load->library('datatables');
        if ($this->db->dbdriver == 'sqlite3') {
            $this->datatables->select($this->db->dbprefix('incomes') . '.id as id, date, reference, amount, note, incomes.category, sub_category, IF(tec_incomes.category=tec_mm_categories.id,mm_categories.name,code) as cat_name, IF(tec_incomes.sub_category=tec_mm_sub_categories.id,mm_sub_categories.sub_name,sub_conf1) as sub_cat_name, (' . $this->db->dbprefix('users') . ".first_name || ' ' || " . $this->db->dbprefix('users') . '.last_name) as user, attachment', false);
        } else {
            $this->datatables->select($this->db->dbprefix('incomes') . '.id as id, date, reference, amount, note, incomes.category, sub_category, IF(tec_incomes.category=tec_mm_categories.id,mm_categories.name,code) as cat_name, IF(tec_incomes.sub_category=tec_mm_sub_categories.id,mm_sub_categories.sub_name,sub_conf1) as sub_cat_name, CONCAT(' . $this->db->dbprefix('users') . ".first_name, ' ', " . $this->db->dbprefix('users') . '.last_name) as user, attachment', false);
        }
        $this->datatables->from('incomes')
            ->join('users', 'users.id=incomes.created_by', 'left')
            ->group_by('incomes.id');
        
        $this->datatables->join('mm_categories', 'incomes.category = mm_categories.id','inner');
        $this->datatables->join('mm_sub_categories', 'incomes.sub_category = mm_sub_categories.id','inner');
        
        if (!$this->Admin && !$this->session->userdata('view_right')) {
            $this->datatables->where('created_by', $this->session->userdata('user_id'));
        }
        $this->datatables->where('incomes.store_id', $this->session->userdata('store_id'));
        $this->datatables->edit_column('category', "<span class='btn btn-info btn-xs'>$3</span>", 'mm_sub_categories.id, category, cat_name');
        $this->datatables->edit_column('sub_category', "<span class='btn btn-info btn-xs'>$4</span>", 'mm_sub_categories.id, category, cat_name, sub_cat_name');
        $this->datatables->add_column('Actions', "<div class='text-center'><div class='btn-group'><a href='" . site_url('purchases/expense_note/$1') . "' title='" . lang('income_note') . "' class='tip btn btn-primary btn-xs' data-toggle='ajax-modal'><i class='fa fa-file-text-o'></i></a> <a href='" . site_url('purchases/edit_income/$1') . "' title='" . lang('edit_income') . "' class='tip btn btn-warning btn-xs'><i class='fa fa-edit'></i></a> <a href='" . site_url('purchases/delete_income/$1') . "' onClick=\"return confirm('" . lang('alert_x_expense') . "')\" title='" . lang('delete_expense') . "' class='tip btn btn-danger btn-xs'><i class='fa fa-trash-o'></i></a></div></div>", 'id');
        $this->datatables->unset_column('id');
        
        if($category) { $this->datatables->where('incomes.category', $category); }
        if($sub_category) { $this->datatables->where('sub_category', $sub_category); }
        if($start_date) { $this->datatables->where('date >=', $start_date); }
        if($end_date) { $this->datatables->where('date <=', $end_date); }
        
        echo $this->datatables->generate();
    }
    // Income Ends Here
    
    
}

Zerion Mini Shell 1.0