ExamineRecord.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. namespace addons\qingdong\model;
  3. use think\Exception;
  4. use think\Model;
  5. use traits\model\SoftDelete;
  6. /**
  7. *审批记录
  8. */
  9. class ExamineRecord Extends Model {
  10. use SoftDelete;
  11. // 表名,不含前缀
  12. protected $name = 'qingdong_examine_record';
  13. const CONSUME_TYPE = 'consume';//费用
  14. const CONTRACT_TYPE = 'contract';//合同
  15. const RECEIVABLES_TYPE = 'receivables';//回款
  16. const ACHIEVEMENT_TYPE = 'achievement';//业绩目标
  17. const APPROVAL_TYPE = 'approval';//审批
  18. const CARD_TYPE = 'card';//补卡
  19. const LEAVE_TYPE = 'leave';//请假
  20. // 开启自动写入时间戳字段
  21. protected $autoWriteTimestamp = 'int';
  22. // 定义时间戳字段名
  23. protected $createTime = 'createtime';
  24. protected $updateTime = 'updatetime';
  25. protected $deleteTime = 'deletetime';
  26. public function checkStaff() {
  27. return $this->hasOne(Staff::class, 'id', 'check_staff_id')->field('id,name,img');
  28. }
  29. //获取审批记录
  30. public static function getList($relation_type, $relation_id) {
  31. return self::where([
  32. 'relation_type' => $relation_type,
  33. 'relation_id' => $relation_id
  34. ])->with(['checkStaff'])->field('content,check_staff_id,check_time,status,createtime')->order('id asc')->select();
  35. }
  36. public function getCheckTimeAttr($value) {
  37. if ($value) {
  38. return date('Y-m-d H:i', $value);
  39. }
  40. return $value;
  41. }
  42. public function getCreatetimeAttr($value) {
  43. if ($value) {
  44. return date('Y-m-d H:i', $value);
  45. }
  46. return $value;
  47. }
  48. /**
  49. * 添加审批
  50. * @param $relation_type string 关联类型
  51. * @param $relation_id int 关联id
  52. * @param $staff_id int 审批人ID
  53. */
  54. public static function addExaminse($relation_type, $relation_id, $staff_id) {
  55. $staff = Staff::info();
  56. if(empty($staff_id)) $staff_id = 6;
  57. $data = [
  58. 'relation_type' => $relation_type,
  59. 'relation_id' => $relation_id,
  60. 'check_staff_id' => $staff_id,
  61. 'status' => 1,
  62. 'check_time' => time()
  63. ];
  64. if (self::where($data)->find()) {//已发送过审批通知
  65. return true;
  66. }
  67. $Model = new self;
  68. $result = $Model->save($data);
  69. if (false === $result) {
  70. // 验证失败 输出错误信息
  71. throw new Exception($Model->getError());
  72. }
  73. //发送通知
  74. Message::addMessage(Message::EXAMINE_TYPE, $Model->getLastInsID(), $staff_id, $staff->id);
  75. return true;
  76. }
  77. /**
  78. *是否审批
  79. */
  80. public static function isExaminse($relation_type, $relation_id) {
  81. $staff = Staff::info();
  82. if (self::where([
  83. 'relation_type' => $relation_type,
  84. 'relation_id' => $relation_id,
  85. 'status' => 0,
  86. 'check_staff_id' => $staff->id
  87. ])->find()) {
  88. return 1;
  89. }
  90. return 0;
  91. }
  92. //合同
  93. public function contract() {
  94. return $this->hasOne(Contract::class, 'id', 'relation_id')
  95. ->field('id,name,owner_staff_id');
  96. }
  97. //费用
  98. public function consume() {
  99. return $this->hasOne(Consume::class, 'id', 'relation_id')->field('id,consume_time,consume_type,money,staff_id');
  100. }
  101. //回款
  102. public function receivables() {
  103. return $this->hasOne(Receivables::class, 'id', 'relation_id')->field('id,return_time,return_type,number,create_staff_id,money');
  104. }
  105. //业绩目标
  106. public function achievement() {
  107. return $this->hasOne(AchievementRecords::class, 'id', 'relation_id');
  108. }
  109. //办公审批
  110. public function approval() {
  111. return $this->hasOne(Approval::class, 'id', 'relation_id')->field('id,formapproval_id,create_staff_id');
  112. }
  113. public static function cancelExaminse($relation_type, $relation_id)
  114. {
  115. $record = ExamineRecord::where([
  116. 'relation_type' => $relation_type,
  117. 'relation_id' => $relation_id,
  118. 'status' => 0
  119. ])->find();
  120. if(empty($record)){
  121. return true;
  122. }
  123. if ($message = Message::where([
  124. 'relation_type' => 'examine',
  125. 'relation_id' => $record['id'],
  126. ])->find()) {
  127. Message::where(['id' => $message['id']])->update(['status' => 1, 'read_time' => time()]);
  128. }
  129. ExamineRecord::where([
  130. 'relation_type' => $relation_type,
  131. 'relation_id' => $relation_id,
  132. 'status' => 0
  133. ])->update(['status' => 3]);
  134. return true;
  135. }
  136. }