口腔客户管理系统
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <?php
  2. namespace app\admin\model;
  3. use think\Model;
  4. class Collection extends Model
  5. {
  6. //收款记录
  7. public function getAll($param, $page, $pageSize)
  8. {
  9. unset($param['page']);
  10. unset($param['pageSize']);
  11. foreach ($param as $k => $v) {
  12. if ($param[$k] === '' || $param[$k] === null) {
  13. unset($param[$k]);
  14. }
  15. }
  16. $where = [];
  17. if (isset($param['type'])) {
  18. $where[] = ['c.type', '=', $param['type']];
  19. }
  20. if (isset($param['demand'])) {
  21. $where[] = ['c.demand', 'like', '%' . $param['demand'] . '%'];
  22. }
  23. if (isset($param['adcode'])) {
  24. $where[] = ['t.adcode', '=', $param['adcode']];
  25. }
  26. if (isset($param['hospital_id'])) {
  27. $where[] = ['a.hospital_id', '=', $param['hospital_id']];
  28. }
  29. if (isset($param['user_id'])) {
  30. $where[] = ['t.user_id', '=', $param['user_id']];
  31. }
  32. if (isset($param['customer_id'])) {
  33. $where[] = ['c.customer_id', '=', $param['customer_id']];
  34. }
  35. if (isset($param['city_id'])) {
  36. $where[] = ['t.city_id', '=', $param['city_id']];
  37. }
  38. if (isset($param['keyword'])) {
  39. $where[] = ['t.name|t.phone', 'like', '%' . $param['keyword'] . '%'];
  40. }
  41. if (isset($param['source'])) {
  42. $where[] = ['t.source', '=', $param['source']];
  43. }
  44. if (isset($param['signup_time_start'])) {
  45. $where[] = ['t.signup_time', 'between', [$param['signup_time_start'], $param['signup_time_end']]];
  46. }
  47. if (isset($param['collection_time_start'])) {
  48. $where[] = ['c.collection_time', 'between', [$param['collection_time_start'], $param['collection_time_end']]];
  49. }
  50. if (isset($param['appoint_time_start'])) {
  51. $where[] = ['a.appoint_time', 'between', [$param['appoint_time_start'], $param['appoint_time_end']]];
  52. }
  53. $Appoint_model = new \app\admin\model\Appoint();
  54. $Customer_model = new \app\admin\model\Customer();
  55. $result = $this->alias('c')->join($Appoint_model->getTable() . ' a', 'c.appoint_id = a.id and a.is_deal = 1')->join($Customer_model->getTable() . ' t', 'a.customer_id = t.id')->field('c.customer_id,t.name,t.sex,t.city_id,t.phone,FROM_UNIXTIME(a.appoint_time) as appoint_time,c.demand,c.type,a.hospital_id,c.deal_money,c.newdeal_money,c.collection_money,FROM_UNIXTIME(c.collection_time) as collection_time,c.refunddeal_money,c.refundcollection_money,c.feedback,c.remark,t.user_id,c.id')->limit($page, $pageSize)->with(['getHospitalOneData', 'getUserOneData', 'getDistrictOneData', 'getExamineOneData'])->where($where)->select()->toArray();
  56. $total = $this->alias('c')->join($Appoint_model->getTable() . ' a', 'c.appoint_id = a.id and a.is_deal = 1')->join($Customer_model->getTable() . ' t', 'a.customer_id = t.id')->where($where)->count();
  57. foreach ($result as $k => $v) {
  58. $result[$k]['demand'] = array_filter(explode(',', $v['demand']));
  59. $result[$k]['collection_money'] = $v['collection_money'] / 100;
  60. $result[$k]['deal_money'] = $v['deal_money'] / 100;
  61. //0=全款,1=收尾款,2=新增成交,3=退款
  62. if ($v['type'] == 2) {
  63. $result[$k]['deal_money'] = $v['newdeal_money'] / 100;
  64. }
  65. if ($v['type'] == 3) {
  66. $result[$k]['collection_money'] = $v['refundcollection_money'] / 100;
  67. $result[$k]['deal_money'] = $v['refunddeal_money'] / 100;
  68. }
  69. $result[$k]['examine_collection_money'] = $v['examine_collection_money'] / 100;
  70. $result[$k]['examine_deal_money'] = $v['examine_deal_money'] / 100;
  71. $result[$k]['examine_status'] = (string)$v['examine_status'];
  72. }
  73. return ['rows' => $result, 'total' => $total];
  74. }
  75. //成交记录
  76. public function getDealAll($param, $page, $pageSize)
  77. {
  78. unset($param['page']);
  79. unset($param['pageSize']);
  80. foreach ($param as $k => $v) {
  81. if ($param[$k] === '' || $param[$k] === null) {
  82. unset($param[$k]);
  83. }
  84. }
  85. $where = [];
  86. if (isset($param['collection'])) {
  87. $where[] = ['a.collection', '=', $param['collection']];
  88. }
  89. if (isset($param['demand'])) {
  90. $where[] = ['c.demand', 'like', '%' . $param['demand'] . '%'];
  91. }
  92. if (isset($param['adcode'])) {
  93. $where[] = ['t.adcode', '=', $param['adcode']];
  94. }
  95. if (isset($param['hospital_id'])) {
  96. $where[] = ['a.hospital_id', '=', $param['hospital_id']];
  97. }
  98. if (isset($param['user_id'])) {
  99. $where[] = ['t.user_id', '=', $param['user_id']];
  100. }
  101. if (isset($param['city_id'])) {
  102. $where[] = ['t.city_id', '=', $param['city_id']];
  103. }
  104. if (isset($param['keyword'])) {
  105. $where[] = ['t.name|t.phone', 'like', '%' . $param['keyword'] . '%'];
  106. }
  107. if (isset($param['source'])) {
  108. $where[] = ['t.source', '=', $param['source']];
  109. }
  110. if (isset($param['signup_time_start'])) {
  111. $where[] = ['t.signup_time', 'between', [$param['signup_time_start'], $param['signup_time_end']]];
  112. }
  113. if (isset($param['collection_time_start'])) {
  114. $where[] = ['c.collection_time', 'between', [$param['collection_time_start'], $param['collection_time_end']]];
  115. }
  116. if (isset($param['appoint_time_start'])) {
  117. $where[] = ['a.appoint_time', 'between', [$param['appoint_time_start'], $param['appoint_time_end']]];
  118. }
  119. $Appoint_model = new \app\admin\model\Appoint();
  120. $Customer_model = new \app\admin\model\Customer();
  121. $result = $this->alias('c')->join($Appoint_model->getTable() . ' a', 'c.appoint_id = a.id and a.is_deal = 1')->join($Customer_model->getTable() . ' t', 'a.customer_id = t.id')->field('c.customer_id,t.name,t.sex,t.city_id,t.phone,FROM_UNIXTIME(a.appoint_time) as appoint_time,c.demand,c.type,a.hospital_id,a.collection,FROM_UNIXTIME(c.collection_time) as collection_time,c.feedback,c.remark,t.user_id,(sum(c.collection_money)-sum(c.refundcollection_money)) as all_collection_money,(sum(c.deal_money)+sum(c.newdeal_money)-sum(c.refunddeal_money)) as all_deal_money')->limit($page, $pageSize)->with(['getHospitalOneData', 'getUserOneData', 'getDistrictOneData'])->group('c.appoint_id')->where($where)->select()->toArray();
  122. $total = $this->alias('c')->join($Appoint_model->getTable() . ' a', 'c.appoint_id = a.id and a.is_deal = 1')->join($Customer_model->getTable() . ' t', 'a.customer_id = t.id')->where($where)->group('c.appoint_id')->count();
  123. foreach ($result as $k => $v) {
  124. $result[$k]['demand'] = array_filter(explode(',', $v['demand']));
  125. $result[$k]['all_collection_money'] = $v['all_collection_money'] / 100;
  126. $result[$k]['all_deal_money'] = $v['all_deal_money'] / 100;
  127. }
  128. return ['rows' => $result, 'total' => $total];
  129. }
  130. //成交信息
  131. public function getDealDetail($param, $page, $pageSize)
  132. {
  133. unset($param['page']);
  134. unset($param['pageSize']);
  135. foreach ($param as $k => $v) {
  136. if ($param[$k] === '' || $param[$k] === null) {
  137. unset($param[$k]);
  138. }
  139. }
  140. $Appoint_model = new \app\admin\model\Appoint();
  141. $result = $this->alias('c')->join($Appoint_model->getTable() . ' a', 'c.appoint_id = a.id and a.customer_id = ' . $param['customer_id'] . ' and a.is_deal = 1')->field('c.appoint_id,a.city_id,a.collection,a.hospital_id,FROM_UNIXTIME(a.appoint_time) as appoint_time,c.demand,(sum(c.deal_money)+sum(c.newdeal_money)-sum(c.refunddeal_money)) as all_deal_money,FROM_UNIXTIME(c.collection_time) as collection_time,(sum(c.collection_money)-sum(c.refundcollection_money)) as all_collection_money')->group('c.appoint_id')->limit($page, $pageSize)->with(['getHospitalOneData', 'getCollectionManyData', 'getDistrictOneData'])->select()->toArray();
  142. $total = $this->alias('c')->join($Appoint_model->getTable() . ' a', 'c.appoint_id = a.id and a.customer_id = ' . $param['customer_id'] . ' and a.is_deal = 1')->group('c.appoint_id')->count();
  143. foreach ($result as $k => $v) {
  144. $result[$k]['demand'] = array_filter(explode(',', $v['demand']));
  145. $result[$k]['all_collection_money'] = $v['all_collection_money'] / 100;
  146. $result[$k]['all_deal_money'] = $v['all_deal_money'] / 100;
  147. if (isset($v['get_collection_many_data']) && !empty($v['get_collection_many_data'])) {
  148. foreach ($v['get_collection_many_data'] as $kk => $vv) {
  149. $result[$k]['get_collection_many_data'][$kk]['collection_time'] = date('Y-m-d H:i:s', $vv['collection_time']);
  150. //0=全款,1=收尾款,2=新增成交,3=退款
  151. if ($vv['type'] == 2) {
  152. $result[$k]['get_collection_many_data'][$kk]['deal_money'] = $vv['newdeal_money'] / 100;
  153. }
  154. if ($vv['type'] == 3) {
  155. $result[$k]['get_collection_many_data'][$kk]['collection_money'] = $vv['refundcollection_money'] / 100;
  156. $result[$k]['get_collection_many_data'][$kk]['deal_money'] = $vv['refunddeal_money'] / 100;
  157. }
  158. }
  159. }
  160. }
  161. return ['rows' => $result, 'total' => $total];
  162. }
  163. //业绩核算
  164. public function getAchievementAccounting($param, $page, $pageSize)
  165. {
  166. unset($param['page']);
  167. unset($param['pageSize']);
  168. foreach ($param as $k => $v) {
  169. if ($param[$k] === '' || $param[$k] === null) {
  170. unset($param[$k]);
  171. }
  172. }
  173. $where = [];
  174. if (isset($param['user_id'])) {
  175. $where[] = ['t.user_id', '=', $param['user_id']];
  176. }
  177. if (isset($param['collection_time'])) {
  178. $month_end = strtotime(date('Y-m-d', strtotime('last day of', $param['collection_time'])) . ' 23:59:59');
  179. $where[] = ['c.collection_time', 'between', [$param['collection_time'], $month_end]];
  180. } else {
  181. $month_start = strtotime(date('Y-01-01 00:00:00'));
  182. $month_end = mktime(23, 59, 59, date('m'), date('t'), date('Y'));//当月最后一天
  183. $where[] = ['c.collection_time', 'between', [$month_start, $month_end]];
  184. }
  185. $Appoint_model = new \app\admin\model\Appoint();
  186. $Customer_model = new \app\admin\model\Customer();
  187. $result = $this->alias('c')->join($Appoint_model->getTable() . ' a', 'c.appoint_id = a.id and a.is_deal = 1')->join($Customer_model->getTable() . ' t', 'a.customer_id = t.id')->field('c.customer_id,c.appoint_id,t.name,t.sex,t.city_id,t.phone,FROM_UNIXTIME(a.appoint_time) as appoint_time,a.hospital_id,FROM_UNIXTIME(c.collection_time) as collection_time,t.user_id,(sum(c.collection_money)-sum(c.refundcollection_money)) as all_collection_money,(sum(c.deal_money)+sum(c.newdeal_money)-sum(c.refunddeal_money)) as all_deal_money')->limit($page, $pageSize)->with(['getHospitalOneData', 'getUserOneData', 'getDistrictOneData', 'getCollectionManyData2'])->group('c.appoint_id')->where($where)->select()->toArray();
  188. $total = $this->alias('c')->join($Appoint_model->getTable() . ' a', 'c.appoint_id = a.id and a.is_deal = 1')->join($Customer_model->getTable() . ' t', 'a.customer_id = t.id')->where($where)->group('c.appoint_id')->count();
  189. $months = [];
  190. foreach ($result as $k => $v) {
  191. if (isset($v['get_collection_many_data2']) && !empty($v['get_collection_many_data2'])) {
  192. foreach ($v['get_collection_many_data2'] as $kk => $vv) {
  193. $months[$k][$kk] = $vv['months'];
  194. $result[$k][$vv['months']] = $vv['all_collection_money'] / 100;
  195. // $result[$k]['months'][$kk] = $vv['months'];
  196. }
  197. $result[$k]['all_collection_money'] = $v['all_collection_money'] / 100;
  198. $result[$k]['all_deal_money'] = $v['all_deal_money'] / 100;
  199. }
  200. }
  201. //月份降维
  202. $months2 = [];
  203. foreach ($months as $k => $v) {
  204. $months2 = array_merge($months2, $v);//降维
  205. }
  206. $months2 = array_values(array_keys(array_flip($months2)));
  207. return ['rows' => $result, 'total' => $total, 'months' => $months2];
  208. }
  209. public function getCollectionManyData2()
  210. {
  211. return $this->hasMany(\app\admin\model\Collection::class, 'appoint_id', 'appoint_id')->field('(sum(collection_money)-sum(refundcollection_money)) as all_collection_money,(sum(deal_money)+sum(newdeal_money)-sum(refunddeal_money)) as all_deal_money,month(from_unixtime(collection_time)) as months,appoint_id')->group('appoint_id,months');
  212. }
  213. public function getDistrictOneData()
  214. {
  215. return $this->hasOne(\app\admin\model\District::class, 'adcode', 'city_id')->bind(['city_name' => 'name', 'province_id' => 'parent_id']);
  216. }
  217. public function getExamineOneData()
  218. {
  219. return $this->hasOne(\app\admin\model\Examine::class, 'collection_id', 'id')->bind(['examine_deal_money' => 'deal_money', 'examine_collection_money' => 'collection_money', 'examine_status' => 'status', 'examine_reason' => 'reason']);
  220. }
  221. public function getUserOneData()
  222. {
  223. return $this->hasOne(\app\admin\model\User::class, 'id', 'user_id')->bind(['username' => 'username']);
  224. }
  225. public function getHospitalOneData()
  226. {
  227. return $this->hasOne(\app\admin\model\Hospital::class, 'id', 'hospital_id')->bind(['hospital_name' => 'name']);
  228. }
  229. public function getCollectionManyData()
  230. {
  231. return $this->hasMany(\app\admin\model\Collection::class, 'appoint_id', 'appoint_id');
  232. }
  233. public function getCustomerOneData()
  234. {
  235. return $this->hasOne(\app\admin\model\Customer::class, 'id', 'customer_id')->bind(['customer_name' => 'name']);
  236. }
  237. public function getAppointOneData()
  238. {
  239. return $this->hasOne(\app\admin\model\Appoint::class, 'id', 'appoint_id')->bind(['hospital_id' => 'hospital_id', 'appoint_time' => 'appoint_time']);
  240. }
  241. // public function read($param)
  242. // {
  243. // $result = $this->where($this->getPk(), $param[$this->getPk()])->find();
  244. // if (isset($result[$this->getPk()])) {
  245. // return $result->toArray();
  246. // } else {
  247. // return [];
  248. // }
  249. // }
  250. }