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

class Saleslogtax_model extends CI_Model {

public function __construct() {
parent::__construct();
}
 function getsalelogtax($datefrom,$dateto) {

    //a.s
   $page = $this->input->post('page');
$limit = $this->input->post('rows'); // get how many rows we want to have into the grid
$sidx = $this->input->post('sidx'); // get index row - i.e. user click to sort
$sord = $this->input->post('sord'); // get the direction

//Default values
if (!$sidx) $sidx = 'TAXID';
if (!$sord) $sord = 'asc';

if (!$page) $page = 1;
if (!$limit) $limit = 25;
//Total Returned Rows Count


     $table_name = "RECEIPTS, TAXLINES, TAXES, TAXCATEGORIES";
        $this->db->select("TAXCATEGORIES.ID AS TAXID, TAXCATEGORIES.NAME AS TAXNAME, SUM(TAXLINES.AMOUNT) AS TOTALTAXES ")->from($table_name);
        if ($datefrom && $dateto) {
            $this->db->where("RECEIPTS.DATENEW >=", $datefrom);
            $this->db->where("RECEIPTS.DATENEW <=", $dateto."+1 day");
        } else if ($datefrom) {
            $this->db->where("RECEIPTS.DATENEW LIKE", $datefrom."%");
        }

        $where = 'RECEIPTS.ID = TAXLINES.RECEIPT AND TAXLINES.TAXID = TAXES.ID AND TAXES.CATEGORY = TAXCATEGORIES.ID ';

            $this->db->where($where);

          $this->db->group_by('TAXCATEGORIES.ID,TAXCATEGORIES.NAME');


$count = $this->db->count_all_results();
//Total Pages
if( $count > 0 && $limit > 0) {
$total_pages = ceil($count/$limit);
} else {
$total_pages = 0;
}

//Check requested page
if ($page > $total_pages) $page=$total_pages;

//Set start record number
$start = $limit*$page - $limit;

//Format data for jqGrid output
$data['page'] = "".$page."";
$data['total'] = "".$total_pages."";
$data['records'] = "".$count."";




       $table_name = "RECEIPTS, TAXLINES, TAXES, TAXCATEGORIES";
        $this->db->select("TAXCATEGORIES.ID AS TAXID, TAXCATEGORIES.NAME AS TAXNAME, SUM(TAXLINES.AMOUNT) AS TOTALTAXES ")->from($table_name);
        if ($datefrom && $dateto) {
            $this->db->where("RECEIPTS.DATENEW >=", $datefrom);
            $this->db->where("RECEIPTS.DATENEW <=", $dateto."+1 day");
        } else if ($datefrom) {
            $this->db->where("RECEIPTS.DATENEW LIKE", $datefrom."%");
        }

        $where = 'RECEIPTS.ID = TAXLINES.RECEIPT AND TAXLINES.TAXID = TAXES.ID AND TAXES.CATEGORY = TAXCATEGORIES.ID ';

            $this->db->where($where);

          $this->db->group_by('TAXCATEGORIES.ID,TAXCATEGORIES.NAME');



                    $this->db->order_by($sidx, $sord);
$this->db->limit($limit, $start);
        $rows = $this->db->get()->result_array();
        $i = 0;

        foreach ($rows as $row) {
            $data['rows'][$i]['id'] = $i + 1;
            $data['rows'][$i]['cell'] = array( $row['TAXNAME'], $row['TOTALTAXES']);
            $i++;
        }
        return json_encode($data);
    }

    //a.e
}


