Receivablesplan.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. namespace app\admin\controller\qingdongams\customer;
  3. use addons\qingdongams\model\Contract;
  4. use addons\qingdongams\model\Customer;
  5. use addons\qingdongams\model\Form;
  6. use addons\qingdongams\model\Staff;
  7. use app\admin\controller\qingdongams\Base;
  8. use app\common\controller\Backend;
  9. use think\Db;
  10. use think\Exception;
  11. /**
  12. * 回款计划
  13. *
  14. * @icon fa fa-circle-o
  15. */
  16. class Receivablesplan extends Base
  17. {
  18. /**
  19. * Receivablesplan模型对象
  20. * @var \addons\qingdongams\model\ReceivablesPlan
  21. */
  22. protected $model = null;
  23. public function _initialize()
  24. {
  25. parent::_initialize();
  26. $this->model = new \addons\qingdongams\model\ReceivablesPlan();
  27. }
  28. /**
  29. * 查看
  30. */
  31. public function index()
  32. {
  33. //当前是否为关联查询
  34. $this->relationSearch = true;
  35. //设置过滤方法
  36. $this->request->filter(['strip_tags', 'trim']);
  37. if ($this->request->isAjax()) {
  38. //如果发送的来源是Selectpage,则转发到Selectpage
  39. if ($this->request->request('keyField')) {
  40. return $this->selectpage();
  41. }
  42. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  43. //0:全部 1:我负责的 2:下属负责的
  44. $type = input('type',0);
  45. switch($type){
  46. case 1:
  47. $staff = Staff::info();
  48. $wheres['owner_staff_id'] = $staff->id;
  49. break;
  50. case 2:
  51. $wheres['owner_staff_id'] = array('in',Staff::getLowerStaffId());
  52. break;
  53. default:
  54. $wheres['owner_staff_id'] = array('in',Staff::getMyStaffIds());
  55. break;
  56. }
  57. $list = $this->model
  58. ->with(['createStaff','ownerStaff','customer','contract'])
  59. ->where($where)
  60. ->where($wheres)
  61. ->order($sort, $order)
  62. ->paginate($limit);
  63. foreach ($list as $row) {
  64. $row->visible(['id','num','receivables_id','status','contract_id','customer_id','money','return_date','return_type','remind','remind_date','remarks','create_staff_id','owner_staff_id','createtime','updatetime']);
  65. $row->visible(['customer','contract']);
  66. $row->create_staff_id=$row['create_staff']['name'];
  67. $row->owner_staff_id=$row['owner_staff']['name'];
  68. $row->customer_id=$row['customer']['name'];
  69. $row->contract_id=$row['contract']['name'];
  70. }
  71. $result = array("total" => $list->total(), "rows" => $list->items());
  72. return json($result);
  73. }
  74. return $this->view->fetch();
  75. }
  76. /**
  77. * 添加回款计划
  78. */
  79. public function add()
  80. {
  81. if ($this->request->isPost()) {
  82. $params = $this->request->post('row/a');
  83. if (($result = $this->qingdongamsValidate($params, 'ReceivablesPlan', 'create')) !== true) {
  84. $this->error($result);
  85. }
  86. if ($this->model::where([
  87. 'num' => $params['num'],
  88. 'contract_id' => $params['contract_id']
  89. ])->find()) {
  90. $this->error('计划回款期数已存在');
  91. }
  92. Db::startTrans();
  93. try {
  94. $this->model::createPlan($params);
  95. Db::commit();
  96. } catch (Exception $e) {
  97. Db::rollback();
  98. $this->error($e->getMessage());
  99. }
  100. if ($result) {
  101. $this->success('新增回款计划成功');
  102. }
  103. $this->error(__('Parameter %s can not be empty', ''));
  104. }
  105. $customer_id= input('customer_id');
  106. $contract_id= input('contract_id');
  107. $this->assign('customer_id',$customer_id);
  108. $this->assign('contract_id', $contract_id);
  109. $this->assign('customer', Customer::get($customer_id));
  110. $this->assign('contract', Contract::get($contract_id));
  111. $this->assign('customer_id', input('customer_id'));
  112. $this->assign('createnum',get_num('receivablesplan'));
  113. return $this->view->fetch();
  114. }
  115. /**
  116. * 修改回款计划
  117. */
  118. public function edit($ids=null)
  119. {
  120. $map['id'] = $ids;
  121. if ($this->request->isPost()) {
  122. $params = $this->request->post('row/a');
  123. if (($result = $this->qingdongamsValidate($params, 'ReceivablesPlan', 'create')) !== true) {
  124. $this->error($result);
  125. }
  126. if (!$this->model::where($map)->find()) {
  127. $this->error('计划回款不存在');
  128. }
  129. if ($this->model::where([
  130. 'num' => $params['num'],
  131. 'contract_id' => $params['contract_id'],
  132. 'id'=>['neq',$ids]
  133. ])->find()) {
  134. $this->error('计划回款期数已存在');
  135. }
  136. Db::startTrans();
  137. try {
  138. $this->model::updatePlan($map,$params);
  139. Db::commit();
  140. } catch (Exception $e) {
  141. Db::rollback();
  142. $this->error($e->getMessage());
  143. }
  144. if ($result) {
  145. $this->success('修改回款计划成功');
  146. }
  147. $this->error('修改失败');
  148. }
  149. $row = $this->model->where($map)->with(['contract','customer'])->find();
  150. $this->assign('row',$row);
  151. $this->assign('createnum',get_num('receivablesplan'));
  152. return $this->view->fetch();
  153. }
  154. /**
  155. * 获取客户列表
  156. */
  157. public function getcustomer()
  158. {
  159. $pageSize = input('pageSize');
  160. $pageNumber = input('pageNumber');
  161. $where = [];
  162. if ($keyValue = $this->request->request("keyValue")) {
  163. $where['id'] = $keyValue;
  164. }
  165. $name = input('name');
  166. if (!empty($name)) {
  167. $where['name'] = ['like', '%' . $name . '%'];
  168. }
  169. $staff = Staff::info();
  170. $staff_id = $staff->id;
  171. $whereStaff = function ($query) use ($staff_id) {
  172. $query->where(['ro_staff_id' => ['like', "%,{$staff_id},%"]])
  173. ->whereOr('rw_staff_id', 'like', "%,{$staff_id},%")
  174. ->whereOr(['owner_staff_id' => ['in', Staff::getMyStaffIds()]]);
  175. };
  176. $customer = Customer::where($whereStaff)->where($where)->field('id,name')->order('id desc')->paginate($pageSize, false, ['page' => $pageNumber]);
  177. return json(['list' => $customer->items(), 'total' => $customer->total()]);
  178. }
  179. /**
  180. * 获取合同
  181. */
  182. public function getcontract()
  183. {
  184. $pageSize = input('pageSize');
  185. $pageNumber = input('pageNumber');
  186. $customer_id = input('customer_id', 0);
  187. $where = [];
  188. if ($keyValue = $this->request->request("keyValue")) {
  189. $where['id'] = $keyValue;
  190. }
  191. $name = input('name');
  192. if(!empty($name)){
  193. $where['name'] = ['like','%'.$name.'%'];
  194. }
  195. $where['customer_id'] = $customer_id;
  196. $where['check_status'] = 2;
  197. $contacts = Contract::where($where)->field('id,name,num')->order('id desc')->paginate($pageSize, false, ['page' => $pageNumber]);
  198. $data = [];
  199. $item=$contacts->items();
  200. foreach ($item as $v) {
  201. $v->name=$v['num'] . "({$v['name']})";
  202. }
  203. return json(['list' => $item, 'total' =>$contacts->total()]);
  204. }
  205. }