unit U_CustomerInput;

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, dxSkinsCore, dxSkinsDefaultPainters, dxSkinscxPCPainter,
  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,
  dxSkinValentine, dxSkinVS2010, dxSkinWhiteprint, dxSkinXmas2008Blue;

type
  TfrmCustomerInput = class(TForm)
    ToolBar1: TToolBar;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    cxGridPopupMenu2: TcxGridPopupMenu;
    ToolButton3: TToolButton;
    ToolBar2: TToolBar;
    ToolButton4: TToolButton;
    ToolButton5: TToolButton;
    CDS_LXR: TClientDataSet;
    DSLXR: TDataSource;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    v3Column2: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    v3Column3: TcxGridDBColumn;
    ToolButton1: TToolButton;
    ScrollBox1: TScrollBox;
    Label2: TLabel;
    Label6: TLabel;
    Label12: TLabel;
    Label39: TLabel;
    Label7: TLabel;
    Label19: TLabel;
    Name: TEdit;
    Address: TEdit;
    Note: TMemo;
    Label1: TLabel;
    ShortName: TEdit;
    Code: TEdit;
    area: TEdit;
    Label3: TLabel;
    Label4: TLabel;
    BankName: TEdit;
    BankNo: TEdit;
    YWYName: TBtnEditC;
    Label5: TLabel;
    ywycode: TEdit;
    Label8: TLabel;
    TJNAME: TEdit;
    ToolButton2: TToolButton;
    Tv1Column2: 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 ToolButton4Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure YWYNameBtnUpClick(Sender: TObject);
    procedure YWYNameBtnDnClick(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
  private
    { Private declarations }
    procedure InitGrid();
    function SaveData(): Boolean;
  public
    fkhType: string;
    { Public declarations }
    FCOID: string;
    fname: string;
    FCONAME: string;

  end;

var
  frmCustomerInput: TfrmCustomerInput;

implementation

uses
  U_DataLink, U_Fun, U_ZDYHelp, U_YWYSel, U_WeiXinHelp;

{$R *.dfm}

procedure TfrmCustomerInput.InitGrid();
begin
  try

    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      SQL.Clear;
      sql.Add(' select * from BS_Company_Main where  CoType=''客户'' and COID=''' + Trim(FCOID) + '''');
      Open;
    end;
    SCSHData(ADOQueryMain, ScrollBox1, 2);
    YWYName.TxtCode := ADOQueryMain.fieldbyname('Ywycode').AsString;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      SQL.Clear;
      sql.Add(' select * from BS_Company_Sub where COID=' + quotedstr(Trim(FCOID)));
      Open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_LXR);
    SInitCDSData20(ADOQueryMain, CDS_LXR);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmCustomerInput.FormDestroy(Sender: TObject);
begin
  frmCustomerInput := nil;
end;

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

procedure TfrmCustomerInput.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid(trim(self.Caption), Tv1, '客户管理');
  ModalResult := 1;
end;

procedure TfrmCustomerInput.FormShow(Sender: TObject);
begin
  ReadCxGrid(trim(self.Caption), Tv1, '客户管理');
  InitGrid();
end;

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

function TfrmCustomerInput.SaveData(): Boolean;
var
  MaxId, MaxSubId, FCode, FCCID, FCoCode: string;
  FENAME: string;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    if Trim(FCOID) = '' then
    begin
      if GetLSNo(ADOQueryCmd, MaxId, 'CO', 'BS_Company_Main', 4, 1) = False then
      begin
        Result := False;
        ADOQueryCmd.Connection.RollbackTrans;
        Application.MessageBox('取最大号失败！', '提示', 0);
        Exit;
      end;

    end
    else
    begin
      MaxId := Trim(FCOID);
    end;

    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('select * from BS_Company_Main 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, 'BS_Company_Main', ScrollBox1, 2);
      FieldByName('Ywycode').Value := ywycode.Text;
      FieldByName('CoType').Value := '客户';
      FieldByName('Valid').Value := 'Y';
      if TJNAME.Text = '' then
      begin
        FieldByName('TJNAME').Value := Self.Name.TEXT;
        TJNAME.Text := Self.Name.TEXT;
      end;
      Post;
    end;
    fname := Name.Text;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from BS_Company_Main where Name=' + quotedstr(trim(fname)));
      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 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;
//    插入联系人
    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('CSID').AsString);
          if Trim(FCCID) = '' then
          begin
            if GetLSNo(ADOQueryCmd, MaxSubId, 'CS', 'BS_Company_Sub', 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 BS_Company_Sub where CSID=' + quotedstr(Trim(MaxSubId)));
            Open;
          end;
          with ADOQueryCmd do
          begin
            if Trim(FCCID) = '' then
            begin
              Append;
            end
            else
            begin
              Edit;
            end;
            FieldByName('CSID').Value := Trim(MaxSubId);
            FieldByName('COID').Value := Trim(MaxId);
            RTSetSaveDataCDS(ADOQueryCmd, Tv1, CDS_LXR, 'BS_Company_Sub', 0);
            Post;
          end;
          with CDS_LXR do
          begin
            Edit;
            FieldByName('CSID').Value := Trim(MaxSubId);
            Post;
          end;
          CDS_LXR.Next;
        end;
      end;
    end;
    if FCONAME <> Trim(Name.Text) then
    begin
      FENAME := Trim(Name.Text);
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
//        SQL.Add('UPDATE CK_SXPB_CR SET FACTORYNAME=' + QuotedStr(trim(Name.Text)));
//        SQL.Add('WHERE FACTORYNAME=' + QuotedStr(trim(FCONAME.Text)));
//        SQL.Add('UPDATE CK_SXPB_CR SET FromFactoryName=' + QuotedStr(trim(Name.Text)));
//        SQL.Add('WHERE FromFactoryName=' + QuotedStr(trim(FCONAME.Text)));
//        SQL.Add('UPDATE CK_SXPB_CR SET TOFactoryName=' + QuotedStr(trim(Name.Text)));
//        SQL.Add('WHERE TOFactoryName=' + QuotedStr(trim(FCONAME.Text)));
//        SQL.Add('UPDATE CK_WarpBeam_CR SET TOFactoryName=' + QuotedStr(trim(Name.Text)));
//        SQL.Add('WHERE TOFactoryName=' + QuotedStr(trim(FCONAME.Text)));


        SQL.Add('UPDATE Contract_Main SET BUYNAME=' + QuotedStr(trim(FENAME)));
        SQL.Add('WHERE BUYNAME=' + QuotedStr(trim(FCONAME)));
        SQL.Add('UPDATE FD_Invoice SET FactoryName=' + QuotedStr(trim(FENAME)));
        SQL.Add('WHERE FactoryName=' + QuotedStr(trim(FCONAME)));
        SQL.Add('UPDATE FD_Invoice SET TOFactoryName=' + QuotedStr(trim(FENAME)));
        SQL.Add('WHERE TOFactoryName=' + QuotedStr(trim(FCONAME)));
        SQL.Add('UPDATE PD_Plan_Main SET KHNAME=' + QuotedStr(trim(FENAME)));
        SQL.Add('WHERE KHNAME=' + QuotedStr(trim(FCONAME)));
        SQL.Add('UPDATE YF_Money_CR SET FactoryName=' + QuotedStr(trim(FENAME)));
        SQL.Add('WHERE FactoryName=' + QuotedStr(trim(FCONAME)));

        sql.Add('insert into SY_SysLog(operor,opertime,Model,acction,opevent,result) values( ');
        sql.Add(' ' + quotedstr(trim(DName)));
        sql.Add(',getdate() ');
        sql.Add(',' + quotedstr(trim(self.Caption)));
        sql.Add(',' + quotedstr(trim('批量更新客户名称')));
        sql.Add(',' + quotedstr(trim('原名称：' + trim(FCONAME) + '现名称:' + trim(FENAME))));
        sql.Add(',' + quotedstr(trim('成功')));
        sql.Add(')');
        EXECSQL;
      end;
      FCONAME := Trim(Name.Text);
    end;
    ADOQueryCmd.Connection.CommitTrans;
    FCCID := Trim(MaxSubId);
    Result := True;
  except
    Result := false;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存异常!', '提示', 0);
  end;
end;

procedure TfrmCustomerInput.ToolButton3Click(Sender: TObject);
var
  FCoCode: string;
begin
  if Trim(Name.Text) = '' then
  begin
    Application.MessageBox('客户名称不能为空!', '提示', 0);
    Exit;
  end;
  if Trim(Code.Text) = '' then
  begin
//    Application.MessageBox('客户编号不能为空!', '提示', 0);
//    Exit;
//      自动读取客户编号
    //if GetLSNo(ADOQueryCmd, FCoCode, 'KH', 'Company', 5, 0) = False then
    if GetLSNo(ADOQueryCmd, FCoCode, 'CF', 'Company', 5, 0) = False then
    begin

      Application.MessageBox('取最大号失败！', '提示', 0);
      Exit;
    end;
    Code.Text := trim(FCoCode);
  end;
  if SaveData() then
  begin
    Application.MessageBox('保存成功!', '提示', 0);
  end;
end;

procedure TfrmCustomerInput.ToolButton4Click(Sender: TObject);
var
  maxno: string;
begin
  if GetLSNo(ADOQueryCmd, maxno, 'CS', 'BS_Company_Sub', 4, 1) = False then
  begin
    Application.MessageBox('取系人最大ID失败!', '提示', 0);
    Exit;
  end;
  if CDS_LXR.IsEmpty then
  begin
    with CDS_LXR do
    begin
      Append;
      fieldbyname('CSID').Value := trim(maxno);
      fieldbyname('IsDefault').Value := true;
      Post;
    end;
  end
  else
  begin
    with CDS_LXR do
    begin
      Append;
      fieldbyname('CSID').Value := trim(maxno);
      fieldbyname('IsDefault').Value := false;
      Post;
    end;
  end;

end;

procedure TfrmCustomerInput.ToolButton5Click(Sender: TObject);
begin
  if CDS_LXR.IsEmpty then
    Exit;
  if Trim(CDS_LXR.fieldbyname('CSID').AsString) <> '' then
  begin
    if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
      Exit;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete from BS_Company_Sub where CSID=''' + Trim(CDS_LXR.fieldbyname('CSID').AsString) + '''');
      ExecSQL;
    end;
  end;
  CDS_LXR.Delete;
  if CDS_LXR.IsEmpty = false then
  begin
    if CDS_LXR.Locate('IsDefault', true, []) = false then
    begin
      CDS_LXR.first;
      CDS_LXR.edit;
      CDS_LXR.FieldByName('IsDefault').value := true;
    end;
  end;
end;

procedure TfrmCustomerInput.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;

procedure TfrmCustomerInput.YWYNameBtnUpClick(Sender: TObject);
begin
  try
    frmYWYSel := TfrmYWYSel.Create(Application);
    with frmYWYSel do
    begin
      if ShowModal = 1 then
      begin
        YWYName.Text := Trim(ClientDataSet1.fieldbyname('UserName').AsString);
        ywycode.Text := Trim(ClientDataSet1.fieldbyname('UserID').AsString);
      end;
    end;
  finally
    frmYWYSel.Free;
  end;
end;

procedure TfrmCustomerInput.YWYNameBtnDnClick(Sender: TObject);
begin
  YWYName.Text := '';
  YWYName.TxtCode := '';
end;

procedure TfrmCustomerInput.ToolButton2Click(Sender: TObject);
var
  FUserId, maxno: string;
begin

  try
    frmWeiXinHelp := TfrmWeiXinHelp.Create(Self);
    with frmWeiXinHelp do
    begin
      if ShowModal = 1 then
      begin
        if GetLSNo(ADOQueryCmd, maxno, 'CS', 'BS_Company_Sub', 4, 1) = False then
        begin
          Application.MessageBox('取系人最大ID失败!', '提示', 0);
          Exit;
        end;

        with CDS_LXR do
        begin
          Append;
          fieldbyname('CSID').Value := trim(maxno);
          fieldbyname('WeChat').Value := Trim(frmWeiXinHelp.CDS_Help.FieldByName('NICKNAME').AsString);
          fieldbyname('WeChatid').Value := Trim(frmWeiXinHelp.CDS_Help.FieldByName('OPENID').AsString);

          Post;
        end;

      end;
    end;
  finally
    frmWeiXinHelp.Free;
  end;
end;

end.

