unit U_JTBY;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxGraphics, cxCustomData, cxStyles, cxTL, cxMaskEdit, DB, ADODB,
  cxInplaceContainer, cxDBTL, cxControls, cxTLData, ComCtrls, ToolWin, StdCtrls,
  cxFilter, cxData, cxDataStorage, cxEdit, cxDBData, DBClient,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, ExtCtrls,
  cxSplitter, cxGridLevel, cxClasses, cxGridCustomView, cxGrid,
  cxGridCustomPopupMenu, cxGridPopupMenu, RM_Dataset, RM_System, RM_Common,
  RM_Class, RM_GridReport, IdBaseComponent, IdComponent, IdTCPConnection,
  IdTCPClient, IdFTP, ShellAPI, IniFiles, cxCheckBox, cxCalendar, cxButtonEdit,
  cxTextEdit, cxPC, cxCheckComboBox, cxDropDownEdit, Menus, RM_e_Xls, TeEngine,
  Series, TeeProcs, Chart, DbChart, cxLookAndFeels, cxLookAndFeelPainters,
  cxNavigator;

type
  TfrmJTBY = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    ToolButton2: TToolButton;
    ADOQueryMain: TADOQuery;
    ToolButton1: TToolButton;
    RM1: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxGridLevel1: TcxGridLevel;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_HZ: TDataSource;
    CDS_HZ: TClientDataSet;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    Panel1: TPanel;
    MachineNo: TEdit;
    RMXLSExport2: TRMXLSExport;
    v2Column8: TcxGridDBColumn;
    Label1: TLabel;
    v2Column12: TcxGridDBColumn;
    ToolButton3: TToolButton;
    ADOQueryPrt: TADOQuery;
    ToolButton4: TToolButton;
    ToolButton5: TToolButton;
    v2Column1: TcxGridDBColumn;
    Tv2Column1: TcxGridDBColumn;
    Tv2Column2: TcxGridDBColumn;
    CDS_DB: TClientDataSet;
    DS_DB: TDataSource;
    ADOQuerySub: TADOQuery;
    ToolButton6: TToolButton;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    cxGridPopupMenu1: TcxGridPopupMenu;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    IsJYTime: TCheckBox;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure CustomerChange(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure v2Column8PropertiesEditValueChanged(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure Tv2MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure cxGridDBColumn2PropertiesEditValueChanged(Sender: TObject);
    procedure cxGridDBColumn4PropertiesEditValueChanged(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
  private
    { Private declarations }
    procedure PrintReport(FZDYNo: string);
    procedure InitGrid();
    procedure InitGridDB();
  public
    fFlag: integer;
    { Public declarations }
    RKFlag, FCYID, fmanage: string;
  end;

var
  frmJTBY: TfrmJTBY;

implementation

uses
  U_DataLink, U_Fun, U_ZDYHelp;

{$R *.dfm}

procedure TfrmJTBY.PrintReport(FZDYNo: string);
var
  fPrintFile, FFCYID: string;
  i, j: Integer;
  Txt, fImagePath: string;
  Moudle: THandle;
  Makebar: TMakebar;
  Mixtext: TMixtext;
begin
  if CDS_HZ.IsEmpty then
    Exit;

  ExportFtErpFile('托盘标签.rmf', ADOQueryTemp);
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\托盘标签.rmf';
  with ADOQueryPrt do
  begin
    close;
    sql.Clear;
    sql.Add(' select * from  BS_Tray where TYId=''' + Trim(FZDYNo) + '''');
    open;
  end;
  try
    Moudle := LoadLibrary('MakeQRBarcode.dll');
    @Makebar := GetProcAddress(Moudle, 'Make');
    @Mixtext := GetProcAddress(Moudle, 'MixText');
    Txt := Trim(FZDYNo);
    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
    CDS_HZ.EnableControls;
    application.MessageBox('条形码生成失败！', '提示信息', MB_ICONERROR);
    exit;
  end;
  RMVariables['QRBARCODE'] := fImagePath;
  if FileExists(fPrintFile) then
  begin
    RM1.LoadFromFile(fPrintFile);
    RM1.PrintReport;
  //  RM1.ShowReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找' + fPrintFile + '!'), '提示', 0);
    Exit;
  end;
end;

procedure TfrmJTBY.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      SQL.Clear;
      sql.Add(' select A.* from  MachineBy_Dqtx A ');
      if IsJYTime.Checked then
      begin
        sql.add(' where NextByDate>=' + QuotedStr(Trim(FormatDateTime('yyyy-MM-dd', BegDate.DateTime))));
        sql.Add(' and NextByDate<=' + QuotedStr(Trim(FormatDateTime('yyyy-MM-dd', enddate.DateTime + 1))));
      end;
      sql.Add('order by MachineNo ');
      Open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_HZ);
    SInitCDSData20(ADOQueryMain, CDS_HZ);
  finally
    ADOQueryMain.EnableControls;
    ToolButton2.Click;
  end;
end;

procedure TfrmJTBY.InitGridDB();
begin
  try
    ADOQuerySub.DisableControls;
    with ADOQuerySub do
    begin
      Close;
      SQL.Clear;
      sql.Add(' select A.*,NextDayS=DateDiff(day, GetDate(),NextTime) ');
      sql.Add(' from  DB_Thing A ');
      if IsJYTime.Checked then
      begin
        sql.add(' where NextTime>=' + QuotedStr(Trim(FormatDateTime('yyyy-MM-dd', BegDate.DateTime))));
        sql.Add(' and NextTime<=' + QuotedStr(Trim(FormatDateTime('yyyy-MM-dd', enddate.DateTime + 1))));
      end;
      sql.Add('ORDER BY isnull(DateDiff(day, GetDate(),NextTime),9999)  ');
//      ShowMessage(SQL.text);
      Open;
    end;
    SCreateCDS20(ADOQuerySub, CDS_DB);
    SInitCDSData20(ADOQuerySub, CDS_DB);
  finally
    ADOQuerySub.EnableControls;
    ToolButton2.Click;
  end;
end;

procedure TfrmJTBY.FormDestroy(Sender: TObject);
begin
  frmJTBY := nil;
end;

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

procedure TfrmJTBY.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid(Trim(Self.Caption), Tv2, '机台管理');
  WriteCxGrid(Trim(Self.Caption) + '1', Tv1, '机台管理');
  Close;
end;

procedure TfrmJTBY.FormShow(Sender: TObject);
begin
  ReadCxGrid(Trim(Self.Caption), Tv2, '机台管理');
  ReadCxGrid(Trim(Self.Caption) + '1', Tv1, '机台管理');
  Enddate.DateTime := SGetServerDate(ADOQueryTemp) + 7;
  begdate.DateTime := Enddate.DateTime - 7;
  InitGrid();
//  InitGridDB();
end;

procedure TfrmJTBY.TBRafreshClick(Sender: TObject);
begin
  InitGrid();
//  InitGridDB();
end;

procedure TfrmJTBY.ToolButton2Click(Sender: TObject);
var
  sql: string;
begin
  if ADOQueryMain.Active then
  begin
    sql := SGetFilters(Panel1, 1, 2);
    SDofilter(ADOQueryMain, sql);
    SCreateCDS20(ADOQueryMain, CDS_HZ);
    SInitCDSData20(ADOQueryMain, CDS_HZ);
  end;
end;

procedure TfrmJTBY.ToolButton1Click(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    Exit;
  SelExportData(Tv2, ADOQueryMain, trim(Self.Caption));
end;

procedure TfrmJTBY.cxTabControl1Change(Sender: TObject);
begin
  InitGrid;
end;

procedure TfrmJTBY.N1Click(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then
    exit;
  with CDS_HZ do
  begin
    DisableControls;
    first;
    while not eof do
    begin
      edit;
      fieldbyname('ssel').Value := true;
      post;
      next;
    end;
    First;
    EnableControls;
  end;
end;

procedure TfrmJTBY.N2Click(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then
    exit;
  with CDS_HZ do
  begin
    DisableControls;
    first;
    while not eof do
    begin
      edit;
      fieldbyname('ssel').Value := false;
      post;
      next;
    end;
    First;
    EnableControls;
  end;
end;

procedure TfrmJTBY.CustomerChange(Sender: TObject);
begin
  ToolButton2.Click;
end;

procedure TfrmJTBY.FormCreate(Sender: TObject);
begin
  fmanage := Trim(DParameters1);
end;

procedure TfrmJTBY.v2Column8PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
begin
  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(Tv2.Controller.FocusedColumn.DataBinding.FilterFieldName);
  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_HZ do
    begin
      Edit;
      FieldByName(FFieldName).Value := Trim(mvalue);
      Post;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('UPdate BS_Tray ');
      sql.Add(' Set ' + FFieldName + '=''' + Trim(mvalue) + '''');
      sql.Add(' , Editer=''' + Trim(DName) + '''');
      sql.Add(' , Edittime=getdate()');
      sql.Add(' where TYId=' + quotedstr(CDS_HZ.fieldbyname('TYId').AsString));
      ExecSQL;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    tv2.Controller.EditingController.ShowEdit();
  except
    tv2.Controller.EditingController.ShowEdit();
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！', '提示', 0);
  end
end;

procedure TfrmJTBY.ToolButton3Click(Sender: TObject);
begin
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('update BS_Tray set EditFlag=0 where TYID=' + QuotedStr(CDS_HZ.FieldByName('TYID').AsString));
    ExecSQL;
  end;
  PrintReport(Trim(CDS_HZ.fieldbyname('TYId').AsString));
end;

procedure TfrmJTBY.ToolButton4Click(Sender: TObject);
var
  maxId: string;
begin
//  KWNo.SetFocus;
  if GetLSNo(ADOQueryCmd, maxId, 'DB', 'DB_Thing', 4, 1) = False then
  begin
    Application.MessageBox('取最大号失败！', '提示', 0);
    Exit;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('insert into DB_Thing(DBID) values(' + quotedstr(Trim(maxId)) + ')');
//    ShowMessage(sql.text);
    ExecSQL;
  end;
//  InitGridDB();
end;

procedure TfrmJTBY.ToolButton5Click(Sender: TObject);
begin
  if CDS_DB.IsEmpty then
    Exit;
  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('delete DB_Thing where DBID=' + QuotedStr(CDS_DB.FieldByName('DBID').AsString));
//    ShowMessage(sql.text);
    ExecSQL;
  end;
  CDS_DB.Delete;
end;

procedure TfrmJTBY.Tv2MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
  i: Integer;
begin
  if not CDS_HZ.fieldbyname('EditFlag').AsBoolean then
  begin
    for i := 0 to tv2.ColumnCount - 1 do
    begin
      if uppercase(tv2.Columns[i].DataBinding.FieldName) <> 'SSEL' then
        tv2.Columns[i].Options.Editing := false;
    end;
  end
  else
  begin
    for i := 0 to tv2.ColumnCount - 1 do
    begin
      if uppercase(tv2.Columns[i].DataBinding.FieldName) <> 'SSEL' then
        tv2.Columns[i].Options.Editing := True;
    end;
  end;
end;

procedure TfrmJTBY.cxGridDBColumn2PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
begin
  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_DB do
    begin
      Edit;
      FieldByName(FFieldName).Value := Trim(mvalue);
      Post;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('UPdate DB_Thing ');
      sql.Add(' Set ' + FFieldName + '=''' + Trim(mvalue) + '''');
      sql.Add(' where DBId=' + quotedstr(CDS_DB.fieldbyname('DBId').AsString));
//      ShowMessage(sql.text);
      ExecSQL;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    tv1.Controller.EditingController.ShowEdit();
  except
    tv1.Controller.EditingController.ShowEdit();
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！', '提示', 0);
  end
end;

procedure TfrmJTBY.cxGridDBColumn4PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName, Lastmvalue: string;
begin
  Lastmvalue := Trim(CDS_DB.fieldbyname('DBZQ').AsString);
  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_DB do
    begin
      Edit;
      FieldByName(FFieldName).Value := Trim(mvalue);
      Post;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('UPdate DB_Thing ');
      sql.Add(' Set ' + FFieldName + '=' + Trim(mvalue) + '');
      if CDS_DB.fieldbyname('LastTime').AsString <> '' then
      begin
        sql.Add(' , NextTime=NextTime+' + CDS_DB.fieldbyname('DBZQ').AsString + '-' + Trim(Lastmvalue));
      end;
      sql.Add(' where DBId=' + quotedstr(CDS_DB.fieldbyname('DBId').AsString));
//      ShowMessage(sql.text);
      ExecSQL;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    tv1.Controller.EditingController.ShowEdit();
  except
    tv1.Controller.EditingController.ShowEdit();
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！', '提示', 0);
  end;
//  InitGridDB();
end;

procedure TfrmJTBY.ToolButton6Click(Sender: TObject);
begin
  if CDS_DB.IsEmpty then
    Exit;
  if Application.MessageBox('确定要完成吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('update DB_Thing set LastTime = CONVERT(varchar(100), GETDATE(), 23)  ');
    sql.Add(' , NextTime=CONVERT(varchar(100), GETDATE()+' + CDS_DB.fieldbyname('DBZQ').AsString);
    sql.Add(' , 23) where DBID=' + QuotedStr(CDS_DB.FieldByName('DBID').AsString));
//    ShowMessage(sql.text);
    ExecSQL;
  end;
//  InitGridDB();
end;

end.

