ExamineRecord.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <?php
  2. namespace addons\qingdong\controller;
  3. use addons\qingdong\model\AchievementRecords;
  4. use addons\qingdong\model\AttendanceStatisc;
  5. use addons\qingdong\model\Business;
  6. use addons\qingdong\model\BusinessStatus;
  7. use addons\qingdong\model\Flow;
  8. use addons\qingdong\model\FormApproval;
  9. use addons\qingdong\model\Message;
  10. use addons\qingdong\model\Contract;
  11. use addons\qingdong\model\Consume;
  12. use addons\qingdong\model\Staff;
  13. use addons\qingdong\model\Receivables;
  14. use addons\qingdong\model\Achievement;
  15. use addons\qingdong\model\Approval;
  16. use addons\qingdong\model\ExamineRecord as ExamineRecordModel;
  17. use think\Cache;
  18. use think\Db;
  19. use think\Exception;
  20. use think\Log;
  21. /**
  22. * 审批记录
  23. */
  24. class ExamineRecord extends StaffApi {
  25. protected $noNeedLogin = [];
  26. protected $noNeedRight = [];
  27. //审批记录
  28. public function getList() {
  29. $relation_type = input('relation_type');
  30. $relation_id = input('relation_id');
  31. $list = ExamineRecordModel::where([
  32. 'relation_type' => $relation_type,
  33. 'relation_id' => $relation_id
  34. ])->with('checkStaff')->select();
  35. $this->success('请求成功', $list);
  36. }
  37. //审核
  38. public function examine() {
  39. $relation_type = input('relation_type');
  40. $relation_id = input('relation_id');
  41. $content = input('content');
  42. $status = input('status');
  43. $record = ExamineRecordModel::where([
  44. 'relation_type' => $relation_type,
  45. 'relation_id' => $relation_id,
  46. 'status' => 0,
  47. 'check_staff_id' => $this->auth->id
  48. ])->find();
  49. if (empty($record)) {
  50. $this->error('没有待审核数据');
  51. }
  52. $staff = Staff::info();
  53. Db::startTrans();
  54. try {
  55. $model = new ExamineRecordModel();
  56. if ($model->isUpdate(true)->save([
  57. 'status' => $status,
  58. 'content' => $content,
  59. 'check_time' => time()
  60. ], ['id' => $record['id']]) == false) {
  61. throw new Exception('修改失败');
  62. }
  63. switch ($relation_type) {
  64. case $model::CONSUME_TYPE://费用
  65. $rowModel = new \addons\qingdong\model\Consume();
  66. $row = $rowModel->where(['id' => $relation_id])->find();
  67. break;
  68. case $model::CONTRACT_TYPE://合同
  69. $rowModel = new \addons\qingdong\model\Contract();
  70. $row = $rowModel->where(['id' => $relation_id])->find();
  71. break;
  72. case $model::RECEIVABLES_TYPE://回款
  73. $rowModel = new \addons\qingdong\model\Receivables();
  74. $row = $rowModel->where(['id' => $relation_id])->find();
  75. break;
  76. case $model::ACHIEVEMENT_TYPE://业绩目标
  77. $rowModel = new \addons\qingdong\model\AchievementRecords();
  78. $row = $rowModel->where(['id' => $relation_id])->find()->toArray();
  79. break;
  80. case $model::APPROVAL_TYPE://审批
  81. $rowModel = new \addons\qingdong\model\Approval();
  82. $row = $rowModel->where(['id' => $relation_id])->find()->toArray();
  83. $row['staff_id'] = $row['create_staff_id'];
  84. break;
  85. case $model::CARD_TYPE://补卡审核
  86. $rowModel = new \addons\qingdong\model\AttendanceCard();
  87. $row = $rowModel->where(['id' => $relation_id])->find()->toArray();
  88. $row['staff_id'] = $row['create_staff_id'];
  89. break;
  90. case $model::LEAVE_TYPE://请假
  91. $rowModel = new \addons\qingdong\model\Leave();
  92. $row = $rowModel->where(['id' => $relation_id])->find()->toArray();
  93. $row['staff_id'] = $row['create_staff_id'];
  94. break;
  95. default:
  96. throw new Exception('参数错误');
  97. }
  98. if ($message = Message::where(['relation_type' => 'examine', 'relation_id' => $record['id'],'to_staff_id'=>$this->auth->id])->find()) {
  99. Message::where(['id' => $message['id']])->update(['status' => 1, 'read_time' => time()]);
  100. }
  101. $check_staff_ids = explode(',', trim($row['check_staff_ids'], ','));
  102. $check_staff_ids[] = $staff->id;
  103. if ($status == 1) {//审核通过
  104. $flow= Flow::getstepdetail($relation_type,$relation_id);
  105. //给下一审核人发送通知
  106. $result=Flow::sendStepRecord($flow,$relation_type, $relation_id,$check_staff_ids,$staff->id);
  107. //已完成审核
  108. if ($result['status']== true) {
  109. $rowModel->save([
  110. 'check_status' => 2,
  111. 'check_staff_ids' => implode(',', $check_staff_ids),
  112. 'order_id'=>$result['order_id']
  113. ], ['id' => $relation_id]);
  114. switch ($relation_type) {
  115. case $model::CONTRACT_TYPE://合同
  116. \addons\qingdong\model\Customer::where(['id'=>$row['customer_id']])->update(['contract_status'=>1]);
  117. //合同签署成功后商机变为赢单
  118. if($row['business_id']){
  119. BusinessStatus::create(array('business_id'=>$row['business_id'],'type'=>4,'remark'=>'合同签署'));
  120. Business::where(array('id'=>$row['business_id']))->update(array('contract_status'=>1,'updatetime'=>time()));
  121. }
  122. break;
  123. case $model::ACHIEVEMENT_TYPE://业绩目标
  124. $m=new \addons\qingdong\model\Achievement();
  125. $m->where(['type'=>$row['type'],'status'=>$row['status'],'obj_id'=>$row['obj_id']])->delete();
  126. unset($row['id']);
  127. unset($row['createtime']);
  128. unset($row['updatetime']);
  129. unset($row['deletetime']);
  130. $m->allowField(true)->save($row);
  131. $row['staff_id']=$row['obj_id'];
  132. break;
  133. case $model::RECEIVABLES_TYPE://回款
  134. $m=new \addons\qingdong\model\Contract();
  135. $contract=$m->where(['id'=>$row['contract_id']])->with(['receivables'])->find();
  136. if($contract['receivables']['repayment_money'] >= $contract['money']){
  137. $m->save(['contract_status'=>1],['id'=>$row['contract_id']]);//已完成
  138. }
  139. break;
  140. case $model::CARD_TYPE://补卡
  141. if($row['type'] == 1){//早退
  142. $name='clock_out_status';
  143. $name_time='clock_out';
  144. }else{//迟到
  145. $name='clock_in_status';
  146. $name_time='clock_in';
  147. }
  148. AttendanceStatisc::where(['id'=>$row['statisc_id']])->update([$name=>5,$name_time=>$row['time']]);
  149. break;
  150. case $model::LEAVE_TYPE://请假
  151. break;
  152. }
  153. Message::addMessage(Message::EXAMINE_ADOPT_TYPE,$record['id'],$row['owner_staff_id']?? $row['staff_id'],$staff->id);
  154. //删除 或签的待审批通知
  155. Message::setRead(Message::EXAMINE_TYPE,$record['id']);
  156. ExamineRecordModel::where([
  157. 'relation_type' => $relation_type,
  158. 'relation_id' => $relation_id,
  159. 'status' => 0,
  160. ])->update(['status'=>3]);
  161. } else {
  162. $rowModel->save([
  163. 'check_staff_ids' => implode(',', $check_staff_ids),
  164. 'order_id'=>$result['order_id']
  165. ], ['id' => $relation_id]);
  166. }
  167. } else {
  168. //审核未通过
  169. $rowModel->save(['check_status' => 3, 'check_staff_ids' => ''], ['id' => $relation_id]);
  170. Message::addMessage(Message::EXAMINE_REFUSE_TYPE,$record['id'],$row['owner_staff_id']?? $row['staff_id'],$staff->id,'');
  171. //删除待审批通知
  172. $ids = ExamineRecordModel::where([
  173. 'relation_type' => $relation_type,
  174. 'relation_id' => $relation_id,
  175. 'status' => 0,
  176. ])->column('id');
  177. Message::where(['relation_type' => Message::EXAMINE_TYPE, 'relation_id' => ['in', $ids], 'status' => 0])->update(['read_time' => time(), 'status' => 1]);
  178. ExamineRecordModel::where([
  179. 'relation_type' => $relation_type,
  180. 'relation_id' => $relation_id,
  181. 'status' => 0,
  182. ])->update(['status'=>3]);
  183. }
  184. Db::commit();
  185. } catch (Exception $e) {
  186. Db::rollback();
  187. Log::record($e->getMessage());
  188. $this->error($e->getMessage());
  189. }
  190. $this->success('审核成功');
  191. }
  192. /**
  193. * 获取审批列表
  194. */
  195. public function get_examine_list()
  196. {
  197. $limit = input("limit/d", 10);
  198. //合同审批,回款审批、业绩目标审批、办公审批 费用
  199. $relation_type = input('type');
  200. $status = input('status',0);
  201. if($status == 1){
  202. $status = array('in','1,2');
  203. }
  204. $data = ExamineRecordModel::where(['relation_type' => $relation_type,
  205. 'status' => $status,
  206. 'check_staff_id' => $this->auth->id])->order('id desc')->paginate($limit)->toArray();
  207. if($data['data']){
  208. foreach($data['data'] as $k=>$v){
  209. $data['data'][$k]['content_info'] = Message::where(array('relation_id'=>$v['id'],'relation_type'=>'examine'))->value('content');
  210. if ($v['relation_type'] == ExamineRecordModel::CONSUME_TYPE) {
  211. $data['data'][$k]['relation_name'] = Consume::where(['id' => $v['relation_id']])->value('consume_type');
  212. } elseif ($v['relation_type'] == ExamineRecordModel::CONTRACT_TYPE) {
  213. $data['data'][$k]['relation_name'] = Contract::where(['id' => $v['relation_id']])->value('name');
  214. } elseif ($v['relation_type'] == ExamineRecordModel::RECEIVABLES_TYPE) {
  215. $data['data'][$k]['relation_name'] = Receivables::where(['id' => $v['relation_id']])->value('number');
  216. } elseif($v['relation_type'] == ExamineRecordModel::ACHIEVEMENT_TYPE) {
  217. $data['data'][$k]['relation_name'] = $acchieve = AchievementRecords::where(['id' => $v['relation_id']])->value('year');;
  218. } elseif($v['relation_type'] == ExamineRecordModel::APPROVAL_TYPE) {
  219. $approval = Approval::where(['id' => $v['relation_id']])->value('formapproval_id');
  220. $data['data'][$k]['relation_name'] = '';
  221. if($approval){
  222. $data['data'][$k]['relation_name'] = FormApproval::where(['id'=>$approval])->value('name');
  223. }
  224. }elseif($v['relation_type'] == ExamineRecordModel::CARD_TYPE) {
  225. $data['data'][$k]['relation_name'] = '补卡';
  226. }elseif($v['relation_type'] == ExamineRecordModel::LEAVE_TYPE) {
  227. $data['data'][$k]['relation_name'] = '请假';
  228. }
  229. else{
  230. $data['data'][$k]['relation_name']='';
  231. }
  232. }
  233. }
  234. $this->success('请求成功', $data);
  235. }
  236. }