unit U_YFCR;

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, cxCheckComboBox, cxDropDownEdit, Menus, cxLookAndFeels,
  cxLookAndFeelPainters, dxBarBuiltInMenu, cxNavigator, dxSkinsCore,
  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, dxSkinsDefaultPainters, dxSkinValentine,
  dxSkinVS2010, dxSkinWhiteprint, dxSkinXmas2008Blue, dxSkinscxPCPainter;

type
  TfrmYFCR = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBDel: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Panel1: TPanel;
    ToolButton2: TToolButton;
    Label1: TLabel;
    ADOQueryMain: TADOQuery;
    ToolButton1: TToolButton;
    RM1: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    Label3: TLabel;
    FactoryName: 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;
    TBEdit: TToolButton;
    VHL: TcxGridDBColumn;
    VBZ: TcxGridDBColumn;
    VYB: TcxGridDBColumn;
    VBB: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    VOrderNo: TcxGridDBColumn;
    v2PayMent: TcxGridDBColumn;
    v2Note: TcxGridDBColumn;
    v2C_CodeName: TcxGridDBColumn;
    v2ssel: TcxGridDBColumn;
    cxTabControl1: TcxTabControl;
    Tchk: TToolButton;
    Tnochk: TToolButton;
    v2Column11: TcxGridDBColumn;
    TBAdd: TToolButton;
    CDS_YFS: TClientDataSet;
    DS_YFS: TDataSource;
    cxGridPopupMenu1: TcxGridPopupMenu;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column4: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Qty: TcxGridDBColumn;
    v1QtyUnit: TcxGridDBColumn;
    v1Price: TcxGridDBColumn;
    v1Money: TcxGridDBColumn;
    v1note: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    YFName: TEdit;
    Label4: TLabel;
    PayMent: TEdit;
    v1Column5: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v2Column2: TcxGridDBColumn;
    YFGSType: TComboBox;
    Label5: TLabel;
    v2Column3: TcxGridDBColumn;
    ToolButton3: TToolButton;
    v2Column4: TcxGridDBColumn;
    Label7: TLabel;
    YFID: TEdit;
    ADOQueryPrint: TADOQuery;
    v1Column9: TcxGridDBColumn;
    ToolButton4: TToolButton;
    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 FactoryNameChange(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure TBAddClick(Sender: TObject);
    procedure Tv2FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure TBEditClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure TchkClick(Sender: TObject);
    procedure TnochkClick(Sender: TObject);
    procedure Tv1DblClick(Sender: TObject);
    procedure YFGSTypeChange(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
  private
    { Private declarations }

    procedure InitGrid();
    procedure SetStatus();
  public
    fFlag: integer;
    { Public declarations }
    RKFlag, FCYID, fmanage: string;
  end;

var
  frmYFCR: TfrmYFCR;

implementation

uses
  U_DataLink, U_Fun, U_YFKInput, U_JXFPHXList, U_FjList10;

{$R *.dfm}

procedure TfrmYFCR.SetStatus();
var
  i: integer;
begin
  Tchk.Visible := false;
  TNochk.Visible := false;
  TBDel.Visible := false;
  if fmanage = '高权限' then
  begin
    case cxTabControl1.TabIndex of
      0:
        begin
          Tchk.Visible := true;
          TBDel.Visible := true;
        end;
      1:
        begin
          TNochk.Visible := true;
        end;
    end;
  end
  else
  begin
    case cxTabControl1.TabIndex of
      0:
        begin
          TBDel.Visible := true;
          Tchk.Visible := true;
        end;
      1:
        begin
          TNochk.Visible := true;
        end;
    end;
  end;
end;

procedure TfrmYFCR.InitGrid();
begin
  with ADOQueryMain do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select * from YF_Money_CR A');
    sql.Add(' where A.CRTime>=''' + FormatDateTime('yyyy-MM-dd', begdate.DateTime) + ''' ');
    sql.Add(' and A.CRTime<''' + FormatDateTime('yyyy-MM-dd', Enddate.DateTime + 1) + ''' ');
    sql.Add(' and A.CRType=''应付款登记''  ');
    if cxTabControl1.TabIndex < 2 then
      sql.Add(' and A.status=''' + inttostr(cxTabControl1.TabIndex) + ''' ');
    Open;
  end;
  SCreateCDS20(ADOQueryMain, CDS_HZ);
  SInitCDSData20(ADOQueryMain, CDS_HZ);
end;

procedure TfrmYFCR.FormDestroy(Sender: TObject);
begin
  frmYFCR := nil;
end;

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

procedure TfrmYFCR.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid(trim(self.caption) + 'Tv1', Tv1, '财务管理');
  WriteCxGrid(trim(self.caption) + 'Tv2', Tv2, '财务管理');
  Close;
end;

procedure TfrmYFCR.TBDelClick(Sender: TObject);
var
  FBBMoney, CRID: string;
begin
  if CDS_HZ.IsEmpty then
    Exit;
  if CDS_HZ.Locate('ssel', true, []) = false then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_HZ do
    begin
      DisableControls;
      First;
      while not eof do
      begin
        if Fieldbyname('ssel').AsBoolean then
        begin
          with ADOQueryCmd do
          begin
            close;
            sql.Clear;
            sql.Add('delete YF_Money_CR where YFID=' + Quotedstr(Trim(CDS_HZ.fieldbyname('YFID').AsString)));
            sql.Add('delete YF_Money_CR_Sub where YFID=''' + Trim(CDS_HZ.fieldbyname('YFID').AsString) + '''');
            execsql;
          end;
        end;
        next;
      end;
      First;
      EnableControls;
    end;
    InitGrid();
    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('操作失败！', '提示信息', 0);
  end;
end;

procedure TfrmYFCR.FormShow(Sender: TObject);
var
  fsj: string;
begin
  ReadCxGrid(trim(self.Caption) + 'Tv1', Tv1, '财务管理');
  ReadCxGrid(trim(self.Caption) + 'Tv2', Tv2, '财务管理');
  Enddate.DateTime := SGetServerDate(ADOQueryTemp);
  begdate.DateTime := Enddate.DateTime - 30;

  fsj := 'select distinct(YFGSType) name from YF_Money_CR where CRType=''应付款登记'' ';
  SInitComBoxBySql(ADOQueryCmd, YFGSType, False, fsj);

  SetStatus();
  InitGrid();
end;

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

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

procedure TfrmYFCR.FactoryNameChange(Sender: TObject);
begin
  ToolButton2.Click;
end;

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

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

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

procedure TfrmYFCR.TBAddClick(Sender: TObject);
begin
  try
    frmYFKInput := TfrmYFKInput.Create(self);
    with frmYFKInput do
    begin
      FYFID := '';
      if showmodal = 1 then
      begin
        initGrid();
      end;
    end;
  finally
    frmYFKInput.free;
  end;
end;

procedure TfrmYFCR.Tv2FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
begin
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add(' select * from YF_Money_CR_SUB A');
    sql.Add('where YFID=' + Quotedstr(Trim(CDS_HZ.fieldbyname('YFID').AsString)));
    Open;
  end;
  SCreateCDS20(ADOQueryTemp, CDS_YFS);
  SInitCDSData20(ADOQueryTemp, CDS_YFS);
end;

procedure TfrmYFCR.N1Click(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then
    exit;
  with CDS_HZ do
  begin
    DisableControls;
    first;
    while not eof do
    begin
      edit;
      fieldbyname('ssel').Value := true;
      post;
      next;
    end;
    First;
    EnableControls;
  end;
end;

procedure TfrmYFCR.N2Click(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then
    exit;
  with CDS_HZ do
  begin
    DisableControls;
    first;
    while not eof do
    begin
      edit;
      fieldbyname('ssel').Value := false;
      post;
      next;
    end;
    First;
    EnableControls;
  end;
end;

procedure TfrmYFCR.TBEditClick(Sender: TObject);
begin
  try
    frmYFKInput := TfrmYFKInput.Create(self);
    with frmYFKInput do
    begin
      FYFID := Trim(CDS_HZ.fieldbyname('YFID').AsString);
      ToolButton1.Visible := false;
      if showmodal = 1 then
      begin
        initGrid();
      end;
    end;
  finally
    frmYFKInput.free;
  end;
end;

procedure TfrmYFCR.FormCreate(Sender: TObject);
begin
  fmanage := trim(DParameters1);
end;

procedure TfrmYFCR.TchkClick(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then
    exit;

  if CDS_HZ.Locate('ssel', true, []) = false then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_HZ do
    begin
      DisableControls;
      First;
      while not eof do
      begin
        if Fieldbyname('ssel').AsBoolean then
        begin
          with ADOQueryCmd do
          begin
            close;
            sql.Clear;
            sql.Add('update YF_Money_CR SET status=''1'' ');
            sql.Add('where YFID =' + quotedstr(trim(CDS_HZ.fieldbyname('YFID').AsString)));
            execsql;
          end;
        end;
        next;
      end;
      First;
      EnableControls;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    InitGrid();
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('操作失败！', '提示信息', 0);
  end;
end;

procedure TfrmYFCR.TnochkClick(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then
    exit;

  if CDS_HZ.Locate('ssel', true, []) = false then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_HZ do
    begin
      DisableControls;
      First;
      while not eof do
      begin
        if Fieldbyname('ssel').AsBoolean then
        begin
          if Fieldbyname('HZFPHX').AsFloat > 0 then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            EnableControls;
            application.MessageBox('不能撤销已核销数据！', '提示信息', 0);
            exit;
          end;
          with ADOQueryCmd do
          begin
            close;
            sql.Clear;
            sql.Add('update YF_Money_CR SET status=''0'' ');
            sql.Add('where YFID =' + quotedstr(trim(CDS_HZ.fieldbyname('YFID').AsString)));
            execsql;
          end;
        end;
        next;
      end;
      First;
      EnableControls;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    InitGrid();
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('操作失败！', '提示信息', 0);
  end;
end;

procedure TfrmYFCR.Tv1DblClick(Sender: TObject);
begin
  frmJXFPHXList := TfrmJXFPHXList.create(self);
  with frmJXFPHXList do
  begin
    FTONO := TRIM(CDS_YFS.Fieldbyname('YFSID').AsString);
    FFromNo := '';
    if showmodal = 1 then
    begin

    end;
    free;
  end;
end;

procedure TfrmYFCR.YFGSTypeChange(Sender: TObject);
begin
  ToolButton2.Click;
end;

procedure TfrmYFCR.ToolButton3Click(Sender: TObject);
var
  filepath: string;
  mBegdate, mEnddate: string;
begin
  if CDS_HZ.IsEmpty then
    exit;
  ExportFtErpFile('应付款明细.rmf', ADOQueryTemp);
  with ADOQueryPrint do
  begin
    Close;
    sql.Clear;
    sql.Add(' select A.*,B.* from YF_Money_CR A');
    sql.Add(' inner join  YF_Money_CR_SUB B on A.YFID=B.YFID');
    sql.Add(' where A.YFID=' + quotedstr(Trim(CDS_HZ.fieldbyname('YFID').AsString)));
//    showmessage(sql.text);
    Open;
  end;

  try

    filepath := ExtractFilePath(Application.ExeName) + 'report\应付款明细.rmf';
    if not FileExists(Pchar(filepath)) then
    begin
      application.MessageBox(pchar('文件[' + filepath + ']不存在！'), '提示信息', MB_IConError);
      exit;
    end;
    RMVariables['zdr'] := trim(DName);
    RM1.LoadFromFile(filepath);
    RM1.ShowReport;

  finally

  end;
end;

procedure TfrmYFCR.ToolButton4Click(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then
    Exit;
  try
    frmFjList10 := TfrmFjList10.Create(Application);
    with frmFjList10 do
    begin
      fkeyNO := Trim(Self.CDS_HZ.fieldbyname('YFID').AsString);
      fType := '应付款';
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmFjList10.Free;
  end;
end;

end.

