ReceivablesPlan.php 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Description: 回款计划
  4. // +----------------------------------------------------------------------
  5. // | Author: Michael_xu | gengxiaoxu@5kcrm.com
  6. // +----------------------------------------------------------------------
  7. namespace app\crm\controller;
  8. use app\admin\controller\ApiCommon;
  9. use think\Hook;
  10. use think\Request;
  11. class ReceivablesPlan extends ApiCommon
  12. {
  13. /**
  14. * 用于判断权限
  15. * @permission 无限制
  16. * @allow 登录用户可访问
  17. * @other 其他根据系统设置
  18. **/
  19. public function _initialize()
  20. {
  21. $action = [
  22. 'permission'=>[''],
  23. 'allow'=>['index','save','read','update','delete']
  24. ];
  25. Hook::listen('check_auth',$action);
  26. $request = Request::instance();
  27. $a = strtolower($request->action());
  28. if (!in_array($a, $action['permission'])) {
  29. parent::_initialize();
  30. }
  31. }
  32. /**
  33. * 回款计划列表
  34. * @author Michael_xu
  35. * @return
  36. */
  37. public function index()
  38. {
  39. $receivablesPlanModel = model('ReceivablesPlan');
  40. $param = $this->param;
  41. $userInfo = $this->userInfo;
  42. $param['user_id'] = $userInfo['id'];
  43. $data = $receivablesPlanModel->getDataList($param);
  44. return resultArray(['data' => $data]);
  45. }
  46. /**
  47. * 添加回款计划
  48. * @author Michael_xu
  49. * @param
  50. * @return
  51. */
  52. public function save()
  53. {
  54. $receivablesPlanModel = model('ReceivablesPlan');
  55. $param = $this->param;
  56. $userInfo = $this->userInfo;
  57. $param['create_user_id'] = $userInfo['id'];
  58. $param['owner_user_id'] = $userInfo['id'];
  59. $res = $receivablesPlanModel->createData($param);
  60. if ($res) {
  61. return resultArray(['data' => '添加成功']);
  62. } else {
  63. return resultArray(['error' => $receivablesPlanModel->getError()]);
  64. }
  65. }
  66. /**
  67. * 回款计划详情
  68. * @author Michael_xu
  69. * @param
  70. * @return
  71. */
  72. public function read()
  73. {
  74. $receivablesPlanModel = model('ReceivablesPlan');
  75. $param = $this->param;
  76. $data = $receivablesPlanModel->getDataById($param['id']);
  77. if (!$data) {
  78. return resultArray(['error' => $receivablesPlanModel->getError()]);
  79. }
  80. return resultArray(['data' => $data]);
  81. }
  82. /**
  83. * 编辑回款计划
  84. * @author Michael_xu
  85. * @param
  86. * @return
  87. */
  88. public function update()
  89. {
  90. $receivablesPlanModel = model('ReceivablesPlan');
  91. $userModel = new \app\admin\model\User();
  92. $param = $this->param;
  93. $userInfo = $this->userInfo;
  94. $plan_id = $param['id'];
  95. $dataInfo = db('crm_receivables_plan')->where(['plan_id' => $plan_id])->find();
  96. //根据合同权限判断
  97. $contractData = db('crm_contract')->where(['contract_id' => $dataInfo['contract_id']])->find();
  98. $auth_user_ids = $userModel->getUserByPer('crm', 'contract', 'update');
  99. //读写权限
  100. $rwPre = $userModel->rwPre($userInfo['id'], $contractData['ro_user_id'], $contractData['rw_user_id'], 'update');
  101. if (!in_array($contractData['owner_user_id'],$auth_user_ids) && !$rwPre) {
  102. header('Content-Type:application/json; charset=utf-8');
  103. exit(json_encode(['code'=>102,'error'=>'无权操作']));
  104. }
  105. $res = $receivablesPlanModel->updateDataById($param, $param['id']);
  106. if ($res) {
  107. return resultArray(['data' => '编辑成功']);
  108. } else {
  109. return resultArray(['error' => $receivablesPlanModel->getError()]);
  110. }
  111. }
  112. /**
  113. * 删除回款计划
  114. * @author Michael_xu
  115. * @param
  116. * @return
  117. */
  118. public function delete()
  119. {
  120. $userModel = new \app\admin\model\User();
  121. $param = $this->param;
  122. $userInfo = $this->userInfo;
  123. $plan_id = $param['id'];
  124. if ($plan_id) {
  125. $dataInfo = db('crm_receivables_plan')->where(['plan_id' => $plan_id])->find();
  126. if (!$dataInfo) {
  127. return resultArray(['error' => '数据不存在或已删除']);
  128. }
  129. // $receivablesInfo = db('crm_receivables')->where(['receivables_id' => $dataInfo['receivables_id']])->find();
  130. // if ($receivablesInfo) {
  131. // return resultArray(['error' => '已关联回款《'.$receivablesInfo['number'].'》,不能删除']);
  132. // }
  133. //根据合同权限判断
  134. $contractData = db('crm_contract')->where(['contract_id' => $dataInfo['contract_id']])->find();
  135. $auth_user_ids = $userModel->getUserByPer('crm', 'contract', 'delete');
  136. //读写权限
  137. $rwPre = $userModel->rwPre($userInfo['id'], $contractData['ro_user_id'], $contractData['rw_user_id'], 'update');
  138. if (!in_array($contractData['owner_user_id'],$auth_user_ids) && !$rwPre) {
  139. header('Content-Type:application/json; charset=utf-8');
  140. exit(json_encode(['code'=>102,'error'=>'无权操作']));
  141. }
  142. $res = model('ReceivablesPlan')->delDataById($plan_id);
  143. if (!$res) {
  144. return resultArray(['error' => model('ReceivablesPlan')->getError()]);
  145. }
  146. return resultArray(['data' => '删除成功']);
  147. } else {
  148. return resultArray(['error'=>'参数错误']);
  149. }
  150. }
  151. }