<?php
declare (strict_types = 1);

namespace app\admin\controller;
use app\admin\BaseController;
use think\facade\Db;

class Customer extends BaseController
{
    /**
     * 获取企业客户列表
     * @return \think\Response
     */
    public function list()
    {
        $post = $this->post;
        $condition=[];
        isset($post['company'])&&$post['company']!=""? $condition[]=['companyName|contactor',"like","%{$post['company']}%"] : "";
        isset($post['contactor'])&&$post['contactor']!=""? $condition[]=['contactor',"like","%{$post['contactor']}%"] :
            "";
        $page = isset($post['page'])&& $post['page']!="" ? intval($post['page']) :1;
        $size = isset($post['size'])&& $post['size']!="" ? intval($post['size']) :10;
        $count = Db::name("customer_info")->where($condition)->count();
        $total = ceil($count/$size)>1 ? ceil($count/$size) : 1;
        $page = $page>=$total?intval($total):$page;
        if( isset($post['company'])&&$post['company']!=""){
            $list = Db::name("customer_info")->where($condition)->page($page,$size)->field("companyNo,companyName,parent,area,contactor,depart,mobile,addtime,updatetime,LENGTH(companyName) as weight")
                ->order('weight asc')->select();
        }else{
            $list = Db::name("customer_info")->where($condition)->page($page,$size)->field("companyNo,companyName,parent,area,contactor,depart,mobile,addtime,updatetime")
                ->select();
        }

        $data =[];
        foreach ($list as $key=>$value){
			$total_fee = Db::name("qrd_info")->where([["customerNo","=",$value['companyNo']]])->field("sum(apay_fee) as apay_fee,sum(wpay_fee+pay_fee) as wpay_fee,sum(ainv_fee) as ainv_fee,sum(winv_fee+inv_fee) as winv_fee")
            ->find();
            $value['ainv_fee'] = $total_fee['ainv_fee']??0;
            $value['winv_fee'] = $total_fee['winv_fee']??0;
            $value['total_fee'] = $value['winv_fee']+ $value['ainv_fee'];
            $data[]=$value;
        }
        return app_show(0,"获取成功",['list'=>$data,"count"=>$count]);
    }

    /**
     * 获取客户信息回款开票数据统计信息
     *
     * @return \think\Response
     */
    public function info()
    {
        $post = $this->post;
        $companyNo= isset($post['companyNo'])&&$post['companyNo']!='' ? $post['companyNo'] : "";
        if($companyNo==""){
            return error_show(1003,"参数companyNo 不能为空");
        }
        $info = Db::name("customer_info")->where("companyNo","=",$companyNo)->find();
        if(empty($info)){
            return error_show(1004,"未找到客户数据");
        }
        $order = Db::name("qrd_info")->where([["customerNo","=",$companyNo]])->field("sum(apay_fee) as apay_fee,sum(wpay_fee+pay_fee) as wpay_fee,sum(ainv_fee) as ainv_fee,sum(winv_fee+inv_fee) as winv_fee")
            ->find();
        array_walk($order,function (&$value){
           if(is_null($value)){
                $value=0;
           }
        });
        return app_show(0,"获取成功",$order);
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
    }

    /**
     * 获取客户企业信息
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read()
    {
        $post = $this->post;
        $companyNo= isset($post['companyNo'])&&$post['companyNo']!='' ? $post['companyNo'] : "";
        if($companyNo==""){
            return error_show(1003,"参数companyNo 不能为空");
        }
        $info = Db::name("customer_info")->where("companyNo","=",$companyNo)->find();
        if(empty($info)){
            return error_show(1004,"未找到客户数据");
        }
        if($info['invoice_code']!=""){
            $list = Db::name("customer_invoice")->where([["invoice_code","=",$info['invoice_code']],["is_del","=",0]])
            ->field("id,invoice_title,invoice_people,invoice_addr,invoice_mobile,invoice_code,invoice_bank,invoice_bankNo")->find();
            if(!empty($list)){
                $info['invoice_title'] = $list['invoice_title'];
                $info['invoice_people'] = $list['invoice_people'];
                $info['invoice_addr'] = $list['invoice_addr'];
                $info['invoice_mobile'] = $list['invoice_mobile'];
                $info['invoice_code'] = $list['invoice_code'];
                $info['invoice_bank'] = $list['invoice_bank'];
                $info['invoice_bankNo'] = $list['invoice_bankNo'];
            }
        }
        return app_show(0,"获取成功",$info);
    }

    /**
     * 获取客户企业发票抬头.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function invoice()
    {
        $post = $this->post;
        $companyNo= isset($post['name'])&&$post['name']!='' ? $post['name'] : "";
        if($companyNo==""){
            return error_show(1003,"参数name 不能为空");
        }

        $condition = [['invoice_title','like',"%{$companyNo}%"],['is_del',"=",0]];
         $company_type= isset($post['company_type'])&&$post['company_type']!='' ? $post['company_type'] : "";
        if($company_type!=""){
          $condition[]=["company_type","=",$company_type];
        }
        $list = Db::name("customer_invoice")->where($condition)->page(1,100)->field("id,invoice_title,invoice_people,invoice_addr,invoice_mobile,invoice_code,invoice_bank,invoice_bankNo,LENGTH(invoice_title) as weight")
            ->order('weight asc')->select();
        if(empty($list)){
            return error_show(1004,"未找到企业财务信息数据");
        }
        return app_show(0,"获取成功",$list);
    }
    //客户开票信息列表翻译
  public  function  invoicelist(){
        $page = isset($this->post['page'])&&$this->post['page']!='' ?intval($this->post['page']):1 ;
        $size = isset($this->post['size'])&&$this->post['size']!='' ?intval($this->post['size']):15 ;
        $where =[['is_del',"=",0]];
        $title = isset($this->post['invoice_title'])&&$this->post['invoice_title']!=''?trim($this->post['invoice_title']) :"";
        if($title!=''){
            $where[]=["invoice_title","like","%$title"];
        }
        $code = isset($this->post['invoice_code'])&&$this->post['invoice_code']!=''?trim($this->post['invoice_code']):"";
        if($code!=''){
            $where[]=["invoice_code","like","%$code"];
        }
        $count=Db::name("customer_invoice")->where($where)->count();
        $total =ceil($count/$size);
        $page= $page>=$total ? intval($total) :$page;
        $list = Db::name("customer_invoice")->where($where)->page($page,$size)->order("id desc")->select();
        return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
  }

   //客户开票信息列表翻译
  public  function  invoiceAdd(){

        $title = isset($this->post['invoice_title'])&&$this->post['invoice_title']!=''?trim($this->post['invoice_title']) :"";
        if($title==''){
            return error_show(1004,"参数 invoice_title 不能为空");
        }
        $code = isset($this->post['invoice_code'])&&$this->post['invoice_code']!=''?trim($this->post['invoice_code']):"";
        if($code==''){
           return error_show(1004,"参数 invoice_code 不能为空");
        }
        $people = isset($this->post['invoice_people'])&&$this->post['invoice_people']!=''?trim($this->post['invoice_people']):"";
//        if($people==''){
//           return error_show(1004,"参数 invoice_people 不能为空");
//        }
        $invoice_addr = isset($this->post['invoice_addr'])&&$this->post['invoice_addr']!=''?trim($this->post['invoice_addr']):"";
        if($invoice_addr==''){
           return error_show(1004,"参数 invoice_addr 不能为空");
        }
        $invoice_mobile = isset($this->post['invoice_mobile'])&&$this->post['invoice_mobile']!=''?trim($this->post['invoice_mobile']):"";
        if($invoice_mobile==''){
           return error_show(1004,"参数 invoice_mobile 不能为空");
        }
        $invoice_bank = isset($this->post['invoice_bank'])&&$this->post['invoice_bank']!=''?trim($this->post['invoice_bank']):"";
        if($invoice_bank==''){
           return error_show(1004,"参数 invoice_bank 不能为空");
        }
        $invoice_bankNo = isset($this->post['invoice_bankNo'])&&$this->post['invoice_bankNo']!=''?trim($this->post['invoice_bankNo']):"";
        if($invoice_bankNo==''){
           return error_show(1004,"参数 invoice_bankNo 不能为空");
        }

        $company_type = isset($this->post['company_type'])&&$this->post['company_type']!=''?trim($this->post['company_type']):"01";
        $isTr =Db::name("customer_invoice")->where(["invoice_code"=>$code,"is_del"=>0])->find();
        if($isTr){
             return error_show(1004,"开票信息已存在");
        }

        $data=[
            "invoice_title"=>$title,
            "invoice_code"=>$code,
            "invoice_people"=>$people,
            "invoice_addr"=>$invoice_addr,
            "invoice_mobile"=>$invoice_mobile,
            "invoice_bank"=>$invoice_bank,
            "invoice_bankNo"=>$invoice_bankNo,
            "company_type"=>$company_type,
            "apply_id"=>$this->uid,
            "apply_name"=>$this->uname,
            "addtime"=>date("Y-m-d H:i:s"),
            "updatetime"=>date("Y-m-d H:i:s")
        ];
        $ind =Db::name("customer_invoice")->insertGetId($data);
        if($ind) return app_show(0,"新建成功",["id"=>$ind]); else return error_show(1004,"新建失败");
  }
    //编辑开票信息
  public function invoiceSave(){
        $id =isset($this->post['id'])&&$this->post['id']!='' ? intval($this->post['id']):"";
        if($id==''){
            return error_show(1004,"参数 id 不能为空");
        }
         $isTr =Db::name("customer_invoice")->where(["id"=>$id,"is_del"=>0])->find();
        if($isTr==false){
             return error_show(1004,"开票信息不存在");
        }
         $title = isset($this->post['invoice_title'])&&$this->post['invoice_title']!=''?trim($this->post['invoice_title']) :"";
        if($title==''){
            return error_show(1004,"参数 invoice_title 不能为空");
        }
        $code = isset($this->post['invoice_code'])&&$this->post['invoice_code']!=''?trim($this->post['invoice_code']):"";
        if($code==''){
           return error_show(1004,"参数 invoice_code 不能为空");
        }
         $isTrs =Db::name("customer_invoice")->where([["invoice_code","=",$code,"is_del","=",0],["id","<>",$id]])
         ->find();
        if($isTrs){
             return error_show(1004,"开票信息已存在");
        }
        $people = isset($this->post['invoice_people'])&&$this->post['invoice_people']!=''?trim($this->post['invoice_people']):"";
        if($people==''){
           return error_show(1004,"参数 invoice_people 不能为空");
        }
        $invoice_addr = isset($this->post['invoice_addr'])&&$this->post['invoice_addr']!=''?trim($this->post['invoice_addr']):"";
        if($invoice_addr==''){
           return error_show(1004,"参数 invoice_addr 不能为空");
        }
        $invoice_mobile = isset($this->post['invoice_mobile'])&&$this->post['invoice_mobile']!=''?trim($this->post['invoice_mobile']):"";
        if($invoice_mobile==''){
           return error_show(1004,"参数 invoice_mobile 不能为空");
        }
        $invoice_bank = isset($this->post['invoice_bank'])&&$this->post['invoice_bank']!=''?trim($this->post['invoice_bank']):"";
        if($invoice_bank==''){
           return error_show(1004,"参数 invoice_bank 不能为空");
        }
        $invoice_bankNo = isset($this->post['invoice_bankNo'])&&$this->post['invoice_bankNo']!=''?trim($this->post['invoice_bankNo']):"";
        if($invoice_bankNo==''){
           return error_show(1004,"参数 invoice_bankNo 不能为空");
        }
        $company_type = isset($this->post['company_type'])&&$this->post['company_type']!=''?trim($this->post['company_type']):"01";
          $data=[
            "invoice_title"=>$title,
            "invoice_code"=>$code,
            "invoice_people"=>$people,
            "invoice_addr"=>$invoice_addr,
            "invoice_mobile"=>$invoice_mobile,
            "invoice_bank"=>$invoice_bank,
            "invoice_bankNo"=>$invoice_bankNo,
            "company_type"=>$company_type,
            "updatetime"=>date("Y-m-d H:i:s")
        ];
        $ind =Db::name("customer_invoice")->where($isTr)->update($data);
        if($ind) return app_show(0,"更新成功"); else return error_show(1004,"更新失败");
  }
    //开票信息删除
  public function  invoiceDel(){
        $id =isset($this->post['id'])&&$this->post['id']!='' ? intval($this->post['id']):"";
        if($id==''){
            return error_show(1004,"参数 id 不能为空");
        }
         $isTr =Db::name("customer_invoice")->where(["id"=>$id,"is_del"=>0])->find();
        if($isTr==false){
             return error_show(1004,"开票信息不存在");
        }

        $ind =Db::name("customer_invoice")->where($isTr)->update(["is_del"=>1,"updatetime"=>date("Y-m-d H:i:s")]);
        if($ind) return app_show(0,"删除成功"); else return error_show(1004,"删除失败");
  }
}