Contract.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <?php
  2. namespace app\index\controller;
  3. use think\Db;
  4. class Contract extends Base
  5. {
  6. public function lists(){
  7. $page = input('page/d',1);
  8. $length = input('size/d',10);
  9. $start = ($page - 1) * $length;
  10. $map['org_id'] = $this->orgId;
  11. $map['status'] = 2;
  12. $map['advanced'] = 3; // 合同
  13. $list = Db::name('apply')
  14. ->where($map)
  15. ->field('id,apply_sn,title,form_json')
  16. ->limit($start,$length)
  17. ->order('id desc')
  18. ->select();
  19. // 获取补充合同单
  20. $addlist = $this->getOtherList(4);
  21. // 获取支付合同单
  22. $paylist = $this->getOtherList(5);
  23. foreach ($list as $k=>$v){
  24. $formJson = json_decode($v['form_json'],true);
  25. $money = 0;
  26. $ptitle = '';
  27. $company = '';
  28. $addTime = '';
  29. foreach ($formJson as $kk=>$vv){
  30. if($vv['componentName'] == 'ddjjcontractfield'){
  31. foreach ($vv['components'] as $kkk=>$vvv){
  32. if($vvv['idx'] === 3){
  33. $money = $vvv['values'];
  34. } else if($vvv['idx'] === 2){
  35. $ptitle = $vvv['values'];
  36. } else if($vvv['idx'] === 5){
  37. $company = $vvv['values'];
  38. } else if($vvv['idx'] === 1){
  39. $addTime = $vvv['values'];
  40. }
  41. }
  42. break;
  43. }
  44. }
  45. $list[$k]['ptitle'] = $ptitle;
  46. $list[$k]['company'] = $company;
  47. $list[$k]['addTime'] = $addTime;
  48. $list[$k]['total_money'] = $this->getMoneyByFormJson($v['id'],$money,$addlist);
  49. $cids = $this->getApplyIds($v['id'],$addlist);
  50. $list[$k]['pay_money'] = $this->getPayMoney($cids,$paylist);
  51. $paybl = floatval($list[$k]['total_money']) > 0?round($list[$k]['pay_money']/$list[$k]['total_money'],2):0;
  52. $paybl = $paybl > 1 ? 1 : $paybl;
  53. $list[$k]['paybl'] = ($paybl*100).'%';
  54. unset($list[$k]['form_json']);
  55. }
  56. $total = Db::name('apply')->where($map)->count();
  57. $data = [
  58. 'total' => $total,
  59. 'list' => $list?$list:[]
  60. ];
  61. ajax_return_ok($data);
  62. }
  63. public function addlists(){
  64. $applyId = input('id/d',0);
  65. $addlists = $this->getOtherList(4);
  66. // 获取原始款项
  67. $info = Db::name('apply')->where('id',$applyId)->field('id,apply_sn,title,form_json')->find();
  68. $formJson = json_decode($info['form_json'],true);
  69. $money = 0;
  70. $ptitle = '';
  71. $company = '';
  72. $addTime = '';
  73. foreach ($formJson as $kk=>$vv){
  74. if($vv['componentName'] == 'ddjjcontractfield'){
  75. foreach ($vv['components'] as $kkk=>$vvv){
  76. if($vvv['idx'] === 3){
  77. $money = $vvv['values'];
  78. } else if($vvv['idx'] === 2){
  79. $ptitle = $vvv['values'];
  80. } else if($vvv['idx'] === 5){
  81. $company = $vvv['values'];
  82. } else if($vvv['idx'] === 1){
  83. $addTime = $vvv['values'];
  84. }
  85. }
  86. break;
  87. }
  88. }
  89. $info['ptitle'] = $ptitle;
  90. $info['company'] = $company;
  91. $info['addTime'] = $addTime;
  92. $info['money'] = $money;
  93. unset($info['form_json']);
  94. $lists = [$info];
  95. foreach ($addlists as $k=>$v){
  96. if($applyId == $v['applyId']){
  97. $lists[] = $v;
  98. }
  99. }
  100. ajax_return_ok($lists);
  101. }
  102. public function paylists(){
  103. $applyId = input('id/d',0);
  104. // 获取补充合同单
  105. $addlist = $this->getOtherList(4);
  106. $cids = $this->getApplyIds($applyId,$addlist);
  107. $paylists = $this->getOtherList(5);
  108. $lists = [];
  109. foreach ($paylists as $k=>$v){
  110. if(in_array($v['applyId'],$cids)){
  111. $lists[] = $v;
  112. }
  113. }
  114. ajax_return_ok($lists);
  115. }
  116. // 获取审批单的支付比例
  117. public function getmoney(){
  118. $applyId = input('id/d',0);
  119. $formJson = Db::name('apply')->where('id',$applyId)->value('form_json');
  120. if(!$formJson){
  121. ajax_return_error('记录不存在');
  122. }
  123. $formJson = json_decode($formJson,true);
  124. $total_money = 0;
  125. foreach ($formJson as $kk=>$vv){
  126. if($vv['componentName'] == 'ddjjcontractfield'||$vv['componentName'] == 'ddaddcontractfield'){
  127. foreach ($vv['components'] as $kkk=>$vvv){
  128. if($vvv['idx'] === 3){
  129. $total_money = $vvv['values'];
  130. }
  131. }
  132. break;
  133. }
  134. }
  135. $paylists = $this->getOtherList(5);
  136. $pay_money = $this->getPayMoney([$applyId],$paylists);
  137. $paybl = 0;
  138. if($total_money){
  139. $paybl = round($pay_money/$total_money,2);
  140. $paybl = $paybl > 1 ? 1 : $paybl;
  141. $paybl = ($paybl*100).'%';
  142. }
  143. ajax_return_ok(['money'=>$pay_money,'paybl'=>$paybl]);
  144. }
  145. private function getOtherList($advanced){
  146. $lists = Db::name('apply')
  147. ->where([
  148. 'advanced' => $advanced,
  149. 'status' => 2,
  150. 'org_id' => $this->orgId
  151. ])->field('id,title,apply_sn,form_json')
  152. ->select();
  153. $lists = $lists?$lists:[];
  154. foreach ($lists as $k=>$v){
  155. $formJson = json_decode($v['form_json'],true);
  156. $applyId = 0;
  157. $money = 0;
  158. $ptitle = '';
  159. $company = '';
  160. $addTime = '';
  161. foreach ($formJson as $kk=>$vv){
  162. if($vv['componentName'] == 'ddaddcontractfield'||$vv['componentName'] == 'ddpaycontractfield'){
  163. foreach ($vv['components'] as $kkk=>$vvv){
  164. if($vvv['idx'] === 9){
  165. $applyId = $vvv['values2'][0]['id'];
  166. } else if($vvv['idx'] === 3){
  167. $money = $vvv['values'];
  168. } else if($vvv['idx'] === 2){
  169. $ptitle = $vvv['values'];
  170. } else if($vvv['idx'] === 5){
  171. $company = $vvv['values'];
  172. } else if($vvv['idx'] === 1){
  173. $addTime = $vvv['values'];
  174. }
  175. }
  176. break;
  177. }
  178. }
  179. $lists[$k]['ptitle'] = $ptitle;
  180. $lists[$k]['company'] = $company;
  181. $lists[$k]['addTime'] = $addTime;
  182. $lists[$k]['applyId'] = $applyId;
  183. $lists[$k]['money'] = $money;
  184. unset($lists[$k]['form_json']);
  185. }
  186. return $lists;
  187. }
  188. // 获取补充合同id
  189. private function getApplyIds($id,$addlist){
  190. $cids = [];
  191. $cids[] = $id;
  192. foreach ($addlist as $k=>$v){
  193. if($id == $v['applyId']){
  194. $cids[] = $v['id'];
  195. }
  196. }
  197. return $cids;
  198. }
  199. // 从formJson中获取金额
  200. private function getMoneyByFormJson($id,$money,$addlist){
  201. $othermoney = 0;
  202. foreach ($addlist as $k=>$v){
  203. if($id == $v['applyId']){
  204. $othermoney += $v['money'];
  205. }
  206. }
  207. return $money + $othermoney;
  208. }
  209. private function getPayMoney($cids,$paylist){
  210. $othermoney = 0;
  211. foreach ($paylist as $k=>$v){
  212. if(in_array($v['applyId'],$cids)){
  213. $othermoney += $v['money'];
  214. }
  215. }
  216. return $othermoney;
  217. }
  218. }