unit U_OrderList;

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,
  MovePanel, cxTextEdit, cxContainer, cxCurrencyEdit, BtnEdit, RM_e_main, RM_e_htm, RM_e_Graphic, RM_e_Jpeg, RM_e_txt, RM_e_csv, RM_E_llPDF, Math,
  cxPC, cxLookAndFeels, cxLookAndFeelPainters, cxNavigator;

type
  TfrmOrderList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    TBExport: TToolButton;
    Order_Main: TClientDataSet;
    Panel4: TPanel;
    Label11: TLabel;
    Panel10: TPanel;
    Image2: TImage;
    RichEdit1: TRichEdit;
    MovePanel2: TMovePanel;
    Cxgrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1OrdDate: TcxGridDBColumn;
    v1MLOrderNo: TcxGridDBColumn;
    v1MLConNo: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1MLCodeName: TcxGridDBColumn;
    v1KHName: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    Cxgrid1Level1: TcxGridLevel;
    Panel1: TPanel;
    Label1: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label9: TLabel;
    Label12: TLabel;
    Label5: TLabel;
    Label7: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    MLOrderNo: TEdit;
    KHNameHZ: TEdit;
    MLConNo: TEdit;
    MPRTKZ: TEdit;
    MLCodeName: TEdit;
    status: TComboBox;
    v1Column3: TcxGridDBColumn;
    Label2: TLabel;
    YWY: TEdit;
    ToolButton1: TToolButton;
    RMDBMain: TRMDBDataSet;
    RM1: TRMGridReport;
    ADOQueryPrint: TADOQuery;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Panel10MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
    procedure Image2Click(Sender: TObject);
    procedure Tv1MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure MLOrderNoKeyPress(Sender: TObject; var Key: Char);
    procedure gangNOKeyPress(Sender: TObject; var Key: Char);
    procedure MLConNoKeyPress(Sender: TObject; var Key: Char);
    procedure TCBNORChange(Sender: TObject);
    procedure MLColorKeyPress(Sender: TObject; var Key: Char);
    procedure BegDateChange(Sender: TObject);
    procedure EndDateChange(Sender: TObject);
    procedure JYTypeChange(Sender: TObject);
    procedure KHNameHZChange(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
  private
    DQdate: TDateTime;
    FLeft, FTop: Integer;
    CurrentPage, RecordsNumber: Integer;
    canshu1: string;
    procedure InitGrid();
    procedure InitForm();
    { Private declarations }
  public
    FFInt: Integer;
    { Public declarations }
  end;

var
  frmOrderList: TfrmOrderList;

implementation

uses
  U_DataLink, U_Fun, U_ZDYHelp;

{$R *.dfm}


procedure TfrmOrderList.FormDestroy(Sender: TObject);
begin
  frmOrderList := nil;
end;

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

procedure TfrmOrderList.FormCreate(Sender: TObject);
begin
  cxgrid1.Align := alClient;
  DQdate := SGetServerDate(ADOQueryTemp);
  canshu1 := Trim(DParameters1);
end;

procedure TfrmOrderList.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('订单信息查询', Tv1, '订单管理');
end;

procedure TfrmOrderList.InitGrid();
var
  fwhere, Pwhere: string;
begin
  Pwhere := SGetFilters(Panel1, 1, 2);
  {if canshu1 <> '高权限' then
  begin
    if Pwhere <> '' then
      Pwhere := Pwhere + ' and filler=''' + trim(DName) + ''''
    else
      Pwhere := '  filler=''' + trim(DName) + '''';
  end; }

  fwhere := ' where OrdDate>' + quotedstr(FormatDateTime('yyyy-MM-dd', BegDate.DateTime));
  fwhere := fwhere + ' and OrdDate<=' + quotedstr(FormatDateTime('yyyy-MM-dd', enddate.DateTime + 1));
  if trim(Pwhere) <> '' then
    fwhere := fwhere + ' and ' + trim(Pwhere);

  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.Add('SELECT AA.* FROM(');
      sql.add('select A.*,KM.KHNameJC,KHNameHZ=isnull(A.KHNO,'''')+isnull(KM.KHName,'''')+isnull(KM.KHNameJC,'''')');
      sql.Add('+dbo.getpinyin(isnull(KM.KHName,''''))+dbo.getpinyin(isnull(KM.KHNameJC,''''))');
      SQL.Add(',ZQty=(select Sum(B.Qty) from ML_OrderSubidNew B where B.NewMLID=A.NewMLID) ');

      sql.Add(',KHConNo=(select TOP 1 X.KHConNo from ML_ConMain X where X.MLConID=A.ConMainId)');
      sql.Add(' from ML_OrderMainNew A ');
      sql.Add(' left join KH_Main KM on KM.KHNO=A.KHNO ');
      sql.Add(')AA');
      sql.Add(fwhere);
     // ShowMessage(SQL.Text);
      Open;
    end;
    SCreateCDS20(ADOQueryMain, Order_Main);
    SInitCDSData20(ADOQueryMain, Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;
procedure TfrmOrderList.InitForm();
var
  fsj: string;
begin
  ReadCxGrid('订单信息查询', Tv1, '订单管理');
  BegDate.DateTime := SGetServerDate10(ADOQueryTemp) - 1;
  EndDate.DateTime := SGetServerDate10(ADOQueryTemp);
end;

procedure TfrmOrderList.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    Exit;
  SelExportData(Tv1, ADOQueryMain, '指示单列表');
end;

procedure TfrmOrderList.TBRafreshClick(Sender: TObject);
begin
  toolbar1.SetFocus;
  InitGrid();
end;

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

procedure TfrmOrderList.Panel10MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
begin
  ReleaseCapture;
  TWinControl(Panel4).Perform(WM_SYSCOMMAND, $F012, 0);
end;

procedure TfrmOrderList.Image2Click(Sender: TObject);
begin
  Panel4.Visible := False;
end;

procedure TfrmOrderList.Tv1MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  FLeft := X;
  FTop := Y;
end;

procedure TfrmOrderList.Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
  Panel4.Left := FLeft;
  Panel4.Top := FTop + 110;
  Panel4.Visible := True;
  Panel4.Refresh;
  Panel10.Caption := Trim(TV1.Controller.FocusedColumn.Caption);
  RichEdit1.Text := Order_Main.fieldbyname(TV1.Controller.FocusedColumn.DataBinding.FilterFieldName).AsString;
end;


procedure TfrmOrderList.MLOrderNoKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    InitGrid();
  end;
end;


procedure TfrmOrderList.gangNOKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    InitGrid();
  end;
end;

procedure TfrmOrderList.MLConNoKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    InitGrid();
  end;
end;

procedure TfrmOrderList.TCBNORChange(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmOrderList.MLColorKeyPress(Sender: TObject; var Key: Char);
begin
  InitGrid();
end;

procedure TfrmOrderList.BegDateChange(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmOrderList.EndDateChange(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmOrderList.JYTypeChange(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmOrderList.KHNameHZChange(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 TfrmOrderList.ToolButton1Click(Sender: TObject);
var
  fPrintFile: string;
  Porderno, LBName, SYRName: string;
  i, j: Integer;
  Txt, fImagePath: string;
  Moudle: THandle;
  Makebar: TMakebar;
  Mixtext: TMixtext;
begin
  if Order_Main.IsEmpty then
    Exit;
  with ADOQueryPrint do
  begin
    Close;
    sql.Clear;
    sql.add('select A.*,B.*');
    sql.add(',KHNameJC1=(select KHNameJC  from KH_Main X where X.KHNo= A.KHNo and X.KHFlag=''KH'')');
    sql.Add(',KHConNo=(select TOP 1 X.KHConNo from ML_ConMain X where X.MLConID=A.ConMainId)');
    sql.add(',EWM=cast('''' as varchar(1000)) from ML_OrderMainNew A ');

    sql.Add('inner Join ML_OrderSubidNew B on A.NewMLID=B.NewMLID ');
    sql.Add('where A.NewMLID=' + quotedstr(Trim(Order_Main.fieldbyname('NewMLID').AsString)));
    Open;
  end;


  try
    Moudle := LoadLibrary('MakeQRBarcode.dll');
    @Makebar := GetProcAddress(Moudle, 'Make');
    @Mixtext := GetProcAddress(Moudle, 'MixText');
    Txt := Trim(ADOQueryPrint.fieldbyname('NewMLID').AsString);
    fImagePath := ExtractFilePath(Application.ExeName) + 'image\temp' + inttostr(i) + '.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\生产指示单通用.rmf';
  if FileExists(fPrintFile) then
  begin
    RMVariables['QRBARCODE'] := fImagePath;
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找' + ExtractFilePath(Application.ExeName) + 'Report\' + Trim(LBName) + '.rmf'), '提示', 0);
  end;
end;


end.


