6c
3 天以前 bc4f6588b92d207bef56edf3b5b38d8e4e2b8236
src/main/java/com/product/contract/service/ContractInfoService.java
@@ -1,5 +1,7 @@
package com.product.contract.service;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.product.common.lang.StringUtils;
import com.product.contract.config.CmnConst;
import com.product.contract.service.ide.IContractInfoService;
@@ -14,13 +16,14 @@
import com.product.core.transfer.Transactional;
import com.product.module.sys.entity.SystemUser;
import com.product.util.BaseUtil;
import org.apache.bcel.generic.IF_ACMPEQ;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
/**
 * Copyright LX
@@ -49,6 +52,9 @@
    @Override
    @Transactional
    public String saveContractInfo(FieldSetEntity fs) throws BaseException {
        if (StringUtils.isEmpty(fs.getString(CmnConst.CONTRACT_STATUS))) {
            fs.setValue(CmnConst.CONTRACT_STATUS, 0);
        }
        if(StringUtils.isEmpty(fs.getString(CmnConst.UUID))){
            fs.setValue("org_level_uuid", SpringMVCContextHolder.getCurrentUser().getOrg_level_uuid());
            fs.setValue("created_by", SpringMVCContextHolder.getCurrentUser().getUser_id());
@@ -129,32 +135,6 @@
     */
    public FieldSetEntity findContractInfo(FieldSetEntity fs) throws BaseException {
       FieldSetEntity cfs =baseDao.getFieldSetEntity(CmnConst.LX_PROJECT_CONTRACT_INFO,fs.getUUID(),true);
       DataTableEntity sub=cfs.getSubDataTable("product_project_contract_info_sub");
        StringBuilder  filter=new StringBuilder(128);
        filter.append(" uuid='"+cfs.getValues().get("project_uuid")+"'");
        DataTableEntity product_project_business = baseDao.listTable("product_project_business",filter.toString(),new Object[]{});
//        String guarantee_period = product_project_business.getData().get(0).getValues().get("guarantee_period").toString();
        String guarantee_period = product_project_business.getString(0, "guarantee_period");
        StringBuilder  dictFilter=new StringBuilder(128);
        dictFilter.append("dict_name='项目管理项目类型' and dict_value="+product_project_business.getData().get(0).getValues().get("project_type").toString());
        DataTableEntity product_sys_dict = baseDao.listTable("product_sys_dict", dictFilter.toString(), new Object[]{});
       //合同有效期:如果是开发项目、实施项目,从项目信息的质保期带过来
        String dict_label = product_sys_dict.getData().get(0).getValues().get("dict_label").toString();
        if(dict_label.equals("开发项目") ||dict_label.equals("实施项目")){
            cfs.setValue("guarantee_period",guarantee_period);
            cfs.setValue("project_type_now",dict_label);
        }
       //结束日期: 如果是开发项目、实施项目,验收日期+合同有效期,维护合同则是签定日期+合同有效期
        if(dict_label.equals("开发项目") || dict_label.equals("实施项目")){
//            String carefully_check = product_project_business.getData().get(0).getValues().get("carefully_check").toString();
            String carefully_check = product_project_business.getString(0, "carefully_check");
            if (!StringUtils.isEmpty(carefully_check)){
                String[] dateTime = carefully_check.split(" ");
                String time=dateTime[0];
                String date = getDate(time,guarantee_period);
                cfs.setValue("end_time",date);
            }
        }
        return cfs;
    }
@@ -178,18 +158,22 @@
     */
    public FieldSetEntity getContractInfoByName(FieldSetEntity fs) throws BaseException {
        FieldSetEntity fss = baseDao.getFieldSetEntity(CmnConst.LX_PROJECT_CONTRACT_INFO,fs.getUUID(),true);
        FieldSetEntity fsProject = baseDao.getFieldSetEntity(CmnConst.PRODUCT_PROJECT_BUSINESS,fss.getString("project_uuid"),false);
        FieldSetEntity fsCustomer = baseDao.getFieldSet("product_project_customer", fsProject.getString("customer_name"), false);
        if (!StringUtils.isEmpty(fss.getString("project_uuid"))) {
            FieldSetEntity fsProject = baseDao.getFieldSetEntity(CmnConst.PRODUCT_PROJECT_BUSINESS,fss.getString("project_uuid"),false);
            FieldSetEntity fsCustomer = baseDao.getFieldSet("product_project_customer", fsProject.getString("customer_name"), false);
            fss.setValue("customer_name", fsCustomer.getString("customer_name"));
            fss.setValue("project_num",fsProject.getString("project_num"));
            fss.setValue("project_name",fsProject.getString("project_name"));
        } else {
            fss.setValue("customer_name", fss.getString("owner"));
        }
        FieldSetEntity contractName = baseDao.getFieldSetEntityBySQL("SELECT sum(invoice_amount) invoice_amount FROM product_project_contract_invoice_info WHERE contract_invoice_uuid in(SELECT uuid FROM product_project_contract_invoice where contract_name=?)", new String[]{fs.getString("uuid")},false);
        if (contractName==null||StringUtils.isEmpty(contractName.getString("invoice_amount"))){
            fss.setValue("invoice_amount",0);
        }else {
            fss.setValue("invoice_amount",contractName.getString("invoice_amount"));
        }
        fss.setValue("customer_name", fsCustomer.getString("customer_name"));
        fss.setValue("collection_amount",0);
        fss.setValue("project_num",fsProject.getString("project_num"));
        fss.setValue("project_name",fsProject.getString("project_name"));
        return fss;
    }
@@ -481,4 +465,90 @@
            return baseDao.update(fieldSetEntity);
        }
    }
    /**
     * 验证发票号,不同次数的开票中不能有相同的发票号(同次的可以)
     * @param fse
     * @return
     */
    public Object[] checkInvoiceNumber(FieldSetEntity fse) {
        String uuid = fse.getUUID();
        String[] invoiceNumberArr = fse.getString("invoice_num").split(",");
        StringBuilder filter = new StringBuilder(128);
        List<String> paramList  = Lists.newArrayList();
        if (!StringUtils.isEmpty(uuid)) {
            filter.append("contract_invoice_uuid<>? AND ");
            paramList.add(uuid);
        }
        filter.append(BaseUtil.buildQuestionMarkFilter("invoice_num", invoiceNumberArr.length, true));
        paramList.addAll(Arrays.asList(invoiceNumberArr));
        DataTableEntity dte = baseDao.listTable(CmnConst.PRODUCT_PROJECT_CONTRACT_INVOICE_INFO, filter.toString(), paramList.toArray());
        if (!DataTableEntity.isEmpty(dte)) {
            return dte.getFieldAllValues("invoice_num");
        }
        return new Object[]{};
    }
    /**
     * 验证发票号,单个发票的回款总额不能超过开票金额
     * @param fse
     * @return
     */
    public Object checkInvoicePayment(FieldSetEntity fse) {
        String uuid = fse.getUUID();
        DataTableEntity paramDte = fse.getSubDataTable("product_project_contract_payment_collection");
        StringBuilder partSql = new StringBuilder(256);
        for (int i = 0; i < paramDte.getRows(); i++) {
            if (partSql.length() > 0) {
                partSql.append("\n        UNION ALL");
            }
            FieldSetEntity paramFse = paramDte.getFieldSetEntity(i);
            partSql.append("\n        SELECT '").append(paramFse.getString("invoice_num")).append("' invoice_num,'").append(paramFse.getString("collection_amount")).append("' cur_payment_amount");
        }
        StringBuilder sql = new StringBuilder(256);
        sql.append("\nSELECT DISTINCT i.invoice_num,i.invoice_amount,i.payment_amount,p.cur_payment_amount,IFNULL(i.payment_amount,0) + p.cur_payment_amount - i.invoice_amount diff_amount");
        sql.append("\nFROM product_project_contract_invoice_info i");
        sql.append("\nLEFT JOIN product_project_contract_payment_collection ps ON i.invoice_num=ps.invoice_num");
        List<String> paramList = Lists.newArrayList();
        if (!StringUtils.isEmpty(uuid)) {
            sql.append(" AND ps.contract_payment_uuid<>?");
            paramList.add(uuid);
        }
        sql.append("\nINNER JOIN (");
        sql.append("\n    SELECT invoice_num,SUM(cur_payment_amount) cur_payment_amount");
        sql.append("\n    FROM (");
        sql.append(partSql);
        sql.append("\n    ) t");
        sql.append("\n    GROUP BY 1");
        sql.append("\n) p ON i.invoice_num=p.invoice_num");
        sql.append("\nWHERE i.invoice_amount - IFNULL(i.payment_amount,0) < p.cur_payment_amount");
        DataTableEntity resultDte = baseDao.listTable(sql.toString(), paramList.toArray());
        return BaseUtil.dataTableEntityToJson(resultDte);
    }
    /**
     * 查询合同下的开票和回款记录
     * @param fse
     * @return
     */
    public JSONObject listContractInvoiceAndCollection(FieldSetEntity fse) {
        String contractUUID = fse.getUUID();
        JSONObject resultObj = new JSONObject();
        StringBuilder sql = new StringBuilder(128);
        sql.append("\nSELECT id.*");
        sql.append("\nFROM product_project_contract_invoice_info id");
        sql.append("\nINNER JOIN product_project_contract_invoice i ON id.contract_invoice_uuid=i.uuid");
        sql.append("\nWHERE i.contract_name=?");
        DataTableEntity invoiceDte = baseDao.listTable(sql.toString(), new Object[]{contractUUID});
        resultObj.put("invoice", BaseUtil.dataTableEntityToJson(invoiceDte));
        sql.setLength(0);
        sql.append("\nSELECT c.*");
        sql.append("\nFROM product_project_contract_payment_collection c");
        sql.append("\nINNER JOIN product_project_contract_payment p ON c.contract_payment_uuid=p.uuid");
        sql.append("\nWHERE p.contract_name=?");
        DataTableEntity collectionDte = baseDao.listTable(sql.toString(), new Object[]{contractUUID});
        resultObj.put("collection", BaseUtil.dataTableEntityToJson(collectionDte));
        return resultObj;
    }
}