<?php
namespace app\admin\controller;

use think\facade\Session;
use app\admin\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();
		$agentlist = AgentModel::order(['id'=>'desc'])->select();
		return view('settle/agentlist',[
			'cateid'		=> $cateid,
			'catelist'		=> $catelist,
			'agentlist'		=> $agentlist
		]);
	}
	
	public function agentForm()
	{
		$id = input('id/d, 0');
		$agent = SettleAgentModel::findOrEmpty($id);
		$catelist = SettleCateModel::order(['priority'=>'desc','id'=>'desc'])->select();
		$agentlist = AgentModel::order(['id'=>'desc'])->select();
		return view('settle/agentform',[
			'catelist'			=> $catelist,
			'agentlist'			=> $agentlist,
			'agent'				=> $agent
		]);
	}
	
	public function agentImport()
	{
		$catetitle = date("YmdHis");
		return view('settle/agentimport',[
			'catetitle'		=> $catetitle
		]);
	}
	
	public function importAgent()
	{
		$execlfile = input('execlfile/s', "");
		if (empty($execlfile)){
			exit(json_encode(array(
				'code' 	=> 1,
				'msg'	=> "请上传导入文件。"
			)));
		}	
		$filedata = read_excel( $execlfile );
		$agentlist = AgentModel::column('idnumber,id,title','idnumber');
		$catetitle = input('catetitle/s', "");
		$cate = SettleCateModel::where(['title'=>$catetitle])->findOrEmpty();
		if ($cate->isEmpty()){
			$cate = new SettleCateModel;
			$cate->save([
				'title' 	=> $catetitle,
				'status'    => 1,
				'remark'	=> "",
				'priority'  => 0
			]);
		}
		$cateid = $cate->id;
		$stype = input('stype/d', 1);
		$createtime = input('createtime/s', "");
		$sagent = new SettleAgentModel;
		$data = array();
		$sucnumber = 0;
		$errnumber = 0;
		foreach( $filedata as $k=>$v ){ 
			$idnumber = trim($v[0]);
			if (empty($idnumber) || empty($agentlist[$idnumber])){
				$errnumber++;
				continue;
			}else{
				$data[] = array(
					'cateid'		=> $cateid,
					'agentid'		=> $agentlist[$idnumber]['id'],
					'yearmonth'		=> $v[1],
					'pername'		=> $v[2],
					'perid'			=> $v[3],
					'permobile'		=> $v[4],
					'pernumber'		=> $v[5],
					'sdate'			=> $v[6],
					'edate'			=> $v[7],
					'factory'		=> $v[8],
					'accountname'	=> $v[9],
					'accountnumber'	=> $v[10],
					'accountbank'	=> $v[11],
					'accountstatus'	=> $v[12],
					'remark'		=> $v[13],
					'accountmoney'	=> floatval($v[14]),
					'createtime'	=> $createtime,
					'stype'			=> $stype,
					'onefield1'		=> $stype==1 ? $v[15] : "",
					'onefield2'		=> $stype==1 ? $v[16] : "",
					'onefield3'		=> $stype==1 ? $v[17] : "",
					'onefield4'		=> $stype==1 ? $v[18] : "",
					'onefield5'		=> $stype==1 ? $v[19] : "",
					'onefield6'		=> $stype==1 ? $v[20] : "",
					
					'thrfield1'		=> $stype==3 ? $v[15] : "",
					'thrfield2'		=> $stype==3 ? $v[16] : "",
					'thrfield3'		=> $stype==3 ? $v[17] : ""
				);
				$sucnumber++;
			}
		}
		$sagent->saveAll($data);
		exit(json_encode(array(
			'code' 	=> 0,
			'sucnumber'	=> $sucnumber,
			'errnumber'	=> $errnumber
		)));
	}
	
	public function editAgent()
	{
		$id = input('id/d');
		$agent = SettleAgentModel::findOrEmpty($id);
		$stype = input('stype/d', 1);
		$data = array(
			'cateid'		=> input('cateid/d', 0),
			'agentid'		=> input('agentid/d', 0),
			'yearmonth'		=> input('yearmonth/s', ""),
			'pername'		=> input('pername/s', ""),
			'perid'			=> input('perid/s', ""),
			'permobile'		=> input('permobile/s', ""),
			'pernumber'		=> input('pernumber/s', ""),
			'sdate'			=> input('sdate/s', ""),
			'edate'			=> input('edate/s', ""),
			'factory'		=> input('factory/s', ""),
			
			'accountname'	=> input('accountname/s', ""),
			'accountnumber'	=> input('accountnumber/s', ""),
			'accountbank'	=> input('accountbank/s', ""),
			'accountstatus'	=> input('accountstatus/s', ""),
			'remark'		=> input('remark/s', ""),
			'accountmoney'	=> floatval(input('accountmoney/f', 0.00)),
			'createtime'	=> input('createtime/s', ""),
			'stype'			=> $stype,
			
			'onefield1'		=> $stype==1 ? input('onefield1/s', "") : "",
			'onefield2'		=> $stype==1 ? input('onefield2/s', "") : "",
			'onefield3'		=> $stype==1 ? input('onefield3/s', "") : "",
			'onefield4'		=> $stype==1 ? input('onefield4/s', "") : "",
			'onefield5'		=> $stype==1 ? input('onefield5/s', "") : "",
			'onefield6'		=> $stype==1 ? input('onefield6/s', "") : "",
			
			'thrfield1'		=> $stype==3 ? input('thrfield1/s', "") : "",
			'thrfield2'		=> $stype==3 ? input('thrfield2/s', "") : "",
			'thrfield3'		=> $stype==3 ? input('thrfield3/s', "") : ""
		);
		if ($agent->isEmpty()){
			$agent = new SettleAgentModel;
			$agent->save($data);
		}else{
			$agent->save($data);
		}
		exit(json_encode(array(
			'code' 	=> 0
		)));
	}
	
	public function fieldAgent()
	{
		$id = input('id/d',0); 
		$info = SettleAgentModel::findOrEmpty($id);
		if ($info->isEmpty()){
			exit(json_encode(array(
				'code' 	=> 1,
				'msg'	=> "信息不存在"
			)));
		}else{
			$info->save([
				input('field/s')  => input('value')
			]);
		}
		exit(json_encode(array(
			'code' 	=> 0
		)));
	}
	
	public function delAgent()
	{
		$idarr = input('idarr/a');
		$info = SettleAgentModel::whereIn('id',$idarr)->select();
		$result = $info->delete();
		if ($result){
			exit(json_encode(array(
				'code' 	=> 0,
				'msg'	=> ""
			)));
		}
		exit(json_encode(array(
			'code' 	=> 1,
			'msg'	=> "删除失败，请稍后重试"
		)));
	}
	
	public function listAgent()
	{
		$limit = input('limit/d',20);
		$page = input('page/d',1);
		$map = array();
		$keywords = input('keywords/s');
		if (!empty($keywords)){
			$map[] =['factory|pername', 'like', '%'.$keywords.'%'];
		}
		$cateid = input('cateid/d', 0);
		if (!empty($cateid)){
			$map[] = ['cateid', '=', $cateid];
		}
		$agentid = input('agentid/d', 0);
		if (!empty($agentid)){
			$map[] = ['agentid', '=', $agentid];
		}
		$stype = input('stype/d', 0);
		if (!empty($stype)){
			$map[] = ['stype', '=', $stype];
		}
		$list = SettleAgentModel::with(['settleCate','agent'])->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()
	{
		$map = array();
		$keywords = input('keywords/s');
		if (!empty($keywords)){
			$map[] =['factory|pername', 'like', '%'.$keywords.'%'];
		}
		$cateid = input('cateid/d', 0);
		if (!empty($cateid)){
			$map[] = ['cateid', '=', $cateid];
		}
		$agentid = input('agentid/d', 0);
		if (!empty($agentid)){
			$map[] = ['agentid', '=', $agentid];
		}
		$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', '类型'),
			
			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 cateForm()
	{
		$id = input('id/d, 0');
		$cate = SettleCateModel::findOrEmpty($id);
		return view('settle/cateform',[
			'cate'	=> $cate
		]);
	}
	
	public function listCate()
	{
		$limit = input('limit');
		$page = input('page');
		$list = SettleCateModel::withCount('settleAgent')->withSum('settleAgent','accountmoney')->order(['priority'=>'desc','id'=>'desc'])->limit($limit)->page($page)->append(['status_text'])->select();
		$count = SettleCateModel::count();
		if ($count==0){
			exit(json_encode(array(
				'code' 	=> 1,
				'msg'	=> "未查询到数据"
			))); 
		}
		exit(json_encode(array(
			'code' 	=> 0,
			'msg'	=> "",
			'count' => $count,
			'data'  => $list
		)));
	}	
	
	public function fieldCate()
	{
		$id = input('id/d');
		$cate = SettleCateModel::find($id);
		if ($cate==null){
			exit(json_encode(array(
				'code' 	=> 1,
				'msg'	=> "批次信息不存在"
			)));
		}else{
			$cate->save([
				input('field/s')  => input('value/s', "")
			]);
		}
		exit(json_encode(array(
			'code' 	=> 0
		)));
	}
	
	public function editCate()
	{
		$id = input('id/d');
		if (empty($id)){
			$cate = SettleCateModel::create([
				'title' 	=> input('title/s', ""),
				'status'    => input('status/d', 0)==1 ? 1 : 2,
				'remark'	=> input('remark/s', ""),
				'priority'  => input('priority/d', 0)
			]);
		}else{
			$cate = SettleCateModel::find($id);
			$cate->save([
				'title' 	=> input('title/s', ""),
				'status'    => input('status/d', 0)==1 ? 1 : 2,
				'remark'	=> input('remark/s', ""),
				'priority'  => input('priority/d', 0)
			]);
		}
		exit(json_encode(array(
			'code' 	=> 0
		)));
	}
	
	public function delCate()
	{
		$id = input('id/d');
		$cate = SettleCateModel::where('id',$id)->select();
		$result = $cate->delete();
		if ($result){
			exit(json_encode(array(
				'code' 	=> 0,
				'msg'	=> ""
			)));
		}
		exit(json_encode(array(
			'code' 	=> 1,
			'msg'	=> "删除失败，请稍后重试"
		)));
	}
	
	
	
}