unit U_FHSQList;

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, cxCheckBox, cxCalendar, cxSplitter, RM_Dataset, RM_System,
  RM_Common, RM_Class, RM_GridReport, RM_e_Xls, Menus, cxPC, cxButtonEdit,
  RM_E_llPDF, cxDropDownEdit, cxTextEdit, cxLookAndFeels, cxLookAndFeelPainters,
  cxNavigator, dxBarBuiltInMenu, dxSkinsCore, dxSkinsDefaultPainters,
  dxSkinscxPCPainter;

type
  TfrmFHSQList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBAdd: TToolButton;
    TBEdit: TToolButton;
    TBDel: TToolButton;
    TBPrint: TToolButton;
    TBClose: TToolButton;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    TBExport: TToolButton;
    v1OrdDate: TcxGridDBColumn;
    Order_Main: TClientDataSet;
    RMDBDataSet1: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    v1CustomerNoName: TcxGridDBColumn;
    ToolButton1: TToolButton;
    ADOQueryPrint: TADOQuery;
    CDS_Print: TClientDataSet;
    v1Column4: TcxGridDBColumn;
    Panel1: TPanel;
    Label1: TLabel;
    Label4: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    Label12: TLabel;
    Label13: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    CustomerNoName: TEdit;
    MPRTCodeName: TEdit;
    HZSCConNo: TEdit;
    MPRTKZ: TEdit;
    MPRTMF: TEdit;
    cxTabControl1: TcxTabControl;
    tchk: TToolButton;
    Tnochk: TToolButton;
    cxSplitter1: TcxSplitter;
    ADOQuerySub: TADOQuery;
    DataSource2: TDataSource;
    v1Column8: TcxGridDBColumn;
    Label2: TLabel;
    //RMllPDFExport1: TRMllPDFExport;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxGridLevel1: TcxGridLevel;
    ClientDataSet2: TClientDataSet;
    cxGridPopupMenu2: TcxGridPopupMenu;
    CDS_Note: TClientDataSet;
    RM1: TRMGridReport;
    v1Column13: TcxGridDBColumn;
    v1Column17: TcxGridDBColumn;
    v1Column18: TcxGridDBColumn;
    v1Column19: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v2Column3: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v1Column26: TcxGridDBColumn;
    ToolButton2: TToolButton;
    Panel5: TPanel;
    Label28: TLabel;
    Button5: TButton;
    Button6: TButton;
    DateTimePicker1: TDateTimePicker;
    Label11: TLabel;
    lblFSID: TLabel;
    v1Column27: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    cv2Column4: TcxGridDBColumn;
    cv2Column6: TcxGridDBColumn;
    btn1: TToolButton;
    cv1Column12: TcxGridDBColumn;
    cv2Column10: TcxGridDBColumn;
    cv2Column12: TcxGridDBColumn;
    cv1Column14: TcxGridDBColumn;
    v2Column2: TcxGridDBColumn;
    v2Column4: TcxGridDBColumn;
    v2Column5: TcxGridDBColumn;
    v2Column7: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v2Column8: TcxGridDBColumn;
    v2Column9: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    Tv2Column1: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    Tv2Column2: TcxGridDBColumn;
    Tv2Column3: TcxGridDBColumn;
    Label3: TLabel;
    SQKHKuanHaoHZ: TEdit;
    Tv2Column4: TcxGridDBColumn;
    Tv2Column5: TcxGridDBColumn;
    ToolButton3: TToolButton;
    Tv1Column2: TcxGridDBColumn;
    ISHT: TCheckBox;
    ToolButton4: TToolButton;
    Tv1Column3: TcxGridDBColumn;
    Tv2Column6: TcxGridDBColumn;
    //RMllPDFExport1: TRMllPDFExport;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure TBEditClick(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBPrintClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure TBAddClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure CheckBox2Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure CustomerNoNameChange(Sender: TObject);
    procedure tchkClick(Sender: TObject);
    procedure TnochkClick(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure btn1Click(Sender: TObject);
    procedure Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
    procedure ToolButton3Click(Sender: TObject);
    procedure ISHTClick(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
  private
    procedure InitGrid();
    procedure InitSubGrid();
    procedure InitForm();
    procedure SetStatus();
    function DelData(): Boolean;
    { Private declarations }
  public
    FFInt: Integer;
    canshu1, canshu2: string;
    { Public declarations }
  end;

//var
  //frmContractListNX: TfrmContractListNX;

implementation

uses
  U_DataLink, U_FHSQInPut, U_Fun, U_ZDYHelp, U_DCDList_Sel;

{$R *.dfm}

procedure TfrmFHSQList.SetStatus();
begin
  tchk.Visible := False;
  Tnochk.Visible := False;
  btn1.Visible := false;
  ToolButton3.Visible := false;

  if canshu1 = '审核' then
  begin
    case cxTabControl1.TabIndex of
      0:
        begin
          tchk.Visible := true;

        end;
      1:
        begin
          Tnochk.Visible := true;

        end;
      2:
        begin

        end;
    end;
  end
  else if canshu1 = '高权限' then
  begin
    case cxTabControl1.TabIndex of
      0:
        begin
          tchk.Visible := true;

        end;
      1:
        begin
          Tnochk.Visible := true;
          btn1.Visible := True;
        end;
      2:
        begin
          ToolButton3.Visible := True;
        end;
    end;
  end
  else
  begin
  end;
end;

procedure TfrmFHSQList.FormDestroy(Sender: TObject);
begin
  //frmFHSQList:=nil;
end;

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

procedure TfrmFHSQList.FormCreate(Sender: TObject);
begin
  cxgrid1.Align := alClient;
end;

procedure TfrmFHSQList.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('发货主表ZY', Tv1, '发货管理');
  WriteCxGrid('发货子表ZY', Tv2, '发货管理');
end;

procedure TfrmFHSQList.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      Sql.Add(' select A.*,isnull(status,''未完成'')status2 ');

      Sql.Add(',SQKHKuanHaoHZ =CAST((select  distinct(SQKHKuanHao) +  ''  '' from FHSQ_Sub X where A.FSID=X.FSID for xml path('''') ) AS VARCHAR(100))');

      Sql.Add(' ,HZSCConNo=CAST((select  distinct(SCConNo) +  ''  '' from FHSQ_Sub X where A.FSID=X.FSID for xml path('''') ) AS VARCHAR(100))');
      Sql.Add(' ,HZSQMoney= (select sum(SQMoney) from FHSQ_Sub X where A.FSID=X.FSID   ) ');
      sql.Add(' from FHSQ_Main A');
      sql.Add(' where SQType=''订单'' ');
      if cxTabControl1.TabIndex <> 0 then
      begin
        sql.Add(' and SQDate>=''' + Trim(FormatDateTime('yyyy-MM-dd', BegDate.Date)) + '''');
        sql.Add(' and SQDate<''' + Trim(FormatDateTime('yyyy-MM-dd', EndDate.Date + 1)) + '''');
      end;
      if cxTabControl1.TabIndex = 0 then
      begin
        SQL.Add(' and isnull(A.Chker,'''')='''' ');
      end
      else if cxTabControl1.TabIndex = 1 then
      begin
        SQL.Add(' and isnull(A.Chker,'''')<>'''' AND ISNULL(status,''未完成'')=''未完成'' ');
      end
      else if cxTabControl1.TabIndex = 2 then
      begin
        SQL.Add(' and isnull(A.Chker,'''')<>'''' AND ISNULL(status,''未完成'')<>''未完成'' ');
      end;
      if Trim(canshu1) <> '高权限' then
      begin
       // sql.Add(' and isnull(A.Filler,'''')=''' + Trim(DName) + '''');
        SQL.Add('and   filler in ( select username from SY_User where UType=(select UType from SY_User where UserName=' + quotedstr(Trim(dname)));
        SQL.Add('))');
      end;
      if (canshu2 = '内销') or (canshu2 = '外销') then
      begin
        sql.Add(' and isnull(A.XSType,'''')=''' + Trim(canshu2) + '''');
      end;
      if ISHT.Checked = True then
      begin
        SQL.Add('AND EXISTS (select * from SalesContract_Main X where X.CONNO IN (SELECT SCCONNO FROM FHSQ_SUB F WHERE F.FSID=A.FSID))');
      end;
      sql.Add(' order by  ChkTime desc ');
//      ShowMessage(sql.Text);
      Open;
    end;
    SCreateCDS20(ADOQueryMain, Order_Main);
    SInitCDSData20(ADOQueryMain, Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmFHSQList.InitForm();
begin

  ReadCxGrid('发货主表ZY', Tv1, '发货管理');
  ReadCxGrid('发货子表ZY', Tv2, '发货管理');

  EndDate.DateTime := SGetServerDate10(ADOQueryTemp);
  BegDate.DateTime := EndDate.DateTime - 60;
  InitGrid();

end;

procedure TfrmFHSQList.TBFindClick(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 TfrmFHSQList.TBEditClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;
//  if cxTabControl1.TabIndex <> 0 then
//    Exit;
  if Trim(canshu1) <> '高权限' then
  begin

    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      SQL.Add('select FILLER from FHSQ_MAIN A  ');
      SQL.Add('where FSID=''' + Trim(Order_Main.fieldbyname('FSID').AsString) + ''' ');
      SQL.Add('and   filler in ( select username from SY_User where UType=(select UType from SY_User where UserName=' + quotedstr(Trim(dname)));
      SQL.Add('))');
      Open;
    end;

    if ADOQueryTemp.ISEMPTY = True then
    begin
      Application.MessageBox('不能操作他人的数据!', '提示', 0);
      Exit;
    end;
//    if Trim(Order_Main.fieldbyname('Filler').AsString) <> Trim(DName) then
//    begin
//      Application.MessageBox('不能操作他人的数据!', '提示', 0);
//      Exit;
//    end;
  end;
  with ADOQueryTemp do
  begin
    close;
    sql.Clear;
    sql.Add('SELECT * FROM YF_MONEY_CR   ');
    sql.Add('where YFID=' + quotedstr(trim('Y' + Order_Main.fieldbyname('FSID').AsString)));
    SQL.Add('AND STATUS=1');
    Open;
  end;
  if ADOQueryTemp.IsEmpty = False then
  begin
    application.MessageBox('财务已审核，禁止撤销！', '提示信息');
    Exit;
  end;
//  with ADOQueryTemp do
//  begin
//    close;
//    sql.Clear;
//    sql.Add('select * from FHSQ_Main');
//    sql.Add('where FSID=' + quotedstr(trim(Order_Main.fieldbyname('FSID').AsString)));
//    Open;
//  end;
//  if Trim(ADOQueryTemp.fieldbyname('YSID').AsString) <> '' then
//  begin
//    Application.MessageBox('已生成应收账款不能修改数据!', '提示', 0);
//    Exit;
//  end;
  try
    frmFHSQInPut := TfrmFHSQInPut.Create(Application);
    with frmFHSQInPut do
    begin
      PState := 1;
      FMainId := Trim(Self.Order_Main.fieldbyname('FSID').AsString);
      if ShowModal = 1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmFHSQInPut.Free;
  end;
end;

procedure TfrmFHSQList.TBDelClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;
  if cxTabControl1.TabIndex <> 0 then
    Exit;
  if canshu1 <> '高权限' then
  begin
    if Trim(Order_Main.fieldbyname('Filler').AsString) <> Trim(DName) then
    begin
      Application.MessageBox('不能操作他人的数据!', '提示', 0);
      Exit;
    end;
  end;

  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add(' select * from CK_BanCP_CR where CRFlag=''出库''');
    sql.add(' and CKOrdNo=''' + Trim(Order_Main.fieldbyname('FSId').AsString) + '''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty = False then
  begin
    Application.MessageBox('已发货不能删除数据!', '提示', 0);
    exit;
  end;
  with ADOQueryTemp do
  begin
    close;
    sql.Clear;
    sql.Add('select * from FHSQ_Main');
    sql.Add('where FSID=' + quotedstr(trim(Order_Main.fieldbyname('FSID').AsString)));
    Open;
  end;
  if Trim(ADOQueryTemp.fieldbyname('YSID').AsString) <> '' then
  begin
    Application.MessageBox('已生成应收账款不能删除!', '提示', 0);
    Exit;
  end;

  if ADOQueryTemp.FieldByName('FHPSHZ').AsInteger > 0 then
  begin
    Application.MessageBox('已发货不能删除数据!', '提示', 0);
    exit;
  end;
  with ADOQueryTemp do
  begin
    close;
    sql.Clear;
    sql.Add('SELECT * FROM YF_MONEY_CR   ');
    sql.Add('where YFID=' + quotedstr(trim('Y' + Order_Main.fieldbyname('FSID').AsString)));
    SQL.Add('AND STATUS=1');
    Open;
  end;
  if ADOQueryTemp.IsEmpty = False then
  begin
    application.MessageBox('财务已审核，禁止撤销！', '提示信息');
    Exit;
  end;
  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  if DelData() then
  begin
    Order_Main.Delete;
    InitSubGrid();
  end;
end;

function TfrmFHSQList.DelData(): Boolean;
begin
  try
    Result := false;
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete FHSQ_Sub where FSId=''' + Trim(Order_Main.fieldbyname('FSId').AsString) + '''');
      ExecSQL;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete FHSQ_Main where FSId=''' + Trim(Order_Main.fieldbyname('FSId').AsString) + '''');
      ExecSQL;
    end;

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

procedure TfrmFHSQList.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    Exit;
  SelExportData(Tv1, ADOQueryMain, '发货申请列表');
end;

procedure TfrmFHSQList.TBPrintClick(Sender: TObject);
var
  fPrintFile: string;
  Txt, fImagePath: string;
  Moudle: THandle;
  Makebar: TMakebar;
  Mixtext: TMixtext;
begin
  if Order_Main.IsEmpty then
    Exit;
//  if cxTabControl1.TabIndex <> 1 then
//    Exit;
  ExportFtErpFile('坯布发货申请单.rmf', ADOQueryTemp);
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\坯布发货申请单.rmf';

  with ADOQueryPrint do
  begin
    Close;
    sql.Clear;
    Sql.Add(' select A.*,B.* ');
    sql.Add(' from FHSQ_Main A left join FHSQ_Sub B on A.FSId=B.FSId ');
    sql.Add(' where A.FSID=' + quotedstr(Trim(Order_Main.fieldbyname('FSID').AsString)));
    Open;
  end;
  if FileExists(fPrintFile) then
  begin
    try
      Moudle := LoadLibrary('MakeQRBarcode.dll');
      @Makebar := GetProcAddress(Moudle, 'Make');
      @Mixtext := GetProcAddress(Moudle, 'MixText');
      Txt := Trim(Order_Main.fieldbyname('FSID').AsString);
      fImagePath := ExtractFilePath(Application.ExeName) + 'image\temp.bmp';
      if not DirectoryExists(pchar(ExtractFilePath(Application.ExeName) + 'image')) then
        CreateDirectory(pchar(ExtractFilePath(Application.ExeName) + 'image'), nil);
      if FileExists(fImagePath) then
        DeleteFile(fImagePath);
      Makebar(pchar(Txt), Length(Txt), 3, 3, 0, PChar(fImagePath), 3);
    except
      application.MessageBox('条形码生成失败！', '提示信息', MB_ICONERROR);
      exit;
    end;
    RMVariables['QRBARCODE'] := fImagePath;
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找' + fPrintFile), '提示', 0);
  end;
end;

procedure TfrmFHSQList.TBRafreshClick(Sender: TObject);
begin
  InitGrid();
  TBFind.Click;
  InitSubGrid();
end;

procedure TfrmFHSQList.TBAddClick(Sender: TObject);
var
  maxno: string;
begin
  try
    frmFHSQInPut := TfrmFHSQInPut.Create(Application);
    with frmFHSQInPut do
    begin
      PState := 0;
      FMainId := '';
      if ShowModal = 1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmFHSQInPut.Free;
  end;
end;

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

procedure TfrmFHSQList.CheckBox1Click(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmFHSQList.CheckBox2Click(Sender: TObject);
begin
  TBRafresh.Click;
end;

procedure TfrmFHSQList.ToolButton1Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;
  try
    frmFHSQInPut := TfrmFHSQInPut.Create(Application);
    with frmFHSQInPut do
    begin
      PState := 1;
      FMainId := Trim(Self.Order_Main.fieldbyname('FSID').AsString);
      ToolBar2.Visible := False;
      TBSave.Visible := False;
      ScrollBox1.Enabled := False;
      Tv1.OptionsSelection.CellSelect := False;
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmFHSQInPut.Free;
  end;
end;

procedure TfrmFHSQList.CustomerNoNameChange(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 TfrmFHSQList.tchkClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    exit;
  if cxTabControl1.TabIndex <> 0 then
    exit;
  try
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('update  FHSQ_Main SET Chker=''' + Trim(DName) + ''',ChkTime=getdate(),ChkStatus=''审核通过'' ');
      sql.Add('where FSID=' + quotedstr(trim(Order_Main.fieldbyname('FSID').AsString)));
      execsql;
    end;
    Order_Main.Delete;
    InitSubGrid();
    application.MessageBox('审核成功！', '提示信息');
  except
    application.MessageBox('审核失败！', '提示信息', 0);
  end;
end;

procedure TfrmFHSQList.InitSubGrid();
begin
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    if Order_Main.IsEmpty = False then
      sql.Add('select A.*,B.ORDERNO,ISHT=cast((case when isnull((select count(CONNO) from SalesContract_Main X where X.CONNO=A.SCCONNO ),0)>0 then 1 else 0 end) as bit) from FHSQ_Sub A LEFT JOIN JYOrder_Main B ON A.MainId=B.MainId where A.FSId=''' + Trim(Order_Main.fieldbyname('FSId').AsString) + '''')
    else
      sql.Add('select A.*,B.ORDERNO from FHSQ_Sub A LEFT JOIN JYOrder_Main B ON A.MainId=B.MainId where 1=2');
//      ShowMessage(SQL.Text);
    Open;
  end;
  SCreateCDS20(ADOQueryTemp, ClientDataSet2);
  SInitCDSData20(ADOQueryTemp, ClientDataSet2);
end;

procedure TfrmFHSQList.TnochkClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    exit;
  if cxTabControl1.TabIndex <> 1 then
    exit;
  with ADOQueryTemp do
  begin
    close;
    sql.Clear;
    sql.Add('select * from FHSQ_Main');
    sql.Add('where FSID=' + quotedstr(trim(Order_Main.fieldbyname('FSID').AsString)));
    Open;
  end;
  if Trim(ADOQueryTemp.fieldbyname('YSID').AsString) <> '' then
  begin
    Application.MessageBox('已生成应收账款不能撤销!', '提示', 0);
    Exit;
  end;
  try

    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('update  FHSQ_Main SET Chker=Null,ChkTime=Null,ChkStatus=Null ');
      sql.Add('where FSID=' + quotedstr(trim(Order_Main.fieldbyname('FSID').AsString)));
      execsql;
    end;
    Order_Main.Delete;
    InitSubGrid();
    application.MessageBox('撤销成功！', '提示信息');
  except
    application.MessageBox('撤销失败！', '提示信息', 0);
  end;
end;

procedure TfrmFHSQList.cxTabControl1Change(Sender: TObject);
begin
  TBRafresh.Click;
  SetStatus();
end;

procedure TfrmFHSQList.ToolButton2Click(Sender: TObject);
begin
  if Trim(canshu1) <> '高权限' then
  begin
    if Trim(Order_Main.fieldbyname('Filler').AsString) <> Trim(DName) then
    begin
      Application.MessageBox('不能操作他人的数据!', '提示', 0);
      Exit;
    end;
  end;
  with ADOQueryTemp do
  begin
    close;
    sql.Clear;
    sql.Add('select * from FHSQ_Main');
    sql.Add('where FSID=' + quotedstr(trim(Order_Main.fieldbyname('FSID').AsString)));
    Open;
  end;
  if Trim(ADOQueryTemp.fieldbyname('YSID').AsString) <> '' then
  begin
    Application.MessageBox('已生成应收账款不能修改数据!', '提示', 0);
    Exit;
  end;
  lblFSID.Caption := Order_Main.fieldbyname('FSID').AsString;
  DateTimePicker1.DateTime := SGetServerDate10(ADOQueryTemp);
  Panel5.Left := (self.Width - Panel5.Width) div 2;
  Panel5.Visible := true;
end;

procedure TfrmFHSQList.Button6Click(Sender: TObject);
begin
  Panel5.Visible := false;
end;

procedure TfrmFHSQList.Button5Click(Sender: TObject);
begin
  try
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('update  FHSQ_Main SET fhDate=' + QuotedStr(Trim(FormatDateTime('yyyy-MM-dd', DateTimePicker1.Date))));
      sql.Add('where FSID=' + quotedstr(trim(lblFSID.Caption)));
      execsql;
    end;
    InitGrid();
    Order_Main.Locate('FSID', lblFSID.Caption, []);
    Panel5.Visible := false;
    application.MessageBox('修改成功！', '提示信息');
  except
    application.MessageBox('修改失败！', '提示信息', 0);
  end;
end;

procedure TfrmFHSQList.btn1Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    exit;

  try
    if Order_Main.FieldByName('crtype').AsString = '销售出库' then
    begin
      with ADOQueryTemp do
      begin
        close;
        sql.Clear;
        sql.Add('SELECT * FROM YF_MONEY_CR   ');
        sql.Add('where YFID=' + quotedstr(trim('Y' + Order_Main.fieldbyname('FSID').AsString)));
        Open;
      end;
      if ADOQueryTemp.IsEmpty = True then
      begin
        with ADOQueryCmd do
        begin
          close;
          sql.Clear;
          SQL.Add('EXEC P_Do_SCYSK @FSID=' + quotedstr(trim(Order_Main.fieldbyname('FSID').AsString)));
          SQL.Add(',@DNAME=' + QuotedStr(Trim(DName)));
          ExecSQL;
        end;
      end;
    end;

    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('update  FHSQ_Main SET status=''已完成'' ');
      sql.Add('where FSID=' + quotedstr(trim(Order_Main.fieldbyname('FSID').AsString)));
//      ShowMessage(SQL.Text);
      execsql;
    end;
    Order_Main.Delete;
    InitSubGrid();
    application.MessageBox('操作成功！', '提示信息');
  except
    application.MessageBox('操作失败！', '提示信息', 0);
  end;
end;

procedure TfrmFHSQList.Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
begin
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    if Order_Main.IsEmpty = False then
    begin
      sql.Add('select A.*, B.ORDERNO,ISHT=cast((case when isnull((select count(CONNO) from SalesContract_Main X where X.CONNO=A.SCCONNO ),0)>0 then 1 else 0 end) as bit) from FHSQ_Sub A LEFT JOIN JYOrder_Main B ON A.MainId=B.MainId ');

      sql.Add(' where A.FSId=''' + Trim(Order_Main.fieldbyname('FSId').AsString) + '''');
    end
    else
    begin
      sql.Add('select A.*, B.ORDERNO from FHSQ_Sub A LEFT JOIN JYOrder_Main B ON A.MainId=B.MainId  ');

      sql.Add('  where 1=2');
    end;

    Open;
  end;
  SCreateCDS20(ADOQueryTemp, ClientDataSet2);
  SInitCDSData20(ADOQueryTemp, ClientDataSet2);
end;

procedure TfrmFHSQList.ToolButton3Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    exit;

  try
    with ADOQueryTemp do
    begin
      close;
      sql.Clear;
      sql.Add('SELECT * FROM YF_MONEY_CR   ');
      sql.Add('where YFID=' + quotedstr(trim('Y' + Order_Main.fieldbyname('FSID').AsString)));
//      SQL.Add('AND STATUS=1');
      Open;
    end;
    if ADOQueryTemp.IsEmpty = False then
    begin
      if ADOQueryTemp.fieldbyname('status').AsString = '1' then
      begin
        application.MessageBox('财务已审核，禁止撤销！', '提示信息');
        Exit;
      end
      else
      begin

        with ADOQueryCmd do
        begin
          close;
          sql.Clear;
          sql.Add('delete   YF_MONEY_CR   ');
          sql.Add('where YFID=' + quotedstr(trim('Y' + Order_Main.fieldbyname('FSID').AsString)));

          sql.Add('delete   YF_MONEY_CR_SUB   ');
          sql.Add('where YFID=' + quotedstr(trim('Y' + Order_Main.fieldbyname('FSID').AsString)));
          ExecSQL;
        end;
      end;

    end;

    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('update  FHSQ_Main SET status=''未完成'' ');
      sql.Add('where FSID=' + quotedstr(trim(Order_Main.fieldbyname('FSID').AsString)));
//      ShowMessage(SQL.Text);
      execsql;
    end;
    Order_Main.Delete;
    InitSubGrid();
    application.MessageBox('操作成功！', '提示信息');
  except
    application.MessageBox('操作失败！', '提示信息', 0);
  end;
end;

procedure TfrmFHSQList.ISHTClick(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmFHSQList.ToolButton4Click(Sender: TObject);
var
  fDCNO: string;
begin
  try
    frmDCDList_Sel := TfrmDCDList_Sel.Create(Application);
    with frmDCDList_Sel do
    begin

      if ShowModal = 1 then
      begin
//        self.TOCOUNTRY.Text := Trim(Order_Main.fieldbyname('TOCOUNTRY').AsString);
        fDCNO := Trim(Order_Main.fieldbyname('DCNO').AsString);
      end;
    end;
  finally
    frmDCDList_Sel.Free;
  end;

  if fDCNO <> '' then
  begin
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update FHSQ_MAIN  ');
      SQL.Add(' Set DCNO=' + QuotedStr(fDCNO));
      sql.Add(' where FSID=''' + Trim(Order_Main.fieldbyname('FSID').AsString) + '''');

      sql.Add('insert into SY_SysLog(operor,opertime,Model,acction,opevent,result) values( ');
      sql.Add(' ' + quotedstr(trim(DName)));
      sql.Add(',getdate() ');
      sql.Add(',' + quotedstr(trim(self.Caption)));
      sql.Add(',' + quotedstr(trim('修改订舱单号')));
      sql.Add(',' + quotedstr(trim('单号：' + trim(Order_Main.FieldByName('fsid').AsString) + '修改舱号为：' + trim(fDCNO))));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');
      ExecSQL;
    end;

    with Order_Main do
    begin
      Edit;
//    CDS_HZ.fieldbyname('KKMONEY').value := Trim(mvalues);

      Order_Main.fieldbyname('DCNO').value := fDCNO;
    end;
    tv1.Controller.EditingController.ShowEdit();
  end;

//  if trim(mvalues) <> '' then
//  begin

end;

end.

