unit U_FinishedLSInspection;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, ComCtrls, ToolWin, cxStyles, cxCustomData,
  cxGraphics, cxFilter, cxData, cxDataStorage, cxEdit, DB, cxDBData, ADODB,
  cxGridCustomPopupMenu, cxGridPopupMenu, cxGridLevel, cxClasses, cxControls,
  cxGridCustomView, cxGridCustomTableView, cxGridTableView, cxGridDBTableView,
  cxGrid, DBClient, cxCheckBox, cxCalendar, cxSplitter, RM_Dataset, RM_System,
  RM_Common, RM_Class, RM_GridReport, RM_e_Xls, Menus, MovePanel, cxTextEdit,
  cxContainer, cxCurrencyEdit, BtnEdit, RM_e_main, RM_e_htm, RM_e_Graphic,
  RM_e_Jpeg, RM_e_txt, RM_e_csv, RM_E_llPDF, Math, Clipbrd, cxPC,
  cxLookAndFeels, cxLookAndFeelPainters, cxNavigator, dxBarBuiltInMenu;

type
  TfrmFinishedLSInspection = class(TForm)
    ToolBar1: TToolBar;
    TBClose: TToolButton;
    Panel1: TPanel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DS_Main: TDataSource;
    TBExport: TToolButton;
    Label10: TLabel;
    JYID: TEdit;
    Label5: TLabel;
    Y_CodeName: TEdit;
    TBManage: TToolButton;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    TBZF: TToolButton;
    ToolButton1: TToolButton;
    RM2: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    ADOQueryPrint: TADOQuery;
    MovePanel2: TMovePanel;
    RMXLSExport1: TRMXLSExport;
    Panel3: TPanel;
    Panel4: TPanel;
    Panel6: TPanel;
    Label31: TLabel;
    LBCPAP: TLabel;
    BTLP: TButton;
    BTNP: TButton;
    TCBNOR: TComboBox;
    ComboBox1: TComboBox;
    Order_Main: TClientDataSet;
    TBRafresh: TToolButton;
    RM1: TRMGridReport;
    cxgrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Sel: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    cxgrdMJLen: TcxGridDBColumn;
    cxgrid1Level1: TcxGridLevel;
    Label1: TLabel;
    Label2: TLabel;
    Y_Spec: TEdit;
    v1Column7: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    Label3: TLabel;
    FactoryName: TEdit;
    Label4: TLabel;
    BatchNo: TEdit;
    v1Column11: TcxGridDBColumn;
    Label6: TLabel;
    Edit1: TEdit;
    Label7: TLabel;
    Y_CodeNameCP: TEdit;
    v1Column13: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    cxTabControl1: TcxTabControl;
    ToolButton2: TToolButton;
    Tv1Column2: TcxGridDBColumn;
    ToolButton3: TToolButton;
    Tv1Column3: TcxGridDBColumn;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Tv1MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure TBManageClick(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure TBZFClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure JYIDKeyPress(Sender: TObject; var Key: Char);
    procedure BTLPClick(Sender: TObject);
    procedure BTNPClick(Sender: TObject);
    procedure TCBNORChange(Sender: TObject);
    procedure Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure OrderNoKeyPress(Sender: TObject; var Key: Char);
    procedure MJStr2Change(Sender: TObject);
    procedure JYTypeChange(Sender: TObject);
    procedure KHConNOKeyPress(Sender: TObject; var Key: Char);
    procedure RKOrdIDKeyPress(Sender: TObject; var Key: Char);
    procedure cxTabControl1Change(Sender: TObject);
    procedure personChange(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure v1Column6CustomDrawCell(Sender: TcxCustomGridTableView;
      ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo;
      var ADone: Boolean);
    procedure ToolButton3Click(Sender: TObject);
  private
    DQdate: TDateTime;
    FLeft, FTop: Integer;
    CurrentPage, RecordsNumber: Integer;
    procedure InitGrid();
    procedure InitForm();
    procedure SetStatus();
    { Private declarations }
  public
    FFInt: Integer;
    canshu1: string;
    { Public declarations }
  end;

implementation

uses
  U_DataLink, U_Fun, U_LSEdit, U_ZDYHelp, U_SysLogHelp;

{$R *.dfm}
procedure TfrmFinishedLSInspection.SetStatus();
begin
  TBManage.Visible := False;
  TBZF.Visible := False;
  ToolButton1.Visible := False;
  if Trim(canshu1) = '管理' then
  begin
    TBManage.Visible := True;
    ToolButton1.Visible := true;
    TBZF.Visible := true;
  end;
end;

procedure TfrmFinishedLSInspection.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := caFree;
end;

procedure TfrmFinishedLSInspection.FormCreate(Sender: TObject);
begin
  cxgrid1.Align := alClient;
  DQdate := SGetServerDate(ADOQueryTemp);
end;

procedure TfrmFinishedLSInspection.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid(self.Caption+'JY', Tv1, '加弹丝检验管理');
  Close;
end;



procedure TfrmFinishedLSInspection.InitGrid();
var
  fwhere, Pwhere: string;
begin
  MovePanel2.Left := (self.Width - MovePanel2.Width) div 2;
  MovePanel2.Visible := True;
  MovePanel2.Refresh;

  fwhere := ' where  1=1 ';
  Pwhere := SGetFilters(Panel1, 1, 2);
  if trim(Pwhere) <> '' then
    fwhere := fwhere + ' and ' + Pwhere;
  if cxTabControl1.TabIndex=0 then
  begin
    fwhere:=fwhere+' and ((JYNW>=20 and JYNW<=40 and ZGKS=6) or ChuLiFlag=1) ';
  end else
  if cxTabControl1.TabIndex=1 then
  begin
    fwhere:=fwhere+' and ((JYNW<20 or JYNW>40 or ZGKS<>6 ) and ChuLiFlag=0) ';
  end;
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.Add('exec P_YTCK_Get_JY ');
      sql.Add('@pageIndex=' + inttostr(CurrentPage));
      sql.Add(',@pageSize=' + inttostr(RecordsNumber));
      sql.Add(',@criteria= ' + quotedstr(fwhere));

      Open;
    end;
    SCreateCDS20(ADOQueryMain, Order_Main);
    SInitCDSData20(ADOQueryMain, Order_Main);
    LBCPAP.Caption := inttostr(CurrentPage) + '/' + inttostr(ceil(Order_Main.FieldByName('TotalCount').AsInteger / RecordsNumber));
    MovePanel2.Visible := false;
  finally
    ADOQueryMain.EnableControls;
    MovePanel2.Visible := false;
  end;
end;

procedure TfrmFinishedLSInspection.InitForm();
var
  fsj, fsj1: string;
begin
  ReadCxGrid(self.Caption+'JY', Tv1, '加弹丝检验管理');
  BegDate.DateTime := SGetServerDate10(ADOQueryTemp) - 10;
  EndDate.DateTime := SGetServerDate10(ADOQueryTemp);
end;

procedure TfrmFinishedLSInspection.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    Exit;
  SelExportData(Tv1, ADOQueryMain, '加弹丝称重报表');
end;

procedure TfrmFinishedLSInspection.TBRafreshClick(Sender: TObject);
begin
  Y_CodeName.SetFocus;
  CurrentPage := 1;
  InitGrid();

end;

procedure TfrmFinishedLSInspection.FormShow(Sender: TObject);
begin
  CurrentPage := 1;
  RecordsNumber := 500;
  InitForm();
  SetStatus();
end;

procedure TfrmFinishedLSInspection.Tv1MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  FLeft := X;
  FTop := Y;
end;

procedure TfrmFinishedLSInspection.TBManageClick(Sender: TObject);
var
  key: Char;
begin
  key := #13;
  if Order_Main.IsEmpty then
    exit;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from CK_YT_CR where isnull(CRFlag,'''')=''出库'' and JYID=''' + Trim(Order_Main.fieldbyname('JYID').AsString) + '''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty = False then
  begin
    Order_Main.EnableControls;
    Application.MessageBox('已出库不能编辑!', '提示', 0);
    Exit;
  end;
  try
    frmLSEdit := TfrmLSEdit.Create(Application);
    with frmLSEdit do
    begin
      JYID.Text := Trim(Self.Order_Main.fieldbyname('JYID').AsString);
      JYIDKeyPress(JYID, key);
      if ShowModal = 1 then
      begin
        TBRafresh.Click;
      end;
    end;
  finally
    frmLSEdit.Free;
  end;
end;

procedure TfrmFinishedLSInspection.N1Click(Sender: TObject);
begin
  SelOKNo(Order_Main, True);
end;

procedure TfrmFinishedLSInspection.N2Click(Sender: TObject);
begin
  SelOKNo(Order_Main, False);
end;

procedure TfrmFinishedLSInspection.TBZFClick(Sender: TObject);
begin
  if Order_Main.Locate('ssel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  if Application.MessageBox('确定要作废数据吗？作废后数据不能恢复。', '提示', 32 + 4) <> IDYES then
    Exit;
  Order_Main.DisableControls;
  with Order_Main do
  begin
    First;
    while not Eof do
    begin
      if Order_Main.FieldByName('SSel').AsBoolean = True then
      begin
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from CK_YT_CR where JYID=''' + Trim(Order_Main.fieldbyname('JYID').AsString) + ''' and CRFlag=''出库''');
          Open;
        end;
        if ADOQueryTemp.IsEmpty = False then
        begin
          Order_Main.EnableControls;
          Application.MessageBox('已产生出库数据不能删除!', '提示', 0);
          Exit;
        end
        else
        begin
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('delete JY_YT where JYID=''' + Trim(Order_Main.fieldbyname('JYID').AsString) + '''');
            sql.Add('delete CK_YT_CR where JYID=''' + Trim(Order_Main.fieldbyname('JYID').AsString) + '''');
            sql.Add('delete CK_YT_KC where JYID=''' + Trim(Order_Main.fieldbyname('JYID').AsString) + '''');

            sql.Add('insert into SY_SysLog(operor,opertime,Model,acction,opevent,result) values( ');
            sql.Add(' ' + quotedstr(trim(DName)));
            sql.Add(',getdate() ');
            sql.Add(',' + quotedstr(trim(self.Caption)));
            sql.Add(',' + quotedstr(trim('作废加弹丝检验数据')));
            sql.Add(',' + quotedstr('加弹条码：' + trim(Order_Main.fieldbyname('JYID').AsString)));
            sql.Add(',' + quotedstr(trim('成功')));
            sql.Add(')');
            ExecSQL;
          end;
        end;
        Order_Main.Delete;
      end
      else
        Next;
    end;
  end;
  Order_Main.EnableControls;

end;

procedure TfrmFinishedLSInspection.ToolButton1Click(Sender: TObject);
var
  fPrintFile: string;
  Txt, fImagePath: string;
  Moudle: THandle;
  Makebar: TMakebar;
  Mixtext: TMixtext;
begin
  with ADOQueryPrint do
  begin
    Close;
    SQL.Clear;
    sql.Add(' EXEC P_JY_Print_YTLab ');
    SQL.Add(' @JYID=''' + Trim(Order_Main.fieldbyname('JYID').AsString) + '''');
    Open;
  end;
  if ADOQueryPrint.IsEmpty then
  begin
    application.MessageBox('标签内容未找到！', '提示信息', MB_ICONERROR);
    exit;
  end;
  try
    Moudle := LoadLibrary('MakeQRBarcode.dll');
    @Makebar := GetProcAddress(Moudle, 'Make');
    @Mixtext := GetProcAddress(Moudle, 'MixText');
    Txt := Trim(ADOQueryPrint.fieldbyname('JYID').AsString);
    fImagePath := ExtractFilePath(Application.ExeName) + 'image\temp.bmp';
    if not DirectoryExists(pchar(ExtractFilePath(Application.ExeName) + 'image')) then
      CreateDirectory(pchar(ExtractFilePath(Application.ExeName) + 'image'), nil);
    if FileExists(fImagePath) then
      DeleteFile(fImagePath);
    Makebar(pchar(Txt), Length(Txt), 3, 3, 0, PChar(fImagePath), 3);
  except
    application.MessageBox('条形码生成失败！', '提示信息', MB_ICONERROR);
    exit;
  end;

  ExportFtErpFile(Trim('加弹丝标签.rmf'), ADOQueryTemp);
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\加弹丝标签.rmf';

  if FileExists(fPrintFile) then
  begin
    RMVariables['QRBARCODE'] := fImagePath;
    RM2.LoadFromFile(fPrintFile);
    Rm2.DefaultCopies := strtointdef(trim(ComboBox1.Text), 1);
    RM2.PrintReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找' + fPrintFile), '提示', 0);
  end;
end;



procedure TfrmFinishedLSInspection.JYIDKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    if Length(JYID.Text) < 4 then
      Exit;
    CurrentPage := 1;
    InitGrid();
  end;
end;

procedure TfrmFinishedLSInspection.BTLPClick(Sender: TObject);
begin
  if CurrentPage > 1 then
    CurrentPage := CurrentPage - 1;
  InitGrid();
end;

procedure TfrmFinishedLSInspection.BTNPClick(Sender: TObject);
begin
  if CurrentPage < Order_Main.FieldByName('TotalCount').AsInteger / RecordsNumber then
    CurrentPage := CurrentPage + 1;
  InitGrid();
end;

procedure TfrmFinishedLSInspection.TCBNORChange(Sender: TObject);
begin
  RecordsNumber := StrToInt(TCBNOR.Text);
  CurrentPage := 1;
  InitGrid();
end;

procedure TfrmFinishedLSInspection.Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
  Clipboard.SetTextBuf(PChar(Trim(Order_Main.fieldbyname(TV1.Controller.FocusedColumn.DataBinding.FilterFieldName).AsString)));
end;

procedure TfrmFinishedLSInspection.OrderNoKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    CurrentPage := 1;
    InitGrid();
  end;
end;

procedure TfrmFinishedLSInspection.MJStr2Change(Sender: TObject);
begin
  CurrentPage := 1;
  InitGrid();
end;

procedure TfrmFinishedLSInspection.JYTypeChange(Sender: TObject);
begin
  CurrentPage := 1;
  InitGrid();
end;

procedure TfrmFinishedLSInspection.KHConNOKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    CurrentPage := 1;
    InitGrid();
  end;
end;

procedure TfrmFinishedLSInspection.RKOrdIDKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    CurrentPage := 1;
    InitGrid();
  end;
end;

procedure TfrmFinishedLSInspection.cxTabControl1Change(Sender: TObject);
begin
  CurrentPage := 1;
  InitGrid();
  SetStatus();
end;

procedure TfrmFinishedLSInspection.personChange(Sender: TObject);
begin
  TBRafresh.Click;
end;

procedure TfrmFinishedLSInspection.ToolButton2Click(Sender: TObject);
begin
  if cxTabControl1.TabIndex<>1 then Exit;
  if Order_Main.IsEmpty then Exit;
  if Order_Main.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  if Application.MessageBox('确定要对异常数据，进行处理吗？','提示',32+4)<>IDYES then Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    Order_Main.DisableControls;
    with Order_Main do
    begin
      First;
      while Locate('SSel',True,[]) do
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update JY_YT Set ChuLiFlag=1 ,ChuLiTime=Getdate(),ChuLiPerson='''+Trim(DName)+'''');
          sql.Add(' where JYID='''+Trim(Order_Main.fieldbyname('JYID').AsString)+'''');
          ExecSQL;
        end;
        Delete;
      end;
    end;
    Order_Main.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
  except
    Order_Main.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('处理异常!','提示',0);
    exit;
  end;
end;

procedure TfrmFinishedLSInspection.v1Column6CustomDrawCell(
  Sender: TcxCustomGridTableView; ACanvas: TcxCanvas;
  AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
var
  id,id10,id20:Integer;
begin
  Id:=TV1.GetColumnByFieldName('JYNW').Index;//;-TV1.GroupedItemCount;
  Id10:=TV1.GetColumnByFieldName('ChuLiPerson').Index;//;-TV1.GroupedItemCount;
  Id20:=TV1.GetColumnByFieldName('ZGKS').Index;//;-TV1.GroupedItemCount;
  if Id<0 then Exit;
  if (AViewInfo.GridRecord.Values[id]<20) and (AViewInfo.GridRecord.Values[id10]<>Null) then
  begin
    ACanvas.Brush.Color:=clSkyBlue;
  end else
  if (AViewInfo.GridRecord.Values[id]<20) and (AViewInfo.GridRecord.Values[id10]=Null) then
  begin
    ACanvas.Brush.Color:=clRed;
  end;
  if (AViewInfo.GridRecord.Values[id]>45) and (AViewInfo.GridRecord.Values[id10]<>Null) then
  begin
    ACanvas.Brush.Color:=clSkyBlue;
  end else
  if (AViewInfo.GridRecord.Values[id]>45) and (AViewInfo.GridRecord.Values[id10]=Null) then
  begin
    ACanvas.Brush.Color:=clRed;
  end;
  if (AViewInfo.GridRecord.Values[id20]<>6) and (AViewInfo.GridRecord.Values[id10]<>Null) then
  begin
    ACanvas.Brush.Color:=clSkyBlue;
  end else
  if (AViewInfo.GridRecord.Values[id20]<>6) and (AViewInfo.GridRecord.Values[id10]=Null) then
  begin
    ACanvas.Brush.Color:=clYellow;
  end;
end;

procedure TfrmFinishedLSInspection.ToolButton3Click(Sender: TObject);
begin
  if cxTabControl1.TabIndex<>0 then Exit;
  if Order_Main.IsEmpty then Exit;
  if Order_Main.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  if Application.MessageBox('确定要对异常数据，进行撤销处理吗？','提示',32+4)<>IDYES then Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    Order_Main.DisableControls;
    with Order_Main do
    begin
      First;
      while Locate('SSel',True,[]) do
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update JY_YT Set ChuLiFlag=0 ,ChuLiTime=Null,ChuLiPerson=Null ');
          sql.Add(' where JYID='''+Trim(Order_Main.fieldbyname('JYID').AsString)+'''');
          ExecSQL;
        end;
        Delete;
      end;
    end;
    Order_Main.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
  except
    Order_Main.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('处理异常!','提示',0);
    exit;
  end;
end;

end.

