<?php
/**
*   此文件是流程模块【contract_list.合同管理】对应控制器接口文件。
*/
class mode_contract_listClassAction extends inputAction{

    /**
    *   重写函数：保存前处理，主要用于判断是否可以保存
    *   $table String 对应表名
    *   $arr Array 表单参数
    *   $id Int 对应表上记录Id 0添加时，大于0修改时
    *   $addbo Boolean 是否添加时
    *   return array('msg'=>'错误提示内容','rows'=> array()) 可返回空字符串，或者数组 rows 是可同时保存到数据库上数组
    */
    protected function savebefore($table, $arr, $id, $addbo){

        if($addbo){
            //保存员工姓名
            $arr['staffname'] = $arr['staff'];

            //保存业务线
            $arr['businame'] = $arr['busi_line'];

            //保存创建的业务部门
            $arr['deptname'] = $arr['departmentname'];
        }


        return ['rows'=>$arr];
    }

    /**
    *   重写函数：保存后处理，主要保存其他表数据
    *   $table String 对应表名
    *   $arr Array 表单参数
    *   $id Int 对应表上记录Id
    *   $addbo Boolean 是否添加时
    */
    protected function saveafter($table, $arr, $id, $addbo){

    }

    public function getbankcodeAjax(){
        $numb = $this->post('numb');

        //甲方收款
        $model = 'supplier';
        $sql = ' number = "'.$numb .'"';
        
        $cdata = m($model)->getone($sql,'id,name');

        if(empty($cdata)){
            $model = 'customer';
            $sql = ' contract_number = "'.$numb .'"';

            $cdata = m($model)->getone($sql,'id,name');
        }
        if($cdata){
            $sql = "select CONCAT('开户银行: ',`open_bank_name` ,' -- 银行账号: ',`bank_code` ) as `name`,`bank_code` as `value` from [Q]".$model."_bank_info where mid = ".$cdata['id'];
             $data = $this->db->getall($sql);
        }else{
           $data=[];
        }
        
       
        $this->returnjson($data);
    }

    public function getbankcodebymidAjax(){
        $mid = $this->post('mid');

        $c = m('c_contract')->getone('id = '.$mid,'id,bankcode,parta_name,partb_name,partc_name,receive');
        $ret = [];
        $num = '';
        if($c){
            $ret['bank'] = $c['bankcode'];
            if($c['reveice'] == 1){
                $num = $c['parta_name'];
            } elseif($c['reveice'] == 2) {
                $num = $c['partb_name'];
            }else{
                 $num = $c['partc_name'];
            }
            $s =  m('supplier')->getone('number = "'.$num.'"','id');
            if($s){
                $sql = "select CONCAT('开户银行: ',`open_bank_name` ,' -- 银行账号: ',`bank_code` ) as `name`,`bank_code` as `value` from [Q]supplier_bank_info where mid = ".$s['id'];
                $ret['data'] = $this->db->getall($sql);
            }
        }

        $this->returnjson($ret);
    }


    //模拟客户接口
    public function getcustomerAjax(){
        $name = $this->post('cname');
        $data = m('option')->getone('`name` like "'.$name.'%"','id,name');
        $this->returnjson($data);
    }

    //格式化子表数据
    public function savesubbefore($data,$tables)
    {
        if($tables == 'c_rhythm'){
            if (count($data) >= 1) {
                foreach ($data as $key => $arr) {
                    if ($arr['amount'] != '') {
                        $data[$key]['cycle_numb'] = $key + 1;
                    }
                }
            }
        }
        return $data;
    }

    public function gettypesAjax(){
        $pid = $this->post('pid');
        if($pid == 0)
            $data = m('option')->getall(' pid in (676,955)','num as value,name');
        else
            $data = m('option')->getall(' pid =  "'.$pid.'"','num as value,name');
        $this->returnjson($data);
    }

    public function getcontractinfoAjax(){
        $id = $this->post('id');
        $data = m('c_contract')->getone(' id = '.$id);
        $this->returnjson($data);
    }


    public function getparta(){
        $receive = $this->get('receive');
        
        
            //如果financetype值等于1说明是成本合同   甲方是客户  乙方 第三方 都是供应商  
            //如果financetype值等于2说明是收入合同   甲方 、第三方 都是供应商  乙方是客户 
        // if($receive == 1){  //如果甲方收款  供应商
        //     $data = m('common')->getsupplier();
        // } elseif($receive == 2)  { //乙方收款
        //     $data = m('common')->getcustomer();
        // } else{
            $customer = m('common')->getcontract_customer();
            $supplier = m('common')->getcontract_supplier();
            $data=array_merge($customer,$supplier);
           
        // }
       
        $this->returnjson($data);
    }

    public function getpartb(){
        $receive = $this->get('receive');
        $data = [];
        // if($receive == 1){  //如果甲方收款
        //     $data = m('common')->getcustomer();
        // } elseif($receive == 2)  { //乙方收款  乙方就是供应商
        //     $data = m('common')->getsupplier();
        // } else{
            $customer = m('common')->getcontract_customer();
            $supplier = m('common')->getcontract_supplier(); 
            $data=array_merge($customer,$supplier);
        // }
        $this->returnjson($data);
    }

    public function getpartc(){
        // $receive = $this->get('receive');
        // $data=[];
        // if($receive == 3){
        $customer = m('common')->getcontract_customer();
        $supplier = m('common')->getcontract_supplier(); 
        
        $data=array_merge($customer,$supplier);
        // }
       
        $this->returnjson($data);
    }
}   
            