unit U_CPFHSQList;

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, IdBaseComponent, IdComponent,
  IdTCPConnection, IdTCPClient, IdHTTP, cxLookAndFeels, cxLookAndFeelPainters,
  cxNavigator, dxBarBuiltInMenu, 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
  TfrmCPFHSQList = 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;
    v1OrdDate: TcxGridDBColumn;
    v1OrdPerson1: TcxGridDBColumn;
    Order_Main: TClientDataSet;
    RMDB_Main: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    ToolButton1: TToolButton;
    ADOQueryPrint: TADOQuery;
    CDS_PM: TClientDataSet;
    v1Column4: TcxGridDBColumn;
    Panel1: TPanel;
    Label1: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    Label12: TLabel;
    Label13: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    SHDanWei: TEdit;
    Filler: TEdit;
    MPRTCodeName: TEdit;
    OrderNo: TEdit;
    MPRTKZ: TEdit;
    MPRTMF: TEdit;
    cxTabControl1: TcxTabControl;
    tchk: TToolButton;
    Tnochk: TToolButton;
    cxSplitter1: TcxSplitter;
    ADOQuerySub: TADOQuery;
    DataSource2: TDataSource;
    v1Column8: TcxGridDBColumn;
    Label2: TLabel;
    ClientDataSet2: TClientDataSet;
    cxGridPopupMenu2: TcxGridPopupMenu;
    CDS_Note: TClientDataSet;
    RM1: TRMGridReport;
    v1Column11: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v1Column17: TcxGridDBColumn;
    v1Column18: TcxGridDBColumn;
    v1Column19: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column21: TcxGridDBColumn;
    v1Column24: TcxGridDBColumn;
    v1Column25: TcxGridDBColumn;
    CDSUser: TClientDataSet;
    IdHTTP1: TIdHTTP;
    v1Column3: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    ToolButton2: TToolButton;
    RMDB_XZC: TRMDBDataSet;
    CDS_PS: TClientDataSet;
    v1Column9: TcxGridDBColumn;
    ToolButton3: TToolButton;
    v1Column12: TcxGridDBColumn;
    ToolButton4: TToolButton;
    v1Column14: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    v1Column16: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    v2Column5: TcxGridDBColumn;
    v2Column12: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v2Column7: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v2Column2: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v2Column3: TcxGridDBColumn;
    v2Column10: TcxGridDBColumn;
    v2Column4: TcxGridDBColumn;
    v2Column8: TcxGridDBColumn;
    v2Column9: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    v1Column20: TcxGridDBColumn;
    Label3: TLabel;
    FHYWY: TEdit;
    v1Column22: TcxGridDBColumn;
    CDS_ZD: TClientDataSet;
    RMDB_ZD: TRMDBDataSet;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBFindClick(Sender: TObject);
    procedure TBEditClick(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure TBPrintClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure TBAddClick(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure CheckBox2Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure SHDanWeiChange(Sender: TObject);
    procedure tchkClick(Sender: TObject);
    procedure TnochkClick(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure Tv1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
  private
    procedure InitGrid();
    procedure InitSubGrid();
    procedure InitForm();
    procedure SetStatus();
    function DelData(): Boolean; 
    { Private declarations }
  public
    FFInt: Integer;
    canshu1: string;
    { Public declarations }
  end;

implementation

uses
  U_DataLink, U_CPFHSQInPut, U_Fun, U_ZDYHelp, U_CPFHSQXZ, U_LabelList, U_RTFun;

{$R *.dfm}
procedure TfrmCPFHSQList.SetStatus();
begin
  tchk.Visible := False;
  Tnochk.Visible := False;
  TBDel.Visible := False;
  TBEdit.Visible := False;
  TBAdd.Visible := False;

  if canshu1 = '录入' then
  begin
    TBAdd.Visible := true;
    case cxTabControl1.TabIndex of
      0:
        begin
          TBEdit.Visible := true;
          TBDel.Visible := true;
        end;
      1:
        begin

        end;
      2:
        begin

        end;
      3:
        begin

        end;
    end;
  end
  else if canshu1 = '审核' then
  begin
    TBAdd.Visible := true;
    case cxTabControl1.TabIndex of
      0:
        begin
          tchk.Visible := true;
          TBEdit.Visible := true;
          TBDel.Visible := true;
        end;
      1:
        begin
          Tnochk.Visible := true;
        end;
      2:
        begin

        end;
      3:
        begin

        end;
    end;
  end;
end;

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

procedure TfrmCPFHSQList.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      Sql.Add(' select A.*');
      sql.Add(' from FHSQ_Main_CP A');
      sql.Add(' where SQType=''成品布''');
      if cxTabControl1.TabIndex <> 0 then
      begin
        sql.Add(' and filltime>=''' + Trim(FormatDateTime('yyyy-MM-dd', BegDate.Date)) + '''');
        sql.Add(' and filltime<''' + Trim(FormatDateTime('yyyy-MM-dd', EndDate.Date + 1)) + '''');
      end;

      case cxTabControl1.TabIndex of
        0:
          begin
            SQL.Add(' and isnull(A.Chker,'''')='''' ');
          end;
        1:
          begin
            SQL.Add(' and isnull(A.Chker,'''')<>'''' ');
            sql.Add('  and ISNULL(Status,'''') not in (''已发货'',''已确认'') ');
          end;
        2:
          begin
            SQL.Add(' and isnull(A.Chker,'''')<>'''' ');
            sql.Add(' and DBPSHZ>0 and Status=''已确认'' ');
          end;
        3:
          begin
            SQL.Add(' and isnull(A.Chker,'''')<>'''' ');
            sql.Add(' and  DBPSHZ>0 and Status=''已发货''   ');
            SQL.Add(' and CwChkStatus<>''审核通过'' ');
          end;
        4:
          begin
            SQL.Add(' and CwChkStatus=''审核通过'' ');
          end;
      end;

      if Trim(canshu1) = '录入' then
      begin
        sql.Add(' and isnull(A.Filler,'''')=''' + Trim(DName) + '''');
      end;
//      showmessage(sql.text);
      Open;
    end;
    SCreateCDS20(ADOQueryMain, Order_Main);
    SInitCDSData20(ADOQueryMain, Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmCPFHSQList.InitForm();
begin

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

end;

procedure TfrmCPFHSQList.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 TfrmCPFHSQList.TBEditClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;
  if cxTabControl1.TabIndex <> 0 then
    Exit;
  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_CP');
//    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
    frmPBFHSQInPut := TfrmPBFHSQInPut.Create(Application);
    with frmPBFHSQInPut do
    begin
      PState := 1;
      FMainId := Trim(Self.Order_Main.fieldbyname('FSID').AsString);

      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmPBFHSQInPut.Free;
  end;
end;

procedure TfrmCPFHSQList.TBDelClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;
  if cxTabControl1.TabIndex <> 0 then
    Exit;
  if Trim(Order_Main.fieldbyname('Filler').AsString) <> Trim(DName) then
  begin
    Application.MessageBox('不能操作他人的数据!', '提示', 0);
    Exit;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from FHSQ_Sub_CP where FSId=''' + Trim(Order_Main.fieldbyname('FSId').AsString) + '''');
    sql.add(' and isnull(FHPS,0)>0');
    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_Sub_CP where FSId=''' + Trim(Order_Main.fieldbyname('FSId').AsString) + '''');
    sql.add(' and isnull(DBPS,0)>0');
    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_CP');
//    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 Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  if DelData() then
  begin
    Order_Main.Delete;
    InitSubGrid();
  end;
end;

function TfrmCPFHSQList.DelData(): Boolean;
begin
  try
    Result := false;
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete FHSQ_Sub_CP where FSId=''' + Trim(Order_Main.fieldbyname('FSId').AsString) + '''');
      ExecSQL;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete FHSQ_Main_CP 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 TfrmCPFHSQList.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;
  with ADOQueryPrint do
  begin
    Close;
    sql.Clear;
    Sql.Add(' select A.*,B.*');
    sql.Add(' from FHSQ_Main_CP A inner join FHSQ_Sub_CP B on A.FSId=B.FSId ');
    sql.Add(' where A.FSId=''' + Trim(Order_Main.fieldbyname('FSId').AsString) + '''');
    open;
//    showmessage(sql.text);
  end;
  SCreateCDS20(ADOQueryPrint, CDS_PM);          
  SInitCDSData20(ADOQueryPrint, CDS_PM);

  with ADOQueryPrint do
  begin
    Close;
    sql.Clear;
    Sql.Add(' exec  P_View_ZDLog_CPFHSQ ');
    sql.Add(' @FSID=''' + Trim(Order_Main.fieldbyname('FSId').AsString) + '''');
    open;
//    showmessage(sql.text);
  end;
  SCreateCDS20(ADOQueryPrint, CDS_ZD);
  SInitCDSData20(ADOQueryPrint, CDS_ZD);

  with ADOQueryPrint do
  begin
    Close;
    sql.Clear;
    Sql.Add(' select  A.* FROM FHSQ_Sub_CP A   ');
    sql.Add(' where  FSId=''限制池'' ');
    sql.Add(' and exists( select PrtCodeName from FHSQ_Sub_CP X where X.PrtCodeName=A.PrtCodeName and X.FSId=' + quotedstr(Trim(Order_Main.fieldbyname('FSId').AsString)) + ') ');
    open;
//    showmessage(sql.text);
  end;
  SCreateCDS20(ADOQueryPrint, CDS_PS);
  SInitCDSData20(ADOQueryPrint, CDS_PS);

  ExportFtErpFile('成品发货申请单.rmf', ADOQueryTemp);
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\成品发货申请单.rmf';
  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('没有找' + ExtractFilePath(Application.ExeName) + 'Report\坯布发货申请单.rmf'), '提示', 0);
  end;
end;

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

procedure TfrmCPFHSQList.TBAddClick(Sender: TObject);
var
  maxno: string;
begin
  try
    frmPBFHSQInPut := TfrmPBFHSQInPut.Create(Application);
    with frmPBFHSQInPut do
    begin
      PState := 0;
      FMainId := '';

      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmPBFHSQInPut.Free;
  end;
  InitGrid();
end;

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

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

procedure TfrmCPFHSQList.ToolButton1Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;
  try
    frmPBFHSQInPut := TfrmPBFHSQInPut.Create(Application);
    with frmPBFHSQInPut 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
    frmPBFHSQInPut.Free;
  end;
end;

procedure TfrmCPFHSQList.SHDanWeiChange(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 TfrmCPFHSQList.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_CP 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 TfrmCPFHSQList.InitSubGrid();
begin
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    if Order_Main.IsEmpty = False then
      sql.Add('select * from FHSQ_Sub_CP A where A.FSId=''' + Trim(Order_Main.fieldbyname('FSId').AsString) + '''')
    else
      sql.Add('select * from FHSQ_Sub_CP A where 1=2');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp, ClientDataSet2);
  SInitCDSData20(ADOQueryTemp, ClientDataSet2);
end;

procedure TfrmCPFHSQList.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_CP');
    sql.Add('where CwChkStatus=''审核通过'' and FSID=' + quotedstr(trim(Order_Main.fieldbyname('FSID').AsString)));
    Open;
  end;
  if not ADOQueryTemp.IsEmpty then
  begin
    Application.MessageBox('财务审核通过不能撤销!', '提示', 0);
    Exit;
  end;
  try

    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('update  FHSQ_Main_CP 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 TfrmCPFHSQList.cxTabControl1Change(Sender: TObject);
begin
  SetStatus();
  InitGrid();
  InitSubGrid();
end;

procedure TfrmCPFHSQList.Tv1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    if Order_Main.IsEmpty = False then
    begin
      sql.Add('EXEC P_DB_SQSub ''' + Trim(Order_Main.fieldbyname('FSId').AsString) + '''');
    end
    else
    begin
      sql.Add('select A.* from FHSQ_Sub_CP A ');

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

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

procedure TfrmCPFHSQList.ToolButton2Click(Sender: TObject);
begin
  try
    frmCPFHSQXZ := TfrmCPFHSQXZ.Create(Application);
    with frmCPFHSQXZ do
    begin
      FMainId := '限制池';

      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmCPFHSQXZ.Free;
  end;
end;

procedure TfrmCPFHSQList.ToolButton3Click(Sender: TObject);
var
  fPrintFile: string;
  Txt, fImagePath: string;
  Moudle: THandle;
  Makebar: TMakebar;
  Mixtext: TMixtext;
begin
  if Order_Main.IsEmpty then
    exit;
  if trim(Order_Main.fieldbyname('LBName').AsString) = '' then
    exit;
  with ADOQueryPrint do
  begin
    Close;
    SQL.Clear;
    sql.Add('exec P_Print_Cs_Roll ');
    sql.Add('@mainID=''' + trim(Order_Main.fieldbyname('mainID').AsString) + ''' ');
    Open;
  end;
  try
    Moudle := LoadLibrary('MakeQRBarcode.dll');
    @Makebar := GetProcAddress(Moudle, 'Make');
    @Mixtext := GetProcAddress(Moudle, 'MixText');
    Txt := Trim(ADOQueryPrint.fieldbyname('MJID').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;

  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\' + Trim(Order_Main.fieldbyname('LBName').AsString) + '.rmf';
  if FileExists(fPrintFile) then
  begin
    RMVariables['QRBARCODE'] := fImagePath;
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找到' + trim(fPrintFile)), '提示信息', 0);
  end;
end;

procedure TfrmCPFHSQList.ToolButton4Click(Sender: TObject);
var
  maxno: string;
begin
  try
    frmLabelList := TfrmLabelList.Create(Application);
    with frmLabelList do
    begin
      if ShowModal = 1 then
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update FHSQ_Main_CP Set BaoLab=''' + Trim(ADOQueryLabel.fieldbyname('labelCaption').AsString) + '''');
          sql.Add(' where FSId=''' + Trim(self.Order_Main.fieldbyname('FSId').AsString) + '''');
          ExecSQL;
        end;
//        TBRafresh.Click;
        with self.Order_Main do
        begin
          Edit;
          FieldByName('BaoLab').Value := Trim(ADOQueryLabel.fieldbyname('labelCaption').AsString);
        end;
      end;
    end;
  finally
    frmLabelList.Free;
  end;
end;

procedure TfrmCPFHSQList.TBCloseClick(Sender: TObject);
begin
  if Trim(DName) = 'ADMIN' then
  begin
    WriteCxGridAll(trim(self.Caption), Tv1, ADOQueryCmd, ADOQueryTemp);
    WriteCxGridAll(trim(self.Caption) + '2', Tv2, ADOQueryCmd, ADOQueryTemp);
  end;

  Close;
end;

procedure TfrmCPFHSQList.FormShow(Sender: TObject);
begin

  ReadCxGridAll(trim(self.Caption), Tv1);
  ReadCxGridAll(trim(self.Caption) + '2', Tv2);
  InitForm();
end;

end.

