unit U_JDDATEManage;

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
  TfrmjddateManage = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    RM1: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    cxGrid2: TcxGrid;
    Tv1: TcxGridDBTableView;
    cxGridLevel1: TcxGridLevel;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_HZ: TDataSource;
    CDS_HZ: TClientDataSet;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    RMXLSExport2: TRMXLSExport;
    v2Column12: TcxGridDBColumn;
    ADOQueryPrt: TADOQuery;
    ToolButton4: TToolButton;
    ToolButton5: TToolButton;
    Tv1Column1: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBRafreshClick(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 Tv1Column1PropertiesEditValueChanged(Sender: TObject);
  private
    { Private declarations }
    procedure PrintReport(FZDYNo: string);
    procedure InitGrid();
  public
    fFlag: integer;
    { Public declarations }
    RKFlag, FCYID, fmanage: string;
  end;

var
  frmjddateManage: TfrmjddateManage;

implementation

uses
  U_DataLink, U_Fun, U_ZDYHelp;

{$R *.dfm}

procedure TfrmjddateManage.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;

  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 TfrmjddateManage.InitGrid();
begin
  try
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('update t1 set t1.ZdyFlt1 = t.rn from KH_ZDY t1');
      sql.Add('join (select *,rn = ROW_NUMBER() OVER(ORDER BY ZdyFlt1) from KH_ZDY where TYPE=''shfs'')t on t1.zdyno = t.zdyno');
      sql.Add('WHERE t1.TYPE=''shfs'' ');
      execsql;
    end;
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      SQL.Clear;
      sql.Add('select A.*,ZJM=dbo.getPinYin(A.ZdyName) from KH_ZDY A where A.Type=''SHFS''');
      SQL.Add('ORDER BY ZdyFlt1');
      Open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_HZ);
    SInitCDSData20(ADOQueryMain, CDS_HZ);
  finally
    ADOQueryMain.EnableControls;

  end;
end;

procedure TfrmjddateManage.FormDestroy(Sender: TObject);
begin
  frmjddateManage := nil;
end;

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

procedure TfrmjddateManage.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid(Trim(Self.Caption), Tv1, '交单日期管理');
  Close;
end;

procedure TfrmjddateManage.FormShow(Sender: TObject);
begin
  ReadCxGrid(Trim(Self.Caption), Tv1, '交单日期管理');
//  Enddate.DateTime:=SGetServerDate(ADOQueryTemp);
//  begdate.DateTime:=Enddate.DateTime-30;
  InitGrid();
end;

procedure TfrmjddateManage.TBRafreshClick(Sender: TObject);
begin

  InitGrid();
end;

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

procedure TfrmjddateManage.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 TfrmjddateManage.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 TfrmjddateManage.FormCreate(Sender: TObject);
begin
  fmanage := Trim(DParameters1);
end;

procedure TfrmjddateManage.v2Column8PropertiesEditValueChanged(Sender: TObject);
var
  maxno, mvalue: string;
begin
  mvalue := TcxTextEdit(Sender).EditingText;
  if Trim(mvalue) = '' then
  begin
    //Application.MessageBox('名称不能为空！','提示',0);
    Exit;
  end;
  with CDS_HZ do
  begin
    Edit;
    FieldByName('ZdyName').Value := Trim(mvalue);
    //Post;
  end;
  try
    ADOQueryCmd.Connection.BeginTrans;

    with ADOQueryCmd do
    begin
      //ClientDataSet1.DisableControls;
      //with ClientDataSet1 do
      //begin
        //First;
        //while not eof do
        //begin
      if Trim(CDS_HZ.FieldByName('ZDYNO').AsString) = '' then
      begin
        if GetLSNo(ADOQueryTemp, maxno, 'SY', 'KH_ZDY', 3, 1) = False then
        begin
          ADOQueryCmd.Connection.RollbackTrans;
              //ClientDataSet1.EnableControls;
          Application.MessageBox('取最大编号失败！', '提示', 0);
          Exit;
        end;
      end
      else
      begin
        maxno := Trim(CDS_HZ.fieldbyname('ZDYNo').AsString);
      end;
      with ADOQueryTemp do
      begin
        Close;
        sql.Clear;
        sql.add('select * from KH_Zdy where Type=''SHFS''');
//        if Trim(MainType) <> '' then
//          SQL.Add(' and MainType=''' + Trim(MainType) + '''');
        sql.Add(' and ZdyName=''' + Trim(CDS_HZ.fieldbyname('ZdyName').AsString) + '''');
        Open;
      end;
      if ADOQueryTemp.IsEmpty = False then
      begin
        if ADOQueryTemp.RecordCount > 1 then
        begin
          ADOQueryCmd.Connection.RollbackTrans;

              //ClientDataSet1.EnableControls;
          Application.MessageBox('名称重复！', '提示', 0);
          Exit;
        end;
        if Trim(CDS_HZ.fieldbyname('ZdyNo').AsString) = '' then
        begin
          ADOQueryCmd.Connection.RollbackTrans;
              //ClientDataSet1.EnableControls;
          Application.MessageBox('名称重复！', '提示', 0);
          Exit;
        end
        else
        begin
          if Trim(ADOQueryTemp.fieldbyname('ZdyNo').AsString) <> Trim(CDS_HZ.fieldbyname('ZdyNo').AsString) then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
                //ClientDataSet1.EnableControls;
            Application.MessageBox('名称重复！', '提示', 0);
            Exit;
          end;
        end;
      end;
//      with ADOQueryCmd do
//      begin
//        Close;
//        sql.Clear;
//        SQL.Add('delete KH_ZDY where ZDYNO=''' + Trim(ClientDataSet1.fieldbyname('ZDYNO').AsString) + '''');
//        ExecSQL;
//      end;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('select * from KH_ZDY where ZDYNO=''' + Trim(CDS_HZ.fieldbyname('ZDYNO').AsString) + '''');
        Open;
      end;
      ADOQueryCmd.Edit;
      ADOQueryCmd.FieldByName('ZDYNo').Value := Trim(maxno);
      ADOQueryCmd.FieldByName('ZDYName').Value := cds_hz.fieldbyname('ZDYName').AsString;
      ADOQueryCmd.FieldByName('note').Value := Trim(cds_hz.fieldbyname('NOTE').AsString);
          //ADOQueryCmd.FieldByName('orderno').Value:=ClientDataSet1.fieldbyname('Name').AsString;
      ADOQueryCmd.FieldByName('Type').Value := 'SHFS';
      ADOQueryCmd.FieldByName('valid').Value := 'Y';
//      if Trim(MainType) <> '' then
//        ADOQueryCmd.FieldByName('MainType').Value := Trim(MainType);
          //ADOQueryCmd.FieldByName('sel').Value:=0;
      ADOQueryCmd.Post;
      CDS_HZ.Edit;
      CDS_HZ.FieldByName('ZDYNo').Value := Trim(maxno);
          //ClientDataSet1.Post;
         // Next;
        //end;
      //end;
     // ClientDataSet1.EnableControls;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    //Application.MessageBox('保存成功！','提示',0);
    //TV1.OptionsData.Editing:=False;
    //TV1.OptionsSelection.CellSelect:=False;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！', '提示', 0);
  end;
//  mvalue := TcxTextEdit(Sender).EditingText;
//  if Trim(mvalue) = '' then
//  begin
//    //Application.MessageBox('名称不能为空！','提示',0);
//    Exit;
//  end;
//  with CDS_HZ do
//  begin
//    Edit;
//    FieldByName('ZdyName').Value := Trim(mvalue);
//    //Post;
//  end;
//
//  try
//    ADOQueryCmd.Connection.BeginTrans;
//    with ADOQueryTemp do
//    begin
//      Close;
//      SQL.Clear;
//      SQL.Add('select * from KH_ZDY where ZdyNo=''SHFS''');
//      open;
//    end;
//    if ADOQueryTemp.IsEmpty then
//    begin
//      with ADOQueryCmd do
//      begin
//        close;
//        sql.Clear;
//        sql.Add('insert into KH_ZDY(ZDYNo,ZDYName,Type) select :ZDYNo,:ZDYName,:Type ');
//        Parameters.ParamByName('ZDYNo').Value := 'SHFS';
//        Parameters.ParamByName('ZDYName').Value := '收汇方式';
//        Parameters.ParamByName('Type').Value := 'Main';
////        Parameters.ParamByName('MainType').Value := Trim(MainType);
//        ExecSQL;
//      end;
//    end;
//    with ADOQueryCmd do
//    begin
//      //ClientDataSet1.DisableControls;
//      //with ClientDataSet1 do
//      //begin
//        //First;
//        //while not eof do
//        //begin
//      if Trim(CDS_HZ.FieldByName('ZDYNO').AsString) = '' then
//      begin
//        if GetLSNo(ADOQueryTemp, maxno, 'SY', 'KH_ZDY', 3, 1) = False then
//        begin
//          ADOQueryCmd.Connection.RollbackTrans;
//              //ClientDataSet1.EnableControls;
//          Application.MessageBox('取最大编号失败！', '提示', 0);
//          Exit;
//        end;
//      end
//      else
//      begin
//        maxno := Trim(CDS_HZ.fieldbyname('ZDYNo').AsString);
//      end;
//      with ADOQueryTemp do
//      begin
//        Close;
//        sql.Clear;
//        sql.add('select * from KH_Zdy where Type= ''SHFS''');
//
//        sql.Add(' and ZdyName=''' + Trim(CDS_HZ.fieldbyname('ZdyName').AsString) + '''');
//        Open;
//      end;
//      if ADOQueryTemp.IsEmpty = False then
//      begin
//        if ADOQueryTemp.RecordCount > 1 then
//        begin
//          ADOQueryCmd.Connection.RollbackTrans;
//
//              //ClientDataSet1.EnableControls;
//          Application.MessageBox('名称重复！', '提示', 0);
//          Exit;
//        end;
//        if Trim(CDS_HZ.fieldbyname('ZdyNo').AsString) = '' then
//        begin
//          ADOQueryCmd.Connection.RollbackTrans;
//              //ClientDataSet1.EnableControls;
//          Application.MessageBox('名称重复！', '提示', 0);
//          Exit;
//        end
//        else
//        begin
//          if Trim(ADOQueryTemp.fieldbyname('ZdyNo').AsString) <> Trim(CDS_HZ.fieldbyname('ZdyNo').AsString) then
//          begin
//            ADOQueryCmd.Connection.RollbackTrans;
//                //ClientDataSet1.EnableControls;
//            Application.MessageBox('名称重复！', '提示', 0);
//            Exit;
//          end;
//        end;
//      end;
//      with ADOQueryCmd do
//      begin
//        Close;
//        sql.Clear;
//        SQL.Add('delete KH_ZDY where ZDYNO=''' + Trim(CDS_HZ.fieldbyname('ZDYNO').AsString) + '''');
//        ExecSQL;
//      end;
//      with ADOQueryCmd do
//      begin
//        Close;
//        sql.Clear;
//        sql.Add('select * from KH_ZDY where 1<>1');
//        Open;
//      end;
//      ADOQueryCmd.Append;
//      ADOQueryCmd.FieldByName('ZDYNo').Value := Trim(maxno);
//      ADOQueryCmd.FieldByName('ZDYName').Value := CDS_HZ.fieldbyname('ZDYName').AsString;
////      ADOQueryCmd.FieldByName('note').Value := Trim(snote);
//          //ADOQueryCmd.FieldByName('orderno').Value:=ClientDataSet1.fieldbyname('Name').AsString;
//      ADOQueryCmd.FieldByName('Type').Value := 'SHFS';
//      ADOQueryCmd.FieldByName('valid').Value := 'Y';
////      if Trim(MainType) <> '' then
////        ADOQueryCmd.FieldByName('MainType').Value := Trim(MainType);
//          //ADOQueryCmd.FieldByName('sel').Value:=0;
//      ADOQueryCmd.Post;
//      CDS_HZ.Edit;
//      CDS_HZ.FieldByName('ZDYNo').Value := Trim(maxno);
//          //ClientDataSet1.Post;
//         // Next;
//        //end;
//      //end;
//     // ClientDataSet1.EnableControls;
//    end;
//    ADOQueryCmd.Connection.CommitTrans;
//    //Application.MessageBox('保存成功！','提示',0);
//    //TV1.OptionsData.Editing:=False;
//    //TV1.OptionsSelection.CellSelect:=False;
//  except
//    ADOQueryCmd.Connection.RollbackTrans;
//    Application.MessageBox('保存失败！', '提示', 0);
//  end;
end;

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

procedure TfrmjddateManage.ToolButton4Click(Sender: TObject);
var
  i: Integer;
  maxno: string;
  fxh: Double;
begin

  TV1.OptionsData.Editing := True;
  TV1.OptionsSelection.CellSelect := True;
//  for i := 0 to 5 do
//  begin
//    with CDS_HZ do
//    begin
//      Append;
//      Post;
//    end;
//  end;
  fxh := CDS_HZ.fieldbyname('ZdyFlt1').Value;
  if GetLSNo(ADOQueryTemp, maxno, 'SY', 'KH_ZDY', 3, 1) = False then
  begin
//    ADOQueryCmd.Connection.RollbackTrans;
              //ClientDataSet1.EnableControls;
    Application.MessageBox('取最大编号失败！', '提示', 0);
    Exit;
  end;

  with ADOQueryCmd do
  begin
    close;
    sql.Clear;
    sql.Add('insert into KH_ZDY(ZDYNo,ZDYName,Type,ZdyFlt1) select :ZDYNo,:ZDYName,:Type,:ZdyFlt1 ');
    Parameters.ParamByName('ZDYNo').Value := Trim(maxno);
    Parameters.ParamByName('ZDYName').Value := '';

    Parameters.ParamByName('Type').Value := 'shfs';
//    Parameters.ParamByName('MainType').Value := Trim(MainType);
    Parameters.ParamByName('ZdyFlt1').Value := fxh + 0.1;

//    ADOQueryCmd.FieldByName('valid').Value := 'Y';
    ExecSQL;
  end;
  InitGrid();
  CDS_HZ.locate('ZdyFlt1', fxh, []);
  CDS_HZ.Next;
end;

procedure TfrmjddateManage.ToolButton5Click(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then
    Exit;
  if (Trim(CDS_HZ.FieldByName('ZDYNo').AsString) <> '') or (Trim(CDS_HZ.FieldByName('ZDYname').AsString) <> '') then
  begin
    if application.MessageBox('确定要删除吗?', '提示信息', 1) = 2 then
      exit;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete KH_ZDY where ZDYNo=''' + Trim(CDS_HZ.fieldbyname('ZDYNo').AsString) + '''');
      SQL.Add(' and Type=''SHFS''');
      ExecSQL;
    end;
  end;
  CDS_HZ.Delete;
end;

procedure TfrmjddateManage.Tv1Column1PropertiesEditValueChanged(Sender: TObject);
var
  mvalue: string;
begin
  if Trim(CDS_HZ.fieldbyname('ZdyName').AsString) = '' then
  begin
    Application.MessageBox('名称不能为空!', '提示', 0);
    Exit;
  end;
  mvalue := TcxTextEdit(Sender).EditingText;
  if Trim(mvalue) = '' then
  begin
    mvalue := '0';
  end;
  with CDS_HZ do
  begin
    Edit;
    FieldByName('Note').Value := mvalue;
    Post;
  end;
  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add('Update KH_Zdy Set Note=''' + Trim(mvalue) + '''');
    sql.Add(' where ZdyNo=''' + Trim(CDS_HZ.fieldbyname('ZdyNo').AsString) + '''');
    ExecSQL;
  end;
end;

end.

