Receivables.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. namespace addons\qingdong\controller;
  3. use addons\qingdong\model\ExamineRecord;
  4. use addons\qingdong\model\FormField;
  5. use addons\qingdong\model\Message;
  6. use addons\qingdong\model\Receivables as ReceivablesModel;
  7. use addons\qingdong\model\ReceivablesOther;
  8. use addons\qingdong\model\Contract;
  9. use addons\qingdong\model\Staff;
  10. use think\Db;
  11. use think\Exception;
  12. /**
  13. * 回款详情
  14. */
  15. class Receivables extends StaffApi {
  16. protected $noNeedLogin = [];
  17. protected $noNeedRight = [];
  18. //新增回款
  19. public function addReceivables() {
  20. $params = $this->request->post();
  21. // 表单验证
  22. if (($result = $this->qingdongValidate($params, get_class(), 'create')) !== true) {
  23. $this->error($result);
  24. }
  25. $result = FormField::checkFields(FormField::RECEIVABLES_TYPE, $params);
  26. if ($result !== true) {
  27. $this->error($result);
  28. }
  29. if (ReceivablesModel::where(['number' => $params['number'], 'contract_id' => $params['contract_id']])->find()) {
  30. $this->error('回款编号已存在');
  31. }
  32. $contract=Contract::where(['id'=>$params['contract_id']])->find();
  33. if(empty($contract)){
  34. $this->error('合同不存在');
  35. }
  36. if($contract['check_status'] != 2){
  37. $this->error('当前合同未审核通过');
  38. }
  39. Db::startTrans();
  40. try {
  41. $params['owner_staff_id'] = $contract['owner_staff_id'];
  42. ReceivablesModel::createReceivables($params);
  43. Db::commit();
  44. } catch (Exception $e) {
  45. Db::rollback();
  46. $this->error($e->getMessage());
  47. }
  48. if ($result) {
  49. $this->success('新增回款成功');
  50. }
  51. }
  52. //获取回款列表
  53. public function getList()
  54. {
  55. $customer_id = input('customer_id');
  56. $contract_id = input('contract_id');
  57. $times = input('times');
  58. $staff_id = input('staff_id');
  59. $check_status = input('check_status');
  60. $limit = input('limit');
  61. $type = input('type',0);
  62. $params=input();
  63. $where= FormField::updateWhereField(FormField::RECEIVABLES_TYPE,$params);
  64. if ($customer_id) {
  65. $where['customer_id'] = $customer_id;
  66. }
  67. if ($contract_id) {
  68. $where['contract_id'] = $contract_id;
  69. }
  70. if ($staff_id) {
  71. $where['owner_staff_id'] = $staff_id;
  72. } else {
  73. if ($type == 1) {//我的客户
  74. $where['owner_staff_id'] = $this->auth->id;
  75. } elseif ($type == 2) {//下属负责的客户
  76. $where['owner_staff_id'] = ['in', Staff::getLowerStaffId()];
  77. }else{
  78. $where['owner_staff_id'] = ['in', Staff::getMyStaffIds()];
  79. }
  80. }
  81. if ($times) {//回款日期
  82. $times = explode(',', $times);
  83. $where['return_time'] = ['between', [$times[0], $times[1]]];
  84. }
  85. if ($check_status) {
  86. if ($check_status == 1) {//审核中
  87. $where['check_status'] = ['in', [0, 1]];
  88. } elseif ($check_status == 2) {//审核通过
  89. $where['check_status'] = 2;
  90. } elseif ($check_status == 3) {//其他
  91. $where['check_status'] = ['in', [3, 4]];
  92. }
  93. }
  94. $list = ReceivablesModel::where($where)->with(['contract'])->order('id desc')->paginate($limit)->toArray();
  95. $remoney = ReceivablesModel::where(['owner_staff_id'=>$where['owner_staff_id']])
  96. ->where(array('check_status'=>2))->sum('money');
  97. $inmoney = ReceivablesModel::where(['owner_staff_id'=>$where['owner_staff_id']])
  98. ->where(array('check_status'=>['in',[0,1]]))->sum('money');
  99. $nomoney = ReceivablesModel::where(['owner_staff_id'=>$where['owner_staff_id']])
  100. ->where(array('check_status'=>['in',[3,4]]))->sum('money');
  101. $moneyinfo['remoney'] = $remoney;//已回款
  102. $moneyinfo['inmoney'] = $inmoney;//回款中
  103. $moneyinfo['nomoney'] = $nomoney;//未回款
  104. $moneyinfo['allmoney'] = $remoney+$inmoney+$nomoney;//总金额
  105. $list['moneyinfo']=$moneyinfo;
  106. $this->success('请求成功',$list);
  107. }
  108. //回款详情
  109. public function getDetail() {
  110. $id = input('id');
  111. $receivables = ReceivablesModel::where(['id' => $id])->with([
  112. 'customer',
  113. 'contract',
  114. 'plan',
  115. 'ownerStaff',
  116. 'createStaff'
  117. ])->find();
  118. if (empty($receivables)) {
  119. //标记通知已读
  120. Message::setRead(Message::RECEIVABLES_TYPE, $id, $this->auth->id);
  121. $this->error('信息不存在');
  122. }
  123. $receivables=$receivables->toArray();
  124. $receivables=ReceivablesOther::getOther($receivables);
  125. //标记通知已读
  126. Message::setRead(Message::RECEIVABLES_TYPE, $id, $this->auth->id);
  127. $this->success('请求成功', $receivables);
  128. }
  129. //撤回审核
  130. public function cancel() {
  131. $id = input('id');
  132. $customer = ReceivablesModel::where(['id' => $id, 'check_status' => 1])->find();
  133. if (empty($customer)) {
  134. $this->error('回款信息不存在');
  135. }
  136. Db::startTrans();
  137. try {
  138. ReceivablesModel::where(['id' => $id])->update(['check_status' => 4]);
  139. ExamineRecord::where([
  140. 'relation_type' => ExamineRecord::RECEIVABLES_TYPE,
  141. 'relation_id' => $id,
  142. 'status' => 0
  143. ])->update(['status' => 3]);
  144. Db::commit();
  145. } catch (Exception $e) {
  146. Db::rollback();;
  147. $this->error($e->getMessage());
  148. }
  149. $this->success('撤回成功');
  150. }
  151. //修改回款
  152. public function editReceivables() {
  153. $id = input('id');
  154. $params = $this->request->post();
  155. $row = ReceivablesModel::where(['id' => $id, 'check_status' => ['in', [3, 4]]])->find();
  156. if (empty($row)) {
  157. $this->error('回款信息不存在');
  158. }
  159. // 表单验证
  160. if (($result = $this->qingdongValidate($params, get_class(), 'create')) !== true) {
  161. $this->error($result);
  162. }
  163. $result = FormField::checkFields(FormField::RECEIVABLES_TYPE, $params,$id);
  164. if ($result !== true) {
  165. $this->error($result);
  166. }
  167. Db::startTrans();
  168. try {
  169. ReceivablesModel::updateReceivables($params);
  170. Db::commit();
  171. } catch (Exception $e) {
  172. Db::rollback();
  173. $this->error($e->getMessage());
  174. }
  175. $this->success('修改回款信息成功');
  176. }
  177. //获取回款编号
  178. public function getReceivablesNumber()
  179. {
  180. $this->success('请求成功', ['number' => ReceivablesModel::getNum()]);
  181. }
  182. }