<?php
class flow_contract_listClassModel extends flowModel
{
    public $pagetitle = '新增合同';
    /**
     * 主表数据格式化
     * @param $rs
     * @return mixed
     */
    public function flowrsreplace($rs)
    {
        foreach ($rs as $k => &$v) {
            switch ($k) {
                case 'finance_type':
                    $v = $this->getOptionType($v);
                    continue;
                    break;
                case 'departmentname':
                    if($v == '') $v = $rs['deptname'];
                    continue;
                    break;
                case 'staff':
                    if($v == '') {
                        $v = $v['staffname'];
                    }
                    continue;
                    break;
                case 'receive':
                    if($v == 1) {
                        $v = '甲方';
                    } else if($v == 2) {
                        $v = '乙方';
                    } else {
                        $v = '';
                    }
                    continue;
                    break;
//                case 'part_a':
//                    $parta = $this->getCustomerLink($rs['parta_name']);
//                    if(!empty($parta)) $v = $parta;
//                    continue;
//                    break;
//                case 'part_b':
//                    $partb = $this->getSupplierLink($rs['partb_name']);
//                    if(!empty($parta)) $v = $partb;
//                    continue;
//                    break;
                case 'busi_line':
                    if($v == '') {
                        $v = $rs['businame'];
                        continue;
                        break;
                    }
                    else {
                        $v= $this->getOptionType($v);
                        continue;
                        break;
                    }
                case 'pid':
                    $v = $this->getCustomerLink($v);
                    continue;
                    break;
                case 'archive':
                    $v = $v == 1 ? '<span style="color:#eb00ff;">已归档</span>' : '未归档';
                    continue;
                    break;
                case 'amount':
                    $v = $this->farmatMoney($v);
                    continue;
                    break;
            }
        }
        return $rs;
    }


    /**
     * 子表数据替换
     * @param $r
     * @param int $lx   =0编辑,1显示
     * @return mixed
     */
    public function flowsubdata($r,$lx){
        foreach ($r as $k => &$v){
            if(is_array($v)){
                foreach ($v as $a => $b){
                    if($lx == 1) {
                        if ($a == 'title' && !empty($b)) {
                            $v[$a] = $this->getfeename($b);
                            continue;
                        }

                        if ($a == 'rate' && !empty($b)) {
                            $v[$a] = $this->getrate($b);
                            continue;
                        }

                        if ($a == 'costmodel' && !empty($b)) {
                            $v[$a] = $this->getcostname($b);
                            continue;
                        }
                        if($a == 'invoicetype' && !empty($b)){
                            $v[$a] = $this->getinvoicename($b);
                            continue;
                        }
                    }

                }
            }
        }
        return $r;
    }

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

    private function farmatMoney($money,$max_length = 2){
        if($money == '') return '';
        $is_dot = stripos($money,'.');
        if($is_dot){
            $tai = substr($money,$is_dot+1);
            $length = strlen($tai);
            $fill_num = $max_length - $length;
            if($fill_num > 0) {
                $money .= str_repeat('0',$fill_num); //补0
            }
            else if($fill_num < 0) {
                $money = round(floatval($money),$max_length);
            }
            return $money;
        }
        return $money . '.'.str_repeat('0', $max_length);
    }

    public function inputtitle(){
        return $this->pagetitle;
    }

    private function getrate($rate){
        $data = $this->db->getone('oa_option', 'num = "'.$rate.'"');
        if($data) return $data['name'];
        return '';
    }

    private function getcostname($costmodel){
        $data = $this->db->getone('oa_option', 'num = "'.$costmodel.'"');
        if($data) return $data['name'];
        return '';
    }

    private function getinvoicename($no){
        $data = $this->db->getone('oa_option', ' pid = 351 and num = "'.$no.'"');
        if($data) return $data['name'];
        return '';
    }

    private function getfeename($num){
        $data = $this->db->getone('oa_option', ' num = "'.$num.'"');
        if($data) return $data['name'];
        return '';
    }

   protected function flowbillwhere($uid,$lx)
   {
       // $where = 'and 1=2';              //默认不可访问
       // if($lx == 'all'){               //检测列表
       //     $uarr = $this->getuids('permisstioncontractall');        //设置可访问列表的人员ID
       //     if(in_array($uid,$uarr)){
       //         $where = 'and 1=1';
       //     }
       // }
       // if($lx == 'archive') {
       //     $uarr = $this->getuids('permisstioncontractarchive');      //设置可访问列表的人员ID
       //     if(in_array($uid,$uarr)){
       //         $where = 'and 1=1 ';
       //     }
       // }
       // if($lx == 'dept'){
       //     $where = '';
       // }
    // if($lx == 'confirm'){
    //        $where = 'inner join  oa_flow_bill as f  on oa_c_contract.id=f.mid where f.nowcourseid =482';
    //        // var_dump($where);die;
    //    }
    //    return array(
    //        'where' => $where
    //    );
   }

    //根据数据字典的编号获取uid
    public function getuids($num){
        $numarr = m('option')->getone('num = "'.$num.'"','id');
        $data = m('option')->getall('pid = "'.$numarr['id'].'"','id,num');

        $ids = [];
        foreach ($data as $v){
            $ids[] = $v['num'];
        }
        return $ids;
    }

}