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

class Paidlog_model extends MY_Model
{
	
	protected $table_name = 'tblpaidlog';
	protected $id_name = 'paid_id';

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

	}
	function getPaidlogs($data) {
        $sql = "SELECT * FROM " . $this->table_name. " INNER JOIN tbluser ON tbluser.us_id = tblpaidlog.us_id ";
        if ($data['keyword']) {
            if ($data['search_field'] == "cellphone") {
                $sql.=" WHERE (cellphone LIKE '%" . $data['keyword'] . "%')";
            } 
        }

        $order = "";
        if ($data['sorting_order'] != "sorting") {
            if ($data['sorting_field'] != "money") {
                $sort = "DESC";
                if ($data['sorting_order'] == "sorting_asc")
                    $sort = "ASC";
                $order = "ORDER BY " . $data['sorting_field'] . " " . $sort;
            }
        }
        $sql=$sql . " " . $order . " LIMIT " . $data['start'] . ", " . $data['perpage'] . " ";
      // echo $sql;
        return $this->db->query($sql)->result_array();
    }



    function countPaidlog($data) {
        $sql = "SELECT COUNT(".$this->id_name.") as total FROM " . $this->table_name." INNER JOIN tbluser ON tbluser.us_id = tblpaidlog.us_id";
        if ($data['keyword']) {
            if ($data['search_field'] == "cellphone") {
                $sql.=" WHERE (cellphone LIKE '%" . $data['keyword'] . "%')";
            } 
        }
        
        $result = $this->db->query($sql)->row_array();
        return $result['total']; 
    }
    function getPaidlog($id)
    {
        $sql="SELECT * FROM ".$this->table_name." WHERE ".$this->id_name."=".$id." LIMIT 1";
        $result = $this->db->query($sql)->row_array();
        return $result;
    }
    function getAllPaidlogs($from)
    {
        // get all paidlogs from the last years until now
        $from = date("Y-m-d 00:00:00",$from); 
        $sql="SELECT * FROM ".$this->table_name." INNER JOIN tbluser ON tbluser.us_id = ".$this->table_name.".us_id WHERE ".$this->table_name.".paid_time > '".$from."'";
        $result = $this->db->query($sql)->result_array();
        foreach ($result as $index=>$paidlog)
        {
            $result[$index]['time']=  strtotime($paidlog['paid_time']);
        }
        return $result;
    }
}