From b012fccf647c4d253174e5dfcdea4d15335c3436 Mon Sep 17 00:00:00 2001
From: doom4ster <doom4ster@gmail.com>
Date: 星期三, 19 九月 2018 16:24:23 +0800
Subject: [PATCH] 66板

---
 CB_IMGPSScanImp.pas |  724 ++++++++++++++++++++++++++++++++++++++++++++-----------
 1 files changed, 580 insertions(+), 144 deletions(-)

diff --git a/CB_IMGPSScanImp.pas b/CB_IMGPSScanImp.pas
index 821aa08..ca8c548 100644
--- a/CB_IMGPSScanImp.pas
+++ b/CB_IMGPSScanImp.pas
@@ -257,6 +257,7 @@
     ISB1: TImageScrollBox;
     Label3: TLabel;
     LogFile1: TLogFile;
+    ScanGrayCB: TCheckBox;
     procedure ActiveFormCreate(Sender: TObject);
     procedure Panel9Resize(Sender: TObject);
     procedure ISB1Click(Sender: TObject);
@@ -372,6 +373,9 @@
     procedure ImageScrollBox1NewGraphic(const Graphic: TDibGraphic);
     procedure SmoothCBClick(Sender: TObject);
     procedure N1Click(Sender: TObject);
+    procedure TreeView1MouseEnter(Sender: TObject);
+    procedure ScrollBox1MouseEnter(Sender: TObject);
+    procedure ScanGrayCBClick(Sender: TObject);
   private
     { Private declarations }
     HotKeyId1,HotKeyId2,HotKeyId3,HotKeyId4 :Integer;
@@ -564,6 +568,8 @@
     NowDivPageFormID:String;
 
     FirstDocDir : String;
+    FMaxUploadSize:String;// 上傳zip大小限制
+    FJpgCompression:integer;// 20171211 jpg to tif 的壓縮率
 
     Draging : Boolean;
 
@@ -847,6 +853,8 @@
     function CheckRequiredColumnValues(workno,caseno:String) :Boolean;  //20171003  此大類下此案是否檢核必填
     function logTimeString :String;
     procedure ReduceLogFile ;
+    function FindLastestDocDirForPage(CaseID, DocNo, formid: String): String;
+    function OMRErrini2ListForLog(CaseID: String):String;
   protected
     { Protected declarations }
     procedure DefinePropertyPages(DefinePropertyPage: TDefinePropertyPage); override;
@@ -1024,32 +1032,89 @@
 end;
 
 procedure TCB_IMGPSScanX.WMMOUSEWHEEL(var message: TWMMouseWheel);
+var
+  I: Integer;
+  iISB : TImageScrollBox;
 begin
     inherited;
+    //lb1.Caption:=IntToStr(message.Keys);
+    if (message.WheelDelta = WHEEL_DELTA) Then
+    begin
 
-    if (message.WheelDelta = WHEEL_DELTA) Then begin
-     if Edit1.Focused then
-     begin
-       ScrollBox1.VertScrollBar.Increment := 50;
-       ScrollBox1.Perform(WM_VSCROLL, SB_LINEUP, 0);
-     end
-     else if DisplayISB.Focused then
-     begin
-       DisplayISB.VertScrollBar.Increment := 50;
-       DisplayISB.Perform(WM_VSCROLL, SB_LINEUP, 0);
-     end;
-     end else if (message.WheelDelta = -WHEEL_DELTA) then begin
-     if Edit1.Focused then
-     begin
-       ScrollBox1.VertScrollBar.Increment := 50;
-       ScrollBox1.Perform(WM_VSCROLL, SB_LINEDOWN, 0);
-     end
-     else if DisplayISB.Focused then
-     begin
-       DisplayISB.VertScrollBar.Increment := 50;
-       DisplayISB.Perform(WM_VSCROLL, SB_LINEDOWN, 0);
-     end;
+      if ScrollBox1.Focused then
+      begin
+        ScrollBox1.VertScrollBar.Increment := 50;
+        ScrollBox1.Perform(WM_VSCROLL,SB_Lineup,0);
+      end;
+      if DisplayISB <> nil then
+      begin
+        if (DisplayISB.Focused) and (message.Keys=0) then
+        begin
+           DisplayISB.VertScrollBar.Increment := 50;
+           DisplayISB.Perform(WM_VSCROLL,SB_Lineup,0);
+        end;
+
+        if (DisplayISB.Focused) and (message.Keys=50) then
+        begin
+          DisplayISB.ZoomMode := zmPercent;
+          if DisplayISB.ZoomPercent < 90 then
+            DisplayISB.ZoomPercent := DisplayISB.ZoomPercent+10;
+        end;
+
+      end;
+      i:=0;
+      while FindComponent(ISBName+inttostr(i)) <> nil do
+      begin
+        iISB := TImageScrollBox(FindComponent(ISBName+inttostr(i)));
+        if iISB.Focused then
+        begin
+          ScrollBox1.VertScrollBar.Increment := 50;
+          ScrollBox1.Perform(WM_VSCROLL,SB_Lineup,0);
+        end;
+        inc(i);
+      end;
+
+    end
+    else if (message.WheelDelta = -WHEEL_DELTA) then
+    begin
+
+      if ScrollBox1.Focused then
+      begin
+        ScrollBox1.VertScrollBar.Increment := 50;
+        ScrollBox1.Perform(WM_VSCROLL, SB_LINEDOWN, 0);
+      end;
+      if DisplayISB <> nil then
+      begin
+        if (DisplayISB.Focused) and (message.Keys=0) then
+        begin
+           DisplayISB.VertScrollBar.Increment := 50;
+           DisplayISB.Perform(WM_VSCROLL,SB_LINEDOWN,0);
+        end;
+        if (DisplayISB.Focused) and (message.Keys=50) then
+        begin
+          DisplayISB.ZoomMode := zmPercent;
+          if DisplayISB.ZoomPercent > 10 then
+            DisplayISB.ZoomPercent := DisplayISB.ZoomPercent-10;
+        end;
+
+
+      end;
+
+      i:=0;
+      while FindComponent(ISBName+inttostr(i)) <> nil do
+      begin
+        iISB := TImageScrollBox(FindComponent(ISBName+inttostr(i)));
+        if iISB.Focused then
+        begin
+          ScrollBox1.VertScrollBar.Increment := 50;
+          ScrollBox1.Perform(WM_VSCROLL,SB_Lineup,0);
+        end;
+        inc(i);
+      end;
+
     end;
+
+
 end;
 
 procedure TCB_IMGPSScanX.WNoteBtnClick(Sender: TObject);
@@ -1223,53 +1288,24 @@
   ST1,ST2,ST3,ST4:TStringList;
   i,j,k:Integer;
 begin
-//ShowMessage('casepath='+casepath);
+
   ST1:=TStringList.Create;
   ST2:=TStringList.Create;
   ST3:=TStringList.Create;
 
-//FImgDelete:='Y';
-  {if FileExists(casepath+'CaseDocNo.dat') then
-  begin
-    ST1.LoadFromFile(casepath+'CaseDocNo.dat');
-    for I := 0 to ST1.Count - 1 do
-    begin
-      ST2.LoadFromFile(casepath+ST1.Strings[i]+'\Context.dat');
-      for j := 0 to ST2.Count - 1 do
-      begin
-        ExistImgList.Add(casepath+ST1.Strings[i]+'\'+ST2.Strings[j])
-      end;
-    end;
-  end;
-
-  if FileExists(casepath+'S_Attach\Context.dat') then
-  begin
-    ST3.LoadFromFile(casepath+'S_Attach\Context.dat');
-    for I := 0 to ST3.Count - 1 do
-    begin
-      ExistImgList.Add(casepath+'S_Attach\'+ST3.Strings[i])
-    end;
-  end;
-
-  if FileExists(casepath+'Attach\Context.dat') then
-  begin
-    ST3.LoadFromFile(casepath+'Attach\Context.dat');
-    for I := 0 to ST3.Count - 1 do
-    begin
-      ExistImgList.Add(casepath+'Attach\'+ST3.Strings[i])
-    end;
-  end; }
   ExistImgList.Clear;
   ST1.LoadFromFile(casepath+'Download\Context.dat');
   for I := 0 to ST1.Count - 1 do
   begin
+LogFile1.LogToFile(logTimeString+casepath+'Download\'+ST1.Strings[i]+',MD5='+LoadFileGetMD5(casepath+'Download\'+ST1.Strings[i]));
     ExistImgList.Add(LoadFileGetMD5(casepath+'Download\'+ST1.Strings[i])) ;
   end;
+LogFile1.LogToFile(logTimeString+'ExistImgList.text'+ExistImgList.CommaText);
 
   ST1.Free;
   ST2.Free;
   ST3.Free;
-//ShowMessage('ExistImgList='+ExistImgList.Text);
+
 end;
 
 procedure TCB_IMGPSScanX.Initialize;
@@ -1333,7 +1369,7 @@
 
 procedure TCB_IMGPSScanX.ISB1Enter(Sender: TObject);
 begin
-  //ISB1.SetFocus;
+  ISB1.SetFocus;
 end;
 
 procedure TCB_IMGPSScanX.ISB1ImageMouseDown(Sender: TObject; Button: TMouseButton;
@@ -1342,7 +1378,8 @@
   p : Integer;
 begin
   DisplayISB := TImageScrollBox(Sender);
-  DisplayISB.SetFocus;
+  if NowClick<>0 then
+    DisplayISB.SetFocus;
   Shape1.Left := TPanel(TImageScrollBox(Sender).Parent).Left - Seg;
   Shape1.Top := TPanel(TImageScrollBox(Sender).Parent).Top - Seg;
   P := strtoint(copy(DisplayISB.Name,4,1)) + ScrollBar1.Position-2;
@@ -1377,17 +1414,25 @@
 begin
    //Edit1.SetFocus;
   //TImageScrollBox(Sender).SetFocus;
+
   if TImageScrollBox(Sender).FileName = '' then
-  begin
-//ShowMessage('AAA');
-Label3.Caption:='FileName='+TImageScrollBox(Sender).FileName+'  time='+FormatDateTime('yyyy/mm/dd HH:MM:SS', now);
     TImageScrollBox(Sender).MouseMode := mmUser
-  end
   Else
-  begin
-Label3.Caption:='FileName='+TImageScrollBox(Sender).FileName;
     ViewMouseMode(NowClick);
-  end;
+
+  ISB1.AlwaysShowAnnotations := False;
+
+//  if TImageScrollBox(Sender).FileName = '' then
+//  begin
+////ShowMessage('AAA');
+//Label3.Caption:='FileName='+TImageScrollBox(Sender).FileName+'  time='+FormatDateTime('yyyy/mm/dd HH:MM:SS', now);
+//    TImageScrollBox(Sender).MouseMode := mmUser
+//  end
+//  Else
+//  begin
+//Label3.Caption:='FileName='+TImageScrollBox(Sender).FileName;
+//    ViewMouseMode(NowClick);
+//  end;
 end;
 
 procedure TCB_IMGPSScanX.ISB1ImageMouseUp(Sender: TObject; Button: TMouseButton;
@@ -1646,6 +1691,12 @@
   ExistImgList.Free;
   reSizeExistImgList.Free;
   //********清單區********
+  if (FMode = 'DSCAN') or (FMode = 'ESCAN')  then //重掃件及異動件要只能掃指定編號的件
+  begin
+    if ImagePath<>'' then
+      _Deltree(ImagePath);
+  end;
+
   if FEvents <> nil then FEvents.OnDestroy;
 end;
 
@@ -1980,6 +2031,7 @@
   if TreeView1.Selected.Parent = MyTreeNode1 then
     PreNode2Name:= GetNode2Name(MyTreeNode2);
   ShowText := _Msg('文件歸類中,請稍候');
+LogFile1.LogToFile(logTimeString+'縮圖  歸類開始');
   DataLoading(True,True);
   GoAtt := False;
   if (MytreeNode2 <> nil) and (Pos('Attach',MyTreeNode2.Text)>0) then
@@ -2041,6 +2093,7 @@
 
 //ShowMessage('DocNoNeedDiv(DocNo)='+BoolToStr(DocNoNeedDiv(DocNo),true));
 //ShowMessage('DocDir='+DocDir);
+
           if DocNoNeedDiv(DocNo) then   //要分份數
           begin
             if ((FormCode2Page(FormID) = '01') and (GetDocDir_Page(NowCaseno,DocDir)>0)) or (DocDir = '') then
@@ -2077,6 +2130,7 @@
               DocDir:=DocNo2DocNoDir(ImageSavePath+NowCaseno+'\',DocNo);
             SetDocNoList('A',-1,NowCaseno,DocDir);
           end; }
+LogFile1.LogToFile(logTimeString+'縮圖  FormID='+FormID);
           if Not DirectoryExists(ImageSavePath+NowCaseno+'\'+DocDir) then
             Mkdir(ImageSavePath+NowCaseno+'\'+DocDir);
           ContextList.Clear;
@@ -2147,6 +2201,7 @@
   end;
   if CustomDocNo = '' then Exit;
   DocDir := CustomDocNo;
+LogFile1.LogToFile(logTimeString+'縮圖 歸類自訂文件 DocDir='+DocDir);
   SavePath := ImageSavePath+NowCaseNo+'\'+DocDir+'\';
   Str2Dir(SavePath);
   SetDocNoList('A',-1,NowCaseNo,DocDir,'1');
@@ -2238,7 +2293,7 @@
 //          Exit;
 //        end;
 //      end;
-
+LogFile1.LogToFile(logTimeString+'縮圖刪除 iISB.FileName='+iISB.FileName);
       DeleteImageFile(ExtractFilePath(iISB.FileName),ExtractFileName(iISB.FileName),NowCaseNo);
     end;
   end;
@@ -2276,6 +2331,7 @@
     Showmessage(_msg('資訊尚未下載完成,請稍候或重新進入'));
     Exit;
   end;
+LogFile1.LogToFile(logTimeString+'掃瞄開始');
   if (FMode = 'RSCAN') or (FMode = 'ESCAN') or (FMode = 'DSCAN') then
   begin
     if NewTreeNode.Count > 0 then
@@ -2305,6 +2361,7 @@
     ContextList.Clear;
     Try
       StatrTwainScan;
+LogFile1.LogToFile(logTimeString+'掃瞄結束');
     Except
       Panel1.Enabled := True;
       Panel2.Enabled := True;
@@ -2465,9 +2522,10 @@
   P,v,v1,v2,ln,i : Integer;
   iDocDir,iDocNo : String;
 begin
+LogFile1.LogToFile(logTimeString+'Tree 按下刪除');
   if TreeView1.Selected = NewTreeNode then  //全刪  //新掃描件
   begin
-
+LogFile1.LogToFile(logTimeString+'Tree 全部刪除');
     If Messagedlg(_Msg('是否刪除所有案件?'),mtconfirmation,[mbyes,mbcancel],0) = mrcancel then Exit;
     clearView(1);
     Application.ProcessMessages;
@@ -2482,7 +2540,7 @@
   end
   Else if TreeView1.Selected = MyTreeNode1 then       //案件編號
   begin
-
+LogFile1.LogToFile(logTimeString+'Tree 案件編號刪除 NowCaseno='+NowCaseno);
     If Messagedlg(Format(_Msg('編號(%s)是否刪除?'),[NowCaseno]),mtconfirmation,[mbyes,mbcancel],0) = mrcancel then Exit;
     clearView(1);
     Application.ProcessMessages;
@@ -2528,6 +2586,7 @@
     ClearView(1);
     Application.ProcessMessages;
 //ShowMessage(NowDocDir);
+LogFile1.LogToFile(logTimeString+'Tree 文件層號刪除 NowDocDir='+NowDocDir);
     if (Length(NowDocDir)=8) or (NowDocDir=AttName) then
     begin
 //ShowMessage('DeleteDocNoFileForESCAN');
@@ -2552,7 +2611,7 @@
   Else if TreeView1.Selected = MyTreeNode3 then       //FormID層
   begin
     If Messagedlg(Format(_Msg('文件(%s)是否刪除?'),[NowFormName]),mtconfirmation,[mbyes,mbcancel],0) = mrcancel then Exit;
-
+LogFile1.LogToFile(logTimeString+'Tree FormID層號刪除 NowFormCode='+NowFormCode);
     DeleteFormCodeFile(NowCaseNo,NowDocDir,NowFormCode);
     SetRecordEditedDocDir('A',NowCaseNo,NowDocDir);
     DrawDocItem2(MytreeNode1,NowCaseno);
@@ -2571,7 +2630,6 @@
   Result:=False;
   ST:=TStringList.Create;
   ST.loadFromfile(DocNopath+'\Context.dat');
-//ShowMessage(ST.Text);
   for I := 0 to ST.Count - 1 do
   begin
     if ISExistImg(DocNopath+ST.Strings[i]) then
@@ -2662,11 +2720,13 @@
   if (Treeview1.Selected = NewTreeNode) {or (Treeview1.Selected = MyTreeNode1)} then
   begin
 //ShowMessage('NewScanBtnClick');
+LogFile1.LogToFile(logTimeString+'Tree NewScanBtnClick');
     NewScanBtnClick(self)
   end
   Else
   begin
 //ShowMessage('AddScanBtnclick');
+LogFile1.LogToFile(logTimeString+'Tree AddScanBtnclick');
     AddScanBtnclick(self);
   end;
 end;
@@ -2688,6 +2748,7 @@
   SaveStream     : TFileStream;
   SaveStreamA:TFileStream;
   SaveStreamB:TFileStream;
+  cooom:integer;
 Begin
   OpenDialog1.Filter := 'Image files|*.TIF;*.JPG';
   If OpenDialog1.Execute Then
@@ -2695,6 +2756,7 @@
     ISB := TImageScrollBox.Create(self);
 try
     ShowText := _Msg('檔案加入中,請稍候');
+LogFile1.LogToFile(logTimeString+'檔案加入中開始');
     DataLoading(True, True);
     If TreeView1.Selected = Nil Then
       Exit;
@@ -2710,12 +2772,15 @@
 //ShowMessage(IntToStr(FileRec.Size)+','+IntToStr(FFileSizeLimit * 1024));
     If FileRec.Size > FFileSizeLimit * 1024 Then // 檢查檔案大小
     Begin
-      ShowMessage(Format('%.3f', [FileRec.Size / 1024]) +
-        ' KB, file size over limit.');
+      ShowMessage(Format('目前檔案大小為 %.3f MB', [FileRec.Size / (1024*1024)]) +
+        ',已超過單一檔案匯入限制'+Format('%.1f',[FFileSizeLimit/1024])+'MB');
       FindClose(FileRec);
       DataLoading(false, false);
       Exit;
     End;
+    //MessageDlg()
+    //cooom:=StrToInt(InputBox('輸入百分比','輸入百分比',''));
+    cooom:=FJpgCompression;//20171211彩色tif採jpg壓縮的比例
 
     FindClose(FileRec);
     CaseID := NowCaseno;
@@ -2726,15 +2791,34 @@
       ShowText := _Msg('檔案加入中,請稍候(' + inttostr(i) + '/' + inttostr(P) + ')');
       DataLoading(True, True);
       ISB.LoadFromFile(FName, i);
-      // ISB.SaveToFile('C:\OCX\aaaaaa'+IntToStr(i)+'.tif');
       DeskewImg(ISB.Graphic);
-      // ISB.SaveToFile('C:\OCX\bbbbbb'+IntToStr(i)+'.tif');
+
+
+      ISB_BW.Graphic.Assign(ISB.Graphic); //20180104
+      If ISB.Graphic.ImageFormat <> ifBlackWhite Then   //20180104
+      begin
+        ConvertToBW(ISB_BW.Graphic);
+      end;
+      ///ISB_BW.SaveToFile('KKKKKKKK.tif');
 
       iGraphic_First := TTiffGraphic.Create;
       iGraphic_sec := TTiffGraphic.Create;
 
+
+      //ShowMessage(IntToStr(iGraphic_First.Palette.palNumEntries)); //彩色 會為0  黑白 為2
+
+      MpsGetBarcode(ISB_BW.Graphic, MpsBarcodeinf); //判斷A3 有用FormID 所以要先辨條碼
+      For n := 1 To MpsBarcodeinf.Count Do
+      Begin
+        If MpsBarcodeinf.r180[n] <> 0 Then // 依條碼角度轉影像
+        Begin
+          Rotate(ISB.Graphic, MpsBarcodeinf.r180[n]);
+          //MpsGetBarcode(iGraphic_First, MpsBarcodeinf);
+          Break;
+        End;
+      End;
+
       iGraphic_First.Assign(ISB.Graphic);
-      MpsGetBarcode(ISB.Graphic, MpsBarcodeinf); //判斷A3 有用FormID 所以要先辨條碼
       //有必要的話先把影像轉正 再開始切圖
       If CheckNeedCrop(iGraphic_First) Then
       Begin
@@ -2762,17 +2846,25 @@
           (TreeView1.Selected = MyTreeNode1) Then
         Begin
           SaveFilename := '';
-          MpsGetBarcode(iGraphic, MpsBarcodeinf);
+          ISB_BW.Graphic.Assign(iGraphic); //20180104
+          If iGraphic.ImageFormat <> ifBlackWhite Then   //20180104
+          begin
+            ConvertToBW(ISB_BW.Graphic);
+          end;
+          MpsGetBarcode(ISB_BW.Graphic, MpsBarcodeinf);
+//ShowMessage(IntToStr(MpsBarcodeinf.Count));
           For n := 1 To MpsBarcodeinf.Count Do
           Begin
             If MpsBarcodeinf.r180[n] <> 0 Then // 依條碼角度轉影像
             Begin
               Rotate(iGraphic, MpsBarcodeinf.r180[n]);
-              MpsGetBarcode(iGraphic, MpsBarcodeinf);
+              MpsGetBarcode(ISB_BW.Graphic, MpsBarcodeinf);
               Break;
             End;
           End;
+//ShowMessage('XXX '+IntToStr(MpsBarcodeinf.Count));
           FormID := BarCode2FormID;
+//ShowMessage('FormID='+FormID);
           // 取出FormID
           SaveFilename := FormID;
           If (TreeView1.Selected = NewTreeNode) Then
@@ -2800,11 +2892,19 @@
 
           DocNo := FormCode2DocNo(FormID);
           DocDir := FindLastestDocDir(CaseID, DocNo);
+          if (FMode='ESCAN') and (FModeName='補件掃描') then
+          begin
+            DocDir := FindLastestDocDirForPage(CaseID, DocNo,FormID);
+//ShowMessage('DocDir='+DocDir);
+          end;
+
           If DocNoNeedDiv(DocNo) Then // 要分份數
           Begin
             If ((FormCode2Page(FormID) = '01') And
               (GetDocDir_Page(CaseID, DocDir) > 0)) Or (DocDir = '') Then
-              DocDir := DocNo2DocNoDir(ImageSavePath + CaseID + '\', DocNo);
+              begin
+                DocDir := DocNo2DocNoDir(ImageSavePath + CaseID + '\', DocNo);
+              end;
           End
           Else // 不分份數
           Begin
@@ -2836,10 +2936,16 @@
             If MpsBarcodeinf.r180[n] <> 0 Then // 依條碼角度轉影像
             Begin
               Rotate(iGraphic, MpsBarcodeinf.r180[n]);
-              MpsGetBarcode(iGraphic, MpsBarcodeinf);
+              MpsGetBarcode(ISB_BW.Graphic, MpsBarcodeinf);
               Break;
             End;
           End;
+//ShowMessage(IntToStr(iGraphic.Palette.palNumEntries));
+          if (iGraphic.Palette.palNumEntries = 0) or (iGraphic.Palette.palNumEntries = 256) then  //20171130 彩色 會為0  黑白 為2  灰階256
+          begin
+            iGraphic.Compression:=tcJPEG;
+            iGraphic.JpegQuality:=cooom;
+          end;
 
           If LowerCase(ExtractFileExt(SavePath + SaveFilename)) = '.tif' Then
           Begin
@@ -2871,9 +2977,6 @@
               // SaveStream.Free;
             End;
           End;
-
-          // Rotate(ISB.Graphic,MpsBarcodeinf.r180[n]);  //20170705 加入有條碼就轉正
-          // ISB.SaveToFile(SavePath+SaveFilename);
 
           SetContextList('A', -1, CaseID, DocDir, SaveFilename);
           If (TreeView1.Selected = NewTreeNode) Then
@@ -2914,7 +3017,12 @@
               Break;
             End;
           End;
-          // Rotate(ISB.Graphic,MpsBarcodeinf.r180[n]); //20170705 加入有條碼就轉正
+
+          if (iGraphic.Palette.palNumEntries = 0) or (iGraphic.Palette.palNumEntries = 256) then  //20171130 彩色 會為0  黑白 為2
+          begin
+            iGraphic.Compression:=tcJPEG;
+            iGraphic.JpegQuality:=cooom;
+          end;
 
           If LowerCase(ExtractFileExt(SavePath + SaveFilename)) = '.tif' Then
           Begin
@@ -3104,6 +3212,7 @@
 begin
   DocListForm := TDocListForm.Create(self);
   try
+LogFile1.LogToFile(logTimeString+'Tree 歸類開始');
     InitialLanguage(DocListForm); //載入多國語言
     DocListForm.CheckBox1.Visible:=True;
     for i := 1 to FORM_INF_List.Count - 1 do
@@ -3129,6 +3238,7 @@
       begin
         //歸類到自訂文件
         NewFormID:=DocListForm.Edit1.Text;
+
         if Trim(DocListForm.Edit1.Text)='' then
         begin
           Showmessage('未輸入文件名稱');
@@ -3151,6 +3261,7 @@
         end;
         CustomDocNo := GetNewCustomDocNo(DisplayPath,NewFormID);
 //ShowMessage('CustomDocNo='+CustomDocNo);
+LogFile1.LogToFile(logTimeString+'Tree 歸類到自訂文件 CustomDocNo='+CustomDocNo);
         ShowText := _Msg('歸類中,請稍侯');
         DataLoading(True,True);
         FormIDReplace(NowCaseNo,NowDocDir,NowFormCode,CustomDocNo+'010101A');
@@ -3170,7 +3281,8 @@
         end;
         ShowText := _Msg('歸類中,請稍侯');
         DataLoading(True,True);
-
+//ShowMessage('NowDocDir='+NowDocDir);
+LogFile1.LogToFile(logTimeString+'Tree 歸類到既有文件 NewFormID='+NewFormID);
         FormIDReplace(NowCaseNo,NowDocDir,NowFormCode,NewFormID);
 
       end;
@@ -3892,6 +4004,11 @@
 
 end;
 
+procedure TCB_IMGPSScanX.ScrollBox1MouseEnter(Sender: TObject);
+begin
+ScrollBox1.SetFocus;
+end;
+
 procedure TCB_IMGPSScanX.SelectScanBtnClick(Sender: TObject);
 begin
   Panel1.Enabled := False;
@@ -4039,11 +4156,11 @@
   //CreateFormID_FormName(DisplayPath);  //產生FormID_FormName.dat
   //CreateDocNo_DocName(DisplayPath); //產生DocNo_Name.dat
   //Showmessage(CreateDocNo_Info(DisplayPath));  //產生 Docno,份數,頁數;Docno,份數,頁數 的回傳字串
-  lb1.Caption:='AAAAAAAAAAA';
+  //lb1.Caption:='AAAAAAAAAAA';
   //Showmessage(CreateDocnoFrom_Info(NowCaseno));
   //Showmessage(self.CreateCustDocNoFrom_Info(NowCaseno));
-
-        initkscan;
+  //ShowMessage('FMaxUploadSize='+FMaxUploadSize);
+        //initkscan;
 	showmessage('FUrl='+FUrl+#10#13+
 	'FCaseID='+FCaseID+#10#13+
 	'FMode='+FMode+#10#13+
@@ -4074,7 +4191,8 @@
     'FImgDelete='+    FImgDelete+#10#13+
     'FIsExternal='+    FIsExternal+#10#13+
     'FWH_category='+FWH_category+
-    'FCheck_main_form='+    FCheck_main_form);
+    'FCheck_main_form='+    FCheck_main_form+#10#13+
+    'FMaxUploadSize='+FMaxUploadSize);
     //FImgDelete:='Y';
   LoadImgFile;
  { ShowMessage('UpLPoint='+IntToStr(UpLPoint.X)+','+IntToStr(UpLPoint.Y)+#10#13+
@@ -4143,6 +4261,10 @@
     Try
       Scanner.OpenSource;
       Scanner.Duplex := ScanDuplex; //雙面
+
+      if FMode='SAMPLESCAN' then
+        Scanner.Duplex:=False;
+
       //Scanner.FEEDERENABLED  := not ScanFlatCB.Checked;     // 先拿掉平台
       If ScanImgSetUse Then
       begin
@@ -4255,14 +4377,14 @@
           end;
         end;
         pScanInfo^.Graphic.Compression := tcJpeg;
-        pScanInfo^.Graphic.JpegQuality := 70;
+        pScanInfo^.Graphic.JpegQuality := FJpgCompression;
     end
     else if pScanInfo^.Graphic.ImageFormat = ifColor256 Then
     begin
       //Ext := '.jpg';
       ConvertToGray(pScanInfo^.Graphic);
       pScanInfo^.Graphic.Compression := tcJpeg;
-      pScanInfo^.Graphic.JpegQuality := 70;
+      pScanInfo^.Graphic.JpegQuality := FJpgCompression;
     end
     else if pScanInfo^.Graphic.ImageFormat = ifGray256 Then
     begin
@@ -4280,7 +4402,13 @@
         end;
       end;
       pScanInfo^.Graphic.Compression := tcJpeg;
-      pScanInfo^.Graphic.JpegQuality := 70;
+      pScanInfo^.Graphic.JpegQuality := FJpgCompression;
+//ShowMessage(IntToStr(pScanInfo^.Graphic.JpegQuality));
+//if pScanInfo^.Graphic.Compression = tcJpeg then
+//begin
+//ShowMessage('jpg');
+//end;
+
     end
     else
     begin
@@ -4313,6 +4441,19 @@
     end;
     //iGraphic.Assign(iGraphic_First);
     iGraphic := iGraphic_First;
+
+    if iGraphic.ImageFormat=ifGray256 then  //20180104
+    begin
+      iGraphic.Compression:=tcJPEG;
+      iGraphic.JpegQuality:=FJpgCompression;
+    end;
+    if iGraphic.ImageFormat=ifTrueColor then  //20180104
+    begin
+      iGraphic.Compression:=tcJPEG;
+      iGraphic.JpegQuality:=FJpgCompression;
+    end;
+
+//ShowMessage('WTF');
     while not iGraphic.IsEmpty do
     begin
       //Application.ProcessMessages;
@@ -4322,6 +4463,25 @@
         ImageScrollBox1NewGraphic(ImageScrollBox1.Graphic);
 
         MpsGetBarcode(ISB_BW.Graphic,MpsBarcodeinf);
+        For i := 1 To MpsBarcodeinf.Count Do
+        Begin
+          If MpsBarcodeinf.r180[i] <> 0 Then // 依條碼角度轉影像
+          Begin
+            Rotate(iGraphic, MpsBarcodeinf.r180[i]);
+            Break;
+          End;
+        End;
+
+        if iGraphic.ImageFormat=ifGray256 then  //20180104 因此旋轉後變為回packbits 所以要改為jpeg
+        begin
+          iGraphic.Compression:=tcJPEG;
+          iGraphic.JpegQuality:=FJpgCompression;
+        end;
+        if iGraphic.ImageFormat=ifTrueColor then
+        begin
+          iGraphic.Compression:=tcJPEG;
+          iGraphic.JpegQuality:=FJpgCompression;
+        end;
         PageEnd;
         IF PEFileName <> '' Then
         begin
@@ -4346,7 +4506,7 @@
             JpgGr := TJpegGraphic.Create;
             try
               JpgGr.Assign(iGraphic);
-              JpgGr.SaveQuality := 30;
+              JpgGr.SaveQuality := FJpgCompression;
               //JpgGr.AppendToStream(SaveStream);
               JpgGr.SaveToFile(PEFileName);
             finally
@@ -4491,7 +4651,9 @@
           Str2Dir(ScanPath);
 
           ScanDocDir := FindLastestDocDir(ScanCaseno,DocNo);
+//ShowMessage('AA ScanDocDir='+ScanDocDir);
 
+//ShowMessage('BB ScanDocDir='+ScanDocDir);
           if DocNoNeedDiv(DocNo)then   //要分份數
           begin
             //Showmessage(DocNo+#13+FormCode2Page(FormID)+#13+inttostr(GetDocDir_Page(ScanCaseno,ScanDocDir))+#13+ScanDocDir);
@@ -4567,7 +4729,12 @@
           FormID := NowGuideFormID;
         DocNo := FormCode2DocNo(FormID);
         ScanDocDir := FindLastestDocDir(ScanCaseno,DocNo);
-//ShowMessage('ScanDocDir='+ScanDocDir);
+
+        if (FMode='ESCAN') and (FModeName='補件掃描') then    //20180207 加入的特殊邏輯
+        begin
+          ScanDocDir := FindLastestDocDirForPage(ScanCaseno, DocNo,FormID);
+        end;
+
         if (DocNoNeedDiv(DocNo)) then   //要分份數
         begin
           if TreeView1.Selected = MyTreeNode1 then   //20170421 掃瞄插頁時選則在案號上才要分份數 選在FormID上就不分份數
@@ -5138,6 +5305,16 @@
     begin
       PARA_CONTENT := GetSQLData(WORK_INF_List,'PARA_CONTENT',i);
       DivPageFormIDList.CommaText := PARA_CONTENT;
+    end
+    Else if UpperCase(GetSQLData(WORK_INF_List,'PARA_NO',i)) = 'FILE_COMPRESSION' Then     //20171211 jpg to tif 壓縮比
+    begin
+      PARA_CONTENT := GetSQLData(WORK_INF_List,'PARA_CONTENT',i);
+      FJpgCompression := StrToInt(PARA_CONTENT);
+    end
+    Else if UpperCase(GetSQLData(WORK_INF_List,'PARA_NO',i)) = 'MAX_UPLOAD_SIZE' Then     //取得 上傳大小的限制(MB)
+    begin
+      PARA_CONTENT := GetSQLData(WORK_INF_List,'PARA_CONTENT',i);
+      FMaxUploadSize := PARA_CONTENT;
     end;
   end;
   ScanDuplex := Def_ScanDuplex;
@@ -5870,19 +6047,26 @@
 Function TCB_IMGPSScanX.CheckCaseDocNoPage(CaseID,DocNo,Version:String;Pages:Integer):Integer; //取案件裡的文件_版本頁數
 var
   i,n,Count : integer;
-  S : TStringlist;
+  S, S2 : TStringlist;
   FormCode,iPage : String;
+  docInt,tempInt:integer;
+  v,v2:integer;
 begin
   Count := 0;
+  docInt:=0;
+  tempInt:=0;
   S := TStringlist.Create;
+  S2 := TStringlist.Create;
+//ShowMessage('page='+IntToStr(Pages));
   try
     S.LoadFromFile(ImageSavePath+CaseID+'\upload\Context.dat');
+    S2.LoadFromFile(ImageSavePath+CaseID+'\upload\DocDir.dat'); //2017 1220 改成只承認第一份的
+
     for I := 1 to pages do //從0到pages-1  改成 1到pages 20170316 這樣可以修改檢核的頁數問題
     begin
       iPage := Add_Zoo(i,2);
       for n := 0 to S.Count - 1 do
       begin
-
         if (FWH_category='N') and (FIs_In_Wh='Y') then
         begin
           if ISExistImg(ImageSavePath+CaseID+'\upload\'+S.Strings[n]) then
@@ -5890,9 +6074,33 @@
             Continue;
           end;
         end;
+
+        if (Length(S2.Strings[n])>8) and (Pos(DocNo,S2.Strings[n])<>0) then //2017 1220 改成只承認第一份的
+        begin
+//LogFile1.LogToFile(logTimeString+'有進'+docno+','+S2.Strings[n]+IntToStr(Pos(DocNo,S2.Strings[n])));
+          v:=Pos('(',S2.Strings[n]);
+          v2:=Pos(')',S2.Strings[n]);
+          tempInt:=StrToInt(Copy(S2.Strings[n],v+1,v2-v-1));
+
+          if docInt=0 then
+          begin
+            docInt:=tempInt;
+          end;
+          if docInt<>tempInt then
+          begin
+//LogFile1.LogToFile(logTimeString+'docInt='+IntToStr(docInt)+',tempInt='+IntToStr(tempInt));
+            Result := Count;
+            Break;
+          end;
+        end;
+
         FormCode := FileName2FormCode(S.Strings[n]);
+//LogFile1.LogToFile(logTimeString+'S.Strings[n]='+S.Strings[n]);
 //Showmessage('1:'+version+','+FormCode2Version(FormCode)+','+DocNo+','+FormCode2DocNo(FormCode)+','+iPage+','+FormCode2Page(FormCode));
+//LogFile1.LogToFile(logTimeString+'FormCode='+FormCode);
 LogFile1.LogToFile(logTimeString+'CheckCaseDocNoPage caseno='+CaseID+','+version+','+FormCode2Version(FormCode)+','+DocNo+','+FormCode2DocNo(FormCode)+','+iPage+','+FormCode2Page(FormCode));
+//ShowMessage(DocNo+','+IntToStr(docInt)+','+IntToStr(tempInt));
+//LogFile1.LogToFile(logTimeString+'FormCode='+FormCode);
         if (version = FormCode2Version(FormCode)) and (DocNo = FormCode2DocNo(FormCode)) and (ipage = FormCode2Page(FormCode)) then
         begin
 LogFile1.LogToFile(logTimeString+'CheckCaseDocNoPage caseno='+CaseID+','+version+','+FormCode2Version(FormCode)+','+DocNo+','+FormCode2DocNo(FormCode)+','+iPage+','+FormCode2Page(FormCode));
@@ -5905,6 +6113,7 @@
     end;
   finally
   S.Free;
+  S2.free;
   end;
   Result := Count;
 end;
@@ -6110,6 +6319,8 @@
   casepath:String;
   filesizeInt:integer;
   case_page:string;
+  Fname:String;
+  FileRec:TSearchrec;
 begin
   Result := True;
   TransName := CaseID;
@@ -6173,7 +6384,7 @@
 
   for I := 0 to ST1.Count - 1 do
   begin
-    if (Pos('_',St1.Strings[i])<>1) and (Pos('_',St1.Strings[i])<>-11) then
+    if (Pos('_',St1.Strings[i])<>1) and (Pos('_',St1.Strings[i])<>-1) then
     begin
       str1:=Copy(ST1.Strings[i],1,Pos('_',St1.Strings[i])-1);
       ST2.Add(str1);
@@ -6227,6 +6438,26 @@
   if HaveMask then
     ZipMaskFile(Path,MaskPath,Path,'MaskImg.zip');  //有遮罩設定的才產生
   /////壓檔////
+
+  ///檢查上傳的zip大小////
+   FName :=Path+ 'Img.zip';
+
+    FindFirst(FName, faAnyfile, FileRec);
+
+    //FMaxUploadSize
+//ShowMessage(IntToStr(FileRec.Size));
+//Result:=False;
+//exit;           //目前上傳檔案大小為xxMB,已超過50MB,無法上傳    %.3f  ,[FileRec.Size / 1048576]
+    If FileRec.Size > StrtoInt(FMaxUploadSize) * 1048576 Then // 檢查檔案大小
+    Begin
+      ShowMessage(Format('%s目前上傳檔案大小為%.3fMB,已超過'+FMaxUploadSize+'MB,無法上傳',[caseid,FileRec.Size / 1048576]) );
+      FindClose(FileRec);
+      Result := False;
+      Exit;
+    End;
+    FindClose(FileRec);
+  ///檢查上傳的zip大小////
+//ShowMessage('last_add_formidstr='+last_add_formidstr);
   ////上傳/////
   SendData:='data='+HTTPEncode(UTF8Encode(FData))
   +'&verify='+FVerify
@@ -6246,19 +6477,19 @@
 //ShowMessage(FData+#10#13+Doc_Data);
   if not upFile(HTTPSClient,FUrl,'service/imgpsc/IMGPSC02/caseupload',SendData,'file',Path+'Img.zip',FReWrite,Memo1,False) then
   begin
-    Showmessage(Format(_Msg('傳送案件編號(%s)檔案時,網路發生錯誤!!'+_Msg('錯誤代碼:')),[CaseID])+Inttostr(HttpError.HttpErrorCode)+' '+HttpError.HttpReason);
+    Showmessage(Inttostr(HttpError.HttpErrorCode)+' '+HttpError.HttpReason+'.');
     Result := False;
     Exit;
   end;
   if memo1.Lines.Strings[0] = '1' then
   begin
-    Showmessage(Format(_Msg('傳送案件編號(%s)檔案時,網路發生錯誤!!')+_Msg('錯誤原因:'),[CaseID])+memo1.Lines.Strings[1]);
+    Showmessage(Format(_Msg('')+_Msg(''),[CaseID])+memo1.Lines.Strings[1]+'。');
     Result := False;
     Exit;
   end
-  Else if Pos('<script type="text/javascript" src="scripts/CW00/login.js"></script>',Memo1.Lines.Text) > 0 then
+  Else if Pos('<script type="text/javascript" src="scripts/IMGPS00/login.js"></script>',Memo1.Lines.Text) > 0 then
   begin
-    Showmessage(Format(_Msg('傳送案件編號(%s)檔案時,網路發生錯誤!!')+_Msg('錯誤原因:')+_Msg('閒置過久或被登出,請重新登入'),[CaseID]));
+    Showmessage(Format(_Msg('')+_Msg('')+_Msg('閒置過久或被登出,請重新登入'),[CaseID]));
     Result := False;
     Exit;
   end;
@@ -6511,7 +6742,7 @@
   end;
   if Memo1.Lines.Strings[0] = '1' then
   begin
-    HttpErrStr :=_Msg('錯誤原因:')+memo1.Lines.Strings[1];
+    HttpErrStr :=_Msg('錯誤原因:')+memo1.Lines.Strings[1]+'。';
     Result := False;
     Exit;
   end
@@ -7208,7 +7439,16 @@
     NewDocNo := FormCode2DocNo(NewFormID);
     NewDocDir := FindLastestDocDir(CaseID,NewDocNo);
     if NewDocDir = '' then
-      NewDocDir := NewDocNo;
+    begin
+      if DocNoNeedDiv(NewDocNo) then
+      begin
+        NewDocDir:=DocNo2DocNoDir(ImageSavePath + NowCaseno+'\',NewDocNo);
+      end
+      else
+      begin
+        NewDocDir := NewDocNo;
+      end;
+    end;
 //ShowMessage('NewDocDir='+NewDocDir);
     if DocNoNeedDiv(NewDocNo) and (FormCode2Page(NewFormID)='01') then
     begin
@@ -8005,6 +8245,35 @@
   //R_W_ScanIni('W');       //user要求改成預設後不能改
 end;
 
+procedure TCB_IMGPSScanX.ScanGrayCBClick(Sender: TObject);
+begin
+
+  if ScanGrayCB.Checked then
+  begin
+    ScanColor:=ifGray256;
+  end
+  else
+  begin
+    if FScanColor = 0 then
+    begin
+      ScanColor := ifBlackWhite;
+    end;
+
+    if FScanColor = 1 then
+    begin
+      //ScanColor := ifGray256 ;
+      ScanColor := ifBlackWhite; //
+    end;
+
+
+    if FScanColor = 2 then
+    begin
+      ScanColor := ifTrueColor ;
+    end;
+  end;
+
+end;
+
 Function TCB_IMGPSScanX.GetFormatID(CaseID: string):String;
 Var
   S : TStringlist;
@@ -8274,12 +8543,14 @@
 begin
   ShowText := _Msg('處理檢核失敗中,請稍候');
   DataLoading(True,True);
+LogFile1.LogToFile(logTimeString+'處理檢核失敗中開始');
   ErrlistForm := TErrlistForm.Create(Self);
   RejectCase := False;
   S := TStringlist.Create;
   try
     InitialLanguage(ErrlistForm);
     Application.ProcessMessages;
+    ErrlistForm.LogFile1.LogFile:=LogFile1.LogFile;
     ErrlistForm.DeleteBt.Caption := ErrlistForm.DeleteBt.Caption+'(&D)';
     ErrlistForm.iniPath := ImageSavePath + NowCaseNo+'\upload\';
     OMRErrini2List(NowCaseno,ErrlistForm);
@@ -8311,7 +8582,16 @@
       end;
       CaseHelpBtn.Visible := False;
       LoadImgFile;
-      Showmessage(NowCaseNo+_Msg('傳送完成'));
+      if (FMode='NSCAN') or (FMode='DSCAN') then
+      begin
+        Showmessage(NowCaseNo+_Msg('影像上傳完成。此案已進入下一流程。'));
+      end;
+
+      if FMode='ESCAN' then
+      begin
+        Showmessage(NowCaseNo+_Msg('影像已補件完成。'));
+      end;
+
       DataLoading(False,False);
     end
     Else
@@ -8401,6 +8681,7 @@
   CaseDocNo_CopiesList := TStringlist.Create;
   StrList := TStringlist.Create;
   ST1:=TStringList.Create;
+LogFile1.LogToFile(logTimeString+'產文件樹開始');
   try
     CaseNode.ImageIndex := 1;
     CaseNode.SelectedIndex := 1;
@@ -8421,7 +8702,8 @@
         FileList.LoadFromFile(ImageSavePath+Caseno+'\'+CaseDocNoList.Strings[i]+'\Context.dat');
       iDocNo := DocNoDir2DocNo(CaseDocNoList.Strings[i]);
       ST1.Clear;
-//ShowMessage(FileList.Text);
+
+LogFile1.LogToFile(logTimeString+'FileList.Text='+FileList.CommaText);
       if (FWH_category='N') and (FIs_In_Wh='Y') then
       begin
         for n := 0 to FileList.Count - 1 do
@@ -8441,11 +8723,9 @@
         end;
       end;
 
-
+LogFile1.LogToFile(logTimeString+'WH_category='+FWH_category+',Is_In_Wh='+FIs_In_Wh+',FileList.Text='+FileList.CommaText);
       if FileList.Count=0 then Continue;
 
-
-//Showmessage('aaa '+FileList.Text);
       DocNoCopies := Strtoint(CaseDocNo_CopiesList.Strings[i]);
       DocNoPage := FileList.Count;
       iDocNo := DocNoDir2DocNo(CaseDocNoList.Strings[i]);
@@ -8464,6 +8744,7 @@
 
       //DocNode := TreeView1.Items.AddChild(CaseNode,Format('%s{%s}-%d'+_msg('頁'),[CaseDocNoList.Strings[i],DocNo2DocName(Caseno,iDocNo),DocNoPage]));
       //DocNode := TreeView1.Items.AddChild(CaseNode,Format('%s{%s}-%d'+_msg('份'),[CaseDocNoList.Strings[i],DocNo2DocName(Caseno,iDocNo),DocNoCopies]));
+//ShowMessage('iDocNo='+iDocNo);
       DocNode := TreeView1.Items.AddChild(CaseNode,Format('%s{%s}-%d'+_msg('份'),[DocNo2DocName(Caseno,iDocNo),CaseDocNoList.Strings[i],DocNoCopies]));
 
       if GetUseCase('F',ImageSavePath+Caseno+'\',CaseDocNoList.Strings[i]) <> '' Then
@@ -8622,7 +8903,7 @@
         end;
       end;
     end;
-
+LogFile1.LogToFile(logTimeString+'產文件樹結束');
   Finally
   FileList.Free;
   CaseDocNoList.Free;
@@ -8686,6 +8967,7 @@
   if FScanColor = 1 then
   begin
     ScanColor := ifGray256 ;
+    ScanGrayCB.Checked:=True;
   end;
 
   if FScanColor = 2 then
@@ -8928,7 +9210,7 @@
   ST1.LoadFromFile(path+'FormCode_Name.dat');
   for I := 0 to ST1.Count - 1 do
   begin
-    if (Pos('_',St1.Strings[i])<>1) and (Pos('_',St1.Strings[i])<>-11) then
+    if (Pos('_',St1.Strings[i])<>1) and (Pos('_',St1.Strings[i])<>-1) then
     begin
       str1:=Copy(ST1.Strings[i],1,Pos('_',St1.Strings[i])-1);
       LastInitFormidList.Add(str1);
@@ -9157,7 +9439,7 @@
 
 function TCB_IMGPSScanX.logTimeString: String;
 begin
-Result:=FormatDateTime('yyyymmdd hh:mm:ss',now) +'  '
+Result:=FormatDateTime('yyyymmdd hh:mm:ss',now) +'  caseNo='+NowCaseno+'  ';
 end;
 
 Function TCB_IMGPSScanX.FindDivFormCode(FormCode:String):Boolean; //找有沒有分案的條碼
@@ -9650,6 +9932,48 @@
   FileList.Free;
   end;
 end;
+
+Function TCB_IMGPSScanX.FindLastestDocDirForPage(CaseID,DocNo,formid:String):String; //找出最新的DocDir  20180207  排除隱藏的資料夾
+var
+  i,j:integer;
+  DocNoList,FileList : TStringlist;
+  Imglist: TStringlist;
+  DirIsHide:Boolean;
+begin
+  Result := '';
+  DocNoList := TStringlist.Create;
+  FileList := TStringlist.Create;
+  imglist := TStringlist.Create;
+  try
+  if FileExists(ImageSavePath+CaseID+'\CaseDocNo.dat') then
+    DocNoList.LoadFromFile(ImageSavePath+CaseID+'\CaseDocNo.dat');
+
+  for i := DocNoList.Count-1 downto 0 do
+  begin
+    if Copy(DocNoList.Strings[i],1,length(DocNo)) = DocNo then
+    begin
+//ShowMessage(ImageSavePath+CaseID+'\'+DocNoList.Strings[i]);
+      if not DocNoIsExistImg(ImageSavePath+CaseID+'\'+DocNoList.Strings[i]+'\') then
+      begin
+//ShowMessage('DDDDD');
+        Result := '';
+        Break;
+      end
+      else
+      begin
+        Result := DocNoList.Strings[i];
+        Break;
+      end;
+    end;
+  end;
+  finally
+    DocNoList.Free;
+    FileList.Free;
+    imglist.Free;
+  end;
+
+end;
+
 
 Procedure TCB_IMGPSScanX.Create_Cust_DocDir(CaseID:String); //產生外面傳入的文件代號及自定文件
 var
@@ -11163,6 +11487,7 @@
   begin
     If FindSQLData(Doc_Inf_List,'DOC_U_DESC','DOC_NO',DocNo,0,FindResult) Then
     begin
+//ShowMessage(FindResult.Text);
       Result := GetFindResult('DOC_U_DESC');
     end;
   end;
@@ -11465,6 +11790,7 @@
               MainFormPage := GetDataDocNoPage(MainDocNo,MainVersion); //資料庫記錄主文件頁數
               CaseFormPage := CheckCaseDocNoPage(CaseID,MainDocNo,MainVersion,MainFormPage); //案件主文件的頁數
 LogFile1.LogToFile(logTimeString+CaseID+',MainDocNo='+MainDocNo+',MainVersion='+MainVersion+',MainFormPage='+IntToStr(MainFormPage)+',CaseFormPage='+IntToStr(CaseFormPage));
+              //CheckFirstDocNoPage
               IF (MainFormPage > 0) and (MainFormPage <> CaseFormPage) then
               begin
                 ErrStr:=MainDocNo+Format(OMRErrInfo[1].Info,[Inttostr(MainFormPage),Inttostr(CaseFormPage)]); //主要文件需有XX頁僅附XX頁
@@ -11475,7 +11801,7 @@
             end;
           end;
           //////主要非主要文件/////
-          //showmessage(MainFormID);
+//showmessage(MainFormID);
           MainDocNo := FormCode2DocNo(MainFormID);
           MainVersion := FormCode2Version(MainFormID);
           if FindSQLData(DM_FORM_INF_List,'DEPE_FORM_ID,MUTEX_FORM_ID','MAIN_FORM_ID,DOC_VERSION',MainDocNo+','+MainVersion,0,FindResult) then
@@ -11536,7 +11862,7 @@
             end;
           end;
           //////互斥文件/////
-        end;
+        end; //MainFormID結束
       //end;
       List.LoadFromFile(ImageSavePath + CaseID+'\upload\Context.dat');
       for i := 0 to List.Count - 1 do
@@ -11620,15 +11946,9 @@
 LogFile1.LogToFile(logTimeString+'必填 '+CaseID+' '+'OMRFileList.Text='+OMRFileList.Text);
         For i := 0 to OMRFileList.Count - 1 do
         begin
+//ShowMessage('OMRFileList.Strings['+IntToStr(i)+']'+OMRFileList.Strings[i]);
           if CheckRequiredColumnValues(FWork_NO,CaseID) then Continue;
 
-//          if FWH_category='N' then
-//          begin
-//            if ISExistImg(ImageSavePath+CaseID+'\upload\'+OMRFileList.Strings[i]) then
-//            begin
-//              Continue;
-//            end;
-//          end;
           OMRFile := OMRFileList.Strings[i];
           OMRFormCode := FileName2FormCode(OMRFileList.Strings[i]);
           OMRFormName := FormCode2FormName(CaseID,OMRFormCode);
@@ -11638,10 +11958,11 @@
           ////依十字定位點縮放///
 //Display1.Lines.Add('OMRFile='+OMRFile+',OMRFormCode='+OMRFormCode+',OMRFormName='+OMRFormName);
 LogFile1.LogToFile(logTimeString+'OMRFile='+OMRFile+',OMRFormCode='+OMRFormCode+',OMRFormName='+OMRFormName);
+//ShowMessage('KKKKK');
           if not FileExists(CheckXmlPath+OMRFormCode+'.xml') then  //沒有Xml就不用檢核
             Continue;
 
-
+//ShowMessage('11638 OMRFileList.Strings['+IntToStr(i)+']'+OMRFileList.Strings[i]);
           XT := TXmltool.Create(CheckXmlPath+OMRFormCode+'.xml');
           RelaXT := TXmltool.Create;
 //ShowMessage('ModeNeedCheck='+BoolToStr(ModeNeedCheck(OMRErrInfo[4].Mode,FMode),true));
@@ -11694,7 +12015,7 @@
               Until not XT.SubNodes['/form/settype1/'].Next ;
             end;
             //*******必填********
-
+//ShowMessage('11692 OMRFileList.Strings['+IntToStr(i)+']'+OMRFileList.Strings[i]);
             //if AllCheck then
             //begin
             //*******有值相關文件的欄位也要有值*******
@@ -11733,10 +12054,16 @@
                       Anchor1 := FormID2Anchor(RelaFormCode);
                       RelaXT.LoadFromFile(CheckXmlPath+RelaFormCode+'.xml');
 Display1.Lines.Add(RelaFile+','+RelaFormName);
-                      ISB8.LoadFromFile(ImageSavePath+CaseID+'\upload\'+RelaFile,1); //20170815
-                      ISB8W:= ISB8.Graphic.Width;
-                      ISB8H:= ISB8.Graphic.Height;
-                      FindPoint(ISB8.Graphic,UpLPoint,UpRPoint,DownLPoint,ISB8W,ISB8H,ANCHOR);  //20170815  抓相關然欄位所在圖檔的定位
+//ShowMessage(RelaFile);
+//ShowMessage(ImageSavePath+CaseID+'\upload\'+RelaFile);
+                      if RelaFile<>'' then
+                      begin
+                        ISB8.LoadFromFile(ImageSavePath+CaseID+'\upload\'+RelaFile,1); //20170815
+                        ISB8W:= ISB8.Graphic.Width;
+                        ISB8H:= ISB8.Graphic.Height;
+                        FindPoint(ISB8.Graphic,UpLPoint,UpRPoint,DownLPoint,ISB8W,ISB8H,ANCHOR);  //20170815  抓相關然欄位所在圖檔的定位
+                      end;
+
                       OMROK := False;
                       //OMROK := False;
                       //if RelaXT.SubNodes['/form/settype2/'].First then
@@ -11790,6 +12117,7 @@
               Until not XT.SubNodes['/form/settype3/'].Next ;
             end;
             //*******有值相關文件的欄位也要有值*******
+//ShowMessage('11788 OMRFileList.Strings['+IntToStr(i)+']'+OMRFileList.Strings[i]);
 
             //*******有值相關文件的欄位不能有值*******
             if ModeNeedCheck(OMRErrInfo[11].Mode,FMode) then //是否要檢核
@@ -11829,10 +12157,15 @@
                     RelaFormName := FormCode2FormName(CaseID,RelaFormCode);
                     Anchor1 := FormID2Anchor(RelaFormCode);
                     RelaXT.LoadFromFile(CheckXmlPath+RelaFormCode+'.xml');
-                    ISB8.LoadFromFile(ImageSavePath+CaseID+'\upload\'+RelaFile,1); //20170815
-                    ISB8W:= ISB8.Graphic.Width;
-                    ISB8H:= ISB8.Graphic.Height;
-                    FindPoint(ISB8.Graphic,UpLPoint,UpRPoint,DownLPoint,ISB8W,ISB8H,ANCHOR);  //20170815  抓相關然欄位所在圖檔的定位
+
+                    if RelaFile<>'' then
+                    begin
+                      ISB8.LoadFromFile(ImageSavePath+CaseID+'\upload\'+RelaFile,1); //20170815
+                      ISB8W:= ISB8.Graphic.Width;
+                      ISB8H:= ISB8.Graphic.Height;
+                      FindPoint(ISB8.Graphic,UpLPoint,UpRPoint,DownLPoint,ISB8W,ISB8H,ANCHOR);  //20170815  抓相關然欄位所在圖檔的定位
+                    end;
+
                     OMROK := False;
                     //OMROK := False;
                     //if RelaXT.SubNodes['/form/settype2/'].First then
@@ -11890,7 +12223,7 @@
             end;
             //*******有值相關文件的欄位不能有值*******
 
-
+//ShowMessage('11889 OMRFileList.Strings['+IntToStr(i)+']'+OMRFileList.Strings[i]);
 
               //*******有值相依文件*******
             if ModeNeedCheck(OMRErrInfo[5].Mode,FMode) then //是否要檢核
@@ -12053,6 +12386,7 @@
           RelaXT.Free;
           end;
         end;
+        //必填迴圈結束
       Finally
       ValueXT.Free;
       end;
@@ -12130,6 +12464,44 @@
   ini.Free;
   end;
 end;
+
+function TCB_IMGPSScanX.OMRErrini2ListForLog(CaseID: String): String;
+var
+  ini : Tinifile;
+  Errcount : Integer;
+  Del : Boolean;
+  i,j : Integer;
+  ST,CaseList:TStringList;
+
+begin
+  Result:='';
+  //CaseList.dat
+  ST:=TStringList.Create;
+  CaseList:=TStringList.Create;
+  CaseList.LoadFromFile(ImageSavePath+'CaseList.dat');
+  for I := 0 to CaseList.Count - 1 do
+  begin
+    ini := Tinifile.Create(ImageSavePath + CaseList.Strings[i]+'\upload\Checkerr.ini');
+    try
+      Errcount := ini.ReadInteger('OMRCount','Count',0);
+
+      for j := 1 to ErrCount do
+      begin
+        Del := ini.ReadBool(inttostr(j),'Del',False); //是否被移除了
+        if Not Del then
+        begin
+          ST.Add(ini.ReadString(inttostr(j),'Reason','')) ;
+        end;
+      end;
+    finally
+    ini.Free;
+    end;
+  end;
+  Result:=ST.Text;
+  ST.Free;
+  CaseList.Free;
+end;
+
 
 Function TCB_IMGPSScanX.DownLanguage:Boolean;  //下載多國語言檔
 begin
@@ -12809,7 +13181,7 @@
 Procedure TCB_IMGPSScanX.ISBMouseMove(Sender: TObject; Shift: TShiftState;
   X, Y: Integer);
 begin
-  Edit1.SetFocus;
+  //Edit1.SetFocus;
   if Draging then
   begin
     if not (TShape(FindComponent('SP'+Copy(TImageScrollBox(Sender).Name,length(ISBName)+1,length(TImageScrollBox(Sender).Name)-length(ISBName)))) = nil) then
@@ -13717,7 +14089,7 @@
 //ShowMessage(Memo1.Lines.Text);
   IF memo1.Lines.Strings[0] <> '0' Then
   begin
-    HttpErrStr := _Msg('錯誤原因:')+memo1.Lines.Strings[1];
+    HttpErrStr := _Msg('')+memo1.Lines.Strings[1]+'。';
     Result := -1;
     Exit;
   end
@@ -13725,7 +14097,7 @@
   begin
     if (memo1.Lines.Count < 2) then
     begin
-      HttpErrStr := _Msg('錯誤原因:')+'OCX:Index Count error';
+      HttpErrStr := _Msg('')+'OCX:Index Count error';
       Result := -1;
       Exit;
     end
@@ -13739,7 +14111,7 @@
         Result := 1
       Else
       begin
-        HttpErrStr := _Msg('錯誤原因:')+'OCX:Value error';
+        HttpErrStr := _Msg('')+'OCX:Value error';
         Result := -1;
         Exit;
       end;
@@ -13990,7 +14362,8 @@
   PageLVclear := True;
   InitialOk := False;
 
-
+  FMaxUploadSize:='10';
+  FJpgCompression:=50;
   //FMode := 'DSCAN' ;
   //FIs_In_Wh := 'Y';
   if FIs_In_Wh = 'Y' then
@@ -14220,6 +14593,8 @@
 
   ReduceLogFile;
 
+  LogFile1.LogToFile(logTimeString+'OCX取表data結束');
+
   ShowText := _Msg('資料載入中,請稍候');
   DataLoading(True,True);
 
@@ -14258,7 +14633,7 @@
     MkDir(ImageSavePath+FCaseID);
     CreateEmptyCase(ImageSavePath,FCaseID);
     MkDir(ImageSavePath+FCaseID+'\Download');
-    IF (FMode = 'ESCAN') then  //異動件先下載影像
+    IF (FMode = 'ESCAN') or (FMode = 'DSCAN') then  //異動件先下載影像
     begin
       ShowText := _Msg('案件下載中,請稍候');
       DataLoading(True,True);
@@ -14290,7 +14665,8 @@
 //        //LoadImgFile;
 //      end;
       Create_Cust_DocDir(FCaseID); //產生外面傳入的文件
-      LastInitFormidListCreate(ImageSavePath+FCaseID+'\Download\');
+      if FMode='ESCAN' then
+        LastInitFormidListCreate(ImageSavePath+FCaseID+'\Download\');
     end;
   end;
 
@@ -14343,6 +14719,40 @@
 //ShowMessage('ImageSavePath='+ImageSavePath);
   DataLoading(False,False);
 
+  LogFile1.LogToFile(logTimeString+'OCX初始化結束');
+  LogFile1.LogToFile(logTimeString+'FUrl='+FUrl+
+	',FCaseID='+FCaseID+
+	',FMode='+FMode+
+	',FModeName='+FModeName+
+	',FWork_no='+FWork_no+
+	',FUserID='+FUserID+
+	',FUserName='+FUserName+
+	',FUserUnit='+FUserUnit+
+	',FData='+FData+
+	',FVerify='+FVerify+
+	',FReWrite='+FReWrite+
+	',FLanguage='+FLanguage+
+	',FLoanDoc_Value='+FLoanDoc_Value+
+	',FLoanDoc_Enable='+FLoanDoc_Enable+
+	',FUseProxy='+FUseProxy+
+	',FC_DocNoList='+FC_DocNoList+
+	',FC_DocNameList='+FC_DocNameList+
+	',FFixFileList='+FFixFileList+
+	',FIs_In_Wh='+FIs_In_Wh+
+	',FOldCaseInfo='+FOldCaseInfo+
+	',FPrintyn='+FPrintyn+
+	',FIs_OldCase='+FIs_OldCase+
+	',FCustDocYN='+FCustDocYN);
+  LogFile1.LogToFile(logTimeString+'FImgDPI='+IntToStr(FImgDPI)+
+    ',FScanColor='+    IntToStr(FScanColor)+
+    ',FFileSizeLimit='+  IntToStr(FFileSizeLimit)+
+    ',FCaseNoLength='+ IntToStr(FCaseNoLength)+
+    ',FImgDelete='+FImgDelete+
+    ',FIsExternal='+FIsExternal+
+    ',FWH_category='+FWH_category+
+    ',FCheck_main_form='+FCheck_main_form+
+    ',FMaxUploadSize='+FMaxUploadSize);
+
 end;
 
 procedure TCB_IMGPSScanX.Timer2Timer(Sender: TObject);
@@ -14365,21 +14775,23 @@
   AreaStr : String;
   S : TStringlist;
   CheckStr : String;
+  uploadMsg:String;//20171122 新增 配合不同mode下要秀不能上傳訊息
 begin
   IF not InitialOk Then
   begin
     Showmessage(_msg('資訊尚未下載完成,請稍候或重新進入'));
     Exit;
   end;
+LogFile1.LogToFile(logTimeString+'按下上傳');
   ClearView(1);
   CaseHelpBtn.Visible := False;
   DisplayPath := '';
   ClearCaseIndex;
   RejectCase := False;
-
+  uploadMsg:='';
   if not CheckCaseID_OK then  //檢查是否有未配號的案件
   begin
-    Showmessage(_Msg('尚有未配號案件,無法上傳'));
+    Showmessage(_Msg('尚有「無案件編號」之案件,無法上傳'));
     Exit;
   end;
 
@@ -14445,7 +14857,7 @@
     end;
     if CaseTrans = -1 then  //失敗
     begin
-      Showmessage(_Msg('詢問案件可否傳送時,網路發生錯誤!!')+HttpErrStr);
+      Showmessage(_Msg('')+HttpErrStr+'.');
       DataLoading(False,False);
       LoadImgFile;
       Exit;
@@ -14498,13 +14910,32 @@
     end;
   end;
   DataLoading(False,False);
-  TransMsg := Format(_Msg('成功件%d件'),[SuccessCount]);
+  TransMsg := Format(_Msg('成功件【%d】件'),[SuccessCount]);
   if ReCasecount > 0 then
-    TransMsg := TransMsg + #13#10 + Format(_Msg('無法上傳件%d件'),[ReCasecount]);
+    TransMsg := TransMsg + #13#10 + Format(_Msg('無法上傳件【%d】件'),[ReCasecount]);
   if CheckErrCount > 0 then
-    TransMsg := TransMsg + #13#10 + Format(_Msg('檢核失敗件%d件'),[CheckErrCount]);
+  begin
+    TransMsg := TransMsg + #13#10 + Format(_Msg('檢核失敗件【%d】件,請先點選「案件編號」,再點選「問號」查看錯誤原因'),[CheckErrCount]);
+LogFile1.LogToFile(logTimeString+'檢核失敗原因:'+OMRErrini2ListForLog(''));
+  end;
 
-  Showmessage(_Msg('傳送完成')+#13#10+TransMsg);
+  if (FMode = 'DSCAN') and (SuccessCount = 1)  then
+  begin
+    uploadMsg:=NowCaseNo+_Msg('影像上傳完成。此案已進入下一流程');
+  end;
+  if (FMode='ESCAN') and (FModeName='補件掃描') and (SuccessCount = 1) then
+  begin
+    uploadMsg:=NowCaseNo+_Msg('影像已補件完成');
+  end;
+//ShowMessage('uploadMsg='+uploadMsg);
+  if uploadMsg<>'' then
+  begin
+    ShowMessage(uploadMsg);
+  end
+  else
+  begin
+    Showmessage(_Msg('傳送完成')+#13#10+TransMsg);
+  end;
 
   LoadImgFile;
   if (FMode = 'ESCAN') and (SuccessCount = 1) then
@@ -14969,10 +15400,15 @@
 
 end;
 
+procedure TCB_IMGPSScanX.TreeView1MouseEnter(Sender: TObject);
+begin
+TreeView1.SetFocus;
+end;
+
 procedure TCB_IMGPSScanX.TreeView1MouseMove(Sender: TObject; Shift: TShiftState;
   X, Y: Integer);
 begin
-  TreeView1.SetFocus;
+  //TreeView1.SetFocus;
   {UnRegisterHotKey(Handle, HotKeyId1);
   UnRegisterHotKey(Handle, HotKeyId2); }
 end;

--
Gitblit v1.8.0