427 lines
		
	
	
		
			12 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			427 lines
		
	
	
		
			12 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
<?php
 | 
						|
 | 
						|
namespace app\common\model\user\invoice;
 | 
						|
 | 
						|
use app\common\model\BaseModel;
 | 
						|
use think\Model;
 | 
						|
 | 
						|
 | 
						|
class Header extends BaseModel
 | 
						|
{
 | 
						|
 | 
						|
    
 | 
						|
 | 
						|
    
 | 
						|
 | 
						|
    // 表名
 | 
						|
    protected $name = 'user_invoice_header';
 | 
						|
    
 | 
						|
    // 自动写入时间戳字段
 | 
						|
    protected $autoWriteTimestamp = 'integer';
 | 
						|
 | 
						|
    // 定义时间戳字段名
 | 
						|
    protected $createTime = 'createtime';
 | 
						|
    protected $updateTime = 'updatetime';
 | 
						|
    protected $deleteTime = false;
 | 
						|
 | 
						|
    // 追加属性
 | 
						|
    protected $append = [
 | 
						|
        'head_type_text',
 | 
						|
        'invoice_type_text',
 | 
						|
        'is_default_text',
 | 
						|
        'createtime_text',
 | 
						|
    ];
 | 
						|
 | 
						|
 | 
						|
    public function getCreatetimeTextAttr($value, $data)
 | 
						|
    {
 | 
						|
        $value = $value ? $value : (isset($data['createtime']) ? $data['createtime'] : '');
 | 
						|
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
 | 
						|
    }
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 | 
						|
    public function getHeadTypeList()
 | 
						|
    {
 | 
						|
        return ['personal' => __('Head_type personal'), 'corporate' => __('Head_type corporate')];
 | 
						|
    }
 | 
						|
 | 
						|
    public function getInvoiceTypeList()
 | 
						|
    {
 | 
						|
        return ['ordinary' => __('Invoice_type ordinary'), 'special' => __('Invoice_type special')];
 | 
						|
    }
 | 
						|
 | 
						|
    public function getIsDefaultList()
 | 
						|
    {
 | 
						|
        return ['0' => __('Is_default 0'), '1' => __('Is_default 1')];
 | 
						|
    }
 | 
						|
 | 
						|
 | 
						|
    public function getHeadTypeTextAttr($value, $data)
 | 
						|
    {
 | 
						|
        $value = $value ? $value : (isset($data['head_type']) ? $data['head_type'] : '');
 | 
						|
        $list = $this->getHeadTypeList();
 | 
						|
        return isset($list[$value]) ? $list[$value] : '';
 | 
						|
    }
 | 
						|
 | 
						|
 | 
						|
    public function getInvoiceTypeTextAttr($value, $data)
 | 
						|
    {
 | 
						|
        $value = $value ? $value : (isset($data['invoice_type']) ? $data['invoice_type'] : '');
 | 
						|
        $list = $this->getInvoiceTypeList();
 | 
						|
        return isset($list[$value]) ? $list[$value] : '';
 | 
						|
    }
 | 
						|
 | 
						|
 | 
						|
    public function getIsDefaultTextAttr($value, $data)
 | 
						|
    {
 | 
						|
        $value = $value ? $value : (isset($data['is_default']) ? $data['is_default'] : '');
 | 
						|
        $list = $this->getIsDefaultList();
 | 
						|
        return isset($list[$value]) ? $list[$value] : '';
 | 
						|
    }
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 | 
						|
    public function user()
 | 
						|
    {
 | 
						|
        return $this->belongsTo('app\admin\model\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
 | 
						|
    }
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 | 
						|
    /** 通用新增(后台api版本)
 | 
						|
     * @param $params
 | 
						|
     * @param $trans
 | 
						|
     * @return $this
 | 
						|
     * @throws \Exception
 | 
						|
     */
 | 
						|
    public function add($params,$trans=false){
 | 
						|
 | 
						|
        if (empty($params)) {
 | 
						|
            throw new \Exception(__('Parameter %s can not be empty', ''));
 | 
						|
        }
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 | 
						|
        $rule =  [
 | 
						|
            'user_id'=>'require',
 | 
						|
            'head_type'=>'require',
 | 
						|
            'invoice_type'=>'require',
 | 
						|
            'invoice_header' => 'require',
 | 
						|
            'invoice_reservation_phone' => 'require',
 | 
						|
            'invoice_reservation_email' => 'require',
 | 
						|
        ];
 | 
						|
 | 
						|
        $rule_msg = [
 | 
						|
            "user_id.require"=>'提交用户必填',
 | 
						|
            "head_type.require"=>'抬头类型必填',
 | 
						|
            "invoice_type.require"=>'发票类型必填',
 | 
						|
            'invoice_header.require' => '发票抬头必填',
 | 
						|
            'invoice_reservation_phone.require' => '发票预留电话必填',
 | 
						|
            'invoice_reservation_email.require' => '发票预留邮箱必填',
 | 
						|
        ];
 | 
						|
 | 
						|
        self::check($params,$rule,$rule_msg);
 | 
						|
 | 
						|
        //个人无法开专用发票
 | 
						|
        if($params['head_type']=='personal' && $params['invoice_type']=='special'){
 | 
						|
            throw new \Exception('个人用户无法开专用发票');
 | 
						|
        }
 | 
						|
 | 
						|
        //企业需要填写纳税人识别号等信息
 | 
						|
        if($params['head_type']=='corporate'){
 | 
						|
            $rule =  [
 | 
						|
                'tax_id'=>'require',
 | 
						|
                'bank_deposit'=>'require',
 | 
						|
                'bank_number'=>'require',
 | 
						|
                'enterprise_address'=>'require',
 | 
						|
                'enterprise_phone'=>'require',
 | 
						|
            ];
 | 
						|
            $rule_msg = [
 | 
						|
                'tax_id.require' => '纳税人识别号必填',
 | 
						|
                'bank_deposit.require' => '开户银行必填',
 | 
						|
                'bank_number.require' => '银行账号必填',
 | 
						|
                'enterprise_address.require' => '企业地址必填',
 | 
						|
                'enterprise_phone.require' => '企业电话必填',
 | 
						|
            ];
 | 
						|
            self::check($params,$rule,$rule_msg);
 | 
						|
        }
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 | 
						|
//判断逻辑
 | 
						|
        if($trans){
 | 
						|
            self::beginTrans();
 | 
						|
        }
 | 
						|
        $res = true;
 | 
						|
        try{
 | 
						|
 | 
						|
            //是否采用模型验证
 | 
						|
            $params["is_default"] = $params["is_default"] ?? '0';
 | 
						|
            if($params["is_default"] == '1'){
 | 
						|
                self::where('user_id',$params['user_id'])->update(['is_default'=>'0']);
 | 
						|
            }
 | 
						|
 | 
						|
 | 
						|
            $result = $this->allowField(true)->save($params);
 | 
						|
 | 
						|
 | 
						|
            if($trans){
 | 
						|
                self::commitTrans();
 | 
						|
            }
 | 
						|
        }catch (\Exception $e){
 | 
						|
            if($trans){
 | 
						|
                self::rollbackTrans();
 | 
						|
            }
 | 
						|
            throw new \Exception($e->getMessage().$e->getFile().$e->getLine());
 | 
						|
        }
 | 
						|
        return $this;
 | 
						|
    }
 | 
						|
 | 
						|
 | 
						|
    /** 通用编辑(后台api版本)
 | 
						|
     * @param $params
 | 
						|
     * @param $trans
 | 
						|
     * @return $this
 | 
						|
     * @throws \Exception
 | 
						|
     */
 | 
						|
    public function edit($id,$params,$trans=false){
 | 
						|
 | 
						|
        $row = $this->get($id);
 | 
						|
        if (!$row) {
 | 
						|
            throw new \Exception(__('No Results were found'));
 | 
						|
        }
 | 
						|
 | 
						|
 | 
						|
 | 
						|
        if (empty($params)) {
 | 
						|
            throw new \Exception(__('Parameter %s can not be empty', ''));
 | 
						|
        }
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 | 
						|
        $rule =  [
 | 
						|
            'user_id'=>'require',
 | 
						|
            'head_type'=>'require',
 | 
						|
            'invoice_type'=>'require',
 | 
						|
            'invoice_header' => 'require',
 | 
						|
            'invoice_reservation_phone' => 'require',
 | 
						|
            'invoice_reservation_email' => 'require',
 | 
						|
        ];
 | 
						|
 | 
						|
        $rule_msg = [
 | 
						|
            "user_id.require"=>'提交用户必填',
 | 
						|
            "head_type.require"=>'抬头类型必填',
 | 
						|
            "invoice_type.require"=>'发票类型必填',
 | 
						|
            'invoice_header.require' => '发票抬头必填',
 | 
						|
            'invoice_reservation_phone.require' => '发票预留电话必填',
 | 
						|
            'invoice_reservation_email.require' => '发票预留邮箱必填',
 | 
						|
        ];
 | 
						|
 | 
						|
        self::check($params,$rule,$rule_msg);
 | 
						|
 | 
						|
        //个人无法开专用发票
 | 
						|
        if($params['head_type']=='personal' && $params['invoice_type']=='special'){
 | 
						|
            throw new \Exception('个人用户无法开专用发票');
 | 
						|
        }
 | 
						|
 | 
						|
        //企业需要填写纳税人识别号等信息
 | 
						|
        if($params['head_type']=='corporate'){
 | 
						|
            $rule =  [
 | 
						|
                'tax_id'=>'require',
 | 
						|
                'bank_deposit'=>'require',
 | 
						|
                'bank_number'=>'require',
 | 
						|
                'enterprise_address'=>'require',
 | 
						|
                'enterprise_phone'=>'require',
 | 
						|
            ];
 | 
						|
            $rule_msg = [
 | 
						|
                'tax_id.require' => '纳税人识别号必填',
 | 
						|
                'bank_deposit.require' => '开户银行必填',
 | 
						|
                'bank_number.require' => '银行账号必填',
 | 
						|
                'enterprise_address.require' => '企业地址必填',
 | 
						|
                'enterprise_phone.require' => '企业电话必填',
 | 
						|
            ];
 | 
						|
            self::check($params,$rule,$rule_msg);
 | 
						|
        }
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 | 
						|
//判断逻辑
 | 
						|
        if($trans){
 | 
						|
            self::beginTrans();
 | 
						|
        }
 | 
						|
        $res = true;
 | 
						|
        try{
 | 
						|
            $params["is_default"] = $params["is_default"] ?? '0';
 | 
						|
            if($params["is_default"] == '1'){
 | 
						|
                self::where('user_id',$params['user_id'])->update(['is_default'=>'0']);
 | 
						|
            }
 | 
						|
 | 
						|
            $result = $row->allowField(true)->save($params);
 | 
						|
 | 
						|
            if($trans){
 | 
						|
                self::commitTrans();
 | 
						|
            }
 | 
						|
        }catch (\Exception $e){
 | 
						|
            if($trans){
 | 
						|
                self::rollbackTrans();
 | 
						|
            }
 | 
						|
            throw new \Exception($e->getMessage().$e->getFile().$e->getLine());
 | 
						|
        }
 | 
						|
        return $row;
 | 
						|
    }
 | 
						|
 | 
						|
 | 
						|
    /** 通用详情(后台api版本)
 | 
						|
     * @param $params
 | 
						|
     * @param $trans
 | 
						|
     * @return $this
 | 
						|
     * @throws \Exception
 | 
						|
     */
 | 
						|
    public function detail($id,$show_field=[],$except_field=[]){
 | 
						|
        $row = $this->get($id);
 | 
						|
        if (!$row) {
 | 
						|
            throw new \Exception(__('No Results were found'));
 | 
						|
        }
 | 
						|
        if($show_field){
 | 
						|
            $row->visible($show_field);
 | 
						|
        }
 | 
						|
        if($except_field){
 | 
						|
            $row->hidden($except_field);
 | 
						|
        }
 | 
						|
        return $row;
 | 
						|
    }
 | 
						|
 | 
						|
//    public function index($page,$limit,$where=[])
 | 
						|
//    {
 | 
						|
//        $adminIds = $this->getDataLimitAdminIds();
 | 
						|
//        $aliasName = "" ;
 | 
						|
//        if (is_array($adminIds)) {
 | 
						|
//            $where[] = [$aliasName . $this->dataLimitField, 'in', $adminIds];
 | 
						|
//        }
 | 
						|
//
 | 
						|
//    }
 | 
						|
 | 
						|
 | 
						|
    /** 通用删除(后台api版本)
 | 
						|
     * @param $params
 | 
						|
     * @param $trans
 | 
						|
     * @return $this
 | 
						|
     * @throws \Exception
 | 
						|
     */
 | 
						|
    public function del($ids = null,$trans=false){
 | 
						|
        if (empty($ids)) {
 | 
						|
            throw new \Exception(__('Parameter %s can not be empty', 'ids'));
 | 
						|
        }
 | 
						|
//判断逻辑
 | 
						|
 | 
						|
        $pk = $this->getPk();
 | 
						|
 | 
						|
        $list = $this->where($pk, 'in', $ids)->select();
 | 
						|
        $count = 0;
 | 
						|
        if($trans){
 | 
						|
            self::beginTrans();
 | 
						|
        }
 | 
						|
        $res = true;
 | 
						|
        try{
 | 
						|
 | 
						|
            foreach ($list as $item) {
 | 
						|
                $count += $item->delete();
 | 
						|
            }
 | 
						|
 | 
						|
            if($trans){
 | 
						|
                self::commitTrans();
 | 
						|
            }
 | 
						|
        }catch (\Exception $e){
 | 
						|
            if($trans){
 | 
						|
                self::rollbackTrans();
 | 
						|
            }
 | 
						|
            throw new \Exception($e->getMessage().$e->getFile().$e->getLine());
 | 
						|
        }
 | 
						|
        return $count;
 | 
						|
    }
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 | 
						|
    /**得到基础条件
 | 
						|
     * @param $status
 | 
						|
     * @param null $model
 | 
						|
     * @param string $alisa
 | 
						|
     */
 | 
						|
    public static function getBaseWhere($whereData = [], $model = null, $alisa = '',$with = false)
 | 
						|
    {
 | 
						|
 | 
						|
        if (!$model) {
 | 
						|
            $model = new static;
 | 
						|
            if ($alisa&&!$with) $model = $model->alias($alisa);
 | 
						|
        }
 | 
						|
        if ($alisa) $alisa = $alisa . '.';
 | 
						|
        $tableFields = (new static)->getTableFields();
 | 
						|
        foreach ($tableFields as $fields)
 | 
						|
        {
 | 
						|
            if(in_array($fields, ["user_id"]))continue;
 | 
						|
//            if (isset($whereData[$fields]) && $whereData[$fields]) $model = $model->where("{$alisa}{$fields}", '=', $whereData[$fields]);
 | 
						|
 | 
						|
            if (isset($whereData[$fields]) && $whereData[$fields]){
 | 
						|
                if(is_array($whereData[$fields])){
 | 
						|
                    $model = $model->where("{$alisa}{$fields}", $whereData[$fields][0], $whereData[$fields][1]);
 | 
						|
                }else{
 | 
						|
                    $model = $model->where("{$alisa}{$fields}", '=', $whereData[$fields]);
 | 
						|
                }
 | 
						|
 | 
						|
            }
 | 
						|
 | 
						|
 | 
						|
        }
 | 
						|
        if (isset($whereData['user_id']) && $whereData['user_id']) $model = $model->where("{$alisa}user_id", 'in', $whereData['user_id']);
 | 
						|
 | 
						|
        if (isset($whereData['keywords'])&&$whereData['keywords']) $model = $model->where("{$alisa}invoice_header|{$alisa}tax_id|{$alisa}bank_number|{$alisa}invoice_reservation_phone|{$alisa}invoice_reservation_email", 'like', "%". $whereData['keywords']."%");
 | 
						|
 | 
						|
        if (isset($whereData['time'])&&$whereData['time']){
 | 
						|
            $model = $model->time(["{$alisa}createtime",$whereData['time']]);
 | 
						|
        }
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 | 
						|
        return $model;
 | 
						|
    }
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 | 
						|
    public static function headerList($page, $limit,$params=[]){
 | 
						|
        $with_field = [
 | 
						|
            'base'=>['*'],
 | 
						|
        ];
 | 
						|
//        $alisa = (new self)->getWithAlisaName();
 | 
						|
//        $sort = "{$alisa}.id desc";
 | 
						|
 | 
						|
        $sort = "is_default desc,id desc";
 | 
						|
 | 
						|
        $serch_where = [];
 | 
						|
        $serch_where = array_merge($serch_where,$params);
 | 
						|
        return (new self)->getBaseList($serch_where, $page, $limit,$sort,$with_field);
 | 
						|
    }
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 | 
						|
}
 |