OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 108 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
108 ilm 1
package org.openconcerto.modules.ocr.parser;
2
 
3
import java.math.BigDecimal;
4
import java.util.Date;
5
import java.util.List;
6
 
7
import org.openconcerto.modules.ocr.InvoiceOCR;
8
import org.openconcerto.modules.ocr.OCRLine;
9
import org.openconcerto.modules.ocr.OCRPage;
10
 
11
public class OrangeInvoiceParser extends AbstractInvoiceParser {
12
	@Override
13
	public boolean parse(OCRPage page) {
14
		if (!page.contains("orange") || page.contains("tech data")) {
15
			return false;
16
		}
17
 
18
		final InvoiceOCR invoice = this.getInvoice();
19
		final List<OCRLine> lines = page.getLines();
20
		invoice.setSupplierName("Orange");
21
		try {
22
			for (OCRLine line : lines) {
23
				final String text = line.getText().toLowerCase();
24
				// Get invoice number
25
				if (invoice.getInvoiceNumber() == null) {
26
					final String[] split = text.split("\\s+");
27
					final StringBuilder n = new StringBuilder();
28
					if (split.length > 2) {
29
		                final String label = split[0].replace("li", "u").replace("t", "f");
30
		                if(label.length() == 7 && label.startsWith("f")){
31
    						int splitLength = split.length;
32
    						for(int i = 2; i < splitLength; i++){
33
    							n.append(split[i]);
34
    						}
35
    						final String[] split2 = n.toString().replace(":", "-").replace("—", "-").replace(".", "-").replace("~", "-").replace(" ", "").replace("(", "").split("-");
36
    						if(split2.length > 1){
37
    						    String subs = split2[0].substring(split2[0].length() - 2, split2[0].length() - 1);
38
    						    if(ParserUtils.isInteger(subs)){
39
    						        subs = (subs.equals("6") || subs.equals("3")) ? "g" : subs;
40
    						        subs = (subs.equals("1")) ? "l" : subs;
41
    						    }
42
    						    split2[0] = split2[0].substring(0, split2[0].length() - 2) + subs + split2[0].substring(split2[0].length() - 1, split2[0].length());
43
 
44
    						}
45
 
46
    						n.delete(0, n.length());
47
                            int split2Length = split2.length;
48
                            for(int i = 0; i < split2Length; i++){
49
                                n.append(split2[i]);
50
                            }
51
 
52
                            if(n.length() > 9){
53
                                n.insert(10, " ");
54
                                if(n.length() > 15){
55
                                    String tempChar = n.substring(1, 2);
56
                                    if(tempChar.equals("8")){
57
                                        n.replace(1, 2, "3");
58
                                    }
59
 
60
                                    tempChar = n.substring(13, 14);
61
                                    if(ParserUtils.isInteger(tempChar)){
62
                                        if(tempChar.equals("9")){
63
                                            n.replace(13, 14, "G");
64
                                        }
65
                                    }
66
                                    tempChar = n.substring(14, 15);
67
                                    if(!ParserUtils.isInteger(tempChar)){
68
                                        if(tempChar.equals("o")){
69
                                            n.replace(14, 15, "0");
70
                                        }
71
                                    }
72
                                    tempChar = n.substring(15, 16);
73
                                    if(!ParserUtils.isInteger(tempChar)){
74
                                        if(tempChar.equals("i") || tempChar.equals("l")){
75
                                            n.replace(15, 16, "1");
76
                                        }
77
                                    }
78
 
79
 
80
                                    tempChar = n.substring(17, 18);
81
                                    if(!ParserUtils.isInteger(tempChar)){
82
                                        if(tempChar.equals("m")){
83
                                            n.replace(18, 19, "11");
84
                                        }
85
                                    }
86
                                    n.insert(15, " - ");
87
                                    if(n.length() >= 22){
88
                                        tempChar = n.substring(19, 20);
89
                                        if(!tempChar.equals("f")){
90
                                            n.replace(19, 20, "f");
91
                                        }
92
                                        tempChar = n.substring(21, 22);
93
                                        if(!ParserUtils.isInteger(tempChar)){
94
                                            if(tempChar.equals("o")){
95
                                                n.delete(21, 22);
96
                                                n.insert(21, "0");
97
                                            }
98
                                        }
99
                                        n.delete(22, n.length());
100
                                    }
101
                                }
102
                            }
103
 
104
                            String finalNumber = n.toString();
105
                            if(checkInvoiceNumber(finalNumber)){
106
                                invoice.setInvoiceNumber(finalNumber.toUpperCase());
107
                            }
108
		                }
109
					}
110
				}
111
				// Get amount and amount with tax
112
				if (invoice.getAmount() == null && text.contains("total de votre facture")) {
113
					List<BigDecimal> listValue = getDecimalsInLine(text);
114
					if (listValue.size() > 1) {
115
						invoice.setAmount(listValue.get(0));
116
						invoice.setAmountWithTax(listValue.get(1));
117
						addHighlight(page, line);
118
					}
119
				}
120
				// Get current page number and total page
121
				if (invoice.getTotalPage() == -1 && text.contains("page")) {
122
					final String[] split = text.split("\\s+");
123
					final String[] split2 = split[split.length - 1].split("/");
124
					if (split2.length > 1) {
125
						if(ParserUtils.isInteger(split[0]) && ParserUtils.isInteger(split[1])){
126
							page.setPageNumber(Integer.parseInt(split2[0]));
127
							invoice.setTotalPage(Integer.parseInt(split2[1]));
128
						}
129
					}
130
				}
131
				// Get date
132
				if(invoice.getDate() == null && text.contains("du")){
133
					final Date d = ParserUtils.parseDate(text);
134
					if (d != null) {
135
						invoice.setDate(d);
136
						addHighlight(page, line);
137
					}
138
				}
139
			}
140
            this.checkInvoice(true);
141
            invoice.setHighlight(this.getHighlight());
142
		} catch (Exception e) {
143
			return false;
144
		}
145
		return true;
146
	}
147
 
148
	@Override
149
	public void reset() {
150
		super.reset();
151
	}
152
 
153
    @Override
154
    protected boolean checkInvoiceNumber(String invoiceNumber) {
155
        boolean result = true;
156
        final String[] split = invoiceNumber.split("\\s+");
157
 
158
        if(split.length != 4){
159
            result = false;
160
        } else {
161
            if(split[0].length() != 10 || !ParserUtils.isLong(split[0])){
162
                result = false;
163
            } else if(split[1].length() != 4){
164
                result = false;
165
            } else if(!split[2].equals("-")){
166
                result = false;
167
            } else if(split[3].length() != 4){
168
                result = false;
169
            }
170
        }
171
        return result;
172
    }
173
}