unit U_YFFPSave;

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
  TfrmYFFPSave = 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;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    Label2: TLabel;
    Label6: TLabel;
    begdate: TDateTimePicker;
    Enddate: TDateTimePicker;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_HZ: TDataSource;
    CDS_HZ: TClientDataSet;
    ToolButton3: TToolButton;
    v2Column1: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v2KP: TcxGridDBColumn;
    v2Note: TcxGridDBColumn;
    v2Column3: TcxGridDBColumn;
    v2Column4: TcxGridDBColumn;
    v2Column2: TcxGridDBColumn;
    Label4: TLabel;
    ComTaiTou: TEdit;
    ToolButton4: TToolButton;
    ToolButton5: TToolButton;
    v2Column5: TcxGridDBColumn;
    Label5: TLabel;
    KPNO: TEdit;
    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 cxGridDBColumn5PropertiesEditValueChanged(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure cxGridDBColumn6PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure ToolButton1Click(Sender: TObject);
    procedure v2Column2PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure ToolButton5Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
  private
    { Private declarations }

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

  public
    { Public declarations }
    RKFlag,FCYID:String;

  end;

var
  frmYFFPSave: TfrmYFFPSave;

implementation
uses
  U_DataLink,U_Fun,U_ZDYHelp,U_ZdyAttachGYS;

{$R *.dfm}



procedure TfrmYFFPSave.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      SQL.Clear;
      sql.Add(' select A.* ');
      sql.Add(',FactoryName10=RTrim(dbo.getPinYin(A.FactoryName))+RTrim(A.FactoryName)');
      sql.Add('  from YF_Money_CR A');
      sql.Add('where A.CRTime>=:begdate and A.CRTime<:Enddate and A.CRType=''应付款发票登记'' ');
      Parameters.ParamByName('begdate').Value:=Trim(FormatDateTime('yyyy-MM-dd',begdate.DateTime));
      Parameters.ParamByName('Enddate').Value:=Trim(FormatDateTime('yyyy-MM-dd',Enddate.DateTime+1));
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_HZ);
    SInitCDSData20(ADOQueryMain,CDS_HZ);
  finally
    ADOQueryMain.EnableControls;
  end;
end;
procedure TfrmYFFPSave.InitGridHZ();
begin
end;

procedure TfrmYFFPSave.FormDestroy(Sender: TObject);
begin
  frmYFFPSave:=nil;
end;

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

procedure TfrmYFFPSave.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('应付款发票',Tv2,'财务管理');
  Close;
end;
procedure TfrmYFFPSave.TBDelClick(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then Exit;
  if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
  if Trim(CDS_HZ.fieldbyname('YFID').AsString)<>'' then
  begin
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('delete YF_Money_CR where YFID='''+Trim(CDS_HZ.fieldbyname('YFID').AsString)+'''');
      ExecSQL;
    end;
  end;
  CDS_HZ.Delete;
end;

procedure TfrmYFFPSave.FormShow(Sender: TObject);
var
  fsj:string;
begin
  ReadCxGrid('应付款发票',Tv2,'财务管理');
  Enddate.DateTime:=SGetServerDate(ADOQueryTemp);
  begdate.DateTime:=Enddate.DateTime-7;
  InitGrid();
end;

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

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

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

function TfrmYFFPSave.SaveData():Boolean;
var
  maxId:String;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    if GetLSNo(ADOQueryCmd,maxId,'KP','YF_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 YF_Money_CR where 1<>1');
      Open;
    end;
    with ADOQueryCmd do
    begin
      Append;
      FieldByName('YFID').Value:=Trim(maxId);
      FieldByName('Filler').Value:=Trim(DName);
      FieldByName('CRType').Value:='应付款发票登记';
      FieldByName('CRFlag').Value:='应付付';
      FieldByName('QtyFlag').Value:=1;
      FieldByName('FactoryNo').Value:=Trim(CDS_HZ.fieldbyname('FactoryNo').AsString);
      FieldByName('FactoryName').Value:=Trim(CDS_HZ.fieldbyname('FactoryName').AsString);
      FieldByName('CRTime').Value:=SGetServerDate(ADOQueryTemp);
      Post;
    end;
    with CDS_HZ do
    begin
      Edit;
      FieldByName('YFId').Value:=Trim(maxId);
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Result:=True;
  except
    Result:=True;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('增行失败!','提示',0);
  end;
end;

procedure TfrmYFFPSave.cxGridDBColumn5PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue,FQty,FPrice,FKPMoney,FFieldName:String;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;
  FFieldName:=Trim(Tv2.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with CDS_HZ do
  begin
    Edit;
    if Trim(mvalue)<>'' then
      FieldByName(FFieldName).Value:=Trim(mvalue)
    else
      FieldByName(FFieldName).Value:=Null;
    Post;
  end;
  FKPMoney:=Trim(CDS_HZ.fieldbyname('KPMoney').AsString);
  if Trim(FKPMoney)='' then FKPMoney:='0';
  FQty:=Trim(CDS_HZ.fieldbyname('Qty').AsString);
  if Trim(FQty)='' then FQty:='0';
  FPrice:=Trim(CDS_HZ.fieldbyname('Price').AsString);
  if Trim(FPrice)='' then FPrice:='0';
  if Trim(FFieldName)<>'KPMoney' then
  begin
    with CDS_HZ do
    begin
      Edit;
      FieldByName('KPMoney').Value:=StrToFloat(FQty)*StrToFloat(FPrice);
      Post;
    end;
  end else
  begin
    if Trim(FQty)<>'0' then
    begin
      with CDS_HZ do
      begin
        Edit;
        FieldByName('Price').Value:=StrToFloat(FKPMoney)*1.00/StrToFloat(FQty);
        Post;
      end;
    end;
  end;

end;

procedure TfrmYFFPSave.ToolButton3Click(Sender: TObject);
begin
  try
    frmZdyAttachGYS:=TfrmZdyAttachGYS.Create(Application);
    with frmZdyAttachGYS do
    begin
      if ShowModal=1 then
      begin
        with Self.CDS_HZ do
        begin
          Append;
          FieldByName('FactoryNo').Value:=Trim(CDS_HZ.fieldbyname('ZdyCode').AsString);
          FieldByName('FactoryName').Value:=Trim(CDS_HZ.fieldbyname('ZdyNameZ').AsString);
          FieldByName('CRTime').Value:=SGetServerDate(ADOQueryTemp);
          Post;
        end;
        Self.SaveData();
      end;
    end;
  finally
    frmZdyAttachGYS.Free;
  end;
  tv2.Controller.EditingController.ShowEdit();

end;

procedure TfrmYFFPSave.cxGridDBColumn6PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='OrderUnit';
      flagname:='数量单位';
      if ShowModal=1 then
      begin
        Self.CDS_HZ.Edit;
        Self.CDS_HZ.FieldByName('QtyUnit').Value:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmYFFPSave.ToolButton1Click(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then Exit;
  SelExportData(Tv2,ADOQueryMain,'进项发票列表');
end;

procedure TfrmYFFPSave.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 TfrmYFFPSave.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 YF_Money_CR where YFID='''+Trim(CDS_HZ.fieldbyname('YFID').AsString)+'''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          Edit;
          RTSetSaveDataCDS(ADOQueryCmd,Tv2,CDS_HZ,'YF_Money_CR',7);
          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;

procedure TfrmYFFPSave.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 YF_Money_CR where YFID='''+Trim(CDS_HZ.fieldbyname('YFID').AsString)+'''');
      Open;
    end;
    with ADOQueryCmd do
    begin
      Edit;
      RTSetSaveDataCDS(ADOQueryCmd,Tv2,CDS_HZ,'YF_Money_CR',7);
      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;

end.
