<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Ticketno_model extends MY_Model{
	function __construct(){
		parent::__construct();
		
		$this->table = "TICKETNO";
		$this->pk = "RecordSet";
		$this->record_sql_action = FALSE;
		$this->created_by = "";
		$this->date_created = "";
		$this->modified_by = "";
		$this->date_modified = "";
		
//		$this->join .= "LEFT JOIN WEB_GROUP ON WEB_GROUP.PKKEY = <T>.USER_GROUP ";
	}
	
	function get_data($params=array()){
		if(! isset($params['select']) || $params['select'] == "") $params['select'] = "<T>.*";
		$sql = $this->sql_select($params);
		$query = $this->db->query($sql);
		return $query->result();
	}
	
	function data_count($params=array()){
		$params['select'] = "COUNT(1) as number_of_rows ";
		$params['sort'] = " ";
		$sql = $this->sql_select($params);
		$query = $this->db->query($sql);
		$data_array = $query->result();
		
		return count($data_array) > 0? $data_array[0]->number_of_rows:0;
	}
	
	function get_next_no($record_set=""){
	//function get_next_no($record_set="", $year="", $month="", $day=""){
		$this->load->model("invoice_model");
		
		$SN = "";
		$current_year = date("y")."-";
		
		if($record_set != ""){
			$condition = "<T>.RecordSet = '".$record_set."'";
			// if($year != "") $condition = $this->fm->append($condition, "<T>.YEAR = '".$year."'");
			// if($month != "") $condition = $this->fm->append($condition, "<T>.MONTH = '".$month."'");
			// if($day != "") $condition = $this->fm->append($condition, "<T>.DAY = '".$day."'");
			$ticket_no_array = $this->get_data(array(
				'condition' => $condition,
			));
			
			if(count($ticket_no_array) > 0){
				$ticket_no = $ticket_no_array[0];
				$next_no = (int)$ticket_no->NextNo;
				if($next_no < 1) $next_no = 1;
				
				//Update Next No---
				unset($data);
				$data['NextNo'] = ($next_no + 1);
				$this->sql_update($data, $ticket_no->RecordSet);
				//-----------------
				
				//Build SN---
				$SN = $ticket_no->Prefix;
				// if($year != "") $SN .= $year;
				// if($month != "") $SN .= $month;
				// if($day != "") $SN .= $day;
				
				$number_length = $ticket_no->Len - strlen($SN);
				
				for($x=strlen($next_no); $x<$number_length; $x++){
					$SN .= "0";
				}
				
				$SN .= $next_no;
				//-----------
				
				//Check existing---
				switch($record_set){
					case "REFNO":
						$existing_data = $this->invoice_model->get_data(array(
							'condition' => "<T>.REFNO = '".$SN."'",
						));
						if(count($existing_data) > 0) $SN = $this->get_next_no($record_set);
						//if(count($existing_data) > 0) $SN = $this->get_next_no($record_set, $year, $month, $day);
						break;
				}
				//-----------------
			}
			else{
				$data['RECORDSET'] = $record_set;
				// if($year != "") $data['YEAR'] = $year;
				// if($month != "") $data['MONTH'] = $month;
				// if($day != "") $data['DAY'] = $day;
				
				switch($record_set){
					case "CASE":
						$data['Prefix'] = "";
						$data['Len'] = 5;
						break;
				}
				
				$this->sql_insert($data);
				
				//return $this->get_next_no($record_set, $year, $month, $day);
				return $this->get_next_no($record_set);
			}
		}
		
		return $current_year.$SN;
	}

}