<?php
namespace app\agent\controller;

use think\facade\Session;
use app\agent\BaseController;

use app\common\model\Agent as AgentModel;
use app\common\model\SettleCate as SettleCateModel;
use app\common\model\SettleAgent as SettleAgentModel;

use PHPExcel_IOFactory;
use PHPExcel;

class Settle extends BaseController
{
	
	
	// 结算名单
	public function agentList()
	{
		$cateid = input('cateid/d', 0);
		$catelist = SettleCateModel::order(['priority'=>'desc','id'=>'desc'])->select();
		return view('settle/agentlist',[
			'cateid'		=> $cateid,
			'catelist'		=> $catelist
		]);
	}
	
	public function agentForm()
	{
		$agentid = $this->access_agent['id'];
		$id = input('id/d, 0');
		$agent = SettleAgentModel::where(['agentid'=>$agentid])->findOrEmpty($id);
		$catelist = SettleCateModel::order(['priority'=>'desc','id'=>'desc'])->select();
		return view('settle/agentform',[
			'catelist'			=> $catelist,
			'agent'				=> $agent
		]);
	}
		
	public function listAgent()
	{
		$agentid = $this->access_agent['id'];
		$limit = input('limit/d',20);
		$page = input('page/d',1);
		$map = array();
		$map[] = ['agentid', '=', $agentid];
		$keywords = input('keywords/s');
		if (!empty($keywords)){
			$map[] =['factory|pername', 'like', '%'.$keywords.'%'];
		}
		$cateid = input('cateid/d', 0);
		if (!empty($cateid)){
			$map[] = ['cateid', '=', $cateid];
		}
		$stype = input('stype/d', 0);
		if (!empty($stype)){
			$map[] = ['stype', '=', $stype];
		}
		$list = SettleAgentModel::with(['settleCate'])->where($map)->order('id','DESC')->limit($limit)->page($page)->append(['stype_text'])->select();
		$count = SettleAgentModel::where($map)->count();
		if ($count==0){
			exit(json_encode(array(
				'code' 	=> 1,
				'msg'	=> "未查询到数据"
			)));
		}
		exit(json_encode(array(
			'code' 	=> 0,
			'msg'	=> "",
			'count' => $count,
			'data'  => $list
		)));
	}
	
	public function exportAgent()
	{
		$agentid = $this->access_agent['id'];
		$map = array();
		$map[] = ['agentid', '=', $agentid];
		$keywords = input('keywords/s');
		if (!empty($keywords)){
			$map[] =['factory|realname', 'like', '%'.$keywords.'%'];
		}
		$cateid = input('cateid/d', 0);
		if (!empty($cateid)){
			$map[] = ['cateid', '=', $cateid];
		}
		$stype = input('stype/d', 0);
		if (!empty($stype)){
			$map[] = ['stype', '=', $stype];
		}
		$xlsData = SettleAgentModel::with(['settleCate','agent'])->where($map)->order('id','DESC')->append(['stype_text'])->select()->toArray();
		$xlsCell = array(
			array('id','表ID'),
			array('settle_cate.title','批次号'),
			array('agent.idnumber','代理编号'),
			array('agent.title','代理门店'),
			array('yearmonth', '年月'),
			array('pername', '员工姓名'),
			array('perid', '员工工号'),
			array('permobile', '员工手机号'),
			array('pernumber', '员工身份证号'),
			array('sdate', '入职日期'),
			array('edate', '离职日期'),
			array('factory', '入职企业'),
			
			array('accountname', '收款人姓名'),
			array('accountnumber', '收款人身份证号码'),
			array('accountbank', '收款人银行账号'),
			array('accountstatus', '结算状态'),
			array('remark', '备注'),
			array('accountmoney', '实结金额'),
			array('createtime', '添加时间'),
			array('stype_text', '类型'),
			
			array('onefield1', '小时工-出勤时数'),
			array('onefield2', '小时工-员工单价'),
			array('onefield3', '小时工-代理费基数'),
			array('onefield4', '小时工-结算单价'),
			array('onefield5', '小时工-应结费用'),
			array('onefield6', '小时工-税费'),
			
			array('thrfield1', '管理费-返费政策'),
			array('thrfield2', '管理费-核对情况'),
			array('thrfield3', '管理费-员工薪资')
		);
		export_excel("代理结算",$xlsCell,$xlsData);
	}
	
	
	// 分类
	public function cateList()
	{
		return view('settle/catelist');
	}

	public function listCate()
	{
		$agentid = $this->access_agent['id'];
		$limit = input('limit');
		$page = input('page');
		$cateidarr = SettleAgentModel::where('agentid','=',$agentid)->column('cateid');
		$list = SettleCateModel::withCount(['settleAgent' => function($query) use($agentid) {
					$query->where('agentid',$agentid);
				}])->withSum(['settleAgent' => function($query) use($agentid) {
					$query->where('agentid',$agentid); 
				}],'accountmoney')->whereIn('id', $cateidarr)->order(['priority'=>'desc','id'=>'desc'])->limit($limit)->page($page)->select();
		$count = SettleCateModel::whereIn('id', $cateidarr)->count();
		if ($count==0){
			exit(json_encode(array(
				'code' 	=> 1,
				'msg'	=> "未查询到数据"
			))); 
		}
		exit(json_encode(array(
			'code' 	=> 0,
			'msg'	=> "",
			'count' => $count,
			'data'  => $list
		)));
	}	
	
	
	
	
}