ExamineRecord.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. namespace addons\qingdongams\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 = 'qingdongams_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. const PARTS_TYPE = 'parts';//零件
  21. const QUOTE_TYPE = 'quote';//报价单
  22. const WORKORDER_TYPE = 'workorder';//工单
  23. const INVOICE_TYPE = 'invoice';//发票
  24. // 开启自动写入时间戳字段
  25. protected $autoWriteTimestamp = 'int';
  26. // 定义时间戳字段名
  27. protected $createTime = 'createtime';
  28. protected $updateTime = 'updatetime';
  29. protected $deleteTime = 'deletetime';
  30. public function getStatusTextAttr()
  31. {
  32. $a = [0 => __('待审批'), 1 => __('审核通过'), 2 => __('审核拒绝'), 3 => __('撤销')];
  33. return $a[$this->status ?? ''] ?? '';
  34. }
  35. public function checkStaff() {
  36. return $this->hasOne(Staff::class, 'id', 'check_staff_id')->field('id,name,img');
  37. }
  38. //获取审批记录
  39. public static function getList($relation_type, $relation_id) {
  40. return self::where([
  41. 'relation_type' => $relation_type,
  42. 'relation_id' => $relation_id
  43. ])->with(['checkStaff'])->field('content,check_staff_id,check_time,status,createtime')->order('id asc')->select();
  44. }
  45. public function getCheckTimeAttr($value) {
  46. if ($value) {
  47. return date('Y-m-d H:i', $value);
  48. }
  49. return $value;
  50. }
  51. public function getCreatetimeAttr($value) {
  52. if ($value) {
  53. return date('Y-m-d H:i', $value);
  54. }
  55. return $value;
  56. }
  57. /**
  58. * 添加审批
  59. * @param $relation_type string 关联类型
  60. * @param $relation_id int 关联id
  61. * @param $staff_id int 审批人ID
  62. */
  63. public static function addExaminse($relation_type, $relation_id, $staff_id) {
  64. $staff = Staff::info();
  65. $data = [
  66. 'relation_type' => $relation_type,
  67. 'relation_id' => $relation_id,
  68. 'check_staff_id' => $staff_id,
  69. 'status' => 0
  70. ];
  71. if (self::where($data)->find()) {//已发送过审批通知
  72. return true;
  73. }
  74. $Model = new self;
  75. $result = $Model->save($data);
  76. if (false === $result) {
  77. // 验证失败 输出错误信息
  78. throw new Exception($Model->getError());
  79. }
  80. //发送通知
  81. Message::addMessage(Message::EXAMINE_TYPE, $Model->getLastInsID(), $staff_id, $staff->id);
  82. return true;
  83. }
  84. /**
  85. *是否审批
  86. */
  87. public static function isExaminse($relation_type, $relation_id) {
  88. $staff = Staff::info();
  89. if (self::where([
  90. 'relation_type' => $relation_type,
  91. 'relation_id' => $relation_id,
  92. 'status' => 0,
  93. 'check_staff_id' => $staff->id
  94. ])->find()) {
  95. return 1;
  96. }
  97. return 0;
  98. }
  99. //合同
  100. public function contract() {
  101. return $this->hasOne(Contract::class, 'id', 'relation_id')
  102. ->field('id,name,owner_staff_id');
  103. }
  104. //费用
  105. public function consume() {
  106. return $this->hasOne(Consume::class, 'id', 'relation_id');
  107. }
  108. //回款
  109. public function receivables() {
  110. return $this->hasOne(Receivables::class, 'id', 'relation_id')->field('id,return_time,return_type,number,create_staff_id,money');
  111. }
  112. //业绩目标
  113. public function achievement() {
  114. return $this->hasOne(AchievementRecords::class, 'id', 'relation_id');
  115. }
  116. //办公审批
  117. public function approval() {
  118. return $this->hasOne(Approval::class, 'id', 'relation_id')->field('id,formapproval_id,create_staff_id');
  119. }
  120. //发票
  121. public function invoice() {
  122. return $this->hasOne(Invoice::class, 'id', 'relation_id');
  123. }
  124. public static function cancelExaminse($relation_type, $relation_id)
  125. {
  126. $record = ExamineRecord::where([
  127. 'relation_type' => $relation_type,
  128. 'relation_id' => $relation_id,
  129. 'status' => 0
  130. ])->find();
  131. if(empty($record)){
  132. return true;
  133. }
  134. if ($message = Message::where([
  135. 'relation_type' => 'examine',
  136. 'relation_id' => $record['id'],
  137. ])->find()) {
  138. Message::where(['id' => $message['id']])->update(['status' => 1, 'read_time' => time()]);
  139. }
  140. ExamineRecord::where([
  141. 'relation_type' => $relation_type,
  142. 'relation_id' => $relation_id,
  143. 'status' => 0
  144. ])->update(['status' => 3]);
  145. return true;
  146. }
  147. //产品入库申请记录
  148. public function partsstock() {
  149. return $this->hasOne(PartsStockReload::class, 'id', 'relation_id')->where(array('type'=>1))->field('id,type,check_status');
  150. }
  151. //产品出库申请记录
  152. public function partsstockout()
  153. {
  154. return $this->belongsTo(PartsStockReload::class, 'id', 'relation_id')->where(array('type' => 2));
  155. }
  156. //费用
  157. public function quote() {
  158. return $this->hasOne(Quote::class, 'id', 'relation_id')->field('id,quote_amount,contract_id,quote_date,owner_staff_id,number,customer_id');
  159. }
  160. //出入库
  161. public function parts() {
  162. return $this->hasOne(PartsStockReload::class, 'id', 'relation_id')->field('id,type,odd_numbers,storage_time,desc,parts,create_staff_id,check_status');
  163. }
  164. }