request->get(['page_no','page_size','contract_code','project_id']); $pageNo = empty($params['page_no']) ? 1 : $params['page_no']; $pageSize = empty($params['page_size']) ? 15 : $params['page_size']; $where = []; if(isset($params['project_id']) && $params['project_id']){ $where[] = ['project_id','=',$params['project_id']]; } if(isset($params['contract_code']) && $params['contract_code']){ $where[] = ['contract_code','like','%'.$params['contract_code'].'%']; } $data = Contract::field('id,project_id,contract_code,contract_name,contract_date,amount')->where($where) ->page($pageNo,$pageSize) ->order(['id' => 'desc']) ->select()->each(function($item){ $project = Project::field('custom_id,name,project_code')->where('id',$item['project_id'])->findOrEmpty(); $custom = Custom::field('name')->where('id',$project['custom_id'])->findOrEmpty(); $item['project_name'] = $project['name']; $item['project_code'] = $project['project_code']; $item['custom_name'] = $custom['name']; //合同洽商金额 $negotiate_amount = ContractNegotiation::where('contract_id',$item['id'])->sum('negotiation_amount'); //合同实际金额 $item['contract_amount'] = $item['amount'] + $negotiate_amount; //已回款金额 $item['has_refund_amount'] = FinanceReturnedRecord::where('contract_id',$item['id'])->sum('amount'); //已开票金额 $item['has_invoice_amount'] = FinanceInvoiceApply::where('contract_id',$item['id'])->sum('invoicing_amount'); //开票未回款 $item['has_invoice_not_refund_amount'] = ($item['has_invoice_amount'] - $item['has_refund_amount']) <= 0 ? 0 : $item['has_invoice_amount'] - $item['has_refund_amount']; //回款未开票 $item['has_refund_not_invoice_amount'] = ($item['has_refund_amount'] - $item['has_invoice_amount']) <= 0 ? 0 : $item['has_refund_amount'] - $item['has_invoice_amount']; unset($item['amount']); return $item; })->toArray(); $count = Contract::field('id')->where($where)->count(); $result = [ 'count' => $count, 'page_no' => $pageNo, 'page_size' => $pageSize, 'lists' => $data ]; return $this->success('请求成功',$result); } }