Просмотр исходного кода

合同接口金额计算及原币值优化

chengchaohua 1 неделя назад
Родитель
Сommit
f996a9b96d

+ 102 - 15
code/jyyy/nckd-jimin-jyyy-fi/src/main/java/nckd/jimin/jyyy/fi/webapi/ContractbillApiPlugin.java

@@ -30,6 +30,7 @@ import org.slf4j.LoggerFactory;
 
 import java.io.Serializable;
 import java.math.BigDecimal;
+import java.math.RoundingMode;
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
 import java.util.*;
@@ -179,13 +180,20 @@ public class ContractbillApiPlugin implements Serializable {
 
         // 付款计划集合
         JSONArray fukuanplanAry = (JSONArray) billData.get("fukuanplan");
+        if(fukuanplanAry.size() > 1) {
+            result.setMessage("付款计划只能传一条记录"); // 已经和客户确认了,他们只会有一条,而我们对第二次同笔数据送入接口会做更新,只有一笔也不需要定位那条分录
+            result.setStatus(false);
+            return result;
+        }
 
         // 1)单据组装
         String entityNumber = "er_contractbill"; // 合同台账单 单据标识
         QFilter qFilter = new QFilter("contractcode", QCP.equals,fcontractcode); // 合同号(原始)
         DynamicObject dynamicObject = null;
+        boolean isnew = true; // 标记:是新的合同,不是第二次送入进行更新的
         if(QueryServiceHelper.exists(entityNumber,new QFilter[]{qFilter})){
             // 合同台账的合同号(原始)已存在
+            isnew = false;
             dynamicObject = BusinessDataServiceHelper.loadSingle(entityNumber,new QFilter[]{qFilter});
             String contractcodenewhistory = dynamicObject.getString("nckd_contractcodenew");// 合同号(最新)
             if (!StringUtils.isEmpty(nckd_contractcodenew)) {
@@ -355,8 +363,13 @@ public class ContractbillApiPlugin implements Serializable {
 
         // 1.3)付款计划分录
         DynamicObjectCollection plannewColl = dynamicObject.getDynamicObjectCollection("expenseentryentity"); // 单据体标识
-        plannewColl.clear();
-        BigDecimal hetongjinetotalfist = new BigDecimal(0); // 合同总额(初始)
+//        plannewColl.clear();
+        BigDecimal hetongjinetotalfist = new BigDecimal(0); // 合同总额(初始)--头表
+        BigDecimal hetongjinetotalfistBen = new BigDecimal(0); // 合同总额(初始)-本位币--头表
+        BigDecimal yifutotal = new BigDecimal(0); // 已付金额--头表
+        BigDecimal yifutotalBen = new BigDecimal(0); // 已付金额-本位币--头表
+        BigDecimal yibaoxiaototal = new BigDecimal(0); // 已报销金额--头表
+        BigDecimal yibaoxiaototalBen = new BigDecimal(0); // 已报销金额-本位币--头表
         for (Object item : fukuanplanAry) {
             JSONObject jsb = (JSONObject)item;
             String fpayterms = jsb.getString("payterms"); // 付款条件
@@ -371,8 +384,51 @@ public class ContractbillApiPlugin implements Serializable {
                 result.setStatus(false);
                 return result;
             }
+            BigDecimal fexpapplyamountBen =fexpapplyamount.multiply(huilv).setScale(2, RoundingMode.HALF_UP); // 含税金额本位币,四舍五入
+
+            // 确认合同单据本次是新增还是更新,分录接口只允许送一条记录
+            DynamicObject newEntry3 = null;
+            if (isnew) {
+                // 新增
+                newEntry3 = new DynamicObject(plannewColl.getDynamicObjectType());
+                newEntry3.set("reimbursedamount",0); // 已报销金额
+                newEntry3.set("reimbursedcurramount",0); // 已报销金额(本位币)
+                newEntry3.set("oriexppayedamount",0); // 已付金额
+                newEntry3.set("exppayedamount",0); // 已付金额(本位币)
+                newEntry3.set("oriexpnotpayamount",fexpapplyamount); // 未付金额
+                newEntry3.set("expnotpayamount",fexpapplyamountBen); // 未付金额(本位币),四舍五入
+                newEntry3.set("canloanamount",fexpapplyamount); // 可预付金额
+                newEntry3.set("canloancurramount",fexpapplyamountBen); // 可预付金额(本位币)
+                newEntry3.set("orgiexpebalanceamount",fexpapplyamount); // 可报销金额
+                newEntry3.set("expebalanceamount",fexpapplyamountBen); // 可报销金额(本位币)
+            } else {
+                // 更新
+                newEntry3 = plannewColl.get(0); // 分录只有一条记录
+                // 未付金额=含税金额 - 已付金额,未付金额(本位币)=含税金额(本位币) - 已付金额(本位币)
+                newEntry3.set("oriexpnotpayamount",fexpapplyamount.subtract(newEntry3.getBigDecimal("oriexppayedamount"))); // 未付金额
+                newEntry3.set("expnotpayamount",fexpapplyamountBen.subtract(newEntry3.getBigDecimal("exppayedamount"))); // 未付金额(本位币)
+                // 可预付金额=含税金额 - 已付金额,可预付金额(本位币)=含税金额(本位币) - 已付金额(本位币)
+                newEntry3.set("canloanamount",fexpapplyamount.subtract(newEntry3.getBigDecimal("oriexppayedamount"))); // 可预付金额
+                newEntry3.set("canloancurramount",fexpapplyamountBen.subtract(newEntry3.getBigDecimal("exppayedamount"))); // 可预付金额(本位币)
+                // 可报销金额=含税金额 - 已报销金额,可报销金额(本位币)=含税金额(本位币) - 已报销金额(本位币)
+                newEntry3.set("orgiexpebalanceamount",fexpapplyamount.subtract(newEntry3.getBigDecimal("reimbursedamount"))); // 可报销金额
+                newEntry3.set("expebalanceamount",fexpapplyamountBen.subtract(newEntry3.getBigDecimal("reimbursedcurramount"))); // 可报销金额(本位币)
+
+                // 累加给头表字段
+                yifutotal = yifutotal.add(newEntry3.getBigDecimal("oriexppayedamount")); // 已付金额--头表
+                yifutotalBen = yifutotalBen.add(newEntry3.getBigDecimal("exppayedamount")); // 已付金额-本位币--头表
+                yibaoxiaototal = yibaoxiaototal.add(newEntry3.getBigDecimal("reimbursedamount")); // 已报销金额--头表
+                yibaoxiaototalBen = yibaoxiaototalBen.add(newEntry3.getBigDecimal("reimbursedcurramount")); // 已报销金额-本位币--头表
+            }
+
+            newEntry3.set("orientryamount",fexpapplyamount); // 不含税金额
+            newEntry3.set("expenseamount",fexpapplyamount); // 含税金额
+            newEntry3.set("currexpenseamount",fexpapplyamountBen); // 含税金额(本位币),四舍五入
+
+            // 累加给头表字段
+            hetongjinetotalfist = hetongjinetotalfist.add(fexpapplyamount); // 进行累加付给合同金额
+            hetongjinetotalfistBen = hetongjinetotalfistBen.add(fexpapplyamountBen); // 进行累加付给合同金额(本位币)
 
-            DynamicObject newEntry3 = new DynamicObject(plannewColl.getDynamicObjectType());
             DynamicObject fukuantype = BusinessDataServiceHelper.loadSingle("cas_paymentbilltype","id,name",new QFilter[]{new QFilter("number",QCP.equals,"201")});
             newEntry3.set("paymenttypeid",fukuantype); // 付款类型
             newEntry3.set("payterms",fpayterms); // 付款条件
@@ -383,14 +439,11 @@ public class ContractbillApiPlugin implements Serializable {
             }
             newEntry3.set("entrycostdept",company); // 费用承担部门
             newEntry3.set("entrycostcompany",company); // 费用承担公司
+            newEntry3.set("exchangerate",huilv); // 汇率
             // 固定值 FYXM99.01 合同款
             DynamicObject feiyongproject = BusinessDataServiceHelper.loadSingle("er_expenseitemedit","id,name",new QFilter[]{new QFilter("number",QCP.equals,"FYXM99.01")});
             newEntry3.set("expenseitem",feiyongproject); // 费用项目
-            newEntry3.set("expenseamount",fexpapplyamount); // 含税金额
-            newEntry3.set("orientryamount",fexpapplyamount); // 不含税金额
-            hetongjinetotalfist = hetongjinetotalfist.add(fexpapplyamount);
 
-            newEntry3.set("exchangerate",huilv); // 汇率
             // 项目
             String std_project = jsb.getString("projectid");
             DynamicObject project = BusinessDataServiceHelper.loadSingle("bd_project","id,name",new QFilter[]{new QFilter("number",QCP.equals,std_project)});
@@ -398,10 +451,12 @@ public class ContractbillApiPlugin implements Serializable {
 
             newEntry3.set("entrycurrency",bizhong); // 分录币别(和头表一致)
 
-            plannewColl.add(newEntry3);
+            if (isnew) {
+                plannewColl.add(newEntry3);
+            }
         }
 
-        // 合同总额(初始)值为0时,为框架合同,则星瀚不需要处理,付款计划栏、以及合同总价栏的合同总额(初始)、合同总额(变更后)
+        // 1.4)头表金额字段处理:合同总额(初始)值为0时,为框架合同,则星瀚不需要处理,付款计划栏、以及合同总价栏的合同总额(初始)、合同总额(变更后)
         if (hetongjinetotalfist.compareTo(BigDecimal.ZERO) == 0) {
             dynamicObject.set("frameworkcontract",true); // 框架合同:是
             dynamicObject.set("contractamount", 0); // 合同总额(初始)
@@ -411,15 +466,47 @@ public class ContractbillApiPlugin implements Serializable {
             plannewColl.clear(); // 付款计划分录无记录
         } else {
             dynamicObject.set("frameworkcontract",false); // 框架合同:否
-            dynamicObject.set("contractamount", hetongjinetotalfist); // 合同总额(初始)
-            dynamicObject.set("oriapplyamount", hetongjinetotalfist); // 合同总额(变更后)
-            dynamicObject.set("originalamount", hetongjinetotalfist); // 合同总额(不含税)
-            dynamicObject.set("orinotpayamount", hetongjinetotalfist); // 未付金额
+            dynamicObject.set("contractamount", hetongjinetotalfist); // 头表字段:合同总额(初始)
+            dynamicObject.set("oriapplyamount", hetongjinetotalfist); // 头表字段:合同总额(变更后)
+            dynamicObject.set("originalamount", hetongjinetotalfist); // 头表字段:合同总额(不含税)
+            dynamicObject.set("oriavailableamount", hetongjinetotalfist); // 头表字段:可用金额
+            dynamicObject.set("availableamount", hetongjinetotalfistBen); // 头表字段:可用金额(本位币)
+            if (isnew) {
+                // 第一次合同送入,新增
+                dynamicObject.set("oripayedamount", 0); // 头表字段:已付金额
+                dynamicObject.set("payedamount", 0); // 头表字段:已付金额(本位币)
+                dynamicObject.set("oriusedamount", 0); // 头表字段:已报销金额
+                dynamicObject.set("usedamount", 0); // 头表字段:已报销金额(本位币)
+                dynamicObject.set("orinotpayamount", hetongjinetotalfist); // 头表字段:未付金额
+                dynamicObject.set("notpayamount",hetongjinetotalfistBen ); // 头表字段:未付金额(本位币)
+                dynamicObject.set("oricanloanamount", hetongjinetotalfist); // 头表字段:可预付金额
+                dynamicObject.set("billcanloanamount", hetongjinetotalfistBen); // 头表字段:可预付金额(本位币)
+                dynamicObject.set("oribalanceamount", hetongjinetotalfist); // 头表字段:可报销金额
+                dynamicObject.set("balanceamount", hetongjinetotalfistBen); // 头表字段:可报销金额(本位币)
+            } else {
+                // 第二次合同送入,更新
+                // a)头表字段:已付金额,已付金额(本位币)是星翰系统反写到合同台账单上的
+                BigDecimal oripayedamount = dynamicObject.getBigDecimal("oripayedamount"); // 头表字段:已付金额
+                BigDecimal payedamount = dynamicObject.getBigDecimal("payedamount"); // 头表字段:已付金额(本位币)
+                // b)头表字段:已报销金额,已报销金额(本位币)是星翰系统反写到合同台账单上的
+                BigDecimal oriusedamount = dynamicObject.getBigDecimal("oriusedamount"); // 头表字段:已报销金额
+                BigDecimal usedamount = dynamicObject.getBigDecimal("usedamount"); // 头表字段:已报销金额(本位币)
+
+                // c)未付金额=合同金额 - 已付金额,未付金额(本位币)=合同金额(本位币) - 已付金额(本位币)
+                dynamicObject.set("orinotpayamount", hetongjinetotalfist.subtract(oripayedamount)); // 头表字段:未付金额
+                dynamicObject.set("notpayamount", hetongjinetotalfistBen.subtract(payedamount)); // 头表字段:未付金额(本位币)
+                // d)可预付金额=合同金额 - 已付金额,可预付金额(本位币)=合同金额(本位币) - 已付金额(本位币)
+                dynamicObject.set("oricanloanamount", hetongjinetotalfist.subtract(oripayedamount)); // 头表字段:可预付金额
+                dynamicObject.set("billcanloanamount", hetongjinetotalfistBen.subtract(payedamount)); // 头表字段:可预付金额(本位币)
+                // e)可报销金额=合同金额 - 已报销金额,可报销金额(本位币)=合同金额(本位币) - 已报销金额(本位币)
+                dynamicObject.set("oribalanceamount", hetongjinetotalfist.subtract(oriusedamount)); // 头表字段:可报销金额
+                dynamicObject.set("balanceamount", hetongjinetotalfist.subtract(usedamount)); // 头表字段:可报销金额(本位币)
+            }
 
             dynamicObject.set("expenseentryentity", plannewColl); // 付款计划分录
         }
 
-        // 先单据保存
+        // 1.5)先单据保存
         OperationResult opResult = SaveServiceHelper.saveOperate(entityNumber, new DynamicObject[]{dynamicObject}, OperateOption.create());
 
         if (!opResult.isSuccess()) {
@@ -432,7 +519,7 @@ public class ContractbillApiPlugin implements Serializable {
         // 2)相同的单据第二次送入接口,新文件和历史文件重名时,删掉历史文件,新增新文件
         // 获取历史单据全部附件
        List<Map<String, Object>> atts = AttachmentServiceHelper.getAttachments(entityNumber, dynamicObject.getPkValue(),"attachmentpanel");
-        if (atts.size() > 0) {
+        if (!atts.isEmpty()) {
             Iterator<Map<String, Object>> iterator = atts.iterator();
             // 遍历所有附件
             while (iterator.hasNext()) {