unit U_YFCRWTS_HZQD_CX;

interface

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

type
  TFrmYFCRWTS_HZQD_CX = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    ToolButton2: TToolButton;
    ToolButton5: TToolButton;
    ToolButton6: TToolButton;
    ToolButton1: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    Label1: TLabel;
    Label3: TLabel;
    Label2: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    QDFactoryName: TEdit;
    QDInvoiceNo: TEdit;
    begdate: TDateTimePicker;
    Enddate: TDateTimePicker;
    QDOrderNo: TEdit;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    v2SSel: TcxGridDBColumn;
    V1OrderNo: TcxGridDBColumn;
    v2QDFactoryName: TcxGridDBColumn;
    VQDNLMoney: TcxGridDBColumn;
    v2QDNote: TcxGridDBColumn;
    v2QDChuanQiDate: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    v2QDHYMoney: TcxGridDBColumn;
    v2InvoiceNo: TcxGridDBColumn;
    cxTabControl1: TcxTabControl;
    TPrint: TToolButton;
    ADOQueryMain: TADOQuery;
    RMDB_Main: TRMDBDataSet;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    RM1: TRMGridReport;
    cxGridPopupMenu2: TcxGridPopupMenu;
    CDS_HZ: TClientDataSet;
    DS_HZ: TDataSource;
    Order_Print: TClientDataSet;
    v2QDChkStatus: TcxGridDBColumn;
    ADOQueryTMP: TADOQuery;
    ADOQueryCMD: TADOQuery;
    procedure TBRafreshClick(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormDestroy(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure QDOrderNoChange(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure TPrintClick(Sender: TObject);
  private
    procedure InitGrid();
    procedure Setstatus();
    { Private declarations }
  public
    { Public declarations }
  end;

var
  FrmYFCRWTS_HZQD_CX: TFrmYFCRWTS_HZQD_CX;

implementation
uses U_Fun,U_DataLink,U_ZDYHelpSel;

{$R *.dfm}
procedure TFrmYFCRWTS_HZQD_CX.setstatus();
begin
  ToolButton5.Visible:=False;
  toolbutton6.Visible:=False;
  case cxtabControl1.TabIndex of
    0:begin
        ToolButton5.Visible:=True;
      end;
    1:begin
        toolbutton6.Visible:=True;
      end;
    2:begin

      end;
  end;
end;

procedure TFrmYFCRWTS_HZQD_CX.InitGrid();
begin
  with ADOQueryMain do
  begin
    close;
    sql.Clear;
    sql.Add('select * from YF_Money_CR_ZQD ');
    sql.add('where QDChuanQiDate>='''+formatdatetime('yyyy-MM-dd',Begdate.DateTime)+''' ');
    sql.add('and QDChuanQiDate<'''+formatdatetime('yyyy-MM-dd',Enddate.DateTime)+''' ');
    if cxTabControl1.TabIndex=0 then
    begin
      sql.add('and (QDChkStatus='''' or QDChkStatus=''不通过'')');
    end;
    if cxTabControl1.TabIndex=1 then
    begin
      sql.add('and QDChkStatus=''已送审''');
    end;
    open;
  end;
  SCreateCDS20(ADOQueryMain,CDS_HZ);
  SInitCDSData20(ADOQueryMain,CDS_HZ);
end;

procedure TFrmYFCRWTS_HZQD_CX.TBRafreshClick(Sender: TObject);
begin
  ToolBar1.SetFocus;
  initGrid();
end;

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

procedure TFrmYFCRWTS_HZQD_CX.FormShow(Sender: TObject);
begin
  setstatus();
  initGrid();
end;

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

procedure TFrmYFCRWTS_HZQD_CX.FormDestroy(Sender: TObject);
begin
  FrmYFCRWTS_HZQD:=nil;
end;

procedure TFrmYFCRWTS_HZQD_CX.ToolButton5Click(Sender: TObject);
var maxno: string;
begin
  if CDS_HZ.IsEmpty then exit;
  if CDS_HZ.Locate('Ssel',true,[])=False then
  begin
    application.MessageBox('没有选择的数据','提示');
    exit;
  end;
  with CDS_HZ do
  begin
    first;
    while not eof do
    begin
      if fieldbyname('Ssel').AsBoolean=True then
      begin
        if CDS_HZ.FieldByName('QDNote').AsString='' then
        begin
          application.MessageBox('备注不能为空','提示');
          exit;
        end;
        with ADOQueryMain do
        begin
          close;
          sql.Clear;
          sql.Add('select * from YF_Money_CR ');
          sql.add('where OrderNo='''+trim(CDS_HZ.fieldbyname('OrderNo').asstring)+'''');
          sql.add('and ChkStatus<>''审核通过'' ');
          open;
        end;
        if ADOQueryMain.IsEmpty=False then
        begin
          application.MessageBox('合同号还有一部分未审核通过','提示');
          exit;
        end;
        frmZDYHelpSel:=TfrmZDYHelpSel.Create(Application);
        with frmZDYHelpSel do
        begin
          flag:='OrdConChkerYFK';
          flagname:='总清单审核人';
          MainType:=Trim(DName);
          if ShowModal=1 then
          begin
            with ClientDataSet1 do
            begin
              First;
              while not Eof do
              begin
                if FieldByName('SSEl').AsBoolean=True then
                begin
                  with ADOQueryTmp do
                  begin
                    Close;
                    sql.Clear;
                    sql.Add('select * from SY_User where UserName='''+Trim(ClientDataSet1.fieldbyname('ZdyName').AsString)+'''');
                    Open;
                  end;
                  if ADOQueryTmp.IsEmpty then
                  begin
                    Application.MessageBox('审核人定义错误!','提示',0);
                    Exit;
                  end;
                  with ADOQuerycmd do
                  begin
                    close;
                    sql.Clear;
                    sql.Add('select * from OrdConDanZH_Chk where MainID='''+trim(CDS_HZ.fieldbyname('QDID').AsString)+'''');
                    sql.add('and Chker='''+trim(ClientDataSet1.fieldbyname('ZdyName').AsString)+'''');
                    open;
                  end;
                  if ADOQuerycmd.IsEmpty then
                  begin
                    if GetLSNo(ADOQueryTmp,maxno,'ZQD','OrdConDanZH_Chk',4,1)=False then
                    begin
                      Application.MessageBox('取最大号失败!','提示',0);
                      Exit;
                    end;
                  end;
                  if ADOQueryCmd.IsEmpty then
                  begin
                    with ADOQueryCmd do
                    begin
                      append;
                      fieldbyname('CKID').Value:=Trim(maxno);
                      fieldbyname('Chker').value:=trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
                      fieldbyname('Mainid').Value:=trim(CDS_HZ.fieldbyname('QDID').AsString);
                      post;
                    end;
                  end;
                end;
                Next;
              end;
            end;
          end;
          free;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add(' Update YF_Money_CR_ZQD Set QDChkstatus=''已送审'',QDchktime=getdate(),QDChker='''+Trim(DName)+''',');
          sql.add(' QDNote='''+trim(CDS_HZ.fieldbyname('QDNote').AsString)+'''');
          sql.add('  where QDID='''+Trim(CDS_HZ.fieldbyname('QDID').AsString)+'''');
          ExecSQL;
        end;
      end;
      next;
    end;
  end;
  application.MessageBox('送审成功','提示');
  initgrid();
end;

procedure TFrmYFCRWTS_HZQD_CX.ToolButton6Click(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then exit;
  if CDS_HZ.Locate('Ssel',true,[])=False then
  begin
    application.MessageBox('没有选择的数据','提示');
    exit;
  end;
  with CDS_HZ do
  begin
    first;
    while not eof do
    begin
      if CDS_HZ.fieldbyname('Ssel').AsBoolean=True then
      begin
        if CDS_HZ.FieldByName('QDChkStatus').AsString<>'已送审' then
        begin
          application.MessageBox('单据审核不能撤销','提示');
          exit;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add(' Update YF_Money_CR_ZQD Set QDChkstatus='''',QDchktime=null,QDChker=''''');
          sql.add('  where QDID='''+Trim(CDS_HZ.fieldbyname('QDID').AsString)+'''');
          ExecSQL;
        end;
        with ADOQueryCmd do
        begin
          close;
          sql.Clear;
          sql.Add('delete from OrdConDanZH_Chk ');
          sql.add('where Mainid='''+trim(CDS_HZ.fieldbyname('QDID').AsString)+'''');
          execsql;
        end;
      end;
      next;
    end;
  end;
  application.MessageBox('撤销成功','提示');
  initgrid();
end;

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

procedure TFrmYFCRWTS_HZQD_CX.QDOrderNoChange(Sender: TObject);
begin
  ToolBar1.SetFocus;
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
    SCreateCDS20(ADOQueryMain,CDS_HZ);
    SInitCDSData20(ADOQueryMain,CDS_HZ);
  end;
end;

procedure TFrmYFCRWTS_HZQD_CX.ToolButton1Click(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then Exit;
  SelExportData(Tv2,ADOQueryMain,'总清单列表');
end;

procedure TFrmYFCRWTS_HZQD_CX.TPrintClick(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then exit;
  with ADOQueryCmd do
  begin
    close;
    sql.Clear;
    sql.Add(' select * ');
    SQL.Add(' from YF_Money_CR_ZQD ');
    sql.add(' where QDID='''+trim(CDS_HZ.fieldbyname('QDID').asstring)+''' ');
    open;
  end;
  SCreateCDS20(ADOQueryCmd,Order_Print);
  SInitCDSData20(ADOQueryCmd,Order_Print);
  fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\运费总清单.rmf';
  if FileExists(fPrintFile) then
  begin
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end else
  begin
    Application.MessageBox('没有找到报表','提示',0);
    exit;
  end;
end;

end.
