unit U_ItemManageNew;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, DB, cxDBData, ToolWin, ComCtrls,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, cxGridLevel,
  cxClasses, cxControls, cxGridCustomView, cxGrid, DBClient, ADODB, ImgList,
  StdCtrls, ExtCtrls;

type
  TfrmItemManageNew = class(TForm)
    TV1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    V1Code: TcxGridDBColumn;
    V1Name: TcxGridDBColumn;
    ToolBar1: TToolBar;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    DataSource1: TDataSource;
    ClientDataSet1: TClientDataSet;
    TBAdd: TToolButton;
    TBSave: TToolButton;
    TBDel: TToolButton;
    TBClose: TToolButton;
    ToolButton1: TToolButton;
    TBEdit: TToolButton;
    V1Note: TcxGridDBColumn;
    V1OrderNo: TcxGridDBColumn;
    ADOConnection1: TADOConnection;
    ImageList24: TImageList;
    Panel1: TPanel;
    Label1: TLabel;
    Edit1: TEdit;
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBAddClick(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure TBEditClick(Sender: TObject);
    procedure TV1CellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
  private
    procedure InitGrid();
    { Private declarations }
  public
    flag,flagname,snote:string;
    fnote,forderno:Boolean;
    { Public declarations }
  end;

var
  frmItemManageNew: TfrmItemManageNew;

implementation
uses
  U_DataLink,U_Fun;

{$R *.dfm}

procedure TfrmItemManageNew.FormCreate(Sender: TObject);
begin
  cxGrid1.Align:=alClient;
  with ADOConnection1 do
  begin
    Connected:=false;
    ConnectionString:=DConString;
    Connected:=true;
  end;
end;

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

procedure TfrmItemManageNew.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from XC_Code where Flag='''+flag+'''');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,ClientDataSet1);
    SInitCDSData20(ADOQueryMain,ClientDataSet1);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmItemManageNew.TBAddClick(Sender: TObject);
begin
  TV1.OptionsData.Editing:=True;
  with ClientDataSet1 do
  begin
    Append;
    Post;
  end;
end;

procedure TfrmItemManageNew.TBSaveClick(Sender: TObject);
begin

  try
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('delete XC_Code where Flag='''+Flag+'''');
      ExecSQL;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from XC_Code where 1<>1');
      Open;
    end;
    with ADOQueryCmd do
    begin
      if ClientDataSet1.IsEmpty then
      begin
        ADOQueryCmd.Close;
        ADOQueryCmd.sql.Clear;
        ADOQueryCmd.sql.Add('delete from XC_Code where Code='''+Flag+'''');
        ADOQueryCmd.ExecSQL;
      end else
      begin
        with ClientDataSet1 do
        begin
          First;
          while not eof do
          begin
            if Trim(ClientDataSet1.FieldByName('Code').AsString)='' then
            begin
              ADOQueryCmd.Connection.RollbackTrans;
              Application.MessageBox('编号不能为空！','提示',0);
              Exit;
            end;
            if Trim(ClientDataSet1.FieldByName('Name').AsString)='' then
            begin
              ADOQueryCmd.Connection.RollbackTrans;
              Application.MessageBox('名称不能为空！','提示',0);
              Exit;
            end;
            with ADOQueryTemp do
            begin
              Close;
              sql.Clear;
              sql.Add('select * from XC_Code where Code='''+Trim(ClientDataSet1.fieldbyname('Code').AsString)+'''');
              Open;
              if not IsEmpty then
              begin
                ADOQueryCmd.Connection.RollbackTrans;
                Application.MessageBox('编号重复！','提示',0);
                Exit;
              end;
            end;
            ADOQueryCmd.Append;
            ADOQueryCmd.FieldByName('Code').Value:=ClientDataSet1.fieldbyname('Code').AsString;
            ADOQueryCmd.FieldByName('Name').Value:=ClientDataSet1.fieldbyname('Name').AsString;
            ADOQueryCmd.FieldByName('note').Value:=Trim(snote);
            //ADOQueryCmd.FieldByName('orderno').Value:=ClientDataSet1.fieldbyname('Name').AsString;
            ADOQueryCmd.FieldByName('Flag').Value:=flag;
            ADOQueryCmd.FieldByName('valid').Value:='Y';
            ADOQueryCmd.FieldByName('sel').Value:=0;
            ADOQueryCmd.Post;
            Next;
          end;
        end;
      end;

    end;
    ADOQueryCmd.Connection.CommitTrans;
    Application.MessageBox('保存成功！','提示',0);
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！','提示',0);
  end;
  TV1.OptionsData.Editing:=False;
end;

procedure TfrmItemManageNew.TBDelClick(Sender: TObject);
begin
  if ClientDataSet1.IsEmpty then Exit;
  if (Trim(ClientDataSet1.FieldByName('Code').AsString)<>'') or
     (Trim(ClientDataSet1.FieldByName('name').AsString)<>'') then
  begin
    if application.MessageBox('确定要删除吗?','提示信息',1)=2 then exit;
    ClientDataSet1.Delete;
  end;
end;

procedure TfrmItemManageNew.TBCloseClick(Sender: TObject);
begin
  ModalResult:=2;
  Close;
end;

procedure TfrmItemManageNew.FormShow(Sender: TObject);
var
  fsj,fsj1:string;
begin
  InitGrid();
  fsj:=Trim(flag)+'01';
  fsj1:=Trim(flagname)+'01';
  if ClientDataSet1.IsEmpty then
  begin
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      //sql.Add('insert into XC_Code(code,name,flag,note)');
      //sql.Add('select '''+Trim(flag)+'''');
      //sql.Add(','''+Trim(flagname)+'''');
      //SQL.Add(',''BASECODE'' ');
      //sql.Add(','''+Trim(snote)+'''');
      sql.Add('insert into XC_Code(code,name,flag,note)');
      sql.Add('select '''+Trim(fsj)+'''');
      sql.Add(','''+Trim(fsj1)+'''');
      SQL.Add(','''+Trim(flag)+'''');
      sql.Add(','''+Trim(snote)+'''');
      ExecSQL;
    end;
    InitGrid();
  end;
  frmItemManageNew.Caption:=Trim(flagname)+'<'+Trim(flag)+'>';
end;

procedure TfrmItemManageNew.ToolButton1Click(Sender: TObject);
begin
  ModalResult:=1;
end;

procedure TfrmItemManageNew.TBEditClick(Sender: TObject);
begin
  TV1.OptionsData.Editing:=True;
end;

procedure TfrmItemManageNew.TV1CellDblClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  if TV1.OptionsData.Editing=False then
  begin
    ModalResult:=1;
  end;
end;

end.
