unit U_PKList;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, ComCtrls, ToolWin, cxStyles, cxCustomData,
  cxGraphics, cxFilter, cxData, cxDataStorage, cxEdit, DB, cxDBData, ADODB,
  cxGridCustomPopupMenu, cxGridPopupMenu, cxGridLevel, cxClasses,
  cxControls, cxGridCustomView, cxGridCustomTableView, cxGridTableView,
  cxGridDBTableView, cxGrid, DBClient, cxCalendar, cxButtonEdit, cxSplitter,
  RM_Common, RM_Class, RM_e_Xls, RM_Dataset, RM_System, RM_GridReport,
  cxTextEdit, cxPC, cxCheckBox, Menus;


type
  TfrmPKList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBAdd: TToolButton;
    TBEdit: TToolButton;
    TBDel: TToolButton;
    TBClose: TToolButton;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    TBExport: TToolButton;
    Order_Main: TClientDataSet;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    CDS_PRT: TClientDataSet;
    ToolButton3: TToolButton;
    cxGridPopupMenu2: TcxGridPopupMenu;
    Panel1: TPanel;
    Label2: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    FactoryName: TEdit;
    Label6: TLabel;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column6: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1ShipDate: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    Label1: TLabel;
    v1Column21: TcxGridDBColumn;
    v1Column22: TcxGridDBColumn;
    ToolButton1: TToolButton;
    ToolButton4: TToolButton;
    v1Column1: TcxGridDBColumn;
    cxTabControl1: TcxTabControl;
    ToolButton2: TToolButton;
    v1Column16: TcxGridDBColumn;
    v1Column17: TcxGridDBColumn;
    v1Column18: TcxGridDBColumn;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    ToolButton5: TToolButton;
    v1Column19: TcxGridDBColumn;
    v1Column20: TcxGridDBColumn;
    v1Column23: TcxGridDBColumn;
    v1Column25: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    Label3: TLabel;
    Label4: TLabel;
    PayMent: TEdit;
    ComTaiTou: TEdit;
    Label7: TLabel;
    JBPerson: TEdit;
    Label8: TLabel;
    PKName: TEdit;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure TBEditClick(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure TBAddClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure cxPageControl1Change(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure FactoryNameChange(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure Tv1StylesGetContentStyle(Sender: TcxCustomGridTableView;
      ARecord: TcxCustomGridRecord; AItem: TcxCustomGridTableItem;
      out AStyle: TcxStyle);
    procedure v1Column22CustomDrawCell(Sender: TcxCustomGridTableView;
      ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo;
      var ADone: Boolean);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);

  private
    FInt,PFInt:Integer;
    canshu1,canshu2:string;
    FNowDate:TDateTime;
    procedure InitGrid();
    procedure InitForm();
    function  DelData():Boolean;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmPKList: TfrmPKList;

implementation
uses
  U_DataLink,U_RTFun,U_ModuleNote, U_ZDYHelp,U_PKInPutWFX;

{$R *.dfm}

procedure TfrmPKList.FormDestroy(Sender: TObject);
begin
  frmPKList:=nil;
end;

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

procedure TfrmPKList.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('资金审批列表生产',Tv1,'财务管理');
end;

procedure TfrmPKList.InitGrid();
var
  fsj:String;
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add(' select A.*');
      SQL.Add(' from YF_Money_PaiKuan A where ');
      sql.Add(' A.PKDate>='''+Trim(FormatDateTime('yyyy-MM-dd',BegDate.Date))+'''');
      sql.Add(' and A.PKDate<'''+Trim(FormatDateTime('yyyy-MM-dd',Enddate.Date+1))+'''');
      sql.Add(' and isnull(HZType,'''')=''生产'' ');
      if cxTabControl1.TabIndex=0 then
      begin
        sql.Add(' and isnull(ChkStatus,'''')='''' ');
      end else
      if cxTabControl1.TabIndex=1 then
      begin
        sql.Add(' and isnull(ChkStatus,'''')=''已送审'' ');
      end else
      if cxTabControl1.TabIndex=2 then
      begin
        sql.Add(' and isnull(ChkStatus,'''')=''审核通过'' ');
      end  else
      if cxTabControl1.TabIndex=3 then
      begin
        sql.Add(' and isnull(ChkStatus,'''')=''审核不通过'' ');
      end;
      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;

end;

procedure TfrmPKList.InitForm();
begin
  FNowDate:=SGetServerDate(ADOQueryTemp);
  EndDate.Date:=FNowDate+7;
  BegDate.Date:=FNowDate-7;
  ReadCxGrid('资金审批列表生产',Tv1,'财务管理');
end;

procedure TfrmPKList.TBEditClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  if (cxTabControl1.TabIndex=0) or (cxTabControl1.TabIndex=3)  then
  begin
    {if Trim(DName)<>Trim(Order_Main.fieldbyname('Filler').AsString) then
    begin
      Application.MessageBox('不能操作他人的信息!','提示',0);
      Exit;
    end;}
    try
      frmPKInPutWFX:=TfrmPKInPutWFX.Create(Application);
      with frmPKInPutWFX do
      begin
        PState:=1;
        FMainId:=Trim(Self.Order_Main.fieldbyname('PKID').AsString);
        ToolButton3.Visible:=False;
        ToolButton4.Visible:=False;
        ToolButton1.Visible:=False;
        if ShowModal=1 then
        begin

        end;
      end;
    finally
      frmPKInPutWFX.Free;
    end;
  end;

end;

procedure TfrmPKList.TBDelClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  if cxTabControl1.TabIndex<>0 then Exit;
  {if Trim(DName)<>Trim(Order_Main.fieldbyname('Filler').AsString) then
  begin
    Application.MessageBox('不能操作他人的信息!','提示',0);
    Exit;
  end; }
  if Order_Main.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
  if DelData() then
  begin
    //Order_Main.Delete;
  end;
end;

function TfrmPKList.DelData():Boolean;
begin
  try
    Result:=false;
    ADOQueryCmd.Connection.BeginTrans;
    with Order_Main do
    begin
      First;
      while Locate('SSel',True,[]) do
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('delete YF_Money_PaiKuan  where PKId='''+Trim(Order_Main.fieldbyname('PKId').AsString)+'''');
          ExecSQL;
        end;
        Order_Main.Delete;
      end;
    end;

    ADOQueryCmd.Connection.CommitTrans;
    Result:=True;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Result:=False;
    Application.MessageBox('数据删除异常！','提示',0);
  end;
end;

procedure TfrmPKList.TBExportClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  TcxGridToExcel('资金申请列表(生产)',cxGrid1);
end;

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

procedure TfrmPKList.TBAddClick(Sender: TObject);
begin
  try
    frmPKInPutWFX:=TfrmPKInPutWFX.Create(Application);
    with frmPKInPutWFX do
    begin
      PState:=0;
      FMainId:='';
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmPKInPutWFX.Free;
  end;
end;

procedure TfrmPKList.FormShow(Sender: TObject);
begin
  InitForm();
end;

procedure TfrmPKList.cxPageControl1Change(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmPKList.ToolButton3Click(Sender: TObject);
begin
  try
    frmModuleNote:=TfrmModuleNote.Create(Application);
    with frmModuleNote do
    begin
      flag:='资金申请(生产)';
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmModuleNote.Free;
  end;
end;

procedure TfrmPKList.FormCreate(Sender: TObject);
begin
  canshu1:=Trim(DParameters1);
  canshu2:=Trim(DParameters2);
end;



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

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

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

procedure TfrmPKList.ToolButton4Click(Sender: TObject);
var
   fPrintFile:string;
begin
  if Order_Main.IsEmpty then Exit;
  if cxTabControl1.TabIndex<>2 then Exit;
  if Order_Main.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\资金审批表.rmf' ;
  with ADOQueryCmd do
  begin
    close;
    sql.Clear;
    sql.Add('delete TBSubID where DName='''+Trim(DName)+'''');
    ExecSQL;
  end;
  Order_Main.DisableControls;
  with Order_Main do
  begin
    First;
    while not Eof do
    begin
      if Order_Main.FieldByName('SSel').AsBoolean=True then
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('insert into TBSubID');
          sql.Add(' select '''+Trim(Order_Main.fieldbyname('PKID').AsString)+'''');
          sql.Add(','''+Trim(DName)+'''');
          ExecSQL;
        end;
        Edit;
        FieldByName('PrtFlag').Value:=True;
        Post;
      end;
      Next;
    end;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update YF_Money_PaiKuan Set PrtFlag=1,PrtTime=getdate(),Prter='''+Trim(DName)+'''');
    sql.Add(',PrtCount=PrtCount+1');
    sql.Add(' where PKID in(select SubId from TBSubID A where A.DName='''+Trim(DName)+''')');
    ExecSQL;
  end;
  Order_Main.EnableControls;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add(' select AA.*,');
    sql.Add(' Case when isnull(FactoryNameOther,'''')<>'''' then RTrim(FactoryNameOther)+''→''+RTrim(FactoryName) ');
    sql.Add(' else RTrim(FactoryNameQC)+''→''+RTrim(FactoryName) end as FactoryNameHZ');
    sql.Add(' from (');
    sql.Add(' select A.PKID,A.ComTaiTou,A.PKYongTu,A.PayMent,A.FactoryNameOther,A.FactoryName,isnull(A.PKMoney,0) PKMoney,A.ChkNote,A.Note  ');
    sql.Add(' ,isnull(A.BQKKMoney,0) BQKKMoney,isnull(A.HZKKMoney,0) HZKKMoney,isnull(A.BQYFMoney,0) BQYFMoney,isnull(A.HZYFMoney,0) HZYFMoney');
    sql.Add(',isnull(A.DDuiZhangMoney,0) DDuiZhangMoney,isnull(A.MaxHZYFMoney,0) MaxHZYFMoney,A.PKDate,A.Filler');
    sql.Add(' ,FYear=Rtrim(Convert(varchar(4),A.PKDate,120))+''年''');
    sql.Add(' ,FactoryNameQC=(select KHName from ZH_KH_Info B where B.KHNameJC=A.FactoryName and B.Type=''GYS'' ) ');
    sql.Add(' from YF_Money_PaiKuan A');
    sql.Add(' where PKID in(select SubId from TBSubID where DName='''+Trim(DName)+'''))AA');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,CDS_PRT);
  SInitCDSData20(ADOQueryTemp,CDS_PRT);
  if FileExists(fPrintFile) then
  begin
    //RMVariables['FMonth']:=Order_Main.fieldbyname('KQDate10').AsString;
    RM1.ShowPrintDialog:=True;
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end else
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'资金审批表'),'提示',0);
  end;   

end;
procedure TfrmPKList.Tv1StylesGetContentStyle(
  Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord;
  AItem: TcxCustomGridTableItem; out AStyle: TcxStyle);
var
  id,id10,id20:Integer;
begin
  {try
    if Tv1.GroupedItemCount=0 then
    begin
      Id:=Tv1.GetColumnByFieldName('YuJinagDate1').Index-tv1.GroupedItemCount;
      Id10:=Tv1.GetColumnByFieldName('YuJinagDate2').Index-tv1.GroupedItemCount;
      Id20:=TV1.GetColumnByFieldName('MoneyKC').Index;
      if ARecord.Values[id20]<=0 then Exit;
      if ARecord.Values[Id10]-FNowDate<=0 then
      begin
        AStyle:=DataLink_KangChi.Red;
      end else
      begin
        if ARecord.Values[Id]-FNowDate<=0 then
        begin
          AStyle:=DataLink_KangChi.Yellow;
        end;
      end;
    end else
    begin

    end;
  except
  end;  }
end;

procedure TfrmPKList.v1Column22CustomDrawCell(
  Sender: TcxCustomGridTableView; ACanvas: TcxCanvas;
  AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
var
  Id,Id10,Id20:Integer;
begin
  {Id:=TV1.GetColumnByFieldName('PKMoney').Index;//;-TV1.GroupedItemCount;
  Id10:=TV1.GetColumnByFieldName('BQYFMoney').Index;
  Id20:=TV1.GetColumnByFieldName('HZYFMoney').Index;
  if Id<0 then Exit;
  if AViewInfo.GridRecord.Values[id]>AViewInfo.GridRecord.Values[id20] then
  begin
    ACanvas.Brush.Color:=clRed;
  end else
  begin
    if AViewInfo.GridRecord.Values[id]>AViewInfo.GridRecord.Values[id10] then
    begin
      ACanvas.Brush.Color:=clPurple;
    end else
    begin
      ACanvas.Brush.Color:=clSkyBlue;
    end;
  end;    }
end;

procedure TfrmPKList.N1Click(Sender: TObject);
begin
  SelOKNo(Order_Main,True);
end;

procedure TfrmPKList.N2Click(Sender: TObject);
begin
  SelOKNo(Order_Main,False);
end;

procedure TfrmPKList.ToolButton2Click(Sender: TObject);
begin
  if cxTabControl1.TabIndex<>0 then Exit;
  if Order_Main.IsEmpty then Exit;
  if Order_Main.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  if Application.MessageBox('确定要执行送审操作吗？','提示',32+4)<>IDYES then Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    Order_Main.DisableControls;
    with Order_Main do
    begin
      First;
      while Order_Main.Locate('SSel',True,[]) do
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update YF_Money_PaiKuan Set SSTime=getdate(),SSPerson='''+Trim(DName)+'''');
          sql.Add(',ChkStatus=''已送审'' ');
          sql.Add(' where PKID='''+Trim(Order_Main.fieldbyname('PKID').AsString)+'''');
          ExecSQL;
        end;
        Order_Main.Delete;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Order_Main.EnableControls;
  except
    Order_Main.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作异常!','提示',0);
  end;
end;

procedure TfrmPKList.ToolButton5Click(Sender: TObject);
begin
  if cxTabControl1.TabIndex<>1 then Exit;
  if Order_Main.IsEmpty then Exit;
  if Order_Main.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  if Application.MessageBox('确定要执行撤销操作吗？','提示',32+4)<>IDYES then Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    Order_Main.DisableControls;
    with Order_Main do
    begin
      First;
      while Order_Main.Locate('SSel',True,[]) do
      begin
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from YF_Money_PaiKuan  ');
          sql.Add(' where PKID='''+Trim(Order_Main.fieldbyname('PKID').AsString)+'''');
          sql.Add(' and isnull(ChkStatus,'''')=''审核通过'' ');
          Open;
        end;
        if ADOQueryTemp.IsEmpty=False then
        begin
          Order_Main.EnableControls;
          ADOQueryCmd.Connection.RollbackTrans;
          Application.MessageBox('有审核通过的数据，请刷新界面数据!','提示',0);
          Exit;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update YF_Money_PaiKuan Set SSTime=NUll,SSPerson=Null');
          sql.Add(',ChkStatus=Null ');
          sql.Add(' where PKID='''+Trim(Order_Main.fieldbyname('PKID').AsString)+'''');
          ExecSQL;
        end;
        Order_Main.Delete;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Order_Main.EnableControls;
  except
    Order_Main.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作异常!','提示',0);
  end;
end;

end.
