TBDataImportController.java 25.6 KB
Newer Older
frank.xa.zhang's avatar
frank.xa.zhang committed
1 2 3
package pwc.taxtech.atms.controller;

import com.alibaba.fastjson.JSONObject;
4
import io.swagger.annotations.ApiOperation;
frank.xa.zhang's avatar
frank.xa.zhang committed
5 6 7
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
8
import org.springframework.web.bind.annotation.*;
9
import pwc.taxtech.atms.common.CommonUtils;
10 11 12
import pwc.taxtech.atms.dto.OperationResultDto;
import pwc.taxtech.atms.dto.vat.ClearTableDto;
import pwc.taxtech.atms.dto.vat.ImportBalanceDto;
13
import pwc.taxtech.atms.dto.vatdto.*;
14 15 16
import pwc.taxtech.atms.service.DataImportService;
import pwc.taxtech.atms.service.ICitTBDataImportService;
import pwc.taxtech.atms.service.IdentityService;
frank.xa.zhang's avatar
frank.xa.zhang committed
17
import pwc.taxtech.atms.vat.service.TBDataImportService;
frank.xa.zhang's avatar
frank.xa.zhang committed
18 19

import java.math.BigDecimal;
20
import java.util.*;
frank.xa.zhang's avatar
frank.xa.zhang committed
21 22 23 24 25 26 27 28 29
import java.util.stream.Collectors;

@RequestMapping(value = "api/v1/DataImport")
@RestController
public class TBDataImportController {

    @Autowired
    TBDataImportService tbDataImportService;

30 31 32
    static final String CIT_CATEGORY = "CIT";

    static final String STANDARD_CATEGORY = "STD";
33 34 35 36 37 38 39
    @Autowired
    private ICitTBDataImportService citTbDataImport;
    @Autowired
    private DataImportService dataImportService;
    @Autowired
    private IdentityService identityService;

frank.xa.zhang's avatar
frank.xa.zhang committed
40 41

    @RequestMapping(value = "GetBalanceDataForDisplay", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
42 43
    public @ResponseBody
    String getBalanceDataForDisplay(@RequestBody JSONObject param) {
frank.xa.zhang's avatar
frank.xa.zhang committed
44 45 46 47 48 49
        String category = param.get("category").toString();
        int fromPeriod = Integer.parseInt(param.get("fromPeriod").toString());
        int toPeriod = Integer.parseInt(param.get("toPeriod").toString());
        String criteria = param.get("criteria").toString();

        TrialBalanceFilter filter = new TrialBalanceFilter();
50
        if (StringUtils.isNotBlank(criteria) && !"null".equals(criteria)) {
frank.xa.zhang's avatar
frank.xa.zhang committed
51 52 53 54 55 56
            JSONObject jsonObject = new JSONObject();
            filter = jsonObject.getObject(criteria, TrialBalanceFilter.class);
        } else {
            filter = null;
        }

57
        return convertBalanceDataToUiGridTree(fromPeriod, toPeriod, filter, category);
frank.xa.zhang's avatar
frank.xa.zhang committed
58 59
    }

60
    private String convertBalanceDataToUiGridTree(int fromPeriod, int toPeriod, TrialBalanceFilter filter, String category) {
frank.xa.zhang's avatar
frank.xa.zhang committed
61 62 63
        String result = "";
        List<TrialBalanceDto> balanceList = new ArrayList<>();

64
        if (category.equals(CIT_CATEGORY)) {
frank.xa.zhang's avatar
frank.xa.zhang committed
65 66 67 68 69 70
            balanceList = tbDataImportService.getTrialBalanceData(fromPeriod, toPeriod);
        } else {
            balanceList = tbDataImportService.getTrialBalanceStdData(fromPeriod, toPeriod);
        }

        if (filter != null && !balanceList.isEmpty()) {
71
            balanceList = filterTrialBalanceList(filter, balanceList);
frank.xa.zhang's avatar
frank.xa.zhang committed
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
        }

        if (!balanceList.isEmpty()) {
            balanceList = balanceList.stream().sorted(Comparator.comparing(TrialBalanceDto::getAcctCode).thenComparing(TrialBalanceDto::getAccotCodeLength)).collect(Collectors.toList());
        }

        BigDecimal subTotalBegCreditBal = BigDecimal.ZERO;
        BigDecimal subTotalBegDebitBal = BigDecimal.ZERO;
        BigDecimal subTotalEndCreditBal = BigDecimal.ZERO;
        BigDecimal subTotalEndDebitBal = BigDecimal.ZERO;
        BigDecimal subTotalCreditBal = BigDecimal.ZERO;
        BigDecimal subTotalDebitBal = BigDecimal.ZERO;

        List<TrialBalanceDto> subTotalList = new ArrayList<>(balanceList);

        boolean isFilterEmpty = true;

        if (filter != null &&
                (StringUtils.isNotBlank(filter.getAccountCode())
                        || StringUtils.isNotBlank(filter.getAccountName())
                        || filter.getAccumulatedCreditAmountFrom() != null
                        || filter.getAccumulatedCreditAmountTo() != null
                        || filter.getAccumulatedDebitAmountFrom() != null
                        || filter.getAccumulatedDebitAmountTo() != null
                        || filter.getCreditClosingBalanceFrom() != null
                        || filter.getCreditClosingBalanceTo() != null
                        || filter.getCreditOpeningBalanceFrom() != null
                        || filter.getCreditOpeningBalanceTo() != null
                        || filter.getDebitClosingBalanceFrom() != null
                        || filter.getDebitClosingBalanceTo() != null
                        || filter.getDebitOpeningBalanceFrom() != null
                        || filter.getDebitOpeningBalanceTo() != null
                        || filter.getDebitClosingBalanceFrom() != null)) {
            isFilterEmpty = false;

107
            List<String> primaryList = subTotalList.stream().filter(x -> "0".equals(x.getParentCode())).map(TrialBalanceDto::getAcctCode).collect(Collectors.toList());
frank.xa.zhang's avatar
frank.xa.zhang committed
108 109 110 111 112 113 114 115 116 117 118 119

            if (!primaryList.isEmpty()) {

                for (String parent : primaryList) {
                    int parentSize = parent.length();
                    subTotalList.removeAll(
                            subTotalList.stream().filter(x -> x.getAcctCode().length() != parentSize
                                    && x.getAcctCode().length() >= parentSize
                                    && x.getAcctCode().substring(0, parentSize).equals(parent)).collect(Collectors.toList()));
                }

                if (!subTotalList.isEmpty()) {
120 121 122 123 124 125
                    subTotalBegCreditBal = BigDecimal.valueOf(subTotalList.stream().mapToDouble(y -> Objects.requireNonNull(y.getBegCreditBal()).doubleValue()).summaryStatistics().getSum());
                    subTotalBegDebitBal = BigDecimal.valueOf(subTotalList.stream().mapToDouble(y -> Objects.requireNonNull(y.getBegDebitBal()).doubleValue()).summaryStatistics().getSum());
                    subTotalEndCreditBal = BigDecimal.valueOf(subTotalList.stream().mapToDouble(y -> Objects.requireNonNull(y.getEndCreditBal()).doubleValue()).summaryStatistics().getSum());
                    subTotalEndDebitBal = BigDecimal.valueOf(subTotalList.stream().mapToDouble(y -> Objects.requireNonNull(y.getEndDebitBal()).doubleValue()).summaryStatistics().getSum());
                    subTotalCreditBal = BigDecimal.valueOf(subTotalList.stream().mapToDouble(y -> Objects.requireNonNull(y.getCreditBal()).doubleValue()).summaryStatistics().getSum());
                    subTotalDebitBal = BigDecimal.valueOf(subTotalList.stream().mapToDouble(y -> Objects.requireNonNull(y.getDebitBal()).doubleValue()).summaryStatistics().getSum());
frank.xa.zhang's avatar
frank.xa.zhang committed
126 127
                }
            } else {
128 129 130 131 132 133
                subTotalBegCreditBal = BigDecimal.valueOf(subTotalList.stream().mapToDouble(y -> Objects.requireNonNull(y.getBegCreditBal()).doubleValue()).summaryStatistics().getSum());
                subTotalBegDebitBal = BigDecimal.valueOf(subTotalList.stream().mapToDouble(y -> Objects.requireNonNull(y.getBegDebitBal()).doubleValue()).summaryStatistics().getSum());
                subTotalEndCreditBal = BigDecimal.valueOf(subTotalList.stream().mapToDouble(y -> Objects.requireNonNull(y.getEndCreditBal()).doubleValue()).summaryStatistics().getSum());
                subTotalEndDebitBal = BigDecimal.valueOf(subTotalList.stream().mapToDouble(y -> Objects.requireNonNull(y.getEndDebitBal()).doubleValue()).summaryStatistics().getSum());
                subTotalCreditBal = BigDecimal.valueOf(subTotalList.stream().mapToDouble(y -> Objects.requireNonNull(y.getCreditBal()).doubleValue()).summaryStatistics().getSum());
                subTotalDebitBal = BigDecimal.valueOf(subTotalList.stream().mapToDouble(y -> Objects.requireNonNull(y.getDebitBal()).doubleValue()).summaryStatistics().getSum());
frank.xa.zhang's avatar
frank.xa.zhang committed
134 135 136
            }
        } else {
            isFilterEmpty = true;
137 138 139 140 141 142 143 144 145 146 147 148
            subTotalBegCreditBal = BigDecimal.valueOf(subTotalList.stream().filter(x -> "0".equals(x.getParentCode()))
                    .mapToDouble(y -> Objects.requireNonNull(y.getBegCreditBal()).doubleValue()).summaryStatistics().getSum());
            subTotalBegDebitBal = BigDecimal.valueOf(subTotalList.stream().filter(x -> "0".equals(x.getParentCode()))
                    .mapToDouble(y -> Objects.requireNonNull(y.getBegDebitBal()).doubleValue()).summaryStatistics().getSum());
            subTotalEndCreditBal = BigDecimal.valueOf(subTotalList.stream().filter(x -> "0".equals(x.getParentCode()))
                    .mapToDouble(y -> Objects.requireNonNull(y.getEndCreditBal()).doubleValue()).summaryStatistics().getSum());
            subTotalEndDebitBal = BigDecimal.valueOf(subTotalList.stream().filter(x -> "0".equals(x.getParentCode()))
                    .mapToDouble(y -> Objects.requireNonNull(y.getEndDebitBal()).doubleValue()).summaryStatistics().getSum());
            subTotalCreditBal = BigDecimal.valueOf(subTotalList.stream().filter(x -> "0".equals(x.getParentCode()))
                    .mapToDouble(y -> Objects.requireNonNull(y.getCreditBal()).doubleValue()).summaryStatistics().getSum());
            subTotalDebitBal = BigDecimal.valueOf(subTotalList.stream().filter(x -> "0".equals(x.getParentCode()))
                    .mapToDouble(y -> Objects.requireNonNull(y.getDebitBal()).doubleValue()).summaryStatistics().getSum());
frank.xa.zhang's avatar
frank.xa.zhang committed
149 150 151 152
        }


        //todo: add the code below here
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
        List<JsonTrialBalanceTreeNode> tree = new ArrayList<>();
        List<JsonTrialBalanceTreeNode> balanceTree = new ArrayList<>();
        JsonTrialBalanceTreeNode note;

        for (TrialBalanceDto item : balanceList) {
            note = new JsonTrialBalanceTreeNode();
            note.setAcctCode(item.getAcctCode());
            note.setAccountName(item.getAccountName());
            note.setBalanceId(item.getBalanceId());
            note.setBegCreditBal(Objects.requireNonNull(item.getBegCreditBal()).toString());
            note.setBegDebitBal(Objects.requireNonNull(item.getBegDebitBal()).toString());
            note.setEndCreditBal(Objects.requireNonNull(item.getEndCreditBal()).toString());
            note.setEndDebitBal(Objects.requireNonNull(item.getEndDebitBal()).toString());
            note.setParentCode(item.getParentCode());
            note.setCreditBal(Objects.requireNonNull(item.getCreditBal()).toString());
            note.setDebitBal(Objects.requireNonNull(item.getDebitBal()).toString());
            note.setAcctProp(item.getAcctProp() != null ? item.getAcctProp().toString() : "7"); // 7 means "Other" category
            note.setDirection(Integer.toString(item.getDirection()));
            balanceTree.add(note);
        }

        BalanceWithSubTotalsResultDto<JsonTrialBalanceTreeNode> balanceWithSubTotalsInfo = new BalanceWithSubTotalsResultDto<>();
        BalanceSubTotalDto subtotalsInfo = new BalanceSubTotalDto();

        subtotalsInfo.setBegCreditBal(subTotalBegCreditBal.toString());
        subtotalsInfo.setBegDebitBal(subTotalBegDebitBal.toString());
        subtotalsInfo.setEndCreditBal(subTotalEndCreditBal.toString());
        subtotalsInfo.setEndDebitBal(subTotalEndDebitBal.toString());
        subtotalsInfo.setCreditBal(subTotalCreditBal.toString());
        subtotalsInfo.setDebitBal(subTotalDebitBal.toString());

        if (isFilterEmpty) {
            initTree(tree, "0", balanceTree);
        } else {
            JsonTrialBalanceTreeNode tempNode;
            if (!balanceTree.isEmpty()) {
                for (JsonTrialBalanceTreeNode row : balanceTree) {
                    tempNode = new JsonTrialBalanceTreeNode();
                    CommonUtils.copyProperties(row, tempNode);
                    tempNode.setAccountName(StringUtils.removeStart(row.getAccountName(), "@"));
                    tempNode.setChildren(new ArrayList<>());
                    tree.add(tempNode);
                }
            }
        }
frank.xa.zhang's avatar
frank.xa.zhang committed
198

199 200 201 202 203 204 205 206
        balanceWithSubTotalsInfo.setList(tree.stream()
                .sorted(Comparator.comparing(JsonTrialBalanceTreeNode::getAcctCode)
                        .thenComparing(JsonTrialBalanceTreeNode::getAcctCodeLength))
                .collect(Collectors.toList()));

        balanceWithSubTotalsInfo.setSubTotals(subtotalsInfo);

        result = JSONObject.toJSONString(balanceWithSubTotalsInfo);
frank.xa.zhang's avatar
frank.xa.zhang committed
207
        return result;
208 209 210 211 212 213 214 215 216 217 218 219 220
    }

    private void initTree(List<JsonTrialBalanceTreeNode> nodes, String parentCode, List<JsonTrialBalanceTreeNode> sources) {
        JsonTrialBalanceTreeNode tempNode;
        //递归寻找子节点
        List<JsonTrialBalanceTreeNode> tempTree = sources.stream().filter(item -> item.getParentCode().equals(parentCode)).collect(Collectors.toList());
        for (JsonTrialBalanceTreeNode row : tempTree) {
            tempNode = new JsonTrialBalanceTreeNode();
            CommonUtils.copyProperties(row, tempNode);
            tempNode.setChildren(new ArrayList<>());
            nodes.add(tempNode);
            initTree(tempNode.getChildren(), row.getAcctCode(), sources);
        }
frank.xa.zhang's avatar
frank.xa.zhang committed
221
    }
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372

    private static List<TrialBalanceDto> filterTrialBalanceList(TrialBalanceFilter filter, List<TrialBalanceDto> balanceList) {
        // Account Code: support multiple codes seperated by space:
        if (StringUtils.isNotBlank(filter.getAccountCode())) {
            List<String> codes = new ArrayList<>(Arrays.asList(filter.getAccountCode().split(" ")));
            balanceList = balanceList.stream().filter(x -> codes.contains(x.getAcctCode())).collect(Collectors.toList());
        }
        // Account Name: support multiple codes seperated by space:
        if (StringUtils.isNotBlank(filter.getAccountName())) {
            List<String> names = new ArrayList<>(Arrays.asList(filter.getAccountName().split(" ")));
            List<TrialBalanceDto> ds = new ArrayList<>();
            for (String name : names) {
                ds.addAll(balanceList.stream().filter(x -> x.getAccountName().contains(name)).collect(Collectors.toList()));
            }
            balanceList = ds;
        }

        // Debit Opening Balance:
        if (filter.getDebitOpeningBalanceFrom() != null || filter.getDebitOpeningBalanceTo() != null) {
            if (filter.getDebitOpeningBalanceFrom() != null && filter.getDebitOpeningBalanceTo() != null) {
                balanceList = balanceList.stream()
                        .filter(x -> x.getBegDebitBal() != null
                                && x.getBegDebitBal().doubleValue() >= filter.getDebitOpeningBalanceFrom().doubleValue()
                                && x.getBegDebitBal().doubleValue() <= filter.getDebitOpeningBalanceTo().doubleValue())
                        .collect(Collectors.toList());
            } else if (filter.getDebitOpeningBalanceFrom() != null) {
                balanceList = balanceList.stream()
                        .filter(x -> x.getBegDebitBal() != null
                                && x.getBegDebitBal().doubleValue() >= filter.getDebitOpeningBalanceFrom().doubleValue())
                        .collect(Collectors.toList());
            } else {
                balanceList = balanceList.stream().filter(x -> x.getBegDebitBal() != null
                        && x.getBegDebitBal().doubleValue() <= filter.getDebitOpeningBalanceTo().doubleValue())
                        .collect(Collectors.toList());
            }
        }

        // Credit Opening Balance:
        if (filter.getCreditOpeningBalanceFrom() != null || filter.getCreditOpeningBalanceTo() != null) {
            if (filter.getCreditOpeningBalanceFrom() != null && filter.getCreditOpeningBalanceTo() != null) {
                balanceList = balanceList.stream()
                        .filter(x -> x.getBegDebitBal() != null
                                && x.getBegDebitBal().doubleValue() >= filter.getCreditOpeningBalanceFrom().doubleValue()
                                && x.getBegDebitBal().doubleValue() <= filter.getCreditOpeningBalanceTo().doubleValue())
                        .collect(Collectors.toList());
            } else if (filter.getCreditOpeningBalanceFrom() != null) {
                balanceList = balanceList.stream()
                        .filter(x -> x.getBegDebitBal() != null
                                && x.getBegDebitBal().doubleValue() >= filter.getCreditOpeningBalanceFrom().doubleValue())
                        .collect(Collectors.toList());
            } else {
                balanceList = balanceList.stream().filter(x -> x.getBegDebitBal() != null
                        && x.getBegDebitBal().doubleValue() <= filter.getCreditOpeningBalanceTo().doubleValue())
                        .collect(Collectors.toList());
            }
        }

        // Accumulated Debit Balance:
        if (filter.getAccumulatedDebitAmountFrom() != null || filter.getAccumulatedDebitAmountTo() != null) {
            if (filter.getAccumulatedDebitAmountFrom() != null && filter.getAccumulatedDebitAmountTo() != null) {
                balanceList = balanceList.stream()
                        .filter(x -> x.getBegDebitBal() != null
                                && x.getBegDebitBal().doubleValue() >= filter.getAccumulatedDebitAmountFrom().doubleValue()
                                && x.getBegDebitBal().doubleValue() <= filter.getAccumulatedDebitAmountTo().doubleValue())
                        .collect(Collectors.toList());
            } else if (filter.getAccumulatedDebitAmountFrom() != null) {
                balanceList = balanceList.stream()
                        .filter(x -> x.getBegDebitBal() != null
                                && x.getBegDebitBal().doubleValue() >= filter.getAccumulatedDebitAmountFrom().doubleValue())
                        .collect(Collectors.toList());
            } else {
                balanceList = balanceList.stream().filter(x -> x.getBegDebitBal() != null
                        && x.getBegDebitBal().doubleValue() <= filter.getAccumulatedDebitAmountTo().doubleValue())
                        .collect(Collectors.toList());
            }
        }

        // Accumulated Credit Balance:
        if (filter.getAccumulatedCreditAmountFrom() != null || filter.getAccumulatedCreditAmountTo() != null) {
            if (filter.getAccumulatedCreditAmountFrom() != null && filter.getAccumulatedCreditAmountTo() != null) {
                balanceList = balanceList.stream()
                        .filter(x -> x.getBegDebitBal() != null
                                && x.getBegDebitBal().doubleValue() >= filter.getAccumulatedCreditAmountFrom().doubleValue()
                                && x.getBegDebitBal().doubleValue() <= filter.getAccumulatedCreditAmountTo().doubleValue())
                        .collect(Collectors.toList());
            } else if (filter.getAccumulatedCreditAmountFrom() != null) {
                balanceList = balanceList.stream()
                        .filter(x -> x.getBegDebitBal() != null
                                && x.getBegDebitBal().doubleValue() >= filter.getAccumulatedCreditAmountFrom().doubleValue())
                        .collect(Collectors.toList());
            } else {
                balanceList = balanceList.stream().filter(x -> x.getBegDebitBal() != null
                        && x.getBegDebitBal().doubleValue() <= filter.getAccumulatedCreditAmountTo().doubleValue())
                        .collect(Collectors.toList());
            }
        }

        // Closing Debit Balance:
        if (filter.getDebitClosingBalanceFrom() != null || filter.getDebitClosingBalanceTo() != null) {
            if (filter.getDebitClosingBalanceFrom() != null && filter.getDebitClosingBalanceTo() != null) {
                balanceList = balanceList.stream()
                        .filter(x -> x.getBegDebitBal() != null
                                && x.getBegDebitBal().doubleValue() >= filter.getDebitClosingBalanceFrom().doubleValue()
                                && x.getBegDebitBal().doubleValue() <= filter.getDebitClosingBalanceTo().doubleValue())
                        .collect(Collectors.toList());
            } else if (filter.getDebitClosingBalanceFrom() != null) {
                balanceList = balanceList.stream()
                        .filter(x -> x.getBegDebitBal() != null
                                && x.getBegDebitBal().doubleValue() >= filter.getAccumulatedCreditAmountFrom().doubleValue())
                        .collect(Collectors.toList());
            } else {
                balanceList = balanceList.stream().filter(x -> x.getBegDebitBal() != null
                        && x.getBegDebitBal().doubleValue() <= filter.getDebitClosingBalanceTo().doubleValue())
                        .collect(Collectors.toList());
            }
        }

        // Closing Credit Balance:
        if (filter.getCreditClosingBalanceFrom() != null || filter.getCreditClosingBalanceTo() != null) {
            if (filter.getCreditClosingBalanceFrom() != null && filter.getCreditClosingBalanceTo() != null) {
                balanceList = balanceList.stream()
                        .filter(x -> x.getBegDebitBal() != null
                                && x.getBegDebitBal().doubleValue() >= filter.getCreditClosingBalanceFrom().doubleValue()
                                && x.getBegDebitBal().doubleValue() <= filter.getCreditClosingBalanceTo().doubleValue())
                        .collect(Collectors.toList());
            } else if (filter.getDebitClosingBalanceFrom() != null) {
                balanceList = balanceList.stream()
                        .filter(x -> x.getBegDebitBal() != null
                                && x.getBegDebitBal().doubleValue() >= filter.getCreditClosingBalanceFrom().doubleValue())
                        .collect(Collectors.toList());
            } else {
                balanceList = balanceList.stream().filter(x -> x.getBegDebitBal() != null
                        && x.getBegDebitBal().doubleValue() <= filter.getCreditClosingBalanceTo().doubleValue())
                        .collect(Collectors.toList());
            }
        }

        assert filter.isHideAllZeroRecords() != null;
        if (filter.isHideAllZeroRecords()) {
            balanceList = balanceList.stream()
                    .filter(x -> ((x.getBegCreditBal() != null && x.getBegCreditBal().doubleValue() != 0)
                            || (x.getEndDebitBal() != null && x.getEndDebitBal().doubleValue() != 0)
                            || (x.getBegDebitBal() != null && x.getBegDebitBal().doubleValue() != 0)
                            || (x.getEndDebitBal() != null && x.getEndDebitBal().doubleValue() != 0)
                            || (x.getCreditBal() != null && x.getCreditBal().doubleValue() != 0)
                            || (x.getDebitBal() != null && x.getDebitBal().doubleValue() != 0)))
                    .collect(Collectors.toList());
        }
        return balanceList;
    }

373 374 375 376 377 378 379 380 381 382 383 384 385 386
    @ApiOperation(value = "clearTableByPeriods", notes = "")
    @RequestMapping(value = "clearTableByPeriods", method = RequestMethod.POST)
    public OperationResultDto<Boolean> clearTableByPeriods(@RequestBody ClearTableDto clearTb) {
        return citTbDataImport.clearTableByPeriods(clearTb.getPeriods());
    }

    @ApiOperation(value = "ImportBalance", notes = "")
    @RequestMapping(value = "ImportBalance", method = RequestMethod.POST)
    public OperationResultDto importBalanceList(@RequestBody ImportBalanceDto importDto) {
        return dataImportService.importTrialBalance(importDto.getBalanceList(), importDto.getImportType(),
                importDto.getServiceTypeId(),
                identityService.getIdentityUser().getID()
        );
    }
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
    @RequestMapping(value = "GetParentCodesForDisplay", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody
    String GetParentCodesForDisplay(String category) {
        return convertParentCodeListToUiGridTree(category);
    }

    private String convertParentCodeListToUiGridTree(String category) {
        String result = "";
        List<VatEnterpriseAccountDto> parentCodesList = null;

        if (category.equals(CIT_CATEGORY)) {
            parentCodesList = tbDataImportService.getEnterpriseAccount();
        } else if (category.equals(STANDARD_CATEGORY)) {
            parentCodesList = tbDataImportService.getStandardAccount();
        }

        List<JsonParentCodesLookup> tree = new ArrayList<>();
        List<JsonParentCodesLookup> balanceTree = new ArrayList<>();
        parentCodesList.forEach(a -> {
            JsonParentCodesLookup note;
            note = new JsonParentCodesLookup();
            note.setAcctCode(a.getAcctCode());
            note.setAccountName(a.getName());
            note.setParentCode(StringUtils.isBlank(a.getParentCode()) ? "0" : a.getParentCode());
            note.setAcctProp(a.getAcctProp() != null ? a.getAcctProp().toString() : "7");
            note.setDirection(Integer.toString(a.getDirection()));
            balanceTree.add(note);
        });
        initParentCodeTree(tree, "0", balanceTree);
        tree = tree.stream().sorted(Comparator.comparing(JsonParentCodesLookup::getAcctCode).thenComparing(JsonParentCodesLookup::getAcctCodeLength)).collect(Collectors.toList());
        result = JSONObject.toJSONString(tree);
        return result;
    }

    private void initParentCodeTree(List<JsonParentCodesLookup> nodes, String parentCode, List<JsonParentCodesLookup> sources) {
        JsonParentCodesLookup tempNode;
        List<JsonParentCodesLookup> tempTree = sources.stream().filter(a -> a.getParentCode().equals(parentCode)).collect(Collectors.toList());
        for (JsonParentCodesLookup jsonParentCodesLookup : tempTree) {
            tempNode = new JsonParentCodesLookup();
            tempNode.setAcctCode(jsonParentCodesLookup.getAcctCode());
            tempNode.setAccountName(jsonParentCodesLookup.getAccountName());
            tempNode.setParentCode(jsonParentCodesLookup.getParentCode());
            tempNode.setAcctProp(jsonParentCodesLookup.getAcctProp());
            tempNode.setDirection(jsonParentCodesLookup.getDirection());
            tempNode.setChildren(new ArrayList<>());
            nodes.add(tempNode);
            initParentCodeTree(tempNode.getChildren(), jsonParentCodesLookup.getAcctCode(), sources);
        }
    }
frank.xa.zhang's avatar
frank.xa.zhang committed
436
}
437