Merge pull request #136 from LQYBill/hotfix/fixUSD

hotfix: invoice excel and pdf for USD complete invoice
pull/8040/head
Qiuyi LI 2025-02-21 17:20:17 +01:00 committed by GitHub
commit e6e9db6cf9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 26 additions and 23 deletions

View File

@ -1262,7 +1262,7 @@ public class InvoiceController {
} catch (IOException e) { } catch (IOException e) {
log.error(e.getMessage()); log.error(e.getMessage());
return Result.error("Sorry, server error, please try later"); return Result.error("Sorry, server error, please try later");
} catch (URISyntaxException e) { } catch (Exception e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }

View File

@ -195,11 +195,13 @@ public abstract class AbstractInvoice<E, F, G, H, I> {
} }
} }
else {// on dépasse forcément le format A4 d'un PDF else {// on dépasse forcément le format A4 d'un PDF
if(((TOTAL_ROW - 44) % 63) < 13) { if(((TOTAL_ROW - 44) % 63) < 16) {
int dataSizeAfterFirstPage = data.size() - 44;
int shift = (int) Math.ceil((double) (dataSizeAfterFirstPage) / 63) * 63; // we shift the footer to the next page
// Not enough space for footer // Not enough space for footer
sheet.shiftRows(startRow, fileLastRow, TOTAL_ROW - LAST_ROW + ((TOTAL_ROW-44)%63), true, false); sheet.shiftRows(startRow, fileLastRow, shift + 20, true, false); // why 20 ? no idea, it just works
imgShift = TOTAL_ROW-44 + ((TOTAL_ROW-44)%63) -1; imgShift = shift + 16; // 16 is the number of lines of footer
TOTAL_ROW += ((TOTAL_ROW-44)%63) + 1; TOTAL_ROW = 63 + shift; // we just yeet the total row to next page
footerRow = TOTAL_ROW+1; footerRow = TOTAL_ROW+1;
situation = 4; situation = 4;
} }
@ -256,7 +258,7 @@ public abstract class AbstractInvoice<E, F, G, H, I> {
for (int i = 0; i < data.size(); i++) { for (int i = 0; i < data.size(); i++) {
lineNum = i + FIRST_ROW; lineNum = i + FIRST_ROW;
rowValue = data.get(i); rowValue = data.get(i);
log.info("Writing line {} with data {}", lineNum, rowValue); // log.info("Writing line {} with data {}", lineNum, rowValue);
configCell("C", lineNum, String.format("%06d", i + 1), factory.leftSideStyle()); configCell("C", lineNum, String.format("%06d", i + 1), factory.leftSideStyle());
configCell("D", lineNum, rowValue.getCol1(), factory.leftSideStyle()); configCell("D", lineNum, rowValue.getCol1(), factory.leftSideStyle());
configCell("E", lineNum, rowValue.getCol2(), factory.rightSideDecimalStyle()); configCell("E", lineNum, rowValue.getCol2(), factory.rightSideDecimalStyle());
@ -342,15 +344,8 @@ public abstract class AbstractInvoice<E, F, G, H, I> {
if (targetClient.getCurrency().equals("USD")) { if (targetClient.getCurrency().equals("USD")) {
org.apache.poi.ss.usermodel.Row dollarRow; org.apache.poi.ss.usermodel.Row dollarRow;
String formula; dollarRow = sheet.getRow(TOTAL_ROW + 2);
if ((((LAST_ROW + additionalRowNum - 44) % 63) < 13) && ((LAST_ROW + additionalRowNum - 44) % 63) > 0) { String formula = "H"+ (TOTAL_ROW + 2) +" *" + exchangeRate;
dollarRow = sheet.getRow(TOTAL_ROW + 2);
formula = "H"+ (TOTAL_ROW + 2) +" *" + exchangeRate;
}
else {
dollarRow = sheet.getRow(data.size() >= 44 ? TOTAL_ROW + 3 : TOTAL_ROW + 2);
formula = "H" + (data.size() >= 44 ? TOTAL_ROW + 3 : TOTAL_ROW + 2) + " *" + exchangeRate;
}
Cell dollarCell = dollarRow.createCell(7); // column H Cell dollarCell = dollarRow.createCell(7); // column H
CellStyle cellStyle = factory.getWorkbook().createCellStyle(); CellStyle cellStyle = factory.getWorkbook().createCellStyle();
DataFormat format = factory.getWorkbook().createDataFormat(); DataFormat format = factory.getWorkbook().createDataFormat();

View File

@ -107,7 +107,7 @@ public interface IPurchaseOrderService extends IService<PurchaseOrder> {
* @throws IOException IO error while reading the file. * @throws IOException IO error while reading the file.
*/ */
InvoiceMetaData makeInvoice(String purchaseID) throws IOException, URISyntaxException, UserException; InvoiceMetaData makeInvoice(String purchaseID) throws IOException, URISyntaxException, UserException;
InvoiceMetaData makeInvoiceTest(int nbOfLines) throws IOException, URISyntaxException, UserException; InvoiceMetaData makeInvoiceTest(int nbOfLines) throws Exception;
byte[] getInvoiceByte(String invoiceCode) throws IOException; byte[] getInvoiceByte(String invoiceCode) throws IOException;

View File

@ -988,6 +988,7 @@ public class PlatformOrderShippingInvoiceService {
public InvoiceMetaData makeCompleteInvoiceTest(int nbOfLines) throws Exception { public InvoiceMetaData makeCompleteInvoiceTest(int nbOfLines) throws Exception {
// Creates invoice by factory // Creates invoice by factory
Client client = clientService.getClientBySku("TEST"); Client client = clientService.getClientBySku("TEST");
client.setCurrency("USD");
Map<PlatformOrder, List<PlatformOrderContent>> ordersToContent = new HashMap<>(); Map<PlatformOrder, List<PlatformOrderContent>> ordersToContent = new HashMap<>();
List<SavRefundWithDetail> savRefunds = new ArrayList<>(); List<SavRefundWithDetail> savRefunds = new ArrayList<>();
List<ExtraFeeResult> extraFees = new ArrayList<>(); List<ExtraFeeResult> extraFees = new ArrayList<>();
@ -1025,8 +1026,11 @@ public class PlatformOrderShippingInvoiceService {
CompleteInvoice invoice = new CompleteInvoice(client, invoiceCode, "Test subject", ordersToContent, savRefunds, extraFees, purchaseInvoiceEntries, promotionDetails, exchangeRate); CompleteInvoice invoice = new CompleteInvoice(client, invoiceCode, "Test subject", ordersToContent, savRefunds, extraFees, purchaseInvoiceEntries, promotionDetails, exchangeRate);
Path src; Path src;
// src = Paths.get(COMPLETE_INVOICE_TEMPLATE_US); if(client.getCurrency().equals("USD")) {
src = Paths.get(COMPLETE_INVOICE_TEMPLATE_EU); src = Paths.get(COMPLETE_INVOICE_TEMPLATE_US);
} else {
src = Paths.get(COMPLETE_INVOICE_TEMPLATE_EU);
}
// Writes invoice content to a new excel file // Writes invoice content to a new excel file
String filename = "Complete invoice N°" + invoice.code() + " (" + invoice.client().getInvoiceEntity() + ") _" + nbOfLines + ".xlsx"; String filename = "Complete invoice N°" + invoice.code() + " (" + invoice.client().getInvoiceEntity() + ") _" + nbOfLines + ".xlsx";
@ -1060,6 +1064,7 @@ public class PlatformOrderShippingInvoiceService {
int maxRow = cells.getMaxDataRow(); int maxRow = cells.getMaxDataRow();
PageSetup pageSetup = sheet.getPageSetup(); PageSetup pageSetup = sheet.getPageSetup();
// Setting the number of pages to which the length of the worksheet will // Setting the number of pages to which the length of the worksheet will
System.out.println("maxRow : " + maxRow);
if(maxRow < 63) { if(maxRow < 63) {
// be spanned // be spanned
pageSetup.setFitToPagesTall(1); pageSetup.setFitToPagesTall(1);
@ -1067,6 +1072,10 @@ public class PlatformOrderShippingInvoiceService {
// Setting the number of pages to which the width of the worksheet will be spanned // Setting the number of pages to which the width of the worksheet will be spanned
pageSetup.setFitToPagesWide(1); pageSetup.setFitToPagesWide(1);
} }
else {
pageSetup.setFitToPagesTall((int) Math.ceil((double) maxRow /63));
pageSetup.setFitToPagesWide(1);
}
// On enregistre le document au format PDF // On enregistre le document au format PDF
workbook.save(pdfFilePath, saveOptions); workbook.save(pdfFilePath, saveOptions);
} }

View File

@ -60,9 +60,7 @@ public class PurchaseOrderServiceImpl extends ServiceImpl<PurchaseOrderMapper, P
@Autowired @Autowired
private PlatformOrderMapper platformOrderMapper; private PlatformOrderMapper platformOrderMapper;
@Autowired @Autowired
private IPlatformOrderContentService platformOrderContentService; private PlatformOrderShippingInvoiceService platformOrderShippingInvoiceService;
@Autowired
private IShippingInvoiceService shippingInvoiceService;
@Autowired @Autowired
private ISkuService skuService; private ISkuService skuService;
@Autowired @Autowired
@ -559,7 +557,7 @@ public class PurchaseOrderServiceImpl extends ServiceImpl<PurchaseOrderMapper, P
return new InvoiceMetaData(filename,invoiceCode, pv.client().getInternalCode(), pv.client().getInvoiceEntity(), ""); return new InvoiceMetaData(filename,invoiceCode, pv.client().getInternalCode(), pv.client().getInvoiceEntity(), "");
} }
@Override @Override
public InvoiceMetaData makeInvoiceTest(int nbOfLines) throws IOException, UserException { public InvoiceMetaData makeInvoiceTest(int nbOfLines) throws Exception {
Client client = clientService.getClientBySku("test"); Client client = clientService.getClientBySku("test");
List<PurchaseInvoiceEntry> purchaseOrderSkuList = new ArrayList<>(); List<PurchaseInvoiceEntry> purchaseOrderSkuList = new ArrayList<>();
// -5 because we have at least 5 lines of promotions // -5 because we have at least 5 lines of promotions
@ -570,7 +568,7 @@ public class PurchaseOrderServiceImpl extends ServiceImpl<PurchaseOrderMapper, P
for(int i = 0; i < 5; i++) { for(int i = 0; i < 5; i++) {
promotionDetails.add(new PromotionDetail(1, BigDecimal.valueOf(0.5), "Test Promotion " + i)); promotionDetails.add(new PromotionDetail(1, BigDecimal.valueOf(0.5), "Test Promotion " + i));
} }
String invoiceCode = "P-TEST-CODE-" + nbOfLines; String invoiceCode = "PI-TEST-1" + nbOfLines;
BigDecimal eurToUsd = exchangeRatesMapper.getLatestExchangeRate("EUR", "USD"); BigDecimal eurToUsd = exchangeRatesMapper.getLatestExchangeRate("EUR", "USD");
String filename = "Invoice N°" + invoiceCode + " (" + client.getInvoiceEntity() + ")_" + nbOfLines + ".xlsx"; String filename = "Invoice N°" + invoiceCode + " (" + client.getInvoiceEntity() + ")_" + nbOfLines + ".xlsx";
@ -579,6 +577,7 @@ public class PurchaseOrderServiceImpl extends ServiceImpl<PurchaseOrderMapper, P
Files.copy(template, newInvoice, StandardCopyOption.REPLACE_EXISTING); Files.copy(template, newInvoice, StandardCopyOption.REPLACE_EXISTING);
PurchaseInvoice pv = new PurchaseInvoice(client, invoiceCode, "Purchase Invoice", purchaseOrderSkuList, promotionDetails, eurToUsd); PurchaseInvoice pv = new PurchaseInvoice(client, invoiceCode, "Purchase Invoice", purchaseOrderSkuList, promotionDetails, eurToUsd);
pv.toExcelFile(newInvoice); pv.toExcelFile(newInvoice);
platformOrderShippingInvoiceService.convertToPdfTest(invoiceCode, "invoice");
return new InvoiceMetaData(filename,invoiceCode, pv.client().getInternalCode(), pv.client().getInvoiceEntity(), ""); return new InvoiceMetaData(filename,invoiceCode, pv.client().getInternalCode(), pv.client().getInvoiceEntity(), "");
} }