unit U_MachineManageJN;

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
  TfrmMachineManageJN = 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;
    v2Column17: TcxGridDBColumn;
    Panel1: TPanel;
    JNCarNo: TEdit;
    RMXLSExport2: TRMXLSExport;
    v2Column7: TcxGridDBColumn;
    Label1: TLabel;
    v2Column12: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    ToolButton3: TToolButton;
    ADOQueryPrt: TADOQuery;
    ToolButton4: TToolButton;
    ToolButton5: TToolButton;
    Label2: TLabel;
    JNCheJIan: TEdit;
    Panel2: TPanel;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    Button1: TButton;
    Tv2Column1: TcxGridDBColumn;
    Button2: TButton;
    Label3: TLabel;
    CarNo: TEdit;
    DataSource1: TDataSource;
    ClientDataSet1: TClientDataSet;
    ToolButton6: TToolButton;
    Panel3: TPanel;
    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 v2Column8PropertiesEditValueChanged(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
    procedure v2Column1PropertiesEditValueChanged(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Tv1DblClick(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
    procedure PrintReport(FZDYNo: string);
    procedure InitGrid();
  public
    fFlag: integer;
    { Public declarations }
    RKFlag, FCYID, fmanage: string;
  end;

var
  frmMachineManageJN: TfrmMachineManageJN;

implementation

uses
  U_DataLink, U_Fun, U_ZDYHelp;

{$R *.dfm}

procedure TfrmMachineManageJN.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  Machine where MCNO=''' + 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 TfrmMachineManageJN.InitGrid();
begin
  JNCarNo.SetFocus;
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      SQL.Clear;
      sql.Add(' select A.* from  JN_Car A where Valid=''Y'' order by JNChejian desc,JNCarNo ');
      Open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_HZ);
    SInitCDSData20(ADOQueryMain, CDS_HZ);
  finally
    ADOQueryMain.EnableControls;
    ToolButton2.Click;
  end;
  with ADOQueryTemp do
  begin
    close;
    sql.Clear;
    sql.Add('select *from JN_Car_BD where 1=2');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp, ClientDataSet1);
  SInitCDSData20(ADOQueryTemp, ClientDataSet1);
end;

procedure TfrmMachineManageJN.FormDestroy(Sender: TObject);
begin
  frmMachineManageJN := nil;
end;

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

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

procedure TfrmMachineManageJN.FormShow(Sender: TObject);
var
  fsj:string;
begin
  fsj := ' select Name=ZdyName,Code=''''  from KH_ZDY where Type=''JNCheJian'' ';
  SInitCxGridComboBoxBySql(ADOQueryCmd,v2Column1,fsj,0,True,'');
  ReadCxGrid(Trim(Self.Caption), Tv2, '机台管理');
  InitGrid();
end;

procedure TfrmMachineManageJN.TBRafreshClick(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmMachineManageJN.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 TfrmMachineManageJN.ToolButton1Click(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    Exit;
  SelExportData(Tv2, ADOQueryMain, trim(Self.Caption));
end;

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

procedure TfrmMachineManageJN.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 TfrmMachineManageJN.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 TfrmMachineManageJN.CustomerChange(Sender: TObject);
begin
  ToolButton2.Click;
end;

procedure TfrmMachineManageJN.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 JN_Car ');
      sql.Add(' Set ' + FFieldName + '=''' + Trim(mvalue) + '''');
      sql.Add('where JCId='''+Trim(CDS_HZ.fieldbyname('JCID').AsString)+'''');
      ExecSQL;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    tv2.Controller.EditingController.ShowEdit();
  except
    tv2.Controller.EditingController.ShowEdit();
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！', '提示', 0);
  end
end;

procedure TfrmMachineManageJN.ToolButton3Click(Sender: TObject);
begin
  PrintReport(Trim(CDS_HZ.fieldbyname('MCNO').AsString));
end;

procedure TfrmMachineManageJN.ToolButton4Click(Sender: TObject);
var
  maxId: string;
begin
  JNCheJIan.SetFocus;
  if GetLSNo(ADOQueryCmd, maxId, 'JN', 'JN_Car', 4, 1) = False then
  begin
    Application.MessageBox('取最大号失败！', '提示', 0);
    Exit;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('insert into JN_Car(JCId,Filler,Valid) values(' + quotedstr(Trim(maxId)) + ',' + quotedstr(Trim(dname)) + ',''Y'' )');
    ExecSQL;
  end;
  InitGrid();
end;

procedure TfrmMachineManageJN.ToolButton5Click(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then
    Exit;
  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update JN_Car Set Valid=''N'',delperson='''+Trim(DName)+''',deltime=getdate()');
    sql.Add('where JCId='''+Trim(CDS_HZ.fieldbyname('JCID').AsString)+'''');
    ExecSQL;
  end;
  CDS_HZ.Delete;
end;

procedure TfrmMachineManageJN.ToolButton6Click(Sender: TObject);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='JNCheJian';
      flagname:='加捻车间';
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmMachineManageJN.v2Column1PropertiesEditValueChanged(
  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 JN_Car ');
      sql.Add(' Set ' + FFieldName + '=''' + Trim(mvalue) + '''');
      sql.Add('where JCId='''+Trim(CDS_HZ.fieldbyname('JCID').AsString)+'''');
      ExecSQL;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    tv2.Controller.EditingController.ShowEdit();
  except
    tv2.Controller.EditingController.ShowEdit();
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！', '提示', 0);
  end
end;

procedure TfrmMachineManageJN.Button1Click(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then
    exit;
  
  if CDS_HZ.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  CDS_HZ.DisableControls;
  with CDS_HZ do
  begin
    first;
    while Locate('SSel',True,[]) do
    begin
      with ClientDataSet1 do
      begin
        Append;
        ClientDataSet1.FieldByName('JNCarNo').Value:=CDS_HZ.FieldByName('JNCarNo').Value;
        ClientDataSet1.FieldByName('CarNo').Value:=CDS_HZ.FieldByName('CarNo').Value;
        Post;
      end;
      CDS_HZ.Delete;
    end;
  end;
  CDS_HZ.EnableControls;
end;

procedure TfrmMachineManageJN.Tv1DblClick(Sender: TObject);
begin
  if ClientDataSet1.IsEmpty then Exit;
  if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
  if Trim(ClientDataSet1.fieldbyname('BDID').AsString)<>'' then
  begin
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete JN_Car_BD where JCId=' + inttostr(CDS_HZ.FieldByName('JCId').AsInteger));
      ExecSQL;
    end;
    ClientDataSet1.Delete;
  end;
end;

procedure TfrmMachineManageJN.Button2Click(Sender: TObject);
var
  FBDID, maxNo: string;
begin
  if ClientDataSet1.IsEmpty then
    Exit;
  JNCarNo.SetFocus;
  if Application.MessageBox('确定要批量绑定圆机数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  Panel3.Caption:='正在保存数据，请稍后。。。';
  Panel3.Visible:=True;
  Panel3.Refresh;
  try
    ADOQueryCmd.Connection.BeginTrans;
    ClientDataSet1.DisableControls;
    with ClientDataSet1 do
    begin
      First;
      while not Eof do
      begin
        FBDID:=Trim(ClientDataSet1.fieldbyname('BDID').AsString);
        if Trim(FBDID) = '' then
          begin
            if GetLSNo(ADOQueryCmd, maxNo, 'BD', 'JN_Car_BD', 4, 1) = False then
            begin
              ADOQueryCmd.Connection.RollbackTrans;
              Application.MessageBox('圆机绑定流水号异常！', '提示', 0);
              exit;
            end;
          end
        else
        begin
          maxNo := Trim(FBDID);
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from JN_Car_BD where BDID=''' + Trim(FBDID) + '''');
          Open;
        end;

        with ADOQueryCmd do
        begin
          if Trim(FBDID)='' then
          begin
            Append;
            FieldByName('BDID').Value := Trim(maxNo);
            FieldByName('Filler').Value := Trim(DName);
            FieldByName('FillTime').Value := SGetServerDateTime(ADOQueryTemp);
            FieldByName('Valid').Value := 'Y';
          end else
          begin
            Edit;
            FieldByName('Editer').Value := Trim(DName);
            FieldByName('EditTime').Value := SGetServerDateTime(ADOQueryTemp);
          end;
          RTSetSaveDataCDS(ADOQueryCmd, Tv1, ClientDataSet1, 'JN_Car_BD', 7);
          Post;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update JN_Car Set BDTime=getdate(),CarNo='''+Trim(ClientDataSet1.fieldbyname('CarNo').AsString)+'''');
          sql.Add(' where JNCarNo=''' + Trim(ClientDataSet1.fieldbyname('JNCarNo').AsString) + ''' and valid=''Y'' ');
          ExecSQL;
        end;
        with ClientDataSet1 do
        begin
          edit;
          FieldByName('BDID').Value:=Trim(maxNo);
          Post;
        end;
        Next;
      end;
    end;
    ClientDataSet1.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
    Panel3.Visible:=False;
    Application.MessageBox('保存成功!', '提示', 0);

  except
    ClientDataSet1.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败!', '提示', 0);
  end;
end;

end.

