unit U_BuDongBPKCInPut;

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, cxLookAndFeels, cxLookAndFeelPainters,
  cxNavigator, dxSkinsCore, dxSkinBlack, dxSkinBlue, dxSkinBlueprint,
  dxSkinCaramel, dxSkinCoffee, dxSkinDarkRoom, dxSkinDarkSide,
  dxSkinDevExpressDarkStyle, dxSkinDevExpressStyle, dxSkinFoggy,
  dxSkinGlassOceans, dxSkinHighContrast, dxSkiniMaginary, dxSkinLilian,
  dxSkinLiquidSky, dxSkinLondonLiquidSky, dxSkinMcSkin, dxSkinMetropolis,
  dxSkinMetropolisDark, dxSkinMoneyTwins, dxSkinOffice2007Black,
  dxSkinOffice2007Blue, dxSkinOffice2007Green, dxSkinOffice2007Pink,
  dxSkinOffice2007Silver, dxSkinOffice2010Black, dxSkinOffice2010Blue,
  dxSkinOffice2010Silver, dxSkinOffice2013DarkGray,
  dxSkinOffice2013LightGray, dxSkinOffice2013White, dxSkinPumpkin,
  dxSkinSeven, dxSkinSevenClassic, dxSkinSharp, dxSkinSharpPlus,
  dxSkinSilver, dxSkinSpringTime, dxSkinStardust, dxSkinSummer2008,
  dxSkinTheAsphaltWorld, dxSkinsDefaultPainters, dxSkinValentine,
  dxSkinVS2010, dxSkinWhiteprint, dxSkinXmas2008Blue, dxSkinscxPCPainter;

type
  TfrmBuDongBPKCInPut = class(TForm)
    ToolBar1: TToolBar;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_HZ: TDataSource;
    CDS_HZ: TClientDataSet;
    ToolButton3: TToolButton;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    v2Column2: TcxGridDBColumn;
    Tv2Column4: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v2Column11: TcxGridDBColumn;
    v2Column3: TcxGridDBColumn;
    v2Column4: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v2Column7: TcxGridDBColumn;
    Tv2Column1: TcxGridDBColumn;
    Tv2Column2: TcxGridDBColumn;
    Tv2Column5: TcxGridDBColumn;
    Tv2Column6: TcxGridDBColumn;
    Tv2Column7: TcxGridDBColumn;
    Tv2Column8: TcxGridDBColumn;
    Tv2Column9: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    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 ToolButton1Click(Sender: TObject);
    procedure Tv2Column7PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure Tv2Column8PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure ToolButton2Click(Sender: TObject);
  private
    { Private declarations }
    FDate:TDateTime;
    procedure InitGrid();
    function SaveData(): Boolean;
  public
    { Public declarations }
    RKFlag, FCYID: string;
    fDEFstr5: string;
  end;

var
  frmBuDongBPKCInPut: TfrmBuDongBPKCInPut;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp;

{$R *.dfm}

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

procedure TfrmBuDongBPKCInPut.FormDestroy(Sender: TObject);
begin
  frmBuDongBPKCInPut := nil;
end;

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

procedure TfrmBuDongBPKCInPut.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('不动白坯库存录入', Tv2, '不动白坯库存');
  Close;
end;

procedure TfrmBuDongBPKCInPut.FormShow(Sender: TObject);
begin
  ReadCxGrid('不动白坯库存录入', Tv2, '不动白坯库存');
  FDate:=SGetServerDate(ADOQueryTemp);
  InitGrid();
  if Trim(FCYID) = '' then
  begin
    CDS_HZ.Append;
    CDS_HZ.Post;
  end;
end;

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

function TfrmBuDongBPKCInPut.SaveData(): Boolean;
var
  maxId: string;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    CDS_HZ.DisableControls;
    with CDS_HZ do
    begin
      First;
      while not Eof do
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from CP_BuDong where CBID='''+Trim(CDS_HZ.fieldbyname('CBID').AsString)+'''');
          open;
        end;
        FCYID:=Trim(ADOQueryCmd.fieldbyname('CBID').AsString);
        if Trim(FCYID) = '' then
        begin
          if GetLSNo(ADOQueryCmd, maxId, 'CB', 'CP_BuDong', 4, 1) = False then
          begin
            Result := False;
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('取最大号失败！', '提示', 0);
            Exit;
          end;
        end
        else
        begin
          maxId := Trim(FCYID);
        end;

        with ADOQueryCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from CP_BuDong where CBID=''' + Trim(FCYID) + '''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          if Trim(FCYID) = '' then
          begin
            Append;
            FieldByName('Filler').Value := Trim(DName);
            FieldByName('FillerCode').Value := Trim(DCode);
          end
          else
          begin
            Edit;
            FieldByName('Editer').Value := Trim(DName);
            FieldByName('EditerCode').Value := Trim(DName);
            FieldByName('Edittime').Value := SGetServerDateTime(ADOQueryTemp);
          end;
          FieldByName('CBID').Value := Trim(maxId);
          FieldByName('type').Value := '不动白坯';
          RTSetSaveDataCDS(ADOQueryCmd, Tv2, CDS_HZ, 'CP_BuDong', 0);
          FieldByName('Filler').Value := Trim(DName);
          Post;
        end;
        Edit;
        FieldByName('CBID').Value := Trim(maxId);
        Post;
        Next;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    CDS_HZ.EnableControls;

    Result := True;
  except
    Result := False;
    CDS_HZ.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存异常!', '提示', 0);
  end;
end;

procedure TfrmBuDongBPKCInPut.ToolButton3Click(Sender: TObject);
begin
  cxGrid2.SetFocus;
  if CDS_HZ.Locate('SPName',Null,[]) then
  begin
    Application.MessageBox('品名不能为空!', '提示', 0);
    Exit;
  end;
  if CDS_HZ.Locate('CRTime',Null,[]) then
  begin
    Application.MessageBox('日期不能为空!', '提示', 0);
    Exit;
  end;
  if CDS_HZ.Locate('WeiZhi',Null,[]) then
  begin
    Application.MessageBox('位置不能为空!', '提示', 0);
    Exit;
  end;
  if CDS_HZ.Locate('ColorName',Null,[]) then
  begin
    Application.MessageBox('颜色不能为空!', '提示', 0);
    Exit;
  end;
  if CDS_HZ.Locate('PS',Null,[]) 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 TfrmBuDongBPKCInPut.ToolButton1Click(Sender: TObject);
begin
  with CDS_HZ do
  begin
    Append;
    FieldByName('CRTime').Value:=FDate;
    Post;
  end;
end;

procedure TfrmBuDongBPKCInPut.Tv2Column7PropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'YWY';
      flagname := '业务员';
      if ShowModal = 1 then
      begin
        with Self.CDS_HZ do
        begin
          Edit;
          FieldByName('YWY').Value := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmBuDongBPKCInPut.Tv2Column8PropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'LiDan';
      flagname := '理单';
      if ShowModal = 1 then
      begin
        with Self.CDS_HZ do
        begin
          Edit;
          FieldByName('LiDan').Value := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmBuDongBPKCInPut.ToolButton2Click(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then
    Exit;

  if Trim(CDS_HZ.fieldbyname('CBId').AsString) <> '' then
  begin
    if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
      Exit;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete CP_BuDong where type = ''不动白坯'' and CBId=''' + Trim(CDS_HZ.fieldbyname('CBId').AsString) + '''');
      ExecSQL;
    end;
  end;
  CDS_HZ.Delete;
end;

end.

