<?php
/**
 * 客户接口
 * Class opencatClassAction
 */
class opencustomerClassAction extends openapiAction
{
    public function __call($name, $arguments)
    {
        $msg = $name.' action not found';
        return $this->showreturn([],$msg,'201');
    }


    //客户列表
    public function listAction(){
        $post = $_POST;
        $page = $post['size'] ?  $post['size']:0;
        $page_size = $post['page_size']? $post['page_size']:10;
        $sort =$post['sort']? $post['sort']: 'desc';
        $sql  = 'select id,contract_number,name from [Q]customer as c  order by c.id '.$sort.' limit '.$page.','.$page_size;
        $data = $this->db->getall($sql);
        foreach ($data as $key => $vo){
            $supp ='select open_bank_name,bank_code,bank_holder,open_address from [Q]customer_bank_info   where mid='.$vo['id'];
            $contacts ='select name,job,tel,email,adress,remarks from [Q]contracts  where mid='.$vo['id'];
            $supp = $this->db->getall($supp);
            $contacts = $this->db->getall($contacts);
            $data[$key]['bank_info']=$supp;
            $data[$key]['contacts_info']=$contacts;
        }
        $data = $this->flowrsreplace($data);
        return $this->showreturn($data);
    }

    public function flowrsreplace($data = []){
        foreach ($data as &$rs){
            if(is_array($rs)){
                foreach ($rs as $k => &$v) {
                    switch ($k) {
                        case 'supplier_group':
                            $v = $this->getOptionType($v);
                            break;
                        case 'company_classify':
                            $v = $this->getOptionType($v);
                            break;
                        case 'company_nature':
                            $v = $this->getOptionType($v);
                            break;
                        case 'company_scale':
                            $v = $this->getOptionType($v);
                            break;
                        case 'correspond_org_id':
                            $v = $this->getOptionType($v);
                            break;
                        case 'pay_currency':
                            $v = $this->getOptionType($v);
                            break;
                        case 'settle_type':
                            $v = $this->getOptionType($v);
                            break;
                        case 'pay_condition':
                            $v = $this->getOptionType($v);
                            break;
                         case 'tax_type':
                            $v = $this->getOptionType($v);
                            break;
                         case 'tend_type':
                            $v = $this->getOptionType($v);
                            break;
                         case 'tax_rate':
                            $v = $this->getOptionType($v);
                            break;
                         case 'bank_currency':
                            $v = $this->getOptionType($v);
                            break;
                        case 'trade':
                            $v = $this->getOptionType($v);
                            break;
                
                    }
                }
            }
        }
        return $data;
    }

 


    private function getOptionType($typeId)
    {
        $data = $this->db->getone('xinhu_option', 'id = '.$typeId);
        return $data['name'];
    }

    private function getDeptName($deptId){
        $data = $this->db->getone('xinhu_dept', 'id = '.$deptId);
        return $data['name'];
    }

    private function getStaffName($staffId){
        $data = $this->db->getone('xinhu_userinfo', 'id = '.$staffId);
        return $data['name'];
    }

    private function formatDate($date){
        $time =  strtotime($date);
        return date('Y-m-d',$time);
    }

    /**
     * 子表数据替换
     * @param $r
     * @param int $lx   =0编辑,1显示
     * @return mixed
     */
    public function flowsubdata($r){
        if(isset($r['end_time'])) $r['end_time'] = $this->formatDate($r['end_time']);
        return $r;
    }
}