OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 108 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

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