unit U_SKCRSave;

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

type
  TfrmSKCRSave = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBDel: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Panel1: TPanel;
    ToolButton2: TToolButton;
    Label1: TLabel;
    YFName: TEdit;
    ADOQueryMain: TADOQuery;
    ToolButton1: TToolButton;
    RM1: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    Label3: TLabel;
    FactoryName10: TEdit;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    Label2: TLabel;
    Label6: TLabel;
    begdate: TDateTimePicker;
    Enddate: TDateTimePicker;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_HZ: TDataSource;
    CDS_HZ: TClientDataSet;
    ToolButton3: TToolButton;
    VYB: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v2Column3: TcxGridDBColumn;
    ToolButton4: TToolButton;
    CheckBox1: TCheckBox;
    Label5: TLabel;
    PayMent: TEdit;
    ToolButton5: TToolButton;
    v2Column2: TcxGridDBColumn;
    Label4: TLabel;
    BZTYpe: TComboBox;
    v2Column4: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure FactoryName10Change(Sender: TObject);
    procedure cxGridDBColumn1PropertiesEditValueChanged(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure cxGridDBColumn3PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v2Column3PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure ToolButton1Click(Sender: TObject);
    procedure v2Column1PropertiesEditValueChanged(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure v2Column2PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure VYBPropertiesEditValueChanged(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure FactoryName10KeyPress(Sender: TObject; var Key: Char);
    procedure ToolButton5Click(Sender: TObject);
  private
    { Private declarations }

    procedure InitGrid();
    function  SaveData():Boolean;

  public
    fflag:integer;
    { Public declarations }
    RKFlag,FCYID,fmanage:String;

  end;

var
  frmSKCRSave: TfrmSKCRSave;

implementation
uses
  U_DataLink,U_Fun,U_ZDYHelp, U_KHListSelJJ,U_RTFun;

{$R *.dfm}



procedure TfrmSKCRSave.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      SQL.Clear;
      sql.Add(' select A.* ');
      sql.Add(',FactoryName10=dbo.getPinYin(A.FactoryName)+RTrim(A.FactoryName)');
      sql.Add('  from YS_Money_CR A');
      if CheckBox1.Checked then
      begin
        sql.Add('where A.CRTime>='''+FormatDateTime('yyyy-MM-dd',begdate.DateTime)+''' ');
        sql.Add(' and CRTime<'''+FormatDateTime('yyyy-MM-dd',Enddate.DateTime+1)+''' ');
      end else
      begin
        sql.Add('where A.CRTime>=''2000-01-01'' ');
        sql.Add(' and CRTime<''2500-01-01'' ');
      end;
      sql.Add(' and A.CRType=''收款登记'' ');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_HZ);
    SInitCDSData20(ADOQueryMain,CDS_HZ);
  finally
    ADOQueryMain.EnableControls;
    ToolButton2.Click;
  end;
end;


procedure TfrmSKCRSave.FormDestroy(Sender: TObject);
begin
  frmSKCRSave:=nil;
end;

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

procedure TfrmSKCRSave.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('收款登记YX',Tv2,'财务管理');
  Close;
end;
procedure TfrmSKCRSave.TBDelClick(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then Exit;
  if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add(' insert into YS_Money_CR_DelLog ');
      sql.Add(' select * from YS_Money_CR where YFID='''+Trim(CDS_HZ.fieldbyname('YFID').AsString)+'''');
      sql.Add(' Update YS_Money_CR_DelLog Set DelTime=getdate(),DelPerson='''+Trim(DName)+'''');
      sql.Add(' where YFID='''+Trim(CDS_HZ.fieldbyname('YFID').AsString)+'''');
      sql.Add(' delete YS_Money_CR where YFID='''+Trim(CDS_HZ.fieldbyname('YFID').AsString)+'''');
      execsql;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    CDS_HZ.Delete;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('删除失败!','提示',0);
  end;
end;

procedure TfrmSKCRSave.FormShow(Sender: TObject);
begin
  ReadCxGrid('收款登记YX',Tv2,'财务管理');
  Enddate.DateTime:=SGetServerDate(ADOQueryTemp);
  begdate.DateTime:=SGetServerDateMBeg(ADOQueryTemp);
  InitGrid();
end;

procedure TfrmSKCRSave.TBRafreshClick(Sender: TObject);
begin
  begdate.SetFocus;
  InitGrid();
end;

procedure TfrmSKCRSave.ToolButton2Click(Sender: TObject);
begin
  if ADOQueryMain.Active=False then Exit;
  SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
  SCreateCDS20(ADOQueryMain,CDS_HZ);
  SInitCDSData20(ADOQueryMain,CDS_HZ);
end;

procedure TfrmSKCRSave.FactoryName10Change(Sender: TObject);
begin
  ToolButton2.Click;
end;

function TfrmSKCRSave.SaveData():Boolean;
var
  maxId,CRID:String;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    if GetLSNo(ADOQueryCmd,maxId,'SK','YS_Money_CR',4,1)=False then
    begin
      Result:=False;
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('取最大号失败！','提示',0);
      Exit;
    end;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('select * from YS_Money_KC where FactoryName='''+Trim(CDS_HZ.fieldbyname('FactoryName').AsString)+'''');
      sql.Add(' and KCType=''KH'' ');
      Open;
    end;
    if ADOQueryCmd.IsEmpty=False then
    begin
      CRID:=ADOQueryCmd.fieldbyname('CRID').AsString;
    end else
    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('Update YS_Money_CRID set CRID=CRID+1');
        sql.Add('select * from YS_Money_CRID ');
        Open;
      end;
      CRID:=ADOQueryCmd.fieldbyname('CRID').AsString;
      with ADOQueryCmd do
      begin
        Close;
        SQL.Clear;
        sql.Add('select * from YS_Money_KC where 1<>1');
        Open;
      end;
      with ADOQueryCmd do
      begin
        Append;
        FieldByName('CRID').Value:=StrToInt(CRID);
        FieldByName('FactoryNo').Value:=Trim(CDS_HZ.fieldbyname('FactoryNo').AsString);
        FieldByName('FactoryName').Value:=Trim(CDS_HZ.fieldbyname('FactoryName').AsString);
        FieldByName('ZdyStr1').Value:='应收收';
        Post;
      end;
    end;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('select * from YS_Money_CR where 1<>1');
      Open;
    end;
    with ADOQueryCmd do
    begin
      Append;
      FieldByName('YFID').Value:=Trim(maxId);
      FieldByName('CRID').Value:=StrToInt(CRID);
      FieldByName('Filler').Value:=Trim(DName);
      FieldByName('CRType').Value:='收款登记';
      FieldByName('CRFlag').Value:='应收收';
      FieldByName('QtyFlag').Value:=-1;
      FieldByName('KPFlag').Value:=0;
      FieldByName('FactoryNo').Value:=Trim(CDS_HZ.fieldbyname('FactoryNo').AsString);
      FieldByName('FactoryName').Value:=Trim(CDS_HZ.fieldbyname('FactoryName').AsString);
      FieldByName('CRTime').Value:=formatdateTIme('yyyy-MM-dd',CDS_HZ.fieldbyname('CRTime').AsDateTime);
      FieldByName('YFDefFlag1').Value:=fFlag;
      FieldByName('status').Value:='0';
      Post;
    end;
    with CDS_HZ do
    begin
      Edit;
      FieldByName('YFId').Value:=Trim(maxId);
      FieldByName('CRID').Value:=StrToInt(CRID);
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Result:=True;
  except
    Result:=True;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('增行失败!','提示',0);
  end;
end;

procedure TfrmSKCRSave.cxGridDBColumn1PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue,FFieldName:String;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;
  FFieldName:=Trim(Tv2.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with CDS_HZ do
  begin
    Edit;
    FieldByName(FFieldName).Value:=Trim(mvalue);
    Post;
  end;
  
end;

procedure TfrmSKCRSave.ToolButton3Click(Sender: TObject);
begin
  try
    frmKHListSelJJ:=TfrmKHListSelJJ.Create(Application);
    with frmKHListSelJJ do
    begin
      frmKHListSelJJ.canshu2:='高权限';
      if ShowModal=1 then
      begin
        with Self.CDS_HZ do
        begin
          Append;
          FieldByName('FactoryNo').Value:=Trim(frmKHListSelJJ.CDS_HZ.fieldbyname('KHNO').AsString);
          FieldByName('FactoryName').Value:=Trim(frmKHListSelJJ.CDS_HZ.fieldbyname('KHNameJC').AsString);
          FieldByName('CRTime').Value:=SGetServerDate(ADOQueryTemp);
          if Trim(frmKHListSelJJ.CDS_HZ.fieldbyname('KHType').AsString)='内销' then
          begin
            FieldByName('BZType').Value:='￥';
          end else
          begin
            FieldByName('BZType').Value:='$';
          end;
          Post;
        end;
        Self.SaveData();
      end;
    end;
  finally
    frmKHListSelJJ.Free;
  end;
end;

procedure TfrmSKCRSave.cxGridDBColumn3PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='YFName';
      flagname:='项目及摘要';
      MainType:='付款';
      if ShowModal=1 then
      begin
        with Self.CDS_HZ do
        begin
          Edit;
          FieldByName('YFCode').Value:=Trim(ClientDataSet1.fieldbyname('ZdyNo').AsString);
          FieldByName('YFName').Value:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;

      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmSKCRSave.v2Column3PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='SKType';
      flagname:='收款方式';
      if ShowModal=1 then
      begin
        Self.CDS_HZ.Edit;
        Self.CDS_HZ.FieldByName('PayMent').Value:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmSKCRSave.ToolButton1Click(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then Exit;
  SelExportData(Tv2,ADOQueryMain,'收款列表');
end;

procedure TfrmSKCRSave.v2Column1PropertiesEditValueChanged(Sender: TObject);
var
  mvalue,FFieldName:String;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;
  FFieldName:=Trim(Tv2.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with CDS_HZ do
  begin
    Edit;
    FieldByName(FFieldName).Value:=Trim(mvalue);
    Post;
  end;

end;

procedure TfrmSKCRSave.cxTabControl1Change(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmSKCRSave.v2Column2PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='FHTaiTou';
      flagname:='公司抬头';
      if ShowModal=1 then
      begin
        with CDS_HZ do
        begin
          Edit;
          FieldByName('ComTaiTou').Value:=Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmSKCRSave.VYBPropertiesEditValueChanged(Sender: TObject);
var
  mvalue,FMoney,FFieldName:String;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;
  if Trim(mvalue)='' then
  begin
    mvalue:='0';
  end;
  FFieldName:=Trim(Tv2.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with CDS_HZ do
  begin
    Edit;
    FieldByName(FFieldName).Value:=Trim(mvalue);
    Post;
  end;
  FMoney:=Trim(CDS_HZ.fieldbyname('Money').AsString);
  if Trim(FMoney)='' then FMoney:='0';
  with CDS_HZ do
  begin
    Edit;
    FieldByName('Money').Value:=StrToFloat(FMoney);
    Post;
  end;
  
end;

procedure TfrmSKCRSave.ToolButton4Click(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then Exit;
  begdate.SetFocus;
  if Application.MessageBox('确定要保存当前选中的数据吗？','提示',32+4)<>IDYES then Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from YS_Money_CR where YFID='''+Trim(CDS_HZ.fieldbyname('YFID').AsString)+'''');
      Open;
    end;
    with ADOQueryCmd do
    begin
      Edit;
      RTSetSaveDataCDS(ADOQueryCmd,Tv2,CDS_HZ,'YS_Money_CR',7);
      FieldByName('JSMoney').Value:=CDS_HZ.fieldbyname('Money').Value;
      FieldByName('Editer').Value:=Trim(DName);
      FieldByName('EditTime').Value:=SGetServerDateTime(ADOQueryTemp);
      Post;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Application.MessageBox('保存成功!','提示',0);
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败!','提示',0);
  end;
end;

procedure TfrmSKCRSave.FactoryName10KeyPress(Sender: TObject;
  var Key: Char);
begin
  if Key=#13 then
  begin
    if Trim(FactoryName10.Text)='' then Exit;
    ToolBar1.SetFocus;
    try
      ADOQueryMain.DisableControls;
      with ADOQueryMain do
      begin
        Close;
        SQL.Clear;
        Filtered:=False;
        sql.Add(' select A.* ');
        sql.Add(',FactoryName10=dbo.getPinYin(A.FactoryName)+RTrim(A.FactoryName)');
        sql.Add(' from YS_Money_CR A where FactoryName='''+Trim(FactoryName10.Text)+'''');
        if CheckBox1.Checked then
        begin
          sql.Add(' and A.CRTime>='''+FormatDateTime('yyyy-MM-dd',begdate.DateTime)+''' ');
          sql.Add(' and A.CRTime<'''+FormatDateTime('yyyy-MM-dd',Enddate.DateTime+1)+''' ');
        end;
        sql.Add(' and A.CRType=''付款登记''  ');
        Open;
      end;
      SCreateCDS20(ADOQueryMain,CDS_HZ);
      SInitCDSData20(ADOQueryMain,CDS_HZ);
    finally
      ADOQueryMain.EnableControls;
      ToolButton2.Click;
    end;
  end;
end;

procedure TfrmSKCRSave.ToolButton5Click(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then Exit;
  begdate.SetFocus;
  if Application.MessageBox('确定要批量保存当前页面的所有数据吗？','提示',32+4)<>IDYES then Exit;
  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 YS_Money_CR where YFID='''+Trim(CDS_HZ.fieldbyname('YFID').AsString)+'''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          Edit;
          RTSetSaveDataCDS(ADOQueryCmd,Tv2,CDS_HZ,'YS_Money_CR',7);
          FieldByName('JSMoney').Value:=CDS_HZ.fieldbyname('Money').Value;
          FieldByName('Editer').Value:=Trim(DName);
          FieldByName('EditTime').Value:=SGetServerDateTime(ADOQueryTemp);
          Post;
        end;
        next;
      end;
    end;
    CDS_HZ.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
    Application.MessageBox('保存成功!','提示',0);
  except
    CDS_HZ.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败!','提示',0);
  end;
end;

end.
