123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249 |
- <?php
- namespace app\index\controller;
- use think\Db;
- class Contract extends Base
- {
- public function lists(){
- $page = input('page/d',1);
- $length = input('size/d',10);
- $start = ($page - 1) * $length;
- $map['org_id'] = $this->orgId;
- $map['status'] = 2;
- $map['advanced'] = 3; // 合同
- $list = Db::name('apply')
- ->where($map)
- ->field('id,apply_sn,title,form_json')
- ->limit($start,$length)
- ->order('id desc')
- ->select();
- // 获取补充合同单
- $addlist = $this->getOtherList(4);
- // 获取支付合同单
- $paylist = $this->getOtherList(5);
- foreach ($list as $k=>$v){
- $formJson = json_decode($v['form_json'],true);
- $money = 0;
- $ptitle = '';
- $company = '';
- $addTime = '';
- foreach ($formJson as $kk=>$vv){
- if($vv['componentName'] == 'ddjjcontractfield'){
- foreach ($vv['components'] as $kkk=>$vvv){
- if($vvv['idx'] === 3){
- $money = $vvv['values'];
- } else if($vvv['idx'] === 2){
- $ptitle = $vvv['values'];
- } else if($vvv['idx'] === 5){
- $company = $vvv['values'];
- } else if($vvv['idx'] === 1){
- $addTime = $vvv['values'];
- }
- }
- break;
- }
- }
- $list[$k]['ptitle'] = $ptitle;
- $list[$k]['company'] = $company;
- $list[$k]['addTime'] = $addTime;
- $list[$k]['total_money'] = $this->getMoneyByFormJson($v['id'],$money,$addlist);
- $cids = $this->getApplyIds($v['id'],$addlist);
- $list[$k]['pay_money'] = $this->getPayMoney($cids,$paylist);
- $paybl = floatval($list[$k]['total_money']) > 0?round($list[$k]['pay_money']/$list[$k]['total_money'],2):0;
- $paybl = $paybl > 1 ? 1 : $paybl;
- $list[$k]['paybl'] = ($paybl*100).'%';
- unset($list[$k]['form_json']);
- }
- $total = Db::name('apply')->where($map)->count();
- $data = [
- 'total' => $total,
- 'list' => $list?$list:[]
- ];
- ajax_return_ok($data);
- }
- public function addlists(){
- $applyId = input('id/d',0);
- $addlists = $this->getOtherList(4);
- // 获取原始款项
- $info = Db::name('apply')->where('id',$applyId)->field('id,apply_sn,title,form_json')->find();
- $formJson = json_decode($info['form_json'],true);
- $money = 0;
- $ptitle = '';
- $company = '';
- $addTime = '';
- foreach ($formJson as $kk=>$vv){
- if($vv['componentName'] == 'ddjjcontractfield'){
- foreach ($vv['components'] as $kkk=>$vvv){
- if($vvv['idx'] === 3){
- $money = $vvv['values'];
- } else if($vvv['idx'] === 2){
- $ptitle = $vvv['values'];
- } else if($vvv['idx'] === 5){
- $company = $vvv['values'];
- } else if($vvv['idx'] === 1){
- $addTime = $vvv['values'];
- }
- }
- break;
- }
- }
- $info['ptitle'] = $ptitle;
- $info['company'] = $company;
- $info['addTime'] = $addTime;
- $info['money'] = $money;
- unset($info['form_json']);
- $lists = [$info];
- foreach ($addlists as $k=>$v){
- if($applyId == $v['applyId']){
- $lists[] = $v;
- }
- }
- ajax_return_ok($lists);
- }
- public function paylists(){
- $applyId = input('id/d',0);
- // 获取补充合同单
- $addlist = $this->getOtherList(4);
- $cids = $this->getApplyIds($applyId,$addlist);
- $paylists = $this->getOtherList(5);
- $lists = [];
- foreach ($paylists as $k=>$v){
- if(in_array($v['applyId'],$cids)){
- $lists[] = $v;
- }
- }
- ajax_return_ok($lists);
- }
- // 获取审批单的支付比例
- public function getmoney(){
- $applyId = input('id/d',0);
- $formJson = Db::name('apply')->where('id',$applyId)->value('form_json');
- if(!$formJson){
- ajax_return_error('记录不存在');
- }
- $formJson = json_decode($formJson,true);
- $total_money = 0;
- foreach ($formJson as $kk=>$vv){
- if($vv['componentName'] == 'ddjjcontractfield'||$vv['componentName'] == 'ddaddcontractfield'){
- foreach ($vv['components'] as $kkk=>$vvv){
- if($vvv['idx'] === 3){
- $total_money = $vvv['values'];
- }
- }
- break;
- }
- }
- $paylists = $this->getOtherList(5);
- $pay_money = $this->getPayMoney([$applyId],$paylists);
- $paybl = 0;
- if($total_money){
- $paybl = round($pay_money/$total_money,2);
- $paybl = $paybl > 1 ? 1 : $paybl;
- $paybl = ($paybl*100).'%';
- }
- ajax_return_ok(['money'=>$pay_money,'paybl'=>$paybl]);
- }
- private function getOtherList($advanced){
- $lists = Db::name('apply')
- ->where([
- 'advanced' => $advanced,
- 'status' => 2,
- 'org_id' => $this->orgId
- ])->field('id,title,apply_sn,form_json')
- ->select();
- $lists = $lists?$lists:[];
- foreach ($lists as $k=>$v){
- $formJson = json_decode($v['form_json'],true);
- $applyId = 0;
- $money = 0;
- $ptitle = '';
- $company = '';
- $addTime = '';
- foreach ($formJson as $kk=>$vv){
- if($vv['componentName'] == 'ddaddcontractfield'||$vv['componentName'] == 'ddpaycontractfield'){
- foreach ($vv['components'] as $kkk=>$vvv){
- if($vvv['idx'] === 9){
- $applyId = $vvv['values2'][0]['id'];
- } else if($vvv['idx'] === 3){
- $money = $vvv['values'];
- } else if($vvv['idx'] === 2){
- $ptitle = $vvv['values'];
- } else if($vvv['idx'] === 5){
- $company = $vvv['values'];
- } else if($vvv['idx'] === 1){
- $addTime = $vvv['values'];
- }
- }
- break;
- }
- }
- $lists[$k]['ptitle'] = $ptitle;
- $lists[$k]['company'] = $company;
- $lists[$k]['addTime'] = $addTime;
- $lists[$k]['applyId'] = $applyId;
- $lists[$k]['money'] = $money;
- unset($lists[$k]['form_json']);
- }
- return $lists;
- }
- // 获取补充合同id
- private function getApplyIds($id,$addlist){
- $cids = [];
- $cids[] = $id;
- foreach ($addlist as $k=>$v){
- if($id == $v['applyId']){
- $cids[] = $v['id'];
- }
- }
- return $cids;
- }
- // 从formJson中获取金额
- private function getMoneyByFormJson($id,$money,$addlist){
- $othermoney = 0;
- foreach ($addlist as $k=>$v){
- if($id == $v['applyId']){
- $othermoney += $v['money'];
- }
- }
- return $money + $othermoney;
- }
- private function getPayMoney($cids,$paylist){
- $othermoney = 0;
- foreach ($paylist as $k=>$v){
- if(in_array($v['applyId'],$cids)){
- $othermoney += $v['money'];
- }
- }
- return $othermoney;
- }
- }
|