123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259 |
- <?php
- namespace addons\qingdongams\controller;
- use addons\qingdongams\model\ExamineRecord;
- use addons\qingdongams\model\FormField;
- use addons\qingdongams\model\Message;
- use addons\qingdongams\model\Receivables as ReceivablesModel;
- use addons\qingdongams\model\ReceivablesOther;
- use addons\qingdongams\model\Contract;
- use addons\qingdongams\model\Staff;
- use think\Db;
- use think\Exception;
- /**
- * @desc 操作文档:https://doc.fastadmin.net/qingdongams
- * @desc 软件介绍:https://www.fastadmin.net/store/qingdongams.html
- * @desc 售后微信:qingdong_crm
- */
- /**
- * 回款详情
- */
- class Receivables extends StaffApi {
- protected $noNeedLogin = [];
- protected $noNeedRight = [];
- //新增回款
- public function addReceivables() {
- $params = $this->request->post();
- // 表单验证
- if (($result = $this->qingdongamsValidate($params,get_class(), 'create')) !== true) {
- $this->error($result);
- }
- if (ReceivablesModel::where(['number' => $params['number'], 'contract_id' => $params['contract_id']])->find()) {
- $this->error('回款编号已存在');
- }
- $contract=Contract::where(['id'=>$params['contract_id']])->find();
- if(empty($contract)){
- $this->error('合同不存在');
- }
- if($contract['check_status'] != 2){
- $this->error('当前合同未审核通过');
- }
- $returnWhere['check_status'] = ['in',[0,1,2]];
- $returnWhere['contract_id'] = $params['contract_id'];
- $returnMoeny = ReceivablesModel::where($returnWhere)->sum('money');
- if($returnMoeny >= $contract['money']){
- $this->error('当前合同已回款完毕');
- }
- if($returnMoeny + $params['money'] > $contract['money']){
- // $this->error('回款累计总金额已超过合同金额');
- }
- $result = FormField::checkFields(FormField::RECEIVABLES_TYPE,$params);
- if ($result !== true) {
- $this->error($result);
- }
- Db::startTrans();
- try {
- $params['customer_id']=$contract['customer_id'];
- ReceivablesModel::createReceivables($params);
- Db::commit();
- } catch (Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- if ($result) {
- $this->success('新增回款成功');
- }
- }
- //获取回款列表
- public function getList() {
- $params = $this->request->post();
- $customer_id = input('customer_id');
- $contract_id = input('contract_id');
- $check_status = input('check_status');
- $name = input('name');
- $staff_id=input('staff_id');
- $createtime=input('createtime');
- $limit = input('limit');
- $where = [];
- //审核状态 1审核中、2审核通过、3审核未通过
- if($check_status){
- //0待审核、1审核中、2审核通过、3审核未通过、4撤销
- if($check_status == 1){
- $where['check_status']=['in',[0,1]];
- }elseif($check_status == 2){
- $where['check_status']=2;
- }elseif($check_status == 3){
- $where['check_status']=3;
- }
- }
- if($customer_id){
- $where['customer_id'] = $customer_id;
- }
- if($name){
- $where['number'] = ['like',"%{$name}%"];
- }
- if($contract_id){
- $where['contract_id'] = $contract_id;
- }
- if($staff_id){
- $where['owner_staff_id'] = $staff_id;
- }else{
- $where['owner_staff_id'] = ['in', Staff::getMyStaffIds()];
- if (isset($params['type']) && $params['type']) {//客户分类
- if ($params['type'] == 1) {//我的创建
- $where['owner_staff_id'] = $this->auth->id;
- } elseif ($params['type'] == 2) {//下属创建
- $where['owner_staff_id'] = ['in', Staff::getLowerStaffId()];
- }
- }
- }
- if($createtime){
- $times = setTimes($createtime, 'time');
- $where['createtime'] = ['between', $times];
- }
- if (isset($params['times']) && $params['times']) {//客户分类
- $times = setTimes($params['times'], 'date');
- $where['return_time'] = ['between', $times];
- }
- if(isset($params['id_list'])){//日志 查询id列表
- $where=[];
- $where['id']=['in',explode(',',$params['id_list'])];
- }
- $list = ReceivablesModel::where($where)->with(['contract','customer','createStaff'])->order('id desc')->paginate($limit)->toArray();
- $data = $list['data'];
- $contractId='';
- foreach($data as $k=>$v){
- $contractId = $v['contract_id'].','.$contractId;
- }
- $where['check_status']=2;
- $remoney = ReceivablesModel::where($where)->sum('money');
- $where['check_status']=array('in','0,1');
- $inmoney = ReceivablesModel::where($where)->sum('money');
- $where['check_status']=array('in','3,4');
- $nomoney = ReceivablesModel::where($where)->sum('money');
- $moneyinfo['remoney'] = $remoney;//已回款
- $moneyinfo['inmoney'] = $inmoney;//回款中
- $moneyinfo['nomoney'] = $nomoney;//未回款
- $moneyinfo['allmoney'] = Contract::where(['id'=>['in',$contractId]])->sum('money');//总金额
- $list['moneyinfo']=$moneyinfo;
- $this->success('请求成功', $list);
- }
- //回款详情
- public function getDetail() {
- $id = input('id');
- $receivables = ReceivablesModel::where(['id' => $id])->with([
- 'customer',
- 'contract',
- 'plan',
- 'ownerStaff',
- 'createStaff'
- ])->find();
- if (empty($receivables)) {
- $this->error('信息不存在');
- }
- $receivables=$receivables->toArray();
- $receivables['is_operation']=0;
- if($receivables['owner_staff_id'] == $this->auth->id){
- //是否可以操作
- $receivables['is_operation']=1;
- }
- if ($receivables['check_status'] == 0 || $receivables['check_status'] == 1) {
- $receivables['is_examine'] = ExamineRecord::isExaminse(ExamineRecord::RECEIVABLES_TYPE, $id);
- } else {
- $receivables['is_examine'] = 0;
- }
- $receivables=ReceivablesOther::getOther($receivables);
- //标记通知已读
- Message::setRead(Message::RECEIVABLES_TYPE, $id, $this->auth->id);
- $this->success('请求成功', $receivables);
- }
- //撤回审核
- public function cancel() {
- $id = input('id');
- $customer = ReceivablesModel::where(['id' => $id, 'check_status' => 1])->find();
- if (empty($customer)) {
- $this->error('回款信息不存在');
- }
- if($customer['owner_staff_id'] != $this->auth->id){
- $this->error('只有负责人可以撤回审批');
- }
- Db::startTrans();
- try {
- ReceivablesModel::where(['id' => $id])->update(['check_status' => 4]);
- ExamineRecord::where([
- 'relation_type' => ExamineRecord::RECEIVABLES_TYPE,
- 'relation_id' => $id,
- 'status' => 0
- ])->update(['status' => 3]);
- Db::commit();
- } catch (Exception $e) {
- Db::rollback();;
- $this->error($e->getMessage());
- }
- $this->success('撤回成功');
- }
- //修改回款
- public function editReceivables() {
- $id = input('id');
- $params = $this->request->post();
- $row = ReceivablesModel::where(['id' => $id, 'check_status' => ['in', [3, 4]]])->find();
- if (empty($row)) {
- $this->error('回款信息不存在');
- }
- // 表单验证
- if (($result = $this->qingdongamsValidate($params,get_class(), 'create')) !== true) {
- $this->error($result);
- }
- $contract=Contract::where(['id'=>$row['contract_id']])->find();
- $returnWhere['check_status'] = ['in',[0,1,2]];
- $returnWhere['contract_id'] =$row['contract_id'];
- $returnMoeny = ReceivablesModel::where($returnWhere)->sum('money');
- if($returnMoeny >= $contract['money']){
- $this->error('合同已全部回款');
- }
- if($returnMoeny + $row['money'] > $contract['money']){
- // $this->error('回款累计总金额已超过合同金额');
- }
- $result = FormField::checkFields(FormField::RECEIVABLES_TYPE,$params,$id);
- if ($result !== true) {
- $this->error($result);
- }
- Db::startTrans();
- try {
- $result = ReceivablesModel::updateReceivables($params);
- Db::commit();
- } catch (Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- $this->success('修改回款信息成功');
- }
- //获取回款编号
- public function getReceivablesNumber() {
- $this->success('请求成功',['number'=>'H' . date('ymd') . rand(100, 999)]);
- }
- }
|