ReceivablesPlan.php 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. $param['user_id'] = $userInfo['id'];
  60. $res = $receivablesPlanModel->createData($param);
  61. if ($res) {
  62. return resultArray(['data' => '添加成功']);
  63. } else {
  64. return resultArray(['error' => $receivablesPlanModel->getError()]);
  65. }
  66. }
  67. /**
  68. * 回款计划详情
  69. * @author Michael_xu
  70. * @param
  71. * @return
  72. */
  73. public function read()
  74. {
  75. $receivablesPlanModel = model('ReceivablesPlan');
  76. $param = $this->param;
  77. $data = $receivablesPlanModel->getDataById($param['id']);
  78. if (!$data) {
  79. return resultArray(['error' => $receivablesPlanModel->getError()]);
  80. }
  81. return resultArray(['data' => $data]);
  82. }
  83. /**
  84. * 编辑回款计划
  85. * @author Michael_xu
  86. * @param
  87. * @return
  88. */
  89. public function update()
  90. {
  91. $receivablesPlanModel = model('ReceivablesPlan');
  92. $userModel = new \app\admin\model\User();
  93. $param = $this->param;
  94. $userInfo = $this->userInfo;
  95. $plan_id = $param['id'];
  96. $dataInfo = db('crm_receivables_plan')->where(['plan_id' => $plan_id])->find();
  97. //根据合同权限判断
  98. $contractData = db('crm_contract')->where(['contract_id' => $dataInfo['contract_id']])->find();
  99. $auth_user_ids = $userModel->getUserByPer('crm', 'contract', 'update');
  100. //读写权限
  101. $rwPre = $userModel->rwPre($userInfo['id'], $contractData['ro_user_id'], $contractData['rw_user_id'], 'update');
  102. if (!in_array($contractData['owner_user_id'],$auth_user_ids) && !$rwPre) {
  103. header('Content-Type:application/json; charset=utf-8');
  104. exit(json_encode(['code'=>102,'error'=>'无权操作']));
  105. }
  106. $param['user_id'] = $userInfo['id'];
  107. $res = $receivablesPlanModel->updateDataById($param, $param['id']);
  108. if ($res) {
  109. return resultArray(['data' => '编辑成功']);
  110. } else {
  111. return resultArray(['error' => $receivablesPlanModel->getError()]);
  112. }
  113. }
  114. /**
  115. * 删除回款计划
  116. * @author Michael_xu
  117. * @param
  118. * @return
  119. */
  120. public function delete()
  121. {
  122. $userModel = new \app\admin\model\User();
  123. $param = $this->param;
  124. $userInfo = $this->userInfo;
  125. $plan_id = $param['id'];
  126. if ($plan_id) {
  127. $dataInfo = db('crm_receivables_plan')->where(['plan_id' => $plan_id])->find();
  128. if (!$dataInfo) {
  129. return resultArray(['error' => '数据不存在或已删除']);
  130. }
  131. // $receivablesInfo = db('crm_receivables')->where(['receivables_id' => $dataInfo['receivables_id']])->find();
  132. // if ($receivablesInfo) {
  133. // return resultArray(['error' => '已关联回款《'.$receivablesInfo['number'].'》,不能删除']);
  134. // }
  135. //根据合同权限判断
  136. $contractData = db('crm_contract')->where(['contract_id' => $dataInfo['contract_id']])->find();
  137. $auth_user_ids = $userModel->getUserByPer('crm', 'contract', 'delete');
  138. //读写权限
  139. $rwPre = $userModel->rwPre($userInfo['id'], $contractData['ro_user_id'], $contractData['rw_user_id'], 'update');
  140. if (!in_array($contractData['owner_user_id'],$auth_user_ids) && !$rwPre) {
  141. header('Content-Type:application/json; charset=utf-8');
  142. exit(json_encode(['code'=>102,'error'=>'无权操作']));
  143. }
  144. $res = model('ReceivablesPlan')->delDataById($plan_id);
  145. if (!$res) {
  146. return resultArray(['error' => model('ReceivablesPlan')->getError()]);
  147. }
  148. // 删除回款计划扩展数据
  149. db('crm_receivables_plan_data')->where('plan_id', $plan_id)->delete();
  150. return resultArray(['data' => '删除成功']);
  151. } else {
  152. return resultArray(['error'=>'参数错误']);
  153. }
  154. }
  155. /**
  156. * 回款计划导出
  157. * @param
  158. * @return
  159. * @author Michael_xu
  160. */
  161. public function excelExport()
  162. {
  163. $param = $this->param;
  164. $userInfo = $this->userInfo;
  165. $param['user_id'] = $userInfo['id'];
  166. $action_name = '导出全部';
  167. if ($param['plan_id']) {
  168. $param['plan_id'] = ['condition' => 'in', 'value' => $param['plan_id'], 'form_type' => 'text', 'name' => ''];
  169. $action_name='导出选中';
  170. }
  171. $excelModel = new \app\admin\model\Excel();
  172. // 导出的字段列表
  173. $fieldModel = new \app\admin\model\Field();
  174. $field_list = $fieldModel->getIndexFieldConfig('crm_receivables_plan', $userInfo['id'],'','excel');
  175. // 文件名
  176. $file_name = '5kcrm_receivables_plan_' . date('Ymd');
  177. $model = model('ReceivablesPlan');
  178. $temp_file = $param['temp_file'];
  179. unset($param['temp_file']);
  180. $page = $param['page'] ?: 1;
  181. unset($param['page']);
  182. unset($param['export_queue_index']);
  183. // RecordActionLog($userInfo['id'],'crm_receivables_plan','excelexport',$action_name,'','','导出回款');
  184. return $excelModel->batchExportCsv($file_name, $temp_file, $field_list, $page, function ($page, $limit) use ($model, $param, $field_list) {
  185. $param['page'] = $page;
  186. $param['limit'] = $limit;
  187. $data = $model->getDataList($param);
  188. $data['list'] = $model->exportHandle($data['list'], $field_list, 'ReceivablesPlan');
  189. return $data;
  190. });
  191. }
  192. }