unit U_ProductFileLR;

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;

type
  TfrmProductFileLR = 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;
    cxGridLevel1: TcxGridLevel;
    v2Column11: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v2Column2: TcxGridDBColumn;
    v2Column3: TcxGridDBColumn;
    v2Column4: TcxGridDBColumn;
    v2Column5: TcxGridDBColumn;
    v2Column7: 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 v2Column8PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v2Column6PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v2Column11PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
  private
    { Private declarations }
    procedure InitGrid();
    function SaveData(): Boolean;
  public
    { Public declarations }
    RKFlag, FCYID: string;
    fDEFstr5: string;
  end;

var
  frmProductFileLR: TfrmProductFileLR;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp;

{$R *.dfm}

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

procedure TfrmProductFileLR.FormDestroy(Sender: TObject);
begin
  frmProductFileLR := nil;
end;

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

procedure TfrmProductFileLR.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('成品信息录入', Tv2, '客户自定义');
  Close;
end;

procedure TfrmProductFileLR.FormShow(Sender: TObject);
begin
  ReadCxGrid('成品信息录入', Tv2, '客户自定义');
  InitGrid();
  if Trim(FCYID) = '' then
  begin
    CDS_HZ.Append;
    CDS_HZ.Post;
  end;
end;

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

function TfrmProductFileLR.SaveData(): Boolean;
var
  maxId, CRID, fzdyCode: string;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    if Trim(FCYID) = '' then
    begin
      if GetLSNo(ADOQueryCmd, maxId, 'CP', 'CP_Info', 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_Info where CPID=''' + 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('CPID').Value := Trim(maxId);
      RTSetSaveDataCDS(ADOQueryCmd, Tv2, CDS_HZ, '', 0);
      FieldByName('DataType').Value := 'CP';
      FieldByName('Filler').Value := Trim(DName);
      Post;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from CP_Info where isnull(SPName,'''')=''' + Trim(CDS_HZ.fieldbyname('SPName').AsString) + '''');
      sql.Add('and isnull(SPSpec,'''')=''' + Trim(CDS_HZ.fieldbyname('SPSpec').AsString) + '''');
      sql.Add('and isnull(SPCode,'''')=''' + Trim(CDS_HZ.fieldbyname('SPCode').AsString) + '''');
      sql.Add(' and DataType=''CP'' ');
      sql.Add(' and ZFFlag=0 ');
      Open;
    end;
    if ADOQueryCmd.RecordCount > 1 then
    begin
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('此成品已录入!', '提示', 0);
      Exit;
    end;
    with CDS_HZ do
    begin
      Edit;
      FieldByName('CPID').Value := Trim(maxId);
      Post;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Result := True;
  except
    Result := True;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存异常!', '提示', 0);
  end;
end;

procedure TfrmProductFileLR.ToolButton3Click(Sender: TObject);
begin
  cxGrid2.SetFocus;
  if Trim(CDS_HZ.FieldByName('SPCode').AsString) = '' then
  begin
    Application.MessageBox('成品编号不能为空!', '提示', 0);
    Exit;
  end;
//  if Trim(CDS_HZ.FieldByName('QtyUnit').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 TfrmProductFileLR.v2Column8PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'QtyUnitCP';
      flagname := '单位';
      if ShowModal = 1 then
      begin
        with Self.CDS_HZ do
        begin
          Edit;
          FieldByName('QtyUnit').Value := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmProductFileLR.v2Column6PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'CPName';
      flagname := '成品名称';
      if ShowModal = 1 then
      begin
        with Self.CDS_HZ do
        begin
          Edit;
          FieldByName('SPName').Value := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmProductFileLR.v2Column11PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'CPSpec';
      flagname := '成品规格';
      if ShowModal = 1 then
      begin
        with Self.CDS_HZ do
        begin
          Edit;
          FieldByName('SPSpec').Value := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

end.

