unit U_FHSQListSel;

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;

type
  TfrmFHSQListSel = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: 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;
    v1ConNo: TcxGridDBColumn;
    v1MPRTCF: TcxGridDBColumn;
    Order_Main: TClientDataSet;
    RMDBDataSet1: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    v1CustomerNoName: TcxGridDBColumn;
    v1PRTOrderQty: TcxGridDBColumn;
    ToolButton1: TToolButton;
    ADOQueryPrint: TADOQuery;
    CDS_Print: TClientDataSet;
    v1Column4: TcxGridDBColumn;
    Panel1: TPanel;
    Label1: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    Label12: TLabel;
    Label13: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    CustomerNoName: TEdit;
    ConPerson1: TEdit;
    MPRTCodeName: TEdit;
    ConNo: TEdit;
    MPRTKZ: TEdit;
    MPRTMF: TEdit;
    cxTabControl1: TcxTabControl;
    cxSplitter1: TcxSplitter;
    ADOQuerySub: TADOQuery;
    DataSource2: TDataSource;
    v1Column8: TcxGridDBColumn;
    Label2: TLabel;
    //RMllPDFExport1: TRMllPDFExport;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    v1Column5: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    ClientDataSet2: TClientDataSet;
    cxGridPopupMenu2: TcxGridPopupMenu;
    CDS_Note: TClientDataSet;
    v2Column2: TcxGridDBColumn;
    RM1: TRMGridReport;
    v1Column3: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    Label7: TLabel;
    Label10: TLabel;
    PrtCode: TEdit;
    PrtCodeName: TEdit;
    v1Column14: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    v1Column16: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v1Column17: TcxGridDBColumn;
    v1Column18: TcxGridDBColumn;
    v1Column19: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v2Column3: TcxGridDBColumn;
    v2Column4: TcxGridDBColumn;
    v2Column5: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v2Column7: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    v1Column20: TcxGridDBColumn;
    Label3: TLabel;
    SQBatchNoHZ: TEdit;
    Label6: TLabel;
    SQGCCode: TEdit;
    v1Column21: TcxGridDBColumn;
    v1Column22: TcxGridDBColumn;
    ToolButton2: TToolButton;
    //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 TBPrintClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure CheckBox2Click(Sender: TObject);
    procedure CustomerNoNameChange(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure Tv1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure ToolButton2Click(Sender: TObject);
    procedure Tv1DblClick(Sender: TObject);
  private
    procedure InitGrid();
    procedure InitSubGrid();
    procedure InitForm();
    function DelData(): Boolean;
    { Private declarations }
  public
    FFInt: Integer;
    canshu1: string;
    { Public declarations }
  end;

var
  frmFHSQListSel: TfrmFHSQListSel;

implementation

uses
  U_DataLink, U_Fun, U_ZDYHelp;

{$R *.dfm}

procedure TfrmFHSQListSel.FormDestroy(Sender: TObject);
begin
  frmFHSQListSel := nil;
end;

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

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

procedure TfrmFHSQListSel.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('发货申请选择1', Tv1, '发货申请管理');
  WriteCxGrid('发货申请选择2', Tv2, '发货申请管理');
  Close;
end;

procedure TfrmFHSQListSel.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      Sql.Add(' select A.*,NoteZ=A.Note ');

      sql.Add(' from FHSQ_Main A');

      sql.Add(' where 1=1');
      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;
      sql.Add(' and (isnull(CRType,'''') in(''总公司销售'',''圆机厂销售'',''来料加工'',''半来料加工'')');
      sql.Add(' or (isnull(CRType,'''')=''对外销售'' and isnull(Chker,'''')<>''''))');

//      ShowMessage(sql.Text);

//      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,'''')<>'''' ');
//      end;
//      if Trim(canshu1)<>'高权限' then
//      begin
//        sql.Add(' and isnull(A.Filler,'''')='''+Trim(DName)+'''');
//      end;
      Open;
    end;
    SCreateCDS20(ADOQueryMain, Order_Main);
    SInitCDSData20(ADOQueryMain, Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmFHSQListSel.InitForm();
begin
//  if Self.Caption='发货申请' then
//  begin
//
//    Tnochk.Visible:=False;
//    canshu1:='';
//  end else
//  if Self.Caption='发货申请(高权限)' then
//  begin
//    tchk.Visible:=False;
//    Tnochk.Visible:=False;
//    canshu1:='高权限';
//  end  else
//  if Self.Caption='发货申请审核' then
//  begin
//    canshu1:='高权限';
//  end;
  ReadCxGrid('发货申请选择1', Tv1, '发货申请管理');
  ReadCxGrid('发货申请选择2', Tv2, '发货申请管理');

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

end;

procedure TfrmFHSQListSel.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;

function TfrmFHSQListSel.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 TfrmFHSQListSel.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;
  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['ConNo'] := Order_Main.fieldbyname('ConNo').Value;
    RMVariables['PrtCode'] := Order_Main.fieldbyname('PrtCode').Value;
    RMVariables['prtmf'] := Order_Main.fieldbyname('prtmf').Value;
    RMVariables['prtkz'] := Order_Main.fieldbyname('prtkz').Value;
    RMVariables['PrtCodeName'] := Order_Main.fieldbyname('PrtCodeName').Value;
    RMVariables['KHName'] := Order_Main.fieldbyname('CustomerNoName').Value;
    RMVariables['SQPSHZ'] := Order_Main.fieldbyname('SQPSHZ').Value;
    RMVariables['ClothType'] := Order_Main.fieldbyname('ClothType').Value;
    RMVariables['SeBie'] := Order_Main.fieldbyname('SeBie').Value;
    RMVariables['NoteZ'] := Trim(Order_Main.fieldbyname('NoteZ').AsString);
    RMVariables['QRBARCODE'] := fImagePath;
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找' + ExtractFilePath(Application.ExeName) + 'Report\发货申请单.rmf'), '提示', 0);
  end;
end;

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

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

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

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

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

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

procedure TfrmFHSQListSel.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('select A.*,B.BatchNoHZ,C.GCCode from FHSQ_Sub A ');
      sql.Add(' inner join CLoth_Sub B on A.SubId=B.SubId');
      sql.Add(' inner join CLoth_Main C on B.MainId=C.MainId');
      sql.Add(' where A.FSId=''' + Trim(Order_Main.fieldbyname('FSId').AsString) + '''');
    end
    else
    begin
      sql.Add('select A.*,B.BatchNoHZ,C.GCCode from FHSQ_Sub A ');
      sql.Add(' inner join CLoth_Sub B on A.SubId=B.SubId');
      sql.Add(' inner join CLoth_Main C on B.MainId=C.MainId');
      sql.Add('  where 1=2');
    end;

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

procedure TfrmFHSQListSel.ToolButton2Click(Sender: TObject);
begin
  ModalResult := 1;
end;

procedure TfrmFHSQListSel.Tv1DblClick(Sender: TObject);
begin
  ModalResult := 1;
end;

end.

