123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- <?php
- namespace addons\qingdongams\model;
- use think\Exception;
- use think\Model;
- use traits\model\SoftDelete;
- /**
- *发票
- */
- class Invoice Extends Model {
- use SoftDelete;
- // 表名,不含前缀
- protected $name = 'qingdongams_invoice';
- // 开启自动写入时间戳字段
- protected $autoWriteTimestamp = 'int';
- // 定义时间戳字段名
- protected $createTime = 'createtime';
- protected $updateTime = 'updatetime';
- protected $deleteTime = 'deletetime';
- public static function getNumber()
- {
- return 'I' . date('ymd') . rand(100, 999);
- }
- public function getImageAttr($value) {
- if($value){
- return cdnurl($value, true);
- }
- return $value;
- }
- //客户
- public function customer() {
- return $this->hasOne(Customer::class, 'id', 'customer_id')->field('id,name');
- }
- //合同
- public function contract() {
- return $this->hasOne(Contract::class, 'id', 'contract_id')->field('id,name,num');
- }
- //负责人
- public function ownerStaff() {
- return $this->hasOne(Staff::class, 'id', 'owner_staff_id')->field('id,name,img');
- }
- public function createStaff() {
- return $this->hasOne(Staff::class, 'id', 'create_staff_id')->field('id,name,img');
- }
- //添加
- public static function createInvoice($params) {
- foreach ($params as $name => $val) {
- if($params[$name] === ''){
- $params[$name]=NULL;
- }
- }
- $staff = Staff::info();
- $staff_id = $staff->id;
- $params['create_staff_id'] = $staff_id;
- $params['owner_staff_id'] = $staff->id;
- $params['check_status'] = 1;
- $flow = Flow::getsteplist(Flow::INVOICE_STATUS);
- $params['flow_id'] = $flow['flow_id'];
- $params['order_id'] = $flow['order_id'];
- if ($flow['status'] == 0) {//发起人自选
- $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();
- $staff_id = explode(',', $params['flow_staff_ids'])[0];
- ExamineRecord::addExaminse(ExamineRecord::INVOICE_TYPE,$lastId, $staff_id);
- return true;
- }
- //编辑
- public static function updateInvoice($params) {
- $params['check_status'] = 1;
- $flow = Flow::getsteplist(Flow::INVOICE_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::INVOICE_STATUS, $params['id']);
- } else {//发起人自选 依次审批
- $staff_id = explode(',', $params['flow_staff_ids'])[0];
- if ($staff_id) {
- ExamineRecord::addExaminse(ExamineRecord::INVOICE_TYPE, $params['id'], $staff_id);
- }
- }
- return true;
- }
- //标记开票
- public static function updateInfo($params){
- $Model = new self;
- // 调用当前模型对应的User验证器类进行数据验证
- $result = $Model->save($params,['id'=>$params['id']]);
- if (false === $result) {
- // 验证失败 输出错误信息
- throw new Exception($Model->getError());
- }
- $contractId = $Model->where(['id'=>$params['id']])->value('contract_id');
- if($contractId){
- $file ='';
- if($params['image']){
- $file = File::where(['file_path'=>$params['image']])->value('id');
- }
- $ret = array(
- 'invoice_date'=>$params['invoice_time'],
- 'invoice_logistics_number'=>$params['logistics'],
- 'invoice_file_ids'=>$file,
- );
- Contract::where(['id'=>$contractId])->update($ret);
- }
- return true;
- }
- }
|