Receivables.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <?php
  2. namespace addons\qingdongams\controller;
  3. use addons\qingdongams\model\ExamineRecord;
  4. use addons\qingdongams\model\FormField;
  5. use addons\qingdongams\model\Message;
  6. use addons\qingdongams\model\Receivables as ReceivablesModel;
  7. use addons\qingdongams\model\ReceivablesOther;
  8. use addons\qingdongams\model\Contract;
  9. use addons\qingdongams\model\Staff;
  10. use think\Db;
  11. use think\Exception;
  12. /**
  13. * @desc 操作文档:https://doc.fastadmin.net/qingdongams
  14. * @desc 软件介绍:https://www.fastadmin.net/store/qingdongams.html
  15. * @desc 售后微信:qingdong_crm
  16. */
  17. /**
  18. * 回款详情
  19. */
  20. class Receivables extends StaffApi {
  21. protected $noNeedLogin = [];
  22. protected $noNeedRight = [];
  23. //新增回款
  24. public function addReceivables() {
  25. $params = $this->request->post();
  26. // 表单验证
  27. if (($result = $this->qingdongamsValidate($params,get_class(), 'create')) !== true) {
  28. $this->error($result);
  29. }
  30. if (ReceivablesModel::where(['number' => $params['number'], 'contract_id' => $params['contract_id']])->find()) {
  31. $this->error('回款编号已存在');
  32. }
  33. $contract=Contract::where(['id'=>$params['contract_id']])->find();
  34. if(empty($contract)){
  35. $this->error('合同不存在');
  36. }
  37. if($contract['check_status'] != 2){
  38. $this->error('当前合同未审核通过');
  39. }
  40. $returnWhere['check_status'] = ['in',[0,1,2]];
  41. $returnWhere['contract_id'] = $params['contract_id'];
  42. $returnMoeny = ReceivablesModel::where($returnWhere)->sum('money');
  43. if($returnMoeny >= $contract['money']){
  44. $this->error('当前合同已回款完毕');
  45. }
  46. if($returnMoeny + $params['money'] > $contract['money']){
  47. // $this->error('回款累计总金额已超过合同金额');
  48. }
  49. $result = FormField::checkFields(FormField::RECEIVABLES_TYPE,$params);
  50. if ($result !== true) {
  51. $this->error($result);
  52. }
  53. Db::startTrans();
  54. try {
  55. $params['customer_id']=$contract['customer_id'];
  56. ReceivablesModel::createReceivables($params);
  57. Db::commit();
  58. } catch (Exception $e) {
  59. Db::rollback();
  60. $this->error($e->getMessage());
  61. }
  62. if ($result) {
  63. $this->success('新增回款成功');
  64. }
  65. }
  66. //获取回款列表
  67. public function getList() {
  68. $params = $this->request->post();
  69. $customer_id = input('customer_id');
  70. $contract_id = input('contract_id');
  71. $check_status = input('check_status');
  72. $name = input('name');
  73. $staff_id=input('staff_id');
  74. $createtime=input('createtime');
  75. $limit = input('limit');
  76. $where = [];
  77. //审核状态 1审核中、2审核通过、3审核未通过
  78. if($check_status){
  79. //0待审核、1审核中、2审核通过、3审核未通过、4撤销
  80. if($check_status == 1){
  81. $where['check_status']=['in',[0,1]];
  82. }elseif($check_status == 2){
  83. $where['check_status']=2;
  84. }elseif($check_status == 3){
  85. $where['check_status']=3;
  86. }
  87. }
  88. if($customer_id){
  89. $where['customer_id'] = $customer_id;
  90. }
  91. if($name){
  92. $where['number'] = ['like',"%{$name}%"];
  93. }
  94. if($contract_id){
  95. $where['contract_id'] = $contract_id;
  96. }
  97. if($staff_id){
  98. $where['owner_staff_id'] = $staff_id;
  99. }else{
  100. $where['owner_staff_id'] = ['in', Staff::getMyStaffIds()];
  101. if (isset($params['type']) && $params['type']) {//客户分类
  102. if ($params['type'] == 1) {//我的创建
  103. $where['owner_staff_id'] = $this->auth->id;
  104. } elseif ($params['type'] == 2) {//下属创建
  105. $where['owner_staff_id'] = ['in', Staff::getLowerStaffId()];
  106. }
  107. }
  108. }
  109. if($createtime){
  110. $times = setTimes($createtime, 'time');
  111. $where['createtime'] = ['between', $times];
  112. }
  113. if (isset($params['times']) && $params['times']) {//客户分类
  114. $times = setTimes($params['times'], 'date');
  115. $where['return_time'] = ['between', $times];
  116. }
  117. if(isset($params['id_list'])){//日志 查询id列表
  118. $where=[];
  119. $where['id']=['in',explode(',',$params['id_list'])];
  120. }
  121. $list = ReceivablesModel::where($where)->with(['contract','customer','createStaff'])->order('id desc')->paginate($limit)->toArray();
  122. $data = $list['data'];
  123. $contractId='';
  124. foreach($data as $k=>$v){
  125. $contractId = $v['contract_id'].','.$contractId;
  126. }
  127. $where['check_status']=2;
  128. $remoney = ReceivablesModel::where($where)->sum('money');
  129. $where['check_status']=array('in','0,1');
  130. $inmoney = ReceivablesModel::where($where)->sum('money');
  131. $where['check_status']=array('in','3,4');
  132. $nomoney = ReceivablesModel::where($where)->sum('money');
  133. $moneyinfo['remoney'] = $remoney;//已回款
  134. $moneyinfo['inmoney'] = $inmoney;//回款中
  135. $moneyinfo['nomoney'] = $nomoney;//未回款
  136. $moneyinfo['allmoney'] = Contract::where(['id'=>['in',$contractId]])->sum('money');//总金额
  137. $list['moneyinfo']=$moneyinfo;
  138. $this->success('请求成功', $list);
  139. }
  140. //回款详情
  141. public function getDetail() {
  142. $id = input('id');
  143. $receivables = ReceivablesModel::where(['id' => $id])->with([
  144. 'customer',
  145. 'contract',
  146. 'plan',
  147. 'ownerStaff',
  148. 'createStaff'
  149. ])->find();
  150. if (empty($receivables)) {
  151. $this->error('信息不存在');
  152. }
  153. $receivables=$receivables->toArray();
  154. $receivables['is_operation']=0;
  155. if($receivables['owner_staff_id'] == $this->auth->id){
  156. //是否可以操作
  157. $receivables['is_operation']=1;
  158. }
  159. if ($receivables['check_status'] == 0 || $receivables['check_status'] == 1) {
  160. $receivables['is_examine'] = ExamineRecord::isExaminse(ExamineRecord::RECEIVABLES_TYPE, $id);
  161. } else {
  162. $receivables['is_examine'] = 0;
  163. }
  164. $receivables=ReceivablesOther::getOther($receivables);
  165. //标记通知已读
  166. Message::setRead(Message::RECEIVABLES_TYPE, $id, $this->auth->id);
  167. $this->success('请求成功', $receivables);
  168. }
  169. //撤回审核
  170. public function cancel() {
  171. $id = input('id');
  172. $customer = ReceivablesModel::where(['id' => $id, 'check_status' => 1])->find();
  173. if (empty($customer)) {
  174. $this->error('回款信息不存在');
  175. }
  176. if($customer['owner_staff_id'] != $this->auth->id){
  177. $this->error('只有负责人可以撤回审批');
  178. }
  179. Db::startTrans();
  180. try {
  181. ReceivablesModel::where(['id' => $id])->update(['check_status' => 4]);
  182. ExamineRecord::where([
  183. 'relation_type' => ExamineRecord::RECEIVABLES_TYPE,
  184. 'relation_id' => $id,
  185. 'status' => 0
  186. ])->update(['status' => 3]);
  187. Db::commit();
  188. } catch (Exception $e) {
  189. Db::rollback();;
  190. $this->error($e->getMessage());
  191. }
  192. $this->success('撤回成功');
  193. }
  194. //修改回款
  195. public function editReceivables() {
  196. $id = input('id');
  197. $params = $this->request->post();
  198. $row = ReceivablesModel::where(['id' => $id, 'check_status' => ['in', [3, 4]]])->find();
  199. if (empty($row)) {
  200. $this->error('回款信息不存在');
  201. }
  202. // 表单验证
  203. if (($result = $this->qingdongamsValidate($params,get_class(), 'create')) !== true) {
  204. $this->error($result);
  205. }
  206. $contract=Contract::where(['id'=>$row['contract_id']])->find();
  207. $returnWhere['check_status'] = ['in',[0,1,2]];
  208. $returnWhere['contract_id'] =$row['contract_id'];
  209. $returnMoeny = ReceivablesModel::where($returnWhere)->sum('money');
  210. if($returnMoeny >= $contract['money']){
  211. $this->error('合同已全部回款');
  212. }
  213. if($returnMoeny + $row['money'] > $contract['money']){
  214. // $this->error('回款累计总金额已超过合同金额');
  215. }
  216. $result = FormField::checkFields(FormField::RECEIVABLES_TYPE,$params,$id);
  217. if ($result !== true) {
  218. $this->error($result);
  219. }
  220. Db::startTrans();
  221. try {
  222. $result = ReceivablesModel::updateReceivables($params);
  223. Db::commit();
  224. } catch (Exception $e) {
  225. Db::rollback();
  226. $this->error($e->getMessage());
  227. }
  228. $this->success('修改回款信息成功');
  229. }
  230. //获取回款编号
  231. public function getReceivablesNumber() {
  232. $this->success('请求成功',['number'=>'H' . date('ymd') . rand(100, 999)]);
  233. }
  234. }