unit U_OurCompanyInput;

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, BtnEdit, cxLookAndFeels, cxLookAndFeelPainters,
  cxNavigator, cxRichEdit, cxMemo;

type
  TfrmOurcompanyInput = class(TForm)
    ToolBar1: TToolBar;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    cxGridPopupMenu2: TcxGridPopupMenu;
    ToolButton3: TToolButton;
    CDS_LXR: TClientDataSet;
    DSLXR: TDataSource;
    ScrollBox1: TScrollBox;
    Label2: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    CoName: TEdit;
    Note: TMemo;
    CoCode: TEdit;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    cxGridLevel1: TcxGridLevel;
    ToolBar2: TToolBar;
    ToolButton4: TToolButton;
    ToolButton5: TToolButton;
    Tv1Column1: TcxGridDBColumn;
    Label1: TLabel;
    CoEName: TEdit;
    Tv1Column2: TcxGridDBColumn;
    Label39: TLabel;
    CoAddress: TEdit;
    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 ToolButton4Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
  private
    { Private declarations }
    procedure InitGrid();
    function SaveData(): Boolean;
  public
    fkhType: string;
    { Public declarations }
    FCOID: string;
  end;

var
  frmOurcompanyInput: TfrmOurcompanyInput;

implementation

uses
  U_DataLink, U_Fun, U_ZDYHelp;

{$R *.dfm}

procedure TfrmOurcompanyInput.InitGrid();
begin
  try

    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      SQL.Clear;
      sql.Add(' select * from Company where  CoType=''本司'' and COID=''' + Trim(FCOID) + '''');
      Open;
    end;
    SCSHData(ADOQueryMain, ScrollBox1, 2);
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      SQL.Clear;
      sql.Add(' select * from Company_Account where COID=' + quotedstr(Trim(FCOID)));
//      ShowMessage(SQL.Text);
      Open;

    end;
    SCreateCDS20(ADOQueryMain, CDS_LXR);
    SInitCDSData20(ADOQueryMain, CDS_LXR);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmOurcompanyInput.FormDestroy(Sender: TObject);
begin
  frmOurcompanyInput := nil;
end;

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

procedure TfrmOurcompanyInput.TBCloseClick(Sender: TObject);
begin

  ModalResult := 1;
end;

procedure TfrmOurcompanyInput.FormShow(Sender: TObject);
begin

  InitGrid();
end;

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

function TfrmOurcompanyInput.SaveData(): Boolean;
var
  MaxId, MaxSubId, FCoCode, FCCID: string;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    if Trim(FCOID) = '' then
    begin
      if GetLSNo(ADOQueryCmd, MaxId, 'OO', 'Company', 4, 1) = False then
      begin
        Result := False;
        ADOQueryCmd.Connection.RollbackTrans;
        Application.MessageBox('取最大号失败！', '提示', 0);
        Exit;
      end;
//      自动生成供应商编号
//      if GetLSNo(ADOQueryCmd, FCoCode, 'G', 'Company', 4, 0) = False then
//      begin
//        Result := False;
//        ADOQueryCmd.Connection.RollbackTrans;
//        Application.MessageBox('取最大号失败！', '提示', 0);
//        Exit;
//      end;
//      CoCode.Text := trim(FCoCode);
//      自动生成供应商编号
      if GetLSNo(ADOQueryCmd, FCoCode, 'Z', 'Company', 4, 0) = False then
      begin
        Result := False;
        ADOQueryCmd.Connection.RollbackTrans;
        Application.MessageBox('取最大号失败！', '提示', 0);
        Exit;
      end;
      CoCode.Text := trim(FCoCode);
    end
    else
    begin
      MaxId := Trim(FCOID);
    end;

    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('select * from Company where COID=''' + Trim(FCOID) + '''');
      Open;
    end;
    with ADOQueryCmd do
    begin
      if Trim(FCOID) = '' then
      begin
        Append;
        FieldByName('Filler').Value := Trim(DName);
//        FieldByName('FillCode').Value := Trim(DCode);
        FieldByName('filltime').Value := SGetServerDateTime(ADOQueryTemp);
      end
      else
      begin
        Edit;
        FieldByName('Editer').Value := Trim(DName);
//        FieldByName('EditCode').Value := Trim(DCode);
        FieldByName('Edittime').Value := SGetServerDateTime(ADOQueryTemp);
      end;
      FieldByName('COID').Value := Trim(MaxId);
      SSetsaveSqlNew(ADOQueryCmd, 'Company', ScrollBox1, 2);
      FieldByName('CoType').Value := '本司';
      FieldByName('Valid').Value := 'Y';
      Post;
    end;

//    with ADOQueryCmd do
//    begin
//      Close;
//      sql.Clear;
//      sql.Add('select * from BS_Company_Main where Code=' + quotedstr(trim(Code.Text)));
//      sql.Add(' and CoType=''供应商''  and Valid=''Y''  ');
//      Open;
//    end;
//    if ADOQueryCmd.RecordCount > 1 then
//    begin
//      ADOQueryCmd.Connection.RollbackTrans;
//      Application.MessageBox('供应商编号重复!', '提示', 0);
//      Exit;
//    end;
//
//    with ADOQueryCmd do
//    begin
//      Close;
//      sql.Clear;
//      sql.Add('select * from BS_Company_Main where Name=' + quotedstr(trim(Name.Text)));
//      sql.Add(' and CoType=''供应商''  and Valid=''Y''  ');
//      Open;
//    end;
//    if ADOQueryCmd.RecordCount > 1 then
//    begin
//      ADOQueryCmd.Connection.RollbackTrans;
//      Application.MessageBox('供应商名称重复!', '提示', 0);
//      Exit;
//    end;
//     添加联系人
    if not CDS_LXR.IsEmpty then
    begin
      with CDS_LXR do
      begin
        CDS_LXR.First;
        while not Eof do
        begin
          FCCID := Trim(CDS_LXR.fieldbyname('CaID').AsString);
          if Trim(FCCID) = '' then
          begin
            if GetLSNo(ADOQueryCmd, MaxSubId, 'CS', 'Company_Account', 4, 1) = False then
            begin
              ADOQueryCmd.Connection.RollbackTrans;
              Application.MessageBox('取联系人最大ID失败!', '提示', 0);
              Exit;
            end;
          end
          else
          begin
            MaxSubId := trim(FCCID);
          end;

          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('select * from Company_Account where CaID=' + quotedstr(Trim(MaxSubId)));
            Open;
          end;
          with ADOQueryCmd do
          begin
            if Trim(FCCID) = '' then
            begin
              Append;
            end
            else
            begin
              Edit;
            end;
            FieldByName('CaID').Value := Trim(MaxSubId);
            FieldByName('COID').Value := Trim(MaxId);
            FieldByName('ACCName').Value := CDS_LXR.fieldbyname('ACCName').AsString;
            FieldByName('CaNote').Value := CDS_LXR.fieldbyname('CaNote').AsString;
//            RTSetSaveDataCDS(ADOQueryCmd, Tv1, CDS_LXR, 'Company_Account', 0);
            Post;
          end;
          with CDS_LXR do
          begin
            Edit;
            FieldByName('CaID').Value := Trim(MaxSubId);
            Post;
          end;
          CDS_LXR.Next;
        end;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Result := True;
  except
    Result := false;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存异常!', '提示', 0);
  end;
end;

procedure TfrmOurcompanyInput.ToolButton3Click(Sender: TObject);
begin

//  if Trim(Code.Text)='' then
//  begin
//    Application.MessageBox('供应商编号不能为空!','提示',0);
//    Exit;
//  end;
  if Trim(CoName.Text) = '' then
  begin
    Application.MessageBox('公司名称不能为空!', '提示', 0);
    Exit;
  end;
  if SaveData() then
  begin
    Application.MessageBox('保存成功!', '提示', 0);
  end;
end;

procedure TfrmOurcompanyInput.ToolButton4Click(Sender: TObject);
begin

  with CDS_LXR do
  begin
    Append;
    Post;
  end;
end;

procedure TfrmOurcompanyInput.ToolButton5Click(Sender: TObject);
begin
  if CDS_LXR.IsEmpty then
    Exit;
  if Trim(CDS_LXR.fieldbyname('CoID').AsString) <> '' then
  begin
    if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
      Exit;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete from Company_Account where CoID=''' + Trim(CDS_LXR.fieldbyname('CoID').AsString) + '''');
      ExecSQL;
    end;
  end;
  CDS_LXR.Delete;

end;

procedure TfrmOurcompanyInput.ToolButton1Click(Sender: TObject);
var
  DwFlag: string;
begin
  DwFlag := trim(CDS_LXR.FieldByName('CSID').AsString);
  if CDS_LXR.Locate('IsDefault', true, []) then
  begin
    CDS_LXR.edit;
    CDS_LXR.FieldByName('IsDefault').value := false;
  end;
  if CDS_LXR.Locate('CSID', DwFlag, []) then
  begin
    CDS_LXR.edit;
    CDS_LXR.FieldByName('IsDefault').value := true;
  end;

end;

end.

