imageOCRAjax.jsp
29.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
<%@page import="weaver.fna.invoice.interfaces.baiwang.FnaBaiwangOpenApi"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="weaver.fna.invoice.interfaces.baiwang.FnaBaiwangInterface"%>
<%@page import="weaver.fna.invoice.interfaces.guoxin.FnaGuoxinInterface"%>
<%@page import="weaver.fna.invoice.common.FnaAbstractInterface"%>
<%@page import="org.apache.commons.lang.StringEscapeUtils"%>
<%@page import="weaver.fna.invoice.entity.FnaInvoiceInterface"%>
<%@page import="net.sf.json.JSONArray"%>
<%@page import="weaver.fna.invoice.common.FnaInvoiceCommon"%>
<%@page import="sun.misc.BASE64Decoder"%>
<%@page import="weaver.fna.general.FnaCommon"%>
<%@page import="java.io.*"%>
<%@page import="net.sf.json.JSONObject"%>
<%@page import="weaver.file.FileUpload"%>
<%@page import="weaver.systeminfo.SystemEnv"%>
<%@page import="weaver.hrm.User"%>
<%@page import="weaver.hrm.HrmUserVarify"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" %>
<%@ page import="weaver.general.*,weaver.conn.*,java.util.*" %>
<%@page import="java.text.DecimalFormat"%>
<%
User user = HrmUserVarify.getUser (request , response) ;
if(user==null){
return;
}
JSONObject returnJson = new JSONObject();
BaseBean bb = new BaseBean();
FnaInvoiceCommon fnaInvoiceCommon = new FnaInvoiceCommon();
DecimalFormat df = new DecimalFormat("############################0.00");
SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat format2 = new SimpleDateFormat("yyyyMMdd");
RecordSet rs = new RecordSet();
JSONArray dataArray = new JSONArray();
FileUpload fu = new FileUpload(request,false);
String operate = Util.null2String(fu.getParameter("operate")).trim();
int requestid = Util.getIntValue(fu.getParameter("requestid"),0);
//是否是编辑页面
boolean editPage = false;
String checktime = Util.date(2);
String checkdate = Util.date(1);
int userid = user.getUID();
String interfaceType = "百望";
String interfaceUrl = "";
String failMsg = "";
double weightpercent = 0;
rs.execute("select interfaceurl,weightpercent from fnainvoiceinterface where type = 2 and interfaceType = 0 and status = 1");
if(rs.next()){
interfaceUrl = Util.null2String(rs.getString("interfaceurl"));
weightpercent = Util.getDoubleValue(rs.getString("weightpercent"),80.00);
}
try{
FnaAbstractInterface fnaAbstractInterface = fnaInvoiceCommon.getInterface(user);
if("callOcr".equals(operate)){//调用ocr接口
//########################上传图片开始########################
String ocrType = Util.null2String(fu.getParameter("ocrType")).trim();
String date = Util.date(1);
String fileName = FnaCommon.getPrimaryKeyGuid1();
String fileData = Util.null2String(fu.getParameter("img")).trim();
fileData = fileData.replaceFirst("data:image/jpeg;base64,", "");
if("".equals(fileData)){
throw new Exception("请上传正确的文件!");
}
BASE64Decoder decoder = new BASE64Decoder();
byte[] b = decoder.decodeBuffer(fileData);
for(int i=0;i<b.length;++i)
{
if(b[i]<0)
{//调整异常数据
b[i]+=256;
}
}
long fileLen = b.length;
if(fileLen > FnaInvoiceCommon.MAX_BYTES){//大于2.5M,进行压缩
b = fnaInvoiceCommon.compressImage(b);
}
if(b.length == 0) throw new Exception("出现问题,请联系系统管理员!");
JSONObject post = fnaInvoiceCommon.postImage(b, ocrType,user);
int status = post.getInt("status");
if(status == 1){
throw new Exception(post.getString("msg"));
}
JSONObject postjson = JSONObject.fromObject(post.get("returnInfo"));
new BaseBean().writeLog("postjson", postjson);
//########################上传图片结束########################
String resultCode = postjson.getString("resultCode");
String resultMessage = postjson.getString("resultMessage");
if(!"succeeded".equals(resultCode)){
//直接返回编辑页面
returnJson.put("flag", true);
returnJson.put("msg", postjson.getString("resultMessage"));
editPage = true;
returnJson.put("editPage", editPage);
returnJson.put("data", dataArray);
out.print(returnJson);
return;
}
//有发票返回的话验证发票是否正确(必须是增值锐专用发票和普票校验,其他的不用校验)
String ocrResult = postjson.getString("ocrResult");
JSONObject ocrResultJson = JSONObject.fromObject(ocrResult);
if("InvoiceNormal".equals(ocrType)){//普通发票
JSONObject json = new JSONObject();
String invoiceNumber = "";
String invoiceCode = "";
String billingDate = "";
String priceWithoutTax = "";
String taxIncludedPrice = "";
String checkCode = "";
JSONArray order = ocrResultJson.getJSONArray("order");
JSONObject words = ocrResultJson.getJSONObject("words");
if(words.containsKey("发票号码")){
JSONObject keyjson = words.getJSONObject("发票号码");
invoiceNumber = Util.null2String(keyjson.getString("word"));
}
if(words.containsKey("发票代码")){
JSONObject keyjson = words.getJSONObject("发票代码");
invoiceCode = Util.null2String(keyjson.getString("word"));
}
if(words.containsKey("开票日期")){
JSONObject keyjson = words.getJSONObject("开票日期");
billingDate = Util.null2String(keyjson.getString("word"));
}
if(words.containsKey("不含税金额")){
JSONObject keyjson = words.getJSONObject("不含税金额");
priceWithoutTax = df.format(Util.getDoubleValue(keyjson.getString("word"),0.00));
}
if(words.containsKey("价税合计")){
JSONObject keyjson = words.getJSONObject("价税合计");
taxIncludedPrice = df.format(Util.getDoubleValue(keyjson.getString("word"),0.00));
}
if(words.containsKey("校验码")){
JSONObject keyjson = words.getJSONObject("校验码");
checkCode = Util.null2String(keyjson.getString("word"));
}
String billingDate_bak = billingDate;
if(!"".equals(billingDate)){
//发票日期转换校验
billingDate = fnaInvoiceCommon.judgeDateFormart(billingDate,format1);
}
if("".equals(invoiceNumber) || "".equals(invoiceCode) || "".equals(billingDate) || "".equals(checkCode)){//校验的条件不满足时,直接返回到编辑页面
editPage = true;
json.put("invoiceNumber", invoiceNumber);
json.put("invoiceCode", invoiceCode);
json.put("billingDate", billingDate);
json.put("priceWithoutTax", priceWithoutTax);
json.put("taxIncludedPrice", taxIncludedPrice);
json.put("checkCode", checkCode);
dataArray.add(json);
returnJson.put("msg", "当前发票有个别字段未识别出,请手动填写");
//插入错误数据
failMsg = "增值税普通发票有字段未识别出,请手动填写";
String insert = "insert into FnaInvoiceCheckFailLog(checktime,checkdate,requestid,userid,interfaceType,interfaceUrl,failMsg,handleStatus) values ("+
"'"+checktime+"','"+checkdate+"',"+requestid+","+userid+",'"+interfaceType+"','"+interfaceUrl+"','"+StringEscapeUtils.escapeSql(failMsg)+"',0)";
}else{
//根据发票号码和发票代码到发票表里面查询是否存在已有数据,存在的话直接返回数据,不需要调用接口
rs.execute("select * from fnaInvoiceLedger where invoiceNumber = '"+invoiceNumber+"' and invoiceCode = '"+invoiceCode+"'");
if(rs.getCounts() == 0){
//拼发票扫二维码字符串
String result = "01,04,"+invoiceCode+","+invoiceNumber+","+priceWithoutTax+","+billingDate_bak+","+fnaInvoiceCommon.addCheckCode(checkCode)+",1111,";
bb.writeLog("result", result);
JSONObject resultObj = fnaAbstractInterface.scanQRCodeCheck("0", result,null);
bb.writeLog("resultObj", resultObj);
int checkstatus = resultObj.getInt("status");
if(checkstatus == 0){
editPage = true;
json.put("invoiceNumber", invoiceNumber);
json.put("invoiceCode", invoiceCode);
json.put("billingDate", billingDate);
json.put("priceWithoutTax", priceWithoutTax);
json.put("taxIncludedPrice", taxIncludedPrice);
json.put("checkCode", checkCode);
dataArray.add(json);
returnJson.put("msg", resultObj.getString("errMsg"));
}else{
//获取发票信息
String title = resultObj.getString("title");
String _taxIncludedPrice = resultObj.getString("_taxIncludedPrice");
String _priceWithoutTax = resultObj.getString("_priceWithoutTax");
String _tax = resultObj.getString("_tax");
String _invoiceNumber = resultObj.getString("_invoiceNumber");
String _invoicecode = resultObj.getString("_invoicecode");
String _purchaser = resultObj.getString("_purchaser");
String _seller = resultObj.getString("_seller");
String _billingDate = resultObj.getString("_billingDate");
String _checkCode = resultObj.getString("_checkCode");
JSONArray detaildata = resultObj.getJSONArray("detaildata");
json.put("title", title);
json.put("_taxIncludedPrice", _taxIncludedPrice);
json.put("_priceWithoutTax", _priceWithoutTax);
json.put("_tax", _tax);
json.put("_invoiceNumber", _invoiceNumber);
json.put("_invoicecode", _invoicecode);
json.put("_purchaser", _purchaser);
json.put("_seller", _seller);
json.put("_billingDate", _billingDate);
json.put("_checkCode", _checkCode.substring(14));
json.put("_checkCodeFull", _checkCode);
json.put("detaildata", detaildata);
returnJson.put("msg", "验票成功!");
dataArray.add(json);
}
}else{
//判断是否是当前人员的发票
rs.execute("select taxIncludedPrice,priceWithoutTax,tax,invoiceNumber,invoicecode,purchaser,seller,billingDate,checkCode "+
" from fnaInvoiceLedger where invoiceNumber = '"+invoiceNumber+"' and invoiceCode = '"+invoiceCode+"' and userid_new="+userid);
if(rs.getCounts() == 0){
throw new Exception("当前发票已经被其他人使用,请勿重复使用!");
}
while(rs.next()){
json.put("title", "增值税普通发票");
json.put("_taxIncludedPrice", Util.null2String(rs.getString("taxIncludedPrice")));
json.put("_priceWithoutTax", Util.null2String(rs.getString("priceWithoutTax")));
json.put("_tax", Util.null2String(rs.getString("tax")));
json.put("_invoiceNumber", Util.null2String(rs.getString("invoiceNumber")));
json.put("_invoicecode", Util.null2String(rs.getString("invoicecode")));
json.put("_purchaser", Util.null2String(rs.getString("purchaser")));
json.put("_seller", Util.null2String(rs.getString("seller")));
json.put("_billingDate",Util.null2String(rs.getString("billingDate")));
json.put("_checkCode", Util.null2String(rs.getString("checkCode")).substring(14));
returnJson.put("msg", "验票成功!");
dataArray.add(json);
}
}
}
}else if("InvoiceSpecial".equals(ocrType)){//专用发票
JSONObject json = new JSONObject();
String invoiceNumber = "";
String invoiceCode = "";
String billingDate = "";
String priceWithoutTax = "";
String taxIncludedPrice = "";
JSONArray order = ocrResultJson.getJSONArray("order");
JSONObject words = ocrResultJson.getJSONObject("words");
if(words.containsKey("发票号码")){
JSONObject keyjson = words.getJSONObject("发票号码");
invoiceNumber = Util.null2String(keyjson.getString("word"));
}
if(words.containsKey("发票代码")){
JSONObject keyjson = words.getJSONObject("发票代码");
invoiceCode = Util.null2String(keyjson.getString("word"));
}
if(words.containsKey("开票日期")){
JSONObject keyjson = words.getJSONObject("开票日期");
billingDate = Util.null2String(keyjson.getString("word"));
}
if(words.containsKey("不含税金额")){
JSONObject keyjson = words.getJSONObject("不含税金额");
priceWithoutTax = df.format(Util.getDoubleValue(keyjson.getString("word"),0.00));
}
if(words.containsKey("价税合计")){
JSONObject keyjson = words.getJSONObject("价税合计");
taxIncludedPrice = df.format(Util.getDoubleValue(keyjson.getString("word"),0.00));
}
String billingDate_bak = billingDate;
if(!"".equals(billingDate)){
//发票日期转换校验
billingDate = fnaInvoiceCommon.judgeDateFormart(billingDate,format1);
}
if("".equals(invoiceNumber) || "".equals(invoiceCode) || "".equals(billingDate) || "".equals(priceWithoutTax)){//校验的条件不满足时,直接返回到编辑页面
editPage = true;
json.put("invoiceNumber", invoiceNumber);
json.put("invoiceCode", invoiceCode);
json.put("billingDate", billingDate);
json.put("priceWithoutTax", priceWithoutTax);
json.put("taxIncludedPrice", taxIncludedPrice);
dataArray.add(json);
returnJson.put("msg", "当前发票有个别字段未识别出,请手动填写");
//插入错误数据
failMsg = "增值税专用发票有字段未识别出,请手动填写";
String insert = "insert into FnaInvoiceCheckFailLog(checktime,checkdate,requestid,userid,interfaceType,interfaceUrl,failMsg,handleStatus) values ("+
"'"+checktime+"','"+checkdate+"',"+requestid+","+userid+",'"+interfaceType+"','"+interfaceUrl+"','"+StringEscapeUtils.escapeSql(failMsg)+"',0)";
}else{
//根据发票号码和发票代码到发票表里面查询是否存在已有数据,存在的话直接返回数据,不需要调用接口
rs.execute("select * from fnaInvoiceLedger where invoiceNumber = '"+invoiceNumber+"' and invoiceCode = '"+invoiceCode+"'");
if(rs.getCounts() == 0){
//拼发票扫二维码字符串
String result = "01,01,"+invoiceCode+","+invoiceNumber+","+priceWithoutTax+","+billingDate_bak+",,1111,";
bb.writeLog("result", result);
JSONObject resultObj = fnaAbstractInterface.scanQRCodeCheck("0", result,null);
bb.writeLog("resultObj", resultObj);
int checkstatus = resultObj.getInt("status");
if(checkstatus == 0){
editPage = true;
json.put("invoiceNumber", invoiceNumber);
json.put("invoiceCode", invoiceCode);
json.put("billingDate", billingDate);
json.put("priceWithoutTax", priceWithoutTax);
json.put("taxIncludedPrice", taxIncludedPrice);
dataArray.add(json);
returnJson.put("msg", resultObj.getString("errMsg"));
}else{
//获取发票信息
String title = resultObj.getString("title");
String _taxIncludedPrice = resultObj.getString("_taxIncludedPrice");
String _priceWithoutTax = resultObj.getString("_priceWithoutTax");
String _tax = resultObj.getString("_tax");
String _invoiceNumber = resultObj.getString("_invoiceNumber");
String _invoicecode = resultObj.getString("_invoicecode");
String _purchaser = resultObj.getString("_purchaser");
String _seller = resultObj.getString("_seller");
String _billingDate = resultObj.getString("_billingDate");
JSONArray detaildata = resultObj.getJSONArray("detaildata");
json.put("title", title);
json.put("_taxIncludedPrice", _taxIncludedPrice);
json.put("_priceWithoutTax", _priceWithoutTax);
json.put("_tax", _tax);
json.put("_invoiceNumber", _invoiceNumber);
json.put("_invoicecode", _invoicecode);
json.put("_purchaser", _purchaser);
json.put("_seller", _seller);
json.put("_billingDate", _billingDate);
json.put("detaildata", detaildata);
returnJson.put("msg", "验票成功!");
dataArray.add(json);
}
}else{
//判断是否是当前人员的发票
rs.execute("select taxIncludedPrice,priceWithoutTax,tax,invoiceNumber,invoicecode,purchaser,seller,billingDate,checkCode "+
" from fnaInvoiceLedger where invoiceNumber = '"+invoiceNumber+"' and invoiceCode = '"+invoiceCode+"' and userid_new="+userid);
if(rs.getCounts() == 0){
throw new Exception("当前发票已经被其他人使用,请勿重复使用!");
}
while(rs.next()){
json.put("title", "增值税专用发票");
json.put("_taxIncludedPrice", Util.null2String(rs.getString("taxIncludedPrice")));
json.put("_priceWithoutTax", Util.null2String(rs.getString("priceWithoutTax")));
json.put("_tax", Util.null2String(rs.getString("tax")));
json.put("_invoiceNumber", Util.null2String(rs.getString("invoiceNumber")));
json.put("_invoicecode", Util.null2String(rs.getString("invoicecode")));
json.put("_purchaser", Util.null2String(rs.getString("purchaser")));
json.put("_seller", Util.null2String(rs.getString("seller")));
json.put("_billingDate",Util.null2String(rs.getString("billingDate")));
returnJson.put("msg", "验票成功!");
dataArray.add(json);
}
}
}
}else{
editPage = true;//其他发票的显示页面都是编辑页面
//获取发票信息
JSONArray order = ocrResultJson.getJSONArray("order");
JSONObject words = ocrResultJson.getJSONObject("words");
for(int i = 0;i<order.size();i++){
JSONObject json = new JSONObject();
String keytype = Util.null2String(order.get(i).toString());
String word = "";
double weight = 0d;
if(words.containsKey(keytype)){
JSONObject keyjson = words.getJSONObject(keytype);
if(keyjson.containsKey("word")) word = Util.null2String(keyjson.getString("word"));
if(keyjson.containsKey("weight")) weight = Util.getDoubleValue(keyjson.getString("weight"),0.00);
}
if(weight < weightpercent){
failMsg = "字段"+keytype+"的置信度(%)小于后台百望接口所设置的最小的置信度!";
String insert = "insert into FnaInvoiceCheckFailLog(checktime,checkdate,requestid,userid,interfaceType,interfaceUrl,failMsg,handleStatus) values ("+
"'"+checktime+"','"+checkdate+"',"+requestid+","+userid+",'"+interfaceType+"','"+interfaceUrl+"','"+StringEscapeUtils.escapeSql(failMsg)+"',0)";
rs.execute(insert);
}
if(("HighSpeedTrainTicket".equals(ocrType) && "出发时间".equals(keytype))
|| keytype.indexOf("日期") >=0 ){//高铁票,火车票的日期需要校验转换,//其他含有日期的发票日期也要校验并转换
word = fnaInvoiceCommon.judgeDateFormart(word,format1);
}else if(("HighSpeedTrainTicket".equals(ocrType) && "出发时间".equals(keytype))
|| keytype.indexOf("金额") >=0 ){
word = word.replace("元", "");
}
json.put("keytype", keytype);
json.put("word", word);
dataArray.add(json);
returnJson.put("msg", "验票成功!请核对字段是否正确!");
}
}
returnJson.put("flag", true);
returnJson.put("editPage", editPage);
returnJson.put("data", dataArray);
}else if("SaveAndAdd".equals(operate)){//保存并添加至流程
int workflowid = Util.getIntValue(fu.getParameter("workflowid"),0);
int dindex = Util.getIntValue(fu.getParameter("dindex"),-99);
String ocrType = Util.null2String(fu.getParameter("ocrType")).trim();
String priceincludetax = Util.null2String(fu.getParameter("priceincludetax")).trim();
String price = Util.null2String(fu.getParameter("price")).trim();
String tax = Util.null2String(fu.getParameter("tax")).trim();
String number = Util.null2String(fu.getParameter("number")).trim();
String code = Util.null2String(fu.getParameter("code")).trim();
String checkcode = Util.null2String(fu.getParameter("checkcode")).trim();
String saledate = Util.null2String(fu.getParameter("saledate")).trim();
editPage = Boolean.parseBoolean(fu.getParameter("editPage"));
String seller_w = Util.null2String(fu.getParameter("seller_w")).trim();
String buyer_w = Util.null2String(fu.getParameter("buyer_w")).trim();
String checkCodeFull = Util.null2String(fu.getParameter("checkCodeFull")).trim();
int invoiceid = 0;
if("InvoiceNormal".equals(ocrType) || "InvoiceSpecial".equals(ocrType)){//普通发票和专用发票 //1,2
if(editPage){//进行编辑的状态下才进行校验
//先进行校验
//调用验票接口进行校验
String result = "";
String invoiceType = "";
if("InvoiceNormal".equals(ocrType)){
invoiceType = "1";
result = "01,04,"+code+","+number+","+price+","+saledate.replace("-", "")+","+checkcode+",1111,";
}else{
invoiceType = "2";
result = "01,01,"+code+","+number+","+price+","+saledate.replace("-", "")+",,1111,";
}
JSONObject resultObj = fnaAbstractInterface.scanQRCodeCheck("0", result,null);
int checkstatus = resultObj.getInt("status");
if(checkstatus == 0){
throw new Exception(resultObj.getString("errMsg"));
}else{
//获取发票信息
String title = resultObj.getString("title");
String _taxIncludedPrice = resultObj.getString("_taxIncludedPrice");
String _priceWithoutTax = resultObj.getString("_priceWithoutTax");
String _tax = resultObj.getString("_tax");
String _invoiceNumber = resultObj.getString("_invoiceNumber");
String _invoicecode = resultObj.getString("_invoicecode");
String _purchaser = resultObj.getString("_purchaser");
String _seller = resultObj.getString("_seller");
String _billingDate = resultObj.getString("_billingDate");
String _taxRate = resultObj.getString("_taxRate");
String _checkCode = resultObj.getString("_checkCode");
//保存到数据库
if("InvoiceSpecial".equals(ocrType)) _checkCode = "";
if(!fnaInvoiceCommon.ifExist(_invoiceNumber, rs)){
//不存在的发票保存到数据库中
int baiwangtype = Util.getIntValue(bb.getPropValue("baiwang", "type"),0);
if(baiwangtype == 0){
_billingDate = _billingDate.replace("年", "").replace("月", "").replace("日", "").trim();
//兼容国信和百望日期
_billingDate = _billingDate.substring(0, 4)+"-"+ _billingDate.substring(4, 6)+"-"+_billingDate.substring(6);
}
String insertInvoice = " insert into fnaInvoiceLedger (billingDate,invoiceCode,invoiceNumber,"+//1
"invoiceType,seller,purchaser,"+//2
"invoiceServiceYype,priceWithoutTax,taxRate,tax,taxIncludedPrice,authenticity,"+//3
"checkcode,userid_new,invoiceSource_new,status)"+//4
" VALUES "+
"('"+StringEscapeUtils.escapeSql(_billingDate)+"','"+StringEscapeUtils.escapeSql(_invoicecode)+"','"+StringEscapeUtils.escapeSql(_invoiceNumber)+"',"+//1
" "+invoiceType+",'"+StringEscapeUtils.escapeSql(_seller)+"','"+StringEscapeUtils.escapeSql(_purchaser)+"',"+//2
" '',"+_priceWithoutTax+","+_taxRate+","+_tax+","+_taxIncludedPrice+",1,"+//3
"'"+StringEscapeUtils.escapeSql(_checkCode)+"',"+userid+","+FnaInvoiceCommon.INVOICESOURCE_BAIWANG+","+FnaInvoiceCommon.INIT_STATUS+")";//4
boolean sqlFlag = rs.execute(insertInvoice);
if(!sqlFlag){
throw new Exception("sql错误,位置imageOcr-00100");
}
//获取发票id
rs.execute("select max(id) invoiceid from fnaInvoiceLedger");
if(rs.next()){
invoiceid = Util.getIntValue(rs.getString("invoiceid"));
}
//写入发票明细信息
JSONArray detaildata = resultObj.getJSONArray("detaildata");
fnaInvoiceCommon.saveInvoiceDetailInfo(detaildata, invoiceid);
}else{
//已经存在的发票获取发票id,先判断发票是否为当前人员的
rs.execute("select id from fnaInvoiceLedger where invoiceNumber = '"+_invoiceNumber+"' and userid_new = "+userid);
if(rs.getCounts() == 0){
throw new Exception("当前发票已经被其他人使用,请勿重复使用!");
}
if(rs.next()){
invoiceid = Util.getIntValue(rs.getString("id"));
}
}
}
}else{
if(!fnaInvoiceCommon.ifExist(number, rs)){
String invoiceType = "";
if("InvoiceNormal".equals(ocrType)){
invoiceType = "1";
}else{
invoiceType = "2";
}
//不存在的发票保存到数据库中
String insertInvoice = " insert into fnaInvoiceLedger (billingDate,invoiceCode,invoiceNumber,"+//1
"invoiceType,seller,purchaser,"+//2
"invoiceServiceYype,priceWithoutTax,taxRate,tax,taxIncludedPrice,authenticity,"+//3
"checkcode,userid_new,invoiceSource_new,status)"+//4
" VALUES "+
"('"+StringEscapeUtils.escapeSql(saledate)+"','"+StringEscapeUtils.escapeSql(code)+"','"+StringEscapeUtils.escapeSql(number)+"',"+//1
" "+invoiceType+",'"+StringEscapeUtils.escapeSql(seller_w)+"','"+StringEscapeUtils.escapeSql(buyer_w)+"',"+//2
" '',"+priceincludetax+","+tax+","+tax+","+price+",1,"+//3
"'"+StringEscapeUtils.escapeSql(checkCodeFull)+"',"+userid+","+FnaInvoiceCommon.INVOICESOURCE_BAIWANG+","+FnaInvoiceCommon.INIT_STATUS+")";//4
boolean sqlFlag = rs.execute(insertInvoice);
if(!sqlFlag){
throw new Exception("sql错误,位置imageOcr-00200");
}
//获取发票id
rs.execute("select max(id) invoiceid from fnaInvoiceLedger");
if(rs.next()){
invoiceid = Util.getIntValue(rs.getString("invoiceid"));
}
String detaildata = Util.null2String(fu.getParameter("detaildata")).trim();
JSONArray detailArray = JSONArray.fromObject(detaildata);
fnaInvoiceCommon.saveInvoiceDetailInfo(detailArray, invoiceid);
}else{
//已经存在的发票获取发票id,先判断发票是否为当前人员的
rs.execute("select id from fnaInvoiceLedger where invoiceNumber = '"+number+"' and userid_new = "+userid);
if(rs.getCounts() == 0){
throw new Exception("当前发票已经被其他人使用,请勿重复使用!");
}
if(rs.next()){
invoiceid = Util.getIntValue(rs.getString("id"));
}
}
}
}else if("GenPrintingInvoice".equals(ocrType) || "RollingInvoice".equals(ocrType) || "FixPriceInvoice".equals(ocrType) || "SubwayTicket".equals(ocrType)
||"TaxiReceit".equals(ocrType)){
//通用机打发票 //3
//卷式发票 //4
//定额发票 //5
//地铁票 //6
//出租发票 //7
//保存到数据库
String invoiceType = "GenPrintingInvoice".equals(ocrType)?"3":"RollingInvoice".equals(ocrType)?"4":"FixPriceInvoice".equals(ocrType)?"5":
"SubwayTicket".equals(ocrType)?"6":"7";
if(!fnaInvoiceCommon.ifExist(number, rs)){
//不存在的发票保存到数据库中
String insertInvoice = " insert into fnaInvoiceLedger (billingDate,invoiceCode,invoiceNumber,"+//1
"invoiceType,"+//2
"taxIncludedPrice,"+//3
"userid_new,invoiceSource_new,status,authenticity)"+//4
" VALUES "+
"('"+StringEscapeUtils.escapeSql(saledate)+"','"+StringEscapeUtils.escapeSql(code)+"','"+StringEscapeUtils.escapeSql(number)+"',"+//1
" "+invoiceType+",'"+//2
" "+price+//3
" "+"',"+userid+","+FnaInvoiceCommon.INVOICESOURCE_BAIWANG+","+FnaInvoiceCommon.INIT_STATUS+",1)";//4
boolean sqlFlag = rs.execute(insertInvoice);
if(!sqlFlag){
throw new Exception("sql错误,位置imageOcr-00300");
}
rs.execute("select max(id) invoiceid from fnaInvoiceLedger");
if(rs.next()){
invoiceid = Util.getIntValue(rs.getString("invoiceid"));
}
}else{
//已经存在的发票获取发票id,先判断发票是否为当前人员的
rs.execute("select id from fnaInvoiceLedger where invoiceNumber = '"+number+"' and userid_new = "+userid);
if(rs.getCounts() == 0){
throw new Exception("当前发票已经被其他人使用,请勿重复使用!");
}
if(rs.next()){
invoiceid = Util.getIntValue(rs.getString("id"));
}
}
}else if("HighSpeedTrainTicket".equals(ocrType)){//动车/高铁票
//获取自增的号码,用于前台显示
String incrementCode = fnaInvoiceCommon.getfnaGrowCode(ocrType);
int invoiceType = 8;
String invoiceServiceYype = "HighSpeedTrainTicket".equals(ocrType)?"动车/高铁票":"火车票";
//保存进数据库
String insert = "insert into fnaInvoiceLedger(invoiceNumber,invoiceCode,invoiceType,invoiceServiceYype,taxIncludedPrice,billingDate,userid_new,invoiceSource_new,authenticity,status) values("+
" '"+incrementCode+"','"+incrementCode+"',"+invoiceType+",'"+invoiceServiceYype+"',"+price+",'"+saledate+"',"+userid+","+FnaInvoiceCommon.INVOICESOURCE_BAIWANG+",1,"+FnaInvoiceCommon.INIT_STATUS+")";
rs.execute(insert);
rs.execute("select max(id) invoiceid from fnaInvoiceLedger");
if(rs.next()){
invoiceid = Util.getIntValue(rs.getString("invoiceid"));
}
}
if(invoiceid <= 0 || workflowid <= 0 || dindex == -99){
throw new Exception("添加至流程失败,请联系系统管理员");
}
//查询当前流程是否关联票据流程
rs.execute("select * from fnaInvoiceWfInfo where workflowid = "+workflowid);
if(rs.getCounts() == 0){
throw new Exception("该流程未关联票据流程设置,请在后台【预算】-【发票管理】-【票据流程设置】中设置!");
}
//关联了票据流程,但是未设置字段对应
if(dindex == -2){
dindex = -1;
}
dindex = dindex + 1;
rs.execute("select fieldid from fnaInvoiceWfInfoField where workflowid = "+workflowid+" and tabindex = "+dindex);
if(rs.getCounts() == 0){
throw new Exception("该流程关联的票据流程未设置:"+(dindex==0?"主表":"明细表"+dindex)+"的发票号码字段对应!");
}
int fieldid = 0;
if(rs.next()){
fieldid = Util.getIntValue(rs.getString("fieldid"));
}
String invoiceNumber_2 = "";
rs.execute("select invoiceNumber from fnaInvoiceLedger where id = "+invoiceid);
if(rs.next()){
invoiceNumber_2 = Util.null2String(rs.getString("invoiceNumber"));
}
returnJson.put("flag", true);
returnJson.put("id", invoiceid);
returnJson.put("name", invoiceNumber_2);
returnJson.put("dindex", dindex);
returnJson.put("fieldid", fieldid);
}else if("getbase64".equals(operate)){
String uploaddata = Util.null2String(fu.getParameter("uploaddata")).trim();
String uploadname = Util.null2String(fu.getParameter("uploadname")).trim();
uploadname = uploadname.substring(uploadname.lastIndexOf(".")+1);
if(!("jpg".equals(uploadname) || "png".equals(uploadname))){
throw new Exception("请选择正确的发票图片!");
}
//判断是否是图片
returnJson.put("flag", true);
returnJson.put("imgdata", uploaddata);
}
}catch(Exception e){
returnJson.put("flag", false);
returnJson.put("msg", e.getMessage());
}
if(!"getbase64".equals(operate)){
new BaseBean().writeLog("returnJson:", returnJson);
}
out.print(returnJson);
%>