<?php
namespace User\Lib;

use Dever;

class Invoice
{
    # 获取发票
    public function get($source_id, $source_table, $uid, $address_id)
    {
        $info = $this->getData($source_id, $source_table, 1);
        if (!$info) {
            $info = $this->getInfo($uid, $address_id);
        }
        return $info;
    }
    
    # 获取默认发票
    public function getData($source_id, $source_table = 'user/info', $type = 2)
    {
        $where['source_table'] = $source_table;
        $where['source_id'] = $source_id;
        $where['type'] = $type;
        $data = Dever::db('user/invoice')->one($where);

        if ($data) {
            $data = $this->info($data);
        }
        
        return $data;
    }

    # 获取某个发票
    public function getInfo($source_id, $id, $source_table = 'user/info')
    {
        $where['source_table'] = $source_table;
        $where['source_id'] = $source_id;
        $where['id'] = $id;
        $data = Dever::db('user/invoice')->one($where);

        if ($data) {
            $data = $this->info($data);
        }

        return $data;
    }

    # 获取发票列表
    public function getList($source_id, $source_table = 'user/info')
    {
        $where['source_table'] = $source_table;
        $where['source_id'] = $source_id;
        $data = Dever::db('user/invoice')->getList($where);

        if ($data) {
            foreach ($data as $k => $v) {
                $data[$k] = $this->info($v);
            }
        }

        return $data;
    }

    public function info($data)
    {
        $data['title_type_name'] = Dever::db('user/invoice')->config['title_type'][$data['title_type']];

        return $data;
    }

    # 更新
    public function update($id, $source_id, $source_table, $type = 2, $title_type = 1, $title = '', $email = '', $mobile = '', $number = '', $phone = '', $address = '', $bank = '', $bank_number = '')
    {
        $update['title_type'] = $title_type;
        if ($title_type == 1) {
            if (!$number) {
                Dever::alert('公司税号不能为空');
            }
            $update['number'] = $number;
        }

        $update['type'] = $type;
        $update['title'] = $title;

        if (!$title) {
            Dever::alert('发票抬头不能为空');
        }

        if ($email) {
            $update['email'] = $email;
        }
        if ($mobile) {
            $update['mobile'] = $mobile;
        }
        if ($phone) {
            $update['phone'] = $phone;
        }
        if ($address) {
            $update['address'] = $address;
        }
        if ($bank) {
            $update['bank'] = $bank;
        }
        if ($bank_number) {
            $update['bank_number'] = $bank_number;
        }

        if ($type == 2) {
            Dever::db('user/invoice')->updateType(array('where_type' => 2, 'set_type' => 1));
        }
        
        if ($id) {
            $update['where_id'] = $id;
            Dever::db('user/invoice')->update($update);
        } else {
            $update['source_id'] = $source_id;
            $update['source_table'] = $source_table;
            $id = Dever::db('user/invoice')->insert($update);
        }

        return $id;
    }

    # 删除和恢复
    public function delete($source_id, $id, $state = 2, $source_table = 'user/info')
    {
        $info = $this->getInfo($source_id, $id, $source_table);
        if ($info) {
            $update['where_id'] = $info['id'];
            $update['state'] = $state;
            Dever::db('user/invoice')->update($update);
        } else {
            Dever::alert('更新失败');
        }

        return 'ok';
    }

    public function getSource($source_table, $source_id)
    {
        $info = Dever::db($source_table)->one($source_id);
        return $info;
    }

    public function getManageUrl($source_table, $source_id, $type)
    {
        $info = Dever::db('user/invoice')->one(array('source_table' => $source_table, 'source_id' => $source_id));
        $url = Dever::url('project/database/update?project=user&table=invoice&search_option_source_table=' . $source_table . '&search_option_source_id=' . $source_id, 'manage');

        if ($info) {
            $url .= '&where_id=' . $info['id'];
        }
        return $url;
    }
}