<?php
/**
 * 供应商接口
 * Class opencatClassAction
 */
class opensupplierClassAction 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';

        $where = '';
        if(!empty($post['email'])){
            $user = $this->db->getone('oa_userinfo', 'email = \''.$post['email'].'\'');
            $where  = ' where c.department_id = \''.$user['deptname'] .'\'';
        }
        if($post['count'] == 'all')
            $sql  = 'select id,number,name from [Q]supplier as c '.$where.' order by c.id '.$sort;

        else
            $sql  = 'select id,number,name from [Q]supplier as c  order by c.id '.$sort.' limit '.$page.','.$page_size;

        m('log')->addlogs('供应商接口调用', 'list: email '.$post['email'].'count '.$post['count'] , 1);

        $data = $this->db->getall($sql);
        foreach ($data as $key => $vo){
            $supp ='select open_bank_name,bank_code,bank_holder,open_address from [Q]supplier_bank_info   where mid='.$vo['id'];
            $contacts ='select name,job,tel,email,adress,remarks from [Q]supplier_contract   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;
    }
}