ok

Mini Shell

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

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

class Reports extends MY_Controller
{

    function __construct() {
        parent::__construct();


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

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

        $this->load->model('reports_model');
    }

    function daily_sales($year = NULL, $month = NULL) {
        if (!$year) { $year = date('Y'); }
        if (!$month) { $month = date('m'); }
        $this->data['error'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('error');
        $this->lang->load('calendar');
        $config = array(
            'show_next_prev' => TRUE,
            'next_prev_url' => site_url('reports/daily_sales'),
            'month_type' => 'long',
            'day_type' => 'long'
            );
        $config['template'] = '

        {table_open}<table border="0" cellpadding="0" cellspacing="0" class="table table-bordered table-calendar" style="min-width:522px;">{/table_open}

        {heading_row_start}<tr class="active">{/heading_row_start}

        {heading_previous_cell}<th><div class="text-center"><a href="{previous_url}">&lt;&lt;</div></a></th>{/heading_previous_cell}
        {heading_title_cell}<th colspan="{colspan}"><div class="text-center">{heading}</div></th>{/heading_title_cell}
        {heading_next_cell}<th><div class="text-center"><a href="{next_url}">&gt;&gt;</a></div></th>{/heading_next_cell}

        {heading_row_end}</tr>{/heading_row_end}

        {week_row_start}<tr>{/week_row_start}
        {week_day_cell}<td class="cl_equal"><div class="cl_wday">{week_day}</div></td>{/week_day_cell}
        {week_row_end}</tr>{/week_row_end}

        {cal_row_start}<tr>{/cal_row_start}
        {cal_cell_start}<td>{/cal_cell_start}

        {cal_cell_content}{day}<br>{content}{/cal_cell_content}
        {cal_cell_content_today}<div class="highlight">{day}</div>{content}{/cal_cell_content_today}

        {cal_cell_no_content}{day}{/cal_cell_no_content}
        {cal_cell_no_content_today}<div class="highlight">{day}</div>{/cal_cell_no_content_today}

        {cal_cell_blank}&nbsp;{/cal_cell_blank}

        {cal_cell_end}</td>{/cal_cell_end}
        {cal_row_end}</tr>{/cal_row_end}

        {table_close}</table>{/table_close}
        ';

        $this->load->library('calendar', $config);

        $sales = $this->reports_model->getDailySales($year, $month);

        if (!empty($sales)) {
            foreach ($sales as $sale) {
                $sale->date = intval($sale->date);
                $daily_sale[$sale->date] = "<table class='table table-condensed table-striped' style='margin-bottom:0;'><tr><td>".lang('total').
                "</td><td style='text-align:right;'>{$this->tec->formatMoney($sale->total)}</td></tr><tr><td><span style='font-weight:normal;'>".lang('product_tax')."<br>".lang('order_tax')."</span><br>".lang('tax').
                "</td><td style='text-align:right;'><span style='font-weight:normal;'>{$this->tec->formatMoney($sale->product_tax)}<br>{$this->tec->formatMoney($sale->order_tax)}</span><br>{$this->tec->formatMoney($sale->total_tax)}</td></tr><tr><td class='violet'>".lang('discount').
                "</td><td style='text-align:right;'>{$this->tec->formatMoney($sale->discount)}</td></tr><tr><td class='violet'>".lang('grand_total').
                "</td><td style='text-align:right;' class='violet'>{$this->tec->formatMoney($sale->grand_total)}</td></tr><tr><td class='green'>".lang('paid').
                "</td><td style='text-align:right;' class='green'>{$this->tec->formatMoney($sale->paid)}</td></tr><tr><td class='orange'>".lang('balance').
                "</td><td style='text-align:right;' class='orange'>{$this->tec->formatMoney(($sale->grand_total+$sale->rounding) - $sale->paid)}</td></tr></table>";
            }
        } else {
            $daily_sale = array();
        }

        $this->data['error'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('error');
        $this->data['calender'] = $this->calendar->generate($year, $month, $daily_sale);

        $start = $year.'-'.$month.'-01 00:00:00';
        $end = $year.'-'.$month.'-'.days_in_month($month, $year).' 23:59:59';
        $this->data['total_purchases'] = $this->reports_model->getTotalPurchases($start, $end);
        $this->data['total_sales'] = $this->reports_model->getTotalSales($start, $end);
        $this->data['total_expenses'] = $this->reports_model->getTotalExpenses($start, $end);

        $this->data['page_title'] = $this->lang->line("daily_sales");
        $bc = array(array('link' => '#', 'page' => lang('reports')), array('link' => '#', 'page' => lang('daily_sales')));
        $meta = array('page_title' => lang('daily_sales'), 'bc' => $bc);
        $this->page_construct('reports/daily', $this->data, $meta);

    }


    function monthly_sales($year = NULL) {
        if(!$year) { $year = date('Y'); }
        $this->load->language('calendar');
        $this->lang->load('calendar');
        $this->data['error'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('error');
        $start = $year.'-01-01 00:00:00';
        $end = $year.'-12-31 23:59:59';
        $this->data['total_purchases'] = $this->reports_model->getTotalPurchases($start, $end);
        $this->data['total_sales'] = $this->reports_model->getTotalSales($start, $end);
        $this->data['total_expenses'] = $this->reports_model->getTotalExpenses($start, $end);
        $this->data['year'] = $year;
        $this->data['sales'] = $this->reports_model->getMonthlySales($year);
        $this->data['page_title'] = $this->lang->line("monthly_sales");
        $bc = array(array('link' => '#', 'page' => lang('reports')), array('link' => '#', 'page' => lang('monthly_sales')));
        $meta = array('page_title' => lang('monthly_sales'), 'bc' => $bc);
        $this->page_construct('reports/monthly', $this->data, $meta);
    }

    function index() {
        if ($this->input->post('customer')) {
            $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;
            $user = $this->input->post('user') ? $this->input->post('user') : NULL;
            $this->data['total_sales'] = $this->reports_model->getTotalCustomerSales($this->input->post('customer'), $user, $start_date, $end_date);
        }
        $this->data['error'] = (validation_errors() ? validation_errors() : $this->session->flashdata('error'));
        $this->data['customers'] = $this->reports_model->getAllCustomers();
        $this->data['users'] = $this->reports_model->getAllStaff();
        $this->data['page_title'] = $this->lang->line("sales_report");
        $bc = array(array('link' => '#', 'page' => lang('reports')), array('link' => '#', 'page' => lang('sales_report')));
        $meta = array('page_title' => lang('sales_report'), 'bc' => $bc);
        $this->page_construct('reports/sales', $this->data, $meta);
    }

    function get_sales() {
        $customer = $this->input->get('customer') ? $this->input->get('customer') : 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;
        $user = $this->input->get('user') ? $this->input->get('user') : NULL;

        $this->load->library('datatables');
        $this->datatables
        ->select("id, DATE_FORMAT(sale_date, '%d-%m-%Y') as sale_date, customer_name, hold_ref, discount, total, total_tax, total_discount, grand_total, paid, (grand_total-(paid+discount)) as balance, status, DATE_FORMAT(due_date, '%d-%m-%Y') as due_date")
        ->from('sales');
        if ($this->session->userdata('store_id')) {
            $this->datatables->where('store_id', $this->session->userdata('store_id'));
        }
        $this->datatables->unset_column('id');
        if($customer) { $this->datatables->where('customer_id', $customer); }
        if($user) { $this->datatables->where('created_by', $user); }
        if($start_date) { $this->datatables->where('due_date >=', $start_date); }
        if($end_date) { $this->datatables->where('due_date <=', $end_date); }

        echo $this->datatables->generate();
    }

    function products() {
        $this->data['error'] = (validation_errors() ? validation_errors() : $this->session->flashdata('error'));
        $this->data['products'] = $this->reports_model->getAllProducts();
        $this->data['page_title'] = $this->lang->line("products_report");
        $this->data['page_title'] = $this->lang->line("products_report");
        $bc = array(array('link' => '#', 'page' => lang('reports')), array('link' => '#', 'page' => lang('products_report')));
        $meta = array('page_title' => lang('products_report'), 'bc' => $bc);
        $this->page_construct('reports/products', $this->data, $meta);
    }

    function get_products() {
        $product = $this->input->get('product') ? $this->input->get('product') : 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;
        //COALESCE(sum(".$this->db->dbprefix('sale_items').".quantity)*".$this->db->dbprefix('products').".cost, 0) as cost,
        $this->load->library('datatables');
        $this->datatables
        ->select($this->db->dbprefix('products').".id as id, ".$this->db->dbprefix('products').".name, ".$this->db->dbprefix('products').".code, COALESCE(sum(".$this->db->dbprefix('sale_items').".quantity), 0) as sold, ROUND(COALESCE(((sum(".$this->db->dbprefix('sale_items').".subtotal)*".$this->db->dbprefix('products').".tax)/100), 0), 2) as tax, COALESCE(sum(".$this->db->dbprefix('sale_items').".quantity)*".$this->db->dbprefix('sale_items').".cost, 0) as cost, COALESCE(sum(".$this->db->dbprefix('sale_items').".subtotal), 0) as income, ROUND((COALESCE(sum(".$this->db->dbprefix('sale_items').".subtotal), 0)) - COALESCE(sum(".$this->db->dbprefix('sale_items').".quantity)*".$this->db->dbprefix('sale_items').".cost, 0) -COALESCE(((sum(".$this->db->dbprefix('sale_items').".subtotal)*".$this->db->dbprefix('products').".tax)/100), 0), 2)
            as profit", FALSE)
        ->from('sale_items')
        ->join('products', 'sale_items.product_id=products.id', 'left')
        ->join('sales', 'sale_items.sale_id=sales.id', 'left');
        if ($this->session->userdata('store_id')) {
            $this->datatables->where('sales.store_id', $this->session->userdata('store_id'));
        }
        $this->datatables->group_by('products.id');

        if($product) { $this->datatables->where('products.id', $product); }
        if($start_date) { $this->datatables->where('date >=', $start_date); }
        if($end_date) { $this->datatables->where('date <=', $end_date); }
        echo $this->datatables->generate();
    }

    function profit( $income, $cost, $tax) {
        return floatval($income)." - ".floatval($cost)." - ".floatval($tax);
    }

    function top_products() {
        $this->data['topProducts'] = $this->reports_model->topProducts();
        $this->data['topProducts1'] = $this->reports_model->topProducts1();
        $this->data['topProducts3'] = $this->reports_model->topProducts3();
        $this->data['topProducts12'] = $this->reports_model->topProducts12();
        $this->data['page_title'] = $this->lang->line("top_products");
        $bc = array(array('link' => '#', 'page' => lang('reports')), array('link' => '#', 'page' => lang('top_products')));
        $meta = array('page_title' => lang('top_products'), 'bc' => $bc);
        $this->page_construct('reports/top', $this->data, $meta);
    }

    function registers() {
        $this->data['error'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('error');
        $this->data['users'] = $this->reports_model->getAllStaff();
        $bc = array(array('link' => '#', 'page' => lang('reports')), array('link' => '#', 'page' => lang('registers_report')));
        $meta = array('page_title' => lang('registers_report'), 'bc' => $bc);
        $this->page_construct('reports/registers', $this->data, $meta);
    }

    function get_register_logs() {
        $user = $this->input->get('user') ? $this->input->get('user') : 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;

        $this->load->library('datatables');
        if ($this->db->dbdriver == 'sqlite3') {
            $this->datatables->select("{$this->db->dbprefix('registers')}.id as id, date, closed_at, ({$this->db->dbprefix('users')}.first_name || ' ' || {$this->db->dbprefix('users')}.last_name || '<br>' || {$this->db->dbprefix('users')}.email) as user, cash_in_hand, (total_cc_slips || ' (' || total_cc_slips_submitted || ')') as cc_slips, (total_cheques || ' (' || total_cheques_submitted || ')') as total_cheques, (total_cash || ' (' || total_cash_submitted || ')') as total_cash, note", FALSE);
        } else {
            $this->datatables->select("{$this->db->dbprefix('registers')}.id as id, date, closed_at, CONCAT(" . $this->db->dbprefix('users') . ".first_name, ' ', " . $this->db->dbprefix('users') . ".last_name, '<br>', " . $this->db->dbprefix('users') . ".email) as user, cash_in_hand, CONCAT(total_cc_slips, ' (', total_cc_slips_submitted, ')') as cc_slips, CONCAT(total_cheques, ' (', total_cheques_submitted, ')') as total_cheques, CONCAT(total_cash, ' (', total_cash_submitted, ')') as total_cash, note", FALSE);
        }
        $this->datatables->from("registers")
        ->join('users', 'users.id=registers.user_id', 'left');

        if ($user) {
            $this->datatables->where('registers.user_id', $user);
        }
        if ($start_date) {
            $this->datatables->where('date  >=', $start_date)->where('date <=', $end_date);
        }
        if ($this->session->userdata('store_id')) {
            $this->datatables->where('registers.store_id', $this->session->userdata('store_id'));
        }

        echo $this->datatables->generate();


    }

    function payments() {
        if ($this->input->post('customer')) {
            $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;
            $user = $this->input->post('user') ? $this->input->post('user') : NULL;
            $this->data['total_sales'] = $this->reports_model->getTotalCustomerSales($this->input->post('customer'), $user, $start_date, $end_date);
        }
        $this->data['error'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('error');
        $this->data['users'] = $this->reports_model->getAllStaff();
        $this->data['customers'] = $this->reports_model->getAllCustomers();
        $bc = array(array('link' => '#', 'page' => lang('reports')), array('link' => '#', 'page' => lang('payments_report')));
        $meta = array('page_title' => lang('payments_report'), 'bc' => $bc);
        $this->page_construct('reports/payments', $this->data, $meta);
    }
    
    

    function get_payments() {
        $user = $this->input->get('user') ? $this->input->get('user') : NULL;
        $ref = $this->input->get('payment_ref') ? $this->input->get('payment_ref') : NULL;
        $sale_id = $this->input->get('sale_no') ? $this->input->get('sale_no') : NULL;
        $customer = $this->input->get('customer') ? $this->input->get('customer') : NULL;
        $paid_by = $this->input->get('paid_by') ? $this->input->get('paid_by') : 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;

        $this->load->library('datatables');
        $this->datatables
        ->select("{$this->db->dbprefix('payments')}.id as id, {$this->db->dbprefix('payments')}.date, {$this->db->dbprefix('payments')}.reference as ref, {$this->db->dbprefix('sales')}.id as sale_no, paid_by, amount, DATE_FORMAT({$this->db->dbprefix('payments')}.date, '%d-%m-%Y') as payment_date, hold_ref")
        ->from('payments')
        ->join('sales', 'payments.sale_id=sales.id', 'left')
        ->group_by('payments.id');

        if ($this->session->userdata('store_id')) {
            $this->datatables->where('payments.store_id', $this->session->userdata('store_id'));
        }
        if ($user) {
            $this->datatables->where('payments.created_by', $user);
        }
        if ($ref) {
            $this->datatables->where('payments.reference', $ref);
        }
        if ($paid_by) {
            $this->datatables->where('payments.paid_by', $paid_by);
        }
        if ($sale_id) {
            $this->datatables->where('sales.hold_ref', $sale_id);
        }
        if ($customer) {
            $this->datatables->where('sales.customer_id', $customer);
        }
        if ($start_date) {
            $this->datatables->where("{$this->db->dbprefix('payments')}.date  >=", $start_date)
                ->where("{$this->db->dbprefix('payments')}.date <=", $end_date);
        }

        echo $this->datatables->generate();

    }
    
    /* Orginal Get Payments Function
    function get_payments() {
        $user = $this->input->get('user') ? $this->input->get('user') : NULL;
        $ref = $this->input->get('payment_ref') ? $this->input->get('payment_ref') : NULL;
        $sale_id = $this->input->get('sale_no') ? $this->input->get('sale_no') : NULL;
        $customer = $this->input->get('customer') ? $this->input->get('customer') : NULL;
        $paid_by = $this->input->get('paid_by') ? $this->input->get('paid_by') : 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;

        $this->load->library('datatables');
        $this->datatables
        ->select("{$this->db->dbprefix('payments')}.id as id, {$this->db->dbprefix('payments')}.date, {$this->db->dbprefix('payments')}.reference as ref, {$this->db->dbprefix('sales')}.id as sale_no, paid_by, amount")
        ->from('payments')
        ->join('sales', 'payments.sale_id=sales.id', 'left')
        ->group_by('payments.id');

        if ($this->session->userdata('store_id')) {
            $this->datatables->where('payments.store_id', $this->session->userdata('store_id'));
        }
        if ($user) {
            $this->datatables->where('payments.created_by', $user);
        }
        if ($ref) {
            $this->datatables->where('payments.reference', $ref);
        }
        if ($paid_by) {
            $this->datatables->where('payments.paid_by', $paid_by);
        }
        if ($sale_id) {
            $this->datatables->where('sales.id', $sale_id);
        }
        if ($customer) {
            $this->datatables->where('sales.customer_id', $customer);
        }
        if ($start_date) {
            $this->datatables->where("{$this->db->dbprefix('payments')}.date  >=", $start_date)
                ->where("{$this->db->dbprefix('payments')}.date <=", $end_date);
        }

        echo $this->datatables->generate();

    }*/

    function alerts() {
        $data['error'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('error');
        $this->data['page_title'] = lang('stock_alert');
        $bc = array(array('link' => '#', 'page' => lang('stock_alert')));
        $meta = array('page_title' => lang('stock_alert'), 'bc' => $bc);
        $this->page_construct('reports/alerts', $this->data, $meta);

    }

    function get_alerts() {
        $this->load->library('datatables');
        $this->datatables->select($this->db->dbprefix('products').".id as id, ".$this->db->dbprefix('products').".image as image, ".$this->db->dbprefix('products').".code as code, ".$this->db->dbprefix('products').".name as pname, type, ".$this->db->dbprefix('categories').".name as cname, (CASE WHEN psq.quantity IS NULL THEN 0 ELSE psq.quantity END) as quantity, alert_quantity, tax, tax_method, cost, (CASE WHEN psq.price > 0 THEN psq.price ELSE {$this->db->dbprefix('products')}.price END) as price", FALSE)
        ->from('products')
        ->join('categories', 'categories.id=products.category_id')
        ->join("( SELECT * from {$this->db->dbprefix('product_store_qty')} WHERE store_id = {$this->session->userdata('store_id')}) psq", 'products.id=psq.product_id', 'left')
        ->where("(CASE WHEN psq.quantity IS NULL THEN 0 ELSE psq.quantity END) < {$this->db->dbprefix('products')}.alert_quantity", NULL, FALSE)
        ->group_by('products.id');
        $this->datatables->add_column("Actions", "<div class='text-center'><a href='#' class='btn btn-xs btn-primary ap tip' data-id='$1' title='".lang('add_to_purcahse_order')."'><i class='fa fa-plus'></i></a></div>", "id");
        // $this->datatables->unset_column('id');
        echo $this->datatables->generate();
    }
    
    
    //////////////////////--------------------------- New Reports Starts Here ----------------------------//////////////////////////
    /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~   Customers Reports Here   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
    public function get_customers()
    {
        $this->load->library('datatables');
        $this->datatables
        ->select('customers.id, name, phone, email, cf1, cf2, (SELECT COUNT(id) from tec_sales where tec_sales.customer_id=tec_customers.id) as dc_count,(SELECT (SUM(tec_sales.grand_total) - SUM(tec_sales.paid))from tec_sales where tec_sales.customer_id=tec_customers.id) AS customer_balance')
        ->from('customers');
        $this->datatables->add_column('Actions', "<div class='text-center'><div class='btn-group'><a href='" . site_url('customers/edit/$1') . "' class='tip btn btn-warning btn-xs' title='" . $this->lang->line('edit_customer') . "'><i class='fa fa-edit'></i></a> <a href='" . site_url('customers/delete/$1') . "' onClick=\"return confirm('" . $this->lang->line('alert_x_customer') . "')\" class='tip btn btn-danger btn-xs' title='" . $this->lang->line('delete_customer') . "'><i class='fa fa-trash-o'></i></a></div></div>", 'id')
        ->unset_column('id');
        $this->datatables->add_column('Clone', "<div class='text-center'><div class='btn-group'><span href='' class='tip btn btn-info btn-xs clone' title='Customers History' id='$1'><i class='fa fa-check-circle-o'> Customers Sales History</i></span></div></div>", 'id,dc_count');
        $this->datatables->add_column('DC Count', "<div class='text-center'><div class='btn-group'><span href='' class='tip btn btn-success btn-xs' title='DC Count'>DC Count : <b>$2</b></i></span></div></div>", 'id,dc_count');
        $this->datatables->add_column('Balance', "<div class='text-center'><div class='btn-group'><span href='' class='tip btn btn-danger btn-xs clone' style='font-family: arial;' title='Customers Balance' id='$1'><i class='fa fa-inr'> Balance: $2 </i></span></div></div>", 'id,customer_balance');

        echo $this->datatables->generate();
    }
    public function customers()
    {
        $this->data['error']      = (validation_errors()) ? validation_errors() : $this->session->flashdata('error');
        $this->data['page_title'] = lang('customers_report');
        $bc                       = [['link' => '#', 'page' => lang('customers_report')]];
        $meta                     = ['page_title' => lang('customers_report'), 'bc' => $bc];
        $this->page_construct('reports/customers', $this->data, $meta);
    }
    /******************************* Get Sale Items Starts Here ********************************/
    public function sale_items_report()
    {
        $this->data['error']      = (validation_errors()) ? validation_errors() : $this->session->flashdata('error');
        $this->data['products'] = $this->reports_model->getAllProducts();
        $this->data['page_title'] = lang('sale_items_report');
        $bc                       = [['link' => '#', 'page' => lang('sale_items_report')]];
        $meta                     = ['page_title' => lang('sale_items_report'), 'bc' => $bc];
        $this->page_construct('reports/sale_items_report', $this->data, $meta);
    }
    function get_all_sale_items() {
        $product = $this->input->get('product') ? $this->input->get('product') : 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;

        /*->select($this->db->dbprefix('sale_items').".id as sale_items_id, DATE_FORMAT(sale_date, '%d-%m-%Y') as sale_date, customer_name, hold_ref, discount, total, total_tax, total_discount, grand_total, paid, (grand_total-(paid+discount)) as balance, status, DATE_FORMAT(due_date, '%d-%m-%Y') as due_date")*/
        
        $this->load->library('datatables');
        $this->datatables
        ->select($this->db->dbprefix('sale_items').".id as sale_items_id, DATE_FORMAT(sale_date, '%d-%m-%Y') as sale_date, customer_name, quantity, hold_ref, total, total_tax, total_discount, grand_total, paid, status, DATE_FORMAT(due_date, '%d-%m-%Y') as due_date, product_name, product_type")
        ->from('sale_items')
        ->join('sales', 'sale_items.sale_id=sales.id', 'left');
        
        if ($this->session->userdata('store_id')) {
            $this->datatables->where('store_id', $this->session->userdata('store_id'));
        }
        
        $this->datatables->unset_column('id');
        if($product) { $this->datatables->where('product_id', $product); }
        if($start_date) { $this->datatables->where('due_date >=', $start_date); }
        if($end_date) { $this->datatables->where('due_date <=', $end_date); }

        echo $this->datatables->generate();
    }
    /******************************* Get Sale Items Endss Here **********************************/
    /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~   Incomes Reports Here   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
    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('incomes/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_FORMAT(date, "%d-%m-%Y") as 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_FORMAT(date, "%d-%m-%Y") as 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');
            
        
        $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('incomes/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('incomes/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('incomes/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); }
        
        /*$this->datatables->order_by('incomes.date', "asc");*/
        
        echo $this->datatables->generate();
    }
    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['mm_category'] = $this->site->getAllMMCategories();
        $this->data['expense_category'] = $this->site->getAllMmCategory(2);
        $bc                       = [['link' => site_url('incomes'), 'page' => lang('incomes')], ['link' => '#', 'page' => lang('incomes')]];
        $meta                     = ['page_title' => lang('incomes'), 'bc' => $bc];
        $this->page_construct('reports/incomes', $this->data, $meta);
    }
    //////////////////////--------------------------- New Reports Endss Here ----------------------------//////////////////////////
    
    //////////////////////------------------------------ New Expenses Report Starts Here -------------------------------//////////////////////////////
    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->expenses_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_report');
        $this->data['mm_category'] = $this->site->getAllMMCategories();
        $this->data['expense_category'] = $this->site->getAllMmCategory(1);
        $bc                       = [['link' => site_url('expenses'), 'page' => lang('expenses_report')], ['link' => '#', 'page' => lang('expenses_report')]];
        $meta                     = ['page_title' => lang('expenses_report'), 'bc' => $bc];
        $this->page_construct('reports/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, SUM(amount) as total_amount', 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, SUM(amount) as total_amount', false);
        }
        $this->datatables->from('expenses')
            ->join('users', 'users.id=expenses.created_by', 'left')
            ->group_by('expenses.sub_category');
        
        $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('expenses/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('expenses/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('expenses/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();
    }
    //////////////////////------------------------------ New Expenses Report Ends Here -------------------------------//////////////////////////////
    
    
    
    /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ New Final Reports Starts Here ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
    function dc_income_report() {
        if ($this->input->post('customer')) {
            $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;
            $user = $this->input->post('user') ? $this->input->post('user') : NULL;
            $this->data['total_sales'] = $this->reports_model->getTotalCustomerSales($this->input->post('customer'), $user, $start_date, $end_date);
        }
        $this->data['error'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('error');
        $this->data['users'] = $this->reports_model->getAllStaff();
        $this->data['customers'] = $this->reports_model->getAllCustomers();
        $bc = array(array('link' => '#', 'page' => lang('reports')), array('link' => '#', 'page' => lang('dc_income_report')));
        $meta = array('page_title' => lang('dc_income_report'), 'bc' => $bc);
        $this->page_construct('reports/dc_incomes_report', $this->data, $meta);
    }
    
    function staff_report() {
        $this->data['error'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('error');
        $this->data['users'] = $this->reports_model->getAllStaff();
        $this->data['customers'] = $this->reports_model->getAllCustomers();
        $bc = array(array('link' => '#', 'page' => lang('reports')), array('link' => '#', 'page' => lang('staff_report')));
        $meta = array('page_title' => lang('staff_report'), 'bc' => $bc);
        $this->page_construct('reports/staff_report', $this->data, $meta);
    }
    function inc_exp_report() {
        $this->data['error'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('error');
        $this->data['users'] = $this->reports_model->getAllStaff();
        $this->data['customers'] = $this->reports_model->getAllCustomers();
        $bc = array(array('link' => '#', 'page' => lang('reports')), array('link' => '#', 'page' => lang('inc_exp_report')));
        $meta = array('page_title' => lang('inc_exp_report'), 'bc' => $bc);
        $this->page_construct('reports/inc_exp_report', $this->data, $meta);
    }
    
    /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ New Final Reports Endss Here ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
    
    /********************* GET DATATABLES NEW REPORTS Starts Here *********************************/
    function get_staff_reports() {
        $user = $this->input->get('users') ? $this->input->get('users') : 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;

$cutting_date_filter='';
$stitching_date_filter='';
if($start_date!='' && $end_date!=''){
    $cutting_date_filter="and wu.cutting_assign_date BETWEEN '$start_date' AND '$end_date'";
    $stitching_date_filter="and wu.stitching_assign_date BETWEEN '$start_date' AND '$end_date'";
}


        $this->load->library('datatables');
        $this->datatables
        ->select("{$this->db->dbprefix('users')}.id as id, first_name, username, 
        IFNULL((SELECT SUM(wu.quantity) FROM `tec_work_units` wu INNER JOIN tec_products ON tec_products.id=wu.product_id WHERE tec_users.id=wu.cutting_user_id and tec_products.pant_shirt_category=1 ".$cutting_date_filter."),0) as cutting_pant, 
        IFNULL((SELECT SUM(wu.quantity) FROM `tec_work_units` wu INNER JOIN tec_products ON tec_products.id=wu.product_id WHERE tec_users.id=wu.cutting_user_id and tec_products.pant_shirt_category=2 ".$cutting_date_filter."),0) as cutting_shirt, 
        IFNULL((SELECT SUM(wu.quantity) FROM `tec_work_units` wu INNER JOIN tec_products ON tec_products.id=wu.product_id WHERE tec_users.id=wu.stitching_user_id and tec_products.pant_shirt_category=2 ".$stitching_date_filter."),0) as stitching_shirt, 
        IFNULL((SELECT SUM(wu.quantity) FROM `tec_work_units` wu INNER JOIN tec_products ON tec_products.id=wu.product_id WHERE tec_users.id=wu.stitching_user_id and tec_products.pant_shirt_category=1 ".$stitching_date_filter."),0) as stitching_pant")
        
        //->select("{$this->db->dbprefix('users')}.id as id, first_name, username, (SELECT COUNT(tec_work_units.id) FROM `tec_work_units` LEFT JOIN tec_products ON tec_products.id=tec_work_units.product_id WHERE tec_users.id=tec_work_units.cutting_user_id and tec_products.pant_shirt_category=1) as pant, (SELECT COUNT(tec_work_units.id) FROM `tec_work_units` LEFT JOIN tec_products ON tec_products.id=tec_work_units.product_id WHERE tec_users.id=tec_work_units.stitching_user_id and tec_products.pant_shirt_category=1) as shirt")
        ->from('users');

        if ($user) {
            $this->datatables->where('users.id', $user);
        }
        /*if ($start_date && $end_date) {
            $where = "(SELECT SUM(wu.quantity) FROM `tec_work_units` wu INNER JOIN tec_products ON tec_products.id=wu.product_id WHERE tec_users.id=wu.cutting_user_id and tec_products.pant_shirt_category=1 ) ";
            $this->datatables->where($where);
           // $this->datatables->where('SELECT SUM(wu.quantity) FROM `tec_work_units` wu INNER JOIN tec_products ON tec_products.id=wu.product_id WHERE tec_users.id=wu.cutting_user_id and tec_products.pant_shirt_category=1', $user);
        }*/
        
        
        
        
        
        /*$where = "(IF(((SELECT sum(tec_sale_items.quantity) FROM tec_sale_items WHERE tec_sale_items.sale_id=tec_sales.id and product_type='service')-(SELECT sum(tec_work_units.quantity) FROM tec_work_units WHERE tec_work_units.sale_id=tec_sales.id and tec_work_units.type=2 and tec_work_units.assign_or_not='assign')) is NULL,(SELECT sum(tec_sale_items.quantity) FROM tec_sale_items WHERE tec_sale_items.sale_id=tec_sales.id  and product_type='service'),((SELECT sum(tec_sale_items.quantity) FROM tec_sale_items WHERE tec_sale_items.sale_id=tec_sales.id and product_type='service')-(SELECT sum(tec_work_units.quantity) FROM tec_work_units WHERE tec_work_units.sale_id=tec_sales.id and tec_work_units.type=2 and tec_work_units.assign_or_not='assign'))) > 0 or IF(((SELECT sum(tec_sale_items.quantity) FROM tec_sale_items WHERE tec_sale_items.sale_id=tec_sales.id  and product_type='service')-(SELECT sum(tec_work_units.quantity) FROM tec_work_units WHERE tec_work_units.sale_id=tec_sales.id and tec_work_units.type=1 and tec_work_units.assign_or_not='assign')) is NULL,(SELECT sum(tec_sale_items.quantity) FROM tec_sale_items WHERE tec_sale_items.sale_id=tec_sales.id  and product_type='service'),(SELECT sum(tec_sale_items.quantity) FROM tec_sale_items WHERE tec_sale_items.sale_id=tec_sales.id and product_type='service')-(SELECT sum(tec_work_units.quantity) FROM tec_work_units WHERE tec_work_units.sale_id=tec_sales.id and tec_work_units.type=1 and tec_work_units.assign_or_not='assign')) > 0) ";
        $this->datatables->where($where);
        
        
        
        
        
        if($end_dc && $start_dc) {$this->datatables->where("hold_ref BETWEEN '$start_dc' AND '$end_dc'");}
        if($due_date) { $this->datatables->where('due_date >=', $due_date); }
        if($due_date) { $this->datatables->where('due_date <=', $due_date); }*/
        

        echo $this->datatables->generate();

    }
    /********************* GET DATATABLES NEW REPORTS Starts Here *********************************/
    
    
    
    
    /*SELECT id, first_name, username, (SELECT COUNT(tec_work_units.id) FROM `tec_work_units` LEFT JOIN tec_products ON tec_products.id=tec_work_units.product_id WHERE tec_users.id=tec_work_units.cutting_user_id and tec_products.pant_shirt_category=1) as cutting_pant,(SELECT COUNT(tec_work_units.id) FROM `tec_work_units` LEFT JOIN tec_products ON tec_products.id=tec_work_units.product_id WHERE tec_users.id=tec_work_units.cutting_user_id and tec_products.pant_shirt_category=2) as cutting_shirt, (SELECT COUNT(tec_work_units.id) FROM `tec_work_units` LEFT JOIN tec_products ON tec_products.id=tec_work_units.product_id WHERE tec_users.id=tec_work_units.stitching_user_id and tec_products.pant_shirt_category=1) as stitching_pant,(SELECT COUNT(tec_work_units.id) FROM `tec_work_units` LEFT JOIN tec_products ON tec_products.id=tec_work_units.product_id WHERE tec_users.id=tec_work_units.stitching_user_id and tec_products.pant_shirt_category=1) as stitching_shirt FROM `tec_users`*/
}

Zerion Mini Shell 1.0