<?php
namespace app\admin\controller;

use think\Controller;
use think\facade\Session;
use think\Request;
use think\Db;
use app\admin\controller\Staff;
class Meter extends Controller
{
// id 
// 支票号  cheque_number
// 缴费员   payment_clerk
// 更新日期 update_time
// 项目编号 proj_number
// 缴费号  payment_number
// 电表数 meters_number
// 缴费次数  payments_umber
// 和移动结算情况 settlement
// 基站类型  base_type
// 是否协议价 is_protocol
// 区域  region 
// 网络标识  network_identity
// 站号   station_number
// 站名  station_name 
// 机房二维码 qrcode
// 缴费周期 payment_cycle
// 预交费日期 advance_pay_time
// 上期表数 last_number
// 本期表数  current_number 
// 实际报移动用电量 report_electric 
// 上期查表日期 last_date
// 本期查表日期 current_date
// 倍率 multiple
// 抄表单单价 unit_price
// 财务单价 finance_unit
// 实际从财务支出金额  finance_pay
// 用电量（1.05元/度） electricone
// 用电量（1.09元/度）electrictwo
// 用电量（1.123元/度） electricthree
// 用电量（协议价） electricity_consumption
// 协议价 agreed_price 
// 移动结算金额 settlement_amount
// 回款次数 back_number

// 毛利 gross_profit
// 电表类型 meter_type

// 电表识别号  meter_number
// 电表问题 meter_problem

// 基站问题 base_problem
// 地址  address
// 所属移动分公司 subordinate
// 出租单位名称 rental_name
// 甲方联系人 contacts
// 实际收款单位 receiving_unit

// 联系方式 contact_information

// 机房位置  room_location 
// 物业位置 property_location

// 备注  remarks
// 申请确认合同额 apply_contract_amount
// 结算日期 settlement_date 
// 确认合同时间 confirmation_time


// 确认收入时间 income_time


// 是否有发票 is_invoice
// 合同编号 contract_number
// 发票类型 invoice_type


// 税点 tax_point

	    //导出
  public function meter_export(){
        $map = Session::get('map');
            if($map){
          	$dianbiao=db::name('dianbiao')->where($map)->where('delete_time',null)->limit(20000)->select();
			}else{
			  $dianbiao=db::name('dianbiao')->where('delete_time',null)->limit(20000)->select();
			}
		  
			if($dianbiao){
                $excel = new Excel;
                $excel->meterexport($dianbiao,'历史抄表资料');
			}else{
				$this->error('导出数据不能为空');
			}
       

    }
    public function index(Request $request){
    	$map=[];
    	$identity = (new Staff)->getIdentity();
    	if($identity ==1 || $identity ==2){
           $orde='id';
    	}else{
           $orde='status';
    	}
    	  $search_text = $request->get('search_text');
			    $search_type = $request->get('search_type'); 
			    if($search_text){
		        switch ($search_type) {
		        	case '1':
		          $this->assign('search_text',$search_text);
		            $map[]=['station_name','like',"%$search_text%"];
		          $this->assign('search_type',$search_type);
		        		break;
		        	case '2':
		          $this->assign('search_text',$search_text);
		            $map[]=['station_number','like',"%$search_text%"];
		          $this->assign('search_type',$search_type);
		        		break;
		
		           case '3':
		          $this->assign('search_text',$search_text);
		           $map[]=['proj_number','like',"%$search_text%"];
		          $this->assign('search_type',$search_type);
		        		break;
		           case '4':
		          $this->assign('search_text',$search_text);
		           $map[]=['contract_number','like',"%$search_text%"];
		          $this->assign('search_type',$search_type);
		        		break;
		           case '5':
		          $this->assign('search_text',$search_text);
		           $map[]=['cheque_number','like',"%$search_text%"];
		          $this->assign('search_type',$search_type);
		        		break;
		          case '6':
		          $this->assign('search_text',$search_text);
		           $map[]=['payment_clerk','like',"%$search_text%"];
		          $this->assign('search_type',$search_type);
		        		break;
		          case '7':
		          $this->assign('search_text',$search_text);
		           $map[]=['tax_point','like',"%$search_text%"];
		          $this->assign('search_type',$search_type);
		        		break;
		          case '8':
		          $this->assign('search_text',$search_text);
		           $map[]=['back_number','like',"%$search_text%"];
		          $this->assign('search_type',$search_type);
		        		break;
		         case '9':
		          $this->assign('search_text',$search_text);
		           $map[]=['meter_number','like',"%$search_text%"];
		          $this->assign('search_type',$search_type);
		        		break;

		        }
			   }else{
			   	  $this->assign('search_type','');
			   	  $this->assign('search_text','');
			   }
			   //和移动结算情况
		      $settlement = $request->get('settlement');
		      if($settlement){
                 switch ($settlement) {
                 	case '1':
		           $map[]=['settlement','=',"已结算"];
		           $this->assign('settlement',$settlement);
                 		break;
                 	case '2':
                   $map[]=['settlement','=',"未结算"];
		           $this->assign('settlement',$settlement);
                 		break;
                 }
		      }else{
		      	 $this->assign('settlement','');
		      }


		         //所属移动分公司
		      $subordinate = $request->get('subordinate');
		      if($subordinate){
                 switch ($subordinate) {
                 	case '1':
		           $map[]=['subordinate','=',"第一分公司"];
		           $this->assign('subordinate',$subordinate);
                 		break;
                 	case '2':
                   $map[]=['subordinate','=',"第二分公司"];
		           $this->assign('subordinate',$subordinate);
                 		break;
                   case '3':
                   $map[]=['subordinate','=',"第三分公司"];
		           $this->assign('subordinate',$subordinate);
                 		break;
                 	   case '4':
                   $map[]=['subordinate','=',"大兴分公司"];
		           $this->assign('subordinate',$subordinate);
                 		break;
                 	   case '5':
                   $map[]=['subordinate','=',"城中铁塔"];
		           $this->assign('subordinate',$subordinate);
                 		break;
                 		   case '6':
                   $map[]=['subordinate','=',"城西铁塔"];
		           $this->assign('subordinate',$subordinate);
                 		break;
                 		   case '7':
                   $map[]=['subordinate','=',"大兴"];
		           $this->assign('subordinate',$subordinate);
                 		break;
                 		   case '8':
                   $map[]=['subordinate','=',"城东铁塔"];
		           $this->assign('subordinate',$subordinate);
                 		break;
                 		   case '9':
                   $map[]=['subordinate','=',"昌平"];
		           $this->assign('subordinate',$subordinate);
                 		break;
                 		   case '10':
                   $map[]=['subordinate','=',"昌平分公司"];
		           $this->assign('subordinate',$subordinate);
                 		break;
                 		   case '11':
                   $map[]=['subordinate','=',"顺义分公司"];
		           $this->assign('subordinate',$subordinate);
                 		break;
                 		   case '12':
                   $map[]=['subordinate','=',"怀柔"];
		           $this->assign('subordinate',$subordinate);
                 		break;
                 		   case '13':
                   $map[]=['subordinate','=',"顺义分公司-广厦"];
		           $this->assign('subordinate',$subordinate);
                 		break;	
                 		   case '14':
                   $map[]=['subordinate','=',"顺义"];
		           $this->assign('subordinate',$subordinate);
                 		break;	
                 }
		      }else{
		      	$this->assign('subordinate','');
		      }

		         //发票类型
		      $invoice_type = $request->get('invoice_type');
		      if($invoice_type){
                 switch ($invoice_type) {
                 	case '1':
		           $map[]=['invoice_type','=',"增值税专用发票"];
		           $this->assign('invoice_type',$invoice_type);
                 		break;
                 	case '2':
                   $map[]=['invoice_type','=',"增值税普通发票"];
		           $this->assign('invoice_type',$invoice_type);
                 		break;
                 	case '3':
                   $map[]=['invoice_type','=',"其它"];
		           $this->assign('invoice_type',$invoice_type);
                 		break;
                 }
		      }else{
		      	 $this->assign('invoice_type','');
		      }
		     // 更新日期
		       $update_time = $request->get('update_time');
		       if($update_time){
				$arr = explode(" - ",$update_time);//转换成数组
				$thismonth_start=$arr[0];
				$thismonth_end=$arr[1];
				$map[]=['update_time','between',[$thismonth_start,$thismonth_end]];
				$this->assign('update_time',$update_time);
		       }else{
		       	$this->assign('update_time','');
		       }
		       // 确收时间
		       $income_time = $request->get('income_time');
		       if($income_time){
				$arr = explode(" - ",$income_time);//转换成数组
				$thismonth_start=$arr[0];
				$thismonth_end=$arr[1];
				$map[]=['income_time','between',[$thismonth_start,$thismonth_end]];
				$this->assign('income_time',$income_time);
		       }else{
		       	$this->assign('income_time','');
		       }
				$page = $request->get('page')?$request->get('page'):1;

				$limit = $request->get('limit')?$request->get('limit'):10;
				if(empty($request->get('page'))){
                   $page_s=0;
				}else{
					$page_s=(($page-1)*$limit);
				}
    
				$dianbiao=db::name('dianbiao')->where($map)->where('delete_time',null)->limit($page_s,$limit)->order($orde.' desc')->select();
				foreach ($dianbiao as $key => $v) {
					if($identity=='admin'){
                       $dianbiao[$key]['identity']=5;
					}else{
					   $dianbiao[$key]['identity']=$identity;
					}
				}
				$dianbiaocnt=db::name('dianbiao')->where($map)->where('delete_time',null)->count();
				if($request->get('page')){
	
				return ['code'=>0,'msg'=>'','count'=>$dianbiaocnt,'data'=>$dianbiao];
				}
				 return $this->fetch('meter_list'); 
    	
    	
    }

   //获取编辑信息数据
    public function show(Request $request){
    	$id = $request->get('id');
    	$dianbiao=db::name('dianbiao')->where("id=$id")->find();
    	$this->assign('data',$dianbiao);
    	return $this->fetch('meter_edit'); 
    }

//      //获取全部编辑信息数据
//     public function all_show_edit(Request $request){
//     	$id = $request->get('id');
//     	$dianbiao=db::name('dianbiao')->where("id=$id")->find();
//     	$this->assign('data',$dianbiao);
//     	return $this->fetch('meter_alledit'); 
//     }

//      public function all_edit(Request $request){
// 		$id = $request->post('id');
// 		$cheque_number= $request->post('cheque_number');//支票号
// 		if($cheque_number){
//           $map['cheque_number']=$cheque_number;
// 		}
// 		// 缴费员   payment_clerk
// 		$payment_clerk= $request->post('payment_clerk');
// 		if($payment_clerk){
//           $map['payment_clerk']=$payment_clerk;
// 		}
// 		// 更新日期 update_time
// 		$update_time= $request->post('update_time');
// 		if($update_time){
//            $map['update_time']=$update_time;
// 		}
// 		// 缴费号  payment_number
// 		$payment_number= $request->post('payment_number');
// 		if($payment_number){
//            $map['payment_number']=$payment_number;
// 		}
// 		// 缴费次数  payments_umber
// 		$payments_umber= $request->post('payments_umber');
// 		if($payments_umber){
//         $map['payments_umber']=$payments_umber;
// 		}
// 		// 和移动结算情况 settlement
// 		$settlement= $request->post('settlement');
// 		if($settlement){
//            $map['settlement']=$settlement;
// 		}
// 		// 发票类型 invoice_type
// 		$invoice_type= $request->post('invoice_type');
// 		if($invoice_type){
//           $map['invoice_type']=$invoice_type;
// 		}
// 		// 税点 tax_point
// 		$tax_point= $request->post('tax_point');
//          if($tax_point){
//           $map['tax_point']=$tax_point;
// 		}
// 		// 移动结算金额 settlement_amount
//        $settlement_amount= $request->post('settlement_amount');
//          if($settlement_amount){
//           $map['settlement_amount']=$settlement_amount;
// 		}
// 		// 财务单价finance_unit
// 		 $finance_unit= $request->post('finance_unit');
//          if($finance_unit){
//           $map['finance_unit']=$finance_unit;
// 		}
// 		// 回款次数 back_number
// 		 $back_number= $request->post('back_number');
//          if($back_number){
//           $map['back_number']=$back_number;
// 		}
// 		//实际从财务支出金额 finance_pay
// 		 $finance_pay= $request->post('finance_pay');
//          if($finance_pay){
//           $map['finance_pay']=$finance_pay;
// 		}
// // 账单备注 bill_remark
// 		$bill_remark= $request->post('bill_remark');
//          if($bill_remark){
//           $map['bill_remark']=$bill_remark;
// 		}
// 		// 电表数 meters_number
// 		$meters_number= $request->post('meters_number');
//          if($meters_number){
//           $map['meters_number']=$meters_number;
// 		}
// 		// 缴费周期 payment_cycle
// 		$payment_cycle= $request->post('payment_cycle');
//          if($payment_cycle){
//           $map['payment_cycle']=$payment_cycle;
// 		}
// 		// 预交费日期 advance_pay_time
// 		$advance_pay_time= $request->post('advance_pay_time');
//          if($advance_pay_time){
//           $map['advance_pay_time']=$advance_pay_time;
// 		}
// 		// 上期表数 last_number
// 		$last_number= $request->post('last_number');
//          if($last_number){
//           $map['last_number']=$last_number;
// 		}
// 		// 本期表数 current_number
// 		$current_number= $request->post('current_number');
//          if($current_number){
//           $map['current_number']=$current_number;
// 		}
// 		// 是否协议价 is_protocol
// 		$is_protocol= $request->post('is_protocol');
//          if($is_protocol){
//           $map['is_protocol']=$is_protocol;
// 		}
// 		// 本期查表日期 current_date
// 		$current_date= $request->post('current_date');
//          if($current_date){
//           $map['current_date']=$current_date;
// 		}
// 		// 上期查表日期 last_date
// 		$last_date= $request->post('last_date');
//          if($last_date){
//           $map['last_date']=$last_date;
// 		}
// 		// 倍率 multiple
// 		$multiple= $request->post('multiple');
//          if($multiple){
//           $map['multiple']=$multiple;
// 		}
// 		// 抄表单单价 unit_price
// 		$unit_price= $request->post('unit_price');
//          if($unit_price){
//           $map['unit_price']=$unit_price;
// 		}
// 		// 用电量（1.05元/度） electricone
// 		$electricone= $request->post('electricone');
//          if($electricone){
//           $map['electricone']=$electricone;
// 		}
// 		// 用电量（1.09元/度） electrictwo
// 		$electrictwo= $request->post('electrictwo');
//          if($electrictwo){
//           $map['electrictwo']=$electrictwo;
// 		}
// 		// 用电量（1.123元/度） electricthree
// 		$electricthree= $request->post('electricthree');
//          if($electricthree){
//           $map['electricthree']=$electricthree;
// 		}
// 		// 用电量（协议价）electricity_consumption
// 		$electricity_consumption= $request->post('electricity_consumption');
//          if($electricity_consumption){
//           $map['electricity_consumption']=$electricity_consumption;
// 		}
// 		// 协议价 agreed_price
// 		$agreed_price= $request->post('agreed_price');
//          if($agreed_price){
//           $map['agreed_price']=$agreed_price;
// 		}
// 		// 实际报移动用电量 report_electric
// 		$report_electric= $request->post('report_electric');
//          if($report_electric){
//           $map['report_electric']=$report_electric;
// 		}
// 		// 毛利 gross_profit
// 		$gross_profit= $request->post('gross_profit');
//          if($gross_profit){
//           $map['gross_profit']=$gross_profit;
// 		}
// 		// 申请确认合同额 apply_contract_amount
// 		$apply_contract_amount= $request->post('apply_contract_amount');
//          if($apply_contract_amount){
//           $map['apply_contract_amount']=$apply_contract_amount;
// 		}
// 		// 确认合同时间 bill_remark
// 		$confirmation_time= $request->post('confirmation_time');
//          if($confirmation_time){
//           $map['confirmation_time']=$confirmation_time;
// 		}
// 		$dianbiao=db::name('dianbiao')->where("id=$id")->update($map);
//          if($dianbiao){
// 	         return  jsonSuc('更新成功');
//           }else{
//           	   return  jsonErr('更新失败,未检测到需要修改的信息');
//           }
//      }
    //财务明细数据
    public function showdetai(Request $request){
    	$id = $request->get('id');
    	$dianbiao=db::name('dianbiao')->where("id=$id")->find();
    	$this->assign('data',$dianbiao);
    	return $this->fetch('meter_detai'); 
    }
    public function edit(Request $request){
		$id = $request->post('id');
		$cheque_number= $request->post('cheque_number');//支票号
		if($cheque_number){
          $map['cheque_number']=$cheque_number;
		}
		// 缴费员   payment_clerk
		$payment_clerk= $request->post('payment_clerk');
		if($payment_clerk){
          $map['payment_clerk']=$payment_clerk;
		}
		// 更新日期 update_time
		$update_time= $request->post('update_time');
		if($update_time){
           $map['update_time']=$update_time;
		}
		// 缴费号  payment_number
		$payment_number= $request->post('payment_number');
		if($payment_number){
           $map['payment_number']=$payment_number;
		}
		// 缴费次数  payments_umber
		$payments_umber= $request->post('payments_umber');
		if($payments_umber){
        $map['payments_umber']=$payments_umber;
		}
		// 和移动结算情况 settlement
		$settlement= $request->post('settlement');
		if($settlement){
           $map['settlement']=$settlement;
		}
		// 发票类型 invoice_type
		$invoice_type= $request->post('invoice_type');
		if($invoice_type){
          $map['invoice_type']=$invoice_type;
		}
		// 税点 tax_point
		$tax_point= $request->post('tax_point');
         if($tax_point){
          $map['tax_point']=$tax_point;
		}
		// 移动结算金额 settlement_amount
       $settlement_amount= $request->post('settlement_amount');
         if($settlement_amount){
          $map['settlement_amount']=$settlement_amount;
		}
		// 财务单价finance_unit
		 $finance_unit= $request->post('finance_unit');
         if($finance_unit){
          $map['finance_unit']=$finance_unit;
		}
		// 回款次数 back_number
		 $back_number= $request->post('back_number');
         if($back_number){
          $map['back_number']=$back_number;
		}
		//实际从财务支出金额 finance_pay
		 $finance_pay= $request->post('finance_pay');
         if($finance_pay){
          $map['finance_pay']=$finance_pay;
		}
// 账单备注 bill_remark
		$bill_remark= $request->post('bill_remark');
         if($bill_remark){
          $map['bill_remark']=$bill_remark;
		}
		$dianbiao=db::name('dianbiao')->where("id=$id")->update($map);
         if($dianbiao){
	         return  jsonSuc('更新成功');
          }else{
          	   return  jsonErr('更新失败,未检测到需要修改的信息');
          }
     }
       public function que(Request $request){
     	$id = $request->post('id');
     	$map['status']=2;
		$dianbiao=db::name('dianbiao')->where("id=$id")->update($map);
		if($dianbiao){
			 return  jsonSuc('确收成功');
		}else{
			 return  jsonErr('确收失败');
		}
     }
//财务确收
     public function queshou(Request $request){
     	$id = $request->post('id');
     	$map['income_time']=date('Y-m-d');
     	$map['status']=1;
		$dianbiao=db::name('dianbiao')->where("id=$id")->update($map);
		if($dianbiao){
			 return  jsonSuc('确收成功');
		}else{
			 return  jsonErr('确收失败');
		}
     }
//财务驳回
       public function bohui(Request $request){
     	$id = $request->post('id');
     	$map['status']=0;
		$dianbiao=db::name('dianbiao')->where("id=$id")->update($map);
		if($dianbiao){
			 return  jsonSuc('驳回成功');
		}else{
			 return  jsonErr('驳回失败');
		}
     }
}