ExamineRecord.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. $data = [
  57. 'relation_type' => $relation_type,
  58. 'relation_id' => $relation_id,
  59. 'check_staff_id' => $staff_id,
  60. 'status' => 0
  61. ];
  62. if (self::where($data)->find()) {//已发送过审批通知
  63. return true;
  64. }
  65. $Model = new self;
  66. $result = $Model->save($data);
  67. if (false === $result) {
  68. // 验证失败 输出错误信息
  69. throw new Exception($Model->getError());
  70. }
  71. //发送通知
  72. Message::addMessage(Message::EXAMINE_TYPE, $Model->getLastInsID(), $staff_id, $staff->id);
  73. return true;
  74. }
  75. /**
  76. *是否审批
  77. */
  78. public static function isExaminse($relation_type, $relation_id) {
  79. $staff = Staff::info();
  80. if (self::where([
  81. 'relation_type' => $relation_type,
  82. 'relation_id' => $relation_id,
  83. 'status' => 0,
  84. 'check_staff_id' => $staff->id
  85. ])->find()) {
  86. return 1;
  87. }
  88. return 0;
  89. }
  90. //合同
  91. public function contract() {
  92. return $this->hasOne(Contract::class, 'id', 'relation_id')
  93. ->field('id,name,owner_staff_id');
  94. }
  95. //费用
  96. public function consume() {
  97. return $this->hasOne(Consume::class, 'id', 'relation_id')->field('id,consume_time,consume_type,money,staff_id');
  98. }
  99. //回款
  100. public function receivables() {
  101. return $this->hasOne(Receivables::class, 'id', 'relation_id')->field('id,return_time,return_type,number,create_staff_id,money');
  102. }
  103. //业绩目标
  104. public function achievement() {
  105. return $this->hasOne(AchievementRecords::class, 'id', 'relation_id');
  106. }
  107. //办公审批
  108. public function approval() {
  109. return $this->hasOne(Approval::class, 'id', 'relation_id')->field('id,formapproval_id,create_staff_id');
  110. }
  111. public static function cancelExaminse($relation_type, $relation_id)
  112. {
  113. $record = ExamineRecord::where([
  114. 'relation_type' => $relation_type,
  115. 'relation_id' => $relation_id,
  116. 'status' => 0
  117. ])->find();
  118. if(empty($record)){
  119. return true;
  120. }
  121. if ($message = Message::where([
  122. 'relation_type' => 'examine',
  123. 'relation_id' => $record['id'],
  124. ])->find()) {
  125. Message::where(['id' => $message['id']])->update(['status' => 1, 'read_time' => time()]);
  126. }
  127. ExamineRecord::where([
  128. 'relation_type' => $relation_type,
  129. 'relation_id' => $relation_id,
  130. 'status' => 0
  131. ])->update(['status' => 3]);
  132. return true;
  133. }
  134. }