Skip to content

Commit

Permalink
Refactored Electronic Journal implementation
Browse files Browse the repository at this point in the history
Documents and shifts searching logic moved to TextReportStorage, simplified TextReportPrinter, fixed document header generated at TextDocumentFilter
  • Loading branch information
nyxiscoo1 committed Feb 14, 2017
1 parent 0281827 commit 71eb8bd
Show file tree
Hide file tree
Showing 7 changed files with 1,327 additions and 290 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2404,7 +2404,7 @@ public FMTotals readFSTotals() throws Exception {

public FMTotals readFPTotals(int mode) throws Exception {
if (capFiscalStorage) {
return readFSTotals();
return new FMTotals(); //readFSTotals();
} else if (isFiscalized()) {
return readFMTotals(mode);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -417,4 +417,13 @@ public PrinterStatus getPrinterStatus() {
public boolean isFiscalized() {
return registrationNumber != 0;
}

public int getCurrentShiftNumber() {
int dayNumber = getDayNumber() + 1;

if(dayNumber == 10000)
dayNumber = 1;

return dayNumber;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ public class TextDocumentFilter implements IPrinterEvents {
private static String SDiscountStornoText = "СТОРНО СКИДКИ";
private static String SChargeStornoText = "СТОРНО НАДБАВКИ";
private static String SXReportText = "СУТОЧНЫЙ ОТЧЕТ БЕЗ ГАШЕНИЯ";
private static String SZReportText = "СУТОЧНЫЙ ОТЧЕТ С ГАШЕНИЕМ";
public static final String SZReportText = "СУТОЧНЫЙ ОТЧЕТ С ГАШЕНИЕМ";
private static String SDayClosed = "СМЕНА ЗАКРЫТА";
public static final String SINN = "ИНН";
private static String[] docNames = {SSaleText, SBuyText, SRetSaleText, SRetBuyText};

public TextDocumentFilter(SMFiscalPrinter printer, PrinterHeader header) {
Expand Down Expand Up @@ -495,17 +496,13 @@ public void connect() throws Exception {
}
}

public int getDayNumber() throws Exception {
return printer.readLongStatus().getDayNumber();
}

public XReport readXReport() throws Exception {
XReport report = new XReport();
FMTotals totals = printer.readFPTotals(1);
report.salesAmountBefore = totals.getSalesAmount();
report.buyAmountBefore = totals.getBuyAmount();
report.xReportNumber = printer.readOperationRegister(158) + 1;
report.zReportNumber = getDayNumber() + 1;
report.zReportNumber = printer.readLongStatus().getCurrentShiftNumber();
for (int i = 0; i <= 3; i++) {
Receipt receipt = report.receipts.get(i);
receipt.number = printer.readOperationRegister(148 + i);
Expand Down Expand Up @@ -629,7 +626,7 @@ private void printReceiptHeader() throws Exception {

// ККМ
String s1 = String.format("%s %s", deviceName, status.getSerial());
String s2 = String.format("ИНН %s #%04d", status.getFiscalIDText(), documentNumber);
String s2 = String.format("ИНН %s %04d", status.getFiscalIDText(), documentNumber);
add(s1, s2);
// Кассир
PrinterDate date = status.getDate();
Expand All @@ -646,7 +643,7 @@ public String getOperatorName() throws Exception {
return operator.getName();
}
}
if ((operatorNumber < 1)||(operatorNumber > 30)){
if ((operatorNumber < 1) || (operatorNumber > 30)) {
operatorNumber = 1;
}
String[] fieldValue = new String[1];
Expand Down Expand Up @@ -684,8 +681,12 @@ public void printZReport(PrintZReport command) throws Exception {
public void printXZReport() throws Exception {
String[] ReceiptText = {"ЧЕКОВ ПРОДАЖ", "ЧЕКОВ ПОКУПОК", "ЧЕКОВ ВОЗВРАТОВ ПРОДАЖ", "ЧЕКОВ ВОЗВРАТОВ ПОКУПОК"};
String[] voidedReceiptText = {"ПРОДАЖ", "ПОКУПОК", "ВОЗВР.ПРОДАЖ", "ВОЗВР.ПОКУПОК"};
add("НЕОБНУЛ.СУММА ПРОДАЖ НА НАЧ.СМЕНЫ", report.salesAmountBefore);
add("НЕОБНУЛ.СУММА ПОКУПОК НА НАЧ.СМЕНЫ", report.buyAmountBefore);

if (!printer.getCapFiscalStorage()) {
add("НЕОБНУЛ.СУММА ПРОДАЖ НА НАЧ.СМЕНЫ", report.salesAmountBefore);
add("НЕОБНУЛ.СУММА ПОКУПОК НА НАЧ.СМЕНЫ", report.buyAmountBefore);
}

for (int i = 0; i <= 3; i++) {
Receipt receipt = report.receipts.get(i);
add(ReceiptText[i], String.format("%04d", receipt.number));
Expand All @@ -707,8 +708,11 @@ public void printXZReport() throws Exception {
}
add("НАЛ. В КАССЕ", report.cashInDrawer);
add("ВЫРУЧКА", report.income);
add("НЕОБНУЛ. СУММА ПРОДАЖ", report.salesAmount);
add("НЕОБ. СУММА ПОКУПОК", report.buyAmount);

if (!printer.getCapFiscalStorage()) {
add("НЕОБНУЛ. СУММА ПРОДАЖ", report.salesAmount);
add("НЕОБ. СУММА ПОКУПОК", report.buyAmount);
}
}

public void addFiscalSign() throws Exception {
Expand Down Expand Up @@ -769,3 +773,4 @@ private void addEJLine(String s) throws Exception {
}

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
package com.shtrih.fiscalprinter.command;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;

public class TextReportStorage {
private final String filePath;

public TextReportStorage(String filePath) {

this.filePath = filePath;
}

public List<String> searchZReport(int dayNumber) throws Exception {
List<String> lines = loadLines();
int index1 = 0;

for (int i = 0; i < lines.size(); i++) {
String line = lines.get(i);

int dayNum = getDayNumber(line);
if (dayNum == -1)
continue;

if (dayNum == dayNumber) {
int index2 = findNextDocument(lines, i + 1);
if (index2 != -1)
index2 -= 1;

return copyLines(lines, index1, index2);
} else {
index1 = findNextDocument(lines, i + 1);
}
}

return null;
}

private List<String> loadLines() throws Exception {
List<String> result = new ArrayList<String>();
BufferedReader reader = new BufferedReader(new FileReader(filePath));
String line;
try {
while ((line = reader.readLine()) != null) {
result.add(line);
}
} finally {
reader.close();
}
return result;
}

private int getDayNumber(String line) throws Exception {
if (line.contains(TextDocumentFilter.SZReportText)) {
int startIndex = line.length() - 4;
String number = line.substring(startIndex, startIndex + 4);
return Integer.parseInt(number);
}
return -1;
}

private int findNextDocument(List<String> lines, int index) {
for (int i = index; i < lines.size(); i++) {
String line = lines.get(i);
if (isDocumentHeader(line)) {
return i;
}
}
return lines.size();
}

private boolean isDocumentHeader(String line) {
return line.contains(TextDocumentFilter.SINN);
}

private List<String> copyLines(List<String> lines, int index1, int index2) {
List<String> result = new ArrayList<String>();
for (int i = index1; i <= index2; i++) {
if (i < 0)
return result;
if (i >= lines.size())
return result;
result.add(lines.get(i));
}
return result;
}

public List<String> getDocument(int docNumber) throws Exception {
int index1 = -1;
int index2 = -1;
List<String> lines = loadLines();

for (int i = lines.size() - 1; i >= 0; i--) {
String line = lines.get(i);
int docNum = getDocumentNumber(line);

if (docNum == -1)
continue;

if (docNum == docNumber) {
if (index2 == -1)
index2 = findNextDocument(lines, i + 1);

if (index2 != -1)
index2 -= 1;

index1 = i;
break;
}
}
if (index1 == -1) {
return null;
}

return copyLines(lines, index1, index2);
}

private int getDocumentNumber(String line) throws Exception {
if (line.contains(TextDocumentFilter.SINN) && line.contains("№")) {
int startIndex = line.indexOf("№") + 1;
String number = line.substring(startIndex, startIndex + 4);
return Integer.parseInt(number);
}
return -1;
}

public List<String> getCurrentDayReport() throws Exception {
List<String> lines = loadLines();
int index1 = 0;

for (int i = 0; i < lines.size(); i++) {
String line = lines.get(i);

int dayNum = getDayNumber(line);
if (dayNum == -1)
continue;

index1 = findNextDocument(lines, i + 1);
}

if(index1 == -1 || index1 == lines.size())
return null;

return copyLines(lines, index1, lines.size());
}
}
Loading

0 comments on commit 71eb8bd

Please sign in to comment.