unit U_CPKWEdit;

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, cxDropDownEdit,strutils;

type
  TfrmCPKWEdit = class(TForm)
    ToolBar1: TToolBar;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    RM1: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_HZ: TDataSource;
    CDS_HZ: TClientDataSet;
    ToolButton3: TToolButton;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    v2Column6: TcxGridDBColumn;
    v2Column8: TcxGridDBColumn;
    v2Column3: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    v2DEFStr6: TcxGridDBColumn;
    v2DEFStr7: 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 ToolButton3Click(Sender: TObject);
    procedure v2Column8PropertiesEditValueChanged(Sender: TObject);
    procedure v2DEFStr6PropertiesEditValueChanged(Sender: TObject);
  private
    { Private declarations }
    procedure InitGrid();
    function  SaveData():Boolean;

  public
    { Public declarations }
    RKFlag,FCYID:String;

  end;

var
  frmCPKWEdit: TfrmCPKWEdit;

implementation
uses
  U_DataLink,U_Fun,U_ZDYHelp;

{$R *.dfm}



procedure TfrmCPKWEdit.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      SQL.Clear;
      sql.Add(' select * from KH_Zdy_Attachment where ATID='''+Trim(FCYID)+'''');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_HZ);
    SInitCDSData20(ADOQueryMain,CDS_HZ);
  finally
    ADOQueryMain.EnableControls;
  end;
end;


procedure TfrmCPKWEdit.FormDestroy(Sender: TObject);
begin
  frmCPKWEdit:=nil;
end;

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

procedure TfrmCPKWEdit.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid(self.Caption,Tv2,'客户自定义');
  Close;
end;
procedure TfrmCPKWEdit.FormShow(Sender: TObject);
begin
  ReadCxGrid(self.Caption,Tv2,'客户自定义');
  InitGrid();
  if Trim(FCYID)='' then
  begin
    CDS_HZ.Append;
    CDS_HZ.Post;
  end;
end;

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

function TfrmCPKWEdit.SaveData():Boolean;
var
  maxId,CRID,fzdyCode:String;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    if Trim(FCYID)='' then
    begin
      if GetLSNo(ADOQueryCmd,maxId,'KW','KH_Zdy_Attachment',4,1)=False then
      begin
        Result:=False;
        ADOQueryCmd.Connection.RollbackTrans;
        Application.MessageBox('取最大号失败！','提示',0);
        Exit;
      end;
    end else
    begin
      maxId:=Trim(FCYID);
    end;

    with ADOQueryTemp do
    begin
      close;
      sql.Clear;
      sql.Add('select * from KH_Zdy_Attachment X where ATID='''+Trim(FCYID)+''' and  defstr3=''固定架'' ');
      open;
    end;
    if  not ADOQueryTemp.IsEmpty then
    begin
      with  ADOQueryCmd do
      begin
        Close;
        SQL.Clear;
        sql.Add('UPDATE CK_BanCP_KC set kckw='''' ');
        sql.Add('where kckw='+quotedstr(trim(ADOQueryTemp.fieldbyname('zdyCode').AsString)));
        sql.Add('UPDATE CK_BanCP_CR set kckw='''' ');
        sql.Add('where kckw='+quotedstr(trim(ADOQueryTemp.fieldbyname('zdyCode').AsString)));
        execsql;
      end;
    end;

    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('select * from KH_Zdy_Attachment where ATID='''+Trim(FCYID)+'''');
      Open;
    end;
    with ADOQueryCmd do
    begin
      if Trim(FCYID)='' then
      begin
        Append;
        FieldByName('Filler').Value:=Trim(DName);
        FieldByName('zdyCode').Value:=Trim(fzdyCode);
      end else
      begin
        Edit;
        FieldByName('Editer').Value:=Trim(DName);
        FieldByName('Edittime').Value:=SGetServerDateTime(ADOQueryTemp);
      end;
      FieldByName('ATID').Value:=Trim(maxId);
      FieldByName('ZdyName').Value:=Trim(CDS_HZ.fieldbyname('ZdyCode').AsString);

      RTSetSaveDataCDS(ADOQueryCmd,Tv2,CDS_HZ,'',0);
      FieldByName('Type').Value:='CPKW';
      FieldByName('mrate').Value:=CDS_HZ.fieldbyname('mrate').AsFloat;
      FieldByName('Filler').Value:=Trim(DName);
      Post;
    end;
    with CDS_HZ do
    begin
      Edit;
      FieldByName('ATID').Value:=Trim(maxId);
      Post;
    end;

    with ADOQueryTemp do
    begin
      close;
      sql.Clear;
      sql.Add('select * from KH_Zdy_Attachment X where ATID='''+Trim(FCYID)+''' and  defstr3=''固定架'' ');
      open;
    end;
    if  not ADOQueryTemp.IsEmpty then
    begin
      with  ADOQueryCmd do
      begin
        Close;
        SQL.Clear;
        sql.Add('UPDATE CK_BanCP_KC set kckw='+quotedstr(trim(ADOQueryTemp.fieldbyname('zdyCode').AsString)));
        sql.Add('where C_CodeName='+quotedstr(trim(ADOQueryTemp.fieldbyname('defstr1').AsString)));
        sql.Add('and C_color='+quotedstr(trim(ADOQueryTemp.fieldbyname('defstr2').AsString)));
        sql.Add('UPDATE CK_BanCP_CR set kckw='+quotedstr(trim(ADOQueryTemp.fieldbyname('zdyCode').AsString)));
        sql.Add('where C_CodeName='+quotedstr(trim(ADOQueryTemp.fieldbyname('defstr1').AsString)));
        sql.Add('and C_color='+quotedstr(trim(ADOQueryTemp.fieldbyname('defstr2').AsString)));
        execsql;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Result:=True;
  except
    Result:=True;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存异常!','提示',0);
  end;
end;

procedure TfrmCPKWEdit.ToolButton3Click(Sender: TObject);
begin
  cxGrid2.SetFocus;
  if Trim(CDS_HZ.FieldByName('ZdyCode').AsString)='' then
  begin
    Application.MessageBox('仓位编号不能为空!','提示',0);
    Exit;
  end;
  if Application.MessageBox('确定要保存数据吗？','提示',32+4)<>IDYES then Exit;

  if SaveData() then
  begin
    Application.MessageBox('保存成功!','提示',0);
    ModalResult:=1;
  end;
end;


procedure TfrmCPKWEdit.v2Column8PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue:string;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;

  IF trim(mvalue)='' then exit;

  IF rightBstr(trim(mvalue),1)<>'#'THEN
  BEGIn
    mvalue:=trim(mvalue)+'#';
  END;


  with CDS_HZ do
  begin
    Edit;
    FieldByName('DEFstr1').Value:=Trim(mvalue);
    Post;
  end;
  self.tv2.Controller.EditingController.ShowEdit();



end;

procedure TfrmCPKWEdit.v2DEFStr6PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue:string;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;

  IF trim(mvalue)='' then exit;

  IF rightBstr(trim(mvalue),1)<>'#'THEN
  BEGIn
    mvalue:=trim(mvalue)+'#';
  END;


  with CDS_HZ do
  begin
    Edit;
    FieldByName('DEFstr2').Value:=Trim(mvalue);
    Post;
  end;
  self.tv2.Controller.EditingController.ShowEdit();



end;

end.
