OutputInvoiceController.java 10 KB
Newer Older
1 2
package pwc.taxtech.atms.controller;

3
import com.github.pagehelper.PageInfo;
4
import com.github.pagehelper.util.StringUtil;
sherlock's avatar
sherlock committed
5
import com.google.common.collect.Lists;
6 7
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
8
import org.springframework.http.ResponseEntity;
9
import org.springframework.web.bind.annotation.*;
10
import org.springframework.web.multipart.MultipartHttpServletRequest;
11
import pwc.taxtech.atms.common.util.BeanUtil;
12
import pwc.taxtech.atms.constant.enums.EnumModule;
sherlock's avatar
sherlock committed
13
import pwc.taxtech.atms.constant.enums.KeyValueConfigResultType;
14
import pwc.taxtech.atms.dpo.PagingResultDto;
15
import pwc.taxtech.atms.dto.vatdto.ImportOutputInvoiceDto;
16
import pwc.taxtech.atms.dto.vatdto.QueryEvidenceDto;
17
import pwc.taxtech.atms.service.impl.IdentityServiceImpl;
18
import pwc.taxtech.atms.thirdparty.ExcelUtil;
19 20
import pwc.taxtech.atms.vat.dpo.OutputVATInvoiceInfoDto;
import pwc.taxtech.atms.vat.dpo.QueryOutputDto;
21
import pwc.taxtech.atms.vat.entity.ImportFile;
22
import pwc.taxtech.atms.vat.entity.OutputInvoice;
23
import pwc.taxtech.atms.vat.service.impl.FileUploadAdapter;
24
import pwc.taxtech.atms.vat.service.impl.OutputInvoiceServiceImpl;
sherlock's avatar
sherlock committed
25
import pwc.taxtech.atms.vat.service.impl.report.functions.FormulaHelper;
26

27 28 29
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
sherlock's avatar
sherlock committed
30
import java.math.BigDecimal;
31
import java.util.*;
32

33 34
import static javax.servlet.http.HttpServletResponse.SC_NO_CONTENT;
import static javax.servlet.http.HttpServletResponse.SC_OK;
35 36
import static pwc.taxtech.atms.constant.Constant.FILE_NAME;
import static pwc.taxtech.atms.constant.Constant.TEMP_FILE_NAME;
37

38 39 40 41
@RestController
@RequestMapping(value = "api/v1/outputInvoiceImport")
public class OutputInvoiceController {

42 43
    @Autowired
    private BeanUtil beanUtil;
44
    @Autowired
45
    OutputInvoiceServiceImpl outputInvoiceService;
46
    @Autowired
47
    private IdentityServiceImpl identityService;
48 49
    @Autowired
    private FileUploadAdapter fileUploadAdapter;
50 51

    @RequestMapping(value = "queryOutputInvoiceList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
52 53 54
    public PageInfo<OutputInvoice> queryOutputInvoiceList(@RequestBody QueryOutputDto queryDto,
                                                          @RequestHeader("from") String projectId) {
            return outputInvoiceService.queryOutputInvoiceList(queryDto,projectId);
55
    }
56

sherlock's avatar
sherlock committed
57 58 59
    @RequestMapping(value = "queryOutputInvoiceAllList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public List<OutputInvoice> queryOutputInvoiceAllList(@RequestBody QueryOutputDto queryDto,
                                                          @RequestHeader("from") String projectId) {
sherlock's avatar
sherlock committed
60 61 62 63 64 65 66 67 68 69 70 71 72
        List<OutputInvoice> list = outputInvoiceService.getOutputInvoice(queryDto, projectId);
        double hjje = 0;
        double hjse = 0;
        for(OutputInvoice outputInvoice : list){
            hjje += FormulaHelper.roundValue(new BigDecimal(outputInvoice.getHJJE()), KeyValueConfigResultType.Accounting, true, null).doubleValue();
            hjse += FormulaHelper.roundValue(new BigDecimal(outputInvoice.getHJSE()), KeyValueConfigResultType.Accounting, true, null).doubleValue();
        }
        list = Lists.newArrayList();
        OutputInvoice o = new OutputInvoice();
        o.setHJJE(hjje + "");
        o.setHJSE(hjse + "");
        list.add(o);
        return list;
sherlock's avatar
sherlock committed
73 74
    }

75
    @RequestMapping(value = "getExportOutputInvoiceList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
76
    public void downloadInvoiceQueryData(@RequestBody QueryOutputDto paras, @RequestHeader("from") String projectId, HttpServletResponse response) {
77
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
sherlock's avatar
sherlock committed
78 79 80
        response.addHeader("Access-Control-Expose-Headers", "Content-Type,Content-Disposition,x-file-name");
        String fileName = paras.getPeriodStart() + "-" + paras.getPeriodEnd();
        response.setCharacterEncoding("UTF-8");
81 82
        OutputStream os = null;
        try {
sherlock's avatar
sherlock committed
83 84
            response.addHeader("Content-Disposition", "attachment;filename="
                    + UUID.randomUUID() + ".xls");
sherlock's avatar
sherlock committed
85
            response.addHeader("x-file-name", fileName);
86
            os = response.getOutputStream();
87
            int count = getDownloadFilePath(paras, projectId, os);
88

89
            if (count == 0) {
90
                response.setStatus(SC_NO_CONTENT);
91
            } else {
92
                response.setStatus(SC_OK);
93 94 95 96 97 98
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

99 100 101 102 103
    @RequestMapping(value = "getEvidenceList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseEntity getEvidenceList(@RequestBody QueryEvidenceDto queryDto) {
        return ResponseEntity.ok().body(outputInvoiceService.getEvidenceList(queryDto));
    }

104
    @RequestMapping(value = "getOutputInvoiceList/{period}", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
105 106 107 108
    public ResponseEntity getEvidenceList(@PathVariable Integer period) {
        return ResponseEntity.ok().body(outputInvoiceService.getOutputInvoiceList(period));
    }

109 110
    private int getDownloadFilePath(QueryOutputDto paras, String projectId, OutputStream outputStream) {
        List<OutputVATInvoiceInfoDto> list = outputInvoiceService.getExportOutputInvoiceList(paras, projectId).getData();
111 112 113
        if (list.size() == 0) {
            return 0;
        }
sherlock's avatar
sherlock committed
114
        pwc.taxtech.atms.dto.vatdto.OutputVATInvoiceInfoDto outputVATInvoiceInfoDto = new pwc.taxtech.atms.dto.vatdto.OutputVATInvoiceInfoDto();
sherlock's avatar
sherlock committed
115 116 117

        outputVATInvoiceInfoDto.setTaxAmount(list.stream().map(OutputVATInvoiceInfoDto::getTaxAmount).reduce(BigDecimal::add).get());
        outputVATInvoiceInfoDto.setAmount(list.stream().map(OutputVATInvoiceInfoDto::getAmount).reduce(BigDecimal::add).get());
sherlock's avatar
sherlock committed
118 119 120
        pwc.taxtech.atms.dto.vatdto.OutputVATInvoiceInfoDto black = new pwc.taxtech.atms.dto.vatdto.OutputVATInvoiceInfoDto();
//        list.add(black);
//        list.add(outputVATInvoiceInfoDto);
121 122 123 124 125 126
        List<pwc.taxtech.atms.dto.vatdto.OutputVATInvoiceInfoDto> list2 = new ArrayList<>();
        list.stream().forEach(x -> {
                pwc.taxtech.atms.dto.vatdto.OutputVATInvoiceInfoDto d =
                new pwc.taxtech.atms.dto.vatdto.OutputVATInvoiceInfoDto();
                d = beanUtil.copyProperties(x,d);
                d.setInvoiceTypeName(d.getInvoiceTypeName());
eddie.woo's avatar
eddie.woo committed
127
                d.setTaxRateS(x.getTaxRate().doubleValue() * 100  + "%");
128 129
                list2.add(d);
        });
sherlock's avatar
sherlock committed
130 131 132
        list2.add(black);
        list2.add(outputVATInvoiceInfoDto);

sherlock's avatar
sherlock committed
133
        Map<String, String> header = new LinkedHashMap<>();
134 135 136 137 138
        header.put("InvoiceTypeName", "发票类型");
        header.put("ClassCode", "发票代码");
        header.put("InvoiceNumber", "发票号码");
        header.put("BuyerName", "购方企业名称");
        header.put("BuyerTaxNumber", "购方税号");
139 140
//        header.put("BankAccount", "银行账号");
//        header.put("PhoneNum", "地址电话");
141
        header.put("InvoiceDate", "开票日期");
142 143
//        header.put("CodeVersion", "商品编码版本号");
//        header.put("DocumentNum", "单据号");
sherlock's avatar
sherlock committed
144
        header.put("ProductName", "商品名称");
145 146
//        header.put("ProductStandar", "规格");
//        header.put("Unit", "单位");
sherlock's avatar
sherlock committed
147 148
        header.put("Quantity", "数量");
        header.put("UnitPrice", "单价");
149
        header.put("Amount", "金额");
sherlock's avatar
sherlock committed
150
        header.put("TaxRateS", "税率");
151
        header.put("TaxAmount", "税额");
152
//        header.put("TaxClassCode", "税收分类编码");
sherlock's avatar
sherlock committed
153

154
        ExcelUtil.exportExcel(header, list2, outputStream);
155 156
        return list.size();
    }
157

158 159
    @RequestMapping(value = "importOutputInvoice", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseEntity ImportInputInvoiceData(@RequestBody ImportOutputInvoiceDto importDto) {
160
        return ResponseEntity.ok(outputInvoiceService.ImportOutputInvoiceData(importDto, identityService.getIdentityUser().getId()));
161 162 163
    }

    @RequestMapping(value = "UploadEvidenceFile", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
164
    public ResponseEntity UploadEvidenceFile(String period, MultipartHttpServletRequest request) {
165 166
        String userId = identityService.getIdentityUser().getId();
        String fullPath = fileUploadAdapter.uploadEvidence(request, EnumModule.Evidence, userId);
167
        if (StringUtil.isNotEmpty(fullPath)) {
168 169 170
            String fileName = request.getParameter(FILE_NAME);
            String tempFileName = request.getParameter(TEMP_FILE_NAME);

171
            ImportFile fileDto = new ImportFile();
172
            fileDto.setFileId(tempFileName.substring(1, tempFileName.length() - 5));
neo's avatar
neo committed
173 174
            fileDto.setFilePath(fullPath);
            fileDto.setFileName(fileName);
175
            fileDto.setPeriodId(Integer.parseInt(period));
neo's avatar
neo committed
176 177
            fileDto.setFileType(GetExtension(fileName));
            fileDto.setFileImportType(EnumModule.Evidence.getCode());
178
            fileDto.setCreatorId(userId);
neo's avatar
neo committed
179
            fileDto.setCreateTime(new Date());
180 181 182 183 184
            return ResponseEntity.ok(outputInvoiceService.addImportFile(fileDto));
        }
        return ResponseEntity.ok(false);
    }

185 186 187
    @RequestMapping(value = "getOutputInvoiceList/{period}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseEntity GetOutputInvoiceList(Integer period) {
        return ResponseEntity.ok(outputInvoiceService.getOutputInvoiceList(period));
188 189
    }

190
    @RequestMapping(value = "queryOutputInvoiceItemList/{invoiceId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
neo's avatar
neo committed
191 192
    public ResponseEntity QueryOutputInvoiceItemList(@PathVariable String invoiceId,@RequestParam(required = false) String tag) {
        return ResponseEntity.ok(outputInvoiceService.QueryOutputInvoiceItemList(invoiceId,tag));
193 194
    }

neo's avatar
neo committed
195
    private String GetExtension(String file) {
196
        return file.substring(file.lastIndexOf(".") + 1, file.length());
neo's avatar
neo committed
197
    }
198

199
}