Browse Source

计算周期

drew 6 years ago
parent
commit
4320aee4be

+ 55 - 7
src/main/java/com/izouma/zhumj/service/sale/ContractService.java

@@ -464,16 +464,28 @@ public class ContractService {
                         .build()).collect(Collectors.toList());
     }
 
-    public List<ContractBill> calcBills(Contract contract) {
+    public static List<ContractBill> calcBills(Contract contract) {
+        try {
+            Objects.requireNonNull(contract.getContractBeginTime(), "开始时间为空");
+            Objects.requireNonNull(contract.getContractEndTime(), "结束时间为空");
+            Objects.requireNonNull(contract.getContractMonthly(), "合同期为空");
+            Objects.requireNonNull(contract.getMonthlyRent(), "月租金为空");
+            Objects.requireNonNull(contract.getAdvancePayment(), "提前收款天数为空");
+            Objects.requireNonNull(contract.getPay(), "付款月数为空");
+            Objects.requireNonNull(contract.getFlowBet(), "押金为空");
+        } catch (NullPointerException e) {
+            log.error("{}  合同号:{}", e.getMessage(), contract.getContractNumber());
+            throw new BusinessException(e.getMessage());
+        }
         List<ContractBill> bills = new ArrayList<>();
         if (contract.getCheckInType() == CheckInType.TEAM
                 || contract.getCheckInType() == CheckInType.SCATTERED_BEDS) {
             int totalMonth = contract.getContractMonthly();
-            LocalDateTime dueTime = contract.getContractBeginTime().plusDays(contract.getAdvancePayment());
             int idx = 0;
             while (totalMonth > 0) {
                 LocalDateTime startTime = contract.getContractBeginTime().plusMonths(contract.getPay() * idx);
                 LocalDateTime endTime = startTime.plusMonths(contract.getPay());
+                LocalDateTime dueTime = startTime.plusDays(contract.getAdvancePayment());
                 if (endTime.isAfter(contract.getContractEndTime())) {
                     endTime = contract.getContractEndTime();
                 } else {
@@ -495,9 +507,8 @@ public class ContractService {
                         .build();
                 bills.add(bill);
                 totalMonth = Math.max(0, totalMonth - contract.getPay());
-                dueTime = dueTime.plusMonths(contract.getPay());
-                System.out.println(String.format("%s %s %s %s",
-                        bill.getStartTime(), bill.getEndTime(), bill.getDueTime(), bill.getMoney()));
+                log.info("{} {} {} {}",
+                        bill.getStartTime(), bill.getEndTime(), bill.getDueTime(), bill.getMoney());
             }
         } else if (contract.getCheckInType() == CheckInType.TEAM_POST_PAID) {
             YearMonth month = YearMonth.from(contract.getContractBeginTime());
@@ -511,10 +522,47 @@ public class ContractService {
                         .status(BillStatus.PENDING)
                         .build();
                 bills.add(bill);
-                System.out.println(String.format("%s %s %s %s",
-                        bill.getStartTime(), bill.getEndTime(), bill.getDueTime(), bill.getMoney()));
+                log.info("{} {} {} {}",
+                        bill.getStartTime(), bill.getEndTime(), bill.getDueTime(), bill.getMoney());
             }
         }
+        for (ContractBill bill : bills) {
+            bill.setCustomerId(contract.getCustomerId());
+            bill.setSaleId(contract.getSaleId());
+        }
         return bills;
     }
+
+    public void addContractBills() {
+        List<Long> ids = new ArrayList<>();
+        for (Contract contract : contractRepo.findAll()) {
+            try {
+                log.info("{} {} ~ {} 共{}个月 {}/月", contract.getContractNumber(),
+                        contract.getContractBeginTime(), contract.getContractEndTime(), contract.getContractMonthly(),
+                        contract.getMonthlyRent());
+                List<ContractBill> bills = calcBills(contract);
+                List<ContractCycle> cycles = contract.getCycles();
+                if (contract.getCheckInType() == CheckInType.TEAM_POST_PAID) {
+                    contract.setBills(bills);
+                } else {
+                    if (cycles.isEmpty()) {
+                        contract.setBills(bills);
+                    } else {
+                        if (cycles.size() != bills.size()) {
+                            ids.add(contract.getId());
+                        } else {
+                            for (int i = 0; i < bills.size(); i++) {
+                                bills.get(i).setDueTime(cycles.get(i).getCycleEndTime());
+                                bills.get(i).setStatus(cycles.get(i).getStatus());
+                            }
+                            contract.setBills(bills);
+                        }
+                    }
+                }
+                contractRepo.save(contract);
+            } catch (Exception e) {
+
+            }
+        }
+    }
 }

+ 9 - 2
src/main/java/com/izouma/zhumj/web/sale/ContractController.java

@@ -36,13 +36,14 @@ import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
 import java.util.Set;
+import java.util.concurrent.ThreadPoolExecutor;
 
 @RestController
 @RequestMapping("/contract")
 @AllArgsConstructor
 public class ContractController extends BaseController {
-    private ContractService   contractService;
-    private ContractRepo      contractRepo;
+    private ContractService contractService;
+    private ContractRepo    contractRepo;
 
     @PostMapping("/save")
     public Contract save(@RequestBody Contract record) {
@@ -85,5 +86,11 @@ public class ContractController extends BaseController {
         List<Contract> data = all(pageQuery, null).getContent();
         ExcelUtils.export(response, data);
     }
+
+    @PostMapping("/addContractBills")
+    public String addContractBills() {
+        new Thread(() -> contractService.addContractBills()).start();
+        return "OK";
+    }
 }
 

+ 24 - 15
src/main/vue/src/views/sale/ContractEdit.vue

@@ -762,6 +762,10 @@ export default {
                 this.$message.error('请输入合同开始时间');
                 return;
             }
+            if (!this.formData.contractMonthly) {
+                this.$message.error('请输入合同期');
+                return;
+            }
             if (!this.formData.contractEndTime) {
                 this.$message.error('请输入合同结束时间');
                 return;
@@ -793,28 +797,33 @@ export default {
             let pay = Number(this.formData.pay);
             let contractBeginTime = parseDate(this.formData.contractBeginTime);
             let contractEndTime = parseDate(this.formData.contractEndTime);
-            let billDate = addDays(contractBeginTime, -this.formData.advancePayment);
-            let maxDate = addMonths(contractEndTime, -pay);
             let bills = [];
-            let index = 0;
-            let totalMoney = 0;
+            let idx = 0;
+            let totalMonth = this.formData.contractMonthly;
             if (this.formData.checkInType == 'TEAM' || this.formData.checkInType == 'SCATTERED_BEDS') {
-                while (totalMoney < this.formData.contractTotalRent + this.formData.flowBet) {
-                    let money = pay * this.formData.monthlyRent;
-                    if (index === 0) {
+                while (totalMonth > 0) {
+                    let startTime = addMonths(contractBeginTime, pay * idx);
+                    let endTime = addMonths(startTime, pay);
+                    let dueTime = addDays(startTime, -this.formData.advancePayment);
+                    if (isAfter(endTime, contractEndTime)) {
+                        endTime = contractEndTime;
+                    } else {
+                        endTime = addSeconds(endTime, -1);
+                    }
+                    let month = totalMonth - pay <= 0 ? totalMonth : pay;
+                    let money = month * this.formData.monthlyRent;
+                    if (idx === 0) {
                         money += this.formData.flowBet;
                     }
                     bills.push({
-                        dueTime: formatDate(billDate),
-                        startTime: formatDate(addDays(billDate, this.formData.advancePayment)),
-                        endTime: formatDate(endOfDay(addMonths(addDays(billDate, this.formData.advancePayment), pay))),
-                        money: money
+                        dueTime: formatDate(dueTime),
+                        startTime: formatDate(startTime),
+                        endTime: formatDate(endTime),
+                        money: money,
+                        idx: ++idx
                     });
-                    totalMoney += money;
-                    index++;
-                    billDate = addMonths(billDate, pay);
+                    totalMonth = Math.max(0, totalMonth - pay);
                 }
-                bills[bills.length - 1].money += this.formData.contractTotalRent + this.formData.flowBet - totalMoney;
                 this.$set(this.formData, 'bills', bills);
             } else {
                 for (let i = 0; i < this.formData.contractMonthly + 1; i++) {

+ 53 - 49
src/test/java/com/izouma/zhumj/service/sale/ContractServiceTest.java

@@ -14,62 +14,66 @@ import org.springframework.beans.factory.annotation.Autowired;
  * @email 771190883@qq.com
  * @date 2019/10/29
  */
-public class ContractServiceTest  extends ZhumjApplicationTests {
+public class ContractServiceTest extends ZhumjApplicationTests {
 
     @Autowired
     private ContractService contractService;
 
     @Test
-    public void save(){
+    public void save() {
         String contractJson =
-"{\n" +
-        "\t\"contractNumber\": \"201910290001\",\n" +
-        "\t\"coFullName\": \"北京科技有限服务公司\",\n" +
-        "\t\"coSimpleName\": \"北京科技有限服务\",\n" +
-        "\t\"deposit\": 800,\n" +
-        "\t\"monthlyRent\": 1300,\n" +
-        "\t\"contractTotalRent\": 13600,\n" +
-        "\t\"contractBeginTime\": \"2019-10-01 00:00:00\",\n" +
-        "\t\"contractEndTime\": \"2020-10-01 00:00:00\",\n" +
-        "\t\"contractDays\": 360,\n" +
-        "\t\"contractMonthly\": 12,\n" +
-        "\t\"payType\": \"付二押一\",\n" +
-        "\t\"contractRenewals\": 0,\n" +
-        "\t\"saleName\": \"王昊\",\n" +
-        "\t\"saleId\": 352,\n" +
-        "\t\"departmentId\": 93952099,\n" +
-        "\t\"departmentName\": \"销售1组\",\n" +
-        "\t\"customerSource\": \"NETWORK\",\n" +
-        "\t\"contactAddress\": \"北京科技大厦\",\n" +
-        "\t\"status\": \"STAY_IN\",\n" +
-        "\t\"enabled\": true,\n" +
-        "\t\"contactsList\": [{\n" +
-        "\t\t\"contractNumber\": \"201910290001\",\n" +
-        "\t\t\"name\": \"王飞\",\n" +
-        "\t\t\"mobile\": \"152525212012\",\n" +
-        "\t\t\"enabled\": true\n" +
-        "\t}, {\n" +
-        "\t\t\"contractNumber\": \"201910290001\",\n" +
-        "\t\t\"name\": \"王楠\",\n" +
-        "\t\t\"mobile\": \"15252363252\",\n" +
-        "\t\t\"enabled\": true\n" +
-        "\t}],\n" +
-        "\t\"roomList\": [{\n" +
-        "\t\t\"contractNumber\": \"201910290001\",\n" +
-        "\t\t\"roomType\": \"两室一厅\",\n" +
-        "\t\t\"beds\": 6,\n" +
-        "\t\t\"price\": 500,\n" +
-        "\t\t\"enabled\": true\n" +
-        "\t}, {\n" +
-        "\t\t\"contractNumber\": \"201910290001\",\n" +
-        "\t\t\"roomType\": \"四室两厅\",\n" +
-        "\t\t\"beds\": 8,\n" +
-        "\t\t\"price\": 800,\n" +
-        "\t\t\"enabled\": true\n" +
-        "\t}]\n" +
-        "}";
-        Contract contract = JSONObject.parseObject(contractJson,Contract.class);
+                "{\n" +
+                        "\t\"contractNumber\": \"201910290001\",\n" +
+                        "\t\"coFullName\": \"北京科技有限服务公司\",\n" +
+                        "\t\"coSimpleName\": \"北京科技有限服务\",\n" +
+                        "\t\"deposit\": 800,\n" +
+                        "\t\"monthlyRent\": 1300,\n" +
+                        "\t\"contractTotalRent\": 13600,\n" +
+                        "\t\"contractBeginTime\": \"2019-10-01 00:00:00\",\n" +
+                        "\t\"contractEndTime\": \"2020-10-01 00:00:00\",\n" +
+                        "\t\"contractDays\": 360,\n" +
+                        "\t\"contractMonthly\": 12,\n" +
+                        "\t\"payType\": \"付二押一\",\n" +
+                        "\t\"contractRenewals\": 0,\n" +
+                        "\t\"saleName\": \"王昊\",\n" +
+                        "\t\"saleId\": 352,\n" +
+                        "\t\"departmentId\": 93952099,\n" +
+                        "\t\"departmentName\": \"销售1组\",\n" +
+                        "\t\"customerSource\": \"NETWORK\",\n" +
+                        "\t\"contactAddress\": \"北京科技大厦\",\n" +
+                        "\t\"status\": \"STAY_IN\",\n" +
+                        "\t\"enabled\": true,\n" +
+                        "\t\"contactsList\": [{\n" +
+                        "\t\t\"contractNumber\": \"201910290001\",\n" +
+                        "\t\t\"name\": \"王飞\",\n" +
+                        "\t\t\"mobile\": \"152525212012\",\n" +
+                        "\t\t\"enabled\": true\n" +
+                        "\t}, {\n" +
+                        "\t\t\"contractNumber\": \"201910290001\",\n" +
+                        "\t\t\"name\": \"王楠\",\n" +
+                        "\t\t\"mobile\": \"15252363252\",\n" +
+                        "\t\t\"enabled\": true\n" +
+                        "\t}],\n" +
+                        "\t\"roomList\": [{\n" +
+                        "\t\t\"contractNumber\": \"201910290001\",\n" +
+                        "\t\t\"roomType\": \"两室一厅\",\n" +
+                        "\t\t\"beds\": 6,\n" +
+                        "\t\t\"price\": 500,\n" +
+                        "\t\t\"enabled\": true\n" +
+                        "\t}, {\n" +
+                        "\t\t\"contractNumber\": \"201910290001\",\n" +
+                        "\t\t\"roomType\": \"四室两厅\",\n" +
+                        "\t\t\"beds\": 8,\n" +
+                        "\t\t\"price\": 800,\n" +
+                        "\t\t\"enabled\": true\n" +
+                        "\t}]\n" +
+                        "}";
+        Contract contract = JSONObject.parseObject(contractJson, Contract.class);
         contractService.save(contract);
     }
 
+    @Test
+    public void addContractBills() {
+        contractService.addContractBills();
+    }
 }