Browse Source

发票导出

fenghaifu 2 tháng trước cách đây
mục cha
commit
d14dd1c4a6

+ 236 - 2
srm-module-code/src/main/java/org/jeecg/modules/saleCode/controller/SaleInvoiceController.java

@@ -1,8 +1,12 @@
 package org.jeecg.modules.saleCode.controller;
 
+import java.io.OutputStream;
 import java.io.UnsupportedEncodingException;
 import java.io.IOException;
+import java.math.BigDecimal;
 import java.net.URLDecoder;
+import java.net.URLEncoder;
+import java.nio.charset.StandardCharsets;
 import java.time.LocalDate;
 import java.time.temporal.ChronoUnit;
 import java.util.ArrayList;
@@ -15,8 +19,22 @@ import java.util.HashMap;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
+import com.alibaba.excel.EasyExcel;
+import com.alibaba.excel.ExcelWriter;
+import com.alibaba.excel.enums.WriteDirectionEnum;
+import com.alibaba.excel.support.ExcelTypeEnum;
+import com.alibaba.excel.write.metadata.WriteSheet;
+import com.alibaba.excel.write.metadata.fill.FillConfig;
+import com.alibaba.excel.write.metadata.fill.FillWrapper;
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import org.apache.commons.lang.StringUtils;
-import org.jeecg.modules.baseCode.service.ISerialPatternService;
+import org.jeecg.common.system.vo.DictModel;
+import org.jeecg.common.util.DateUtils;
+import org.jeecg.common.util.UUIDGenerator;
+import org.jeecg.modules.baseCode.entity.*;
+import org.jeecg.modules.baseCode.service.*;
+import org.jeecg.modules.cuspCode.entity.CuspCustomerProfile;
+import org.jeecg.modules.cuspCode.service.ICuspCustomerProfileService;
 import org.jeecg.modules.purCode.entity.PurOrder;
 import org.jeecg.modules.purCode.service.IPurOrderService;
 import org.jeecg.modules.saleCode.entity.*;
@@ -24,6 +42,10 @@ import org.jeecg.modules.saleCode.service.*;
 import org.jeecg.modules.saleCode.vo.SaleDeliveryAlert;
 import org.jeecg.modules.storeCode.entity.StoreSaleOut;
 import org.jeecg.modules.storeCode.service.IStoreSaleOutService;
+import org.jeecg.modules.system.entity.SysUser;
+import org.jeecg.modules.system.service.ISysDictService;
+import org.jeecg.modules.system.service.ISysUserService;
+import org.jeecg.modules.utils.ExcelExportUtils;
 import org.jeecgframework.poi.excel.ExcelImportUtil;
 import org.jeecgframework.poi.excel.def.NormalExcelConstants;
 import org.jeecgframework.poi.excel.entity.ExportParams;
@@ -39,6 +61,7 @@ import org.jeecg.modules.saleCode.entity.SaleInvoice;
 import org.jeecg.modules.saleCode.vo.SaleInvoicePage;
 import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.web.bind.annotation.*;
 import org.springframework.web.servlet.ModelAndView;
 import org.springframework.web.multipart.MultipartFile;
@@ -83,6 +106,29 @@ public class SaleInvoiceController {
     private ISaleOrderService saleOrderService;
     @Autowired
     private ISerialPatternService serialPatternService;
+    @Autowired
+    private IBaseTemplatesService baseTemplatesService;
+    @Autowired
+    private ISysUserService sysUserService;
+    @Autowired
+    private IBaseProductClassService baseProductClassService;
+    @Autowired
+    private ICuspCustomerProfileService cuspCustomerProfileService;
+    @Autowired
+    private IBaseShipArchiveService baseShipArchiveService;
+    @Autowired
+    private IBaseProductArchiveService baseProductArchiveService;
+    @Autowired
+    private IBaseProjectArchiveService baseProjectArchiveService;
+    @Autowired
+    private ISaleOrderProductService saleOrderProductService;
+    @Autowired
+    private ISaleDeliveryDetailsService saleDeliveryDetailsService;
+    @Autowired
+    private ISysDictService sysDictService;
+
+    @Value(value = "${jeecg.path.upload}")
+    private String uploadpath;
 
     /**
      * 分页列表查询
@@ -634,8 +680,196 @@ public class SaleInvoiceController {
      *
      */
     @RequestMapping(value = "/exportBillXls")
-    public void exportBillXls(HttpServletResponse response, SaleOrder saleOrder1) throws Exception {
+    public void exportBillXls(HttpServletResponse response, SaleInvoice saleInvoice1) throws Exception {
+        String id = saleInvoice1.getId();
+        BaseTemplates templates = baseTemplatesService.getByTemplateType("销售发票");
+        SaleInvoice saleInvoice = saleInvoiceService.getById(id);
+        List<SaleInvoiceProduct> productList = saleInvoiceProductService.selectByMainId(id);
+        List<SaleInvoiceShip> invoiceShipList = saleInvoiceShipService.selectByMainId(id);
+        String productClass = "";
+        String orderCode = "";
+        String currency = "";
+        if (oConvertUtils.isNotEmpty(saleInvoice.getSourceCode())) {
+            LambdaQueryWrapper<SaleDelivery> deliveryLambdaQueryWrapper = new LambdaQueryWrapper<>();
+            deliveryLambdaQueryWrapper.eq(SaleDelivery::getBillCode, saleInvoice.getSourceCode());
+            SaleDelivery delivery = saleDeliveryService.getOne(deliveryLambdaQueryWrapper);
+            if (delivery != null){
+                productClass = delivery.getProductionClass();
+                orderCode = delivery.getSourceCode();
+                currency = delivery.getCurrency();
+            }
+        }
+        saleInvoice.setProductionClass(productClass);
+        saleInvoice.setCurrency(currency);
+
+
+        if (templates != null) {
+            try {
+
+                String productionClass = saleInvoice.getProductionClass();
+                if (StringUtils.isNotBlank(productionClass)) {
+
+                    BaseProductClass classEnt = baseProductClassService.getById(productionClass);
+                    if (classEnt != null) {
+                        saleInvoice.setProductionClass(classEnt.getName());
+                    }
+                }
+                String customerId = saleInvoice.getCustomer();
+                if (StringUtils.isNotBlank(customerId)) {
+
+                    CuspCustomerProfile classEnt = cuspCustomerProfileService.getById(customerId);
+                    if (classEnt != null) {
+
+                        saleInvoice.setContacts(classEnt.getContacts());
+                        saleInvoice.setPhone(classEnt.getPhone());
+                        saleInvoice.setEmail(classEnt.getEmail());
+                    }
+                }
+                // 获取计产品档案
+                List<BaseProductArchive> productArchiveList = new ArrayList<>();
+                if (oConvertUtils.listIsNotEmpty(productList)){
+                    LambdaQueryWrapper<BaseProductArchive> productWrapper = new LambdaQueryWrapper<>();
+                    productWrapper.in(BaseProductArchive::getId, productList.stream().map(SaleInvoiceProduct::getProductId).collect(Collectors.toList()));
+                    productArchiveList = baseProductArchiveService.list(productWrapper);
+                }
+                // 获取发货单行
+                List<SaleDeliveryDetails> saleDeliveryDetailsList = new ArrayList<>();
+                if (oConvertUtils.isNotEmpty(productList)){
+                    LambdaQueryWrapper<SaleDeliveryDetails> queryWrapper = new LambdaQueryWrapper<>();
+                    queryWrapper.in(SaleDeliveryDetails::getId, productList.stream().map(SaleInvoiceProduct::getSourceId).collect(Collectors.toList()));
+                    saleDeliveryDetailsList = saleDeliveryDetailsService.list(queryWrapper);
+                }
+                // 获取销售订单行
+                List<SaleOrderProduct> saleOrderProductList = new ArrayList<>();
+                if (oConvertUtils.listIsNotEmpty(saleDeliveryDetailsList)){
+                    LambdaQueryWrapper<SaleOrderProduct> productWrapper = new LambdaQueryWrapper<>();
+                    productWrapper.in(SaleOrderProduct::getId, saleDeliveryDetailsList.stream().map(SaleDeliveryDetails::getSourceId).collect(Collectors.toList()));
+                    saleOrderProductList = saleOrderProductService.list(productWrapper);
+                }
+                // 获取销售订单
+                if (oConvertUtils.isNotEmpty(orderCode)){
+                    LambdaQueryWrapper<SaleOrder> orderLambdaQueryWrapper = new LambdaQueryWrapper<>();
+                    orderLambdaQueryWrapper.eq(SaleOrder::getBillCode, orderCode);
+                    SaleOrder saleOrder = saleOrderService.getOne(orderLambdaQueryWrapper);
+                    if (saleOrder != null){
+                        saleInvoice.setPaymentTerms(saleOrder.getPaymentTerms());
+                    }
+                }
+                // 查询数据字典
+                String currencyText = sysDictService.queryDictTextByKey("currency", saleInvoice.getCurrency());
+                saleInvoice.setCurrencyText(currencyText);
+                String paymentTermsText = sysDictService.queryDictTextByKey("customer_payment_terms", saleInvoice.getPaymentTerms());
+                saleInvoice.setPaymentTermsText(paymentTermsText);
+                List<DictModel> unitDictList = sysDictService.queryDictItemsByCode("measurement_unit");
+
+                // 产品设置
+                int rowNum = 1;
+                BigDecimal invoiceMoney = BigDecimal.ZERO;
+                for(SaleInvoiceProduct o:productList){
+                    BigDecimal amount = o.getTaxMoney() == null ? BigDecimal.ZERO: o.getTaxMoney();
+                    invoiceMoney = invoiceMoney.add(amount);
+                    o.setRowNumber(rowNum++);
+
+
+                    BaseProductArchive findProduct = productArchiveList.stream().filter(e->e.getId().equals(o.getProductId())).findFirst().orElse(null);
+                    if (findProduct != null){
+                        o.setUnit(findProduct.getMeasurementUnit());
+                        if (findProduct.getMeasurementUnit() != null){
+                            DictModel findDict = unitDictList.stream().filter(d->d.getValue().equals(findProduct.getMeasurementUnit())).findFirst().orElse(null);
+                            if (findDict != null){
+                                o.setUnit(findDict.getText());
+                            }
+                        }
+                    }
+                    SaleDeliveryDetails saleDeliveryDetails = saleDeliveryDetailsList.stream().filter(e->e.getId().equals(o.getSourceId())).findFirst().orElse(null);
+                    if (saleDeliveryDetails != null) {
+                        SaleOrderProduct saleOrderProduct = saleOrderProductList.stream().filter(e -> e.getId().equals(saleDeliveryDetails.getSourceId())).findFirst().orElse(null);
+                        if (saleOrderProduct != null) {
+                            if (oConvertUtils.isNotEmpty(saleOrderProduct.getDiscount()) && saleOrderProduct.getDiscount().intValue() != 0) {
+                                o.setDiscountText(saleOrderProduct.getDiscount().intValue() + "%");
+                            }
+                        }
+                    }
+                }
+                saleInvoice.setInvoiceMoney(invoiceMoney.setScale(2));
+
+                String shipname = "";
+                String imo = "";
 
+                if(oConvertUtils.listIsNotEmpty(invoiceShipList)){
+
+                    shipname = invoiceShipList.stream().map(SaleInvoiceShip::getShipName).distinct().collect(Collectors.joining());
+
+                    LambdaQueryWrapper<BaseShipArchive> shipArchiveLambdaQueryWrapper = new LambdaQueryWrapper<>();
+                    shipArchiveLambdaQueryWrapper.in(BaseShipArchive::getId,invoiceShipList.stream().map(SaleInvoiceShip::getShipId).collect(Collectors.joining()));
+                    List<BaseShipArchive> baseShipArchiveList = baseShipArchiveService.list(shipArchiveLambdaQueryWrapper);
+
+                    if(oConvertUtils.listIsNotEmpty(baseShipArchiveList)){
+                        imo = baseShipArchiveList.stream().map(BaseShipArchive::getImo).distinct().collect(Collectors.joining());
+                    }
+                }
+                saleInvoice.setShipName(shipname);
+                saleInvoice.setImo(imo);
+                // 项目名称
+                if (saleInvoice.getProject() != null){
+                    BaseProjectArchive projectArchive = baseProjectArchiveService.getById(saleInvoice.getProject());
+                    if (projectArchive != null){
+                        saleInvoice.setContactsNo(projectArchive.getName());
+                    }
+
+                }
+
+                if(productList.size() > 0){
+
+                    saleInvoice.setQualityGrade(productList.get(0).getQualityGrade());
+                }
+                //
+                saleInvoice.setBillDateText(DateUtils.date2Str(saleInvoice.getBillDate(), DateUtils.date_sdf.get()));
+
+
+                // 制单人信息
+                SysUser sysUser = sysUserService.getUserByName(saleInvoice.getCreateBy());
+                saleInvoice.setSalesPhone(sysUser.getPhone());
+                saleInvoice.setSalesEmail(sysUser.getEmail());
+
+                String templateFilePath = uploadpath + templates.getTemplateFile();
+                String tempFilePath = uploadpath + "/"+ UUIDGenerator.generate()+".xlsx";
+
+                try (ExcelWriter excelWriter = EasyExcel.write(tempFilePath).withTemplate(templateFilePath).build()) {
+                    WriteSheet writeSheet = EasyExcel.writerSheet().build();
+                    FillConfig fillConfig = FillConfig.builder().direction(WriteDirectionEnum.VERTICAL).forceNewRow(Boolean.TRUE).build();
+                    excelWriter.fill(new FillWrapper(productList), fillConfig, writeSheet);
+                    excelWriter.fill(saleInvoice, writeSheet);
+                }
+                List<String> noteList = new ArrayList<>();
+                productList.forEach(p->noteList.add(p.getNotes()));
+
+                ExcelExportUtils.excelInsertRowNotes(getOutputStream(saleInvoice.getInvoiceCode()+".xlsx", response),
+                        tempFilePath, 17, noteList, 1, 3);
+
+
+
+            } catch (Exception ex) {
+                ex.printStackTrace();
+            }
+
+        }
+
+    }
+
+    /**
+     * 这是ExcelUtil.getOutputStream
+     * 这里就是将文件下载交给了浏览器
+     *
+     * @param
+     * @return
+     */
+    public OutputStream getOutputStream(String fileName, HttpServletResponse response) throws Exception {
+        // 这里文件名如果涉及中文一定要使用URL编码,否则会乱码
+        String exportFileName = URLEncoder.encode(fileName + ExcelTypeEnum.XLSX.getValue(), StandardCharsets.UTF_8.toString());
+        response.setContentType("application/force-download");
+        response.setHeader("Content-Disposition", "attachment;filename=" + exportFileName);
+        return response.getOutputStream();
     }
 
 }

+ 37 - 4
srm-module-code/src/main/java/org/jeecg/modules/saleCode/entity/SaleInvoice.java

@@ -4,10 +4,8 @@ import java.io.Serializable;
 import java.io.UnsupportedEncodingException;
 import java.math.BigDecimal;
 import java.util.Date;
-import com.baomidou.mybatisplus.annotation.IdType;
-import com.baomidou.mybatisplus.annotation.TableId;
-import com.baomidou.mybatisplus.annotation.TableName;
-import com.baomidou.mybatisplus.annotation.TableLogic;
+
+import com.baomidou.mybatisplus.annotation.*;
 import org.jeecg.common.constant.ProvinceCityArea;
 import org.jeecg.common.util.SpringContextUtils;
 import lombok.Data;
@@ -182,4 +180,39 @@ public class SaleInvoice implements Serializable {
 	@Excel(name = "盖章发票(Stamp invoice)", width = 15)
     @ApiModelProperty(value = "盖章发票(Stamp invoice)")
     private String stampInvoice;
+
+    //导出字段使用
+    @TableField(exist = false)
+    private String salesPhone;
+    @TableField(exist = false)
+    private String salesEmail;
+
+    @TableField(exist = false)
+    private String contactsNo;
+    @TableField(exist = false)
+    private String contacts;
+    @TableField(exist = false)
+    private String phone;
+    @TableField(exist = false)
+    private String email;
+    @TableField(exist = false)
+    private String shipName;
+    @TableField(exist = false)
+    private String imo;
+    @TableField(exist = false)
+    private String qualityGrade;
+    @TableField(exist = false)
+    private String billDateText;
+    @TableField(exist = false)
+    private BigDecimal invoiceMoney;
+    @TableField(exist = false)
+    private String paymentTerms;
+    @TableField(exist = false)
+    private String currencyText;
+    @TableField(exist = false)
+    private String paymentTermsText;
+    @TableField(exist = false)
+    private String productionClass;
+    @TableField(exist = false)
+    private String currency;
 }

+ 10 - 4
srm-module-code/src/main/java/org/jeecg/modules/saleCode/entity/SaleInvoiceProduct.java

@@ -1,10 +1,8 @@
 package org.jeecg.modules.saleCode.entity;
 
 import java.io.Serializable;
-import com.baomidou.mybatisplus.annotation.IdType;
-import com.baomidou.mybatisplus.annotation.TableId;
-import com.baomidou.mybatisplus.annotation.TableName;
-import com.baomidou.mybatisplus.annotation.TableLogic;
+
+import com.baomidou.mybatisplus.annotation.*;
 import org.jeecg.common.constant.ProvinceCityArea;
 import org.jeecg.common.util.SpringContextUtils;
 import lombok.Data;
@@ -142,4 +140,12 @@ public class SaleInvoiceProduct implements Serializable {
 	@Excel(name = "参照来源id", width = 15)
     @ApiModelProperty(value = "参照来源id")
     private String sourceId;
+
+    // 导出用
+    @TableField(exist = false)
+    private Integer rowNumber;
+    @TableField(exist = false)
+    private String discountText;
+    @TableField(exist = false)
+    private String unit;
 }