Archived
0

Добавлен параметр для ФСС + алгоритм изменен для поддержки многостроковых отчетов

This commit is contained in:
2018-06-07 14:42:17 +03:00
parent 9a51608722
commit b182fe1763
2 changed files with 144 additions and 88 deletions

View File

@@ -12,7 +12,9 @@ import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
@Slf4j @Slf4j
@@ -21,7 +23,15 @@ public class Corrector1S {
private class ExcelRecord { private class ExcelRecord {
private String name; private String name;
private Integer addition; private Integer addition;
private Double data; private List<Double> dataList;
ExcelRecord(int capacity) {
dataList = new ArrayList<>(capacity);
}
boolean isEmptyDataList() {
return dataList.stream().mapToDouble(Double::doubleValue).sum() == 0.0d;
}
} }
@Setter @Setter
@@ -30,14 +40,19 @@ public class Corrector1S {
private File targetFile; private File targetFile;
@Setter @Setter
private double honorarium; private double honorarium;
@Setter
private double fss;
private Map<String, ExcelRecord> mapRecords; private Map<String, ExcelRecord> mapRecords;
private Workbook workbook; private Workbook workbook;
private Sheet sheet; private Sheet sheet;
/** номер колонки "Итого начислено" */
private int cellNumSum = 0; private int cellNumSum = 0;
/** номер колонки с больничными */
private int cellBol = 0; private int cellBol = 0;
private int lastColumn = 0; private int lastColumn = 0;
private CellStyle cellStyle1, cellStyle2; private CellStyle cellStyle1, cellStyle2;
private int maxLines = 0;
private String getFileExtension(File file) { private String getFileExtension(File file) {
int i = file.getAbsolutePath().lastIndexOf('.'); int i = file.getAbsolutePath().lastIndexOf('.');
@@ -48,8 +63,14 @@ public class Corrector1S {
} }
} }
private String buildSumFormula(int column, int startLine, int endLine) {
String columnChar = CellReference.convertNumToColString(column);
return String.format("SUM(%s%d:%s%d)", columnChar, startLine, columnChar, endLine);
}
public void check() throws Exception { public void check() throws Exception {
if (honorarium == 0.0d) throw new AssertionError("Honorarium not be 0.0%!"); if (honorarium == 0.0d) throw new AssertionError("Honorarium not be 0.0%!");
if (fss == 0.0d) throw new AssertionError("FSS not be 0.0%!");
if (sourceFile == null) throw new AssertionError("Source file not be null!"); if (sourceFile == null) throw new AssertionError("Source file not be null!");
else if (!sourceFile.exists()) throw new FileNotFoundException(sourceFile.getAbsolutePath()); else if (!sourceFile.exists()) throw new FileNotFoundException(sourceFile.getAbsolutePath());
@@ -64,37 +85,47 @@ public class Corrector1S {
sheet.getRow(6).getCell(8); sheet.getRow(6).getCell(8);
sheet.getRow(7).getCell(8); sheet.getRow(7).getCell(8);
sheet.getRow(8).getCell(8); sheet.getRow(8).getCell(8);
sheet.getRow(9).getCell(8);
} catch (Exception e) { } catch (Exception e) {
throw new FormatFileException(e);
} finally {
workbook.close(); workbook.close();
throw new FormatFileException(e);
} }
// get max lines
for (int line = 8; true; line++) {
if (sheet.getRow(line) == null) {
this.maxLines = line - 2;
break;
}
}
workbook.close();
} }
public void createSnapshotData() throws IOException, InvalidFormatException { public void createSnapshotData() throws IOException, InvalidFormatException {
workbook = WorkbookFactory.create(sourceFile); workbook = WorkbookFactory.create(sourceFile);
sheet = workbook.getSheetAt(0); sheet = workbook.getSheetAt(0);
mapRecords = new HashMap<>(19); // ожидается 19 записей для сохранения mapRecords = new HashMap<>(20);
Cell cell; Cell cell;
loop: loop:
for (int c = 8; true; c++) { for (int column = 8; true; column++) {
ExcelRecord record = new ExcelRecord(); ExcelRecord record = new ExcelRecord(this.maxLines);
for (int r = 6; r < 9; r++) { for (int line = 6; line <= this.maxLines; line++) {
cell = sheet.getRow(r).getCell(c); cell = sheet.getRow(line).getCell(column);
if (cell == null) break loop; if (cell == null) break loop;
if (r == 6) record.name = cell.getStringCellValue(); if (line == 6) {
else if (r == 7) { record.name = cell.getStringCellValue();
} else if (line == 7) {
if (cell.getCellTypeEnum().equals(CellType.NUMERIC)) { if (cell.getCellTypeEnum().equals(CellType.NUMERIC)) {
record.addition = (int) cell.getNumericCellValue(); record.addition = (int) cell.getNumericCellValue();
} else { } else {
record.addition = 0; record.addition = 0;
} }
} else {
record.dataList.add(cell.getNumericCellValue());
} }
else record.data = cell.getNumericCellValue();
} }
String key = (record.addition == 0 ? record.name.trim() : record.name.trim() + "_" + record.addition); String key = (record.addition == 0 ? record.name.trim() : record.name.trim() + "_" + record.addition);
@@ -115,10 +146,12 @@ public class Corrector1S {
} }
private void setCellRecord(ExcelRecord record, int column) { private void setCellRecord(ExcelRecord record, int column) {
// NAME
Cell cell = sheet.getRow(6).getCell(column, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK); Cell cell = sheet.getRow(6).getCell(column, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
cell.setCellType(CellType.STRING); cell.setCellType(CellType.STRING);
cell.setCellValue(record.name); cell.setCellValue(record.name);
// ADDITION
cell = sheet.getRow(7).getCell(column, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK); cell = sheet.getRow(7).getCell(column, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
if (record.addition == 0) { if (record.addition == 0) {
cell.setCellType(CellType.BLANK); cell.setCellType(CellType.BLANK);
@@ -127,62 +160,68 @@ public class Corrector1S {
cell.setCellValue(record.addition); cell.setCellValue(record.addition);
} }
cell = sheet.getRow(8).getCell(column, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK); // DATA
if (record.data == 0) { for (int line = 8; line <= this.maxLines; line++) {
cell.setCellType(CellType.BLANK); cell = sheet.getRow(line).getCell(column, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
} else { double data = record.dataList.get(line-8);
cell.setCellType(CellType.NUMERIC); if (data == 0) {
cell.setCellValue(record.data); cell.setCellType(CellType.BLANK);
} else {
cell.setCellType(CellType.NUMERIC);
cell.setCellValue(data);
}
} }
cell = sheet.getRow(9).getCell(column, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK); // SUM
if (record.data == 0) { cell = sheet.getRow(this.maxLines+1).getCell(column, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
if (record.isEmptyDataList()) {
cell.setCellType(CellType.BLANK); cell.setCellType(CellType.BLANK);
} else { } else {
cell.setCellType(CellType.NUMERIC); cell.setCellType(CellType.FORMULA);
cell.setCellValue(record.data); cell.setCellFormula(buildSumFormula(column, 8, this.maxLines));
} }
} }
public void replaceData() { public void replaceData() {
int c = 8; int column = 8;
ExcelRecord record; ExcelRecord record;
if ((record = mapRecords.remove("Отпуск очередной_104")) != null) setCellRecord(record, column++);
if ((record = mapRecords.remove("Больничный за счет работодателя_105")) != null) { if ((record = mapRecords.remove("Больничный за счет работодателя_105")) != null) {
setCellRecord(record, c++); setCellRecord(record, column++);
this.cellBol = c-1; this.cellBol = column-1;
} }
if ((record = mapRecords.remove("Дневные")) != null) setCellRecord(record, c++); if ((record = mapRecords.remove("Дневные")) != null) setCellRecord(record, column++);
if ((record = mapRecords.remove("Ночные")) != null) setCellRecord(record, c++); if ((record = mapRecords.remove("Ночные")) != null) setCellRecord(record, column++);
if ((record = mapRecords.remove("Ночные_123")) != null) setCellRecord(record, c++); if ((record = mapRecords.remove("Ночные_123")) != null) setCellRecord(record, column++);
if ((record = mapRecords.remove("питание_124")) != null) setCellRecord(record, c++); if ((record = mapRecords.remove("питание_124")) != null) setCellRecord(record, column++);
if ((record = mapRecords.remove("Выходные")) != null) setCellRecord(record, c++); if ((record = mapRecords.remove("Выходные")) != null) setCellRecord(record, column++);
if ((record = mapRecords.remove("выходные_135")) != null) setCellRecord(record, c++); if ((record = mapRecords.remove("выходные_135")) != null) setCellRecord(record, column++);
if ((record = mapRecords.remove("пересчет за прошлый месяц_141")) != null) setCellRecord(record, c++); if ((record = mapRecords.remove("пересчет за прошлый месяц_141")) != null) setCellRecord(record, column++);
if ((record = mapRecords.remove("вредность_145")) != null) setCellRecord(record, c++); if ((record = mapRecords.remove("вредность_145")) != null) setCellRecord(record, column++);
if ((record = mapRecords.remove("Материальная помощь при рождении ребенка_148")) != null) setCellRecord(record, c++); if ((record = mapRecords.remove("Материальная помощь при рождении ребенка_148")) != null) setCellRecord(record, column++);
if ((record = mapRecords.remove("компенсация за задержку зарплаты_169")) != null) setCellRecord(record, c++); if ((record = mapRecords.remove("компенсация за задержку зарплаты_169")) != null) setCellRecord(record, column++);
if ((record = mapRecords.remove("Оплата по среднему (донорство, посещение врача)_174")) != null) setCellRecord(record, c++); if ((record = mapRecords.remove("Оплата по среднему (донорство, посещение врача)_174")) != null) setCellRecord(record, column++);
if ((record = mapRecords.remove("Компенсация проезда при использовании личного транспорта_176")) != null) setCellRecord(record, c++); if ((record = mapRecords.remove("Компенсация проезда при использовании личного транспорта_176")) != null) setCellRecord(record, column++);
if ((record = mapRecords.remove("сверхурочные_133")) != null) setCellRecord(record, c++); if ((record = mapRecords.remove("сверхурочные_133")) != null) setCellRecord(record, column++);
if ((record = mapRecords.remove("Сверхурочные 1.5 ставки")) != null) setCellRecord(record, c++); if ((record = mapRecords.remove("Сверхурочные 1.5 ставки")) != null) setCellRecord(record, column++);
if ((record = mapRecords.remove("сверхурочные 1,5_180")) != null) setCellRecord(record, c++); if ((record = mapRecords.remove("сверхурочные 1,5_180")) != null) setCellRecord(record, column++);
if ((record = mapRecords.remove("Сверхурочные 2 ставки")) != null) setCellRecord(record, c++); if ((record = mapRecords.remove("Сверхурочные 2 ставки")) != null) setCellRecord(record, column++);
if ((record = mapRecords.remove("сверхурочные 2_181")) != null) setCellRecord(record, c++); if ((record = mapRecords.remove("сверхурочные 2_181")) != null) setCellRecord(record, column++);
if ((record = mapRecords.remove("Компенсация за неиспользованный отпуск (с декабря 2017)_184")) != null) setCellRecord(record, c++); if ((record = mapRecords.remove("Компенсация за неиспользованный отпуск (с декабря 2017)_184")) != null) setCellRecord(record, column++);
if ((record = mapRecords.remove("Месячная премия 2018_186")) != null) setCellRecord(record, c++); if ((record = mapRecords.remove("Месячная премия 2018_186")) != null) setCellRecord(record, column++);
if (mapRecords.size() > 1) { if (mapRecords.size() > 1) {
for (ExcelRecord rec : mapRecords.values()) { for (ExcelRecord rec : mapRecords.values()) {
if (!rec.name.equalsIgnoreCase("Итого начислено")) { if (!rec.name.equalsIgnoreCase("Итого начислено")) {
setCellRecord(rec, c++); setCellRecord(rec, column++);
} }
} }
} }
if ((record = mapRecords.remove("Итого начислено")) != null) setCellRecord(record, c++); if ((record = mapRecords.remove("Итого начислено")) != null) setCellRecord(record, column++);
cellNumSum = c-1; cellNumSum = column-1;
this.lastColumn = c; this.lastColumn = column;
} }
private void initStyle() { private void initStyle() {
@@ -208,89 +247,99 @@ public class Corrector1S {
cellStyle2.setBorderRight(BorderStyle.THIN); cellStyle2.setBorderRight(BorderStyle.THIN);
} }
private void insertCell(String title, String addition, String formula1, String formula2, int columnNumber) { private void insertCell(String title, String addition, String formulaTemplate, String formulaSum, int column) {
Cell cell = sheet.getRow(6).createCell(columnNumber); // NAME
Cell cell = sheet.getRow(6).createCell(column);
cell.setCellType(CellType.STRING); cell.setCellType(CellType.STRING);
cell.setCellValue(title); cell.setCellValue(title);
cell.setCellStyle(cellStyle1); cell.setCellStyle(cellStyle1);
cell = sheet.getRow(7).createCell(columnNumber); // ADDITION
cell = sheet.getRow(7).createCell(column);
cell.setCellType(CellType.STRING); cell.setCellType(CellType.STRING);
cell.setCellValue(addition); cell.setCellValue(addition);
cell.setCellStyle(cellStyle1); cell.setCellStyle(cellStyle1);
cell = sheet.getRow(8).createCell(columnNumber); // DATA
cell.setCellStyle(cellStyle2); for (int line = 8; line <= this.maxLines; line++) {
cell.setCellType(CellType.FORMULA); cell = sheet.getRow(line).createCell(column);
cell.setCellFormula(formula1); cell.setCellStyle(cellStyle2);
cell.setCellType(CellType.FORMULA);
cell.setCellFormula(formulaTemplate.replace("{L}", String.valueOf(line+1)).replace(',','.'));
}
cell = sheet.getRow(9).createCell(columnNumber); // SUM
cell = sheet.getRow(this.maxLines+1).createCell(column);
cell.setCellStyle(cellStyle2); cell.setCellStyle(cellStyle2);
cell.setCellType(CellType.FORMULA); cell.setCellType(CellType.FORMULA);
cell.setCellFormula(formula2); cell.setCellFormula(formulaSum);
} }
public void insertNewCols() { public void insertNewCols() {
initStyle(); initStyle();
int column = lastColumn; int column = lastColumn;
String form1, form2;
// FSS
String formulaTemplate;
if (cellBol == 0) { if (cellBol == 0) {
form1 = String.format("(%s9)*31%%", CellReference.convertNumToColString(cellNumSum)); formulaTemplate = String.format(
form2 = String.format("(%s10)*31%%", CellReference.convertNumToColString(cellNumSum)); "(%s{L})*%.2f%%",
CellReference.convertNumToColString(cellNumSum),
this.fss
);
} else { } else {
form1 = String.format("(%s9-%s9)*31%%", formulaTemplate = String.format(
"(%s{L}-%s{L})*%.2f%%",
CellReference.convertNumToColString(cellNumSum), CellReference.convertNumToColString(cellNumSum),
CellReference.convertNumToColString(cellBol)); CellReference.convertNumToColString(cellBol),
form2 = String.format("(%s10-%s10)*31%%", this.fss
CellReference.convertNumToColString(cellNumSum), );
CellReference.convertNumToColString(cellBol));
} }
insertCell( insertCell(
"Страховые взносы + ФСС НС (31%)", String.format("Страховые взносы + ФСС НС (%.2f%%)", this.fss),
"Начислено", "Начислено",
form1, formulaTemplate,
form2, buildSumFormula(column, 8, this.maxLines),
column++ column++
); );
// BASE
insertCell( insertCell(
"БАЗА", "БАЗА",
"для начисления стоимости услуг", "для начисления стоимости услуг",
String.format("%s9+%s9", String.format("%s{L}+%s{L}",
CellReference.convertNumToColString(cellNumSum),
CellReference.convertNumToColString(column-1)),
String.format("%s10+%s10",
CellReference.convertNumToColString(cellNumSum), CellReference.convertNumToColString(cellNumSum),
CellReference.convertNumToColString(column-1)), CellReference.convertNumToColString(column-1)),
buildSumFormula(column, 8, this.maxLines),
column++ column++
); );
// HONORARIUM
insertCell( insertCell(
"Гонорар", "Гонорар",
(int)this.honorarium + "%", String.format("%.2f%%", this.honorarium),
String.format("%s9*%d%%", String.format("%s{L}*%.2f%%",
CellReference.convertNumToColString(column-1), CellReference.convertNumToColString(column-1),
(int)this.honorarium), this.honorarium),
String.format("%s10*%d%%", buildSumFormula(column, 8, this.maxLines),
CellReference.convertNumToColString(column-1),
(int)this.honorarium),
column++ column++
); );
// SUM ALL
insertCell( insertCell(
"Итого, для счета", "Итого, для счета",
"Результат", "Результат",
String.format("%s9+%s9", String.format("%s{L}+%s{L}",
CellReference.convertNumToColString(column-2),
CellReference.convertNumToColString(column-1)),
String.format("%s10+%s10",
CellReference.convertNumToColString(column-2), CellReference.convertNumToColString(column-2),
CellReference.convertNumToColString(column-1)), CellReference.convertNumToColString(column-1)),
buildSumFormula(column, 8, this.maxLines),
column column
); );
workbook.getCreationHelper().createFormulaEvaluator().evaluateAll();
} }
public void saveFile() throws IOException { public void saveFile() throws IOException {
workbook.getCreationHelper().createFormulaEvaluator().evaluateAll();
try (FileOutputStream fos = new FileOutputStream(targetFile)) { try (FileOutputStream fos = new FileOutputStream(targetFile)) {
workbook.write(fos); workbook.write(fos);
workbook.close(); workbook.close();

View File

@@ -24,10 +24,14 @@ public class Main {
optionParser.acceptsAll(Arrays.asList("t", "target"), "Target Excel file (default: target = source)") optionParser.acceptsAll(Arrays.asList("t", "target"), "Target Excel file (default: target = source)")
.withRequiredArg() .withRequiredArg()
.ofType(File.class); .ofType(File.class);
optionParser.acceptsAll(Arrays.asList("f", "fee"), "Honorarium (in percentages)") optionParser.acceptsAll(Arrays.asList("h", "honorarium"), "Honorarium (in percentages)")
.withRequiredArg() .withRequiredArg()
.ofType(Double.class) .ofType(Double.class)
.defaultsTo(9.0d); .defaultsTo(9.0d);
optionParser.acceptsAll(Arrays.asList("f", "fss"), "FSS (in percentages)")
.withRequiredArg()
.ofType(Double.class)
.defaultsTo(31.0d);
optionParser.acceptsAll(Arrays.asList("h", "help"), "Help page. Display this message") optionParser.acceptsAll(Arrays.asList("h", "help"), "Help page. Display this message")
.forHelp(); .forHelp();
optionParser.acceptsAll(Arrays.asList("v", "version"), "Version") optionParser.acceptsAll(Arrays.asList("v", "version"), "Version")
@@ -72,12 +76,15 @@ public class Main {
log.debug("Source file: {} (exists: {})", sourceFile.getAbsolutePath(), Files.exists(sourceFile.toPath())); log.debug("Source file: {} (exists: {})", sourceFile.getAbsolutePath(), Files.exists(sourceFile.toPath()));
log.debug("Target file: {}", targetFile.getAbsolutePath()); log.debug("Target file: {}", targetFile.getAbsolutePath());
log.debug("Honorarium: {}%", optionSet.valueOf("fee")); log.debug("Honorarium: {}%", optionSet.valueOf("honorarium"));
log.debug("FSS: {}%", optionSet.valueOf("fss"));
Corrector1S corrector1S = new Corrector1S(); Corrector1S corrector1S = new Corrector1S();
corrector1S.setSourceFile(sourceFile); corrector1S.setSourceFile(sourceFile);
corrector1S.setTargetFile(targetFile); corrector1S.setTargetFile(targetFile);
corrector1S.setHonorarium((Double) optionSet.valueOf("fee")); corrector1S.setHonorarium((Double) optionSet.valueOf("honorarium"));
corrector1S.setFss((Double) optionSet.valueOf("fss"));
corrector1S.check(); corrector1S.check();
corrector1S.createSnapshotData(); corrector1S.createSnapshotData();
corrector1S.removeMergedCells(); corrector1S.removeMergedCells();