Consume.php 4.56 KB
<?php

namespace addons\antis\model;

use think\Exception;
use think\Model;
use traits\model\SoftDelete;

/**
 *费用表
 */
class Consume Extends Model {
	use SoftDelete;
    // 表名,不含前缀
    protected $name = 'qingdong_consume';
	// 开启自动写入时间戳字段
	protected $autoWriteTimestamp = 'int';
	// 定义时间戳字段名
	protected $createTime = 'createtime';
	protected $updateTime = 'updatetime';
	protected $deleteTime = 'deletetime';


    // 追加属性
    protected $append = [
        'file_text',
    ];

    // 图片
    public function getFileTextAttr($value, $data)
    {
        if(!isset($data['file_ids'])){
            return '';
        }
        if(is_array($value)){
            return $value;
        }
        $value = $value ? $value : $data['file_ids'];
        $files      = explode(',',$value);
        $files_v = [];
        foreach ($files as $fid) {
            if($fid){
                $files_v[] = cdnurl(File::getUrl($fid), true);
            }
        }
        return $files_v;
    }

	//添加费用
	public static function createConsume($params) {
		$staff                  = Staff::info();
        $staff_id = $staff->id;
		if($params['customer_id']){
            $staff_id = Customer::where(['id'=>$params['customer_id']])->value('owner_staff_id');
        }
		$params['staff_id']     = $staff_id;
		$params['check_status'] = 1;
        $flow = Flow::getsteplist(Flow::CONSUME_STATUS);
        $params['flow_id'] = $flow['flow_id'];
        $params['order_id'] = $flow['order_id'];

        if ($flow['status'] == 0) {//发起人自选
            if (empty($params['flow_staff_ids'])) {
                throw new Exception('审批人必须选择');
            }
            $params['flow_staff_ids'] = trim($params['flow_staff_ids']);
        } else {
            $params['flow_staff_ids'] = trim($flow['flow_staff_ids']);
        }


        $Model                  = new self;
        $result = $Model->allowField(true)->save($params);
		if (false === $result) {
			// 验证失败 输出错误信息
			throw new Exception($Model->getError());
		}
        $lastId= $Model->getLastInsID();
        if ($params['file_ids'] && $params['customer_id']) {
            CustomerFile::addFiles($params['file_ids'], $params['customer_id']);
        }

		$staff_id = explode(',', $params['flow_staff_ids'])[0];

		ExamineRecord::addExaminse(ExamineRecord::CONSUME_TYPE,$lastId, $staff_id);

		return true;
	}


	//编辑费用
	public static function updateConsume($params) {
		$params['check_status'] = 1;

        $flow = Flow::getsteplist(Flow::CONSUME_STATUS);
        $params['flow_id'] = $flow['flow_id'];
        $params['order_id'] = $flow['order_id'];

        if ($flow['status'] == 0) {//发起人自选
            if (empty($params['flow_staff_ids'])) {
                throw new Exception('审批人必须选择');
            }
            $params['flow_staff_ids'] = trim($params['flow_staff_ids']);
        } else {
            $params['flow_staff_ids'] = trim($flow['flow_staff_ids']);
        }

		$Model                  = new self;
		// 调用当前模型对应的User验证器类进行数据验证
		$result = $Model->save($params,['id'=>$params['id']]);
		if (false === $result) {
			// 验证失败 输出错误信息
			throw new Exception($Model->getError());
		}

        if ($flow['status'] == 1) {//固定审批
            //发送审批通知
            Flow::sendStepRecord($flow,Flow::CONSUME_STATUS, $params['id']);
        } else {//发起人自选 依次审批
            $staff_id   = explode(',', $params['flow_staff_ids'])[0];
            if ($staff_id) {
                ExamineRecord::addExaminse(ExamineRecord::CONSUME_TYPE, $params['id'], $staff_id);
            }
        }

		return true;
	}

    public function getCreatetimeAttr($value){
        if(empty($value)){
            return $value;
        }
        return date('Y-m-d H:i:s',$value);
    }
    public function getUpdatetimeAttr($value){
        if(empty($value)){
            return $value;
        }
        return date('Y-m-d H:i:s',$value);
    }

	//销售
	public function staff() {
		return $this->hasOne(Staff::class, 'id', 'staff_id')->field('id,img,name');
	}

    //审核人
    public function followStaff() {
        return $this->hasOne(Staff::class, 'id', 'flow_staff_ids')->field('id,img,name');
    }

	//客户
	public function customer() {
		return $this->hasOne(Customer::class, 'id', 'customer_id')->field('id,name');
	}
}