<?php
 /**
 *      [QKSHOP] (C) QIKEWEB Inc.
 *      This is NOT a freeware, use is subject to license terms
 */

class FinanceModel extends BaseModel{
	public $errString="";
	public $msg='';
	function getAllTypes(){
		$sql = "select * from qk_codemap where parentId=16 and iStatus=1";
		return $this->db->getAll($sql);
	}
	function del($id){
		$sql = "delete from qk_finance where id='$id' limit 1";
		return $this->db->query($sql);
	}
	function getInfo($id){
		$sql = "select * from `qk_finance` where id='$id'";
		$info = $this->db->getRow($sql);
		if(!$info){return false;}
		if($info['userId']){ $info['userInfo'] = $this->db->getRow("select * from `qk_user` where id='".$info['userId']."'"); }
		return $info;
	}
	function add($param){$param['act']='add';return $this->query($param);}
	function edit($param){$param['act']='edit';return $this->query($param);}
	function query($param){
		$act = $param['act'];
		$id = $param['id'];
		$loginName = $param['loginName'];
		$userId = $param['userId'];
		$amount = $param['amount'];
		$typeId = $param['typeId'];
		$about = $param['about'];
		$remark = $param['remark'];
		$orderId = $param['orderId'];
		$iStatus = $param['iStatus'];
		$effectiveTime = $param['effectiveTime'];
		if($amount==0){$this->msg='金额不可为0';return false;}
		if(!$effectiveTime || ($effectiveTime=strtotime($effectiveTime))==false){$this->msg='生效时间格式不正确';return false;}
		$outTradeNo = $param['outTradeNo'];
		if($act=="add"){
			$sql = "insert into `qk_finance` set ";
			$sqlEnd = ",posttime=UNIX_TIMESTAMP()";
		}else if($act=="edit" && $id && settype($id,"integer")){
			$sql = "update `qk_finance` set ";
			$sqlEnd = " where id='$id'";

		}
		load_class("user");
		if($userId==""){
			$userId = UserModel::getId($loginName);
		}
		if(!$userId){$this->msg='未找到用户';return false;}
		$sql .= " userId='$userId',amount='$amount',typeId='$typeId',about='$about',remark='$remark',iStatus='$iStatus'";
		$sql .= ",effectiveTime='$effectiveTime',outTradeNo='$outTradeNo'";
		$sql .= $sqlEnd;
		if($this->db->query($sql)){return true;}
		return false;
	}
	function ajaxColEdit($param){
				$col = $param['col'];
				$val = $param['val'];
				$id = $param['id'];
				if($col && settype($id,"integer")){
					$wheres='';
					if($col=='iStatus'){
						$wheres=',effectiveTime=UNIX_TIMESTAMP()';
					}
					$sql = "update `qk_finance` set `$col`='$val'$wheres where id='".$id."'";
					if($this->db->query($sql)){
						return true;
					}
				}else{
					$this->msg = '参数不完整';
				}
		return false;
	}
}
?>
