ReceivablesPlan.php 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Description: 回款计划计划
  4. // +----------------------------------------------------------------------
  5. // | Author: Michael_xu | gengxiaoxu@5kcrm.com
  6. // +----------------------------------------------------------------------
  7. namespace app\crm\model;
  8. use think\Db;
  9. use app\admin\model\Common;
  10. use app\crm\model\Contract as ContractModel;
  11. use think\Request;
  12. use think\Validate;
  13. class ReceivablesPlan extends Common
  14. {
  15. /**
  16. * 为了数据库的整洁,同时又不影响Model和Controller的名称
  17. * 我们约定每个模块的数据表都加上相同的前缀,比如CRM模块用crm作为数据表前缀
  18. */
  19. protected $name = 'crm_receivables_plan';
  20. protected $createTime = 'create_time';
  21. protected $updateTime = 'update_time';
  22. protected $autoWriteTimestamp = true;
  23. /**
  24. * [getDataList 回款计划list]
  25. * @author Michael_xu
  26. * @param [string] $map [查询条件]
  27. * @param [number] $page [当前页数]
  28. * @param [number] $limit [每页数量]
  29. * @param [string] $types 1 未使用的回款计划
  30. * @return [array] [description]
  31. */
  32. public function getDataList($request)
  33. {
  34. $userModel = new \app\admin\model\User();
  35. $search = $request['search'];
  36. $user_id = $request['user_id'];
  37. $scene_id = (int)$request['scene_id'];
  38. $check_status = $request['check_status'];
  39. $types = $request['types'];
  40. $getCount = $request['getCount'];
  41. $status = isset($request['status']) ? $request['status'] : 1;
  42. $dealt = $request['dealt']; # 待办事项
  43. unset($request['scene_id']);
  44. unset($request['search']);
  45. unset($request['user_id']);
  46. unset($request['check_status']);
  47. unset($request['types']);
  48. unset($request['getCount']);
  49. unset($request['status']);
  50. unset($request['dealt']);
  51. $request = $this->fmtRequest( $request );
  52. $map = $request['map'] ? : [];
  53. if (isset($map['search'])) {
  54. //普通筛选
  55. $map['name'] = ['like', '%'.$map['search'].'%'];
  56. unset($map['search']);
  57. } else {
  58. $map = where_arr($map, 'crm', 'receivables_plan', 'index'); //高级筛选
  59. }
  60. if ($map['receivables_plan.owner_user_id']) {
  61. $map['contract.owner_user_id'] = $map['receivables_plan.owner_user_id'];
  62. unset($map['receivables_plan.owner_user_id']);
  63. }
  64. $whereData = [];
  65. if ($check_status) {
  66. unset($map['receivables_plan.check_status']);
  67. if ($check_status == 2) {
  68. $map['receivables.check_status'] = $check_status;
  69. } else {
  70. unset($map['receivables_plan.receivables_id']);
  71. $data = [];
  72. $data['check_status'] = $check_status;
  73. $whereData = function($query) use ($data){
  74. $query->where(['receivables_plan.receivables_id'=> ['eq',0]])
  75. ->whereOr(['receivables.check_status' => $data['check_status']]);
  76. };
  77. }
  78. }
  79. // @ymob 2019-12-11 17:51:54
  80. // 修改回款时,回款计划选项列表应该包含该回款对应的回款计划 不能过滤
  81. // 将types改为status,status:可用的回款计划 fanqi
  82. if (empty($dealt)) { # 不是待办事项
  83. if ($request['map']['receivables_id']) {
  84. if (!empty($request['map']['contract_id'])) {
  85. $map = "
  86. (`receivables_plan`.`contract_id` = {$request['map']['contract_id']} AND `receivables_plan`.`receivables_id` = {$request['map']['receivables_id']})
  87. OR
  88. (`receivables_plan`.`contract_id` = {$request['map']['contract_id']} AND `receivables_plan`.`receivables_id` = 0)
  89. ";
  90. } else {
  91. $map = " (`receivables_plan`.`receivables_id` = 0 )";
  92. }
  93. } elseif ($status == 0) {
  94. $map['receivables_plan.receivables_id'] = 0;
  95. }
  96. }
  97. # 待办事项-待回款提醒-已回款
  98. if (!empty($dealt)) {
  99. $map = " (`receivables_plan`.`receivables_id` > ".$request['map']['receivables_id'][1]." )";
  100. }
  101. $dataCount = db('crm_receivables_plan')
  102. ->alias('receivables_plan')
  103. ->join('__CRM_CONTRACT__ contract','receivables_plan.contract_id = contract.contract_id','LEFT')
  104. ->join('__CRM_CUSTOMER__ customer','receivables_plan.customer_id = customer.customer_id','LEFT')
  105. ->join('__CRM_RECEIVABLES__ receivables','receivables_plan.plan_id = receivables.plan_id','LEFT')
  106. ->where($map)
  107. ->where($whereData)
  108. ->count('receivables_plan.plan_id');
  109. if (!empty($getCount) && $getCount == 1) {
  110. $data['dataCount'] = !empty($dataCount) ? $dataCount : 0;
  111. return $data;
  112. }
  113. $list = db('crm_receivables_plan')
  114. ->alias('receivables_plan')
  115. ->join('__CRM_CONTRACT__ contract','receivables_plan.contract_id = contract.contract_id','LEFT')
  116. ->join('__CRM_CUSTOMER__ customer','receivables_plan.customer_id = customer.customer_id','LEFT')
  117. ->join('__CRM_RECEIVABLES__ receivables','receivables_plan.plan_id = receivables.plan_id','LEFT')
  118. ->limit($request['offset'], $request['length'])
  119. ->field('receivables_plan.*,customer.name as customer_name,contract.num as contract_name,receivables.receivables_id,receivables.check_status')
  120. ->where($map)
  121. ->where($whereData)
  122. ->select();
  123. foreach ($list as $k=>$v) {
  124. $list[$k]['create_user_id_info'] = $userModel->getUserById($v['create_user_id']);
  125. $list[$k]['contract_id_info']['name'] = $v['contract_name'] ? : '';
  126. $list[$k]['contract_id_info']['contract_id'] = $v['contract_id'] ? : '';
  127. $list[$k]['customer_id_info']['name'] = $v['customer_name'] ? : '';
  128. $list[$k]['customer_id_info']['customer_id'] = $v['customer_id'] ? : '';
  129. }
  130. $data = [];
  131. $data['list'] = $list;
  132. $data['dataCount'] = $dataCount ? : 0;
  133. return $data ? : [];
  134. }
  135. /**
  136. * 创建回款计划信息
  137. * @author Michael_xu
  138. * @param
  139. * @return
  140. */
  141. public function createData($param)
  142. {
  143. if (!$param['contract_id']) {
  144. $this->error = '请先选择合同';
  145. return false;
  146. } else {
  147. $res = ContractModel::where(['contract_id' => $param['contract_id']])->value('check_status');
  148. if (6 == $res) {
  149. $this->error = '合同已作废';
  150. return false;
  151. }
  152. if (!in_array($res,['2'])) {
  153. $this->error = '当前合同未审核通过,不能添加回款';
  154. return false;
  155. }
  156. }
  157. if ($param['remind'] > 90) {
  158. $this->error = '提前提醒最大时间为 90 天';
  159. return false;
  160. }
  161. // 自动验证
  162. $validate = validate($this->name);
  163. if (!$validate->check($param)) {
  164. $this->error = $validate->getError();
  165. return false;
  166. }
  167. if ($param['file_ids']) $param['file'] = arrayToString($param['file_ids']); //附件
  168. //期数规则(1,2,3..)
  169. $maxNum = db('crm_receivables_plan')->where(['contract_id' => $param['contract_id']])->max('num');
  170. $param['num'] = $maxNum ? $maxNum+1 : 1;
  171. //提醒日期
  172. $param['remind_date'] = $param['remind'] ? date('Y-m-d',strtotime($param['return_date'])-86400*$param['remind']) : $param['return_date'];
  173. if ($this->data($param)->allowField(true)->save()) {
  174. $data = [];
  175. $data['plan_id'] = $this->plan_id;
  176. return $data;
  177. } else {
  178. $this->error = '添加失败';
  179. return false;
  180. }
  181. }
  182. /**
  183. * 编辑回款计划
  184. * @author Michael_xu
  185. * @param
  186. * @return
  187. */
  188. public function updateDataById($param, $plan_id = '')
  189. {
  190. $dataInfo = $this->getDataById($plan_id);
  191. if (!$dataInfo) {
  192. $this->error = '数据不存在或已删除';
  193. return false;
  194. }
  195. $param['plan_id'] = $plan_id;
  196. //过滤不能修改的字段
  197. $unUpdateField = ['num','create_user_id','is_deleted','delete_time','delete_user_id'];
  198. foreach ($unUpdateField as $v) {
  199. unset($param[$v]);
  200. }
  201. // 自动验证
  202. $validate = validate($this->name);
  203. if (!$validate->check($param)) {
  204. $this->error = $validate->getError();
  205. return false;
  206. }
  207. if ($param['file_ids']) $param['file'] = arrayToString($param['file_ids']); //附件
  208. //提醒日期
  209. $param['remind_date'] = $param['remind'] ? date('Y-m-d',strtotime($param['return_date'])-86400*$param['remind']) : $param['return_date'];
  210. if ($this->allowField(true)->save($param, ['plan_id' => $plan_id])) {
  211. $data = [];
  212. $data['plan_id'] = $plan_id;
  213. return $data;
  214. } else {
  215. $this->error = '编辑失败';
  216. return false;
  217. }
  218. }
  219. /**
  220. * 回款计划数据
  221. * @param $id 回款计划ID
  222. * @return
  223. */
  224. public function getDataById($id = '')
  225. {
  226. $map['plan_id'] = $id;
  227. $dataInfo = $this->where($map)->find();
  228. if (!$dataInfo) {
  229. $this->error = '暂无此数据';
  230. return false;
  231. }
  232. $userModel = new \app\admin\model\User();
  233. $dataInfo['create_user_info'] = $userModel->getUserById($dataInfo['create_user_id']);
  234. $dataInfo['plan_id'] = $id;
  235. return $dataInfo;
  236. }
  237. //模拟自定义字段返回
  238. public function getField()
  239. {
  240. $field_arr = [
  241. '0' => [
  242. 'field' => 'customer_id',
  243. 'name' => '客户名称',
  244. 'form_type' => 'customer',
  245. 'setting' => []
  246. ],
  247. '1' => [
  248. 'field' => 'contract_id',
  249. 'name' => '合同名称',
  250. 'form_type' => 'contract',
  251. 'setting' => []
  252. ],
  253. '2' => [
  254. 'field' => 'money',
  255. 'name' => '计划回款金额',
  256. 'form_type' => 'floatnumber',
  257. 'setting' => []
  258. ],
  259. '3' => [
  260. 'field' => 'return_date',
  261. 'name' => '计划回款日期',
  262. 'form_type' => 'date',
  263. 'setting' => []
  264. ],
  265. '4' => [
  266. 'field' => 'return_type',
  267. 'name' => '计划回款方式',
  268. 'form_type' => 'select',
  269. 'setting' => '支付宝\n微信\n转账'
  270. ],
  271. '5' => [
  272. 'field' => 'remind',
  273. 'name' => '提前几日提醒',
  274. 'form_type' => 'number',
  275. 'setting' => []
  276. ],
  277. '6' => [
  278. 'field' => 'remark',
  279. 'name' => '备注',
  280. 'form_type' => 'textarea',
  281. 'setting' => []
  282. ],
  283. '7' => [
  284. 'field' => 'file',
  285. 'name' => '附件',
  286. 'form_type' => 'file',
  287. 'setting' => []
  288. ]
  289. ];
  290. return $field_arr;
  291. }
  292. }